adi.c 14 KB

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