tmdc.c 11 KB

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