Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / input / joystick / adi.c
blob83f6dafc171697ae1ed9e0898dfd7038c15200c2
1 /*
2 * Copyright (c) 1998-2005 Vojtech Pavlik
3 */
5 /*
6 * Logitech ADI joystick family driver for Linux
7 */
9 /*
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * Should you need to contact me, the author, you can do so either by
25 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
26 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
29 #include <linux/delay.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/string.h>
33 #include <linux/slab.h>
34 #include <linux/input.h>
35 #include <linux/gameport.h>
36 #include <linux/init.h>
38 #define DRIVER_DESC "Logitech ADI joystick family driver"
40 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
41 MODULE_DESCRIPTION(DRIVER_DESC);
42 MODULE_LICENSE("GPL");
45 * Times, array sizes, flags, ids.
48 #define ADI_MAX_START 200 /* Trigger to packet timeout [200us] */
49 #define ADI_MAX_STROBE 40 /* Single bit timeout [40us] */
50 #define ADI_INIT_DELAY 10 /* Delay after init packet [10ms] */
51 #define ADI_DATA_DELAY 4 /* Delay after data packet [4ms] */
53 #define ADI_MAX_LENGTH 256
54 #define ADI_MIN_LENGTH 8
55 #define ADI_MIN_LEN_LENGTH 10
56 #define ADI_MIN_ID_LENGTH 66
57 #define ADI_MAX_NAME_LENGTH 48
58 #define ADI_MAX_CNAME_LENGTH 16
59 #define ADI_MAX_PHYS_LENGTH 64
61 #define ADI_FLAG_HAT 0x04
62 #define ADI_FLAG_10BIT 0x08
64 #define ADI_ID_TPD 0x01
65 #define ADI_ID_WGP 0x06
66 #define ADI_ID_WGPE 0x08
67 #define ADI_ID_MAX 0x0a
70 * Names, buttons, axes ...
73 static char *adi_names[] = { "WingMan Extreme Digital", "ThunderPad Digital", "SideCar", "CyberMan 2",
74 "WingMan Interceptor", "WingMan Formula", "WingMan GamePad",
75 "WingMan Extreme Digital 3D", "WingMan GamePad Extreme",
76 "WingMan GamePad USB", "Unknown Device %#x" };
78 static char adi_wmgpe_abs[] = { ABS_X, ABS_Y, ABS_HAT0X, ABS_HAT0Y };
79 static char adi_wmi_abs[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y };
80 static char adi_wmed3d_abs[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_RZ, ABS_HAT0X, ABS_HAT0Y };
81 static char adi_cm2_abs[] = { ABS_X, ABS_Y, ABS_Z, ABS_RX, ABS_RY, ABS_RZ };
82 static char adi_wmf_abs[] = { ABS_WHEEL, ABS_GAS, ABS_BRAKE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y };
84 static short adi_wmgpe_key[] = { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_START, BTN_MODE, BTN_SELECT };
85 static short adi_wmi_key[] = { BTN_TRIGGER, BTN_TOP, BTN_THUMB, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_EXTRA };
86 static short adi_wmed3d_key[] = { BTN_TRIGGER, BTN_THUMB, BTN_THUMB2, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2 };
87 static short adi_cm2_key[] = { BTN_1, BTN_2, BTN_3, BTN_4, BTN_5, BTN_6, BTN_7, BTN_8 };
89 static char* adi_abs[] = { adi_wmi_abs, adi_wmgpe_abs, adi_wmf_abs, adi_cm2_abs, adi_wmi_abs, adi_wmf_abs,
90 adi_wmgpe_abs, adi_wmed3d_abs, adi_wmgpe_abs, adi_wmgpe_abs, adi_wmi_abs };
92 static short* adi_key[] = { adi_wmi_key, adi_wmgpe_key, adi_cm2_key, adi_cm2_key, adi_wmi_key, adi_cm2_key,
93 adi_wmgpe_key, adi_wmed3d_key, adi_wmgpe_key, adi_wmgpe_key, adi_wmi_key };
96 * Hat to axis conversion arrays.
99 static struct {
100 int x;
101 int y;
102 } adi_hat_to_axis[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
105 * Per-port information.
108 struct adi {
109 struct input_dev dev;
110 int length;
111 int ret;
112 int idx;
113 unsigned char id;
114 char buttons;
115 char axes10;
116 char axes8;
117 signed char pad;
118 char hats;
119 char *abs;
120 short *key;
121 char name[ADI_MAX_NAME_LENGTH];
122 char cname[ADI_MAX_CNAME_LENGTH];
123 char phys[ADI_MAX_PHYS_LENGTH];
124 unsigned char data[ADI_MAX_LENGTH];
127 struct adi_port {
128 struct gameport *gameport;
129 struct adi adi[2];
130 int bad;
131 int reads;
135 * adi_read_packet() reads a Logitech ADI packet.
138 static void adi_read_packet(struct adi_port *port)
140 struct adi *adi = port->adi;
141 struct gameport *gameport = port->gameport;
142 unsigned char u, v, w, x, z;
143 int t[2], s[2], i;
144 unsigned long flags;
146 for (i = 0; i < 2; i++) {
147 adi[i].ret = -1;
148 t[i] = gameport_time(gameport, ADI_MAX_START);
149 s[i] = 0;
152 local_irq_save(flags);
154 gameport_trigger(gameport);
155 v = z = gameport_read(gameport);
157 do {
158 u = v;
159 w = u ^ (v = x = gameport_read(gameport));
160 for (i = 0; i < 2; i++, w >>= 2, x >>= 2) {
161 t[i]--;
162 if ((w & 0x30) && s[i]) {
163 if ((w & 0x30) < 0x30 && adi[i].ret < ADI_MAX_LENGTH && t[i] > 0) {
164 adi[i].data[++adi[i].ret] = w;
165 t[i] = gameport_time(gameport, ADI_MAX_STROBE);
166 } else t[i] = 0;
167 } else if (!(x & 0x30)) s[i] = 1;
169 } while (t[0] > 0 || t[1] > 0);
171 local_irq_restore(flags);
173 return;
177 * adi_move_bits() detects a possible 2-stream mode, and moves
178 * the bits accordingly.
181 static void adi_move_bits(struct adi_port *port, int length)
183 int i;
184 struct adi *adi = port->adi;
186 adi[0].idx = adi[1].idx = 0;
188 if (adi[0].ret <= 0 || adi[1].ret <= 0) return;
189 if (adi[0].data[0] & 0x20 || ~adi[1].data[0] & 0x20) return;
191 for (i = 1; i <= adi[1].ret; i++)
192 adi[0].data[((length - 1) >> 1) + i + 1] = adi[1].data[i];
194 adi[0].ret += adi[1].ret;
195 adi[1].ret = -1;
199 * adi_get_bits() gathers bits from the data packet.
202 static inline int adi_get_bits(struct adi *adi, int count)
204 int bits = 0;
205 int i;
206 if ((adi->idx += count) > adi->ret) return 0;
207 for (i = 0; i < count; i++)
208 bits |= ((adi->data[adi->idx - i] >> 5) & 1) << i;
209 return bits;
213 * adi_decode() decodes Logitech joystick data into input events.
216 static int adi_decode(struct adi *adi)
218 struct input_dev *dev = &adi->dev;
219 char *abs = adi->abs;
220 short *key = adi->key;
221 int i, t;
223 if (adi->ret < adi->length || adi->id != (adi_get_bits(adi, 4) | (adi_get_bits(adi, 4) << 4)))
224 return -1;
226 for (i = 0; i < adi->axes10; i++)
227 input_report_abs(dev, *abs++, adi_get_bits(adi, 10));
229 for (i = 0; i < adi->axes8; i++)
230 input_report_abs(dev, *abs++, adi_get_bits(adi, 8));
232 for (i = 0; i < adi->buttons && i < 63; i++) {
233 if (i == adi->pad) {
234 t = adi_get_bits(adi, 4);
235 input_report_abs(dev, *abs++, ((t >> 2) & 1) - ( t & 1));
236 input_report_abs(dev, *abs++, ((t >> 1) & 1) - ((t >> 3) & 1));
238 input_report_key(dev, *key++, adi_get_bits(adi, 1));
241 for (i = 0; i < adi->hats; i++) {
242 if ((t = adi_get_bits(adi, 4)) > 8) t = 0;
243 input_report_abs(dev, *abs++, adi_hat_to_axis[t].x);
244 input_report_abs(dev, *abs++, adi_hat_to_axis[t].y);
247 for (i = 63; i < adi->buttons; i++)
248 input_report_key(dev, *key++, adi_get_bits(adi, 1));
250 input_sync(dev);
252 return 0;
256 * adi_read() reads the data packet and decodes it.
259 static int adi_read(struct adi_port *port)
261 int i;
262 int result = 0;
264 adi_read_packet(port);
265 adi_move_bits(port, port->adi[0].length);
267 for (i = 0; i < 2; i++)
268 if (port->adi[i].length)
269 result |= adi_decode(port->adi + i);
271 return result;
275 * adi_poll() repeatedly polls the Logitech joysticks.
278 static void adi_poll(struct gameport *gameport)
280 struct adi_port *port = gameport_get_drvdata(gameport);
282 port->bad -= adi_read(port);
283 port->reads++;
287 * adi_open() is a callback from the input open routine.
290 static int adi_open(struct input_dev *dev)
292 struct adi_port *port = dev->private;
294 gameport_start_polling(port->gameport);
295 return 0;
299 * adi_close() is a callback from the input close routine.
302 static void adi_close(struct input_dev *dev)
304 struct adi_port *port = dev->private;
306 gameport_stop_polling(port->gameport);
310 * adi_init_digital() sends a trigger & delay sequence
311 * to reset and initialize a Logitech joystick into digital mode.
314 static void adi_init_digital(struct gameport *gameport)
316 int seq[] = { 4, -2, -3, 10, -6, -11, -7, -9, 11, 0 };
317 int i;
319 for (i = 0; seq[i]; i++) {
320 gameport_trigger(gameport);
321 if (seq[i] > 0) msleep(seq[i]);
322 if (seq[i] < 0) {
323 mdelay(-seq[i]);
324 udelay(-seq[i]*14); /* It looks like mdelay() is off by approx 1.4% */
329 static void adi_id_decode(struct adi *adi, struct adi_port *port)
331 int i, t;
333 if (adi->ret < ADI_MIN_ID_LENGTH) /* Minimum ID packet length */
334 return;
336 if (adi->ret < (t = adi_get_bits(adi, 10))) {
337 printk(KERN_WARNING "adi: Short ID packet: reported: %d != read: %d\n", t, adi->ret);
338 return;
341 adi->id = adi_get_bits(adi, 4) | (adi_get_bits(adi, 4) << 4);
343 if ((t = adi_get_bits(adi, 4)) & ADI_FLAG_HAT) adi->hats++;
345 adi->length = adi_get_bits(adi, 10);
347 if (adi->length >= ADI_MAX_LENGTH || adi->length < ADI_MIN_LENGTH) {
348 printk(KERN_WARNING "adi: Bad data packet length (%d).\n", adi->length);
349 adi->length = 0;
350 return;
353 adi->axes8 = adi_get_bits(adi, 4);
354 adi->buttons = adi_get_bits(adi, 6);
356 if (adi_get_bits(adi, 6) != 8 && adi->hats) {
357 printk(KERN_WARNING "adi: Other than 8-dir POVs not supported yet.\n");
358 adi->length = 0;
359 return;
362 adi->buttons += adi_get_bits(adi, 6);
363 adi->hats += adi_get_bits(adi, 4);
365 i = adi_get_bits(adi, 4);
367 if (t & ADI_FLAG_10BIT) {
368 adi->axes10 = adi->axes8 - i;
369 adi->axes8 = i;
372 t = adi_get_bits(adi, 4);
374 for (i = 0; i < t; i++)
375 adi->cname[i] = adi_get_bits(adi, 8);
376 adi->cname[i] = 0;
378 t = 8 + adi->buttons + adi->axes10 * 10 + adi->axes8 * 8 + adi->hats * 4;
379 if (adi->length != t && adi->length != t + (t & 1)) {
380 printk(KERN_WARNING "adi: Expected length %d != data length %d\n", t, adi->length);
381 adi->length = 0;
382 return;
385 switch (adi->id) {
386 case ADI_ID_TPD:
387 adi->pad = 4;
388 adi->buttons -= 4;
389 break;
390 case ADI_ID_WGP:
391 adi->pad = 0;
392 adi->buttons -= 4;
393 break;
394 default:
395 adi->pad = -1;
396 break;
400 static void adi_init_input(struct adi *adi, struct adi_port *port, int half)
402 int i, t;
403 char buf[ADI_MAX_NAME_LENGTH];
405 if (!adi->length) return;
407 init_input_dev(&adi->dev);
409 t = adi->id < ADI_ID_MAX ? adi->id : ADI_ID_MAX;
411 snprintf(buf, ADI_MAX_PHYS_LENGTH, adi_names[t], adi->id);
412 snprintf(adi->name, ADI_MAX_NAME_LENGTH, "Logitech %s", buf);
413 snprintf(adi->phys, ADI_MAX_PHYS_LENGTH, "%s/input%d", port->gameport->phys, half);
415 adi->abs = adi_abs[t];
416 adi->key = adi_key[t];
418 adi->dev.open = adi_open;
419 adi->dev.close = adi_close;
421 adi->dev.name = adi->name;
422 adi->dev.phys = adi->phys;
423 adi->dev.id.bustype = BUS_GAMEPORT;
424 adi->dev.id.vendor = GAMEPORT_ID_VENDOR_LOGITECH;
425 adi->dev.id.product = adi->id;
426 adi->dev.id.version = 0x0100;
428 adi->dev.private = port;
429 adi->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
431 for (i = 0; i < adi->axes10 + adi->axes8 + (adi->hats + (adi->pad != -1)) * 2; i++)
432 set_bit(adi->abs[i], adi->dev.absbit);
434 for (i = 0; i < adi->buttons; i++)
435 set_bit(adi->key[i], adi->dev.keybit);
438 static void adi_init_center(struct adi *adi)
440 int i, t, x;
442 if (!adi->length)
443 return;
445 for (i = 0; i < adi->axes10 + adi->axes8 + (adi->hats + (adi->pad != -1)) * 2; i++) {
447 t = adi->abs[i];
448 x = adi->dev.abs[t];
450 if (t == ABS_THROTTLE || t == ABS_RUDDER || adi->id == ADI_ID_WGPE)
451 x = i < adi->axes10 ? 512 : 128;
453 if (i < adi->axes10)
454 input_set_abs_params(&adi->dev, t, 64, x * 2 - 64, 2, 16);
455 else if (i < adi->axes10 + adi->axes8)
456 input_set_abs_params(&adi->dev, t, 48, x * 2 - 48, 1, 16);
457 else
458 input_set_abs_params(&adi->dev, t, -1, 1, 0, 0);
463 * adi_connect() probes for Logitech ADI joysticks.
466 static int adi_connect(struct gameport *gameport, struct gameport_driver *drv)
468 struct adi_port *port;
469 int i;
470 int err;
472 if (!(port = kcalloc(1, sizeof(struct adi_port), GFP_KERNEL)))
473 return -ENOMEM;
475 port->gameport = gameport;
477 gameport_set_drvdata(gameport, port);
479 err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
480 if (err) {
481 kfree(port);
482 return err;
485 adi_init_digital(gameport);
486 adi_read_packet(port);
488 if (port->adi[0].ret >= ADI_MIN_LEN_LENGTH)
489 adi_move_bits(port, adi_get_bits(port->adi, 10));
491 for (i = 0; i < 2; i++) {
492 adi_id_decode(port->adi + i, port);
493 adi_init_input(port->adi + i, port, i);
496 if (!port->adi[0].length && !port->adi[1].length) {
497 gameport_close(gameport);
498 kfree(port);
499 return -ENODEV;
502 gameport_set_poll_handler(gameport, adi_poll);
503 gameport_set_poll_interval(gameport, 20);
505 msleep(ADI_INIT_DELAY);
506 if (adi_read(port)) {
507 msleep(ADI_DATA_DELAY);
508 adi_read(port);
511 for (i = 0; i < 2; i++)
512 if (port->adi[i].length > 0) {
513 adi_init_center(port->adi + i);
514 input_register_device(&port->adi[i].dev);
515 printk(KERN_INFO "input: %s [%s] on %s\n",
516 port->adi[i].name, port->adi[i].cname, gameport->phys);
519 return 0;
522 static void adi_disconnect(struct gameport *gameport)
524 int i;
525 struct adi_port *port = gameport_get_drvdata(gameport);
527 for (i = 0; i < 2; i++)
528 if (port->adi[i].length > 0)
529 input_unregister_device(&port->adi[i].dev);
530 gameport_close(gameport);
531 gameport_set_drvdata(gameport, NULL);
532 kfree(port);
536 * The gameport device structure.
539 static struct gameport_driver adi_drv = {
540 .driver = {
541 .name = "adi",
543 .description = DRIVER_DESC,
544 .connect = adi_connect,
545 .disconnect = adi_disconnect,
548 static int __init adi_init(void)
550 gameport_register_driver(&adi_drv);
551 return 0;
554 static void __exit adi_exit(void)
556 gameport_unregister_driver(&adi_drv);
559 module_init(adi_init);
560 module_exit(adi_exit);