1 /* 2 * Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz> 3 * Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com> 4 * 5 * USB/RS232 I-Force joysticks and wheels. 6 */ 7 8 /* 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; either version 2 of the License, or 12 * (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program; if not, write to the Free Software 21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 22 */ 23 24 #include "iforce.h" 25 26 static struct { 27 __s32 x; 28 __s32 y; 29 } iforce_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}}; 30 31 32 void iforce_dump_packet(struct iforce *iforce, char *msg, u16 cmd, unsigned char *data) 33 { 34 dev_dbg(iforce->dev->dev.parent, "%s %s cmd = %04x, data = %*ph\n", 35 __func__, msg, cmd, LO(cmd), data); 36 } 37 38 /* 39 * Send a packet of bytes to the device 40 */ 41 int iforce_send_packet(struct iforce *iforce, u16 cmd, unsigned char* data) 42 { 43 /* Copy data to buffer */ 44 int n = LO(cmd); 45 int c; 46 int empty; 47 int head, tail; 48 unsigned long flags; 49 50 /* 51 * Update head and tail of xmit buffer 52 */ 53 spin_lock_irqsave(&iforce->xmit_lock, flags); 54 55 head = iforce->xmit.head; 56 tail = iforce->xmit.tail; 57 58 59 if (CIRC_SPACE(head, tail, XMIT_SIZE) < n+2) { 60 dev_warn(&iforce->dev->dev, 61 "not enough space in xmit buffer to send new packet\n"); 62 spin_unlock_irqrestore(&iforce->xmit_lock, flags); 63 return -1; 64 } 65 66 empty = head == tail; 67 XMIT_INC(iforce->xmit.head, n+2); 68 69 /* 70 * Store packet in xmit buffer 71 */ 72 iforce->xmit.buf[head] = HI(cmd); 73 XMIT_INC(head, 1); 74 iforce->xmit.buf[head] = LO(cmd); 75 XMIT_INC(head, 1); 76 77 c = CIRC_SPACE_TO_END(head, tail, XMIT_SIZE); 78 if (n < c) c=n; 79 80 memcpy(&iforce->xmit.buf[head], 81 data, 82 c); 83 if (n != c) { 84 memcpy(&iforce->xmit.buf[0], 85 data + c, 86 n - c); 87 } 88 XMIT_INC(head, n); 89 90 spin_unlock_irqrestore(&iforce->xmit_lock, flags); 91 /* 92 * If necessary, start the transmission 93 */ 94 switch (iforce->bus) { 95 96 #ifdef CONFIG_JOYSTICK_IFORCE_232 97 case IFORCE_232: 98 if (empty) 99 iforce_serial_xmit(iforce); 100 break; 101 #endif 102 #ifdef CONFIG_JOYSTICK_IFORCE_USB 103 case IFORCE_USB: 104 105 if (iforce->usbdev && empty && 106 !test_and_set_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags)) { 107 108 iforce_usb_xmit(iforce); 109 } 110 break; 111 #endif 112 } 113 return 0; 114 } 115 116 /* Start or stop an effect */ 117 int iforce_control_playback(struct iforce* iforce, u16 id, unsigned int value) 118 { 119 unsigned char data[3]; 120 121 data[0] = LO(id); 122 data[1] = (value > 0) ? ((value > 1) ? 0x41 : 0x01) : 0; 123 data[2] = LO(value); 124 return iforce_send_packet(iforce, FF_CMD_PLAY, data); 125 } 126 127 /* Mark an effect that was being updated as ready. That means it can be updated 128 * again */ 129 static int mark_core_as_ready(struct iforce *iforce, unsigned short addr) 130 { 131 int i; 132 133 if (!iforce->dev->ff) 134 return 0; 135 136 for (i = 0; i < iforce->dev->ff->max_effects; ++i) { 137 if (test_bit(FF_CORE_IS_USED, iforce->core_effects[i].flags) && 138 (iforce->core_effects[i].mod1_chunk.start == addr || 139 iforce->core_effects[i].mod2_chunk.start == addr)) { 140 clear_bit(FF_CORE_UPDATE, iforce->core_effects[i].flags); 141 return 0; 142 } 143 } 144 dev_warn(&iforce->dev->dev, "unused effect %04x updated !!!\n", addr); 145 return -1; 146 } 147 148 void iforce_process_packet(struct iforce *iforce, u16 cmd, unsigned char *data) 149 { 150 struct input_dev *dev = iforce->dev; 151 int i; 152 static int being_used = 0; 153 154 if (being_used) 155 dev_warn(&iforce->dev->dev, 156 "re-entrant call to iforce_process %d\n", being_used); 157 being_used++; 158 159 #ifdef CONFIG_JOYSTICK_IFORCE_232 160 if (HI(iforce->expect_packet) == HI(cmd)) { 161 iforce->expect_packet = 0; 162 iforce->ecmd = cmd; 163 memcpy(iforce->edata, data, IFORCE_MAX_LENGTH); 164 } 165 #endif 166 wake_up(&iforce->wait); 167 168 if (!iforce->type) { 169 being_used--; 170 return; 171 } 172 173 switch (HI(cmd)) { 174 175 case 0x01: /* joystick position data */ 176 case 0x03: /* wheel position data */ 177 if (HI(cmd) == 1) { 178 input_report_abs(dev, ABS_X, (__s16) (((__s16)data[1] << 8) | data[0])); 179 input_report_abs(dev, ABS_Y, (__s16) (((__s16)data[3] << 8) | data[2])); 180 input_report_abs(dev, ABS_THROTTLE, 255 - data[4]); 181 if (LO(cmd) >= 8 && test_bit(ABS_RUDDER ,dev->absbit)) 182 input_report_abs(dev, ABS_RUDDER, (__s8)data[7]); 183 } else { 184 input_report_abs(dev, ABS_WHEEL, (__s16) (((__s16)data[1] << 8) | data[0])); 185 input_report_abs(dev, ABS_GAS, 255 - data[2]); 186 input_report_abs(dev, ABS_BRAKE, 255 - data[3]); 187 } 188 189 input_report_abs(dev, ABS_HAT0X, iforce_hat_to_axis[data[6] >> 4].x); 190 input_report_abs(dev, ABS_HAT0Y, iforce_hat_to_axis[data[6] >> 4].y); 191 192 for (i = 0; iforce->type->btn[i] >= 0; i++) 193 input_report_key(dev, iforce->type->btn[i], data[(i >> 3) + 5] & (1 << (i & 7))); 194 195 /* If there are untouched bits left, interpret them as the second hat */ 196 if (i <= 8) { 197 int btns = data[6]; 198 if (test_bit(ABS_HAT1X, dev->absbit)) { 199 if (btns & 8) input_report_abs(dev, ABS_HAT1X, -1); 200 else if (btns & 2) input_report_abs(dev, ABS_HAT1X, 1); 201 else input_report_abs(dev, ABS_HAT1X, 0); 202 } 203 if (test_bit(ABS_HAT1Y, dev->absbit)) { 204 if (btns & 1) input_report_abs(dev, ABS_HAT1Y, -1); 205 else if (btns & 4) input_report_abs(dev, ABS_HAT1Y, 1); 206 else input_report_abs(dev, ABS_HAT1Y, 0); 207 } 208 } 209 210 input_sync(dev); 211 212 break; 213 214 case 0x02: /* status report */ 215 input_report_key(dev, BTN_DEAD, data[0] & 0x02); 216 input_sync(dev); 217 218 /* Check if an effect was just started or stopped */ 219 i = data[1] & 0x7f; 220 if (data[1] & 0x80) { 221 if (!test_and_set_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) { 222 /* Report play event */ 223 input_report_ff_status(dev, i, FF_STATUS_PLAYING); 224 } 225 } else if (test_and_clear_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) { 226 /* Report stop event */ 227 input_report_ff_status(dev, i, FF_STATUS_STOPPED); 228 } 229 if (LO(cmd) > 3) { 230 int j; 231 for (j = 3; j < LO(cmd); j += 2) 232 mark_core_as_ready(iforce, data[j] | (data[j+1]<<8)); 233 } 234 break; 235 } 236 being_used--; 237 } 238 239 int iforce_get_id_packet(struct iforce *iforce, char *packet) 240 { 241 switch (iforce->bus) { 242 243 case IFORCE_USB: { 244 #ifdef CONFIG_JOYSTICK_IFORCE_USB 245 int status; 246 247 iforce->cr.bRequest = packet[0]; 248 iforce->ctrl->dev = iforce->usbdev; 249 250 status = usb_submit_urb(iforce->ctrl, GFP_KERNEL); 251 if (status) { 252 dev_err(&iforce->intf->dev, 253 "usb_submit_urb failed %d\n", status); 254 return -1; 255 } 256 257 wait_event_interruptible_timeout(iforce->wait, 258 iforce->ctrl->status != -EINPROGRESS, HZ); 259 260 if (iforce->ctrl->status) { 261 dev_dbg(&iforce->intf->dev, 262 "iforce->ctrl->status = %d\n", 263 iforce->ctrl->status); 264 usb_unlink_urb(iforce->ctrl); 265 return -1; 266 } 267 #else 268 printk(KERN_DEBUG "iforce_get_id_packet: iforce->bus = USB!\n"); 269 #endif 270 } 271 break; 272 273 case IFORCE_232: 274 275 #ifdef CONFIG_JOYSTICK_IFORCE_232 276 iforce->expect_packet = FF_CMD_QUERY; 277 iforce_send_packet(iforce, FF_CMD_QUERY, packet); 278 279 wait_event_interruptible_timeout(iforce->wait, 280 !iforce->expect_packet, HZ); 281 282 if (iforce->expect_packet) { 283 iforce->expect_packet = 0; 284 return -1; 285 } 286 #else 287 dev_err(&iforce->dev->dev, 288 "iforce_get_id_packet: iforce->bus = SERIO!\n"); 289 #endif 290 break; 291 292 default: 293 dev_err(&iforce->dev->dev, 294 "iforce_get_id_packet: iforce->bus = %d\n", 295 iforce->bus); 296 break; 297 } 298 299 return -(iforce->edata[0] != packet[0]); 300 } 301 302