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