rt2x00: Simplify rt2x00_check_rev
[linux/fpc-iii.git] / drivers / input / joystick / gamecon.c
blob07a32aff5a31340cdd87b6285d77f041326f73aa
1 /*
2 * NES, SNES, N64, MultiSystem, PSX gamepad driver for Linux
4 * Copyright (c) 1999-2004 Vojtech Pavlik <vojtech@suse.cz>
5 * Copyright (c) 2004 Peter Nelson <rufus-kernel@hackish.org>
7 * Based on the work of:
8 * Andree Borrmann John Dahlstrom
9 * David Kuder Nathan Hand
10 * Raphael Assenat
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * Should you need to contact me, the author, you can do so either by
29 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
30 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
33 #include <linux/kernel.h>
34 #include <linux/delay.h>
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/parport.h>
38 #include <linux/input.h>
39 #include <linux/mutex.h>
41 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
42 MODULE_DESCRIPTION("NES, SNES, N64, MultiSystem, PSX gamepad driver");
43 MODULE_LICENSE("GPL");
45 #define GC_MAX_PORTS 3
46 #define GC_MAX_DEVICES 5
48 struct gc_config {
49 int args[GC_MAX_DEVICES + 1];
50 unsigned int nargs;
53 static struct gc_config gc_cfg[GC_MAX_PORTS] __initdata;
55 module_param_array_named(map, gc_cfg[0].args, int, &gc_cfg[0].nargs, 0);
56 MODULE_PARM_DESC(map, "Describes first set of devices (<parport#>,<pad1>,<pad2>,..<pad5>)");
57 module_param_array_named(map2, gc_cfg[1].args, int, &gc_cfg[1].nargs, 0);
58 MODULE_PARM_DESC(map2, "Describes second set of devices");
59 module_param_array_named(map3, gc_cfg[2].args, int, &gc_cfg[2].nargs, 0);
60 MODULE_PARM_DESC(map3, "Describes third set of devices");
62 /* see also gs_psx_delay parameter in PSX support section */
64 #define GC_SNES 1
65 #define GC_NES 2
66 #define GC_NES4 3
67 #define GC_MULTI 4
68 #define GC_MULTI2 5
69 #define GC_N64 6
70 #define GC_PSX 7
71 #define GC_DDR 8
72 #define GC_SNESMOUSE 9
74 #define GC_MAX 9
76 #define GC_REFRESH_TIME HZ/100
78 struct gc {
79 struct pardevice *pd;
80 struct input_dev *dev[GC_MAX_DEVICES];
81 struct timer_list timer;
82 unsigned char pads[GC_MAX + 1];
83 int used;
84 struct mutex mutex;
85 char phys[GC_MAX_DEVICES][32];
88 static struct gc *gc_base[3];
90 static int gc_status_bit[] = { 0x40, 0x80, 0x20, 0x10, 0x08 };
92 static char *gc_names[] = { NULL, "SNES pad", "NES pad", "NES FourPort", "Multisystem joystick",
93 "Multisystem 2-button joystick", "N64 controller", "PSX controller",
94 "PSX DDR controller", "SNES mouse" };
96 * N64 support.
99 static unsigned char gc_n64_bytes[] = { 0, 1, 13, 15, 14, 12, 10, 11, 2, 3 };
100 static short gc_n64_btn[] = { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_TRIGGER, BTN_START };
102 #define GC_N64_LENGTH 32 /* N64 bit length, not including stop bit */
103 #define GC_N64_REQUEST_LENGTH 37 /* transmit request sequence is 9 bits long */
104 #define GC_N64_DELAY 133 /* delay between transmit request, and response ready (us) */
105 #define GC_N64_REQUEST 0x1dd1111111ULL /* the request data command (encoded for 000000011) */
106 #define GC_N64_DWS 3 /* delay between write segments (required for sound playback because of ISA DMA) */
107 /* GC_N64_DWS > 24 is known to fail */
108 #define GC_N64_POWER_W 0xe2 /* power during write (transmit request) */
109 #define GC_N64_POWER_R 0xfd /* power during read */
110 #define GC_N64_OUT 0x1d /* output bits to the 4 pads */
111 /* Reading the main axes of any N64 pad is known to fail if the corresponding bit */
112 /* in GC_N64_OUT is pulled low on the output port (by any routine) for more */
113 /* than 123 us */
114 #define GC_N64_CLOCK 0x02 /* clock bits for read */
117 * gc_n64_read_packet() reads an N64 packet.
118 * Each pad uses one bit per byte. So all pads connected to this port are read in parallel.
121 static void gc_n64_read_packet(struct gc *gc, unsigned char *data)
123 int i;
124 unsigned long flags;
127 * Request the pad to transmit data
130 local_irq_save(flags);
131 for (i = 0; i < GC_N64_REQUEST_LENGTH; i++) {
132 parport_write_data(gc->pd->port, GC_N64_POWER_W | ((GC_N64_REQUEST >> i) & 1 ? GC_N64_OUT : 0));
133 udelay(GC_N64_DWS);
135 local_irq_restore(flags);
138 * Wait for the pad response to be loaded into the 33-bit register of the adapter
141 udelay(GC_N64_DELAY);
144 * Grab data (ignoring the last bit, which is a stop bit)
147 for (i = 0; i < GC_N64_LENGTH; i++) {
148 parport_write_data(gc->pd->port, GC_N64_POWER_R);
149 data[i] = parport_read_status(gc->pd->port);
150 parport_write_data(gc->pd->port, GC_N64_POWER_R | GC_N64_CLOCK);
154 * We must wait 200 ms here for the controller to reinitialize before the next read request.
155 * No worries as long as gc_read is polled less frequently than this.
160 static void gc_n64_process_packet(struct gc *gc)
162 unsigned char data[GC_N64_LENGTH];
163 signed char axes[2];
164 struct input_dev *dev;
165 int i, j, s;
167 gc_n64_read_packet(gc, data);
169 for (i = 0; i < GC_MAX_DEVICES; i++) {
171 dev = gc->dev[i];
172 if (!dev)
173 continue;
175 s = gc_status_bit[i];
177 if (s & gc->pads[GC_N64] & ~(data[8] | data[9])) {
179 axes[0] = axes[1] = 0;
181 for (j = 0; j < 8; j++) {
182 if (data[23 - j] & s)
183 axes[0] |= 1 << j;
184 if (data[31 - j] & s)
185 axes[1] |= 1 << j;
188 input_report_abs(dev, ABS_X, axes[0]);
189 input_report_abs(dev, ABS_Y, -axes[1]);
191 input_report_abs(dev, ABS_HAT0X, !(s & data[6]) - !(s & data[7]));
192 input_report_abs(dev, ABS_HAT0Y, !(s & data[4]) - !(s & data[5]));
194 for (j = 0; j < 10; j++)
195 input_report_key(dev, gc_n64_btn[j], s & data[gc_n64_bytes[j]]);
197 input_sync(dev);
203 * NES/SNES support.
206 #define GC_NES_DELAY 6 /* Delay between bits - 6us */
207 #define GC_NES_LENGTH 8 /* The NES pads use 8 bits of data */
208 #define GC_SNES_LENGTH 12 /* The SNES true length is 16, but the
209 last 4 bits are unused */
210 #define GC_SNESMOUSE_LENGTH 32 /* The SNES mouse uses 32 bits, the first
211 16 bits are equivalent to a gamepad */
213 #define GC_NES_POWER 0xfc
214 #define GC_NES_CLOCK 0x01
215 #define GC_NES_LATCH 0x02
217 static unsigned char gc_nes_bytes[] = { 0, 1, 2, 3 };
218 static unsigned char gc_snes_bytes[] = { 8, 0, 2, 3, 9, 1, 10, 11 };
219 static short gc_snes_btn[] = { BTN_A, BTN_B, BTN_SELECT, BTN_START, BTN_X, BTN_Y, BTN_TL, BTN_TR };
222 * gc_nes_read_packet() reads a NES/SNES packet.
223 * Each pad uses one bit per byte. So all pads connected to
224 * this port are read in parallel.
227 static void gc_nes_read_packet(struct gc *gc, int length, unsigned char *data)
229 int i;
231 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK | GC_NES_LATCH);
232 udelay(GC_NES_DELAY * 2);
233 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
235 for (i = 0; i < length; i++) {
236 udelay(GC_NES_DELAY);
237 parport_write_data(gc->pd->port, GC_NES_POWER);
238 data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
239 udelay(GC_NES_DELAY);
240 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
244 static void gc_nes_process_packet(struct gc *gc)
246 unsigned char data[GC_SNESMOUSE_LENGTH];
247 struct input_dev *dev;
248 int i, j, s, len;
249 char x_rel, y_rel;
251 len = gc->pads[GC_SNESMOUSE] ? GC_SNESMOUSE_LENGTH :
252 (gc->pads[GC_SNES] ? GC_SNES_LENGTH : GC_NES_LENGTH);
254 gc_nes_read_packet(gc, len, data);
256 for (i = 0; i < GC_MAX_DEVICES; i++) {
258 dev = gc->dev[i];
259 if (!dev)
260 continue;
262 s = gc_status_bit[i];
264 if (s & (gc->pads[GC_NES] | gc->pads[GC_SNES])) {
265 input_report_abs(dev, ABS_X, !(s & data[6]) - !(s & data[7]));
266 input_report_abs(dev, ABS_Y, !(s & data[4]) - !(s & data[5]));
269 if (s & gc->pads[GC_NES])
270 for (j = 0; j < 4; j++)
271 input_report_key(dev, gc_snes_btn[j], s & data[gc_nes_bytes[j]]);
273 if (s & gc->pads[GC_SNES])
274 for (j = 0; j < 8; j++)
275 input_report_key(dev, gc_snes_btn[j], s & data[gc_snes_bytes[j]]);
277 if (s & gc->pads[GC_SNESMOUSE]) {
279 * The 4 unused bits from SNES controllers appear to be ID bits
280 * so use them to make sure iwe are dealing with a mouse.
281 * gamepad is connected. This is important since
282 * my SNES gamepad sends 1's for bits 16-31, which
283 * cause the mouse pointer to quickly move to the
284 * upper left corner of the screen.
286 if (!(s & data[12]) && !(s & data[13]) &&
287 !(s & data[14]) && (s & data[15])) {
288 input_report_key(dev, BTN_LEFT, s & data[9]);
289 input_report_key(dev, BTN_RIGHT, s & data[8]);
291 x_rel = y_rel = 0;
292 for (j = 0; j < 7; j++) {
293 x_rel <<= 1;
294 if (data[25 + j] & s)
295 x_rel |= 1;
297 y_rel <<= 1;
298 if (data[17 + j] & s)
299 y_rel |= 1;
302 if (x_rel) {
303 if (data[24] & s)
304 x_rel = -x_rel;
305 input_report_rel(dev, REL_X, x_rel);
308 if (y_rel) {
309 if (data[16] & s)
310 y_rel = -y_rel;
311 input_report_rel(dev, REL_Y, y_rel);
315 input_sync(dev);
320 * Multisystem joystick support
323 #define GC_MULTI_LENGTH 5 /* Multi system joystick packet length is 5 */
324 #define GC_MULTI2_LENGTH 6 /* One more bit for one more button */
327 * gc_multi_read_packet() reads a Multisystem joystick packet.
330 static void gc_multi_read_packet(struct gc *gc, int length, unsigned char *data)
332 int i;
334 for (i = 0; i < length; i++) {
335 parport_write_data(gc->pd->port, ~(1 << i));
336 data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
340 static void gc_multi_process_packet(struct gc *gc)
342 unsigned char data[GC_MULTI2_LENGTH];
343 struct input_dev *dev;
344 int i, s;
346 gc_multi_read_packet(gc, gc->pads[GC_MULTI2] ? GC_MULTI2_LENGTH : GC_MULTI_LENGTH, data);
348 for (i = 0; i < GC_MAX_DEVICES; i++) {
350 dev = gc->dev[i];
351 if (!dev)
352 continue;
354 s = gc_status_bit[i];
356 if (s & (gc->pads[GC_MULTI] | gc->pads[GC_MULTI2])) {
357 input_report_abs(dev, ABS_X, !(s & data[2]) - !(s & data[3]));
358 input_report_abs(dev, ABS_Y, !(s & data[0]) - !(s & data[1]));
359 input_report_key(dev, BTN_TRIGGER, s & data[4]);
362 if (s & gc->pads[GC_MULTI2])
363 input_report_key(dev, BTN_THUMB, s & data[5]);
365 input_sync(dev);
370 * PSX support
372 * See documentation at:
373 * http://www.dim.com/~mackys/psxmemcard/ps-eng2.txt
374 * http://www.gamesx.com/controldata/psxcont/psxcont.htm
375 * ftp://milano.usal.es/pablo/
379 #define GC_PSX_DELAY 25 /* 25 usec */
380 #define GC_PSX_LENGTH 8 /* talk to the controller in bits */
381 #define GC_PSX_BYTES 6 /* the maximum number of bytes to read off the controller */
383 #define GC_PSX_MOUSE 1 /* Mouse */
384 #define GC_PSX_NEGCON 2 /* NegCon */
385 #define GC_PSX_NORMAL 4 /* Digital / Analog or Rumble in Digital mode */
386 #define GC_PSX_ANALOG 5 /* Analog in Analog mode / Rumble in Green mode */
387 #define GC_PSX_RUMBLE 7 /* Rumble in Red mode */
389 #define GC_PSX_CLOCK 0x04 /* Pin 4 */
390 #define GC_PSX_COMMAND 0x01 /* Pin 2 */
391 #define GC_PSX_POWER 0xf8 /* Pins 5-9 */
392 #define GC_PSX_SELECT 0x02 /* Pin 3 */
394 #define GC_PSX_ID(x) ((x) >> 4) /* High nibble is device type */
395 #define GC_PSX_LEN(x) (((x) & 0xf) << 1) /* Low nibble is length in bytes/2 */
397 static int gc_psx_delay = GC_PSX_DELAY;
398 module_param_named(psx_delay, gc_psx_delay, uint, 0);
399 MODULE_PARM_DESC(psx_delay, "Delay when accessing Sony PSX controller (usecs)");
401 static short gc_psx_abs[] = { ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_HAT0X, ABS_HAT0Y };
402 static short gc_psx_btn[] = { BTN_TL, BTN_TR, BTN_TL2, BTN_TR2, BTN_A, BTN_B, BTN_X, BTN_Y,
403 BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR };
404 static short gc_psx_ddr_btn[] = { BTN_0, BTN_1, BTN_2, BTN_3 };
407 * gc_psx_command() writes 8bit command and reads 8bit data from
408 * the psx pad.
411 static void gc_psx_command(struct gc *gc, int b, unsigned char data[GC_MAX_DEVICES])
413 int i, j, cmd, read;
415 for (i = 0; i < GC_MAX_DEVICES; i++)
416 data[i] = 0;
418 for (i = 0; i < GC_PSX_LENGTH; i++, b >>= 1) {
419 cmd = (b & 1) ? GC_PSX_COMMAND : 0;
420 parport_write_data(gc->pd->port, cmd | GC_PSX_POWER);
421 udelay(gc_psx_delay);
422 read = parport_read_status(gc->pd->port) ^ 0x80;
423 for (j = 0; j < GC_MAX_DEVICES; j++)
424 data[j] |= (read & gc_status_bit[j] & (gc->pads[GC_PSX] | gc->pads[GC_DDR])) ? (1 << i) : 0;
425 parport_write_data(gc->pd->port, cmd | GC_PSX_CLOCK | GC_PSX_POWER);
426 udelay(gc_psx_delay);
431 * gc_psx_read_packet() reads a whole psx packet and returns
432 * device identifier code.
435 static void gc_psx_read_packet(struct gc *gc, unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES],
436 unsigned char id[GC_MAX_DEVICES])
438 int i, j, max_len = 0;
439 unsigned long flags;
440 unsigned char data2[GC_MAX_DEVICES];
442 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER); /* Select pad */
443 udelay(gc_psx_delay);
444 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_POWER); /* Deselect, begin command */
445 udelay(gc_psx_delay);
447 local_irq_save(flags);
449 gc_psx_command(gc, 0x01, data2); /* Access pad */
450 gc_psx_command(gc, 0x42, id); /* Get device ids */
451 gc_psx_command(gc, 0, data2); /* Dump status */
453 for (i =0; i < GC_MAX_DEVICES; i++) /* Find the longest pad */
454 if((gc_status_bit[i] & (gc->pads[GC_PSX] | gc->pads[GC_DDR]))
455 && (GC_PSX_LEN(id[i]) > max_len)
456 && (GC_PSX_LEN(id[i]) <= GC_PSX_BYTES))
457 max_len = GC_PSX_LEN(id[i]);
459 for (i = 0; i < max_len; i++) { /* Read in all the data */
460 gc_psx_command(gc, 0, data2);
461 for (j = 0; j < GC_MAX_DEVICES; j++)
462 data[j][i] = data2[j];
465 local_irq_restore(flags);
467 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
469 for(i = 0; i < GC_MAX_DEVICES; i++) /* Set id's to the real value */
470 id[i] = GC_PSX_ID(id[i]);
473 static void gc_psx_process_packet(struct gc *gc)
475 unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES];
476 unsigned char id[GC_MAX_DEVICES];
477 struct input_dev *dev;
478 int i, j;
480 gc_psx_read_packet(gc, data, id);
482 for (i = 0; i < GC_MAX_DEVICES; i++) {
484 dev = gc->dev[i];
485 if (!dev)
486 continue;
488 switch (id[i]) {
490 case GC_PSX_RUMBLE:
492 input_report_key(dev, BTN_THUMBL, ~data[i][0] & 0x04);
493 input_report_key(dev, BTN_THUMBR, ~data[i][0] & 0x02);
495 case GC_PSX_NEGCON:
496 case GC_PSX_ANALOG:
498 if (gc->pads[GC_DDR] & gc_status_bit[i]) {
499 for(j = 0; j < 4; j++)
500 input_report_key(dev, gc_psx_ddr_btn[j], ~data[i][0] & (0x10 << j));
501 } else {
502 for (j = 0; j < 4; j++)
503 input_report_abs(dev, gc_psx_abs[j + 2], data[i][j + 2]);
505 input_report_abs(dev, ABS_X, 128 + !(data[i][0] & 0x20) * 127 - !(data[i][0] & 0x80) * 128);
506 input_report_abs(dev, ABS_Y, 128 + !(data[i][0] & 0x40) * 127 - !(data[i][0] & 0x10) * 128);
509 for (j = 0; j < 8; j++)
510 input_report_key(dev, gc_psx_btn[j], ~data[i][1] & (1 << j));
512 input_report_key(dev, BTN_START, ~data[i][0] & 0x08);
513 input_report_key(dev, BTN_SELECT, ~data[i][0] & 0x01);
515 input_sync(dev);
517 break;
519 case GC_PSX_NORMAL:
520 if (gc->pads[GC_DDR] & gc_status_bit[i]) {
521 for(j = 0; j < 4; j++)
522 input_report_key(dev, gc_psx_ddr_btn[j], ~data[i][0] & (0x10 << j));
523 } else {
524 input_report_abs(dev, ABS_X, 128 + !(data[i][0] & 0x20) * 127 - !(data[i][0] & 0x80) * 128);
525 input_report_abs(dev, ABS_Y, 128 + !(data[i][0] & 0x40) * 127 - !(data[i][0] & 0x10) * 128);
527 /* for some reason if the extra axes are left unset they drift */
528 /* for (j = 0; j < 4; j++)
529 input_report_abs(dev, gc_psx_abs[j + 2], 128);
530 * This needs to be debugged properly,
531 * maybe fuzz processing needs to be done in input_sync()
532 * --vojtech
536 for (j = 0; j < 8; j++)
537 input_report_key(dev, gc_psx_btn[j], ~data[i][1] & (1 << j));
539 input_report_key(dev, BTN_START, ~data[i][0] & 0x08);
540 input_report_key(dev, BTN_SELECT, ~data[i][0] & 0x01);
542 input_sync(dev);
544 break;
546 case 0: /* not a pad, ignore */
547 break;
553 * gc_timer() initiates reads of console pads data.
556 static void gc_timer(unsigned long private)
558 struct gc *gc = (void *) private;
561 * N64 pads - must be read first, any read confuses them for 200 us
564 if (gc->pads[GC_N64])
565 gc_n64_process_packet(gc);
568 * NES and SNES pads or mouse
571 if (gc->pads[GC_NES] || gc->pads[GC_SNES] || gc->pads[GC_SNESMOUSE])
572 gc_nes_process_packet(gc);
575 * Multi and Multi2 joysticks
578 if (gc->pads[GC_MULTI] || gc->pads[GC_MULTI2])
579 gc_multi_process_packet(gc);
582 * PSX controllers
585 if (gc->pads[GC_PSX] || gc->pads[GC_DDR])
586 gc_psx_process_packet(gc);
588 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
591 static int gc_open(struct input_dev *dev)
593 struct gc *gc = input_get_drvdata(dev);
594 int err;
596 err = mutex_lock_interruptible(&gc->mutex);
597 if (err)
598 return err;
600 if (!gc->used++) {
601 parport_claim(gc->pd);
602 parport_write_control(gc->pd->port, 0x04);
603 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
606 mutex_unlock(&gc->mutex);
607 return 0;
610 static void gc_close(struct input_dev *dev)
612 struct gc *gc = input_get_drvdata(dev);
614 mutex_lock(&gc->mutex);
615 if (!--gc->used) {
616 del_timer_sync(&gc->timer);
617 parport_write_control(gc->pd->port, 0x00);
618 parport_release(gc->pd);
620 mutex_unlock(&gc->mutex);
623 static int __init gc_setup_pad(struct gc *gc, int idx, int pad_type)
625 struct input_dev *input_dev;
626 int i;
628 if (!pad_type)
629 return 0;
631 if (pad_type < 1 || pad_type > GC_MAX) {
632 printk(KERN_WARNING "gamecon.c: Pad type %d unknown\n", pad_type);
633 return -EINVAL;
636 gc->dev[idx] = input_dev = input_allocate_device();
637 if (!input_dev) {
638 printk(KERN_ERR "gamecon.c: Not enough memory for input device\n");
639 return -ENOMEM;
642 input_dev->name = gc_names[pad_type];
643 input_dev->phys = gc->phys[idx];
644 input_dev->id.bustype = BUS_PARPORT;
645 input_dev->id.vendor = 0x0001;
646 input_dev->id.product = pad_type;
647 input_dev->id.version = 0x0100;
649 input_set_drvdata(input_dev, gc);
651 input_dev->open = gc_open;
652 input_dev->close = gc_close;
654 if (pad_type != GC_SNESMOUSE) {
655 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
657 for (i = 0; i < 2; i++)
658 input_set_abs_params(input_dev, ABS_X + i, -1, 1, 0, 0);
659 } else
660 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
662 gc->pads[0] |= gc_status_bit[idx];
663 gc->pads[pad_type] |= gc_status_bit[idx];
665 switch (pad_type) {
667 case GC_N64:
668 for (i = 0; i < 10; i++)
669 set_bit(gc_n64_btn[i], input_dev->keybit);
671 for (i = 0; i < 2; i++) {
672 input_set_abs_params(input_dev, ABS_X + i, -127, 126, 0, 2);
673 input_set_abs_params(input_dev, ABS_HAT0X + i, -1, 1, 0, 0);
676 break;
678 case GC_SNESMOUSE:
679 set_bit(BTN_LEFT, input_dev->keybit);
680 set_bit(BTN_RIGHT, input_dev->keybit);
681 set_bit(REL_X, input_dev->relbit);
682 set_bit(REL_Y, input_dev->relbit);
683 break;
685 case GC_SNES:
686 for (i = 4; i < 8; i++)
687 set_bit(gc_snes_btn[i], input_dev->keybit);
688 case GC_NES:
689 for (i = 0; i < 4; i++)
690 set_bit(gc_snes_btn[i], input_dev->keybit);
691 break;
693 case GC_MULTI2:
694 set_bit(BTN_THUMB, input_dev->keybit);
695 case GC_MULTI:
696 set_bit(BTN_TRIGGER, input_dev->keybit);
697 break;
699 case GC_PSX:
700 for (i = 0; i < 6; i++)
701 input_set_abs_params(input_dev, gc_psx_abs[i], 4, 252, 0, 2);
702 for (i = 0; i < 12; i++)
703 set_bit(gc_psx_btn[i], input_dev->keybit);
705 break;
707 case GC_DDR:
708 for (i = 0; i < 4; i++)
709 set_bit(gc_psx_ddr_btn[i], input_dev->keybit);
710 for (i = 0; i < 12; i++)
711 set_bit(gc_psx_btn[i], input_dev->keybit);
713 break;
716 return 0;
719 static struct gc __init *gc_probe(int parport, int *pads, int n_pads)
721 struct gc *gc;
722 struct parport *pp;
723 struct pardevice *pd;
724 int i;
725 int err;
727 pp = parport_find_number(parport);
728 if (!pp) {
729 printk(KERN_ERR "gamecon.c: no such parport\n");
730 err = -EINVAL;
731 goto err_out;
734 pd = parport_register_device(pp, "gamecon", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
735 if (!pd) {
736 printk(KERN_ERR "gamecon.c: parport busy already - lp.o loaded?\n");
737 err = -EBUSY;
738 goto err_put_pp;
741 gc = kzalloc(sizeof(struct gc), GFP_KERNEL);
742 if (!gc) {
743 printk(KERN_ERR "gamecon.c: Not enough memory\n");
744 err = -ENOMEM;
745 goto err_unreg_pardev;
748 mutex_init(&gc->mutex);
749 gc->pd = pd;
750 init_timer(&gc->timer);
751 gc->timer.data = (long) gc;
752 gc->timer.function = gc_timer;
754 for (i = 0; i < n_pads && i < GC_MAX_DEVICES; i++) {
755 if (!pads[i])
756 continue;
758 snprintf(gc->phys[i], sizeof(gc->phys[i]),
759 "%s/input%d", gc->pd->port->name, i);
760 err = gc_setup_pad(gc, i, pads[i]);
761 if (err)
762 goto err_unreg_devs;
764 err = input_register_device(gc->dev[i]);
765 if (err)
766 goto err_free_dev;
769 if (!gc->pads[0]) {
770 printk(KERN_ERR "gamecon.c: No valid devices specified\n");
771 err = -EINVAL;
772 goto err_free_gc;
775 parport_put_port(pp);
776 return gc;
778 err_free_dev:
779 input_free_device(gc->dev[i]);
780 err_unreg_devs:
781 while (--i >= 0)
782 if (gc->dev[i])
783 input_unregister_device(gc->dev[i]);
784 err_free_gc:
785 kfree(gc);
786 err_unreg_pardev:
787 parport_unregister_device(pd);
788 err_put_pp:
789 parport_put_port(pp);
790 err_out:
791 return ERR_PTR(err);
794 static void gc_remove(struct gc *gc)
796 int i;
798 for (i = 0; i < GC_MAX_DEVICES; i++)
799 if (gc->dev[i])
800 input_unregister_device(gc->dev[i]);
801 parport_unregister_device(gc->pd);
802 kfree(gc);
805 static int __init gc_init(void)
807 int i;
808 int have_dev = 0;
809 int err = 0;
811 for (i = 0; i < GC_MAX_PORTS; i++) {
812 if (gc_cfg[i].nargs == 0 || gc_cfg[i].args[0] < 0)
813 continue;
815 if (gc_cfg[i].nargs < 2) {
816 printk(KERN_ERR "gamecon.c: at least one device must be specified\n");
817 err = -EINVAL;
818 break;
821 gc_base[i] = gc_probe(gc_cfg[i].args[0],
822 gc_cfg[i].args + 1, gc_cfg[i].nargs - 1);
823 if (IS_ERR(gc_base[i])) {
824 err = PTR_ERR(gc_base[i]);
825 break;
828 have_dev = 1;
831 if (err) {
832 while (--i >= 0)
833 if (gc_base[i])
834 gc_remove(gc_base[i]);
835 return err;
838 return have_dev ? 0 : -ENODEV;
841 static void __exit gc_exit(void)
843 int i;
845 for (i = 0; i < GC_MAX_PORTS; i++)
846 if (gc_base[i])
847 gc_remove(gc_base[i]);
850 module_init(gc_init);
851 module_exit(gc_exit);