gf2k.c 9.4 KB

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