tmdc.c 11 KB

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