[PATCH] drivers/input/joystick: convert to dynamic input_dev allocation
[firewire-audio.git] / drivers / input / joystick / gamecon.c
blob7df2d82f2c83b2bb4c468ea08bed5e51f366a0e0
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
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 * Should you need to contact me, the author, you can do so either by
28 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
29 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
32 #include <linux/kernel.h>
33 #include <linux/delay.h>
34 #include <linux/module.h>
35 #include <linux/moduleparam.h>
36 #include <linux/init.h>
37 #include <linux/parport.h>
38 #include <linux/input.h>
40 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
41 MODULE_DESCRIPTION("NES, SNES, N64, MultiSystem, PSX gamepad driver");
42 MODULE_LICENSE("GPL");
44 #define GC_MAX_PORTS 3
45 #define GC_MAX_DEVICES 5
47 struct gc_config {
48 int args[GC_MAX_DEVICES + 1];
49 int nargs;
52 static struct gc_config gc[GC_MAX_PORTS] __initdata;
54 module_param_array_named(map, gc[0].args, int, &gc[0].nargs, 0);
55 MODULE_PARM_DESC(map, "Describes first set of devices (<parport#>,<pad1>,<pad2>,..<pad5>)");
56 module_param_array_named(map2, gc[1].args, int, &gc[1].nargs, 0);
57 MODULE_PARM_DESC(map2, "Describes second set of devices");
58 module_param_array_named(map3, gc[2].args, int, &gc[2].nargs, 0);
59 MODULE_PARM_DESC(map3, "Describes third set of devices");
61 __obsolete_setup("gc=");
62 __obsolete_setup("gc_2=");
63 __obsolete_setup("gc_3=");
65 /* see also gs_psx_delay parameter in PSX support section */
67 #define GC_SNES 1
68 #define GC_NES 2
69 #define GC_NES4 3
70 #define GC_MULTI 4
71 #define GC_MULTI2 5
72 #define GC_N64 6
73 #define GC_PSX 7
74 #define GC_DDR 8
76 #define GC_MAX 8
78 #define GC_REFRESH_TIME HZ/100
80 struct gc {
81 struct pardevice *pd;
82 struct input_dev *dev[GC_MAX_DEVICES];
83 struct timer_list timer;
84 unsigned char pads[GC_MAX + 1];
85 int used;
86 struct semaphore sem;
87 char phys[GC_MAX_DEVICES][32];
90 static struct gc *gc_base[3];
92 static int gc_status_bit[] = { 0x40, 0x80, 0x20, 0x10, 0x08 };
94 static char *gc_names[] = { NULL, "SNES pad", "NES pad", "NES FourPort", "Multisystem joystick",
95 "Multisystem 2-button joystick", "N64 controller", "PSX controller",
96 "PSX DDR controller" };
98 * N64 support.
101 static unsigned char gc_n64_bytes[] = { 0, 1, 13, 15, 14, 12, 10, 11, 2, 3 };
102 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 };
104 #define GC_N64_LENGTH 32 /* N64 bit length, not including stop bit */
105 #define GC_N64_REQUEST_LENGTH 37 /* transmit request sequence is 9 bits long */
106 #define GC_N64_DELAY 133 /* delay between transmit request, and response ready (us) */
107 #define GC_N64_REQUEST 0x1dd1111111ULL /* the request data command (encoded for 000000011) */
108 #define GC_N64_DWS 3 /* delay between write segments (required for sound playback because of ISA DMA) */
109 /* GC_N64_DWS > 24 is known to fail */
110 #define GC_N64_POWER_W 0xe2 /* power during write (transmit request) */
111 #define GC_N64_POWER_R 0xfd /* power during read */
112 #define GC_N64_OUT 0x1d /* output bits to the 4 pads */
113 /* Reading the main axes of any N64 pad is known to fail if the corresponding bit */
114 /* in GC_N64_OUT is pulled low on the output port (by any routine) for more */
115 /* than 123 us */
116 #define GC_N64_CLOCK 0x02 /* clock bits for read */
119 * gc_n64_read_packet() reads an N64 packet.
120 * Each pad uses one bit per byte. So all pads connected to this port are read in parallel.
123 static void gc_n64_read_packet(struct gc *gc, unsigned char *data)
125 int i;
126 unsigned long flags;
129 * Request the pad to transmit data
132 local_irq_save(flags);
133 for (i = 0; i < GC_N64_REQUEST_LENGTH; i++) {
134 parport_write_data(gc->pd->port, GC_N64_POWER_W | ((GC_N64_REQUEST >> i) & 1 ? GC_N64_OUT : 0));
135 udelay(GC_N64_DWS);
137 local_irq_restore(flags);
140 * Wait for the pad response to be loaded into the 33-bit register of the adapter
143 udelay(GC_N64_DELAY);
146 * Grab data (ignoring the last bit, which is a stop bit)
149 for (i = 0; i < GC_N64_LENGTH; i++) {
150 parport_write_data(gc->pd->port, GC_N64_POWER_R);
151 data[i] = parport_read_status(gc->pd->port);
152 parport_write_data(gc->pd->port, GC_N64_POWER_R | GC_N64_CLOCK);
156 * We must wait 200 ms here for the controller to reinitialize before the next read request.
157 * No worries as long as gc_read is polled less frequently than this.
163 * NES/SNES support.
166 #define GC_NES_DELAY 6 /* Delay between bits - 6us */
167 #define GC_NES_LENGTH 8 /* The NES pads use 8 bits of data */
168 #define GC_SNES_LENGTH 12 /* The SNES true length is 16, but the last 4 bits are unused */
170 #define GC_NES_POWER 0xfc
171 #define GC_NES_CLOCK 0x01
172 #define GC_NES_LATCH 0x02
174 static unsigned char gc_nes_bytes[] = { 0, 1, 2, 3 };
175 static unsigned char gc_snes_bytes[] = { 8, 0, 2, 3, 9, 1, 10, 11 };
176 static short gc_snes_btn[] = { BTN_A, BTN_B, BTN_SELECT, BTN_START, BTN_X, BTN_Y, BTN_TL, BTN_TR };
179 * gc_nes_read_packet() reads a NES/SNES packet.
180 * Each pad uses one bit per byte. So all pads connected to
181 * this port are read in parallel.
184 static void gc_nes_read_packet(struct gc *gc, int length, unsigned char *data)
186 int i;
188 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK | GC_NES_LATCH);
189 udelay(GC_NES_DELAY * 2);
190 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
192 for (i = 0; i < length; i++) {
193 udelay(GC_NES_DELAY);
194 parport_write_data(gc->pd->port, GC_NES_POWER);
195 data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
196 udelay(GC_NES_DELAY);
197 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
202 * Multisystem joystick support
205 #define GC_MULTI_LENGTH 5 /* Multi system joystick packet length is 5 */
206 #define GC_MULTI2_LENGTH 6 /* One more bit for one more button */
209 * gc_multi_read_packet() reads a Multisystem joystick packet.
212 static void gc_multi_read_packet(struct gc *gc, int length, unsigned char *data)
214 int i;
216 for (i = 0; i < length; i++) {
217 parport_write_data(gc->pd->port, ~(1 << i));
218 data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
223 * PSX support
225 * See documentation at:
226 * http://www.dim.com/~mackys/psxmemcard/ps-eng2.txt
227 * http://www.gamesx.com/controldata/psxcont/psxcont.htm
228 * ftp://milano.usal.es/pablo/
232 #define GC_PSX_DELAY 25 /* 25 usec */
233 #define GC_PSX_LENGTH 8 /* talk to the controller in bits */
234 #define GC_PSX_BYTES 6 /* the maximum number of bytes to read off the controller */
236 #define GC_PSX_MOUSE 1 /* Mouse */
237 #define GC_PSX_NEGCON 2 /* NegCon */
238 #define GC_PSX_NORMAL 4 /* Digital / Analog or Rumble in Digital mode */
239 #define GC_PSX_ANALOG 5 /* Analog in Analog mode / Rumble in Green mode */
240 #define GC_PSX_RUMBLE 7 /* Rumble in Red mode */
242 #define GC_PSX_CLOCK 0x04 /* Pin 4 */
243 #define GC_PSX_COMMAND 0x01 /* Pin 2 */
244 #define GC_PSX_POWER 0xf8 /* Pins 5-9 */
245 #define GC_PSX_SELECT 0x02 /* Pin 3 */
247 #define GC_PSX_ID(x) ((x) >> 4) /* High nibble is device type */
248 #define GC_PSX_LEN(x) (((x) & 0xf) << 1) /* Low nibble is length in bytes/2 */
250 static int gc_psx_delay = GC_PSX_DELAY;
251 module_param_named(psx_delay, gc_psx_delay, uint, 0);
252 MODULE_PARM_DESC(psx_delay, "Delay when accessing Sony PSX controller (usecs)");
254 __obsolete_setup("gc_psx_delay=");
256 static short gc_psx_abs[] = { ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_HAT0X, ABS_HAT0Y };
257 static short gc_psx_btn[] = { BTN_TL, BTN_TR, BTN_TL2, BTN_TR2, BTN_A, BTN_B, BTN_X, BTN_Y,
258 BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR };
259 static short gc_psx_ddr_btn[] = { BTN_0, BTN_1, BTN_2, BTN_3 };
262 * gc_psx_command() writes 8bit command and reads 8bit data from
263 * the psx pad.
266 static void gc_psx_command(struct gc *gc, int b, unsigned char data[5])
268 int i, j, cmd, read;
269 for (i = 0; i < 5; i++)
270 data[i] = 0;
272 for (i = 0; i < GC_PSX_LENGTH; i++, b >>= 1) {
273 cmd = (b & 1) ? GC_PSX_COMMAND : 0;
274 parport_write_data(gc->pd->port, cmd | GC_PSX_POWER);
275 udelay(gc_psx_delay);
276 read = parport_read_status(gc->pd->port) ^ 0x80;
277 for (j = 0; j < 5; j++)
278 data[j] |= (read & gc_status_bit[j] & (gc->pads[GC_PSX] | gc->pads[GC_DDR])) ? (1 << i) : 0;
279 parport_write_data(gc->pd->port, cmd | GC_PSX_CLOCK | GC_PSX_POWER);
280 udelay(gc_psx_delay);
285 * gc_psx_read_packet() reads a whole psx packet and returns
286 * device identifier code.
289 static void gc_psx_read_packet(struct gc *gc, unsigned char data[5][GC_PSX_BYTES], unsigned char id[5])
291 int i, j, max_len = 0;
292 unsigned long flags;
293 unsigned char data2[5];
295 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER); /* Select pad */
296 udelay(gc_psx_delay);
297 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_POWER); /* Deselect, begin command */
298 udelay(gc_psx_delay);
300 local_irq_save(flags);
302 gc_psx_command(gc, 0x01, data2); /* Access pad */
303 gc_psx_command(gc, 0x42, id); /* Get device ids */
304 gc_psx_command(gc, 0, data2); /* Dump status */
306 for (i =0; i < 5; i++) /* Find the longest pad */
307 if((gc_status_bit[i] & (gc->pads[GC_PSX] | gc->pads[GC_DDR]))
308 && (GC_PSX_LEN(id[i]) > max_len)
309 && (GC_PSX_LEN(id[i]) <= GC_PSX_BYTES))
310 max_len = GC_PSX_LEN(id[i]);
312 for (i = 0; i < max_len; i++) { /* Read in all the data */
313 gc_psx_command(gc, 0, data2);
314 for (j = 0; j < 5; j++)
315 data[j][i] = data2[j];
318 local_irq_restore(flags);
320 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
322 for(i = 0; i < 5; i++) /* Set id's to the real value */
323 id[i] = GC_PSX_ID(id[i]);
327 * gc_timer() reads and analyzes console pads data.
330 #define GC_MAX_LENGTH GC_N64_LENGTH
332 static void gc_timer(unsigned long private)
334 struct gc *gc = (void *) private;
335 unsigned char data[GC_MAX_LENGTH];
336 unsigned char data_psx[5][GC_PSX_BYTES];
337 int i, j, s;
340 * N64 pads - must be read first, any read confuses them for 200 us
343 if (gc->pads[GC_N64]) {
345 gc_n64_read_packet(gc, data);
347 for (i = 0; i < 5; i++) {
349 s = gc_status_bit[i];
351 if (s & gc->pads[GC_N64] & ~(data[8] | data[9])) {
353 signed char axes[2];
354 axes[0] = axes[1] = 0;
356 for (j = 0; j < 8; j++) {
357 if (data[23 - j] & s) axes[0] |= 1 << j;
358 if (data[31 - j] & s) axes[1] |= 1 << j;
361 input_report_abs(gc->dev[i], ABS_X, axes[0]);
362 input_report_abs(gc->dev[i], ABS_Y, -axes[1]);
364 input_report_abs(gc->dev[i], ABS_HAT0X, !(s & data[6]) - !(s & data[7]));
365 input_report_abs(gc->dev[i], ABS_HAT0Y, !(s & data[4]) - !(s & data[5]));
367 for (j = 0; j < 10; j++)
368 input_report_key(gc->dev[i], gc_n64_btn[j], s & data[gc_n64_bytes[j]]);
370 input_sync(gc->dev[i]);
376 * NES and SNES pads
379 if (gc->pads[GC_NES] || gc->pads[GC_SNES]) {
381 gc_nes_read_packet(gc, gc->pads[GC_SNES] ? GC_SNES_LENGTH : GC_NES_LENGTH, data);
383 for (i = 0; i < 5; i++) {
385 s = gc_status_bit[i];
387 if (s & (gc->pads[GC_NES] | gc->pads[GC_SNES])) {
388 input_report_abs(gc->dev[i], ABS_X, !(s & data[6]) - !(s & data[7]));
389 input_report_abs(gc->dev[i], ABS_Y, !(s & data[4]) - !(s & data[5]));
392 if (s & gc->pads[GC_NES])
393 for (j = 0; j < 4; j++)
394 input_report_key(gc->dev[i], gc_snes_btn[j], s & data[gc_nes_bytes[j]]);
396 if (s & gc->pads[GC_SNES])
397 for (j = 0; j < 8; j++)
398 input_report_key(gc->dev[i], gc_snes_btn[j], s & data[gc_snes_bytes[j]]);
400 input_sync(gc->dev[i]);
405 * Multi and Multi2 joysticks
408 if (gc->pads[GC_MULTI] || gc->pads[GC_MULTI2]) {
410 gc_multi_read_packet(gc, gc->pads[GC_MULTI2] ? GC_MULTI2_LENGTH : GC_MULTI_LENGTH, data);
412 for (i = 0; i < 5; i++) {
414 s = gc_status_bit[i];
416 if (s & (gc->pads[GC_MULTI] | gc->pads[GC_MULTI2])) {
417 input_report_abs(gc->dev[i], ABS_X, !(s & data[2]) - !(s & data[3]));
418 input_report_abs(gc->dev[i], ABS_Y, !(s & data[0]) - !(s & data[1]));
419 input_report_key(gc->dev[i], BTN_TRIGGER, s & data[4]);
422 if (s & gc->pads[GC_MULTI2])
423 input_report_key(gc->dev[i], BTN_THUMB, s & data[5]);
425 input_sync(gc->dev[i]);
430 * PSX controllers
433 if (gc->pads[GC_PSX] || gc->pads[GC_DDR]) {
435 gc_psx_read_packet(gc, data_psx, data);
437 for (i = 0; i < 5; i++) {
438 switch (data[i]) {
440 case GC_PSX_RUMBLE:
442 input_report_key(gc->dev[i], BTN_THUMBL, ~data_psx[i][0] & 0x04);
443 input_report_key(gc->dev[i], BTN_THUMBR, ~data_psx[i][0] & 0x02);
445 case GC_PSX_NEGCON:
446 case GC_PSX_ANALOG:
448 if (gc->pads[GC_DDR] & gc_status_bit[i]) {
449 for(j = 0; j < 4; j++)
450 input_report_key(gc->dev[i], gc_psx_ddr_btn[j], ~data_psx[i][0] & (0x10 << j));
451 } else {
452 for (j = 0; j < 4; j++)
453 input_report_abs(gc->dev[i], gc_psx_abs[j+2], data_psx[i][j + 2]);
455 input_report_abs(gc->dev[i], ABS_X, 128 + !(data_psx[i][0] & 0x20) * 127 - !(data_psx[i][0] & 0x80) * 128);
456 input_report_abs(gc->dev[i], ABS_Y, 128 + !(data_psx[i][0] & 0x40) * 127 - !(data_psx[i][0] & 0x10) * 128);
459 for (j = 0; j < 8; j++)
460 input_report_key(gc->dev[i], gc_psx_btn[j], ~data_psx[i][1] & (1 << j));
462 input_report_key(gc->dev[i], BTN_START, ~data_psx[i][0] & 0x08);
463 input_report_key(gc->dev[i], BTN_SELECT, ~data_psx[i][0] & 0x01);
465 input_sync(gc->dev[i]);
467 break;
469 case GC_PSX_NORMAL:
470 if (gc->pads[GC_DDR] & gc_status_bit[i]) {
471 for(j = 0; j < 4; j++)
472 input_report_key(gc->dev[i], gc_psx_ddr_btn[j], ~data_psx[i][0] & (0x10 << j));
473 } else {
474 input_report_abs(gc->dev[i], ABS_X, 128 + !(data_psx[i][0] & 0x20) * 127 - !(data_psx[i][0] & 0x80) * 128);
475 input_report_abs(gc->dev[i], ABS_Y, 128 + !(data_psx[i][0] & 0x40) * 127 - !(data_psx[i][0] & 0x10) * 128);
477 /* for some reason if the extra axes are left unset they drift */
478 /* for (j = 0; j < 4; j++)
479 input_report_abs(gc->dev[i], gc_psx_abs[j+2], 128);
480 * This needs to be debugged properly,
481 * maybe fuzz processing needs to be done in input_sync()
482 * --vojtech
486 for (j = 0; j < 8; j++)
487 input_report_key(gc->dev[i], gc_psx_btn[j], ~data_psx[i][1] & (1 << j));
489 input_report_key(gc->dev[i], BTN_START, ~data_psx[i][0] & 0x08);
490 input_report_key(gc->dev[i], BTN_SELECT, ~data_psx[i][0] & 0x01);
492 input_sync(gc->dev[i]);
494 break;
496 case 0: /* not a pad, ignore */
497 break;
502 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
505 static int gc_open(struct input_dev *dev)
507 struct gc *gc = dev->private;
508 int err;
510 err = down_interruptible(&gc->sem);
511 if (err)
512 return err;
514 if (!gc->used++) {
515 parport_claim(gc->pd);
516 parport_write_control(gc->pd->port, 0x04);
517 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
520 up(&gc->sem);
521 return 0;
524 static void gc_close(struct input_dev *dev)
526 struct gc *gc = dev->private;
528 down(&gc->sem);
529 if (!--gc->used) {
530 del_timer_sync(&gc->timer);
531 parport_write_control(gc->pd->port, 0x00);
532 parport_release(gc->pd);
534 up(&gc->sem);
537 static int __init gc_setup_pad(struct gc *gc, int idx, int pad_type)
539 struct input_dev *input_dev;
540 int i;
542 if (!pad_type)
543 return 0;
545 if (pad_type < 1 || pad_type > GC_MAX) {
546 printk(KERN_WARNING "gamecon.c: Pad type %d unknown\n", pad_type);
547 return -EINVAL;
550 gc->dev[idx] = input_dev = input_allocate_device();
551 if (!input_dev) {
552 printk(KERN_ERR "gamecon.c: Not enough memory for input device\n");
553 return -ENOMEM;
556 input_dev->name = gc_names[pad_type];
557 input_dev->phys = gc->phys[idx];
558 input_dev->id.bustype = BUS_PARPORT;
559 input_dev->id.vendor = 0x0001;
560 input_dev->id.product = pad_type;
561 input_dev->id.version = 0x0100;
562 input_dev->private = gc;
564 input_dev->open = gc_open;
565 input_dev->close = gc_close;
567 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
569 for (i = 0; i < 2; i++)
570 input_set_abs_params(input_dev, ABS_X + i, -1, 1, 0, 0);
572 gc->pads[0] |= gc_status_bit[idx];
573 gc->pads[pad_type] |= gc_status_bit[idx];
575 switch (pad_type) {
577 case GC_N64:
578 for (i = 0; i < 10; i++)
579 set_bit(gc_n64_btn[i], input_dev->keybit);
581 for (i = 0; i < 2; i++) {
582 input_set_abs_params(input_dev, ABS_X + i, -127, 126, 0, 2);
583 input_set_abs_params(input_dev, ABS_HAT0X + i, -1, 1, 0, 0);
586 break;
588 case GC_SNES:
589 for (i = 4; i < 8; i++)
590 set_bit(gc_snes_btn[i], input_dev->keybit);
591 case GC_NES:
592 for (i = 0; i < 4; i++)
593 set_bit(gc_snes_btn[i], input_dev->keybit);
594 break;
596 case GC_MULTI2:
597 set_bit(BTN_THUMB, input_dev->keybit);
598 case GC_MULTI:
599 set_bit(BTN_TRIGGER, input_dev->keybit);
600 break;
602 case GC_PSX:
603 for (i = 0; i < 6; i++)
604 input_set_abs_params(input_dev, gc_psx_abs[i], 4, 252, 0, 2);
605 for (i = 0; i < 12; i++)
606 set_bit(gc_psx_btn[i], input_dev->keybit);
608 break;
610 case GC_DDR:
611 for (i = 0; i < 4; i++)
612 set_bit(gc_psx_ddr_btn[i], input_dev->keybit);
613 for (i = 0; i < 12; i++)
614 set_bit(gc_psx_btn[i], input_dev->keybit);
616 break;
619 return 0;
622 static struct gc __init *gc_probe(int parport, int *pads, int n_pads)
624 struct gc *gc;
625 struct parport *pp;
626 struct pardevice *pd;
627 int i;
628 int err;
630 pp = parport_find_number(parport);
631 if (!pp) {
632 printk(KERN_ERR "gamecon.c: no such parport\n");
633 err = -EINVAL;
634 goto err_out;
637 pd = parport_register_device(pp, "gamecon", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
638 if (!pd) {
639 printk(KERN_ERR "gamecon.c: parport busy already - lp.o loaded?\n");
640 err = -EBUSY;
641 goto err_put_pp;
644 gc = kzalloc(sizeof(struct gc), GFP_KERNEL);
645 if (!gc) {
646 printk(KERN_ERR "gamecon.c: Not enough memory\n");
647 err = -ENOMEM;
648 goto err_unreg_pardev;
651 init_MUTEX(&gc->sem);
652 gc->pd = pd;
653 init_timer(&gc->timer);
654 gc->timer.data = (long) gc;
655 gc->timer.function = gc_timer;
657 for (i = 0; i < n_pads; i++) {
658 if (!pads[i])
659 continue;
661 sprintf(gc->phys[i], "%s/input%d", gc->pd->port->name, i);
662 err = gc_setup_pad(gc, i, pads[i]);
663 if (err)
664 goto err_free_devs;
666 input_register_device(gc->dev[i]);
669 if (!gc->pads[0]) {
670 printk(KERN_ERR "gamecon.c: No valid devices specified\n");
671 err = -EINVAL;
672 goto err_free_gc;
675 parport_put_port(pp);
676 return gc;
678 err_free_devs:
679 while (--i >= 0)
680 input_unregister_device(gc->dev[i]);
681 err_free_gc:
682 kfree(gc);
683 err_unreg_pardev:
684 parport_unregister_device(pd);
685 err_put_pp:
686 parport_put_port(pp);
687 err_out:
688 return ERR_PTR(err);
691 static void __exit gc_remove(struct gc *gc)
693 int i;
695 for (i = 0; i < GC_MAX_DEVICES; i++)
696 if (gc->dev[i])
697 input_unregister_device(gc->dev[i]);
698 parport_unregister_device(gc->pd);
699 kfree(gc);
702 static int __init gc_init(void)
704 int i;
705 int have_dev = 0;
706 int err = 0;
708 for (i = 0; i < GC_MAX_PORTS; i++) {
709 if (gc[i].nargs == 0 || gc[i].args[0] < 0)
710 continue;
712 if (gc[i].nargs < 2) {
713 printk(KERN_ERR "gamecon.c: at least one device must be specified\n");
714 err = -EINVAL;
715 break;
718 gc_base[i] = gc_probe(gc[i].args[0], gc[i].args + 1, gc[i].nargs - 1);
719 if (IS_ERR(gc_base[i])) {
720 err = PTR_ERR(gc_base[i]);
721 break;
724 have_dev = 1;
727 if (err) {
728 while (--i >= 0)
729 gc_remove(gc_base[i]);
730 return err;
733 return have_dev ? 0 : -ENODEV;
736 static void __exit gc_exit(void)
738 int i;
740 for (i = 0; i < GC_MAX_PORTS; i++)
741 if (gc_base[i])
742 gc_remove(gc_base[i]);
745 module_init(gc_init);
746 module_exit(gc_exit);