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