guillemot.c 7.2 KB

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