Linux-2.6.12-rc2
[linux-2.6] / drivers / input / joystick / iforce / iforce-packets.c
1 /*
2  * $Id: iforce-packets.c,v 1.16 2002/07/07 10:22:50 jdeneux Exp $
3  *
4  *  Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz>
5  *  Copyright (c) 2001-2002 Johann Deneux <deneux@ifrance.com>
6  *
7  *  USB/RS232 I-Force joysticks and wheels.
8  */
9
10 /*
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24  *
25  * Should you need to contact me, the author, you can do so either by
26  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
27  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
28  */
29
30 #include "iforce.h"
31
32 static struct {
33         __s32 x;
34         __s32 y;
35 } iforce_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
36
37
38 void iforce_dump_packet(char *msg, u16 cmd, unsigned char *data)
39 {
40         int i;
41
42         printk(KERN_DEBUG "iforce.c: %s ( cmd = %04x, data = ", msg, cmd);
43         for (i = 0; i < LO(cmd); i++)
44                 printk("%02x ", data[i]);
45         printk(")\n");
46 }
47
48 /*
49  * Send a packet of bytes to the device
50  */
51 int iforce_send_packet(struct iforce *iforce, u16 cmd, unsigned char* data)
52 {
53         /* Copy data to buffer */
54         int n = LO(cmd);
55         int c;
56         int empty;
57         int head, tail;
58         unsigned long flags;
59
60 /*
61  * Update head and tail of xmit buffer
62  */
63         spin_lock_irqsave(&iforce->xmit_lock, flags);
64
65         head = iforce->xmit.head;
66         tail = iforce->xmit.tail;
67
68         if (CIRC_SPACE(head, tail, XMIT_SIZE) < n+2) {
69                 printk(KERN_WARNING "iforce.c: not enough space in xmit buffer to send new packet\n");
70                 spin_unlock_irqrestore(&iforce->xmit_lock, flags);
71                 return -1;
72         }
73
74         empty = head == tail;
75         XMIT_INC(iforce->xmit.head, n+2);
76
77 /*
78  * Store packet in xmit buffer
79  */
80         iforce->xmit.buf[head] = HI(cmd);
81         XMIT_INC(head, 1);
82         iforce->xmit.buf[head] = LO(cmd);
83         XMIT_INC(head, 1);
84
85         c = CIRC_SPACE_TO_END(head, tail, XMIT_SIZE);
86         if (n < c) c=n;
87
88         memcpy(&iforce->xmit.buf[head],
89                data,
90                c);
91         if (n != c) {
92                 memcpy(&iforce->xmit.buf[0],
93                        data + c,
94                        n - c);
95         }
96         XMIT_INC(head, n);
97
98         spin_unlock_irqrestore(&iforce->xmit_lock, flags);
99 /*
100  * If necessary, start the transmission
101  */
102         switch (iforce->bus) {
103
104 #ifdef CONFIG_JOYSTICK_IFORCE_232
105                 case IFORCE_232:
106                 if (empty)
107                         iforce_serial_xmit(iforce);
108                 break;
109 #endif
110 #ifdef CONFIG_JOYSTICK_IFORCE_USB
111                 case IFORCE_USB:
112
113                 if (iforce->usbdev && empty &&
114                         !test_and_set_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags)) {
115
116                         iforce_usb_xmit(iforce);
117                 }
118                 break;
119 #endif
120         }
121         return 0;
122 }
123
124 /* Start or stop an effect */
125 int iforce_control_playback(struct iforce* iforce, u16 id, unsigned int value)
126 {
127         unsigned char data[3];
128
129 printk(KERN_DEBUG "iforce-packets.c: control_playback %d %d\n", id, value);
130
131         data[0] = LO(id);
132         data[1] = (value > 0) ? ((value > 1) ? 0x41 : 0x01) : 0;
133         data[2] = LO(value);
134         return iforce_send_packet(iforce, FF_CMD_PLAY, data);
135 }
136
137 /* Mark an effect that was being updated as ready. That means it can be updated
138  * again */
139 static int mark_core_as_ready(struct iforce *iforce, unsigned short addr)
140 {
141         int i;
142         for (i=0; i<iforce->dev.ff_effects_max; ++i) {
143                 if (test_bit(FF_CORE_IS_USED, iforce->core_effects[i].flags) &&
144                     (iforce->core_effects[i].mod1_chunk.start == addr ||
145                      iforce->core_effects[i].mod2_chunk.start == addr)) {
146                         clear_bit(FF_CORE_UPDATE, iforce->core_effects[i].flags);
147                         return 0;
148                 }
149         }
150         printk(KERN_WARNING "iforce-packets.c: unused effect %04x updated !!!\n", addr);
151         return -1;
152 }
153
154 void iforce_process_packet(struct iforce *iforce, u16 cmd, unsigned char *data, struct pt_regs *regs)
155 {
156         struct input_dev *dev = &iforce->dev;
157         int i;
158         static int being_used = 0;
159
160         if (being_used)
161                 printk(KERN_WARNING "iforce-packets.c: re-entrant call to iforce_process %d\n", being_used);
162         being_used++;
163
164 #ifdef CONFIG_JOYSTICK_IFORCE_232
165         if (HI(iforce->expect_packet) == HI(cmd)) {
166                 iforce->expect_packet = 0;
167                 iforce->ecmd = cmd;
168                 memcpy(iforce->edata, data, IFORCE_MAX_LENGTH);
169                 wake_up(&iforce->wait);
170         }
171 #endif
172
173         if (!iforce->type) {
174                 being_used--;
175                 return;
176         }
177
178         switch (HI(cmd)) {
179
180                 case 0x01:      /* joystick position data */
181                 case 0x03:      /* wheel position data */
182
183                         input_regs(dev, regs);
184
185                         if (HI(cmd) == 1) {
186                                 input_report_abs(dev, ABS_X, (__s16) (((__s16)data[1] << 8) | data[0]));
187                                 input_report_abs(dev, ABS_Y, (__s16) (((__s16)data[3] << 8) | data[2]));
188                                 input_report_abs(dev, ABS_THROTTLE, 255 - data[4]);
189                                 if (LO(cmd) >= 8 && test_bit(ABS_RUDDER ,dev->absbit))
190                                         input_report_abs(dev, ABS_RUDDER, (__s8)data[7]);
191                         } else {
192                                 input_report_abs(dev, ABS_WHEEL, (__s16) (((__s16)data[1] << 8) | data[0]));
193                                 input_report_abs(dev, ABS_GAS,   255 - data[2]);
194                                 input_report_abs(dev, ABS_BRAKE, 255 - data[3]);
195                         }
196
197                         input_report_abs(dev, ABS_HAT0X, iforce_hat_to_axis[data[6] >> 4].x);
198                         input_report_abs(dev, ABS_HAT0Y, iforce_hat_to_axis[data[6] >> 4].y);
199
200                         for (i = 0; iforce->type->btn[i] >= 0; i++)
201                                 input_report_key(dev, iforce->type->btn[i], data[(i >> 3) + 5] & (1 << (i & 7)));
202
203                         /* If there are untouched bits left, interpret them as the second hat */
204                         if (i <= 8) {
205                                 int btns = data[6];
206                                 if (test_bit(ABS_HAT1X, dev->absbit)) {
207                                         if (btns & 8) input_report_abs(dev, ABS_HAT1X, -1);
208                                         else if (btns & 2) input_report_abs(dev, ABS_HAT1X, 1);
209                                         else input_report_abs(dev, ABS_HAT1X, 0);
210                                 }
211                                 if (test_bit(ABS_HAT1Y, dev->absbit)) {
212                                         if (btns & 1) input_report_abs(dev, ABS_HAT1Y, -1);
213                                         else if (btns & 4) input_report_abs(dev, ABS_HAT1Y, 1);
214                                         else input_report_abs(dev, ABS_HAT1Y, 0);
215                                 }
216                         }
217
218                         input_sync(dev);
219
220                         break;
221
222                 case 0x02:      /* status report */
223                         input_regs(dev, regs);
224                         input_report_key(dev, BTN_DEAD, data[0] & 0x02);
225                         input_sync(dev);
226
227                         /* Check if an effect was just started or stopped */
228                         i = data[1] & 0x7f;
229                         if (data[1] & 0x80) {
230                                 if (!test_and_set_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
231                                 /* Report play event */
232                                 input_report_ff_status(dev, i, FF_STATUS_PLAYING);
233                                 }
234                         }
235                         else if (test_and_clear_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
236                                 /* Report stop event */
237                                 input_report_ff_status(dev, i, FF_STATUS_STOPPED);
238                         }
239                         if (LO(cmd) > 3) {
240                                 int j;
241                                 for (j=3; j<LO(cmd); j+=2) {
242                                         mark_core_as_ready(iforce, data[j] | (data[j+1]<<8));
243                                 }
244                         }
245                         break;
246         }
247         being_used--;
248 }
249
250 int iforce_get_id_packet(struct iforce *iforce, char *packet)
251 {
252         DECLARE_WAITQUEUE(wait, current);
253         int timeout = HZ; /* 1 second */
254
255         switch (iforce->bus) {
256
257         case IFORCE_USB:
258
259 #ifdef CONFIG_JOYSTICK_IFORCE_USB
260                 iforce->cr.bRequest = packet[0];
261                 iforce->ctrl->dev = iforce->usbdev;
262
263                 set_current_state(TASK_INTERRUPTIBLE);
264                 add_wait_queue(&iforce->wait, &wait);
265
266                 if (usb_submit_urb(iforce->ctrl, GFP_ATOMIC)) {
267                         set_current_state(TASK_RUNNING);
268                         remove_wait_queue(&iforce->wait, &wait);
269                         return -1;
270                 }
271
272                 while (timeout && iforce->ctrl->status == -EINPROGRESS)
273                         timeout = schedule_timeout(timeout);
274
275                 set_current_state(TASK_RUNNING);
276                 remove_wait_queue(&iforce->wait, &wait);
277
278                 if (!timeout) {
279                         usb_unlink_urb(iforce->ctrl);
280                         return -1;
281                 }
282 #else
283                 printk(KERN_ERR "iforce_get_id_packet: iforce->bus = USB!\n");
284 #endif
285                 break;
286
287         case IFORCE_232:
288
289 #ifdef CONFIG_JOYSTICK_IFORCE_232
290                 iforce->expect_packet = FF_CMD_QUERY;
291                 iforce_send_packet(iforce, FF_CMD_QUERY, packet);
292
293                 set_current_state(TASK_INTERRUPTIBLE);
294                 add_wait_queue(&iforce->wait, &wait);
295
296                 while (timeout && iforce->expect_packet)
297                         timeout = schedule_timeout(timeout);
298
299                 set_current_state(TASK_RUNNING);
300                 remove_wait_queue(&iforce->wait, &wait);
301
302                 if (!timeout) {
303                         iforce->expect_packet = 0;
304                         return -1;
305                 }
306 #else
307                 printk(KERN_ERR "iforce_get_id_packet: iforce->bus = SERIO!\n");
308 #endif
309                 break;
310
311         default:
312                 printk(KERN_ERR "iforce_get_id_packet: iforce->bus = %d\n",
313                        iforce->bus);
314                 break;
315         }
316
317         return -(iforce->edata[0] != packet[0]);
318 }
319