blob: e9a02db36eccc3b0d431f1276f94fe090d4f2923 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * $Id: analog.c,v 1.68 2002/01/22 20:18:32 vojtech Exp $
3 *
4 * Copyright (c) 1996-2001 Vojtech Pavlik
5 */
6
7/*
8 * Analog joystick and gamepad driver for Linux
9 */
10
11/*
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Should you need to contact me, the author, you can do so either by
27 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
28 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
29 */
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/delay.h>
32#include <linux/kernel.h>
33#include <linux/module.h>
34#include <linux/moduleparam.h>
35#include <linux/slab.h>
36#include <linux/bitops.h>
37#include <linux/init.h>
38#include <linux/input.h>
39#include <linux/gameport.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080040#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/timex.h>
42
43#define DRIVER_DESC "Analog joystick and gamepad driver"
44
45MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
46MODULE_DESCRIPTION(DRIVER_DESC);
47MODULE_LICENSE("GPL");
48
49/*
50 * Option parsing.
51 */
52
53#define ANALOG_PORTS 16
54
55static char *js[ANALOG_PORTS];
56static int js_nargs;
57static int analog_options[ANALOG_PORTS];
58module_param_array_named(map, js, charp, &js_nargs, 0);
59MODULE_PARM_DESC(map, "Describes analog joysticks type/capabilities");
60
61__obsolete_setup("js=");
62
63/*
64 * Times, feature definitions.
65 */
66
67#define ANALOG_RUDDER 0x00004
68#define ANALOG_THROTTLE 0x00008
69#define ANALOG_AXES_STD 0x0000f
70#define ANALOG_BTNS_STD 0x000f0
71
72#define ANALOG_BTNS_CHF 0x00100
73#define ANALOG_HAT1_CHF 0x00200
74#define ANALOG_HAT2_CHF 0x00400
75#define ANALOG_HAT_FCS 0x00800
76#define ANALOG_HATS_ALL 0x00e00
77#define ANALOG_BTN_TL 0x01000
78#define ANALOG_BTN_TR 0x02000
79#define ANALOG_BTN_TL2 0x04000
80#define ANALOG_BTN_TR2 0x08000
81#define ANALOG_BTNS_TLR 0x03000
82#define ANALOG_BTNS_TLR2 0x0c000
83#define ANALOG_BTNS_GAMEPAD 0x0f000
84
85#define ANALOG_HBTN_CHF 0x10000
86#define ANALOG_ANY_CHF 0x10700
87#define ANALOG_SAITEK 0x20000
88#define ANALOG_EXTENSIONS 0x7ff00
89#define ANALOG_GAMEPAD 0x80000
90
91#define ANALOG_MAX_TIME 3 /* 3 ms */
92#define ANALOG_LOOP_TIME 2000 /* 2 * loop */
93#define ANALOG_SAITEK_DELAY 200 /* 200 us */
94#define ANALOG_SAITEK_TIME 2000 /* 2000 us */
95#define ANALOG_AXIS_TIME 2 /* 2 * refresh */
96#define ANALOG_INIT_RETRIES 8 /* 8 times */
97#define ANALOG_FUZZ_BITS 2 /* 2 bit more */
98#define ANALOG_FUZZ_MAGIC 36 /* 36 u*ms/loop */
99
100#define ANALOG_MAX_NAME_LENGTH 128
101#define ANALOG_MAX_PHYS_LENGTH 32
102
103static short analog_axes[] = { ABS_X, ABS_Y, ABS_RUDDER, ABS_THROTTLE };
104static short analog_hats[] = { ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y };
105static short analog_pads[] = { BTN_Y, BTN_Z, BTN_TL, BTN_TR };
106static short analog_exts[] = { ANALOG_HAT1_CHF, ANALOG_HAT2_CHF, ANALOG_HAT_FCS };
107static short analog_pad_btn[] = { BTN_A, BTN_B, BTN_C, BTN_X, BTN_TL2, BTN_TR2, BTN_SELECT, BTN_START, BTN_MODE, BTN_BASE };
108static short analog_joy_btn[] = { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2,
109 BTN_BASE3, BTN_BASE4, BTN_BASE5, BTN_BASE6 };
110
111static unsigned char analog_chf[] = { 0xf, 0x0, 0x1, 0x9, 0x2, 0x4, 0xc, 0x8, 0x3, 0x5, 0xb, 0x7, 0xd, 0xe, 0xa, 0x6 };
112
113struct analog {
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500114 struct input_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 int mask;
116 short *buttons;
117 char name[ANALOG_MAX_NAME_LENGTH];
118 char phys[ANALOG_MAX_PHYS_LENGTH];
119};
120
121struct analog_port {
122 struct gameport *gameport;
123 struct analog analog[2];
124 unsigned char mask;
125 char saitek;
126 char cooked;
127 int bads;
128 int reads;
129 int speed;
130 int loop;
131 int fuzz;
132 int axes[4];
133 int buttons;
134 int initial[4];
135 int axtime;
136};
137
138/*
139 * Time macros.
140 */
141
142#ifdef __i386__
Ingo Molnar306e4402005-06-30 02:58:55 -0700143
144#include <asm/i8253.h>
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146#define GET_TIME(x) do { if (cpu_has_tsc) rdtscl(x); else x = get_time_pit(); } while (0)
147#define DELTA(x,y) (cpu_has_tsc ? ((y) - (x)) : ((x) - (y) + ((x) < (y) ? CLOCK_TICK_RATE / HZ : 0)))
148#define TIME_NAME (cpu_has_tsc?"TSC":"PIT")
149static unsigned int get_time_pit(void)
150{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 unsigned long flags;
152 unsigned int count;
153
154 spin_lock_irqsave(&i8253_lock, flags);
155 outb_p(0x00, 0x43);
156 count = inb_p(0x40);
157 count |= inb_p(0x40) << 8;
158 spin_unlock_irqrestore(&i8253_lock, flags);
159
160 return count;
161}
162#elif defined(__x86_64__)
163#define GET_TIME(x) rdtscl(x)
164#define DELTA(x,y) ((y)-(x))
165#define TIME_NAME "TSC"
166#elif defined(__alpha__)
167#define GET_TIME(x) do { x = get_cycles(); } while (0)
168#define DELTA(x,y) ((y)-(x))
169#define TIME_NAME "PCC"
170#else
171#define FAKE_TIME
172static unsigned long analog_faketime = 0;
173#define GET_TIME(x) do { x = analog_faketime++; } while(0)
174#define DELTA(x,y) ((y)-(x))
175#define TIME_NAME "Unreliable"
176#warning Precise timer not defined for this architecture.
177#endif
178
179/*
180 * analog_decode() decodes analog joystick data and reports input events.
181 */
182
183static void analog_decode(struct analog *analog, int *axes, int *initial, int buttons)
184{
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500185 struct input_dev *dev = analog->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 int i, j;
187
188 if (analog->mask & ANALOG_HAT_FCS)
189 for (i = 0; i < 4; i++)
190 if (axes[3] < ((initial[3] * ((i << 1) + 1)) >> 3)) {
191 buttons |= 1 << (i + 14);
192 break;
193 }
194
195 for (i = j = 0; i < 6; i++)
196 if (analog->mask & (0x10 << i))
197 input_report_key(dev, analog->buttons[j++], (buttons >> i) & 1);
198
199 if (analog->mask & ANALOG_HBTN_CHF)
200 for (i = 0; i < 4; i++)
201 input_report_key(dev, analog->buttons[j++], (buttons >> (i + 10)) & 1);
202
203 if (analog->mask & ANALOG_BTN_TL)
204 input_report_key(dev, analog_pads[0], axes[2] < (initial[2] >> 1));
205 if (analog->mask & ANALOG_BTN_TR)
206 input_report_key(dev, analog_pads[1], axes[3] < (initial[3] >> 1));
207 if (analog->mask & ANALOG_BTN_TL2)
208 input_report_key(dev, analog_pads[2], axes[2] > (initial[2] + (initial[2] >> 1)));
209 if (analog->mask & ANALOG_BTN_TR2)
210 input_report_key(dev, analog_pads[3], axes[3] > (initial[3] + (initial[3] >> 1)));
211
212 for (i = j = 0; i < 4; i++)
213 if (analog->mask & (1 << i))
214 input_report_abs(dev, analog_axes[j++], axes[i]);
215
216 for (i = j = 0; i < 3; i++)
217 if (analog->mask & analog_exts[i]) {
218 input_report_abs(dev, analog_hats[j++],
219 ((buttons >> ((i << 2) + 7)) & 1) - ((buttons >> ((i << 2) + 9)) & 1));
220 input_report_abs(dev, analog_hats[j++],
221 ((buttons >> ((i << 2) + 8)) & 1) - ((buttons >> ((i << 2) + 6)) & 1));
222 }
223
224 input_sync(dev);
225}
226
227/*
228 * analog_cooked_read() reads analog joystick data.
229 */
230
231static int analog_cooked_read(struct analog_port *port)
232{
233 struct gameport *gameport = port->gameport;
234 unsigned int time[4], start, loop, now, loopout, timeout;
235 unsigned char data[4], this, last;
236 unsigned long flags;
237 int i, j;
238
239 loopout = (ANALOG_LOOP_TIME * port->loop) / 1000;
240 timeout = ANALOG_MAX_TIME * port->speed;
241
242 local_irq_save(flags);
243 gameport_trigger(gameport);
244 GET_TIME(now);
245 local_irq_restore(flags);
246
247 start = now;
248 this = port->mask;
249 i = 0;
250
251 do {
252 loop = now;
253 last = this;
254
255 local_irq_disable();
256 this = gameport_read(gameport) & port->mask;
257 GET_TIME(now);
258 local_irq_restore(flags);
259
260 if ((last ^ this) && (DELTA(loop, now) < loopout)) {
261 data[i] = last ^ this;
262 time[i] = now;
263 i++;
264 }
265
266 } while (this && (i < 4) && (DELTA(start, now) < timeout));
267
268 this <<= 4;
269
270 for (--i; i >= 0; i--) {
271 this |= data[i];
272 for (j = 0; j < 4; j++)
273 if (data[i] & (1 << j))
274 port->axes[j] = (DELTA(start, time[i]) << ANALOG_FUZZ_BITS) / port->loop;
275 }
276
277 return -(this != port->mask);
278}
279
280static int analog_button_read(struct analog_port *port, char saitek, char chf)
281{
282 unsigned char u;
283 int t = 1, i = 0;
284 int strobe = gameport_time(port->gameport, ANALOG_SAITEK_TIME);
285
286 u = gameport_read(port->gameport);
287
288 if (!chf) {
289 port->buttons = (~u >> 4) & 0xf;
290 return 0;
291 }
292
293 port->buttons = 0;
294
295 while ((~u & 0xf0) && (i < 16) && t) {
296 port->buttons |= 1 << analog_chf[(~u >> 4) & 0xf];
297 if (!saitek) return 0;
298 udelay(ANALOG_SAITEK_DELAY);
299 t = strobe;
300 gameport_trigger(port->gameport);
301 while (((u = gameport_read(port->gameport)) & port->mask) && t) t--;
302 i++;
303 }
304
305 return -(!t || (i == 16));
306}
307
308/*
309 * analog_poll() repeatedly polls the Analog joysticks.
310 */
311
312static void analog_poll(struct gameport *gameport)
313{
314 struct analog_port *port = gameport_get_drvdata(gameport);
315 int i;
316
317 char saitek = !!(port->analog[0].mask & ANALOG_SAITEK);
318 char chf = !!(port->analog[0].mask & ANALOG_ANY_CHF);
319
320 if (port->cooked) {
321 port->bads -= gameport_cooked_read(port->gameport, port->axes, &port->buttons);
322 if (chf)
323 port->buttons = port->buttons ? (1 << analog_chf[port->buttons]) : 0;
324 port->reads++;
325 } else {
326 if (!port->axtime--) {
327 port->bads -= analog_cooked_read(port);
328 port->bads -= analog_button_read(port, saitek, chf);
329 port->reads++;
330 port->axtime = ANALOG_AXIS_TIME - 1;
331 } else {
332 if (!saitek)
333 analog_button_read(port, saitek, chf);
334 }
335 }
336
337 for (i = 0; i < 2; i++)
338 if (port->analog[i].mask)
339 analog_decode(port->analog + i, port->axes, port->initial, port->buttons);
340}
341
342/*
343 * analog_open() is a callback from the input open routine.
344 */
345
346static int analog_open(struct input_dev *dev)
347{
348 struct analog_port *port = dev->private;
349
350 gameport_start_polling(port->gameport);
351 return 0;
352}
353
354/*
355 * analog_close() is a callback from the input close routine.
356 */
357
358static void analog_close(struct input_dev *dev)
359{
360 struct analog_port *port = dev->private;
361
362 gameport_stop_polling(port->gameport);
363}
364
365/*
366 * analog_calibrate_timer() calibrates the timer and computes loop
367 * and timeout values for a joystick port.
368 */
369
370static void analog_calibrate_timer(struct analog_port *port)
371{
372 struct gameport *gameport = port->gameport;
373 unsigned int i, t, tx, t1, t2, t3;
374 unsigned long flags;
375
376 local_irq_save(flags);
377 GET_TIME(t1);
378#ifdef FAKE_TIME
379 analog_faketime += 830;
380#endif
381 mdelay(1);
382 GET_TIME(t2);
383 GET_TIME(t3);
384 local_irq_restore(flags);
385
386 port->speed = DELTA(t1, t2) - DELTA(t2, t3);
387
388 tx = ~0;
389
390 for (i = 0; i < 50; i++) {
391 local_irq_save(flags);
392 GET_TIME(t1);
393 for (t = 0; t < 50; t++) { gameport_read(gameport); GET_TIME(t2); }
394 GET_TIME(t3);
395 local_irq_restore(flags);
396 udelay(i);
397 t = DELTA(t1, t2) - DELTA(t2, t3);
398 if (t < tx) tx = t;
399 }
400
401 port->loop = tx / 50;
402}
403
404/*
405 * analog_name() constructs a name for an analog joystick.
406 */
407
408static void analog_name(struct analog *analog)
409{
Dmitry Torokhov10ca4c02006-06-26 01:45:48 -0400410 snprintf(analog->name, sizeof(analog->name), "Analog %d-axis %d-button",
411 hweight8(analog->mask & ANALOG_AXES_STD),
412 hweight8(analog->mask & ANALOG_BTNS_STD) + !!(analog->mask & ANALOG_BTNS_CHF) * 2 +
413 hweight16(analog->mask & ANALOG_BTNS_GAMEPAD) + !!(analog->mask & ANALOG_HBTN_CHF) * 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 if (analog->mask & ANALOG_HATS_ALL)
Dmitry Torokhov10ca4c02006-06-26 01:45:48 -0400416 snprintf(analog->name, sizeof(analog->name), "%s %d-hat",
417 analog->name, hweight16(analog->mask & ANALOG_HATS_ALL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 if (analog->mask & ANALOG_HAT_FCS)
Dmitry Torokhov10ca4c02006-06-26 01:45:48 -0400420 strlcat(analog->name, " FCS", sizeof(analog->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (analog->mask & ANALOG_ANY_CHF)
Dmitry Torokhov10ca4c02006-06-26 01:45:48 -0400422 strlcat(analog->name, (analog->mask & ANALOG_SAITEK) ? " Saitek" : " CHF",
423 sizeof(analog->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Dmitry Torokhov10ca4c02006-06-26 01:45:48 -0400425 strlcat(analog->name, (analog->mask & ANALOG_GAMEPAD) ? " gamepad": " joystick",
426 sizeof(analog->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
429/*
430 * analog_init_device()
431 */
432
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500433static int analog_init_device(struct analog_port *port, struct analog *analog, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500435 struct input_dev *input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 int i, j, t, v, w, x, y, z;
437
438 analog_name(analog);
Dmitry Torokhov10ca4c02006-06-26 01:45:48 -0400439 snprintf(analog->phys, sizeof(analog->phys),
440 "%s/input%d", port->gameport->phys, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 analog->buttons = (analog->mask & ANALOG_GAMEPAD) ? analog_pad_btn : analog_joy_btn;
442
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500443 analog->dev = input_dev = input_allocate_device();
444 if (!input_dev)
445 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500447 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500454 input_dev->open = analog_open;
455 input_dev->close = analog_close;
456 input_dev->private = port;
457 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 for (i = j = 0; i < 4; i++)
460 if (analog->mask & (1 << i)) {
461
462 t = analog_axes[j];
463 x = port->axes[i];
464 y = (port->axes[0] + port->axes[1]) >> 1;
465 z = y - port->axes[i];
466 z = z > 0 ? z : -z;
467 v = (x >> 3);
468 w = (x >> 3);
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if ((i == 2 || i == 3) && (j == 2 || j == 3) && (z > (y >> 3)))
471 x = y;
472
473 if (analog->mask & ANALOG_SAITEK) {
474 if (i == 2) x = port->axes[i];
475 v = x - (x >> 2);
476 w = (x >> 4);
477 }
478
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500479 input_set_abs_params(input_dev, t, v, (x << 1) - v, port->fuzz, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 j++;
481 }
482
483 for (i = j = 0; i < 3; i++)
484 if (analog->mask & analog_exts[i])
485 for (x = 0; x < 2; x++) {
486 t = analog_hats[j++];
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500487 input_set_abs_params(input_dev, t, -1, 1, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489
490 for (i = j = 0; i < 4; i++)
491 if (analog->mask & (0x10 << i))
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500492 set_bit(analog->buttons[j++], input_dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 if (analog->mask & ANALOG_BTNS_CHF)
495 for (i = 0; i < 2; i++)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500496 set_bit(analog->buttons[j++], input_dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 if (analog->mask & ANALOG_HBTN_CHF)
499 for (i = 0; i < 4; i++)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500500 set_bit(analog->buttons[j++], input_dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 for (i = 0; i < 4; i++)
503 if (analog->mask & (ANALOG_BTN_TL << i))
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500504 set_bit(analog_pads[i], input_dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 analog_decode(analog, port->axes, port->initial, port->buttons);
507
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500508 input_register_device(analog->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500510 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
513/*
514 * analog_init_devices() sets up device-specific values and registers the input devices.
515 */
516
517static int analog_init_masks(struct analog_port *port)
518{
519 int i;
520 struct analog *analog = port->analog;
521 int max[4];
522
523 if (!port->mask)
524 return -1;
525
526 if ((port->mask & 3) != 3 && port->mask != 0xc) {
527 printk(KERN_WARNING "analog.c: Unknown joystick device found "
528 "(data=%#x, %s), probably not analog joystick.\n",
529 port->mask, port->gameport->phys);
530 return -1;
531 }
532
533
534 i = analog_options[0]; /* FIXME !!! - need to specify options for different ports */
535
536 analog[0].mask = i & 0xfffff;
537
538 analog[0].mask &= ~(ANALOG_AXES_STD | ANALOG_HAT_FCS | ANALOG_BTNS_GAMEPAD)
539 | port->mask | ((port->mask << 8) & ANALOG_HAT_FCS)
540 | ((port->mask << 10) & ANALOG_BTNS_TLR) | ((port->mask << 12) & ANALOG_BTNS_TLR2);
541
542 analog[0].mask &= ~(ANALOG_HAT2_CHF)
543 | ((analog[0].mask & ANALOG_HBTN_CHF) ? 0 : ANALOG_HAT2_CHF);
544
545 analog[0].mask &= ~(ANALOG_THROTTLE | ANALOG_BTN_TR | ANALOG_BTN_TR2)
546 | ((~analog[0].mask & ANALOG_HAT_FCS) >> 8)
547 | ((~analog[0].mask & ANALOG_HAT_FCS) << 2)
548 | ((~analog[0].mask & ANALOG_HAT_FCS) << 4);
549
550 analog[0].mask &= ~(ANALOG_THROTTLE | ANALOG_RUDDER)
551 | (((~analog[0].mask & ANALOG_BTNS_TLR ) >> 10)
552 & ((~analog[0].mask & ANALOG_BTNS_TLR2) >> 12));
553
554 analog[1].mask = ((i >> 20) & 0xff) | ((i >> 12) & 0xf0000);
555
556 analog[1].mask &= (analog[0].mask & ANALOG_EXTENSIONS) ? ANALOG_GAMEPAD
557 : (((ANALOG_BTNS_STD | port->mask) & ~analog[0].mask) | ANALOG_GAMEPAD);
558
559 if (port->cooked) {
560
561 for (i = 0; i < 4; i++) max[i] = port->axes[i] << 1;
562
563 if ((analog[0].mask & 0x7) == 0x7) max[2] = (max[0] + max[1]) >> 1;
564 if ((analog[0].mask & 0xb) == 0xb) max[3] = (max[0] + max[1]) >> 1;
565 if ((analog[0].mask & ANALOG_BTN_TL) && !(analog[0].mask & ANALOG_BTN_TL2)) max[2] >>= 1;
566 if ((analog[0].mask & ANALOG_BTN_TR) && !(analog[0].mask & ANALOG_BTN_TR2)) max[3] >>= 1;
567 if ((analog[0].mask & ANALOG_HAT_FCS)) max[3] >>= 1;
568
569 gameport_calibrate(port->gameport, port->axes, max);
570 }
571
572 for (i = 0; i < 4; i++)
573 port->initial[i] = port->axes[i];
574
575 return -!(analog[0].mask || analog[1].mask);
576}
577
578static int analog_init_port(struct gameport *gameport, struct gameport_driver *drv, struct analog_port *port)
579{
580 int i, t, u, v;
581
582 port->gameport = gameport;
583
584 gameport_set_drvdata(gameport, port);
585
586 if (!gameport_open(gameport, drv, GAMEPORT_MODE_RAW)) {
587
588 analog_calibrate_timer(port);
589
590 gameport_trigger(gameport);
591 t = gameport_read(gameport);
592 msleep(ANALOG_MAX_TIME);
593 port->mask = (gameport_read(gameport) ^ t) & t & 0xf;
594 port->fuzz = (port->speed * ANALOG_FUZZ_MAGIC) / port->loop / 1000 + ANALOG_FUZZ_BITS;
595
596 for (i = 0; i < ANALOG_INIT_RETRIES; i++) {
597 if (!analog_cooked_read(port))
598 break;
599 msleep(ANALOG_MAX_TIME);
600 }
601
602 u = v = 0;
603
604 msleep(ANALOG_MAX_TIME);
605 t = gameport_time(gameport, ANALOG_MAX_TIME * 1000);
606 gameport_trigger(gameport);
607 while ((gameport_read(port->gameport) & port->mask) && (u < t))
608 u++;
609 udelay(ANALOG_SAITEK_DELAY);
610 t = gameport_time(gameport, ANALOG_SAITEK_TIME);
611 gameport_trigger(gameport);
612 while ((gameport_read(port->gameport) & port->mask) && (v < t))
613 v++;
614
615 if (v < (u >> 1)) { /* FIXME - more than one port */
616 analog_options[0] |= /* FIXME - more than one port */
617 ANALOG_SAITEK | ANALOG_BTNS_CHF | ANALOG_HBTN_CHF | ANALOG_HAT1_CHF;
618 return 0;
619 }
620
621 gameport_close(gameport);
622 }
623
624 if (!gameport_open(gameport, drv, GAMEPORT_MODE_COOKED)) {
625
626 for (i = 0; i < ANALOG_INIT_RETRIES; i++)
627 if (!gameport_cooked_read(gameport, port->axes, &port->buttons))
628 break;
629 for (i = 0; i < 4; i++)
630 if (port->axes[i] != -1)
631 port->mask |= 1 << i;
632
633 port->fuzz = gameport->fuzz;
634 port->cooked = 1;
635 return 0;
636 }
637
638 return gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
639}
640
641static int analog_connect(struct gameport *gameport, struct gameport_driver *drv)
642{
643 struct analog_port *port;
644 int i;
645 int err;
646
Pekka Enberga97e1482005-09-06 15:18:33 -0700647 if (!(port = kzalloc(sizeof(struct analog_port), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return - ENOMEM;
649
650 err = analog_init_port(gameport, drv, port);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500651 if (err)
652 goto fail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 err = analog_init_masks(port);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500655 if (err)
656 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 gameport_set_poll_handler(gameport, analog_poll);
659 gameport_set_poll_interval(gameport, 10);
660
661 for (i = 0; i < 2; i++)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500662 if (port->analog[i].mask) {
663 err = analog_init_device(port, port->analog + i, i);
664 if (err)
665 goto fail3;
666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 return 0;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500669
670 fail3: while (--i >= 0)
671 input_unregister_device(port->analog[i].dev);
672 fail2: gameport_close(gameport);
673 fail1: gameport_set_drvdata(gameport, NULL);
674 kfree(port);
675 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
678static void analog_disconnect(struct gameport *gameport)
679{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 struct analog_port *port = gameport_get_drvdata(gameport);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500681 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 for (i = 0; i < 2; i++)
684 if (port->analog[i].mask)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500685 input_unregister_device(port->analog[i].dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 gameport_close(gameport);
687 gameport_set_drvdata(gameport, NULL);
688 printk(KERN_INFO "analog.c: %d out of %d reads (%d%%) on %s failed\n",
689 port->bads, port->reads, port->reads ? (port->bads * 100 / port->reads) : 0,
690 port->gameport->phys);
691 kfree(port);
692}
693
694struct analog_types {
695 char *name;
696 int value;
697};
698
699static struct analog_types analog_types[] = {
700 { "none", 0x00000000 },
701 { "auto", 0x000000ff },
702 { "2btn", 0x0000003f },
703 { "y-joy", 0x0cc00033 },
704 { "y-pad", 0x8cc80033 },
705 { "fcs", 0x000008f7 },
706 { "chf", 0x000002ff },
707 { "fullchf", 0x000007ff },
708 { "gamepad", 0x000830f3 },
709 { "gamepad8", 0x0008f0f3 },
710 { NULL, 0 }
711};
712
713static void analog_parse_options(void)
714{
715 int i, j;
716 char *end;
717
718 for (i = 0; i < js_nargs; i++) {
719
720 for (j = 0; analog_types[j].name; j++)
721 if (!strcmp(analog_types[j].name, js[i])) {
722 analog_options[i] = analog_types[j].value;
723 break;
724 }
725 if (analog_types[j].name) continue;
726
727 analog_options[i] = simple_strtoul(js[i], &end, 0);
728 if (end != js[i]) continue;
729
730 analog_options[i] = 0xff;
731 if (!strlen(js[i])) continue;
732
733 printk(KERN_WARNING "analog.c: Bad config for port %d - \"%s\"\n", i, js[i]);
734 }
735
736 for (; i < ANALOG_PORTS; i++)
737 analog_options[i] = 0xff;
738}
739
740/*
741 * The gameport device structure.
742 */
743
744static struct gameport_driver analog_drv = {
745 .driver = {
746 .name = "analog",
747 },
748 .description = DRIVER_DESC,
749 .connect = analog_connect,
750 .disconnect = analog_disconnect,
751};
752
753static int __init analog_init(void)
754{
755 analog_parse_options();
756 gameport_register_driver(&analog_drv);
757
758 return 0;
759}
760
761static void __exit analog_exit(void)
762{
763 gameport_unregister_driver(&analog_drv);
764}
765
766module_init(analog_init);
767module_exit(analog_exit);