interact.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Copyright (c) 2001 Vojtech Pavlik
  3. *
  4. * Based on the work of:
  5. * Toby Deshane
  6. */
  7. /*
  8. * InterAct digital gamepad/joystick driver for Linux
  9. */
  10. /*
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. *
  25. * Should you need to contact me, the author, you can do so either by
  26. * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
  27. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  28. */
  29. #include <linux/kernel.h>
  30. #include <linux/slab.h>
  31. #include <linux/module.h>
  32. #include <linux/delay.h>
  33. #include <linux/init.h>
  34. #include <linux/gameport.h>
  35. #include <linux/input.h>
  36. #include <linux/jiffies.h>
  37. #define DRIVER_DESC "InterAct digital joystick driver"
  38. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  39. MODULE_DESCRIPTION(DRIVER_DESC);
  40. MODULE_LICENSE("GPL");
  41. #define INTERACT_MAX_START 600 /* 400 us */
  42. #define INTERACT_MAX_STROBE 60 /* 40 us */
  43. #define INTERACT_MAX_LENGTH 32 /* 32 bits */
  44. #define INTERACT_TYPE_HHFX 0 /* HammerHead/FX */
  45. #define INTERACT_TYPE_PP8D 1 /* ProPad 8 */
  46. struct interact {
  47. struct gameport *gameport;
  48. struct input_dev *dev;
  49. int bads;
  50. int reads;
  51. unsigned char type;
  52. unsigned char length;
  53. char phys[32];
  54. };
  55. static short interact_abs_hhfx[] =
  56. { ABS_RX, ABS_RY, ABS_X, ABS_Y, ABS_HAT0X, ABS_HAT0Y, -1 };
  57. static short interact_abs_pp8d[] =
  58. { ABS_X, ABS_Y, -1 };
  59. static short interact_btn_hhfx[] =
  60. { BTN_TR, BTN_X, BTN_Y, BTN_Z, BTN_A, BTN_B, BTN_C, BTN_TL, BTN_TL2, BTN_TR2, BTN_MODE, BTN_SELECT, -1 };
  61. static short interact_btn_pp8d[] =
  62. { BTN_C, BTN_TL, BTN_TR, BTN_A, BTN_B, BTN_Y, BTN_Z, BTN_X, -1 };
  63. struct interact_type {
  64. int id;
  65. short *abs;
  66. short *btn;
  67. char *name;
  68. unsigned char length;
  69. unsigned char b8;
  70. };
  71. static struct interact_type interact_type[] = {
  72. { 0x6202, interact_abs_hhfx, interact_btn_hhfx, "InterAct HammerHead/FX", 32, 4 },
  73. { 0x53f8, interact_abs_pp8d, interact_btn_pp8d, "InterAct ProPad 8 Digital", 16, 0 },
  74. { 0 }};
  75. /*
  76. * interact_read_packet() reads and InterAct joystick data.
  77. */
  78. static int interact_read_packet(struct gameport *gameport, int length, u32 *data)
  79. {
  80. unsigned long flags;
  81. unsigned char u, v;
  82. unsigned int t, s;
  83. int i;
  84. i = 0;
  85. data[0] = data[1] = data[2] = 0;
  86. t = gameport_time(gameport, INTERACT_MAX_START);
  87. s = gameport_time(gameport, INTERACT_MAX_STROBE);
  88. local_irq_save(flags);
  89. gameport_trigger(gameport);
  90. v = gameport_read(gameport);
  91. while (t > 0 && i < length) {
  92. t--;
  93. u = v; v = gameport_read(gameport);
  94. if (v & ~u & 0x40) {
  95. data[0] = (data[0] << 1) | ((v >> 4) & 1);
  96. data[1] = (data[1] << 1) | ((v >> 5) & 1);
  97. data[2] = (data[2] << 1) | ((v >> 7) & 1);
  98. i++;
  99. t = s;
  100. }
  101. }
  102. local_irq_restore(flags);
  103. return i;
  104. }
  105. /*
  106. * interact_poll() reads and analyzes InterAct joystick data.
  107. */
  108. static void interact_poll(struct gameport *gameport)
  109. {
  110. struct interact *interact = gameport_get_drvdata(gameport);
  111. struct input_dev *dev = interact->dev;
  112. u32 data[3];
  113. int i;
  114. interact->reads++;
  115. if (interact_read_packet(interact->gameport, interact->length, data) < interact->length) {
  116. interact->bads++;
  117. } else {
  118. for (i = 0; i < 3; i++)
  119. data[i] <<= INTERACT_MAX_LENGTH - interact->length;
  120. switch (interact->type) {
  121. case INTERACT_TYPE_HHFX:
  122. for (i = 0; i < 4; i++)
  123. input_report_abs(dev, interact_abs_hhfx[i], (data[i & 1] >> ((i >> 1) << 3)) & 0xff);
  124. for (i = 0; i < 2; i++)
  125. input_report_abs(dev, ABS_HAT0Y - i,
  126. ((data[1] >> ((i << 1) + 17)) & 1) - ((data[1] >> ((i << 1) + 16)) & 1));
  127. for (i = 0; i < 8; i++)
  128. input_report_key(dev, interact_btn_hhfx[i], (data[0] >> (i + 16)) & 1);
  129. for (i = 0; i < 4; i++)
  130. input_report_key(dev, interact_btn_hhfx[i + 8], (data[1] >> (i + 20)) & 1);
  131. break;
  132. case INTERACT_TYPE_PP8D:
  133. for (i = 0; i < 2; i++)
  134. input_report_abs(dev, interact_abs_pp8d[i],
  135. ((data[0] >> ((i << 1) + 20)) & 1) - ((data[0] >> ((i << 1) + 21)) & 1));
  136. for (i = 0; i < 8; i++)
  137. input_report_key(dev, interact_btn_pp8d[i], (data[1] >> (i + 16)) & 1);
  138. break;
  139. }
  140. }
  141. input_sync(dev);
  142. }
  143. /*
  144. * interact_open() is a callback from the input open routine.
  145. */
  146. static int interact_open(struct input_dev *dev)
  147. {
  148. struct interact *interact = input_get_drvdata(dev);
  149. gameport_start_polling(interact->gameport);
  150. return 0;
  151. }
  152. /*
  153. * interact_close() is a callback from the input close routine.
  154. */
  155. static void interact_close(struct input_dev *dev)
  156. {
  157. struct interact *interact = input_get_drvdata(dev);
  158. gameport_stop_polling(interact->gameport);
  159. }
  160. /*
  161. * interact_connect() probes for InterAct joysticks.
  162. */
  163. static int interact_connect(struct gameport *gameport, struct gameport_driver *drv)
  164. {
  165. struct interact *interact;
  166. struct input_dev *input_dev;
  167. __u32 data[3];
  168. int i, t;
  169. int err;
  170. interact = kzalloc(sizeof(struct interact), GFP_KERNEL);
  171. input_dev = input_allocate_device();
  172. if (!interact || !input_dev) {
  173. err = -ENOMEM;
  174. goto fail1;
  175. }
  176. interact->gameport = gameport;
  177. interact->dev = input_dev;
  178. gameport_set_drvdata(gameport, interact);
  179. err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
  180. if (err)
  181. goto fail1;
  182. i = interact_read_packet(gameport, INTERACT_MAX_LENGTH * 2, data);
  183. if (i != 32 || (data[0] >> 24) != 0x0c || (data[1] >> 24) != 0x02) {
  184. err = -ENODEV;
  185. goto fail2;
  186. }
  187. for (i = 0; interact_type[i].length; i++)
  188. if (interact_type[i].id == (data[2] >> 16))
  189. break;
  190. if (!interact_type[i].length) {
  191. printk(KERN_WARNING "interact.c: Unknown joystick on %s. [len %d d0 %08x d1 %08x i2 %08x]\n",
  192. gameport->phys, i, data[0], data[1], data[2]);
  193. err = -ENODEV;
  194. goto fail2;
  195. }
  196. gameport_set_poll_handler(gameport, interact_poll);
  197. gameport_set_poll_interval(gameport, 20);
  198. snprintf(interact->phys, sizeof(interact->phys), "%s/input0", gameport->phys);
  199. interact->type = i;
  200. interact->length = interact_type[i].length;
  201. input_dev->name = interact_type[i].name;
  202. input_dev->phys = interact->phys;
  203. input_dev->id.bustype = BUS_GAMEPORT;
  204. input_dev->id.vendor = GAMEPORT_ID_VENDOR_INTERACT;
  205. input_dev->id.product = interact_type[i].id;
  206. input_dev->id.version = 0x0100;
  207. input_dev->dev.parent = &gameport->dev;
  208. input_set_drvdata(input_dev, interact);
  209. input_dev->open = interact_open;
  210. input_dev->close = interact_close;
  211. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  212. for (i = 0; (t = interact_type[interact->type].abs[i]) >= 0; i++) {
  213. if (i < interact_type[interact->type].b8)
  214. input_set_abs_params(input_dev, t, 0, 255, 0, 0);
  215. else
  216. input_set_abs_params(input_dev, t, -1, 1, 0, 0);
  217. }
  218. for (i = 0; (t = interact_type[interact->type].btn[i]) >= 0; i++)
  219. __set_bit(t, input_dev->keybit);
  220. err = input_register_device(interact->dev);
  221. if (err)
  222. goto fail2;
  223. return 0;
  224. fail2: gameport_close(gameport);
  225. fail1: gameport_set_drvdata(gameport, NULL);
  226. input_free_device(input_dev);
  227. kfree(interact);
  228. return err;
  229. }
  230. static void interact_disconnect(struct gameport *gameport)
  231. {
  232. struct interact *interact = gameport_get_drvdata(gameport);
  233. input_unregister_device(interact->dev);
  234. gameport_close(gameport);
  235. gameport_set_drvdata(gameport, NULL);
  236. kfree(interact);
  237. }
  238. static struct gameport_driver interact_drv = {
  239. .driver = {
  240. .name = "interact",
  241. },
  242. .description = DRIVER_DESC,
  243. .connect = interact_connect,
  244. .disconnect = interact_disconnect,
  245. };
  246. static int __init interact_init(void)
  247. {
  248. return gameport_register_driver(&interact_drv);
  249. }
  250. static void __exit interact_exit(void)
  251. {
  252. gameport_unregister_driver(&interact_drv);
  253. }
  254. module_init(interact_init);
  255. module_exit(interact_exit);