2 * Copyright (c) 1998-2005 Vojtech Pavlik
6 * Microsoft SideWinder joystick family driver for Linux
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * Should you need to contact me, the author, you can do so either by
25 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
26 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
29 #include <linux/delay.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <linux/init.h>
34 #include <linux/input.h>
35 #include <linux/gameport.h>
36 #include <linux/jiffies.h>
38 #define DRIVER_DESC "Microsoft SideWinder joystick family driver"
40 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
41 MODULE_DESCRIPTION(DRIVER_DESC);
42 MODULE_LICENSE("GPL");
45 * These are really magic values. Changing them can make a problem go away,
46 * as well as break everything.
52 #define SW_START 600 /* The time we wait for the first bit [600 us] */
53 #define SW_STROBE 60 /* Max time per bit [60 us] */
54 #define SW_TIMEOUT 6 /* Wait for everything to settle [6 ms] */
55 #define SW_KICK 45 /* Wait after A0 fall till kick [45 us] */
56 #define SW_END 8 /* Number of bits before end of packet to kick */
57 #define SW_FAIL 16 /* Number of packet read errors to fail and reinitialize */
58 #define SW_BAD 2 /* Number of packet read errors to switch off 3d Pro optimization */
59 #define SW_OK 64 /* Number of packet read successes to switch optimization back on */
60 #define SW_LENGTH 512 /* Max number of bits in a packet */
63 #define dbg(format, arg...) printk(KERN_DEBUG __FILE__ ": " format "\n" , ## arg)
65 #define dbg(format, arg...) do {} while (0)
69 * SideWinder joystick types ...
80 * Names, buttons, axes ...
83 static char *sw_name[] = { "3D Pro", "GamePad", "Precision Pro", "Force Feedback Pro", "FreeStyle Pro",
84 "Force Feedback Wheel" };
86 static char sw_abs[][7] = {
87 { ABS_X, ABS_Y, ABS_RZ, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y },
89 { ABS_X, ABS_Y, ABS_RZ, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y },
90 { ABS_X, ABS_Y, ABS_RZ, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y },
91 { ABS_X, ABS_Y, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y },
92 { ABS_RX, ABS_RUDDER, ABS_THROTTLE }};
94 static char sw_bit[][7] = {
95 { 10, 10, 9, 10, 1, 1 },
97 { 10, 10, 6, 7, 1, 1 },
98 { 10, 10, 6, 7, 1, 1 },
102 static short sw_btn[][12] = {
103 { BTN_TRIGGER, BTN_TOP, BTN_THUMB, BTN_THUMB2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_MODE },
104 { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_START, BTN_MODE },
105 { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_SELECT },
106 { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_SELECT },
107 { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_START, BTN_MODE, BTN_SELECT },
108 { BTN_TRIGGER, BTN_TOP, BTN_THUMB, BTN_THUMB2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4 }};
113 } sw_hat_to_axis[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
116 struct gameport *gameport;
117 struct input_dev *dev[4];
131 * sw_read_packet() is a function which reads either a data packet, or an
132 * identification packet from a SideWinder joystick. The protocol is very,
133 * very, very braindamaged. Microsoft patented it in US patent #5628686.
136 static int sw_read_packet(struct gameport *gameport, unsigned char *buf, int length, int id)
139 int timeout, bitout, sched, i, kick, start, strobe;
140 unsigned char pending, u, v;
142 i = -id; /* Don't care about data, only want ID */
143 timeout = id ? gameport_time(gameport, SW_TIMEOUT * 1000) : 0; /* Set up global timeout for ID packet */
144 kick = id ? gameport_time(gameport, SW_KICK) : 0; /* Set up kick timeout for ID packet */
145 start = gameport_time(gameport, SW_START);
146 strobe = gameport_time(gameport, SW_STROBE);
151 local_irq_save(flags); /* Quiet, please */
153 gameport_trigger(gameport); /* Trigger */
154 v = gameport_read(gameport);
159 v = gameport_read(gameport);
160 } while (!(~v & u & 0x10) && (bitout > 0)); /* Wait for first falling edge on clock */
163 bitout = strobe; /* Extend time if not timed out */
165 while ((timeout > 0 || bitout > 0) && (i < length)) {
168 bitout--; /* Decrement timers */
172 v = gameport_read(gameport);
174 if ((~u & v & 0x10) && (bitout > 0)) { /* Rising edge on clock - data bit */
175 if (i >= 0) /* Want this data */
176 buf[i] = v >> 5; /* Store it */
177 i++; /* Advance index */
178 bitout = strobe; /* Extend timeout for next bit */
181 if (kick && (~v & u & 0x01)) { /* Falling edge on axis 0 */
182 sched = kick; /* Schedule second trigger */
183 kick = 0; /* Don't schedule next time on falling edge */
184 pending = 1; /* Mark schedule */
187 if (pending && sched < 0 && (i > -SW_END)) { /* Second trigger time */
188 gameport_trigger(gameport); /* Trigger */
189 bitout = start; /* Long bit timeout */
190 pending = 0; /* Unmark schedule */
191 timeout = 0; /* Switch from global to bit timeouts */
195 local_irq_restore(flags); /* Done - relax */
200 printk(KERN_DEBUG "sidewinder.c: Read %d triplets. [", i);
201 for (j = 0; j < i; j++) printk("%d", buf[j]);
210 * sw_get_bits() and GB() compose bits from the triplet buffer into a __u64.
211 * Parameter 'pos' is bit number inside packet where to start at, 'num' is number
212 * of bits to be read, 'shift' is offset in the resulting __u64 to start at, bits
213 * is number of bits per triplet.
216 #define GB(pos,num) sw_get_bits(buf, pos, num, sw->bits)
218 static __u64 sw_get_bits(unsigned char *buf, int pos, int num, char bits)
221 int tri = pos % bits; /* Start position */
226 data |= (__u64)((buf[i] >> tri++) & 1) << bit++; /* Transfer bit */
228 i++; /* Next triplet */
237 * sw_init_digital() initializes a SideWinder 3D Pro joystick
241 static void sw_init_digital(struct gameport *gameport)
243 int seq[] = { 140, 140+725, 140+300, 0 };
247 local_irq_save(flags);
251 gameport_trigger(gameport); /* Trigger */
252 t = gameport_time(gameport, SW_TIMEOUT * 1000);
253 while ((gameport_read(gameport) & 1) && t) t--; /* Wait for axis to fall back to 0 */
254 udelay(seq[i]); /* Delay magic time */
257 gameport_trigger(gameport); /* Last trigger */
259 local_irq_restore(flags);
263 * sw_parity() computes parity of __u64
266 static int sw_parity(__u64 t)
268 int x = t ^ (t >> 32);
279 * sw_ccheck() checks synchronization bits and computes checksum of nibbles.
282 static int sw_check(__u64 t)
284 unsigned char sum = 0;
286 if ((t & 0x8080808080808080ULL) ^ 0x80) /* Sync */
289 while (t) { /* Sum */
298 * sw_parse() analyzes SideWinder joystick data, and writes the results into
299 * the axes and buttons arrays.
302 static int sw_parse(unsigned char *buf, struct sw *sw)
305 struct input_dev *dev;
311 if (sw_check(GB(0,64)) || (hat = (GB(6,1) << 3) | GB(60,3)) > 8)
316 input_report_abs(dev, ABS_X, (GB( 3,3) << 7) | GB(16,7));
317 input_report_abs(dev, ABS_Y, (GB( 0,3) << 7) | GB(24,7));
318 input_report_abs(dev, ABS_RZ, (GB(35,2) << 7) | GB(40,7));
319 input_report_abs(dev, ABS_THROTTLE, (GB(32,3) << 7) | GB(48,7));
321 input_report_abs(dev, ABS_HAT0X, sw_hat_to_axis[hat].x);
322 input_report_abs(dev, ABS_HAT0Y, sw_hat_to_axis[hat].y);
324 for (j = 0; j < 7; j++)
325 input_report_key(dev, sw_btn[SW_ID_3DP][j], !GB(j+8,1));
327 input_report_key(dev, BTN_BASE4, !GB(38,1));
328 input_report_key(dev, BTN_BASE5, !GB(37,1));
336 for (i = 0; i < sw->number; i ++) {
338 if (sw_parity(GB(i*15,15)))
341 input_report_abs(sw->dev[i], ABS_X, GB(i*15+3,1) - GB(i*15+2,1));
342 input_report_abs(sw->dev[i], ABS_Y, GB(i*15+0,1) - GB(i*15+1,1));
344 for (j = 0; j < 10; j++)
345 input_report_key(sw->dev[i], sw_btn[SW_ID_GP][j], !GB(i*15+j+4,1));
347 input_sync(sw->dev[i]);
355 if (!sw_parity(GB(0,48)) || (hat = GB(42,4)) > 8)
359 input_report_abs(dev, ABS_X, GB( 9,10));
360 input_report_abs(dev, ABS_Y, GB(19,10));
361 input_report_abs(dev, ABS_RZ, GB(36, 6));
362 input_report_abs(dev, ABS_THROTTLE, GB(29, 7));
364 input_report_abs(dev, ABS_HAT0X, sw_hat_to_axis[hat].x);
365 input_report_abs(dev, ABS_HAT0Y, sw_hat_to_axis[hat].y);
367 for (j = 0; j < 9; j++)
368 input_report_key(dev, sw_btn[SW_ID_PP][j], !GB(j,1));
376 if (!sw_parity(GB(0,43)) || (hat = GB(28,4)) > 8)
380 input_report_abs(dev, ABS_X, GB( 0,10));
381 input_report_abs(dev, ABS_Y, GB(16,10));
382 input_report_abs(dev, ABS_THROTTLE, GB(32, 6));
384 input_report_abs(dev, ABS_HAT0X, sw_hat_to_axis[hat].x);
385 input_report_abs(dev, ABS_HAT0Y, sw_hat_to_axis[hat].y);
387 for (j = 0; j < 6; j++)
388 input_report_key(dev, sw_btn[SW_ID_FSP][j], !GB(j+10,1));
390 input_report_key(dev, BTN_TR, !GB(26,1));
391 input_report_key(dev, BTN_START, !GB(27,1));
392 input_report_key(dev, BTN_MODE, !GB(38,1));
393 input_report_key(dev, BTN_SELECT, !GB(39,1));
401 if (!sw_parity(GB(0,33)))
405 input_report_abs(dev, ABS_RX, GB( 0,10));
406 input_report_abs(dev, ABS_RUDDER, GB(10, 6));
407 input_report_abs(dev, ABS_THROTTLE, GB(16, 6));
409 for (j = 0; j < 8; j++)
410 input_report_key(dev, sw_btn[SW_ID_FFW][j], !GB(j+22,1));
421 * sw_read() reads SideWinder joystick data, and reinitializes
422 * the joystick in case of persistent problems. This is the function that is
423 * called from the generic code to poll the joystick.
426 static int sw_read(struct sw *sw)
428 unsigned char buf[SW_LENGTH];
431 i = sw_read_packet(sw->gameport, buf, sw->length, 0);
433 if (sw->type == SW_ID_3DP && sw->length == 66 && i != 66) { /* Broken packet, try to fix */
435 if (i == 64 && !sw_check(sw_get_bits(buf,0,64,1))) { /* Last init failed, 1 bit mode */
436 printk(KERN_WARNING "sidewinder.c: Joystick in wrong mode on %s"
437 " - going to reinitialize.\n", sw->gameport->phys);
438 sw->fail = SW_FAIL; /* Reinitialize */
439 i = 128; /* Bogus value */
442 if (i < 66 && GB(0,64) == GB(i*3-66,64)) /* 1 == 3 */
443 i = 66; /* Everything is fine */
445 if (i < 66 && GB(0,64) == GB(66,64)) /* 1 == 2 */
446 i = 66; /* Everything is fine */
448 if (i < 66 && GB(i*3-132,64) == GB(i*3-66,64)) { /* 2 == 3 */
449 memmove(buf, buf + i - 22, 22); /* Move data */
450 i = 66; /* Carry on */
454 if (i == sw->length && !sw_parse(buf, sw)) { /* Parse data */
459 if (sw->type == SW_ID_3DP && sw->length == 66 /* Many packets OK */
462 printk(KERN_INFO "sidewinder.c: No more trouble on %s"
463 " - enabling optimization again.\n", sw->gameport->phys);
473 if (sw->type == SW_ID_3DP && sw->length == 22 && sw->fail > SW_BAD) { /* Consecutive bad packets */
475 printk(KERN_INFO "sidewinder.c: Many bit errors on %s"
476 " - disabling optimization.\n", sw->gameport->phys);
480 if (sw->fail < SW_FAIL)
481 return -1; /* Not enough, don't reinitialize yet */
483 printk(KERN_WARNING "sidewinder.c: Too many bit errors on %s"
484 " - reinitializing joystick.\n", sw->gameport->phys);
486 if (!i && sw->type == SW_ID_3DP) { /* 3D Pro can be in analog mode */
487 mdelay(3 * SW_TIMEOUT);
488 sw_init_digital(sw->gameport);
492 i = sw_read_packet(sw->gameport, buf, SW_LENGTH, 0); /* Read normal data packet */
494 sw_read_packet(sw->gameport, buf, SW_LENGTH, i); /* Read ID packet, this initializes the stick */
501 static void sw_poll(struct gameport *gameport)
503 struct sw *sw = gameport_get_drvdata(gameport);
510 static int sw_open(struct input_dev *dev)
512 struct sw *sw = dev->private;
514 gameport_start_polling(sw->gameport);
518 static void sw_close(struct input_dev *dev)
520 struct sw *sw = dev->private;
522 gameport_stop_polling(sw->gameport);
526 * sw_print_packet() prints the contents of a SideWinder packet.
529 static void sw_print_packet(char *name, int length, unsigned char *buf, char bits)
533 printk(KERN_INFO "sidewinder.c: %s packet, %d bits. [", name, length);
534 for (i = (((length + 3) >> 2) - 1); i >= 0; i--)
535 printk("%x", (int)sw_get_bits(buf, i << 2, 4, bits));
540 * sw_3dp_id() translates the 3DP id into a human legible string.
541 * Unfortunately I don't know how to do this for the other SW types.
544 static void sw_3dp_id(unsigned char *buf, char *comment)
549 for (i = 0; i < 7; i++) /* ASCII PnP ID */
550 pnp[i] = sw_get_bits(buf, 24+8*i, 8, 1);
552 for (i = 0; i < 8; i++) /* ASCII firmware revision */
553 rev[i] = sw_get_bits(buf, 88+8*i, 8, 1);
557 sprintf(comment, " [PnP %d.%02d id %s rev %s]",
558 (int) ((sw_get_bits(buf, 8, 6, 1) << 6) | /* Two 6-bit values */
559 sw_get_bits(buf, 16, 6, 1)) / 100,
560 (int) ((sw_get_bits(buf, 8, 6, 1) << 6) |
561 sw_get_bits(buf, 16, 6, 1)) % 100,
566 * sw_guess_mode() checks the upper two button bits for toggling -
567 * indication of that the joystick is in 3-bit mode. This is documented
568 * behavior for 3DP ID packet, and for example the FSP does this in
569 * normal packets instead. Fun ...
572 static int sw_guess_mode(unsigned char *buf, int len)
575 unsigned char xor = 0;
577 for (i = 1; i < len; i++)
578 xor |= (buf[i - 1] ^ buf[i]) & 6;
580 return !!xor * 2 + 1;
584 * sw_connect() probes for SideWinder type joysticks.
587 static int sw_connect(struct gameport *gameport, struct gameport_driver *drv)
590 struct input_dev *input_dev;
593 unsigned char *buf = NULL; /* [SW_LENGTH] */
594 unsigned char *idbuf = NULL; /* [SW_LENGTH] */
600 sw = kzalloc(sizeof(struct sw), GFP_KERNEL);
601 buf = kmalloc(SW_LENGTH, GFP_KERNEL);
602 idbuf = kmalloc(SW_LENGTH, GFP_KERNEL);
603 if (!sw || !buf || !idbuf) {
608 sw->gameport = gameport;
610 gameport_set_drvdata(gameport, sw);
612 err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
616 dbg("Init 0: Opened %s, io %#x, speed %d",
617 gameport->phys, gameport->io, gameport->speed);
619 i = sw_read_packet(gameport, buf, SW_LENGTH, 0); /* Read normal packet */
621 dbg("Init 1: Mode %d. Length %d.", m , i);
623 if (!i) { /* No data. 3d Pro analog mode? */
624 sw_init_digital(gameport); /* Switch to digital */
626 i = sw_read_packet(gameport, buf, SW_LENGTH, 0); /* Retry reading packet */
628 dbg("Init 1b: Length %d.", i);
629 if (!i) { /* No data -> FAIL */
635 j = sw_read_packet(gameport, idbuf, SW_LENGTH, i); /* Read ID. This initializes the stick */
636 m |= sw_guess_mode(idbuf, j); /* ID packet should carry mode info [3DP] */
637 dbg("Init 2: Mode %d. ID Length %d.", m, j);
639 if (j <= 0) { /* Read ID failed. Happens in 1-bit mode on PP */
641 i = sw_read_packet(gameport, buf, SW_LENGTH, 0); /* Retry reading packet */
642 m |= sw_guess_mode(buf, i);
643 dbg("Init 2b: Mode %d. Length %d.", m, i);
649 j = sw_read_packet(gameport, idbuf, SW_LENGTH, i); /* Retry reading ID */
650 dbg("Init 2c: ID Length %d.", j);
654 k = SW_FAIL; /* Try SW_FAIL times */
660 i = sw_read_packet(gameport, buf, SW_LENGTH, 0); /* Read data packet */
661 dbg("Init 3: Mode %d. Length %d. Last %d. Tries %d.", m, i, l, k);
663 if (i > l) { /* Longer? As we can only lose bits, it makes */
664 /* no sense to try detection for a packet shorter */
665 l = i; /* than the previous one */
668 sw->gameport = gameport;
672 dbg("Init 3a: Case %d.\n", i * m);
677 case 45: /* Ambiguous packet length */
678 if (j <= 40) { /* ID length less or eq 40 -> FSP */
680 sw->type = SW_ID_FSP;
691 sw->type = SW_ID_FFW;
693 case 48: /* Ambiguous */
694 if (j == 14) { /* ID length 14*3 -> FFP */
695 sw->type = SW_ID_FFP;
696 sprintf(comment, " [AC %s]", sw_get_bits(idbuf,38,1,3) ? "off" : "on");
705 sw->type = SW_ID_3DP;
706 if (j == 160) sw_3dp_id(idbuf, comment);
711 } while (k && sw->type == -1);
713 if (sw->type == -1) {
714 printk(KERN_WARNING "sidewinder.c: unknown joystick device detected "
715 "on %s, contact <vojtech@ucw.cz>\n", gameport->phys);
716 sw_print_packet("ID", j * 3, idbuf, 3);
717 sw_print_packet("Data", i * m, buf, m);
723 sw_print_packet("ID", j * 3, idbuf, 3);
724 sw_print_packet("Data", i * m, buf, m);
727 gameport_set_poll_handler(gameport, sw_poll);
728 gameport_set_poll_interval(gameport, 20);
733 for (i = 0; i < sw->number; i++) {
736 sprintf(sw->name, "Microsoft SideWinder %s", sw_name[sw->type]);
737 sprintf(sw->phys[i], "%s/input%d", gameport->phys, i);
739 input_dev = input_allocate_device();
745 input_dev->name = sw->name;
746 input_dev->phys = sw->phys[i];
747 input_dev->id.bustype = BUS_GAMEPORT;
748 input_dev->id.vendor = GAMEPORT_ID_VENDOR_MICROSOFT;
749 input_dev->id.product = sw->type;
750 input_dev->id.version = 0x0100;
751 input_dev->cdev.dev = &gameport->dev;
752 input_dev->private = sw;
754 input_dev->open = sw_open;
755 input_dev->close = sw_close;
757 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
759 for (j = 0; (bits = sw_bit[sw->type][j]); j++) {
760 code = sw_abs[sw->type][j];
761 set_bit(code, input_dev->absbit);
762 input_dev->absmax[code] = (1 << bits) - 1;
763 input_dev->absmin[code] = (bits == 1) ? -1 : 0;
764 input_dev->absfuzz[code] = ((bits >> 1) >= 2) ? (1 << ((bits >> 1) - 2)) : 0;
765 if (code != ABS_THROTTLE)
766 input_dev->absflat[code] = (bits >= 5) ? (1 << (bits - 5)) : 0;
769 for (j = 0; (code = sw_btn[sw->type][j]); j++)
770 set_bit(code, input_dev->keybit);
772 dbg("%s%s [%d-bit id %d data %d]\n", sw->name, comment, m, l, k);
774 input_register_device(sw->dev[i]);
779 fail3: while (--i >= 0)
780 input_unregister_device(sw->dev[i]);
781 fail2: gameport_close(gameport);
782 fail1: gameport_set_drvdata(gameport, NULL);
789 static void sw_disconnect(struct gameport *gameport)
791 struct sw *sw = gameport_get_drvdata(gameport);
794 for (i = 0; i < sw->number; i++)
795 input_unregister_device(sw->dev[i]);
796 gameport_close(gameport);
797 gameport_set_drvdata(gameport, NULL);
801 static struct gameport_driver sw_drv = {
803 .name = "sidewinder",
805 .description = DRIVER_DESC,
806 .connect = sw_connect,
807 .disconnect = sw_disconnect,
810 static int __init sw_init(void)
812 gameport_register_driver(&sw_drv);
816 static void __exit sw_exit(void)
818 gameport_unregister_driver(&sw_drv);
821 module_init(sw_init);
822 module_exit(sw_exit);