xref: /openbmc/linux/drivers/input/joystick/adi.c (revision 0da85d1e)
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/jiffies.h>
37 
38 #define DRIVER_DESC	"Logitech ADI joystick family driver"
39 
40 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
41 MODULE_DESCRIPTION(DRIVER_DESC);
42 MODULE_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	64
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 
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" };
77 
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 };
83 
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 };
88 
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 };
91 
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 };
94 
95 /*
96  * Hat to axis conversion arrays.
97  */
98 
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}};
103 
104 /*
105  * Per-port information.
106  */
107 
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];
125 };
126 
127 struct 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 
138 static 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 
181 static 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 
202 static 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 
216 static 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 
259 static 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 
278 static 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 
290 static int adi_open(struct input_dev *dev)
291 {
292 	struct adi_port *port = input_get_drvdata(dev);
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 
302 static void adi_close(struct input_dev *dev)
303 {
304 	struct adi_port *port = input_get_drvdata(dev);
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 
314 static 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)
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% */
326 		}
327 	}
328 }
329 
330 static void adi_id_decode(struct adi *adi, struct adi_port *port)
331 {
332 	int i, t;
333 
334 	if (adi->ret < ADI_MIN_ID_LENGTH) /* Minimum ID packet length */
335 		return;
336 
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;
340 	}
341 
342 	adi->id = adi_get_bits(adi, 4) | (adi_get_bits(adi, 4) << 4);
343 
344 	if ((t = adi_get_bits(adi, 4)) & ADI_FLAG_HAT) adi->hats++;
345 
346 	adi->length = adi_get_bits(adi, 10);
347 
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;
352 	}
353 
354 	adi->axes8 = adi_get_bits(adi, 4);
355 	adi->buttons = adi_get_bits(adi, 6);
356 
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;
361 	}
362 
363 	adi->buttons += adi_get_bits(adi, 6);
364 	adi->hats += adi_get_bits(adi, 4);
365 
366 	i = adi_get_bits(adi, 4);
367 
368 	if (t & ADI_FLAG_10BIT) {
369 		adi->axes10 = adi->axes8 - i;
370 		adi->axes8 = i;
371 	}
372 
373 	t = adi_get_bits(adi, 4);
374 
375 	for (i = 0; i < t; i++)
376 		adi->cname[i] = adi_get_bits(adi, 8);
377 	adi->cname[i] = 0;
378 
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;
384 	}
385 
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;
398 	}
399 }
400 
401 static int adi_init_input(struct adi *adi, struct adi_port *port, int half)
402 {
403 	struct input_dev *input_dev;
404 	char buf[ADI_MAX_NAME_LENGTH];
405 	int i, t;
406 
407 	adi->dev = input_dev = input_allocate_device();
408 	if (!input_dev)
409 		return -ENOMEM;
410 
411 	t = adi->id < ADI_ID_MAX ? adi->id : ADI_ID_MAX;
412 
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);
416 
417 	adi->abs = adi_abs[t];
418 	adi->key = adi_key[t];
419 
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;
427 
428 	input_set_drvdata(input_dev, port);
429 
430 	input_dev->open = adi_open;
431 	input_dev->close = adi_close;
432 
433 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
434 
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);
437 
438 	for (i = 0; i < adi->buttons; i++)
439 		set_bit(adi->key[i], input_dev->keybit);
440 
441 	return 0;
442 }
443 
444 static void adi_init_center(struct adi *adi)
445 {
446 	int i, t, x;
447 
448 	if (!adi->length)
449 		return;
450 
451 	for (i = 0; i < adi->axes10 + adi->axes8 + (adi->hats + (adi->pad != -1)) * 2; i++) {
452 
453 		t = adi->abs[i];
454 		x = input_abs_get_val(adi->dev, t);
455 
456 		if (t == ABS_THROTTLE || t == ABS_RUDDER || adi->id == ADI_ID_WGPE)
457 			x = i < adi->axes10 ? 512 : 128;
458 
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);
465 	}
466 }
467 
468 /*
469  * adi_connect() probes for Logitech ADI joysticks.
470  */
471 
472 static int adi_connect(struct gameport *gameport, struct gameport_driver *drv)
473 {
474 	struct adi_port *port;
475 	int i;
476 	int err;
477 
478 	port = kzalloc(sizeof(struct adi_port), GFP_KERNEL);
479 	if (!port)
480 		return -ENOMEM;
481 
482 	port->gameport = gameport;
483 
484 	gameport_set_drvdata(gameport, port);
485 
486 	err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
487 	if (err)
488 		goto fail1;
489 
490 	adi_init_digital(gameport);
491 	adi_read_packet(port);
492 
493 	if (port->adi[0].ret >= ADI_MIN_LEN_LENGTH)
494 		adi_move_bits(port, adi_get_bits(port->adi, 10));
495 
496 	for (i = 0; i < 2; i++) {
497 		adi_id_decode(port->adi + i, port);
498 
499 		if (!port->adi[i].length)
500 			continue;
501 
502 		err = adi_init_input(port->adi + i, port, i);
503 		if (err)
504 			goto fail2;
505 	}
506 
507 	if (!port->adi[0].length && !port->adi[1].length) {
508 		err = -ENODEV;
509 		goto fail2;
510 	}
511 
512 	gameport_set_poll_handler(gameport, adi_poll);
513 	gameport_set_poll_interval(gameport, 20);
514 
515 	msleep(ADI_INIT_DELAY);
516 	if (adi_read(port)) {
517 		msleep(ADI_DATA_DELAY);
518 		adi_read(port);
519 	}
520 
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;
527 		}
528 
529 	return 0;
530 
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;
535 		}
536 	}
537  fail2:	for (i = 0; i < 2; i++)
538 		input_free_device(port->adi[i].dev);
539 	gameport_close(gameport);
540  fail1:	gameport_set_drvdata(gameport, NULL);
541 	kfree(port);
542 	return err;
543 }
544 
545 static void adi_disconnect(struct gameport *gameport)
546 {
547 	int i;
548 	struct adi_port *port = gameport_get_drvdata(gameport);
549 
550 	for (i = 0; i < 2; i++)
551 		if (port->adi[i].length > 0)
552 			input_unregister_device(port->adi[i].dev);
553 	gameport_close(gameport);
554 	gameport_set_drvdata(gameport, NULL);
555 	kfree(port);
556 }
557 
558 static struct gameport_driver adi_drv = {
559 	.driver		= {
560 		.name	= "adi",
561 	},
562 	.description	= DRIVER_DESC,
563 	.connect	= adi_connect,
564 	.disconnect	= adi_disconnect,
565 };
566 
567 module_gameport_driver(adi_drv);
568