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 /* 10 */ 11 12 #include "iforce.h" 13 14 static struct { 15 __s32 x; 16 __s32 y; 17 } iforce_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}}; 18 19 20 void iforce_dump_packet(struct iforce *iforce, char *msg, u16 cmd, unsigned char *data) 21 { 22 dev_dbg(iforce->dev->dev.parent, "%s %s cmd = %04x, data = %*ph\n", 23 __func__, msg, cmd, LO(cmd), data); 24 } 25 26 /* 27 * Send a packet of bytes to the device 28 */ 29 int iforce_send_packet(struct iforce *iforce, u16 cmd, unsigned char* data) 30 { 31 /* Copy data to buffer */ 32 int n = LO(cmd); 33 int c; 34 int empty; 35 int head, tail; 36 unsigned long flags; 37 38 /* 39 * Update head and tail of xmit buffer 40 */ 41 spin_lock_irqsave(&iforce->xmit_lock, flags); 42 43 head = iforce->xmit.head; 44 tail = iforce->xmit.tail; 45 46 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); 51 return -1; 52 } 53 54 empty = head == tail; 55 XMIT_INC(iforce->xmit.head, n+2); 56 57 /* 58 * Store packet in xmit buffer 59 */ 60 iforce->xmit.buf[head] = HI(cmd); 61 XMIT_INC(head, 1); 62 iforce->xmit.buf[head] = LO(cmd); 63 XMIT_INC(head, 1); 64 65 c = CIRC_SPACE_TO_END(head, tail, XMIT_SIZE); 66 if (n < c) c=n; 67 68 memcpy(&iforce->xmit.buf[head], 69 data, 70 c); 71 if (n != c) { 72 memcpy(&iforce->xmit.buf[0], 73 data + c, 74 n - c); 75 } 76 XMIT_INC(head, n); 77 78 spin_unlock_irqrestore(&iforce->xmit_lock, flags); 79 /* 80 * If necessary, start the transmission 81 */ 82 switch (iforce->bus) { 83 84 #ifdef CONFIG_JOYSTICK_IFORCE_232 85 case IFORCE_232: 86 if (empty) 87 iforce_serial_xmit(iforce); 88 break; 89 #endif 90 #ifdef CONFIG_JOYSTICK_IFORCE_USB 91 case IFORCE_USB: 92 93 if (iforce->usbdev && empty && 94 !test_and_set_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags)) { 95 96 iforce_usb_xmit(iforce); 97 } 98 break; 99 #endif 100 } 101 return 0; 102 } 103 104 /* Start or stop an effect */ 105 int iforce_control_playback(struct iforce* iforce, u16 id, unsigned int value) 106 { 107 unsigned char data[3]; 108 109 data[0] = LO(id); 110 data[1] = (value > 0) ? ((value > 1) ? 0x41 : 0x01) : 0; 111 data[2] = LO(value); 112 return iforce_send_packet(iforce, FF_CMD_PLAY, data); 113 } 114 115 /* Mark an effect that was being updated as ready. That means it can be updated 116 * again */ 117 static int mark_core_as_ready(struct iforce *iforce, unsigned short addr) 118 { 119 int i; 120 121 if (!iforce->dev->ff) 122 return 0; 123 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); 129 return 0; 130 } 131 } 132 dev_warn(&iforce->dev->dev, "unused effect %04x updated !!!\n", addr); 133 return -1; 134 } 135 136 void iforce_process_packet(struct iforce *iforce, u16 cmd, unsigned char *data) 137 { 138 struct input_dev *dev = iforce->dev; 139 int i; 140 static int being_used = 0; 141 142 if (being_used) 143 dev_warn(&iforce->dev->dev, 144 "re-entrant call to iforce_process %d\n", being_used); 145 being_used++; 146 147 #ifdef CONFIG_JOYSTICK_IFORCE_232 148 if (HI(iforce->expect_packet) == HI(cmd)) { 149 iforce->expect_packet = 0; 150 iforce->ecmd = cmd; 151 memcpy(iforce->edata, data, IFORCE_MAX_LENGTH); 152 } 153 #endif 154 wake_up(&iforce->wait); 155 156 if (!iforce->type) { 157 being_used--; 158 return; 159 } 160 161 switch (HI(cmd)) { 162 163 case 0x01: /* joystick position data */ 164 case 0x03: /* wheel position data */ 165 if (HI(cmd) == 1) { 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]); 171 } else { 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]); 175 } 176 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); 179 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))); 182 183 /* If there are untouched bits left, interpret them as the second hat */ 184 if (i <= 8) { 185 int btns = data[6]; 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); 190 } 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); 195 } 196 } 197 198 input_sync(dev); 199 200 break; 201 202 case 0x02: /* status report */ 203 input_report_key(dev, BTN_DEAD, data[0] & 0x02); 204 input_sync(dev); 205 206 /* Check if an effect was just started or stopped */ 207 i = data[1] & 0x7f; 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); 212 } 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); 216 } 217 if (LO(cmd) > 3) { 218 int j; 219 for (j = 3; j < LO(cmd); j += 2) 220 mark_core_as_ready(iforce, data[j] | (data[j+1]<<8)); 221 } 222 break; 223 } 224 being_used--; 225 } 226 227 int iforce_get_id_packet(struct iforce *iforce, char *packet) 228 { 229 switch (iforce->bus) { 230 231 case IFORCE_USB: { 232 #ifdef CONFIG_JOYSTICK_IFORCE_USB 233 int status; 234 235 iforce->cr.bRequest = packet[0]; 236 iforce->ctrl->dev = iforce->usbdev; 237 238 status = usb_submit_urb(iforce->ctrl, GFP_KERNEL); 239 if (status) { 240 dev_err(&iforce->intf->dev, 241 "usb_submit_urb failed %d\n", status); 242 return -1; 243 } 244 245 wait_event_interruptible_timeout(iforce->wait, 246 iforce->ctrl->status != -EINPROGRESS, HZ); 247 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); 253 return -1; 254 } 255 #else 256 printk(KERN_DEBUG "iforce_get_id_packet: iforce->bus = USB!\n"); 257 #endif 258 } 259 break; 260 261 case IFORCE_232: 262 263 #ifdef CONFIG_JOYSTICK_IFORCE_232 264 iforce->expect_packet = FF_CMD_QUERY; 265 iforce_send_packet(iforce, FF_CMD_QUERY, packet); 266 267 wait_event_interruptible_timeout(iforce->wait, 268 !iforce->expect_packet, HZ); 269 270 if (iforce->expect_packet) { 271 iforce->expect_packet = 0; 272 return -1; 273 } 274 #else 275 dev_err(&iforce->dev->dev, 276 "iforce_get_id_packet: iforce->bus = SERIO!\n"); 277 #endif 278 break; 279 280 default: 281 dev_err(&iforce->dev->dev, 282 "iforce_get_id_packet: iforce->bus = %d\n", 283 iforce->bus); 284 break; 285 } 286 287 return -(iforce->edata[0] != packet[0]); 288 } 289 290