File: /usr/src/linux/drivers/char/joystick/analog.c
1 /*
2 * $Id: analog.c,v 1.52 2000/06/07 13:07:06 vojtech Exp $
3 *
4 * Copyright (c) 1996-2000 Vojtech Pavlik
5 *
6 * Sponsored by SuSE
7 */
8
9 /*
10 * Analog joystick and gamepad driver for Linux
11 */
12
13 /*
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 * Should you need to contact me, the author, you can do so either by
29 * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
30 * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
31 */
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@suse.cz>");
45 MODULE_DESCRIPTION("Analog joystick and gamepad driver for Linux");
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
99 static short analog_axes[] = { ABS_X, ABS_Y, ABS_RUDDER, ABS_THROTTLE };
100 static short analog_hats[] = { ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y };
101 static short analog_pads[] = { BTN_Y, BTN_Z, BTN_TL, BTN_TR };
102 static short analog_exts[] = { ANALOG_HAT1_CHF, ANALOG_HAT2_CHF, ANALOG_HAT_FCS };
103 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 };
104 static short analog_joy_btn[] = { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2,
105 BTN_BASE3, BTN_BASE4, BTN_BASE5, BTN_BASE6 };
106
107 static unsigned char analog_chf[] = { 0xf, 0x0, 0x1, 0x9, 0x2, 0x4, 0xc, 0x8, 0x3, 0x5, 0xb, 0x7, 0xd, 0xe, 0xa, 0x6 };
108
109 struct analog {
110 struct input_dev dev;
111 int mask;
112 short *buttons;
113 char name[ANALOG_MAX_NAME_LENGTH];
114 };
115
116 struct analog_port {
117 struct gameport *gameport;
118 struct timer_list timer;
119 struct analog analog[2];
120 unsigned char mask;
121 char saitek;
122 char cooked;
123 int bads;
124 int reads;
125 int speed;
126 int loop;
127 int fuzz;
128 int axes[4];
129 int buttons;
130 int initial[4];
131 int used;
132 int axtime;
133 };
134
135 /*
136 * Time macros.
137 */
138
139 #ifdef __i386__
140 #define TSC_PRESENT (test_bit(X86_FEATURE_TSC, &boot_cpu_data.x86_capability))
141 #define GET_TIME(x) do { if (TSC_PRESENT) rdtscl(x); else outb(0, 0x43); x = inb(0x40); x |= inb(0x40) << 8; } while (0)
142 #define DELTA(x,y) (TSC_PRESENT?((y)-(x)):((x)-(y)+((x)<(y)?1193180L/HZ:0)))
143 #define TIME_NAME (TSC_PRESENT?"TSC":"PIT")
144 #elif __x86_64__
145 #define GET_TIME(x) rdtscl(x)
146 #define DELTA(x,y) ((y)-(x))
147 #define TIME_NAME "TSC"
148 #elif __alpha__
149 #define GET_TIME(x) get_cycles(x)
150 #define DELTA(x,y) ((y)-(x))
151 #define TIME_NAME "PCC"
152 #else
153 #define FAKE_TIME
154 static unsigned long analog_faketime = 0;
155 #define GET_TIME(x) do { x = analog_faketime++; } while(0)
156 #define DELTA(x,y) ((y)-(x))
157 #define TIME_NAME "Unreliable"
158 #warning Precise timer not defined for this architecture.
159 #endif
160
161 /*
162 * analog_decode() decodes analog joystick data and reports input events.
163 */
164
165 static void analog_decode(struct analog *analog, int *axes, int *initial, int buttons)
166 {
167 struct input_dev *dev = &analog->dev;
168 int i, j;
169
170 if (analog->mask & ANALOG_HAT_FCS)
171 for (i = 0; i < 4; i++)
172 if (axes[3] < ((initial[3] * ((i << 1) + 1)) >> 3)) {
173 buttons |= 1 << (i + 14);
174 break;
175 }
176
177 for (i = j = 0; i < 6; i++)
178 if (analog->mask & (0x10 << i))
179 input_report_key(dev, analog->buttons[j++], (buttons >> i) & 1);
180
181 if (analog->mask & ANALOG_HBTN_CHF)
182 for (i = 0; i < 4; i++)
183 input_report_key(dev, analog->buttons[j++], (buttons >> (i + 10)) & 1);
184
185 if (analog->mask & ANALOG_BTN_TL)
186 input_report_key(dev, analog_pads[0], axes[2] < (initial[2] >> 1));
187 if (analog->mask & ANALOG_BTN_TR)
188 input_report_key(dev, analog_pads[1], axes[3] < (initial[3] >> 1));
189 if (analog->mask & ANALOG_BTN_TL2)
190 input_report_key(dev, analog_pads[2], axes[2] > (initial[2] + (initial[2] >> 1)));
191 if (analog->mask & ANALOG_BTN_TR2)
192 input_report_key(dev, analog_pads[3], axes[3] > (initial[3] + (initial[3] >> 1)));
193
194 for (i = j = 0; i < 4; i++)
195 if (analog->mask & (1 << i))
196 input_report_abs(dev, analog_axes[j++], axes[i]);
197
198 for (i = j = 0; i < 3; i++)
199 if (analog->mask & analog_exts[i]) {
200 input_report_abs(dev, analog_hats[j++],
201 ((buttons >> ((i << 2) + 7)) & 1) - ((buttons >> ((i << 2) + 9)) & 1));
202 input_report_abs(dev, analog_hats[j++],
203 ((buttons >> ((i << 2) + 8)) & 1) - ((buttons >> ((i << 2) + 6)) & 1));
204 }
205 }
206
207 /*
208 * analog_cooked_read() reads analog joystick data.
209 */
210
211 static int analog_cooked_read(struct analog_port *port)
212 {
213 struct gameport *gameport = port->gameport;
214 unsigned int time[4], start, loop, now, loopout, timeout;
215 unsigned char data[4], this, last;
216 unsigned long flags;
217 int i, j;
218
219 loopout = (ANALOG_LOOP_TIME * port->loop) / 1000;
220 timeout = ANALOG_MAX_TIME * port->speed;
221
222 __save_flags(flags);
223 __cli();
224 gameport_trigger(gameport);
225 GET_TIME(now);
226 __restore_flags(flags);
227
228 start = now;
229 this = port->mask;
230 i = 0;
231
232 do {
233 loop = now;
234 last = this;
235
236 __cli();
237 this = gameport_read(gameport) & port->mask;
238 GET_TIME(now);
239 __restore_flags(flags);
240
241 if ((last ^ this) && (DELTA(loop, now) < loopout)) {
242 data[i] = last ^ this;
243 time[i] = now;
244 i++;
245 }
246
247 } while (this && (i < 4) && (DELTA(start, now) < timeout));
248
249 this <<= 4;
250
251 for (--i; i >= 0; i--) {
252 this |= data[i];
253 for (j = 0; j < 4; j++)
254 if (data[i] & (1 << j))
255 port->axes[j] = (DELTA(start, time[i]) << ANALOG_FUZZ_BITS) / port->loop;
256 }
257
258 return -(this != port->mask);
259 }
260
261 static int analog_button_read(struct analog_port *port, char saitek, char chf)
262 {
263 unsigned char u;
264 int t = 1, i = 0;
265 int strobe = gameport_time(port->gameport, ANALOG_SAITEK_TIME);
266
267 u = gameport_read(port->gameport);
268
269 if (!chf) {
270 port->buttons = (~u >> 4) & 0xf;
271 return 0;
272 }
273
274 port->buttons = 0;
275
276 while ((~u & 0xf0) && (i < 16) && t) {
277 port->buttons |= 1 << analog_chf[(~u >> 4) & 0xf];
278 if (!saitek) return 0;
279 udelay(ANALOG_SAITEK_DELAY);
280 t = strobe;
281 gameport_trigger(port->gameport);
282 while (((u = gameport_read(port->gameport)) & port->mask) && t) t--;
283 i++;
284 }
285
286 return -(!t || (i == 16));
287 }
288
289 /*
290 * analog_timer() repeatedly polls the Analog joysticks.
291 */
292
293 static void analog_timer(unsigned long data)
294 {
295 struct analog_port *port = (void *) data;
296 int i;
297
298 char saitek = !!(port->analog[0].mask & ANALOG_SAITEK);
299 char chf = !!(port->analog[0].mask & ANALOG_ANY_CHF);
300
301 if (port->cooked) {
302 port->bads -= gameport_cooked_read(port->gameport, port->axes, &port->buttons);
303 if (chf)
304 port->buttons = port->buttons ? (1 << analog_chf[port->buttons]) : 0;
305 port->reads++;
306 } else {
307 if (!port->axtime--) {
308 port->bads -= analog_cooked_read(port);
309 port->bads -= analog_button_read(port, saitek, chf);
310 port->reads++;
311 port->axtime = ANALOG_AXIS_TIME - 1;
312 } else {
313 if (!saitek)
314 analog_button_read(port, saitek, chf);
315 }
316 }
317
318 for (i = 0; i < 2; i++)
319 if (port->analog[i].mask)
320 analog_decode(port->analog + i, port->axes, port->initial, port->buttons);
321
322 mod_timer(&port->timer, jiffies + ANALOG_REFRESH_TIME);
323 }
324
325 /*
326 * analog_open() is a callback from the input open routine.
327 */
328
329 static int analog_open(struct input_dev *dev)
330 {
331 struct analog_port *port = dev->private;
332 if (!port->used++)
333 mod_timer(&port->timer, jiffies + ANALOG_REFRESH_TIME);
334 return 0;
335 }
336
337 /*
338 * analog_close() is a callback from the input close routine.
339 */
340
341 static void analog_close(struct input_dev *dev)
342 {
343 struct analog_port *port = dev->private;
344 if (!--port->used)
345 del_timer(&port->timer);
346 }
347
348 /*
349 * analog_calibrate_timer() calibrates the timer and computes loop
350 * and timeout values for a joystick port.
351 */
352
353 static void analog_calibrate_timer(struct analog_port *port)
354 {
355 struct gameport *gameport = port->gameport;
356 unsigned int i, t, tx, t1, t2, t3;
357 unsigned long flags;
358
359 save_flags(flags);
360 cli();
361 GET_TIME(t1);
362 #ifdef FAKE_TIME
363 analog_faketime += 830;
364 #endif
365 udelay(1000);
366 GET_TIME(t2);
367 GET_TIME(t3);
368 restore_flags(flags);
369
370 port->speed = DELTA(t1, t2) - DELTA(t2, t3);
371
372 tx = ~0;
373
374 for (i = 0; i < 50; i++) {
375 save_flags(flags);
376 cli();
377 GET_TIME(t1);
378 for (t = 0; t < 50; t++) { gameport_read(gameport); GET_TIME(t2); }
379 GET_TIME(t3);
380 restore_flags(flags);
381 udelay(i);
382 t = DELTA(t1, t2) - DELTA(t2, t3);
383 if (t < tx) tx = t;
384 }
385
386 port->loop = tx / 50;
387 }
388
389 /*
390 * analog_name() constructs a name for an analog joystick.
391 */
392
393 static void analog_name(struct analog *analog)
394 {
395 sprintf(analog->name, "Analog %d-axis %d-button",
396 hweight8(analog->mask & ANALOG_AXES_STD),
397 hweight8(analog->mask & ANALOG_BTNS_STD) + !!(analog->mask & ANALOG_BTNS_CHF) * 2 +
398 hweight16(analog->mask & ANALOG_BTNS_GAMEPAD) + !!(analog->mask & ANALOG_HBTN_CHF) * 4);
399
400 if (analog->mask & ANALOG_HATS_ALL)
401 sprintf(analog->name, "%s %d-hat",
402 analog->name, hweight16(analog->mask & ANALOG_HATS_ALL));
403
404 if (analog->mask & ANALOG_HAT_FCS)
405 strcat(analog->name, " FCS");
406 if (analog->mask & ANALOG_ANY_CHF)
407 strcat(analog->name, (analog->mask & ANALOG_SAITEK) ? " Saitek" : " CHF");
408
409 strcat(analog->name, (analog->mask & ANALOG_GAMEPAD) ? " gamepad": " joystick");
410 }
411
412 /*
413 * analog_init_device()
414 */
415
416 static void analog_init_device(struct analog_port *port, struct analog *analog, int index)
417 {
418 int i, j, t, v, w, x, y, z;
419
420 analog_name(analog);
421
422 analog->buttons = (analog->mask & ANALOG_GAMEPAD) ? analog_pad_btn : analog_joy_btn;
423
424 analog->dev.name = analog->name;
425 analog->dev.idbus = BUS_GAMEPORT;
426 analog->dev.idvendor = GAMEPORT_ID_VENDOR_ANALOG;
427 analog->dev.idproduct = analog->mask >> 4;
428 analog->dev.idversion = 0x0100;
429
430 analog->dev.open = analog_open;
431 analog->dev.close = analog_close;
432 analog->dev.private = port;
433 analog->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
434
435 for (i = j = 0; i < 4; i++)
436 if (analog->mask & (1 << i)) {
437
438 t = analog_axes[j];
439 x = port->axes[i];
440 y = (port->axes[0] + port->axes[1]) >> 1;
441 z = y - port->axes[i];
442 z = z > 0 ? z : -z;
443 v = (x >> 3);
444 w = (x >> 3);
445
446 set_bit(t, analog->dev.absbit);
447
448 if ((i == 2 || i == 3) && (j == 2 || j == 3) && (z > (y >> 3)))
449 x = y;
450
451 if (analog->mask & ANALOG_SAITEK) {
452 if (i == 2) x = port->axes[i];
453 v = x - (x >> 2);
454 w = (x >> 4);
455 }
456
457 analog->dev.absmax[t] = (x << 1) - v;
458 analog->dev.absmin[t] = v;
459 analog->dev.absfuzz[t] = port->fuzz;
460 analog->dev.absflat[t] = w;
461
462 j++;
463 }
464
465 for (i = j = 0; i < 3; i++)
466 if (analog->mask & analog_exts[i])
467 for (x = 0; x < 2; x++) {
468 t = analog_hats[j++];
469 set_bit(t, analog->dev.absbit);
470 analog->dev.absmax[t] = 1;
471 analog->dev.absmin[t] = -1;
472 }
473
474 for (i = j = 0; i < 4; i++)
475 if (analog->mask & (0x10 << i))
476 set_bit(analog->buttons[j++], analog->dev.keybit);
477
478 if (analog->mask & ANALOG_BTNS_CHF)
479 for (i = 0; i < 2; i++)
480 set_bit(analog->buttons[j++], analog->dev.keybit);
481
482 if (analog->mask & ANALOG_HBTN_CHF)
483 for (i = 0; i < 4; i++)
484 set_bit(analog->buttons[j++], analog->dev.keybit);
485
486 for (i = 0; i < 4; i++)
487 if (analog->mask & (ANALOG_BTN_TL << i))
488 set_bit(analog_pads[i], analog->dev.keybit);
489
490 analog_decode(analog, port->axes, port->initial, port->buttons);
491
492 input_register_device(&analog->dev);
493
494 printk(KERN_INFO "input%d: %s at gameport%d.%d",
495 analog->dev.number, analog->name, port->gameport->number, index);
496
497 if (port->cooked)
498 printk(" [ADC port]\n");
499 else
500 printk(" [%s timer, %d %sHz clock, %d ns res]\n", TIME_NAME,
501 port->speed > 10000 ? (port->speed + 800) / 1000 : port->speed,
502 port->speed > 10000 ? "M" : "k", (port->loop * 1000000) / port->speed);
503 }
504
505 /*
506 * analog_init_devices() sets up device-specific values and registers the input devices.
507 */
508
509 static int analog_init_masks(struct analog_port *port)
510 {
511 int i;
512 struct analog *analog = port->analog;
513 int max[4];
514
515 if (!port->mask)
516 return -1;
517
518 if ((port->mask & 3) != 3 && port->mask != 0xc) {
519 printk(KERN_WARNING "analog.c: Unknown joystick device found "
520 "(data=%#x, gameport%d), probably not analog joystick.\n",
521 port->mask, port->gameport->number);
522 return -1;
523 }
524
525 i = port->gameport->number < ANALOG_PORTS ? analog_options[port->gameport->number] : 0xff;
526
527 analog[0].mask = i & 0xfffff;
528
529 analog[0].mask &= ~(ANALOG_AXES_STD | ANALOG_HAT_FCS | ANALOG_BTNS_GAMEPAD)
530 | port->mask | ((port->mask << 8) & ANALOG_HAT_FCS)
531 | ((port->mask << 10) & ANALOG_BTNS_TLR) | ((port->mask << 12) & ANALOG_BTNS_TLR2);
532
533 analog[0].mask &= ~(ANALOG_HAT2_CHF)
534 | ((analog[0].mask & ANALOG_HBTN_CHF) ? 0 : ANALOG_HAT2_CHF);
535
536 analog[0].mask &= ~(ANALOG_THROTTLE | ANALOG_BTN_TR | ANALOG_BTN_TR2)
537 | ((~analog[0].mask & ANALOG_HAT_FCS) >> 8)
538 | ((~analog[0].mask & ANALOG_HAT_FCS) << 2)
539 | ((~analog[0].mask & ANALOG_HAT_FCS) << 4);
540
541 analog[0].mask &= ~(ANALOG_THROTTLE | ANALOG_RUDDER)
542 | (((~analog[0].mask & ANALOG_BTNS_TLR ) >> 10)
543 & ((~analog[0].mask & ANALOG_BTNS_TLR2) >> 12));
544
545 analog[1].mask = ((i >> 20) & 0xff) | ((i >> 12) & 0xf0000);
546
547 analog[1].mask &= (analog[0].mask & ANALOG_EXTENSIONS) ? ANALOG_GAMEPAD
548 : (((ANALOG_BTNS_STD | port->mask) & ~analog[0].mask) | ANALOG_GAMEPAD);
549
550 if (port->cooked) {
551
552 for (i = 0; i < 4; i++) max[i] = port->axes[i] << 1;
553
554 if ((analog[0].mask & 0x7) == 0x7) max[2] = (max[0] + max[1]) >> 1;
555 if ((analog[0].mask & 0xb) == 0xb) max[3] = (max[0] + max[1]) >> 1;
556 if ((analog[0].mask & ANALOG_BTN_TL) && !(analog[0].mask & ANALOG_BTN_TL2)) max[2] >>= 1;
557 if ((analog[0].mask & ANALOG_BTN_TR) && !(analog[0].mask & ANALOG_BTN_TR2)) max[3] >>= 1;
558 if ((analog[0].mask & ANALOG_HAT_FCS)) max[3] >>= 1;
559
560 gameport_calibrate(port->gameport, port->axes, max);
561 }
562
563 for (i = 0; i < 4; i++)
564 port->initial[i] = port->axes[i];
565
566 return -!(analog[0].mask || analog[1].mask);
567 }
568
569 static int analog_init_port(struct gameport *gameport, struct gameport_dev *dev, struct analog_port *port)
570 {
571 int i, t, u, v;
572
573 gameport->private = port;
574 port->gameport = gameport;
575 init_timer(&port->timer);
576 port->timer.data = (long) port;
577 port->timer.function = analog_timer;
578
579 if (!gameport_open(gameport, dev, GAMEPORT_MODE_RAW)) {
580
581 analog_calibrate_timer(port);
582
583 gameport_trigger(gameport);
584 t = gameport_read(gameport);
585 wait_ms(ANALOG_MAX_TIME);
586 port->mask = (gameport_read(gameport) ^ t) & t & 0xf;
587 port->fuzz = (port->speed * ANALOG_FUZZ_MAGIC) / port->loop / 1000 + ANALOG_FUZZ_BITS;
588
589 for (i = 0; i < ANALOG_INIT_RETRIES; i++) {
590 if (!analog_cooked_read(port)) break;
591 wait_ms(ANALOG_MAX_TIME);
592 }
593
594 u = v = 0;
595
596 wait_ms(ANALOG_MAX_TIME);
597 t = gameport_time(gameport, ANALOG_MAX_TIME * 1000);
598 gameport_trigger(gameport);
599 while ((gameport_read(port->gameport) & port->mask) && (u < t)) u++;
600 udelay(ANALOG_SAITEK_DELAY);
601 t = gameport_time(gameport, ANALOG_SAITEK_TIME);
602 gameport_trigger(gameport);
603 while ((gameport_read(port->gameport) & port->mask) && (v < t)) v++;
604
605 if (v < (u >> 1) && port->gameport->number < ANALOG_PORTS) {
606 analog_options[port->gameport->number] |=
607 ANALOG_SAITEK | ANALOG_BTNS_CHF | ANALOG_HBTN_CHF | ANALOG_HAT1_CHF;
608 return 0;
609 }
610
611 gameport_close(gameport);
612 }
613
614 if (!gameport_open(gameport, dev, GAMEPORT_MODE_COOKED)) {
615
616 for (i = 0; i < ANALOG_INIT_RETRIES; i++)
617 if (!gameport_cooked_read(gameport, port->axes, &port->buttons))
618 break;
619 for (i = 0; i < 4; i++)
620 if (port->axes[i] != -1) port->mask |= 1 << i;
621
622 port->fuzz = gameport->fuzz;
623 port->cooked = 1;
624 return 0;
625 }
626
627 if (!gameport_open(gameport, dev, GAMEPORT_MODE_RAW))
628 return 0;
629
630 return -1;
631 }
632
633 static void analog_connect(struct gameport *gameport, struct gameport_dev *dev)
634 {
635 struct analog_port *port;
636 int i;
637
638 if (!(port = kmalloc(sizeof(struct analog_port), GFP_KERNEL)))
639 return;
640 memset(port, 0, sizeof(struct analog_port));
641
642 if (analog_init_port(gameport, dev, port)) {
643 kfree(port);
644 return;
645 }
646
647 if (analog_init_masks(port)) {
648 gameport_close(gameport);
649 kfree(port);
650 return;
651 }
652
653 for (i = 0; i < 2; i++)
654 if (port->analog[i].mask)
655 analog_init_device(port, port->analog + i, i);
656 }
657
658 static void analog_disconnect(struct gameport *gameport)
659 {
660 int i;
661
662 struct analog_port *port = gameport->private;
663 for (i = 0; i < 2; i++)
664 if (port->analog[i].mask)
665 input_unregister_device(&port->analog[i].dev);
666 gameport_close(gameport);
667 printk(KERN_INFO "analog.c: %d out of %d reads (%d%%) on gameport%d failed\n",
668 port->bads, port->reads, port->reads ? (port->bads * 100 / port->reads) : 0,
669 port->gameport->number);
670 kfree(port);
671 }
672
673 struct analog_types {
674 char *name;
675 int value;
676 };
677
678 struct analog_types analog_types[] = {
679 { "none", 0x00000000 },
680 { "auto", 0x000000ff },
681 { "2btn", 0x0000003f },
682 { "y-joy", 0x0cc00033 },
683 { "y-pad", 0x8cc80033 },
684 { "fcs", 0x000008f7 },
685 { "chf", 0x000002ff },
686 { "fullchf", 0x000007ff },
687 { "gamepad", 0x000830f3 },
688 { "gamepad8", 0x0008f0f3 },
689 { NULL, 0 }
690 };
691
692 static void analog_parse_options(void)
693 {
694 int i, j;
695 char *end;
696
697 for (i = 0; i < ANALOG_PORTS && js[i]; i++) {
698
699 for (j = 0; analog_types[j].name; j++)
700 if (!strcmp(analog_types[j].name, js[i])) {
701 analog_options[i] = analog_types[j].value;
702 break;
703 }
704 if (analog_types[j].name) continue;
705
706 analog_options[i] = simple_strtoul(js[i], &end, 0);
707 if (end != js[i]) continue;
708
709 analog_options[i] = 0xff;
710 if (!strlen(js[i])) continue;
711
712 printk(KERN_WARNING "analog.c: Bad config for port %d - \"%s\"\n", i, js[i]);
713 }
714
715 for (; i < ANALOG_PORTS; i++)
716 analog_options[i] = 0xff;
717 }
718
719 /*
720 * The gameport device structure.
721 */
722
723 static struct gameport_dev analog_dev = {
724 connect: analog_connect,
725 disconnect: analog_disconnect,
726 };
727
728 #ifndef MODULE
729 static int __init analog_setup(char *str)
730 {
731 char *s = str;
732 int i = 0;
733
734 if (!str || !*str) return 0;
735
736 while ((str = s) && (i < ANALOG_PORTS)) {
737 if ((s = strchr(str,','))) *s++ = 0;
738 js[i++] = str;
739 }
740
741 return 1;
742 }
743 __setup("js=", analog_setup);
744 #endif
745
746 int __init analog_init(void)
747 {
748 analog_parse_options();
749 gameport_register_device(&analog_dev);
750 return 0;
751 }
752
753 void __exit analog_exit(void)
754 {
755 gameport_unregister_device(&analog_dev);
756 }
757
758 module_init(analog_init);
759 module_exit(analog_exit);
760