]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/input/joystick/gamecon.c
Merge tag 'v4.10-rc5' into for-linus
[mirror_ubuntu-zesty-kernel.git] / drivers / input / joystick / gamecon.c
CommitLineData
1da177e4
LT
1/*
2 * NES, SNES, N64, MultiSystem, PSX gamepad driver for Linux
3 *
ab0c3443
DT
4 * Copyright (c) 1999-2004 Vojtech Pavlik <vojtech@suse.cz>
5 * Copyright (c) 2004 Peter Nelson <rufus-kernel@hackish.org>
1da177e4
LT
6 *
7 * Based on the work of:
ab0c3443
DT
8 * Andree Borrmann John Dahlstrom
9 * David Kuder Nathan Hand
b157d55e 10 * Raphael Assenat
1da177e4
LT
11 */
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
a1e12747
DT
33#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34
1da177e4
LT
35#include <linux/kernel.h>
36#include <linux/delay.h>
37#include <linux/module.h>
1da177e4
LT
38#include <linux/init.h>
39#include <linux/parport.h>
40#include <linux/input.h>
72ba9f0c 41#include <linux/mutex.h>
5a0e3ad6 42#include <linux/slab.h>
1da177e4
LT
43
44MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
45MODULE_DESCRIPTION("NES, SNES, N64, MultiSystem, PSX gamepad driver");
46MODULE_LICENSE("GPL");
47
17dd3f0f
DT
48#define GC_MAX_PORTS 3
49#define GC_MAX_DEVICES 5
1da177e4 50
17dd3f0f
DT
51struct gc_config {
52 int args[GC_MAX_DEVICES + 1];
78167236 53 unsigned int nargs;
17dd3f0f
DT
54};
55
a517e87c 56static struct gc_config gc_cfg[GC_MAX_PORTS];
1da177e4 57
78167236 58module_param_array_named(map, gc_cfg[0].args, int, &gc_cfg[0].nargs, 0);
17dd3f0f 59MODULE_PARM_DESC(map, "Describes first set of devices (<parport#>,<pad1>,<pad2>,..<pad5>)");
78167236 60module_param_array_named(map2, gc_cfg[1].args, int, &gc_cfg[1].nargs, 0);
17dd3f0f 61MODULE_PARM_DESC(map2, "Describes second set of devices");
78167236 62module_param_array_named(map3, gc_cfg[2].args, int, &gc_cfg[2].nargs, 0);
17dd3f0f 63MODULE_PARM_DESC(map3, "Describes third set of devices");
1da177e4 64
1da177e4
LT
65/* see also gs_psx_delay parameter in PSX support section */
66
0995174d
DT
67enum gc_type {
68 GC_NONE = 0,
69 GC_SNES,
70 GC_NES,
71 GC_NES4,
72 GC_MULTI,
73 GC_MULTI2,
74 GC_N64,
75 GC_PSX,
76 GC_DDR,
77 GC_SNESMOUSE,
78 GC_MAX
79};
1da177e4
LT
80
81#define GC_REFRESH_TIME HZ/100
82
0995174d
DT
83struct gc_pad {
84 struct input_dev *dev;
85 enum gc_type type;
86 char phys[32];
87};
88
1da177e4
LT
89struct gc {
90 struct pardevice *pd;
0995174d 91 struct gc_pad pads[GC_MAX_DEVICES];
1da177e4 92 struct timer_list timer;
0995174d 93 int pad_count[GC_MAX];
1da177e4 94 int used;
a517e87c 95 int parportno;
72ba9f0c 96 struct mutex mutex;
1da177e4
LT
97};
98
7aa9e0e8
SM
99struct gc_subdev {
100 unsigned int idx;
101};
102
1da177e4
LT
103static struct gc *gc_base[3];
104
af930d64 105static const int gc_status_bit[] = { 0x40, 0x80, 0x20, 0x10, 0x08 };
1da177e4 106
af930d64 107static const char *gc_names[] = {
d38fcb96
DT
108 NULL, "SNES pad", "NES pad", "NES FourPort", "Multisystem joystick",
109 "Multisystem 2-button joystick", "N64 controller", "PSX controller",
110 "PSX DDR controller", "SNES mouse"
111};
112
1da177e4
LT
113/*
114 * N64 support.
115 */
116
af930d64
DT
117static const unsigned char gc_n64_bytes[] = { 0, 1, 13, 15, 14, 12, 10, 11, 2, 3 };
118static const short gc_n64_btn[] = {
d38fcb96
DT
119 BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z,
120 BTN_TL, BTN_TR, BTN_TRIGGER, BTN_START
121};
1da177e4
LT
122
123#define GC_N64_LENGTH 32 /* N64 bit length, not including stop bit */
7aa9e0e8
SM
124#define GC_N64_STOP_LENGTH 5 /* Length of encoded stop bit */
125#define GC_N64_CMD_00 0x11111111UL
126#define GC_N64_CMD_01 0xd1111111UL
127#define GC_N64_CMD_03 0xdd111111UL
128#define GC_N64_CMD_1b 0xdd1dd111UL
129#define GC_N64_CMD_c0 0x111111ddUL
130#define GC_N64_CMD_80 0x1111111dUL
131#define GC_N64_STOP_BIT 0x1d /* Encoded stop bit */
132#define GC_N64_REQUEST_DATA GC_N64_CMD_01 /* the request data command */
1da177e4 133#define GC_N64_DELAY 133 /* delay between transmit request, and response ready (us) */
1da177e4
LT
134#define GC_N64_DWS 3 /* delay between write segments (required for sound playback because of ISA DMA) */
135 /* GC_N64_DWS > 24 is known to fail */
136#define GC_N64_POWER_W 0xe2 /* power during write (transmit request) */
137#define GC_N64_POWER_R 0xfd /* power during read */
138#define GC_N64_OUT 0x1d /* output bits to the 4 pads */
139 /* Reading the main axes of any N64 pad is known to fail if the corresponding bit */
140 /* in GC_N64_OUT is pulled low on the output port (by any routine) for more */
141 /* than 123 us */
142#define GC_N64_CLOCK 0x02 /* clock bits for read */
143
7aa9e0e8
SM
144/*
145 * Used for rumble code.
146 */
147
148/* Send encoded command */
149static void gc_n64_send_command(struct gc *gc, unsigned long cmd,
150 unsigned char target)
151{
152 struct parport *port = gc->pd->port;
153 int i;
154
155 for (i = 0; i < GC_N64_LENGTH; i++) {
156 unsigned char data = (cmd >> i) & 1 ? target : 0;
157 parport_write_data(port, GC_N64_POWER_W | data);
158 udelay(GC_N64_DWS);
159 }
160}
161
162/* Send stop bit */
163static void gc_n64_send_stop_bit(struct gc *gc, unsigned char target)
164{
165 struct parport *port = gc->pd->port;
166 int i;
167
168 for (i = 0; i < GC_N64_STOP_LENGTH; i++) {
169 unsigned char data = (GC_N64_STOP_BIT >> i) & 1 ? target : 0;
170 parport_write_data(port, GC_N64_POWER_W | data);
171 udelay(GC_N64_DWS);
172 }
173}
174
1da177e4
LT
175/*
176 * gc_n64_read_packet() reads an N64 packet.
d38fcb96
DT
177 * Each pad uses one bit per byte. So all pads connected to this port
178 * are read in parallel.
1da177e4
LT
179 */
180
181static void gc_n64_read_packet(struct gc *gc, unsigned char *data)
182{
183 int i;
184 unsigned long flags;
185
186/*
187 * Request the pad to transmit data
188 */
189
190 local_irq_save(flags);
7aa9e0e8
SM
191 gc_n64_send_command(gc, GC_N64_REQUEST_DATA, GC_N64_OUT);
192 gc_n64_send_stop_bit(gc, GC_N64_OUT);
1da177e4
LT
193 local_irq_restore(flags);
194
195/*
d38fcb96
DT
196 * Wait for the pad response to be loaded into the 33-bit register
197 * of the adapter.
1da177e4
LT
198 */
199
200 udelay(GC_N64_DELAY);
201
202/*
203 * Grab data (ignoring the last bit, which is a stop bit)
204 */
205
206 for (i = 0; i < GC_N64_LENGTH; i++) {
207 parport_write_data(gc->pd->port, GC_N64_POWER_R);
7aa9e0e8 208 udelay(2);
1da177e4
LT
209 data[i] = parport_read_status(gc->pd->port);
210 parport_write_data(gc->pd->port, GC_N64_POWER_R | GC_N64_CLOCK);
211 }
212
213/*
d38fcb96
DT
214 * We must wait 200 ms here for the controller to reinitialize before
215 * the next read request. No worries as long as gc_read is polled less
216 * frequently than this.
1da177e4
LT
217 */
218
219}
220
c7fd018d
DT
221static void gc_n64_process_packet(struct gc *gc)
222{
223 unsigned char data[GC_N64_LENGTH];
c7fd018d
DT
224 struct input_dev *dev;
225 int i, j, s;
d38fcb96 226 signed char x, y;
c7fd018d
DT
227
228 gc_n64_read_packet(gc, data);
229
230 for (i = 0; i < GC_MAX_DEVICES; i++) {
231
0995174d 232 if (gc->pads[i].type != GC_N64)
c7fd018d
DT
233 continue;
234
0995174d 235 dev = gc->pads[i].dev;
c7fd018d
DT
236 s = gc_status_bit[i];
237
0995174d 238 if (s & ~(data[8] | data[9])) {
c7fd018d 239
d38fcb96 240 x = y = 0;
c7fd018d
DT
241
242 for (j = 0; j < 8; j++) {
243 if (data[23 - j] & s)
d38fcb96 244 x |= 1 << j;
c7fd018d 245 if (data[31 - j] & s)
d38fcb96 246 y |= 1 << j;
c7fd018d
DT
247 }
248
d38fcb96
DT
249 input_report_abs(dev, ABS_X, x);
250 input_report_abs(dev, ABS_Y, -y);
c7fd018d 251
d38fcb96
DT
252 input_report_abs(dev, ABS_HAT0X,
253 !(s & data[6]) - !(s & data[7]));
254 input_report_abs(dev, ABS_HAT0Y,
255 !(s & data[4]) - !(s & data[5]));
c7fd018d
DT
256
257 for (j = 0; j < 10; j++)
d38fcb96
DT
258 input_report_key(dev, gc_n64_btn[j],
259 s & data[gc_n64_bytes[j]]);
c7fd018d
DT
260
261 input_sync(dev);
262 }
263 }
264}
265
7aa9e0e8
SM
266static int gc_n64_play_effect(struct input_dev *dev, void *data,
267 struct ff_effect *effect)
268{
269 int i;
270 unsigned long flags;
271 struct gc *gc = input_get_drvdata(dev);
272 struct gc_subdev *sdev = data;
273 unsigned char target = 1 << sdev->idx; /* select desired pin */
274
275 if (effect->type == FF_RUMBLE) {
276 struct ff_rumble_effect *rumble = &effect->u.rumble;
277 unsigned int cmd =
278 rumble->strong_magnitude || rumble->weak_magnitude ?
279 GC_N64_CMD_01 : GC_N64_CMD_00;
280
281 local_irq_save(flags);
282
283 /* Init Rumble - 0x03, 0x80, 0x01, (34)0x80 */
284 gc_n64_send_command(gc, GC_N64_CMD_03, target);
285 gc_n64_send_command(gc, GC_N64_CMD_80, target);
286 gc_n64_send_command(gc, GC_N64_CMD_01, target);
287 for (i = 0; i < 32; i++)
288 gc_n64_send_command(gc, GC_N64_CMD_80, target);
289 gc_n64_send_stop_bit(gc, target);
290
291 udelay(GC_N64_DELAY);
292
293 /* Now start or stop it - 0x03, 0xc0, 0zx1b, (32)0x01/0x00 */
294 gc_n64_send_command(gc, GC_N64_CMD_03, target);
295 gc_n64_send_command(gc, GC_N64_CMD_c0, target);
296 gc_n64_send_command(gc, GC_N64_CMD_1b, target);
297 for (i = 0; i < 32; i++)
298 gc_n64_send_command(gc, cmd, target);
299 gc_n64_send_stop_bit(gc, target);
300
301 local_irq_restore(flags);
302
303 }
304
305 return 0;
306}
307
a517e87c 308static int gc_n64_init_ff(struct input_dev *dev, int i)
7aa9e0e8
SM
309{
310 struct gc_subdev *sdev;
311 int err;
312
313 sdev = kmalloc(sizeof(*sdev), GFP_KERNEL);
314 if (!sdev)
315 return -ENOMEM;
316
317 sdev->idx = i;
318
319 input_set_capability(dev, EV_FF, FF_RUMBLE);
320
321 err = input_ff_create_memless(dev, sdev, gc_n64_play_effect);
322 if (err) {
323 kfree(sdev);
324 return err;
325 }
326
327 return 0;
328}
329
1da177e4
LT
330/*
331 * NES/SNES support.
332 */
333
b157d55e
RA
334#define GC_NES_DELAY 6 /* Delay between bits - 6us */
335#define GC_NES_LENGTH 8 /* The NES pads use 8 bits of data */
336#define GC_SNES_LENGTH 12 /* The SNES true length is 16, but the
337 last 4 bits are unused */
338#define GC_SNESMOUSE_LENGTH 32 /* The SNES mouse uses 32 bits, the first
339 16 bits are equivalent to a gamepad */
1da177e4
LT
340
341#define GC_NES_POWER 0xfc
342#define GC_NES_CLOCK 0x01
343#define GC_NES_LATCH 0x02
344
af930d64
DT
345static const unsigned char gc_nes_bytes[] = { 0, 1, 2, 3 };
346static const unsigned char gc_snes_bytes[] = { 8, 0, 2, 3, 9, 1, 10, 11 };
347static const short gc_snes_btn[] = {
d38fcb96
DT
348 BTN_A, BTN_B, BTN_SELECT, BTN_START, BTN_X, BTN_Y, BTN_TL, BTN_TR
349};
1da177e4
LT
350
351/*
352 * gc_nes_read_packet() reads a NES/SNES packet.
353 * Each pad uses one bit per byte. So all pads connected to
354 * this port are read in parallel.
355 */
356
357static void gc_nes_read_packet(struct gc *gc, int length, unsigned char *data)
358{
359 int i;
360
361 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK | GC_NES_LATCH);
362 udelay(GC_NES_DELAY * 2);
363 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
364
365 for (i = 0; i < length; i++) {
366 udelay(GC_NES_DELAY);
367 parport_write_data(gc->pd->port, GC_NES_POWER);
368 data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
369 udelay(GC_NES_DELAY);
370 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
371 }
372}
373
c7fd018d
DT
374static void gc_nes_process_packet(struct gc *gc)
375{
b157d55e 376 unsigned char data[GC_SNESMOUSE_LENGTH];
0995174d 377 struct gc_pad *pad;
c7fd018d 378 struct input_dev *dev;
b157d55e
RA
379 int i, j, s, len;
380 char x_rel, y_rel;
381
0995174d
DT
382 len = gc->pad_count[GC_SNESMOUSE] ? GC_SNESMOUSE_LENGTH :
383 (gc->pad_count[GC_SNES] ? GC_SNES_LENGTH : GC_NES_LENGTH);
c7fd018d 384
b157d55e 385 gc_nes_read_packet(gc, len, data);
c7fd018d
DT
386
387 for (i = 0; i < GC_MAX_DEVICES; i++) {
388
0995174d 389 pad = &gc->pads[i];
7b5d3312 390 dev = pad->dev;
c7fd018d
DT
391 s = gc_status_bit[i];
392
0995174d
DT
393 switch (pad->type) {
394
395 case GC_NES:
396
c7fd018d
DT
397 input_report_abs(dev, ABS_X, !(s & data[6]) - !(s & data[7]));
398 input_report_abs(dev, ABS_Y, !(s & data[4]) - !(s & data[5]));
c7fd018d 399
c7fd018d 400 for (j = 0; j < 4; j++)
d38fcb96
DT
401 input_report_key(dev, gc_snes_btn[j],
402 s & data[gc_nes_bytes[j]]);
0995174d
DT
403 input_sync(dev);
404 break;
405
406 case GC_SNES:
407
408 input_report_abs(dev, ABS_X, !(s & data[6]) - !(s & data[7]));
409 input_report_abs(dev, ABS_Y, !(s & data[4]) - !(s & data[5]));
c7fd018d 410
c7fd018d 411 for (j = 0; j < 8; j++)
d38fcb96
DT
412 input_report_key(dev, gc_snes_btn[j],
413 s & data[gc_snes_bytes[j]]);
0995174d
DT
414 input_sync(dev);
415 break;
c7fd018d 416
0995174d 417 case GC_SNESMOUSE:
b157d55e 418 /*
d38fcb96
DT
419 * The 4 unused bits from SNES controllers appear
420 * to be ID bits so use them to make sure we are
421 * dealing with a mouse.
b157d55e
RA
422 * gamepad is connected. This is important since
423 * my SNES gamepad sends 1's for bits 16-31, which
424 * cause the mouse pointer to quickly move to the
425 * upper left corner of the screen.
426 */
427 if (!(s & data[12]) && !(s & data[13]) &&
428 !(s & data[14]) && (s & data[15])) {
429 input_report_key(dev, BTN_LEFT, s & data[9]);
430 input_report_key(dev, BTN_RIGHT, s & data[8]);
431
432 x_rel = y_rel = 0;
433 for (j = 0; j < 7; j++) {
434 x_rel <<= 1;
435 if (data[25 + j] & s)
436 x_rel |= 1;
437
438 y_rel <<= 1;
439 if (data[17 + j] & s)
440 y_rel |= 1;
441 }
442
443 if (x_rel) {
444 if (data[24] & s)
445 x_rel = -x_rel;
446 input_report_rel(dev, REL_X, x_rel);
447 }
448
449 if (y_rel) {
450 if (data[16] & s)
451 y_rel = -y_rel;
452 input_report_rel(dev, REL_Y, y_rel);
453 }
0995174d
DT
454
455 input_sync(dev);
b157d55e 456 }
0995174d
DT
457 break;
458
459 default:
460 break;
b157d55e 461 }
c7fd018d
DT
462 }
463}
464
1da177e4
LT
465/*
466 * Multisystem joystick support
467 */
468
469#define GC_MULTI_LENGTH 5 /* Multi system joystick packet length is 5 */
470#define GC_MULTI2_LENGTH 6 /* One more bit for one more button */
471
472/*
473 * gc_multi_read_packet() reads a Multisystem joystick packet.
474 */
475
476static void gc_multi_read_packet(struct gc *gc, int length, unsigned char *data)
477{
478 int i;
479
480 for (i = 0; i < length; i++) {
481 parport_write_data(gc->pd->port, ~(1 << i));
482 data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
483 }
484}
485
c7fd018d
DT
486static void gc_multi_process_packet(struct gc *gc)
487{
488 unsigned char data[GC_MULTI2_LENGTH];
0995174d
DT
489 int data_len = gc->pad_count[GC_MULTI2] ? GC_MULTI2_LENGTH : GC_MULTI_LENGTH;
490 struct gc_pad *pad;
c7fd018d
DT
491 struct input_dev *dev;
492 int i, s;
493
d38fcb96 494 gc_multi_read_packet(gc, data_len, data);
c7fd018d
DT
495
496 for (i = 0; i < GC_MAX_DEVICES; i++) {
0995174d
DT
497 pad = &gc->pads[i];
498 dev = pad->dev;
c7fd018d
DT
499 s = gc_status_bit[i];
500
0995174d
DT
501 switch (pad->type) {
502 case GC_MULTI2:
503 input_report_key(dev, BTN_THUMB, s & data[5]);
504 /* fall through */
505
506 case GC_MULTI:
d38fcb96
DT
507 input_report_abs(dev, ABS_X,
508 !(s & data[2]) - !(s & data[3]));
509 input_report_abs(dev, ABS_Y,
510 !(s & data[0]) - !(s & data[1]));
c7fd018d 511 input_report_key(dev, BTN_TRIGGER, s & data[4]);
0995174d
DT
512 input_sync(dev);
513 break;
c7fd018d 514
0995174d
DT
515 default:
516 break;
517 }
c7fd018d
DT
518 }
519}
520
1da177e4
LT
521/*
522 * PSX support
523 *
524 * See documentation at:
631dd1a8 525 * http://www.geocities.co.jp/Playtown/2004/psx/ps_eng.txt
1da177e4 526 * http://www.gamesx.com/controldata/psxcont/psxcont.htm
1da177e4
LT
527 *
528 */
529
530#define GC_PSX_DELAY 25 /* 25 usec */
531#define GC_PSX_LENGTH 8 /* talk to the controller in bits */
532#define GC_PSX_BYTES 6 /* the maximum number of bytes to read off the controller */
533
534#define GC_PSX_MOUSE 1 /* Mouse */
535#define GC_PSX_NEGCON 2 /* NegCon */
536#define GC_PSX_NORMAL 4 /* Digital / Analog or Rumble in Digital mode */
537#define GC_PSX_ANALOG 5 /* Analog in Analog mode / Rumble in Green mode */
538#define GC_PSX_RUMBLE 7 /* Rumble in Red mode */
539
540#define GC_PSX_CLOCK 0x04 /* Pin 4 */
541#define GC_PSX_COMMAND 0x01 /* Pin 2 */
542#define GC_PSX_POWER 0xf8 /* Pins 5-9 */
543#define GC_PSX_SELECT 0x02 /* Pin 3 */
544
545#define GC_PSX_ID(x) ((x) >> 4) /* High nibble is device type */
546#define GC_PSX_LEN(x) (((x) & 0xf) << 1) /* Low nibble is length in bytes/2 */
547
548static int gc_psx_delay = GC_PSX_DELAY;
549module_param_named(psx_delay, gc_psx_delay, uint, 0);
550MODULE_PARM_DESC(psx_delay, "Delay when accessing Sony PSX controller (usecs)");
551
af930d64 552static const short gc_psx_abs[] = {
d38fcb96
DT
553 ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_HAT0X, ABS_HAT0Y
554};
af930d64 555static const short gc_psx_btn[] = {
d38fcb96
DT
556 BTN_TL, BTN_TR, BTN_TL2, BTN_TR2, BTN_A, BTN_B, BTN_X, BTN_Y,
557 BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR
558};
af930d64 559static const short gc_psx_ddr_btn[] = { BTN_0, BTN_1, BTN_2, BTN_3 };
1da177e4
LT
560
561/*
562 * gc_psx_command() writes 8bit command and reads 8bit data from
563 * the psx pad.
564 */
565
d38fcb96 566static void gc_psx_command(struct gc *gc, int b, unsigned char *data)
1da177e4 567{
d38fcb96 568 struct parport *port = gc->pd->port;
1da177e4 569 int i, j, cmd, read;
c7fd018d 570
d38fcb96 571 memset(data, 0, GC_MAX_DEVICES);
1da177e4
LT
572
573 for (i = 0; i < GC_PSX_LENGTH; i++, b >>= 1) {
574 cmd = (b & 1) ? GC_PSX_COMMAND : 0;
d38fcb96 575 parport_write_data(port, cmd | GC_PSX_POWER);
1da177e4 576 udelay(gc_psx_delay);
0995174d 577
d38fcb96 578 read = parport_read_status(port) ^ 0x80;
0995174d
DT
579
580 for (j = 0; j < GC_MAX_DEVICES; j++) {
c25f7b76 581 struct gc_pad *pad = &gc->pads[j];
0995174d
DT
582
583 if (pad->type == GC_PSX || pad->type == GC_DDR)
584 data[j] |= (read & gc_status_bit[j]) ? (1 << i) : 0;
585 }
586
1da177e4
LT
587 parport_write_data(gc->pd->port, cmd | GC_PSX_CLOCK | GC_PSX_POWER);
588 udelay(gc_psx_delay);
589 }
590}
591
592/*
593 * gc_psx_read_packet() reads a whole psx packet and returns
594 * device identifier code.
595 */
596
0995174d
DT
597static void gc_psx_read_packet(struct gc *gc,
598 unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES],
c7fd018d 599 unsigned char id[GC_MAX_DEVICES])
1da177e4
LT
600{
601 int i, j, max_len = 0;
602 unsigned long flags;
c7fd018d 603 unsigned char data2[GC_MAX_DEVICES];
1da177e4 604
d38fcb96
DT
605 /* Select pad */
606 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
1da177e4 607 udelay(gc_psx_delay);
d38fcb96
DT
608 /* Deselect, begin command */
609 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_POWER);
1da177e4
LT
610 udelay(gc_psx_delay);
611
612 local_irq_save(flags);
613
d38fcb96
DT
614 gc_psx_command(gc, 0x01, data2); /* Access pad */
615 gc_psx_command(gc, 0x42, id); /* Get device ids */
616 gc_psx_command(gc, 0, data2); /* Dump status */
1da177e4 617
d38fcb96 618 /* Find the longest pad */
0995174d
DT
619 for (i = 0; i < GC_MAX_DEVICES; i++) {
620 struct gc_pad *pad = &gc->pads[i];
621
622 if ((pad->type == GC_PSX || pad->type == GC_DDR) &&
d38fcb96
DT
623 GC_PSX_LEN(id[i]) > max_len &&
624 GC_PSX_LEN(id[i]) <= GC_PSX_BYTES) {
1da177e4 625 max_len = GC_PSX_LEN(id[i]);
d38fcb96 626 }
0995174d 627 }
1da177e4 628
d38fcb96
DT
629 /* Read in all the data */
630 for (i = 0; i < max_len; i++) {
1da177e4 631 gc_psx_command(gc, 0, data2);
c7fd018d 632 for (j = 0; j < GC_MAX_DEVICES; j++)
1da177e4
LT
633 data[j][i] = data2[j];
634 }
635
636 local_irq_restore(flags);
637
638 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
639
d38fcb96
DT
640 /* Set id's to the real value */
641 for (i = 0; i < GC_MAX_DEVICES; i++)
1da177e4
LT
642 id[i] = GC_PSX_ID(id[i]);
643}
644
0995174d 645static void gc_psx_report_one(struct gc_pad *pad, unsigned char psx_type,
d38fcb96 646 unsigned char *data)
c7fd018d 647{
0995174d 648 struct input_dev *dev = pad->dev;
d38fcb96 649 int i;
1da177e4 650
0995174d 651 switch (psx_type) {
1da177e4 652
d38fcb96 653 case GC_PSX_RUMBLE:
1da177e4 654
d38fcb96
DT
655 input_report_key(dev, BTN_THUMBL, ~data[0] & 0x04);
656 input_report_key(dev, BTN_THUMBR, ~data[0] & 0x02);
1da177e4 657
d38fcb96
DT
658 case GC_PSX_NEGCON:
659 case GC_PSX_ANALOG:
1da177e4 660
0995174d 661 if (pad->type == GC_DDR) {
d38fcb96
DT
662 for (i = 0; i < 4; i++)
663 input_report_key(dev, gc_psx_ddr_btn[i],
664 ~data[0] & (0x10 << i));
665 } else {
666 for (i = 0; i < 4; i++)
667 input_report_abs(dev, gc_psx_abs[i + 2],
668 data[i + 2]);
1da177e4 669
315543fd
DT
670 input_report_abs(dev, ABS_X,
671 !!(data[0] & 0x80) * 128 + !(data[0] & 0x20) * 127);
672 input_report_abs(dev, ABS_Y,
673 !!(data[0] & 0x10) * 128 + !(data[0] & 0x40) * 127);
d38fcb96 674 }
1da177e4 675
d38fcb96
DT
676 for (i = 0; i < 8; i++)
677 input_report_key(dev, gc_psx_btn[i], ~data[1] & (1 << i));
1da177e4 678
d38fcb96
DT
679 input_report_key(dev, BTN_START, ~data[0] & 0x08);
680 input_report_key(dev, BTN_SELECT, ~data[0] & 0x01);
1da177e4 681
d38fcb96 682 input_sync(dev);
1da177e4 683
d38fcb96 684 break;
c7fd018d 685
d38fcb96 686 case GC_PSX_NORMAL:
0995174d
DT
687
688 if (pad->type == GC_DDR) {
d38fcb96
DT
689 for (i = 0; i < 4; i++)
690 input_report_key(dev, gc_psx_ddr_btn[i],
691 ~data[0] & (0x10 << i));
692 } else {
315543fd
DT
693 input_report_abs(dev, ABS_X,
694 !!(data[0] & 0x80) * 128 + !(data[0] & 0x20) * 127);
695 input_report_abs(dev, ABS_Y,
696 !!(data[0] & 0x10) * 128 + !(data[0] & 0x40) * 127);
c7fd018d 697
d38fcb96
DT
698 /*
699 * For some reason if the extra axes are left unset
700 * they drift.
701 * for (i = 0; i < 4; i++)
702 input_report_abs(dev, gc_psx_abs[i + 2], 128);
703 * This needs to be debugged properly,
704 * maybe fuzz processing needs to be done
705 * in input_sync()
706 * --vojtech
707 */
708 }
c7fd018d 709
d38fcb96
DT
710 for (i = 0; i < 8; i++)
711 input_report_key(dev, gc_psx_btn[i], ~data[1] & (1 << i));
c7fd018d 712
d38fcb96
DT
713 input_report_key(dev, BTN_START, ~data[0] & 0x08);
714 input_report_key(dev, BTN_SELECT, ~data[0] & 0x01);
c7fd018d 715
d38fcb96 716 input_sync(dev);
1da177e4 717
d38fcb96 718 break;
1da177e4 719
0995174d 720 default: /* not a pad, ignore */
d38fcb96
DT
721 break;
722 }
723}
1da177e4 724
d38fcb96
DT
725static void gc_psx_process_packet(struct gc *gc)
726{
727 unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES];
728 unsigned char id[GC_MAX_DEVICES];
0995174d 729 struct gc_pad *pad;
d38fcb96 730 int i;
1da177e4 731
d38fcb96 732 gc_psx_read_packet(gc, data, id);
c7fd018d 733
d38fcb96 734 for (i = 0; i < GC_MAX_DEVICES; i++) {
0995174d
DT
735 pad = &gc->pads[i];
736 if (pad->type == GC_PSX || pad->type == GC_DDR)
737 gc_psx_report_one(pad, id[i], data[i]);
1da177e4 738 }
c7fd018d 739}
1da177e4
LT
740
741/*
c7fd018d 742 * gc_timer() initiates reads of console pads data.
1da177e4
LT
743 */
744
c7fd018d
DT
745static void gc_timer(unsigned long private)
746{
747 struct gc *gc = (void *) private;
1da177e4 748
c7fd018d
DT
749/*
750 * N64 pads - must be read first, any read confuses them for 200 us
751 */
1da177e4 752
0995174d 753 if (gc->pad_count[GC_N64])
c7fd018d 754 gc_n64_process_packet(gc);
1da177e4 755
c7fd018d 756/*
b157d55e 757 * NES and SNES pads or mouse
c7fd018d 758 */
1da177e4 759
0995174d
DT
760 if (gc->pad_count[GC_NES] ||
761 gc->pad_count[GC_SNES] ||
762 gc->pad_count[GC_SNESMOUSE]) {
c7fd018d 763 gc_nes_process_packet(gc);
0995174d 764 }
1da177e4
LT
765
766/*
767 * Multi and Multi2 joysticks
768 */
769
0995174d 770 if (gc->pad_count[GC_MULTI] || gc->pad_count[GC_MULTI2])
c7fd018d 771 gc_multi_process_packet(gc);
1da177e4
LT
772
773/*
774 * PSX controllers
775 */
776
0995174d 777 if (gc->pad_count[GC_PSX] || gc->pad_count[GC_DDR])
c7fd018d 778 gc_psx_process_packet(gc);
1da177e4
LT
779
780 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
781}
782
783static int gc_open(struct input_dev *dev)
784{
8715c1cf 785 struct gc *gc = input_get_drvdata(dev);
8b1a198b
DT
786 int err;
787
72ba9f0c 788 err = mutex_lock_interruptible(&gc->mutex);
8b1a198b
DT
789 if (err)
790 return err;
791
1da177e4
LT
792 if (!gc->used++) {
793 parport_claim(gc->pd);
794 parport_write_control(gc->pd->port, 0x04);
795 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
796 }
8b1a198b 797
72ba9f0c 798 mutex_unlock(&gc->mutex);
1da177e4
LT
799 return 0;
800}
801
802static void gc_close(struct input_dev *dev)
803{
8715c1cf 804 struct gc *gc = input_get_drvdata(dev);
8b1a198b 805
72ba9f0c 806 mutex_lock(&gc->mutex);
1da177e4 807 if (!--gc->used) {
8b1a198b 808 del_timer_sync(&gc->timer);
1da177e4
LT
809 parport_write_control(gc->pd->port, 0x00);
810 parport_release(gc->pd);
811 }
72ba9f0c 812 mutex_unlock(&gc->mutex);
1da177e4
LT
813}
814
a517e87c 815static int gc_setup_pad(struct gc *gc, int idx, int pad_type)
1da177e4 816{
0995174d 817 struct gc_pad *pad = &gc->pads[idx];
17dd3f0f
DT
818 struct input_dev *input_dev;
819 int i;
7aa9e0e8 820 int err;
1da177e4 821
5bc923c5 822 if (pad_type < 1 || pad_type >= GC_MAX) {
a1e12747 823 pr_err("Pad type %d unknown\n", pad_type);
17dd3f0f 824 return -EINVAL;
1da177e4
LT
825 }
826
0995174d 827 pad->dev = input_dev = input_allocate_device();
17dd3f0f 828 if (!input_dev) {
a1e12747 829 pr_err("Not enough memory for input device\n");
17dd3f0f 830 return -ENOMEM;
1da177e4
LT
831 }
832
0995174d
DT
833 pad->type = pad_type;
834
835 snprintf(pad->phys, sizeof(pad->phys),
836 "%s/input%d", gc->pd->port->name, idx);
837
17dd3f0f 838 input_dev->name = gc_names[pad_type];
0995174d 839 input_dev->phys = pad->phys;
17dd3f0f
DT
840 input_dev->id.bustype = BUS_PARPORT;
841 input_dev->id.vendor = 0x0001;
842 input_dev->id.product = pad_type;
843 input_dev->id.version = 0x0100;
8715c1cf
DT
844
845 input_set_drvdata(input_dev, gc);
17dd3f0f
DT
846
847 input_dev->open = gc_open;
848 input_dev->close = gc_close;
849
b157d55e 850 if (pad_type != GC_SNESMOUSE) {
7b19ada2 851 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
17dd3f0f 852
b157d55e
RA
853 for (i = 0; i < 2; i++)
854 input_set_abs_params(input_dev, ABS_X + i, -1, 1, 0, 0);
855 } else
7b19ada2 856 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
17dd3f0f 857
0995174d 858 gc->pad_count[pad_type]++;
17dd3f0f
DT
859
860 switch (pad_type) {
861
d38fcb96
DT
862 case GC_N64:
863 for (i = 0; i < 10; i++)
864 __set_bit(gc_n64_btn[i], input_dev->keybit);
b157d55e 865
d38fcb96
DT
866 for (i = 0; i < 2; i++) {
867 input_set_abs_params(input_dev, ABS_X + i, -127, 126, 0, 2);
868 input_set_abs_params(input_dev, ABS_HAT0X + i, -1, 1, 0, 0);
869 }
17dd3f0f 870
d38fcb96
DT
871 err = gc_n64_init_ff(input_dev, idx);
872 if (err) {
a1e12747 873 pr_warning("Failed to initiate rumble for N64 device %d\n", idx);
0995174d 874 goto err_free_dev;
d38fcb96 875 }
17dd3f0f 876
d38fcb96
DT
877 break;
878
879 case GC_SNESMOUSE:
880 __set_bit(BTN_LEFT, input_dev->keybit);
881 __set_bit(BTN_RIGHT, input_dev->keybit);
882 __set_bit(REL_X, input_dev->relbit);
883 __set_bit(REL_Y, input_dev->relbit);
884 break;
885
886 case GC_SNES:
887 for (i = 4; i < 8; i++)
888 __set_bit(gc_snes_btn[i], input_dev->keybit);
889 case GC_NES:
890 for (i = 0; i < 4; i++)
891 __set_bit(gc_snes_btn[i], input_dev->keybit);
892 break;
893
894 case GC_MULTI2:
895 __set_bit(BTN_THUMB, input_dev->keybit);
896 case GC_MULTI:
897 __set_bit(BTN_TRIGGER, input_dev->keybit);
898 break;
899
900 case GC_PSX:
901 for (i = 0; i < 6; i++)
902 input_set_abs_params(input_dev,
903 gc_psx_abs[i], 4, 252, 0, 2);
904 for (i = 0; i < 12; i++)
905 __set_bit(gc_psx_btn[i], input_dev->keybit);
906
907 break;
908
909 case GC_DDR:
910 for (i = 0; i < 4; i++)
911 __set_bit(gc_psx_ddr_btn[i], input_dev->keybit);
912 for (i = 0; i < 12; i++)
913 __set_bit(gc_psx_btn[i], input_dev->keybit);
914
915 break;
1da177e4 916 }
8b1a198b 917
0995174d
DT
918 err = input_register_device(pad->dev);
919 if (err)
920 goto err_free_dev;
921
17dd3f0f 922 return 0;
0995174d
DT
923
924err_free_dev:
925 input_free_device(pad->dev);
926 pad->dev = NULL;
927 return err;
17dd3f0f 928}
1da177e4 929
a517e87c 930static void gc_attach(struct parport *pp)
17dd3f0f
DT
931{
932 struct gc *gc;
17dd3f0f 933 struct pardevice *pd;
c36906c5 934 int i, port_idx;
0995174d 935 int count = 0;
a517e87c
SM
936 int *pads, n_pads;
937 struct pardev_cb gc_parport_cb;
938
c36906c5
SM
939 for (port_idx = 0; port_idx < GC_MAX_PORTS; port_idx++) {
940 if (gc_cfg[port_idx].nargs == 0 || gc_cfg[port_idx].args[0] < 0)
a517e87c
SM
941 continue;
942
c36906c5 943 if (gc_cfg[port_idx].args[0] == pp->number)
a517e87c
SM
944 break;
945 }
1da177e4 946
c36906c5 947 if (port_idx == GC_MAX_PORTS) {
a517e87c
SM
948 pr_debug("Not using parport%d.\n", pp->number);
949 return;
17dd3f0f 950 }
c36906c5
SM
951 pads = gc_cfg[port_idx].args + 1;
952 n_pads = gc_cfg[port_idx].nargs - 1;
a517e87c 953
eb12a5f5 954 memset(&gc_parport_cb, 0, sizeof(gc_parport_cb));
a517e87c 955 gc_parport_cb.flags = PARPORT_FLAG_EXCL;
1da177e4 956
c36906c5
SM
957 pd = parport_register_dev_model(pp, "gamecon", &gc_parport_cb,
958 port_idx);
17dd3f0f 959 if (!pd) {
a1e12747 960 pr_err("parport busy already - lp.o loaded?\n");
a517e87c 961 return;
1da177e4
LT
962 }
963
17dd3f0f
DT
964 gc = kzalloc(sizeof(struct gc), GFP_KERNEL);
965 if (!gc) {
a1e12747 966 pr_err("Not enough memory\n");
17dd3f0f
DT
967 goto err_unreg_pardev;
968 }
1da177e4 969
72ba9f0c 970 mutex_init(&gc->mutex);
17dd3f0f 971 gc->pd = pd;
a517e87c 972 gc->parportno = pp->number;
d38fcb96 973 setup_timer(&gc->timer, gc_timer, (long) gc);
1da177e4 974
c7fd018d 975 for (i = 0; i < n_pads && i < GC_MAX_DEVICES; i++) {
17dd3f0f 976 if (!pads[i])
1da177e4
LT
977 continue;
978
a517e87c 979 if (gc_setup_pad(gc, i, pads[i]))
77fc46ca 980 goto err_unreg_devs;
1da177e4 981
0995174d 982 count++;
17dd3f0f 983 }
1da177e4 984
0995174d 985 if (count == 0) {
a1e12747 986 pr_err("No valid devices specified\n");
17dd3f0f
DT
987 goto err_free_gc;
988 }
1da177e4 989
c36906c5 990 gc_base[port_idx] = gc;
a517e87c 991 return;
1da177e4 992
77fc46ca 993 err_unreg_devs:
17dd3f0f 994 while (--i >= 0)
0995174d
DT
995 if (gc->pads[i].dev)
996 input_unregister_device(gc->pads[i].dev);
17dd3f0f
DT
997 err_free_gc:
998 kfree(gc);
999 err_unreg_pardev:
1000 parport_unregister_device(pd);
17dd3f0f 1001}
1da177e4 1002
a517e87c 1003static void gc_detach(struct parport *port)
17dd3f0f
DT
1004{
1005 int i;
a517e87c
SM
1006 struct gc *gc;
1007
1008 for (i = 0; i < GC_MAX_PORTS; i++) {
1009 if (gc_base[i] && gc_base[i]->parportno == port->number)
1010 break;
1011 }
1012
1013 if (i == GC_MAX_PORTS)
1014 return;
1015
1016 gc = gc_base[i];
1017 gc_base[i] = NULL;
1da177e4 1018
17dd3f0f 1019 for (i = 0; i < GC_MAX_DEVICES; i++)
0995174d
DT
1020 if (gc->pads[i].dev)
1021 input_unregister_device(gc->pads[i].dev);
17dd3f0f
DT
1022 parport_unregister_device(gc->pd);
1023 kfree(gc);
1024}
1da177e4 1025
a517e87c
SM
1026static struct parport_driver gc_parport_driver = {
1027 .name = "gamecon",
1028 .match_port = gc_attach,
1029 .detach = gc_detach,
1030 .devmodel = true,
1031};
1032
17dd3f0f
DT
1033static int __init gc_init(void)
1034{
1035 int i;
1036 int have_dev = 0;
1da177e4 1037
17dd3f0f 1038 for (i = 0; i < GC_MAX_PORTS; i++) {
78167236 1039 if (gc_cfg[i].nargs == 0 || gc_cfg[i].args[0] < 0)
17dd3f0f 1040 continue;
1da177e4 1041
78167236 1042 if (gc_cfg[i].nargs < 2) {
a1e12747 1043 pr_err("at least one device must be specified\n");
a517e87c 1044 return -EINVAL;
17dd3f0f 1045 }
1da177e4 1046
17dd3f0f 1047 have_dev = 1;
1da177e4
LT
1048 }
1049
a517e87c
SM
1050 if (!have_dev)
1051 return -ENODEV;
1da177e4 1052
a517e87c 1053 return parport_register_driver(&gc_parport_driver);
1da177e4
LT
1054}
1055
1056static void __exit gc_exit(void)
1057{
a517e87c 1058 parport_unregister_driver(&gc_parport_driver);
1da177e4
LT
1059}
1060
1061module_init(gc_init);
1062module_exit(gc_exit);