Rev 538 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
494 | giacomo | 1 | /* |
547 | mauro | 2 | * $Id: analog.c,v 1.3 2004-03-31 11:27:13 mauro Exp $ |
494 | giacomo | 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 */ |
||
547 | mauro | 88 | #define ANALOG_LOOP_TIME 2 /* 2 * loop */ |
494 | giacomo | 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__ |
||
538 | mauro | 142 | /* !!! Added by Nino !!! */ |
547 | mauro | 143 | extern TIME sys_gettime(struct timespec *t); |
144 | #define GET_TIME(x) (x = sys_gettime(NULL)) |
||
538 | mauro | 145 | #define DELTA(x,y) ((y)-(x)) |
146 | #define TIME_NAME ("Shark") |
||
147 | /*#define GET_TIME(x) do { if (cpu_has_tsc) rdtscl(x); else x = get_time_pit(); } while (0) |
||
494 | giacomo | 148 | #define DELTA(x,y) (cpu_has_tsc?((y)-(x)):((x)-(y)+((x)<(y)?1193182L/HZ:0))) |
547 | mauro | 149 | #define TIME_NAME (cpu_has_tsc?"TSC":"PIT") |
494 | giacomo | 150 | static unsigned int get_time_pit(void) |
151 | { |
||
152 | extern spinlock_t i8253_lock; |
||
153 | unsigned long flags; |
||
154 | unsigned int count; |
||
155 | |||
156 | spin_lock_irqsave(&i8253_lock, flags); |
||
157 | outb_p(0x00, 0x43); |
||
158 | count = inb_p(0x40); |
||
159 | count |= inb_p(0x40) << 8; |
||
160 | spin_unlock_irqrestore(&i8253_lock, flags); |
||
161 | |||
162 | return count; |
||
547 | mauro | 163 | }*/ |
494 | giacomo | 164 | #elif __x86_64__ |
165 | #define GET_TIME(x) rdtscl(x) |
||
166 | #define DELTA(x,y) ((y)-(x)) |
||
167 | #define TIME_NAME "TSC" |
||
168 | #elif __alpha__ |
||
169 | #define GET_TIME(x) do { x = get_cycles(); } while (0) |
||
170 | #define DELTA(x,y) ((y)-(x)) |
||
171 | #define TIME_NAME "PCC" |
||
172 | #else |
||
173 | #define FAKE_TIME |
||
174 | static unsigned long analog_faketime = 0; |
||
175 | #define GET_TIME(x) do { x = analog_faketime++; } while(0) |
||
176 | #define DELTA(x,y) ((y)-(x)) |
||
177 | #define TIME_NAME "Unreliable" |
||
178 | #warning Precise timer not defined for this architecture. |
||
179 | #endif |
||
180 | |||
181 | /* |
||
182 | * analog_decode() decodes analog joystick data and reports input events. |
||
183 | */ |
||
184 | |||
185 | static void analog_decode(struct analog *analog, int *axes, int *initial, int buttons) |
||
186 | { |
||
187 | struct input_dev *dev = &analog->dev; |
||
188 | int i, j; |
||
189 | |||
190 | if (analog->mask & ANALOG_HAT_FCS) |
||
191 | for (i = 0; i < 4; i++) |
||
192 | if (axes[3] < ((initial[3] * ((i << 1) + 1)) >> 3)) { |
||
193 | buttons |= 1 << (i + 14); |
||
194 | break; |
||
195 | } |
||
196 | |||
197 | for (i = j = 0; i < 6; i++) |
||
198 | if (analog->mask & (0x10 << i)) |
||
199 | input_report_key(dev, analog->buttons[j++], (buttons >> i) & 1); |
||
200 | |||
201 | if (analog->mask & ANALOG_HBTN_CHF) |
||
202 | for (i = 0; i < 4; i++) |
||
203 | input_report_key(dev, analog->buttons[j++], (buttons >> (i + 10)) & 1); |
||
204 | |||
205 | if (analog->mask & ANALOG_BTN_TL) |
||
206 | input_report_key(dev, analog_pads[0], axes[2] < (initial[2] >> 1)); |
||
207 | if (analog->mask & ANALOG_BTN_TR) |
||
208 | input_report_key(dev, analog_pads[1], axes[3] < (initial[3] >> 1)); |
||
209 | if (analog->mask & ANALOG_BTN_TL2) |
||
210 | input_report_key(dev, analog_pads[2], axes[2] > (initial[2] + (initial[2] >> 1))); |
||
211 | if (analog->mask & ANALOG_BTN_TR2) |
||
212 | input_report_key(dev, analog_pads[3], axes[3] > (initial[3] + (initial[3] >> 1))); |
||
213 | |||
214 | for (i = j = 0; i < 4; i++) |
||
215 | if (analog->mask & (1 << i)) |
||
216 | input_report_abs(dev, analog_axes[j++], axes[i]); |
||
217 | |||
218 | for (i = j = 0; i < 3; i++) |
||
219 | if (analog->mask & analog_exts[i]) { |
||
220 | input_report_abs(dev, analog_hats[j++], |
||
221 | ((buttons >> ((i << 2) + 7)) & 1) - ((buttons >> ((i << 2) + 9)) & 1)); |
||
222 | input_report_abs(dev, analog_hats[j++], |
||
223 | ((buttons >> ((i << 2) + 8)) & 1) - ((buttons >> ((i << 2) + 6)) & 1)); |
||
224 | } |
||
225 | |||
226 | input_sync(dev); |
||
227 | } |
||
228 | |||
229 | /* |
||
230 | * analog_cooked_read() reads analog joystick data. |
||
231 | */ |
||
232 | |||
233 | static int analog_cooked_read(struct analog_port *port) |
||
234 | { |
||
235 | struct gameport *gameport = port->gameport; |
||
236 | unsigned int time[4], start, loop, now, loopout, timeout; |
||
237 | unsigned char data[4], this, last; |
||
238 | unsigned long flags; |
||
239 | int i, j; |
||
240 | |||
547 | mauro | 241 | loopout = ANALOG_LOOP_TIME * port->loop; |
494 | giacomo | 242 | timeout = ANALOG_MAX_TIME * port->speed; |
243 | |||
244 | local_irq_save(flags); |
||
245 | gameport_trigger(gameport); |
||
246 | GET_TIME(now); |
||
247 | local_irq_restore(flags); |
||
248 | |||
249 | start = now; |
||
250 | this = port->mask; |
||
251 | i = 0; |
||
252 | |||
253 | do { |
||
254 | loop = now; |
||
255 | last = this; |
||
256 | |||
257 | local_irq_disable(); |
||
258 | this = gameport_read(gameport) & port->mask; |
||
259 | GET_TIME(now); |
||
260 | local_irq_restore(flags); |
||
261 | |||
262 | if ((last ^ this) && (DELTA(loop, now) < loopout)) { |
||
263 | data[i] = last ^ this; |
||
264 | time[i] = now; |
||
265 | i++; |
||
266 | } |
||
267 | |||
268 | } while (this && (i < 4) && (DELTA(start, now) < timeout)); |
||
269 | |||
270 | this <<= 4; |
||
271 | |||
272 | for (--i; i >= 0; i--) { |
||
273 | this |= data[i]; |
||
274 | for (j = 0; j < 4; j++) |
||
275 | if (data[i] & (1 << j)) |
||
276 | port->axes[j] = (DELTA(start, time[i]) << ANALOG_FUZZ_BITS) / port->loop; |
||
277 | } |
||
278 | |||
279 | return -(this != port->mask); |
||
280 | } |
||
281 | |||
282 | static int analog_button_read(struct analog_port *port, char saitek, char chf) |
||
283 | { |
||
284 | unsigned char u; |
||
285 | int t = 1, i = 0; |
||
286 | int strobe = gameport_time(port->gameport, ANALOG_SAITEK_TIME); |
||
287 | |||
288 | u = gameport_read(port->gameport); |
||
289 | |||
290 | if (!chf) { |
||
291 | port->buttons = (~u >> 4) & 0xf; |
||
292 | return 0; |
||
293 | } |
||
294 | |||
295 | port->buttons = 0; |
||
296 | |||
297 | while ((~u & 0xf0) && (i < 16) && t) { |
||
298 | port->buttons |= 1 << analog_chf[(~u >> 4) & 0xf]; |
||
299 | if (!saitek) return 0; |
||
300 | udelay(ANALOG_SAITEK_DELAY); |
||
301 | t = strobe; |
||
302 | gameport_trigger(port->gameport); |
||
303 | while (((u = gameport_read(port->gameport)) & port->mask) && t) t--; |
||
304 | i++; |
||
305 | } |
||
306 | |||
307 | return -(!t || (i == 16)); |
||
308 | } |
||
309 | |||
310 | /* |
||
311 | * analog_timer() repeatedly polls the Analog joysticks. |
||
312 | */ |
||
313 | |||
314 | static void analog_timer(unsigned long data) |
||
315 | { |
||
316 | struct analog_port *port = (void *) data; |
||
317 | int i; |
||
318 | |||
319 | char saitek = !!(port->analog[0].mask & ANALOG_SAITEK); |
||
320 | char chf = !!(port->analog[0].mask & ANALOG_ANY_CHF); |
||
321 | |||
322 | if (port->cooked) { |
||
323 | port->bads -= gameport_cooked_read(port->gameport, port->axes, &port->buttons); |
||
324 | if (chf) |
||
325 | port->buttons = port->buttons ? (1 << analog_chf[port->buttons]) : 0; |
||
326 | port->reads++; |
||
327 | } else { |
||
328 | if (!port->axtime--) { |
||
329 | port->bads -= analog_cooked_read(port); |
||
330 | port->bads -= analog_button_read(port, saitek, chf); |
||
331 | port->reads++; |
||
332 | port->axtime = ANALOG_AXIS_TIME - 1; |
||
333 | } else { |
||
334 | if (!saitek) |
||
335 | analog_button_read(port, saitek, chf); |
||
336 | } |
||
337 | } |
||
338 | |||
339 | for (i = 0; i < 2; i++) |
||
340 | if (port->analog[i].mask) |
||
341 | analog_decode(port->analog + i, port->axes, port->initial, port->buttons); |
||
342 | |||
538 | mauro | 343 | mod_timer(&port->timer, jiffies26 + ANALOG_REFRESH_TIME); |
494 | giacomo | 344 | } |
345 | |||
346 | /* |
||
347 | * analog_open() is a callback from the input open routine. |
||
348 | */ |
||
349 | |||
350 | static int analog_open(struct input_dev *dev) |
||
351 | { |
||
352 | struct analog_port *port = dev->private; |
||
353 | if (!port->used++) |
||
538 | mauro | 354 | mod_timer(&port->timer, jiffies26 + ANALOG_REFRESH_TIME); |
494 | giacomo | 355 | return 0; |
356 | } |
||
357 | |||
358 | /* |
||
359 | * analog_close() is a callback from the input close routine. |
||
360 | */ |
||
361 | |||
362 | static void analog_close(struct input_dev *dev) |
||
363 | { |
||
364 | struct analog_port *port = dev->private; |
||
365 | if (!--port->used) |
||
366 | del_timer(&port->timer); |
||
367 | } |
||
368 | |||
369 | /* |
||
370 | * analog_calibrate_timer() calibrates the timer and computes loop |
||
371 | * and timeout values for a joystick port. |
||
372 | */ |
||
373 | |||
374 | static void analog_calibrate_timer(struct analog_port *port) |
||
375 | { |
||
376 | struct gameport *gameport = port->gameport; |
||
377 | unsigned int i, t, tx, t1, t2, t3; |
||
378 | unsigned long flags; |
||
379 | |||
547 | mauro | 380 | /*local_irq_save(flags); |
494 | giacomo | 381 | GET_TIME(t1); |
382 | #ifdef FAKE_TIME |
||
383 | analog_faketime += 830; |
||
384 | #endif |
||
385 | udelay(1000); |
||
386 | GET_TIME(t2); |
||
387 | GET_TIME(t3); |
||
388 | local_irq_restore(flags); |
||
389 | |||
547 | mauro | 390 | port->speed = DELTA(t1, t2) - DELTA(t2, t3);*/ |
391 | |||
392 | /* !!! Added by Nino !!! */ |
||
393 | port->speed = 1000; |
||
494 | giacomo | 394 | |
395 | tx = ~0; |
||
396 | |||
397 | for (i = 0; i < 50; i++) { |
||
398 | local_irq_save(flags); |
||
399 | GET_TIME(t1); |
||
400 | for (t = 0; t < 50; t++) { gameport_read(gameport); GET_TIME(t2); } |
||
401 | GET_TIME(t3); |
||
402 | local_irq_restore(flags); |
||
403 | udelay(i); |
||
404 | t = DELTA(t1, t2) - DELTA(t2, t3); |
||
405 | if (t < tx) tx = t; |
||
406 | } |
||
407 | |||
547 | mauro | 408 | port->loop = tx / 5; /* !!! In Linux is set to 50 !!! */ |
494 | giacomo | 409 | } |
410 | |||
411 | /* |
||
412 | * analog_name() constructs a name for an analog joystick. |
||
413 | */ |
||
414 | |||
415 | static void analog_name(struct analog *analog) |
||
416 | { |
||
417 | sprintf26(analog->name, "Analog %d-axis %d-button", |
||
418 | hweight8(analog->mask & ANALOG_AXES_STD), |
||
419 | hweight8(analog->mask & ANALOG_BTNS_STD) + !!(analog->mask & ANALOG_BTNS_CHF) * 2 + |
||
420 | hweight16(analog->mask & ANALOG_BTNS_GAMEPAD) + !!(analog->mask & ANALOG_HBTN_CHF) * 4); |
||
421 | |||
422 | if (analog->mask & ANALOG_HATS_ALL) |
||
423 | sprintf26(analog->name, "%s %d-hat", |
||
424 | analog->name, hweight16(analog->mask & ANALOG_HATS_ALL)); |
||
425 | |||
426 | if (analog->mask & ANALOG_HAT_FCS) |
||
427 | strcat(analog->name, " FCS"); |
||
428 | if (analog->mask & ANALOG_ANY_CHF) |
||
429 | strcat(analog->name, (analog->mask & ANALOG_SAITEK) ? " Saitek" : " CHF"); |
||
430 | |||
431 | strcat(analog->name, (analog->mask & ANALOG_GAMEPAD) ? " gamepad": " joystick"); |
||
432 | } |
||
433 | |||
434 | /* |
||
435 | * analog_init_device() |
||
436 | */ |
||
437 | |||
438 | static void analog_init_device(struct analog_port *port, struct analog *analog, int index) |
||
439 | { |
||
440 | int i, j, t, v, w, x, y, z; |
||
441 | |||
442 | analog_name(analog); |
||
443 | sprintf26(analog->phys, "%s/input%d", port->gameport->phys, index); |
||
444 | analog->buttons = (analog->mask & ANALOG_GAMEPAD) ? analog_pad_btn : analog_joy_btn; |
||
445 | |||
446 | init_input_dev(&analog->dev); |
||
447 | |||
448 | analog->dev.name = analog->name; |
||
449 | analog->dev.phys = analog->phys; |
||
450 | analog->dev.id.bustype = BUS_GAMEPORT; |
||
451 | analog->dev.id.vendor = GAMEPORT_ID_VENDOR_ANALOG; |
||
452 | analog->dev.id.product = analog->mask >> 4; |
||
453 | analog->dev.id.version = 0x0100; |
||
454 | |||
455 | analog->dev.open = analog_open; |
||
456 | analog->dev.close = analog_close; |
||
457 | analog->dev.private = port; |
||
458 | analog->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS); |
||
459 | |||
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 | |||
471 | set_bit(t, analog->dev.absbit); |
||
472 | |||
473 | if ((i == 2 || i == 3) && (j == 2 || j == 3) && (z > (y >> 3))) |
||
474 | x = y; |
||
475 | |||
476 | if (analog->mask & ANALOG_SAITEK) { |
||
477 | if (i == 2) x = port->axes[i]; |
||
478 | v = x - (x >> 2); |
||
479 | w = (x >> 4); |
||
480 | } |
||
481 | |||
482 | analog->dev.absmax[t] = (x << 1) - v; |
||
483 | analog->dev.absmin[t] = v; |
||
484 | analog->dev.absfuzz[t] = port->fuzz; |
||
485 | analog->dev.absflat[t] = w; |
||
486 | |||
487 | j++; |
||
488 | } |
||
489 | |||
490 | for (i = j = 0; i < 3; i++) |
||
491 | if (analog->mask & analog_exts[i]) |
||
492 | for (x = 0; x < 2; x++) { |
||
493 | t = analog_hats[j++]; |
||
494 | set_bit(t, analog->dev.absbit); |
||
495 | analog->dev.absmax[t] = 1; |
||
496 | analog->dev.absmin[t] = -1; |
||
497 | } |
||
498 | |||
499 | for (i = j = 0; i < 4; i++) |
||
500 | if (analog->mask & (0x10 << i)) |
||
501 | set_bit(analog->buttons[j++], analog->dev.keybit); |
||
502 | |||
503 | if (analog->mask & ANALOG_BTNS_CHF) |
||
504 | for (i = 0; i < 2; i++) |
||
505 | set_bit(analog->buttons[j++], analog->dev.keybit); |
||
506 | |||
507 | if (analog->mask & ANALOG_HBTN_CHF) |
||
508 | for (i = 0; i < 4; i++) |
||
509 | set_bit(analog->buttons[j++], analog->dev.keybit); |
||
510 | |||
511 | for (i = 0; i < 4; i++) |
||
512 | if (analog->mask & (ANALOG_BTN_TL << i)) |
||
513 | set_bit(analog_pads[i], analog->dev.keybit); |
||
514 | |||
515 | analog_decode(analog, port->axes, port->initial, port->buttons); |
||
516 | |||
517 | input_register_device(&analog->dev); |
||
518 | |||
519 | printk(KERN_INFO "input: %s at %s", analog->name, port->gameport->phys); |
||
520 | |||
521 | if (port->cooked) |
||
522 | printk(" [ADC port]\n"); |
||
523 | else |
||
524 | printk(" [%s timer, %d %sHz clock, %d ns res]\n", TIME_NAME, |
||
525 | port->speed > 10000 ? (port->speed + 800) / 1000 : port->speed, |
||
526 | port->speed > 10000 ? "M" : "k", |
||
527 | port->speed > 10000 ? (port->loop * 1000) / (port->speed / 1000) |
||
528 | : (port->loop * 1000000) / port->speed); |
||
529 | } |
||
530 | |||
531 | /* |
||
532 | * analog_init_devices() sets up device-specific values and registers the input devices. |
||
533 | */ |
||
534 | |||
535 | static int analog_init_masks(struct analog_port *port) |
||
536 | { |
||
537 | int i; |
||
538 | struct analog *analog = port->analog; |
||
539 | int max[4]; |
||
540 | |||
541 | if (!port->mask) |
||
542 | return -1; |
||
543 | |||
544 | if ((port->mask & 3) != 3 && port->mask != 0xc) { |
||
545 | printk(KERN_WARNING "analog.c: Unknown joystick device found " |
||
546 | "(data=%#x, %s), probably not analog joystick.\n", |
||
547 | port->mask, port->gameport->phys); |
||
548 | return -1; |
||
549 | } |
||
550 | |||
551 | |||
552 | i = analog_options[0]; /* FIXME !!! - need to specify options for different ports */ |
||
553 | |||
554 | analog[0].mask = i & 0xfffff; |
||
555 | |||
556 | analog[0].mask &= ~(ANALOG_AXES_STD | ANALOG_HAT_FCS | ANALOG_BTNS_GAMEPAD) |
||
557 | | port->mask | ((port->mask << 8) & ANALOG_HAT_FCS) |
||
558 | | ((port->mask << 10) & ANALOG_BTNS_TLR) | ((port->mask << 12) & ANALOG_BTNS_TLR2); |
||
559 | |||
560 | analog[0].mask &= ~(ANALOG_HAT2_CHF) |
||
561 | | ((analog[0].mask & ANALOG_HBTN_CHF) ? 0 : ANALOG_HAT2_CHF); |
||
562 | |||
563 | analog[0].mask &= ~(ANALOG_THROTTLE | ANALOG_BTN_TR | ANALOG_BTN_TR2) |
||
564 | | ((~analog[0].mask & ANALOG_HAT_FCS) >> 8) |
||
565 | | ((~analog[0].mask & ANALOG_HAT_FCS) << 2) |
||
566 | | ((~analog[0].mask & ANALOG_HAT_FCS) << 4); |
||
567 | |||
568 | analog[0].mask &= ~(ANALOG_THROTTLE | ANALOG_RUDDER) |
||
569 | | (((~analog[0].mask & ANALOG_BTNS_TLR ) >> 10) |
||
570 | & ((~analog[0].mask & ANALOG_BTNS_TLR2) >> 12)); |
||
571 | |||
572 | analog[1].mask = ((i >> 20) & 0xff) | ((i >> 12) & 0xf0000); |
||
573 | |||
574 | analog[1].mask &= (analog[0].mask & ANALOG_EXTENSIONS) ? ANALOG_GAMEPAD |
||
575 | : (((ANALOG_BTNS_STD | port->mask) & ~analog[0].mask) | ANALOG_GAMEPAD); |
||
576 | |||
577 | if (port->cooked) { |
||
578 | |||
579 | for (i = 0; i < 4; i++) max[i] = port->axes[i] << 1; |
||
580 | |||
581 | if ((analog[0].mask & 0x7) == 0x7) max[2] = (max[0] + max[1]) >> 1; |
||
582 | if ((analog[0].mask & 0xb) == 0xb) max[3] = (max[0] + max[1]) >> 1; |
||
583 | if ((analog[0].mask & ANALOG_BTN_TL) && !(analog[0].mask & ANALOG_BTN_TL2)) max[2] >>= 1; |
||
584 | if ((analog[0].mask & ANALOG_BTN_TR) && !(analog[0].mask & ANALOG_BTN_TR2)) max[3] >>= 1; |
||
585 | if ((analog[0].mask & ANALOG_HAT_FCS)) max[3] >>= 1; |
||
586 | |||
587 | gameport_calibrate(port->gameport, port->axes, max); |
||
588 | } |
||
589 | |||
590 | for (i = 0; i < 4; i++) |
||
591 | port->initial[i] = port->axes[i]; |
||
592 | |||
593 | return -!(analog[0].mask || analog[1].mask); |
||
594 | } |
||
595 | |||
596 | static int analog_init_port(struct gameport *gameport, struct gameport_dev *dev, struct analog_port *port) |
||
597 | { |
||
598 | int i, t, u, v; |
||
599 | |||
600 | gameport->private = port; |
||
601 | port->gameport = gameport; |
||
602 | init_timer(&port->timer); |
||
603 | port->timer.data = (long) port; |
||
604 | port->timer.function = analog_timer; |
||
605 | |||
606 | if (!gameport_open(gameport, dev, GAMEPORT_MODE_RAW)) { |
||
607 | |||
608 | analog_calibrate_timer(port); |
||
609 | |||
610 | gameport_trigger(gameport); |
||
611 | t = gameport_read(gameport); |
||
612 | wait_ms(ANALOG_MAX_TIME); |
||
613 | port->mask = (gameport_read(gameport) ^ t) & t & 0xf; |
||
614 | port->fuzz = (port->speed * ANALOG_FUZZ_MAGIC) / port->loop / 1000 + ANALOG_FUZZ_BITS; |
||
615 | |||
616 | for (i = 0; i < ANALOG_INIT_RETRIES; i++) { |
||
617 | if (!analog_cooked_read(port)) break; |
||
618 | wait_ms(ANALOG_MAX_TIME); |
||
619 | } |
||
620 | |||
621 | u = v = 0; |
||
622 | |||
623 | wait_ms(ANALOG_MAX_TIME); |
||
624 | t = gameport_time(gameport, ANALOG_MAX_TIME * 1000); |
||
625 | gameport_trigger(gameport); |
||
626 | while ((gameport_read(port->gameport) & port->mask) && (u < t)) u++; |
||
627 | udelay(ANALOG_SAITEK_DELAY); |
||
628 | t = gameport_time(gameport, ANALOG_SAITEK_TIME); |
||
629 | gameport_trigger(gameport); |
||
630 | while ((gameport_read(port->gameport) & port->mask) && (v < t)) v++; |
||
631 | |||
632 | if (v < (u >> 1)) { /* FIXME - more than one port */ |
||
633 | analog_options[0] |= /* FIXME - more than one port */ |
||
634 | ANALOG_SAITEK | ANALOG_BTNS_CHF | ANALOG_HBTN_CHF | ANALOG_HAT1_CHF; |
||
635 | return 0; |
||
636 | } |
||
637 | |||
638 | gameport_close(gameport); |
||
639 | } |
||
640 | |||
641 | if (!gameport_open(gameport, dev, GAMEPORT_MODE_COOKED)) { |
||
642 | |||
643 | for (i = 0; i < ANALOG_INIT_RETRIES; i++) |
||
644 | if (!gameport_cooked_read(gameport, port->axes, &port->buttons)) |
||
645 | break; |
||
646 | for (i = 0; i < 4; i++) |
||
647 | if (port->axes[i] != -1) port->mask |= 1 << i; |
||
648 | |||
649 | port->fuzz = gameport->fuzz; |
||
650 | port->cooked = 1; |
||
651 | return 0; |
||
652 | } |
||
653 | |||
654 | if (!gameport_open(gameport, dev, GAMEPORT_MODE_RAW)) |
||
655 | return 0; |
||
656 | |||
657 | return -1; |
||
658 | } |
||
659 | |||
660 | static void analog_connect(struct gameport *gameport, struct gameport_dev *dev) |
||
661 | { |
||
662 | struct analog_port *port; |
||
663 | int i; |
||
664 | |||
665 | if (!(port = kmalloc(sizeof(struct analog_port), GFP_KERNEL))) |
||
666 | return; |
||
667 | memset(port, 0, sizeof(struct analog_port)); |
||
668 | |||
669 | if (analog_init_port(gameport, dev, port)) { |
||
670 | kfree(port); |
||
671 | return; |
||
672 | } |
||
673 | |||
674 | if (analog_init_masks(port)) { |
||
675 | gameport_close(gameport); |
||
676 | kfree(port); |
||
677 | return; |
||
678 | } |
||
679 | |||
680 | for (i = 0; i < 2; i++) |
||
681 | if (port->analog[i].mask) |
||
682 | analog_init_device(port, port->analog + i, i); |
||
683 | } |
||
684 | |||
685 | static void analog_disconnect(struct gameport *gameport) |
||
686 | { |
||
687 | int i; |
||
688 | |||
689 | struct analog_port *port = gameport->private; |
||
690 | for (i = 0; i < 2; i++) |
||
691 | if (port->analog[i].mask) |
||
692 | input_unregister_device(&port->analog[i].dev); |
||
693 | gameport_close(gameport); |
||
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 | |||
700 | struct analog_types { |
||
701 | char *name; |
||
702 | int value; |
||
703 | }; |
||
704 | |||
705 | 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 | |||
719 | static void analog_parse_options(void) |
||
720 | { |
||
721 | int i, j; |
||
722 | char *end; |
||
723 | |||
724 | for (i = 0; i < ANALOG_PORTS && js[i]; 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 | |||
750 | static struct gameport_dev analog_dev = { |
||
751 | .connect = analog_connect, |
||
752 | .disconnect = analog_disconnect, |
||
753 | }; |
||
754 | |||
755 | #ifndef MODULE |
||
756 | static int __init analog_setup(char *str) |
||
757 | { |
||
758 | char *s = str; |
||
759 | int i = 0; |
||
760 | |||
761 | if (!str || !*str) return 0; |
||
762 | |||
763 | while ((str = s) && (i < ANALOG_PORTS)) { |
||
764 | if ((s = strchr(str,','))) *s++ = 0; |
||
765 | js[i++] = str; |
||
766 | } |
||
767 | |||
768 | return 1; |
||
769 | } |
||
770 | __setup("js=", analog_setup); |
||
771 | #endif |
||
772 | |||
773 | int __init analog_init(void) |
||
774 | { |
||
775 | analog_parse_options(); |
||
776 | gameport_register_device(&analog_dev); |
||
777 | return 0; |
||
778 | } |
||
779 | |||
780 | void __exit analog_exit(void) |
||
781 | { |
||
782 | gameport_unregister_device(&analog_dev); |
||
783 | } |
||
784 | |||
785 | module_init(analog_init); |
||
786 | module_exit(analog_exit); |