grip.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * Copyright (c) 1998-2001 Vojtech Pavlik
  3. */
  4. /*
  5. * Gravis/Kensington GrIP protocol joystick and gamepad 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/module.h>
  28. #include <linux/init.h>
  29. #include <linux/slab.h>
  30. #include <linux/gameport.h>
  31. #include <linux/input.h>
  32. #include <linux/jiffies.h>
  33. #define DRIVER_DESC "Gravis GrIP protocol joystick driver"
  34. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  35. MODULE_DESCRIPTION(DRIVER_DESC);
  36. MODULE_LICENSE("GPL");
  37. #define GRIP_MODE_GPP 1
  38. #define GRIP_MODE_BD 2
  39. #define GRIP_MODE_XT 3
  40. #define GRIP_MODE_DC 4
  41. #define GRIP_LENGTH_GPP 24
  42. #define GRIP_STROBE_GPP 200 /* 200 us */
  43. #define GRIP_LENGTH_XT 4
  44. #define GRIP_STROBE_XT 64 /* 64 us */
  45. #define GRIP_MAX_CHUNKS_XT 10
  46. #define GRIP_MAX_BITS_XT 30
  47. struct grip {
  48. struct gameport *gameport;
  49. struct input_dev *dev[2];
  50. unsigned char mode[2];
  51. int reads;
  52. int bads;
  53. char phys[2][32];
  54. };
  55. static int grip_btn_gpp[] = { BTN_START, BTN_SELECT, BTN_TR2, BTN_Y, 0, BTN_TL2, BTN_A, BTN_B, BTN_X, 0, BTN_TL, BTN_TR, -1 };
  56. static int grip_btn_bd[] = { BTN_THUMB, BTN_THUMB2, BTN_TRIGGER, BTN_TOP, BTN_BASE, -1 };
  57. static int grip_btn_xt[] = { BTN_TRIGGER, BTN_THUMB, BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_SELECT, BTN_START, BTN_MODE, -1 };
  58. static int grip_btn_dc[] = { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_BASE5, -1 };
  59. static int grip_abs_gpp[] = { ABS_X, ABS_Y, -1 };
  60. static int grip_abs_bd[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, -1 };
  61. static int grip_abs_xt[] = { ABS_X, ABS_Y, ABS_BRAKE, ABS_GAS, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, -1 };
  62. static int grip_abs_dc[] = { ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, -1 };
  63. static char *grip_name[] = { NULL, "Gravis GamePad Pro", "Gravis Blackhawk Digital",
  64. "Gravis Xterminator Digital", "Gravis Xterminator DualControl" };
  65. static int *grip_abs[] = { NULL, grip_abs_gpp, grip_abs_bd, grip_abs_xt, grip_abs_dc };
  66. static int *grip_btn[] = { NULL, grip_btn_gpp, grip_btn_bd, grip_btn_xt, grip_btn_dc };
  67. static char grip_anx[] = { 0, 0, 3, 5, 5 };
  68. static char grip_cen[] = { 0, 0, 2, 2, 4 };
  69. /*
  70. * grip_gpp_read_packet() reads a Gravis GamePad Pro packet.
  71. */
  72. static int grip_gpp_read_packet(struct gameport *gameport, int shift, unsigned int *data)
  73. {
  74. unsigned long flags;
  75. unsigned char u, v;
  76. unsigned int t;
  77. int i;
  78. int strobe = gameport_time(gameport, GRIP_STROBE_GPP);
  79. data[0] = 0;
  80. t = strobe;
  81. i = 0;
  82. local_irq_save(flags);
  83. v = gameport_read(gameport) >> shift;
  84. do {
  85. t--;
  86. u = v; v = (gameport_read(gameport) >> shift) & 3;
  87. if (~v & u & 1) {
  88. data[0] |= (v >> 1) << i++;
  89. t = strobe;
  90. }
  91. } while (i < GRIP_LENGTH_GPP && t > 0);
  92. local_irq_restore(flags);
  93. if (i < GRIP_LENGTH_GPP) return -1;
  94. for (i = 0; i < GRIP_LENGTH_GPP && (data[0] & 0xfe4210) ^ 0x7c0000; i++)
  95. data[0] = data[0] >> 1 | (data[0] & 1) << (GRIP_LENGTH_GPP - 1);
  96. return -(i == GRIP_LENGTH_GPP);
  97. }
  98. /*
  99. * grip_xt_read_packet() reads a Gravis Xterminator packet.
  100. */
  101. static int grip_xt_read_packet(struct gameport *gameport, int shift, unsigned int *data)
  102. {
  103. unsigned int i, j, buf, crc;
  104. unsigned char u, v, w;
  105. unsigned long flags;
  106. unsigned int t;
  107. char status;
  108. int strobe = gameport_time(gameport, GRIP_STROBE_XT);
  109. data[0] = data[1] = data[2] = data[3] = 0;
  110. status = buf = i = j = 0;
  111. t = strobe;
  112. local_irq_save(flags);
  113. v = w = (gameport_read(gameport) >> shift) & 3;
  114. do {
  115. t--;
  116. u = (gameport_read(gameport) >> shift) & 3;
  117. if (u ^ v) {
  118. if ((u ^ v) & 1) {
  119. buf = (buf << 1) | (u >> 1);
  120. t = strobe;
  121. i++;
  122. } else
  123. if ((((u ^ v) & (v ^ w)) >> 1) & ~(u | v | w) & 1) {
  124. if (i == 20) {
  125. crc = buf ^ (buf >> 7) ^ (buf >> 14);
  126. if (!((crc ^ (0x25cb9e70 >> ((crc >> 2) & 0x1c))) & 0xf)) {
  127. data[buf >> 18] = buf >> 4;
  128. status |= 1 << (buf >> 18);
  129. }
  130. j++;
  131. }
  132. t = strobe;
  133. buf = 0;
  134. i = 0;
  135. }
  136. w = v;
  137. v = u;
  138. }
  139. } while (status != 0xf && i < GRIP_MAX_BITS_XT && j < GRIP_MAX_CHUNKS_XT && t > 0);
  140. local_irq_restore(flags);
  141. return -(status != 0xf);
  142. }
  143. /*
  144. * grip_timer() repeatedly polls the joysticks and generates events.
  145. */
  146. static void grip_poll(struct gameport *gameport)
  147. {
  148. struct grip *grip = gameport_get_drvdata(gameport);
  149. unsigned int data[GRIP_LENGTH_XT];
  150. struct input_dev *dev;
  151. int i, j;
  152. for (i = 0; i < 2; i++) {
  153. dev = grip->dev[i];
  154. if (!dev)
  155. continue;
  156. grip->reads++;
  157. switch (grip->mode[i]) {
  158. case GRIP_MODE_GPP:
  159. if (grip_gpp_read_packet(grip->gameport, (i << 1) + 4, data)) {
  160. grip->bads++;
  161. break;
  162. }
  163. input_report_abs(dev, ABS_X, ((*data >> 15) & 1) - ((*data >> 16) & 1));
  164. input_report_abs(dev, ABS_Y, ((*data >> 13) & 1) - ((*data >> 12) & 1));
  165. for (j = 0; j < 12; j++)
  166. if (grip_btn_gpp[j])
  167. input_report_key(dev, grip_btn_gpp[j], (*data >> j) & 1);
  168. break;
  169. case GRIP_MODE_BD:
  170. if (grip_xt_read_packet(grip->gameport, (i << 1) + 4, data)) {
  171. grip->bads++;
  172. break;
  173. }
  174. input_report_abs(dev, ABS_X, (data[0] >> 2) & 0x3f);
  175. input_report_abs(dev, ABS_Y, 63 - ((data[0] >> 8) & 0x3f));
  176. input_report_abs(dev, ABS_THROTTLE, (data[2] >> 8) & 0x3f);
  177. input_report_abs(dev, ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2] & 1));
  178. input_report_abs(dev, ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1));
  179. for (j = 0; j < 5; j++)
  180. input_report_key(dev, grip_btn_bd[j], (data[3] >> (j + 4)) & 1);
  181. break;
  182. case GRIP_MODE_XT:
  183. if (grip_xt_read_packet(grip->gameport, (i << 1) + 4, data)) {
  184. grip->bads++;
  185. break;
  186. }
  187. input_report_abs(dev, ABS_X, (data[0] >> 2) & 0x3f);
  188. input_report_abs(dev, ABS_Y, 63 - ((data[0] >> 8) & 0x3f));
  189. input_report_abs(dev, ABS_BRAKE, (data[1] >> 2) & 0x3f);
  190. input_report_abs(dev, ABS_GAS, (data[1] >> 8) & 0x3f);
  191. input_report_abs(dev, ABS_THROTTLE, (data[2] >> 8) & 0x3f);
  192. input_report_abs(dev, ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2] & 1));
  193. input_report_abs(dev, ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1));
  194. input_report_abs(dev, ABS_HAT1X, ((data[2] >> 5) & 1) - ((data[2] >> 4) & 1));
  195. input_report_abs(dev, ABS_HAT1Y, ((data[2] >> 6) & 1) - ((data[2] >> 7) & 1));
  196. for (j = 0; j < 11; j++)
  197. input_report_key(dev, grip_btn_xt[j], (data[3] >> (j + 3)) & 1);
  198. break;
  199. case GRIP_MODE_DC:
  200. if (grip_xt_read_packet(grip->gameport, (i << 1) + 4, data)) {
  201. grip->bads++;
  202. break;
  203. }
  204. input_report_abs(dev, ABS_X, (data[0] >> 2) & 0x3f);
  205. input_report_abs(dev, ABS_Y, (data[0] >> 8) & 0x3f);
  206. input_report_abs(dev, ABS_RX, (data[1] >> 2) & 0x3f);
  207. input_report_abs(dev, ABS_RY, (data[1] >> 8) & 0x3f);
  208. input_report_abs(dev, ABS_THROTTLE, (data[2] >> 8) & 0x3f);
  209. input_report_abs(dev, ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2] & 1));
  210. input_report_abs(dev, ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1));
  211. for (j = 0; j < 9; j++)
  212. input_report_key(dev, grip_btn_dc[j], (data[3] >> (j + 3)) & 1);
  213. break;
  214. }
  215. input_sync(dev);
  216. }
  217. }
  218. static int grip_open(struct input_dev *dev)
  219. {
  220. struct grip *grip = input_get_drvdata(dev);
  221. gameport_start_polling(grip->gameport);
  222. return 0;
  223. }
  224. static void grip_close(struct input_dev *dev)
  225. {
  226. struct grip *grip = input_get_drvdata(dev);
  227. gameport_stop_polling(grip->gameport);
  228. }
  229. static int grip_connect(struct gameport *gameport, struct gameport_driver *drv)
  230. {
  231. struct grip *grip;
  232. struct input_dev *input_dev;
  233. unsigned int data[GRIP_LENGTH_XT];
  234. int i, j, t;
  235. int err;
  236. if (!(grip = kzalloc(sizeof(struct grip), GFP_KERNEL)))
  237. return -ENOMEM;
  238. grip->gameport = gameport;
  239. gameport_set_drvdata(gameport, grip);
  240. err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
  241. if (err)
  242. goto fail1;
  243. for (i = 0; i < 2; i++) {
  244. if (!grip_gpp_read_packet(gameport, (i << 1) + 4, data)) {
  245. grip->mode[i] = GRIP_MODE_GPP;
  246. continue;
  247. }
  248. if (!grip_xt_read_packet(gameport, (i << 1) + 4, data)) {
  249. if (!(data[3] & 7)) {
  250. grip->mode[i] = GRIP_MODE_BD;
  251. continue;
  252. }
  253. if (!(data[2] & 0xf0)) {
  254. grip->mode[i] = GRIP_MODE_XT;
  255. continue;
  256. }
  257. grip->mode[i] = GRIP_MODE_DC;
  258. continue;
  259. }
  260. }
  261. if (!grip->mode[0] && !grip->mode[1]) {
  262. err = -ENODEV;
  263. goto fail2;
  264. }
  265. gameport_set_poll_handler(gameport, grip_poll);
  266. gameport_set_poll_interval(gameport, 20);
  267. for (i = 0; i < 2; i++) {
  268. if (!grip->mode[i])
  269. continue;
  270. grip->dev[i] = input_dev = input_allocate_device();
  271. if (!input_dev) {
  272. err = -ENOMEM;
  273. goto fail3;
  274. }
  275. snprintf(grip->phys[i], sizeof(grip->phys[i]),
  276. "%s/input%d", gameport->phys, i);
  277. input_dev->name = grip_name[grip->mode[i]];
  278. input_dev->phys = grip->phys[i];
  279. input_dev->id.bustype = BUS_GAMEPORT;
  280. input_dev->id.vendor = GAMEPORT_ID_VENDOR_GRAVIS;
  281. input_dev->id.product = grip->mode[i];
  282. input_dev->id.version = 0x0100;
  283. input_dev->dev.parent = &gameport->dev;
  284. input_set_drvdata(input_dev, grip);
  285. input_dev->open = grip_open;
  286. input_dev->close = grip_close;
  287. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  288. for (j = 0; (t = grip_abs[grip->mode[i]][j]) >= 0; j++) {
  289. if (j < grip_cen[grip->mode[i]])
  290. input_set_abs_params(input_dev, t, 14, 52, 1, 2);
  291. else if (j < grip_anx[grip->mode[i]])
  292. input_set_abs_params(input_dev, t, 3, 57, 1, 0);
  293. else
  294. input_set_abs_params(input_dev, t, -1, 1, 0, 0);
  295. }
  296. for (j = 0; (t = grip_btn[grip->mode[i]][j]) >= 0; j++)
  297. if (t > 0)
  298. set_bit(t, input_dev->keybit);
  299. err = input_register_device(grip->dev[i]);
  300. if (err)
  301. goto fail4;
  302. }
  303. return 0;
  304. fail4: input_free_device(grip->dev[i]);
  305. fail3: while (--i >= 0)
  306. if (grip->dev[i])
  307. input_unregister_device(grip->dev[i]);
  308. fail2: gameport_close(gameport);
  309. fail1: gameport_set_drvdata(gameport, NULL);
  310. kfree(grip);
  311. return err;
  312. }
  313. static void grip_disconnect(struct gameport *gameport)
  314. {
  315. struct grip *grip = gameport_get_drvdata(gameport);
  316. int i;
  317. for (i = 0; i < 2; i++)
  318. if (grip->dev[i])
  319. input_unregister_device(grip->dev[i]);
  320. gameport_close(gameport);
  321. gameport_set_drvdata(gameport, NULL);
  322. kfree(grip);
  323. }
  324. static struct gameport_driver grip_drv = {
  325. .driver = {
  326. .name = "grip",
  327. .owner = THIS_MODULE,
  328. },
  329. .description = DRIVER_DESC,
  330. .connect = grip_connect,
  331. .disconnect = grip_disconnect,
  332. };
  333. static int __init grip_init(void)
  334. {
  335. return gameport_register_driver(&grip_drv);
  336. }
  337. static void __exit grip_exit(void)
  338. {
  339. gameport_unregister_driver(&grip_drv);
  340. }
  341. module_init(grip_init);
  342. module_exit(grip_exit);