sidewinder.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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/input.h>
  31. #include <linux/gameport.h>
  32. #include <linux/jiffies.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;
  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. dev = sw->dev[0];
  255. input_report_abs(dev, ABS_X, (GB( 3,3) << 7) | GB(16,7));
  256. input_report_abs(dev, ABS_Y, (GB( 0,3) << 7) | GB(24,7));
  257. input_report_abs(dev, ABS_RZ, (GB(35,2) << 7) | GB(40,7));
  258. input_report_abs(dev, ABS_THROTTLE, (GB(32,3) << 7) | GB(48,7));
  259. input_report_abs(dev, ABS_HAT0X, sw_hat_to_axis[hat].x);
  260. input_report_abs(dev, ABS_HAT0Y, sw_hat_to_axis[hat].y);
  261. for (j = 0; j < 7; j++)
  262. input_report_key(dev, sw_btn[SW_ID_3DP][j], !GB(j+8,1));
  263. input_report_key(dev, BTN_BASE4, !GB(38,1));
  264. input_report_key(dev, BTN_BASE5, !GB(37,1));
  265. input_sync(dev);
  266. return 0;
  267. case SW_ID_GP:
  268. for (i = 0; i < sw->number; i ++) {
  269. if (sw_parity(GB(i*15,15)))
  270. return -1;
  271. input_report_abs(sw->dev[i], ABS_X, GB(i*15+3,1) - GB(i*15+2,1));
  272. input_report_abs(sw->dev[i], ABS_Y, GB(i*15+0,1) - GB(i*15+1,1));
  273. for (j = 0; j < 10; j++)
  274. input_report_key(sw->dev[i], sw_btn[SW_ID_GP][j], !GB(i*15+j+4,1));
  275. input_sync(sw->dev[i]);
  276. }
  277. return 0;
  278. case SW_ID_PP:
  279. case SW_ID_FFP:
  280. if (!sw_parity(GB(0,48)) || (hat = GB(42,4)) > 8)
  281. return -1;
  282. dev = sw->dev[0];
  283. input_report_abs(dev, ABS_X, GB( 9,10));
  284. input_report_abs(dev, ABS_Y, GB(19,10));
  285. input_report_abs(dev, ABS_RZ, GB(36, 6));
  286. input_report_abs(dev, ABS_THROTTLE, GB(29, 7));
  287. input_report_abs(dev, ABS_HAT0X, sw_hat_to_axis[hat].x);
  288. input_report_abs(dev, ABS_HAT0Y, sw_hat_to_axis[hat].y);
  289. for (j = 0; j < 9; j++)
  290. input_report_key(dev, sw_btn[SW_ID_PP][j], !GB(j,1));
  291. input_sync(dev);
  292. return 0;
  293. case SW_ID_FSP:
  294. if (!sw_parity(GB(0,43)) || (hat = GB(28,4)) > 8)
  295. return -1;
  296. dev = sw->dev[0];
  297. input_report_abs(dev, ABS_X, GB( 0,10));
  298. input_report_abs(dev, ABS_Y, GB(16,10));
  299. input_report_abs(dev, ABS_THROTTLE, GB(32, 6));
  300. input_report_abs(dev, ABS_HAT0X, sw_hat_to_axis[hat].x);
  301. input_report_abs(dev, ABS_HAT0Y, sw_hat_to_axis[hat].y);
  302. for (j = 0; j < 6; j++)
  303. input_report_key(dev, sw_btn[SW_ID_FSP][j], !GB(j+10,1));
  304. input_report_key(dev, BTN_TR, !GB(26,1));
  305. input_report_key(dev, BTN_START, !GB(27,1));
  306. input_report_key(dev, BTN_MODE, !GB(38,1));
  307. input_report_key(dev, BTN_SELECT, !GB(39,1));
  308. input_sync(dev);
  309. return 0;
  310. case SW_ID_FFW:
  311. if (!sw_parity(GB(0,33)))
  312. return -1;
  313. dev = sw->dev[0];
  314. input_report_abs(dev, ABS_RX, GB( 0,10));
  315. input_report_abs(dev, ABS_RUDDER, GB(10, 6));
  316. input_report_abs(dev, ABS_THROTTLE, GB(16, 6));
  317. for (j = 0; j < 8; j++)
  318. input_report_key(dev, sw_btn[SW_ID_FFW][j], !GB(j+22,1));
  319. input_sync(dev);
  320. return 0;
  321. }
  322. return -1;
  323. }
  324. /*
  325. * sw_read() reads SideWinder joystick data, and reinitializes
  326. * the joystick in case of persistent problems. This is the function that is
  327. * called from the generic code to poll the joystick.
  328. */
  329. static int sw_read(struct sw *sw)
  330. {
  331. unsigned char buf[SW_LENGTH];
  332. int i;
  333. i = sw_read_packet(sw->gameport, buf, sw->length, 0);
  334. if (sw->type == SW_ID_3DP && sw->length == 66 && i != 66) { /* Broken packet, try to fix */
  335. if (i == 64 && !sw_check(sw_get_bits(buf,0,64,1))) { /* Last init failed, 1 bit mode */
  336. printk(KERN_WARNING "sidewinder.c: Joystick in wrong mode on %s"
  337. " - going to reinitialize.\n", sw->gameport->phys);
  338. sw->fail = SW_FAIL; /* Reinitialize */
  339. i = 128; /* Bogus value */
  340. }
  341. if (i < 66 && GB(0,64) == GB(i*3-66,64)) /* 1 == 3 */
  342. i = 66; /* Everything is fine */
  343. if (i < 66 && GB(0,64) == GB(66,64)) /* 1 == 2 */
  344. i = 66; /* Everything is fine */
  345. if (i < 66 && GB(i*3-132,64) == GB(i*3-66,64)) { /* 2 == 3 */
  346. memmove(buf, buf + i - 22, 22); /* Move data */
  347. i = 66; /* Carry on */
  348. }
  349. }
  350. if (i == sw->length && !sw_parse(buf, sw)) { /* Parse data */
  351. sw->fail = 0;
  352. sw->ok++;
  353. if (sw->type == SW_ID_3DP && sw->length == 66 /* Many packets OK */
  354. && sw->ok > SW_OK) {
  355. printk(KERN_INFO "sidewinder.c: No more trouble on %s"
  356. " - enabling optimization again.\n", sw->gameport->phys);
  357. sw->length = 22;
  358. }
  359. return 0;
  360. }
  361. sw->ok = 0;
  362. sw->fail++;
  363. if (sw->type == SW_ID_3DP && sw->length == 22 && sw->fail > SW_BAD) { /* Consecutive bad packets */
  364. printk(KERN_INFO "sidewinder.c: Many bit errors on %s"
  365. " - disabling optimization.\n", sw->gameport->phys);
  366. sw->length = 66;
  367. }
  368. if (sw->fail < SW_FAIL)
  369. return -1; /* Not enough, don't reinitialize yet */
  370. printk(KERN_WARNING "sidewinder.c: Too many bit errors on %s"
  371. " - reinitializing joystick.\n", sw->gameport->phys);
  372. if (!i && sw->type == SW_ID_3DP) { /* 3D Pro can be in analog mode */
  373. mdelay(3 * SW_TIMEOUT);
  374. sw_init_digital(sw->gameport);
  375. }
  376. mdelay(SW_TIMEOUT);
  377. i = sw_read_packet(sw->gameport, buf, SW_LENGTH, 0); /* Read normal data packet */
  378. mdelay(SW_TIMEOUT);
  379. sw_read_packet(sw->gameport, buf, SW_LENGTH, i); /* Read ID packet, this initializes the stick */
  380. sw->fail = SW_FAIL;
  381. return -1;
  382. }
  383. static void sw_poll(struct gameport *gameport)
  384. {
  385. struct sw *sw = gameport_get_drvdata(gameport);
  386. sw->reads++;
  387. if (sw_read(sw))
  388. sw->bads++;
  389. }
  390. static int sw_open(struct input_dev *dev)
  391. {
  392. struct sw *sw = input_get_drvdata(dev);
  393. gameport_start_polling(sw->gameport);
  394. return 0;
  395. }
  396. static void sw_close(struct input_dev *dev)
  397. {
  398. struct sw *sw = input_get_drvdata(dev);
  399. gameport_stop_polling(sw->gameport);
  400. }
  401. /*
  402. * sw_print_packet() prints the contents of a SideWinder packet.
  403. */
  404. static void sw_print_packet(char *name, int length, unsigned char *buf, char bits)
  405. {
  406. int i;
  407. printk(KERN_INFO "sidewinder.c: %s packet, %d bits. [", name, length);
  408. for (i = (((length + 3) >> 2) - 1); i >= 0; i--)
  409. printk("%x", (int)sw_get_bits(buf, i << 2, 4, bits));
  410. printk("]\n");
  411. }
  412. /*
  413. * sw_3dp_id() translates the 3DP id into a human legible string.
  414. * Unfortunately I don't know how to do this for the other SW types.
  415. */
  416. static void sw_3dp_id(unsigned char *buf, char *comment, size_t size)
  417. {
  418. int i;
  419. char pnp[8], rev[9];
  420. for (i = 0; i < 7; i++) /* ASCII PnP ID */
  421. pnp[i] = sw_get_bits(buf, 24+8*i, 8, 1);
  422. for (i = 0; i < 8; i++) /* ASCII firmware revision */
  423. rev[i] = sw_get_bits(buf, 88+8*i, 8, 1);
  424. pnp[7] = rev[8] = 0;
  425. snprintf(comment, size, " [PnP %d.%02d id %s rev %s]",
  426. (int) ((sw_get_bits(buf, 8, 6, 1) << 6) | /* Two 6-bit values */
  427. sw_get_bits(buf, 16, 6, 1)) / 100,
  428. (int) ((sw_get_bits(buf, 8, 6, 1) << 6) |
  429. sw_get_bits(buf, 16, 6, 1)) % 100,
  430. pnp, rev);
  431. }
  432. /*
  433. * sw_guess_mode() checks the upper two button bits for toggling -
  434. * indication of that the joystick is in 3-bit mode. This is documented
  435. * behavior for 3DP ID packet, and for example the FSP does this in
  436. * normal packets instead. Fun ...
  437. */
  438. static int sw_guess_mode(unsigned char *buf, int len)
  439. {
  440. int i;
  441. unsigned char xor = 0;
  442. for (i = 1; i < len; i++)
  443. xor |= (buf[i - 1] ^ buf[i]) & 6;
  444. return !!xor * 2 + 1;
  445. }
  446. /*
  447. * sw_connect() probes for SideWinder type joysticks.
  448. */
  449. static int sw_connect(struct gameport *gameport, struct gameport_driver *drv)
  450. {
  451. struct sw *sw;
  452. struct input_dev *input_dev;
  453. int i, j, k, l;
  454. int err = 0;
  455. unsigned char *buf = NULL; /* [SW_LENGTH] */
  456. unsigned char *idbuf = NULL; /* [SW_LENGTH] */
  457. unsigned char m = 1;
  458. char comment[40];
  459. comment[0] = 0;
  460. sw = kzalloc(sizeof(struct sw), GFP_KERNEL);
  461. buf = kmalloc(SW_LENGTH, GFP_KERNEL);
  462. idbuf = kmalloc(SW_LENGTH, GFP_KERNEL);
  463. if (!sw || !buf || !idbuf) {
  464. err = -ENOMEM;
  465. goto fail1;
  466. }
  467. sw->gameport = gameport;
  468. gameport_set_drvdata(gameport, sw);
  469. err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
  470. if (err)
  471. goto fail1;
  472. dbg("Init 0: Opened %s, io %#x, speed %d",
  473. gameport->phys, gameport->io, gameport->speed);
  474. i = sw_read_packet(gameport, buf, SW_LENGTH, 0); /* Read normal packet */
  475. msleep(SW_TIMEOUT);
  476. dbg("Init 1: Mode %d. Length %d.", m , i);
  477. if (!i) { /* No data. 3d Pro analog mode? */
  478. sw_init_digital(gameport); /* Switch to digital */
  479. msleep(SW_TIMEOUT);
  480. i = sw_read_packet(gameport, buf, SW_LENGTH, 0); /* Retry reading packet */
  481. msleep(SW_TIMEOUT);
  482. dbg("Init 1b: Length %d.", i);
  483. if (!i) { /* No data -> FAIL */
  484. err = -ENODEV;
  485. goto fail2;
  486. }
  487. }
  488. j = sw_read_packet(gameport, idbuf, SW_LENGTH, i); /* Read ID. This initializes the stick */
  489. m |= sw_guess_mode(idbuf, j); /* ID packet should carry mode info [3DP] */
  490. dbg("Init 2: Mode %d. ID Length %d.", m, j);
  491. if (j <= 0) { /* Read ID failed. Happens in 1-bit mode on PP */
  492. msleep(SW_TIMEOUT);
  493. i = sw_read_packet(gameport, buf, SW_LENGTH, 0); /* Retry reading packet */
  494. m |= sw_guess_mode(buf, i);
  495. dbg("Init 2b: Mode %d. Length %d.", m, i);
  496. if (!i) {
  497. err = -ENODEV;
  498. goto fail2;
  499. }
  500. msleep(SW_TIMEOUT);
  501. j = sw_read_packet(gameport, idbuf, SW_LENGTH, i); /* Retry reading ID */
  502. dbg("Init 2c: ID Length %d.", j);
  503. }
  504. sw->type = -1;
  505. k = SW_FAIL; /* Try SW_FAIL times */
  506. l = 0;
  507. do {
  508. k--;
  509. msleep(SW_TIMEOUT);
  510. i = sw_read_packet(gameport, buf, SW_LENGTH, 0); /* Read data packet */
  511. dbg("Init 3: Mode %d. Length %d. Last %d. Tries %d.", m, i, l, k);
  512. if (i > l) { /* Longer? As we can only lose bits, it makes */
  513. /* no sense to try detection for a packet shorter */
  514. l = i; /* than the previous one */
  515. sw->number = 1;
  516. sw->gameport = gameport;
  517. sw->length = i;
  518. sw->bits = m;
  519. dbg("Init 3a: Case %d.\n", i * m);
  520. switch (i * m) {
  521. case 60:
  522. sw->number++;
  523. case 45: /* Ambiguous packet length */
  524. if (j <= 40) { /* ID length less or eq 40 -> FSP */
  525. case 43:
  526. sw->type = SW_ID_FSP;
  527. break;
  528. }
  529. sw->number++;
  530. case 30:
  531. sw->number++;
  532. case 15:
  533. sw->type = SW_ID_GP;
  534. break;
  535. case 33:
  536. case 31:
  537. sw->type = SW_ID_FFW;
  538. break;
  539. case 48: /* Ambiguous */
  540. if (j == 14) { /* ID length 14*3 -> FFP */
  541. sw->type = SW_ID_FFP;
  542. sprintf(comment, " [AC %s]", sw_get_bits(idbuf,38,1,3) ? "off" : "on");
  543. } else
  544. sw->type = SW_ID_PP;
  545. break;
  546. case 66:
  547. sw->bits = 3;
  548. case 198:
  549. sw->length = 22;
  550. case 64:
  551. sw->type = SW_ID_3DP;
  552. if (j == 160)
  553. sw_3dp_id(idbuf, comment, sizeof(comment));
  554. break;
  555. }
  556. }
  557. } while (k && sw->type == -1);
  558. if (sw->type == -1) {
  559. printk(KERN_WARNING "sidewinder.c: unknown joystick device detected "
  560. "on %s, contact <vojtech@ucw.cz>\n", gameport->phys);
  561. sw_print_packet("ID", j * 3, idbuf, 3);
  562. sw_print_packet("Data", i * m, buf, m);
  563. err = -ENODEV;
  564. goto fail2;
  565. }
  566. #ifdef SW_DEBUG
  567. sw_print_packet("ID", j * 3, idbuf, 3);
  568. sw_print_packet("Data", i * m, buf, m);
  569. #endif
  570. gameport_set_poll_handler(gameport, sw_poll);
  571. gameport_set_poll_interval(gameport, 20);
  572. k = i;
  573. l = j;
  574. for (i = 0; i < sw->number; i++) {
  575. int bits, code;
  576. snprintf(sw->name, sizeof(sw->name),
  577. "Microsoft SideWinder %s", sw_name[sw->type]);
  578. snprintf(sw->phys[i], sizeof(sw->phys[i]),
  579. "%s/input%d", gameport->phys, i);
  580. sw->dev[i] = input_dev = input_allocate_device();
  581. if (!input_dev) {
  582. err = -ENOMEM;
  583. goto fail3;
  584. }
  585. input_dev->name = sw->name;
  586. input_dev->phys = sw->phys[i];
  587. input_dev->id.bustype = BUS_GAMEPORT;
  588. input_dev->id.vendor = GAMEPORT_ID_VENDOR_MICROSOFT;
  589. input_dev->id.product = sw->type;
  590. input_dev->id.version = 0x0100;
  591. input_dev->dev.parent = &gameport->dev;
  592. input_set_drvdata(input_dev, sw);
  593. input_dev->open = sw_open;
  594. input_dev->close = sw_close;
  595. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  596. for (j = 0; (bits = sw_bit[sw->type][j]); j++) {
  597. int min, max, fuzz, flat;
  598. code = sw_abs[sw->type][j];
  599. min = bits == 1 ? -1 : 0;
  600. max = (1 << bits) - 1;
  601. fuzz = (bits >> 1) >= 2 ? 1 << ((bits >> 1) - 2) : 0;
  602. flat = code == ABS_THROTTLE || bits < 5 ?
  603. 0 : 1 << (bits - 5);
  604. input_set_abs_params(input_dev, code,
  605. min, max, fuzz, flat);
  606. }
  607. for (j = 0; (code = sw_btn[sw->type][j]); j++)
  608. __set_bit(code, input_dev->keybit);
  609. dbg("%s%s [%d-bit id %d data %d]\n", sw->name, comment, m, l, k);
  610. err = input_register_device(sw->dev[i]);
  611. if (err)
  612. goto fail4;
  613. }
  614. out: kfree(buf);
  615. kfree(idbuf);
  616. return err;
  617. fail4: input_free_device(sw->dev[i]);
  618. fail3: while (--i >= 0)
  619. input_unregister_device(sw->dev[i]);
  620. fail2: gameport_close(gameport);
  621. fail1: gameport_set_drvdata(gameport, NULL);
  622. kfree(sw);
  623. goto out;
  624. }
  625. static void sw_disconnect(struct gameport *gameport)
  626. {
  627. struct sw *sw = gameport_get_drvdata(gameport);
  628. int i;
  629. for (i = 0; i < sw->number; i++)
  630. input_unregister_device(sw->dev[i]);
  631. gameport_close(gameport);
  632. gameport_set_drvdata(gameport, NULL);
  633. kfree(sw);
  634. }
  635. static struct gameport_driver sw_drv = {
  636. .driver = {
  637. .name = "sidewinder",
  638. .owner = THIS_MODULE,
  639. },
  640. .description = DRIVER_DESC,
  641. .connect = sw_connect,
  642. .disconnect = sw_disconnect,
  643. };
  644. module_gameport_driver(sw_drv);