blob: ae998d99a5ae619b87d66bd8e20ebf5c1c2a5571 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NES, SNES, N64, MultiSystem, PSX gamepad driver for Linux
3 *
Dmitry Torokhovab0c3442005-05-29 02:28:55 -05004 * Copyright (c) 1999-2004 Vojtech Pavlik <vojtech@suse.cz>
5 * Copyright (c) 2004 Peter Nelson <rufus-kernel@hackish.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Based on the work of:
Dmitry Torokhovab0c3442005-05-29 02:28:55 -05008 * Andree Borrmann John Dahlstrom
9 * David Kuder Nathan Hand
Raphael Assenatb157d552006-04-02 00:10:05 -050010 * Raphael Assenat
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
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 Torokhova1e12742010-02-21 20:55:31 -080033#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/kernel.h>
36#include <linux/delay.h>
37#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/init.h>
39#include <linux/parport.h>
40#include <linux/input.h>
Ingo Molnar72ba9f02006-02-19 00:22:30 -050041#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
44MODULE_DESCRIPTION("NES, SNES, N64, MultiSystem, PSX gamepad driver");
45MODULE_LICENSE("GPL");
46
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050047#define GC_MAX_PORTS 3
48#define GC_MAX_DEVICES 5
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050050struct gc_config {
51 int args[GC_MAX_DEVICES + 1];
Dmitry Torokhov78167232007-05-03 00:52:51 -040052 unsigned int nargs;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050053};
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Dmitry Torokhov78167232007-05-03 00:52:51 -040055static struct gc_config gc_cfg[GC_MAX_PORTS] __initdata;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050056
Dmitry Torokhov78167232007-05-03 00:52:51 -040057module_param_array_named(map, gc_cfg[0].args, int, &gc_cfg[0].nargs, 0);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050058MODULE_PARM_DESC(map, "Describes first set of devices (<parport#>,<pad1>,<pad2>,..<pad5>)");
Dmitry Torokhov78167232007-05-03 00:52:51 -040059module_param_array_named(map2, gc_cfg[1].args, int, &gc_cfg[1].nargs, 0);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050060MODULE_PARM_DESC(map2, "Describes second set of devices");
Dmitry Torokhov78167232007-05-03 00:52:51 -040061module_param_array_named(map3, gc_cfg[2].args, int, &gc_cfg[2].nargs, 0);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050062MODULE_PARM_DESC(map3, "Describes third set of devices");
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/* see also gs_psx_delay parameter in PSX support section */
65
Dmitry Torokhov09951742010-02-21 20:54:54 -080066enum gc_type {
67 GC_NONE = 0,
68 GC_SNES,
69 GC_NES,
70 GC_NES4,
71 GC_MULTI,
72 GC_MULTI2,
73 GC_N64,
74 GC_PSX,
75 GC_DDR,
76 GC_SNESMOUSE,
77 GC_MAX
78};
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80#define GC_REFRESH_TIME HZ/100
81
Dmitry Torokhov09951742010-02-21 20:54:54 -080082struct gc_pad {
83 struct input_dev *dev;
84 enum gc_type type;
85 char phys[32];
86};
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088struct gc {
89 struct pardevice *pd;
Dmitry Torokhov09951742010-02-21 20:54:54 -080090 struct gc_pad pads[GC_MAX_DEVICES];
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050091 struct input_dev *dev[GC_MAX_DEVICES];
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 struct timer_list timer;
Dmitry Torokhov09951742010-02-21 20:54:54 -080093 int pad_count[GC_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 int used;
Ingo Molnar72ba9f02006-02-19 00:22:30 -050095 struct mutex mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096};
97
Scott Moreau7aa9e0e2010-02-21 20:53:55 -080098struct gc_subdev {
99 unsigned int idx;
100};
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static struct gc *gc_base[3];
103
Dmitry Torokhovaf930d62010-02-21 20:55:09 -0800104static const int gc_status_bit[] = { 0x40, 0x80, 0x20, 0x10, 0x08 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Dmitry Torokhovaf930d62010-02-21 20:55:09 -0800106static const char *gc_names[] = {
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800107 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 Torvalds1da177e2005-04-16 15:20:36 -0700112/*
113 * N64 support.
114 */
115
Dmitry Torokhovaf930d62010-02-21 20:55:09 -0800116static const unsigned char gc_n64_bytes[] = { 0, 1, 13, 15, 14, 12, 10, 11, 2, 3 };
117static const short gc_n64_btn[] = {
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800118 BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z,
119 BTN_TL, BTN_TR, BTN_TRIGGER, BTN_START
120};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122#define GC_N64_LENGTH 32 /* N64 bit length, not including stop bit */
Scott Moreau7aa9e0e2010-02-21 20:53:55 -0800123#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 Torvalds1da177e2005-04-16 15:20:36 -0700132#define GC_N64_DELAY 133 /* delay between transmit request, and response ready (us) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#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 Moreau7aa9e0e2010-02-21 20:53:55 -0800144 * Used for rumble code.
145 */
146
147/* Send encoded command */
148static 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 */
162static 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 Torvalds1da177e2005-04-16 15:20:36 -0700175 * gc_n64_read_packet() reads an N64 packet.
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800176 * Each pad uses one bit per byte. So all pads connected to this port
177 * are read in parallel.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 */
179
180static 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 Moreau7aa9e0e2010-02-21 20:53:55 -0800190 gc_n64_send_command(gc, GC_N64_REQUEST_DATA, GC_N64_OUT);
191 gc_n64_send_stop_bit(gc, GC_N64_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 local_irq_restore(flags);
193
194/*
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800195 * Wait for the pad response to be loaded into the 33-bit register
196 * of the adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 */
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 Moreau7aa9e0e2010-02-21 20:53:55 -0800207 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 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 Torokhovd38fcb92010-02-21 20:54:28 -0800213 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700216 */
217
218}
219
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500220static void gc_n64_process_packet(struct gc *gc)
221{
222 unsigned char data[GC_N64_LENGTH];
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500223 struct input_dev *dev;
224 int i, j, s;
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800225 signed char x, y;
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500226
227 gc_n64_read_packet(gc, data);
228
229 for (i = 0; i < GC_MAX_DEVICES; i++) {
230
Dmitry Torokhov09951742010-02-21 20:54:54 -0800231 if (gc->pads[i].type != GC_N64)
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500232 continue;
233
Dmitry Torokhov09951742010-02-21 20:54:54 -0800234 dev = gc->pads[i].dev;
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500235 s = gc_status_bit[i];
236
Dmitry Torokhov09951742010-02-21 20:54:54 -0800237 if (s & ~(data[8] | data[9])) {
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500238
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800239 x = y = 0;
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500240
241 for (j = 0; j < 8; j++) {
242 if (data[23 - j] & s)
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800243 x |= 1 << j;
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500244 if (data[31 - j] & s)
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800245 y |= 1 << j;
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500246 }
247
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800248 input_report_abs(dev, ABS_X, x);
249 input_report_abs(dev, ABS_Y, -y);
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500250
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800251 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 Torokhovc7fd0182006-01-29 21:52:04 -0500255
256 for (j = 0; j < 10; j++)
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800257 input_report_key(dev, gc_n64_btn[j],
258 s & data[gc_n64_bytes[j]]);
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500259
260 input_sync(dev);
261 }
262 }
263}
264
Scott Moreau7aa9e0e2010-02-21 20:53:55 -0800265static 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
307static 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 Torvalds1da177e2005-04-16 15:20:36 -0700329/*
330 * NES/SNES support.
331 */
332
Raphael Assenatb157d552006-04-02 00:10:05 -0500333#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 Torvalds1da177e2005-04-16 15:20:36 -0700339
340#define GC_NES_POWER 0xfc
341#define GC_NES_CLOCK 0x01
342#define GC_NES_LATCH 0x02
343
Dmitry Torokhovaf930d62010-02-21 20:55:09 -0800344static const unsigned char gc_nes_bytes[] = { 0, 1, 2, 3 };
345static const unsigned char gc_snes_bytes[] = { 8, 0, 2, 3, 9, 1, 10, 11 };
346static const short gc_snes_btn[] = {
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800347 BTN_A, BTN_B, BTN_SELECT, BTN_START, BTN_X, BTN_Y, BTN_TL, BTN_TR
348};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
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
356static 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 Torokhovc7fd0182006-01-29 21:52:04 -0500373static void gc_nes_process_packet(struct gc *gc)
374{
Raphael Assenatb157d552006-04-02 00:10:05 -0500375 unsigned char data[GC_SNESMOUSE_LENGTH];
Dmitry Torokhov09951742010-02-21 20:54:54 -0800376 struct gc_pad *pad;
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500377 struct input_dev *dev;
Raphael Assenatb157d552006-04-02 00:10:05 -0500378 int i, j, s, len;
379 char x_rel, y_rel;
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500380
Dmitry Torokhov09951742010-02-21 20:54:54 -0800381 len = gc->pad_count[GC_SNESMOUSE] ? GC_SNESMOUSE_LENGTH :
382 (gc->pad_count[GC_SNES] ? GC_SNES_LENGTH : GC_NES_LENGTH);
Raphael Assenatb157d552006-04-02 00:10:05 -0500383
384 gc_nes_read_packet(gc, len, data);
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500385
386 for (i = 0; i < GC_MAX_DEVICES; i++) {
387
Dmitry Torokhov09951742010-02-21 20:54:54 -0800388 pad = &gc->pads[i];
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500389 dev = gc->dev[i];
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500390 s = gc_status_bit[i];
391
Dmitry Torokhov09951742010-02-21 20:54:54 -0800392 switch (pad->type) {
393
394 case GC_NES:
395
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500396 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 Torokhovc7fd0182006-01-29 21:52:04 -0500398
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500399 for (j = 0; j < 4; j++)
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800400 input_report_key(dev, gc_snes_btn[j],
401 s & data[gc_nes_bytes[j]]);
Dmitry Torokhov09951742010-02-21 20:54:54 -0800402 input_sync(dev);
403 break;
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500404
Dmitry Torokhov09951742010-02-21 20:54:54 -0800405 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 Torokhovc7fd0182006-01-29 21:52:04 -0500410 for (j = 0; j < 8; j++)
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800411 input_report_key(dev, gc_snes_btn[j],
412 s & data[gc_snes_bytes[j]]);
Dmitry Torokhov09951742010-02-21 20:54:54 -0800413 input_sync(dev);
414 break;
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500415
Dmitry Torokhov09951742010-02-21 20:54:54 -0800416 case GC_SNESMOUSE:
Raphael Assenatb157d552006-04-02 00:10:05 -0500417 /*
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800418 * 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 Assenatb157d552006-04-02 00:10:05 -0500421 * 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 Torokhov09951742010-02-21 20:54:54 -0800453
454 input_sync(dev);
Raphael Assenatb157d552006-04-02 00:10:05 -0500455 }
Dmitry Torokhov09951742010-02-21 20:54:54 -0800456 break;
457
458 default:
459 break;
Raphael Assenatb157d552006-04-02 00:10:05 -0500460 }
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500461 }
462}
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464/*
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
475static 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 Torokhovc7fd0182006-01-29 21:52:04 -0500485static void gc_multi_process_packet(struct gc *gc)
486{
487 unsigned char data[GC_MULTI2_LENGTH];
Dmitry Torokhov09951742010-02-21 20:54:54 -0800488 int data_len = gc->pad_count[GC_MULTI2] ? GC_MULTI2_LENGTH : GC_MULTI_LENGTH;
489 struct gc_pad *pad;
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500490 struct input_dev *dev;
491 int i, s;
492
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800493 gc_multi_read_packet(gc, data_len, data);
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500494
495 for (i = 0; i < GC_MAX_DEVICES; i++) {
Dmitry Torokhov09951742010-02-21 20:54:54 -0800496 pad = &gc->pads[i];
497 dev = pad->dev;
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500498 s = gc_status_bit[i];
499
Dmitry Torokhov09951742010-02-21 20:54:54 -0800500 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 Torokhovd38fcb92010-02-21 20:54:28 -0800506 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 Torokhovc7fd0182006-01-29 21:52:04 -0500510 input_report_key(dev, BTN_TRIGGER, s & data[4]);
Dmitry Torokhov09951742010-02-21 20:54:54 -0800511 input_sync(dev);
512 break;
513
514 default:
515 break;
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500516 }
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500517 }
518}
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520/*
521 * PSX support
522 *
523 * See documentation at:
524 * http://www.dim.com/~mackys/psxmemcard/ps-eng2.txt
525 * http://www.gamesx.com/controldata/psxcont/psxcont.htm
526 * ftp://milano.usal.es/pablo/
527 *
528 */
529
530#define GC_PSX_DELAY 25 /* 25 usec */
531#define GC_PSX_LENGTH 8 /* talk to the controller in bits */
532#define GC_PSX_BYTES 6 /* the maximum number of bytes to read off the controller */
533
534#define GC_PSX_MOUSE 1 /* Mouse */
535#define GC_PSX_NEGCON 2 /* NegCon */
536#define GC_PSX_NORMAL 4 /* Digital / Analog or Rumble in Digital mode */
537#define GC_PSX_ANALOG 5 /* Analog in Analog mode / Rumble in Green mode */
538#define GC_PSX_RUMBLE 7 /* Rumble in Red mode */
539
540#define GC_PSX_CLOCK 0x04 /* Pin 4 */
541#define GC_PSX_COMMAND 0x01 /* Pin 2 */
542#define GC_PSX_POWER 0xf8 /* Pins 5-9 */
543#define GC_PSX_SELECT 0x02 /* Pin 3 */
544
545#define GC_PSX_ID(x) ((x) >> 4) /* High nibble is device type */
546#define GC_PSX_LEN(x) (((x) & 0xf) << 1) /* Low nibble is length in bytes/2 */
547
548static int gc_psx_delay = GC_PSX_DELAY;
549module_param_named(psx_delay, gc_psx_delay, uint, 0);
550MODULE_PARM_DESC(psx_delay, "Delay when accessing Sony PSX controller (usecs)");
551
Dmitry Torokhovaf930d62010-02-21 20:55:09 -0800552static const short gc_psx_abs[] = {
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800553 ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_HAT0X, ABS_HAT0Y
554};
Dmitry Torokhovaf930d62010-02-21 20:55:09 -0800555static const short gc_psx_btn[] = {
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800556 BTN_TL, BTN_TR, BTN_TL2, BTN_TR2, BTN_A, BTN_B, BTN_X, BTN_Y,
557 BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR
558};
Dmitry Torokhovaf930d62010-02-21 20:55:09 -0800559static const short gc_psx_ddr_btn[] = { BTN_0, BTN_1, BTN_2, BTN_3 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561/*
562 * gc_psx_command() writes 8bit command and reads 8bit data from
563 * the psx pad.
564 */
565
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800566static void gc_psx_command(struct gc *gc, int b, unsigned char *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800568 struct parport *port = gc->pd->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 int i, j, cmd, read;
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500570
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800571 memset(data, 0, GC_MAX_DEVICES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 for (i = 0; i < GC_PSX_LENGTH; i++, b >>= 1) {
574 cmd = (b & 1) ? GC_PSX_COMMAND : 0;
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800575 parport_write_data(port, cmd | GC_PSX_POWER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 udelay(gc_psx_delay);
Dmitry Torokhov09951742010-02-21 20:54:54 -0800577
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800578 read = parport_read_status(port) ^ 0x80;
Dmitry Torokhov09951742010-02-21 20:54:54 -0800579
580 for (j = 0; j < GC_MAX_DEVICES; j++) {
581 struct gc_pad *pad = &gc->pads[i];
582
583 if (pad->type == GC_PSX || pad->type == GC_DDR)
584 data[j] |= (read & gc_status_bit[j]) ? (1 << i) : 0;
585 }
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 parport_write_data(gc->pd->port, cmd | GC_PSX_CLOCK | GC_PSX_POWER);
588 udelay(gc_psx_delay);
589 }
590}
591
592/*
593 * gc_psx_read_packet() reads a whole psx packet and returns
594 * device identifier code.
595 */
596
Dmitry Torokhov09951742010-02-21 20:54:54 -0800597static void gc_psx_read_packet(struct gc *gc,
598 unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES],
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500599 unsigned char id[GC_MAX_DEVICES])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
601 int i, j, max_len = 0;
602 unsigned long flags;
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500603 unsigned char data2[GC_MAX_DEVICES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800605 /* Select pad */
606 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 udelay(gc_psx_delay);
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800608 /* Deselect, begin command */
609 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_POWER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 udelay(gc_psx_delay);
611
612 local_irq_save(flags);
613
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800614 gc_psx_command(gc, 0x01, data2); /* Access pad */
615 gc_psx_command(gc, 0x42, id); /* Get device ids */
616 gc_psx_command(gc, 0, data2); /* Dump status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800618 /* Find the longest pad */
Dmitry Torokhov09951742010-02-21 20:54:54 -0800619 for (i = 0; i < GC_MAX_DEVICES; i++) {
620 struct gc_pad *pad = &gc->pads[i];
621
622 if ((pad->type == GC_PSX || pad->type == GC_DDR) &&
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800623 GC_PSX_LEN(id[i]) > max_len &&
624 GC_PSX_LEN(id[i]) <= GC_PSX_BYTES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 max_len = GC_PSX_LEN(id[i]);
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800626 }
Dmitry Torokhov09951742010-02-21 20:54:54 -0800627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800629 /* Read in all the data */
630 for (i = 0; i < max_len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 gc_psx_command(gc, 0, data2);
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500632 for (j = 0; j < GC_MAX_DEVICES; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 data[j][i] = data2[j];
634 }
635
636 local_irq_restore(flags);
637
638 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
639
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800640 /* Set id's to the real value */
641 for (i = 0; i < GC_MAX_DEVICES; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 id[i] = GC_PSX_ID(id[i]);
643}
644
Dmitry Torokhov09951742010-02-21 20:54:54 -0800645static void gc_psx_report_one(struct gc_pad *pad, unsigned char psx_type,
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800646 unsigned char *data)
647{
Dmitry Torokhov09951742010-02-21 20:54:54 -0800648 struct input_dev *dev = pad->dev;
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800649 int i;
650
Dmitry Torokhov09951742010-02-21 20:54:54 -0800651 switch (psx_type) {
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800652
653 case GC_PSX_RUMBLE:
654
655 input_report_key(dev, BTN_THUMBL, ~data[0] & 0x04);
656 input_report_key(dev, BTN_THUMBR, ~data[0] & 0x02);
657
658 case GC_PSX_NEGCON:
659 case GC_PSX_ANALOG:
660
Dmitry Torokhov09951742010-02-21 20:54:54 -0800661 if (pad->type == GC_DDR) {
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800662 for (i = 0; i < 4; i++)
663 input_report_key(dev, gc_psx_ddr_btn[i],
664 ~data[0] & (0x10 << i));
665 } else {
666 for (i = 0; i < 4; i++)
667 input_report_abs(dev, gc_psx_abs[i + 2],
668 data[i + 2]);
669
Dmitry Torokhov315543f2010-02-21 20:54:31 -0800670 input_report_abs(dev, ABS_X,
671 !!(data[0] & 0x80) * 128 + !(data[0] & 0x20) * 127);
672 input_report_abs(dev, ABS_Y,
673 !!(data[0] & 0x10) * 128 + !(data[0] & 0x40) * 127);
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800674 }
675
676 for (i = 0; i < 8; i++)
677 input_report_key(dev, gc_psx_btn[i], ~data[1] & (1 << i));
678
679 input_report_key(dev, BTN_START, ~data[0] & 0x08);
680 input_report_key(dev, BTN_SELECT, ~data[0] & 0x01);
681
682 input_sync(dev);
683
684 break;
685
686 case GC_PSX_NORMAL:
Dmitry Torokhov09951742010-02-21 20:54:54 -0800687
688 if (pad->type == GC_DDR) {
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800689 for (i = 0; i < 4; i++)
690 input_report_key(dev, gc_psx_ddr_btn[i],
691 ~data[0] & (0x10 << i));
692 } else {
Dmitry Torokhov315543f2010-02-21 20:54:31 -0800693 input_report_abs(dev, ABS_X,
694 !!(data[0] & 0x80) * 128 + !(data[0] & 0x20) * 127);
695 input_report_abs(dev, ABS_Y,
696 !!(data[0] & 0x10) * 128 + !(data[0] & 0x40) * 127);
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800697
698 /*
699 * For some reason if the extra axes are left unset
700 * they drift.
701 * for (i = 0; i < 4; i++)
702 input_report_abs(dev, gc_psx_abs[i + 2], 128);
703 * This needs to be debugged properly,
704 * maybe fuzz processing needs to be done
705 * in input_sync()
706 * --vojtech
707 */
708 }
709
710 for (i = 0; i < 8; i++)
711 input_report_key(dev, gc_psx_btn[i], ~data[1] & (1 << i));
712
713 input_report_key(dev, BTN_START, ~data[0] & 0x08);
714 input_report_key(dev, BTN_SELECT, ~data[0] & 0x01);
715
716 input_sync(dev);
717
718 break;
719
Dmitry Torokhov09951742010-02-21 20:54:54 -0800720 default: /* not a pad, ignore */
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800721 break;
722 }
723}
724
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500725static void gc_psx_process_packet(struct gc *gc)
726{
727 unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES];
728 unsigned char id[GC_MAX_DEVICES];
Dmitry Torokhov09951742010-02-21 20:54:54 -0800729 struct gc_pad *pad;
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800730 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500732 gc_psx_read_packet(gc, data, id);
733
734 for (i = 0; i < GC_MAX_DEVICES; i++) {
Dmitry Torokhov09951742010-02-21 20:54:54 -0800735 pad = &gc->pads[i];
736 if (pad->type == GC_PSX || pad->type == GC_DDR)
737 gc_psx_report_one(pad, id[i], data[i]);
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500738 }
739}
740
741/*
742 * gc_timer() initiates reads of console pads data.
743 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745static void gc_timer(unsigned long private)
746{
747 struct gc *gc = (void *) private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749/*
750 * N64 pads - must be read first, any read confuses them for 200 us
751 */
752
Dmitry Torokhov09951742010-02-21 20:54:54 -0800753 if (gc->pad_count[GC_N64])
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500754 gc_n64_process_packet(gc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756/*
Raphael Assenatb157d552006-04-02 00:10:05 -0500757 * NES and SNES pads or mouse
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 */
759
Dmitry Torokhov09951742010-02-21 20:54:54 -0800760 if (gc->pad_count[GC_NES] ||
761 gc->pad_count[GC_SNES] ||
762 gc->pad_count[GC_SNESMOUSE]) {
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500763 gc_nes_process_packet(gc);
Dmitry Torokhov09951742010-02-21 20:54:54 -0800764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766/*
767 * Multi and Multi2 joysticks
768 */
769
Dmitry Torokhov09951742010-02-21 20:54:54 -0800770 if (gc->pad_count[GC_MULTI] || gc->pad_count[GC_MULTI2])
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500771 gc_multi_process_packet(gc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773/*
774 * PSX controllers
775 */
776
Dmitry Torokhov09951742010-02-21 20:54:54 -0800777 if (gc->pad_count[GC_PSX] || gc->pad_count[GC_DDR])
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500778 gc_psx_process_packet(gc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
781}
782
783static int gc_open(struct input_dev *dev)
784{
Dmitry Torokhov8715c1c2007-04-12 01:34:14 -0400785 struct gc *gc = input_get_drvdata(dev);
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500786 int err;
787
Ingo Molnar72ba9f02006-02-19 00:22:30 -0500788 err = mutex_lock_interruptible(&gc->mutex);
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500789 if (err)
790 return err;
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (!gc->used++) {
793 parport_claim(gc->pd);
794 parport_write_control(gc->pd->port, 0x04);
795 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
796 }
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500797
Ingo Molnar72ba9f02006-02-19 00:22:30 -0500798 mutex_unlock(&gc->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return 0;
800}
801
802static void gc_close(struct input_dev *dev)
803{
Dmitry Torokhov8715c1c2007-04-12 01:34:14 -0400804 struct gc *gc = input_get_drvdata(dev);
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500805
Ingo Molnar72ba9f02006-02-19 00:22:30 -0500806 mutex_lock(&gc->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (!--gc->used) {
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500808 del_timer_sync(&gc->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 parport_write_control(gc->pd->port, 0x00);
810 parport_release(gc->pd);
811 }
Ingo Molnar72ba9f02006-02-19 00:22:30 -0500812 mutex_unlock(&gc->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
814
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500815static int __init gc_setup_pad(struct gc *gc, int idx, int pad_type)
816{
Dmitry Torokhov09951742010-02-21 20:54:54 -0800817 struct gc_pad *pad = &gc->pads[idx];
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500818 struct input_dev *input_dev;
819 int i;
Scott Moreau7aa9e0e2010-02-21 20:53:55 -0800820 int err;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500821
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500822 if (pad_type < 1 || pad_type > GC_MAX) {
Dmitry Torokhova1e12742010-02-21 20:55:31 -0800823 pr_err("Pad type %d unknown\n", pad_type);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500824 return -EINVAL;
825 }
826
Dmitry Torokhov09951742010-02-21 20:54:54 -0800827 pad->dev = input_dev = input_allocate_device();
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500828 if (!input_dev) {
Dmitry Torokhova1e12742010-02-21 20:55:31 -0800829 pr_err("Not enough memory for input device\n");
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500830 return -ENOMEM;
831 }
832
Dmitry Torokhov09951742010-02-21 20:54:54 -0800833 pad->type = pad_type;
834
835 snprintf(pad->phys, sizeof(pad->phys),
836 "%s/input%d", gc->pd->port->name, idx);
837
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500838 input_dev->name = gc_names[pad_type];
Dmitry Torokhov09951742010-02-21 20:54:54 -0800839 input_dev->phys = pad->phys;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500840 input_dev->id.bustype = BUS_PARPORT;
841 input_dev->id.vendor = 0x0001;
842 input_dev->id.product = pad_type;
843 input_dev->id.version = 0x0100;
Dmitry Torokhov8715c1c2007-04-12 01:34:14 -0400844
845 input_set_drvdata(input_dev, gc);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500846
847 input_dev->open = gc_open;
848 input_dev->close = gc_close;
849
Raphael Assenatb157d552006-04-02 00:10:05 -0500850 if (pad_type != GC_SNESMOUSE) {
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700851 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500852
Raphael Assenatb157d552006-04-02 00:10:05 -0500853 for (i = 0; i < 2; i++)
854 input_set_abs_params(input_dev, ABS_X + i, -1, 1, 0, 0);
855 } else
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700856 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500857
Dmitry Torokhov09951742010-02-21 20:54:54 -0800858 gc->pad_count[pad_type]++;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500859
860 switch (pad_type) {
861
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800862 case GC_N64:
863 for (i = 0; i < 10; i++)
864 __set_bit(gc_n64_btn[i], input_dev->keybit);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500865
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800866 for (i = 0; i < 2; i++) {
867 input_set_abs_params(input_dev, ABS_X + i, -127, 126, 0, 2);
868 input_set_abs_params(input_dev, ABS_HAT0X + i, -1, 1, 0, 0);
869 }
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500870
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800871 err = gc_n64_init_ff(input_dev, idx);
872 if (err) {
Dmitry Torokhova1e12742010-02-21 20:55:31 -0800873 pr_warning("Failed to initiate rumble for N64 device %d\n", idx);
Dmitry Torokhov09951742010-02-21 20:54:54 -0800874 goto err_free_dev;
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800875 }
Scott Moreau7aa9e0e2010-02-21 20:53:55 -0800876
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800877 break;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500878
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800879 case GC_SNESMOUSE:
880 __set_bit(BTN_LEFT, input_dev->keybit);
881 __set_bit(BTN_RIGHT, input_dev->keybit);
882 __set_bit(REL_X, input_dev->relbit);
883 __set_bit(REL_Y, input_dev->relbit);
884 break;
Raphael Assenatb157d552006-04-02 00:10:05 -0500885
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800886 case GC_SNES:
887 for (i = 4; i < 8; i++)
888 __set_bit(gc_snes_btn[i], input_dev->keybit);
889 case GC_NES:
890 for (i = 0; i < 4; i++)
891 __set_bit(gc_snes_btn[i], input_dev->keybit);
892 break;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500893
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800894 case GC_MULTI2:
895 __set_bit(BTN_THUMB, input_dev->keybit);
896 case GC_MULTI:
897 __set_bit(BTN_TRIGGER, input_dev->keybit);
898 break;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500899
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800900 case GC_PSX:
901 for (i = 0; i < 6; i++)
902 input_set_abs_params(input_dev,
903 gc_psx_abs[i], 4, 252, 0, 2);
904 for (i = 0; i < 12; i++)
905 __set_bit(gc_psx_btn[i], input_dev->keybit);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500906
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800907 break;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500908
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800909 case GC_DDR:
910 for (i = 0; i < 4; i++)
911 __set_bit(gc_psx_ddr_btn[i], input_dev->keybit);
912 for (i = 0; i < 12; i++)
913 __set_bit(gc_psx_btn[i], input_dev->keybit);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500914
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800915 break;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500916 }
917
Dmitry Torokhov09951742010-02-21 20:54:54 -0800918 err = input_register_device(pad->dev);
919 if (err)
920 goto err_free_dev;
921
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500922 return 0;
Dmitry Torokhov09951742010-02-21 20:54:54 -0800923
924err_free_dev:
925 input_free_device(pad->dev);
926 pad->dev = NULL;
927 return err;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500928}
929
930static struct gc __init *gc_probe(int parport, int *pads, int n_pads)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
932 struct gc *gc;
933 struct parport *pp;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500934 struct pardevice *pd;
935 int i;
Dmitry Torokhov09951742010-02-21 20:54:54 -0800936 int count = 0;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500937 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500939 pp = parport_find_number(parport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (!pp) {
Dmitry Torokhova1e12742010-02-21 20:55:31 -0800941 pr_err("no such parport %d\n", parport);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500942 err = -EINVAL;
943 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
945
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500946 pd = parport_register_device(pp, "gamecon", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
947 if (!pd) {
Dmitry Torokhova1e12742010-02-21 20:55:31 -0800948 pr_err("parport busy already - lp.o loaded?\n");
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500949 err = -EBUSY;
950 goto err_put_pp;
951 }
952
953 gc = kzalloc(sizeof(struct gc), GFP_KERNEL);
954 if (!gc) {
Dmitry Torokhova1e12742010-02-21 20:55:31 -0800955 pr_err("Not enough memory\n");
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500956 err = -ENOMEM;
957 goto err_unreg_pardev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
Dmitry Torokhov8b1a1982005-05-29 02:29:52 -0500959
Ingo Molnar72ba9f02006-02-19 00:22:30 -0500960 mutex_init(&gc->mutex);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500961 gc->pd = pd;
Dmitry Torokhovd38fcb92010-02-21 20:54:28 -0800962 setup_timer(&gc->timer, gc_timer, (long) gc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Dmitry Torokhovc7fd0182006-01-29 21:52:04 -0500964 for (i = 0; i < n_pads && i < GC_MAX_DEVICES; i++) {
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500965 if (!pads[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 continue;
967
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500968 err = gc_setup_pad(gc, i, pads[i]);
969 if (err)
Dmitry Torokhov77fc46c2006-01-29 21:52:11 -0500970 goto err_unreg_devs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Dmitry Torokhov09951742010-02-21 20:54:54 -0800972 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
974
Dmitry Torokhov09951742010-02-21 20:54:54 -0800975 if (count == 0) {
Dmitry Torokhova1e12742010-02-21 20:55:31 -0800976 pr_err("No valid devices specified\n");
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500977 err = -EINVAL;
978 goto err_free_gc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500981 parport_put_port(pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return gc;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500983
Dmitry Torokhov77fc46c2006-01-29 21:52:11 -0500984 err_unreg_devs:
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500985 while (--i >= 0)
Dmitry Torokhov09951742010-02-21 20:54:54 -0800986 if (gc->pads[i].dev)
987 input_unregister_device(gc->pads[i].dev);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500988 err_free_gc:
989 kfree(gc);
990 err_unreg_pardev:
991 parport_unregister_device(pd);
992 err_put_pp:
993 parport_put_port(pp);
994 err_out:
995 return ERR_PTR(err);
996}
997
Dmitry Torokhov77fc46c2006-01-29 21:52:11 -0500998static void gc_remove(struct gc *gc)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500999{
1000 int i;
1001
1002 for (i = 0; i < GC_MAX_DEVICES; i++)
Dmitry Torokhov09951742010-02-21 20:54:54 -08001003 if (gc->pads[i].dev)
1004 input_unregister_device(gc->pads[i].dev);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -05001005 parport_unregister_device(gc->pd);
1006 kfree(gc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
1009static int __init gc_init(void)
1010{
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -05001011 int i;
1012 int have_dev = 0;
1013 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -05001015 for (i = 0; i < GC_MAX_PORTS; i++) {
Dmitry Torokhov78167232007-05-03 00:52:51 -04001016 if (gc_cfg[i].nargs == 0 || gc_cfg[i].args[0] < 0)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -05001017 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Dmitry Torokhov78167232007-05-03 00:52:51 -04001019 if (gc_cfg[i].nargs < 2) {
Dmitry Torokhova1e12742010-02-21 20:55:31 -08001020 pr_err("at least one device must be specified\n");
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -05001021 err = -EINVAL;
1022 break;
1023 }
1024
Dmitry Torokhov78167232007-05-03 00:52:51 -04001025 gc_base[i] = gc_probe(gc_cfg[i].args[0],
1026 gc_cfg[i].args + 1, gc_cfg[i].nargs - 1);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -05001027 if (IS_ERR(gc_base[i])) {
1028 err = PTR_ERR(gc_base[i]);
1029 break;
1030 }
1031
1032 have_dev = 1;
1033 }
1034
1035 if (err) {
1036 while (--i >= 0)
Dmitry Torokhov77fc46c2006-01-29 21:52:11 -05001037 if (gc_base[i])
1038 gc_remove(gc_base[i]);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -05001039 return err;
1040 }
1041
1042 return have_dev ? 0 : -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044
1045static void __exit gc_exit(void)
1046{
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -05001047 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -05001049 for (i = 0; i < GC_MAX_PORTS; i++)
1050 if (gc_base[i])
1051 gc_remove(gc_base[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
1054module_init(gc_init);
1055module_exit(gc_exit);