]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/input/joystick/gamecon.c
Input: joysticks - semaphore to mutex conversion
[mv-sheeva.git] / drivers / input / joystick / gamecon.c
1 /*
2  * NES, SNES, N64, MultiSystem, PSX gamepad driver for Linux
3  *
4  *  Copyright (c) 1999-2004     Vojtech Pavlik <vojtech@suse.cz>
5  *  Copyright (c) 2004          Peter Nelson <rufus-kernel@hackish.org>
6  *
7  *  Based on the work of:
8  *      Andree Borrmann         John Dahlstrom
9  *      David Kuder             Nathan Hand
10  */
11
12 /*
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.
17  *
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.
22  *
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
26  *
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
30  */
31
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>
39 #include <linux/mutex.h>
40
41 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
42 MODULE_DESCRIPTION("NES, SNES, N64, MultiSystem, PSX gamepad driver");
43 MODULE_LICENSE("GPL");
44
45 #define GC_MAX_PORTS            3
46 #define GC_MAX_DEVICES          5
47
48 struct gc_config {
49         int args[GC_MAX_DEVICES + 1];
50         int nargs;
51 };
52
53 static struct gc_config gc[GC_MAX_PORTS] __initdata;
54
55 module_param_array_named(map, gc[0].args, int, &gc[0].nargs, 0);
56 MODULE_PARM_DESC(map, "Describes first set of devices (<parport#>,<pad1>,<pad2>,..<pad5>)");
57 module_param_array_named(map2, gc[1].args, int, &gc[1].nargs, 0);
58 MODULE_PARM_DESC(map2, "Describes second set of devices");
59 module_param_array_named(map3, gc[2].args, int, &gc[2].nargs, 0);
60 MODULE_PARM_DESC(map3, "Describes third set of devices");
61
62 __obsolete_setup("gc=");
63 __obsolete_setup("gc_2=");
64 __obsolete_setup("gc_3=");
65
66 /* see also gs_psx_delay parameter in PSX support section */
67
68 #define GC_SNES         1
69 #define GC_NES          2
70 #define GC_NES4         3
71 #define GC_MULTI        4
72 #define GC_MULTI2       5
73 #define GC_N64          6
74 #define GC_PSX          7
75 #define GC_DDR          8
76
77 #define GC_MAX          8
78
79 #define GC_REFRESH_TIME HZ/100
80
81 struct gc {
82         struct pardevice *pd;
83         struct input_dev *dev[GC_MAX_DEVICES];
84         struct timer_list timer;
85         unsigned char pads[GC_MAX + 1];
86         int used;
87         struct mutex mutex;
88         char phys[GC_MAX_DEVICES][32];
89 };
90
91 static struct gc *gc_base[3];
92
93 static int gc_status_bit[] = { 0x40, 0x80, 0x20, 0x10, 0x08 };
94
95 static char *gc_names[] = { NULL, "SNES pad", "NES pad", "NES FourPort", "Multisystem joystick",
96                                 "Multisystem 2-button joystick", "N64 controller", "PSX controller",
97                                 "PSX DDR controller" };
98 /*
99  * N64 support.
100  */
101
102 static unsigned char gc_n64_bytes[] = { 0, 1, 13, 15, 14, 12, 10, 11, 2, 3 };
103 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
105 #define GC_N64_LENGTH           32              /* N64 bit length, not including stop bit */
106 #define GC_N64_REQUEST_LENGTH   37              /* transmit request sequence is 9 bits long */
107 #define GC_N64_DELAY            133             /* delay between transmit request, and response ready (us) */
108 #define GC_N64_REQUEST          0x1dd1111111ULL /* the request data command (encoded for 000000011) */
109 #define GC_N64_DWS              3               /* delay between write segments (required for sound playback because of ISA DMA) */
110                                                 /* GC_N64_DWS > 24 is known to fail */
111 #define GC_N64_POWER_W          0xe2            /* power during write (transmit request) */
112 #define GC_N64_POWER_R          0xfd            /* power during read */
113 #define GC_N64_OUT              0x1d            /* output bits to the 4 pads */
114                                                 /* Reading the main axes of any N64 pad is known to fail if the corresponding bit */
115                                                 /* in GC_N64_OUT is pulled low on the output port (by any routine) for more */
116                                                 /* than 123 us */
117 #define GC_N64_CLOCK            0x02            /* clock bits for read */
118
119 /*
120  * gc_n64_read_packet() reads an N64 packet.
121  * Each pad uses one bit per byte. So all pads connected to this port are read in parallel.
122  */
123
124 static void gc_n64_read_packet(struct gc *gc, unsigned char *data)
125 {
126         int i;
127         unsigned long flags;
128
129 /*
130  * Request the pad to transmit data
131  */
132
133         local_irq_save(flags);
134         for (i = 0; i < GC_N64_REQUEST_LENGTH; i++) {
135                 parport_write_data(gc->pd->port, GC_N64_POWER_W | ((GC_N64_REQUEST >> i) & 1 ? GC_N64_OUT : 0));
136                 udelay(GC_N64_DWS);
137         }
138         local_irq_restore(flags);
139
140 /*
141  * Wait for the pad response to be loaded into the 33-bit register of the adapter
142  */
143
144         udelay(GC_N64_DELAY);
145
146 /*
147  * Grab data (ignoring the last bit, which is a stop bit)
148  */
149
150         for (i = 0; i < GC_N64_LENGTH; i++) {
151                 parport_write_data(gc->pd->port, GC_N64_POWER_R);
152                 data[i] = parport_read_status(gc->pd->port);
153                 parport_write_data(gc->pd->port, GC_N64_POWER_R | GC_N64_CLOCK);
154          }
155
156 /*
157  * We must wait 200 ms here for the controller to reinitialize before the next read request.
158  * No worries as long as gc_read is polled less frequently than this.
159  */
160
161 }
162
163 static void gc_n64_process_packet(struct gc *gc)
164 {
165         unsigned char data[GC_N64_LENGTH];
166         signed char axes[2];
167         struct input_dev *dev;
168         int i, j, s;
169
170         gc_n64_read_packet(gc, data);
171
172         for (i = 0; i < GC_MAX_DEVICES; i++) {
173
174                 dev = gc->dev[i];
175                 if (!dev)
176                         continue;
177
178                 s = gc_status_bit[i];
179
180                 if (s & gc->pads[GC_N64] & ~(data[8] | data[9])) {
181
182                         axes[0] = axes[1] = 0;
183
184                         for (j = 0; j < 8; j++) {
185                                 if (data[23 - j] & s)
186                                         axes[0] |= 1 << j;
187                                 if (data[31 - j] & s)
188                                         axes[1] |= 1 << j;
189                         }
190
191                         input_report_abs(dev, ABS_X,  axes[0]);
192                         input_report_abs(dev, ABS_Y, -axes[1]);
193
194                         input_report_abs(dev, ABS_HAT0X, !(s & data[6]) - !(s & data[7]));
195                         input_report_abs(dev, ABS_HAT0Y, !(s & data[4]) - !(s & data[5]));
196
197                         for (j = 0; j < 10; j++)
198                                 input_report_key(dev, gc_n64_btn[j], s & data[gc_n64_bytes[j]]);
199
200                         input_sync(dev);
201                 }
202         }
203 }
204
205 /*
206  * NES/SNES support.
207  */
208
209 #define GC_NES_DELAY    6       /* Delay between bits - 6us */
210 #define GC_NES_LENGTH   8       /* The NES pads use 8 bits of data */
211 #define GC_SNES_LENGTH  12      /* The SNES true length is 16, but the last 4 bits are unused */
212
213 #define GC_NES_POWER    0xfc
214 #define GC_NES_CLOCK    0x01
215 #define GC_NES_LATCH    0x02
216
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 };
220
221 /*
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.
225  */
226
227 static void gc_nes_read_packet(struct gc *gc, int length, unsigned char *data)
228 {
229         int i;
230
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);
234
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);
241         }
242 }
243
244 static void gc_nes_process_packet(struct gc *gc)
245 {
246         unsigned char data[GC_SNES_LENGTH];
247         struct input_dev *dev;
248         int i, j, s;
249
250         gc_nes_read_packet(gc, gc->pads[GC_SNES] ? GC_SNES_LENGTH : GC_NES_LENGTH, data);
251
252         for (i = 0; i < GC_MAX_DEVICES; i++) {
253
254                 dev = gc->dev[i];
255                 if (!dev)
256                         continue;
257
258                 s = gc_status_bit[i];
259
260                 if (s & (gc->pads[GC_NES] | gc->pads[GC_SNES])) {
261                         input_report_abs(dev, ABS_X, !(s & data[6]) - !(s & data[7]));
262                         input_report_abs(dev, ABS_Y, !(s & data[4]) - !(s & data[5]));
263                 }
264
265                 if (s & gc->pads[GC_NES])
266                         for (j = 0; j < 4; j++)
267                                 input_report_key(dev, gc_snes_btn[j], s & data[gc_nes_bytes[j]]);
268
269                 if (s & gc->pads[GC_SNES])
270                         for (j = 0; j < 8; j++)
271                                 input_report_key(dev, gc_snes_btn[j], s & data[gc_snes_bytes[j]]);
272
273                 input_sync(dev);
274         }
275 }
276
277 /*
278  * Multisystem joystick support
279  */
280
281 #define GC_MULTI_LENGTH         5       /* Multi system joystick packet length is 5 */
282 #define GC_MULTI2_LENGTH        6       /* One more bit for one more button */
283
284 /*
285  * gc_multi_read_packet() reads a Multisystem joystick packet.
286  */
287
288 static void gc_multi_read_packet(struct gc *gc, int length, unsigned char *data)
289 {
290         int i;
291
292         for (i = 0; i < length; i++) {
293                 parport_write_data(gc->pd->port, ~(1 << i));
294                 data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
295         }
296 }
297
298 static void gc_multi_process_packet(struct gc *gc)
299 {
300         unsigned char data[GC_MULTI2_LENGTH];
301         struct input_dev *dev;
302         int i, s;
303
304         gc_multi_read_packet(gc, gc->pads[GC_MULTI2] ? GC_MULTI2_LENGTH : GC_MULTI_LENGTH, data);
305
306         for (i = 0; i < GC_MAX_DEVICES; i++) {
307
308                 dev = gc->dev[i];
309                 if (!dev)
310                         continue;
311
312                 s = gc_status_bit[i];
313
314                 if (s & (gc->pads[GC_MULTI] | gc->pads[GC_MULTI2])) {
315                         input_report_abs(dev, ABS_X,  !(s & data[2]) - !(s & data[3]));
316                         input_report_abs(dev, ABS_Y,  !(s & data[0]) - !(s & data[1]));
317                         input_report_key(dev, BTN_TRIGGER, s & data[4]);
318                 }
319
320                 if (s & gc->pads[GC_MULTI2])
321                         input_report_key(dev, BTN_THUMB, s & data[5]);
322
323                 input_sync(dev);
324         }
325 }
326
327 /*
328  * PSX support
329  *
330  * See documentation at:
331  *      http://www.dim.com/~mackys/psxmemcard/ps-eng2.txt
332  *      http://www.gamesx.com/controldata/psxcont/psxcont.htm
333  *      ftp://milano.usal.es/pablo/
334  *
335  */
336
337 #define GC_PSX_DELAY    25              /* 25 usec */
338 #define GC_PSX_LENGTH   8               /* talk to the controller in bits */
339 #define GC_PSX_BYTES    6               /* the maximum number of bytes to read off the controller */
340
341 #define GC_PSX_MOUSE    1               /* Mouse */
342 #define GC_PSX_NEGCON   2               /* NegCon */
343 #define GC_PSX_NORMAL   4               /* Digital / Analog or Rumble in Digital mode  */
344 #define GC_PSX_ANALOG   5               /* Analog in Analog mode / Rumble in Green mode */
345 #define GC_PSX_RUMBLE   7               /* Rumble in Red mode */
346
347 #define GC_PSX_CLOCK    0x04            /* Pin 4 */
348 #define GC_PSX_COMMAND  0x01            /* Pin 2 */
349 #define GC_PSX_POWER    0xf8            /* Pins 5-9 */
350 #define GC_PSX_SELECT   0x02            /* Pin 3 */
351
352 #define GC_PSX_ID(x)    ((x) >> 4)      /* High nibble is device type */
353 #define GC_PSX_LEN(x)   (((x) & 0xf) << 1)      /* Low nibble is length in bytes/2 */
354
355 static int gc_psx_delay = GC_PSX_DELAY;
356 module_param_named(psx_delay, gc_psx_delay, uint, 0);
357 MODULE_PARM_DESC(psx_delay, "Delay when accessing Sony PSX controller (usecs)");
358
359 __obsolete_setup("gc_psx_delay=");
360
361 static short gc_psx_abs[] = { ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_HAT0X, ABS_HAT0Y };
362 static short gc_psx_btn[] = { BTN_TL, BTN_TR, BTN_TL2, BTN_TR2, BTN_A, BTN_B, BTN_X, BTN_Y,
363                                 BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR };
364 static short gc_psx_ddr_btn[] = { BTN_0, BTN_1, BTN_2, BTN_3 };
365
366 /*
367  * gc_psx_command() writes 8bit command and reads 8bit data from
368  * the psx pad.
369  */
370
371 static void gc_psx_command(struct gc *gc, int b, unsigned char data[GC_MAX_DEVICES])
372 {
373         int i, j, cmd, read;
374
375         for (i = 0; i < GC_MAX_DEVICES; i++)
376                 data[i] = 0;
377
378         for (i = 0; i < GC_PSX_LENGTH; i++, b >>= 1) {
379                 cmd = (b & 1) ? GC_PSX_COMMAND : 0;
380                 parport_write_data(gc->pd->port, cmd | GC_PSX_POWER);
381                 udelay(gc_psx_delay);
382                 read = parport_read_status(gc->pd->port) ^ 0x80;
383                 for (j = 0; j < GC_MAX_DEVICES; j++)
384                         data[j] |= (read & gc_status_bit[j] & (gc->pads[GC_PSX] | gc->pads[GC_DDR])) ? (1 << i) : 0;
385                 parport_write_data(gc->pd->port, cmd | GC_PSX_CLOCK | GC_PSX_POWER);
386                 udelay(gc_psx_delay);
387         }
388 }
389
390 /*
391  * gc_psx_read_packet() reads a whole psx packet and returns
392  * device identifier code.
393  */
394
395 static void gc_psx_read_packet(struct gc *gc, unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES],
396                                unsigned char id[GC_MAX_DEVICES])
397 {
398         int i, j, max_len = 0;
399         unsigned long flags;
400         unsigned char data2[GC_MAX_DEVICES];
401
402         parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);  /* Select pad */
403         udelay(gc_psx_delay);
404         parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_POWER);                  /* Deselect, begin command */
405         udelay(gc_psx_delay);
406
407         local_irq_save(flags);
408
409         gc_psx_command(gc, 0x01, data2);                                                /* Access pad */
410         gc_psx_command(gc, 0x42, id);                                                   /* Get device ids */
411         gc_psx_command(gc, 0, data2);                                                   /* Dump status */
412
413         for (i =0; i < GC_MAX_DEVICES; i++)                                                             /* Find the longest pad */
414                 if((gc_status_bit[i] & (gc->pads[GC_PSX] | gc->pads[GC_DDR]))
415                         && (GC_PSX_LEN(id[i]) > max_len)
416                         && (GC_PSX_LEN(id[i]) <= GC_PSX_BYTES))
417                         max_len = GC_PSX_LEN(id[i]);
418
419         for (i = 0; i < max_len; i++) {                                         /* Read in all the data */
420                 gc_psx_command(gc, 0, data2);
421                 for (j = 0; j < GC_MAX_DEVICES; j++)
422                         data[j][i] = data2[j];
423         }
424
425         local_irq_restore(flags);
426
427         parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
428
429         for(i = 0; i < GC_MAX_DEVICES; i++)                                                             /* Set id's to the real value */
430                 id[i] = GC_PSX_ID(id[i]);
431 }
432
433 static void gc_psx_process_packet(struct gc *gc)
434 {
435         unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES];
436         unsigned char id[GC_MAX_DEVICES];
437         struct input_dev *dev;
438         int i, j;
439
440         gc_psx_read_packet(gc, data, id);
441
442         for (i = 0; i < GC_MAX_DEVICES; i++) {
443
444                 dev = gc->dev[i];
445                 if (!dev)
446                         continue;
447
448                 switch (id[i]) {
449
450                         case GC_PSX_RUMBLE:
451
452                                 input_report_key(dev, BTN_THUMBL, ~data[i][0] & 0x04);
453                                 input_report_key(dev, BTN_THUMBR, ~data[i][0] & 0x02);
454
455                         case GC_PSX_NEGCON:
456                         case GC_PSX_ANALOG:
457
458                                 if (gc->pads[GC_DDR] & gc_status_bit[i]) {
459                                         for(j = 0; j < 4; j++)
460                                                 input_report_key(dev, gc_psx_ddr_btn[j], ~data[i][0] & (0x10 << j));
461                                 } else {
462                                         for (j = 0; j < 4; j++)
463                                                 input_report_abs(dev, gc_psx_abs[j + 2], data[i][j + 2]);
464
465                                         input_report_abs(dev, ABS_X, 128 + !(data[i][0] & 0x20) * 127 - !(data[i][0] & 0x80) * 128);
466                                         input_report_abs(dev, ABS_Y, 128 + !(data[i][0] & 0x40) * 127 - !(data[i][0] & 0x10) * 128);
467                                 }
468
469                                 for (j = 0; j < 8; j++)
470                                         input_report_key(dev, gc_psx_btn[j], ~data[i][1] & (1 << j));
471
472                                 input_report_key(dev, BTN_START,  ~data[i][0] & 0x08);
473                                 input_report_key(dev, BTN_SELECT, ~data[i][0] & 0x01);
474
475                                 input_sync(dev);
476
477                                 break;
478
479                         case GC_PSX_NORMAL:
480                                 if (gc->pads[GC_DDR] & gc_status_bit[i]) {
481                                         for(j = 0; j < 4; j++)
482                                                 input_report_key(dev, gc_psx_ddr_btn[j], ~data[i][0] & (0x10 << j));
483                                 } else {
484                                         input_report_abs(dev, ABS_X, 128 + !(data[i][0] & 0x20) * 127 - !(data[i][0] & 0x80) * 128);
485                                         input_report_abs(dev, ABS_Y, 128 + !(data[i][0] & 0x40) * 127 - !(data[i][0] & 0x10) * 128);
486
487                                         /* for some reason if the extra axes are left unset they drift */
488                                         /* for (j = 0; j < 4; j++)
489                                                 input_report_abs(dev, gc_psx_abs[j + 2], 128);
490                                          * This needs to be debugged properly,
491                                          * maybe fuzz processing needs to be done in input_sync()
492                                          *                               --vojtech
493                                          */
494                                 }
495
496                                 for (j = 0; j < 8; j++)
497                                         input_report_key(dev, gc_psx_btn[j], ~data[i][1] & (1 << j));
498
499                                 input_report_key(dev, BTN_START,  ~data[i][0] & 0x08);
500                                 input_report_key(dev, BTN_SELECT, ~data[i][0] & 0x01);
501
502                                 input_sync(dev);
503
504                                 break;
505
506                         case 0: /* not a pad, ignore */
507                                 break;
508                 }
509         }
510 }
511
512 /*
513  * gc_timer() initiates reads of console pads data.
514  */
515
516 static void gc_timer(unsigned long private)
517 {
518         struct gc *gc = (void *) private;
519
520 /*
521  * N64 pads - must be read first, any read confuses them for 200 us
522  */
523
524         if (gc->pads[GC_N64])
525                 gc_n64_process_packet(gc);
526
527 /*
528  * NES and SNES pads
529  */
530
531         if (gc->pads[GC_NES] || gc->pads[GC_SNES])
532                 gc_nes_process_packet(gc);
533
534 /*
535  * Multi and Multi2 joysticks
536  */
537
538         if (gc->pads[GC_MULTI] || gc->pads[GC_MULTI2])
539                 gc_multi_process_packet(gc);
540
541 /*
542  * PSX controllers
543  */
544
545         if (gc->pads[GC_PSX] || gc->pads[GC_DDR])
546                 gc_psx_process_packet(gc);
547
548         mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
549 }
550
551 static int gc_open(struct input_dev *dev)
552 {
553         struct gc *gc = dev->private;
554         int err;
555
556         err = mutex_lock_interruptible(&gc->mutex);
557         if (err)
558                 return err;
559
560         if (!gc->used++) {
561                 parport_claim(gc->pd);
562                 parport_write_control(gc->pd->port, 0x04);
563                 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
564         }
565
566         mutex_unlock(&gc->mutex);
567         return 0;
568 }
569
570 static void gc_close(struct input_dev *dev)
571 {
572         struct gc *gc = dev->private;
573
574         mutex_lock(&gc->mutex);
575         if (!--gc->used) {
576                 del_timer_sync(&gc->timer);
577                 parport_write_control(gc->pd->port, 0x00);
578                 parport_release(gc->pd);
579         }
580         mutex_unlock(&gc->mutex);
581 }
582
583 static int __init gc_setup_pad(struct gc *gc, int idx, int pad_type)
584 {
585         struct input_dev *input_dev;
586         int i;
587
588         if (!pad_type)
589                 return 0;
590
591         if (pad_type < 1 || pad_type > GC_MAX) {
592                 printk(KERN_WARNING "gamecon.c: Pad type %d unknown\n", pad_type);
593                 return -EINVAL;
594         }
595
596         gc->dev[idx] = input_dev = input_allocate_device();
597         if (!input_dev) {
598                 printk(KERN_ERR "gamecon.c: Not enough memory for input device\n");
599                 return -ENOMEM;
600         }
601
602         input_dev->name = gc_names[pad_type];
603         input_dev->phys = gc->phys[idx];
604         input_dev->id.bustype = BUS_PARPORT;
605         input_dev->id.vendor = 0x0001;
606         input_dev->id.product = pad_type;
607         input_dev->id.version = 0x0100;
608         input_dev->private = gc;
609
610         input_dev->open = gc_open;
611         input_dev->close = gc_close;
612
613         input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
614
615         for (i = 0; i < 2; i++)
616                 input_set_abs_params(input_dev, ABS_X + i, -1, 1, 0, 0);
617
618         gc->pads[0] |= gc_status_bit[idx];
619         gc->pads[pad_type] |= gc_status_bit[idx];
620
621         switch (pad_type) {
622
623                 case GC_N64:
624                         for (i = 0; i < 10; i++)
625                                 set_bit(gc_n64_btn[i], input_dev->keybit);
626
627                         for (i = 0; i < 2; i++) {
628                                 input_set_abs_params(input_dev, ABS_X + i, -127, 126, 0, 2);
629                                 input_set_abs_params(input_dev, ABS_HAT0X + i, -1, 1, 0, 0);
630                         }
631
632                         break;
633
634                 case GC_SNES:
635                         for (i = 4; i < 8; i++)
636                                 set_bit(gc_snes_btn[i], input_dev->keybit);
637                 case GC_NES:
638                         for (i = 0; i < 4; i++)
639                                 set_bit(gc_snes_btn[i], input_dev->keybit);
640                         break;
641
642                 case GC_MULTI2:
643                         set_bit(BTN_THUMB, input_dev->keybit);
644                 case GC_MULTI:
645                         set_bit(BTN_TRIGGER, input_dev->keybit);
646                         break;
647
648                 case GC_PSX:
649                         for (i = 0; i < 6; i++)
650                                 input_set_abs_params(input_dev, gc_psx_abs[i], 4, 252, 0, 2);
651                         for (i = 0; i < 12; i++)
652                                 set_bit(gc_psx_btn[i], input_dev->keybit);
653
654                         break;
655
656                 case GC_DDR:
657                         for (i = 0; i < 4; i++)
658                                 set_bit(gc_psx_ddr_btn[i], input_dev->keybit);
659                         for (i = 0; i < 12; i++)
660                                 set_bit(gc_psx_btn[i], input_dev->keybit);
661
662                         break;
663         }
664
665         return 0;
666 }
667
668 static struct gc __init *gc_probe(int parport, int *pads, int n_pads)
669 {
670         struct gc *gc;
671         struct parport *pp;
672         struct pardevice *pd;
673         int i;
674         int err;
675
676         pp = parport_find_number(parport);
677         if (!pp) {
678                 printk(KERN_ERR "gamecon.c: no such parport\n");
679                 err = -EINVAL;
680                 goto err_out;
681         }
682
683         pd = parport_register_device(pp, "gamecon", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
684         if (!pd) {
685                 printk(KERN_ERR "gamecon.c: parport busy already - lp.o loaded?\n");
686                 err = -EBUSY;
687                 goto err_put_pp;
688         }
689
690         gc = kzalloc(sizeof(struct gc), GFP_KERNEL);
691         if (!gc) {
692                 printk(KERN_ERR "gamecon.c: Not enough memory\n");
693                 err = -ENOMEM;
694                 goto err_unreg_pardev;
695         }
696
697         mutex_init(&gc->mutex);
698         gc->pd = pd;
699         init_timer(&gc->timer);
700         gc->timer.data = (long) gc;
701         gc->timer.function = gc_timer;
702
703         for (i = 0; i < n_pads && i < GC_MAX_DEVICES; i++) {
704                 if (!pads[i])
705                         continue;
706
707                 sprintf(gc->phys[i], "%s/input%d", gc->pd->port->name, i);
708                 err = gc_setup_pad(gc, i, pads[i]);
709                 if (err)
710                         goto err_unreg_devs;
711
712                 err = input_register_device(gc->dev[i]);
713                 if (err)
714                         goto err_free_dev;
715         }
716
717         if (!gc->pads[0]) {
718                 printk(KERN_ERR "gamecon.c: No valid devices specified\n");
719                 err = -EINVAL;
720                 goto err_free_gc;
721         }
722
723         parport_put_port(pp);
724         return gc;
725
726  err_free_dev:
727         input_free_device(gc->dev[i]);
728  err_unreg_devs:
729         while (--i >= 0)
730                 if (gc->dev[i])
731                         input_unregister_device(gc->dev[i]);
732  err_free_gc:
733         kfree(gc);
734  err_unreg_pardev:
735         parport_unregister_device(pd);
736  err_put_pp:
737         parport_put_port(pp);
738  err_out:
739         return ERR_PTR(err);
740 }
741
742 static void gc_remove(struct gc *gc)
743 {
744         int i;
745
746         for (i = 0; i < GC_MAX_DEVICES; i++)
747                 if (gc->dev[i])
748                         input_unregister_device(gc->dev[i]);
749         parport_unregister_device(gc->pd);
750         kfree(gc);
751 }
752
753 static int __init gc_init(void)
754 {
755         int i;
756         int have_dev = 0;
757         int err = 0;
758
759         for (i = 0; i < GC_MAX_PORTS; i++) {
760                 if (gc[i].nargs == 0 || gc[i].args[0] < 0)
761                         continue;
762
763                 if (gc[i].nargs < 2) {
764                         printk(KERN_ERR "gamecon.c: at least one device must be specified\n");
765                         err = -EINVAL;
766                         break;
767                 }
768
769                 gc_base[i] = gc_probe(gc[i].args[0], gc[i].args + 1, gc[i].nargs - 1);
770                 if (IS_ERR(gc_base[i])) {
771                         err = PTR_ERR(gc_base[i]);
772                         break;
773                 }
774
775                 have_dev = 1;
776         }
777
778         if (err) {
779                 while (--i >= 0)
780                         if (gc_base[i])
781                                 gc_remove(gc_base[i]);
782                 return err;
783         }
784
785         return have_dev ? 0 : -ENODEV;
786 }
787
788 static void __exit gc_exit(void)
789 {
790         int i;
791
792         for (i = 0; i < GC_MAX_PORTS; i++)
793                 if (gc_base[i])
794                         gc_remove(gc_base[i]);
795 }
796
797 module_init(gc_init);
798 module_exit(gc_exit);