aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/joystick/adi.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/input/joystick/adi.c
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'drivers/input/joystick/adi.c')
-rw-r--r--drivers/input/joystick/adi.c560
1 files changed, 560 insertions, 0 deletions
diff --git a/drivers/input/joystick/adi.c b/drivers/input/joystick/adi.c
new file mode 100644
index 000000000000..83f6dafc1716
--- /dev/null
+++ b/drivers/input/joystick/adi.c
@@ -0,0 +1,560 @@
1/*
2 * Copyright (c) 1998-2005 Vojtech Pavlik
3 */
4
5/*
6 * Logitech ADI joystick family driver for Linux
7 */
8
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.
14 *
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.
19 *
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
23 *
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
27 */
28
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>
37
38#define DRIVER_DESC "Logitech ADI joystick family driver"
39
40MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
41MODULE_DESCRIPTION(DRIVER_DESC);
42MODULE_LICENSE("GPL");
43
44/*
45 * Times, array sizes, flags, ids.
46 */
47
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] */
52
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
60
61#define ADI_FLAG_HAT 0x04
62#define ADI_FLAG_10BIT 0x08
63
64#define ADI_ID_TPD 0x01
65#define ADI_ID_WGP 0x06
66#define ADI_ID_WGPE 0x08
67#define ADI_ID_MAX 0x0a
68
69/*
70 * Names, buttons, axes ...
71 */
72
73static 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" };
77
78static char adi_wmgpe_abs[] = { ABS_X, ABS_Y, ABS_HAT0X, ABS_HAT0Y };
79static char adi_wmi_abs[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y };
80static char adi_wmed3d_abs[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_RZ, ABS_HAT0X, ABS_HAT0Y };
81static char adi_cm2_abs[] = { ABS_X, ABS_Y, ABS_Z, ABS_RX, ABS_RY, ABS_RZ };
82static char adi_wmf_abs[] = { ABS_WHEEL, ABS_GAS, ABS_BRAKE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y };
83
84static 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 };
85static short adi_wmi_key[] = { BTN_TRIGGER, BTN_TOP, BTN_THUMB, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_EXTRA };
86static short adi_wmed3d_key[] = { BTN_TRIGGER, BTN_THUMB, BTN_THUMB2, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2 };
87static short adi_cm2_key[] = { BTN_1, BTN_2, BTN_3, BTN_4, BTN_5, BTN_6, BTN_7, BTN_8 };
88
89static 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 };
91
92static 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 };
94
95/*
96 * Hat to axis conversion arrays.
97 */
98
99static 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}};
103
104/*
105 * Per-port information.
106 */
107
108struct 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];
125};
126
127struct adi_port {
128 struct gameport *gameport;
129 struct adi adi[2];
130 int bad;
131 int reads;
132};
133
134/*
135 * adi_read_packet() reads a Logitech ADI packet.
136 */
137
138static void adi_read_packet(struct adi_port *port)
139{
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;
145
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;
150 }
151
152 local_irq_save(flags);
153
154 gameport_trigger(gameport);
155 v = z = gameport_read(gameport);
156
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;
168 }
169 } while (t[0] > 0 || t[1] > 0);
170
171 local_irq_restore(flags);
172
173 return;
174}
175
176/*
177 * adi_move_bits() detects a possible 2-stream mode, and moves
178 * the bits accordingly.
179 */
180
181static void adi_move_bits(struct adi_port *port, int length)
182{
183 int i;
184 struct adi *adi = port->adi;
185
186 adi[0].idx = adi[1].idx = 0;
187
188 if (adi[0].ret <= 0 || adi[1].ret <= 0) return;
189 if (adi[0].data[0] & 0x20 || ~adi[1].data[0] & 0x20) return;
190
191 for (i = 1; i <= adi[1].ret; i++)
192 adi[0].data[((length - 1) >> 1) + i + 1] = adi[1].data[i];
193
194 adi[0].ret += adi[1].ret;
195 adi[1].ret = -1;
196}
197
198/*
199 * adi_get_bits() gathers bits from the data packet.
200 */
201
202static inline int adi_get_bits(struct adi *adi, int count)
203{
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;
210}
211
212/*
213 * adi_decode() decodes Logitech joystick data into input events.
214 */
215
216static int adi_decode(struct adi *adi)
217{
218 struct input_dev *dev = &adi->dev;
219 char *abs = adi->abs;
220 short *key = adi->key;
221 int i, t;
222
223 if (adi->ret < adi->length || adi->id != (adi_get_bits(adi, 4) | (adi_get_bits(adi, 4) << 4)))
224 return -1;
225
226 for (i = 0; i < adi->axes10; i++)
227 input_report_abs(dev, *abs++, adi_get_bits(adi, 10));
228
229 for (i = 0; i < adi->axes8; i++)
230 input_report_abs(dev, *abs++, adi_get_bits(adi, 8));
231
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));
237 }
238 input_report_key(dev, *key++, adi_get_bits(adi, 1));
239 }
240
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);
245 }
246
247 for (i = 63; i < adi->buttons; i++)
248 input_report_key(dev, *key++, adi_get_bits(adi, 1));
249
250 input_sync(dev);
251
252 return 0;
253}
254
255/*
256 * adi_read() reads the data packet and decodes it.
257 */
258
259static int adi_read(struct adi_port *port)
260{
261 int i;
262 int result = 0;
263
264 adi_read_packet(port);
265 adi_move_bits(port, port->adi[0].length);
266
267 for (i = 0; i < 2; i++)
268 if (port->adi[i].length)
269 result |= adi_decode(port->adi + i);
270
271 return result;
272}
273
274/*
275 * adi_poll() repeatedly polls the Logitech joysticks.
276 */
277
278static void adi_poll(struct gameport *gameport)
279{
280 struct adi_port *port = gameport_get_drvdata(gameport);
281
282 port->bad -= adi_read(port);
283 port->reads++;
284}
285
286/*
287 * adi_open() is a callback from the input open routine.
288 */
289
290static int adi_open(struct input_dev *dev)
291{
292 struct adi_port *port = dev->private;
293
294 gameport_start_polling(port->gameport);
295 return 0;
296}
297
298/*
299 * adi_close() is a callback from the input close routine.
300 */
301
302static void adi_close(struct input_dev *dev)
303{
304 struct adi_port *port = dev->private;
305
306 gameport_stop_polling(port->gameport);
307}
308
309/*
310 * adi_init_digital() sends a trigger & delay sequence
311 * to reset and initialize a Logitech joystick into digital mode.
312 */
313
314static void adi_init_digital(struct gameport *gameport)
315{
316 int seq[] = { 4, -2, -3, 10, -6, -11, -7, -9, 11, 0 };
317 int i;
318
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% */
325 }
326 }
327}
328
329static void adi_id_decode(struct adi *adi, struct adi_port *port)
330{
331 int i, t;
332
333 if (adi->ret < ADI_MIN_ID_LENGTH) /* Minimum ID packet length */
334 return;
335
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;
339 }
340
341 adi->id = adi_get_bits(adi, 4) | (adi_get_bits(adi, 4) << 4);
342
343 if ((t = adi_get_bits(adi, 4)) & ADI_FLAG_HAT) adi->hats++;
344
345 adi->length = adi_get_bits(adi, 10);
346
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;
351 }
352
353 adi->axes8 = adi_get_bits(adi, 4);
354 adi->buttons = adi_get_bits(adi, 6);
355
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;
360 }
361
362 adi->buttons += adi_get_bits(adi, 6);
363 adi->hats += adi_get_bits(adi, 4);
364
365 i = adi_get_bits(adi, 4);
366
367 if (t & ADI_FLAG_10BIT) {
368 adi->axes10 = adi->axes8 - i;
369 adi->axes8 = i;
370 }
371
372 t = adi_get_bits(adi, 4);
373
374 for (i = 0; i < t; i++)
375 adi->cname[i] = adi_get_bits(adi, 8);
376 adi->cname[i] = 0;
377
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;
383 }
384
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;
397 }
398}
399
400static void adi_init_input(struct adi *adi, struct adi_port *port, int half)
401{
402 int i, t;
403 char buf[ADI_MAX_NAME_LENGTH];
404
405 if (!adi->length) return;
406
407 init_input_dev(&adi->dev);
408
409 t = adi->id < ADI_ID_MAX ? adi->id : ADI_ID_MAX;
410
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);
414
415 adi->abs = adi_abs[t];
416 adi->key = adi_key[t];
417
418 adi->dev.open = adi_open;
419 adi->dev.close = adi_close;
420
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;
427
428 adi->dev.private = port;
429 adi->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
430
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);
433
434 for (i = 0; i < adi->buttons; i++)
435 set_bit(adi->key[i], adi->dev.keybit);
436}
437
438static void adi_init_center(struct adi *adi)
439{
440 int i, t, x;
441
442 if (!adi->length)
443 return;
444
445 for (i = 0; i < adi->axes10 + adi->axes8 + (adi->hats + (adi->pad != -1)) * 2; i++) {
446
447 t = adi->abs[i];
448 x = adi->dev.abs[t];
449
450 if (t == ABS_THROTTLE || t == ABS_RUDDER || adi->id == ADI_ID_WGPE)
451 x = i < adi->axes10 ? 512 : 128;
452
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);
459 }
460}
461
462/*
463 * adi_connect() probes for Logitech ADI joysticks.
464 */
465
466static int adi_connect(struct gameport *gameport, struct gameport_driver *drv)
467{
468 struct adi_port *port;
469 int i;
470 int err;
471
472 if (!(port = kcalloc(1, sizeof(struct adi_port), GFP_KERNEL)))
473 return -ENOMEM;
474
475 port->gameport = gameport;
476
477 gameport_set_drvdata(gameport, port);
478
479 err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
480 if (err) {
481 kfree(port);
482 return err;
483 }
484
485 adi_init_digital(gameport);
486 adi_read_packet(port);
487
488 if (port->adi[0].ret >= ADI_MIN_LEN_LENGTH)
489 adi_move_bits(port, adi_get_bits(port->adi, 10));
490
491 for (i = 0; i < 2; i++) {
492 adi_id_decode(port->adi + i, port);
493 adi_init_input(port->adi + i, port, i);
494 }
495
496 if (!port->adi[0].length && !port->adi[1].length) {
497 gameport_close(gameport);
498 kfree(port);
499 return -ENODEV;
500 }
501
502 gameport_set_poll_handler(gameport, adi_poll);
503 gameport_set_poll_interval(gameport, 20);
504
505 msleep(ADI_INIT_DELAY);
506 if (adi_read(port)) {
507 msleep(ADI_DATA_DELAY);
508 adi_read(port);
509 }
510
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);
517 }
518
519 return 0;
520}
521
522static void adi_disconnect(struct gameport *gameport)
523{
524 int i;
525 struct adi_port *port = gameport_get_drvdata(gameport);
526
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);
533}
534
535/*
536 * The gameport device structure.
537 */
538
539static struct gameport_driver adi_drv = {
540 .driver = {
541 .name = "adi",
542 },
543 .description = DRIVER_DESC,
544 .connect = adi_connect,
545 .disconnect = adi_disconnect,
546};
547
548static int __init adi_init(void)
549{
550 gameport_register_driver(&adi_drv);
551 return 0;
552}
553
554static void __exit adi_exit(void)
555{
556 gameport_unregister_driver(&adi_drv);
557}
558
559module_init(adi_init);
560module_exit(adi_exit);