xref: /openbmc/linux/drivers/input/joystick/adi.c (revision 384740dc)
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 #include <linux/jiffies.h>
38 
39 #define DRIVER_DESC	"Logitech ADI joystick family driver"
40 
41 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
42 MODULE_DESCRIPTION(DRIVER_DESC);
43 MODULE_LICENSE("GPL");
44 
45 /*
46  * Times, array sizes, flags, ids.
47  */
48 
49 #define ADI_MAX_START		200	/* Trigger to packet timeout [200us] */
50 #define ADI_MAX_STROBE		40	/* Single bit timeout [40us] */
51 #define ADI_INIT_DELAY		10	/* Delay after init packet [10ms] */
52 #define ADI_DATA_DELAY		4	/* Delay after data packet [4ms] */
53 
54 #define ADI_MAX_LENGTH		256
55 #define ADI_MIN_LENGTH		8
56 #define ADI_MIN_LEN_LENGTH	10
57 #define ADI_MIN_ID_LENGTH	66
58 #define ADI_MAX_NAME_LENGTH	64
59 #define ADI_MAX_CNAME_LENGTH	16
60 #define ADI_MAX_PHYS_LENGTH	64
61 
62 #define ADI_FLAG_HAT		0x04
63 #define ADI_FLAG_10BIT		0x08
64 
65 #define ADI_ID_TPD		0x01
66 #define ADI_ID_WGP		0x06
67 #define ADI_ID_WGPE		0x08
68 #define ADI_ID_MAX		0x0a
69 
70 /*
71  * Names, buttons, axes ...
72  */
73 
74 static char *adi_names[] = {	"WingMan Extreme Digital", "ThunderPad Digital", "SideCar", "CyberMan 2",
75 				"WingMan Interceptor", "WingMan Formula", "WingMan GamePad",
76 				"WingMan Extreme Digital 3D", "WingMan GamePad Extreme",
77 				"WingMan GamePad USB", "Unknown Device %#x" };
78 
79 static char adi_wmgpe_abs[] =	{ ABS_X, ABS_Y, ABS_HAT0X, ABS_HAT0Y };
80 static char adi_wmi_abs[] =	{ ABS_X, ABS_Y, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y };
81 static char adi_wmed3d_abs[] =	{ ABS_X, ABS_Y, ABS_THROTTLE, ABS_RZ, ABS_HAT0X, ABS_HAT0Y };
82 static char adi_cm2_abs[] =	{ ABS_X, ABS_Y, ABS_Z, ABS_RX, ABS_RY, ABS_RZ };
83 static char adi_wmf_abs[] =	{ ABS_WHEEL, ABS_GAS, ABS_BRAKE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y };
84 
85 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 };
86 static short adi_wmi_key[] =	{ BTN_TRIGGER,  BTN_TOP, BTN_THUMB, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_EXTRA };
87 static short adi_wmed3d_key[] =	{ BTN_TRIGGER, BTN_THUMB, BTN_THUMB2, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2 };
88 static short adi_cm2_key[] =	{ BTN_1, BTN_2, BTN_3, BTN_4, BTN_5, BTN_6, BTN_7, BTN_8 };
89 
90 static char* adi_abs[] = { adi_wmi_abs, adi_wmgpe_abs, adi_wmf_abs, adi_cm2_abs, adi_wmi_abs, adi_wmf_abs,
91 			   adi_wmgpe_abs, adi_wmed3d_abs, adi_wmgpe_abs, adi_wmgpe_abs, adi_wmi_abs };
92 
93 static short* adi_key[] = { adi_wmi_key, adi_wmgpe_key, adi_cm2_key, adi_cm2_key, adi_wmi_key, adi_cm2_key,
94 			    adi_wmgpe_key, adi_wmed3d_key, adi_wmgpe_key, adi_wmgpe_key, adi_wmi_key };
95 
96 /*
97  * Hat to axis conversion arrays.
98  */
99 
100 static struct {
101 	int x;
102 	int y;
103 } adi_hat_to_axis[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
104 
105 /*
106  * Per-port information.
107  */
108 
109 struct adi {
110 	struct input_dev *dev;
111 	int length;
112 	int ret;
113 	int idx;
114 	unsigned char id;
115 	char buttons;
116 	char axes10;
117 	char axes8;
118 	signed char pad;
119 	char hats;
120 	char *abs;
121 	short *key;
122 	char name[ADI_MAX_NAME_LENGTH];
123 	char cname[ADI_MAX_CNAME_LENGTH];
124 	char phys[ADI_MAX_PHYS_LENGTH];
125 	unsigned char data[ADI_MAX_LENGTH];
126 };
127 
128 struct adi_port {
129 	struct gameport *gameport;
130 	struct adi adi[2];
131 	int bad;
132 	int reads;
133 };
134 
135 /*
136  * adi_read_packet() reads a Logitech ADI packet.
137  */
138 
139 static void adi_read_packet(struct adi_port *port)
140 {
141 	struct adi *adi = port->adi;
142 	struct gameport *gameport = port->gameport;
143 	unsigned char u, v, w, x, z;
144 	int t[2], s[2], i;
145 	unsigned long flags;
146 
147 	for (i = 0; i < 2; i++) {
148 		adi[i].ret = -1;
149 		t[i] = gameport_time(gameport, ADI_MAX_START);
150 		s[i] = 0;
151 	}
152 
153 	local_irq_save(flags);
154 
155 	gameport_trigger(gameport);
156 	v = z = gameport_read(gameport);
157 
158 	do {
159 		u = v;
160 		w = u ^ (v = x = gameport_read(gameport));
161 		for (i = 0; i < 2; i++, w >>= 2, x >>= 2) {
162 			t[i]--;
163 			if ((w & 0x30) && s[i]) {
164 				if ((w & 0x30) < 0x30 && adi[i].ret < ADI_MAX_LENGTH && t[i] > 0) {
165 					adi[i].data[++adi[i].ret] = w;
166 					t[i] = gameport_time(gameport, ADI_MAX_STROBE);
167 				} else t[i] = 0;
168 			} else if (!(x & 0x30)) s[i] = 1;
169 		}
170 	} while (t[0] > 0 || t[1] > 0);
171 
172 	local_irq_restore(flags);
173 
174 	return;
175 }
176 
177 /*
178  * adi_move_bits() detects a possible 2-stream mode, and moves
179  * the bits accordingly.
180  */
181 
182 static void adi_move_bits(struct adi_port *port, int length)
183 {
184 	int i;
185 	struct adi *adi = port->adi;
186 
187 	adi[0].idx = adi[1].idx = 0;
188 
189 	if (adi[0].ret <= 0 || adi[1].ret <= 0) return;
190 	if (adi[0].data[0] & 0x20 || ~adi[1].data[0] & 0x20) return;
191 
192 	for (i = 1; i <= adi[1].ret; i++)
193 		adi[0].data[((length - 1) >> 1) + i + 1] = adi[1].data[i];
194 
195 	adi[0].ret += adi[1].ret;
196 	adi[1].ret = -1;
197 }
198 
199 /*
200  * adi_get_bits() gathers bits from the data packet.
201  */
202 
203 static inline int adi_get_bits(struct adi *adi, int count)
204 {
205 	int bits = 0;
206 	int i;
207 	if ((adi->idx += count) > adi->ret) return 0;
208 	for (i = 0; i < count; i++)
209 		bits |= ((adi->data[adi->idx - i] >> 5) & 1) << i;
210 	return bits;
211 }
212 
213 /*
214  * adi_decode() decodes Logitech joystick data into input events.
215  */
216 
217 static int adi_decode(struct adi *adi)
218 {
219 	struct input_dev *dev = adi->dev;
220 	char *abs = adi->abs;
221 	short *key = adi->key;
222 	int i, t;
223 
224 	if (adi->ret < adi->length || adi->id != (adi_get_bits(adi, 4) | (adi_get_bits(adi, 4) << 4)))
225 		return -1;
226 
227 	for (i = 0; i < adi->axes10; i++)
228 		input_report_abs(dev, *abs++, adi_get_bits(adi, 10));
229 
230 	for (i = 0; i < adi->axes8; i++)
231 		input_report_abs(dev, *abs++, adi_get_bits(adi, 8));
232 
233 	for (i = 0; i < adi->buttons && i < 63; i++) {
234 		if (i == adi->pad) {
235 			t = adi_get_bits(adi, 4);
236 			input_report_abs(dev, *abs++, ((t >> 2) & 1) - ( t       & 1));
237 			input_report_abs(dev, *abs++, ((t >> 1) & 1) - ((t >> 3) & 1));
238 		}
239 		input_report_key(dev, *key++, adi_get_bits(adi, 1));
240 	}
241 
242 	for (i = 0; i < adi->hats; i++) {
243 		if ((t = adi_get_bits(adi, 4)) > 8) t = 0;
244 		input_report_abs(dev, *abs++, adi_hat_to_axis[t].x);
245 		input_report_abs(dev, *abs++, adi_hat_to_axis[t].y);
246 	}
247 
248 	for (i = 63; i < adi->buttons; i++)
249 		input_report_key(dev, *key++, adi_get_bits(adi, 1));
250 
251 	input_sync(dev);
252 
253 	return 0;
254 }
255 
256 /*
257  * adi_read() reads the data packet and decodes it.
258  */
259 
260 static int adi_read(struct adi_port *port)
261 {
262 	int i;
263 	int result = 0;
264 
265 	adi_read_packet(port);
266 	adi_move_bits(port, port->adi[0].length);
267 
268 	for (i = 0; i < 2; i++)
269 		if (port->adi[i].length)
270 			 result |= adi_decode(port->adi + i);
271 
272 	return result;
273 }
274 
275 /*
276  * adi_poll() repeatedly polls the Logitech joysticks.
277  */
278 
279 static void adi_poll(struct gameport *gameport)
280 {
281 	struct adi_port *port = gameport_get_drvdata(gameport);
282 
283 	port->bad -= adi_read(port);
284 	port->reads++;
285 }
286 
287 /*
288  * adi_open() is a callback from the input open routine.
289  */
290 
291 static int adi_open(struct input_dev *dev)
292 {
293 	struct adi_port *port = input_get_drvdata(dev);
294 
295 	gameport_start_polling(port->gameport);
296 	return 0;
297 }
298 
299 /*
300  * adi_close() is a callback from the input close routine.
301  */
302 
303 static void adi_close(struct input_dev *dev)
304 {
305 	struct adi_port *port = input_get_drvdata(dev);
306 
307 	gameport_stop_polling(port->gameport);
308 }
309 
310 /*
311  * adi_init_digital() sends a trigger & delay sequence
312  * to reset and initialize a Logitech joystick into digital mode.
313  */
314 
315 static void adi_init_digital(struct gameport *gameport)
316 {
317 	int seq[] = { 4, -2, -3, 10, -6, -11, -7, -9, 11, 0 };
318 	int i;
319 
320 	for (i = 0; seq[i]; i++) {
321 		gameport_trigger(gameport);
322 		if (seq[i] > 0)
323 			msleep(seq[i]);
324 		if (seq[i] < 0) {
325 			mdelay(-seq[i]);
326 			udelay(-seq[i]*14);	/* It looks like mdelay() is off by approx 1.4% */
327 		}
328 	}
329 }
330 
331 static void adi_id_decode(struct adi *adi, struct adi_port *port)
332 {
333 	int i, t;
334 
335 	if (adi->ret < ADI_MIN_ID_LENGTH) /* Minimum ID packet length */
336 		return;
337 
338 	if (adi->ret < (t = adi_get_bits(adi, 10))) {
339 		printk(KERN_WARNING "adi: Short ID packet: reported: %d != read: %d\n", t, adi->ret);
340 		return;
341 	}
342 
343 	adi->id = adi_get_bits(adi, 4) | (adi_get_bits(adi, 4) << 4);
344 
345 	if ((t = adi_get_bits(adi, 4)) & ADI_FLAG_HAT) adi->hats++;
346 
347 	adi->length = adi_get_bits(adi, 10);
348 
349 	if (adi->length >= ADI_MAX_LENGTH || adi->length < ADI_MIN_LENGTH) {
350 		printk(KERN_WARNING "adi: Bad data packet length (%d).\n", adi->length);
351 		adi->length = 0;
352 		return;
353 	}
354 
355 	adi->axes8 = adi_get_bits(adi, 4);
356 	adi->buttons = adi_get_bits(adi, 6);
357 
358 	if (adi_get_bits(adi, 6) != 8 && adi->hats) {
359 		printk(KERN_WARNING "adi: Other than 8-dir POVs not supported yet.\n");
360 		adi->length = 0;
361 		return;
362 	}
363 
364 	adi->buttons += adi_get_bits(adi, 6);
365 	adi->hats += adi_get_bits(adi, 4);
366 
367 	i = adi_get_bits(adi, 4);
368 
369 	if (t & ADI_FLAG_10BIT) {
370 		adi->axes10 = adi->axes8 - i;
371 		adi->axes8 = i;
372 	}
373 
374 	t = adi_get_bits(adi, 4);
375 
376 	for (i = 0; i < t; i++)
377 		adi->cname[i] = adi_get_bits(adi, 8);
378 	adi->cname[i] = 0;
379 
380 	t = 8 + adi->buttons + adi->axes10 * 10 + adi->axes8 * 8 + adi->hats * 4;
381 	if (adi->length != t && adi->length != t + (t & 1)) {
382 		printk(KERN_WARNING "adi: Expected length %d != data length %d\n", t, adi->length);
383 		adi->length = 0;
384 		return;
385 	}
386 
387 	switch (adi->id) {
388 		case ADI_ID_TPD:
389 			adi->pad = 4;
390 			adi->buttons -= 4;
391 			break;
392 		case ADI_ID_WGP:
393 			adi->pad = 0;
394 			adi->buttons -= 4;
395 			break;
396 		default:
397 			adi->pad = -1;
398 			break;
399 	}
400 }
401 
402 static int adi_init_input(struct adi *adi, struct adi_port *port, int half)
403 {
404 	struct input_dev *input_dev;
405 	char buf[ADI_MAX_NAME_LENGTH];
406 	int i, t;
407 
408 	adi->dev = input_dev = input_allocate_device();
409 	if (!input_dev)
410 		return -ENOMEM;
411 
412 	t = adi->id < ADI_ID_MAX ? adi->id : ADI_ID_MAX;
413 
414 	snprintf(buf, ADI_MAX_PHYS_LENGTH, adi_names[t], adi->id);
415 	snprintf(adi->name, ADI_MAX_NAME_LENGTH, "Logitech %s [%s]", buf, adi->cname);
416 	snprintf(adi->phys, ADI_MAX_PHYS_LENGTH, "%s/input%d", port->gameport->phys, half);
417 
418 	adi->abs = adi_abs[t];
419 	adi->key = adi_key[t];
420 
421 	input_dev->name = adi->name;
422 	input_dev->phys = adi->phys;
423 	input_dev->id.bustype = BUS_GAMEPORT;
424 	input_dev->id.vendor = GAMEPORT_ID_VENDOR_LOGITECH;
425 	input_dev->id.product = adi->id;
426 	input_dev->id.version = 0x0100;
427 	input_dev->dev.parent = &port->gameport->dev;
428 
429 	input_set_drvdata(input_dev, port);
430 
431 	input_dev->open = adi_open;
432 	input_dev->close = adi_close;
433 
434 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
435 
436 	for (i = 0; i < adi->axes10 + adi->axes8 + (adi->hats + (adi->pad != -1)) * 2; i++)
437 		set_bit(adi->abs[i], input_dev->absbit);
438 
439 	for (i = 0; i < adi->buttons; i++)
440 		set_bit(adi->key[i], input_dev->keybit);
441 
442 	return 0;
443 }
444 
445 static void adi_init_center(struct adi *adi)
446 {
447 	int i, t, x;
448 
449 	if (!adi->length)
450 		return;
451 
452 	for (i = 0; i < adi->axes10 + adi->axes8 + (adi->hats + (adi->pad != -1)) * 2; i++) {
453 
454 		t = adi->abs[i];
455 		x = adi->dev->abs[t];
456 
457 		if (t == ABS_THROTTLE || t == ABS_RUDDER || adi->id == ADI_ID_WGPE)
458 			x = i < adi->axes10 ? 512 : 128;
459 
460 		if (i < adi->axes10)
461 			input_set_abs_params(adi->dev, t, 64, x * 2 - 64, 2, 16);
462 		else if (i < adi->axes10 + adi->axes8)
463 			input_set_abs_params(adi->dev, t, 48, x * 2 - 48, 1, 16);
464 		else
465 			input_set_abs_params(adi->dev, t, -1, 1, 0, 0);
466 	}
467 }
468 
469 /*
470  * adi_connect() probes for Logitech ADI joysticks.
471  */
472 
473 static int adi_connect(struct gameport *gameport, struct gameport_driver *drv)
474 {
475 	struct adi_port *port;
476 	int i;
477 	int err;
478 
479 	port = kzalloc(sizeof(struct adi_port), GFP_KERNEL);
480 	if (!port)
481 		return -ENOMEM;
482 
483 	port->gameport = gameport;
484 
485 	gameport_set_drvdata(gameport, port);
486 
487 	err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
488 	if (err)
489 		goto fail1;
490 
491 	adi_init_digital(gameport);
492 	adi_read_packet(port);
493 
494 	if (port->adi[0].ret >= ADI_MIN_LEN_LENGTH)
495 		adi_move_bits(port, adi_get_bits(port->adi, 10));
496 
497 	for (i = 0; i < 2; i++) {
498 		adi_id_decode(port->adi + i, port);
499 
500 		if (!port->adi[i].length)
501 			continue;
502 
503 		err = adi_init_input(port->adi + i, port, i);
504 		if (err)
505 			goto fail2;
506 	}
507 
508 	if (!port->adi[0].length && !port->adi[1].length) {
509 		err = -ENODEV;
510 		goto fail2;
511 	}
512 
513 	gameport_set_poll_handler(gameport, adi_poll);
514 	gameport_set_poll_interval(gameport, 20);
515 
516 	msleep(ADI_INIT_DELAY);
517 	if (adi_read(port)) {
518 		msleep(ADI_DATA_DELAY);
519 		adi_read(port);
520 	}
521 
522 	for (i = 0; i < 2; i++)
523 		if (port->adi[i].length > 0) {
524 			adi_init_center(port->adi + i);
525 			err = input_register_device(port->adi[i].dev);
526 			if (err)
527 				goto fail3;
528 		}
529 
530 	return 0;
531 
532  fail3: while (--i >= 0) {
533 		if (port->adi[i].length > 0) {
534 			input_unregister_device(port->adi[i].dev);
535 			port->adi[i].dev = NULL;
536 		}
537 	}
538  fail2:	for (i = 0; i < 2; i++)
539 		if (port->adi[i].dev)
540 			input_free_device(port->adi[i].dev);
541 	gameport_close(gameport);
542  fail1:	gameport_set_drvdata(gameport, NULL);
543 	kfree(port);
544 	return err;
545 }
546 
547 static void adi_disconnect(struct gameport *gameport)
548 {
549 	int i;
550 	struct adi_port *port = gameport_get_drvdata(gameport);
551 
552 	for (i = 0; i < 2; i++)
553 		if (port->adi[i].length > 0)
554 			input_unregister_device(port->adi[i].dev);
555 	gameport_close(gameport);
556 	gameport_set_drvdata(gameport, NULL);
557 	kfree(port);
558 }
559 
560 /*
561  * The gameport device structure.
562  */
563 
564 static struct gameport_driver adi_drv = {
565 	.driver		= {
566 		.name	= "adi",
567 	},
568 	.description	= DRIVER_DESC,
569 	.connect	= adi_connect,
570 	.disconnect	= adi_disconnect,
571 };
572 
573 static int __init adi_init(void)
574 {
575 	gameport_register_driver(&adi_drv);
576 	return 0;
577 }
578 
579 static void __exit adi_exit(void)
580 {
581 	gameport_unregister_driver(&adi_drv);
582 }
583 
584 module_init(adi_init);
585 module_exit(adi_exit);
586