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