guillemot.c 7.4 KB

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