Linux Audio

Check our new training course

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