30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <linux/bitops.h>
35 #include <linux/input.h>
36 #include <linux/gameport.h>
38 #include <linux/timex.h>
40 #define DRIVER_DESC "Analog joystick and gamepad driver"
50 #define ANALOG_PORTS 16
53 static unsigned int js_nargs;
62 #define ANALOG_RUDDER 0x00004
63 #define ANALOG_THROTTLE 0x00008
64 #define ANALOG_AXES_STD 0x0000f
65 #define ANALOG_BTNS_STD 0x000f0
67 #define ANALOG_BTNS_CHF 0x00100
68 #define ANALOG_HAT1_CHF 0x00200
69 #define ANALOG_HAT2_CHF 0x00400
70 #define ANALOG_HAT_FCS 0x00800
71 #define ANALOG_HATS_ALL 0x00e00
72 #define ANALOG_BTN_TL 0x01000
73 #define ANALOG_BTN_TR 0x02000
74 #define ANALOG_BTN_TL2 0x04000
75 #define ANALOG_BTN_TR2 0x08000
76 #define ANALOG_BTNS_TLR 0x03000
77 #define ANALOG_BTNS_TLR2 0x0c000
78 #define ANALOG_BTNS_GAMEPAD 0x0f000
80 #define ANALOG_HBTN_CHF 0x10000
81 #define ANALOG_ANY_CHF 0x10700
82 #define ANALOG_SAITEK 0x20000
83 #define ANALOG_EXTENSIONS 0x7ff00
84 #define ANALOG_GAMEPAD 0x80000
86 #define ANALOG_MAX_TIME 3
87 #define ANALOG_LOOP_TIME 2000
88 #define ANALOG_SAITEK_DELAY 200
89 #define ANALOG_SAITEK_TIME 2000
90 #define ANALOG_AXIS_TIME 2
91 #define ANALOG_INIT_RETRIES 8
92 #define ANALOG_FUZZ_BITS 2
93 #define ANALOG_FUZZ_MAGIC 36
95 #define ANALOG_MAX_NAME_LENGTH 128
96 #define ANALOG_MAX_PHYS_LENGTH 32
106 static unsigned char analog_chf[] = { 0xf, 0x0, 0x1, 0x9, 0x2, 0x4, 0xc, 0x8, 0x3, 0x5, 0xb, 0x7, 0xd, 0xe, 0xa, 0x6 };
141 #define GET_TIME(x) do { if (cpu_has_tsc) rdtscl(x); else x = get_time_pit(); } while (0)
142 #define DELTA(x,y) (cpu_has_tsc ? ((y) - (x)) : ((x) - (y) + ((x) < (y) ? PIT_TICK_RATE / HZ : 0)))
143 #define TIME_NAME (cpu_has_tsc?"TSC":"PIT")
144 static unsigned int get_time_pit(
void)
152 count |=
inb_p(0x40) << 8;
157 #elif defined(__x86_64__)
158 #define GET_TIME(x) rdtscl(x)
159 #define DELTA(x,y) ((y)-(x))
160 #define TIME_NAME "TSC"
161 #elif defined(__alpha__)
162 #define GET_TIME(x) do { x = get_cycles(); } while (0)
163 #define DELTA(x,y) ((y)-(x))
164 #define TIME_NAME "PCC"
165 #elif defined(CONFIG_MN10300)
166 #define GET_TIME(x) do { x = get_cycles(); } while (0)
167 #define DELTA(x, y) ((x) - (y))
168 #define TIME_NAME "TSC"
171 static unsigned long analog_faketime = 0;
172 #define GET_TIME(x) do { x = analog_faketime++; } while(0)
173 #define DELTA(x,y) ((y)-(x))
174 #define TIME_NAME "Unreliable"
175 #warning Precise timer not defined for this architecture.
184 struct input_dev *
dev = analog->
dev;
188 for (i = 0; i < 4; i++)
189 if (axes[3] < ((initial[3] * ((i << 1) + 1)) >> 3)) {
190 buttons |= 1 << (i + 14);
194 for (i = j = 0; i < 6; i++)
195 if (analog->
mask & (0x10 << i))
196 input_report_key(dev, analog->
buttons[j++], (buttons >> i) & 1);
199 for (i = 0; i < 4; i++)
200 input_report_key(dev, analog->
buttons[j++], (buttons >> (i + 10)) & 1);
203 input_report_key(dev, analog_pads[0], axes[2] < (initial[2] >> 1));
205 input_report_key(dev, analog_pads[1], axes[3] < (initial[3] >> 1));
207 input_report_key(dev, analog_pads[2], axes[2] > (initial[2] + (initial[2] >> 1)));
209 input_report_key(dev, analog_pads[3], axes[3] > (initial[3] + (initial[3] >> 1)));
211 for (i = j = 0; i < 4; i++)
212 if (analog->
mask & (1 << i))
213 input_report_abs(dev, analog_axes[j++], axes[i]);
215 for (i = j = 0; i < 3; i++)
216 if (analog->
mask & analog_exts[i]) {
217 input_report_abs(dev, analog_hats[j++],
218 ((buttons >> ((i << 2) + 7)) & 1) - ((buttons >> ((i << 2) + 9)) & 1));
219 input_report_abs(dev, analog_hats[j++],
220 ((buttons >> ((i << 2) + 8)) & 1) - ((buttons >> ((i << 2) + 6)) & 1));
242 gameport_trigger(gameport);
255 this = gameport_read(gameport) & port->
mask;
259 if ((last ^
this) && (
DELTA(loop, now) < loopout)) {
260 data[
i] = last ^
this;
265 }
while (
this && (i < 4) && (
DELTA(start, now) < timeout));
269 for (--i; i >= 0; i--) {
271 for (j = 0; j < 4; j++)
272 if (data[i] & (1 << j))
276 return -(
this != port->
mask);
279 static int analog_button_read(
struct analog_port *port,
char saitek,
char chf)
288 port->
buttons = (~u >> 4) & 0xf;
294 while ((~u & 0xf0) && (i < 16) && t) {
295 port->
buttons |= 1 << analog_chf[(~u >> 4) & 0xf];
296 if (!saitek)
return 0;
300 while (((u = gameport_read(port->
gameport)) & port->
mask) && t) t--;
304 return -(!t || (i == 16));
311 static void analog_poll(
struct gameport *gameport)
313 struct analog_port *port = gameport_get_drvdata(gameport);
326 port->
bads -= analog_cooked_read(port);
327 port->
bads -= analog_button_read(port, saitek, chf);
332 analog_button_read(port, saitek, chf);
336 for (i = 0; i < 2; i++)
345 static int analog_open(
struct input_dev *
dev)
357 static void analog_close(
struct input_dev *
dev)
369 static void analog_calibrate_timer(
struct analog_port *port)
371 struct gameport *gameport = port->
gameport;
378 analog_faketime += 830;
389 for (i = 0; i < 50; i++) {
392 for (t = 0; t < 50; t++) { gameport_read(gameport);
GET_TIME(t2); }
400 port->
loop = tx / 50;
422 sizeof(analog->
name));
425 sizeof(analog->
name));
432 static int analog_init_device(
struct analog_port *port,
struct analog *analog,
int index)
434 struct input_dev *input_dev;
440 "%s/input%d", port->
gameport->phys, index);
443 analog->
dev = input_dev = input_allocate_device();
447 input_dev->name = analog->
name;
448 input_dev->phys = analog->
phys;
451 input_dev->id.product = analog->
mask >> 4;
452 input_dev->id.version = 0x0100;
453 input_dev->dev.parent = &port->
gameport->dev;
455 input_set_drvdata(input_dev, port);
457 input_dev->open = analog_open;
458 input_dev->close = analog_close;
462 for (i = j = 0; i < 4; i++)
463 if (analog->
mask & (1 << i)) {
467 y = (port->
axes[0] + port->
axes[1]) >> 1;
468 z = y - port->
axes[
i];
473 if ((i == 2 || i == 3) && (j == 2 || j == 3) && (z > (y >> 3)))
477 if (i == 2) x = port->
axes[
i];
482 input_set_abs_params(input_dev, t, v, (x << 1) - v, port->
fuzz, w);
486 for (i = j = 0; i < 3; i++)
487 if (analog->
mask & analog_exts[i])
488 for (x = 0; x < 2; x++) {
489 t = analog_hats[j++];
490 input_set_abs_params(input_dev, t, -1, 1, 0, 0);
493 for (i = j = 0; i < 4; i++)
494 if (analog->
mask & (0x10 << i))
498 for (i = 0; i < 2; i++)
502 for (i = 0; i < 4; i++)
505 for (i = 0; i < 4; i++)
507 set_bit(analog_pads[i], input_dev->keybit);
511 error = input_register_device(analog->
dev);
513 input_free_device(analog->
dev);
524 static int analog_init_masks(
struct analog_port *port)
527 struct analog *analog = port->
analog;
533 if ((port->
mask & 3) != 3 && port->
mask != 0
xc) {
535 "(data=%#x, %s), probably not analog joystick.\n",
541 i = analog_options[0];
543 analog[0].
mask = i & 0xfffff;
554 | ((~analog[0].
mask & ANALOG_HAT_FCS) << 2)
555 | ((~analog[0].
mask & ANALOG_HAT_FCS) << 4);
561 analog[1].
mask = ((i >> 20) & 0xff) | ((i >> 12) & 0xf0000);
568 for (i = 0; i < 4; i++) max[i] = port->
axes[i] << 1;
570 if ((analog[0].
mask & 0x7) == 0x7) max[2] = (max[0] + max[1]) >> 1;
571 if ((analog[0].
mask & 0
xb) == 0xb) max[3] = (max[0] + max[1]) >> 1;
574 if ((analog[0].
mask & ANALOG_HAT_FCS)) max[3] >>= 1;
579 for (i = 0; i < 4; i++)
582 return -!(analog[0].
mask || analog[1].
mask);
591 gameport_set_drvdata(gameport, port);
595 analog_calibrate_timer(port);
597 gameport_trigger(gameport);
598 t = gameport_read(gameport);
600 port->
mask = (gameport_read(gameport) ^
t) & t & 0xf;
604 if (!analog_cooked_read(port))
613 gameport_trigger(gameport);
614 while ((gameport_read(port->
gameport) & port->
mask) && (u < t))
618 gameport_trigger(gameport);
619 while ((gameport_read(port->
gameport) & port->
mask) && (v < t))
634 if (!gameport_cooked_read(gameport, port->
axes, &port->
buttons))
636 for (i = 0; i < 4; i++)
637 if (port->
axes[i] != -1)
638 port->
mask |= 1 <<
i;
648 static int analog_connect(
struct gameport *gameport,
struct gameport_driver *drv)
657 err = analog_init_port(gameport, drv, port);
661 err = analog_init_masks(port);
665 gameport_set_poll_handler(gameport, analog_poll);
666 gameport_set_poll_interval(gameport, 10);
668 for (i = 0; i < 2; i++)
669 if (port->
analog[i].mask) {
670 err = analog_init_device(port, port->
analog + i, i);
677 fail3:
while (--i >= 0)
679 input_unregister_device(port->
analog[i].dev);
681 fail1: gameport_set_drvdata(gameport,
NULL);
686 static void analog_disconnect(
struct gameport *gameport)
688 struct analog_port *port = gameport_get_drvdata(gameport);
691 for (i = 0; i < 2; i++)
693 input_unregister_device(port->
analog[i].dev);
695 gameport_set_drvdata(gameport,
NULL);
708 {
"none", 0x00000000 },
709 {
"auto", 0x000000ff },
710 {
"2btn", 0x0000003f },
711 {
"y-joy", 0x0cc00033 },
712 {
"y-pad", 0x8cc80033 },
713 {
"fcs", 0x000008f7 },
714 {
"chf", 0x000002ff },
715 {
"fullchf", 0x000007ff },
716 {
"gamepad", 0x000830f3 },
717 {
"gamepad8", 0x0008f0f3 },
721 static void analog_parse_options(
void)
726 for (i = 0; i < js_nargs; i++) {
728 for (j = 0; analog_types[
j].
name; j++)
730 analog_options[
i] = analog_types[
j].
value;
733 if (analog_types[j].name)
continue;
736 if (end != js[i])
continue;
738 analog_options[
i] = 0xff;
739 if (!
strlen(js[i]))
continue;
745 analog_options[i] = 0xff;
757 .connect = analog_connect,
758 .disconnect = analog_disconnect,
761 static int __init analog_init(
void)
763 analog_parse_options();
767 static void __exit analog_exit(
void)