sidewinder.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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. #include <linux/delay.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/slab.h>
  26. #include <linux/input.h>
  27. #include <linux/gameport.h>
  28. #include <linux/jiffies.h>
  29. #define DRIVER_DESC "Microsoft SideWinder joystick family driver"
  30. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  31. MODULE_DESCRIPTION(DRIVER_DESC);
  32. MODULE_LICENSE("GPL");
  33. /*
  34. * These are really magic values. Changing them can make a problem go away,
  35. * as well as break everything.
  36. */
  37. #undef SW_DEBUG
  38. #undef SW_DEBUG_DATA
  39. #define SW_START 600 /* The time we wait for the first bit [600 us] */
  40. #define SW_STROBE 60 /* Max time per bit [60 us] */
  41. #define SW_TIMEOUT 6 /* Wait for everything to settle [6 ms] */
  42. #define SW_KICK 45 /* Wait after A0 fall till kick [45 us] */
  43. #define SW_END 8 /* Number of bits before end of packet to kick */
  44. #define SW_FAIL 16 /* Number of packet read errors to fail and reinitialize */
  45. #define SW_BAD 2 /* Number of packet read errors to switch off 3d Pro optimization */
  46. #define SW_OK 64 /* Number of packet read successes to switch optimization back on */
  47. #define SW_LENGTH 512 /* Max number of bits in a packet */
  48. #ifdef SW_DEBUG
  49. #define dbg(format, arg...) printk(KERN_DEBUG __FILE__ ": " format "\n" , ## arg)
  50. #else
  51. #define dbg(format, arg...) do {} while (0)
  52. #endif
  53. /*
  54. * SideWinder joystick types ...
  55. */
  56. #define SW_ID_3DP 0
  57. #define SW_ID_GP 1
  58. #define SW_ID_PP 2
  59. #define SW_ID_FFP 3
  60. #define SW_ID_FSP 4
  61. #define SW_ID_FFW 5
  62. /*
  63. * Names, buttons, axes ...
  64. */
  65. static char *sw_name[] = { "3D Pro", "GamePad", "Precision Pro", "Force Feedback Pro", "FreeStyle Pro",
  66. "Force Feedback Wheel" };
  67. static char sw_abs[][7] = {
  68. { ABS_X, ABS_Y, ABS_RZ, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y },
  69. { ABS_X, ABS_Y },
  70. { ABS_X, ABS_Y, ABS_RZ, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y },
  71. { ABS_X, ABS_Y, ABS_RZ, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y },
  72. { ABS_X, ABS_Y, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y },
  73. { ABS_RX, ABS_RUDDER, ABS_THROTTLE }};
  74. static char sw_bit[][7] = {
  75. { 10, 10, 9, 10, 1, 1 },
  76. { 1, 1 },
  77. { 10, 10, 6, 7, 1, 1 },
  78. { 10, 10, 6, 7, 1, 1 },
  79. { 10, 10, 6, 1, 1 },
  80. { 10, 7, 7, 1, 1 }};
  81. static short sw_btn[][12] = {
  82. { BTN_TRIGGER, BTN_TOP, BTN_THUMB, BTN_THUMB2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_MODE },
  83. { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_START, BTN_MODE },
  84. { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_SELECT },
  85. { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_SELECT },
  86. { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_START, BTN_MODE, BTN_SELECT },
  87. { BTN_TRIGGER, BTN_TOP, BTN_THUMB, BTN_THUMB2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4 }};
  88. static struct {
  89. int x;
  90. int y;
  91. } sw_hat_to_axis[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
  92. struct sw {
  93. struct gameport *gameport;
  94. struct input_dev *dev[4];
  95. char name[64];
  96. char phys[4][32];
  97. int length;
  98. int type;
  99. int bits;
  100. int number;
  101. int fail;
  102. int ok;
  103. int reads;
  104. int bads;
  105. };
  106. /*
  107. * sw_read_packet() is a function which reads either a data packet, or an
  108. * identification packet from a SideWinder joystick. The protocol is very,
  109. * very, very braindamaged. Microsoft patented it in US patent #5628686.
  110. */
  111. static int sw_read_packet(struct gameport *gameport, unsigned char *buf, int length, int id)
  112. {
  113. unsigned long flags;
  114. int timeout, bitout, sched, i, kick, start, strobe;
  115. unsigned char pending, u, v;
  116. i = -id; /* Don't care about data, only want ID */
  117. timeout = id ? gameport_time(gameport, SW_TIMEOUT * 1000) : 0; /* Set up global timeout for ID packet */
  118. kick = id ? gameport_time(gameport, SW_KICK) : 0; /* Set up kick timeout for ID packet */
  119. start = gameport_time(gameport, SW_START);
  120. strobe = gameport_time(gameport, SW_STROBE);
  121. bitout = start;
  122. pending = 0;
  123. sched = 0;
  124. local_irq_save(flags); /* Quiet, please */
  125. gameport_trigger(gameport); /* Trigger */
  126. v = gameport_read(gameport);
  127. do {
  128. bitout--;
  129. u = v;
  130. v = gameport_read(gameport);
  131. } while (!(~v & u & 0x10) && (bitout > 0)); /* Wait for first falling edge on clock */
  132. if (bitout > 0)
  133. bitout = strobe; /* Extend time if not timed out */
  134. while ((timeout > 0 || bitout > 0) && (i < length)) {
  135. timeout--;
  136. bitout--; /* Decrement timers */
  137. sched--;
  138. u = v;
  139. v = gameport_read(gameport);
  140. if ((~u & v & 0x10) && (bitout > 0)) { /* Rising edge on clock - data bit */
  141. if (i >= 0) /* Want this data */
  142. buf[i] = v >> 5; /* Store it */
  143. i++; /* Advance index */
  144. bitout = strobe; /* Extend timeout for next bit */
  145. }
  146. if (kick && (~v & u & 0x01)) { /* Falling edge on axis 0 */
  147. sched = kick; /* Schedule second trigger */
  148. kick = 0; /* Don't schedule next time on falling edge */
  149. pending = 1; /* Mark schedule */
  150. }
  151. if (pending && sched < 0 && (i > -SW_END)) { /* Second trigger time */
  152. gameport_trigger(gameport); /* Trigger */
  153. bitout = start; /* Long bit timeout */
  154. pending = 0; /* Unmark schedule */
  155. timeout = 0; /* Switch from global to bit timeouts */
  156. }
  157. }
  158. local_irq_restore(flags); /* Done - relax */
  159. #ifdef SW_DEBUG_DATA
  160. {
  161. int j;
  162. printk(KERN_DEBUG "sidewinder.c: Read %d triplets. [", i);
  163. for (j = 0; j < i; j++) printk("%d", buf[j]);
  164. printk("]\n");
  165. }
  166. #endif
  167. return i;
  168. }
  169. /*
  170. * sw_get_bits() and GB() compose bits from the triplet buffer into a __u64.
  171. * Parameter 'pos' is bit number inside packet where to start at, 'num' is number
  172. * of bits to be read, 'shift' is offset in the resulting __u64 to start at, bits
  173. * is number of bits per triplet.
  174. */
  175. #define GB(pos,num) sw_get_bits(buf, pos, num, sw->bits)
  176. static __u64 sw_get_bits(unsigned char *buf, int pos, int num, char bits)
  177. {
  178. __u64 data = 0;
  179. int tri = pos % bits; /* Start position */
  180. int i = pos / bits;
  181. int bit = 0;
  182. while (num--) {
  183. data |= (__u64)((buf[i] >> tri++) & 1) << bit++; /* Transfer bit */
  184. if (tri == bits) {
  185. i++; /* Next triplet */
  186. tri = 0;
  187. }
  188. }
  189. return data;
  190. }
  191. /*
  192. * sw_init_digital() initializes a SideWinder 3D Pro joystick
  193. * into digital mode.
  194. */
  195. static void sw_init_digital(struct gameport *gameport)
  196. {
  197. int seq[] = { 140, 140+725, 140+300, 0 };
  198. unsigned long flags;
  199. int i, t;
  200. local_irq_save(flags);
  201. i = 0;
  202. do {
  203. gameport_trigger(gameport); /* Trigger */
  204. t = gameport_time(gameport, SW_TIMEOUT * 1000);
  205. while ((gameport_read(gameport) & 1) && t) t--; /* Wait for axis to fall back to 0 */
  206. udelay(seq[i]); /* Delay magic time */
  207. } while (seq[++i]);
  208. gameport_trigger(gameport); /* Last trigger */
  209. local_irq_restore(flags);
  210. }
  211. /*
  212. * sw_parity() computes parity of __u64
  213. */
  214. static int sw_parity(__u64 t)
  215. {
  216. int x = t ^ (t >> 32);
  217. x ^= x >> 16;
  218. x ^= x >> 8;
  219. x ^= x >> 4;
  220. x ^= x >> 2;
  221. x ^= x >> 1;
  222. return x & 1;
  223. }
  224. /*
  225. * sw_ccheck() checks synchronization bits and computes checksum of nibbles.
  226. */
  227. static int sw_check(__u64 t)
  228. {
  229. unsigned char sum = 0;
  230. if ((t & 0x8080808080808080ULL) ^ 0x80) /* Sync */
  231. return -1;
  232. while (t) { /* Sum */
  233. sum += t & 0xf;
  234. t >>= 4;
  235. }
  236. return sum & 0xf;
  237. }
  238. /*
  239. * sw_parse() analyzes SideWinder joystick data, and writes the results into
  240. * the axes and buttons arrays.
  241. */
  242. static int sw_parse(unsigned char *buf, struct sw *sw)
  243. {
  244. int hat, i, j;
  245. struct input_dev *dev;
  246. switch (sw->type) {
  247. case SW_ID_3DP:
  248. if (sw_check(GB(0,64)) || (hat = (GB(6,1) << 3) | GB(60,3)) > 8)
  249. return -1;
  250. dev = sw->dev[0];
  251. input_report_abs(dev, ABS_X, (GB( 3,3) << 7) | GB(16,7));
  252. input_report_abs(dev, ABS_Y, (GB( 0,3) << 7) | GB(24,7));
  253. input_report_abs(dev, ABS_RZ, (GB(35,2) << 7) | GB(40,7));
  254. input_report_abs(dev, ABS_THROTTLE, (GB(32,3) << 7) | GB(48,7));
  255. input_report_abs(dev, ABS_HAT0X, sw_hat_to_axis[hat].x);
  256. input_report_abs(dev, ABS_HAT0Y, sw_hat_to_axis[hat].y);
  257. for (j = 0; j < 7; j++)
  258. input_report_key(dev, sw_btn[SW_ID_3DP][j], !GB(j+8,1));
  259. input_report_key(dev, BTN_BASE4, !GB(38,1));
  260. input_report_key(dev, BTN_BASE5, !GB(37,1));
  261. input_sync(dev);
  262. return 0;
  263. case SW_ID_GP:
  264. for (i = 0; i < sw->number; i ++) {
  265. if (sw_parity(GB(i*15,15)))
  266. return -1;
  267. input_report_abs(sw->dev[i], ABS_X, GB(i*15+3,1) - GB(i*15+2,1));
  268. input_report_abs(sw->dev[i], ABS_Y, GB(i*15+0,1) - GB(i*15+1,1));
  269. for (j = 0; j < 10; j++)
  270. input_report_key(sw->dev[i], sw_btn[SW_ID_GP][j], !GB(i*15+j+4,1));
  271. input_sync(sw->dev[i]);
  272. }
  273. return 0;
  274. case SW_ID_PP:
  275. case SW_ID_FFP:
  276. if (!sw_parity(GB(0,48)) || (hat = GB(42,4)) > 8)
  277. return -1;
  278. dev = sw->dev[0];
  279. input_report_abs(dev, ABS_X, GB( 9,10));
  280. input_report_abs(dev, ABS_Y, GB(19,10));
  281. input_report_abs(dev, ABS_RZ, GB(36, 6));
  282. input_report_abs(dev, ABS_THROTTLE, GB(29, 7));
  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 < 9; j++)
  286. input_report_key(dev, sw_btn[SW_ID_PP][j], !GB(j,1));
  287. input_sync(dev);
  288. return 0;
  289. case SW_ID_FSP:
  290. if (!sw_parity(GB(0,43)) || (hat = GB(28,4)) > 8)
  291. return -1;
  292. dev = sw->dev[0];
  293. input_report_abs(dev, ABS_X, GB( 0,10));
  294. input_report_abs(dev, ABS_Y, GB(16,10));
  295. input_report_abs(dev, ABS_THROTTLE, GB(32, 6));
  296. input_report_abs(dev, ABS_HAT0X, sw_hat_to_axis[hat].x);
  297. input_report_abs(dev, ABS_HAT0Y, sw_hat_to_axis[hat].y);
  298. for (j = 0; j < 6; j++)
  299. input_report_key(dev, sw_btn[SW_ID_FSP][j], !GB(j+10,1));
  300. input_report_key(dev, BTN_TR, !GB(26,1));
  301. input_report_key(dev, BTN_START, !GB(27,1));
  302. input_report_key(dev, BTN_MODE, !GB(38,1));
  303. input_report_key(dev, BTN_SELECT, !GB(39,1));
  304. input_sync(dev);
  305. return 0;
  306. case SW_ID_FFW:
  307. if (!sw_parity(GB(0,33)))
  308. return -1;
  309. dev = sw->dev[0];
  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 = input_get_drvdata(dev);
  389. gameport_start_polling(sw->gameport);
  390. return 0;
  391. }
  392. static void sw_close(struct input_dev *dev)
  393. {
  394. struct sw *sw = input_get_drvdata(dev);
  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, size_t size)
  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. snprintf(comment, size, " [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. struct input_dev *input_dev;
  449. int i, j, k, l;
  450. int err = 0;
  451. unsigned char *buf = NULL; /* [SW_LENGTH] */
  452. unsigned char *idbuf = NULL; /* [SW_LENGTH] */
  453. unsigned char m = 1;
  454. char comment[40];
  455. comment[0] = 0;
  456. sw = kzalloc(sizeof(struct sw), GFP_KERNEL);
  457. buf = kmalloc(SW_LENGTH, GFP_KERNEL);
  458. idbuf = kmalloc(SW_LENGTH, GFP_KERNEL);
  459. if (!sw || !buf || !idbuf) {
  460. err = -ENOMEM;
  461. goto fail1;
  462. }
  463. sw->gameport = gameport;
  464. gameport_set_drvdata(gameport, sw);
  465. err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
  466. if (err)
  467. goto fail1;
  468. dbg("Init 0: Opened %s, io %#x, speed %d",
  469. gameport->phys, gameport->io, gameport->speed);
  470. i = sw_read_packet(gameport, buf, SW_LENGTH, 0); /* Read normal packet */
  471. msleep(SW_TIMEOUT);
  472. dbg("Init 1: Mode %d. Length %d.", m , i);
  473. if (!i) { /* No data. 3d Pro analog mode? */
  474. sw_init_digital(gameport); /* Switch to digital */
  475. msleep(SW_TIMEOUT);
  476. i = sw_read_packet(gameport, buf, SW_LENGTH, 0); /* Retry reading packet */
  477. msleep(SW_TIMEOUT);
  478. dbg("Init 1b: Length %d.", i);
  479. if (!i) { /* No data -> FAIL */
  480. err = -ENODEV;
  481. goto fail2;
  482. }
  483. }
  484. j = sw_read_packet(gameport, idbuf, SW_LENGTH, i); /* Read ID. This initializes the stick */
  485. m |= sw_guess_mode(idbuf, j); /* ID packet should carry mode info [3DP] */
  486. dbg("Init 2: Mode %d. ID Length %d.", m, j);
  487. if (j <= 0) { /* Read ID failed. Happens in 1-bit mode on PP */
  488. msleep(SW_TIMEOUT);
  489. i = sw_read_packet(gameport, buf, SW_LENGTH, 0); /* Retry reading packet */
  490. m |= sw_guess_mode(buf, i);
  491. dbg("Init 2b: Mode %d. Length %d.", m, i);
  492. if (!i) {
  493. err = -ENODEV;
  494. goto fail2;
  495. }
  496. msleep(SW_TIMEOUT);
  497. j = sw_read_packet(gameport, idbuf, SW_LENGTH, i); /* Retry reading ID */
  498. dbg("Init 2c: ID Length %d.", j);
  499. }
  500. sw->type = -1;
  501. k = SW_FAIL; /* Try SW_FAIL times */
  502. l = 0;
  503. do {
  504. k--;
  505. msleep(SW_TIMEOUT);
  506. i = sw_read_packet(gameport, buf, SW_LENGTH, 0); /* Read data packet */
  507. dbg("Init 3: Mode %d. Length %d. Last %d. Tries %d.", m, i, l, k);
  508. if (i > l) { /* Longer? As we can only lose bits, it makes */
  509. /* no sense to try detection for a packet shorter */
  510. l = i; /* than the previous one */
  511. sw->number = 1;
  512. sw->gameport = gameport;
  513. sw->length = i;
  514. sw->bits = m;
  515. dbg("Init 3a: Case %d.\n", i * m);
  516. switch (i * m) {
  517. case 60:
  518. sw->number++; /* fall through */
  519. case 45: /* Ambiguous packet length */
  520. if (j <= 40) { /* ID length less or eq 40 -> FSP */
  521. case 43:
  522. sw->type = SW_ID_FSP;
  523. break;
  524. }
  525. sw->number++; /* fall through */
  526. case 30:
  527. sw->number++; /* fall through */
  528. case 15:
  529. sw->type = SW_ID_GP;
  530. break;
  531. case 33:
  532. case 31:
  533. sw->type = SW_ID_FFW;
  534. break;
  535. case 48: /* Ambiguous */
  536. if (j == 14) { /* ID length 14*3 -> FFP */
  537. sw->type = SW_ID_FFP;
  538. sprintf(comment, " [AC %s]", sw_get_bits(idbuf,38,1,3) ? "off" : "on");
  539. } else
  540. sw->type = SW_ID_PP;
  541. break;
  542. case 66:
  543. sw->bits = 3; /* fall through */
  544. case 198:
  545. sw->length = 22; /* fall through */
  546. case 64:
  547. sw->type = SW_ID_3DP;
  548. if (j == 160)
  549. sw_3dp_id(idbuf, comment, sizeof(comment));
  550. break;
  551. }
  552. }
  553. } while (k && sw->type == -1);
  554. if (sw->type == -1) {
  555. printk(KERN_WARNING "sidewinder.c: unknown joystick device detected "
  556. "on %s, contact <vojtech@ucw.cz>\n", gameport->phys);
  557. sw_print_packet("ID", j * 3, idbuf, 3);
  558. sw_print_packet("Data", i * m, buf, m);
  559. err = -ENODEV;
  560. goto fail2;
  561. }
  562. #ifdef SW_DEBUG
  563. sw_print_packet("ID", j * 3, idbuf, 3);
  564. sw_print_packet("Data", i * m, buf, m);
  565. #endif
  566. gameport_set_poll_handler(gameport, sw_poll);
  567. gameport_set_poll_interval(gameport, 20);
  568. k = i;
  569. l = j;
  570. for (i = 0; i < sw->number; i++) {
  571. int bits, code;
  572. snprintf(sw->name, sizeof(sw->name),
  573. "Microsoft SideWinder %s", sw_name[sw->type]);
  574. snprintf(sw->phys[i], sizeof(sw->phys[i]),
  575. "%s/input%d", gameport->phys, i);
  576. sw->dev[i] = input_dev = input_allocate_device();
  577. if (!input_dev) {
  578. err = -ENOMEM;
  579. goto fail3;
  580. }
  581. input_dev->name = sw->name;
  582. input_dev->phys = sw->phys[i];
  583. input_dev->id.bustype = BUS_GAMEPORT;
  584. input_dev->id.vendor = GAMEPORT_ID_VENDOR_MICROSOFT;
  585. input_dev->id.product = sw->type;
  586. input_dev->id.version = 0x0100;
  587. input_dev->dev.parent = &gameport->dev;
  588. input_set_drvdata(input_dev, sw);
  589. input_dev->open = sw_open;
  590. input_dev->close = sw_close;
  591. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  592. for (j = 0; (bits = sw_bit[sw->type][j]); j++) {
  593. int min, max, fuzz, flat;
  594. code = sw_abs[sw->type][j];
  595. min = bits == 1 ? -1 : 0;
  596. max = (1 << bits) - 1;
  597. fuzz = (bits >> 1) >= 2 ? 1 << ((bits >> 1) - 2) : 0;
  598. flat = code == ABS_THROTTLE || bits < 5 ?
  599. 0 : 1 << (bits - 5);
  600. input_set_abs_params(input_dev, code,
  601. min, max, fuzz, flat);
  602. }
  603. for (j = 0; (code = sw_btn[sw->type][j]); j++)
  604. __set_bit(code, input_dev->keybit);
  605. dbg("%s%s [%d-bit id %d data %d]\n", sw->name, comment, m, l, k);
  606. err = input_register_device(sw->dev[i]);
  607. if (err)
  608. goto fail4;
  609. }
  610. out: kfree(buf);
  611. kfree(idbuf);
  612. return err;
  613. fail4: input_free_device(sw->dev[i]);
  614. fail3: while (--i >= 0)
  615. input_unregister_device(sw->dev[i]);
  616. fail2: gameport_close(gameport);
  617. fail1: gameport_set_drvdata(gameport, NULL);
  618. kfree(sw);
  619. goto out;
  620. }
  621. static void sw_disconnect(struct gameport *gameport)
  622. {
  623. struct sw *sw = gameport_get_drvdata(gameport);
  624. int i;
  625. for (i = 0; i < sw->number; i++)
  626. input_unregister_device(sw->dev[i]);
  627. gameport_close(gameport);
  628. gameport_set_drvdata(gameport, NULL);
  629. kfree(sw);
  630. }
  631. static struct gameport_driver sw_drv = {
  632. .driver = {
  633. .name = "sidewinder",
  634. .owner = THIS_MODULE,
  635. },
  636. .description = DRIVER_DESC,
  637. .connect = sw_connect,
  638. .disconnect = sw_disconnect,
  639. };
  640. module_gameport_driver(sw_drv);