sidewinder.c 21 KB

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