Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[linux/fpc-iii.git] / drivers / input / joystick / adi.c
blobb78425765d3eb12ccd02fd1c3dc644a1956110a1
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/jiffies.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 64
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 = input_get_drvdata(dev);
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 = input_get_drvdata(dev);
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)
322 msleep(seq[i]);
323 if (seq[i] < 0) {
324 mdelay(-seq[i]);
325 udelay(-seq[i]*14); /* It looks like mdelay() is off by approx 1.4% */
330 static void adi_id_decode(struct adi *adi, struct adi_port *port)
332 int i, t;
334 if (adi->ret < ADI_MIN_ID_LENGTH) /* Minimum ID packet length */
335 return;
337 if (adi->ret < (t = adi_get_bits(adi, 10))) {
338 printk(KERN_WARNING "adi: Short ID packet: reported: %d != read: %d\n", t, adi->ret);
339 return;
342 adi->id = adi_get_bits(adi, 4) | (adi_get_bits(adi, 4) << 4);
344 if ((t = adi_get_bits(adi, 4)) & ADI_FLAG_HAT) adi->hats++;
346 adi->length = adi_get_bits(adi, 10);
348 if (adi->length >= ADI_MAX_LENGTH || adi->length < ADI_MIN_LENGTH) {
349 printk(KERN_WARNING "adi: Bad data packet length (%d).\n", adi->length);
350 adi->length = 0;
351 return;
354 adi->axes8 = adi_get_bits(adi, 4);
355 adi->buttons = adi_get_bits(adi, 6);
357 if (adi_get_bits(adi, 6) != 8 && adi->hats) {
358 printk(KERN_WARNING "adi: Other than 8-dir POVs not supported yet.\n");
359 adi->length = 0;
360 return;
363 adi->buttons += adi_get_bits(adi, 6);
364 adi->hats += adi_get_bits(adi, 4);
366 i = adi_get_bits(adi, 4);
368 if (t & ADI_FLAG_10BIT) {
369 adi->axes10 = adi->axes8 - i;
370 adi->axes8 = i;
373 t = adi_get_bits(adi, 4);
375 for (i = 0; i < t; i++)
376 adi->cname[i] = adi_get_bits(adi, 8);
377 adi->cname[i] = 0;
379 t = 8 + adi->buttons + adi->axes10 * 10 + adi->axes8 * 8 + adi->hats * 4;
380 if (adi->length != t && adi->length != t + (t & 1)) {
381 printk(KERN_WARNING "adi: Expected length %d != data length %d\n", t, adi->length);
382 adi->length = 0;
383 return;
386 switch (adi->id) {
387 case ADI_ID_TPD:
388 adi->pad = 4;
389 adi->buttons -= 4;
390 break;
391 case ADI_ID_WGP:
392 adi->pad = 0;
393 adi->buttons -= 4;
394 break;
395 default:
396 adi->pad = -1;
397 break;
401 static int adi_init_input(struct adi *adi, struct adi_port *port, int half)
403 struct input_dev *input_dev;
404 char buf[ADI_MAX_NAME_LENGTH];
405 int i, t;
407 adi->dev = input_dev = input_allocate_device();
408 if (!input_dev)
409 return -ENOMEM;
411 t = adi->id < ADI_ID_MAX ? adi->id : ADI_ID_MAX;
413 snprintf(buf, ADI_MAX_PHYS_LENGTH, adi_names[t], adi->id);
414 snprintf(adi->name, ADI_MAX_NAME_LENGTH, "Logitech %s [%s]", buf, adi->cname);
415 snprintf(adi->phys, ADI_MAX_PHYS_LENGTH, "%s/input%d", port->gameport->phys, half);
417 adi->abs = adi_abs[t];
418 adi->key = adi_key[t];
420 input_dev->name = adi->name;
421 input_dev->phys = adi->phys;
422 input_dev->id.bustype = BUS_GAMEPORT;
423 input_dev->id.vendor = GAMEPORT_ID_VENDOR_LOGITECH;
424 input_dev->id.product = adi->id;
425 input_dev->id.version = 0x0100;
426 input_dev->dev.parent = &port->gameport->dev;
428 input_set_drvdata(input_dev, port);
430 input_dev->open = adi_open;
431 input_dev->close = adi_close;
433 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
435 for (i = 0; i < adi->axes10 + adi->axes8 + (adi->hats + (adi->pad != -1)) * 2; i++)
436 set_bit(adi->abs[i], input_dev->absbit);
438 for (i = 0; i < adi->buttons; i++)
439 set_bit(adi->key[i], input_dev->keybit);
441 return 0;
444 static void adi_init_center(struct adi *adi)
446 int i, t, x;
448 if (!adi->length)
449 return;
451 for (i = 0; i < adi->axes10 + adi->axes8 + (adi->hats + (adi->pad != -1)) * 2; i++) {
453 t = adi->abs[i];
454 x = input_abs_get_val(adi->dev, t);
456 if (t == ABS_THROTTLE || t == ABS_RUDDER || adi->id == ADI_ID_WGPE)
457 x = i < adi->axes10 ? 512 : 128;
459 if (i < adi->axes10)
460 input_set_abs_params(adi->dev, t, 64, x * 2 - 64, 2, 16);
461 else if (i < adi->axes10 + adi->axes8)
462 input_set_abs_params(adi->dev, t, 48, x * 2 - 48, 1, 16);
463 else
464 input_set_abs_params(adi->dev, t, -1, 1, 0, 0);
469 * adi_connect() probes for Logitech ADI joysticks.
472 static int adi_connect(struct gameport *gameport, struct gameport_driver *drv)
474 struct adi_port *port;
475 int i;
476 int err;
478 port = kzalloc(sizeof(struct adi_port), GFP_KERNEL);
479 if (!port)
480 return -ENOMEM;
482 port->gameport = gameport;
484 gameport_set_drvdata(gameport, port);
486 err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
487 if (err)
488 goto fail1;
490 adi_init_digital(gameport);
491 adi_read_packet(port);
493 if (port->adi[0].ret >= ADI_MIN_LEN_LENGTH)
494 adi_move_bits(port, adi_get_bits(port->adi, 10));
496 for (i = 0; i < 2; i++) {
497 adi_id_decode(port->adi + i, port);
499 if (!port->adi[i].length)
500 continue;
502 err = adi_init_input(port->adi + i, port, i);
503 if (err)
504 goto fail2;
507 if (!port->adi[0].length && !port->adi[1].length) {
508 err = -ENODEV;
509 goto fail2;
512 gameport_set_poll_handler(gameport, adi_poll);
513 gameport_set_poll_interval(gameport, 20);
515 msleep(ADI_INIT_DELAY);
516 if (adi_read(port)) {
517 msleep(ADI_DATA_DELAY);
518 adi_read(port);
521 for (i = 0; i < 2; i++)
522 if (port->adi[i].length > 0) {
523 adi_init_center(port->adi + i);
524 err = input_register_device(port->adi[i].dev);
525 if (err)
526 goto fail3;
529 return 0;
531 fail3: while (--i >= 0) {
532 if (port->adi[i].length > 0) {
533 input_unregister_device(port->adi[i].dev);
534 port->adi[i].dev = NULL;
537 fail2: for (i = 0; i < 2; i++)
538 if (port->adi[i].dev)
539 input_free_device(port->adi[i].dev);
540 gameport_close(gameport);
541 fail1: gameport_set_drvdata(gameport, NULL);
542 kfree(port);
543 return err;
546 static void adi_disconnect(struct gameport *gameport)
548 int i;
549 struct adi_port *port = gameport_get_drvdata(gameport);
551 for (i = 0; i < 2; i++)
552 if (port->adi[i].length > 0)
553 input_unregister_device(port->adi[i].dev);
554 gameport_close(gameport);
555 gameport_set_drvdata(gameport, NULL);
556 kfree(port);
559 static struct gameport_driver adi_drv = {
560 .driver = {
561 .name = "adi",
563 .description = DRIVER_DESC,
564 .connect = adi_connect,
565 .disconnect = adi_disconnect,
568 module_gameport_driver(adi_drv);