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