Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz> |
Johann Deneux | 598972d | 2007-04-12 01:30:24 -0400 | [diff] [blame] | 3 | * Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * Should you need to contact me, the author, you can do so either by |
| 24 | * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail: |
| 25 | * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic |
| 26 | */ |
| 27 | |
| 28 | #include "iforce.h" |
| 29 | |
| 30 | static struct { |
| 31 | __s32 x; |
| 32 | __s32 y; |
| 33 | } iforce_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}}; |
| 34 | |
| 35 | |
| 36 | void iforce_dump_packet(char *msg, u16 cmd, unsigned char *data) |
| 37 | { |
| 38 | int i; |
| 39 | |
Johann Deneux | 598972d | 2007-04-12 01:30:24 -0400 | [diff] [blame] | 40 | printk(KERN_DEBUG __FILE__ ": %s cmd = %04x, data = ", msg, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | for (i = 0; i < LO(cmd); i++) |
| 42 | printk("%02x ", data[i]); |
Johann Deneux | 598972d | 2007-04-12 01:30:24 -0400 | [diff] [blame] | 43 | printk("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /* |
| 47 | * Send a packet of bytes to the device |
| 48 | */ |
| 49 | int iforce_send_packet(struct iforce *iforce, u16 cmd, unsigned char* data) |
| 50 | { |
| 51 | /* Copy data to buffer */ |
| 52 | int n = LO(cmd); |
| 53 | int c; |
| 54 | int empty; |
| 55 | int head, tail; |
| 56 | unsigned long flags; |
| 57 | |
| 58 | /* |
| 59 | * Update head and tail of xmit buffer |
| 60 | */ |
| 61 | spin_lock_irqsave(&iforce->xmit_lock, flags); |
| 62 | |
| 63 | head = iforce->xmit.head; |
| 64 | tail = iforce->xmit.tail; |
| 65 | |
Johann Deneux | 598972d | 2007-04-12 01:30:24 -0400 | [diff] [blame] | 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | if (CIRC_SPACE(head, tail, XMIT_SIZE) < n+2) { |
Greg Kroah-Hartman | 1817b16 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 68 | dev_warn(&iforce->dev->dev, |
| 69 | "not enough space in xmit buffer to send new packet\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | spin_unlock_irqrestore(&iforce->xmit_lock, flags); |
| 71 | return -1; |
| 72 | } |
| 73 | |
| 74 | empty = head == tail; |
| 75 | XMIT_INC(iforce->xmit.head, n+2); |
| 76 | |
| 77 | /* |
| 78 | * Store packet in xmit buffer |
| 79 | */ |
| 80 | iforce->xmit.buf[head] = HI(cmd); |
| 81 | XMIT_INC(head, 1); |
| 82 | iforce->xmit.buf[head] = LO(cmd); |
| 83 | XMIT_INC(head, 1); |
| 84 | |
| 85 | c = CIRC_SPACE_TO_END(head, tail, XMIT_SIZE); |
| 86 | if (n < c) c=n; |
| 87 | |
| 88 | memcpy(&iforce->xmit.buf[head], |
| 89 | data, |
| 90 | c); |
| 91 | if (n != c) { |
| 92 | memcpy(&iforce->xmit.buf[0], |
| 93 | data + c, |
| 94 | n - c); |
| 95 | } |
| 96 | XMIT_INC(head, n); |
| 97 | |
| 98 | spin_unlock_irqrestore(&iforce->xmit_lock, flags); |
| 99 | /* |
| 100 | * If necessary, start the transmission |
| 101 | */ |
| 102 | switch (iforce->bus) { |
| 103 | |
| 104 | #ifdef CONFIG_JOYSTICK_IFORCE_232 |
| 105 | case IFORCE_232: |
| 106 | if (empty) |
| 107 | iforce_serial_xmit(iforce); |
| 108 | break; |
| 109 | #endif |
| 110 | #ifdef CONFIG_JOYSTICK_IFORCE_USB |
| 111 | case IFORCE_USB: |
| 112 | |
| 113 | if (iforce->usbdev && empty && |
| 114 | !test_and_set_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags)) { |
| 115 | |
| 116 | iforce_usb_xmit(iforce); |
| 117 | } |
| 118 | break; |
| 119 | #endif |
| 120 | } |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | /* Start or stop an effect */ |
| 125 | int iforce_control_playback(struct iforce* iforce, u16 id, unsigned int value) |
| 126 | { |
| 127 | unsigned char data[3]; |
| 128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | data[0] = LO(id); |
| 130 | data[1] = (value > 0) ? ((value > 1) ? 0x41 : 0x01) : 0; |
| 131 | data[2] = LO(value); |
| 132 | return iforce_send_packet(iforce, FF_CMD_PLAY, data); |
| 133 | } |
| 134 | |
| 135 | /* Mark an effect that was being updated as ready. That means it can be updated |
| 136 | * again */ |
| 137 | static int mark_core_as_ready(struct iforce *iforce, unsigned short addr) |
| 138 | { |
| 139 | int i; |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 140 | |
Anssi Hannula | f6a01c8 | 2006-07-19 01:40:39 -0400 | [diff] [blame] | 141 | if (!iforce->dev->ff) |
| 142 | return 0; |
| 143 | |
| 144 | for (i = 0; i < iforce->dev->ff->max_effects; ++i) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | if (test_bit(FF_CORE_IS_USED, iforce->core_effects[i].flags) && |
| 146 | (iforce->core_effects[i].mod1_chunk.start == addr || |
| 147 | iforce->core_effects[i].mod2_chunk.start == addr)) { |
| 148 | clear_bit(FF_CORE_UPDATE, iforce->core_effects[i].flags); |
| 149 | return 0; |
| 150 | } |
| 151 | } |
Greg Kroah-Hartman | 1817b16 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 152 | dev_warn(&iforce->dev->dev, "unused effect %04x updated !!!\n", addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | return -1; |
| 154 | } |
| 155 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 156 | void iforce_process_packet(struct iforce *iforce, u16 cmd, unsigned char *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | { |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 158 | struct input_dev *dev = iforce->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | int i; |
| 160 | static int being_used = 0; |
| 161 | |
| 162 | if (being_used) |
Greg Kroah-Hartman | 1817b16 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 163 | dev_warn(&iforce->dev->dev, |
| 164 | "re-entrant call to iforce_process %d\n", being_used); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | being_used++; |
| 166 | |
| 167 | #ifdef CONFIG_JOYSTICK_IFORCE_232 |
| 168 | if (HI(iforce->expect_packet) == HI(cmd)) { |
| 169 | iforce->expect_packet = 0; |
| 170 | iforce->ecmd = cmd; |
| 171 | memcpy(iforce->edata, data, IFORCE_MAX_LENGTH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
| 173 | #endif |
Dmitry Torokhov | 97d4ebf | 2006-01-31 01:31:07 -0500 | [diff] [blame] | 174 | wake_up(&iforce->wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
| 176 | if (!iforce->type) { |
| 177 | being_used--; |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | switch (HI(cmd)) { |
| 182 | |
| 183 | case 0x01: /* joystick position data */ |
| 184 | case 0x03: /* wheel position data */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | if (HI(cmd) == 1) { |
| 186 | input_report_abs(dev, ABS_X, (__s16) (((__s16)data[1] << 8) | data[0])); |
| 187 | input_report_abs(dev, ABS_Y, (__s16) (((__s16)data[3] << 8) | data[2])); |
| 188 | input_report_abs(dev, ABS_THROTTLE, 255 - data[4]); |
| 189 | if (LO(cmd) >= 8 && test_bit(ABS_RUDDER ,dev->absbit)) |
| 190 | input_report_abs(dev, ABS_RUDDER, (__s8)data[7]); |
| 191 | } else { |
| 192 | input_report_abs(dev, ABS_WHEEL, (__s16) (((__s16)data[1] << 8) | data[0])); |
| 193 | input_report_abs(dev, ABS_GAS, 255 - data[2]); |
| 194 | input_report_abs(dev, ABS_BRAKE, 255 - data[3]); |
| 195 | } |
| 196 | |
| 197 | input_report_abs(dev, ABS_HAT0X, iforce_hat_to_axis[data[6] >> 4].x); |
| 198 | input_report_abs(dev, ABS_HAT0Y, iforce_hat_to_axis[data[6] >> 4].y); |
| 199 | |
| 200 | for (i = 0; iforce->type->btn[i] >= 0; i++) |
| 201 | input_report_key(dev, iforce->type->btn[i], data[(i >> 3) + 5] & (1 << (i & 7))); |
| 202 | |
| 203 | /* If there are untouched bits left, interpret them as the second hat */ |
| 204 | if (i <= 8) { |
| 205 | int btns = data[6]; |
| 206 | if (test_bit(ABS_HAT1X, dev->absbit)) { |
| 207 | if (btns & 8) input_report_abs(dev, ABS_HAT1X, -1); |
| 208 | else if (btns & 2) input_report_abs(dev, ABS_HAT1X, 1); |
| 209 | else input_report_abs(dev, ABS_HAT1X, 0); |
| 210 | } |
| 211 | if (test_bit(ABS_HAT1Y, dev->absbit)) { |
| 212 | if (btns & 1) input_report_abs(dev, ABS_HAT1Y, -1); |
| 213 | else if (btns & 4) input_report_abs(dev, ABS_HAT1Y, 1); |
| 214 | else input_report_abs(dev, ABS_HAT1Y, 0); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | input_sync(dev); |
| 219 | |
| 220 | break; |
| 221 | |
| 222 | case 0x02: /* status report */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | input_report_key(dev, BTN_DEAD, data[0] & 0x02); |
| 224 | input_sync(dev); |
| 225 | |
| 226 | /* Check if an effect was just started or stopped */ |
| 227 | i = data[1] & 0x7f; |
| 228 | if (data[1] & 0x80) { |
| 229 | if (!test_and_set_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) { |
Anssi Hannula | f6a01c8 | 2006-07-19 01:40:39 -0400 | [diff] [blame] | 230 | /* Report play event */ |
| 231 | input_report_ff_status(dev, i, FF_STATUS_PLAYING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | } |
Anssi Hannula | f6a01c8 | 2006-07-19 01:40:39 -0400 | [diff] [blame] | 233 | } else if (test_and_clear_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | /* Report stop event */ |
| 235 | input_report_ff_status(dev, i, FF_STATUS_STOPPED); |
| 236 | } |
| 237 | if (LO(cmd) > 3) { |
| 238 | int j; |
Anssi Hannula | f6a01c8 | 2006-07-19 01:40:39 -0400 | [diff] [blame] | 239 | for (j = 3; j < LO(cmd); j += 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | mark_core_as_ready(iforce, data[j] | (data[j+1]<<8)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
| 242 | break; |
| 243 | } |
| 244 | being_used--; |
| 245 | } |
| 246 | |
| 247 | int iforce_get_id_packet(struct iforce *iforce, char *packet) |
| 248 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | switch (iforce->bus) { |
| 250 | |
Andrew Morton | d9f0383 | 2007-08-30 00:04:33 -0400 | [diff] [blame] | 251 | case IFORCE_USB: { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | #ifdef CONFIG_JOYSTICK_IFORCE_USB |
Andrew Morton | d9f0383 | 2007-08-30 00:04:33 -0400 | [diff] [blame] | 253 | int status; |
| 254 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | iforce->cr.bRequest = packet[0]; |
| 256 | iforce->ctrl->dev = iforce->usbdev; |
| 257 | |
Johann Deneux | c0338c1 | 2007-05-14 00:09:33 -0400 | [diff] [blame] | 258 | status = usb_submit_urb(iforce->ctrl, GFP_ATOMIC); |
| 259 | if (status) { |
| 260 | err("usb_submit_urb failed %d", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | return -1; |
Johann Deneux | c0338c1 | 2007-05-14 00:09:33 -0400 | [diff] [blame] | 262 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
Vojtech Pavlik | fb76b09 | 2005-09-05 00:12:39 -0500 | [diff] [blame] | 264 | wait_event_interruptible_timeout(iforce->wait, |
| 265 | iforce->ctrl->status != -EINPROGRESS, HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
Dmitry Torokhov | 97d4ebf | 2006-01-31 01:31:07 -0500 | [diff] [blame] | 267 | if (iforce->ctrl->status) { |
Johann Deneux | c0338c1 | 2007-05-14 00:09:33 -0400 | [diff] [blame] | 268 | dbg("iforce->ctrl->status = %d", iforce->ctrl->status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | usb_unlink_urb(iforce->ctrl); |
| 270 | return -1; |
| 271 | } |
| 272 | #else |
Johann Deneux | c0338c1 | 2007-05-14 00:09:33 -0400 | [diff] [blame] | 273 | dbg("iforce_get_id_packet: iforce->bus = USB!"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | #endif |
Andrew Morton | d9f0383 | 2007-08-30 00:04:33 -0400 | [diff] [blame] | 275 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | break; |
| 277 | |
| 278 | case IFORCE_232: |
| 279 | |
| 280 | #ifdef CONFIG_JOYSTICK_IFORCE_232 |
| 281 | iforce->expect_packet = FF_CMD_QUERY; |
| 282 | iforce_send_packet(iforce, FF_CMD_QUERY, packet); |
| 283 | |
Vojtech Pavlik | fb76b09 | 2005-09-05 00:12:39 -0500 | [diff] [blame] | 284 | wait_event_interruptible_timeout(iforce->wait, |
| 285 | !iforce->expect_packet, HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
Vojtech Pavlik | fb76b09 | 2005-09-05 00:12:39 -0500 | [diff] [blame] | 287 | if (iforce->expect_packet) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | iforce->expect_packet = 0; |
| 289 | return -1; |
| 290 | } |
| 291 | #else |
Johann Deneux | 598972d | 2007-04-12 01:30:24 -0400 | [diff] [blame] | 292 | err("iforce_get_id_packet: iforce->bus = SERIO!"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | #endif |
| 294 | break; |
| 295 | |
| 296 | default: |
Johann Deneux | 598972d | 2007-04-12 01:30:24 -0400 | [diff] [blame] | 297 | err("iforce_get_id_packet: iforce->bus = %d", iforce->bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | break; |
| 299 | } |
| 300 | |
| 301 | return -(iforce->edata[0] != packet[0]); |
| 302 | } |
| 303 | |