xref: /linux/drivers/input/joystick/iforce/iforce-packets.c (revision d3cc100069f945a392d6cde5ea326bb686418193)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz>
3598972d4SJohann Deneux  *  Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com>
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  USB/RS232 I-Force joysticks and wheels.
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds /*
91da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
101da177e4SLinus Torvalds  * it under the terms of the GNU General Public License as published by
111da177e4SLinus Torvalds  * the Free Software Foundation; either version 2 of the License, or
121da177e4SLinus Torvalds  * (at your option) any later version.
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  * This program is distributed in the hope that it will be useful,
151da177e4SLinus Torvalds  * but WITHOUT ANY WARRANTY; without even the implied warranty of
161da177e4SLinus Torvalds  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
171da177e4SLinus Torvalds  * GNU General Public License for more details.
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public License
201da177e4SLinus Torvalds  * along with this program; if not, write to the Free Software
211da177e4SLinus Torvalds  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
221da177e4SLinus Torvalds  */
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #include "iforce.h"
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds static struct {
271da177e4SLinus Torvalds 	__s32 x;
281da177e4SLinus Torvalds 	__s32 y;
291da177e4SLinus Torvalds } iforce_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds 
32305180bcSTim Schumacher void iforce_dump_packet(struct iforce *iforce, char *msg, u16 cmd, unsigned char *data)
331da177e4SLinus Torvalds {
34305180bcSTim Schumacher 	dev_dbg(iforce->dev->dev.parent, "%s %s cmd = %04x, data = %*ph\n",
35305180bcSTim Schumacher 		__func__, msg, cmd, LO(cmd), data);
361da177e4SLinus Torvalds }
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds /*
391da177e4SLinus Torvalds  * Send a packet of bytes to the device
401da177e4SLinus Torvalds  */
411da177e4SLinus Torvalds int iforce_send_packet(struct iforce *iforce, u16 cmd, unsigned char* data)
421da177e4SLinus Torvalds {
431da177e4SLinus Torvalds 	/* Copy data to buffer */
441da177e4SLinus Torvalds 	int n = LO(cmd);
451da177e4SLinus Torvalds 	int c;
461da177e4SLinus Torvalds 	int empty;
471da177e4SLinus Torvalds 	int head, tail;
481da177e4SLinus Torvalds 	unsigned long flags;
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds /*
511da177e4SLinus Torvalds  * Update head and tail of xmit buffer
521da177e4SLinus Torvalds  */
531da177e4SLinus Torvalds 	spin_lock_irqsave(&iforce->xmit_lock, flags);
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds 	head = iforce->xmit.head;
561da177e4SLinus Torvalds 	tail = iforce->xmit.tail;
571da177e4SLinus Torvalds 
58598972d4SJohann Deneux 
591da177e4SLinus Torvalds 	if (CIRC_SPACE(head, tail, XMIT_SIZE) < n+2) {
601817b169SGreg Kroah-Hartman 		dev_warn(&iforce->dev->dev,
611817b169SGreg Kroah-Hartman 			 "not enough space in xmit buffer to send new packet\n");
621da177e4SLinus Torvalds 		spin_unlock_irqrestore(&iforce->xmit_lock, flags);
631da177e4SLinus Torvalds 		return -1;
641da177e4SLinus Torvalds 	}
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds 	empty = head == tail;
671da177e4SLinus Torvalds 	XMIT_INC(iforce->xmit.head, n+2);
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds /*
701da177e4SLinus Torvalds  * Store packet in xmit buffer
711da177e4SLinus Torvalds  */
721da177e4SLinus Torvalds 	iforce->xmit.buf[head] = HI(cmd);
731da177e4SLinus Torvalds 	XMIT_INC(head, 1);
741da177e4SLinus Torvalds 	iforce->xmit.buf[head] = LO(cmd);
751da177e4SLinus Torvalds 	XMIT_INC(head, 1);
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds 	c = CIRC_SPACE_TO_END(head, tail, XMIT_SIZE);
781da177e4SLinus Torvalds 	if (n < c) c=n;
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds 	memcpy(&iforce->xmit.buf[head],
811da177e4SLinus Torvalds 	       data,
821da177e4SLinus Torvalds 	       c);
831da177e4SLinus Torvalds 	if (n != c) {
841da177e4SLinus Torvalds 		memcpy(&iforce->xmit.buf[0],
851da177e4SLinus Torvalds 		       data + c,
861da177e4SLinus Torvalds 		       n - c);
871da177e4SLinus Torvalds 	}
881da177e4SLinus Torvalds 	XMIT_INC(head, n);
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds 	spin_unlock_irqrestore(&iforce->xmit_lock, flags);
911da177e4SLinus Torvalds /*
921da177e4SLinus Torvalds  * If necessary, start the transmission
931da177e4SLinus Torvalds  */
941da177e4SLinus Torvalds 	if (empty)
9538d10769SDmitry Torokhov 		iforce->xport_ops->xmit(iforce);
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	return 0;
981da177e4SLinus Torvalds }
994f99de6dSDmitry Torokhov EXPORT_SYMBOL(iforce_send_packet);
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds /* Start or stop an effect */
1021da177e4SLinus Torvalds int iforce_control_playback(struct iforce* iforce, u16 id, unsigned int value)
1031da177e4SLinus Torvalds {
1041da177e4SLinus Torvalds 	unsigned char data[3];
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds 	data[0] = LO(id);
1071da177e4SLinus Torvalds 	data[1] = (value > 0) ? ((value > 1) ? 0x41 : 0x01) : 0;
1081da177e4SLinus Torvalds 	data[2] = LO(value);
1091da177e4SLinus Torvalds 	return iforce_send_packet(iforce, FF_CMD_PLAY, data);
1101da177e4SLinus Torvalds }
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds /* Mark an effect that was being updated as ready. That means it can be updated
1131da177e4SLinus Torvalds  * again */
1141da177e4SLinus Torvalds static int mark_core_as_ready(struct iforce *iforce, unsigned short addr)
1151da177e4SLinus Torvalds {
1161da177e4SLinus Torvalds 	int i;
11717dd3f0fSDmitry Torokhov 
118f6a01c85SAnssi Hannula 	if (!iforce->dev->ff)
119f6a01c85SAnssi Hannula 		return 0;
120f6a01c85SAnssi Hannula 
121f6a01c85SAnssi Hannula 	for (i = 0; i < iforce->dev->ff->max_effects; ++i) {
1221da177e4SLinus Torvalds 		if (test_bit(FF_CORE_IS_USED, iforce->core_effects[i].flags) &&
1231da177e4SLinus Torvalds 		    (iforce->core_effects[i].mod1_chunk.start == addr ||
1241da177e4SLinus Torvalds 		     iforce->core_effects[i].mod2_chunk.start == addr)) {
1251da177e4SLinus Torvalds 			clear_bit(FF_CORE_UPDATE, iforce->core_effects[i].flags);
1261da177e4SLinus Torvalds 			return 0;
1271da177e4SLinus Torvalds 		}
1281da177e4SLinus Torvalds 	}
1291817b169SGreg Kroah-Hartman 	dev_warn(&iforce->dev->dev, "unused effect %04x updated !!!\n", addr);
1301da177e4SLinus Torvalds 	return -1;
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
1338a25e058SDmitry Torokhov static void iforce_report_hats_buttons(struct iforce *iforce, u8 *data)
1341da177e4SLinus Torvalds {
13517dd3f0fSDmitry Torokhov 	struct input_dev *dev = iforce->dev;
1361da177e4SLinus Torvalds 	int i;
1371da177e4SLinus Torvalds 
1388a25e058SDmitry Torokhov 	input_report_abs(dev, ABS_HAT0X, iforce_hat_to_axis[data[6] >> 4].x);
1398a25e058SDmitry Torokhov 	input_report_abs(dev, ABS_HAT0Y, iforce_hat_to_axis[data[6] >> 4].y);
1408a25e058SDmitry Torokhov 
1418a25e058SDmitry Torokhov 	for (i = 0; iforce->type->btn[i] >= 0; i++)
1428a25e058SDmitry Torokhov 		input_report_key(dev, iforce->type->btn[i],
1438a25e058SDmitry Torokhov 				 data[(i >> 3) + 5] & (1 << (i & 7)));
1448a25e058SDmitry Torokhov 
1458a25e058SDmitry Torokhov 	/* If there are untouched bits left, interpret them as the second hat */
1468a25e058SDmitry Torokhov 	if (i <= 8) {
1478a25e058SDmitry Torokhov 		u8 btns = data[6];
1488a25e058SDmitry Torokhov 
1498a25e058SDmitry Torokhov 		if (test_bit(ABS_HAT1X, dev->absbit)) {
1508a25e058SDmitry Torokhov 			if (btns & BIT(3))
1518a25e058SDmitry Torokhov 				input_report_abs(dev, ABS_HAT1X, -1);
1528a25e058SDmitry Torokhov 			else if (btns & BIT(1))
1538a25e058SDmitry Torokhov 				input_report_abs(dev, ABS_HAT1X, 1);
1548a25e058SDmitry Torokhov 			else
1558a25e058SDmitry Torokhov 				input_report_abs(dev, ABS_HAT1X, 0);
1568a25e058SDmitry Torokhov 		}
1578a25e058SDmitry Torokhov 
1588a25e058SDmitry Torokhov 		if (test_bit(ABS_HAT1Y, dev->absbit)) {
1598a25e058SDmitry Torokhov 			if (btns & BIT(0))
1608a25e058SDmitry Torokhov 				input_report_abs(dev, ABS_HAT1Y, -1);
1618a25e058SDmitry Torokhov 			else if (btns & BIT(2))
1628a25e058SDmitry Torokhov 				input_report_abs(dev, ABS_HAT1Y, 1);
1638a25e058SDmitry Torokhov 			else
1648a25e058SDmitry Torokhov 				input_report_abs(dev, ABS_HAT1Y, 0);
1658a25e058SDmitry Torokhov 		}
1668a25e058SDmitry Torokhov 	}
1678a25e058SDmitry Torokhov }
1688a25e058SDmitry Torokhov 
169*d3cc1000SDmitry Torokhov void iforce_process_packet(struct iforce *iforce,
170*d3cc1000SDmitry Torokhov 			   u8 packet_id, u8 *data, size_t len)
1718a25e058SDmitry Torokhov {
1728a25e058SDmitry Torokhov 	struct input_dev *dev = iforce->dev;
1738a25e058SDmitry Torokhov 	int i, j;
1748a25e058SDmitry Torokhov 
17597d4ebfeSDmitry Torokhov 	wake_up(&iforce->wait);
1761da177e4SLinus Torvalds 
177f7f3651eSDmitry Torokhov 	if (!iforce->type)
1781da177e4SLinus Torvalds 		return;
1791da177e4SLinus Torvalds 
180*d3cc1000SDmitry Torokhov 	switch (packet_id) {
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 	case 0x01:	/* joystick position data */
1831da177e4SLinus Torvalds 		input_report_abs(dev, ABS_X, (__s16) (((__s16)data[1] << 8) | data[0]));
1841da177e4SLinus Torvalds 		input_report_abs(dev, ABS_Y, (__s16) (((__s16)data[3] << 8) | data[2]));
1851da177e4SLinus Torvalds 		input_report_abs(dev, ABS_THROTTLE, 255 - data[4]);
1868a25e058SDmitry Torokhov 
187*d3cc1000SDmitry Torokhov 		if (len >= 8 && test_bit(ABS_RUDDER ,dev->absbit))
1881da177e4SLinus Torvalds 			input_report_abs(dev, ABS_RUDDER, (__s8)data[7]);
1898a25e058SDmitry Torokhov 
1908a25e058SDmitry Torokhov 		iforce_report_hats_buttons(iforce, data);
1918a25e058SDmitry Torokhov 
1928a25e058SDmitry Torokhov 		input_sync(dev);
1938a25e058SDmitry Torokhov 		break;
1948a25e058SDmitry Torokhov 
1958a25e058SDmitry Torokhov 	case 0x03:	/* wheel position data */
1961da177e4SLinus Torvalds 		input_report_abs(dev, ABS_WHEEL, (__s16) (((__s16)data[1] << 8) | data[0]));
1971da177e4SLinus Torvalds 		input_report_abs(dev, ABS_GAS,   255 - data[2]);
1981da177e4SLinus Torvalds 		input_report_abs(dev, ABS_BRAKE, 255 - data[3]);
1991da177e4SLinus Torvalds 
2008a25e058SDmitry Torokhov 		iforce_report_hats_buttons(iforce, data);
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 		input_sync(dev);
2031da177e4SLinus Torvalds 		break;
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 	case 0x02:	/* status report */
2061da177e4SLinus Torvalds 		input_report_key(dev, BTN_DEAD, data[0] & 0x02);
2071da177e4SLinus Torvalds 		input_sync(dev);
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 		/* Check if an effect was just started or stopped */
2101da177e4SLinus Torvalds 		i = data[1] & 0x7f;
2111da177e4SLinus Torvalds 		if (data[1] & 0x80) {
2121da177e4SLinus Torvalds 			if (!test_and_set_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
2131da177e4SLinus Torvalds 				/* Report play event */
2141da177e4SLinus Torvalds 				input_report_ff_status(dev, i, FF_STATUS_PLAYING);
2151da177e4SLinus Torvalds 			}
216f6a01c85SAnssi Hannula 		} else if (test_and_clear_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
2171da177e4SLinus Torvalds 			/* Report stop event */
2181da177e4SLinus Torvalds 			input_report_ff_status(dev, i, FF_STATUS_STOPPED);
2191da177e4SLinus Torvalds 		}
2208a25e058SDmitry Torokhov 
221*d3cc1000SDmitry Torokhov 		for (j = 3; j < len; j += 2)
2221da177e4SLinus Torvalds 			mark_core_as_ready(iforce, data[j] | (data[j + 1] << 8));
2238a25e058SDmitry Torokhov 
2241da177e4SLinus Torvalds 		break;
2251da177e4SLinus Torvalds 	}
2261da177e4SLinus Torvalds }
2274f99de6dSDmitry Torokhov EXPORT_SYMBOL(iforce_process_packet);
228