xref: /linux/drivers/input/joystick/iforce/iforce-packets.c (revision 1a59d1b8e05ea6ab45f7e18897de1ef0e6bc3da6)
1*1a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz>
4598972d4SJohann Deneux  *  Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com>
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *  USB/RS232 I-Force joysticks and wheels.
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds /*
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #include "iforce.h"
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds static struct {
151da177e4SLinus Torvalds 	__s32 x;
161da177e4SLinus Torvalds 	__s32 y;
171da177e4SLinus Torvalds } iforce_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds 
20305180bcSTim Schumacher void iforce_dump_packet(struct iforce *iforce, char *msg, u16 cmd, unsigned char *data)
211da177e4SLinus Torvalds {
22305180bcSTim Schumacher 	dev_dbg(iforce->dev->dev.parent, "%s %s cmd = %04x, data = %*ph\n",
23305180bcSTim Schumacher 		__func__, msg, cmd, LO(cmd), data);
241da177e4SLinus Torvalds }
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds /*
271da177e4SLinus Torvalds  * Send a packet of bytes to the device
281da177e4SLinus Torvalds  */
291da177e4SLinus Torvalds int iforce_send_packet(struct iforce *iforce, u16 cmd, unsigned char* data)
301da177e4SLinus Torvalds {
311da177e4SLinus Torvalds 	/* Copy data to buffer */
321da177e4SLinus Torvalds 	int n = LO(cmd);
331da177e4SLinus Torvalds 	int c;
341da177e4SLinus Torvalds 	int empty;
351da177e4SLinus Torvalds 	int head, tail;
361da177e4SLinus Torvalds 	unsigned long flags;
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds /*
391da177e4SLinus Torvalds  * Update head and tail of xmit buffer
401da177e4SLinus Torvalds  */
411da177e4SLinus Torvalds 	spin_lock_irqsave(&iforce->xmit_lock, flags);
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds 	head = iforce->xmit.head;
441da177e4SLinus Torvalds 	tail = iforce->xmit.tail;
451da177e4SLinus Torvalds 
46598972d4SJohann Deneux 
471da177e4SLinus Torvalds 	if (CIRC_SPACE(head, tail, XMIT_SIZE) < n+2) {
481817b169SGreg Kroah-Hartman 		dev_warn(&iforce->dev->dev,
491817b169SGreg Kroah-Hartman 			 "not enough space in xmit buffer to send new packet\n");
501da177e4SLinus Torvalds 		spin_unlock_irqrestore(&iforce->xmit_lock, flags);
511da177e4SLinus Torvalds 		return -1;
521da177e4SLinus Torvalds 	}
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds 	empty = head == tail;
551da177e4SLinus Torvalds 	XMIT_INC(iforce->xmit.head, n+2);
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds /*
581da177e4SLinus Torvalds  * Store packet in xmit buffer
591da177e4SLinus Torvalds  */
601da177e4SLinus Torvalds 	iforce->xmit.buf[head] = HI(cmd);
611da177e4SLinus Torvalds 	XMIT_INC(head, 1);
621da177e4SLinus Torvalds 	iforce->xmit.buf[head] = LO(cmd);
631da177e4SLinus Torvalds 	XMIT_INC(head, 1);
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds 	c = CIRC_SPACE_TO_END(head, tail, XMIT_SIZE);
661da177e4SLinus Torvalds 	if (n < c) c=n;
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds 	memcpy(&iforce->xmit.buf[head],
691da177e4SLinus Torvalds 	       data,
701da177e4SLinus Torvalds 	       c);
711da177e4SLinus Torvalds 	if (n != c) {
721da177e4SLinus Torvalds 		memcpy(&iforce->xmit.buf[0],
731da177e4SLinus Torvalds 		       data + c,
741da177e4SLinus Torvalds 		       n - c);
751da177e4SLinus Torvalds 	}
761da177e4SLinus Torvalds 	XMIT_INC(head, n);
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds 	spin_unlock_irqrestore(&iforce->xmit_lock, flags);
791da177e4SLinus Torvalds /*
801da177e4SLinus Torvalds  * If necessary, start the transmission
811da177e4SLinus Torvalds  */
821da177e4SLinus Torvalds 	switch (iforce->bus) {
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds #ifdef CONFIG_JOYSTICK_IFORCE_232
851da177e4SLinus Torvalds 		case IFORCE_232:
861da177e4SLinus Torvalds 		if (empty)
871da177e4SLinus Torvalds 			iforce_serial_xmit(iforce);
881da177e4SLinus Torvalds 		break;
891da177e4SLinus Torvalds #endif
901da177e4SLinus Torvalds #ifdef CONFIG_JOYSTICK_IFORCE_USB
911da177e4SLinus Torvalds 		case IFORCE_USB:
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 		if (iforce->usbdev && empty &&
941da177e4SLinus Torvalds 			!test_and_set_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags)) {
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 			iforce_usb_xmit(iforce);
971da177e4SLinus Torvalds 		}
981da177e4SLinus Torvalds 		break;
991da177e4SLinus Torvalds #endif
1001da177e4SLinus Torvalds 	}
1011da177e4SLinus Torvalds 	return 0;
1021da177e4SLinus Torvalds }
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds /* Start or stop an effect */
1051da177e4SLinus Torvalds int iforce_control_playback(struct iforce* iforce, u16 id, unsigned int value)
1061da177e4SLinus Torvalds {
1071da177e4SLinus Torvalds 	unsigned char data[3];
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds 	data[0] = LO(id);
1101da177e4SLinus Torvalds 	data[1] = (value > 0) ? ((value > 1) ? 0x41 : 0x01) : 0;
1111da177e4SLinus Torvalds 	data[2] = LO(value);
1121da177e4SLinus Torvalds 	return iforce_send_packet(iforce, FF_CMD_PLAY, data);
1131da177e4SLinus Torvalds }
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds /* Mark an effect that was being updated as ready. That means it can be updated
1161da177e4SLinus Torvalds  * again */
1171da177e4SLinus Torvalds static int mark_core_as_ready(struct iforce *iforce, unsigned short addr)
1181da177e4SLinus Torvalds {
1191da177e4SLinus Torvalds 	int i;
12017dd3f0fSDmitry Torokhov 
121f6a01c85SAnssi Hannula 	if (!iforce->dev->ff)
122f6a01c85SAnssi Hannula 		return 0;
123f6a01c85SAnssi Hannula 
124f6a01c85SAnssi Hannula 	for (i = 0; i < iforce->dev->ff->max_effects; ++i) {
1251da177e4SLinus Torvalds 		if (test_bit(FF_CORE_IS_USED, iforce->core_effects[i].flags) &&
1261da177e4SLinus Torvalds 		    (iforce->core_effects[i].mod1_chunk.start == addr ||
1271da177e4SLinus Torvalds 		     iforce->core_effects[i].mod2_chunk.start == addr)) {
1281da177e4SLinus Torvalds 			clear_bit(FF_CORE_UPDATE, iforce->core_effects[i].flags);
1291da177e4SLinus Torvalds 			return 0;
1301da177e4SLinus Torvalds 		}
1311da177e4SLinus Torvalds 	}
1321817b169SGreg Kroah-Hartman 	dev_warn(&iforce->dev->dev, "unused effect %04x updated !!!\n", addr);
1331da177e4SLinus Torvalds 	return -1;
1341da177e4SLinus Torvalds }
1351da177e4SLinus Torvalds 
1367d12e780SDavid Howells void iforce_process_packet(struct iforce *iforce, u16 cmd, unsigned char *data)
1371da177e4SLinus Torvalds {
13817dd3f0fSDmitry Torokhov 	struct input_dev *dev = iforce->dev;
1391da177e4SLinus Torvalds 	int i;
1401da177e4SLinus Torvalds 	static int being_used = 0;
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	if (being_used)
1431817b169SGreg Kroah-Hartman 		dev_warn(&iforce->dev->dev,
1441817b169SGreg Kroah-Hartman 			 "re-entrant call to iforce_process %d\n", being_used);
1451da177e4SLinus Torvalds 	being_used++;
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds #ifdef CONFIG_JOYSTICK_IFORCE_232
1481da177e4SLinus Torvalds 	if (HI(iforce->expect_packet) == HI(cmd)) {
1491da177e4SLinus Torvalds 		iforce->expect_packet = 0;
1501da177e4SLinus Torvalds 		iforce->ecmd = cmd;
1511da177e4SLinus Torvalds 		memcpy(iforce->edata, data, IFORCE_MAX_LENGTH);
1521da177e4SLinus Torvalds 	}
1531da177e4SLinus Torvalds #endif
15497d4ebfeSDmitry Torokhov 	wake_up(&iforce->wait);
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	if (!iforce->type) {
1571da177e4SLinus Torvalds 		being_used--;
1581da177e4SLinus Torvalds 		return;
1591da177e4SLinus Torvalds 	}
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 	switch (HI(cmd)) {
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds 		case 0x01:	/* joystick position data */
1641da177e4SLinus Torvalds 		case 0x03:	/* wheel position data */
1651da177e4SLinus Torvalds 			if (HI(cmd) == 1) {
1661da177e4SLinus Torvalds 				input_report_abs(dev, ABS_X, (__s16) (((__s16)data[1] << 8) | data[0]));
1671da177e4SLinus Torvalds 				input_report_abs(dev, ABS_Y, (__s16) (((__s16)data[3] << 8) | data[2]));
1681da177e4SLinus Torvalds 				input_report_abs(dev, ABS_THROTTLE, 255 - data[4]);
1691da177e4SLinus Torvalds 				if (LO(cmd) >= 8 && test_bit(ABS_RUDDER ,dev->absbit))
1701da177e4SLinus Torvalds 					input_report_abs(dev, ABS_RUDDER, (__s8)data[7]);
1711da177e4SLinus Torvalds 			} else {
1721da177e4SLinus Torvalds 				input_report_abs(dev, ABS_WHEEL, (__s16) (((__s16)data[1] << 8) | data[0]));
1731da177e4SLinus Torvalds 				input_report_abs(dev, ABS_GAS,   255 - data[2]);
1741da177e4SLinus Torvalds 				input_report_abs(dev, ABS_BRAKE, 255 - data[3]);
1751da177e4SLinus Torvalds 			}
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 			input_report_abs(dev, ABS_HAT0X, iforce_hat_to_axis[data[6] >> 4].x);
1781da177e4SLinus Torvalds 			input_report_abs(dev, ABS_HAT0Y, iforce_hat_to_axis[data[6] >> 4].y);
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 			for (i = 0; iforce->type->btn[i] >= 0; i++)
1811da177e4SLinus Torvalds 				input_report_key(dev, iforce->type->btn[i], data[(i >> 3) + 5] & (1 << (i & 7)));
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 			/* If there are untouched bits left, interpret them as the second hat */
1841da177e4SLinus Torvalds 			if (i <= 8) {
1851da177e4SLinus Torvalds 				int btns = data[6];
1861da177e4SLinus Torvalds 				if (test_bit(ABS_HAT1X, dev->absbit)) {
1871da177e4SLinus Torvalds 					if (btns & 8) input_report_abs(dev, ABS_HAT1X, -1);
1881da177e4SLinus Torvalds 					else if (btns & 2) input_report_abs(dev, ABS_HAT1X, 1);
1891da177e4SLinus Torvalds 					else input_report_abs(dev, ABS_HAT1X, 0);
1901da177e4SLinus Torvalds 				}
1911da177e4SLinus Torvalds 				if (test_bit(ABS_HAT1Y, dev->absbit)) {
1921da177e4SLinus Torvalds 					if (btns & 1) input_report_abs(dev, ABS_HAT1Y, -1);
1931da177e4SLinus Torvalds 					else if (btns & 4) input_report_abs(dev, ABS_HAT1Y, 1);
1941da177e4SLinus Torvalds 					else input_report_abs(dev, ABS_HAT1Y, 0);
1951da177e4SLinus Torvalds 				}
1961da177e4SLinus Torvalds 			}
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 			input_sync(dev);
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 			break;
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 		case 0x02:	/* status report */
2031da177e4SLinus Torvalds 			input_report_key(dev, BTN_DEAD, data[0] & 0x02);
2041da177e4SLinus Torvalds 			input_sync(dev);
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 			/* Check if an effect was just started or stopped */
2071da177e4SLinus Torvalds 			i = data[1] & 0x7f;
2081da177e4SLinus Torvalds 			if (data[1] & 0x80) {
2091da177e4SLinus Torvalds 				if (!test_and_set_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
2101da177e4SLinus Torvalds 					/* Report play event */
2111da177e4SLinus Torvalds 					input_report_ff_status(dev, i, FF_STATUS_PLAYING);
2121da177e4SLinus Torvalds 				}
213f6a01c85SAnssi Hannula 			} else if (test_and_clear_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
2141da177e4SLinus Torvalds 				/* Report stop event */
2151da177e4SLinus Torvalds 				input_report_ff_status(dev, i, FF_STATUS_STOPPED);
2161da177e4SLinus Torvalds 			}
2171da177e4SLinus Torvalds 			if (LO(cmd) > 3) {
2181da177e4SLinus Torvalds 				int j;
219f6a01c85SAnssi Hannula 				for (j = 3; j < LO(cmd); j += 2)
2201da177e4SLinus Torvalds 					mark_core_as_ready(iforce, data[j] | (data[j+1]<<8));
2211da177e4SLinus Torvalds 			}
2221da177e4SLinus Torvalds 			break;
2231da177e4SLinus Torvalds 	}
2241da177e4SLinus Torvalds 	being_used--;
2251da177e4SLinus Torvalds }
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds int iforce_get_id_packet(struct iforce *iforce, char *packet)
2281da177e4SLinus Torvalds {
2291da177e4SLinus Torvalds 	switch (iforce->bus) {
2301da177e4SLinus Torvalds 
231d9f03831SAndrew Morton 	case IFORCE_USB: {
2321da177e4SLinus Torvalds #ifdef CONFIG_JOYSTICK_IFORCE_USB
233d9f03831SAndrew Morton 		int status;
234d9f03831SAndrew Morton 
2351da177e4SLinus Torvalds 		iforce->cr.bRequest = packet[0];
2361da177e4SLinus Torvalds 		iforce->ctrl->dev = iforce->usbdev;
2371da177e4SLinus Torvalds 
238381a2c6fSSebastian Andrzej Siewior 		status = usb_submit_urb(iforce->ctrl, GFP_KERNEL);
239c0338c15SJohann Deneux 		if (status) {
240a852d78eSGreg Kroah-Hartman 			dev_err(&iforce->intf->dev,
2417b22a885SGreg Kroah-Hartman 				"usb_submit_urb failed %d\n", status);
2421da177e4SLinus Torvalds 			return -1;
243c0338c15SJohann Deneux 		}
2441da177e4SLinus Torvalds 
245fb76b099SVojtech Pavlik 		wait_event_interruptible_timeout(iforce->wait,
246fb76b099SVojtech Pavlik 			iforce->ctrl->status != -EINPROGRESS, HZ);
2471da177e4SLinus Torvalds 
24897d4ebfeSDmitry Torokhov 		if (iforce->ctrl->status) {
249a852d78eSGreg Kroah-Hartman 			dev_dbg(&iforce->intf->dev,
250f576125dSGreg Kroah-Hartman 				"iforce->ctrl->status = %d\n",
251f576125dSGreg Kroah-Hartman 				iforce->ctrl->status);
2521da177e4SLinus Torvalds 			usb_unlink_urb(iforce->ctrl);
2531da177e4SLinus Torvalds 			return -1;
2541da177e4SLinus Torvalds 		}
2551da177e4SLinus Torvalds #else
256f576125dSGreg Kroah-Hartman 		printk(KERN_DEBUG "iforce_get_id_packet: iforce->bus = USB!\n");
2571da177e4SLinus Torvalds #endif
258d9f03831SAndrew Morton 		}
2591da177e4SLinus Torvalds 		break;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	case IFORCE_232:
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds #ifdef CONFIG_JOYSTICK_IFORCE_232
2641da177e4SLinus Torvalds 		iforce->expect_packet = FF_CMD_QUERY;
2651da177e4SLinus Torvalds 		iforce_send_packet(iforce, FF_CMD_QUERY, packet);
2661da177e4SLinus Torvalds 
267fb76b099SVojtech Pavlik 		wait_event_interruptible_timeout(iforce->wait,
268fb76b099SVojtech Pavlik 			!iforce->expect_packet, HZ);
2691da177e4SLinus Torvalds 
270fb76b099SVojtech Pavlik 		if (iforce->expect_packet) {
2711da177e4SLinus Torvalds 			iforce->expect_packet = 0;
2721da177e4SLinus Torvalds 			return -1;
2731da177e4SLinus Torvalds 		}
2741da177e4SLinus Torvalds #else
2757b22a885SGreg Kroah-Hartman 		dev_err(&iforce->dev->dev,
2767b22a885SGreg Kroah-Hartman 			"iforce_get_id_packet: iforce->bus = SERIO!\n");
2771da177e4SLinus Torvalds #endif
2781da177e4SLinus Torvalds 		break;
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 	default:
2817b22a885SGreg Kroah-Hartman 		dev_err(&iforce->dev->dev,
2827b22a885SGreg Kroah-Hartman 			"iforce_get_id_packet: iforce->bus = %d\n",
2837b22a885SGreg Kroah-Hartman 			iforce->bus);
2841da177e4SLinus Torvalds 		break;
2851da177e4SLinus Torvalds 	}
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	return -(iforce->edata[0] != packet[0]);
2881da177e4SLinus Torvalds }
2891da177e4SLinus Torvalds 
290