PageRenderTime 66ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/input/joystick/grip.c

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