sidewinder.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 1998-2005 Vojtech Pavlik
  4. */
  5. /*
  6. * Microsoft SideWinder joystick family driver for Linux
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include <linux/input.h>
  13. #include <linux/gameport.h>
  14. #include <linux/jiffies.h>
  15. #include <linux/string_choices.h>
  16. #define DRIVER_DESC "Microsoft SideWinder joystick family driver"
  17. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  18. MODULE_DESCRIPTION(DRIVER_DESC);
  19. MODULE_LICENSE("GPL");
  20. /*
  21. * These are really magic values. Changing them can make a problem go away,
  22. * as well as break everything.
  23. */
  24. #undef SW_DEBUG
  25. #undef SW_DEBUG_DATA
  26. #define SW_START 600 /* The time we wait for the first bit [600 us] */
  27. #define SW_STROBE 60 /* Max time per bit [60 us] */
  28. #define SW_TIMEOUT 6 /* Wait for everything to settle [6 ms] */
  29. #define SW_KICK 45 /* Wait after A0 fall till kick [45 us] */
  30. #define SW_END 8 /* Number of bits before end of packet to kick */
  31. #define SW_FAIL 16 /* Number of packet read errors to fail and reinitialize */
  32. #define SW_BAD 2 /* Number of packet read errors to switch off 3d Pro optimization */
  33. #define SW_OK 64 /* Number of packet read successes to switch optimization back on */
  34. #define SW_LENGTH 512 /* Max number of bits in a packet */
  35. #ifdef SW_DEBUG
  36. #define dbg(format, arg...) printk(KERN_DEBUG __FILE__ ": " format "\n" , ## arg)
  37. #else
  38. #define dbg(format, arg...) do {} while (0)
  39. #endif
  40. /*
  41. * SideWinder joystick types ...
  42. */
  43. #define SW_ID_3DP 0
  44. #define SW_ID_GP 1
  45. #define SW_ID_PP 2
  46. #define SW_ID_FFP 3
  47. #define SW_ID_FSP 4
  48. #define SW_ID_FFW 5
  49. /*
  50. * Names, buttons, axes ...
  51. */
  52. static char *sw_name[] = { "3D Pro", "GamePad", "Precision Pro", "Force Feedback Pro", "FreeStyle Pro",
  53. "Force Feedback Wheel" };
  54. static char sw_abs[][7] = {
  55. { ABS_X, ABS_Y, ABS_RZ, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y },
  56. { ABS_X, ABS_Y },
  57. { ABS_X, ABS_Y, ABS_RZ, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y },
  58. { ABS_X, ABS_Y, ABS_RZ, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y },
  59. { ABS_X, ABS_Y, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y },
  60. { ABS_RX, ABS_RUDDER, ABS_THROTTLE }};
  61. static char sw_bit[][7] = {
  62. { 10, 10, 9, 10, 1, 1 },
  63. { 1, 1 },
  64. { 10, 10, 6, 7, 1, 1 },
  65. { 10, 10, 6, 7, 1, 1 },
  66. { 10, 10, 6, 1, 1 },
  67. { 10, 7, 7, 1, 1 }};
  68. static short sw_btn[][12] = {
  69. { BTN_TRIGGER, BTN_TOP, BTN_THUMB, BTN_THUMB2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_MODE },
  70. { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_START, BTN_MODE },
  71. { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_SELECT },
  72. { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_SELECT },
  73. { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_START, BTN_MODE, BTN_SELECT },
  74. { BTN_TRIGGER, BTN_TOP, BTN_THUMB, BTN_THUMB2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4 }};
  75. static struct {
  76. int x;
  77. int y;
  78. } sw_hat_to_axis[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
  79. struct sw {
  80. struct gameport *gameport;
  81. struct input_dev *dev[4];
  82. char name[64];
  83. char phys[4][32];
  84. int length;
  85. int type;
  86. int bits;
  87. int number;
  88. int fail;
  89. int ok;
  90. int reads;
  91. int bads;
  92. };
  93. /*
  94. * sw_read_packet() is a function which reads either a data packet, or an
  95. * identification packet from a SideWinder joystick. The protocol is very,
  96. * very, very braindamaged. Microsoft patented it in US patent #5628686.
  97. */
  98. static int sw_read_packet(struct gameport *gameport, unsigned char *buf, int length, int id)
  99. {
  100. unsigned long flags;
  101. int timeout, bitout, sched, i, kick, start, strobe;
  102. unsigned char pending, u, v;
  103. i = -id; /* Don't care about data, only want ID */
  104. timeout = id ? gameport_time(gameport, SW_TIMEOUT * 1000) : 0; /* Set up global timeout for ID packet */
  105. kick = id ? gameport_time(gameport, SW_KICK) : 0; /* Set up kick timeout for ID packet */
  106. start = gameport_time(gameport, SW_START);
  107. strobe = gameport_time(gameport, SW_STROBE);
  108. bitout = start;
  109. pending = 0;
  110. sched = 0;
  111. local_irq_save(flags); /* Quiet, please */
  112. gameport_trigger(gameport); /* Trigger */
  113. v = gameport_read(gameport);
  114. do {
  115. bitout--;
  116. u = v;
  117. v = gameport_read(gameport);
  118. } while (!(~v & u & 0x10) && (bitout > 0)); /* Wait for first falling edge on clock */
  119. if (bitout > 0)
  120. bitout = strobe; /* Extend time if not timed out */
  121. while ((timeout > 0 || bitout > 0) && (i < length)) {
  122. timeout--;
  123. bitout--; /* Decrement timers */
  124. sched--;
  125. u = v;
  126. v = gameport_read(gameport);
  127. if ((~u & v & 0x10) && (bitout > 0)) { /* Rising edge on clock - data bit */
  128. if (i >= 0) /* Want this data */
  129. buf[i] = v >> 5; /* Store it */
  130. i++; /* Advance index */
  131. bitout = strobe; /* Extend timeout for next bit */
  132. }
  133. if (kick && (~v & u & 0x01)) { /* Falling edge on axis 0 */
  134. sched = kick; /* Schedule second trigger */
  135. kick = 0; /* Don't schedule next time on falling edge */
  136. pending = 1; /* Mark schedule */
  137. }
  138. if (pending && sched < 0 && (i > -SW_END)) { /* Second trigger time */
  139. gameport_trigger(gameport); /* Trigger */
  140. bitout = start; /* Long bit timeout */
  141. pending = 0; /* Unmark schedule */
  142. timeout = 0; /* Switch from global to bit timeouts */
  143. }
  144. }
  145. local_irq_restore(flags); /* Done - relax */
  146. #ifdef SW_DEBUG_DATA
  147. {
  148. int j;
  149. printk(KERN_DEBUG "sidewinder.c: Read %d triplets. [", i);
  150. for (j = 0; j < i; j++) printk("%d", buf[j]);
  151. printk("]\n");
  152. }
  153. #endif
  154. return i;
  155. }
  156. /*
  157. * sw_get_bits() and GB() compose bits from the triplet buffer into a __u64.
  158. * Parameter 'pos' is bit number inside packet where to start at, 'num' is number
  159. * of bits to be read, 'shift' is offset in the resulting __u64 to start at, bits
  160. * is number of bits per triplet.
  161. */
  162. #define GB(pos,num) sw_get_bits(buf, pos, num, sw->bits)
  163. static __u64 sw_get_bits(unsigned char *buf, int pos, int num, char bits)
  164. {
  165. __u64 data = 0;
  166. int tri = pos % bits; /* Start position */
  167. int i = pos / bits;
  168. int bit = 0;
  169. while (num--) {
  170. data |= (__u64)((buf[i] >> tri++) & 1) << bit++; /* Transfer bit */
  171. if (tri == bits) {
  172. i++; /* Next triplet */
  173. tri = 0;
  174. }
  175. }
  176. return data;
  177. }
  178. /*
  179. * sw_init_digital() initializes a SideWinder 3D Pro joystick
  180. * into digital mode.
  181. */
  182. static void sw_init_digital(struct gameport *gameport)
  183. {
  184. static const int seq[] = { 140, 140+725, 140+300, 0 };
  185. unsigned long flags;
  186. int i, t;
  187. local_irq_save(flags);
  188. i = 0;
  189. do {
  190. gameport_trigger(gameport); /* Trigger */
  191. t = gameport_time(gameport, SW_TIMEOUT * 1000);
  192. while ((gameport_read(gameport) & 1) && t) t--; /* Wait for axis to fall back to 0 */
  193. udelay(seq[i]); /* Delay magic time */
  194. } while (seq[++i]);
  195. gameport_trigger(gameport); /* Last trigger */
  196. local_irq_restore(flags);
  197. }
  198. /*
  199. * sw_parity() computes parity of __u64
  200. */
  201. static int sw_parity(__u64 t)
  202. {
  203. int x = t ^ (t >> 32);
  204. x ^= x >> 16;
  205. x ^= x >> 8;
  206. x ^= x >> 4;
  207. x ^= x >> 2;
  208. x ^= x >> 1;
  209. return x & 1;
  210. }
  211. /*
  212. * sw_ccheck() checks synchronization bits and computes checksum of nibbles.
  213. */
  214. static int sw_check(__u64 t)
  215. {
  216. unsigned char sum = 0;
  217. if ((t & 0x8080808080808080ULL) ^ 0x80) /* Sync */
  218. return -1;
  219. while (t) { /* Sum */
  220. sum += t & 0xf;
  221. t >>= 4;
  222. }
  223. return sum & 0xf;
  224. }
  225. /*
  226. * sw_parse() analyzes SideWinder joystick data, and writes the results into
  227. * the axes and buttons arrays.
  228. */
  229. static int sw_parse(unsigned char *buf, struct sw *sw)
  230. {
  231. int hat, i, j;
  232. struct input_dev *dev;
  233. switch (sw->type) {
  234. case SW_ID_3DP:
  235. if (sw_check(GB(0,64)) || (hat = (GB(6,1) << 3) | GB(60,3)) > 8)
  236. return -1;
  237. dev = sw->dev[0];
  238. input_report_abs(dev, ABS_X, (GB( 3,3) << 7) | GB(16,7));
  239. input_report_abs(dev, ABS_Y, (GB( 0,3) << 7) | GB(24,7));
  240. input_report_abs(dev, ABS_RZ, (GB(35,2) << 7) | GB(40,7));
  241. input_report_abs(dev, ABS_THROTTLE, (GB(32,3) << 7) | GB(48,7));
  242. input_report_abs(dev, ABS_HAT0X, sw_hat_to_axis[hat].x);
  243. input_report_abs(dev, ABS_HAT0Y, sw_hat_to_axis[hat].y);
  244. for (j = 0; j < 7; j++)
  245. input_report_key(dev, sw_btn[SW_ID_3DP][j], !GB(j+8,1));
  246. input_report_key(dev, BTN_BASE4, !GB(38,1));
  247. input_report_key(dev, BTN_BASE5, !GB(37,1));
  248. input_sync(dev);
  249. return 0;
  250. case SW_ID_GP:
  251. for (i = 0; i < sw->number; i ++) {
  252. if (sw_parity(GB(i*15,15)))
  253. return -1;
  254. input_report_abs(sw->dev[i], ABS_X, GB(i*15+3,1) - GB(i*15+2,1));
  255. input_report_abs(sw->dev[i], ABS_Y, GB(i*15+0,1) - GB(i*15+1,1));
  256. for (j = 0; j < 10; j++)
  257. input_report_key(sw->dev[i], sw_btn[SW_ID_GP][j], !GB(i*15+j+4,1));
  258. input_sync(sw->dev[i]);
  259. }
  260. return 0;
  261. case SW_ID_PP:
  262. case SW_ID_FFP:
  263. if (!sw_parity(GB(0,48)) || (hat = GB(42,4)) > 8)
  264. return -1;
  265. dev = sw->dev[0];
  266. input_report_abs(dev, ABS_X, GB( 9,10));
  267. input_report_abs(dev, ABS_Y, GB(19,10));
  268. input_report_abs(dev, ABS_RZ, GB(36, 6));
  269. input_report_abs(dev, ABS_THROTTLE, GB(29, 7));
  270. input_report_abs(dev, ABS_HAT0X, sw_hat_to_axis[hat].x);
  271. input_report_abs(dev, ABS_HAT0Y, sw_hat_to_axis[hat].y);
  272. for (j = 0; j < 9; j++)
  273. input_report_key(dev, sw_btn[SW_ID_PP][j], !GB(j,1));
  274. input_sync(dev);
  275. return 0;
  276. case SW_ID_FSP:
  277. if (!sw_parity(GB(0,43)) || (hat = GB(28,4)) > 8)
  278. return -1;
  279. dev = sw->dev[0];
  280. input_report_abs(dev, ABS_X, GB( 0,10));
  281. input_report_abs(dev, ABS_Y, GB(16,10));
  282. input_report_abs(dev, ABS_THROTTLE, GB(32, 6));
  283. input_report_abs(dev, ABS_HAT0X, sw_hat_to_axis[hat].x);
  284. input_report_abs(dev, ABS_HAT0Y, sw_hat_to_axis[hat].y);
  285. for (j = 0; j < 6; j++)
  286. input_report_key(dev, sw_btn[SW_ID_FSP][j], !GB(j+10,1));
  287. input_report_key(dev, BTN_TR, !GB(26,1));
  288. input_report_key(dev, BTN_START, !GB(27,1));
  289. input_report_key(dev, BTN_MODE, !GB(38,1));
  290. input_report_key(dev, BTN_SELECT, !GB(39,1));
  291. input_sync(dev);
  292. return 0;
  293. case SW_ID_FFW:
  294. if (!sw_parity(GB(0,33)))
  295. return -1;
  296. dev = sw->dev[0];
  297. input_report_abs(dev, ABS_RX, GB( 0,10));
  298. input_report_abs(dev, ABS_RUDDER, GB(10, 6));
  299. input_report_abs(dev, ABS_THROTTLE, GB(16, 6));
  300. for (j = 0; j < 8; j++)
  301. input_report_key(dev, sw_btn[SW_ID_FFW][j], !GB(j+22,1));
  302. input_sync(dev);
  303. return 0;
  304. }
  305. return -1;
  306. }
  307. /*
  308. * sw_read() reads SideWinder joystick data, and reinitializes
  309. * the joystick in case of persistent problems. This is the function that is
  310. * called from the generic code to poll the joystick.
  311. */
  312. static int sw_read(struct sw *sw)
  313. {
  314. unsigned char buf[SW_LENGTH];
  315. int i;
  316. i = sw_read_packet(sw->gameport, buf, sw->length, 0);
  317. if (sw->type == SW_ID_3DP && sw->length == 66 && i != 66) { /* Broken packet, try to fix */
  318. if (i == 64 && !sw_check(sw_get_bits(buf,0,64,1))) { /* Last init failed, 1 bit mode */
  319. printk(KERN_WARNING "sidewinder.c: Joystick in wrong mode on %s"
  320. " - going to reinitialize.\n", sw->gameport->phys);
  321. sw->fail = SW_FAIL; /* Reinitialize */
  322. i = 128; /* Bogus value */
  323. }
  324. if (i < 66 && GB(0,64) == GB(i*3-66,64)) /* 1 == 3 */
  325. i = 66; /* Everything is fine */
  326. if (i < 66 && GB(0,64) == GB(66,64)) /* 1 == 2 */
  327. i = 66; /* Everything is fine */
  328. if (i < 66 && GB(i*3-132,64) == GB(i*3-66,64)) { /* 2 == 3 */
  329. memmove(buf, buf + i - 22, 22); /* Move data */
  330. i = 66; /* Carry on */
  331. }
  332. }
  333. if (i == sw->length && !sw_parse(buf, sw)) { /* Parse data */
  334. sw->fail = 0;
  335. sw->ok++;
  336. if (sw->type == SW_ID_3DP && sw->length == 66 /* Many packets OK */
  337. && sw->ok > SW_OK) {
  338. printk(KERN_INFO "sidewinder.c: No more trouble on %s"
  339. " - enabling optimization again.\n", sw->gameport->phys);
  340. sw->length = 22;
  341. }
  342. return 0;
  343. }
  344. sw->ok = 0;
  345. sw->fail++;
  346. if (sw->type == SW_ID_3DP && sw->length == 22 && sw->fail > SW_BAD) { /* Consecutive bad packets */
  347. printk(KERN_INFO "sidewinder.c: Many bit errors on %s"
  348. " - disabling optimization.\n", sw->gameport->phys);
  349. sw->length = 66;
  350. }
  351. if (sw->fail < SW_FAIL)
  352. return -1; /* Not enough, don't reinitialize yet */
  353. printk(KERN_WARNING "sidewinder.c: Too many bit errors on %s"
  354. " - reinitializing joystick.\n", sw->gameport->phys);
  355. if (!i && sw->type == SW_ID_3DP) { /* 3D Pro can be in analog mode */
  356. mdelay(3 * SW_TIMEOUT);
  357. sw_init_digital(sw->gameport);
  358. }
  359. mdelay(SW_TIMEOUT);
  360. i = sw_read_packet(sw->gameport, buf, SW_LENGTH, 0); /* Read normal data packet */
  361. mdelay(SW_TIMEOUT);
  362. sw_read_packet(sw->gameport, buf, SW_LENGTH, i); /* Read ID packet, this initializes the stick */
  363. sw->fail = SW_FAIL;
  364. return -1;
  365. }
  366. static void sw_poll(struct gameport *gameport)
  367. {
  368. struct sw *sw = gameport_get_drvdata(gameport);
  369. sw->reads++;
  370. if (sw_read(sw))
  371. sw->bads++;
  372. }
  373. static int sw_open(struct input_dev *dev)
  374. {
  375. struct sw *sw = input_get_drvdata(dev);
  376. gameport_start_polling(sw->gameport);
  377. return 0;
  378. }
  379. static void sw_close(struct input_dev *dev)
  380. {
  381. struct sw *sw = input_get_drvdata(dev);
  382. gameport_stop_polling(sw->gameport);
  383. }
  384. /*
  385. * sw_print_packet() prints the contents of a SideWinder packet.
  386. */
  387. static void sw_print_packet(char *name, int length, unsigned char *buf, char bits)
  388. {
  389. int i;
  390. printk(KERN_INFO "sidewinder.c: %s packet, %d bits. [", name, length);
  391. for (i = (((length + 3) >> 2) - 1); i >= 0; i--)
  392. printk("%x", (int)sw_get_bits(buf, i << 2, 4, bits));
  393. printk("]\n");
  394. }
  395. /*
  396. * sw_3dp_id() translates the 3DP id into a human legible string.
  397. * Unfortunately I don't know how to do this for the other SW types.
  398. */
  399. static void sw_3dp_id(unsigned char *buf, char *comment, size_t size)
  400. {
  401. int i;
  402. char pnp[8], rev[9];
  403. for (i = 0; i < 7; i++) /* ASCII PnP ID */
  404. pnp[i] = sw_get_bits(buf, 24+8*i, 8, 1);
  405. for (i = 0; i < 8; i++) /* ASCII firmware revision */
  406. rev[i] = sw_get_bits(buf, 88+8*i, 8, 1);
  407. pnp[7] = rev[8] = 0;
  408. snprintf(comment, size, " [PnP %d.%02d id %s rev %s]",
  409. (int) ((sw_get_bits(buf, 8, 6, 1) << 6) | /* Two 6-bit values */
  410. sw_get_bits(buf, 16, 6, 1)) / 100,
  411. (int) ((sw_get_bits(buf, 8, 6, 1) << 6) |
  412. sw_get_bits(buf, 16, 6, 1)) % 100,
  413. pnp, rev);
  414. }
  415. /*
  416. * sw_guess_mode() checks the upper two button bits for toggling -
  417. * indication of that the joystick is in 3-bit mode. This is documented
  418. * behavior for 3DP ID packet, and for example the FSP does this in
  419. * normal packets instead. Fun ...
  420. */
  421. static int sw_guess_mode(unsigned char *buf, int len)
  422. {
  423. int i;
  424. unsigned char xor = 0;
  425. for (i = 1; i < len; i++)
  426. xor |= (buf[i - 1] ^ buf[i]) & 6;
  427. return !!xor * 2 + 1;
  428. }
  429. /*
  430. * sw_connect() probes for SideWinder type joysticks.
  431. */
  432. static int sw_connect(struct gameport *gameport, struct gameport_driver *drv)
  433. {
  434. struct sw *sw;
  435. struct input_dev *input_dev;
  436. int i, j, k, l;
  437. int err = 0;
  438. unsigned char *buf = NULL; /* [SW_LENGTH] */
  439. unsigned char *idbuf = NULL; /* [SW_LENGTH] */
  440. unsigned char m = 1;
  441. char comment[40];
  442. comment[0] = 0;
  443. sw = kzalloc_obj(*sw);
  444. buf = kmalloc(SW_LENGTH, GFP_KERNEL);
  445. idbuf = kmalloc(SW_LENGTH, GFP_KERNEL);
  446. if (!sw || !buf || !idbuf) {
  447. err = -ENOMEM;
  448. goto fail1;
  449. }
  450. sw->gameport = gameport;
  451. gameport_set_drvdata(gameport, sw);
  452. err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
  453. if (err)
  454. goto fail1;
  455. dbg("Init 0: Opened %s, io %#x, speed %d",
  456. gameport->phys, gameport->io, gameport->speed);
  457. i = sw_read_packet(gameport, buf, SW_LENGTH, 0); /* Read normal packet */
  458. msleep(SW_TIMEOUT);
  459. dbg("Init 1: Mode %d. Length %d.", m , i);
  460. if (!i) { /* No data. 3d Pro analog mode? */
  461. sw_init_digital(gameport); /* Switch to digital */
  462. msleep(SW_TIMEOUT);
  463. i = sw_read_packet(gameport, buf, SW_LENGTH, 0); /* Retry reading packet */
  464. msleep(SW_TIMEOUT);
  465. dbg("Init 1b: Length %d.", i);
  466. if (!i) { /* No data -> FAIL */
  467. err = -ENODEV;
  468. goto fail2;
  469. }
  470. }
  471. j = sw_read_packet(gameport, idbuf, SW_LENGTH, i); /* Read ID. This initializes the stick */
  472. m |= sw_guess_mode(idbuf, j); /* ID packet should carry mode info [3DP] */
  473. dbg("Init 2: Mode %d. ID Length %d.", m, j);
  474. if (j <= 0) { /* Read ID failed. Happens in 1-bit mode on PP */
  475. msleep(SW_TIMEOUT);
  476. i = sw_read_packet(gameport, buf, SW_LENGTH, 0); /* Retry reading packet */
  477. m |= sw_guess_mode(buf, i);
  478. dbg("Init 2b: Mode %d. Length %d.", m, i);
  479. if (!i) {
  480. err = -ENODEV;
  481. goto fail2;
  482. }
  483. msleep(SW_TIMEOUT);
  484. j = sw_read_packet(gameport, idbuf, SW_LENGTH, i); /* Retry reading ID */
  485. dbg("Init 2c: ID Length %d.", j);
  486. }
  487. sw->type = -1;
  488. k = SW_FAIL; /* Try SW_FAIL times */
  489. l = 0;
  490. do {
  491. k--;
  492. msleep(SW_TIMEOUT);
  493. i = sw_read_packet(gameport, buf, SW_LENGTH, 0); /* Read data packet */
  494. dbg("Init 3: Mode %d. Length %d. Last %d. Tries %d.", m, i, l, k);
  495. if (i > l) { /* Longer? As we can only lose bits, it makes */
  496. /* no sense to try detection for a packet shorter */
  497. l = i; /* than the previous one */
  498. sw->number = 1;
  499. sw->gameport = gameport;
  500. sw->length = i;
  501. sw->bits = m;
  502. dbg("Init 3a: Case %d.\n", i * m);
  503. switch (i * m) {
  504. case 60:
  505. sw->number++;
  506. fallthrough;
  507. case 45: /* Ambiguous packet length */
  508. if (j <= 40) { /* ID length less or eq 40 -> FSP */
  509. fallthrough;
  510. case 43:
  511. sw->type = SW_ID_FSP;
  512. break;
  513. }
  514. sw->number++;
  515. fallthrough;
  516. case 30:
  517. sw->number++;
  518. fallthrough;
  519. case 15:
  520. sw->type = SW_ID_GP;
  521. break;
  522. case 33:
  523. case 31:
  524. sw->type = SW_ID_FFW;
  525. break;
  526. case 48: /* Ambiguous */
  527. if (j == 14) { /* ID length 14*3 -> FFP */
  528. sw->type = SW_ID_FFP;
  529. sprintf(comment, " [AC %s]", str_off_on(sw_get_bits(idbuf,38,1,3)));
  530. } else
  531. sw->type = SW_ID_PP;
  532. break;
  533. case 66:
  534. sw->bits = 3;
  535. fallthrough;
  536. case 198:
  537. sw->length = 22;
  538. fallthrough;
  539. case 64:
  540. sw->type = SW_ID_3DP;
  541. if (j == 160)
  542. sw_3dp_id(idbuf, comment, sizeof(comment));
  543. break;
  544. }
  545. }
  546. } while (k && sw->type == -1);
  547. if (sw->type == -1) {
  548. printk(KERN_WARNING "sidewinder.c: unknown joystick device detected "
  549. "on %s, contact <vojtech@ucw.cz>\n", gameport->phys);
  550. sw_print_packet("ID", j * 3, idbuf, 3);
  551. sw_print_packet("Data", i * m, buf, m);
  552. err = -ENODEV;
  553. goto fail2;
  554. }
  555. #ifdef SW_DEBUG
  556. sw_print_packet("ID", j * 3, idbuf, 3);
  557. sw_print_packet("Data", i * m, buf, m);
  558. #endif
  559. gameport_set_poll_handler(gameport, sw_poll);
  560. gameport_set_poll_interval(gameport, 20);
  561. k = i;
  562. l = j;
  563. for (i = 0; i < sw->number; i++) {
  564. int bits, code;
  565. snprintf(sw->name, sizeof(sw->name),
  566. "Microsoft SideWinder %s", sw_name[sw->type]);
  567. snprintf(sw->phys[i], sizeof(sw->phys[i]),
  568. "%s/input%d", gameport->phys, i);
  569. sw->dev[i] = input_dev = input_allocate_device();
  570. if (!input_dev) {
  571. err = -ENOMEM;
  572. goto fail3;
  573. }
  574. input_dev->name = sw->name;
  575. input_dev->phys = sw->phys[i];
  576. input_dev->id.bustype = BUS_GAMEPORT;
  577. input_dev->id.vendor = GAMEPORT_ID_VENDOR_MICROSOFT;
  578. input_dev->id.product = sw->type;
  579. input_dev->id.version = 0x0100;
  580. input_dev->dev.parent = &gameport->dev;
  581. input_set_drvdata(input_dev, sw);
  582. input_dev->open = sw_open;
  583. input_dev->close = sw_close;
  584. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  585. for (j = 0; (bits = sw_bit[sw->type][j]); j++) {
  586. int min, max, fuzz, flat;
  587. code = sw_abs[sw->type][j];
  588. min = bits == 1 ? -1 : 0;
  589. max = (1 << bits) - 1;
  590. fuzz = (bits >> 1) >= 2 ? 1 << ((bits >> 1) - 2) : 0;
  591. flat = code == ABS_THROTTLE || bits < 5 ?
  592. 0 : 1 << (bits - 5);
  593. input_set_abs_params(input_dev, code,
  594. min, max, fuzz, flat);
  595. }
  596. for (j = 0; (code = sw_btn[sw->type][j]); j++)
  597. __set_bit(code, input_dev->keybit);
  598. dbg("%s%s [%d-bit id %d data %d]\n", sw->name, comment, m, l, k);
  599. err = input_register_device(sw->dev[i]);
  600. if (err)
  601. goto fail4;
  602. }
  603. out: kfree(buf);
  604. kfree(idbuf);
  605. return err;
  606. fail4: input_free_device(sw->dev[i]);
  607. fail3: while (--i >= 0)
  608. input_unregister_device(sw->dev[i]);
  609. fail2: gameport_close(gameport);
  610. fail1: gameport_set_drvdata(gameport, NULL);
  611. kfree(sw);
  612. goto out;
  613. }
  614. static void sw_disconnect(struct gameport *gameport)
  615. {
  616. struct sw *sw = gameport_get_drvdata(gameport);
  617. int i;
  618. for (i = 0; i < sw->number; i++)
  619. input_unregister_device(sw->dev[i]);
  620. gameport_close(gameport);
  621. gameport_set_drvdata(gameport, NULL);
  622. kfree(sw);
  623. }
  624. static struct gameport_driver sw_drv = {
  625. .driver = {
  626. .name = "sidewinder",
  627. .owner = THIS_MODULE,
  628. },
  629. .description = DRIVER_DESC,
  630. .connect = sw_connect,
  631. .disconnect = sw_disconnect,
  632. };
  633. module_gameport_driver(sw_drv);