Rev 538 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
494 | giacomo | 1 | /* |
2 | * $Id: analog.c,v 1.1 2004-03-08 18:47:38 giacomo 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 | |||
31 | #include <linuxcomp.h> |
||
32 | |||
33 | #include <linux/config.h> |
||
34 | #include <linux/delay.h> |
||
35 | #include <linux/kernel.h> |
||
36 | #include <linux/module.h> |
||
37 | #include <linux/slab.h> |
||
38 | #include <linux/bitops.h> |
||
39 | #include <linux/init.h> |
||
40 | #include <linux/input.h> |
||
41 | #include <linux/gameport.h> |
||
42 | #include <asm/timex.h> |
||
43 | |||
44 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); |
||
45 | MODULE_DESCRIPTION("Analog joystick and gamepad driver"); |
||
46 | MODULE_LICENSE("GPL"); |
||
47 | |||
48 | /* |
||
49 | * Option parsing. |
||
50 | */ |
||
51 | |||
52 | #define ANALOG_PORTS 16 |
||
53 | |||
54 | static char *js[ANALOG_PORTS]; |
||
55 | static int analog_options[ANALOG_PORTS]; |
||
56 | MODULE_PARM(js, "1-" __MODULE_STRING(ANALOG_PORTS) "s"); |
||
57 | MODULE_PARM_DESC(js, "Analog joystick options"); |
||
58 | |||
59 | /* |
||
60 | * Times, feature definitions. |
||
61 | */ |
||
62 | |||
63 | #define ANALOG_RUDDER 0x00004 |
||
64 | #define ANALOG_THROTTLE 0x00008 |
||
65 | #define ANALOG_AXES_STD 0x0000f |
||
66 | #define ANALOG_BTNS_STD 0x000f0 |
||
67 | |||
68 | #define ANALOG_BTNS_CHF 0x00100 |
||
69 | #define ANALOG_HAT1_CHF 0x00200 |
||
70 | #define ANALOG_HAT2_CHF 0x00400 |
||
71 | #define ANALOG_HAT_FCS 0x00800 |
||
72 | #define ANALOG_HATS_ALL 0x00e00 |
||
73 | #define ANALOG_BTN_TL 0x01000 |
||
74 | #define ANALOG_BTN_TR 0x02000 |
||
75 | #define ANALOG_BTN_TL2 0x04000 |
||
76 | #define ANALOG_BTN_TR2 0x08000 |
||
77 | #define ANALOG_BTNS_TLR 0x03000 |
||
78 | #define ANALOG_BTNS_TLR2 0x0c000 |
||
79 | #define ANALOG_BTNS_GAMEPAD 0x0f000 |
||
80 | |||
81 | #define ANALOG_HBTN_CHF 0x10000 |
||
82 | #define ANALOG_ANY_CHF 0x10700 |
||
83 | #define ANALOG_SAITEK 0x20000 |
||
84 | #define ANALOG_EXTENSIONS 0x7ff00 |
||
85 | #define ANALOG_GAMEPAD 0x80000 |
||
86 | |||
87 | #define ANALOG_MAX_TIME 3 /* 3 ms */ |
||
88 | #define ANALOG_LOOP_TIME 2000 /* 2 * loop */ |
||
89 | #define ANALOG_REFRESH_TIME HZ/100 /* 10 ms */ |
||
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 | |||
100 | static short analog_axes[] = { ABS_X, ABS_Y, ABS_RUDDER, ABS_THROTTLE }; |
||
101 | static short analog_hats[] = { ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y }; |
||
102 | static short analog_pads[] = { BTN_Y, BTN_Z, BTN_TL, BTN_TR }; |
||
103 | static short analog_exts[] = { ANALOG_HAT1_CHF, ANALOG_HAT2_CHF, ANALOG_HAT_FCS }; |
||
104 | static short analog_pad_btn[] = { BTN_A, BTN_B, BTN_C, BTN_X, BTN_TL2, BTN_TR2, BTN_SELECT, BTN_START, BTN_MODE, BTN_BASE }; |
||
105 | static 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 | |||
108 | static unsigned char analog_chf[] = { 0xf, 0x0, 0x1, 0x9, 0x2, 0x4, 0xc, 0x8, 0x3, 0x5, 0xb, 0x7, 0xd, 0xe, 0xa, 0x6 }; |
||
109 | |||
110 | struct analog { |
||
111 | struct input_dev dev; |
||
112 | int mask; |
||
113 | short *buttons; |
||
114 | char name[ANALOG_MAX_NAME_LENGTH]; |
||
115 | char phys[ANALOG_MAX_PHYS_LENGTH]; |
||
116 | }; |
||
117 | |||
118 | struct analog_port { |
||
119 | struct gameport *gameport; |
||
120 | struct timer_list timer; |
||
121 | struct analog analog[2]; |
||
122 | unsigned char mask; |
||
123 | char saitek; |
||
124 | char cooked; |
||
125 | int bads; |
||
126 | int reads; |
||
127 | int speed; |
||
128 | int loop; |
||
129 | int fuzz; |
||
130 | int axes[4]; |
||
131 | int buttons; |
||
132 | int initial[4]; |
||
133 | int used; |
||
134 | int axtime; |
||
135 | }; |
||
136 | |||
137 | /* |
||
138 | * Time macros. |
||
139 | */ |
||
140 | |||
141 | #ifdef __i386__ |
||
142 | #define GET_TIME(x) do { if (cpu_has_tsc) rdtscl(x); else x = get_time_pit(); } while (0) |
||
143 | #define DELTA(x,y) (cpu_has_tsc?((y)-(x)):((x)-(y)+((x)<(y)?1193182L/HZ:0))) |
||
144 | #define TIME_NAME (cpu_has_tsc?"TSC":"PIT") |
||
145 | static unsigned int get_time_pit(void) |
||
146 | { |
||
147 | extern spinlock_t i8253_lock; |
||
148 | 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 __x86_64__ |
||
160 | #define GET_TIME(x) rdtscl(x) |
||
161 | #define DELTA(x,y) ((y)-(x)) |
||
162 | #define TIME_NAME "TSC" |
||
163 | #elif __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 |
||
169 | static 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 | |||
180 | static void analog_decode(struct analog *analog, int *axes, int *initial, int buttons) |
||
181 | { |
||
182 | struct input_dev *dev = &analog->dev; |
||
183 | 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 | |||
228 | static 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 | |||
277 | static 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_timer() repeatedly polls the Analog joysticks. |
||
307 | */ |
||
308 | |||
309 | static void analog_timer(unsigned long data) |
||
310 | { |
||
311 | struct analog_port *port = (void *) data; |
||
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 | mod_timer(&port->timer, jiffies + ANALOG_REFRESH_TIME); |
||
339 | } |
||
340 | |||
341 | /* |
||
342 | * analog_open() is a callback from the input open routine. |
||
343 | */ |
||
344 | |||
345 | static int analog_open(struct input_dev *dev) |
||
346 | { |
||
347 | struct analog_port *port = dev->private; |
||
348 | if (!port->used++) |
||
349 | mod_timer(&port->timer, jiffies + ANALOG_REFRESH_TIME); |
||
350 | return 0; |
||
351 | } |
||
352 | |||
353 | /* |
||
354 | * analog_close() is a callback from the input close routine. |
||
355 | */ |
||
356 | |||
357 | static void analog_close(struct input_dev *dev) |
||
358 | { |
||
359 | struct analog_port *port = dev->private; |
||
360 | if (!--port->used) |
||
361 | del_timer(&port->timer); |
||
362 | } |
||
363 | |||
364 | /* |
||
365 | * analog_calibrate_timer() calibrates the timer and computes loop |
||
366 | * and timeout values for a joystick port. |
||
367 | */ |
||
368 | |||
369 | static 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 | udelay(1000); |
||
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 | |||
407 | static void analog_name(struct analog *analog) |
||
408 | { |
||
409 | sprintf26(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 | sprintf26(analog->name, "%s %d-hat", |
||
416 | analog->name, hweight16(analog->mask & ANALOG_HATS_ALL)); |
||
417 | |||
418 | if (analog->mask & ANALOG_HAT_FCS) |
||
419 | strcat(analog->name, " FCS"); |
||
420 | if (analog->mask & ANALOG_ANY_CHF) |
||
421 | strcat(analog->name, (analog->mask & ANALOG_SAITEK) ? " Saitek" : " CHF"); |
||
422 | |||
423 | strcat(analog->name, (analog->mask & ANALOG_GAMEPAD) ? " gamepad": " joystick"); |
||
424 | } |
||
425 | |||
426 | /* |
||
427 | * analog_init_device() |
||
428 | */ |
||
429 | |||
430 | static void analog_init_device(struct analog_port *port, struct analog *analog, int index) |
||
431 | { |
||
432 | int i, j, t, v, w, x, y, z; |
||
433 | |||
434 | analog_name(analog); |
||
435 | sprintf26(analog->phys, "%s/input%d", port->gameport->phys, index); |
||
436 | analog->buttons = (analog->mask & ANALOG_GAMEPAD) ? analog_pad_btn : analog_joy_btn; |
||
437 | |||
438 | init_input_dev(&analog->dev); |
||
439 | |||
440 | analog->dev.name = analog->name; |
||
441 | analog->dev.phys = analog->phys; |
||
442 | analog->dev.id.bustype = BUS_GAMEPORT; |
||
443 | analog->dev.id.vendor = GAMEPORT_ID_VENDOR_ANALOG; |
||
444 | analog->dev.id.product = analog->mask >> 4; |
||
445 | analog->dev.id.version = 0x0100; |
||
446 | |||
447 | analog->dev.open = analog_open; |
||
448 | analog->dev.close = analog_close; |
||
449 | analog->dev.private = port; |
||
450 | analog->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS); |
||
451 | |||
452 | for (i = j = 0; i < 4; i++) |
||
453 | if (analog->mask & (1 << i)) { |
||
454 | |||
455 | t = analog_axes[j]; |
||
456 | x = port->axes[i]; |
||
457 | y = (port->axes[0] + port->axes[1]) >> 1; |
||
458 | z = y - port->axes[i]; |
||
459 | z = z > 0 ? z : -z; |
||
460 | v = (x >> 3); |
||
461 | w = (x >> 3); |
||
462 | |||
463 | set_bit(t, analog->dev.absbit); |
||
464 | |||
465 | if ((i == 2 || i == 3) && (j == 2 || j == 3) && (z > (y >> 3))) |
||
466 | x = y; |
||
467 | |||
468 | if (analog->mask & ANALOG_SAITEK) { |
||
469 | if (i == 2) x = port->axes[i]; |
||
470 | v = x - (x >> 2); |
||
471 | w = (x >> 4); |
||
472 | } |
||
473 | |||
474 | analog->dev.absmax[t] = (x << 1) - v; |
||
475 | analog->dev.absmin[t] = v; |
||
476 | analog->dev.absfuzz[t] = port->fuzz; |
||
477 | analog->dev.absflat[t] = w; |
||
478 | |||
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 | set_bit(t, analog->dev.absbit); |
||
487 | analog->dev.absmax[t] = 1; |
||
488 | analog->dev.absmin[t] = -1; |
||
489 | } |
||
490 | |||
491 | for (i = j = 0; i < 4; i++) |
||
492 | if (analog->mask & (0x10 << i)) |
||
493 | set_bit(analog->buttons[j++], analog->dev.keybit); |
||
494 | |||
495 | if (analog->mask & ANALOG_BTNS_CHF) |
||
496 | for (i = 0; i < 2; i++) |
||
497 | set_bit(analog->buttons[j++], analog->dev.keybit); |
||
498 | |||
499 | if (analog->mask & ANALOG_HBTN_CHF) |
||
500 | for (i = 0; i < 4; i++) |
||
501 | set_bit(analog->buttons[j++], analog->dev.keybit); |
||
502 | |||
503 | for (i = 0; i < 4; i++) |
||
504 | if (analog->mask & (ANALOG_BTN_TL << i)) |
||
505 | set_bit(analog_pads[i], analog->dev.keybit); |
||
506 | |||
507 | analog_decode(analog, port->axes, port->initial, port->buttons); |
||
508 | |||
509 | input_register_device(&analog->dev); |
||
510 | |||
511 | printk(KERN_INFO "input: %s at %s", analog->name, port->gameport->phys); |
||
512 | |||
513 | if (port->cooked) |
||
514 | printk(" [ADC port]\n"); |
||
515 | else |
||
516 | printk(" [%s timer, %d %sHz clock, %d ns res]\n", TIME_NAME, |
||
517 | port->speed > 10000 ? (port->speed + 800) / 1000 : port->speed, |
||
518 | port->speed > 10000 ? "M" : "k", |
||
519 | port->speed > 10000 ? (port->loop * 1000) / (port->speed / 1000) |
||
520 | : (port->loop * 1000000) / port->speed); |
||
521 | } |
||
522 | |||
523 | /* |
||
524 | * analog_init_devices() sets up device-specific values and registers the input devices. |
||
525 | */ |
||
526 | |||
527 | static int analog_init_masks(struct analog_port *port) |
||
528 | { |
||
529 | int i; |
||
530 | struct analog *analog = port->analog; |
||
531 | int max[4]; |
||
532 | |||
533 | if (!port->mask) |
||
534 | return -1; |
||
535 | |||
536 | if ((port->mask & 3) != 3 && port->mask != 0xc) { |
||
537 | printk(KERN_WARNING "analog.c: Unknown joystick device found " |
||
538 | "(data=%#x, %s), probably not analog joystick.\n", |
||
539 | port->mask, port->gameport->phys); |
||
540 | return -1; |
||
541 | } |
||
542 | |||
543 | |||
544 | i = analog_options[0]; /* FIXME !!! - need to specify options for different ports */ |
||
545 | |||
546 | analog[0].mask = i & 0xfffff; |
||
547 | |||
548 | analog[0].mask &= ~(ANALOG_AXES_STD | ANALOG_HAT_FCS | ANALOG_BTNS_GAMEPAD) |
||
549 | | port->mask | ((port->mask << 8) & ANALOG_HAT_FCS) |
||
550 | | ((port->mask << 10) & ANALOG_BTNS_TLR) | ((port->mask << 12) & ANALOG_BTNS_TLR2); |
||
551 | |||
552 | analog[0].mask &= ~(ANALOG_HAT2_CHF) |
||
553 | | ((analog[0].mask & ANALOG_HBTN_CHF) ? 0 : ANALOG_HAT2_CHF); |
||
554 | |||
555 | analog[0].mask &= ~(ANALOG_THROTTLE | ANALOG_BTN_TR | ANALOG_BTN_TR2) |
||
556 | | ((~analog[0].mask & ANALOG_HAT_FCS) >> 8) |
||
557 | | ((~analog[0].mask & ANALOG_HAT_FCS) << 2) |
||
558 | | ((~analog[0].mask & ANALOG_HAT_FCS) << 4); |
||
559 | |||
560 | analog[0].mask &= ~(ANALOG_THROTTLE | ANALOG_RUDDER) |
||
561 | | (((~analog[0].mask & ANALOG_BTNS_TLR ) >> 10) |
||
562 | & ((~analog[0].mask & ANALOG_BTNS_TLR2) >> 12)); |
||
563 | |||
564 | analog[1].mask = ((i >> 20) & 0xff) | ((i >> 12) & 0xf0000); |
||
565 | |||
566 | analog[1].mask &= (analog[0].mask & ANALOG_EXTENSIONS) ? ANALOG_GAMEPAD |
||
567 | : (((ANALOG_BTNS_STD | port->mask) & ~analog[0].mask) | ANALOG_GAMEPAD); |
||
568 | |||
569 | if (port->cooked) { |
||
570 | |||
571 | for (i = 0; i < 4; i++) max[i] = port->axes[i] << 1; |
||
572 | |||
573 | if ((analog[0].mask & 0x7) == 0x7) max[2] = (max[0] + max[1]) >> 1; |
||
574 | if ((analog[0].mask & 0xb) == 0xb) max[3] = (max[0] + max[1]) >> 1; |
||
575 | if ((analog[0].mask & ANALOG_BTN_TL) && !(analog[0].mask & ANALOG_BTN_TL2)) max[2] >>= 1; |
||
576 | if ((analog[0].mask & ANALOG_BTN_TR) && !(analog[0].mask & ANALOG_BTN_TR2)) max[3] >>= 1; |
||
577 | if ((analog[0].mask & ANALOG_HAT_FCS)) max[3] >>= 1; |
||
578 | |||
579 | gameport_calibrate(port->gameport, port->axes, max); |
||
580 | } |
||
581 | |||
582 | for (i = 0; i < 4; i++) |
||
583 | port->initial[i] = port->axes[i]; |
||
584 | |||
585 | return -!(analog[0].mask || analog[1].mask); |
||
586 | } |
||
587 | |||
588 | static int analog_init_port(struct gameport *gameport, struct gameport_dev *dev, struct analog_port *port) |
||
589 | { |
||
590 | int i, t, u, v; |
||
591 | |||
592 | gameport->private = port; |
||
593 | port->gameport = gameport; |
||
594 | init_timer(&port->timer); |
||
595 | port->timer.data = (long) port; |
||
596 | port->timer.function = analog_timer; |
||
597 | |||
598 | if (!gameport_open(gameport, dev, GAMEPORT_MODE_RAW)) { |
||
599 | |||
600 | analog_calibrate_timer(port); |
||
601 | |||
602 | gameport_trigger(gameport); |
||
603 | t = gameport_read(gameport); |
||
604 | wait_ms(ANALOG_MAX_TIME); |
||
605 | port->mask = (gameport_read(gameport) ^ t) & t & 0xf; |
||
606 | port->fuzz = (port->speed * ANALOG_FUZZ_MAGIC) / port->loop / 1000 + ANALOG_FUZZ_BITS; |
||
607 | |||
608 | for (i = 0; i < ANALOG_INIT_RETRIES; i++) { |
||
609 | if (!analog_cooked_read(port)) break; |
||
610 | wait_ms(ANALOG_MAX_TIME); |
||
611 | } |
||
612 | |||
613 | u = v = 0; |
||
614 | |||
615 | wait_ms(ANALOG_MAX_TIME); |
||
616 | t = gameport_time(gameport, ANALOG_MAX_TIME * 1000); |
||
617 | gameport_trigger(gameport); |
||
618 | while ((gameport_read(port->gameport) & port->mask) && (u < t)) u++; |
||
619 | udelay(ANALOG_SAITEK_DELAY); |
||
620 | t = gameport_time(gameport, ANALOG_SAITEK_TIME); |
||
621 | gameport_trigger(gameport); |
||
622 | while ((gameport_read(port->gameport) & port->mask) && (v < t)) v++; |
||
623 | |||
624 | if (v < (u >> 1)) { /* FIXME - more than one port */ |
||
625 | analog_options[0] |= /* FIXME - more than one port */ |
||
626 | ANALOG_SAITEK | ANALOG_BTNS_CHF | ANALOG_HBTN_CHF | ANALOG_HAT1_CHF; |
||
627 | return 0; |
||
628 | } |
||
629 | |||
630 | gameport_close(gameport); |
||
631 | } |
||
632 | |||
633 | if (!gameport_open(gameport, dev, GAMEPORT_MODE_COOKED)) { |
||
634 | |||
635 | for (i = 0; i < ANALOG_INIT_RETRIES; i++) |
||
636 | if (!gameport_cooked_read(gameport, port->axes, &port->buttons)) |
||
637 | break; |
||
638 | for (i = 0; i < 4; i++) |
||
639 | if (port->axes[i] != -1) port->mask |= 1 << i; |
||
640 | |||
641 | port->fuzz = gameport->fuzz; |
||
642 | port->cooked = 1; |
||
643 | return 0; |
||
644 | } |
||
645 | |||
646 | if (!gameport_open(gameport, dev, GAMEPORT_MODE_RAW)) |
||
647 | return 0; |
||
648 | |||
649 | return -1; |
||
650 | } |
||
651 | |||
652 | static void analog_connect(struct gameport *gameport, struct gameport_dev *dev) |
||
653 | { |
||
654 | struct analog_port *port; |
||
655 | int i; |
||
656 | |||
657 | if (!(port = kmalloc(sizeof(struct analog_port), GFP_KERNEL))) |
||
658 | return; |
||
659 | memset(port, 0, sizeof(struct analog_port)); |
||
660 | |||
661 | if (analog_init_port(gameport, dev, port)) { |
||
662 | kfree(port); |
||
663 | return; |
||
664 | } |
||
665 | |||
666 | if (analog_init_masks(port)) { |
||
667 | gameport_close(gameport); |
||
668 | kfree(port); |
||
669 | return; |
||
670 | } |
||
671 | |||
672 | for (i = 0; i < 2; i++) |
||
673 | if (port->analog[i].mask) |
||
674 | analog_init_device(port, port->analog + i, i); |
||
675 | } |
||
676 | |||
677 | static void analog_disconnect(struct gameport *gameport) |
||
678 | { |
||
679 | int i; |
||
680 | |||
681 | struct analog_port *port = gameport->private; |
||
682 | for (i = 0; i < 2; i++) |
||
683 | if (port->analog[i].mask) |
||
684 | input_unregister_device(&port->analog[i].dev); |
||
685 | gameport_close(gameport); |
||
686 | printk(KERN_INFO "analog.c: %d out of %d reads (%d%%) on %s failed\n", |
||
687 | port->bads, port->reads, port->reads ? (port->bads * 100 / port->reads) : 0, |
||
688 | port->gameport->phys); |
||
689 | kfree(port); |
||
690 | } |
||
691 | |||
692 | struct analog_types { |
||
693 | char *name; |
||
694 | int value; |
||
695 | }; |
||
696 | |||
697 | struct analog_types analog_types[] = { |
||
698 | { "none", 0x00000000 }, |
||
699 | { "auto", 0x000000ff }, |
||
700 | { "2btn", 0x0000003f }, |
||
701 | { "y-joy", 0x0cc00033 }, |
||
702 | { "y-pad", 0x8cc80033 }, |
||
703 | { "fcs", 0x000008f7 }, |
||
704 | { "chf", 0x000002ff }, |
||
705 | { "fullchf", 0x000007ff }, |
||
706 | { "gamepad", 0x000830f3 }, |
||
707 | { "gamepad8", 0x0008f0f3 }, |
||
708 | { NULL, 0 } |
||
709 | }; |
||
710 | |||
711 | static void analog_parse_options(void) |
||
712 | { |
||
713 | int i, j; |
||
714 | char *end; |
||
715 | |||
716 | for (i = 0; i < ANALOG_PORTS && js[i]; i++) { |
||
717 | |||
718 | for (j = 0; analog_types[j].name; j++) |
||
719 | if (!strcmp(analog_types[j].name, js[i])) { |
||
720 | analog_options[i] = analog_types[j].value; |
||
721 | break; |
||
722 | } |
||
723 | if (analog_types[j].name) continue; |
||
724 | |||
725 | analog_options[i] = simple_strtoul(js[i], &end, 0); |
||
726 | if (end != js[i]) continue; |
||
727 | |||
728 | analog_options[i] = 0xff; |
||
729 | if (!strlen(js[i])) continue; |
||
730 | |||
731 | printk(KERN_WARNING "analog.c: Bad config for port %d - \"%s\"\n", i, js[i]); |
||
732 | } |
||
733 | |||
734 | for (; i < ANALOG_PORTS; i++) |
||
735 | analog_options[i] = 0xff; |
||
736 | } |
||
737 | |||
738 | /* |
||
739 | * The gameport device structure. |
||
740 | */ |
||
741 | |||
742 | static struct gameport_dev analog_dev = { |
||
743 | .connect = analog_connect, |
||
744 | .disconnect = analog_disconnect, |
||
745 | }; |
||
746 | |||
747 | #ifndef MODULE |
||
748 | static int __init analog_setup(char *str) |
||
749 | { |
||
750 | char *s = str; |
||
751 | int i = 0; |
||
752 | |||
753 | if (!str || !*str) return 0; |
||
754 | |||
755 | while ((str = s) && (i < ANALOG_PORTS)) { |
||
756 | if ((s = strchr(str,','))) *s++ = 0; |
||
757 | js[i++] = str; |
||
758 | } |
||
759 | |||
760 | return 1; |
||
761 | } |
||
762 | __setup("js=", analog_setup); |
||
763 | #endif |
||
764 | |||
765 | int __init analog_init(void) |
||
766 | { |
||
767 | analog_parse_options(); |
||
768 | gameport_register_device(&analog_dev); |
||
769 | return 0; |
||
770 | } |
||
771 | |||
772 | void __exit analog_exit(void) |
||
773 | { |
||
774 | gameport_unregister_device(&analog_dev); |
||
775 | } |
||
776 | |||
777 | module_init(analog_init); |
||
778 | module_exit(analog_exit); |