blob: 7df2d82f2c83b2bb4c468ea08bed5e51f366a0e0 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
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
40MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
41MODULE_DESCRIPTION("NES, SNES, N64, MultiSystem, PSX gamepad driver");
42MODULE_LICENSE("GPL");
43
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050044#define GC_MAX_PORTS 3
45#define GC_MAX_DEVICES 5
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050047struct gc_config {
48 int args[GC_MAX_DEVICES + 1];
49 int nargs;
50};
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050052static struct gc_config gc[GC_MAX_PORTS] __initdata;
53
54module_param_array_named(map, gc[0].args, int, &gc[0].nargs, 0);
55MODULE_PARM_DESC(map, "Describes first set of devices (<parport#>,<pad1>,<pad2>,..<pad5>)");
56module_param_array_named(map2, gc[1].args, int, &gc[1].nargs, 0);
57MODULE_PARM_DESC(map2, "Describes second set of devices");
58module_param_array_named(map3, gc[2].args, int, &gc[2].nargs, 0);
59MODULE_PARM_DESC(map3, "Describes third set of devices");
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61__obsolete_setup("gc=");
62__obsolete_setup("gc_2=");
63__obsolete_setup("gc_3=");
64
65/* see also gs_psx_delay parameter in PSX support section */
66
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
75
76#define GC_MAX 8
77
78#define GC_REFRESH_TIME HZ/100
79
80struct gc {
81 struct pardevice *pd;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050082 struct input_dev *dev[GC_MAX_DEVICES];
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 struct timer_list timer;
84 unsigned char pads[GC_MAX + 1];
85 int used;
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -050086 struct semaphore sem;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050087 char phys[GC_MAX_DEVICES][32];
Linus Torvalds1da177e2005-04-16 15:20:36 -070088};
89
90static struct gc *gc_base[3];
91
92static int gc_status_bit[] = { 0x40, 0x80, 0x20, 0x10, 0x08 };
93
94static 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" };
97/*
98 * N64 support.
99 */
100
101static unsigned char gc_n64_bytes[] = { 0, 1, 13, 15, 14, 12, 10, 11, 2, 3 };
102static short gc_n64_btn[] = { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_TRIGGER, BTN_START };
103
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 */
117
118/*
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.
121 */
122
123static void gc_n64_read_packet(struct gc *gc, unsigned char *data)
124{
125 int i;
126 unsigned long flags;
127
128/*
129 * Request the pad to transmit data
130 */
131
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);
136 }
137 local_irq_restore(flags);
138
139/*
140 * Wait for the pad response to be loaded into the 33-bit register of the adapter
141 */
142
143 udelay(GC_N64_DELAY);
144
145/*
146 * Grab data (ignoring the last bit, which is a stop bit)
147 */
148
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);
153 }
154
155/*
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.
158 */
159
160}
161
162/*
163 * NES/SNES support.
164 */
165
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 */
169
170#define GC_NES_POWER 0xfc
171#define GC_NES_CLOCK 0x01
172#define GC_NES_LATCH 0x02
173
174static unsigned char gc_nes_bytes[] = { 0, 1, 2, 3 };
175static unsigned char gc_snes_bytes[] = { 8, 0, 2, 3, 9, 1, 10, 11 };
176static short gc_snes_btn[] = { BTN_A, BTN_B, BTN_SELECT, BTN_START, BTN_X, BTN_Y, BTN_TL, BTN_TR };
177
178/*
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.
182 */
183
184static void gc_nes_read_packet(struct gc *gc, int length, unsigned char *data)
185{
186 int i;
187
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);
191
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);
198 }
199}
200
201/*
202 * Multisystem joystick support
203 */
204
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 */
207
208/*
209 * gc_multi_read_packet() reads a Multisystem joystick packet.
210 */
211
212static void gc_multi_read_packet(struct gc *gc, int length, unsigned char *data)
213{
214 int i;
215
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;
219 }
220}
221
222/*
223 * PSX support
224 *
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/
229 *
230 */
231
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 */
235
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 */
241
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 */
246
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 */
249
250static int gc_psx_delay = GC_PSX_DELAY;
251module_param_named(psx_delay, gc_psx_delay, uint, 0);
252MODULE_PARM_DESC(psx_delay, "Delay when accessing Sony PSX controller (usecs)");
253
254__obsolete_setup("gc_psx_delay=");
255
256static short gc_psx_abs[] = { ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_HAT0X, ABS_HAT0Y };
257static 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 };
259static short gc_psx_ddr_btn[] = { BTN_0, BTN_1, BTN_2, BTN_3 };
260
261/*
262 * gc_psx_command() writes 8bit command and reads 8bit data from
263 * the psx pad.
264 */
265
266static void gc_psx_command(struct gc *gc, int b, unsigned char data[5])
267{
268 int i, j, cmd, read;
269 for (i = 0; i < 5; i++)
270 data[i] = 0;
271
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);
281 }
282}
283
284/*
285 * gc_psx_read_packet() reads a whole psx packet and returns
286 * device identifier code.
287 */
288
289static void gc_psx_read_packet(struct gc *gc, unsigned char data[5][GC_PSX_BYTES], unsigned char id[5])
290{
291 int i, j, max_len = 0;
292 unsigned long flags;
293 unsigned char data2[5];
294
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);
299
300 local_irq_save(flags);
301
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 */
305
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]);
311
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];
316 }
317
318 local_irq_restore(flags);
319
320 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
321
322 for(i = 0; i < 5; i++) /* Set id's to the real value */
323 id[i] = GC_PSX_ID(id[i]);
324}
325
326/*
327 * gc_timer() reads and analyzes console pads data.
328 */
329
330#define GC_MAX_LENGTH GC_N64_LENGTH
331
332static void gc_timer(unsigned long private)
333{
334 struct gc *gc = (void *) private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 unsigned char data[GC_MAX_LENGTH];
336 unsigned char data_psx[5][GC_PSX_BYTES];
337 int i, j, s;
338
339/*
340 * N64 pads - must be read first, any read confuses them for 200 us
341 */
342
343 if (gc->pads[GC_N64]) {
344
345 gc_n64_read_packet(gc, data);
346
347 for (i = 0; i < 5; i++) {
348
349 s = gc_status_bit[i];
350
351 if (s & gc->pads[GC_N64] & ~(data[8] | data[9])) {
352
353 signed char axes[2];
354 axes[0] = axes[1] = 0;
355
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;
359 }
360
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500361 input_report_abs(gc->dev[i], ABS_X, axes[0]);
362 input_report_abs(gc->dev[i], ABS_Y, -axes[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500364 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]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 for (j = 0; j < 10; j++)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500368 input_report_key(gc->dev[i], gc_n64_btn[j], s & data[gc_n64_bytes[j]]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500370 input_sync(gc->dev[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372 }
373 }
374
375/*
376 * NES and SNES pads
377 */
378
379 if (gc->pads[GC_NES] || gc->pads[GC_SNES]) {
380
381 gc_nes_read_packet(gc, gc->pads[GC_SNES] ? GC_SNES_LENGTH : GC_NES_LENGTH, data);
382
383 for (i = 0; i < 5; i++) {
384
385 s = gc_status_bit[i];
386
387 if (s & (gc->pads[GC_NES] | gc->pads[GC_SNES])) {
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500388 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]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391
392 if (s & gc->pads[GC_NES])
393 for (j = 0; j < 4; j++)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500394 input_report_key(gc->dev[i], gc_snes_btn[j], s & data[gc_nes_bytes[j]]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 if (s & gc->pads[GC_SNES])
397 for (j = 0; j < 8; j++)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500398 input_report_key(gc->dev[i], gc_snes_btn[j], s & data[gc_snes_bytes[j]]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500400 input_sync(gc->dev[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402 }
403
404/*
405 * Multi and Multi2 joysticks
406 */
407
408 if (gc->pads[GC_MULTI] || gc->pads[GC_MULTI2]) {
409
410 gc_multi_read_packet(gc, gc->pads[GC_MULTI2] ? GC_MULTI2_LENGTH : GC_MULTI_LENGTH, data);
411
412 for (i = 0; i < 5; i++) {
413
414 s = gc_status_bit[i];
415
416 if (s & (gc->pads[GC_MULTI] | gc->pads[GC_MULTI2])) {
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500417 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]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421
422 if (s & gc->pads[GC_MULTI2])
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500423 input_report_key(gc->dev[i], BTN_THUMB, s & data[5]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500425 input_sync(gc->dev[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
427 }
428
429/*
430 * PSX controllers
431 */
432
433 if (gc->pads[GC_PSX] || gc->pads[GC_DDR]) {
434
435 gc_psx_read_packet(gc, data_psx, data);
436
437 for (i = 0; i < 5; i++) {
Dmitry Torokhovab0c3442005-05-29 02:28:55 -0500438 switch (data[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 case GC_PSX_RUMBLE:
441
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500442 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 case GC_PSX_NEGCON:
446 case GC_PSX_ANALOG:
447
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500448 if (gc->pads[GC_DDR] & gc_status_bit[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 for(j = 0; j < 4; j++)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500450 input_report_key(gc->dev[i], gc_psx_ddr_btn[j], ~data_psx[i][0] & (0x10 << j));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 } else {
452 for (j = 0; j < 4; j++)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500453 input_report_abs(gc->dev[i], gc_psx_abs[j+2], data_psx[i][j + 2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500455 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458
459 for (j = 0; j < 8; j++)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500460 input_report_key(gc->dev[i], gc_psx_btn[j], ~data_psx[i][1] & (1 << j));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500462 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500465 input_sync(gc->dev[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 break;
468
469 case GC_PSX_NORMAL:
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500470 if (gc->pads[GC_DDR] & gc_status_bit[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 for(j = 0; j < 4; j++)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500472 input_report_key(gc->dev[i], gc_psx_ddr_btn[j], ~data_psx[i][0] & (0x10 << j));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 } else {
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500474 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 /* for some reason if the extra axes are left unset they drift */
478 /* for (j = 0; j < 4; j++)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500479 input_report_abs(gc->dev[i], gc_psx_abs[j+2], 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 * This needs to be debugged properly,
481 * maybe fuzz processing needs to be done in input_sync()
482 * --vojtech
483 */
484 }
485
486 for (j = 0; j < 8; j++)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500487 input_report_key(gc->dev[i], gc_psx_btn[j], ~data_psx[i][1] & (1 << j));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500489 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500492 input_sync(gc->dev[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 break;
495
496 case 0: /* not a pad, ignore */
497 break;
498 }
499 }
500 }
501
502 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
503}
504
505static int gc_open(struct input_dev *dev)
506{
507 struct gc *gc = dev->private;
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500508 int err;
509
510 err = down_interruptible(&gc->sem);
511 if (err)
512 return err;
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 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);
518 }
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500519
520 up(&gc->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return 0;
522}
523
524static void gc_close(struct input_dev *dev)
525{
526 struct gc *gc = dev->private;
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500527
528 down(&gc->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (!--gc->used) {
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500530 del_timer_sync(&gc->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 parport_write_control(gc->pd->port, 0x00);
532 parport_release(gc->pd);
533 }
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500534 up(&gc->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500537static int __init gc_setup_pad(struct gc *gc, int idx, int pad_type)
538{
539 struct input_dev *input_dev;
540 int i;
541
542 if (!pad_type)
543 return 0;
544
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;
548 }
549
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;
554 }
555
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;
563
564 input_dev->open = gc_open;
565 input_dev->close = gc_close;
566
567 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
568
569 for (i = 0; i < 2; i++)
570 input_set_abs_params(input_dev, ABS_X + i, -1, 1, 0, 0);
571
572 gc->pads[0] |= gc_status_bit[idx];
573 gc->pads[pad_type] |= gc_status_bit[idx];
574
575 switch (pad_type) {
576
577 case GC_N64:
578 for (i = 0; i < 10; i++)
579 set_bit(gc_n64_btn[i], input_dev->keybit);
580
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);
584 }
585
586 break;
587
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;
595
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;
601
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);
607
608 break;
609
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);
615
616 break;
617 }
618
619 return 0;
620}
621
622static struct gc __init *gc_probe(int parport, int *pads, int n_pads)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
624 struct gc *gc;
625 struct parport *pp;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500626 struct pardevice *pd;
627 int i;
628 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500630 pp = parport_find_number(parport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (!pp) {
632 printk(KERN_ERR "gamecon.c: no such parport\n");
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500633 err = -EINVAL;
634 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500637 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;
642 }
643
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500650
651 init_MUTEX(&gc->sem);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500652 gc->pd = pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 init_timer(&gc->timer);
654 gc->timer.data = (long) gc;
655 gc->timer.function = gc_timer;
656
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500657 for (i = 0; i < n_pads; i++) {
658 if (!pads[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 continue;
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 sprintf(gc->phys[i], "%s/input%d", gc->pd->port->name, i);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500662 err = gc_setup_pad(gc, i, pads[i]);
663 if (err)
664 goto err_free_devs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500666 input_register_device(gc->dev[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (!gc->pads[0]) {
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500670 printk(KERN_ERR "gamecon.c: No valid devices specified\n");
671 err = -EINVAL;
672 goto err_free_gc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
674
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500675 parport_put_port(pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return gc;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500677
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);
689}
690
691static void __exit gc_remove(struct gc *gc)
692{
693 int i;
694
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
702static int __init gc_init(void)
703{
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500704 int i;
705 int have_dev = 0;
706 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500708 for (i = 0; i < GC_MAX_PORTS; i++) {
709 if (gc[i].nargs == 0 || gc[i].args[0] < 0)
710 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500712 if (gc[i].nargs < 2) {
713 printk(KERN_ERR "gamecon.c: at least one device must be specified\n");
714 err = -EINVAL;
715 break;
716 }
717
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;
722 }
723
724 have_dev = 1;
725 }
726
727 if (err) {
728 while (--i >= 0)
729 gc_remove(gc_base[i]);
730 return err;
731 }
732
733 return have_dev ? 0 : -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
736static void __exit gc_exit(void)
737{
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500738 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500740 for (i = 0; i < GC_MAX_PORTS; i++)
741 if (gc_base[i])
742 gc_remove(gc_base[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
745module_init(gc_init);
746module_exit(gc_exit);