Merge branch 'next' into for-linus
[platform/kernel/linux-rpi.git] / drivers / input / joystick / iforce / iforce-packets.c
1 /*
2  *  Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz>
3  *  Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com>
4  *
5  *  USB/RS232 I-Force joysticks and wheels.
6  */
7
8 /*
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23
24 #include <asm/unaligned.h>
25 #include "iforce.h"
26
27 static struct {
28         __s32 x;
29         __s32 y;
30 } iforce_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
31
32
33 void iforce_dump_packet(struct iforce *iforce, char *msg, u16 cmd, unsigned char *data)
34 {
35         dev_dbg(iforce->dev->dev.parent, "%s %s cmd = %04x, data = %*ph\n",
36                 __func__, msg, cmd, LO(cmd), data);
37 }
38
39 /*
40  * Send a packet of bytes to the device
41  */
42 int iforce_send_packet(struct iforce *iforce, u16 cmd, unsigned char* data)
43 {
44         /* Copy data to buffer */
45         int n = LO(cmd);
46         int c;
47         int empty;
48         int head, tail;
49         unsigned long flags;
50
51 /*
52  * Update head and tail of xmit buffer
53  */
54         spin_lock_irqsave(&iforce->xmit_lock, flags);
55
56         head = iforce->xmit.head;
57         tail = iforce->xmit.tail;
58
59
60         if (CIRC_SPACE(head, tail, XMIT_SIZE) < n+2) {
61                 dev_warn(&iforce->dev->dev,
62                          "not enough space in xmit buffer to send new packet\n");
63                 spin_unlock_irqrestore(&iforce->xmit_lock, flags);
64                 return -1;
65         }
66
67         empty = head == tail;
68         XMIT_INC(iforce->xmit.head, n+2);
69
70 /*
71  * Store packet in xmit buffer
72  */
73         iforce->xmit.buf[head] = HI(cmd);
74         XMIT_INC(head, 1);
75         iforce->xmit.buf[head] = LO(cmd);
76         XMIT_INC(head, 1);
77
78         c = CIRC_SPACE_TO_END(head, tail, XMIT_SIZE);
79         if (n < c) c=n;
80
81         memcpy(&iforce->xmit.buf[head],
82                data,
83                c);
84         if (n != c) {
85                 memcpy(&iforce->xmit.buf[0],
86                        data + c,
87                        n - c);
88         }
89         XMIT_INC(head, n);
90
91         spin_unlock_irqrestore(&iforce->xmit_lock, flags);
92 /*
93  * If necessary, start the transmission
94  */
95         if (empty)
96                 iforce->xport_ops->xmit(iforce);
97
98         return 0;
99 }
100 EXPORT_SYMBOL(iforce_send_packet);
101
102 /* Start or stop an effect */
103 int iforce_control_playback(struct iforce* iforce, u16 id, unsigned int value)
104 {
105         unsigned char data[3];
106
107         data[0] = LO(id);
108         data[1] = (value > 0) ? ((value > 1) ? 0x41 : 0x01) : 0;
109         data[2] = LO(value);
110         return iforce_send_packet(iforce, FF_CMD_PLAY, data);
111 }
112
113 /* Mark an effect that was being updated as ready. That means it can be updated
114  * again */
115 static int mark_core_as_ready(struct iforce *iforce, unsigned short addr)
116 {
117         int i;
118
119         if (!iforce->dev->ff)
120                 return 0;
121
122         for (i = 0; i < iforce->dev->ff->max_effects; ++i) {
123                 if (test_bit(FF_CORE_IS_USED, iforce->core_effects[i].flags) &&
124                     (iforce->core_effects[i].mod1_chunk.start == addr ||
125                      iforce->core_effects[i].mod2_chunk.start == addr)) {
126                         clear_bit(FF_CORE_UPDATE, iforce->core_effects[i].flags);
127                         return 0;
128                 }
129         }
130         dev_warn(&iforce->dev->dev, "unused effect %04x updated !!!\n", addr);
131         return -1;
132 }
133
134 static void iforce_report_hats_buttons(struct iforce *iforce, u8 *data)
135 {
136         struct input_dev *dev = iforce->dev;
137         int i;
138
139         input_report_abs(dev, ABS_HAT0X, iforce_hat_to_axis[data[6] >> 4].x);
140         input_report_abs(dev, ABS_HAT0Y, iforce_hat_to_axis[data[6] >> 4].y);
141
142         for (i = 0; iforce->type->btn[i] >= 0; i++)
143                 input_report_key(dev, iforce->type->btn[i],
144                                  data[(i >> 3) + 5] & (1 << (i & 7)));
145
146         /* If there are untouched bits left, interpret them as the second hat */
147         if (i <= 8) {
148                 u8 btns = data[6];
149
150                 if (test_bit(ABS_HAT1X, dev->absbit)) {
151                         if (btns & BIT(3))
152                                 input_report_abs(dev, ABS_HAT1X, -1);
153                         else if (btns & BIT(1))
154                                 input_report_abs(dev, ABS_HAT1X, 1);
155                         else
156                                 input_report_abs(dev, ABS_HAT1X, 0);
157                 }
158
159                 if (test_bit(ABS_HAT1Y, dev->absbit)) {
160                         if (btns & BIT(0))
161                                 input_report_abs(dev, ABS_HAT1Y, -1);
162                         else if (btns & BIT(2))
163                                 input_report_abs(dev, ABS_HAT1Y, 1);
164                         else
165                                 input_report_abs(dev, ABS_HAT1Y, 0);
166                 }
167         }
168 }
169
170 void iforce_process_packet(struct iforce *iforce,
171                            u8 packet_id, u8 *data, size_t len)
172 {
173         struct input_dev *dev = iforce->dev;
174         int i, j;
175
176         switch (packet_id) {
177
178         case 0x01:      /* joystick position data */
179                 input_report_abs(dev, ABS_X,
180                                  (__s16) get_unaligned_le16(data));
181                 input_report_abs(dev, ABS_Y,
182                                  (__s16) get_unaligned_le16(data + 2));
183                 input_report_abs(dev, ABS_THROTTLE, 255 - data[4]);
184
185                 if (len >= 8 && test_bit(ABS_RUDDER ,dev->absbit))
186                         input_report_abs(dev, ABS_RUDDER, (__s8)data[7]);
187
188                 iforce_report_hats_buttons(iforce, data);
189
190                 input_sync(dev);
191                 break;
192
193         case 0x03:      /* wheel position data */
194                 input_report_abs(dev, ABS_WHEEL,
195                                  (__s16) get_unaligned_le16(data));
196                 input_report_abs(dev, ABS_GAS,   255 - data[2]);
197                 input_report_abs(dev, ABS_BRAKE, 255 - data[3]);
198
199                 iforce_report_hats_buttons(iforce, data);
200
201                 input_sync(dev);
202                 break;
203
204         case 0x02:      /* status report */
205                 input_report_key(dev, BTN_DEAD, data[0] & 0x02);
206                 input_sync(dev);
207
208                 /* Check if an effect was just started or stopped */
209                 i = data[1] & 0x7f;
210                 if (data[1] & 0x80) {
211                         if (!test_and_set_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
212                                 /* Report play event */
213                                 input_report_ff_status(dev, i, FF_STATUS_PLAYING);
214                         }
215                 } else if (test_and_clear_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
216                         /* Report stop event */
217                         input_report_ff_status(dev, i, FF_STATUS_STOPPED);
218                 }
219
220                 for (j = 3; j < len; j += 2)
221                         mark_core_as_ready(iforce, get_unaligned_le16(data + j));
222
223                 break;
224         }
225 }
226 EXPORT_SYMBOL(iforce_process_packet);