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