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
34
35
36
37#include <linux/kernel.h>
38#include <linux/delay.h>
39#include <linux/module.h>
40#include <linux/init.h>
41#include <linux/parport.h>
42#include <linux/input.h>
43
44MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
45MODULE_LICENSE("GPL");
46MODULE_PARM(gc, "2-6i");
47MODULE_PARM(gc_2,"2-6i");
48MODULE_PARM(gc_3,"2-6i");
49
50#define GC_SNES 1
51#define GC_NES 2
52#define GC_NES4 3
53#define GC_MULTI 4
54#define GC_MULTI2 5
55#define GC_N64 6
56#define GC_PSX 7
57
58#define GC_MAX 7
59
60#define GC_REFRESH_TIME HZ/100
61
62struct gc {
63 struct pardevice *pd;
64 struct input_dev dev[5];
65 struct timer_list timer;
66 unsigned char pads[GC_MAX + 1];
67 int used;
68};
69
70static struct gc *gc_base[3];
71
72static int gc[] __initdata = { -1, 0, 0, 0, 0, 0 };
73static int gc_2[] __initdata = { -1, 0, 0, 0, 0, 0 };
74static int gc_3[] __initdata = { -1, 0, 0, 0, 0, 0 };
75
76static int gc_status_bit[] = { 0x40, 0x80, 0x20, 0x10, 0x08 };
77
78static char *gc_names[] = { NULL, "SNES pad", "NES pad", "NES FourPort", "Multisystem joystick",
79 "Multisystem 2-button joystick", "N64 controller", "PSX controller" };
80
81
82
83
84static unsigned char gc_n64_bytes[] = { 0, 1, 13, 15, 14, 12, 10, 11, 2, 3 };
85static short gc_n64_btn[] = { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_TRIGGER, BTN_START };
86
87#define GC_N64_LENGTH 32
88#define GC_N64_REQUEST_LENGTH 37
89#define GC_N64_DELAY 133
90#define GC_N64_REQUEST 0x1dd1111111ULL
91#define GC_N64_DWS 3
92
93#define GC_N64_POWER_W 0xe2
94#define GC_N64_POWER_R 0xfd
95#define GC_N64_OUT 0x1d
96
97
98
99#define GC_N64_CLOCK 0x02
100
101
102
103
104
105
106static void gc_n64_read_packet(struct gc *gc, unsigned char *data)
107{
108 int i;
109 unsigned long flags;
110
111
112
113
114
115 __save_flags(flags);
116 __cli();
117 for (i = 0; i < GC_N64_REQUEST_LENGTH; i++) {
118 parport_write_data(gc->pd->port, GC_N64_POWER_W | ((GC_N64_REQUEST >> i) & 1 ? GC_N64_OUT : 0));
119 udelay(GC_N64_DWS);
120 }
121 __restore_flags(flags);
122
123
124
125
126
127 udelay(GC_N64_DELAY);
128
129
130
131
132
133 for (i = 0; i < GC_N64_LENGTH; i++) {
134 parport_write_data(gc->pd->port, GC_N64_POWER_R);
135 data[i] = parport_read_status(gc->pd->port);
136 parport_write_data(gc->pd->port, GC_N64_POWER_R | GC_N64_CLOCK);
137 }
138
139
140
141
142
143
144}
145
146
147
148
149
150#define GC_NES_DELAY 6
151#define GC_NES_LENGTH 8
152#define GC_SNES_LENGTH 12
153
154#define GC_NES_POWER 0xfc
155#define GC_NES_CLOCK 0x01
156#define GC_NES_LATCH 0x02
157
158static unsigned char gc_nes_bytes[] = { 0, 1, 2, 3 };
159static unsigned char gc_snes_bytes[] = { 8, 0, 2, 3, 9, 1, 10, 11 };
160static short gc_snes_btn[] = { BTN_A, BTN_B, BTN_SELECT, BTN_START, BTN_X, BTN_Y, BTN_TL, BTN_TR };
161
162
163
164
165
166
167
168static void gc_nes_read_packet(struct gc *gc, int length, unsigned char *data)
169{
170 int i;
171
172 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK | GC_NES_LATCH);
173 udelay(GC_NES_DELAY * 2);
174 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
175
176 for (i = 0; i < length; i++) {
177 udelay(GC_NES_DELAY);
178 parport_write_data(gc->pd->port, GC_NES_POWER);
179 data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
180 udelay(GC_NES_DELAY);
181 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
182 }
183}
184
185
186
187
188
189#define GC_MULTI_LENGTH 5
190#define GC_MULTI2_LENGTH 6
191
192
193
194
195
196static void gc_multi_read_packet(struct gc *gc, int length, unsigned char *data)
197{
198 int i;
199
200 for (i = 0; i < length; i++) {
201 parport_write_data(gc->pd->port, ~(1 << i));
202 data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
203 }
204}
205
206
207
208
209
210
211
212
213
214
215
216#define GC_PSX_DELAY 60
217#define GC_PSX_LENGTH 8
218
219#define GC_PSX_MOUSE 1
220#define GC_PSX_NEGCON 2
221#define GC_PSX_NORMAL 4
222#define GC_PSX_ANALOG 5
223#define GC_PSX_RUMBLE 7
224
225#define GC_PSX_CLOCK 0x04
226#define GC_PSX_COMMAND 0x01
227#define GC_PSX_POWER 0xf8
228#define GC_PSX_SELECT 0x02
229
230#define GC_PSX_ID(x) ((x) >> 4)
231#define GC_PSX_LEN(x) ((x) & 0xf)
232
233static short gc_psx_abs[] = { ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_HAT0X, ABS_HAT0Y };
234static short gc_psx_btn[] = { BTN_TL, BTN_TR, BTN_TL2, BTN_TR2, BTN_A, BTN_B, BTN_X, BTN_Y,
235 BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR };
236
237
238
239
240
241
242static int gc_psx_command(struct gc *gc, int b)
243{
244 int i, cmd, data = 0;
245
246 for (i = 0; i < 8; i++, b >>= 1) {
247 cmd = (b & 1) ? GC_PSX_COMMAND : 0;
248 parport_write_data(gc->pd->port, cmd | GC_PSX_POWER);
249 udelay(GC_PSX_DELAY);
250 data |= ((parport_read_status(gc->pd->port) ^ 0x80) & gc->pads[GC_PSX]) ? (1 << i) : 0;
251 parport_write_data(gc->pd->port, cmd | GC_PSX_CLOCK | GC_PSX_POWER);
252 udelay(GC_PSX_DELAY);
253 }
254 return data;
255}
256
257
258
259
260
261
262static int gc_psx_read_packet(struct gc *gc, unsigned char *data)
263{
264 int i, id;
265 unsigned long flags;
266
267 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
268 udelay(GC_PSX_DELAY * 2);
269 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_POWER);
270 udelay(GC_PSX_DELAY * 2);
271
272 __save_flags(flags);
273 __cli();
274
275 gc_psx_command(gc, 0x01);
276 id = gc_psx_command(gc, 0x42);
277 if (gc_psx_command(gc, 0) == 0x5a) {
278 for (i = 0; i < GC_PSX_LEN(id) * 2; i++)
279 data[i] = gc_psx_command(gc, 0);
280 } else id = 0;
281
282 __restore_flags(flags);
283
284 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
285
286 return GC_PSX_ID(id);
287}
288
289
290
291
292
293#define GC_MAX_LENGTH GC_N64_LENGTH
294
295static void gc_timer(unsigned long private)
296{
297 struct gc *gc = (void *) private;
298 struct input_dev *dev = gc->dev;
299 unsigned char data[GC_MAX_LENGTH];
300 int i, j, s;
301
302
303
304
305
306 if (gc->pads[GC_N64]) {
307
308 gc_n64_read_packet(gc, data);
309
310 for (i = 0; i < 5; i++) {
311
312 s = gc_status_bit[i];
313
314 if (s & gc->pads[GC_N64] & ~(data[8] | data[9])) {
315
316 signed char axes[2];
317 axes[0] = axes[1] = 0;
318
319 for (j = 0; j < 8; j++) {
320 if (data[23 - j] & s) axes[0] |= 1 << j;
321 if (data[31 - j] & s) axes[1] |= 1 << j;
322 }
323
324 input_report_abs(dev + i, ABS_X, axes[0]);
325 input_report_abs(dev + i, ABS_Y, -axes[1]);
326
327 input_report_abs(dev + i, ABS_HAT0X, !(s & data[6]) - !(s & data[7]));
328 input_report_abs(dev + i, ABS_HAT0Y, !(s & data[4]) - !(s & data[5]));
329
330 for (j = 0; j < 10; j++)
331 input_report_key(dev + i, gc_n64_btn[j], s & data[gc_n64_bytes[j]]);
332 }
333 }
334 }
335
336
337
338
339
340 if (gc->pads[GC_NES] || gc->pads[GC_SNES]) {
341
342 gc_nes_read_packet(gc, gc->pads[GC_SNES] ? GC_SNES_LENGTH : GC_NES_LENGTH, data);
343
344 for (i = 0; i < 5; i++) {
345
346 s = gc_status_bit[i];
347
348 if (s & (gc->pads[GC_NES] | gc->pads[GC_SNES])) {
349 input_report_abs(dev + i, ABS_X, !(s & data[6]) - !(s & data[7]));
350 input_report_abs(dev + i, ABS_Y, !(s & data[4]) - !(s & data[5]));
351 }
352
353 if (s & gc->pads[GC_NES])
354 for (j = 0; j < 4; j++)
355 input_report_key(dev + i, gc_snes_btn[j], s & data[gc_nes_bytes[j]]);
356
357 if (s & gc->pads[GC_SNES])
358 for (j = 0; j < 8; j++)
359 input_report_key(dev + i, gc_snes_btn[j], s & data[gc_snes_bytes[j]]);
360 }
361 }
362
363
364
365
366
367 if (gc->pads[GC_MULTI] || gc->pads[GC_MULTI2]) {
368
369 gc_multi_read_packet(gc, gc->pads[GC_MULTI2] ? GC_MULTI2_LENGTH : GC_MULTI_LENGTH, data);
370
371 for (i = 0; i < 5; i++) {
372
373 s = gc_status_bit[i];
374
375 if (s & (gc->pads[GC_MULTI] | gc->pads[GC_MULTI2])) {
376 input_report_abs(dev + i, ABS_X, !(s & data[2]) - !(s & data[3]));
377 input_report_abs(dev + i, ABS_Y, !(s & data[0]) - !(s & data[1]));
378 input_report_key(dev + i, BTN_TRIGGER, s & data[4]);
379 }
380
381 if (s & gc->pads[GC_MULTI2])
382 input_report_key(dev + i, BTN_THUMB, s & data[5]);
383 }
384 }
385
386
387
388
389
390 if (gc->pads[GC_PSX]) {
391
392 for (i = 0; i < 5; i++)
393 if (gc->pads[GC_PSX] & gc_status_bit[i])
394 break;
395
396 switch (gc_psx_read_packet(gc, data)) {
397
398 case GC_PSX_RUMBLE:
399
400 input_report_key(dev + i, BTN_THUMB, ~data[0] & 0x04);
401 input_report_key(dev + i, BTN_THUMB2, ~data[0] & 0x02);
402
403 case GC_PSX_NEGCON:
404 case GC_PSX_ANALOG:
405
406 for (j = 0; j < 4; j++)
407 input_report_abs(dev + i, gc_psx_abs[j], data[j + 2]);
408
409 input_report_abs(dev + i, ABS_HAT0X, !(data[0] & 0x20) - !(data[0] & 0x80));
410 input_report_abs(dev + i, ABS_HAT0Y, !(data[0] & 0x40) - !(data[0] & 0x10));
411
412 for (j = 0; j < 8; j++)
413 input_report_key(dev + i, gc_psx_btn[j], ~data[1] & (1 << j));
414
415 input_report_key(dev + i, BTN_START, ~data[0] & 0x08);
416 input_report_key(dev + i, BTN_SELECT, ~data[0] & 0x01);
417
418 break;
419
420 case GC_PSX_NORMAL:
421
422 input_report_abs(dev + i, ABS_X, 128 + !(data[0] & 0x20) * 127 - !(data[0] & 0x80) * 128);
423 input_report_abs(dev + i, ABS_Y, 128 + !(data[0] & 0x40) * 127 - !(data[0] & 0x10) * 128);
424
425 for (j = 0; j < 8; j++)
426 input_report_key(dev + i, gc_psx_btn[j], ~data[1] & (1 << j));
427
428 input_report_key(dev + i, BTN_START, ~data[0] & 0x08);
429 input_report_key(dev + i, BTN_SELECT, ~data[0] & 0x01);
430
431 break;
432 }
433 }
434
435 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
436}
437
438static int gc_open(struct input_dev *dev)
439{
440 struct gc *gc = dev->private;
441 if (!gc->used++) {
442 parport_claim(gc->pd);
443 parport_write_control(gc->pd->port, 0x04);
444 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
445 }
446 return 0;
447}
448
449static void gc_close(struct input_dev *dev)
450{
451 struct gc *gc = dev->private;
452 if (!--gc->used) {
453 del_timer(&gc->timer);
454 parport_write_control(gc->pd->port, 0x00);
455 parport_release(gc->pd);
456 }
457}
458
459static struct gc __init *gc_probe(int *config)
460{
461 struct gc *gc;
462 struct parport *pp;
463 int i, j, psx;
464 unsigned char data[32];
465
466 if (config[0] < 0)
467 return NULL;
468
469 for (pp = parport_enumerate(); pp && (config[0] > 0); pp = pp->next)
470 config[0]--;
471
472 if (!pp) {
473 printk(KERN_ERR "gamecon.c: no such parport\n");
474 return NULL;
475 }
476
477 if (!(gc = kmalloc(sizeof(struct gc), GFP_KERNEL)))
478 return NULL;
479 memset(gc, 0, sizeof(struct gc));
480
481 gc->pd = parport_register_device(pp, "gamecon", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
482
483 if (!gc->pd) {
484 printk(KERN_ERR "gamecon.c: parport busy already - lp.o loaded?\n");
485 kfree(gc);
486 return NULL;
487 }
488
489 parport_claim(gc->pd);
490
491 init_timer(&gc->timer);
492 gc->timer.data = (long) gc;
493 gc->timer.function = gc_timer;
494
495 for (i = 0; i < 5; i++) {
496
497 if (!config[i + 1])
498 continue;
499
500 if (config[i + 1] < 1 || config[i + 1] > GC_MAX) {
501 printk(KERN_WARNING "gamecon.c: Pad type %d unknown\n", config[i + 1]);
502 continue;
503 }
504
505 gc->dev[i].private = gc;
506 gc->dev[i].open = gc_open;
507 gc->dev[i].close = gc_close;
508
509 gc->dev[i].evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
510
511 for (j = 0; j < 2; j++) {
512 set_bit(ABS_X + j, gc->dev[i].absbit);
513 gc->dev[i].absmin[ABS_X + j] = -1;
514 gc->dev[i].absmax[ABS_X + j] = 1;
515 }
516
517 gc->pads[0] |= gc_status_bit[i];
518 gc->pads[config[i + 1]] |= gc_status_bit[i];
519
520 switch(config[i + 1]) {
521
522 case GC_N64:
523 for (j = 0; j < 10; j++)
524 set_bit(gc_n64_btn[j], gc->dev[i].keybit);
525
526 for (j = 0; j < 2; j++) {
527 set_bit(ABS_X + j, gc->dev[i].absbit);
528 gc->dev[i].absmin[ABS_X + j] = -127;
529 gc->dev[i].absmax[ABS_X + j] = 126;
530 gc->dev[i].absflat[ABS_X + j] = 2;
531 set_bit(ABS_HAT0X + j, gc->dev[i].absbit);
532 gc->dev[i].absmin[ABS_HAT0X + j] = -1;
533 gc->dev[i].absmax[ABS_HAT0X + j] = 1;
534 }
535
536 break;
537
538 case GC_SNES:
539 for (j = 4; j < 8; j++)
540 set_bit(gc_snes_btn[j], gc->dev[i].keybit);
541 case GC_NES:
542 for (j = 0; j < 4; j++)
543 set_bit(gc_snes_btn[j], gc->dev[i].keybit);
544 break;
545
546 case GC_MULTI2:
547 set_bit(BTN_THUMB, gc->dev[i].keybit);
548 case GC_MULTI:
549 set_bit(BTN_TRIGGER, gc->dev[i].keybit);
550 break;
551
552 case GC_PSX:
553
554 psx = gc_psx_read_packet(gc, data);
555
556 switch(psx) {
557 case GC_PSX_NEGCON:
558 case GC_PSX_NORMAL:
559 case GC_PSX_ANALOG:
560 case GC_PSX_RUMBLE:
561
562 for (j = 0; j < 6; j++) {
563 psx = gc_psx_abs[j];
564 set_bit(psx, gc->dev[i].absbit);
565 if (j < 4) {
566 gc->dev[i].absmin[psx] = 4;
567 gc->dev[i].absmax[psx] = 252;
568 gc->dev[i].absflat[psx] = 2;
569 } else {
570 gc->dev[i].absmin[psx] = -1;
571 gc->dev[i].absmax[psx] = 1;
572 }
573 }
574
575 for (j = 0; j < 12; j++)
576 set_bit(gc_psx_btn[j], gc->dev[i].keybit);
577
578 break;
579
580 case 0:
581 gc->pads[GC_PSX] &= ~gc_status_bit[i];
582 printk(KERN_ERR "gamecon.c: No PSX controller found.\n");
583 break;
584
585 default:
586 gc->pads[GC_PSX] &= ~gc_status_bit[i];
587 printk(KERN_WARNING "gamecon.c: Unsupported PSX controller %#x,"
588 " please report to <vojtech@suse.cz>.\n", psx);
589 }
590 break;
591 }
592
593 gc->dev[i].name = gc_names[config[i + 1]];
594 gc->dev[i].idbus = BUS_PARPORT;
595 gc->dev[i].idvendor = 0x0001;
596 gc->dev[i].idproduct = config[i + 1];
597 gc->dev[i].idversion = 0x0100;
598 }
599
600 parport_release(gc->pd);
601
602 if (!gc->pads[0]) {
603 parport_unregister_device(gc->pd);
604 kfree(gc);
605 return NULL;
606 }
607
608 for (i = 0; i < 5; i++)
609 if (gc->pads[0] & gc_status_bit[i]) {
610 input_register_device(gc->dev + i);
611 printk(KERN_INFO "input%d: %s on %s\n", gc->dev[i].number, gc->dev[i].name, gc->pd->port->name);
612 }
613
614 return gc;
615}
616
617#ifndef MODULE
618int __init gc_setup(char *str)
619{
620 int i, ints[7];
621 get_options(str, ARRAY_SIZE(ints), ints);
622 for (i = 0; i <= ints[0] && i < 6; i++) gc[i] = ints[i + 1];
623 return 1;
624}
625int __init gc_setup_2(char *str)
626{
627 int i, ints[7];
628 get_options(str, ARRAY_SIZE(ints), ints);
629 for (i = 0; i <= ints[0] && i < 6; i++) gc_2[i] = ints[i + 1];
630 return 1;
631}
632int __init gc_setup_3(char *str)
633{
634 int i, ints[7];
635 get_options(str, ARRAY_SIZE(ints), ints);
636 for (i = 0; i <= ints[0] && i < 6; i++) gc_3[i] = ints[i + 1];
637 return 1;
638}
639__setup("gc=", gc_setup);
640__setup("gc_2=", gc_setup_2);
641__setup("gc_3=", gc_setup_3);
642#endif
643
644int __init gc_init(void)
645{
646 gc_base[0] = gc_probe(gc);
647 gc_base[1] = gc_probe(gc_2);
648 gc_base[2] = gc_probe(gc_3);
649
650 if (gc_base[0] || gc_base[1] || gc_base[2])
651 return 0;
652
653 return -ENODEV;
654}
655
656void __exit gc_exit(void)
657{
658 int i, j;
659
660 for (i = 0; i < 3; i++)
661 if (gc_base[i]) {
662 for (j = 0; j < 5; j++)
663 if (gc_base[i]->pads[0] & gc_status_bit[j])
664 input_unregister_device(gc_base[i]->dev + j);
665 parport_unregister_device(gc_base[i]->pd);
666 }
667}
668
669module_init(gc_init);
670module_exit(gc_exit);
671