xref: /linux/drivers/input/joystick/a3d.c (revision bd628c1bed7902ec1f24ba0fe70758949146abbe)
1 /*
2  *  Copyright (c) 1998-2001 Vojtech Pavlik
3  */
4 
5 /*
6  * FP-Gaming Assassin 3D joystick 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 
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/gameport.h>
29 #include <linux/input.h>
30 #include <linux/jiffies.h>
31 
32 #define DRIVER_DESC	"FP-Gaming Assassin 3D joystick driver"
33 
34 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
35 MODULE_DESCRIPTION(DRIVER_DESC);
36 MODULE_LICENSE("GPL");
37 
38 #define A3D_MAX_START		600	/* 600 us */
39 #define A3D_MAX_STROBE		80	/* 80 us */
40 #define A3D_MAX_LENGTH		40	/* 40*3 bits */
41 
42 #define A3D_MODE_A3D		1	/* Assassin 3D */
43 #define A3D_MODE_PAN		2	/* Panther */
44 #define A3D_MODE_OEM		3	/* Panther OEM version */
45 #define A3D_MODE_PXL		4	/* Panther XL */
46 
47 static char *a3d_names[] = { NULL, "FP-Gaming Assassin 3D", "MadCatz Panther", "OEM Panther",
48 			"MadCatz Panther XL", "MadCatz Panther XL w/ rudder" };
49 
50 struct a3d {
51 	struct gameport *gameport;
52 	struct gameport *adc;
53 	struct input_dev *dev;
54 	int axes[4];
55 	int buttons;
56 	int mode;
57 	int length;
58 	int reads;
59 	int bads;
60 	char phys[32];
61 };
62 
63 /*
64  * a3d_read_packet() reads an Assassin 3D packet.
65  */
66 
67 static int a3d_read_packet(struct gameport *gameport, int length, char *data)
68 {
69 	unsigned long flags;
70 	unsigned char u, v;
71 	unsigned int t, s;
72 	int i;
73 
74 	i = 0;
75 	t = gameport_time(gameport, A3D_MAX_START);
76 	s = gameport_time(gameport, A3D_MAX_STROBE);
77 
78 	local_irq_save(flags);
79 	gameport_trigger(gameport);
80 	v = gameport_read(gameport);
81 
82 	while (t > 0 && i < length) {
83 		t--;
84 		u = v; v = gameport_read(gameport);
85 		if (~v & u & 0x10) {
86 			data[i++] = v >> 5;
87 			t = s;
88 		}
89 	}
90 
91 	local_irq_restore(flags);
92 
93 	return i;
94 }
95 
96 /*
97  * a3d_csum() computes checksum of triplet packet
98  */
99 
100 static int a3d_csum(char *data, int count)
101 {
102 	int i, csum = 0;
103 
104 	for (i = 0; i < count - 2; i++)
105 		csum += data[i];
106 	return (csum & 0x3f) != ((data[count - 2] << 3) | data[count - 1]);
107 }
108 
109 static void a3d_read(struct a3d *a3d, unsigned char *data)
110 {
111 	struct input_dev *dev = a3d->dev;
112 
113 	switch (a3d->mode) {
114 
115 		case A3D_MODE_A3D:
116 		case A3D_MODE_OEM:
117 		case A3D_MODE_PAN:
118 
119 			input_report_rel(dev, REL_X, ((data[5] << 6) | (data[6] << 3) | data[ 7]) - ((data[5] & 4) << 7));
120 			input_report_rel(dev, REL_Y, ((data[8] << 6) | (data[9] << 3) | data[10]) - ((data[8] & 4) << 7));
121 
122 			input_report_key(dev, BTN_RIGHT,  data[2] & 1);
123 			input_report_key(dev, BTN_LEFT,   data[3] & 2);
124 			input_report_key(dev, BTN_MIDDLE, data[3] & 4);
125 
126 			input_sync(dev);
127 
128 			a3d->axes[0] = ((signed char)((data[11] << 6) | (data[12] << 3) | (data[13]))) + 128;
129 			a3d->axes[1] = ((signed char)((data[14] << 6) | (data[15] << 3) | (data[16]))) + 128;
130 			a3d->axes[2] = ((signed char)((data[17] << 6) | (data[18] << 3) | (data[19]))) + 128;
131 			a3d->axes[3] = ((signed char)((data[20] << 6) | (data[21] << 3) | (data[22]))) + 128;
132 
133 			a3d->buttons = ((data[3] << 3) | data[4]) & 0xf;
134 
135 			break;
136 
137 		case A3D_MODE_PXL:
138 
139 			input_report_rel(dev, REL_X, ((data[ 9] << 6) | (data[10] << 3) | data[11]) - ((data[ 9] & 4) << 7));
140 			input_report_rel(dev, REL_Y, ((data[12] << 6) | (data[13] << 3) | data[14]) - ((data[12] & 4) << 7));
141 
142 			input_report_key(dev, BTN_RIGHT,  data[2] & 1);
143 			input_report_key(dev, BTN_LEFT,   data[3] & 2);
144 			input_report_key(dev, BTN_MIDDLE, data[3] & 4);
145 			input_report_key(dev, BTN_SIDE,   data[7] & 2);
146 			input_report_key(dev, BTN_EXTRA,  data[7] & 4);
147 
148 			input_report_abs(dev, ABS_X,        ((signed char)((data[15] << 6) | (data[16] << 3) | (data[17]))) + 128);
149 			input_report_abs(dev, ABS_Y,        ((signed char)((data[18] << 6) | (data[19] << 3) | (data[20]))) + 128);
150 			input_report_abs(dev, ABS_RUDDER,   ((signed char)((data[21] << 6) | (data[22] << 3) | (data[23]))) + 128);
151 			input_report_abs(dev, ABS_THROTTLE, ((signed char)((data[24] << 6) | (data[25] << 3) | (data[26]))) + 128);
152 
153 			input_report_abs(dev, ABS_HAT0X, ( data[5]       & 1) - ((data[5] >> 2) & 1));
154 			input_report_abs(dev, ABS_HAT0Y, ((data[5] >> 1) & 1) - ((data[6] >> 2) & 1));
155 			input_report_abs(dev, ABS_HAT1X, ((data[4] >> 1) & 1) - ( data[3]       & 1));
156 			input_report_abs(dev, ABS_HAT1Y, ((data[4] >> 2) & 1) - ( data[4]       & 1));
157 
158 			input_report_key(dev, BTN_TRIGGER, data[8] & 1);
159 			input_report_key(dev, BTN_THUMB,   data[8] & 2);
160 			input_report_key(dev, BTN_TOP,     data[8] & 4);
161 			input_report_key(dev, BTN_PINKIE,  data[7] & 1);
162 
163 			input_sync(dev);
164 
165 			break;
166 	}
167 }
168 
169 
170 /*
171  * a3d_poll() reads and analyzes A3D joystick data.
172  */
173 
174 static void a3d_poll(struct gameport *gameport)
175 {
176 	struct a3d *a3d = gameport_get_drvdata(gameport);
177 	unsigned char data[A3D_MAX_LENGTH];
178 
179 	a3d->reads++;
180 	if (a3d_read_packet(a3d->gameport, a3d->length, data) != a3d->length ||
181 	    data[0] != a3d->mode || a3d_csum(data, a3d->length))
182 		a3d->bads++;
183 	else
184 		a3d_read(a3d, data);
185 }
186 
187 /*
188  * a3d_adc_cooked_read() copies the acis and button data to the
189  * callers arrays. It could do the read itself, but the caller could
190  * call this more than 50 times a second, which would use too much CPU.
191  */
192 
193 static int a3d_adc_cooked_read(struct gameport *gameport, int *axes, int *buttons)
194 {
195 	struct a3d *a3d = gameport->port_data;
196 	int i;
197 
198 	for (i = 0; i < 4; i++)
199 		axes[i] = (a3d->axes[i] < 254) ? a3d->axes[i] : -1;
200 	*buttons = a3d->buttons;
201 	return 0;
202 }
203 
204 /*
205  * a3d_adc_open() is the gameport open routine. It refuses to serve
206  * any but cooked data.
207  */
208 
209 static int a3d_adc_open(struct gameport *gameport, int mode)
210 {
211 	struct a3d *a3d = gameport->port_data;
212 
213 	if (mode != GAMEPORT_MODE_COOKED)
214 		return -1;
215 
216 	gameport_start_polling(a3d->gameport);
217 	return 0;
218 }
219 
220 /*
221  * a3d_adc_close() is a callback from the input close routine.
222  */
223 
224 static void a3d_adc_close(struct gameport *gameport)
225 {
226 	struct a3d *a3d = gameport->port_data;
227 
228 	gameport_stop_polling(a3d->gameport);
229 }
230 
231 /*
232  * a3d_open() is a callback from the input open routine.
233  */
234 
235 static int a3d_open(struct input_dev *dev)
236 {
237 	struct a3d *a3d = input_get_drvdata(dev);
238 
239 	gameport_start_polling(a3d->gameport);
240 	return 0;
241 }
242 
243 /*
244  * a3d_close() is a callback from the input close routine.
245  */
246 
247 static void a3d_close(struct input_dev *dev)
248 {
249 	struct a3d *a3d = input_get_drvdata(dev);
250 
251 	gameport_stop_polling(a3d->gameport);
252 }
253 
254 /*
255  * a3d_connect() probes for A3D joysticks.
256  */
257 
258 static int a3d_connect(struct gameport *gameport, struct gameport_driver *drv)
259 {
260 	struct a3d *a3d;
261 	struct input_dev *input_dev;
262 	struct gameport *adc;
263 	unsigned char data[A3D_MAX_LENGTH];
264 	int i;
265 	int err;
266 
267 	a3d = kzalloc(sizeof(struct a3d), GFP_KERNEL);
268 	input_dev = input_allocate_device();
269 	if (!a3d || !input_dev) {
270 		err = -ENOMEM;
271 		goto fail1;
272 	}
273 
274 	a3d->dev = input_dev;
275 	a3d->gameport = gameport;
276 
277 	gameport_set_drvdata(gameport, a3d);
278 
279 	err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
280 	if (err)
281 		goto fail1;
282 
283 	i = a3d_read_packet(gameport, A3D_MAX_LENGTH, data);
284 
285 	if (!i || a3d_csum(data, i)) {
286 		err = -ENODEV;
287 		goto fail2;
288 	}
289 
290 	a3d->mode = data[0];
291 
292 	if (!a3d->mode || a3d->mode > 5) {
293 		printk(KERN_WARNING "a3d.c: Unknown A3D device detected "
294 			"(%s, id=%d), contact <vojtech@ucw.cz>\n", gameport->phys, a3d->mode);
295 		err = -ENODEV;
296 		goto fail2;
297 	}
298 
299 	gameport_set_poll_handler(gameport, a3d_poll);
300 	gameport_set_poll_interval(gameport, 20);
301 
302 	snprintf(a3d->phys, sizeof(a3d->phys), "%s/input0", gameport->phys);
303 
304 	input_dev->name = a3d_names[a3d->mode];
305 	input_dev->phys = a3d->phys;
306 	input_dev->id.bustype = BUS_GAMEPORT;
307 	input_dev->id.vendor = GAMEPORT_ID_VENDOR_MADCATZ;
308 	input_dev->id.product = a3d->mode;
309 	input_dev->id.version = 0x0100;
310 	input_dev->dev.parent = &gameport->dev;
311 	input_dev->open = a3d_open;
312 	input_dev->close = a3d_close;
313 
314 	input_set_drvdata(input_dev, a3d);
315 
316 	if (a3d->mode == A3D_MODE_PXL) {
317 
318 		int axes[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_RUDDER };
319 
320 		a3d->length = 33;
321 
322 		input_dev->evbit[0] |= BIT_MASK(EV_ABS) | BIT_MASK(EV_KEY) |
323 			BIT_MASK(EV_REL);
324 		input_dev->relbit[0] |= BIT_MASK(REL_X) | BIT_MASK(REL_Y);
325 		input_dev->absbit[0] |= BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) |
326 			BIT_MASK(ABS_THROTTLE) | BIT_MASK(ABS_RUDDER) |
327 			BIT_MASK(ABS_HAT0X) | BIT_MASK(ABS_HAT0Y) |
328 			BIT_MASK(ABS_HAT1X) | BIT_MASK(ABS_HAT1Y);
329 		input_dev->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_RIGHT) |
330 			BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) |
331 			BIT_MASK(BTN_SIDE) | BIT_MASK(BTN_EXTRA);
332 		input_dev->keybit[BIT_WORD(BTN_JOYSTICK)] |=
333 			BIT_MASK(BTN_TRIGGER) | BIT_MASK(BTN_THUMB) |
334 			BIT_MASK(BTN_TOP) | BIT_MASK(BTN_PINKIE);
335 
336 		a3d_read(a3d, data);
337 
338 		for (i = 0; i < 4; i++) {
339 			if (i < 2)
340 				input_set_abs_params(input_dev, axes[i],
341 					48, input_abs_get_val(input_dev, axes[i]) * 2 - 48, 0, 8);
342 			else
343 				input_set_abs_params(input_dev, axes[i], 2, 253, 0, 0);
344 			input_set_abs_params(input_dev, ABS_HAT0X + i, -1, 1, 0, 0);
345 		}
346 
347 	} else {
348 		a3d->length = 29;
349 
350 		input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
351 		input_dev->relbit[0] |= BIT_MASK(REL_X) | BIT_MASK(REL_Y);
352 		input_dev->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_RIGHT) |
353 			BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE);
354 
355 		a3d_read(a3d, data);
356 
357 		if (!(a3d->adc = adc = gameport_allocate_port()))
358 			printk(KERN_ERR "a3d: Not enough memory for ADC port\n");
359 		else {
360 			adc->port_data = a3d;
361 			adc->open = a3d_adc_open;
362 			adc->close = a3d_adc_close;
363 			adc->cooked_read = a3d_adc_cooked_read;
364 			adc->fuzz = 1;
365 
366 			gameport_set_name(adc, a3d_names[a3d->mode]);
367 			gameport_set_phys(adc, "%s/gameport0", gameport->phys);
368 			adc->dev.parent = &gameport->dev;
369 
370 			gameport_register_port(adc);
371 		}
372 	}
373 
374 	err = input_register_device(a3d->dev);
375 	if (err)
376 		goto fail3;
377 
378 	return 0;
379 
380  fail3:	if (a3d->adc)
381 		gameport_unregister_port(a3d->adc);
382  fail2:	gameport_close(gameport);
383  fail1:	gameport_set_drvdata(gameport, NULL);
384 	input_free_device(input_dev);
385 	kfree(a3d);
386 	return err;
387 }
388 
389 static void a3d_disconnect(struct gameport *gameport)
390 {
391 	struct a3d *a3d = gameport_get_drvdata(gameport);
392 
393 	input_unregister_device(a3d->dev);
394 	if (a3d->adc)
395 		gameport_unregister_port(a3d->adc);
396 	gameport_close(gameport);
397 	gameport_set_drvdata(gameport, NULL);
398 	kfree(a3d);
399 }
400 
401 static struct gameport_driver a3d_drv = {
402 	.driver		= {
403 		.name	= "adc",
404 		.owner	= THIS_MODULE,
405 	},
406 	.description	= DRIVER_DESC,
407 	.connect	= a3d_connect,
408 	.disconnect	= a3d_disconnect,
409 };
410 
411 module_gameport_driver(a3d_drv);
412