1 /* 2 * NES, SNES, N64, MultiSystem, PSX gamepad driver for Linux 3 * 4 * Copyright (c) 1999-2004 Vojtech Pavlik <vojtech@suse.cz> 5 * Copyright (c) 2004 Peter Nelson <rufus-kernel@hackish.org> 6 * 7 * Based on the work of: 8 * Andree Borrmann John Dahlstrom 9 * David Kuder Nathan Hand 10 */ 11 12 /* 13 * This program is free software; you can redistribute it and/or modify 14 * it under the terms of the GNU General Public License as published by 15 * the Free Software Foundation; either version 2 of the License, or 16 * (at your option) any later version. 17 * 18 * This program is distributed in the hope that it will be useful, 19 * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 * GNU General Public License for more details. 22 * 23 * You should have received a copy of the GNU General Public License 24 * along with this program; if not, write to the Free Software 25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 26 * 27 * Should you need to contact me, the author, you can do so either by 28 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail: 29 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic 30 */ 31 32 #include <linux/kernel.h> 33 #include <linux/delay.h> 34 #include <linux/module.h> 35 #include <linux/moduleparam.h> 36 #include <linux/init.h> 37 #include <linux/parport.h> 38 #include <linux/input.h> 39 40 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); 41 MODULE_DESCRIPTION("NES, SNES, N64, MultiSystem, PSX gamepad driver"); 42 MODULE_LICENSE("GPL"); 43 44 #define GC_MAX_PORTS 3 45 #define GC_MAX_DEVICES 5 46 47 struct gc_config { 48 int args[GC_MAX_DEVICES + 1]; 49 int nargs; 50 }; 51 52 static struct gc_config gc[GC_MAX_PORTS] __initdata; 53 54 module_param_array_named(map, gc[0].args, int, &gc[0].nargs, 0); 55 MODULE_PARM_DESC(map, "Describes first set of devices (<parport#>,<pad1>,<pad2>,..<pad5>)"); 56 module_param_array_named(map2, gc[1].args, int, &gc[1].nargs, 0); 57 MODULE_PARM_DESC(map2, "Describes second set of devices"); 58 module_param_array_named(map3, gc[2].args, int, &gc[2].nargs, 0); 59 MODULE_PARM_DESC(map3, "Describes third set of devices"); 60 61 __obsolete_setup("gc="); 62 __obsolete_setup("gc_2="); 63 __obsolete_setup("gc_3="); 64 65 /* see also gs_psx_delay parameter in PSX support section */ 66 67 #define GC_SNES 1 68 #define GC_NES 2 69 #define GC_NES4 3 70 #define GC_MULTI 4 71 #define GC_MULTI2 5 72 #define GC_N64 6 73 #define GC_PSX 7 74 #define GC_DDR 8 75 76 #define GC_MAX 8 77 78 #define GC_REFRESH_TIME HZ/100 79 80 struct gc { 81 struct pardevice *pd; 82 struct input_dev *dev[GC_MAX_DEVICES]; 83 struct timer_list timer; 84 unsigned char pads[GC_MAX + 1]; 85 int used; 86 struct semaphore sem; 87 char phys[GC_MAX_DEVICES][32]; 88 }; 89 90 static struct gc *gc_base[3]; 91 92 static int gc_status_bit[] = { 0x40, 0x80, 0x20, 0x10, 0x08 }; 93 94 static char *gc_names[] = { NULL, "SNES pad", "NES pad", "NES FourPort", "Multisystem joystick", 95 "Multisystem 2-button joystick", "N64 controller", "PSX controller", 96 "PSX DDR controller" }; 97 /* 98 * N64 support. 99 */ 100 101 static unsigned char gc_n64_bytes[] = { 0, 1, 13, 15, 14, 12, 10, 11, 2, 3 }; 102 static short gc_n64_btn[] = { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_TRIGGER, BTN_START }; 103 104 #define GC_N64_LENGTH 32 /* N64 bit length, not including stop bit */ 105 #define GC_N64_REQUEST_LENGTH 37 /* transmit request sequence is 9 bits long */ 106 #define GC_N64_DELAY 133 /* delay between transmit request, and response ready (us) */ 107 #define GC_N64_REQUEST 0x1dd1111111ULL /* the request data command (encoded for 000000011) */ 108 #define GC_N64_DWS 3 /* delay between write segments (required for sound playback because of ISA DMA) */ 109 /* GC_N64_DWS > 24 is known to fail */ 110 #define GC_N64_POWER_W 0xe2 /* power during write (transmit request) */ 111 #define GC_N64_POWER_R 0xfd /* power during read */ 112 #define GC_N64_OUT 0x1d /* output bits to the 4 pads */ 113 /* Reading the main axes of any N64 pad is known to fail if the corresponding bit */ 114 /* in GC_N64_OUT is pulled low on the output port (by any routine) for more */ 115 /* than 123 us */ 116 #define GC_N64_CLOCK 0x02 /* clock bits for read */ 117 118 /* 119 * gc_n64_read_packet() reads an N64 packet. 120 * Each pad uses one bit per byte. So all pads connected to this port are read in parallel. 121 */ 122 123 static void gc_n64_read_packet(struct gc *gc, unsigned char *data) 124 { 125 int i; 126 unsigned long flags; 127 128 /* 129 * Request the pad to transmit data 130 */ 131 132 local_irq_save(flags); 133 for (i = 0; i < GC_N64_REQUEST_LENGTH; i++) { 134 parport_write_data(gc->pd->port, GC_N64_POWER_W | ((GC_N64_REQUEST >> i) & 1 ? GC_N64_OUT : 0)); 135 udelay(GC_N64_DWS); 136 } 137 local_irq_restore(flags); 138 139 /* 140 * Wait for the pad response to be loaded into the 33-bit register of the adapter 141 */ 142 143 udelay(GC_N64_DELAY); 144 145 /* 146 * Grab data (ignoring the last bit, which is a stop bit) 147 */ 148 149 for (i = 0; i < GC_N64_LENGTH; i++) { 150 parport_write_data(gc->pd->port, GC_N64_POWER_R); 151 data[i] = parport_read_status(gc->pd->port); 152 parport_write_data(gc->pd->port, GC_N64_POWER_R | GC_N64_CLOCK); 153 } 154 155 /* 156 * We must wait 200 ms here for the controller to reinitialize before the next read request. 157 * No worries as long as gc_read is polled less frequently than this. 158 */ 159 160 } 161 162 static void gc_n64_process_packet(struct gc *gc) 163 { 164 unsigned char data[GC_N64_LENGTH]; 165 signed char axes[2]; 166 struct input_dev *dev; 167 int i, j, s; 168 169 gc_n64_read_packet(gc, data); 170 171 for (i = 0; i < GC_MAX_DEVICES; i++) { 172 173 dev = gc->dev[i]; 174 if (!dev) 175 continue; 176 177 s = gc_status_bit[i]; 178 179 if (s & gc->pads[GC_N64] & ~(data[8] | data[9])) { 180 181 axes[0] = axes[1] = 0; 182 183 for (j = 0; j < 8; j++) { 184 if (data[23 - j] & s) 185 axes[0] |= 1 << j; 186 if (data[31 - j] & s) 187 axes[1] |= 1 << j; 188 } 189 190 input_report_abs(dev, ABS_X, axes[0]); 191 input_report_abs(dev, ABS_Y, -axes[1]); 192 193 input_report_abs(dev, ABS_HAT0X, !(s & data[6]) - !(s & data[7])); 194 input_report_abs(dev, ABS_HAT0Y, !(s & data[4]) - !(s & data[5])); 195 196 for (j = 0; j < 10; j++) 197 input_report_key(dev, gc_n64_btn[j], s & data[gc_n64_bytes[j]]); 198 199 input_sync(dev); 200 } 201 } 202 } 203 204 /* 205 * NES/SNES support. 206 */ 207 208 #define GC_NES_DELAY 6 /* Delay between bits - 6us */ 209 #define GC_NES_LENGTH 8 /* The NES pads use 8 bits of data */ 210 #define GC_SNES_LENGTH 12 /* The SNES true length is 16, but the last 4 bits are unused */ 211 212 #define GC_NES_POWER 0xfc 213 #define GC_NES_CLOCK 0x01 214 #define GC_NES_LATCH 0x02 215 216 static unsigned char gc_nes_bytes[] = { 0, 1, 2, 3 }; 217 static unsigned char gc_snes_bytes[] = { 8, 0, 2, 3, 9, 1, 10, 11 }; 218 static short gc_snes_btn[] = { BTN_A, BTN_B, BTN_SELECT, BTN_START, BTN_X, BTN_Y, BTN_TL, BTN_TR }; 219 220 /* 221 * gc_nes_read_packet() reads a NES/SNES packet. 222 * Each pad uses one bit per byte. So all pads connected to 223 * this port are read in parallel. 224 */ 225 226 static void gc_nes_read_packet(struct gc *gc, int length, unsigned char *data) 227 { 228 int i; 229 230 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK | GC_NES_LATCH); 231 udelay(GC_NES_DELAY * 2); 232 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK); 233 234 for (i = 0; i < length; i++) { 235 udelay(GC_NES_DELAY); 236 parport_write_data(gc->pd->port, GC_NES_POWER); 237 data[i] = parport_read_status(gc->pd->port) ^ 0x7f; 238 udelay(GC_NES_DELAY); 239 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK); 240 } 241 } 242 243 static void gc_nes_process_packet(struct gc *gc) 244 { 245 unsigned char data[GC_SNES_LENGTH]; 246 struct input_dev *dev; 247 int i, j, s; 248 249 gc_nes_read_packet(gc, gc->pads[GC_SNES] ? GC_SNES_LENGTH : GC_NES_LENGTH, data); 250 251 for (i = 0; i < GC_MAX_DEVICES; i++) { 252 253 dev = gc->dev[i]; 254 if (!dev) 255 continue; 256 257 s = gc_status_bit[i]; 258 259 if (s & (gc->pads[GC_NES] | gc->pads[GC_SNES])) { 260 input_report_abs(dev, ABS_X, !(s & data[6]) - !(s & data[7])); 261 input_report_abs(dev, ABS_Y, !(s & data[4]) - !(s & data[5])); 262 } 263 264 if (s & gc->pads[GC_NES]) 265 for (j = 0; j < 4; j++) 266 input_report_key(dev, gc_snes_btn[j], s & data[gc_nes_bytes[j]]); 267 268 if (s & gc->pads[GC_SNES]) 269 for (j = 0; j < 8; j++) 270 input_report_key(dev, gc_snes_btn[j], s & data[gc_snes_bytes[j]]); 271 272 input_sync(dev); 273 } 274 } 275 276 /* 277 * Multisystem joystick support 278 */ 279 280 #define GC_MULTI_LENGTH 5 /* Multi system joystick packet length is 5 */ 281 #define GC_MULTI2_LENGTH 6 /* One more bit for one more button */ 282 283 /* 284 * gc_multi_read_packet() reads a Multisystem joystick packet. 285 */ 286 287 static void gc_multi_read_packet(struct gc *gc, int length, unsigned char *data) 288 { 289 int i; 290 291 for (i = 0; i < length; i++) { 292 parport_write_data(gc->pd->port, ~(1 << i)); 293 data[i] = parport_read_status(gc->pd->port) ^ 0x7f; 294 } 295 } 296 297 static void gc_multi_process_packet(struct gc *gc) 298 { 299 unsigned char data[GC_MULTI2_LENGTH]; 300 struct input_dev *dev; 301 int i, s; 302 303 gc_multi_read_packet(gc, gc->pads[GC_MULTI2] ? GC_MULTI2_LENGTH : GC_MULTI_LENGTH, data); 304 305 for (i = 0; i < GC_MAX_DEVICES; i++) { 306 307 dev = gc->dev[i]; 308 if (!dev) 309 continue; 310 311 s = gc_status_bit[i]; 312 313 if (s & (gc->pads[GC_MULTI] | gc->pads[GC_MULTI2])) { 314 input_report_abs(dev, ABS_X, !(s & data[2]) - !(s & data[3])); 315 input_report_abs(dev, ABS_Y, !(s & data[0]) - !(s & data[1])); 316 input_report_key(dev, BTN_TRIGGER, s & data[4]); 317 } 318 319 if (s & gc->pads[GC_MULTI2]) 320 input_report_key(dev, BTN_THUMB, s & data[5]); 321 322 input_sync(dev); 323 } 324 } 325 326 /* 327 * PSX support 328 * 329 * See documentation at: 330 * http://www.dim.com/~mackys/psxmemcard/ps-eng2.txt 331 * http://www.gamesx.com/controldata/psxcont/psxcont.htm 332 * ftp://milano.usal.es/pablo/ 333 * 334 */ 335 336 #define GC_PSX_DELAY 25 /* 25 usec */ 337 #define GC_PSX_LENGTH 8 /* talk to the controller in bits */ 338 #define GC_PSX_BYTES 6 /* the maximum number of bytes to read off the controller */ 339 340 #define GC_PSX_MOUSE 1 /* Mouse */ 341 #define GC_PSX_NEGCON 2 /* NegCon */ 342 #define GC_PSX_NORMAL 4 /* Digital / Analog or Rumble in Digital mode */ 343 #define GC_PSX_ANALOG 5 /* Analog in Analog mode / Rumble in Green mode */ 344 #define GC_PSX_RUMBLE 7 /* Rumble in Red mode */ 345 346 #define GC_PSX_CLOCK 0x04 /* Pin 4 */ 347 #define GC_PSX_COMMAND 0x01 /* Pin 2 */ 348 #define GC_PSX_POWER 0xf8 /* Pins 5-9 */ 349 #define GC_PSX_SELECT 0x02 /* Pin 3 */ 350 351 #define GC_PSX_ID(x) ((x) >> 4) /* High nibble is device type */ 352 #define GC_PSX_LEN(x) (((x) & 0xf) << 1) /* Low nibble is length in bytes/2 */ 353 354 static int gc_psx_delay = GC_PSX_DELAY; 355 module_param_named(psx_delay, gc_psx_delay, uint, 0); 356 MODULE_PARM_DESC(psx_delay, "Delay when accessing Sony PSX controller (usecs)"); 357 358 __obsolete_setup("gc_psx_delay="); 359 360 static short gc_psx_abs[] = { ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_HAT0X, ABS_HAT0Y }; 361 static short gc_psx_btn[] = { BTN_TL, BTN_TR, BTN_TL2, BTN_TR2, BTN_A, BTN_B, BTN_X, BTN_Y, 362 BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR }; 363 static short gc_psx_ddr_btn[] = { BTN_0, BTN_1, BTN_2, BTN_3 }; 364 365 /* 366 * gc_psx_command() writes 8bit command and reads 8bit data from 367 * the psx pad. 368 */ 369 370 static void gc_psx_command(struct gc *gc, int b, unsigned char data[GC_MAX_DEVICES]) 371 { 372 int i, j, cmd, read; 373 374 for (i = 0; i < GC_MAX_DEVICES; i++) 375 data[i] = 0; 376 377 for (i = 0; i < GC_PSX_LENGTH; i++, b >>= 1) { 378 cmd = (b & 1) ? GC_PSX_COMMAND : 0; 379 parport_write_data(gc->pd->port, cmd | GC_PSX_POWER); 380 udelay(gc_psx_delay); 381 read = parport_read_status(gc->pd->port) ^ 0x80; 382 for (j = 0; j < GC_MAX_DEVICES; j++) 383 data[j] |= (read & gc_status_bit[j] & (gc->pads[GC_PSX] | gc->pads[GC_DDR])) ? (1 << i) : 0; 384 parport_write_data(gc->pd->port, cmd | GC_PSX_CLOCK | GC_PSX_POWER); 385 udelay(gc_psx_delay); 386 } 387 } 388 389 /* 390 * gc_psx_read_packet() reads a whole psx packet and returns 391 * device identifier code. 392 */ 393 394 static void gc_psx_read_packet(struct gc *gc, unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES], 395 unsigned char id[GC_MAX_DEVICES]) 396 { 397 int i, j, max_len = 0; 398 unsigned long flags; 399 unsigned char data2[GC_MAX_DEVICES]; 400 401 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER); /* Select pad */ 402 udelay(gc_psx_delay); 403 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_POWER); /* Deselect, begin command */ 404 udelay(gc_psx_delay); 405 406 local_irq_save(flags); 407 408 gc_psx_command(gc, 0x01, data2); /* Access pad */ 409 gc_psx_command(gc, 0x42, id); /* Get device ids */ 410 gc_psx_command(gc, 0, data2); /* Dump status */ 411 412 for (i =0; i < GC_MAX_DEVICES; i++) /* Find the longest pad */ 413 if((gc_status_bit[i] & (gc->pads[GC_PSX] | gc->pads[GC_DDR])) 414 && (GC_PSX_LEN(id[i]) > max_len) 415 && (GC_PSX_LEN(id[i]) <= GC_PSX_BYTES)) 416 max_len = GC_PSX_LEN(id[i]); 417 418 for (i = 0; i < max_len; i++) { /* Read in all the data */ 419 gc_psx_command(gc, 0, data2); 420 for (j = 0; j < GC_MAX_DEVICES; j++) 421 data[j][i] = data2[j]; 422 } 423 424 local_irq_restore(flags); 425 426 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER); 427 428 for(i = 0; i < GC_MAX_DEVICES; i++) /* Set id's to the real value */ 429 id[i] = GC_PSX_ID(id[i]); 430 } 431 432 static void gc_psx_process_packet(struct gc *gc) 433 { 434 unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES]; 435 unsigned char id[GC_MAX_DEVICES]; 436 struct input_dev *dev; 437 int i, j; 438 439 gc_psx_read_packet(gc, data, id); 440 441 for (i = 0; i < GC_MAX_DEVICES; i++) { 442 443 dev = gc->dev[i]; 444 if (!dev) 445 continue; 446 447 switch (id[i]) { 448 449 case GC_PSX_RUMBLE: 450 451 input_report_key(dev, BTN_THUMBL, ~data[i][0] & 0x04); 452 input_report_key(dev, BTN_THUMBR, ~data[i][0] & 0x02); 453 454 case GC_PSX_NEGCON: 455 case GC_PSX_ANALOG: 456 457 if (gc->pads[GC_DDR] & gc_status_bit[i]) { 458 for(j = 0; j < 4; j++) 459 input_report_key(dev, gc_psx_ddr_btn[j], ~data[i][0] & (0x10 << j)); 460 } else { 461 for (j = 0; j < 4; j++) 462 input_report_abs(dev, gc_psx_abs[j + 2], data[i][j + 2]); 463 464 input_report_abs(dev, ABS_X, 128 + !(data[i][0] & 0x20) * 127 - !(data[i][0] & 0x80) * 128); 465 input_report_abs(dev, ABS_Y, 128 + !(data[i][0] & 0x40) * 127 - !(data[i][0] & 0x10) * 128); 466 } 467 468 for (j = 0; j < 8; j++) 469 input_report_key(dev, gc_psx_btn[j], ~data[i][1] & (1 << j)); 470 471 input_report_key(dev, BTN_START, ~data[i][0] & 0x08); 472 input_report_key(dev, BTN_SELECT, ~data[i][0] & 0x01); 473 474 input_sync(dev); 475 476 break; 477 478 case GC_PSX_NORMAL: 479 if (gc->pads[GC_DDR] & gc_status_bit[i]) { 480 for(j = 0; j < 4; j++) 481 input_report_key(dev, gc_psx_ddr_btn[j], ~data[i][0] & (0x10 << j)); 482 } else { 483 input_report_abs(dev, ABS_X, 128 + !(data[i][0] & 0x20) * 127 - !(data[i][0] & 0x80) * 128); 484 input_report_abs(dev, ABS_Y, 128 + !(data[i][0] & 0x40) * 127 - !(data[i][0] & 0x10) * 128); 485 486 /* for some reason if the extra axes are left unset they drift */ 487 /* for (j = 0; j < 4; j++) 488 input_report_abs(dev, gc_psx_abs[j + 2], 128); 489 * This needs to be debugged properly, 490 * maybe fuzz processing needs to be done in input_sync() 491 * --vojtech 492 */ 493 } 494 495 for (j = 0; j < 8; j++) 496 input_report_key(dev, gc_psx_btn[j], ~data[i][1] & (1 << j)); 497 498 input_report_key(dev, BTN_START, ~data[i][0] & 0x08); 499 input_report_key(dev, BTN_SELECT, ~data[i][0] & 0x01); 500 501 input_sync(dev); 502 503 break; 504 505 case 0: /* not a pad, ignore */ 506 break; 507 } 508 } 509 } 510 511 /* 512 * gc_timer() initiates reads of console pads data. 513 */ 514 515 static void gc_timer(unsigned long private) 516 { 517 struct gc *gc = (void *) private; 518 519 /* 520 * N64 pads - must be read first, any read confuses them for 200 us 521 */ 522 523 if (gc->pads[GC_N64]) 524 gc_n64_process_packet(gc); 525 526 /* 527 * NES and SNES pads 528 */ 529 530 if (gc->pads[GC_NES] || gc->pads[GC_SNES]) 531 gc_nes_process_packet(gc); 532 533 /* 534 * Multi and Multi2 joysticks 535 */ 536 537 if (gc->pads[GC_MULTI] || gc->pads[GC_MULTI2]) 538 gc_multi_process_packet(gc); 539 540 /* 541 * PSX controllers 542 */ 543 544 if (gc->pads[GC_PSX] || gc->pads[GC_DDR]) 545 gc_psx_process_packet(gc); 546 547 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME); 548 } 549 550 static int gc_open(struct input_dev *dev) 551 { 552 struct gc *gc = dev->private; 553 int err; 554 555 err = down_interruptible(&gc->sem); 556 if (err) 557 return err; 558 559 if (!gc->used++) { 560 parport_claim(gc->pd); 561 parport_write_control(gc->pd->port, 0x04); 562 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME); 563 } 564 565 up(&gc->sem); 566 return 0; 567 } 568 569 static void gc_close(struct input_dev *dev) 570 { 571 struct gc *gc = dev->private; 572 573 down(&gc->sem); 574 if (!--gc->used) { 575 del_timer_sync(&gc->timer); 576 parport_write_control(gc->pd->port, 0x00); 577 parport_release(gc->pd); 578 } 579 up(&gc->sem); 580 } 581 582 static int __init gc_setup_pad(struct gc *gc, int idx, int pad_type) 583 { 584 struct input_dev *input_dev; 585 int i; 586 587 if (!pad_type) 588 return 0; 589 590 if (pad_type < 1 || pad_type > GC_MAX) { 591 printk(KERN_WARNING "gamecon.c: Pad type %d unknown\n", pad_type); 592 return -EINVAL; 593 } 594 595 gc->dev[idx] = input_dev = input_allocate_device(); 596 if (!input_dev) { 597 printk(KERN_ERR "gamecon.c: Not enough memory for input device\n"); 598 return -ENOMEM; 599 } 600 601 input_dev->name = gc_names[pad_type]; 602 input_dev->phys = gc->phys[idx]; 603 input_dev->id.bustype = BUS_PARPORT; 604 input_dev->id.vendor = 0x0001; 605 input_dev->id.product = pad_type; 606 input_dev->id.version = 0x0100; 607 input_dev->private = gc; 608 609 input_dev->open = gc_open; 610 input_dev->close = gc_close; 611 612 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS); 613 614 for (i = 0; i < 2; i++) 615 input_set_abs_params(input_dev, ABS_X + i, -1, 1, 0, 0); 616 617 gc->pads[0] |= gc_status_bit[idx]; 618 gc->pads[pad_type] |= gc_status_bit[idx]; 619 620 switch (pad_type) { 621 622 case GC_N64: 623 for (i = 0; i < 10; i++) 624 set_bit(gc_n64_btn[i], input_dev->keybit); 625 626 for (i = 0; i < 2; i++) { 627 input_set_abs_params(input_dev, ABS_X + i, -127, 126, 0, 2); 628 input_set_abs_params(input_dev, ABS_HAT0X + i, -1, 1, 0, 0); 629 } 630 631 break; 632 633 case GC_SNES: 634 for (i = 4; i < 8; i++) 635 set_bit(gc_snes_btn[i], input_dev->keybit); 636 case GC_NES: 637 for (i = 0; i < 4; i++) 638 set_bit(gc_snes_btn[i], input_dev->keybit); 639 break; 640 641 case GC_MULTI2: 642 set_bit(BTN_THUMB, input_dev->keybit); 643 case GC_MULTI: 644 set_bit(BTN_TRIGGER, input_dev->keybit); 645 break; 646 647 case GC_PSX: 648 for (i = 0; i < 6; i++) 649 input_set_abs_params(input_dev, gc_psx_abs[i], 4, 252, 0, 2); 650 for (i = 0; i < 12; i++) 651 set_bit(gc_psx_btn[i], input_dev->keybit); 652 653 break; 654 655 case GC_DDR: 656 for (i = 0; i < 4; i++) 657 set_bit(gc_psx_ddr_btn[i], input_dev->keybit); 658 for (i = 0; i < 12; i++) 659 set_bit(gc_psx_btn[i], input_dev->keybit); 660 661 break; 662 } 663 664 return 0; 665 } 666 667 static struct gc __init *gc_probe(int parport, int *pads, int n_pads) 668 { 669 struct gc *gc; 670 struct parport *pp; 671 struct pardevice *pd; 672 int i; 673 int err; 674 675 pp = parport_find_number(parport); 676 if (!pp) { 677 printk(KERN_ERR "gamecon.c: no such parport\n"); 678 err = -EINVAL; 679 goto err_out; 680 } 681 682 pd = parport_register_device(pp, "gamecon", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL); 683 if (!pd) { 684 printk(KERN_ERR "gamecon.c: parport busy already - lp.o loaded?\n"); 685 err = -EBUSY; 686 goto err_put_pp; 687 } 688 689 gc = kzalloc(sizeof(struct gc), GFP_KERNEL); 690 if (!gc) { 691 printk(KERN_ERR "gamecon.c: Not enough memory\n"); 692 err = -ENOMEM; 693 goto err_unreg_pardev; 694 } 695 696 init_MUTEX(&gc->sem); 697 gc->pd = pd; 698 init_timer(&gc->timer); 699 gc->timer.data = (long) gc; 700 gc->timer.function = gc_timer; 701 702 for (i = 0; i < n_pads && i < GC_MAX_DEVICES; i++) { 703 if (!pads[i]) 704 continue; 705 706 sprintf(gc->phys[i], "%s/input%d", gc->pd->port->name, i); 707 err = gc_setup_pad(gc, i, pads[i]); 708 if (err) 709 goto err_unreg_devs; 710 711 err = input_register_device(gc->dev[i]); 712 if (err) 713 goto err_free_dev; 714 } 715 716 if (!gc->pads[0]) { 717 printk(KERN_ERR "gamecon.c: No valid devices specified\n"); 718 err = -EINVAL; 719 goto err_free_gc; 720 } 721 722 parport_put_port(pp); 723 return gc; 724 725 err_free_dev: 726 input_free_device(gc->dev[i]); 727 err_unreg_devs: 728 while (--i >= 0) 729 if (gc->dev[i]) 730 input_unregister_device(gc->dev[i]); 731 err_free_gc: 732 kfree(gc); 733 err_unreg_pardev: 734 parport_unregister_device(pd); 735 err_put_pp: 736 parport_put_port(pp); 737 err_out: 738 return ERR_PTR(err); 739 } 740 741 static void gc_remove(struct gc *gc) 742 { 743 int i; 744 745 for (i = 0; i < GC_MAX_DEVICES; i++) 746 if (gc->dev[i]) 747 input_unregister_device(gc->dev[i]); 748 parport_unregister_device(gc->pd); 749 kfree(gc); 750 } 751 752 static int __init gc_init(void) 753 { 754 int i; 755 int have_dev = 0; 756 int err = 0; 757 758 for (i = 0; i < GC_MAX_PORTS; i++) { 759 if (gc[i].nargs == 0 || gc[i].args[0] < 0) 760 continue; 761 762 if (gc[i].nargs < 2) { 763 printk(KERN_ERR "gamecon.c: at least one device must be specified\n"); 764 err = -EINVAL; 765 break; 766 } 767 768 gc_base[i] = gc_probe(gc[i].args[0], gc[i].args + 1, gc[i].nargs - 1); 769 if (IS_ERR(gc_base[i])) { 770 err = PTR_ERR(gc_base[i]); 771 break; 772 } 773 774 have_dev = 1; 775 } 776 777 if (err) { 778 while (--i >= 0) 779 if (gc_base[i]) 780 gc_remove(gc_base[i]); 781 return err; 782 } 783 784 return have_dev ? 0 : -ENODEV; 785 } 786 787 static void __exit gc_exit(void) 788 { 789 int i; 790 791 for (i = 0; i < GC_MAX_PORTS; i++) 792 if (gc_base[i]) 793 gc_remove(gc_base[i]); 794 } 795 796 module_init(gc_init); 797 module_exit(gc_exit); 798