blob: 015b50aa76fcf5f7fe752b2f63cf857bdf9c9a26 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz>
Johann Deneux598972d2007-04-12 01:30:24 -04003 * Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
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
30static 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
36void iforce_dump_packet(char *msg, u16 cmd, unsigned char *data)
37{
38 int i;
39
Johann Deneux598972d2007-04-12 01:30:24 -040040 printk(KERN_DEBUG __FILE__ ": %s cmd = %04x, data = ", msg, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 for (i = 0; i < LO(cmd); i++)
42 printk("%02x ", data[i]);
Johann Deneux598972d2007-04-12 01:30:24 -040043 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070044}
45
46/*
47 * Send a packet of bytes to the device
48 */
49int 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
Johann Deneux598972d2007-04-12 01:30:24 -040066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 if (CIRC_SPACE(head, tail, XMIT_SIZE) < n+2) {
Johann Deneux598972d2007-04-12 01:30:24 -040068 warn("not enough space in xmit buffer to send new packet");
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 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 */
124int iforce_control_playback(struct iforce* iforce, u16 id, unsigned int value)
125{
126 unsigned char data[3];
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 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 */
136static int mark_core_as_ready(struct iforce *iforce, unsigned short addr)
137{
138 int i;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500139
Anssi Hannulaf6a01c82006-07-19 01:40:39 -0400140 if (!iforce->dev->ff)
141 return 0;
142
143 for (i = 0; i < iforce->dev->ff->max_effects; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 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 }
Johann Deneux598972d2007-04-12 01:30:24 -0400151 warn("unused effect %04x updated !!!", addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 return -1;
153}
154
David Howells7d12e782006-10-05 14:55:46 +0100155void iforce_process_packet(struct iforce *iforce, u16 cmd, unsigned char *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500157 struct input_dev *dev = iforce->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 int i;
159 static int being_used = 0;
160
161 if (being_used)
Johann Deneux598972d2007-04-12 01:30:24 -0400162 warn("re-entrant call to iforce_process %d", being_used);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171#endif
Dmitry Torokhov97d4ebf2006-01-31 01:31:07 -0500172 wake_up(&iforce->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 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)) {
Anssi Hannulaf6a01c82006-07-19 01:40:39 -0400228 /* Report play event */
229 input_report_ff_status(dev, i, FF_STATUS_PLAYING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
Anssi Hannulaf6a01c82006-07-19 01:40:39 -0400231 } else if (test_and_clear_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 /* Report stop event */
233 input_report_ff_status(dev, i, FF_STATUS_STOPPED);
234 }
235 if (LO(cmd) > 3) {
236 int j;
Anssi Hannulaf6a01c82006-07-19 01:40:39 -0400237 for (j = 3; j < LO(cmd); j += 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 mark_core_as_ready(iforce, data[j] | (data[j+1]<<8));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240 break;
241 }
242 being_used--;
243}
244
245int iforce_get_id_packet(struct iforce *iforce, char *packet)
246{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 switch (iforce->bus) {
248
Andrew Mortond9f03832007-08-30 00:04:33 -0400249 case IFORCE_USB: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250#ifdef CONFIG_JOYSTICK_IFORCE_USB
Andrew Mortond9f03832007-08-30 00:04:33 -0400251 int status;
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 iforce->cr.bRequest = packet[0];
254 iforce->ctrl->dev = iforce->usbdev;
255
Johann Deneuxc0338c12007-05-14 00:09:33 -0400256 status = usb_submit_urb(iforce->ctrl, GFP_ATOMIC);
257 if (status) {
258 err("usb_submit_urb failed %d", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return -1;
Johann Deneuxc0338c12007-05-14 00:09:33 -0400260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Vojtech Pavlikfb76b092005-09-05 00:12:39 -0500262 wait_event_interruptible_timeout(iforce->wait,
263 iforce->ctrl->status != -EINPROGRESS, HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Dmitry Torokhov97d4ebf2006-01-31 01:31:07 -0500265 if (iforce->ctrl->status) {
Johann Deneuxc0338c12007-05-14 00:09:33 -0400266 dbg("iforce->ctrl->status = %d", iforce->ctrl->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 usb_unlink_urb(iforce->ctrl);
268 return -1;
269 }
270#else
Johann Deneuxc0338c12007-05-14 00:09:33 -0400271 dbg("iforce_get_id_packet: iforce->bus = USB!");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272#endif
Andrew Mortond9f03832007-08-30 00:04:33 -0400273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 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
Vojtech Pavlikfb76b092005-09-05 00:12:39 -0500282 wait_event_interruptible_timeout(iforce->wait,
283 !iforce->expect_packet, HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Vojtech Pavlikfb76b092005-09-05 00:12:39 -0500285 if (iforce->expect_packet) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 iforce->expect_packet = 0;
287 return -1;
288 }
289#else
Johann Deneux598972d2007-04-12 01:30:24 -0400290 err("iforce_get_id_packet: iforce->bus = SERIO!");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291#endif
292 break;
293
294 default:
Johann Deneux598972d2007-04-12 01:30:24 -0400295 err("iforce_get_id_packet: iforce->bus = %d", iforce->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 break;
297 }
298
299 return -(iforce->edata[0] != packet[0]);
300}
301