blob: 3d8ff09eba57696677b242d29e33fb1bac3f592d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/delay.h>
30#include <linux/kernel.h>
31#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/slab.h>
33#include <linux/bitops.h>
34#include <linux/init.h>
35#include <linux/input.h>
36#include <linux/gameport.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080037#include <linux/jiffies.h>
Arnd Bergmann08604bd2009-06-16 15:31:12 -070038#include <linux/timex.h>
Takashi Iwai76460a72014-09-11 10:28:30 -070039#include <linux/timekeeping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#define DRIVER_DESC "Analog joystick and gamepad driver"
42
43MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
44MODULE_DESCRIPTION(DRIVER_DESC);
45MODULE_LICENSE("GPL");
46
Takashi Iwai76460a72014-09-11 10:28:30 -070047static bool use_ktime = true;
48module_param(use_ktime, bool, 0400);
49MODULE_PARM_DESC(use_ktime, "Use ktime for measuring I/O speed");
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * Option parsing.
53 */
54
55#define ANALOG_PORTS 16
56
57static char *js[ANALOG_PORTS];
Dmitry Torokhov78167232007-05-03 00:52:51 -040058static unsigned int js_nargs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static int analog_options[ANALOG_PORTS];
60module_param_array_named(map, js, charp, &js_nargs, 0);
61MODULE_PARM_DESC(map, "Describes analog joysticks type/capabilities");
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/*
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
Ralf Baechle334955e2011-06-01 19:04:57 +0100144#include <linux/i8253.h>
Ingo Molnar306e4402005-06-30 02:58:55 -0700145
Borislav Petkov59e21e32016-04-04 22:24:59 +0200146#define GET_TIME(x) do { if (boot_cpu_has(X86_FEATURE_TSC)) x = (unsigned int)rdtsc(); else x = get_time_pit(); } while (0)
147#define DELTA(x,y) (boot_cpu_has(X86_FEATURE_TSC) ? ((y) - (x)) : ((x) - (y) + ((x) < (y) ? PIT_TICK_RATE / HZ : 0)))
148#define TIME_NAME (boot_cpu_has(X86_FEATURE_TSC)?"TSC":"PIT")
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149static unsigned int get_time_pit(void)
150{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 unsigned long flags;
152 unsigned int count;
153
Thomas Gleixnerced918e2010-02-17 16:47:10 +0000154 raw_spin_lock_irqsave(&i8253_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 outb_p(0x00, 0x43);
156 count = inb_p(0x40);
157 count |= inb_p(0x40) << 8;
Thomas Gleixnerced918e2010-02-17 16:47:10 +0000158 raw_spin_unlock_irqrestore(&i8253_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 return count;
161}
162#elif defined(__x86_64__)
Andy Lutomirski4ea16362015-06-25 18:44:07 +0200163#define GET_TIME(x) do { x = (unsigned int)rdtsc(); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164#define DELTA(x,y) ((y)-(x))
165#define TIME_NAME "TSC"
Mark Browna6b48692014-08-11 11:08:56 -0700166#elif defined(__alpha__) || defined(CONFIG_MN10300) || defined(CONFIG_ARM) || defined(CONFIG_ARM64) || defined(CONFIG_TILE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#define GET_TIME(x) do { x = get_cycles(); } while (0)
168#define DELTA(x,y) ((y)-(x))
Arnd Bergmann6fdd4962013-02-13 17:11:09 +0100169#define TIME_NAME "get_cycles"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170#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
Takashi Iwai76460a72014-09-11 10:28:30 -0700179static inline u64 get_time(void)
180{
181 if (use_ktime) {
182 return ktime_get_ns();
183 } else {
184 unsigned int x;
185 GET_TIME(x);
186 return x;
187 }
188}
189
190static inline unsigned int delta(u64 x, u64 y)
191{
192 if (use_ktime)
193 return y - x;
194 else
195 return DELTA((unsigned int)x, (unsigned int)y);
196}
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198/*
199 * analog_decode() decodes analog joystick data and reports input events.
200 */
201
202static void analog_decode(struct analog *analog, int *axes, int *initial, int buttons)
203{
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500204 struct input_dev *dev = analog->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 int i, j;
206
207 if (analog->mask & ANALOG_HAT_FCS)
208 for (i = 0; i < 4; i++)
209 if (axes[3] < ((initial[3] * ((i << 1) + 1)) >> 3)) {
210 buttons |= 1 << (i + 14);
211 break;
212 }
213
214 for (i = j = 0; i < 6; i++)
215 if (analog->mask & (0x10 << i))
216 input_report_key(dev, analog->buttons[j++], (buttons >> i) & 1);
217
218 if (analog->mask & ANALOG_HBTN_CHF)
219 for (i = 0; i < 4; i++)
220 input_report_key(dev, analog->buttons[j++], (buttons >> (i + 10)) & 1);
221
222 if (analog->mask & ANALOG_BTN_TL)
223 input_report_key(dev, analog_pads[0], axes[2] < (initial[2] >> 1));
224 if (analog->mask & ANALOG_BTN_TR)
225 input_report_key(dev, analog_pads[1], axes[3] < (initial[3] >> 1));
226 if (analog->mask & ANALOG_BTN_TL2)
227 input_report_key(dev, analog_pads[2], axes[2] > (initial[2] + (initial[2] >> 1)));
228 if (analog->mask & ANALOG_BTN_TR2)
229 input_report_key(dev, analog_pads[3], axes[3] > (initial[3] + (initial[3] >> 1)));
230
231 for (i = j = 0; i < 4; i++)
232 if (analog->mask & (1 << i))
233 input_report_abs(dev, analog_axes[j++], axes[i]);
234
235 for (i = j = 0; i < 3; i++)
236 if (analog->mask & analog_exts[i]) {
237 input_report_abs(dev, analog_hats[j++],
238 ((buttons >> ((i << 2) + 7)) & 1) - ((buttons >> ((i << 2) + 9)) & 1));
239 input_report_abs(dev, analog_hats[j++],
240 ((buttons >> ((i << 2) + 8)) & 1) - ((buttons >> ((i << 2) + 6)) & 1));
241 }
242
243 input_sync(dev);
244}
245
246/*
247 * analog_cooked_read() reads analog joystick data.
248 */
249
250static int analog_cooked_read(struct analog_port *port)
251{
252 struct gameport *gameport = port->gameport;
Takashi Iwai76460a72014-09-11 10:28:30 -0700253 u64 time[4], start, loop, now;
254 unsigned int loopout, timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 unsigned char data[4], this, last;
256 unsigned long flags;
257 int i, j;
258
259 loopout = (ANALOG_LOOP_TIME * port->loop) / 1000;
260 timeout = ANALOG_MAX_TIME * port->speed;
261
262 local_irq_save(flags);
263 gameport_trigger(gameport);
Takashi Iwai76460a72014-09-11 10:28:30 -0700264 now = get_time();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 local_irq_restore(flags);
266
267 start = now;
268 this = port->mask;
269 i = 0;
270
271 do {
272 loop = now;
273 last = this;
274
275 local_irq_disable();
276 this = gameport_read(gameport) & port->mask;
Takashi Iwai76460a72014-09-11 10:28:30 -0700277 now = get_time();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 local_irq_restore(flags);
279
Takashi Iwai76460a72014-09-11 10:28:30 -0700280 if ((last ^ this) && (delta(loop, now) < loopout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 data[i] = last ^ this;
282 time[i] = now;
283 i++;
284 }
285
Takashi Iwai76460a72014-09-11 10:28:30 -0700286 } while (this && (i < 4) && (delta(start, now) < timeout));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 this <<= 4;
289
290 for (--i; i >= 0; i--) {
291 this |= data[i];
292 for (j = 0; j < 4; j++)
293 if (data[i] & (1 << j))
Takashi Iwai76460a72014-09-11 10:28:30 -0700294 port->axes[j] = (delta(start, time[i]) << ANALOG_FUZZ_BITS) / port->loop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296
297 return -(this != port->mask);
298}
299
300static int analog_button_read(struct analog_port *port, char saitek, char chf)
301{
302 unsigned char u;
303 int t = 1, i = 0;
304 int strobe = gameport_time(port->gameport, ANALOG_SAITEK_TIME);
305
306 u = gameport_read(port->gameport);
307
308 if (!chf) {
309 port->buttons = (~u >> 4) & 0xf;
310 return 0;
311 }
312
313 port->buttons = 0;
314
315 while ((~u & 0xf0) && (i < 16) && t) {
316 port->buttons |= 1 << analog_chf[(~u >> 4) & 0xf];
317 if (!saitek) return 0;
318 udelay(ANALOG_SAITEK_DELAY);
319 t = strobe;
320 gameport_trigger(port->gameport);
321 while (((u = gameport_read(port->gameport)) & port->mask) && t) t--;
322 i++;
323 }
324
325 return -(!t || (i == 16));
326}
327
328/*
329 * analog_poll() repeatedly polls the Analog joysticks.
330 */
331
332static void analog_poll(struct gameport *gameport)
333{
334 struct analog_port *port = gameport_get_drvdata(gameport);
335 int i;
336
337 char saitek = !!(port->analog[0].mask & ANALOG_SAITEK);
338 char chf = !!(port->analog[0].mask & ANALOG_ANY_CHF);
339
340 if (port->cooked) {
341 port->bads -= gameport_cooked_read(port->gameport, port->axes, &port->buttons);
342 if (chf)
343 port->buttons = port->buttons ? (1 << analog_chf[port->buttons]) : 0;
344 port->reads++;
345 } else {
346 if (!port->axtime--) {
347 port->bads -= analog_cooked_read(port);
348 port->bads -= analog_button_read(port, saitek, chf);
349 port->reads++;
350 port->axtime = ANALOG_AXIS_TIME - 1;
351 } else {
352 if (!saitek)
353 analog_button_read(port, saitek, chf);
354 }
355 }
356
357 for (i = 0; i < 2; i++)
358 if (port->analog[i].mask)
359 analog_decode(port->analog + i, port->axes, port->initial, port->buttons);
360}
361
362/*
363 * analog_open() is a callback from the input open routine.
364 */
365
366static int analog_open(struct input_dev *dev)
367{
Dmitry Torokhov8715c1c2007-04-12 01:34:14 -0400368 struct analog_port *port = input_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 gameport_start_polling(port->gameport);
371 return 0;
372}
373
374/*
375 * analog_close() is a callback from the input close routine.
376 */
377
378static void analog_close(struct input_dev *dev)
379{
Dmitry Torokhov8715c1c2007-04-12 01:34:14 -0400380 struct analog_port *port = input_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 gameport_stop_polling(port->gameport);
383}
384
385/*
386 * analog_calibrate_timer() calibrates the timer and computes loop
387 * and timeout values for a joystick port.
388 */
389
390static void analog_calibrate_timer(struct analog_port *port)
391{
392 struct gameport *gameport = port->gameport;
Takashi Iwai76460a72014-09-11 10:28:30 -0700393 unsigned int i, t, tx;
394 u64 t1, t2, t3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 unsigned long flags;
396
Takashi Iwai76460a72014-09-11 10:28:30 -0700397 if (use_ktime) {
398 port->speed = 1000000;
399 } else {
400 local_irq_save(flags);
401 t1 = get_time();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402#ifdef FAKE_TIME
Takashi Iwai76460a72014-09-11 10:28:30 -0700403 analog_faketime += 830;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404#endif
Takashi Iwai76460a72014-09-11 10:28:30 -0700405 mdelay(1);
406 t2 = get_time();
407 t3 = get_time();
408 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Takashi Iwai76460a72014-09-11 10:28:30 -0700410 port->speed = delta(t1, t2) - delta(t2, t3);
411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 tx = ~0;
414
415 for (i = 0; i < 50; i++) {
416 local_irq_save(flags);
Takashi Iwai76460a72014-09-11 10:28:30 -0700417 t1 = get_time();
418 for (t = 0; t < 50; t++) {
419 gameport_read(gameport);
420 t2 = get_time();
421 }
422 t3 = get_time();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 local_irq_restore(flags);
424 udelay(i);
Takashi Iwai76460a72014-09-11 10:28:30 -0700425 t = delta(t1, t2) - delta(t2, t3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (t < tx) tx = t;
427 }
428
429 port->loop = tx / 50;
430}
431
432/*
433 * analog_name() constructs a name for an analog joystick.
434 */
435
436static void analog_name(struct analog *analog)
437{
Dmitry Torokhov10ca4c02006-06-26 01:45:48 -0400438 snprintf(analog->name, sizeof(analog->name), "Analog %d-axis %d-button",
439 hweight8(analog->mask & ANALOG_AXES_STD),
440 hweight8(analog->mask & ANALOG_BTNS_STD) + !!(analog->mask & ANALOG_BTNS_CHF) * 2 +
441 hweight16(analog->mask & ANALOG_BTNS_GAMEPAD) + !!(analog->mask & ANALOG_HBTN_CHF) * 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 if (analog->mask & ANALOG_HATS_ALL)
Dmitry Torokhov10ca4c02006-06-26 01:45:48 -0400444 snprintf(analog->name, sizeof(analog->name), "%s %d-hat",
445 analog->name, hweight16(analog->mask & ANALOG_HATS_ALL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 if (analog->mask & ANALOG_HAT_FCS)
Dmitry Torokhov10ca4c02006-06-26 01:45:48 -0400448 strlcat(analog->name, " FCS", sizeof(analog->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (analog->mask & ANALOG_ANY_CHF)
Dmitry Torokhov10ca4c02006-06-26 01:45:48 -0400450 strlcat(analog->name, (analog->mask & ANALOG_SAITEK) ? " Saitek" : " CHF",
451 sizeof(analog->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Dmitry Torokhov10ca4c02006-06-26 01:45:48 -0400453 strlcat(analog->name, (analog->mask & ANALOG_GAMEPAD) ? " gamepad": " joystick",
454 sizeof(analog->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457/*
458 * analog_init_device()
459 */
460
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500461static int analog_init_device(struct analog_port *port, struct analog *analog, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500463 struct input_dev *input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 int i, j, t, v, w, x, y, z;
Dmitry Torokhov127278c2006-11-05 22:40:09 -0500465 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 analog_name(analog);
Dmitry Torokhov10ca4c02006-06-26 01:45:48 -0400468 snprintf(analog->phys, sizeof(analog->phys),
469 "%s/input%d", port->gameport->phys, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 analog->buttons = (analog->mask & ANALOG_GAMEPAD) ? analog_pad_btn : analog_joy_btn;
471
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500472 analog->dev = input_dev = input_allocate_device();
473 if (!input_dev)
474 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500476 input_dev->name = analog->name;
477 input_dev->phys = analog->phys;
478 input_dev->id.bustype = BUS_GAMEPORT;
479 input_dev->id.vendor = GAMEPORT_ID_VENDOR_ANALOG;
480 input_dev->id.product = analog->mask >> 4;
481 input_dev->id.version = 0x0100;
Dmitry Torokhov935e6582007-04-12 01:35:26 -0400482 input_dev->dev.parent = &port->gameport->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Dmitry Torokhov8715c1c2007-04-12 01:34:14 -0400484 input_set_drvdata(input_dev, port);
485
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500486 input_dev->open = analog_open;
487 input_dev->close = analog_close;
Dmitry Torokhov8715c1c2007-04-12 01:34:14 -0400488
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700489 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 for (i = j = 0; i < 4; i++)
492 if (analog->mask & (1 << i)) {
493
494 t = analog_axes[j];
495 x = port->axes[i];
496 y = (port->axes[0] + port->axes[1]) >> 1;
497 z = y - port->axes[i];
498 z = z > 0 ? z : -z;
499 v = (x >> 3);
500 w = (x >> 3);
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if ((i == 2 || i == 3) && (j == 2 || j == 3) && (z > (y >> 3)))
503 x = y;
504
505 if (analog->mask & ANALOG_SAITEK) {
506 if (i == 2) x = port->axes[i];
507 v = x - (x >> 2);
508 w = (x >> 4);
509 }
510
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500511 input_set_abs_params(input_dev, t, v, (x << 1) - v, port->fuzz, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 j++;
513 }
514
515 for (i = j = 0; i < 3; i++)
516 if (analog->mask & analog_exts[i])
517 for (x = 0; x < 2; x++) {
518 t = analog_hats[j++];
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500519 input_set_abs_params(input_dev, t, -1, 1, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521
522 for (i = j = 0; i < 4; i++)
523 if (analog->mask & (0x10 << i))
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500524 set_bit(analog->buttons[j++], input_dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 if (analog->mask & ANALOG_BTNS_CHF)
527 for (i = 0; i < 2; i++)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500528 set_bit(analog->buttons[j++], input_dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 if (analog->mask & ANALOG_HBTN_CHF)
531 for (i = 0; i < 4; i++)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500532 set_bit(analog->buttons[j++], input_dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 for (i = 0; i < 4; i++)
535 if (analog->mask & (ANALOG_BTN_TL << i))
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500536 set_bit(analog_pads[i], input_dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 analog_decode(analog, port->axes, port->initial, port->buttons);
539
Dmitry Torokhov127278c2006-11-05 22:40:09 -0500540 error = input_register_device(analog->dev);
541 if (error) {
542 input_free_device(analog->dev);
543 return error;
544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500546 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
549/*
550 * analog_init_devices() sets up device-specific values and registers the input devices.
551 */
552
553static int analog_init_masks(struct analog_port *port)
554{
555 int i;
556 struct analog *analog = port->analog;
557 int max[4];
558
559 if (!port->mask)
560 return -1;
561
562 if ((port->mask & 3) != 3 && port->mask != 0xc) {
563 printk(KERN_WARNING "analog.c: Unknown joystick device found "
564 "(data=%#x, %s), probably not analog joystick.\n",
565 port->mask, port->gameport->phys);
566 return -1;
567 }
568
569
570 i = analog_options[0]; /* FIXME !!! - need to specify options for different ports */
571
572 analog[0].mask = i & 0xfffff;
573
574 analog[0].mask &= ~(ANALOG_AXES_STD | ANALOG_HAT_FCS | ANALOG_BTNS_GAMEPAD)
575 | port->mask | ((port->mask << 8) & ANALOG_HAT_FCS)
576 | ((port->mask << 10) & ANALOG_BTNS_TLR) | ((port->mask << 12) & ANALOG_BTNS_TLR2);
577
578 analog[0].mask &= ~(ANALOG_HAT2_CHF)
579 | ((analog[0].mask & ANALOG_HBTN_CHF) ? 0 : ANALOG_HAT2_CHF);
580
581 analog[0].mask &= ~(ANALOG_THROTTLE | ANALOG_BTN_TR | ANALOG_BTN_TR2)
582 | ((~analog[0].mask & ANALOG_HAT_FCS) >> 8)
583 | ((~analog[0].mask & ANALOG_HAT_FCS) << 2)
584 | ((~analog[0].mask & ANALOG_HAT_FCS) << 4);
585
586 analog[0].mask &= ~(ANALOG_THROTTLE | ANALOG_RUDDER)
587 | (((~analog[0].mask & ANALOG_BTNS_TLR ) >> 10)
588 & ((~analog[0].mask & ANALOG_BTNS_TLR2) >> 12));
589
590 analog[1].mask = ((i >> 20) & 0xff) | ((i >> 12) & 0xf0000);
591
592 analog[1].mask &= (analog[0].mask & ANALOG_EXTENSIONS) ? ANALOG_GAMEPAD
593 : (((ANALOG_BTNS_STD | port->mask) & ~analog[0].mask) | ANALOG_GAMEPAD);
594
595 if (port->cooked) {
596
597 for (i = 0; i < 4; i++) max[i] = port->axes[i] << 1;
598
599 if ((analog[0].mask & 0x7) == 0x7) max[2] = (max[0] + max[1]) >> 1;
600 if ((analog[0].mask & 0xb) == 0xb) max[3] = (max[0] + max[1]) >> 1;
601 if ((analog[0].mask & ANALOG_BTN_TL) && !(analog[0].mask & ANALOG_BTN_TL2)) max[2] >>= 1;
602 if ((analog[0].mask & ANALOG_BTN_TR) && !(analog[0].mask & ANALOG_BTN_TR2)) max[3] >>= 1;
603 if ((analog[0].mask & ANALOG_HAT_FCS)) max[3] >>= 1;
604
605 gameport_calibrate(port->gameport, port->axes, max);
606 }
607
608 for (i = 0; i < 4; i++)
609 port->initial[i] = port->axes[i];
610
611 return -!(analog[0].mask || analog[1].mask);
612}
613
614static int analog_init_port(struct gameport *gameport, struct gameport_driver *drv, struct analog_port *port)
615{
616 int i, t, u, v;
617
618 port->gameport = gameport;
619
620 gameport_set_drvdata(gameport, port);
621
622 if (!gameport_open(gameport, drv, GAMEPORT_MODE_RAW)) {
623
624 analog_calibrate_timer(port);
625
626 gameport_trigger(gameport);
627 t = gameport_read(gameport);
628 msleep(ANALOG_MAX_TIME);
629 port->mask = (gameport_read(gameport) ^ t) & t & 0xf;
630 port->fuzz = (port->speed * ANALOG_FUZZ_MAGIC) / port->loop / 1000 + ANALOG_FUZZ_BITS;
631
632 for (i = 0; i < ANALOG_INIT_RETRIES; i++) {
633 if (!analog_cooked_read(port))
634 break;
635 msleep(ANALOG_MAX_TIME);
636 }
637
638 u = v = 0;
639
640 msleep(ANALOG_MAX_TIME);
641 t = gameport_time(gameport, ANALOG_MAX_TIME * 1000);
642 gameport_trigger(gameport);
643 while ((gameport_read(port->gameport) & port->mask) && (u < t))
644 u++;
645 udelay(ANALOG_SAITEK_DELAY);
646 t = gameport_time(gameport, ANALOG_SAITEK_TIME);
647 gameport_trigger(gameport);
648 while ((gameport_read(port->gameport) & port->mask) && (v < t))
649 v++;
650
651 if (v < (u >> 1)) { /* FIXME - more than one port */
652 analog_options[0] |= /* FIXME - more than one port */
653 ANALOG_SAITEK | ANALOG_BTNS_CHF | ANALOG_HBTN_CHF | ANALOG_HAT1_CHF;
654 return 0;
655 }
656
657 gameport_close(gameport);
658 }
659
660 if (!gameport_open(gameport, drv, GAMEPORT_MODE_COOKED)) {
661
662 for (i = 0; i < ANALOG_INIT_RETRIES; i++)
663 if (!gameport_cooked_read(gameport, port->axes, &port->buttons))
664 break;
665 for (i = 0; i < 4; i++)
666 if (port->axes[i] != -1)
667 port->mask |= 1 << i;
668
669 port->fuzz = gameport->fuzz;
670 port->cooked = 1;
671 return 0;
672 }
673
674 return gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
675}
676
677static int analog_connect(struct gameport *gameport, struct gameport_driver *drv)
678{
679 struct analog_port *port;
680 int i;
681 int err;
682
Pekka Enberga97e1482005-09-06 15:18:33 -0700683 if (!(port = kzalloc(sizeof(struct analog_port), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return - ENOMEM;
685
686 err = analog_init_port(gameport, drv, port);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500687 if (err)
688 goto fail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 err = analog_init_masks(port);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500691 if (err)
692 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 gameport_set_poll_handler(gameport, analog_poll);
695 gameport_set_poll_interval(gameport, 10);
696
697 for (i = 0; i < 2; i++)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500698 if (port->analog[i].mask) {
699 err = analog_init_device(port, port->analog + i, i);
700 if (err)
701 goto fail3;
702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 return 0;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500705
706 fail3: while (--i >= 0)
Dmitry Torokhov127278c2006-11-05 22:40:09 -0500707 if (port->analog[i].mask)
708 input_unregister_device(port->analog[i].dev);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500709 fail2: gameport_close(gameport);
710 fail1: gameport_set_drvdata(gameport, NULL);
711 kfree(port);
712 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
715static void analog_disconnect(struct gameport *gameport)
716{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 struct analog_port *port = gameport_get_drvdata(gameport);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500718 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 for (i = 0; i < 2; i++)
721 if (port->analog[i].mask)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500722 input_unregister_device(port->analog[i].dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 gameport_close(gameport);
724 gameport_set_drvdata(gameport, NULL);
725 printk(KERN_INFO "analog.c: %d out of %d reads (%d%%) on %s failed\n",
726 port->bads, port->reads, port->reads ? (port->bads * 100 / port->reads) : 0,
727 port->gameport->phys);
728 kfree(port);
729}
730
731struct analog_types {
732 char *name;
733 int value;
734};
735
736static struct analog_types analog_types[] = {
737 { "none", 0x00000000 },
738 { "auto", 0x000000ff },
739 { "2btn", 0x0000003f },
740 { "y-joy", 0x0cc00033 },
741 { "y-pad", 0x8cc80033 },
742 { "fcs", 0x000008f7 },
743 { "chf", 0x000002ff },
744 { "fullchf", 0x000007ff },
745 { "gamepad", 0x000830f3 },
746 { "gamepad8", 0x0008f0f3 },
747 { NULL, 0 }
748};
749
750static void analog_parse_options(void)
751{
752 int i, j;
753 char *end;
754
755 for (i = 0; i < js_nargs; i++) {
756
757 for (j = 0; analog_types[j].name; j++)
758 if (!strcmp(analog_types[j].name, js[i])) {
759 analog_options[i] = analog_types[j].value;
760 break;
761 }
762 if (analog_types[j].name) continue;
763
764 analog_options[i] = simple_strtoul(js[i], &end, 0);
765 if (end != js[i]) continue;
766
767 analog_options[i] = 0xff;
768 if (!strlen(js[i])) continue;
769
770 printk(KERN_WARNING "analog.c: Bad config for port %d - \"%s\"\n", i, js[i]);
771 }
772
773 for (; i < ANALOG_PORTS; i++)
774 analog_options[i] = 0xff;
775}
776
777/*
778 * The gameport device structure.
779 */
780
781static struct gameport_driver analog_drv = {
782 .driver = {
783 .name = "analog",
784 },
785 .description = DRIVER_DESC,
786 .connect = analog_connect,
787 .disconnect = analog_disconnect,
788};
789
790static int __init analog_init(void)
791{
792 analog_parse_options();
Dmitry Torokhov25472032008-06-06 01:33:37 -0400793 return gameport_register_driver(&analog_drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
796static void __exit analog_exit(void)
797{
798 gameport_unregister_driver(&analog_drv);
799}
800
801module_init(analog_init);
802module_exit(analog_exit);