1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz>
4 * Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com>
6 * USB/RS232 I-Force joysticks and wheels.
17 } iforce_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
20 void iforce_dump_packet(struct iforce *iforce, char *msg, u16 cmd, unsigned char *data)
22 dev_dbg(iforce->dev->dev.parent, "%s %s cmd = %04x, data = %*ph\n",
23 __func__, msg, cmd, LO(cmd), data);
27 * Send a packet of bytes to the device
29 int iforce_send_packet(struct iforce *iforce, u16 cmd, unsigned char* data)
31 /* Copy data to buffer */
39 * Update head and tail of xmit buffer
41 spin_lock_irqsave(&iforce->xmit_lock, flags);
43 head = iforce->xmit.head;
44 tail = iforce->xmit.tail;
47 if (CIRC_SPACE(head, tail, XMIT_SIZE) < n+2) {
48 dev_warn(&iforce->dev->dev,
49 "not enough space in xmit buffer to send new packet\n");
50 spin_unlock_irqrestore(&iforce->xmit_lock, flags);
55 XMIT_INC(iforce->xmit.head, n+2);
58 * Store packet in xmit buffer
60 iforce->xmit.buf[head] = HI(cmd);
62 iforce->xmit.buf[head] = LO(cmd);
65 c = CIRC_SPACE_TO_END(head, tail, XMIT_SIZE);
68 memcpy(&iforce->xmit.buf[head],
72 memcpy(&iforce->xmit.buf[0],
78 spin_unlock_irqrestore(&iforce->xmit_lock, flags);
80 * If necessary, start the transmission
82 switch (iforce->bus) {
84 #ifdef CONFIG_JOYSTICK_IFORCE_232
87 iforce_serial_xmit(iforce);
90 #ifdef CONFIG_JOYSTICK_IFORCE_USB
93 if (iforce->usbdev && empty &&
94 !test_and_set_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags)) {
96 iforce_usb_xmit(iforce);
104 /* Start or stop an effect */
105 int iforce_control_playback(struct iforce* iforce, u16 id, unsigned int value)
107 unsigned char data[3];
110 data[1] = (value > 0) ? ((value > 1) ? 0x41 : 0x01) : 0;
112 return iforce_send_packet(iforce, FF_CMD_PLAY, data);
115 /* Mark an effect that was being updated as ready. That means it can be updated
117 static int mark_core_as_ready(struct iforce *iforce, unsigned short addr)
121 if (!iforce->dev->ff)
124 for (i = 0; i < iforce->dev->ff->max_effects; ++i) {
125 if (test_bit(FF_CORE_IS_USED, iforce->core_effects[i].flags) &&
126 (iforce->core_effects[i].mod1_chunk.start == addr ||
127 iforce->core_effects[i].mod2_chunk.start == addr)) {
128 clear_bit(FF_CORE_UPDATE, iforce->core_effects[i].flags);
132 dev_warn(&iforce->dev->dev, "unused effect %04x updated !!!\n", addr);
136 void iforce_process_packet(struct iforce *iforce, u16 cmd, unsigned char *data)
138 struct input_dev *dev = iforce->dev;
140 static int being_used = 0;
143 dev_warn(&iforce->dev->dev,
144 "re-entrant call to iforce_process %d\n", being_used);
147 #ifdef CONFIG_JOYSTICK_IFORCE_232
148 if (HI(iforce->expect_packet) == HI(cmd)) {
149 iforce->expect_packet = 0;
151 memcpy(iforce->edata, data, IFORCE_MAX_LENGTH);
154 wake_up(&iforce->wait);
163 case 0x01: /* joystick position data */
164 case 0x03: /* wheel position data */
166 input_report_abs(dev, ABS_X, (__s16) (((__s16)data[1] << 8) | data[0]));
167 input_report_abs(dev, ABS_Y, (__s16) (((__s16)data[3] << 8) | data[2]));
168 input_report_abs(dev, ABS_THROTTLE, 255 - data[4]);
169 if (LO(cmd) >= 8 && test_bit(ABS_RUDDER ,dev->absbit))
170 input_report_abs(dev, ABS_RUDDER, (__s8)data[7]);
172 input_report_abs(dev, ABS_WHEEL, (__s16) (((__s16)data[1] << 8) | data[0]));
173 input_report_abs(dev, ABS_GAS, 255 - data[2]);
174 input_report_abs(dev, ABS_BRAKE, 255 - data[3]);
177 input_report_abs(dev, ABS_HAT0X, iforce_hat_to_axis[data[6] >> 4].x);
178 input_report_abs(dev, ABS_HAT0Y, iforce_hat_to_axis[data[6] >> 4].y);
180 for (i = 0; iforce->type->btn[i] >= 0; i++)
181 input_report_key(dev, iforce->type->btn[i], data[(i >> 3) + 5] & (1 << (i & 7)));
183 /* If there are untouched bits left, interpret them as the second hat */
186 if (test_bit(ABS_HAT1X, dev->absbit)) {
187 if (btns & 8) input_report_abs(dev, ABS_HAT1X, -1);
188 else if (btns & 2) input_report_abs(dev, ABS_HAT1X, 1);
189 else input_report_abs(dev, ABS_HAT1X, 0);
191 if (test_bit(ABS_HAT1Y, dev->absbit)) {
192 if (btns & 1) input_report_abs(dev, ABS_HAT1Y, -1);
193 else if (btns & 4) input_report_abs(dev, ABS_HAT1Y, 1);
194 else input_report_abs(dev, ABS_HAT1Y, 0);
202 case 0x02: /* status report */
203 input_report_key(dev, BTN_DEAD, data[0] & 0x02);
206 /* Check if an effect was just started or stopped */
208 if (data[1] & 0x80) {
209 if (!test_and_set_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
210 /* Report play event */
211 input_report_ff_status(dev, i, FF_STATUS_PLAYING);
213 } else if (test_and_clear_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
214 /* Report stop event */
215 input_report_ff_status(dev, i, FF_STATUS_STOPPED);
219 for (j = 3; j < LO(cmd); j += 2)
220 mark_core_as_ready(iforce, data[j] | (data[j+1]<<8));
227 int iforce_get_id_packet(struct iforce *iforce, char *packet)
229 switch (iforce->bus) {
232 #ifdef CONFIG_JOYSTICK_IFORCE_USB
235 iforce->cr.bRequest = packet[0];
236 iforce->ctrl->dev = iforce->usbdev;
238 status = usb_submit_urb(iforce->ctrl, GFP_KERNEL);
240 dev_err(&iforce->intf->dev,
241 "usb_submit_urb failed %d\n", status);
245 wait_event_interruptible_timeout(iforce->wait,
246 iforce->ctrl->status != -EINPROGRESS, HZ);
248 if (iforce->ctrl->status) {
249 dev_dbg(&iforce->intf->dev,
250 "iforce->ctrl->status = %d\n",
251 iforce->ctrl->status);
252 usb_unlink_urb(iforce->ctrl);
256 printk(KERN_DEBUG "iforce_get_id_packet: iforce->bus = USB!\n");
263 #ifdef CONFIG_JOYSTICK_IFORCE_232
264 iforce->expect_packet = FF_CMD_QUERY;
265 iforce_send_packet(iforce, FF_CMD_QUERY, packet);
267 wait_event_interruptible_timeout(iforce->wait,
268 !iforce->expect_packet, HZ);
270 if (iforce->expect_packet) {
271 iforce->expect_packet = 0;
275 dev_err(&iforce->dev->dev,
276 "iforce_get_id_packet: iforce->bus = SERIO!\n");
281 dev_err(&iforce->dev->dev,
282 "iforce_get_id_packet: iforce->bus = %d\n",
287 return -(iforce->edata[0] != packet[0]);