xref: /linux/drivers/input/joystick/analog.c (revision da1d9caf95def6f0320819cf941c9fd1069ba9e1)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (c) 1996-2001 Vojtech Pavlik
4  */
5 
6 /*
7  * Analog joystick and gamepad driver for Linux
8  */
9 
10 /*
11  */
12 
13 #include <linux/delay.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/bitops.h>
18 #include <linux/init.h>
19 #include <linux/input.h>
20 #include <linux/gameport.h>
21 #include <linux/jiffies.h>
22 #include <linux/seq_buf.h>
23 #include <linux/timex.h>
24 #include <linux/timekeeping.h>
25 
26 #define DRIVER_DESC	"Analog joystick and gamepad driver"
27 
28 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
29 MODULE_DESCRIPTION(DRIVER_DESC);
30 MODULE_LICENSE("GPL");
31 
32 /*
33  * Option parsing.
34  */
35 
36 #define ANALOG_PORTS		16
37 
38 static char *js[ANALOG_PORTS];
39 static unsigned int js_nargs;
40 static int analog_options[ANALOG_PORTS];
41 module_param_array_named(map, js, charp, &js_nargs, 0);
42 MODULE_PARM_DESC(map, "Describes analog joysticks type/capabilities");
43 
44 /*
45  * Times, feature definitions.
46  */
47 
48 #define ANALOG_RUDDER		0x00004
49 #define ANALOG_THROTTLE		0x00008
50 #define ANALOG_AXES_STD		0x0000f
51 #define ANALOG_BTNS_STD		0x000f0
52 
53 #define ANALOG_BTNS_CHF		0x00100
54 #define ANALOG_HAT1_CHF		0x00200
55 #define ANALOG_HAT2_CHF		0x00400
56 #define ANALOG_HAT_FCS		0x00800
57 #define ANALOG_HATS_ALL		0x00e00
58 #define ANALOG_BTN_TL		0x01000
59 #define ANALOG_BTN_TR		0x02000
60 #define ANALOG_BTN_TL2		0x04000
61 #define ANALOG_BTN_TR2		0x08000
62 #define ANALOG_BTNS_TLR		0x03000
63 #define ANALOG_BTNS_TLR2	0x0c000
64 #define ANALOG_BTNS_GAMEPAD	0x0f000
65 
66 #define ANALOG_HBTN_CHF		0x10000
67 #define ANALOG_ANY_CHF		0x10700
68 #define ANALOG_SAITEK		0x20000
69 #define ANALOG_EXTENSIONS	0x7ff00
70 #define ANALOG_GAMEPAD		0x80000
71 
72 #define ANALOG_MAX_TIME		3	/* 3 ms */
73 #define ANALOG_LOOP_TIME	2000	/* 2 * loop */
74 #define ANALOG_SAITEK_DELAY	200	/* 200 us */
75 #define ANALOG_SAITEK_TIME	2000	/* 2000 us */
76 #define ANALOG_AXIS_TIME	2	/* 2 * refresh */
77 #define ANALOG_INIT_RETRIES	8	/* 8 times */
78 #define ANALOG_FUZZ_BITS	2	/* 2 bit more */
79 #define ANALOG_FUZZ_MAGIC	36	/* 36 u*ms/loop */
80 
81 #define ANALOG_MAX_NAME_LENGTH  128
82 #define ANALOG_MAX_PHYS_LENGTH	32
83 
84 static short analog_axes[] = { ABS_X, ABS_Y, ABS_RUDDER, ABS_THROTTLE };
85 static short analog_hats[] = { ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y };
86 static short analog_pads[] = { BTN_Y, BTN_Z, BTN_TL, BTN_TR };
87 static short analog_exts[] = { ANALOG_HAT1_CHF, ANALOG_HAT2_CHF, ANALOG_HAT_FCS };
88 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 };
89 static short analog_joy_btn[] = { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2,
90 				  BTN_BASE3, BTN_BASE4, BTN_BASE5, BTN_BASE6 };
91 
92 static unsigned char analog_chf[] = { 0xf, 0x0, 0x1, 0x9, 0x2, 0x4, 0xc, 0x8, 0x3, 0x5, 0xb, 0x7, 0xd, 0xe, 0xa, 0x6 };
93 
94 struct analog {
95 	struct input_dev *dev;
96 	int mask;
97 	short *buttons;
98 	char name[ANALOG_MAX_NAME_LENGTH];
99 	char phys[ANALOG_MAX_PHYS_LENGTH];
100 };
101 
102 struct analog_port {
103 	struct gameport *gameport;
104 	struct analog analog[2];
105 	unsigned char mask;
106 	char saitek;
107 	char cooked;
108 	int bads;
109 	int reads;
110 	int loop;
111 	int fuzz;
112 	int axes[4];
113 	int buttons;
114 	int initial[4];
115 	int axtime;
116 };
117 
118 /*
119  * analog_decode() decodes analog joystick data and reports input events.
120  */
121 
122 static void analog_decode(struct analog *analog, int *axes, int *initial, int buttons)
123 {
124 	struct input_dev *dev = analog->dev;
125 	int i, j;
126 
127 	if (analog->mask & ANALOG_HAT_FCS)
128 		for (i = 0; i < 4; i++)
129 			if (axes[3] < ((initial[3] * ((i << 1) + 1)) >> 3)) {
130 				buttons |= 1 << (i + 14);
131 				break;
132 			}
133 
134 	for (i = j = 0; i < 6; i++)
135 		if (analog->mask & (0x10 << i))
136 			input_report_key(dev, analog->buttons[j++], (buttons >> i) & 1);
137 
138 	if (analog->mask & ANALOG_HBTN_CHF)
139 		for (i = 0; i < 4; i++)
140 			input_report_key(dev, analog->buttons[j++], (buttons >> (i + 10)) & 1);
141 
142 	if (analog->mask & ANALOG_BTN_TL)
143 		input_report_key(dev, analog_pads[0], axes[2] < (initial[2] >> 1));
144 	if (analog->mask & ANALOG_BTN_TR)
145 		input_report_key(dev, analog_pads[1], axes[3] < (initial[3] >> 1));
146 	if (analog->mask & ANALOG_BTN_TL2)
147 		input_report_key(dev, analog_pads[2], axes[2] > (initial[2] + (initial[2] >> 1)));
148 	if (analog->mask & ANALOG_BTN_TR2)
149 		input_report_key(dev, analog_pads[3], axes[3] > (initial[3] + (initial[3] >> 1)));
150 
151 	for (i = j = 0; i < 4; i++)
152 		if (analog->mask & (1 << i))
153 			input_report_abs(dev, analog_axes[j++], axes[i]);
154 
155 	for (i = j = 0; i < 3; i++)
156 		if (analog->mask & analog_exts[i]) {
157 			input_report_abs(dev, analog_hats[j++],
158 				((buttons >> ((i << 2) + 7)) & 1) - ((buttons >> ((i << 2) + 9)) & 1));
159 			input_report_abs(dev, analog_hats[j++],
160 				((buttons >> ((i << 2) + 8)) & 1) - ((buttons >> ((i << 2) + 6)) & 1));
161 		}
162 
163 	input_sync(dev);
164 }
165 
166 /*
167  * analog_cooked_read() reads analog joystick data.
168  */
169 
170 static int analog_cooked_read(struct analog_port *port)
171 {
172 	struct gameport *gameport = port->gameport;
173 	ktime_t time[4], start, loop, now;
174 	unsigned int loopout, timeout;
175 	unsigned char data[4], this, last;
176 	unsigned long flags;
177 	int i, j;
178 
179 	loopout = (ANALOG_LOOP_TIME * port->loop) / 1000;
180 	timeout = ANALOG_MAX_TIME * NSEC_PER_MSEC;
181 
182 	local_irq_save(flags);
183 	gameport_trigger(gameport);
184 	now = ktime_get();
185 	local_irq_restore(flags);
186 
187 	start = now;
188 	this = port->mask;
189 	i = 0;
190 
191 	do {
192 		loop = now;
193 		last = this;
194 
195 		local_irq_disable();
196 		this = gameport_read(gameport) & port->mask;
197 		now = ktime_get();
198 		local_irq_restore(flags);
199 
200 		if ((last ^ this) && (ktime_sub(now, loop) < loopout)) {
201 			data[i] = last ^ this;
202 			time[i] = now;
203 			i++;
204 		}
205 
206 	} while (this && (i < 4) && (ktime_sub(now, start) < timeout));
207 
208 	this <<= 4;
209 
210 	for (--i; i >= 0; i--) {
211 		this |= data[i];
212 		for (j = 0; j < 4; j++)
213 			if (data[i] & (1 << j))
214 				port->axes[j] = ((u32)ktime_sub(time[i], start) << ANALOG_FUZZ_BITS) / port->loop;
215 	}
216 
217 	return -(this != port->mask);
218 }
219 
220 static int analog_button_read(struct analog_port *port, char saitek, char chf)
221 {
222 	unsigned char u;
223 	int t = 1, i = 0;
224 	int strobe = gameport_time(port->gameport, ANALOG_SAITEK_TIME);
225 
226 	u = gameport_read(port->gameport);
227 
228 	if (!chf) {
229 		port->buttons = (~u >> 4) & 0xf;
230 		return 0;
231 	}
232 
233 	port->buttons = 0;
234 
235 	while ((~u & 0xf0) && (i < 16) && t) {
236 		port->buttons |= 1 << analog_chf[(~u >> 4) & 0xf];
237 		if (!saitek) return 0;
238 		udelay(ANALOG_SAITEK_DELAY);
239 		t = strobe;
240 		gameport_trigger(port->gameport);
241 		while (((u = gameport_read(port->gameport)) & port->mask) && t) t--;
242 		i++;
243 	}
244 
245 	return -(!t || (i == 16));
246 }
247 
248 /*
249  * analog_poll() repeatedly polls the Analog joysticks.
250  */
251 
252 static void analog_poll(struct gameport *gameport)
253 {
254 	struct analog_port *port = gameport_get_drvdata(gameport);
255 	int i;
256 
257 	char saitek = !!(port->analog[0].mask & ANALOG_SAITEK);
258 	char chf = !!(port->analog[0].mask & ANALOG_ANY_CHF);
259 
260 	if (port->cooked) {
261 		port->bads -= gameport_cooked_read(port->gameport, port->axes, &port->buttons);
262 		if (chf)
263 			port->buttons = port->buttons ? (1 << analog_chf[port->buttons]) : 0;
264 		port->reads++;
265 	} else {
266 		if (!port->axtime--) {
267 			port->bads -= analog_cooked_read(port);
268 			port->bads -= analog_button_read(port, saitek, chf);
269 			port->reads++;
270 			port->axtime = ANALOG_AXIS_TIME - 1;
271 		} else {
272 			if (!saitek)
273 				analog_button_read(port, saitek, chf);
274 		}
275 	}
276 
277 	for (i = 0; i < 2; i++)
278 		if (port->analog[i].mask)
279 			analog_decode(port->analog + i, port->axes, port->initial, port->buttons);
280 }
281 
282 /*
283  * analog_open() is a callback from the input open routine.
284  */
285 
286 static int analog_open(struct input_dev *dev)
287 {
288 	struct analog_port *port = input_get_drvdata(dev);
289 
290 	gameport_start_polling(port->gameport);
291 	return 0;
292 }
293 
294 /*
295  * analog_close() is a callback from the input close routine.
296  */
297 
298 static void analog_close(struct input_dev *dev)
299 {
300 	struct analog_port *port = input_get_drvdata(dev);
301 
302 	gameport_stop_polling(port->gameport);
303 }
304 
305 /*
306  * analog_calibrate_timer() calibrates the timer and computes loop
307  * and timeout values for a joystick port.
308  */
309 
310 static void analog_calibrate_timer(struct analog_port *port)
311 {
312 	struct gameport *gameport = port->gameport;
313 	unsigned int i, t, tx;
314 	ktime_t t1, t2, t3;
315 	unsigned long flags;
316 
317 	tx = ~0;
318 
319 	for (i = 0; i < 50; i++) {
320 		local_irq_save(flags);
321 		t1 = ktime_get();
322 		for (t = 0; t < 50; t++) {
323 			gameport_read(gameport);
324 			t2 = ktime_get();
325 		}
326 		t3 = ktime_get();
327 		local_irq_restore(flags);
328 		udelay(i);
329 		t = ktime_sub(t2, t1) - ktime_sub(t3, t2);
330 		if (t < tx) tx = t;
331 	}
332 
333         port->loop = tx / 50;
334 }
335 
336 /*
337  * analog_name() constructs a name for an analog joystick.
338  */
339 
340 static void analog_name(struct analog *analog)
341 {
342 	struct seq_buf s;
343 
344 	seq_buf_init(&s, analog->name, sizeof(analog->name));
345 	seq_buf_printf(&s, "Analog %d-axis %d-button",
346 		 hweight8(analog->mask & ANALOG_AXES_STD),
347 		 hweight8(analog->mask & ANALOG_BTNS_STD) + !!(analog->mask & ANALOG_BTNS_CHF) * 2 +
348 		 hweight16(analog->mask & ANALOG_BTNS_GAMEPAD) + !!(analog->mask & ANALOG_HBTN_CHF) * 4);
349 
350 	if (analog->mask & ANALOG_HATS_ALL)
351 		seq_buf_printf(&s, " %d-hat",
352 			       hweight16(analog->mask & ANALOG_HATS_ALL));
353 
354 	if (analog->mask & ANALOG_HAT_FCS)
355 		seq_buf_printf(&s, " FCS");
356 	if (analog->mask & ANALOG_ANY_CHF)
357 		seq_buf_printf(&s, (analog->mask & ANALOG_SAITEK) ? " Saitek" : " CHF");
358 
359 	seq_buf_printf(&s, (analog->mask & ANALOG_GAMEPAD) ? " gamepad" : " joystick");
360 }
361 
362 /*
363  * analog_init_device()
364  */
365 
366 static int analog_init_device(struct analog_port *port, struct analog *analog, int index)
367 {
368 	struct input_dev *input_dev;
369 	int i, j, t, v, w, x, y, z;
370 	int error;
371 
372 	analog_name(analog);
373 	snprintf(analog->phys, sizeof(analog->phys),
374 		 "%s/input%d", port->gameport->phys, index);
375 	analog->buttons = (analog->mask & ANALOG_GAMEPAD) ? analog_pad_btn : analog_joy_btn;
376 
377 	analog->dev = input_dev = input_allocate_device();
378 	if (!input_dev)
379 		return -ENOMEM;
380 
381 	input_dev->name = analog->name;
382 	input_dev->phys = analog->phys;
383 	input_dev->id.bustype = BUS_GAMEPORT;
384 	input_dev->id.vendor = GAMEPORT_ID_VENDOR_ANALOG;
385 	input_dev->id.product = analog->mask >> 4;
386 	input_dev->id.version = 0x0100;
387 	input_dev->dev.parent = &port->gameport->dev;
388 
389 	input_set_drvdata(input_dev, port);
390 
391 	input_dev->open = analog_open;
392 	input_dev->close = analog_close;
393 
394 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
395 
396 	for (i = j = 0; i < 4; i++)
397 		if (analog->mask & (1 << i)) {
398 
399 			t = analog_axes[j];
400 			x = port->axes[i];
401 			y = (port->axes[0] + port->axes[1]) >> 1;
402 			z = y - port->axes[i];
403 			z = z > 0 ? z : -z;
404 			v = (x >> 3);
405 			w = (x >> 3);
406 
407 			if ((i == 2 || i == 3) && (j == 2 || j == 3) && (z > (y >> 3)))
408 				x = y;
409 
410 			if (analog->mask & ANALOG_SAITEK) {
411 				if (i == 2) x = port->axes[i];
412 				v = x - (x >> 2);
413 				w = (x >> 4);
414 			}
415 
416 			input_set_abs_params(input_dev, t, v, (x << 1) - v, port->fuzz, w);
417 			j++;
418 		}
419 
420 	for (i = j = 0; i < 3; i++)
421 		if (analog->mask & analog_exts[i])
422 			for (x = 0; x < 2; x++) {
423 				t = analog_hats[j++];
424 				input_set_abs_params(input_dev, t, -1, 1, 0, 0);
425 			}
426 
427 	for (i = j = 0; i < 4; i++)
428 		if (analog->mask & (0x10 << i))
429 			set_bit(analog->buttons[j++], input_dev->keybit);
430 
431 	if (analog->mask & ANALOG_BTNS_CHF)
432 		for (i = 0; i < 2; i++)
433 			set_bit(analog->buttons[j++], input_dev->keybit);
434 
435 	if (analog->mask & ANALOG_HBTN_CHF)
436 		for (i = 0; i < 4; i++)
437 			set_bit(analog->buttons[j++], input_dev->keybit);
438 
439 	for (i = 0; i < 4; i++)
440 		if (analog->mask & (ANALOG_BTN_TL << i))
441 			set_bit(analog_pads[i], input_dev->keybit);
442 
443 	analog_decode(analog, port->axes, port->initial, port->buttons);
444 
445 	error = input_register_device(analog->dev);
446 	if (error) {
447 		input_free_device(analog->dev);
448 		return error;
449 	}
450 
451 	return 0;
452 }
453 
454 /*
455  * analog_init_devices() sets up device-specific values and registers the input devices.
456  */
457 
458 static int analog_init_masks(struct analog_port *port)
459 {
460 	int i;
461 	struct analog *analog = port->analog;
462 	int max[4];
463 
464 	if (!port->mask)
465 		return -1;
466 
467 	if ((port->mask & 3) != 3 && port->mask != 0xc) {
468 		printk(KERN_WARNING "analog.c: Unknown joystick device found  "
469 			"(data=%#x, %s), probably not analog joystick.\n",
470 			port->mask, port->gameport->phys);
471 		return -1;
472 	}
473 
474 
475 	i = analog_options[0]; /* FIXME !!! - need to specify options for different ports */
476 
477 	analog[0].mask = i & 0xfffff;
478 
479 	analog[0].mask &= ~(ANALOG_AXES_STD | ANALOG_HAT_FCS | ANALOG_BTNS_GAMEPAD)
480 			| port->mask | ((port->mask << 8) & ANALOG_HAT_FCS)
481 			| ((port->mask << 10) & ANALOG_BTNS_TLR) | ((port->mask << 12) & ANALOG_BTNS_TLR2);
482 
483 	analog[0].mask &= ~(ANALOG_HAT2_CHF)
484 			| ((analog[0].mask & ANALOG_HBTN_CHF) ? 0 : ANALOG_HAT2_CHF);
485 
486 	analog[0].mask &= ~(ANALOG_THROTTLE | ANALOG_BTN_TR | ANALOG_BTN_TR2)
487 			| ((~analog[0].mask & ANALOG_HAT_FCS) >> 8)
488 			| ((~analog[0].mask & ANALOG_HAT_FCS) << 2)
489 			| ((~analog[0].mask & ANALOG_HAT_FCS) << 4);
490 
491 	analog[0].mask &= ~(ANALOG_THROTTLE | ANALOG_RUDDER)
492 			| (((~analog[0].mask & ANALOG_BTNS_TLR ) >> 10)
493 			&  ((~analog[0].mask & ANALOG_BTNS_TLR2) >> 12));
494 
495 	analog[1].mask = ((i >> 20) & 0xff) | ((i >> 12) & 0xf0000);
496 
497 	analog[1].mask &= (analog[0].mask & ANALOG_EXTENSIONS) ? ANALOG_GAMEPAD
498 			: (((ANALOG_BTNS_STD | port->mask) & ~analog[0].mask) | ANALOG_GAMEPAD);
499 
500 	if (port->cooked) {
501 
502 		for (i = 0; i < 4; i++) max[i] = port->axes[i] << 1;
503 
504 		if ((analog[0].mask & 0x7) == 0x7) max[2] = (max[0] + max[1]) >> 1;
505 		if ((analog[0].mask & 0xb) == 0xb) max[3] = (max[0] + max[1]) >> 1;
506 		if ((analog[0].mask & ANALOG_BTN_TL) && !(analog[0].mask & ANALOG_BTN_TL2)) max[2] >>= 1;
507 		if ((analog[0].mask & ANALOG_BTN_TR) && !(analog[0].mask & ANALOG_BTN_TR2)) max[3] >>= 1;
508 		if ((analog[0].mask & ANALOG_HAT_FCS)) max[3] >>= 1;
509 
510 		gameport_calibrate(port->gameport, port->axes, max);
511 	}
512 
513 	for (i = 0; i < 4; i++)
514 		port->initial[i] = port->axes[i];
515 
516 	return -!(analog[0].mask || analog[1].mask);
517 }
518 
519 static int analog_init_port(struct gameport *gameport, struct gameport_driver *drv, struct analog_port *port)
520 {
521 	int i, t, u, v;
522 
523 	port->gameport = gameport;
524 
525 	gameport_set_drvdata(gameport, port);
526 
527 	if (!gameport_open(gameport, drv, GAMEPORT_MODE_RAW)) {
528 
529 		analog_calibrate_timer(port);
530 
531 		gameport_trigger(gameport);
532 		t = gameport_read(gameport);
533 		msleep(ANALOG_MAX_TIME);
534 		port->mask = (gameport_read(gameport) ^ t) & t & 0xf;
535 		port->fuzz = (NSEC_PER_MSEC * ANALOG_FUZZ_MAGIC) / port->loop / 1000 + ANALOG_FUZZ_BITS;
536 
537 		for (i = 0; i < ANALOG_INIT_RETRIES; i++) {
538 			if (!analog_cooked_read(port))
539 				break;
540 			msleep(ANALOG_MAX_TIME);
541 		}
542 
543 		u = v = 0;
544 
545 		msleep(ANALOG_MAX_TIME);
546 		t = gameport_time(gameport, ANALOG_MAX_TIME * 1000);
547 		gameport_trigger(gameport);
548 		while ((gameport_read(port->gameport) & port->mask) && (u < t))
549 			u++;
550 		udelay(ANALOG_SAITEK_DELAY);
551 		t = gameport_time(gameport, ANALOG_SAITEK_TIME);
552 		gameport_trigger(gameport);
553 		while ((gameport_read(port->gameport) & port->mask) && (v < t))
554 			v++;
555 
556 		if (v < (u >> 1)) { /* FIXME - more than one port */
557 			analog_options[0] |= /* FIXME - more than one port */
558 				ANALOG_SAITEK | ANALOG_BTNS_CHF | ANALOG_HBTN_CHF | ANALOG_HAT1_CHF;
559 			return 0;
560 		}
561 
562 		gameport_close(gameport);
563 	}
564 
565 	if (!gameport_open(gameport, drv, GAMEPORT_MODE_COOKED)) {
566 
567 		for (i = 0; i < ANALOG_INIT_RETRIES; i++)
568 			if (!gameport_cooked_read(gameport, port->axes, &port->buttons))
569 				break;
570 		for (i = 0; i < 4; i++)
571 			if (port->axes[i] != -1)
572 				port->mask |= 1 << i;
573 
574 		port->fuzz = gameport->fuzz;
575 		port->cooked = 1;
576 		return 0;
577 	}
578 
579 	return gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
580 }
581 
582 static int analog_connect(struct gameport *gameport, struct gameport_driver *drv)
583 {
584 	struct analog_port *port;
585 	int i;
586 	int err;
587 
588 	if (!(port = kzalloc(sizeof(struct analog_port), GFP_KERNEL)))
589 		return -ENOMEM;
590 
591 	err = analog_init_port(gameport, drv, port);
592 	if (err)
593 		goto fail1;
594 
595 	err = analog_init_masks(port);
596 	if (err)
597 		goto fail2;
598 
599 	gameport_set_poll_handler(gameport, analog_poll);
600 	gameport_set_poll_interval(gameport, 10);
601 
602 	for (i = 0; i < 2; i++)
603 		if (port->analog[i].mask) {
604 			err = analog_init_device(port, port->analog + i, i);
605 			if (err)
606 				goto fail3;
607 		}
608 
609 	return 0;
610 
611  fail3: while (--i >= 0)
612 		if (port->analog[i].mask)
613 			input_unregister_device(port->analog[i].dev);
614  fail2:	gameport_close(gameport);
615  fail1:	gameport_set_drvdata(gameport, NULL);
616 	kfree(port);
617 	return err;
618 }
619 
620 static void analog_disconnect(struct gameport *gameport)
621 {
622 	struct analog_port *port = gameport_get_drvdata(gameport);
623 	int i;
624 
625 	for (i = 0; i < 2; i++)
626 		if (port->analog[i].mask)
627 			input_unregister_device(port->analog[i].dev);
628 	gameport_close(gameport);
629 	gameport_set_drvdata(gameport, NULL);
630 	printk(KERN_INFO "analog.c: %d out of %d reads (%d%%) on %s failed\n",
631 		port->bads, port->reads, port->reads ? (port->bads * 100 / port->reads) : 0,
632 		port->gameport->phys);
633 	kfree(port);
634 }
635 
636 struct analog_types {
637 	char *name;
638 	int value;
639 };
640 
641 static struct analog_types analog_types[] = {
642 	{ "none",	0x00000000 },
643 	{ "auto",	0x000000ff },
644 	{ "2btn",	0x0000003f },
645 	{ "y-joy",	0x0cc00033 },
646 	{ "y-pad",	0x8cc80033 },
647 	{ "fcs",	0x000008f7 },
648 	{ "chf",	0x000002ff },
649 	{ "fullchf",	0x000007ff },
650 	{ "gamepad",	0x000830f3 },
651 	{ "gamepad8",	0x0008f0f3 },
652 	{ NULL, 0 }
653 };
654 
655 static void analog_parse_options(void)
656 {
657 	int i, j;
658 	char *end;
659 
660 	for (i = 0; i < js_nargs; i++) {
661 
662 		for (j = 0; analog_types[j].name; j++)
663 			if (!strcmp(analog_types[j].name, js[i])) {
664 				analog_options[i] = analog_types[j].value;
665 				break;
666 			}
667 		if (analog_types[j].name) continue;
668 
669 		analog_options[i] = simple_strtoul(js[i], &end, 0);
670 		if (end != js[i]) continue;
671 
672 		analog_options[i] = 0xff;
673 		if (!strlen(js[i])) continue;
674 
675 		printk(KERN_WARNING "analog.c: Bad config for port %d - \"%s\"\n", i, js[i]);
676 	}
677 
678 	for (; i < ANALOG_PORTS; i++)
679 		analog_options[i] = 0xff;
680 }
681 
682 /*
683  * The gameport device structure.
684  */
685 
686 static struct gameport_driver analog_drv = {
687 	.driver		= {
688 		.name	= "analog",
689 	},
690 	.description	= DRIVER_DESC,
691 	.connect	= analog_connect,
692 	.disconnect	= analog_disconnect,
693 };
694 
695 static int __init analog_init(void)
696 {
697 	analog_parse_options();
698 	return gameport_register_driver(&analog_drv);
699 }
700 
701 static void __exit analog_exit(void)
702 {
703 	gameport_unregister_driver(&analog_drv);
704 }
705 
706 module_init(analog_init);
707 module_exit(analog_exit);
708