blob: d57edd4a5992995b31d49f0759e12f8daa333e5a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NES, SNES, N64, MultiSystem, PSX gamepad driver for Linux
3 *
Dmitry Torokhovab0c3442005-05-29 02:28:55 -05004 * Copyright (c) 1999-2004 Vojtech Pavlik <vojtech@suse.cz>
5 * Copyright (c) 2004 Peter Nelson <rufus-kernel@hackish.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Based on the work of:
Dmitry Torokhovab0c3442005-05-29 02:28:55 -05008 * Andree Borrmann John Dahlstrom
9 * David Kuder Nathan Hand
Raphael Assenatb157d552006-04-02 00:10:05 -050010 * Raphael Assenat
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
13/*
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.
18 *
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.
23 *
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
27 *
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
31 */
32
33#include <linux/kernel.h>
34#include <linux/delay.h>
35#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/init.h>
37#include <linux/parport.h>
38#include <linux/input.h>
Ingo Molnar72ba9f02006-02-19 00:22:30 -050039#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
42MODULE_DESCRIPTION("NES, SNES, N64, MultiSystem, PSX gamepad driver");
43MODULE_LICENSE("GPL");
44
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050045#define GC_MAX_PORTS 3
46#define GC_MAX_DEVICES 5
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050048struct gc_config {
49 int args[GC_MAX_DEVICES + 1];
Dmitry Torokhov78167232007-05-03 00:52:51 -040050 unsigned int nargs;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050051};
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Dmitry Torokhov78167232007-05-03 00:52:51 -040053static struct gc_config gc_cfg[GC_MAX_PORTS] __initdata;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050054
Dmitry Torokhov78167232007-05-03 00:52:51 -040055module_param_array_named(map, gc_cfg[0].args, int, &gc_cfg[0].nargs, 0);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050056MODULE_PARM_DESC(map, "Describes first set of devices (<parport#>,<pad1>,<pad2>,..<pad5>)");
Dmitry Torokhov78167232007-05-03 00:52:51 -040057module_param_array_named(map2, gc_cfg[1].args, int, &gc_cfg[1].nargs, 0);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050058MODULE_PARM_DESC(map2, "Describes second set of devices");
Dmitry Torokhov78167232007-05-03 00:52:51 -040059module_param_array_named(map3, gc_cfg[2].args, int, &gc_cfg[2].nargs, 0);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050060MODULE_PARM_DESC(map3, "Describes third set of devices");
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/* see also gs_psx_delay parameter in PSX support section */
63
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
Raphael Assenatb157d552006-04-02 00:10:05 -050072#define GC_SNESMOUSE 9
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Raphael Assenatb157d552006-04-02 00:10:05 -050074#define GC_MAX 9
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76#define GC_REFRESH_TIME HZ/100
77
78struct gc {
79 struct pardevice *pd;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050080 struct input_dev *dev[GC_MAX_DEVICES];
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 struct timer_list timer;
82 unsigned char pads[GC_MAX + 1];
83 int used;
Ingo Molnar72ba9f02006-02-19 00:22:30 -050084 struct mutex mutex;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050085 char phys[GC_MAX_DEVICES][32];
Linus Torvalds1da177e2005-04-16 15:20:36 -070086};
87
Scott Moreau7aa9e0e2010-02-21 20:53:55 -080088struct gc_subdev {
89 unsigned int idx;
90};
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static struct gc *gc_base[3];
93
94static int gc_status_bit[] = { 0x40, 0x80, 0x20, 0x10, 0x08 };
95
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -080096static char *gc_names[] = {
97 NULL, "SNES pad", "NES pad", "NES FourPort", "Multisystem joystick",
98 "Multisystem 2-button joystick", "N64 controller", "PSX controller",
99 "PSX DDR controller", "SNES mouse"
100};
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/*
103 * N64 support.
104 */
105
106static unsigned char gc_n64_bytes[] = { 0, 1, 13, 15, 14, 12, 10, 11, 2, 3 };
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800107static short gc_n64_btn[] = {
108 BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z,
109 BTN_TL, BTN_TR, BTN_TRIGGER, BTN_START
110};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112#define GC_N64_LENGTH 32 /* N64 bit length, not including stop bit */
Scott Moreau7aa9e0e2010-02-21 20:53:55 -0800113#define GC_N64_STOP_LENGTH 5 /* Length of encoded stop bit */
114#define GC_N64_CMD_00 0x11111111UL
115#define GC_N64_CMD_01 0xd1111111UL
116#define GC_N64_CMD_03 0xdd111111UL
117#define GC_N64_CMD_1b 0xdd1dd111UL
118#define GC_N64_CMD_c0 0x111111ddUL
119#define GC_N64_CMD_80 0x1111111dUL
120#define GC_N64_STOP_BIT 0x1d /* Encoded stop bit */
121#define GC_N64_REQUEST_DATA GC_N64_CMD_01 /* the request data command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#define GC_N64_DELAY 133 /* delay between transmit request, and response ready (us) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123#define GC_N64_DWS 3 /* delay between write segments (required for sound playback because of ISA DMA) */
124 /* GC_N64_DWS > 24 is known to fail */
125#define GC_N64_POWER_W 0xe2 /* power during write (transmit request) */
126#define GC_N64_POWER_R 0xfd /* power during read */
127#define GC_N64_OUT 0x1d /* output bits to the 4 pads */
128 /* Reading the main axes of any N64 pad is known to fail if the corresponding bit */
129 /* in GC_N64_OUT is pulled low on the output port (by any routine) for more */
130 /* than 123 us */
131#define GC_N64_CLOCK 0x02 /* clock bits for read */
132
133/*
Scott Moreau7aa9e0e2010-02-21 20:53:55 -0800134 * Used for rumble code.
135 */
136
137/* Send encoded command */
138static void gc_n64_send_command(struct gc *gc, unsigned long cmd,
139 unsigned char target)
140{
141 struct parport *port = gc->pd->port;
142 int i;
143
144 for (i = 0; i < GC_N64_LENGTH; i++) {
145 unsigned char data = (cmd >> i) & 1 ? target : 0;
146 parport_write_data(port, GC_N64_POWER_W | data);
147 udelay(GC_N64_DWS);
148 }
149}
150
151/* Send stop bit */
152static void gc_n64_send_stop_bit(struct gc *gc, unsigned char target)
153{
154 struct parport *port = gc->pd->port;
155 int i;
156
157 for (i = 0; i < GC_N64_STOP_LENGTH; i++) {
158 unsigned char data = (GC_N64_STOP_BIT >> i) & 1 ? target : 0;
159 parport_write_data(port, GC_N64_POWER_W | data);
160 udelay(GC_N64_DWS);
161 }
162}
163
164/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 * gc_n64_read_packet() reads an N64 packet.
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800166 * Each pad uses one bit per byte. So all pads connected to this port
167 * are read in parallel.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 */
169
170static void gc_n64_read_packet(struct gc *gc, unsigned char *data)
171{
172 int i;
173 unsigned long flags;
174
175/*
176 * Request the pad to transmit data
177 */
178
179 local_irq_save(flags);
Scott Moreau7aa9e0e2010-02-21 20:53:55 -0800180 gc_n64_send_command(gc, GC_N64_REQUEST_DATA, GC_N64_OUT);
181 gc_n64_send_stop_bit(gc, GC_N64_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 local_irq_restore(flags);
183
184/*
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800185 * Wait for the pad response to be loaded into the 33-bit register
186 * of the adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 */
188
189 udelay(GC_N64_DELAY);
190
191/*
192 * Grab data (ignoring the last bit, which is a stop bit)
193 */
194
195 for (i = 0; i < GC_N64_LENGTH; i++) {
196 parport_write_data(gc->pd->port, GC_N64_POWER_R);
Scott Moreau7aa9e0e2010-02-21 20:53:55 -0800197 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 data[i] = parport_read_status(gc->pd->port);
199 parport_write_data(gc->pd->port, GC_N64_POWER_R | GC_N64_CLOCK);
200 }
201
202/*
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800203 * We must wait 200 ms here for the controller to reinitialize before
204 * the next read request. No worries as long as gc_read is polled less
205 * frequently than this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 */
207
208}
209
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500210static void gc_n64_process_packet(struct gc *gc)
211{
212 unsigned char data[GC_N64_LENGTH];
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500213 struct input_dev *dev;
214 int i, j, s;
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800215 signed char x, y;
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500216
217 gc_n64_read_packet(gc, data);
218
219 for (i = 0; i < GC_MAX_DEVICES; i++) {
220
221 dev = gc->dev[i];
222 if (!dev)
223 continue;
224
225 s = gc_status_bit[i];
226
227 if (s & gc->pads[GC_N64] & ~(data[8] | data[9])) {
228
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800229 x = y = 0;
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500230
231 for (j = 0; j < 8; j++) {
232 if (data[23 - j] & s)
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800233 x |= 1 << j;
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500234 if (data[31 - j] & s)
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800235 y |= 1 << j;
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500236 }
237
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800238 input_report_abs(dev, ABS_X, x);
239 input_report_abs(dev, ABS_Y, -y);
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500240
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800241 input_report_abs(dev, ABS_HAT0X,
242 !(s & data[6]) - !(s & data[7]));
243 input_report_abs(dev, ABS_HAT0Y,
244 !(s & data[4]) - !(s & data[5]));
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500245
246 for (j = 0; j < 10; j++)
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800247 input_report_key(dev, gc_n64_btn[j],
248 s & data[gc_n64_bytes[j]]);
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500249
250 input_sync(dev);
251 }
252 }
253}
254
Scott Moreau7aa9e0e2010-02-21 20:53:55 -0800255static int gc_n64_play_effect(struct input_dev *dev, void *data,
256 struct ff_effect *effect)
257{
258 int i;
259 unsigned long flags;
260 struct gc *gc = input_get_drvdata(dev);
261 struct gc_subdev *sdev = data;
262 unsigned char target = 1 << sdev->idx; /* select desired pin */
263
264 if (effect->type == FF_RUMBLE) {
265 struct ff_rumble_effect *rumble = &effect->u.rumble;
266 unsigned int cmd =
267 rumble->strong_magnitude || rumble->weak_magnitude ?
268 GC_N64_CMD_01 : GC_N64_CMD_00;
269
270 local_irq_save(flags);
271
272 /* Init Rumble - 0x03, 0x80, 0x01, (34)0x80 */
273 gc_n64_send_command(gc, GC_N64_CMD_03, target);
274 gc_n64_send_command(gc, GC_N64_CMD_80, target);
275 gc_n64_send_command(gc, GC_N64_CMD_01, target);
276 for (i = 0; i < 32; i++)
277 gc_n64_send_command(gc, GC_N64_CMD_80, target);
278 gc_n64_send_stop_bit(gc, target);
279
280 udelay(GC_N64_DELAY);
281
282 /* Now start or stop it - 0x03, 0xc0, 0zx1b, (32)0x01/0x00 */
283 gc_n64_send_command(gc, GC_N64_CMD_03, target);
284 gc_n64_send_command(gc, GC_N64_CMD_c0, target);
285 gc_n64_send_command(gc, GC_N64_CMD_1b, target);
286 for (i = 0; i < 32; i++)
287 gc_n64_send_command(gc, cmd, target);
288 gc_n64_send_stop_bit(gc, target);
289
290 local_irq_restore(flags);
291
292 }
293
294 return 0;
295}
296
297static int __init gc_n64_init_ff(struct input_dev *dev, int i)
298{
299 struct gc_subdev *sdev;
300 int err;
301
302 sdev = kmalloc(sizeof(*sdev), GFP_KERNEL);
303 if (!sdev)
304 return -ENOMEM;
305
306 sdev->idx = i;
307
308 input_set_capability(dev, EV_FF, FF_RUMBLE);
309
310 err = input_ff_create_memless(dev, sdev, gc_n64_play_effect);
311 if (err) {
312 kfree(sdev);
313 return err;
314 }
315
316 return 0;
317}
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319/*
320 * NES/SNES support.
321 */
322
Raphael Assenatb157d552006-04-02 00:10:05 -0500323#define GC_NES_DELAY 6 /* Delay between bits - 6us */
324#define GC_NES_LENGTH 8 /* The NES pads use 8 bits of data */
325#define GC_SNES_LENGTH 12 /* The SNES true length is 16, but the
326 last 4 bits are unused */
327#define GC_SNESMOUSE_LENGTH 32 /* The SNES mouse uses 32 bits, the first
328 16 bits are equivalent to a gamepad */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330#define GC_NES_POWER 0xfc
331#define GC_NES_CLOCK 0x01
332#define GC_NES_LATCH 0x02
333
334static unsigned char gc_nes_bytes[] = { 0, 1, 2, 3 };
335static unsigned char gc_snes_bytes[] = { 8, 0, 2, 3, 9, 1, 10, 11 };
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800336static short gc_snes_btn[] = {
337 BTN_A, BTN_B, BTN_SELECT, BTN_START, BTN_X, BTN_Y, BTN_TL, BTN_TR
338};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340/*
341 * gc_nes_read_packet() reads a NES/SNES packet.
342 * Each pad uses one bit per byte. So all pads connected to
343 * this port are read in parallel.
344 */
345
346static void gc_nes_read_packet(struct gc *gc, int length, unsigned char *data)
347{
348 int i;
349
350 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK | GC_NES_LATCH);
351 udelay(GC_NES_DELAY * 2);
352 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
353
354 for (i = 0; i < length; i++) {
355 udelay(GC_NES_DELAY);
356 parport_write_data(gc->pd->port, GC_NES_POWER);
357 data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
358 udelay(GC_NES_DELAY);
359 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
360 }
361}
362
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500363static void gc_nes_process_packet(struct gc *gc)
364{
Raphael Assenatb157d552006-04-02 00:10:05 -0500365 unsigned char data[GC_SNESMOUSE_LENGTH];
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500366 struct input_dev *dev;
Raphael Assenatb157d552006-04-02 00:10:05 -0500367 int i, j, s, len;
368 char x_rel, y_rel;
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500369
Raphael Assenatb157d552006-04-02 00:10:05 -0500370 len = gc->pads[GC_SNESMOUSE] ? GC_SNESMOUSE_LENGTH :
371 (gc->pads[GC_SNES] ? GC_SNES_LENGTH : GC_NES_LENGTH);
372
373 gc_nes_read_packet(gc, len, data);
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500374
375 for (i = 0; i < GC_MAX_DEVICES; i++) {
376
377 dev = gc->dev[i];
378 if (!dev)
379 continue;
380
381 s = gc_status_bit[i];
382
383 if (s & (gc->pads[GC_NES] | gc->pads[GC_SNES])) {
384 input_report_abs(dev, ABS_X, !(s & data[6]) - !(s & data[7]));
385 input_report_abs(dev, ABS_Y, !(s & data[4]) - !(s & data[5]));
386 }
387
388 if (s & gc->pads[GC_NES])
389 for (j = 0; j < 4; j++)
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800390 input_report_key(dev, gc_snes_btn[j],
391 s & data[gc_nes_bytes[j]]);
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500392
393 if (s & gc->pads[GC_SNES])
394 for (j = 0; j < 8; j++)
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800395 input_report_key(dev, gc_snes_btn[j],
396 s & data[gc_snes_bytes[j]]);
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500397
Raphael Assenatb157d552006-04-02 00:10:05 -0500398 if (s & gc->pads[GC_SNESMOUSE]) {
399 /*
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800400 * The 4 unused bits from SNES controllers appear
401 * to be ID bits so use them to make sure we are
402 * dealing with a mouse.
Raphael Assenatb157d552006-04-02 00:10:05 -0500403 * gamepad is connected. This is important since
404 * my SNES gamepad sends 1's for bits 16-31, which
405 * cause the mouse pointer to quickly move to the
406 * upper left corner of the screen.
407 */
408 if (!(s & data[12]) && !(s & data[13]) &&
409 !(s & data[14]) && (s & data[15])) {
410 input_report_key(dev, BTN_LEFT, s & data[9]);
411 input_report_key(dev, BTN_RIGHT, s & data[8]);
412
413 x_rel = y_rel = 0;
414 for (j = 0; j < 7; j++) {
415 x_rel <<= 1;
416 if (data[25 + j] & s)
417 x_rel |= 1;
418
419 y_rel <<= 1;
420 if (data[17 + j] & s)
421 y_rel |= 1;
422 }
423
424 if (x_rel) {
425 if (data[24] & s)
426 x_rel = -x_rel;
427 input_report_rel(dev, REL_X, x_rel);
428 }
429
430 if (y_rel) {
431 if (data[16] & s)
432 y_rel = -y_rel;
433 input_report_rel(dev, REL_Y, y_rel);
434 }
435 }
436 }
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500437 input_sync(dev);
438 }
439}
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441/*
442 * Multisystem joystick support
443 */
444
445#define GC_MULTI_LENGTH 5 /* Multi system joystick packet length is 5 */
446#define GC_MULTI2_LENGTH 6 /* One more bit for one more button */
447
448/*
449 * gc_multi_read_packet() reads a Multisystem joystick packet.
450 */
451
452static void gc_multi_read_packet(struct gc *gc, int length, unsigned char *data)
453{
454 int i;
455
456 for (i = 0; i < length; i++) {
457 parport_write_data(gc->pd->port, ~(1 << i));
458 data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
459 }
460}
461
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500462static void gc_multi_process_packet(struct gc *gc)
463{
464 unsigned char data[GC_MULTI2_LENGTH];
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800465 int data_len = gc->pads[GC_MULTI2] ? GC_MULTI2_LENGTH : GC_MULTI_LENGTH;
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500466 struct input_dev *dev;
467 int i, s;
468
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800469 gc_multi_read_packet(gc, data_len, data);
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500470
471 for (i = 0; i < GC_MAX_DEVICES; i++) {
472
473 dev = gc->dev[i];
474 if (!dev)
475 continue;
476
477 s = gc_status_bit[i];
478
479 if (s & (gc->pads[GC_MULTI] | gc->pads[GC_MULTI2])) {
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800480 input_report_abs(dev, ABS_X,
481 !(s & data[2]) - !(s & data[3]));
482 input_report_abs(dev, ABS_Y,
483 !(s & data[0]) - !(s & data[1]));
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500484 input_report_key(dev, BTN_TRIGGER, s & data[4]);
485 }
486
487 if (s & gc->pads[GC_MULTI2])
488 input_report_key(dev, BTN_THUMB, s & data[5]);
489
490 input_sync(dev);
491 }
492}
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494/*
495 * PSX support
496 *
497 * See documentation at:
498 * http://www.dim.com/~mackys/psxmemcard/ps-eng2.txt
499 * http://www.gamesx.com/controldata/psxcont/psxcont.htm
500 * ftp://milano.usal.es/pablo/
501 *
502 */
503
504#define GC_PSX_DELAY 25 /* 25 usec */
505#define GC_PSX_LENGTH 8 /* talk to the controller in bits */
506#define GC_PSX_BYTES 6 /* the maximum number of bytes to read off the controller */
507
508#define GC_PSX_MOUSE 1 /* Mouse */
509#define GC_PSX_NEGCON 2 /* NegCon */
510#define GC_PSX_NORMAL 4 /* Digital / Analog or Rumble in Digital mode */
511#define GC_PSX_ANALOG 5 /* Analog in Analog mode / Rumble in Green mode */
512#define GC_PSX_RUMBLE 7 /* Rumble in Red mode */
513
514#define GC_PSX_CLOCK 0x04 /* Pin 4 */
515#define GC_PSX_COMMAND 0x01 /* Pin 2 */
516#define GC_PSX_POWER 0xf8 /* Pins 5-9 */
517#define GC_PSX_SELECT 0x02 /* Pin 3 */
518
519#define GC_PSX_ID(x) ((x) >> 4) /* High nibble is device type */
520#define GC_PSX_LEN(x) (((x) & 0xf) << 1) /* Low nibble is length in bytes/2 */
521
522static int gc_psx_delay = GC_PSX_DELAY;
523module_param_named(psx_delay, gc_psx_delay, uint, 0);
524MODULE_PARM_DESC(psx_delay, "Delay when accessing Sony PSX controller (usecs)");
525
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800526static short gc_psx_abs[] = {
527 ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_HAT0X, ABS_HAT0Y
528};
529static short gc_psx_btn[] = {
530 BTN_TL, BTN_TR, BTN_TL2, BTN_TR2, BTN_A, BTN_B, BTN_X, BTN_Y,
531 BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR
532};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533static short gc_psx_ddr_btn[] = { BTN_0, BTN_1, BTN_2, BTN_3 };
534
535/*
536 * gc_psx_command() writes 8bit command and reads 8bit data from
537 * the psx pad.
538 */
539
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800540static void gc_psx_command(struct gc *gc, int b, unsigned char *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800542 struct parport *port = gc->pd->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 int i, j, cmd, read;
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500544
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800545 memset(data, 0, GC_MAX_DEVICES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 for (i = 0; i < GC_PSX_LENGTH; i++, b >>= 1) {
548 cmd = (b & 1) ? GC_PSX_COMMAND : 0;
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800549 parport_write_data(port, cmd | GC_PSX_POWER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 udelay(gc_psx_delay);
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800551 read = parport_read_status(port) ^ 0x80;
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500552 for (j = 0; j < GC_MAX_DEVICES; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 data[j] |= (read & gc_status_bit[j] & (gc->pads[GC_PSX] | gc->pads[GC_DDR])) ? (1 << i) : 0;
554 parport_write_data(gc->pd->port, cmd | GC_PSX_CLOCK | GC_PSX_POWER);
555 udelay(gc_psx_delay);
556 }
557}
558
559/*
560 * gc_psx_read_packet() reads a whole psx packet and returns
561 * device identifier code.
562 */
563
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500564static void gc_psx_read_packet(struct gc *gc, unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES],
565 unsigned char id[GC_MAX_DEVICES])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
567 int i, j, max_len = 0;
568 unsigned long flags;
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500569 unsigned char data2[GC_MAX_DEVICES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800571 /* Select pad */
572 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 udelay(gc_psx_delay);
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800574 /* Deselect, begin command */
575 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_POWER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 udelay(gc_psx_delay);
577
578 local_irq_save(flags);
579
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800580 gc_psx_command(gc, 0x01, data2); /* Access pad */
581 gc_psx_command(gc, 0x42, id); /* Get device ids */
582 gc_psx_command(gc, 0, data2); /* Dump status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800584 /* Find the longest pad */
585 for (i = 0; i < GC_MAX_DEVICES; i++)
586 if ((gc_status_bit[i] & (gc->pads[GC_PSX] | gc->pads[GC_DDR])) &&
587 GC_PSX_LEN(id[i]) > max_len &&
588 GC_PSX_LEN(id[i]) <= GC_PSX_BYTES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 max_len = GC_PSX_LEN(id[i]);
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800592 /* Read in all the data */
593 for (i = 0; i < max_len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 gc_psx_command(gc, 0, data2);
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500595 for (j = 0; j < GC_MAX_DEVICES; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 data[j][i] = data2[j];
597 }
598
599 local_irq_restore(flags);
600
601 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
602
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800603 /* Set id's to the real value */
604 for (i = 0; i < GC_MAX_DEVICES; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 id[i] = GC_PSX_ID(id[i]);
606}
607
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800608static void gc_psx_report_one(struct gc *gc, struct input_dev *dev,
609 unsigned char pad_type, unsigned char status_bit,
610 unsigned char *data)
611{
612 int i;
613
614 switch (pad_type) {
615
616 case GC_PSX_RUMBLE:
617
618 input_report_key(dev, BTN_THUMBL, ~data[0] & 0x04);
619 input_report_key(dev, BTN_THUMBR, ~data[0] & 0x02);
620
621 case GC_PSX_NEGCON:
622 case GC_PSX_ANALOG:
623
624 if (gc->pads[GC_DDR] & status_bit) {
625 for (i = 0; i < 4; i++)
626 input_report_key(dev, gc_psx_ddr_btn[i],
627 ~data[0] & (0x10 << i));
628 } else {
629 for (i = 0; i < 4; i++)
630 input_report_abs(dev, gc_psx_abs[i + 2],
631 data[i + 2]);
632
Dmitry Torokhov315543f2010-02-21 20:54:31 -0800633 input_report_abs(dev, ABS_X,
634 !!(data[0] & 0x80) * 128 + !(data[0] & 0x20) * 127);
635 input_report_abs(dev, ABS_Y,
636 !!(data[0] & 0x10) * 128 + !(data[0] & 0x40) * 127);
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800637 }
638
639 for (i = 0; i < 8; i++)
640 input_report_key(dev, gc_psx_btn[i], ~data[1] & (1 << i));
641
642 input_report_key(dev, BTN_START, ~data[0] & 0x08);
643 input_report_key(dev, BTN_SELECT, ~data[0] & 0x01);
644
645 input_sync(dev);
646
647 break;
648
649 case GC_PSX_NORMAL:
650 if (gc->pads[GC_DDR] & status_bit) {
651 for (i = 0; i < 4; i++)
652 input_report_key(dev, gc_psx_ddr_btn[i],
653 ~data[0] & (0x10 << i));
654 } else {
Dmitry Torokhov315543f2010-02-21 20:54:31 -0800655 input_report_abs(dev, ABS_X,
656 !!(data[0] & 0x80) * 128 + !(data[0] & 0x20) * 127);
657 input_report_abs(dev, ABS_Y,
658 !!(data[0] & 0x10) * 128 + !(data[0] & 0x40) * 127);
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800659
660 /*
661 * For some reason if the extra axes are left unset
662 * they drift.
663 * for (i = 0; i < 4; i++)
664 input_report_abs(dev, gc_psx_abs[i + 2], 128);
665 * This needs to be debugged properly,
666 * maybe fuzz processing needs to be done
667 * in input_sync()
668 * --vojtech
669 */
670 }
671
672 for (i = 0; i < 8; i++)
673 input_report_key(dev, gc_psx_btn[i], ~data[1] & (1 << i));
674
675 input_report_key(dev, BTN_START, ~data[0] & 0x08);
676 input_report_key(dev, BTN_SELECT, ~data[0] & 0x01);
677
678 input_sync(dev);
679
680 break;
681
682 case 0: /* not a pad, ignore */
683 break;
684 }
685}
686
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500687static void gc_psx_process_packet(struct gc *gc)
688{
689 unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES];
690 unsigned char id[GC_MAX_DEVICES];
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800691 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500693 gc_psx_read_packet(gc, data, id);
694
695 for (i = 0; i < GC_MAX_DEVICES; i++) {
696
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800697 if (gc->dev[i])
698 gc_psx_report_one(gc, gc->dev[i],
699 id[i], gc_status_bit[i], data[i]);
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500700 }
701}
702
703/*
704 * gc_timer() initiates reads of console pads data.
705 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707static void gc_timer(unsigned long private)
708{
709 struct gc *gc = (void *) private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711/*
712 * N64 pads - must be read first, any read confuses them for 200 us
713 */
714
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500715 if (gc->pads[GC_N64])
716 gc_n64_process_packet(gc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718/*
Raphael Assenatb157d552006-04-02 00:10:05 -0500719 * NES and SNES pads or mouse
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 */
721
Raphael Assenatb157d552006-04-02 00:10:05 -0500722 if (gc->pads[GC_NES] || gc->pads[GC_SNES] || gc->pads[GC_SNESMOUSE])
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500723 gc_nes_process_packet(gc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725/*
726 * Multi and Multi2 joysticks
727 */
728
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500729 if (gc->pads[GC_MULTI] || gc->pads[GC_MULTI2])
730 gc_multi_process_packet(gc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732/*
733 * PSX controllers
734 */
735
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500736 if (gc->pads[GC_PSX] || gc->pads[GC_DDR])
737 gc_psx_process_packet(gc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
740}
741
742static int gc_open(struct input_dev *dev)
743{
Dmitry Torokhov8715c1c2007-04-12 01:34:14 -0400744 struct gc *gc = input_get_drvdata(dev);
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500745 int err;
746
Ingo Molnar72ba9f02006-02-19 00:22:30 -0500747 err = mutex_lock_interruptible(&gc->mutex);
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500748 if (err)
749 return err;
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (!gc->used++) {
752 parport_claim(gc->pd);
753 parport_write_control(gc->pd->port, 0x04);
754 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
755 }
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500756
Ingo Molnar72ba9f02006-02-19 00:22:30 -0500757 mutex_unlock(&gc->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return 0;
759}
760
761static void gc_close(struct input_dev *dev)
762{
Dmitry Torokhov8715c1c2007-04-12 01:34:14 -0400763 struct gc *gc = input_get_drvdata(dev);
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500764
Ingo Molnar72ba9f02006-02-19 00:22:30 -0500765 mutex_lock(&gc->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (!--gc->used) {
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500767 del_timer_sync(&gc->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 parport_write_control(gc->pd->port, 0x00);
769 parport_release(gc->pd);
770 }
Ingo Molnar72ba9f02006-02-19 00:22:30 -0500771 mutex_unlock(&gc->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500774static int __init gc_setup_pad(struct gc *gc, int idx, int pad_type)
775{
776 struct input_dev *input_dev;
777 int i;
Scott Moreau7aa9e0e2010-02-21 20:53:55 -0800778 int err;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500779
780 if (!pad_type)
781 return 0;
782
783 if (pad_type < 1 || pad_type > GC_MAX) {
784 printk(KERN_WARNING "gamecon.c: Pad type %d unknown\n", pad_type);
785 return -EINVAL;
786 }
787
788 gc->dev[idx] = input_dev = input_allocate_device();
789 if (!input_dev) {
790 printk(KERN_ERR "gamecon.c: Not enough memory for input device\n");
791 return -ENOMEM;
792 }
793
794 input_dev->name = gc_names[pad_type];
795 input_dev->phys = gc->phys[idx];
796 input_dev->id.bustype = BUS_PARPORT;
797 input_dev->id.vendor = 0x0001;
798 input_dev->id.product = pad_type;
799 input_dev->id.version = 0x0100;
Dmitry Torokhov8715c1c2007-04-12 01:34:14 -0400800
801 input_set_drvdata(input_dev, gc);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500802
803 input_dev->open = gc_open;
804 input_dev->close = gc_close;
805
Raphael Assenatb157d552006-04-02 00:10:05 -0500806 if (pad_type != GC_SNESMOUSE) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700807 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500808
Raphael Assenatb157d552006-04-02 00:10:05 -0500809 for (i = 0; i < 2; i++)
810 input_set_abs_params(input_dev, ABS_X + i, -1, 1, 0, 0);
811 } else
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700812 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500813
814 gc->pads[0] |= gc_status_bit[idx];
815 gc->pads[pad_type] |= gc_status_bit[idx];
816
817 switch (pad_type) {
818
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800819 case GC_N64:
820 for (i = 0; i < 10; i++)
821 __set_bit(gc_n64_btn[i], input_dev->keybit);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500822
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800823 for (i = 0; i < 2; i++) {
824 input_set_abs_params(input_dev, ABS_X + i, -127, 126, 0, 2);
825 input_set_abs_params(input_dev, ABS_HAT0X + i, -1, 1, 0, 0);
826 }
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500827
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800828 err = gc_n64_init_ff(input_dev, idx);
829 if (err) {
830 printk(KERN_WARNING "gamecon.c: Failed to initiate rumble for N64 device %d\n", idx);
831 input_free_device(input_dev);
832 return err;
833 }
Scott Moreau7aa9e0e2010-02-21 20:53:55 -0800834
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800835 break;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500836
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800837 case GC_SNESMOUSE:
838 __set_bit(BTN_LEFT, input_dev->keybit);
839 __set_bit(BTN_RIGHT, input_dev->keybit);
840 __set_bit(REL_X, input_dev->relbit);
841 __set_bit(REL_Y, input_dev->relbit);
842 break;
Raphael Assenatb157d552006-04-02 00:10:05 -0500843
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800844 case GC_SNES:
845 for (i = 4; i < 8; i++)
846 __set_bit(gc_snes_btn[i], input_dev->keybit);
847 case GC_NES:
848 for (i = 0; i < 4; i++)
849 __set_bit(gc_snes_btn[i], input_dev->keybit);
850 break;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500851
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800852 case GC_MULTI2:
853 __set_bit(BTN_THUMB, input_dev->keybit);
854 case GC_MULTI:
855 __set_bit(BTN_TRIGGER, input_dev->keybit);
856 break;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500857
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800858 case GC_PSX:
859 for (i = 0; i < 6; i++)
860 input_set_abs_params(input_dev,
861 gc_psx_abs[i], 4, 252, 0, 2);
862 for (i = 0; i < 12; i++)
863 __set_bit(gc_psx_btn[i], input_dev->keybit);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500864
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800865 break;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500866
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800867 case GC_DDR:
868 for (i = 0; i < 4; i++)
869 __set_bit(gc_psx_ddr_btn[i], input_dev->keybit);
870 for (i = 0; i < 12; i++)
871 __set_bit(gc_psx_btn[i], input_dev->keybit);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500872
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800873 break;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500874 }
875
876 return 0;
877}
878
879static struct gc __init *gc_probe(int parport, int *pads, int n_pads)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
881 struct gc *gc;
882 struct parport *pp;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500883 struct pardevice *pd;
884 int i;
885 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500887 pp = parport_find_number(parport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 if (!pp) {
889 printk(KERN_ERR "gamecon.c: no such parport\n");
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500890 err = -EINVAL;
891 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
893
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500894 pd = parport_register_device(pp, "gamecon", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
895 if (!pd) {
896 printk(KERN_ERR "gamecon.c: parport busy already - lp.o loaded?\n");
897 err = -EBUSY;
898 goto err_put_pp;
899 }
900
901 gc = kzalloc(sizeof(struct gc), GFP_KERNEL);
902 if (!gc) {
903 printk(KERN_ERR "gamecon.c: Not enough memory\n");
904 err = -ENOMEM;
905 goto err_unreg_pardev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500907
Ingo Molnar72ba9f02006-02-19 00:22:30 -0500908 mutex_init(&gc->mutex);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500909 gc->pd = pd;
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800910 setup_timer(&gc->timer, gc_timer, (long) gc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500912 for (i = 0; i < n_pads && i < GC_MAX_DEVICES; i++) {
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500913 if (!pads[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 continue;
915
Dmitry Torokhov10ca4c02006-06-26 01:45:48 -0400916 snprintf(gc->phys[i], sizeof(gc->phys[i]),
917 "%s/input%d", gc->pd->port->name, i);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500918 err = gc_setup_pad(gc, i, pads[i]);
919 if (err)
Dmitry Torokhov77fc46c2006-01-29 21:52:11 -0500920 goto err_unreg_devs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Dmitry Torokhov77fc46c2006-01-29 21:52:11 -0500922 err = input_register_device(gc->dev[i]);
923 if (err)
924 goto err_free_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (!gc->pads[0]) {
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500928 printk(KERN_ERR "gamecon.c: No valid devices specified\n");
929 err = -EINVAL;
930 goto err_free_gc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500933 parport_put_port(pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return gc;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500935
Dmitry Torokhov77fc46c2006-01-29 21:52:11 -0500936 err_free_dev:
937 input_free_device(gc->dev[i]);
938 err_unreg_devs:
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500939 while (--i >= 0)
Dmitry Torokhov77fc46c2006-01-29 21:52:11 -0500940 if (gc->dev[i])
941 input_unregister_device(gc->dev[i]);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500942 err_free_gc:
943 kfree(gc);
944 err_unreg_pardev:
945 parport_unregister_device(pd);
946 err_put_pp:
947 parport_put_port(pp);
948 err_out:
949 return ERR_PTR(err);
950}
951
Dmitry Torokhov77fc46c2006-01-29 21:52:11 -0500952static void gc_remove(struct gc *gc)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500953{
954 int i;
955
956 for (i = 0; i < GC_MAX_DEVICES; i++)
957 if (gc->dev[i])
958 input_unregister_device(gc->dev[i]);
959 parport_unregister_device(gc->pd);
960 kfree(gc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
963static int __init gc_init(void)
964{
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500965 int i;
966 int have_dev = 0;
967 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500969 for (i = 0; i < GC_MAX_PORTS; i++) {
Dmitry Torokhov78167232007-05-03 00:52:51 -0400970 if (gc_cfg[i].nargs == 0 || gc_cfg[i].args[0] < 0)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500971 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Dmitry Torokhov78167232007-05-03 00:52:51 -0400973 if (gc_cfg[i].nargs < 2) {
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500974 printk(KERN_ERR "gamecon.c: at least one device must be specified\n");
975 err = -EINVAL;
976 break;
977 }
978
Dmitry Torokhov78167232007-05-03 00:52:51 -0400979 gc_base[i] = gc_probe(gc_cfg[i].args[0],
980 gc_cfg[i].args + 1, gc_cfg[i].nargs - 1);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500981 if (IS_ERR(gc_base[i])) {
982 err = PTR_ERR(gc_base[i]);
983 break;
984 }
985
986 have_dev = 1;
987 }
988
989 if (err) {
990 while (--i >= 0)
Dmitry Torokhov77fc46c2006-01-29 21:52:11 -0500991 if (gc_base[i])
992 gc_remove(gc_base[i]);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500993 return err;
994 }
995
996 return have_dev ? 0 : -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
999static void __exit gc_exit(void)
1000{
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -05001001 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -05001003 for (i = 0; i < GC_MAX_PORTS; i++)
1004 if (gc_base[i])
1005 gc_remove(gc_base[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
1007
1008module_init(gc_init);
1009module_exit(gc_exit);