1 /* 2 * Copyright (c) 1998-2005 Vojtech Pavlik 3 */ 4 5 /* 6 * Logitech ADI joystick family driver for Linux 7 */ 8 9 /* 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation; either version 2 of the License, or 13 * (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program; if not, write to the Free Software 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 */ 24 25 #include <linux/delay.h> 26 #include <linux/kernel.h> 27 #include <linux/module.h> 28 #include <linux/string.h> 29 #include <linux/slab.h> 30 #include <linux/input.h> 31 #include <linux/gameport.h> 32 #include <linux/jiffies.h> 33 34 #define DRIVER_DESC "Logitech ADI joystick family driver" 35 36 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); 37 MODULE_DESCRIPTION(DRIVER_DESC); 38 MODULE_LICENSE("GPL"); 39 40 /* 41 * Times, array sizes, flags, ids. 42 */ 43 44 #define ADI_MAX_START 200 /* Trigger to packet timeout [200us] */ 45 #define ADI_MAX_STROBE 40 /* Single bit timeout [40us] */ 46 #define ADI_INIT_DELAY 10 /* Delay after init packet [10ms] */ 47 #define ADI_DATA_DELAY 4 /* Delay after data packet [4ms] */ 48 49 #define ADI_MAX_LENGTH 256 50 #define ADI_MIN_LENGTH 8 51 #define ADI_MIN_LEN_LENGTH 10 52 #define ADI_MIN_ID_LENGTH 66 53 #define ADI_MAX_NAME_LENGTH 64 54 #define ADI_MAX_CNAME_LENGTH 16 55 #define ADI_MAX_PHYS_LENGTH 64 56 57 #define ADI_FLAG_HAT 0x04 58 #define ADI_FLAG_10BIT 0x08 59 60 #define ADI_ID_TPD 0x01 61 #define ADI_ID_WGP 0x06 62 #define ADI_ID_WGPE 0x08 63 #define ADI_ID_MAX 0x0a 64 65 /* 66 * Names, buttons, axes ... 67 */ 68 69 static char *adi_names[] = { "WingMan Extreme Digital", "ThunderPad Digital", "SideCar", "CyberMan 2", 70 "WingMan Interceptor", "WingMan Formula", "WingMan GamePad", 71 "WingMan Extreme Digital 3D", "WingMan GamePad Extreme", 72 "WingMan GamePad USB", "Unknown Device %#x" }; 73 74 static char adi_wmgpe_abs[] = { ABS_X, ABS_Y, ABS_HAT0X, ABS_HAT0Y }; 75 static char adi_wmi_abs[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y }; 76 static char adi_wmed3d_abs[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_RZ, ABS_HAT0X, ABS_HAT0Y }; 77 static char adi_cm2_abs[] = { ABS_X, ABS_Y, ABS_Z, ABS_RX, ABS_RY, ABS_RZ }; 78 static char adi_wmf_abs[] = { ABS_WHEEL, ABS_GAS, ABS_BRAKE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y }; 79 80 static short adi_wmgpe_key[] = { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_START, BTN_MODE, BTN_SELECT }; 81 static short adi_wmi_key[] = { BTN_TRIGGER, BTN_TOP, BTN_THUMB, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_EXTRA }; 82 static short adi_wmed3d_key[] = { BTN_TRIGGER, BTN_THUMB, BTN_THUMB2, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2 }; 83 static short adi_cm2_key[] = { BTN_1, BTN_2, BTN_3, BTN_4, BTN_5, BTN_6, BTN_7, BTN_8 }; 84 85 static char* adi_abs[] = { adi_wmi_abs, adi_wmgpe_abs, adi_wmf_abs, adi_cm2_abs, adi_wmi_abs, adi_wmf_abs, 86 adi_wmgpe_abs, adi_wmed3d_abs, adi_wmgpe_abs, adi_wmgpe_abs, adi_wmi_abs }; 87 88 static short* adi_key[] = { adi_wmi_key, adi_wmgpe_key, adi_cm2_key, adi_cm2_key, adi_wmi_key, adi_cm2_key, 89 adi_wmgpe_key, adi_wmed3d_key, adi_wmgpe_key, adi_wmgpe_key, adi_wmi_key }; 90 91 /* 92 * Hat to axis conversion arrays. 93 */ 94 95 static struct { 96 int x; 97 int y; 98 } adi_hat_to_axis[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}}; 99 100 /* 101 * Per-port information. 102 */ 103 104 struct adi { 105 struct input_dev *dev; 106 int length; 107 int ret; 108 int idx; 109 unsigned char id; 110 char buttons; 111 char axes10; 112 char axes8; 113 signed char pad; 114 char hats; 115 char *abs; 116 short *key; 117 char name[ADI_MAX_NAME_LENGTH]; 118 char cname[ADI_MAX_CNAME_LENGTH]; 119 char phys[ADI_MAX_PHYS_LENGTH]; 120 unsigned char data[ADI_MAX_LENGTH]; 121 }; 122 123 struct adi_port { 124 struct gameport *gameport; 125 struct adi adi[2]; 126 int bad; 127 int reads; 128 }; 129 130 /* 131 * adi_read_packet() reads a Logitech ADI packet. 132 */ 133 134 static void adi_read_packet(struct adi_port *port) 135 { 136 struct adi *adi = port->adi; 137 struct gameport *gameport = port->gameport; 138 unsigned char u, v, w, x, z; 139 int t[2], s[2], i; 140 unsigned long flags; 141 142 for (i = 0; i < 2; i++) { 143 adi[i].ret = -1; 144 t[i] = gameport_time(gameport, ADI_MAX_START); 145 s[i] = 0; 146 } 147 148 local_irq_save(flags); 149 150 gameport_trigger(gameport); 151 v = z = gameport_read(gameport); 152 153 do { 154 u = v; 155 w = u ^ (v = x = gameport_read(gameport)); 156 for (i = 0; i < 2; i++, w >>= 2, x >>= 2) { 157 t[i]--; 158 if ((w & 0x30) && s[i]) { 159 if ((w & 0x30) < 0x30 && adi[i].ret < ADI_MAX_LENGTH && t[i] > 0) { 160 adi[i].data[++adi[i].ret] = w; 161 t[i] = gameport_time(gameport, ADI_MAX_STROBE); 162 } else t[i] = 0; 163 } else if (!(x & 0x30)) s[i] = 1; 164 } 165 } while (t[0] > 0 || t[1] > 0); 166 167 local_irq_restore(flags); 168 169 return; 170 } 171 172 /* 173 * adi_move_bits() detects a possible 2-stream mode, and moves 174 * the bits accordingly. 175 */ 176 177 static void adi_move_bits(struct adi_port *port, int length) 178 { 179 int i; 180 struct adi *adi = port->adi; 181 182 adi[0].idx = adi[1].idx = 0; 183 184 if (adi[0].ret <= 0 || adi[1].ret <= 0) return; 185 if (adi[0].data[0] & 0x20 || ~adi[1].data[0] & 0x20) return; 186 187 for (i = 1; i <= adi[1].ret; i++) 188 adi[0].data[((length - 1) >> 1) + i + 1] = adi[1].data[i]; 189 190 adi[0].ret += adi[1].ret; 191 adi[1].ret = -1; 192 } 193 194 /* 195 * adi_get_bits() gathers bits from the data packet. 196 */ 197 198 static inline int adi_get_bits(struct adi *adi, int count) 199 { 200 int bits = 0; 201 int i; 202 if ((adi->idx += count) > adi->ret) return 0; 203 for (i = 0; i < count; i++) 204 bits |= ((adi->data[adi->idx - i] >> 5) & 1) << i; 205 return bits; 206 } 207 208 /* 209 * adi_decode() decodes Logitech joystick data into input events. 210 */ 211 212 static int adi_decode(struct adi *adi) 213 { 214 struct input_dev *dev = adi->dev; 215 char *abs = adi->abs; 216 short *key = adi->key; 217 int i, t; 218 219 if (adi->ret < adi->length || adi->id != (adi_get_bits(adi, 4) | (adi_get_bits(adi, 4) << 4))) 220 return -1; 221 222 for (i = 0; i < adi->axes10; i++) 223 input_report_abs(dev, *abs++, adi_get_bits(adi, 10)); 224 225 for (i = 0; i < adi->axes8; i++) 226 input_report_abs(dev, *abs++, adi_get_bits(adi, 8)); 227 228 for (i = 0; i < adi->buttons && i < 63; i++) { 229 if (i == adi->pad) { 230 t = adi_get_bits(adi, 4); 231 input_report_abs(dev, *abs++, ((t >> 2) & 1) - ( t & 1)); 232 input_report_abs(dev, *abs++, ((t >> 1) & 1) - ((t >> 3) & 1)); 233 } 234 input_report_key(dev, *key++, adi_get_bits(adi, 1)); 235 } 236 237 for (i = 0; i < adi->hats; i++) { 238 if ((t = adi_get_bits(adi, 4)) > 8) t = 0; 239 input_report_abs(dev, *abs++, adi_hat_to_axis[t].x); 240 input_report_abs(dev, *abs++, adi_hat_to_axis[t].y); 241 } 242 243 for (i = 63; i < adi->buttons; i++) 244 input_report_key(dev, *key++, adi_get_bits(adi, 1)); 245 246 input_sync(dev); 247 248 return 0; 249 } 250 251 /* 252 * adi_read() reads the data packet and decodes it. 253 */ 254 255 static int adi_read(struct adi_port *port) 256 { 257 int i; 258 int result = 0; 259 260 adi_read_packet(port); 261 adi_move_bits(port, port->adi[0].length); 262 263 for (i = 0; i < 2; i++) 264 if (port->adi[i].length) 265 result |= adi_decode(port->adi + i); 266 267 return result; 268 } 269 270 /* 271 * adi_poll() repeatedly polls the Logitech joysticks. 272 */ 273 274 static void adi_poll(struct gameport *gameport) 275 { 276 struct adi_port *port = gameport_get_drvdata(gameport); 277 278 port->bad -= adi_read(port); 279 port->reads++; 280 } 281 282 /* 283 * adi_open() is a callback from the input open routine. 284 */ 285 286 static int adi_open(struct input_dev *dev) 287 { 288 struct adi_port *port = input_get_drvdata(dev); 289 290 gameport_start_polling(port->gameport); 291 return 0; 292 } 293 294 /* 295 * adi_close() is a callback from the input close routine. 296 */ 297 298 static void adi_close(struct input_dev *dev) 299 { 300 struct adi_port *port = input_get_drvdata(dev); 301 302 gameport_stop_polling(port->gameport); 303 } 304 305 /* 306 * adi_init_digital() sends a trigger & delay sequence 307 * to reset and initialize a Logitech joystick into digital mode. 308 */ 309 310 static void adi_init_digital(struct gameport *gameport) 311 { 312 static const int seq[] = { 4, -2, -3, 10, -6, -11, -7, -9, 11, 0 }; 313 int i; 314 315 for (i = 0; seq[i]; i++) { 316 gameport_trigger(gameport); 317 if (seq[i] > 0) 318 msleep(seq[i]); 319 if (seq[i] < 0) { 320 mdelay(-seq[i]); 321 udelay(-seq[i]*14); /* It looks like mdelay() is off by approx 1.4% */ 322 } 323 } 324 } 325 326 static void adi_id_decode(struct adi *adi, struct adi_port *port) 327 { 328 int i, t; 329 330 if (adi->ret < ADI_MIN_ID_LENGTH) /* Minimum ID packet length */ 331 return; 332 333 if (adi->ret < (t = adi_get_bits(adi, 10))) { 334 printk(KERN_WARNING "adi: Short ID packet: reported: %d != read: %d\n", t, adi->ret); 335 return; 336 } 337 338 adi->id = adi_get_bits(adi, 4) | (adi_get_bits(adi, 4) << 4); 339 340 if ((t = adi_get_bits(adi, 4)) & ADI_FLAG_HAT) adi->hats++; 341 342 adi->length = adi_get_bits(adi, 10); 343 344 if (adi->length >= ADI_MAX_LENGTH || adi->length < ADI_MIN_LENGTH) { 345 printk(KERN_WARNING "adi: Bad data packet length (%d).\n", adi->length); 346 adi->length = 0; 347 return; 348 } 349 350 adi->axes8 = adi_get_bits(adi, 4); 351 adi->buttons = adi_get_bits(adi, 6); 352 353 if (adi_get_bits(adi, 6) != 8 && adi->hats) { 354 printk(KERN_WARNING "adi: Other than 8-dir POVs not supported yet.\n"); 355 adi->length = 0; 356 return; 357 } 358 359 adi->buttons += adi_get_bits(adi, 6); 360 adi->hats += adi_get_bits(adi, 4); 361 362 i = adi_get_bits(adi, 4); 363 364 if (t & ADI_FLAG_10BIT) { 365 adi->axes10 = adi->axes8 - i; 366 adi->axes8 = i; 367 } 368 369 t = adi_get_bits(adi, 4); 370 371 for (i = 0; i < t; i++) 372 adi->cname[i] = adi_get_bits(adi, 8); 373 adi->cname[i] = 0; 374 375 t = 8 + adi->buttons + adi->axes10 * 10 + adi->axes8 * 8 + adi->hats * 4; 376 if (adi->length != t && adi->length != t + (t & 1)) { 377 printk(KERN_WARNING "adi: Expected length %d != data length %d\n", t, adi->length); 378 adi->length = 0; 379 return; 380 } 381 382 switch (adi->id) { 383 case ADI_ID_TPD: 384 adi->pad = 4; 385 adi->buttons -= 4; 386 break; 387 case ADI_ID_WGP: 388 adi->pad = 0; 389 adi->buttons -= 4; 390 break; 391 default: 392 adi->pad = -1; 393 break; 394 } 395 } 396 397 static int adi_init_input(struct adi *adi, struct adi_port *port, int half) 398 { 399 struct input_dev *input_dev; 400 char buf[ADI_MAX_NAME_LENGTH]; 401 int i, t; 402 403 adi->dev = input_dev = input_allocate_device(); 404 if (!input_dev) 405 return -ENOMEM; 406 407 t = adi->id < ADI_ID_MAX ? adi->id : ADI_ID_MAX; 408 409 snprintf(buf, ADI_MAX_PHYS_LENGTH, adi_names[t], adi->id); 410 snprintf(adi->name, ADI_MAX_NAME_LENGTH, "Logitech %s [%s]", buf, adi->cname); 411 snprintf(adi->phys, ADI_MAX_PHYS_LENGTH, "%s/input%d", port->gameport->phys, half); 412 413 adi->abs = adi_abs[t]; 414 adi->key = adi_key[t]; 415 416 input_dev->name = adi->name; 417 input_dev->phys = adi->phys; 418 input_dev->id.bustype = BUS_GAMEPORT; 419 input_dev->id.vendor = GAMEPORT_ID_VENDOR_LOGITECH; 420 input_dev->id.product = adi->id; 421 input_dev->id.version = 0x0100; 422 input_dev->dev.parent = &port->gameport->dev; 423 424 input_set_drvdata(input_dev, port); 425 426 input_dev->open = adi_open; 427 input_dev->close = adi_close; 428 429 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); 430 431 for (i = 0; i < adi->axes10 + adi->axes8 + (adi->hats + (adi->pad != -1)) * 2; i++) 432 set_bit(adi->abs[i], input_dev->absbit); 433 434 for (i = 0; i < adi->buttons; i++) 435 set_bit(adi->key[i], input_dev->keybit); 436 437 return 0; 438 } 439 440 static void adi_init_center(struct adi *adi) 441 { 442 int i, t, x; 443 444 if (!adi->length) 445 return; 446 447 for (i = 0; i < adi->axes10 + adi->axes8 + (adi->hats + (adi->pad != -1)) * 2; i++) { 448 449 t = adi->abs[i]; 450 x = input_abs_get_val(adi->dev, t); 451 452 if (t == ABS_THROTTLE || t == ABS_RUDDER || adi->id == ADI_ID_WGPE) 453 x = i < adi->axes10 ? 512 : 128; 454 455 if (i < adi->axes10) 456 input_set_abs_params(adi->dev, t, 64, x * 2 - 64, 2, 16); 457 else if (i < adi->axes10 + adi->axes8) 458 input_set_abs_params(adi->dev, t, 48, x * 2 - 48, 1, 16); 459 else 460 input_set_abs_params(adi->dev, t, -1, 1, 0, 0); 461 } 462 } 463 464 /* 465 * adi_connect() probes for Logitech ADI joysticks. 466 */ 467 468 static int adi_connect(struct gameport *gameport, struct gameport_driver *drv) 469 { 470 struct adi_port *port; 471 int i; 472 int err; 473 474 port = kzalloc(sizeof(struct adi_port), GFP_KERNEL); 475 if (!port) 476 return -ENOMEM; 477 478 port->gameport = gameport; 479 480 gameport_set_drvdata(gameport, port); 481 482 err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW); 483 if (err) 484 goto fail1; 485 486 adi_init_digital(gameport); 487 adi_read_packet(port); 488 489 if (port->adi[0].ret >= ADI_MIN_LEN_LENGTH) 490 adi_move_bits(port, adi_get_bits(port->adi, 10)); 491 492 for (i = 0; i < 2; i++) { 493 adi_id_decode(port->adi + i, port); 494 495 if (!port->adi[i].length) 496 continue; 497 498 err = adi_init_input(port->adi + i, port, i); 499 if (err) 500 goto fail2; 501 } 502 503 if (!port->adi[0].length && !port->adi[1].length) { 504 err = -ENODEV; 505 goto fail2; 506 } 507 508 gameport_set_poll_handler(gameport, adi_poll); 509 gameport_set_poll_interval(gameport, 20); 510 511 msleep(ADI_INIT_DELAY); 512 if (adi_read(port)) { 513 msleep(ADI_DATA_DELAY); 514 adi_read(port); 515 } 516 517 for (i = 0; i < 2; i++) 518 if (port->adi[i].length > 0) { 519 adi_init_center(port->adi + i); 520 err = input_register_device(port->adi[i].dev); 521 if (err) 522 goto fail3; 523 } 524 525 return 0; 526 527 fail3: while (--i >= 0) { 528 if (port->adi[i].length > 0) { 529 input_unregister_device(port->adi[i].dev); 530 port->adi[i].dev = NULL; 531 } 532 } 533 fail2: for (i = 0; i < 2; i++) 534 input_free_device(port->adi[i].dev); 535 gameport_close(gameport); 536 fail1: gameport_set_drvdata(gameport, NULL); 537 kfree(port); 538 return err; 539 } 540 541 static void adi_disconnect(struct gameport *gameport) 542 { 543 int i; 544 struct adi_port *port = gameport_get_drvdata(gameport); 545 546 for (i = 0; i < 2; i++) 547 if (port->adi[i].length > 0) 548 input_unregister_device(port->adi[i].dev); 549 gameport_close(gameport); 550 gameport_set_drvdata(gameport, NULL); 551 kfree(port); 552 } 553 554 static struct gameport_driver adi_drv = { 555 .driver = { 556 .name = "adi", 557 }, 558 .description = DRIVER_DESC, 559 .connect = adi_connect, 560 .disconnect = adi_disconnect, 561 }; 562 563 module_gameport_driver(adi_drv); 564