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