grip.c 11 KB

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