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