blob: bbde4e524da34a540e9bfa8222f487ccd2f01b09 [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
96static char *gc_names[] = { NULL, "SNES pad", "NES pad", "NES FourPort", "Multisystem joystick",
97 "Multisystem 2-button joystick", "N64 controller", "PSX controller",
Raphael Assenatb157d552006-04-02 00:10:05 -050098 "PSX DDR controller", "SNES mouse" };
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/*
100 * N64 support.
101 */
102
103static unsigned char gc_n64_bytes[] = { 0, 1, 13, 15, 14, 12, 10, 11, 2, 3 };
104static short gc_n64_btn[] = { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_TRIGGER, BTN_START };
105
106#define GC_N64_LENGTH 32 /* N64 bit length, not including stop bit */
Scott Moreau7aa9e0e2010-02-21 20:53:55 -0800107#define GC_N64_STOP_LENGTH 5 /* Length of encoded stop bit */
108#define GC_N64_CMD_00 0x11111111UL
109#define GC_N64_CMD_01 0xd1111111UL
110#define GC_N64_CMD_03 0xdd111111UL
111#define GC_N64_CMD_1b 0xdd1dd111UL
112#define GC_N64_CMD_c0 0x111111ddUL
113#define GC_N64_CMD_80 0x1111111dUL
114#define GC_N64_STOP_BIT 0x1d /* Encoded stop bit */
115#define GC_N64_REQUEST_DATA GC_N64_CMD_01 /* the request data command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#define GC_N64_DELAY 133 /* delay between transmit request, and response ready (us) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#define GC_N64_DWS 3 /* delay between write segments (required for sound playback because of ISA DMA) */
118 /* GC_N64_DWS > 24 is known to fail */
119#define GC_N64_POWER_W 0xe2 /* power during write (transmit request) */
120#define GC_N64_POWER_R 0xfd /* power during read */
121#define GC_N64_OUT 0x1d /* output bits to the 4 pads */
122 /* Reading the main axes of any N64 pad is known to fail if the corresponding bit */
123 /* in GC_N64_OUT is pulled low on the output port (by any routine) for more */
124 /* than 123 us */
125#define GC_N64_CLOCK 0x02 /* clock bits for read */
126
127/*
Scott Moreau7aa9e0e2010-02-21 20:53:55 -0800128 * Used for rumble code.
129 */
130
131/* Send encoded command */
132static void gc_n64_send_command(struct gc *gc, unsigned long cmd,
133 unsigned char target)
134{
135 struct parport *port = gc->pd->port;
136 int i;
137
138 for (i = 0; i < GC_N64_LENGTH; i++) {
139 unsigned char data = (cmd >> i) & 1 ? target : 0;
140 parport_write_data(port, GC_N64_POWER_W | data);
141 udelay(GC_N64_DWS);
142 }
143}
144
145/* Send stop bit */
146static void gc_n64_send_stop_bit(struct gc *gc, unsigned char target)
147{
148 struct parport *port = gc->pd->port;
149 int i;
150
151 for (i = 0; i < GC_N64_STOP_LENGTH; i++) {
152 unsigned char data = (GC_N64_STOP_BIT >> i) & 1 ? target : 0;
153 parport_write_data(port, GC_N64_POWER_W | data);
154 udelay(GC_N64_DWS);
155 }
156}
157
158/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 * gc_n64_read_packet() reads an N64 packet.
160 * Each pad uses one bit per byte. So all pads connected to this port are read in parallel.
161 */
162
163static void gc_n64_read_packet(struct gc *gc, unsigned char *data)
164{
165 int i;
166 unsigned long flags;
167
168/*
169 * Request the pad to transmit data
170 */
171
172 local_irq_save(flags);
Scott Moreau7aa9e0e2010-02-21 20:53:55 -0800173 gc_n64_send_command(gc, GC_N64_REQUEST_DATA, GC_N64_OUT);
174 gc_n64_send_stop_bit(gc, GC_N64_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 local_irq_restore(flags);
176
177/*
178 * Wait for the pad response to be loaded into the 33-bit register of the adapter
179 */
180
181 udelay(GC_N64_DELAY);
182
183/*
184 * Grab data (ignoring the last bit, which is a stop bit)
185 */
186
187 for (i = 0; i < GC_N64_LENGTH; i++) {
188 parport_write_data(gc->pd->port, GC_N64_POWER_R);
Scott Moreau7aa9e0e2010-02-21 20:53:55 -0800189 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 data[i] = parport_read_status(gc->pd->port);
191 parport_write_data(gc->pd->port, GC_N64_POWER_R | GC_N64_CLOCK);
192 }
193
194/*
195 * We must wait 200 ms here for the controller to reinitialize before the next read request.
196 * No worries as long as gc_read is polled less frequently than this.
197 */
198
199}
200
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500201static void gc_n64_process_packet(struct gc *gc)
202{
203 unsigned char data[GC_N64_LENGTH];
204 signed char axes[2];
205 struct input_dev *dev;
206 int i, j, s;
207
208 gc_n64_read_packet(gc, data);
209
210 for (i = 0; i < GC_MAX_DEVICES; i++) {
211
212 dev = gc->dev[i];
213 if (!dev)
214 continue;
215
216 s = gc_status_bit[i];
217
218 if (s & gc->pads[GC_N64] & ~(data[8] | data[9])) {
219
220 axes[0] = axes[1] = 0;
221
222 for (j = 0; j < 8; j++) {
223 if (data[23 - j] & s)
224 axes[0] |= 1 << j;
225 if (data[31 - j] & s)
226 axes[1] |= 1 << j;
227 }
228
229 input_report_abs(dev, ABS_X, axes[0]);
230 input_report_abs(dev, ABS_Y, -axes[1]);
231
232 input_report_abs(dev, ABS_HAT0X, !(s & data[6]) - !(s & data[7]));
233 input_report_abs(dev, ABS_HAT0Y, !(s & data[4]) - !(s & data[5]));
234
235 for (j = 0; j < 10; j++)
236 input_report_key(dev, gc_n64_btn[j], s & data[gc_n64_bytes[j]]);
237
238 input_sync(dev);
239 }
240 }
241}
242
Scott Moreau7aa9e0e2010-02-21 20:53:55 -0800243static int gc_n64_play_effect(struct input_dev *dev, void *data,
244 struct ff_effect *effect)
245{
246 int i;
247 unsigned long flags;
248 struct gc *gc = input_get_drvdata(dev);
249 struct gc_subdev *sdev = data;
250 unsigned char target = 1 << sdev->idx; /* select desired pin */
251
252 if (effect->type == FF_RUMBLE) {
253 struct ff_rumble_effect *rumble = &effect->u.rumble;
254 unsigned int cmd =
255 rumble->strong_magnitude || rumble->weak_magnitude ?
256 GC_N64_CMD_01 : GC_N64_CMD_00;
257
258 local_irq_save(flags);
259
260 /* Init Rumble - 0x03, 0x80, 0x01, (34)0x80 */
261 gc_n64_send_command(gc, GC_N64_CMD_03, target);
262 gc_n64_send_command(gc, GC_N64_CMD_80, target);
263 gc_n64_send_command(gc, GC_N64_CMD_01, target);
264 for (i = 0; i < 32; i++)
265 gc_n64_send_command(gc, GC_N64_CMD_80, target);
266 gc_n64_send_stop_bit(gc, target);
267
268 udelay(GC_N64_DELAY);
269
270 /* Now start or stop it - 0x03, 0xc0, 0zx1b, (32)0x01/0x00 */
271 gc_n64_send_command(gc, GC_N64_CMD_03, target);
272 gc_n64_send_command(gc, GC_N64_CMD_c0, target);
273 gc_n64_send_command(gc, GC_N64_CMD_1b, target);
274 for (i = 0; i < 32; i++)
275 gc_n64_send_command(gc, cmd, target);
276 gc_n64_send_stop_bit(gc, target);
277
278 local_irq_restore(flags);
279
280 }
281
282 return 0;
283}
284
285static int __init gc_n64_init_ff(struct input_dev *dev, int i)
286{
287 struct gc_subdev *sdev;
288 int err;
289
290 sdev = kmalloc(sizeof(*sdev), GFP_KERNEL);
291 if (!sdev)
292 return -ENOMEM;
293
294 sdev->idx = i;
295
296 input_set_capability(dev, EV_FF, FF_RUMBLE);
297
298 err = input_ff_create_memless(dev, sdev, gc_n64_play_effect);
299 if (err) {
300 kfree(sdev);
301 return err;
302 }
303
304 return 0;
305}
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307/*
308 * NES/SNES support.
309 */
310
Raphael Assenatb157d552006-04-02 00:10:05 -0500311#define GC_NES_DELAY 6 /* Delay between bits - 6us */
312#define GC_NES_LENGTH 8 /* The NES pads use 8 bits of data */
313#define GC_SNES_LENGTH 12 /* The SNES true length is 16, but the
314 last 4 bits are unused */
315#define GC_SNESMOUSE_LENGTH 32 /* The SNES mouse uses 32 bits, the first
316 16 bits are equivalent to a gamepad */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318#define GC_NES_POWER 0xfc
319#define GC_NES_CLOCK 0x01
320#define GC_NES_LATCH 0x02
321
322static unsigned char gc_nes_bytes[] = { 0, 1, 2, 3 };
323static unsigned char gc_snes_bytes[] = { 8, 0, 2, 3, 9, 1, 10, 11 };
324static short gc_snes_btn[] = { BTN_A, BTN_B, BTN_SELECT, BTN_START, BTN_X, BTN_Y, BTN_TL, BTN_TR };
325
326/*
327 * gc_nes_read_packet() reads a NES/SNES packet.
328 * Each pad uses one bit per byte. So all pads connected to
329 * this port are read in parallel.
330 */
331
332static void gc_nes_read_packet(struct gc *gc, int length, unsigned char *data)
333{
334 int i;
335
336 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK | GC_NES_LATCH);
337 udelay(GC_NES_DELAY * 2);
338 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
339
340 for (i = 0; i < length; i++) {
341 udelay(GC_NES_DELAY);
342 parport_write_data(gc->pd->port, GC_NES_POWER);
343 data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
344 udelay(GC_NES_DELAY);
345 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
346 }
347}
348
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500349static void gc_nes_process_packet(struct gc *gc)
350{
Raphael Assenatb157d552006-04-02 00:10:05 -0500351 unsigned char data[GC_SNESMOUSE_LENGTH];
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500352 struct input_dev *dev;
Raphael Assenatb157d552006-04-02 00:10:05 -0500353 int i, j, s, len;
354 char x_rel, y_rel;
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500355
Raphael Assenatb157d552006-04-02 00:10:05 -0500356 len = gc->pads[GC_SNESMOUSE] ? GC_SNESMOUSE_LENGTH :
357 (gc->pads[GC_SNES] ? GC_SNES_LENGTH : GC_NES_LENGTH);
358
359 gc_nes_read_packet(gc, len, data);
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500360
361 for (i = 0; i < GC_MAX_DEVICES; i++) {
362
363 dev = gc->dev[i];
364 if (!dev)
365 continue;
366
367 s = gc_status_bit[i];
368
369 if (s & (gc->pads[GC_NES] | gc->pads[GC_SNES])) {
370 input_report_abs(dev, ABS_X, !(s & data[6]) - !(s & data[7]));
371 input_report_abs(dev, ABS_Y, !(s & data[4]) - !(s & data[5]));
372 }
373
374 if (s & gc->pads[GC_NES])
375 for (j = 0; j < 4; j++)
376 input_report_key(dev, gc_snes_btn[j], s & data[gc_nes_bytes[j]]);
377
378 if (s & gc->pads[GC_SNES])
379 for (j = 0; j < 8; j++)
380 input_report_key(dev, gc_snes_btn[j], s & data[gc_snes_bytes[j]]);
381
Raphael Assenatb157d552006-04-02 00:10:05 -0500382 if (s & gc->pads[GC_SNESMOUSE]) {
383 /*
384 * The 4 unused bits from SNES controllers appear to be ID bits
385 * so use them to make sure iwe are dealing with a mouse.
386 * gamepad is connected. This is important since
387 * my SNES gamepad sends 1's for bits 16-31, which
388 * cause the mouse pointer to quickly move to the
389 * upper left corner of the screen.
390 */
391 if (!(s & data[12]) && !(s & data[13]) &&
392 !(s & data[14]) && (s & data[15])) {
393 input_report_key(dev, BTN_LEFT, s & data[9]);
394 input_report_key(dev, BTN_RIGHT, s & data[8]);
395
396 x_rel = y_rel = 0;
397 for (j = 0; j < 7; j++) {
398 x_rel <<= 1;
399 if (data[25 + j] & s)
400 x_rel |= 1;
401
402 y_rel <<= 1;
403 if (data[17 + j] & s)
404 y_rel |= 1;
405 }
406
407 if (x_rel) {
408 if (data[24] & s)
409 x_rel = -x_rel;
410 input_report_rel(dev, REL_X, x_rel);
411 }
412
413 if (y_rel) {
414 if (data[16] & s)
415 y_rel = -y_rel;
416 input_report_rel(dev, REL_Y, y_rel);
417 }
418 }
419 }
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500420 input_sync(dev);
421 }
422}
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424/*
425 * Multisystem joystick support
426 */
427
428#define GC_MULTI_LENGTH 5 /* Multi system joystick packet length is 5 */
429#define GC_MULTI2_LENGTH 6 /* One more bit for one more button */
430
431/*
432 * gc_multi_read_packet() reads a Multisystem joystick packet.
433 */
434
435static void gc_multi_read_packet(struct gc *gc, int length, unsigned char *data)
436{
437 int i;
438
439 for (i = 0; i < length; i++) {
440 parport_write_data(gc->pd->port, ~(1 << i));
441 data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
442 }
443}
444
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500445static void gc_multi_process_packet(struct gc *gc)
446{
447 unsigned char data[GC_MULTI2_LENGTH];
448 struct input_dev *dev;
449 int i, s;
450
451 gc_multi_read_packet(gc, gc->pads[GC_MULTI2] ? GC_MULTI2_LENGTH : GC_MULTI_LENGTH, data);
452
453 for (i = 0; i < GC_MAX_DEVICES; i++) {
454
455 dev = gc->dev[i];
456 if (!dev)
457 continue;
458
459 s = gc_status_bit[i];
460
461 if (s & (gc->pads[GC_MULTI] | gc->pads[GC_MULTI2])) {
462 input_report_abs(dev, ABS_X, !(s & data[2]) - !(s & data[3]));
463 input_report_abs(dev, ABS_Y, !(s & data[0]) - !(s & data[1]));
464 input_report_key(dev, BTN_TRIGGER, s & data[4]);
465 }
466
467 if (s & gc->pads[GC_MULTI2])
468 input_report_key(dev, BTN_THUMB, s & data[5]);
469
470 input_sync(dev);
471 }
472}
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474/*
475 * PSX support
476 *
477 * See documentation at:
478 * http://www.dim.com/~mackys/psxmemcard/ps-eng2.txt
479 * http://www.gamesx.com/controldata/psxcont/psxcont.htm
480 * ftp://milano.usal.es/pablo/
481 *
482 */
483
484#define GC_PSX_DELAY 25 /* 25 usec */
485#define GC_PSX_LENGTH 8 /* talk to the controller in bits */
486#define GC_PSX_BYTES 6 /* the maximum number of bytes to read off the controller */
487
488#define GC_PSX_MOUSE 1 /* Mouse */
489#define GC_PSX_NEGCON 2 /* NegCon */
490#define GC_PSX_NORMAL 4 /* Digital / Analog or Rumble in Digital mode */
491#define GC_PSX_ANALOG 5 /* Analog in Analog mode / Rumble in Green mode */
492#define GC_PSX_RUMBLE 7 /* Rumble in Red mode */
493
494#define GC_PSX_CLOCK 0x04 /* Pin 4 */
495#define GC_PSX_COMMAND 0x01 /* Pin 2 */
496#define GC_PSX_POWER 0xf8 /* Pins 5-9 */
497#define GC_PSX_SELECT 0x02 /* Pin 3 */
498
499#define GC_PSX_ID(x) ((x) >> 4) /* High nibble is device type */
500#define GC_PSX_LEN(x) (((x) & 0xf) << 1) /* Low nibble is length in bytes/2 */
501
502static int gc_psx_delay = GC_PSX_DELAY;
503module_param_named(psx_delay, gc_psx_delay, uint, 0);
504MODULE_PARM_DESC(psx_delay, "Delay when accessing Sony PSX controller (usecs)");
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506static short gc_psx_abs[] = { ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_HAT0X, ABS_HAT0Y };
507static short gc_psx_btn[] = { BTN_TL, BTN_TR, BTN_TL2, BTN_TR2, BTN_A, BTN_B, BTN_X, BTN_Y,
508 BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR };
509static short gc_psx_ddr_btn[] = { BTN_0, BTN_1, BTN_2, BTN_3 };
510
511/*
512 * gc_psx_command() writes 8bit command and reads 8bit data from
513 * the psx pad.
514 */
515
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500516static void gc_psx_command(struct gc *gc, int b, unsigned char data[GC_MAX_DEVICES])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 int i, j, cmd, read;
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500519
520 for (i = 0; i < GC_MAX_DEVICES; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 data[i] = 0;
522
523 for (i = 0; i < GC_PSX_LENGTH; i++, b >>= 1) {
524 cmd = (b & 1) ? GC_PSX_COMMAND : 0;
525 parport_write_data(gc->pd->port, cmd | GC_PSX_POWER);
526 udelay(gc_psx_delay);
527 read = parport_read_status(gc->pd->port) ^ 0x80;
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500528 for (j = 0; j < GC_MAX_DEVICES; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 data[j] |= (read & gc_status_bit[j] & (gc->pads[GC_PSX] | gc->pads[GC_DDR])) ? (1 << i) : 0;
530 parport_write_data(gc->pd->port, cmd | GC_PSX_CLOCK | GC_PSX_POWER);
531 udelay(gc_psx_delay);
532 }
533}
534
535/*
536 * gc_psx_read_packet() reads a whole psx packet and returns
537 * device identifier code.
538 */
539
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500540static void gc_psx_read_packet(struct gc *gc, unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES],
541 unsigned char id[GC_MAX_DEVICES])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 int i, j, max_len = 0;
544 unsigned long flags;
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500545 unsigned char data2[GC_MAX_DEVICES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER); /* Select pad */
548 udelay(gc_psx_delay);
549 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_POWER); /* Deselect, begin command */
550 udelay(gc_psx_delay);
551
552 local_irq_save(flags);
553
554 gc_psx_command(gc, 0x01, data2); /* Access pad */
555 gc_psx_command(gc, 0x42, id); /* Get device ids */
556 gc_psx_command(gc, 0, data2); /* Dump status */
557
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500558 for (i =0; i < GC_MAX_DEVICES; i++) /* Find the longest pad */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if((gc_status_bit[i] & (gc->pads[GC_PSX] | gc->pads[GC_DDR]))
560 && (GC_PSX_LEN(id[i]) > max_len)
561 && (GC_PSX_LEN(id[i]) <= GC_PSX_BYTES))
562 max_len = GC_PSX_LEN(id[i]);
563
564 for (i = 0; i < max_len; i++) { /* Read in all the data */
565 gc_psx_command(gc, 0, data2);
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500566 for (j = 0; j < GC_MAX_DEVICES; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 data[j][i] = data2[j];
568 }
569
570 local_irq_restore(flags);
571
572 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
573
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500574 for(i = 0; i < GC_MAX_DEVICES; i++) /* Set id's to the real value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 id[i] = GC_PSX_ID(id[i]);
576}
577
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500578static void gc_psx_process_packet(struct gc *gc)
579{
580 unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES];
581 unsigned char id[GC_MAX_DEVICES];
582 struct input_dev *dev;
583 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500585 gc_psx_read_packet(gc, data, id);
586
587 for (i = 0; i < GC_MAX_DEVICES; i++) {
588
589 dev = gc->dev[i];
590 if (!dev)
591 continue;
592
593 switch (id[i]) {
594
595 case GC_PSX_RUMBLE:
596
597 input_report_key(dev, BTN_THUMBL, ~data[i][0] & 0x04);
598 input_report_key(dev, BTN_THUMBR, ~data[i][0] & 0x02);
599
600 case GC_PSX_NEGCON:
601 case GC_PSX_ANALOG:
602
603 if (gc->pads[GC_DDR] & gc_status_bit[i]) {
604 for(j = 0; j < 4; j++)
605 input_report_key(dev, gc_psx_ddr_btn[j], ~data[i][0] & (0x10 << j));
606 } else {
607 for (j = 0; j < 4; j++)
608 input_report_abs(dev, gc_psx_abs[j + 2], data[i][j + 2]);
609
610 input_report_abs(dev, ABS_X, 128 + !(data[i][0] & 0x20) * 127 - !(data[i][0] & 0x80) * 128);
611 input_report_abs(dev, ABS_Y, 128 + !(data[i][0] & 0x40) * 127 - !(data[i][0] & 0x10) * 128);
612 }
613
614 for (j = 0; j < 8; j++)
615 input_report_key(dev, gc_psx_btn[j], ~data[i][1] & (1 << j));
616
617 input_report_key(dev, BTN_START, ~data[i][0] & 0x08);
618 input_report_key(dev, BTN_SELECT, ~data[i][0] & 0x01);
619
620 input_sync(dev);
621
622 break;
623
624 case GC_PSX_NORMAL:
625 if (gc->pads[GC_DDR] & gc_status_bit[i]) {
626 for(j = 0; j < 4; j++)
627 input_report_key(dev, gc_psx_ddr_btn[j], ~data[i][0] & (0x10 << j));
628 } else {
629 input_report_abs(dev, ABS_X, 128 + !(data[i][0] & 0x20) * 127 - !(data[i][0] & 0x80) * 128);
630 input_report_abs(dev, ABS_Y, 128 + !(data[i][0] & 0x40) * 127 - !(data[i][0] & 0x10) * 128);
631
632 /* for some reason if the extra axes are left unset they drift */
633 /* for (j = 0; j < 4; j++)
634 input_report_abs(dev, gc_psx_abs[j + 2], 128);
635 * This needs to be debugged properly,
636 * maybe fuzz processing needs to be done in input_sync()
637 * --vojtech
638 */
639 }
640
641 for (j = 0; j < 8; j++)
642 input_report_key(dev, gc_psx_btn[j], ~data[i][1] & (1 << j));
643
644 input_report_key(dev, BTN_START, ~data[i][0] & 0x08);
645 input_report_key(dev, BTN_SELECT, ~data[i][0] & 0x01);
646
647 input_sync(dev);
648
649 break;
650
651 case 0: /* not a pad, ignore */
652 break;
653 }
654 }
655}
656
657/*
658 * gc_timer() initiates reads of console pads data.
659 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661static void gc_timer(unsigned long private)
662{
663 struct gc *gc = (void *) private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665/*
666 * N64 pads - must be read first, any read confuses them for 200 us
667 */
668
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500669 if (gc->pads[GC_N64])
670 gc_n64_process_packet(gc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672/*
Raphael Assenatb157d552006-04-02 00:10:05 -0500673 * NES and SNES pads or mouse
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 */
675
Raphael Assenatb157d552006-04-02 00:10:05 -0500676 if (gc->pads[GC_NES] || gc->pads[GC_SNES] || gc->pads[GC_SNESMOUSE])
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500677 gc_nes_process_packet(gc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679/*
680 * Multi and Multi2 joysticks
681 */
682
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500683 if (gc->pads[GC_MULTI] || gc->pads[GC_MULTI2])
684 gc_multi_process_packet(gc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686/*
687 * PSX controllers
688 */
689
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500690 if (gc->pads[GC_PSX] || gc->pads[GC_DDR])
691 gc_psx_process_packet(gc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
694}
695
696static int gc_open(struct input_dev *dev)
697{
Dmitry Torokhov8715c1c2007-04-12 01:34:14 -0400698 struct gc *gc = input_get_drvdata(dev);
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500699 int err;
700
Ingo Molnar72ba9f02006-02-19 00:22:30 -0500701 err = mutex_lock_interruptible(&gc->mutex);
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500702 if (err)
703 return err;
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (!gc->used++) {
706 parport_claim(gc->pd);
707 parport_write_control(gc->pd->port, 0x04);
708 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
709 }
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500710
Ingo Molnar72ba9f02006-02-19 00:22:30 -0500711 mutex_unlock(&gc->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return 0;
713}
714
715static void gc_close(struct input_dev *dev)
716{
Dmitry Torokhov8715c1c2007-04-12 01:34:14 -0400717 struct gc *gc = input_get_drvdata(dev);
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500718
Ingo Molnar72ba9f02006-02-19 00:22:30 -0500719 mutex_lock(&gc->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (!--gc->used) {
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500721 del_timer_sync(&gc->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 parport_write_control(gc->pd->port, 0x00);
723 parport_release(gc->pd);
724 }
Ingo Molnar72ba9f02006-02-19 00:22:30 -0500725 mutex_unlock(&gc->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500728static int __init gc_setup_pad(struct gc *gc, int idx, int pad_type)
729{
730 struct input_dev *input_dev;
731 int i;
Scott Moreau7aa9e0e2010-02-21 20:53:55 -0800732 int err;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500733
734 if (!pad_type)
735 return 0;
736
737 if (pad_type < 1 || pad_type > GC_MAX) {
738 printk(KERN_WARNING "gamecon.c: Pad type %d unknown\n", pad_type);
739 return -EINVAL;
740 }
741
742 gc->dev[idx] = input_dev = input_allocate_device();
743 if (!input_dev) {
744 printk(KERN_ERR "gamecon.c: Not enough memory for input device\n");
745 return -ENOMEM;
746 }
747
748 input_dev->name = gc_names[pad_type];
749 input_dev->phys = gc->phys[idx];
750 input_dev->id.bustype = BUS_PARPORT;
751 input_dev->id.vendor = 0x0001;
752 input_dev->id.product = pad_type;
753 input_dev->id.version = 0x0100;
Dmitry Torokhov8715c1c2007-04-12 01:34:14 -0400754
755 input_set_drvdata(input_dev, gc);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500756
757 input_dev->open = gc_open;
758 input_dev->close = gc_close;
759
Raphael Assenatb157d552006-04-02 00:10:05 -0500760 if (pad_type != GC_SNESMOUSE) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700761 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500762
Raphael Assenatb157d552006-04-02 00:10:05 -0500763 for (i = 0; i < 2; i++)
764 input_set_abs_params(input_dev, ABS_X + i, -1, 1, 0, 0);
765 } else
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700766 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500767
768 gc->pads[0] |= gc_status_bit[idx];
769 gc->pads[pad_type] |= gc_status_bit[idx];
770
771 switch (pad_type) {
772
773 case GC_N64:
774 for (i = 0; i < 10; i++)
775 set_bit(gc_n64_btn[i], input_dev->keybit);
776
777 for (i = 0; i < 2; i++) {
778 input_set_abs_params(input_dev, ABS_X + i, -127, 126, 0, 2);
779 input_set_abs_params(input_dev, ABS_HAT0X + i, -1, 1, 0, 0);
780 }
781
Scott Moreau7aa9e0e2010-02-21 20:53:55 -0800782 err = gc_n64_init_ff(input_dev, idx);
783 if (err) {
784 printk(KERN_WARNING "gamecon.c: Failed to initiate rumble for N64 device %d\n", idx);
785 input_free_device(input_dev);
786 return err;
787 }
788
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500789 break;
790
Raphael Assenatb157d552006-04-02 00:10:05 -0500791 case GC_SNESMOUSE:
792 set_bit(BTN_LEFT, input_dev->keybit);
793 set_bit(BTN_RIGHT, input_dev->keybit);
794 set_bit(REL_X, input_dev->relbit);
795 set_bit(REL_Y, input_dev->relbit);
796 break;
797
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500798 case GC_SNES:
799 for (i = 4; i < 8; i++)
800 set_bit(gc_snes_btn[i], input_dev->keybit);
801 case GC_NES:
802 for (i = 0; i < 4; i++)
803 set_bit(gc_snes_btn[i], input_dev->keybit);
804 break;
805
806 case GC_MULTI2:
807 set_bit(BTN_THUMB, input_dev->keybit);
808 case GC_MULTI:
809 set_bit(BTN_TRIGGER, input_dev->keybit);
810 break;
811
812 case GC_PSX:
813 for (i = 0; i < 6; i++)
814 input_set_abs_params(input_dev, gc_psx_abs[i], 4, 252, 0, 2);
815 for (i = 0; i < 12; i++)
816 set_bit(gc_psx_btn[i], input_dev->keybit);
817
818 break;
819
820 case GC_DDR:
821 for (i = 0; i < 4; i++)
822 set_bit(gc_psx_ddr_btn[i], input_dev->keybit);
823 for (i = 0; i < 12; i++)
824 set_bit(gc_psx_btn[i], input_dev->keybit);
825
826 break;
827 }
828
829 return 0;
830}
831
832static struct gc __init *gc_probe(int parport, int *pads, int n_pads)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
834 struct gc *gc;
835 struct parport *pp;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500836 struct pardevice *pd;
837 int i;
838 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500840 pp = parport_find_number(parport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (!pp) {
842 printk(KERN_ERR "gamecon.c: no such parport\n");
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500843 err = -EINVAL;
844 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
846
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500847 pd = parport_register_device(pp, "gamecon", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
848 if (!pd) {
849 printk(KERN_ERR "gamecon.c: parport busy already - lp.o loaded?\n");
850 err = -EBUSY;
851 goto err_put_pp;
852 }
853
854 gc = kzalloc(sizeof(struct gc), GFP_KERNEL);
855 if (!gc) {
856 printk(KERN_ERR "gamecon.c: Not enough memory\n");
857 err = -ENOMEM;
858 goto err_unreg_pardev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500860
Ingo Molnar72ba9f02006-02-19 00:22:30 -0500861 mutex_init(&gc->mutex);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500862 gc->pd = pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 init_timer(&gc->timer);
864 gc->timer.data = (long) gc;
865 gc->timer.function = gc_timer;
866
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500867 for (i = 0; i < n_pads && i < GC_MAX_DEVICES; i++) {
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500868 if (!pads[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 continue;
870
Dmitry Torokhov10ca4c02006-06-26 01:45:48 -0400871 snprintf(gc->phys[i], sizeof(gc->phys[i]),
872 "%s/input%d", gc->pd->port->name, i);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500873 err = gc_setup_pad(gc, i, pads[i]);
874 if (err)
Dmitry Torokhov77fc46c2006-01-29 21:52:11 -0500875 goto err_unreg_devs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Dmitry Torokhov77fc46c2006-01-29 21:52:11 -0500877 err = input_register_device(gc->dev[i]);
878 if (err)
879 goto err_free_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (!gc->pads[0]) {
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500883 printk(KERN_ERR "gamecon.c: No valid devices specified\n");
884 err = -EINVAL;
885 goto err_free_gc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
887
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500888 parport_put_port(pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return gc;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500890
Dmitry Torokhov77fc46c2006-01-29 21:52:11 -0500891 err_free_dev:
892 input_free_device(gc->dev[i]);
893 err_unreg_devs:
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500894 while (--i >= 0)
Dmitry Torokhov77fc46c2006-01-29 21:52:11 -0500895 if (gc->dev[i])
896 input_unregister_device(gc->dev[i]);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500897 err_free_gc:
898 kfree(gc);
899 err_unreg_pardev:
900 parport_unregister_device(pd);
901 err_put_pp:
902 parport_put_port(pp);
903 err_out:
904 return ERR_PTR(err);
905}
906
Dmitry Torokhov77fc46c2006-01-29 21:52:11 -0500907static void gc_remove(struct gc *gc)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500908{
909 int i;
910
911 for (i = 0; i < GC_MAX_DEVICES; i++)
912 if (gc->dev[i])
913 input_unregister_device(gc->dev[i]);
914 parport_unregister_device(gc->pd);
915 kfree(gc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
918static int __init gc_init(void)
919{
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500920 int i;
921 int have_dev = 0;
922 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500924 for (i = 0; i < GC_MAX_PORTS; i++) {
Dmitry Torokhov78167232007-05-03 00:52:51 -0400925 if (gc_cfg[i].nargs == 0 || gc_cfg[i].args[0] < 0)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500926 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Dmitry Torokhov78167232007-05-03 00:52:51 -0400928 if (gc_cfg[i].nargs < 2) {
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500929 printk(KERN_ERR "gamecon.c: at least one device must be specified\n");
930 err = -EINVAL;
931 break;
932 }
933
Dmitry Torokhov78167232007-05-03 00:52:51 -0400934 gc_base[i] = gc_probe(gc_cfg[i].args[0],
935 gc_cfg[i].args + 1, gc_cfg[i].nargs - 1);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500936 if (IS_ERR(gc_base[i])) {
937 err = PTR_ERR(gc_base[i]);
938 break;
939 }
940
941 have_dev = 1;
942 }
943
944 if (err) {
945 while (--i >= 0)
Dmitry Torokhov77fc46c2006-01-29 21:52:11 -0500946 if (gc_base[i])
947 gc_remove(gc_base[i]);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500948 return err;
949 }
950
951 return have_dev ? 0 : -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
954static void __exit gc_exit(void)
955{
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500956 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500958 for (i = 0; i < GC_MAX_PORTS; i++)
959 if (gc_base[i])
960 gc_remove(gc_base[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
963module_init(gc_init);
964module_exit(gc_exit);