tmdc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * Copyright (c) 1998-2001 Vojtech Pavlik
  3. *
  4. * Based on the work of:
  5. * Trystan Larey-Williams
  6. */
  7. /*
  8. * ThrustMaster DirectConnect (BSP) joystick family driver for Linux
  9. */
  10. /*
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. *
  25. * Should you need to contact me, the author, you can do so either by
  26. * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
  27. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  28. */
  29. #include <linux/delay.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/module.h>
  33. #include <linux/init.h>
  34. #include <linux/gameport.h>
  35. #include <linux/input.h>
  36. #include <linux/jiffies.h>
  37. #define DRIVER_DESC "ThrustMaster DirectConnect joystick driver"
  38. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  39. MODULE_DESCRIPTION(DRIVER_DESC);
  40. MODULE_LICENSE("GPL");
  41. #define TMDC_MAX_START 600 /* 600 us */
  42. #define TMDC_MAX_STROBE 60 /* 60 us */
  43. #define TMDC_MAX_LENGTH 13
  44. #define TMDC_MODE_M3DI 1
  45. #define TMDC_MODE_3DRP 3
  46. #define TMDC_MODE_AT 4
  47. #define TMDC_MODE_FM 8
  48. #define TMDC_MODE_FGP 163
  49. #define TMDC_BYTE_ID 10
  50. #define TMDC_BYTE_REV 11
  51. #define TMDC_BYTE_DEF 12
  52. #define TMDC_ABS 7
  53. #define TMDC_ABS_HAT 4
  54. #define TMDC_BTN 16
  55. static const unsigned char tmdc_byte_a[16] = { 0, 1, 3, 4, 6, 7 };
  56. static const unsigned char tmdc_byte_d[16] = { 2, 5, 8, 9 };
  57. static const signed char tmdc_abs[TMDC_ABS] =
  58. { ABS_X, ABS_Y, ABS_RUDDER, ABS_THROTTLE, ABS_RX, ABS_RY, ABS_RZ };
  59. static const signed char tmdc_abs_hat[TMDC_ABS_HAT] =
  60. { ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y };
  61. static const signed char tmdc_abs_at[TMDC_ABS] =
  62. { ABS_X, ABS_Y, ABS_RUDDER, -1, ABS_THROTTLE };
  63. static const signed char tmdc_abs_fm[TMDC_ABS] =
  64. { ABS_RX, ABS_RY, ABS_X, ABS_Y };
  65. static const short tmdc_btn_pad[TMDC_BTN] =
  66. { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_START, BTN_SELECT, BTN_TL, BTN_TR };
  67. static const short tmdc_btn_joy[TMDC_BTN] =
  68. { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_THUMB2, BTN_PINKIE,
  69. BTN_BASE3, BTN_BASE4, BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z };
  70. static const short tmdc_btn_fm[TMDC_BTN] =
  71. { BTN_TRIGGER, BTN_C, BTN_B, BTN_A, BTN_THUMB, BTN_X, BTN_Y, BTN_Z, BTN_TOP, BTN_TOP2 };
  72. static const short tmdc_btn_at[TMDC_BTN] =
  73. { BTN_TRIGGER, BTN_THUMB2, BTN_PINKIE, BTN_THUMB, BTN_BASE6, BTN_BASE5, BTN_BASE4,
  74. BTN_BASE3, BTN_BASE2, BTN_BASE };
  75. static const struct {
  76. int x;
  77. int y;
  78. } tmdc_hat_to_axis[] = {{ 0, 0}, { 1, 0}, { 0,-1}, {-1, 0}, { 0, 1}};
  79. static const struct tmdc_model {
  80. unsigned char id;
  81. const char *name;
  82. char abs;
  83. char hats;
  84. char btnc[4];
  85. char btno[4];
  86. const signed char *axes;
  87. const short *buttons;
  88. } tmdc_models[] = {
  89. { 1, "ThrustMaster Millenium 3D Inceptor", 6, 2, { 4, 2 }, { 4, 6 }, tmdc_abs, tmdc_btn_joy },
  90. { 3, "ThrustMaster Rage 3D Gamepad", 2, 0, { 8, 2 }, { 0, 0 }, tmdc_abs, tmdc_btn_pad },
  91. { 4, "ThrustMaster Attack Throttle", 5, 2, { 4, 6 }, { 4, 2 }, tmdc_abs_at, tmdc_btn_at },
  92. { 8, "ThrustMaster FragMaster", 4, 0, { 8, 2 }, { 0, 0 }, tmdc_abs_fm, tmdc_btn_fm },
  93. { 163, "Thrustmaster Fusion GamePad", 2, 0, { 8, 2 }, { 0, 0 }, tmdc_abs, tmdc_btn_pad },
  94. { 0, "Unknown %d-axis, %d-button TM device %d", 0, 0, { 0, 0 }, { 0, 0 }, tmdc_abs, tmdc_btn_joy }
  95. };
  96. struct tmdc_port {
  97. struct input_dev *dev;
  98. char name[64];
  99. char phys[32];
  100. int mode;
  101. const signed char *abs;
  102. const short *btn;
  103. unsigned char absc;
  104. unsigned char btnc[4];
  105. unsigned char btno[4];
  106. };
  107. struct tmdc {
  108. struct gameport *gameport;
  109. struct tmdc_port *port[2];
  110. #if 0
  111. struct input_dev *dev[2];
  112. char name[2][64];
  113. char phys[2][32];
  114. int mode[2];
  115. signed char *abs[2];
  116. short *btn[2];
  117. unsigned char absc[2];
  118. unsigned char btnc[2][4];
  119. unsigned char btno[2][4];
  120. #endif
  121. int reads;
  122. int bads;
  123. unsigned char exists;
  124. };
  125. /*
  126. * tmdc_read_packet() reads a ThrustMaster packet.
  127. */
  128. static int tmdc_read_packet(struct gameport *gameport, unsigned char data[2][TMDC_MAX_LENGTH])
  129. {
  130. unsigned char u, v, w, x;
  131. unsigned long flags;
  132. int i[2], j[2], t[2], p, k;
  133. p = gameport_time(gameport, TMDC_MAX_STROBE);
  134. for (k = 0; k < 2; k++) {
  135. t[k] = gameport_time(gameport, TMDC_MAX_START);
  136. i[k] = j[k] = 0;
  137. }
  138. local_irq_save(flags);
  139. gameport_trigger(gameport);
  140. w = gameport_read(gameport) >> 4;
  141. do {
  142. x = w;
  143. w = gameport_read(gameport) >> 4;
  144. for (k = 0, v = w, u = x; k < 2; k++, v >>= 2, u >>= 2) {
  145. if (~v & u & 2) {
  146. if (t[k] <= 0 || i[k] >= TMDC_MAX_LENGTH) continue;
  147. t[k] = p;
  148. if (j[k] == 0) { /* Start bit */
  149. if (~v & 1) t[k] = 0;
  150. data[k][i[k]] = 0; j[k]++; continue;
  151. }
  152. if (j[k] == 9) { /* Stop bit */
  153. if (v & 1) t[k] = 0;
  154. j[k] = 0; i[k]++; continue;
  155. }
  156. data[k][i[k]] |= (~v & 1) << (j[k]++ - 1); /* Data bit */
  157. }
  158. t[k]--;
  159. }
  160. } while (t[0] > 0 || t[1] > 0);
  161. local_irq_restore(flags);
  162. return (i[0] == TMDC_MAX_LENGTH) | ((i[1] == TMDC_MAX_LENGTH) << 1);
  163. }
  164. static int tmdc_parse_packet(struct tmdc_port *port, unsigned char *data)
  165. {
  166. int i, k, l;
  167. if (data[TMDC_BYTE_ID] != port->mode)
  168. return -1;
  169. for (i = 0; i < port->absc; i++) {
  170. if (port->abs[i] < 0)
  171. return 0;
  172. input_report_abs(port->dev, port->abs[i], data[tmdc_byte_a[i]]);
  173. }
  174. switch (port->mode) {
  175. case TMDC_MODE_M3DI:
  176. i = tmdc_byte_d[0];
  177. input_report_abs(port->dev, ABS_HAT0X, ((data[i] >> 3) & 1) - ((data[i] >> 1) & 1));
  178. input_report_abs(port->dev, ABS_HAT0Y, ((data[i] >> 2) & 1) - ( data[i] & 1));
  179. break;
  180. case TMDC_MODE_AT:
  181. i = tmdc_byte_a[3];
  182. input_report_abs(port->dev, ABS_HAT0X, tmdc_hat_to_axis[(data[i] - 141) / 25].x);
  183. input_report_abs(port->dev, ABS_HAT0Y, tmdc_hat_to_axis[(data[i] - 141) / 25].y);
  184. break;
  185. }
  186. for (k = l = 0; k < 4; k++) {
  187. for (i = 0; i < port->btnc[k]; i++)
  188. input_report_key(port->dev, port->btn[i + l],
  189. ((data[tmdc_byte_d[k]] >> (i + port->btno[k])) & 1));
  190. l += port->btnc[k];
  191. }
  192. input_sync(port->dev);
  193. return 0;
  194. }
  195. /*
  196. * tmdc_poll() reads and analyzes ThrustMaster joystick data.
  197. */
  198. static void tmdc_poll(struct gameport *gameport)
  199. {
  200. unsigned char data[2][TMDC_MAX_LENGTH];
  201. struct tmdc *tmdc = gameport_get_drvdata(gameport);
  202. unsigned char r, bad = 0;
  203. int i;
  204. tmdc->reads++;
  205. if ((r = tmdc_read_packet(tmdc->gameport, data)) != tmdc->exists)
  206. bad = 1;
  207. else {
  208. for (i = 0; i < 2; i++) {
  209. if (r & (1 << i) & tmdc->exists) {
  210. if (tmdc_parse_packet(tmdc->port[i], data[i]))
  211. bad = 1;
  212. }
  213. }
  214. }
  215. tmdc->bads += bad;
  216. }
  217. static int tmdc_open(struct input_dev *dev)
  218. {
  219. struct tmdc *tmdc = input_get_drvdata(dev);
  220. gameport_start_polling(tmdc->gameport);
  221. return 0;
  222. }
  223. static void tmdc_close(struct input_dev *dev)
  224. {
  225. struct tmdc *tmdc = input_get_drvdata(dev);
  226. gameport_stop_polling(tmdc->gameport);
  227. }
  228. static int tmdc_setup_port(struct tmdc *tmdc, int idx, unsigned char *data)
  229. {
  230. const struct tmdc_model *model;
  231. struct tmdc_port *port;
  232. struct input_dev *input_dev;
  233. int i, j, b = 0;
  234. int err;
  235. tmdc->port[idx] = port = kzalloc(sizeof (struct tmdc_port), GFP_KERNEL);
  236. input_dev = input_allocate_device();
  237. if (!port || !input_dev) {
  238. err = -ENOMEM;
  239. goto fail;
  240. }
  241. port->mode = data[TMDC_BYTE_ID];
  242. for (model = tmdc_models; model->id && model->id != port->mode; model++)
  243. /* empty */;
  244. port->abs = model->axes;
  245. port->btn = model->buttons;
  246. if (!model->id) {
  247. port->absc = data[TMDC_BYTE_DEF] >> 4;
  248. for (i = 0; i < 4; i++)
  249. port->btnc[i] = i < (data[TMDC_BYTE_DEF] & 0xf) ? 8 : 0;
  250. } else {
  251. port->absc = model->abs;
  252. for (i = 0; i < 4; i++)
  253. port->btnc[i] = model->btnc[i];
  254. }
  255. for (i = 0; i < 4; i++)
  256. port->btno[i] = model->btno[i];
  257. snprintf(port->name, sizeof(port->name), model->name,
  258. port->absc, (data[TMDC_BYTE_DEF] & 0xf) << 3, port->mode);
  259. snprintf(port->phys, sizeof(port->phys), "%s/input%d", tmdc->gameport->phys, i);
  260. port->dev = input_dev;
  261. input_dev->name = port->name;
  262. input_dev->phys = port->phys;
  263. input_dev->id.bustype = BUS_GAMEPORT;
  264. input_dev->id.vendor = GAMEPORT_ID_VENDOR_THRUSTMASTER;
  265. input_dev->id.product = model->id;
  266. input_dev->id.version = 0x0100;
  267. input_dev->dev.parent = &tmdc->gameport->dev;
  268. input_set_drvdata(input_dev, tmdc);
  269. input_dev->open = tmdc_open;
  270. input_dev->close = tmdc_close;
  271. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  272. for (i = 0; i < port->absc && i < TMDC_ABS; i++)
  273. if (port->abs[i] >= 0)
  274. input_set_abs_params(input_dev, port->abs[i], 8, 248, 2, 4);
  275. for (i = 0; i < model->hats && i < TMDC_ABS_HAT; i++)
  276. input_set_abs_params(input_dev, tmdc_abs_hat[i], -1, 1, 0, 0);
  277. for (i = 0; i < 4; i++) {
  278. for (j = 0; j < port->btnc[i] && j < TMDC_BTN; j++)
  279. set_bit(port->btn[j + b], input_dev->keybit);
  280. b += port->btnc[i];
  281. }
  282. err = input_register_device(port->dev);
  283. if (err)
  284. goto fail;
  285. return 0;
  286. fail: input_free_device(input_dev);
  287. kfree(port);
  288. return err;
  289. }
  290. /*
  291. * tmdc_probe() probes for ThrustMaster type joysticks.
  292. */
  293. static int tmdc_connect(struct gameport *gameport, struct gameport_driver *drv)
  294. {
  295. unsigned char data[2][TMDC_MAX_LENGTH];
  296. struct tmdc *tmdc;
  297. int i;
  298. int err;
  299. if (!(tmdc = kzalloc(sizeof(struct tmdc), GFP_KERNEL)))
  300. return -ENOMEM;
  301. tmdc->gameport = gameport;
  302. gameport_set_drvdata(gameport, tmdc);
  303. err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
  304. if (err)
  305. goto fail1;
  306. if (!(tmdc->exists = tmdc_read_packet(gameport, data))) {
  307. err = -ENODEV;
  308. goto fail2;
  309. }
  310. gameport_set_poll_handler(gameport, tmdc_poll);
  311. gameport_set_poll_interval(gameport, 20);
  312. for (i = 0; i < 2; i++) {
  313. if (tmdc->exists & (1 << i)) {
  314. err = tmdc_setup_port(tmdc, i, data[i]);
  315. if (err)
  316. goto fail3;
  317. }
  318. }
  319. return 0;
  320. fail3: while (--i >= 0) {
  321. if (tmdc->port[i]) {
  322. input_unregister_device(tmdc->port[i]->dev);
  323. kfree(tmdc->port[i]);
  324. }
  325. }
  326. fail2: gameport_close(gameport);
  327. fail1: gameport_set_drvdata(gameport, NULL);
  328. kfree(tmdc);
  329. return err;
  330. }
  331. static void tmdc_disconnect(struct gameport *gameport)
  332. {
  333. struct tmdc *tmdc = gameport_get_drvdata(gameport);
  334. int i;
  335. for (i = 0; i < 2; i++) {
  336. if (tmdc->port[i]) {
  337. input_unregister_device(tmdc->port[i]->dev);
  338. kfree(tmdc->port[i]);
  339. }
  340. }
  341. gameport_close(gameport);
  342. gameport_set_drvdata(gameport, NULL);
  343. kfree(tmdc);
  344. }
  345. static struct gameport_driver tmdc_drv = {
  346. .driver = {
  347. .name = "tmdc",
  348. .owner = THIS_MODULE,
  349. },
  350. .description = DRIVER_DESC,
  351. .connect = tmdc_connect,
  352. .disconnect = tmdc_disconnect,
  353. };
  354. static int __init tmdc_init(void)
  355. {
  356. return gameport_register_driver(&tmdc_drv);
  357. }
  358. static void __exit tmdc_exit(void)
  359. {
  360. gameport_unregister_driver(&tmdc_drv);
  361. }
  362. module_init(tmdc_init);
  363. module_exit(tmdc_exit);