1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34
35#include <linux/kernel.h>
36#include <linux/delay.h>
37#include <linux/module.h>
38#include <linux/init.h>
39#include <linux/parport.h>
40#include <linux/input.h>
41#include <linux/mutex.h>
42#include <linux/slab.h>
43
44MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
45MODULE_DESCRIPTION("NES, SNES, N64, MultiSystem, PSX gamepad driver");
46MODULE_LICENSE("GPL");
47
48#define GC_MAX_PORTS 3
49#define GC_MAX_DEVICES 5
50
51struct gc_config {
52 int args[GC_MAX_DEVICES + 1];
53 unsigned int nargs;
54};
55
56static struct gc_config gc_cfg[GC_MAX_PORTS];
57
58module_param_array_named(map, gc_cfg[0].args, int, &gc_cfg[0].nargs, 0);
59MODULE_PARM_DESC(map, "Describes first set of devices (<parport#>,<pad1>,<pad2>,..<pad5>)");
60module_param_array_named(map2, gc_cfg[1].args, int, &gc_cfg[1].nargs, 0);
61MODULE_PARM_DESC(map2, "Describes second set of devices");
62module_param_array_named(map3, gc_cfg[2].args, int, &gc_cfg[2].nargs, 0);
63MODULE_PARM_DESC(map3, "Describes third set of devices");
64
65
66
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};
80
81#define GC_REFRESH_TIME HZ/100
82
83struct gc_pad {
84 struct input_dev *dev;
85 enum gc_type type;
86 char phys[32];
87};
88
89struct gc {
90 struct pardevice *pd;
91 struct gc_pad pads[GC_MAX_DEVICES];
92 struct timer_list timer;
93 int pad_count[GC_MAX];
94 int used;
95 int parportno;
96 struct mutex mutex;
97};
98
99struct gc_subdev {
100 unsigned int idx;
101};
102
103static struct gc *gc_base[3];
104
105static const int gc_status_bit[] = { 0x40, 0x80, 0x20, 0x10, 0x08 };
106
107static const char *gc_names[] = {
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
113
114
115
116
117static const unsigned char gc_n64_bytes[] = { 0, 1, 13, 15, 14, 12, 10, 11, 2, 3 };
118static const short gc_n64_btn[] = {
119 BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z,
120 BTN_TL, BTN_TR, BTN_TRIGGER, BTN_START
121};
122
123#define GC_N64_LENGTH 32
124#define GC_N64_STOP_LENGTH 5
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
132#define GC_N64_REQUEST_DATA GC_N64_CMD_01
133#define GC_N64_DELAY 133
134#define GC_N64_DWS 3
135
136#define GC_N64_POWER_W 0xe2
137#define GC_N64_POWER_R 0xfd
138#define GC_N64_OUT 0x1d
139
140
141
142#define GC_N64_CLOCK 0x02
143
144
145
146
147
148
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
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
175
176
177
178
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
188
189
190 local_irq_save(flags);
191 gc_n64_send_command(gc, GC_N64_REQUEST_DATA, GC_N64_OUT);
192 gc_n64_send_stop_bit(gc, GC_N64_OUT);
193 local_irq_restore(flags);
194
195
196
197
198
199
200 udelay(GC_N64_DELAY);
201
202
203
204
205
206 for (i = 0; i < GC_N64_LENGTH; i++) {
207 parport_write_data(gc->pd->port, GC_N64_POWER_R);
208 udelay(2);
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
214
215
216
217
218
219}
220
221static void gc_n64_process_packet(struct gc *gc)
222{
223 unsigned char data[GC_N64_LENGTH];
224 struct input_dev *dev;
225 int i, j, s;
226 signed char x, y;
227
228 gc_n64_read_packet(gc, data);
229
230 for (i = 0; i < GC_MAX_DEVICES; i++) {
231
232 if (gc->pads[i].type != GC_N64)
233 continue;
234
235 dev = gc->pads[i].dev;
236 s = gc_status_bit[i];
237
238 if (s & ~(data[8] | data[9])) {
239
240 x = y = 0;
241
242 for (j = 0; j < 8; j++) {
243 if (data[23 - j] & s)
244 x |= 1 << j;
245 if (data[31 - j] & s)
246 y |= 1 << j;
247 }
248
249 input_report_abs(dev, ABS_X, x);
250 input_report_abs(dev, ABS_Y, -y);
251
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]));
256
257 for (j = 0; j < 10; j++)
258 input_report_key(dev, gc_n64_btn[j],
259 s & data[gc_n64_bytes[j]]);
260
261 input_sync(dev);
262 }
263 }
264}
265
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;
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
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
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
308static int gc_n64_init_ff(struct input_dev *dev, int i)
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
330
331
332
333
334#define GC_NES_DELAY 6
335#define GC_NES_LENGTH 8
336#define GC_SNES_LENGTH 12
337
338#define GC_SNESMOUSE_LENGTH 32
339
340
341#define GC_NES_POWER 0xfc
342#define GC_NES_CLOCK 0x01
343#define GC_NES_LATCH 0x02
344
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[] = {
348 BTN_A, BTN_B, BTN_SELECT, BTN_START, BTN_X, BTN_Y, BTN_TL, BTN_TR
349};
350
351
352
353
354
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
374static void gc_nes_process_packet(struct gc *gc)
375{
376 unsigned char data[GC_SNESMOUSE_LENGTH];
377 struct gc_pad *pad;
378 struct input_dev *dev;
379 int i, j, s, len;
380 char x_rel, y_rel;
381
382 len = gc->pad_count[GC_SNESMOUSE] ? GC_SNESMOUSE_LENGTH :
383 (gc->pad_count[GC_SNES] ? GC_SNES_LENGTH : GC_NES_LENGTH);
384
385 gc_nes_read_packet(gc, len, data);
386
387 for (i = 0; i < GC_MAX_DEVICES; i++) {
388
389 pad = &gc->pads[i];
390 dev = pad->dev;
391 s = gc_status_bit[i];
392
393 switch (pad->type) {
394
395 case GC_NES:
396
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]));
399
400 for (j = 0; j < 4; j++)
401 input_report_key(dev, gc_snes_btn[j],
402 s & data[gc_nes_bytes[j]]);
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]));
410
411 for (j = 0; j < 8; j++)
412 input_report_key(dev, gc_snes_btn[j],
413 s & data[gc_snes_bytes[j]]);
414 input_sync(dev);
415 break;
416
417 case GC_SNESMOUSE:
418
419
420
421
422
423
424
425
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 }
454
455 input_sync(dev);
456 }
457 break;
458
459 default:
460 break;
461 }
462 }
463}
464
465
466
467
468
469#define GC_MULTI_LENGTH 5
470#define GC_MULTI2_LENGTH 6
471
472
473
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
486static void gc_multi_process_packet(struct gc *gc)
487{
488 unsigned char data[GC_MULTI2_LENGTH];
489 int data_len = gc->pad_count[GC_MULTI2] ? GC_MULTI2_LENGTH : GC_MULTI_LENGTH;
490 struct gc_pad *pad;
491 struct input_dev *dev;
492 int i, s;
493
494 gc_multi_read_packet(gc, data_len, data);
495
496 for (i = 0; i < GC_MAX_DEVICES; i++) {
497 pad = &gc->pads[i];
498 dev = pad->dev;
499 s = gc_status_bit[i];
500
501 switch (pad->type) {
502 case GC_MULTI2:
503 input_report_key(dev, BTN_THUMB, s & data[5]);
504
505
506 case GC_MULTI:
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]));
511 input_report_key(dev, BTN_TRIGGER, s & data[4]);
512 input_sync(dev);
513 break;
514
515 default:
516 break;
517 }
518 }
519}
520
521
522
523
524
525
526
527
528
529
530#define GC_PSX_DELAY 25
531#define GC_PSX_LENGTH 8
532#define GC_PSX_BYTES 6
533
534#define GC_PSX_MOUSE 1
535#define GC_PSX_NEGCON 2
536#define GC_PSX_NORMAL 4
537#define GC_PSX_ANALOG 5
538#define GC_PSX_RUMBLE 7
539
540#define GC_PSX_CLOCK 0x04
541#define GC_PSX_COMMAND 0x01
542#define GC_PSX_POWER 0xf8
543#define GC_PSX_SELECT 0x02
544
545#define GC_PSX_ID(x) ((x) >> 4)
546#define GC_PSX_LEN(x) (((x) & 0xf) << 1)
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
552static const short gc_psx_abs[] = {
553 ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_HAT0X, ABS_HAT0Y
554};
555static const short gc_psx_btn[] = {
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};
559static const short gc_psx_ddr_btn[] = { BTN_0, BTN_1, BTN_2, BTN_3 };
560
561
562
563
564
565
566static void gc_psx_command(struct gc *gc, int b, unsigned char *data)
567{
568 struct parport *port = gc->pd->port;
569 int i, j, cmd, read;
570
571 memset(data, 0, GC_MAX_DEVICES);
572
573 for (i = 0; i < GC_PSX_LENGTH; i++, b >>= 1) {
574 cmd = (b & 1) ? GC_PSX_COMMAND : 0;
575 parport_write_data(port, cmd | GC_PSX_POWER);
576 udelay(gc_psx_delay);
577
578 read = parport_read_status(port) ^ 0x80;
579
580 for (j = 0; j < GC_MAX_DEVICES; j++) {
581 struct gc_pad *pad = &gc->pads[j];
582
583 if (pad->type == GC_PSX || pad->type == GC_DDR)
584 data[j] |= (read & gc_status_bit[j]) ? (1 << i) : 0;
585 }
586
587 parport_write_data(gc->pd->port, cmd | GC_PSX_CLOCK | GC_PSX_POWER);
588 udelay(gc_psx_delay);
589 }
590}
591
592
593
594
595
596
597static void gc_psx_read_packet(struct gc *gc,
598 unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES],
599 unsigned char id[GC_MAX_DEVICES])
600{
601 int i, j, max_len = 0;
602 unsigned long flags;
603 unsigned char data2[GC_MAX_DEVICES];
604
605
606 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
607 udelay(gc_psx_delay);
608
609 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_POWER);
610 udelay(gc_psx_delay);
611
612 local_irq_save(flags);
613
614 gc_psx_command(gc, 0x01, data2);
615 gc_psx_command(gc, 0x42, id);
616 gc_psx_command(gc, 0, data2);
617
618
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) &&
623 GC_PSX_LEN(id[i]) > max_len &&
624 GC_PSX_LEN(id[i]) <= GC_PSX_BYTES) {
625 max_len = GC_PSX_LEN(id[i]);
626 }
627 }
628
629
630 for (i = 0; i < max_len; i++) {
631 gc_psx_command(gc, 0, data2);
632 for (j = 0; j < GC_MAX_DEVICES; j++)
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
640
641 for (i = 0; i < GC_MAX_DEVICES; i++)
642 id[i] = GC_PSX_ID(id[i]);
643}
644
645static void gc_psx_report_one(struct gc_pad *pad, unsigned char psx_type,
646 unsigned char *data)
647{
648 struct input_dev *dev = pad->dev;
649 int i;
650
651 switch (psx_type) {
652
653 case GC_PSX_RUMBLE:
654
655 input_report_key(dev, BTN_THUMBL, ~data[0] & 0x04);
656 input_report_key(dev, BTN_THUMBR, ~data[0] & 0x02);
657
658
659 case GC_PSX_NEGCON:
660 case GC_PSX_ANALOG:
661
662 if (pad->type == GC_DDR) {
663 for (i = 0; i < 4; i++)
664 input_report_key(dev, gc_psx_ddr_btn[i],
665 ~data[0] & (0x10 << i));
666 } else {
667 for (i = 0; i < 4; i++)
668 input_report_abs(dev, gc_psx_abs[i + 2],
669 data[i + 2]);
670
671 input_report_abs(dev, ABS_X,
672 !!(data[0] & 0x80) * 128 + !(data[0] & 0x20) * 127);
673 input_report_abs(dev, ABS_Y,
674 !!(data[0] & 0x10) * 128 + !(data[0] & 0x40) * 127);
675 }
676
677 for (i = 0; i < 8; i++)
678 input_report_key(dev, gc_psx_btn[i], ~data[1] & (1 << i));
679
680 input_report_key(dev, BTN_START, ~data[0] & 0x08);
681 input_report_key(dev, BTN_SELECT, ~data[0] & 0x01);
682
683 input_sync(dev);
684
685 break;
686
687 case GC_PSX_NORMAL:
688
689 if (pad->type == GC_DDR) {
690 for (i = 0; i < 4; i++)
691 input_report_key(dev, gc_psx_ddr_btn[i],
692 ~data[0] & (0x10 << i));
693 } else {
694 input_report_abs(dev, ABS_X,
695 !!(data[0] & 0x80) * 128 + !(data[0] & 0x20) * 127);
696 input_report_abs(dev, ABS_Y,
697 !!(data[0] & 0x10) * 128 + !(data[0] & 0x40) * 127);
698
699
700
701
702
703
704
705
706
707
708
709 }
710
711 for (i = 0; i < 8; i++)
712 input_report_key(dev, gc_psx_btn[i], ~data[1] & (1 << i));
713
714 input_report_key(dev, BTN_START, ~data[0] & 0x08);
715 input_report_key(dev, BTN_SELECT, ~data[0] & 0x01);
716
717 input_sync(dev);
718
719 break;
720
721 default:
722 break;
723 }
724}
725
726static void gc_psx_process_packet(struct gc *gc)
727{
728 unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES];
729 unsigned char id[GC_MAX_DEVICES];
730 struct gc_pad *pad;
731 int i;
732
733 gc_psx_read_packet(gc, data, id);
734
735 for (i = 0; i < GC_MAX_DEVICES; i++) {
736 pad = &gc->pads[i];
737 if (pad->type == GC_PSX || pad->type == GC_DDR)
738 gc_psx_report_one(pad, id[i], data[i]);
739 }
740}
741
742
743
744
745
746static void gc_timer(struct timer_list *t)
747{
748 struct gc *gc = from_timer(gc, t, timer);
749
750
751
752
753
754 if (gc->pad_count[GC_N64])
755 gc_n64_process_packet(gc);
756
757
758
759
760
761 if (gc->pad_count[GC_NES] ||
762 gc->pad_count[GC_SNES] ||
763 gc->pad_count[GC_SNESMOUSE]) {
764 gc_nes_process_packet(gc);
765 }
766
767
768
769
770
771 if (gc->pad_count[GC_MULTI] || gc->pad_count[GC_MULTI2])
772 gc_multi_process_packet(gc);
773
774
775
776
777
778 if (gc->pad_count[GC_PSX] || gc->pad_count[GC_DDR])
779 gc_psx_process_packet(gc);
780
781 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
782}
783
784static int gc_open(struct input_dev *dev)
785{
786 struct gc *gc = input_get_drvdata(dev);
787 int err;
788
789 err = mutex_lock_interruptible(&gc->mutex);
790 if (err)
791 return err;
792
793 if (!gc->used++) {
794 parport_claim(gc->pd);
795 parport_write_control(gc->pd->port, 0x04);
796 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
797 }
798
799 mutex_unlock(&gc->mutex);
800 return 0;
801}
802
803static void gc_close(struct input_dev *dev)
804{
805 struct gc *gc = input_get_drvdata(dev);
806
807 mutex_lock(&gc->mutex);
808 if (!--gc->used) {
809 del_timer_sync(&gc->timer);
810 parport_write_control(gc->pd->port, 0x00);
811 parport_release(gc->pd);
812 }
813 mutex_unlock(&gc->mutex);
814}
815
816static int gc_setup_pad(struct gc *gc, int idx, int pad_type)
817{
818 struct gc_pad *pad = &gc->pads[idx];
819 struct input_dev *input_dev;
820 int i;
821 int err;
822
823 if (pad_type < 1 || pad_type >= GC_MAX) {
824 pr_err("Pad type %d unknown\n", pad_type);
825 return -EINVAL;
826 }
827
828 pad->dev = input_dev = input_allocate_device();
829 if (!input_dev) {
830 pr_err("Not enough memory for input device\n");
831 return -ENOMEM;
832 }
833
834 pad->type = pad_type;
835
836 snprintf(pad->phys, sizeof(pad->phys),
837 "%s/input%d", gc->pd->port->name, idx);
838
839 input_dev->name = gc_names[pad_type];
840 input_dev->phys = pad->phys;
841 input_dev->id.bustype = BUS_PARPORT;
842 input_dev->id.vendor = 0x0001;
843 input_dev->id.product = pad_type;
844 input_dev->id.version = 0x0100;
845
846 input_set_drvdata(input_dev, gc);
847
848 input_dev->open = gc_open;
849 input_dev->close = gc_close;
850
851 if (pad_type != GC_SNESMOUSE) {
852 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
853
854 for (i = 0; i < 2; i++)
855 input_set_abs_params(input_dev, ABS_X + i, -1, 1, 0, 0);
856 } else
857 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
858
859 gc->pad_count[pad_type]++;
860
861 switch (pad_type) {
862
863 case GC_N64:
864 for (i = 0; i < 10; i++)
865 input_set_capability(input_dev, EV_KEY, gc_n64_btn[i]);
866
867 for (i = 0; i < 2; i++) {
868 input_set_abs_params(input_dev, ABS_X + i, -127, 126, 0, 2);
869 input_set_abs_params(input_dev, ABS_HAT0X + i, -1, 1, 0, 0);
870 }
871
872 err = gc_n64_init_ff(input_dev, idx);
873 if (err) {
874 pr_warn("Failed to initiate rumble for N64 device %d\n",
875 idx);
876 goto err_free_dev;
877 }
878
879 break;
880
881 case GC_SNESMOUSE:
882 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
883 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
884 input_set_capability(input_dev, EV_REL, REL_X);
885 input_set_capability(input_dev, EV_REL, REL_Y);
886 break;
887
888 case GC_SNES:
889 for (i = 4; i < 8; i++)
890 input_set_capability(input_dev, EV_KEY, gc_snes_btn[i]);
891
892 case GC_NES:
893 for (i = 0; i < 4; i++)
894 input_set_capability(input_dev, EV_KEY, gc_snes_btn[i]);
895 break;
896
897 case GC_MULTI2:
898 input_set_capability(input_dev, EV_KEY, BTN_THUMB);
899
900 case GC_MULTI:
901 input_set_capability(input_dev, EV_KEY, BTN_TRIGGER);
902
903 break;
904
905 case GC_PSX:
906 for (i = 0; i < 6; i++)
907 input_set_abs_params(input_dev,
908 gc_psx_abs[i], 4, 252, 0, 2);
909 for (i = 0; i < 12; i++)
910 input_set_capability(input_dev, EV_KEY, gc_psx_btn[i]);
911 break;
912
913 break;
914
915 case GC_DDR:
916 for (i = 0; i < 4; i++)
917 input_set_capability(input_dev, EV_KEY,
918 gc_psx_ddr_btn[i]);
919 for (i = 0; i < 12; i++)
920 input_set_capability(input_dev, EV_KEY, gc_psx_btn[i]);
921
922 break;
923 }
924
925 err = input_register_device(pad->dev);
926 if (err)
927 goto err_free_dev;
928
929 return 0;
930
931err_free_dev:
932 input_free_device(pad->dev);
933 pad->dev = NULL;
934 return err;
935}
936
937static void gc_attach(struct parport *pp)
938{
939 struct gc *gc;
940 struct pardevice *pd;
941 int i, port_idx;
942 int count = 0;
943 int *pads, n_pads;
944 struct pardev_cb gc_parport_cb;
945
946 for (port_idx = 0; port_idx < GC_MAX_PORTS; port_idx++) {
947 if (gc_cfg[port_idx].nargs == 0 || gc_cfg[port_idx].args[0] < 0)
948 continue;
949
950 if (gc_cfg[port_idx].args[0] == pp->number)
951 break;
952 }
953
954 if (port_idx == GC_MAX_PORTS) {
955 pr_debug("Not using parport%d.\n", pp->number);
956 return;
957 }
958 pads = gc_cfg[port_idx].args + 1;
959 n_pads = gc_cfg[port_idx].nargs - 1;
960
961 memset(&gc_parport_cb, 0, sizeof(gc_parport_cb));
962 gc_parport_cb.flags = PARPORT_FLAG_EXCL;
963
964 pd = parport_register_dev_model(pp, "gamecon", &gc_parport_cb,
965 port_idx);
966 if (!pd) {
967 pr_err("parport busy already - lp.o loaded?\n");
968 return;
969 }
970
971 gc = kzalloc(sizeof(struct gc), GFP_KERNEL);
972 if (!gc) {
973 pr_err("Not enough memory\n");
974 goto err_unreg_pardev;
975 }
976
977 mutex_init(&gc->mutex);
978 gc->pd = pd;
979 gc->parportno = pp->number;
980 timer_setup(&gc->timer, gc_timer, 0);
981
982 for (i = 0; i < n_pads && i < GC_MAX_DEVICES; i++) {
983 if (!pads[i])
984 continue;
985
986 if (gc_setup_pad(gc, i, pads[i]))
987 goto err_unreg_devs;
988
989 count++;
990 }
991
992 if (count == 0) {
993 pr_err("No valid devices specified\n");
994 goto err_free_gc;
995 }
996
997 gc_base[port_idx] = gc;
998 return;
999
1000 err_unreg_devs:
1001 while (--i >= 0)
1002 if (gc->pads[i].dev)
1003 input_unregister_device(gc->pads[i].dev);
1004 err_free_gc:
1005 kfree(gc);
1006 err_unreg_pardev:
1007 parport_unregister_device(pd);
1008}
1009
1010static void gc_detach(struct parport *port)
1011{
1012 int i;
1013 struct gc *gc;
1014
1015 for (i = 0; i < GC_MAX_PORTS; i++) {
1016 if (gc_base[i] && gc_base[i]->parportno == port->number)
1017 break;
1018 }
1019
1020 if (i == GC_MAX_PORTS)
1021 return;
1022
1023 gc = gc_base[i];
1024 gc_base[i] = NULL;
1025
1026 for (i = 0; i < GC_MAX_DEVICES; i++)
1027 if (gc->pads[i].dev)
1028 input_unregister_device(gc->pads[i].dev);
1029 parport_unregister_device(gc->pd);
1030 kfree(gc);
1031}
1032
1033static struct parport_driver gc_parport_driver = {
1034 .name = "gamecon",
1035 .match_port = gc_attach,
1036 .detach = gc_detach,
1037 .devmodel = true,
1038};
1039
1040static int __init gc_init(void)
1041{
1042 int i;
1043 int have_dev = 0;
1044
1045 for (i = 0; i < GC_MAX_PORTS; i++) {
1046 if (gc_cfg[i].nargs == 0 || gc_cfg[i].args[0] < 0)
1047 continue;
1048
1049 if (gc_cfg[i].nargs < 2) {
1050 pr_err("at least one device must be specified\n");
1051 return -EINVAL;
1052 }
1053
1054 have_dev = 1;
1055 }
1056
1057 if (!have_dev)
1058 return -ENODEV;
1059
1060 return parport_register_driver(&gc_parport_driver);
1061}
1062
1063static void __exit gc_exit(void)
1064{
1065 parport_unregister_driver(&gc_parport_driver);
1066}
1067
1068module_init(gc_init);
1069module_exit(gc_exit);
1070