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