guillemot.c 7.3 KB

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