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