blob: f32e031dcb27f53a04f1c774b283ce2381dadd4d [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/slab.h>
35#include <linux/bitops.h>
36#include <linux/init.h>
37#include <linux/input.h>
38#include <linux/gameport.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080039#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/timex.h>
41
42#define DRIVER_DESC "Analog joystick and gamepad driver"
43
44MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
45MODULE_DESCRIPTION(DRIVER_DESC);
46MODULE_LICENSE("GPL");
47
48/*
49 * Option parsing.
50 */
51
52#define ANALOG_PORTS 16
53
54static char *js[ANALOG_PORTS];
Dmitry Torokhov78167232007-05-03 00:52:51 -040055static unsigned int js_nargs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static int analog_options[ANALOG_PORTS];
57module_param_array_named(map, js, charp, &js_nargs, 0);
58MODULE_PARM_DESC(map, "Describes analog joysticks type/capabilities");
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/*
61 * Times, feature definitions.
62 */
63
64#define ANALOG_RUDDER 0x00004
65#define ANALOG_THROTTLE 0x00008
66#define ANALOG_AXES_STD 0x0000f
67#define ANALOG_BTNS_STD 0x000f0
68
69#define ANALOG_BTNS_CHF 0x00100
70#define ANALOG_HAT1_CHF 0x00200
71#define ANALOG_HAT2_CHF 0x00400
72#define ANALOG_HAT_FCS 0x00800
73#define ANALOG_HATS_ALL 0x00e00
74#define ANALOG_BTN_TL 0x01000
75#define ANALOG_BTN_TR 0x02000
76#define ANALOG_BTN_TL2 0x04000
77#define ANALOG_BTN_TR2 0x08000
78#define ANALOG_BTNS_TLR 0x03000
79#define ANALOG_BTNS_TLR2 0x0c000
80#define ANALOG_BTNS_GAMEPAD 0x0f000
81
82#define ANALOG_HBTN_CHF 0x10000
83#define ANALOG_ANY_CHF 0x10700
84#define ANALOG_SAITEK 0x20000
85#define ANALOG_EXTENSIONS 0x7ff00
86#define ANALOG_GAMEPAD 0x80000
87
88#define ANALOG_MAX_TIME 3 /* 3 ms */
89#define ANALOG_LOOP_TIME 2000 /* 2 * loop */
90#define ANALOG_SAITEK_DELAY 200 /* 200 us */
91#define ANALOG_SAITEK_TIME 2000 /* 2000 us */
92#define ANALOG_AXIS_TIME 2 /* 2 * refresh */
93#define ANALOG_INIT_RETRIES 8 /* 8 times */
94#define ANALOG_FUZZ_BITS 2 /* 2 bit more */
95#define ANALOG_FUZZ_MAGIC 36 /* 36 u*ms/loop */
96
97#define ANALOG_MAX_NAME_LENGTH 128
98#define ANALOG_MAX_PHYS_LENGTH 32
99
100static short analog_axes[] = { ABS_X, ABS_Y, ABS_RUDDER, ABS_THROTTLE };
101static short analog_hats[] = { ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y };
102static short analog_pads[] = { BTN_Y, BTN_Z, BTN_TL, BTN_TR };
103static short analog_exts[] = { ANALOG_HAT1_CHF, ANALOG_HAT2_CHF, ANALOG_HAT_FCS };
104static short analog_pad_btn[] = { BTN_A, BTN_B, BTN_C, BTN_X, BTN_TL2, BTN_TR2, BTN_SELECT, BTN_START, BTN_MODE, BTN_BASE };
105static short analog_joy_btn[] = { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2,
106 BTN_BASE3, BTN_BASE4, BTN_BASE5, BTN_BASE6 };
107
108static unsigned char analog_chf[] = { 0xf, 0x0, 0x1, 0x9, 0x2, 0x4, 0xc, 0x8, 0x3, 0x5, 0xb, 0x7, 0xd, 0xe, 0xa, 0x6 };
109
110struct analog {
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500111 struct input_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 int mask;
113 short *buttons;
114 char name[ANALOG_MAX_NAME_LENGTH];
115 char phys[ANALOG_MAX_PHYS_LENGTH];
116};
117
118struct analog_port {
119 struct gameport *gameport;
120 struct analog analog[2];
121 unsigned char mask;
122 char saitek;
123 char cooked;
124 int bads;
125 int reads;
126 int speed;
127 int loop;
128 int fuzz;
129 int axes[4];
130 int buttons;
131 int initial[4];
132 int axtime;
133};
134
135/*
136 * Time macros.
137 */
138
139#ifdef __i386__
Ingo Molnar306e4402005-06-30 02:58:55 -0700140
141#include <asm/i8253.h>
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143#define GET_TIME(x) do { if (cpu_has_tsc) rdtscl(x); else x = get_time_pit(); } while (0)
144#define DELTA(x,y) (cpu_has_tsc ? ((y) - (x)) : ((x) - (y) + ((x) < (y) ? CLOCK_TICK_RATE / HZ : 0)))
145#define TIME_NAME (cpu_has_tsc?"TSC":"PIT")
146static unsigned int get_time_pit(void)
147{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 unsigned long flags;
149 unsigned int count;
150
151 spin_lock_irqsave(&i8253_lock, flags);
152 outb_p(0x00, 0x43);
153 count = inb_p(0x40);
154 count |= inb_p(0x40) << 8;
155 spin_unlock_irqrestore(&i8253_lock, flags);
156
157 return count;
158}
159#elif defined(__x86_64__)
160#define GET_TIME(x) rdtscl(x)
161#define DELTA(x,y) ((y)-(x))
162#define TIME_NAME "TSC"
163#elif defined(__alpha__)
164#define GET_TIME(x) do { x = get_cycles(); } while (0)
165#define DELTA(x,y) ((y)-(x))
166#define TIME_NAME "PCC"
167#else
168#define FAKE_TIME
169static unsigned long analog_faketime = 0;
170#define GET_TIME(x) do { x = analog_faketime++; } while(0)
171#define DELTA(x,y) ((y)-(x))
172#define TIME_NAME "Unreliable"
173#warning Precise timer not defined for this architecture.
174#endif
175
176/*
177 * analog_decode() decodes analog joystick data and reports input events.
178 */
179
180static void analog_decode(struct analog *analog, int *axes, int *initial, int buttons)
181{
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500182 struct input_dev *dev = analog->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 int i, j;
184
185 if (analog->mask & ANALOG_HAT_FCS)
186 for (i = 0; i < 4; i++)
187 if (axes[3] < ((initial[3] * ((i << 1) + 1)) >> 3)) {
188 buttons |= 1 << (i + 14);
189 break;
190 }
191
192 for (i = j = 0; i < 6; i++)
193 if (analog->mask & (0x10 << i))
194 input_report_key(dev, analog->buttons[j++], (buttons >> i) & 1);
195
196 if (analog->mask & ANALOG_HBTN_CHF)
197 for (i = 0; i < 4; i++)
198 input_report_key(dev, analog->buttons[j++], (buttons >> (i + 10)) & 1);
199
200 if (analog->mask & ANALOG_BTN_TL)
201 input_report_key(dev, analog_pads[0], axes[2] < (initial[2] >> 1));
202 if (analog->mask & ANALOG_BTN_TR)
203 input_report_key(dev, analog_pads[1], axes[3] < (initial[3] >> 1));
204 if (analog->mask & ANALOG_BTN_TL2)
205 input_report_key(dev, analog_pads[2], axes[2] > (initial[2] + (initial[2] >> 1)));
206 if (analog->mask & ANALOG_BTN_TR2)
207 input_report_key(dev, analog_pads[3], axes[3] > (initial[3] + (initial[3] >> 1)));
208
209 for (i = j = 0; i < 4; i++)
210 if (analog->mask & (1 << i))
211 input_report_abs(dev, analog_axes[j++], axes[i]);
212
213 for (i = j = 0; i < 3; i++)
214 if (analog->mask & analog_exts[i]) {
215 input_report_abs(dev, analog_hats[j++],
216 ((buttons >> ((i << 2) + 7)) & 1) - ((buttons >> ((i << 2) + 9)) & 1));
217 input_report_abs(dev, analog_hats[j++],
218 ((buttons >> ((i << 2) + 8)) & 1) - ((buttons >> ((i << 2) + 6)) & 1));
219 }
220
221 input_sync(dev);
222}
223
224/*
225 * analog_cooked_read() reads analog joystick data.
226 */
227
228static int analog_cooked_read(struct analog_port *port)
229{
230 struct gameport *gameport = port->gameport;
231 unsigned int time[4], start, loop, now, loopout, timeout;
232 unsigned char data[4], this, last;
233 unsigned long flags;
234 int i, j;
235
236 loopout = (ANALOG_LOOP_TIME * port->loop) / 1000;
237 timeout = ANALOG_MAX_TIME * port->speed;
238
239 local_irq_save(flags);
240 gameport_trigger(gameport);
241 GET_TIME(now);
242 local_irq_restore(flags);
243
244 start = now;
245 this = port->mask;
246 i = 0;
247
248 do {
249 loop = now;
250 last = this;
251
252 local_irq_disable();
253 this = gameport_read(gameport) & port->mask;
254 GET_TIME(now);
255 local_irq_restore(flags);
256
257 if ((last ^ this) && (DELTA(loop, now) < loopout)) {
258 data[i] = last ^ this;
259 time[i] = now;
260 i++;
261 }
262
263 } while (this && (i < 4) && (DELTA(start, now) < timeout));
264
265 this <<= 4;
266
267 for (--i; i >= 0; i--) {
268 this |= data[i];
269 for (j = 0; j < 4; j++)
270 if (data[i] & (1 << j))
271 port->axes[j] = (DELTA(start, time[i]) << ANALOG_FUZZ_BITS) / port->loop;
272 }
273
274 return -(this != port->mask);
275}
276
277static int analog_button_read(struct analog_port *port, char saitek, char chf)
278{
279 unsigned char u;
280 int t = 1, i = 0;
281 int strobe = gameport_time(port->gameport, ANALOG_SAITEK_TIME);
282
283 u = gameport_read(port->gameport);
284
285 if (!chf) {
286 port->buttons = (~u >> 4) & 0xf;
287 return 0;
288 }
289
290 port->buttons = 0;
291
292 while ((~u & 0xf0) && (i < 16) && t) {
293 port->buttons |= 1 << analog_chf[(~u >> 4) & 0xf];
294 if (!saitek) return 0;
295 udelay(ANALOG_SAITEK_DELAY);
296 t = strobe;
297 gameport_trigger(port->gameport);
298 while (((u = gameport_read(port->gameport)) & port->mask) && t) t--;
299 i++;
300 }
301
302 return -(!t || (i == 16));
303}
304
305/*
306 * analog_poll() repeatedly polls the Analog joysticks.
307 */
308
309static void analog_poll(struct gameport *gameport)
310{
311 struct analog_port *port = gameport_get_drvdata(gameport);
312 int i;
313
314 char saitek = !!(port->analog[0].mask & ANALOG_SAITEK);
315 char chf = !!(port->analog[0].mask & ANALOG_ANY_CHF);
316
317 if (port->cooked) {
318 port->bads -= gameport_cooked_read(port->gameport, port->axes, &port->buttons);
319 if (chf)
320 port->buttons = port->buttons ? (1 << analog_chf[port->buttons]) : 0;
321 port->reads++;
322 } else {
323 if (!port->axtime--) {
324 port->bads -= analog_cooked_read(port);
325 port->bads -= analog_button_read(port, saitek, chf);
326 port->reads++;
327 port->axtime = ANALOG_AXIS_TIME - 1;
328 } else {
329 if (!saitek)
330 analog_button_read(port, saitek, chf);
331 }
332 }
333
334 for (i = 0; i < 2; i++)
335 if (port->analog[i].mask)
336 analog_decode(port->analog + i, port->axes, port->initial, port->buttons);
337}
338
339/*
340 * analog_open() is a callback from the input open routine.
341 */
342
343static int analog_open(struct input_dev *dev)
344{
Dmitry Torokhov8715c1c2007-04-12 01:34:14 -0400345 struct analog_port *port = input_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 gameport_start_polling(port->gameport);
348 return 0;
349}
350
351/*
352 * analog_close() is a callback from the input close routine.
353 */
354
355static void analog_close(struct input_dev *dev)
356{
Dmitry Torokhov8715c1c2007-04-12 01:34:14 -0400357 struct analog_port *port = input_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 gameport_stop_polling(port->gameport);
360}
361
362/*
363 * analog_calibrate_timer() calibrates the timer and computes loop
364 * and timeout values for a joystick port.
365 */
366
367static void analog_calibrate_timer(struct analog_port *port)
368{
369 struct gameport *gameport = port->gameport;
370 unsigned int i, t, tx, t1, t2, t3;
371 unsigned long flags;
372
373 local_irq_save(flags);
374 GET_TIME(t1);
375#ifdef FAKE_TIME
376 analog_faketime += 830;
377#endif
378 mdelay(1);
379 GET_TIME(t2);
380 GET_TIME(t3);
381 local_irq_restore(flags);
382
383 port->speed = DELTA(t1, t2) - DELTA(t2, t3);
384
385 tx = ~0;
386
387 for (i = 0; i < 50; i++) {
388 local_irq_save(flags);
389 GET_TIME(t1);
390 for (t = 0; t < 50; t++) { gameport_read(gameport); GET_TIME(t2); }
391 GET_TIME(t3);
392 local_irq_restore(flags);
393 udelay(i);
394 t = DELTA(t1, t2) - DELTA(t2, t3);
395 if (t < tx) tx = t;
396 }
397
398 port->loop = tx / 50;
399}
400
401/*
402 * analog_name() constructs a name for an analog joystick.
403 */
404
405static void analog_name(struct analog *analog)
406{
Dmitry Torokhov10ca4c02006-06-26 01:45:48 -0400407 snprintf(analog->name, sizeof(analog->name), "Analog %d-axis %d-button",
408 hweight8(analog->mask & ANALOG_AXES_STD),
409 hweight8(analog->mask & ANALOG_BTNS_STD) + !!(analog->mask & ANALOG_BTNS_CHF) * 2 +
410 hweight16(analog->mask & ANALOG_BTNS_GAMEPAD) + !!(analog->mask & ANALOG_HBTN_CHF) * 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 if (analog->mask & ANALOG_HATS_ALL)
Dmitry Torokhov10ca4c02006-06-26 01:45:48 -0400413 snprintf(analog->name, sizeof(analog->name), "%s %d-hat",
414 analog->name, hweight16(analog->mask & ANALOG_HATS_ALL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 if (analog->mask & ANALOG_HAT_FCS)
Dmitry Torokhov10ca4c02006-06-26 01:45:48 -0400417 strlcat(analog->name, " FCS", sizeof(analog->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (analog->mask & ANALOG_ANY_CHF)
Dmitry Torokhov10ca4c02006-06-26 01:45:48 -0400419 strlcat(analog->name, (analog->mask & ANALOG_SAITEK) ? " Saitek" : " CHF",
420 sizeof(analog->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Dmitry Torokhov10ca4c02006-06-26 01:45:48 -0400422 strlcat(analog->name, (analog->mask & ANALOG_GAMEPAD) ? " gamepad": " joystick",
423 sizeof(analog->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
426/*
427 * analog_init_device()
428 */
429
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500430static int analog_init_device(struct analog_port *port, struct analog *analog, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500432 struct input_dev *input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 int i, j, t, v, w, x, y, z;
Dmitry Torokhov127278c2006-11-05 22:40:09 -0500434 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 analog_name(analog);
Dmitry Torokhov10ca4c02006-06-26 01:45:48 -0400437 snprintf(analog->phys, sizeof(analog->phys),
438 "%s/input%d", port->gameport->phys, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 analog->buttons = (analog->mask & ANALOG_GAMEPAD) ? analog_pad_btn : analog_joy_btn;
440
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500441 analog->dev = input_dev = input_allocate_device();
442 if (!input_dev)
443 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500445 input_dev->name = analog->name;
446 input_dev->phys = analog->phys;
447 input_dev->id.bustype = BUS_GAMEPORT;
448 input_dev->id.vendor = GAMEPORT_ID_VENDOR_ANALOG;
449 input_dev->id.product = analog->mask >> 4;
450 input_dev->id.version = 0x0100;
Dmitry Torokhov935e6582007-04-12 01:35:26 -0400451 input_dev->dev.parent = &port->gameport->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Dmitry Torokhov8715c1c2007-04-12 01:34:14 -0400453 input_set_drvdata(input_dev, port);
454
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500455 input_dev->open = analog_open;
456 input_dev->close = analog_close;
Dmitry Torokhov8715c1c2007-04-12 01:34:14 -0400457
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700458 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 for (i = j = 0; i < 4; i++)
461 if (analog->mask & (1 << i)) {
462
463 t = analog_axes[j];
464 x = port->axes[i];
465 y = (port->axes[0] + port->axes[1]) >> 1;
466 z = y - port->axes[i];
467 z = z > 0 ? z : -z;
468 v = (x >> 3);
469 w = (x >> 3);
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if ((i == 2 || i == 3) && (j == 2 || j == 3) && (z > (y >> 3)))
472 x = y;
473
474 if (analog->mask & ANALOG_SAITEK) {
475 if (i == 2) x = port->axes[i];
476 v = x - (x >> 2);
477 w = (x >> 4);
478 }
479
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500480 input_set_abs_params(input_dev, t, v, (x << 1) - v, port->fuzz, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 j++;
482 }
483
484 for (i = j = 0; i < 3; i++)
485 if (analog->mask & analog_exts[i])
486 for (x = 0; x < 2; x++) {
487 t = analog_hats[j++];
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500488 input_set_abs_params(input_dev, t, -1, 1, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
490
491 for (i = j = 0; i < 4; i++)
492 if (analog->mask & (0x10 << i))
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500493 set_bit(analog->buttons[j++], input_dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 if (analog->mask & ANALOG_BTNS_CHF)
496 for (i = 0; i < 2; i++)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500497 set_bit(analog->buttons[j++], input_dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 if (analog->mask & ANALOG_HBTN_CHF)
500 for (i = 0; i < 4; i++)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500501 set_bit(analog->buttons[j++], input_dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 for (i = 0; i < 4; i++)
504 if (analog->mask & (ANALOG_BTN_TL << i))
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500505 set_bit(analog_pads[i], input_dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 analog_decode(analog, port->axes, port->initial, port->buttons);
508
Dmitry Torokhov127278c2006-11-05 22:40:09 -0500509 error = input_register_device(analog->dev);
510 if (error) {
511 input_free_device(analog->dev);
512 return error;
513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500515 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517
518/*
519 * analog_init_devices() sets up device-specific values and registers the input devices.
520 */
521
522static int analog_init_masks(struct analog_port *port)
523{
524 int i;
525 struct analog *analog = port->analog;
526 int max[4];
527
528 if (!port->mask)
529 return -1;
530
531 if ((port->mask & 3) != 3 && port->mask != 0xc) {
532 printk(KERN_WARNING "analog.c: Unknown joystick device found "
533 "(data=%#x, %s), probably not analog joystick.\n",
534 port->mask, port->gameport->phys);
535 return -1;
536 }
537
538
539 i = analog_options[0]; /* FIXME !!! - need to specify options for different ports */
540
541 analog[0].mask = i & 0xfffff;
542
543 analog[0].mask &= ~(ANALOG_AXES_STD | ANALOG_HAT_FCS | ANALOG_BTNS_GAMEPAD)
544 | port->mask | ((port->mask << 8) & ANALOG_HAT_FCS)
545 | ((port->mask << 10) & ANALOG_BTNS_TLR) | ((port->mask << 12) & ANALOG_BTNS_TLR2);
546
547 analog[0].mask &= ~(ANALOG_HAT2_CHF)
548 | ((analog[0].mask & ANALOG_HBTN_CHF) ? 0 : ANALOG_HAT2_CHF);
549
550 analog[0].mask &= ~(ANALOG_THROTTLE | ANALOG_BTN_TR | ANALOG_BTN_TR2)
551 | ((~analog[0].mask & ANALOG_HAT_FCS) >> 8)
552 | ((~analog[0].mask & ANALOG_HAT_FCS) << 2)
553 | ((~analog[0].mask & ANALOG_HAT_FCS) << 4);
554
555 analog[0].mask &= ~(ANALOG_THROTTLE | ANALOG_RUDDER)
556 | (((~analog[0].mask & ANALOG_BTNS_TLR ) >> 10)
557 & ((~analog[0].mask & ANALOG_BTNS_TLR2) >> 12));
558
559 analog[1].mask = ((i >> 20) & 0xff) | ((i >> 12) & 0xf0000);
560
561 analog[1].mask &= (analog[0].mask & ANALOG_EXTENSIONS) ? ANALOG_GAMEPAD
562 : (((ANALOG_BTNS_STD | port->mask) & ~analog[0].mask) | ANALOG_GAMEPAD);
563
564 if (port->cooked) {
565
566 for (i = 0; i < 4; i++) max[i] = port->axes[i] << 1;
567
568 if ((analog[0].mask & 0x7) == 0x7) max[2] = (max[0] + max[1]) >> 1;
569 if ((analog[0].mask & 0xb) == 0xb) max[3] = (max[0] + max[1]) >> 1;
570 if ((analog[0].mask & ANALOG_BTN_TL) && !(analog[0].mask & ANALOG_BTN_TL2)) max[2] >>= 1;
571 if ((analog[0].mask & ANALOG_BTN_TR) && !(analog[0].mask & ANALOG_BTN_TR2)) max[3] >>= 1;
572 if ((analog[0].mask & ANALOG_HAT_FCS)) max[3] >>= 1;
573
574 gameport_calibrate(port->gameport, port->axes, max);
575 }
576
577 for (i = 0; i < 4; i++)
578 port->initial[i] = port->axes[i];
579
580 return -!(analog[0].mask || analog[1].mask);
581}
582
583static int analog_init_port(struct gameport *gameport, struct gameport_driver *drv, struct analog_port *port)
584{
585 int i, t, u, v;
586
587 port->gameport = gameport;
588
589 gameport_set_drvdata(gameport, port);
590
591 if (!gameport_open(gameport, drv, GAMEPORT_MODE_RAW)) {
592
593 analog_calibrate_timer(port);
594
595 gameport_trigger(gameport);
596 t = gameport_read(gameport);
597 msleep(ANALOG_MAX_TIME);
598 port->mask = (gameport_read(gameport) ^ t) & t & 0xf;
599 port->fuzz = (port->speed * ANALOG_FUZZ_MAGIC) / port->loop / 1000 + ANALOG_FUZZ_BITS;
600
601 for (i = 0; i < ANALOG_INIT_RETRIES; i++) {
602 if (!analog_cooked_read(port))
603 break;
604 msleep(ANALOG_MAX_TIME);
605 }
606
607 u = v = 0;
608
609 msleep(ANALOG_MAX_TIME);
610 t = gameport_time(gameport, ANALOG_MAX_TIME * 1000);
611 gameport_trigger(gameport);
612 while ((gameport_read(port->gameport) & port->mask) && (u < t))
613 u++;
614 udelay(ANALOG_SAITEK_DELAY);
615 t = gameport_time(gameport, ANALOG_SAITEK_TIME);
616 gameport_trigger(gameport);
617 while ((gameport_read(port->gameport) & port->mask) && (v < t))
618 v++;
619
620 if (v < (u >> 1)) { /* FIXME - more than one port */
621 analog_options[0] |= /* FIXME - more than one port */
622 ANALOG_SAITEK | ANALOG_BTNS_CHF | ANALOG_HBTN_CHF | ANALOG_HAT1_CHF;
623 return 0;
624 }
625
626 gameport_close(gameport);
627 }
628
629 if (!gameport_open(gameport, drv, GAMEPORT_MODE_COOKED)) {
630
631 for (i = 0; i < ANALOG_INIT_RETRIES; i++)
632 if (!gameport_cooked_read(gameport, port->axes, &port->buttons))
633 break;
634 for (i = 0; i < 4; i++)
635 if (port->axes[i] != -1)
636 port->mask |= 1 << i;
637
638 port->fuzz = gameport->fuzz;
639 port->cooked = 1;
640 return 0;
641 }
642
643 return gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
644}
645
646static int analog_connect(struct gameport *gameport, struct gameport_driver *drv)
647{
648 struct analog_port *port;
649 int i;
650 int err;
651
Pekka Enberga97e1482005-09-06 15:18:33 -0700652 if (!(port = kzalloc(sizeof(struct analog_port), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return - ENOMEM;
654
655 err = analog_init_port(gameport, drv, port);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500656 if (err)
657 goto fail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 err = analog_init_masks(port);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500660 if (err)
661 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 gameport_set_poll_handler(gameport, analog_poll);
664 gameport_set_poll_interval(gameport, 10);
665
666 for (i = 0; i < 2; i++)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500667 if (port->analog[i].mask) {
668 err = analog_init_device(port, port->analog + i, i);
669 if (err)
670 goto fail3;
671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 return 0;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500674
675 fail3: while (--i >= 0)
Dmitry Torokhov127278c2006-11-05 22:40:09 -0500676 if (port->analog[i].mask)
677 input_unregister_device(port->analog[i].dev);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500678 fail2: gameport_close(gameport);
679 fail1: gameport_set_drvdata(gameport, NULL);
680 kfree(port);
681 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
684static void analog_disconnect(struct gameport *gameport)
685{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 struct analog_port *port = gameport_get_drvdata(gameport);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500687 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 for (i = 0; i < 2; i++)
690 if (port->analog[i].mask)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500691 input_unregister_device(port->analog[i].dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 gameport_close(gameport);
693 gameport_set_drvdata(gameport, NULL);
694 printk(KERN_INFO "analog.c: %d out of %d reads (%d%%) on %s failed\n",
695 port->bads, port->reads, port->reads ? (port->bads * 100 / port->reads) : 0,
696 port->gameport->phys);
697 kfree(port);
698}
699
700struct analog_types {
701 char *name;
702 int value;
703};
704
705static struct analog_types analog_types[] = {
706 { "none", 0x00000000 },
707 { "auto", 0x000000ff },
708 { "2btn", 0x0000003f },
709 { "y-joy", 0x0cc00033 },
710 { "y-pad", 0x8cc80033 },
711 { "fcs", 0x000008f7 },
712 { "chf", 0x000002ff },
713 { "fullchf", 0x000007ff },
714 { "gamepad", 0x000830f3 },
715 { "gamepad8", 0x0008f0f3 },
716 { NULL, 0 }
717};
718
719static void analog_parse_options(void)
720{
721 int i, j;
722 char *end;
723
724 for (i = 0; i < js_nargs; i++) {
725
726 for (j = 0; analog_types[j].name; j++)
727 if (!strcmp(analog_types[j].name, js[i])) {
728 analog_options[i] = analog_types[j].value;
729 break;
730 }
731 if (analog_types[j].name) continue;
732
733 analog_options[i] = simple_strtoul(js[i], &end, 0);
734 if (end != js[i]) continue;
735
736 analog_options[i] = 0xff;
737 if (!strlen(js[i])) continue;
738
739 printk(KERN_WARNING "analog.c: Bad config for port %d - \"%s\"\n", i, js[i]);
740 }
741
742 for (; i < ANALOG_PORTS; i++)
743 analog_options[i] = 0xff;
744}
745
746/*
747 * The gameport device structure.
748 */
749
750static struct gameport_driver analog_drv = {
751 .driver = {
752 .name = "analog",
753 },
754 .description = DRIVER_DESC,
755 .connect = analog_connect,
756 .disconnect = analog_disconnect,
757};
758
759static int __init analog_init(void)
760{
761 analog_parse_options();
762 gameport_register_driver(&analog_drv);
763
764 return 0;
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);