1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 1998-2005 Vojtech Pavlik
7 * Microsoft SideWinder joystick family driver for Linux
10 #include <linux/delay.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/input.h>
15 #include <linux/gameport.h>
16 #include <linux/jiffies.h>
18 #define DRIVER_DESC "Microsoft SideWinder joystick family driver"
21 MODULE_DESCRIPTION(DRIVER_DESC);
22 MODULE_LICENSE("GPL");
25 * These are really magic values. Changing them can make a problem go away,
26 * as well as break everything.
32 #define SW_START 600 /* The time we wait for the first bit [600 us] */
33 #define SW_STROBE 60 /* Max time per bit [60 us] */
34 #define SW_TIMEOUT 6 /* Wait for everything to settle [6 ms] */
35 #define SW_KICK 45 /* Wait after A0 fall till kick [45 us] */
36 #define SW_END 8 /* Number of bits before end of packet to kick */
37 #define SW_FAIL 16 /* Number of packet read errors to fail and reinitialize */
38 #define SW_BAD 2 /* Number of packet read errors to switch off 3d Pro optimization */
39 #define SW_OK 64 /* Number of packet read successes to switch optimization back on */
40 #define SW_LENGTH 512 /* Max number of bits in a packet */
43 #define dbg(format, arg...) printk(KERN_DEBUG __FILE__ ": " format "\n" , ## arg)
45 #define dbg(format, arg...) do {} while (0)
49 * SideWinder joystick types ...
60 * Names, buttons, axes ...
63 static char *sw_name[] = { "3D Pro", "GamePad", "Precision Pro", "Force Feedback Pro", "FreeStyle Pro",
64 "Force Feedback Wheel" };
66 static char sw_abs[][7] = {
67 { ABS_X, ABS_Y, ABS_RZ, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y },
69 { ABS_X, ABS_Y, ABS_RZ, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y },
70 { ABS_X, ABS_Y, ABS_RZ, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y },
71 { ABS_X, ABS_Y, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y },
72 { ABS_RX, ABS_RUDDER, ABS_THROTTLE }};
74 static char sw_bit[][7] = {
75 { 10, 10, 9, 10, 1, 1 },
77 { 10, 10, 6, 7, 1, 1 },
78 { 10, 10, 6, 7, 1, 1 },
82 static short sw_btn[][12] = {
83 { BTN_TRIGGER, BTN_TOP, BTN_THUMB, BTN_THUMB2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_MODE },
84 { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_START, BTN_MODE },
85 { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_SELECT },
86 { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_SELECT },
87 { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_START, BTN_MODE, BTN_SELECT },
88 { BTN_TRIGGER, BTN_TOP, BTN_THUMB, BTN_THUMB2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4 }};
93 } sw_hat_to_axis[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
96 struct gameport *gameport;
97 struct input_dev *dev[4];
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.
116 static int sw_read_packet(struct gameport *gameport, unsigned char *buf, int length, int id)
119 int timeout, bitout, sched, i, kick, start, strobe;
120 unsigned char pending, u, v;
122 i = -id; /* Don't care about data, only want ID */
123 timeout = id ? gameport_time(gameport, SW_TIMEOUT * 1000) : 0; /* Set up global timeout for ID packet */
124 kick = id ? gameport_time(gameport, SW_KICK) : 0; /* Set up kick timeout for ID packet */
125 start = gameport_time(gameport, SW_START);
126 strobe = gameport_time(gameport, SW_STROBE);
131 local_irq_save(flags); /* Quiet, please */
133 gameport_trigger(gameport); /* Trigger */
134 v = gameport_read(gameport);
139 v = gameport_read(gameport);
140 } while (!(~v & u & 0x10) && (bitout > 0)); /* Wait for first falling edge on clock */
143 bitout = strobe; /* Extend time if not timed out */
145 while ((timeout > 0 || bitout > 0) && (i < length)) {
148 bitout--; /* Decrement timers */
152 v = gameport_read(gameport);
154 if ((~u & v & 0x10) && (bitout > 0)) { /* Rising edge on clock - data bit */
155 if (i >= 0) /* Want this data */
156 buf[i] = v >> 5; /* Store it */
157 i++; /* Advance index */
158 bitout = strobe; /* Extend timeout for next bit */
161 if (kick && (~v & u & 0x01)) { /* Falling edge on axis 0 */
162 sched = kick; /* Schedule second trigger */
163 kick = 0; /* Don't schedule next time on falling edge */
164 pending = 1; /* Mark schedule */
167 if (pending && sched < 0 && (i > -SW_END)) { /* Second trigger time */
168 gameport_trigger(gameport); /* Trigger */
169 bitout = start; /* Long bit timeout */
170 pending = 0; /* Unmark schedule */
171 timeout = 0; /* Switch from global to bit timeouts */
175 local_irq_restore(flags); /* Done - relax */
180 printk(KERN_DEBUG "sidewinder.c: Read %d triplets. [", i);
181 for (j = 0; j < i; j++) printk("%d", buf[j]);
190 * sw_get_bits() and GB() compose bits from the triplet buffer into a __u64.
191 * Parameter 'pos' is bit number inside packet where to start at, 'num' is number
192 * of bits to be read, 'shift' is offset in the resulting __u64 to start at, bits
193 * is number of bits per triplet.
196 #define GB(pos,num) sw_get_bits(buf, pos, num, sw->bits)
198 static __u64 sw_get_bits(unsigned char *buf, int pos, int num, char bits)
201 int tri = pos % bits; /* Start position */
206 data |= (__u64)((buf[i] >> tri++) & 1) << bit++; /* Transfer bit */
208 i++; /* Next triplet */
217 * sw_init_digital() initializes a SideWinder 3D Pro joystick
221 static void sw_init_digital(struct gameport *gameport)
223 static const int seq[] = { 140, 140+725, 140+300, 0 };
227 local_irq_save(flags);
231 gameport_trigger(gameport); /* Trigger */
232 t = gameport_time(gameport, SW_TIMEOUT * 1000);
233 while ((gameport_read(gameport) & 1) && t) t--; /* Wait for axis to fall back to 0 */
234 udelay(seq[i]); /* Delay magic time */
237 gameport_trigger(gameport); /* Last trigger */
239 local_irq_restore(flags);
243 * sw_parity() computes parity of __u64
246 static int sw_parity(__u64 t)
248 int x = t ^ (t >> 32);
259 * sw_ccheck() checks synchronization bits and computes checksum of nibbles.
262 static int sw_check(__u64 t)
264 unsigned char sum = 0;
266 if ((t & 0x8080808080808080ULL) ^ 0x80) /* Sync */
269 while (t) { /* Sum */
278 * sw_parse() analyzes SideWinder joystick data, and writes the results into
279 * the axes and buttons arrays.
282 static int sw_parse(unsigned char *buf, struct sw *sw)
285 struct input_dev *dev;
291 if (sw_check(GB(0,64)) || (hat = (GB(6,1) << 3) | GB(60,3)) > 8)
296 input_report_abs(dev, ABS_X, (GB( 3,3) << 7) | GB(16,7));
297 input_report_abs(dev, ABS_Y, (GB( 0,3) << 7) | GB(24,7));
298 input_report_abs(dev, ABS_RZ, (GB(35,2) << 7) | GB(40,7));
299 input_report_abs(dev, ABS_THROTTLE, (GB(32,3) << 7) | GB(48,7));
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);
304 for (j = 0; j < 7; j++)
305 input_report_key(dev, sw_btn[SW_ID_3DP][j], !GB(j+8,1));
307 input_report_key(dev, BTN_BASE4, !GB(38,1));
308 input_report_key(dev, BTN_BASE5, !GB(37,1));
316 for (i = 0; i < sw->number; i ++) {
318 if (sw_parity(GB(i*15,15)))
321 input_report_abs(sw->dev[i], ABS_X, GB(i*15+3,1) - GB(i*15+2,1));
322 input_report_abs(sw->dev[i], ABS_Y, GB(i*15+0,1) - GB(i*15+1,1));
324 for (j = 0; j < 10; j++)
325 input_report_key(sw->dev[i], sw_btn[SW_ID_GP][j], !GB(i*15+j+4,1));
327 input_sync(sw->dev[i]);
335 if (!sw_parity(GB(0,48)) || (hat = GB(42,4)) > 8)
339 input_report_abs(dev, ABS_X, GB( 9,10));
340 input_report_abs(dev, ABS_Y, GB(19,10));
341 input_report_abs(dev, ABS_RZ, GB(36, 6));
342 input_report_abs(dev, ABS_THROTTLE, GB(29, 7));
344 input_report_abs(dev, ABS_HAT0X, sw_hat_to_axis[hat].x);
345 input_report_abs(dev, ABS_HAT0Y, sw_hat_to_axis[hat].y);
347 for (j = 0; j < 9; j++)
348 input_report_key(dev, sw_btn[SW_ID_PP][j], !GB(j,1));
356 if (!sw_parity(GB(0,43)) || (hat = GB(28,4)) > 8)
360 input_report_abs(dev, ABS_X, GB( 0,10));
361 input_report_abs(dev, ABS_Y, GB(16,10));
362 input_report_abs(dev, ABS_THROTTLE, GB(32, 6));
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 < 6; j++)
368 input_report_key(dev, sw_btn[SW_ID_FSP][j], !GB(j+10,1));
370 input_report_key(dev, BTN_TR, !GB(26,1));
371 input_report_key(dev, BTN_START, !GB(27,1));
372 input_report_key(dev, BTN_MODE, !GB(38,1));
373 input_report_key(dev, BTN_SELECT, !GB(39,1));
381 if (!sw_parity(GB(0,33)))
385 input_report_abs(dev, ABS_RX, GB( 0,10));
386 input_report_abs(dev, ABS_RUDDER, GB(10, 6));
387 input_report_abs(dev, ABS_THROTTLE, GB(16, 6));
389 for (j = 0; j < 8; j++)
390 input_report_key(dev, sw_btn[SW_ID_FFW][j], !GB(j+22,1));
401 * sw_read() reads SideWinder joystick data, and reinitializes
402 * the joystick in case of persistent problems. This is the function that is
403 * called from the generic code to poll the joystick.
406 static int sw_read(struct sw *sw)
408 unsigned char buf[SW_LENGTH];
411 i = sw_read_packet(sw->gameport, buf, sw->length, 0);
413 if (sw->type == SW_ID_3DP && sw->length == 66 && i != 66) { /* Broken packet, try to fix */
415 if (i == 64 && !sw_check(sw_get_bits(buf,0,64,1))) { /* Last init failed, 1 bit mode */
416 printk(KERN_WARNING "sidewinder.c: Joystick in wrong mode on %s"
417 " - going to reinitialize.\n", sw->gameport->phys);
418 sw->fail = SW_FAIL; /* Reinitialize */
419 i = 128; /* Bogus value */
422 if (i < 66 && GB(0,64) == GB(i*3-66,64)) /* 1 == 3 */
423 i = 66; /* Everything is fine */
425 if (i < 66 && GB(0,64) == GB(66,64)) /* 1 == 2 */
426 i = 66; /* Everything is fine */
428 if (i < 66 && GB(i*3-132,64) == GB(i*3-66,64)) { /* 2 == 3 */
429 memmove(buf, buf + i - 22, 22); /* Move data */
430 i = 66; /* Carry on */
434 if (i == sw->length && !sw_parse(buf, sw)) { /* Parse data */
439 if (sw->type == SW_ID_3DP && sw->length == 66 /* Many packets OK */
442 printk(KERN_INFO "sidewinder.c: No more trouble on %s"
443 " - enabling optimization again.\n", sw->gameport->phys);
453 if (sw->type == SW_ID_3DP && sw->length == 22 && sw->fail > SW_BAD) { /* Consecutive bad packets */
455 printk(KERN_INFO "sidewinder.c: Many bit errors on %s"
456 " - disabling optimization.\n", sw->gameport->phys);
460 if (sw->fail < SW_FAIL)
461 return -1; /* Not enough, don't reinitialize yet */
463 printk(KERN_WARNING "sidewinder.c: Too many bit errors on %s"
464 " - reinitializing joystick.\n", sw->gameport->phys);
466 if (!i && sw->type == SW_ID_3DP) { /* 3D Pro can be in analog mode */
467 mdelay(3 * SW_TIMEOUT);
468 sw_init_digital(sw->gameport);
472 i = sw_read_packet(sw->gameport, buf, SW_LENGTH, 0); /* Read normal data packet */
474 sw_read_packet(sw->gameport, buf, SW_LENGTH, i); /* Read ID packet, this initializes the stick */
481 static void sw_poll(struct gameport *gameport)
483 struct sw *sw = gameport_get_drvdata(gameport);
490 static int sw_open(struct input_dev *dev)
492 struct sw *sw = input_get_drvdata(dev);
494 gameport_start_polling(sw->gameport);
498 static void sw_close(struct input_dev *dev)
500 struct sw *sw = input_get_drvdata(dev);
502 gameport_stop_polling(sw->gameport);
506 * sw_print_packet() prints the contents of a SideWinder packet.
509 static void sw_print_packet(char *name, int length, unsigned char *buf, char bits)
513 printk(KERN_INFO "sidewinder.c: %s packet, %d bits. [", name, length);
514 for (i = (((length + 3) >> 2) - 1); i >= 0; i--)
515 printk("%x", (int)sw_get_bits(buf, i << 2, 4, bits));
520 * sw_3dp_id() translates the 3DP id into a human legible string.
521 * Unfortunately I don't know how to do this for the other SW types.
524 static void sw_3dp_id(unsigned char *buf, char *comment, size_t size)
529 for (i = 0; i < 7; i++) /* ASCII PnP ID */
530 pnp[i] = sw_get_bits(buf, 24+8*i, 8, 1);
532 for (i = 0; i < 8; i++) /* ASCII firmware revision */
533 rev[i] = sw_get_bits(buf, 88+8*i, 8, 1);
537 snprintf(comment, size, " [PnP %d.%02d id %s rev %s]",
538 (int) ((sw_get_bits(buf, 8, 6, 1) << 6) | /* Two 6-bit values */
539 sw_get_bits(buf, 16, 6, 1)) / 100,
540 (int) ((sw_get_bits(buf, 8, 6, 1) << 6) |
541 sw_get_bits(buf, 16, 6, 1)) % 100,
546 * sw_guess_mode() checks the upper two button bits for toggling -
547 * indication of that the joystick is in 3-bit mode. This is documented
548 * behavior for 3DP ID packet, and for example the FSP does this in
549 * normal packets instead. Fun ...
552 static int sw_guess_mode(unsigned char *buf, int len)
555 unsigned char xor = 0;
557 for (i = 1; i < len; i++)
558 xor |= (buf[i - 1] ^ buf[i]) & 6;
560 return !!xor * 2 + 1;
564 * sw_connect() probes for SideWinder type joysticks.
567 static int sw_connect(struct gameport *gameport, struct gameport_driver *drv)
570 struct input_dev *input_dev;
573 unsigned char *buf = NULL; /* [SW_LENGTH] */
574 unsigned char *idbuf = NULL; /* [SW_LENGTH] */
580 sw = kzalloc(sizeof(struct sw), GFP_KERNEL);
581 buf = kmalloc(SW_LENGTH, GFP_KERNEL);
582 idbuf = kmalloc(SW_LENGTH, GFP_KERNEL);
583 if (!sw || !buf || !idbuf) {
588 sw->gameport = gameport;
590 gameport_set_drvdata(gameport, sw);
592 err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
596 dbg("Init 0: Opened %s, io %#x, speed %d",
597 gameport->phys, gameport->io, gameport->speed);
599 i = sw_read_packet(gameport, buf, SW_LENGTH, 0); /* Read normal packet */
601 dbg("Init 1: Mode %d. Length %d.", m , i);
603 if (!i) { /* No data. 3d Pro analog mode? */
604 sw_init_digital(gameport); /* Switch to digital */
606 i = sw_read_packet(gameport, buf, SW_LENGTH, 0); /* Retry reading packet */
608 dbg("Init 1b: Length %d.", i);
609 if (!i) { /* No data -> FAIL */
615 j = sw_read_packet(gameport, idbuf, SW_LENGTH, i); /* Read ID. This initializes the stick */
616 m |= sw_guess_mode(idbuf, j); /* ID packet should carry mode info [3DP] */
617 dbg("Init 2: Mode %d. ID Length %d.", m, j);
619 if (j <= 0) { /* Read ID failed. Happens in 1-bit mode on PP */
621 i = sw_read_packet(gameport, buf, SW_LENGTH, 0); /* Retry reading packet */
622 m |= sw_guess_mode(buf, i);
623 dbg("Init 2b: Mode %d. Length %d.", m, i);
629 j = sw_read_packet(gameport, idbuf, SW_LENGTH, i); /* Retry reading ID */
630 dbg("Init 2c: ID Length %d.", j);
634 k = SW_FAIL; /* Try SW_FAIL times */
640 i = sw_read_packet(gameport, buf, SW_LENGTH, 0); /* Read data packet */
641 dbg("Init 3: Mode %d. Length %d. Last %d. Tries %d.", m, i, l, k);
643 if (i > l) { /* Longer? As we can only lose bits, it makes */
644 /* no sense to try detection for a packet shorter */
645 l = i; /* than the previous one */
648 sw->gameport = gameport;
652 dbg("Init 3a: Case %d.\n", i * m);
658 case 45: /* Ambiguous packet length */
659 if (j <= 40) { /* ID length less or eq 40 -> FSP */
662 sw->type = SW_ID_FSP;
675 sw->type = SW_ID_FFW;
677 case 48: /* Ambiguous */
678 if (j == 14) { /* ID length 14*3 -> FFP */
679 sw->type = SW_ID_FFP;
680 sprintf(comment, " [AC %s]", sw_get_bits(idbuf,38,1,3) ? "off" : "on");
691 sw->type = SW_ID_3DP;
693 sw_3dp_id(idbuf, comment, sizeof(comment));
698 } while (k && sw->type == -1);
700 if (sw->type == -1) {
701 printk(KERN_WARNING "sidewinder.c: unknown joystick device detected "
703 sw_print_packet("ID", j * 3, idbuf, 3);
704 sw_print_packet("Data", i * m, buf, m);
710 sw_print_packet("ID", j * 3, idbuf, 3);
711 sw_print_packet("Data", i * m, buf, m);
714 gameport_set_poll_handler(gameport, sw_poll);
715 gameport_set_poll_interval(gameport, 20);
720 for (i = 0; i < sw->number; i++) {
723 snprintf(sw->name, sizeof(sw->name),
724 "Microsoft SideWinder %s", sw_name[sw->type]);
725 snprintf(sw->phys[i], sizeof(sw->phys[i]),
726 "%s/input%d", gameport->phys, i);
728 sw->dev[i] = input_dev = input_allocate_device();
734 input_dev->name = sw->name;
735 input_dev->phys = sw->phys[i];
736 input_dev->id.bustype = BUS_GAMEPORT;
737 input_dev->id.vendor = GAMEPORT_ID_VENDOR_MICROSOFT;
738 input_dev->id.product = sw->type;
739 input_dev->id.version = 0x0100;
740 input_dev->dev.parent = &gameport->dev;
742 input_set_drvdata(input_dev, sw);
744 input_dev->open = sw_open;
745 input_dev->close = sw_close;
747 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
749 for (j = 0; (bits = sw_bit[sw->type][j]); j++) {
750 int min, max, fuzz, flat;
752 code = sw_abs[sw->type][j];
753 min = bits == 1 ? -1 : 0;
754 max = (1 << bits) - 1;
755 fuzz = (bits >> 1) >= 2 ? 1 << ((bits >> 1) - 2) : 0;
756 flat = code == ABS_THROTTLE || bits < 5 ?
759 input_set_abs_params(input_dev, code,
760 min, max, fuzz, flat);
763 for (j = 0; (code = sw_btn[sw->type][j]); j++)
764 __set_bit(code, input_dev->keybit);
766 dbg("%s%s [%d-bit id %d data %d]\n", sw->name, comment, m, l, k);
768 err = input_register_device(sw->dev[i]);
778 fail4: input_free_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);
787 static void sw_disconnect(struct gameport *gameport)
789 struct sw *sw = gameport_get_drvdata(gameport);
792 for (i = 0; i < sw->number; i++)
793 input_unregister_device(sw->dev[i]);
794 gameport_close(gameport);
795 gameport_set_drvdata(gameport, NULL);
799 static struct gameport_driver sw_drv = {
801 .name = "sidewinder",
802 .owner = THIS_MODULE,
804 .description = DRIVER_DESC,
805 .connect = sw_connect,
806 .disconnect = sw_disconnect,
809 module_gameport_driver(sw_drv);