gf2k.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * Copyright (c) 1998-2001 Vojtech Pavlik
  3. */
  4. /*
  5. * Genius Flight 2000 joystick 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/delay.h>
  27. #include <linux/kernel.h>
  28. #include <linux/slab.h>
  29. #include <linux/module.h>
  30. #include <linux/init.h>
  31. #include <linux/input.h>
  32. #include <linux/gameport.h>
  33. #include <linux/jiffies.h>
  34. #define DRIVER_DESC "Genius Flight 2000 joystick driver"
  35. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  36. MODULE_DESCRIPTION(DRIVER_DESC);
  37. MODULE_LICENSE("GPL");
  38. #define GF2K_START 400 /* The time we wait for the first bit [400 us] */
  39. #define GF2K_STROBE 40 /* The time we wait for the first bit [40 us] */
  40. #define GF2K_TIMEOUT 4 /* Wait for everything to settle [4 ms] */
  41. #define GF2K_LENGTH 80 /* Max number of triplets in a packet */
  42. /*
  43. * Genius joystick ids ...
  44. */
  45. #define GF2K_ID_G09 1
  46. #define GF2K_ID_F30D 2
  47. #define GF2K_ID_F30 3
  48. #define GF2K_ID_F31D 4
  49. #define GF2K_ID_F305 5
  50. #define GF2K_ID_F23P 6
  51. #define GF2K_ID_F31 7
  52. #define GF2K_ID_MAX 7
  53. static char gf2k_length[] = { 40, 40, 40, 40, 40, 40, 40, 40 };
  54. static char gf2k_hat_to_axis[][2] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
  55. static char *gf2k_names[] = {"", "Genius G-09D", "Genius F-30D", "Genius F-30", "Genius MaxFighter F-31D",
  56. "Genius F-30-5", "Genius Flight2000 F-23", "Genius F-31"};
  57. static unsigned char gf2k_hats[] = { 0, 2, 0, 0, 2, 0, 2, 0 };
  58. static unsigned char gf2k_axes[] = { 0, 2, 0, 0, 4, 0, 4, 0 };
  59. static unsigned char gf2k_joys[] = { 0, 0, 0, 0,10, 0, 8, 0 };
  60. static unsigned char gf2k_pads[] = { 0, 6, 0, 0, 0, 0, 0, 0 };
  61. static unsigned char gf2k_lens[] = { 0,18, 0, 0,18, 0,18, 0 };
  62. static unsigned char gf2k_abs[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_RUDDER, ABS_GAS, ABS_BRAKE };
  63. static short gf2k_btn_joy[] = { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4 };
  64. static short gf2k_btn_pad[] = { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_TL2, BTN_TR2, BTN_START, BTN_SELECT };
  65. static short gf2k_seq_reset[] = { 240, 340, 0 };
  66. static short gf2k_seq_digital[] = { 590, 320, 860, 0 };
  67. struct gf2k {
  68. struct gameport *gameport;
  69. struct input_dev *dev;
  70. int reads;
  71. int bads;
  72. unsigned char id;
  73. unsigned char length;
  74. char phys[32];
  75. };
  76. /*
  77. * gf2k_read_packet() reads a Genius Flight2000 packet.
  78. */
  79. static int gf2k_read_packet(struct gameport *gameport, int length, char *data)
  80. {
  81. unsigned char u, v;
  82. int i;
  83. unsigned int t, p;
  84. unsigned long flags;
  85. t = gameport_time(gameport, GF2K_START);
  86. p = gameport_time(gameport, GF2K_STROBE);
  87. i = 0;
  88. local_irq_save(flags);
  89. gameport_trigger(gameport);
  90. v = gameport_read(gameport);
  91. while (t > 0 && i < length) {
  92. t--; u = v;
  93. v = gameport_read(gameport);
  94. if (v & ~u & 0x10) {
  95. data[i++] = v >> 5;
  96. t = p;
  97. }
  98. }
  99. local_irq_restore(flags);
  100. return i;
  101. }
  102. /*
  103. * gf2k_trigger_seq() initializes a Genius Flight2000 joystick
  104. * into digital mode.
  105. */
  106. static void gf2k_trigger_seq(struct gameport *gameport, short *seq)
  107. {
  108. unsigned long flags;
  109. int i, t;
  110. local_irq_save(flags);
  111. i = 0;
  112. do {
  113. gameport_trigger(gameport);
  114. t = gameport_time(gameport, GF2K_TIMEOUT * 1000);
  115. while ((gameport_read(gameport) & 1) && t) t--;
  116. udelay(seq[i]);
  117. } while (seq[++i]);
  118. gameport_trigger(gameport);
  119. local_irq_restore(flags);
  120. }
  121. /*
  122. * js_sw_get_bits() composes bits from the triplet buffer into a __u64.
  123. * Parameter 'pos' is bit number inside packet where to start at, 'num' is number
  124. * of bits to be read, 'shift' is offset in the resulting __u64 to start at, bits
  125. * is number of bits per triplet.
  126. */
  127. #define GB(p,n,s) gf2k_get_bits(data, p, n, s)
  128. static int gf2k_get_bits(unsigned char *buf, int pos, int num, int shift)
  129. {
  130. __u64 data = 0;
  131. int i;
  132. for (i = 0; i < num / 3 + 2; i++)
  133. data |= buf[pos / 3 + i] << (i * 3);
  134. data >>= pos % 3;
  135. data &= (1 << num) - 1;
  136. data <<= shift;
  137. return data;
  138. }
  139. static void gf2k_read(struct gf2k *gf2k, unsigned char *data)
  140. {
  141. struct input_dev *dev = gf2k->dev;
  142. int i, t;
  143. for (i = 0; i < 4 && i < gf2k_axes[gf2k->id]; i++)
  144. input_report_abs(dev, gf2k_abs[i], GB(i<<3,8,0) | GB(i+46,1,8) | GB(i+50,1,9));
  145. for (i = 0; i < 2 && i < gf2k_axes[gf2k->id] - 4; i++)
  146. input_report_abs(dev, gf2k_abs[i], GB(i*9+60,8,0) | GB(i+54,1,9));
  147. t = GB(40,4,0);
  148. for (i = 0; i < gf2k_hats[gf2k->id]; i++)
  149. input_report_abs(dev, ABS_HAT0X + i, gf2k_hat_to_axis[t][i]);
  150. t = GB(44,2,0) | GB(32,8,2) | GB(78,2,10);
  151. for (i = 0; i < gf2k_joys[gf2k->id]; i++)
  152. input_report_key(dev, gf2k_btn_joy[i], (t >> i) & 1);
  153. for (i = 0; i < gf2k_pads[gf2k->id]; i++)
  154. input_report_key(dev, gf2k_btn_pad[i], (t >> i) & 1);
  155. input_sync(dev);
  156. }
  157. /*
  158. * gf2k_poll() reads and analyzes Genius joystick data.
  159. */
  160. static void gf2k_poll(struct gameport *gameport)
  161. {
  162. struct gf2k *gf2k = gameport_get_drvdata(gameport);
  163. unsigned char data[GF2K_LENGTH];
  164. gf2k->reads++;
  165. if (gf2k_read_packet(gf2k->gameport, gf2k_length[gf2k->id], data) < gf2k_length[gf2k->id])
  166. gf2k->bads++;
  167. else
  168. gf2k_read(gf2k, data);
  169. }
  170. static int gf2k_open(struct input_dev *dev)
  171. {
  172. struct gf2k *gf2k = input_get_drvdata(dev);
  173. gameport_start_polling(gf2k->gameport);
  174. return 0;
  175. }
  176. static void gf2k_close(struct input_dev *dev)
  177. {
  178. struct gf2k *gf2k = input_get_drvdata(dev);
  179. gameport_stop_polling(gf2k->gameport);
  180. }
  181. /*
  182. * gf2k_connect() probes for Genius id joysticks.
  183. */
  184. static int gf2k_connect(struct gameport *gameport, struct gameport_driver *drv)
  185. {
  186. struct gf2k *gf2k;
  187. struct input_dev *input_dev;
  188. unsigned char data[GF2K_LENGTH];
  189. int i, err;
  190. gf2k = kzalloc(sizeof(struct gf2k), GFP_KERNEL);
  191. input_dev = input_allocate_device();
  192. if (!gf2k || !input_dev) {
  193. err = -ENOMEM;
  194. goto fail1;
  195. }
  196. gf2k->gameport = gameport;
  197. gf2k->dev = input_dev;
  198. gameport_set_drvdata(gameport, gf2k);
  199. err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
  200. if (err)
  201. goto fail1;
  202. gf2k_trigger_seq(gameport, gf2k_seq_reset);
  203. msleep(GF2K_TIMEOUT);
  204. gf2k_trigger_seq(gameport, gf2k_seq_digital);
  205. msleep(GF2K_TIMEOUT);
  206. if (gf2k_read_packet(gameport, GF2K_LENGTH, data) < 12) {
  207. err = -ENODEV;
  208. goto fail2;
  209. }
  210. if (!(gf2k->id = GB(7,2,0) | GB(3,3,2) | GB(0,3,5))) {
  211. err = -ENODEV;
  212. goto fail2;
  213. }
  214. #ifdef RESET_WORKS
  215. if ((gf2k->id != (GB(19,2,0) | GB(15,3,2) | GB(12,3,5))) &&
  216. (gf2k->id != (GB(31,2,0) | GB(27,3,2) | GB(24,3,5)))) {
  217. err = -ENODEV;
  218. goto fail2;
  219. }
  220. #else
  221. gf2k->id = 6;
  222. #endif
  223. if (gf2k->id > GF2K_ID_MAX || !gf2k_axes[gf2k->id]) {
  224. printk(KERN_WARNING "gf2k.c: Not yet supported joystick on %s. [id: %d type:%s]\n",
  225. gameport->phys, gf2k->id, gf2k->id > GF2K_ID_MAX ? "Unknown" : gf2k_names[gf2k->id]);
  226. err = -ENODEV;
  227. goto fail2;
  228. }
  229. gameport_set_poll_handler(gameport, gf2k_poll);
  230. gameport_set_poll_interval(gameport, 20);
  231. snprintf(gf2k->phys, sizeof(gf2k->phys), "%s/input0", gameport->phys);
  232. gf2k->length = gf2k_lens[gf2k->id];
  233. input_dev->name = gf2k_names[gf2k->id];
  234. input_dev->phys = gf2k->phys;
  235. input_dev->id.bustype = BUS_GAMEPORT;
  236. input_dev->id.vendor = GAMEPORT_ID_VENDOR_GENIUS;
  237. input_dev->id.product = gf2k->id;
  238. input_dev->id.version = 0x0100;
  239. input_dev->dev.parent = &gameport->dev;
  240. input_set_drvdata(input_dev, gf2k);
  241. input_dev->open = gf2k_open;
  242. input_dev->close = gf2k_close;
  243. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  244. for (i = 0; i < gf2k_axes[gf2k->id]; i++)
  245. set_bit(gf2k_abs[i], input_dev->absbit);
  246. for (i = 0; i < gf2k_hats[gf2k->id]; i++)
  247. input_set_abs_params(input_dev, ABS_HAT0X + i, -1, 1, 0, 0);
  248. for (i = 0; i < gf2k_joys[gf2k->id]; i++)
  249. set_bit(gf2k_btn_joy[i], input_dev->keybit);
  250. for (i = 0; i < gf2k_pads[gf2k->id]; i++)
  251. set_bit(gf2k_btn_pad[i], input_dev->keybit);
  252. gf2k_read_packet(gameport, gf2k->length, data);
  253. gf2k_read(gf2k, data);
  254. for (i = 0; i < gf2k_axes[gf2k->id]; i++) {
  255. int max = i < 2 ?
  256. input_abs_get_val(input_dev, gf2k_abs[i]) * 2 :
  257. input_abs_get_val(input_dev, gf2k_abs[0]) +
  258. input_abs_get_val(input_dev, gf2k_abs[1]);
  259. int flat = i < 2 ? 24 : 0;
  260. input_set_abs_params(input_dev, gf2k_abs[i],
  261. 32, max - 32, 8, flat);
  262. }
  263. err = input_register_device(gf2k->dev);
  264. if (err)
  265. goto fail2;
  266. return 0;
  267. fail2: gameport_close(gameport);
  268. fail1: gameport_set_drvdata(gameport, NULL);
  269. input_free_device(input_dev);
  270. kfree(gf2k);
  271. return err;
  272. }
  273. static void gf2k_disconnect(struct gameport *gameport)
  274. {
  275. struct gf2k *gf2k = gameport_get_drvdata(gameport);
  276. input_unregister_device(gf2k->dev);
  277. gameport_close(gameport);
  278. gameport_set_drvdata(gameport, NULL);
  279. kfree(gf2k);
  280. }
  281. static struct gameport_driver gf2k_drv = {
  282. .driver = {
  283. .name = "gf2k",
  284. },
  285. .description = DRIVER_DESC,
  286. .connect = gf2k_connect,
  287. .disconnect = gf2k_disconnect,
  288. };
  289. static int __init gf2k_init(void)
  290. {
  291. return gameport_register_driver(&gf2k_drv);
  292. }
  293. static void __exit gf2k_exit(void)
  294. {
  295. gameport_unregister_driver(&gf2k_drv);
  296. }
  297. module_init(gf2k_init);
  298. module_exit(gf2k_exit);