a3d.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * Copyright (c) 1998-2001 Vojtech Pavlik
  3. */
  4. /*
  5. * FP-Gaming Assassin 3D 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/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/slab.h>
  29. #include <linux/init.h>
  30. #include <linux/gameport.h>
  31. #include <linux/input.h>
  32. #include <linux/jiffies.h>
  33. #define DRIVER_DESC "FP-Gaming Assassin 3D joystick driver"
  34. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  35. MODULE_DESCRIPTION(DRIVER_DESC);
  36. MODULE_LICENSE("GPL");
  37. #define A3D_MAX_START 600 /* 600 us */
  38. #define A3D_MAX_STROBE 80 /* 80 us */
  39. #define A3D_MAX_LENGTH 40 /* 40*3 bits */
  40. #define A3D_MODE_A3D 1 /* Assassin 3D */
  41. #define A3D_MODE_PAN 2 /* Panther */
  42. #define A3D_MODE_OEM 3 /* Panther OEM version */
  43. #define A3D_MODE_PXL 4 /* Panther XL */
  44. static char *a3d_names[] = { NULL, "FP-Gaming Assassin 3D", "MadCatz Panther", "OEM Panther",
  45. "MadCatz Panther XL", "MadCatz Panther XL w/ rudder" };
  46. struct a3d {
  47. struct gameport *gameport;
  48. struct gameport *adc;
  49. struct input_dev *dev;
  50. int axes[4];
  51. int buttons;
  52. int mode;
  53. int length;
  54. int reads;
  55. int bads;
  56. char phys[32];
  57. };
  58. /*
  59. * a3d_read_packet() reads an Assassin 3D packet.
  60. */
  61. static int a3d_read_packet(struct gameport *gameport, int length, char *data)
  62. {
  63. unsigned long flags;
  64. unsigned char u, v;
  65. unsigned int t, s;
  66. int i;
  67. i = 0;
  68. t = gameport_time(gameport, A3D_MAX_START);
  69. s = gameport_time(gameport, A3D_MAX_STROBE);
  70. local_irq_save(flags);
  71. gameport_trigger(gameport);
  72. v = gameport_read(gameport);
  73. while (t > 0 && i < length) {
  74. t--;
  75. u = v; v = gameport_read(gameport);
  76. if (~v & u & 0x10) {
  77. data[i++] = v >> 5;
  78. t = s;
  79. }
  80. }
  81. local_irq_restore(flags);
  82. return i;
  83. }
  84. /*
  85. * a3d_csum() computes checksum of triplet packet
  86. */
  87. static int a3d_csum(char *data, int count)
  88. {
  89. int i, csum = 0;
  90. for (i = 0; i < count - 2; i++)
  91. csum += data[i];
  92. return (csum & 0x3f) != ((data[count - 2] << 3) | data[count - 1]);
  93. }
  94. static void a3d_read(struct a3d *a3d, unsigned char *data)
  95. {
  96. struct input_dev *dev = a3d->dev;
  97. switch (a3d->mode) {
  98. case A3D_MODE_A3D:
  99. case A3D_MODE_OEM:
  100. case A3D_MODE_PAN:
  101. input_report_rel(dev, REL_X, ((data[5] << 6) | (data[6] << 3) | data[ 7]) - ((data[5] & 4) << 7));
  102. input_report_rel(dev, REL_Y, ((data[8] << 6) | (data[9] << 3) | data[10]) - ((data[8] & 4) << 7));
  103. input_report_key(dev, BTN_RIGHT, data[2] & 1);
  104. input_report_key(dev, BTN_LEFT, data[3] & 2);
  105. input_report_key(dev, BTN_MIDDLE, data[3] & 4);
  106. input_sync(dev);
  107. a3d->axes[0] = ((signed char)((data[11] << 6) | (data[12] << 3) | (data[13]))) + 128;
  108. a3d->axes[1] = ((signed char)((data[14] << 6) | (data[15] << 3) | (data[16]))) + 128;
  109. a3d->axes[2] = ((signed char)((data[17] << 6) | (data[18] << 3) | (data[19]))) + 128;
  110. a3d->axes[3] = ((signed char)((data[20] << 6) | (data[21] << 3) | (data[22]))) + 128;
  111. a3d->buttons = ((data[3] << 3) | data[4]) & 0xf;
  112. break;
  113. case A3D_MODE_PXL:
  114. input_report_rel(dev, REL_X, ((data[ 9] << 6) | (data[10] << 3) | data[11]) - ((data[ 9] & 4) << 7));
  115. input_report_rel(dev, REL_Y, ((data[12] << 6) | (data[13] << 3) | data[14]) - ((data[12] & 4) << 7));
  116. input_report_key(dev, BTN_RIGHT, data[2] & 1);
  117. input_report_key(dev, BTN_LEFT, data[3] & 2);
  118. input_report_key(dev, BTN_MIDDLE, data[3] & 4);
  119. input_report_key(dev, BTN_SIDE, data[7] & 2);
  120. input_report_key(dev, BTN_EXTRA, data[7] & 4);
  121. input_report_abs(dev, ABS_X, ((signed char)((data[15] << 6) | (data[16] << 3) | (data[17]))) + 128);
  122. input_report_abs(dev, ABS_Y, ((signed char)((data[18] << 6) | (data[19] << 3) | (data[20]))) + 128);
  123. input_report_abs(dev, ABS_RUDDER, ((signed char)((data[21] << 6) | (data[22] << 3) | (data[23]))) + 128);
  124. input_report_abs(dev, ABS_THROTTLE, ((signed char)((data[24] << 6) | (data[25] << 3) | (data[26]))) + 128);
  125. input_report_abs(dev, ABS_HAT0X, ( data[5] & 1) - ((data[5] >> 2) & 1));
  126. input_report_abs(dev, ABS_HAT0Y, ((data[5] >> 1) & 1) - ((data[6] >> 2) & 1));
  127. input_report_abs(dev, ABS_HAT1X, ((data[4] >> 1) & 1) - ( data[3] & 1));
  128. input_report_abs(dev, ABS_HAT1Y, ((data[4] >> 2) & 1) - ( data[4] & 1));
  129. input_report_key(dev, BTN_TRIGGER, data[8] & 1);
  130. input_report_key(dev, BTN_THUMB, data[8] & 2);
  131. input_report_key(dev, BTN_TOP, data[8] & 4);
  132. input_report_key(dev, BTN_PINKIE, data[7] & 1);
  133. input_sync(dev);
  134. break;
  135. }
  136. }
  137. /*
  138. * a3d_poll() reads and analyzes A3D joystick data.
  139. */
  140. static void a3d_poll(struct gameport *gameport)
  141. {
  142. struct a3d *a3d = gameport_get_drvdata(gameport);
  143. unsigned char data[A3D_MAX_LENGTH];
  144. a3d->reads++;
  145. if (a3d_read_packet(a3d->gameport, a3d->length, data) != a3d->length ||
  146. data[0] != a3d->mode || a3d_csum(data, a3d->length))
  147. a3d->bads++;
  148. else
  149. a3d_read(a3d, data);
  150. }
  151. /*
  152. * a3d_adc_cooked_read() copies the acis and button data to the
  153. * callers arrays. It could do the read itself, but the caller could
  154. * call this more than 50 times a second, which would use too much CPU.
  155. */
  156. static int a3d_adc_cooked_read(struct gameport *gameport, int *axes, int *buttons)
  157. {
  158. struct a3d *a3d = gameport->port_data;
  159. int i;
  160. for (i = 0; i < 4; i++)
  161. axes[i] = (a3d->axes[i] < 254) ? a3d->axes[i] : -1;
  162. *buttons = a3d->buttons;
  163. return 0;
  164. }
  165. /*
  166. * a3d_adc_open() is the gameport open routine. It refuses to serve
  167. * any but cooked data.
  168. */
  169. static int a3d_adc_open(struct gameport *gameport, int mode)
  170. {
  171. struct a3d *a3d = gameport->port_data;
  172. if (mode != GAMEPORT_MODE_COOKED)
  173. return -1;
  174. gameport_start_polling(a3d->gameport);
  175. return 0;
  176. }
  177. /*
  178. * a3d_adc_close() is a callback from the input close routine.
  179. */
  180. static void a3d_adc_close(struct gameport *gameport)
  181. {
  182. struct a3d *a3d = gameport->port_data;
  183. gameport_stop_polling(a3d->gameport);
  184. }
  185. /*
  186. * a3d_open() is a callback from the input open routine.
  187. */
  188. static int a3d_open(struct input_dev *dev)
  189. {
  190. struct a3d *a3d = input_get_drvdata(dev);
  191. gameport_start_polling(a3d->gameport);
  192. return 0;
  193. }
  194. /*
  195. * a3d_close() is a callback from the input close routine.
  196. */
  197. static void a3d_close(struct input_dev *dev)
  198. {
  199. struct a3d *a3d = input_get_drvdata(dev);
  200. gameport_stop_polling(a3d->gameport);
  201. }
  202. /*
  203. * a3d_connect() probes for A3D joysticks.
  204. */
  205. static int a3d_connect(struct gameport *gameport, struct gameport_driver *drv)
  206. {
  207. struct a3d *a3d;
  208. struct input_dev *input_dev;
  209. struct gameport *adc;
  210. unsigned char data[A3D_MAX_LENGTH];
  211. int i;
  212. int err;
  213. a3d = kzalloc(sizeof(struct a3d), GFP_KERNEL);
  214. input_dev = input_allocate_device();
  215. if (!a3d || !input_dev) {
  216. err = -ENOMEM;
  217. goto fail1;
  218. }
  219. a3d->dev = input_dev;
  220. a3d->gameport = gameport;
  221. gameport_set_drvdata(gameport, a3d);
  222. err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
  223. if (err)
  224. goto fail1;
  225. i = a3d_read_packet(gameport, A3D_MAX_LENGTH, data);
  226. if (!i || a3d_csum(data, i)) {
  227. err = -ENODEV;
  228. goto fail2;
  229. }
  230. a3d->mode = data[0];
  231. if (!a3d->mode || a3d->mode > 5) {
  232. printk(KERN_WARNING "a3d.c: Unknown A3D device detected "
  233. "(%s, id=%d), contact <vojtech@ucw.cz>\n", gameport->phys, a3d->mode);
  234. err = -ENODEV;
  235. goto fail2;
  236. }
  237. gameport_set_poll_handler(gameport, a3d_poll);
  238. gameport_set_poll_interval(gameport, 20);
  239. snprintf(a3d->phys, sizeof(a3d->phys), "%s/input0", gameport->phys);
  240. input_dev->name = a3d_names[a3d->mode];
  241. input_dev->phys = a3d->phys;
  242. input_dev->id.bustype = BUS_GAMEPORT;
  243. input_dev->id.vendor = GAMEPORT_ID_VENDOR_MADCATZ;
  244. input_dev->id.product = a3d->mode;
  245. input_dev->id.version = 0x0100;
  246. input_dev->dev.parent = &gameport->dev;
  247. input_dev->open = a3d_open;
  248. input_dev->close = a3d_close;
  249. input_set_drvdata(input_dev, a3d);
  250. if (a3d->mode == A3D_MODE_PXL) {
  251. int axes[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_RUDDER };
  252. a3d->length = 33;
  253. input_dev->evbit[0] |= BIT_MASK(EV_ABS) | BIT_MASK(EV_KEY) |
  254. BIT_MASK(EV_REL);
  255. input_dev->relbit[0] |= BIT_MASK(REL_X) | BIT_MASK(REL_Y);
  256. input_dev->absbit[0] |= BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) |
  257. BIT_MASK(ABS_THROTTLE) | BIT_MASK(ABS_RUDDER) |
  258. BIT_MASK(ABS_HAT0X) | BIT_MASK(ABS_HAT0Y) |
  259. BIT_MASK(ABS_HAT1X) | BIT_MASK(ABS_HAT1Y);
  260. input_dev->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_RIGHT) |
  261. BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) |
  262. BIT_MASK(BTN_SIDE) | BIT_MASK(BTN_EXTRA);
  263. input_dev->keybit[BIT_WORD(BTN_JOYSTICK)] |=
  264. BIT_MASK(BTN_TRIGGER) | BIT_MASK(BTN_THUMB) |
  265. BIT_MASK(BTN_TOP) | BIT_MASK(BTN_PINKIE);
  266. a3d_read(a3d, data);
  267. for (i = 0; i < 4; i++) {
  268. if (i < 2)
  269. input_set_abs_params(input_dev, axes[i],
  270. 48, input_abs_get_val(input_dev, axes[i]) * 2 - 48, 0, 8);
  271. else
  272. input_set_abs_params(input_dev, axes[i], 2, 253, 0, 0);
  273. input_set_abs_params(input_dev, ABS_HAT0X + i, -1, 1, 0, 0);
  274. }
  275. } else {
  276. a3d->length = 29;
  277. input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
  278. input_dev->relbit[0] |= BIT_MASK(REL_X) | BIT_MASK(REL_Y);
  279. input_dev->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_RIGHT) |
  280. BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE);
  281. a3d_read(a3d, data);
  282. if (!(a3d->adc = adc = gameport_allocate_port()))
  283. printk(KERN_ERR "a3d: Not enough memory for ADC port\n");
  284. else {
  285. adc->port_data = a3d;
  286. adc->open = a3d_adc_open;
  287. adc->close = a3d_adc_close;
  288. adc->cooked_read = a3d_adc_cooked_read;
  289. adc->fuzz = 1;
  290. gameport_set_name(adc, a3d_names[a3d->mode]);
  291. gameport_set_phys(adc, "%s/gameport0", gameport->phys);
  292. adc->dev.parent = &gameport->dev;
  293. gameport_register_port(adc);
  294. }
  295. }
  296. err = input_register_device(a3d->dev);
  297. if (err)
  298. goto fail3;
  299. return 0;
  300. fail3: if (a3d->adc)
  301. gameport_unregister_port(a3d->adc);
  302. fail2: gameport_close(gameport);
  303. fail1: gameport_set_drvdata(gameport, NULL);
  304. input_free_device(input_dev);
  305. kfree(a3d);
  306. return err;
  307. }
  308. static void a3d_disconnect(struct gameport *gameport)
  309. {
  310. struct a3d *a3d = gameport_get_drvdata(gameport);
  311. input_unregister_device(a3d->dev);
  312. if (a3d->adc)
  313. gameport_unregister_port(a3d->adc);
  314. gameport_close(gameport);
  315. gameport_set_drvdata(gameport, NULL);
  316. kfree(a3d);
  317. }
  318. static struct gameport_driver a3d_drv = {
  319. .driver = {
  320. .name = "adc",
  321. .owner = THIS_MODULE,
  322. },
  323. .description = DRIVER_DESC,
  324. .connect = a3d_connect,
  325. .disconnect = a3d_disconnect,
  326. };
  327. static int __init a3d_init(void)
  328. {
  329. return gameport_register_driver(&a3d_drv);
  330. }
  331. static void __exit a3d_exit(void)
  332. {
  333. gameport_unregister_driver(&a3d_drv);
  334. }
  335. module_init(a3d_init);
  336. module_exit(a3d_exit);