xref: /linux/drivers/input/joystick/iforce/iforce-packets.c (revision 3e51108c72e8adbcf3180ed40527a2a9d2d0123b)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz>
4  *  Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com>
5  *
6  *  USB/RS232 I-Force joysticks and wheels.
7  */
8 
9 #include <linux/unaligned.h>
10 #include "iforce.h"
11 
12 static struct {
13 	__s32 x;
14 	__s32 y;
15 } iforce_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
16 
17 
iforce_dump_packet(struct iforce * iforce,char * msg,u16 cmd,unsigned char * data)18 void iforce_dump_packet(struct iforce *iforce, char *msg, u16 cmd, unsigned char *data)
19 {
20 	dev_dbg(iforce->dev->dev.parent, "%s %s cmd = %04x, data = %*ph\n",
21 		__func__, msg, cmd, LO(cmd), data);
22 }
23 
24 /*
25  * Send a packet of bytes to the device
26  */
iforce_send_packet(struct iforce * iforce,u16 cmd,unsigned char * data)27 int iforce_send_packet(struct iforce *iforce, u16 cmd, unsigned char* data)
28 {
29 	/* Copy data to buffer */
30 	int n = LO(cmd);
31 	int c;
32 	int empty;
33 	int head, tail;
34 
35 /*
36  * Update head and tail of xmit buffer
37  */
38 	scoped_guard(spinlock_irqsave, &iforce->xmit_lock) {
39 		head = iforce->xmit.head;
40 		tail = iforce->xmit.tail;
41 
42 		if (CIRC_SPACE(head, tail, XMIT_SIZE) < n + 2) {
43 			dev_warn(&iforce->dev->dev,
44 				 "not enough space in xmit buffer to send new packet\n");
45 			return -1;
46 		}
47 
48 		empty = head == tail;
49 		XMIT_INC(iforce->xmit.head, n + 2);
50 
51 /*
52  * Store packet in xmit buffer
53  */
54 		iforce->xmit.buf[head] = HI(cmd);
55 		XMIT_INC(head, 1);
56 		iforce->xmit.buf[head] = LO(cmd);
57 		XMIT_INC(head, 1);
58 
59 		c = CIRC_SPACE_TO_END(head, tail, XMIT_SIZE);
60 		if (n < c)
61 			c = n;
62 
63 		memcpy(&iforce->xmit.buf[head], data, c);
64 		if (n != c)
65 			memcpy(&iforce->xmit.buf[0], data + c, n - c);
66 
67 		XMIT_INC(head, n);
68 	}
69 
70 /*
71  * If necessary, start the transmission
72  */
73 	if (empty)
74 		iforce->xport_ops->xmit(iforce);
75 
76 	return 0;
77 }
78 EXPORT_SYMBOL(iforce_send_packet);
79 
80 /* Start or stop an effect */
iforce_control_playback(struct iforce * iforce,u16 id,unsigned int value)81 int iforce_control_playback(struct iforce* iforce, u16 id, unsigned int value)
82 {
83 	unsigned char data[3];
84 
85 	data[0] = LO(id);
86 	data[1] = (value > 0) ? ((value > 1) ? 0x41 : 0x01) : 0;
87 	data[2] = LO(value);
88 	return iforce_send_packet(iforce, FF_CMD_PLAY, data);
89 }
90 
91 /* Mark an effect that was being updated as ready. That means it can be updated
92  * again */
mark_core_as_ready(struct iforce * iforce,unsigned short addr)93 static int mark_core_as_ready(struct iforce *iforce, unsigned short addr)
94 {
95 	int i;
96 
97 	if (!iforce->dev->ff)
98 		return 0;
99 
100 	for (i = 0; i < iforce->dev->ff->max_effects; ++i) {
101 		if (test_bit(FF_CORE_IS_USED, iforce->core_effects[i].flags) &&
102 		    (iforce->core_effects[i].mod1_chunk.start == addr ||
103 		     iforce->core_effects[i].mod2_chunk.start == addr)) {
104 			clear_bit(FF_CORE_UPDATE, iforce->core_effects[i].flags);
105 			return 0;
106 		}
107 	}
108 	dev_warn(&iforce->dev->dev, "unused effect %04x updated !!!\n", addr);
109 	return -1;
110 }
111 
iforce_report_hats_buttons(struct iforce * iforce,u8 * data)112 static void iforce_report_hats_buttons(struct iforce *iforce, u8 *data)
113 {
114 	struct input_dev *dev = iforce->dev;
115 	int i;
116 
117 	input_report_abs(dev, ABS_HAT0X, iforce_hat_to_axis[data[6] >> 4].x);
118 	input_report_abs(dev, ABS_HAT0Y, iforce_hat_to_axis[data[6] >> 4].y);
119 
120 	for (i = 0; iforce->type->btn[i] >= 0; i++)
121 		input_report_key(dev, iforce->type->btn[i],
122 				 data[(i >> 3) + 5] & (1 << (i & 7)));
123 
124 	/* If there are untouched bits left, interpret them as the second hat */
125 	if (i <= 8) {
126 		u8 btns = data[6];
127 
128 		if (test_bit(ABS_HAT1X, dev->absbit)) {
129 			if (btns & BIT(3))
130 				input_report_abs(dev, ABS_HAT1X, -1);
131 			else if (btns & BIT(1))
132 				input_report_abs(dev, ABS_HAT1X, 1);
133 			else
134 				input_report_abs(dev, ABS_HAT1X, 0);
135 		}
136 
137 		if (test_bit(ABS_HAT1Y, dev->absbit)) {
138 			if (btns & BIT(0))
139 				input_report_abs(dev, ABS_HAT1Y, -1);
140 			else if (btns & BIT(2))
141 				input_report_abs(dev, ABS_HAT1Y, 1);
142 			else
143 				input_report_abs(dev, ABS_HAT1Y, 0);
144 		}
145 	}
146 }
147 
iforce_process_packet(struct iforce * iforce,u8 packet_id,u8 * data,size_t len)148 void iforce_process_packet(struct iforce *iforce,
149 			   u8 packet_id, u8 *data, size_t len)
150 {
151 	struct input_dev *dev = iforce->dev;
152 	int i, j;
153 
154 	switch (packet_id) {
155 
156 	case 0x01:	/* joystick position data */
157 		input_report_abs(dev, ABS_X,
158 				 (__s16) get_unaligned_le16(data));
159 		input_report_abs(dev, ABS_Y,
160 				 (__s16) get_unaligned_le16(data + 2));
161 		input_report_abs(dev, ABS_THROTTLE, 255 - data[4]);
162 
163 		if (len >= 8 && test_bit(ABS_RUDDER ,dev->absbit))
164 			input_report_abs(dev, ABS_RUDDER, (__s8)data[7]);
165 
166 		iforce_report_hats_buttons(iforce, data);
167 
168 		input_sync(dev);
169 		break;
170 
171 	case 0x03:	/* wheel position data */
172 		input_report_abs(dev, ABS_WHEEL,
173 				 (__s16) get_unaligned_le16(data));
174 		input_report_abs(dev, ABS_GAS,   255 - data[2]);
175 		input_report_abs(dev, ABS_BRAKE, 255 - data[3]);
176 
177 		iforce_report_hats_buttons(iforce, data);
178 
179 		input_sync(dev);
180 		break;
181 
182 	case 0x02:	/* status report */
183 		input_report_key(dev, BTN_DEAD, data[0] & 0x02);
184 		input_sync(dev);
185 
186 		/* Check if an effect was just started or stopped */
187 		i = data[1] & 0x7f;
188 		if (data[1] & 0x80) {
189 			if (!test_and_set_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
190 				/* Report play event */
191 				input_report_ff_status(dev, i, FF_STATUS_PLAYING);
192 			}
193 		} else if (test_and_clear_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
194 			/* Report stop event */
195 			input_report_ff_status(dev, i, FF_STATUS_STOPPED);
196 		}
197 
198 		for (j = 3; j < len; j += 2)
199 			mark_core_as_ready(iforce, get_unaligned_le16(data + j));
200 
201 		break;
202 	}
203 }
204 EXPORT_SYMBOL(iforce_process_packet);
205