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