Merge branch 'release-2.6.27' of git://git.kernel.org/pub/scm/linux/kernel/git/ak...
[pandora-kernel.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  * Should you need to contact me, the author, you can do so either by
24  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
25  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
26  */
27
28 #include "iforce.h"
29
30 static struct {
31         __s32 x;
32         __s32 y;
33 } iforce_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
34
35
36 void iforce_dump_packet(char *msg, u16 cmd, unsigned char *data)
37 {
38         int i;
39
40         printk(KERN_DEBUG __FILE__ ": %s cmd = %04x, data = ", msg, cmd);
41         for (i = 0; i < LO(cmd); i++)
42                 printk("%02x ", data[i]);
43         printk("\n");
44 }
45
46 /*
47  * Send a packet of bytes to the device
48  */
49 int iforce_send_packet(struct iforce *iforce, u16 cmd, unsigned char* data)
50 {
51         /* Copy data to buffer */
52         int n = LO(cmd);
53         int c;
54         int empty;
55         int head, tail;
56         unsigned long flags;
57
58 /*
59  * Update head and tail of xmit buffer
60  */
61         spin_lock_irqsave(&iforce->xmit_lock, flags);
62
63         head = iforce->xmit.head;
64         tail = iforce->xmit.tail;
65
66
67         if (CIRC_SPACE(head, tail, XMIT_SIZE) < n+2) {
68                 warn("not enough space in xmit buffer to send new packet");
69                 spin_unlock_irqrestore(&iforce->xmit_lock, flags);
70                 return -1;
71         }
72
73         empty = head == tail;
74         XMIT_INC(iforce->xmit.head, n+2);
75
76 /*
77  * Store packet in xmit buffer
78  */
79         iforce->xmit.buf[head] = HI(cmd);
80         XMIT_INC(head, 1);
81         iforce->xmit.buf[head] = LO(cmd);
82         XMIT_INC(head, 1);
83
84         c = CIRC_SPACE_TO_END(head, tail, XMIT_SIZE);
85         if (n < c) c=n;
86
87         memcpy(&iforce->xmit.buf[head],
88                data,
89                c);
90         if (n != c) {
91                 memcpy(&iforce->xmit.buf[0],
92                        data + c,
93                        n - c);
94         }
95         XMIT_INC(head, n);
96
97         spin_unlock_irqrestore(&iforce->xmit_lock, flags);
98 /*
99  * If necessary, start the transmission
100  */
101         switch (iforce->bus) {
102
103 #ifdef CONFIG_JOYSTICK_IFORCE_232
104                 case IFORCE_232:
105                 if (empty)
106                         iforce_serial_xmit(iforce);
107                 break;
108 #endif
109 #ifdef CONFIG_JOYSTICK_IFORCE_USB
110                 case IFORCE_USB:
111
112                 if (iforce->usbdev && empty &&
113                         !test_and_set_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags)) {
114
115                         iforce_usb_xmit(iforce);
116                 }
117                 break;
118 #endif
119         }
120         return 0;
121 }
122
123 /* Start or stop an effect */
124 int iforce_control_playback(struct iforce* iforce, u16 id, unsigned int value)
125 {
126         unsigned char data[3];
127
128         data[0] = LO(id);
129         data[1] = (value > 0) ? ((value > 1) ? 0x41 : 0x01) : 0;
130         data[2] = LO(value);
131         return iforce_send_packet(iforce, FF_CMD_PLAY, data);
132 }
133
134 /* Mark an effect that was being updated as ready. That means it can be updated
135  * again */
136 static int mark_core_as_ready(struct iforce *iforce, unsigned short addr)
137 {
138         int i;
139
140         if (!iforce->dev->ff)
141                 return 0;
142
143         for (i = 0; i < iforce->dev->ff->max_effects; ++i) {
144                 if (test_bit(FF_CORE_IS_USED, iforce->core_effects[i].flags) &&
145                     (iforce->core_effects[i].mod1_chunk.start == addr ||
146                      iforce->core_effects[i].mod2_chunk.start == addr)) {
147                         clear_bit(FF_CORE_UPDATE, iforce->core_effects[i].flags);
148                         return 0;
149                 }
150         }
151         warn("unused effect %04x updated !!!", addr);
152         return -1;
153 }
154
155 void iforce_process_packet(struct iforce *iforce, u16 cmd, unsigned char *data)
156 {
157         struct input_dev *dev = iforce->dev;
158         int i;
159         static int being_used = 0;
160
161         if (being_used)
162                 warn("re-entrant call to iforce_process %d", being_used);
163         being_used++;
164
165 #ifdef CONFIG_JOYSTICK_IFORCE_232
166         if (HI(iforce->expect_packet) == HI(cmd)) {
167                 iforce->expect_packet = 0;
168                 iforce->ecmd = cmd;
169                 memcpy(iforce->edata, data, IFORCE_MAX_LENGTH);
170         }
171 #endif
172         wake_up(&iforce->wait);
173
174         if (!iforce->type) {
175                 being_used--;
176                 return;
177         }
178
179         switch (HI(cmd)) {
180
181                 case 0x01:      /* joystick position data */
182                 case 0x03:      /* wheel position data */
183                         if (HI(cmd) == 1) {
184                                 input_report_abs(dev, ABS_X, (__s16) (((__s16)data[1] << 8) | data[0]));
185                                 input_report_abs(dev, ABS_Y, (__s16) (((__s16)data[3] << 8) | data[2]));
186                                 input_report_abs(dev, ABS_THROTTLE, 255 - data[4]);
187                                 if (LO(cmd) >= 8 && test_bit(ABS_RUDDER ,dev->absbit))
188                                         input_report_abs(dev, ABS_RUDDER, (__s8)data[7]);
189                         } else {
190                                 input_report_abs(dev, ABS_WHEEL, (__s16) (((__s16)data[1] << 8) | data[0]));
191                                 input_report_abs(dev, ABS_GAS,   255 - data[2]);
192                                 input_report_abs(dev, ABS_BRAKE, 255 - data[3]);
193                         }
194
195                         input_report_abs(dev, ABS_HAT0X, iforce_hat_to_axis[data[6] >> 4].x);
196                         input_report_abs(dev, ABS_HAT0Y, iforce_hat_to_axis[data[6] >> 4].y);
197
198                         for (i = 0; iforce->type->btn[i] >= 0; i++)
199                                 input_report_key(dev, iforce->type->btn[i], data[(i >> 3) + 5] & (1 << (i & 7)));
200
201                         /* If there are untouched bits left, interpret them as the second hat */
202                         if (i <= 8) {
203                                 int btns = data[6];
204                                 if (test_bit(ABS_HAT1X, dev->absbit)) {
205                                         if (btns & 8) input_report_abs(dev, ABS_HAT1X, -1);
206                                         else if (btns & 2) input_report_abs(dev, ABS_HAT1X, 1);
207                                         else input_report_abs(dev, ABS_HAT1X, 0);
208                                 }
209                                 if (test_bit(ABS_HAT1Y, dev->absbit)) {
210                                         if (btns & 1) input_report_abs(dev, ABS_HAT1Y, -1);
211                                         else if (btns & 4) input_report_abs(dev, ABS_HAT1Y, 1);
212                                         else input_report_abs(dev, ABS_HAT1Y, 0);
213                                 }
214                         }
215
216                         input_sync(dev);
217
218                         break;
219
220                 case 0x02:      /* status report */
221                         input_report_key(dev, BTN_DEAD, data[0] & 0x02);
222                         input_sync(dev);
223
224                         /* Check if an effect was just started or stopped */
225                         i = data[1] & 0x7f;
226                         if (data[1] & 0x80) {
227                                 if (!test_and_set_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
228                                         /* Report play event */
229                                         input_report_ff_status(dev, i, FF_STATUS_PLAYING);
230                                 }
231                         } else if (test_and_clear_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
232                                 /* Report stop event */
233                                 input_report_ff_status(dev, i, FF_STATUS_STOPPED);
234                         }
235                         if (LO(cmd) > 3) {
236                                 int j;
237                                 for (j = 3; j < LO(cmd); j += 2)
238                                         mark_core_as_ready(iforce, data[j] | (data[j+1]<<8));
239                         }
240                         break;
241         }
242         being_used--;
243 }
244
245 int iforce_get_id_packet(struct iforce *iforce, char *packet)
246 {
247         switch (iforce->bus) {
248
249         case IFORCE_USB: {
250 #ifdef CONFIG_JOYSTICK_IFORCE_USB
251                 int status;
252
253                 iforce->cr.bRequest = packet[0];
254                 iforce->ctrl->dev = iforce->usbdev;
255
256                 status = usb_submit_urb(iforce->ctrl, GFP_ATOMIC);
257                 if (status) {
258                         err("usb_submit_urb failed %d", status);
259                         return -1;
260                 }
261
262                 wait_event_interruptible_timeout(iforce->wait,
263                         iforce->ctrl->status != -EINPROGRESS, HZ);
264
265                 if (iforce->ctrl->status) {
266                         dbg("iforce->ctrl->status = %d", iforce->ctrl->status);
267                         usb_unlink_urb(iforce->ctrl);
268                         return -1;
269                 }
270 #else
271                 dbg("iforce_get_id_packet: iforce->bus = USB!");
272 #endif
273                 }
274                 break;
275
276         case IFORCE_232:
277
278 #ifdef CONFIG_JOYSTICK_IFORCE_232
279                 iforce->expect_packet = FF_CMD_QUERY;
280                 iforce_send_packet(iforce, FF_CMD_QUERY, packet);
281
282                 wait_event_interruptible_timeout(iforce->wait,
283                         !iforce->expect_packet, HZ);
284
285                 if (iforce->expect_packet) {
286                         iforce->expect_packet = 0;
287                         return -1;
288                 }
289 #else
290                 err("iforce_get_id_packet: iforce->bus = SERIO!");
291 #endif
292                 break;
293
294         default:
295                 err("iforce_get_id_packet: iforce->bus = %d", iforce->bus);
296                 break;
297         }
298
299         return -(iforce->edata[0] != packet[0]);
300 }
301