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