2 * Copyright (c) 1996-2001 Vojtech Pavlik
6 * Analog joystick and gamepad 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
25 #include <linux/delay.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/bitops.h>
30 #include <linux/init.h>
31 #include <linux/input.h>
32 #include <linux/gameport.h>
33 #include <linux/jiffies.h>
34 #include <linux/timex.h>
35 #include <linux/timekeeping.h>
37 #define DRIVER_DESC "Analog joystick and gamepad driver"
39 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
40 MODULE_DESCRIPTION(DRIVER_DESC
);
41 MODULE_LICENSE("GPL");
43 static bool use_ktime
= true;
44 module_param(use_ktime
, bool, 0400);
45 MODULE_PARM_DESC(use_ktime
, "Use ktime for measuring I/O speed");
51 #define ANALOG_PORTS 16
53 static char *js
[ANALOG_PORTS
];
54 static unsigned int js_nargs
;
55 static int analog_options
[ANALOG_PORTS
];
56 module_param_array_named(map
, js
, charp
, &js_nargs
, 0);
57 MODULE_PARM_DESC(map
, "Describes analog joysticks type/capabilities");
60 * Times, feature definitions.
63 #define ANALOG_RUDDER 0x00004
64 #define ANALOG_THROTTLE 0x00008
65 #define ANALOG_AXES_STD 0x0000f
66 #define ANALOG_BTNS_STD 0x000f0
68 #define ANALOG_BTNS_CHF 0x00100
69 #define ANALOG_HAT1_CHF 0x00200
70 #define ANALOG_HAT2_CHF 0x00400
71 #define ANALOG_HAT_FCS 0x00800
72 #define ANALOG_HATS_ALL 0x00e00
73 #define ANALOG_BTN_TL 0x01000
74 #define ANALOG_BTN_TR 0x02000
75 #define ANALOG_BTN_TL2 0x04000
76 #define ANALOG_BTN_TR2 0x08000
77 #define ANALOG_BTNS_TLR 0x03000
78 #define ANALOG_BTNS_TLR2 0x0c000
79 #define ANALOG_BTNS_GAMEPAD 0x0f000
81 #define ANALOG_HBTN_CHF 0x10000
82 #define ANALOG_ANY_CHF 0x10700
83 #define ANALOG_SAITEK 0x20000
84 #define ANALOG_EXTENSIONS 0x7ff00
85 #define ANALOG_GAMEPAD 0x80000
87 #define ANALOG_MAX_TIME 3 /* 3 ms */
88 #define ANALOG_LOOP_TIME 2000 /* 2 * loop */
89 #define ANALOG_SAITEK_DELAY 200 /* 200 us */
90 #define ANALOG_SAITEK_TIME 2000 /* 2000 us */
91 #define ANALOG_AXIS_TIME 2 /* 2 * refresh */
92 #define ANALOG_INIT_RETRIES 8 /* 8 times */
93 #define ANALOG_FUZZ_BITS 2 /* 2 bit more */
94 #define ANALOG_FUZZ_MAGIC 36 /* 36 u*ms/loop */
96 #define ANALOG_MAX_NAME_LENGTH 128
97 #define ANALOG_MAX_PHYS_LENGTH 32
99 static short analog_axes
[] = { ABS_X
, ABS_Y
, ABS_RUDDER
, ABS_THROTTLE
};
100 static short analog_hats
[] = { ABS_HAT0X
, ABS_HAT0Y
, ABS_HAT1X
, ABS_HAT1Y
, ABS_HAT2X
, ABS_HAT2Y
};
101 static short analog_pads
[] = { BTN_Y
, BTN_Z
, BTN_TL
, BTN_TR
};
102 static short analog_exts
[] = { ANALOG_HAT1_CHF
, ANALOG_HAT2_CHF
, ANALOG_HAT_FCS
};
103 static short analog_pad_btn
[] = { BTN_A
, BTN_B
, BTN_C
, BTN_X
, BTN_TL2
, BTN_TR2
, BTN_SELECT
, BTN_START
, BTN_MODE
, BTN_BASE
};
104 static short analog_joy_btn
[] = { BTN_TRIGGER
, BTN_THUMB
, BTN_TOP
, BTN_TOP2
, BTN_BASE
, BTN_BASE2
,
105 BTN_BASE3
, BTN_BASE4
, BTN_BASE5
, BTN_BASE6
};
107 static unsigned char analog_chf
[] = { 0xf, 0x0, 0x1, 0x9, 0x2, 0x4, 0xc, 0x8, 0x3, 0x5, 0xb, 0x7, 0xd, 0xe, 0xa, 0x6 };
110 struct input_dev
*dev
;
113 char name
[ANALOG_MAX_NAME_LENGTH
];
114 char phys
[ANALOG_MAX_PHYS_LENGTH
];
118 struct gameport
*gameport
;
119 struct analog analog
[2];
140 #include <linux/i8253.h>
142 #define GET_TIME(x) do { if (boot_cpu_has(X86_FEATURE_TSC)) x = (unsigned int)rdtsc(); else x = get_time_pit(); } while (0)
143 #define DELTA(x,y) (boot_cpu_has(X86_FEATURE_TSC) ? ((y) - (x)) : ((x) - (y) + ((x) < (y) ? PIT_TICK_RATE / HZ : 0)))
144 #define TIME_NAME (boot_cpu_has(X86_FEATURE_TSC)?"TSC":"PIT")
145 static unsigned int get_time_pit(void)
150 raw_spin_lock_irqsave(&i8253_lock
, flags
);
153 count
|= inb_p(0x40) << 8;
154 raw_spin_unlock_irqrestore(&i8253_lock
, flags
);
158 #elif defined(__x86_64__)
159 #define GET_TIME(x) do { x = (unsigned int)rdtsc(); } while (0)
160 #define DELTA(x,y) ((y)-(x))
161 #define TIME_NAME "TSC"
162 #elif defined(__alpha__) || defined(CONFIG_ARM) || defined(CONFIG_ARM64) || defined(CONFIG_PPC) || defined(CONFIG_RISCV)
163 #define GET_TIME(x) do { x = get_cycles(); } while (0)
164 #define DELTA(x,y) ((y)-(x))
165 #define TIME_NAME "get_cycles"
168 static unsigned long analog_faketime
= 0;
169 #define GET_TIME(x) do { x = analog_faketime++; } while(0)
170 #define DELTA(x,y) ((y)-(x))
171 #define TIME_NAME "Unreliable"
172 #warning Precise timer not defined for this architecture.
175 static inline u64
get_time(void)
178 return ktime_get_ns();
186 static inline unsigned int delta(u64 x
, u64 y
)
191 return DELTA((unsigned int)x
, (unsigned int)y
);
195 * analog_decode() decodes analog joystick data and reports input events.
198 static void analog_decode(struct analog
*analog
, int *axes
, int *initial
, int buttons
)
200 struct input_dev
*dev
= analog
->dev
;
203 if (analog
->mask
& ANALOG_HAT_FCS
)
204 for (i
= 0; i
< 4; i
++)
205 if (axes
[3] < ((initial
[3] * ((i
<< 1) + 1)) >> 3)) {
206 buttons
|= 1 << (i
+ 14);
210 for (i
= j
= 0; i
< 6; i
++)
211 if (analog
->mask
& (0x10 << i
))
212 input_report_key(dev
, analog
->buttons
[j
++], (buttons
>> i
) & 1);
214 if (analog
->mask
& ANALOG_HBTN_CHF
)
215 for (i
= 0; i
< 4; i
++)
216 input_report_key(dev
, analog
->buttons
[j
++], (buttons
>> (i
+ 10)) & 1);
218 if (analog
->mask
& ANALOG_BTN_TL
)
219 input_report_key(dev
, analog_pads
[0], axes
[2] < (initial
[2] >> 1));
220 if (analog
->mask
& ANALOG_BTN_TR
)
221 input_report_key(dev
, analog_pads
[1], axes
[3] < (initial
[3] >> 1));
222 if (analog
->mask
& ANALOG_BTN_TL2
)
223 input_report_key(dev
, analog_pads
[2], axes
[2] > (initial
[2] + (initial
[2] >> 1)));
224 if (analog
->mask
& ANALOG_BTN_TR2
)
225 input_report_key(dev
, analog_pads
[3], axes
[3] > (initial
[3] + (initial
[3] >> 1)));
227 for (i
= j
= 0; i
< 4; i
++)
228 if (analog
->mask
& (1 << i
))
229 input_report_abs(dev
, analog_axes
[j
++], axes
[i
]);
231 for (i
= j
= 0; i
< 3; i
++)
232 if (analog
->mask
& analog_exts
[i
]) {
233 input_report_abs(dev
, analog_hats
[j
++],
234 ((buttons
>> ((i
<< 2) + 7)) & 1) - ((buttons
>> ((i
<< 2) + 9)) & 1));
235 input_report_abs(dev
, analog_hats
[j
++],
236 ((buttons
>> ((i
<< 2) + 8)) & 1) - ((buttons
>> ((i
<< 2) + 6)) & 1));
243 * analog_cooked_read() reads analog joystick data.
246 static int analog_cooked_read(struct analog_port
*port
)
248 struct gameport
*gameport
= port
->gameport
;
249 u64 time
[4], start
, loop
, now
;
250 unsigned int loopout
, timeout
;
251 unsigned char data
[4], this, last
;
255 loopout
= (ANALOG_LOOP_TIME
* port
->loop
) / 1000;
256 timeout
= ANALOG_MAX_TIME
* port
->speed
;
258 local_irq_save(flags
);
259 gameport_trigger(gameport
);
261 local_irq_restore(flags
);
272 this = gameport_read(gameport
) & port
->mask
;
274 local_irq_restore(flags
);
276 if ((last
^ this) && (delta(loop
, now
) < loopout
)) {
277 data
[i
] = last
^ this;
282 } while (this && (i
< 4) && (delta(start
, now
) < timeout
));
286 for (--i
; i
>= 0; i
--) {
288 for (j
= 0; j
< 4; j
++)
289 if (data
[i
] & (1 << j
))
290 port
->axes
[j
] = (delta(start
, time
[i
]) << ANALOG_FUZZ_BITS
) / port
->loop
;
293 return -(this != port
->mask
);
296 static int analog_button_read(struct analog_port
*port
, char saitek
, char chf
)
300 int strobe
= gameport_time(port
->gameport
, ANALOG_SAITEK_TIME
);
302 u
= gameport_read(port
->gameport
);
305 port
->buttons
= (~u
>> 4) & 0xf;
311 while ((~u
& 0xf0) && (i
< 16) && t
) {
312 port
->buttons
|= 1 << analog_chf
[(~u
>> 4) & 0xf];
313 if (!saitek
) return 0;
314 udelay(ANALOG_SAITEK_DELAY
);
316 gameport_trigger(port
->gameport
);
317 while (((u
= gameport_read(port
->gameport
)) & port
->mask
) && t
) t
--;
321 return -(!t
|| (i
== 16));
325 * analog_poll() repeatedly polls the Analog joysticks.
328 static void analog_poll(struct gameport
*gameport
)
330 struct analog_port
*port
= gameport_get_drvdata(gameport
);
333 char saitek
= !!(port
->analog
[0].mask
& ANALOG_SAITEK
);
334 char chf
= !!(port
->analog
[0].mask
& ANALOG_ANY_CHF
);
337 port
->bads
-= gameport_cooked_read(port
->gameport
, port
->axes
, &port
->buttons
);
339 port
->buttons
= port
->buttons
? (1 << analog_chf
[port
->buttons
]) : 0;
342 if (!port
->axtime
--) {
343 port
->bads
-= analog_cooked_read(port
);
344 port
->bads
-= analog_button_read(port
, saitek
, chf
);
346 port
->axtime
= ANALOG_AXIS_TIME
- 1;
349 analog_button_read(port
, saitek
, chf
);
353 for (i
= 0; i
< 2; i
++)
354 if (port
->analog
[i
].mask
)
355 analog_decode(port
->analog
+ i
, port
->axes
, port
->initial
, port
->buttons
);
359 * analog_open() is a callback from the input open routine.
362 static int analog_open(struct input_dev
*dev
)
364 struct analog_port
*port
= input_get_drvdata(dev
);
366 gameport_start_polling(port
->gameport
);
371 * analog_close() is a callback from the input close routine.
374 static void analog_close(struct input_dev
*dev
)
376 struct analog_port
*port
= input_get_drvdata(dev
);
378 gameport_stop_polling(port
->gameport
);
382 * analog_calibrate_timer() calibrates the timer and computes loop
383 * and timeout values for a joystick port.
386 static void analog_calibrate_timer(struct analog_port
*port
)
388 struct gameport
*gameport
= port
->gameport
;
389 unsigned int i
, t
, tx
;
394 port
->speed
= 1000000;
396 local_irq_save(flags
);
399 analog_faketime
+= 830;
404 local_irq_restore(flags
);
406 port
->speed
= delta(t1
, t2
) - delta(t2
, t3
);
411 for (i
= 0; i
< 50; i
++) {
412 local_irq_save(flags
);
414 for (t
= 0; t
< 50; t
++) {
415 gameport_read(gameport
);
419 local_irq_restore(flags
);
421 t
= delta(t1
, t2
) - delta(t2
, t3
);
425 port
->loop
= tx
/ 50;
429 * analog_name() constructs a name for an analog joystick.
432 static void analog_name(struct analog
*analog
)
434 snprintf(analog
->name
, sizeof(analog
->name
), "Analog %d-axis %d-button",
435 hweight8(analog
->mask
& ANALOG_AXES_STD
),
436 hweight8(analog
->mask
& ANALOG_BTNS_STD
) + !!(analog
->mask
& ANALOG_BTNS_CHF
) * 2 +
437 hweight16(analog
->mask
& ANALOG_BTNS_GAMEPAD
) + !!(analog
->mask
& ANALOG_HBTN_CHF
) * 4);
439 if (analog
->mask
& ANALOG_HATS_ALL
)
440 snprintf(analog
->name
, sizeof(analog
->name
), "%s %d-hat",
441 analog
->name
, hweight16(analog
->mask
& ANALOG_HATS_ALL
));
443 if (analog
->mask
& ANALOG_HAT_FCS
)
444 strlcat(analog
->name
, " FCS", sizeof(analog
->name
));
445 if (analog
->mask
& ANALOG_ANY_CHF
)
446 strlcat(analog
->name
, (analog
->mask
& ANALOG_SAITEK
) ? " Saitek" : " CHF",
447 sizeof(analog
->name
));
449 strlcat(analog
->name
, (analog
->mask
& ANALOG_GAMEPAD
) ? " gamepad": " joystick",
450 sizeof(analog
->name
));
454 * analog_init_device()
457 static int analog_init_device(struct analog_port
*port
, struct analog
*analog
, int index
)
459 struct input_dev
*input_dev
;
460 int i
, j
, t
, v
, w
, x
, y
, z
;
464 snprintf(analog
->phys
, sizeof(analog
->phys
),
465 "%s/input%d", port
->gameport
->phys
, index
);
466 analog
->buttons
= (analog
->mask
& ANALOG_GAMEPAD
) ? analog_pad_btn
: analog_joy_btn
;
468 analog
->dev
= input_dev
= input_allocate_device();
472 input_dev
->name
= analog
->name
;
473 input_dev
->phys
= analog
->phys
;
474 input_dev
->id
.bustype
= BUS_GAMEPORT
;
475 input_dev
->id
.vendor
= GAMEPORT_ID_VENDOR_ANALOG
;
476 input_dev
->id
.product
= analog
->mask
>> 4;
477 input_dev
->id
.version
= 0x0100;
478 input_dev
->dev
.parent
= &port
->gameport
->dev
;
480 input_set_drvdata(input_dev
, port
);
482 input_dev
->open
= analog_open
;
483 input_dev
->close
= analog_close
;
485 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_ABS
);
487 for (i
= j
= 0; i
< 4; i
++)
488 if (analog
->mask
& (1 << i
)) {
492 y
= (port
->axes
[0] + port
->axes
[1]) >> 1;
493 z
= y
- port
->axes
[i
];
498 if ((i
== 2 || i
== 3) && (j
== 2 || j
== 3) && (z
> (y
>> 3)))
501 if (analog
->mask
& ANALOG_SAITEK
) {
502 if (i
== 2) x
= port
->axes
[i
];
507 input_set_abs_params(input_dev
, t
, v
, (x
<< 1) - v
, port
->fuzz
, w
);
511 for (i
= j
= 0; i
< 3; i
++)
512 if (analog
->mask
& analog_exts
[i
])
513 for (x
= 0; x
< 2; x
++) {
514 t
= analog_hats
[j
++];
515 input_set_abs_params(input_dev
, t
, -1, 1, 0, 0);
518 for (i
= j
= 0; i
< 4; i
++)
519 if (analog
->mask
& (0x10 << i
))
520 set_bit(analog
->buttons
[j
++], input_dev
->keybit
);
522 if (analog
->mask
& ANALOG_BTNS_CHF
)
523 for (i
= 0; i
< 2; i
++)
524 set_bit(analog
->buttons
[j
++], input_dev
->keybit
);
526 if (analog
->mask
& ANALOG_HBTN_CHF
)
527 for (i
= 0; i
< 4; i
++)
528 set_bit(analog
->buttons
[j
++], input_dev
->keybit
);
530 for (i
= 0; i
< 4; i
++)
531 if (analog
->mask
& (ANALOG_BTN_TL
<< i
))
532 set_bit(analog_pads
[i
], input_dev
->keybit
);
534 analog_decode(analog
, port
->axes
, port
->initial
, port
->buttons
);
536 error
= input_register_device(analog
->dev
);
538 input_free_device(analog
->dev
);
546 * analog_init_devices() sets up device-specific values and registers the input devices.
549 static int analog_init_masks(struct analog_port
*port
)
552 struct analog
*analog
= port
->analog
;
558 if ((port
->mask
& 3) != 3 && port
->mask
!= 0xc) {
559 printk(KERN_WARNING
"analog.c: Unknown joystick device found "
560 "(data=%#x, %s), probably not analog joystick.\n",
561 port
->mask
, port
->gameport
->phys
);
566 i
= analog_options
[0]; /* FIXME !!! - need to specify options for different ports */
568 analog
[0].mask
= i
& 0xfffff;
570 analog
[0].mask
&= ~(ANALOG_AXES_STD
| ANALOG_HAT_FCS
| ANALOG_BTNS_GAMEPAD
)
571 | port
->mask
| ((port
->mask
<< 8) & ANALOG_HAT_FCS
)
572 | ((port
->mask
<< 10) & ANALOG_BTNS_TLR
) | ((port
->mask
<< 12) & ANALOG_BTNS_TLR2
);
574 analog
[0].mask
&= ~(ANALOG_HAT2_CHF
)
575 | ((analog
[0].mask
& ANALOG_HBTN_CHF
) ? 0 : ANALOG_HAT2_CHF
);
577 analog
[0].mask
&= ~(ANALOG_THROTTLE
| ANALOG_BTN_TR
| ANALOG_BTN_TR2
)
578 | ((~analog
[0].mask
& ANALOG_HAT_FCS
) >> 8)
579 | ((~analog
[0].mask
& ANALOG_HAT_FCS
) << 2)
580 | ((~analog
[0].mask
& ANALOG_HAT_FCS
) << 4);
582 analog
[0].mask
&= ~(ANALOG_THROTTLE
| ANALOG_RUDDER
)
583 | (((~analog
[0].mask
& ANALOG_BTNS_TLR
) >> 10)
584 & ((~analog
[0].mask
& ANALOG_BTNS_TLR2
) >> 12));
586 analog
[1].mask
= ((i
>> 20) & 0xff) | ((i
>> 12) & 0xf0000);
588 analog
[1].mask
&= (analog
[0].mask
& ANALOG_EXTENSIONS
) ? ANALOG_GAMEPAD
589 : (((ANALOG_BTNS_STD
| port
->mask
) & ~analog
[0].mask
) | ANALOG_GAMEPAD
);
593 for (i
= 0; i
< 4; i
++) max
[i
] = port
->axes
[i
] << 1;
595 if ((analog
[0].mask
& 0x7) == 0x7) max
[2] = (max
[0] + max
[1]) >> 1;
596 if ((analog
[0].mask
& 0xb) == 0xb) max
[3] = (max
[0] + max
[1]) >> 1;
597 if ((analog
[0].mask
& ANALOG_BTN_TL
) && !(analog
[0].mask
& ANALOG_BTN_TL2
)) max
[2] >>= 1;
598 if ((analog
[0].mask
& ANALOG_BTN_TR
) && !(analog
[0].mask
& ANALOG_BTN_TR2
)) max
[3] >>= 1;
599 if ((analog
[0].mask
& ANALOG_HAT_FCS
)) max
[3] >>= 1;
601 gameport_calibrate(port
->gameport
, port
->axes
, max
);
604 for (i
= 0; i
< 4; i
++)
605 port
->initial
[i
] = port
->axes
[i
];
607 return -!(analog
[0].mask
|| analog
[1].mask
);
610 static int analog_init_port(struct gameport
*gameport
, struct gameport_driver
*drv
, struct analog_port
*port
)
614 port
->gameport
= gameport
;
616 gameport_set_drvdata(gameport
, port
);
618 if (!gameport_open(gameport
, drv
, GAMEPORT_MODE_RAW
)) {
620 analog_calibrate_timer(port
);
622 gameport_trigger(gameport
);
623 t
= gameport_read(gameport
);
624 msleep(ANALOG_MAX_TIME
);
625 port
->mask
= (gameport_read(gameport
) ^ t
) & t
& 0xf;
626 port
->fuzz
= (port
->speed
* ANALOG_FUZZ_MAGIC
) / port
->loop
/ 1000 + ANALOG_FUZZ_BITS
;
628 for (i
= 0; i
< ANALOG_INIT_RETRIES
; i
++) {
629 if (!analog_cooked_read(port
))
631 msleep(ANALOG_MAX_TIME
);
636 msleep(ANALOG_MAX_TIME
);
637 t
= gameport_time(gameport
, ANALOG_MAX_TIME
* 1000);
638 gameport_trigger(gameport
);
639 while ((gameport_read(port
->gameport
) & port
->mask
) && (u
< t
))
641 udelay(ANALOG_SAITEK_DELAY
);
642 t
= gameport_time(gameport
, ANALOG_SAITEK_TIME
);
643 gameport_trigger(gameport
);
644 while ((gameport_read(port
->gameport
) & port
->mask
) && (v
< t
))
647 if (v
< (u
>> 1)) { /* FIXME - more than one port */
648 analog_options
[0] |= /* FIXME - more than one port */
649 ANALOG_SAITEK
| ANALOG_BTNS_CHF
| ANALOG_HBTN_CHF
| ANALOG_HAT1_CHF
;
653 gameport_close(gameport
);
656 if (!gameport_open(gameport
, drv
, GAMEPORT_MODE_COOKED
)) {
658 for (i
= 0; i
< ANALOG_INIT_RETRIES
; i
++)
659 if (!gameport_cooked_read(gameport
, port
->axes
, &port
->buttons
))
661 for (i
= 0; i
< 4; i
++)
662 if (port
->axes
[i
] != -1)
663 port
->mask
|= 1 << i
;
665 port
->fuzz
= gameport
->fuzz
;
670 return gameport_open(gameport
, drv
, GAMEPORT_MODE_RAW
);
673 static int analog_connect(struct gameport
*gameport
, struct gameport_driver
*drv
)
675 struct analog_port
*port
;
679 if (!(port
= kzalloc(sizeof(struct analog_port
), GFP_KERNEL
)))
682 err
= analog_init_port(gameport
, drv
, port
);
686 err
= analog_init_masks(port
);
690 gameport_set_poll_handler(gameport
, analog_poll
);
691 gameport_set_poll_interval(gameport
, 10);
693 for (i
= 0; i
< 2; i
++)
694 if (port
->analog
[i
].mask
) {
695 err
= analog_init_device(port
, port
->analog
+ i
, i
);
702 fail3
: while (--i
>= 0)
703 if (port
->analog
[i
].mask
)
704 input_unregister_device(port
->analog
[i
].dev
);
705 fail2
: gameport_close(gameport
);
706 fail1
: gameport_set_drvdata(gameport
, NULL
);
711 static void analog_disconnect(struct gameport
*gameport
)
713 struct analog_port
*port
= gameport_get_drvdata(gameport
);
716 for (i
= 0; i
< 2; i
++)
717 if (port
->analog
[i
].mask
)
718 input_unregister_device(port
->analog
[i
].dev
);
719 gameport_close(gameport
);
720 gameport_set_drvdata(gameport
, NULL
);
721 printk(KERN_INFO
"analog.c: %d out of %d reads (%d%%) on %s failed\n",
722 port
->bads
, port
->reads
, port
->reads
? (port
->bads
* 100 / port
->reads
) : 0,
723 port
->gameport
->phys
);
727 struct analog_types
{
732 static struct analog_types analog_types
[] = {
733 { "none", 0x00000000 },
734 { "auto", 0x000000ff },
735 { "2btn", 0x0000003f },
736 { "y-joy", 0x0cc00033 },
737 { "y-pad", 0x8cc80033 },
738 { "fcs", 0x000008f7 },
739 { "chf", 0x000002ff },
740 { "fullchf", 0x000007ff },
741 { "gamepad", 0x000830f3 },
742 { "gamepad8", 0x0008f0f3 },
746 static void analog_parse_options(void)
751 for (i
= 0; i
< js_nargs
; i
++) {
753 for (j
= 0; analog_types
[j
].name
; j
++)
754 if (!strcmp(analog_types
[j
].name
, js
[i
])) {
755 analog_options
[i
] = analog_types
[j
].value
;
758 if (analog_types
[j
].name
) continue;
760 analog_options
[i
] = simple_strtoul(js
[i
], &end
, 0);
761 if (end
!= js
[i
]) continue;
763 analog_options
[i
] = 0xff;
764 if (!strlen(js
[i
])) continue;
766 printk(KERN_WARNING
"analog.c: Bad config for port %d - \"%s\"\n", i
, js
[i
]);
769 for (; i
< ANALOG_PORTS
; i
++)
770 analog_options
[i
] = 0xff;
774 * The gameport device structure.
777 static struct gameport_driver analog_drv
= {
781 .description
= DRIVER_DESC
,
782 .connect
= analog_connect
,
783 .disconnect
= analog_disconnect
,
786 static int __init
analog_init(void)
788 analog_parse_options();
789 return gameport_register_driver(&analog_drv
);
792 static void __exit
analog_exit(void)
794 gameport_unregister_driver(&analog_drv
);
797 module_init(analog_init
);
798 module_exit(analog_exit
);