sidewinder.c 20 KB

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