guillemot.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Copyright (c) 2001 Vojtech Pavlik
  3. */
  4. /*
  5. * Guillemot Digital Interface Protocol 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/kernel.h>
  27. #include <linux/slab.h>
  28. #include <linux/module.h>
  29. #include <linux/delay.h>
  30. #include <linux/init.h>
  31. #include <linux/gameport.h>
  32. #include <linux/input.h>
  33. #include <linux/jiffies.h>
  34. #define DRIVER_DESC "Guillemot Digital joystick driver"
  35. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  36. MODULE_DESCRIPTION(DRIVER_DESC);
  37. MODULE_LICENSE("GPL");
  38. #define GUILLEMOT_MAX_START 600 /* 600 us */
  39. #define GUILLEMOT_MAX_STROBE 60 /* 60 us */
  40. #define GUILLEMOT_MAX_LENGTH 17 /* 17 bytes */
  41. static short guillemot_abs_pad[] =
  42. { ABS_X, ABS_Y, ABS_THROTTLE, ABS_RUDDER, -1 };
  43. static short guillemot_btn_pad[] =
  44. { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_MODE, BTN_SELECT, -1 };
  45. static struct {
  46. int x;
  47. int y;
  48. } guillemot_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
  49. struct guillemot_type {
  50. unsigned char id;
  51. short *abs;
  52. short *btn;
  53. int hat;
  54. char *name;
  55. };
  56. struct guillemot {
  57. struct gameport *gameport;
  58. struct input_dev *dev;
  59. int bads;
  60. int reads;
  61. struct guillemot_type *type;
  62. unsigned char length;
  63. char phys[32];
  64. };
  65. static struct guillemot_type guillemot_type[] = {
  66. { 0x00, guillemot_abs_pad, guillemot_btn_pad, 1, "Guillemot Pad" },
  67. { 0 }};
  68. /*
  69. * guillemot_read_packet() reads Guillemot joystick data.
  70. */
  71. static int guillemot_read_packet(struct gameport *gameport, u8 *data)
  72. {
  73. unsigned long flags;
  74. unsigned char u, v;
  75. unsigned int t, s;
  76. int i;
  77. for (i = 0; i < GUILLEMOT_MAX_LENGTH; i++)
  78. data[i] = 0;
  79. i = 0;
  80. t = gameport_time(gameport, GUILLEMOT_MAX_START);
  81. s = gameport_time(gameport, GUILLEMOT_MAX_STROBE);
  82. local_irq_save(flags);
  83. gameport_trigger(gameport);
  84. v = gameport_read(gameport);
  85. while (t > 0 && i < GUILLEMOT_MAX_LENGTH * 8) {
  86. t--;
  87. u = v; v = gameport_read(gameport);
  88. if (v & ~u & 0x10) {
  89. data[i >> 3] |= ((v >> 5) & 1) << (i & 7);
  90. i++;
  91. t = s;
  92. }
  93. }
  94. local_irq_restore(flags);
  95. return i;
  96. }
  97. /*
  98. * guillemot_poll() reads and analyzes Guillemot joystick data.
  99. */
  100. static void guillemot_poll(struct gameport *gameport)
  101. {
  102. struct guillemot *guillemot = gameport_get_drvdata(gameport);
  103. struct input_dev *dev = guillemot->dev;
  104. u8 data[GUILLEMOT_MAX_LENGTH];
  105. int i;
  106. guillemot->reads++;
  107. if (guillemot_read_packet(guillemot->gameport, data) != GUILLEMOT_MAX_LENGTH * 8 ||
  108. data[0] != 0x55 || data[16] != 0xaa) {
  109. guillemot->bads++;
  110. } else {
  111. for (i = 0; i < 6 && guillemot->type->abs[i] >= 0; i++)
  112. input_report_abs(dev, guillemot->type->abs[i], data[i + 5]);
  113. if (guillemot->type->hat) {
  114. input_report_abs(dev, ABS_HAT0X, guillemot_hat_to_axis[data[4] >> 4].x);
  115. input_report_abs(dev, ABS_HAT0Y, guillemot_hat_to_axis[data[4] >> 4].y);
  116. }
  117. for (i = 0; i < 16 && guillemot->type->btn[i] >= 0; i++)
  118. input_report_key(dev, guillemot->type->btn[i], (data[2 + (i >> 3)] >> (i & 7)) & 1);
  119. }
  120. input_sync(dev);
  121. }
  122. /*
  123. * guillemot_open() is a callback from the input open routine.
  124. */
  125. static int guillemot_open(struct input_dev *dev)
  126. {
  127. struct guillemot *guillemot = input_get_drvdata(dev);
  128. gameport_start_polling(guillemot->gameport);
  129. return 0;
  130. }
  131. /*
  132. * guillemot_close() is a callback from the input close routine.
  133. */
  134. static void guillemot_close(struct input_dev *dev)
  135. {
  136. struct guillemot *guillemot = input_get_drvdata(dev);
  137. gameport_stop_polling(guillemot->gameport);
  138. }
  139. /*
  140. * guillemot_connect() probes for Guillemot joysticks.
  141. */
  142. static int guillemot_connect(struct gameport *gameport, struct gameport_driver *drv)
  143. {
  144. struct guillemot *guillemot;
  145. struct input_dev *input_dev;
  146. u8 data[GUILLEMOT_MAX_LENGTH];
  147. int i, t;
  148. int err;
  149. guillemot = kzalloc(sizeof(struct guillemot), GFP_KERNEL);
  150. input_dev = input_allocate_device();
  151. if (!guillemot || !input_dev) {
  152. err = -ENOMEM;
  153. goto fail1;
  154. }
  155. guillemot->gameport = gameport;
  156. guillemot->dev = input_dev;
  157. gameport_set_drvdata(gameport, guillemot);
  158. err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
  159. if (err)
  160. goto fail1;
  161. i = guillemot_read_packet(gameport, data);
  162. if (i != GUILLEMOT_MAX_LENGTH * 8 || data[0] != 0x55 || data[16] != 0xaa) {
  163. err = -ENODEV;
  164. goto fail2;
  165. }
  166. for (i = 0; guillemot_type[i].name; i++)
  167. if (guillemot_type[i].id == data[11])
  168. break;
  169. if (!guillemot_type[i].name) {
  170. printk(KERN_WARNING "guillemot.c: Unknown joystick on %s. [ %02x%02x:%04x, ver %d.%02d ]\n",
  171. gameport->phys, data[12], data[13], data[11], data[14], data[15]);
  172. err = -ENODEV;
  173. goto fail2;
  174. }
  175. gameport_set_poll_handler(gameport, guillemot_poll);
  176. gameport_set_poll_interval(gameport, 20);
  177. snprintf(guillemot->phys, sizeof(guillemot->phys), "%s/input0", gameport->phys);
  178. guillemot->type = guillemot_type + i;
  179. input_dev->name = guillemot_type[i].name;
  180. input_dev->phys = guillemot->phys;
  181. input_dev->id.bustype = BUS_GAMEPORT;
  182. input_dev->id.vendor = GAMEPORT_ID_VENDOR_GUILLEMOT;
  183. input_dev->id.product = guillemot_type[i].id;
  184. input_dev->id.version = (int)data[14] << 8 | data[15];
  185. input_dev->dev.parent = &gameport->dev;
  186. input_set_drvdata(input_dev, guillemot);
  187. input_dev->open = guillemot_open;
  188. input_dev->close = guillemot_close;
  189. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  190. for (i = 0; (t = guillemot->type->abs[i]) >= 0; i++)
  191. input_set_abs_params(input_dev, t, 0, 255, 0, 0);
  192. if (guillemot->type->hat) {
  193. input_set_abs_params(input_dev, ABS_HAT0X, -1, 1, 0, 0);
  194. input_set_abs_params(input_dev, ABS_HAT0Y, -1, 1, 0, 0);
  195. }
  196. for (i = 0; (t = guillemot->type->btn[i]) >= 0; i++)
  197. set_bit(t, input_dev->keybit);
  198. err = input_register_device(guillemot->dev);
  199. if (err)
  200. goto fail2;
  201. return 0;
  202. fail2: gameport_close(gameport);
  203. fail1: gameport_set_drvdata(gameport, NULL);
  204. input_free_device(input_dev);
  205. kfree(guillemot);
  206. return err;
  207. }
  208. static void guillemot_disconnect(struct gameport *gameport)
  209. {
  210. struct guillemot *guillemot = gameport_get_drvdata(gameport);
  211. printk(KERN_INFO "guillemot.c: Failed %d reads out of %d on %s\n", guillemot->reads, guillemot->bads, guillemot->phys);
  212. input_unregister_device(guillemot->dev);
  213. gameport_close(gameport);
  214. kfree(guillemot);
  215. }
  216. static struct gameport_driver guillemot_drv = {
  217. .driver = {
  218. .name = "guillemot",
  219. },
  220. .description = DRIVER_DESC,
  221. .connect = guillemot_connect,
  222. .disconnect = guillemot_disconnect,
  223. };
  224. static int __init guillemot_init(void)
  225. {
  226. return gameport_register_driver(&guillemot_drv);
  227. }
  228. static void __exit guillemot_exit(void)
  229. {
  230. gameport_unregister_driver(&guillemot_drv);
  231. }
  232. module_init(guillemot_init);
  233. module_exit(guillemot_exit);