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