sidewinder.c 21 KB

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