sidewinder.c 20 KB

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