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