xref: /linux/drivers/input/joystick/iforce/iforce-packets.c (revision fb76b099f86624d3c629cfab071aa2296f65b7bb)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * $Id: iforce-packets.c,v 1.16 2002/07/07 10:22:50 jdeneux Exp $
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz>
51da177e4SLinus Torvalds  *  Copyright (c) 2001-2002 Johann Deneux <deneux@ifrance.com>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  USB/RS232 I-Force joysticks and wheels.
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds /*
111da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
121da177e4SLinus Torvalds  * it under the terms of the GNU General Public License as published by
131da177e4SLinus Torvalds  * the Free Software Foundation; either version 2 of the License, or
141da177e4SLinus Torvalds  * (at your option) any later version.
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  * This program is distributed in the hope that it will be useful,
171da177e4SLinus Torvalds  * but WITHOUT ANY WARRANTY; without even the implied warranty of
181da177e4SLinus Torvalds  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
191da177e4SLinus Torvalds  * GNU General Public License for more details.
201da177e4SLinus Torvalds  *
211da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public License
221da177e4SLinus Torvalds  * along with this program; if not, write to the Free Software
231da177e4SLinus Torvalds  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  * Should you need to contact me, the author, you can do so either by
261da177e4SLinus Torvalds  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
271da177e4SLinus Torvalds  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
281da177e4SLinus Torvalds  */
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds #include "iforce.h"
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds static struct {
331da177e4SLinus Torvalds 	__s32 x;
341da177e4SLinus Torvalds 	__s32 y;
351da177e4SLinus Torvalds } iforce_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds void iforce_dump_packet(char *msg, u16 cmd, unsigned char *data)
391da177e4SLinus Torvalds {
401da177e4SLinus Torvalds 	int i;
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds 	printk(KERN_DEBUG "iforce.c: %s ( cmd = %04x, data = ", msg, cmd);
431da177e4SLinus Torvalds 	for (i = 0; i < LO(cmd); i++)
441da177e4SLinus Torvalds 		printk("%02x ", data[i]);
451da177e4SLinus Torvalds 	printk(")\n");
461da177e4SLinus Torvalds }
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds /*
491da177e4SLinus Torvalds  * Send a packet of bytes to the device
501da177e4SLinus Torvalds  */
511da177e4SLinus Torvalds int iforce_send_packet(struct iforce *iforce, u16 cmd, unsigned char* data)
521da177e4SLinus Torvalds {
531da177e4SLinus Torvalds 	/* Copy data to buffer */
541da177e4SLinus Torvalds 	int n = LO(cmd);
551da177e4SLinus Torvalds 	int c;
561da177e4SLinus Torvalds 	int empty;
571da177e4SLinus Torvalds 	int head, tail;
581da177e4SLinus Torvalds 	unsigned long flags;
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds /*
611da177e4SLinus Torvalds  * Update head and tail of xmit buffer
621da177e4SLinus Torvalds  */
631da177e4SLinus Torvalds 	spin_lock_irqsave(&iforce->xmit_lock, flags);
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds 	head = iforce->xmit.head;
661da177e4SLinus Torvalds 	tail = iforce->xmit.tail;
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds 	if (CIRC_SPACE(head, tail, XMIT_SIZE) < n+2) {
691da177e4SLinus Torvalds 		printk(KERN_WARNING "iforce.c: not enough space in xmit buffer to send new packet\n");
701da177e4SLinus Torvalds 		spin_unlock_irqrestore(&iforce->xmit_lock, flags);
711da177e4SLinus Torvalds 		return -1;
721da177e4SLinus Torvalds 	}
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds 	empty = head == tail;
751da177e4SLinus Torvalds 	XMIT_INC(iforce->xmit.head, n+2);
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds /*
781da177e4SLinus Torvalds  * Store packet in xmit buffer
791da177e4SLinus Torvalds  */
801da177e4SLinus Torvalds 	iforce->xmit.buf[head] = HI(cmd);
811da177e4SLinus Torvalds 	XMIT_INC(head, 1);
821da177e4SLinus Torvalds 	iforce->xmit.buf[head] = LO(cmd);
831da177e4SLinus Torvalds 	XMIT_INC(head, 1);
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds 	c = CIRC_SPACE_TO_END(head, tail, XMIT_SIZE);
861da177e4SLinus Torvalds 	if (n < c) c=n;
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds 	memcpy(&iforce->xmit.buf[head],
891da177e4SLinus Torvalds 	       data,
901da177e4SLinus Torvalds 	       c);
911da177e4SLinus Torvalds 	if (n != c) {
921da177e4SLinus Torvalds 		memcpy(&iforce->xmit.buf[0],
931da177e4SLinus Torvalds 		       data + c,
941da177e4SLinus Torvalds 		       n - c);
951da177e4SLinus Torvalds 	}
961da177e4SLinus Torvalds 	XMIT_INC(head, n);
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 	spin_unlock_irqrestore(&iforce->xmit_lock, flags);
991da177e4SLinus Torvalds /*
1001da177e4SLinus Torvalds  * If necessary, start the transmission
1011da177e4SLinus Torvalds  */
1021da177e4SLinus Torvalds 	switch (iforce->bus) {
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds #ifdef CONFIG_JOYSTICK_IFORCE_232
1051da177e4SLinus Torvalds 		case IFORCE_232:
1061da177e4SLinus Torvalds 		if (empty)
1071da177e4SLinus Torvalds 			iforce_serial_xmit(iforce);
1081da177e4SLinus Torvalds 		break;
1091da177e4SLinus Torvalds #endif
1101da177e4SLinus Torvalds #ifdef CONFIG_JOYSTICK_IFORCE_USB
1111da177e4SLinus Torvalds 		case IFORCE_USB:
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds 		if (iforce->usbdev && empty &&
1141da177e4SLinus Torvalds 			!test_and_set_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags)) {
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds 			iforce_usb_xmit(iforce);
1171da177e4SLinus Torvalds 		}
1181da177e4SLinus Torvalds 		break;
1191da177e4SLinus Torvalds #endif
1201da177e4SLinus Torvalds 	}
1211da177e4SLinus Torvalds 	return 0;
1221da177e4SLinus Torvalds }
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds /* Start or stop an effect */
1251da177e4SLinus Torvalds int iforce_control_playback(struct iforce* iforce, u16 id, unsigned int value)
1261da177e4SLinus Torvalds {
1271da177e4SLinus Torvalds 	unsigned char data[3];
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds printk(KERN_DEBUG "iforce-packets.c: control_playback %d %d\n", id, value);
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 	data[0] = LO(id);
1321da177e4SLinus Torvalds 	data[1] = (value > 0) ? ((value > 1) ? 0x41 : 0x01) : 0;
1331da177e4SLinus Torvalds 	data[2] = LO(value);
1341da177e4SLinus Torvalds 	return iforce_send_packet(iforce, FF_CMD_PLAY, data);
1351da177e4SLinus Torvalds }
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds /* Mark an effect that was being updated as ready. That means it can be updated
1381da177e4SLinus Torvalds  * again */
1391da177e4SLinus Torvalds static int mark_core_as_ready(struct iforce *iforce, unsigned short addr)
1401da177e4SLinus Torvalds {
1411da177e4SLinus Torvalds 	int i;
1421da177e4SLinus Torvalds 	for (i=0; i<iforce->dev.ff_effects_max; ++i) {
1431da177e4SLinus Torvalds 		if (test_bit(FF_CORE_IS_USED, iforce->core_effects[i].flags) &&
1441da177e4SLinus Torvalds 		    (iforce->core_effects[i].mod1_chunk.start == addr ||
1451da177e4SLinus Torvalds 		     iforce->core_effects[i].mod2_chunk.start == addr)) {
1461da177e4SLinus Torvalds 			clear_bit(FF_CORE_UPDATE, iforce->core_effects[i].flags);
1471da177e4SLinus Torvalds 			return 0;
1481da177e4SLinus Torvalds 		}
1491da177e4SLinus Torvalds 	}
1501da177e4SLinus Torvalds 	printk(KERN_WARNING "iforce-packets.c: unused effect %04x updated !!!\n", addr);
1511da177e4SLinus Torvalds 	return -1;
1521da177e4SLinus Torvalds }
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds void iforce_process_packet(struct iforce *iforce, u16 cmd, unsigned char *data, struct pt_regs *regs)
1551da177e4SLinus Torvalds {
1561da177e4SLinus Torvalds 	struct input_dev *dev = &iforce->dev;
1571da177e4SLinus Torvalds 	int i;
1581da177e4SLinus Torvalds 	static int being_used = 0;
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	if (being_used)
1611da177e4SLinus Torvalds 		printk(KERN_WARNING "iforce-packets.c: re-entrant call to iforce_process %d\n", being_used);
1621da177e4SLinus Torvalds 	being_used++;
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds #ifdef CONFIG_JOYSTICK_IFORCE_232
1651da177e4SLinus Torvalds 	if (HI(iforce->expect_packet) == HI(cmd)) {
1661da177e4SLinus Torvalds 		iforce->expect_packet = 0;
1671da177e4SLinus Torvalds 		iforce->ecmd = cmd;
1681da177e4SLinus Torvalds 		memcpy(iforce->edata, data, IFORCE_MAX_LENGTH);
1691da177e4SLinus Torvalds 		wake_up(&iforce->wait);
1701da177e4SLinus Torvalds 	}
1711da177e4SLinus Torvalds #endif
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 	if (!iforce->type) {
1741da177e4SLinus Torvalds 		being_used--;
1751da177e4SLinus Torvalds 		return;
1761da177e4SLinus Torvalds 	}
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	switch (HI(cmd)) {
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 		case 0x01:	/* joystick position data */
1811da177e4SLinus Torvalds 		case 0x03:	/* wheel position data */
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 			input_regs(dev, regs);
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 			if (HI(cmd) == 1) {
1861da177e4SLinus Torvalds 				input_report_abs(dev, ABS_X, (__s16) (((__s16)data[1] << 8) | data[0]));
1871da177e4SLinus Torvalds 				input_report_abs(dev, ABS_Y, (__s16) (((__s16)data[3] << 8) | data[2]));
1881da177e4SLinus Torvalds 				input_report_abs(dev, ABS_THROTTLE, 255 - data[4]);
1891da177e4SLinus Torvalds 				if (LO(cmd) >= 8 && test_bit(ABS_RUDDER ,dev->absbit))
1901da177e4SLinus Torvalds 					input_report_abs(dev, ABS_RUDDER, (__s8)data[7]);
1911da177e4SLinus Torvalds 			} else {
1921da177e4SLinus Torvalds 				input_report_abs(dev, ABS_WHEEL, (__s16) (((__s16)data[1] << 8) | data[0]));
1931da177e4SLinus Torvalds 				input_report_abs(dev, ABS_GAS,   255 - data[2]);
1941da177e4SLinus Torvalds 				input_report_abs(dev, ABS_BRAKE, 255 - data[3]);
1951da177e4SLinus Torvalds 			}
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 			input_report_abs(dev, ABS_HAT0X, iforce_hat_to_axis[data[6] >> 4].x);
1981da177e4SLinus Torvalds 			input_report_abs(dev, ABS_HAT0Y, iforce_hat_to_axis[data[6] >> 4].y);
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 			for (i = 0; iforce->type->btn[i] >= 0; i++)
2011da177e4SLinus Torvalds 				input_report_key(dev, iforce->type->btn[i], data[(i >> 3) + 5] & (1 << (i & 7)));
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 			/* If there are untouched bits left, interpret them as the second hat */
2041da177e4SLinus Torvalds 			if (i <= 8) {
2051da177e4SLinus Torvalds 				int btns = data[6];
2061da177e4SLinus Torvalds 				if (test_bit(ABS_HAT1X, dev->absbit)) {
2071da177e4SLinus Torvalds 					if (btns & 8) input_report_abs(dev, ABS_HAT1X, -1);
2081da177e4SLinus Torvalds 					else if (btns & 2) input_report_abs(dev, ABS_HAT1X, 1);
2091da177e4SLinus Torvalds 					else input_report_abs(dev, ABS_HAT1X, 0);
2101da177e4SLinus Torvalds 				}
2111da177e4SLinus Torvalds 				if (test_bit(ABS_HAT1Y, dev->absbit)) {
2121da177e4SLinus Torvalds 					if (btns & 1) input_report_abs(dev, ABS_HAT1Y, -1);
2131da177e4SLinus Torvalds 					else if (btns & 4) input_report_abs(dev, ABS_HAT1Y, 1);
2141da177e4SLinus Torvalds 					else input_report_abs(dev, ABS_HAT1Y, 0);
2151da177e4SLinus Torvalds 				}
2161da177e4SLinus Torvalds 			}
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 			input_sync(dev);
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 			break;
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 		case 0x02:	/* status report */
2231da177e4SLinus Torvalds 			input_regs(dev, regs);
2241da177e4SLinus Torvalds 			input_report_key(dev, BTN_DEAD, data[0] & 0x02);
2251da177e4SLinus Torvalds 			input_sync(dev);
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds 			/* Check if an effect was just started or stopped */
2281da177e4SLinus Torvalds 			i = data[1] & 0x7f;
2291da177e4SLinus Torvalds 			if (data[1] & 0x80) {
2301da177e4SLinus Torvalds 				if (!test_and_set_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
2311da177e4SLinus Torvalds 				/* Report play event */
2321da177e4SLinus Torvalds 				input_report_ff_status(dev, i, FF_STATUS_PLAYING);
2331da177e4SLinus Torvalds 				}
2341da177e4SLinus Torvalds 			}
2351da177e4SLinus Torvalds 			else if (test_and_clear_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
2361da177e4SLinus Torvalds 				/* Report stop event */
2371da177e4SLinus Torvalds 				input_report_ff_status(dev, i, FF_STATUS_STOPPED);
2381da177e4SLinus Torvalds 			}
2391da177e4SLinus Torvalds 			if (LO(cmd) > 3) {
2401da177e4SLinus Torvalds 				int j;
2411da177e4SLinus Torvalds 				for (j=3; j<LO(cmd); j+=2) {
2421da177e4SLinus Torvalds 					mark_core_as_ready(iforce, data[j] | (data[j+1]<<8));
2431da177e4SLinus Torvalds 				}
2441da177e4SLinus Torvalds 			}
2451da177e4SLinus Torvalds 			break;
2461da177e4SLinus Torvalds 	}
2471da177e4SLinus Torvalds 	being_used--;
2481da177e4SLinus Torvalds }
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds int iforce_get_id_packet(struct iforce *iforce, char *packet)
2511da177e4SLinus Torvalds {
2521da177e4SLinus Torvalds 	switch (iforce->bus) {
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	case IFORCE_USB:
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds #ifdef CONFIG_JOYSTICK_IFORCE_USB
2571da177e4SLinus Torvalds 		iforce->cr.bRequest = packet[0];
2581da177e4SLinus Torvalds 		iforce->ctrl->dev = iforce->usbdev;
2591da177e4SLinus Torvalds 
260*fb76b099SVojtech Pavlik 		if (usb_submit_urb(iforce->ctrl, GFP_ATOMIC))
2611da177e4SLinus Torvalds 			return -1;
2621da177e4SLinus Torvalds 
263*fb76b099SVojtech Pavlik 		wait_event_interruptible_timeout(iforce->wait,
264*fb76b099SVojtech Pavlik 			iforce->ctrl->status != -EINPROGRESS, HZ);
2651da177e4SLinus Torvalds 
266*fb76b099SVojtech Pavlik 		if (iforce->ctrl->status != -EINPROGRESS) {
2671da177e4SLinus Torvalds 			usb_unlink_urb(iforce->ctrl);
2681da177e4SLinus Torvalds 			return -1;
2691da177e4SLinus Torvalds 		}
2701da177e4SLinus Torvalds #else
2711da177e4SLinus Torvalds 		printk(KERN_ERR "iforce_get_id_packet: iforce->bus = USB!\n");
2721da177e4SLinus Torvalds #endif
2731da177e4SLinus Torvalds 		break;
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	case IFORCE_232:
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds #ifdef CONFIG_JOYSTICK_IFORCE_232
2781da177e4SLinus Torvalds 		iforce->expect_packet = FF_CMD_QUERY;
2791da177e4SLinus Torvalds 		iforce_send_packet(iforce, FF_CMD_QUERY, packet);
2801da177e4SLinus Torvalds 
281*fb76b099SVojtech Pavlik 		wait_event_interruptible_timeout(iforce->wait,
282*fb76b099SVojtech Pavlik 			!iforce->expect_packet, HZ);
2831da177e4SLinus Torvalds 
284*fb76b099SVojtech Pavlik 		if (iforce->expect_packet) {
2851da177e4SLinus Torvalds 			iforce->expect_packet = 0;
2861da177e4SLinus Torvalds 			return -1;
2871da177e4SLinus Torvalds 		}
2881da177e4SLinus Torvalds #else
2891da177e4SLinus Torvalds 		printk(KERN_ERR "iforce_get_id_packet: iforce->bus = SERIO!\n");
2901da177e4SLinus Torvalds #endif
2911da177e4SLinus Torvalds 		break;
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	default:
2941da177e4SLinus Torvalds 		printk(KERN_ERR "iforce_get_id_packet: iforce->bus = %d\n",
2951da177e4SLinus Torvalds 		       iforce->bus);
2961da177e4SLinus Torvalds 		break;
2971da177e4SLinus Torvalds 	}
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	return -(iforce->edata[0] != packet[0]);
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds 
302