33 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35 #include <linux/kernel.h>
37 #include <linux/module.h>
39 #include <linux/parport.h>
40 #include <linux/input.h>
42 #include <linux/slab.h>
48 #define GC_MAX_PORTS 3
49 #define GC_MAX_DEVICES 5
59 MODULE_PARM_DESC(
map,
"Describes first set of devices (<parport#>,<pad1>,<pad2>,..<pad5>)");
81 #define GC_REFRESH_TIME HZ/100
84 struct input_dev *
dev;
102 static struct gc *gc_base[3];
104 static const int gc_status_bit[] = { 0x40, 0x80, 0x20, 0x10, 0x08 };
106 static const char *gc_names[] = {
107 NULL,
"SNES pad",
"NES pad",
"NES FourPort",
"Multisystem joystick",
108 "Multisystem 2-button joystick",
"N64 controller",
"PSX controller",
109 "PSX DDR controller",
"SNES mouse"
116 static const unsigned char gc_n64_bytes[] = { 0, 1, 13, 15, 14, 12, 10, 11, 2, 3 };
117 static const short gc_n64_btn[] = {
122 #define GC_N64_LENGTH 32
123 #define GC_N64_STOP_LENGTH 5
124 #define GC_N64_CMD_00 0x11111111UL
125 #define GC_N64_CMD_01 0xd1111111UL
126 #define GC_N64_CMD_03 0xdd111111UL
127 #define GC_N64_CMD_1b 0xdd1dd111UL
128 #define GC_N64_CMD_c0 0x111111ddUL
129 #define GC_N64_CMD_80 0x1111111dUL
130 #define GC_N64_STOP_BIT 0x1d
131 #define GC_N64_REQUEST_DATA GC_N64_CMD_01
132 #define GC_N64_DELAY 133
135 #define GC_N64_POWER_W 0xe2
136 #define GC_N64_POWER_R 0xfd
137 #define GC_N64_OUT 0x1d
141 #define GC_N64_CLOCK 0x02
148 static void gc_n64_send_command(
struct gc *
gc,
unsigned long cmd,
155 unsigned char data = (cmd >>
i) & 1 ? target : 0;
162 static void gc_n64_send_stop_bit(
struct gc *
gc,
unsigned char target)
180 static void gc_n64_read_packet(
struct gc *gc,
unsigned char *
data)
220 static void gc_n64_process_packet(
struct gc *gc)
223 struct input_dev *
dev;
227 gc_n64_read_packet(gc, data);
234 dev = gc->
pads[
i].dev;
235 s = gc_status_bit[
i];
237 if (s & ~(data[8] | data[9])) {
241 for (j = 0; j < 8; j++) {
242 if (data[23 - j] & s)
244 if (data[31 - j] & s)
248 input_report_abs(dev,
ABS_X, x);
249 input_report_abs(dev,
ABS_Y, -y);
252 !(s & data[6]) - !(s & data[7]));
254 !(s & data[4]) - !(s & data[5]));
256 for (j = 0; j < 10; j++)
257 input_report_key(dev, gc_n64_btn[j],
258 s & data[gc_n64_bytes[j]]);
265 static int gc_n64_play_effect(
struct input_dev *dev,
void *data,
270 struct gc *gc = input_get_drvdata(dev);
272 unsigned char target = 1 << sdev->
idx;
286 for (i = 0; i < 32; i++)
288 gc_n64_send_stop_bit(gc, target);
296 for (i = 0; i < 32; i++)
297 gc_n64_send_command(gc, cmd, target);
298 gc_n64_send_stop_bit(gc, target);
307 static int __init gc_n64_init_ff(
struct input_dev *dev,
int i)
333 #define GC_NES_DELAY 6
334 #define GC_NES_LENGTH 8
335 #define GC_SNES_LENGTH 12
337 #define GC_SNESMOUSE_LENGTH 32
340 #define GC_NES_POWER 0xfc
341 #define GC_NES_CLOCK 0x01
342 #define GC_NES_LATCH 0x02
344 static const unsigned char gc_nes_bytes[] = { 0, 1, 2, 3 };
345 static const unsigned char gc_snes_bytes[] = { 8, 0, 2, 3, 9, 1, 10, 11 };
346 static const short gc_snes_btn[] = {
356 static void gc_nes_read_packet(
struct gc *gc,
int length,
unsigned char *data)
364 for (i = 0; i <
length; i++) {
373 static void gc_nes_process_packet(
struct gc *gc)
377 struct input_dev *
dev;
384 gc_nes_read_packet(gc, len, data);
390 s = gc_status_bit[
i];
396 input_report_abs(dev,
ABS_X, !(s & data[6]) - !(s & data[7]));
397 input_report_abs(dev,
ABS_Y, !(s & data[4]) - !(s & data[5]));
399 for (j = 0; j < 4; j++)
400 input_report_key(dev, gc_snes_btn[j],
401 s & data[gc_nes_bytes[j]]);
407 input_report_abs(dev,
ABS_X, !(s & data[6]) - !(s & data[7]));
408 input_report_abs(dev,
ABS_Y, !(s & data[4]) - !(s & data[5]));
410 for (j = 0; j < 8; j++)
411 input_report_key(dev, gc_snes_btn[j],
412 s & data[gc_snes_bytes[j]]);
426 if (!(s & data[12]) && !(s & data[13]) &&
427 !(s & data[14]) && (s & data[15])) {
428 input_report_key(dev,
BTN_LEFT, s & data[9]);
429 input_report_key(dev,
BTN_RIGHT, s & data[8]);
432 for (j = 0; j < 7; j++) {
434 if (data[25 + j] & s)
438 if (data[17 + j] & s)
445 input_report_rel(dev,
REL_X, x_rel);
451 input_report_rel(dev,
REL_Y, y_rel);
468 #define GC_MULTI_LENGTH 5
469 #define GC_MULTI2_LENGTH 6
475 static void gc_multi_read_packet(
struct gc *gc,
int length,
unsigned char *data)
479 for (i = 0; i <
length; i++) {
485 static void gc_multi_process_packet(
struct gc *gc)
490 struct input_dev *
dev;
493 gc_multi_read_packet(gc, data_len, data);
498 s = gc_status_bit[
i];
502 input_report_key(dev,
BTN_THUMB, s & data[5]);
506 input_report_abs(dev,
ABS_X,
507 !(s & data[2]) - !(s & data[3]));
508 input_report_abs(dev,
ABS_Y,
509 !(s & data[0]) - !(s & data[1]));
529 #define GC_PSX_DELAY 25
530 #define GC_PSX_LENGTH 8
531 #define GC_PSX_BYTES 6
533 #define GC_PSX_MOUSE 1
534 #define GC_PSX_NEGCON 2
535 #define GC_PSX_NORMAL 4
536 #define GC_PSX_ANALOG 5
537 #define GC_PSX_RUMBLE 7
539 #define GC_PSX_CLOCK 0x04
540 #define GC_PSX_COMMAND 0x01
541 #define GC_PSX_POWER 0xf8
542 #define GC_PSX_SELECT 0x02
544 #define GC_PSX_ID(x) ((x) >> 4)
545 #define GC_PSX_LEN(x) (((x) & 0xf) << 1)
549 MODULE_PARM_DESC(psx_delay,
"Delay when accessing Sony PSX controller (usecs)");
551 static const short gc_psx_abs[] = {
554 static const short gc_psx_btn[] = {
565 static void gc_psx_command(
struct gc *gc,
int b,
unsigned char *data)
570 memset(data, 0, GC_MAX_DEVICES);
572 for (i = 0; i < GC_PSX_LENGTH; i++, b >>= 1) {
583 data[
j] |= (read & gc_status_bit[
j]) ? (1 << i) : 0;
596 static void gc_psx_read_packet(
struct gc *gc,
598 unsigned char id[GC_MAX_DEVICES])
600 int i,
j, max_len = 0;
613 gc_psx_command(gc, 0x01, data2);
614 gc_psx_command(gc, 0x42,
id);
615 gc_psx_command(gc, 0, data2);
629 for (i = 0; i < max_len; i++) {
630 gc_psx_command(gc, 0, data2);
632 data[j][i] = data2[j];
644 static void gc_psx_report_one(
struct gc_pad *pad,
unsigned char psx_type,
647 struct input_dev *dev = pad->
dev;
654 input_report_key(dev,
BTN_THUMBL, ~data[0] & 0x04);
655 input_report_key(dev,
BTN_THUMBR, ~data[0] & 0x02);
661 for (i = 0; i < 4; i++)
662 input_report_key(dev, gc_psx_ddr_btn[i],
663 ~data[0] & (0x10 << i));
665 for (i = 0; i < 4; i++)
666 input_report_abs(dev, gc_psx_abs[i + 2],
669 input_report_abs(dev,
ABS_X,
670 !!(data[0] & 0x80) * 128 + !(data[0] & 0x20) * 127);
671 input_report_abs(dev,
ABS_Y,
672 !!(data[0] & 0x10) * 128 + !(data[0] & 0x40) * 127);
675 for (i = 0; i < 8; i++)
676 input_report_key(dev, gc_psx_btn[i], ~data[1] & (1 << i));
678 input_report_key(dev,
BTN_START, ~data[0] & 0x08);
679 input_report_key(dev,
BTN_SELECT, ~data[0] & 0x01);
688 for (i = 0; i < 4; i++)
689 input_report_key(dev, gc_psx_ddr_btn[i],
690 ~data[0] & (0x10 << i));
692 input_report_abs(dev,
ABS_X,
693 !!(data[0] & 0x80) * 128 + !(data[0] & 0x20) * 127);
694 input_report_abs(dev,
ABS_Y,
695 !!(data[0] & 0x10) * 128 + !(data[0] & 0x40) * 127);
709 for (i = 0; i < 8; i++)
710 input_report_key(dev, gc_psx_btn[i], ~data[1] & (1 << i));
712 input_report_key(dev,
BTN_START, ~data[0] & 0x08);
713 input_report_key(dev,
BTN_SELECT, ~data[0] & 0x01);
724 static void gc_psx_process_packet(
struct gc *gc)
731 gc_psx_read_packet(gc, data,
id);
736 gc_psx_report_one(pad,
id[i], data[i]);
744 static void gc_timer(
unsigned long private)
746 struct gc *gc = (
void *)
private;
753 gc_n64_process_packet(gc);
762 gc_nes_process_packet(gc);
770 gc_multi_process_packet(gc);
777 gc_psx_process_packet(gc);
782 static int gc_open(
struct input_dev *dev)
784 struct gc *gc = input_get_drvdata(dev);
801 static void gc_close(
struct input_dev *dev)
803 struct gc *gc = input_get_drvdata(dev);
817 struct input_dev *input_dev;
821 if (pad_type < 1 || pad_type >=
GC_MAX) {
822 pr_err(
"Pad type %d unknown\n", pad_type);
826 pad->
dev = input_dev = input_allocate_device();
828 pr_err(
"Not enough memory for input device\n");
835 "%s/input%d", gc->
pd->port->name, idx);
837 input_dev->name = gc_names[
pad_type];
838 input_dev->phys = pad->
phys;
840 input_dev->id.vendor = 0x0001;
842 input_dev->id.version = 0x0100;
844 input_set_drvdata(input_dev, gc);
846 input_dev->open = gc_open;
847 input_dev->close = gc_close;
852 for (i = 0; i < 2; i++)
853 input_set_abs_params(input_dev,
ABS_X + i, -1, 1, 0, 0);
862 for (i = 0; i < 10; i++)
863 __set_bit(gc_n64_btn[i], input_dev->keybit);
865 for (i = 0; i < 2; i++) {
866 input_set_abs_params(input_dev,
ABS_X + i, -127, 126, 0, 2);
867 input_set_abs_params(input_dev,
ABS_HAT0X + i, -1, 1, 0, 0);
870 err = gc_n64_init_ff(input_dev, idx);
872 pr_warning(
"Failed to initiate rumble for N64 device %d\n", idx);
886 for (i = 4; i < 8; i++)
887 __set_bit(gc_snes_btn[i], input_dev->keybit);
889 for (i = 0; i < 4; i++)
890 __set_bit(gc_snes_btn[i], input_dev->keybit);
900 for (i = 0; i < 6; i++)
901 input_set_abs_params(input_dev,
902 gc_psx_abs[i], 4, 252, 0, 2);
903 for (i = 0; i < 12; i++)
904 __set_bit(gc_psx_btn[i], input_dev->keybit);
909 for (i = 0; i < 4; i++)
910 __set_bit(gc_psx_ddr_btn[i], input_dev->keybit);
911 for (i = 0; i < 12; i++)
912 __set_bit(gc_psx_btn[i], input_dev->keybit);
917 err = input_register_device(pad->
dev);
924 input_free_device(pad->
dev);
940 pr_err(
"no such parport %d\n", parport);
947 pr_err(
"parport busy already - lp.o loaded?\n");
954 pr_err(
"Not enough memory\n");
956 goto err_unreg_pardev;
967 err = gc_setup_pad(gc, i, pads[i]);
975 pr_err(
"No valid devices specified\n");
986 input_unregister_device(gc->
pads[i].dev);
997 static void gc_remove(
struct gc *gc)
1002 if (gc->
pads[i].dev)
1003 input_unregister_device(gc->
pads[i].dev);
1008 static int __init gc_init(
void)
1015 if (gc_cfg[i].nargs == 0 || gc_cfg[i].args[0] < 0)
1018 if (gc_cfg[i].nargs < 2) {
1019 pr_err(
"at least one device must be specified\n");
1024 gc_base[
i] = gc_probe(gc_cfg[i].args[0],
1025 gc_cfg[i].args + 1, gc_cfg[i].nargs - 1);
1026 if (IS_ERR(gc_base[i])) {
1027 err = PTR_ERR(gc_base[i]);
1037 gc_remove(gc_base[i]);
1041 return have_dev ? 0 : -
ENODEV;
1044 static void __exit gc_exit(
void)
1050 gc_remove(gc_base[i]);