a3d.c 11 KB

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