interact.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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/gameport.h>
  34. #include <linux/input.h>
  35. #include <linux/jiffies.h>
  36. #define DRIVER_DESC "InterAct digital joystick driver"
  37. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  38. MODULE_DESCRIPTION(DRIVER_DESC);
  39. MODULE_LICENSE("GPL");
  40. #define INTERACT_MAX_START 600 /* 400 us */
  41. #define INTERACT_MAX_STROBE 60 /* 40 us */
  42. #define INTERACT_MAX_LENGTH 32 /* 32 bits */
  43. #define INTERACT_TYPE_HHFX 0 /* HammerHead/FX */
  44. #define INTERACT_TYPE_PP8D 1 /* ProPad 8 */
  45. struct interact {
  46. struct gameport *gameport;
  47. struct input_dev *dev;
  48. int bads;
  49. int reads;
  50. unsigned char type;
  51. unsigned char length;
  52. char phys[32];
  53. };
  54. static short interact_abs_hhfx[] =
  55. { ABS_RX, ABS_RY, ABS_X, ABS_Y, ABS_HAT0X, ABS_HAT0Y, -1 };
  56. static short interact_abs_pp8d[] =
  57. { ABS_X, ABS_Y, -1 };
  58. static short interact_btn_hhfx[] =
  59. { 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 };
  60. static short interact_btn_pp8d[] =
  61. { BTN_C, BTN_TL, BTN_TR, BTN_A, BTN_B, BTN_Y, BTN_Z, BTN_X, -1 };
  62. struct interact_type {
  63. int id;
  64. short *abs;
  65. short *btn;
  66. char *name;
  67. unsigned char length;
  68. unsigned char b8;
  69. };
  70. static struct interact_type interact_type[] = {
  71. { 0x6202, interact_abs_hhfx, interact_btn_hhfx, "InterAct HammerHead/FX", 32, 4 },
  72. { 0x53f8, interact_abs_pp8d, interact_btn_pp8d, "InterAct ProPad 8 Digital", 16, 0 },
  73. { 0 }};
  74. /*
  75. * interact_read_packet() reads and InterAct joystick data.
  76. */
  77. static int interact_read_packet(struct gameport *gameport, int length, u32 *data)
  78. {
  79. unsigned long flags;
  80. unsigned char u, v;
  81. unsigned int t, s;
  82. int i;
  83. i = 0;
  84. data[0] = data[1] = data[2] = 0;
  85. t = gameport_time(gameport, INTERACT_MAX_START);
  86. s = gameport_time(gameport, INTERACT_MAX_STROBE);
  87. local_irq_save(flags);
  88. gameport_trigger(gameport);
  89. v = gameport_read(gameport);
  90. while (t > 0 && i < length) {
  91. t--;
  92. u = v; v = gameport_read(gameport);
  93. if (v & ~u & 0x40) {
  94. data[0] = (data[0] << 1) | ((v >> 4) & 1);
  95. data[1] = (data[1] << 1) | ((v >> 5) & 1);
  96. data[2] = (data[2] << 1) | ((v >> 7) & 1);
  97. i++;
  98. t = s;
  99. }
  100. }
  101. local_irq_restore(flags);
  102. return i;
  103. }
  104. /*
  105. * interact_poll() reads and analyzes InterAct joystick data.
  106. */
  107. static void interact_poll(struct gameport *gameport)
  108. {
  109. struct interact *interact = gameport_get_drvdata(gameport);
  110. struct input_dev *dev = interact->dev;
  111. u32 data[3];
  112. int i;
  113. interact->reads++;
  114. if (interact_read_packet(interact->gameport, interact->length, data) < interact->length) {
  115. interact->bads++;
  116. } else {
  117. for (i = 0; i < 3; i++)
  118. data[i] <<= INTERACT_MAX_LENGTH - interact->length;
  119. switch (interact->type) {
  120. case INTERACT_TYPE_HHFX:
  121. for (i = 0; i < 4; i++)
  122. input_report_abs(dev, interact_abs_hhfx[i], (data[i & 1] >> ((i >> 1) << 3)) & 0xff);
  123. for (i = 0; i < 2; i++)
  124. input_report_abs(dev, ABS_HAT0Y - i,
  125. ((data[1] >> ((i << 1) + 17)) & 1) - ((data[1] >> ((i << 1) + 16)) & 1));
  126. for (i = 0; i < 8; i++)
  127. input_report_key(dev, interact_btn_hhfx[i], (data[0] >> (i + 16)) & 1);
  128. for (i = 0; i < 4; i++)
  129. input_report_key(dev, interact_btn_hhfx[i + 8], (data[1] >> (i + 20)) & 1);
  130. break;
  131. case INTERACT_TYPE_PP8D:
  132. for (i = 0; i < 2; i++)
  133. input_report_abs(dev, interact_abs_pp8d[i],
  134. ((data[0] >> ((i << 1) + 20)) & 1) - ((data[0] >> ((i << 1) + 21)) & 1));
  135. for (i = 0; i < 8; i++)
  136. input_report_key(dev, interact_btn_pp8d[i], (data[1] >> (i + 16)) & 1);
  137. break;
  138. }
  139. }
  140. input_sync(dev);
  141. }
  142. /*
  143. * interact_open() is a callback from the input open routine.
  144. */
  145. static int interact_open(struct input_dev *dev)
  146. {
  147. struct interact *interact = input_get_drvdata(dev);
  148. gameport_start_polling(interact->gameport);
  149. return 0;
  150. }
  151. /*
  152. * interact_close() is a callback from the input close routine.
  153. */
  154. static void interact_close(struct input_dev *dev)
  155. {
  156. struct interact *interact = input_get_drvdata(dev);
  157. gameport_stop_polling(interact->gameport);
  158. }
  159. /*
  160. * interact_connect() probes for InterAct joysticks.
  161. */
  162. static int interact_connect(struct gameport *gameport, struct gameport_driver *drv)
  163. {
  164. struct interact *interact;
  165. struct input_dev *input_dev;
  166. __u32 data[3];
  167. int i, t;
  168. int err;
  169. interact = kzalloc(sizeof(struct interact), GFP_KERNEL);
  170. input_dev = input_allocate_device();
  171. if (!interact || !input_dev) {
  172. err = -ENOMEM;
  173. goto fail1;
  174. }
  175. interact->gameport = gameport;
  176. interact->dev = input_dev;
  177. gameport_set_drvdata(gameport, interact);
  178. err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
  179. if (err)
  180. goto fail1;
  181. i = interact_read_packet(gameport, INTERACT_MAX_LENGTH * 2, data);
  182. if (i != 32 || (data[0] >> 24) != 0x0c || (data[1] >> 24) != 0x02) {
  183. err = -ENODEV;
  184. goto fail2;
  185. }
  186. for (i = 0; interact_type[i].length; i++)
  187. if (interact_type[i].id == (data[2] >> 16))
  188. break;
  189. if (!interact_type[i].length) {
  190. printk(KERN_WARNING "interact.c: Unknown joystick on %s. [len %d d0 %08x d1 %08x i2 %08x]\n",
  191. gameport->phys, i, data[0], data[1], data[2]);
  192. err = -ENODEV;
  193. goto fail2;
  194. }
  195. gameport_set_poll_handler(gameport, interact_poll);
  196. gameport_set_poll_interval(gameport, 20);
  197. snprintf(interact->phys, sizeof(interact->phys), "%s/input0", gameport->phys);
  198. interact->type = i;
  199. interact->length = interact_type[i].length;
  200. input_dev->name = interact_type[i].name;
  201. input_dev->phys = interact->phys;
  202. input_dev->id.bustype = BUS_GAMEPORT;
  203. input_dev->id.vendor = GAMEPORT_ID_VENDOR_INTERACT;
  204. input_dev->id.product = interact_type[i].id;
  205. input_dev->id.version = 0x0100;
  206. input_dev->dev.parent = &gameport->dev;
  207. input_set_drvdata(input_dev, interact);
  208. input_dev->open = interact_open;
  209. input_dev->close = interact_close;
  210. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  211. for (i = 0; (t = interact_type[interact->type].abs[i]) >= 0; i++) {
  212. if (i < interact_type[interact->type].b8)
  213. input_set_abs_params(input_dev, t, 0, 255, 0, 0);
  214. else
  215. input_set_abs_params(input_dev, t, -1, 1, 0, 0);
  216. }
  217. for (i = 0; (t = interact_type[interact->type].btn[i]) >= 0; i++)
  218. __set_bit(t, input_dev->keybit);
  219. err = input_register_device(interact->dev);
  220. if (err)
  221. goto fail2;
  222. return 0;
  223. fail2: gameport_close(gameport);
  224. fail1: gameport_set_drvdata(gameport, NULL);
  225. input_free_device(input_dev);
  226. kfree(interact);
  227. return err;
  228. }
  229. static void interact_disconnect(struct gameport *gameport)
  230. {
  231. struct interact *interact = gameport_get_drvdata(gameport);
  232. input_unregister_device(interact->dev);
  233. gameport_close(gameport);
  234. gameport_set_drvdata(gameport, NULL);
  235. kfree(interact);
  236. }
  237. static struct gameport_driver interact_drv = {
  238. .driver = {
  239. .name = "interact",
  240. },
  241. .description = DRIVER_DESC,
  242. .connect = interact_connect,
  243. .disconnect = interact_disconnect,
  244. };
  245. module_gameport_driver(interact_drv);