blob: 52ba16f487c793a36acc464dc4c0e3cc80e77fbc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * $Id: a3d.c,v 1.21 2002/01/22 20:11:50 vojtech Exp $
3 *
4 * Copyright (c) 1998-2001 Vojtech Pavlik
5 */
6
7/*
8 * FP-Gaming Assasin 3D joystick driver for Linux
9 */
10
11/*
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Should you need to contact me, the author, you can do so either by
27 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
28 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
29 */
30
31#include <linux/kernel.h>
32#include <linux/module.h>
33#include <linux/slab.h>
34#include <linux/init.h>
35#include <linux/gameport.h>
36#include <linux/input.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080037#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#define DRIVER_DESC "FP-Gaming Assasin 3D joystick driver"
40
41MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
42MODULE_DESCRIPTION(DRIVER_DESC);
43MODULE_LICENSE("GPL");
44
45#define A3D_MAX_START 600 /* 600 us */
46#define A3D_MAX_STROBE 80 /* 80 us */
47#define A3D_MAX_LENGTH 40 /* 40*3 bits */
48
49#define A3D_MODE_A3D 1 /* Assassin 3D */
50#define A3D_MODE_PAN 2 /* Panther */
51#define A3D_MODE_OEM 3 /* Panther OEM version */
52#define A3D_MODE_PXL 4 /* Panther XL */
53
54static char *a3d_names[] = { NULL, "FP-Gaming Assassin 3D", "MadCatz Panther", "OEM Panther",
55 "MadCatz Panther XL", "MadCatz Panther XL w/ rudder" };
56
57struct a3d {
58 struct gameport *gameport;
59 struct gameport *adc;
Dmitry Torokhov275c6ce2006-01-29 21:52:39 -050060 struct input_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 int axes[4];
62 int buttons;
63 int mode;
64 int length;
65 int reads;
66 int bads;
67 char phys[32];
68};
69
70/*
71 * a3d_read_packet() reads an Assassin 3D packet.
72 */
73
74static int a3d_read_packet(struct gameport *gameport, int length, char *data)
75{
76 unsigned long flags;
77 unsigned char u, v;
78 unsigned int t, s;
79 int i;
80
81 i = 0;
82 t = gameport_time(gameport, A3D_MAX_START);
83 s = gameport_time(gameport, A3D_MAX_STROBE);
84
85 local_irq_save(flags);
86 gameport_trigger(gameport);
87 v = gameport_read(gameport);
88
89 while (t > 0 && i < length) {
90 t--;
91 u = v; v = gameport_read(gameport);
92 if (~v & u & 0x10) {
93 data[i++] = v >> 5;
94 t = s;
95 }
96 }
97
98 local_irq_restore(flags);
99
100 return i;
101}
102
103/*
104 * a3d_csum() computes checksum of triplet packet
105 */
106
107static int a3d_csum(char *data, int count)
108{
109 int i, csum = 0;
110
111 for (i = 0; i < count - 2; i++)
112 csum += data[i];
113 return (csum & 0x3f) != ((data[count - 2] << 3) | data[count - 1]);
114}
115
116static void a3d_read(struct a3d *a3d, unsigned char *data)
117{
Dmitry Torokhov275c6ce2006-01-29 21:52:39 -0500118 struct input_dev *dev = a3d->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 switch (a3d->mode) {
121
122 case A3D_MODE_A3D:
123 case A3D_MODE_OEM:
124 case A3D_MODE_PAN:
125
126 input_report_rel(dev, REL_X, ((data[5] << 6) | (data[6] << 3) | data[ 7]) - ((data[5] & 4) << 7));
127 input_report_rel(dev, REL_Y, ((data[8] << 6) | (data[9] << 3) | data[10]) - ((data[8] & 4) << 7));
128
129 input_report_key(dev, BTN_RIGHT, data[2] & 1);
130 input_report_key(dev, BTN_LEFT, data[3] & 2);
131 input_report_key(dev, BTN_MIDDLE, data[3] & 4);
132
133 input_sync(dev);
134
135 a3d->axes[0] = ((signed char)((data[11] << 6) | (data[12] << 3) | (data[13]))) + 128;
136 a3d->axes[1] = ((signed char)((data[14] << 6) | (data[15] << 3) | (data[16]))) + 128;
137 a3d->axes[2] = ((signed char)((data[17] << 6) | (data[18] << 3) | (data[19]))) + 128;
138 a3d->axes[3] = ((signed char)((data[20] << 6) | (data[21] << 3) | (data[22]))) + 128;
139
140 a3d->buttons = ((data[3] << 3) | data[4]) & 0xf;
141
142 break;
143
144 case A3D_MODE_PXL:
145
146 input_report_rel(dev, REL_X, ((data[ 9] << 6) | (data[10] << 3) | data[11]) - ((data[ 9] & 4) << 7));
147 input_report_rel(dev, REL_Y, ((data[12] << 6) | (data[13] << 3) | data[14]) - ((data[12] & 4) << 7));
148
149 input_report_key(dev, BTN_RIGHT, data[2] & 1);
150 input_report_key(dev, BTN_LEFT, data[3] & 2);
151 input_report_key(dev, BTN_MIDDLE, data[3] & 4);
152 input_report_key(dev, BTN_SIDE, data[7] & 2);
153 input_report_key(dev, BTN_EXTRA, data[7] & 4);
154
155 input_report_abs(dev, ABS_X, ((signed char)((data[15] << 6) | (data[16] << 3) | (data[17]))) + 128);
156 input_report_abs(dev, ABS_Y, ((signed char)((data[18] << 6) | (data[19] << 3) | (data[20]))) + 128);
157 input_report_abs(dev, ABS_RUDDER, ((signed char)((data[21] << 6) | (data[22] << 3) | (data[23]))) + 128);
158 input_report_abs(dev, ABS_THROTTLE, ((signed char)((data[24] << 6) | (data[25] << 3) | (data[26]))) + 128);
159
160 input_report_abs(dev, ABS_HAT0X, ( data[5] & 1) - ((data[5] >> 2) & 1));
161 input_report_abs(dev, ABS_HAT0Y, ((data[5] >> 1) & 1) - ((data[6] >> 2) & 1));
162 input_report_abs(dev, ABS_HAT1X, ((data[4] >> 1) & 1) - ( data[3] & 1));
163 input_report_abs(dev, ABS_HAT1Y, ((data[4] >> 2) & 1) - ( data[4] & 1));
164
165 input_report_key(dev, BTN_TRIGGER, data[8] & 1);
166 input_report_key(dev, BTN_THUMB, data[8] & 2);
167 input_report_key(dev, BTN_TOP, data[8] & 4);
168 input_report_key(dev, BTN_PINKIE, data[7] & 1);
169
170 input_sync(dev);
171
172 break;
173 }
174}
175
176
177/*
178 * a3d_poll() reads and analyzes A3D joystick data.
179 */
180
181static void a3d_poll(struct gameport *gameport)
182{
183 struct a3d *a3d = gameport_get_drvdata(gameport);
184 unsigned char data[A3D_MAX_LENGTH];
185
186 a3d->reads++;
187 if (a3d_read_packet(a3d->gameport, a3d->length, data) != a3d->length ||
188 data[0] != a3d->mode || a3d_csum(data, a3d->length))
Dmitry Torokhovab0c3442005-05-29 02:28:55 -0500189 a3d->bads++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 else
191 a3d_read(a3d, data);
192}
193
194/*
195 * a3d_adc_cooked_read() copies the acis and button data to the
196 * callers arrays. It could do the read itself, but the caller could
197 * call this more than 50 times a second, which would use too much CPU.
198 */
199
200static int a3d_adc_cooked_read(struct gameport *gameport, int *axes, int *buttons)
201{
202 struct a3d *a3d = gameport->port_data;
203 int i;
204
205 for (i = 0; i < 4; i++)
206 axes[i] = (a3d->axes[i] < 254) ? a3d->axes[i] : -1;
207 *buttons = a3d->buttons;
208 return 0;
209}
210
211/*
212 * a3d_adc_open() is the gameport open routine. It refuses to serve
213 * any but cooked data.
214 */
215
216static int a3d_adc_open(struct gameport *gameport, int mode)
217{
218 struct a3d *a3d = gameport->port_data;
219
220 if (mode != GAMEPORT_MODE_COOKED)
221 return -1;
222
223 gameport_start_polling(a3d->gameport);
224 return 0;
225}
226
227/*
228 * a3d_adc_close() is a callback from the input close routine.
229 */
230
231static void a3d_adc_close(struct gameport *gameport)
232{
233 struct a3d *a3d = gameport->port_data;
234
235 gameport_stop_polling(a3d->gameport);
236}
237
238/*
239 * a3d_open() is a callback from the input open routine.
240 */
241
242static int a3d_open(struct input_dev *dev)
243{
Dmitry Torokhov8715c1c2007-04-12 01:34:14 -0400244 struct a3d *a3d = input_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 gameport_start_polling(a3d->gameport);
247 return 0;
248}
249
250/*
251 * a3d_close() is a callback from the input close routine.
252 */
253
254static void a3d_close(struct input_dev *dev)
255{
Dmitry Torokhov8715c1c2007-04-12 01:34:14 -0400256 struct a3d *a3d = input_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 gameport_stop_polling(a3d->gameport);
259}
260
261/*
262 * a3d_connect() probes for A3D joysticks.
263 */
264
265static int a3d_connect(struct gameport *gameport, struct gameport_driver *drv)
266{
267 struct a3d *a3d;
Dmitry Torokhov275c6ce2006-01-29 21:52:39 -0500268 struct input_dev *input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 struct gameport *adc;
270 unsigned char data[A3D_MAX_LENGTH];
271 int i;
272 int err;
273
Dmitry Torokhov275c6ce2006-01-29 21:52:39 -0500274 a3d = kzalloc(sizeof(struct a3d), GFP_KERNEL);
275 input_dev = input_allocate_device();
276 if (!a3d || !input_dev) {
277 err = -ENOMEM;
278 goto fail1;
279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Dmitry Torokhov275c6ce2006-01-29 21:52:39 -0500281 a3d->dev = input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 a3d->gameport = gameport;
283
284 gameport_set_drvdata(gameport, a3d);
285
286 err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
287 if (err)
288 goto fail1;
289
290 i = a3d_read_packet(gameport, A3D_MAX_LENGTH, data);
291
292 if (!i || a3d_csum(data, i)) {
293 err = -ENODEV;
294 goto fail2;
295 }
296
297 a3d->mode = data[0];
298
299 if (!a3d->mode || a3d->mode > 5) {
300 printk(KERN_WARNING "a3d.c: Unknown A3D device detected "
301 "(%s, id=%d), contact <vojtech@ucw.cz>\n", gameport->phys, a3d->mode);
302 err = -ENODEV;
303 goto fail2;
304 }
305
306 gameport_set_poll_handler(gameport, a3d_poll);
307 gameport_set_poll_interval(gameport, 20);
308
Dmitry Torokhov10ca4c02006-06-26 01:45:48 -0400309 snprintf(a3d->phys, sizeof(a3d->phys), "%s/input0", gameport->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Dmitry Torokhov275c6ce2006-01-29 21:52:39 -0500311 input_dev->name = a3d_names[a3d->mode];
312 input_dev->phys = a3d->phys;
313 input_dev->id.bustype = BUS_GAMEPORT;
314 input_dev->id.vendor = GAMEPORT_ID_VENDOR_MADCATZ;
315 input_dev->id.product = a3d->mode;
316 input_dev->id.version = 0x0100;
Dmitry Torokhov935e6582007-04-12 01:35:26 -0400317 input_dev->dev.parent = &gameport->dev;
Dmitry Torokhov275c6ce2006-01-29 21:52:39 -0500318 input_dev->open = a3d_open;
319 input_dev->close = a3d_close;
320
Dmitry Torokhov8715c1c2007-04-12 01:34:14 -0400321 input_set_drvdata(input_dev, a3d);
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (a3d->mode == A3D_MODE_PXL) {
324
325 int axes[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_RUDDER };
326
327 a3d->length = 33;
328
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700329 input_dev->evbit[0] |= BIT_MASK(EV_ABS) | BIT_MASK(EV_KEY) |
330 BIT_MASK(EV_REL);
331 input_dev->relbit[0] |= BIT_MASK(REL_X) | BIT_MASK(REL_Y);
332 input_dev->absbit[0] |= BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) |
333 BIT_MASK(ABS_THROTTLE) | BIT_MASK(ABS_RUDDER) |
334 BIT_MASK(ABS_HAT0X) | BIT_MASK(ABS_HAT0Y) |
335 BIT_MASK(ABS_HAT1X) | BIT_MASK(ABS_HAT1Y);
336 input_dev->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_RIGHT) |
337 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) |
338 BIT_MASK(BTN_SIDE) | BIT_MASK(BTN_EXTRA);
339 input_dev->keybit[BIT_WORD(BTN_JOYSTICK)] |=
340 BIT_MASK(BTN_TRIGGER) | BIT_MASK(BTN_THUMB) |
341 BIT_MASK(BTN_TOP) | BIT_MASK(BTN_PINKIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 a3d_read(a3d, data);
344
345 for (i = 0; i < 4; i++) {
346 if (i < 2)
Dmitry Torokhov275c6ce2006-01-29 21:52:39 -0500347 input_set_abs_params(input_dev, axes[i], 48, input_dev->abs[axes[i]] * 2 - 48, 0, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 else
Dmitry Torokhov275c6ce2006-01-29 21:52:39 -0500349 input_set_abs_params(input_dev, axes[i], 2, 253, 0, 0);
350 input_set_abs_params(input_dev, ABS_HAT0X + i, -1, 1, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
352
353 } else {
354 a3d->length = 29;
355
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700356 input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
357 input_dev->relbit[0] |= BIT_MASK(REL_X) | BIT_MASK(REL_Y);
358 input_dev->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_RIGHT) |
359 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 a3d_read(a3d, data);
362
363 if (!(a3d->adc = adc = gameport_allocate_port()))
364 printk(KERN_ERR "a3d: Not enough memory for ADC port\n");
365 else {
366 adc->port_data = a3d;
367 adc->open = a3d_adc_open;
368 adc->close = a3d_adc_close;
369 adc->cooked_read = a3d_adc_cooked_read;
370 adc->fuzz = 1;
371
372 gameport_set_name(adc, a3d_names[a3d->mode]);
373 gameport_set_phys(adc, "%s/gameport0", gameport->phys);
374 adc->dev.parent = &gameport->dev;
375
376 gameport_register_port(adc);
377 }
378 }
379
Dmitry Torokhov275c6ce2006-01-29 21:52:39 -0500380 err = input_register_device(a3d->dev);
381 if (err)
382 goto fail3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 return 0;
385
Dmitry Torokhov275c6ce2006-01-29 21:52:39 -0500386 fail3: if (a3d->adc)
387 gameport_unregister_port(a3d->adc);
388 fail2: gameport_close(gameport);
389 fail1: gameport_set_drvdata(gameport, NULL);
390 input_free_device(input_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 kfree(a3d);
392 return err;
393}
394
395static void a3d_disconnect(struct gameport *gameport)
396{
397 struct a3d *a3d = gameport_get_drvdata(gameport);
398
Dmitry Torokhov275c6ce2006-01-29 21:52:39 -0500399 input_unregister_device(a3d->dev);
400 if (a3d->adc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 gameport_unregister_port(a3d->adc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 gameport_close(gameport);
403 gameport_set_drvdata(gameport, NULL);
404 kfree(a3d);
405}
406
407static struct gameport_driver a3d_drv = {
408 .driver = {
409 .name = "adc",
Dmitry Torokhov275c6ce2006-01-29 21:52:39 -0500410 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 },
412 .description = DRIVER_DESC,
413 .connect = a3d_connect,
414 .disconnect = a3d_disconnect,
415};
416
417static int __init a3d_init(void)
418{
419 gameport_register_driver(&a3d_drv);
420 return 0;
421}
422
423static void __exit a3d_exit(void)
424{
425 gameport_unregister_driver(&a3d_drv);
426}
427
428module_init(a3d_init);
429module_exit(a3d_exit);