aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/joystick
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/input/joystick')
-rw-r--r--drivers/input/joystick/Kconfig1
-rw-r--r--drivers/input/joystick/gamecon.c664
-rw-r--r--drivers/input/joystick/xpad.c253
3 files changed, 593 insertions, 325 deletions
diff --git a/drivers/input/joystick/Kconfig b/drivers/input/joystick/Kconfig
index b11419590cf..5b596165b57 100644
--- a/drivers/input/joystick/Kconfig
+++ b/drivers/input/joystick/Kconfig
@@ -221,6 +221,7 @@ config JOYSTICK_DB9
221config JOYSTICK_GAMECON 221config JOYSTICK_GAMECON
222 tristate "Multisystem, NES, SNES, N64, PSX joysticks and gamepads" 222 tristate "Multisystem, NES, SNES, N64, PSX joysticks and gamepads"
223 depends on PARPORT 223 depends on PARPORT
224 select INPUT_FF_MEMLESS
224 ---help--- 225 ---help---
225 Say Y here if you have a Nintendo Entertainment System gamepad, 226 Say Y here if you have a Nintendo Entertainment System gamepad,
226 Super Nintendo Entertainment System gamepad, Nintendo 64 gamepad, 227 Super Nintendo Entertainment System gamepad, Nintendo 64 gamepad,
diff --git a/drivers/input/joystick/gamecon.c b/drivers/input/joystick/gamecon.c
index 07a32aff5a3..ae998d99a5a 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>
@@ -61,48 +63,73 @@ MODULE_PARM_DESC(map3, "Describes third set of devices");
61 63
62/* see also gs_psx_delay parameter in PSX support section */ 64/* see also gs_psx_delay parameter in PSX support section */
63 65
64#define GC_SNES 1 66enum gc_type {
65#define GC_NES 2 67 GC_NONE = 0,
66#define GC_NES4 3 68 GC_SNES,
67#define GC_MULTI 4 69 GC_NES,
68#define GC_MULTI2 5 70 GC_NES4,
69#define GC_N64 6 71 GC_MULTI,
70#define GC_PSX 7 72 GC_MULTI2,
71#define GC_DDR 8 73 GC_N64,
72#define GC_SNESMOUSE 9 74 GC_PSX,
73 75 GC_DDR,
74#define GC_MAX 9 76 GC_SNESMOUSE,
77 GC_MAX
78};
75 79
76#define GC_REFRESH_TIME HZ/100 80#define GC_REFRESH_TIME HZ/100
77 81
82struct gc_pad {
83 struct input_dev *dev;
84 enum gc_type type;
85 char phys[32];
86};
87
78struct gc { 88struct gc {
79 struct pardevice *pd; 89 struct pardevice *pd;
90 struct gc_pad pads[GC_MAX_DEVICES];
80 struct input_dev *dev[GC_MAX_DEVICES]; 91 struct input_dev *dev[GC_MAX_DEVICES];
81 struct timer_list timer; 92 struct timer_list timer;
82 unsigned char pads[GC_MAX + 1]; 93 int pad_count[GC_MAX];
83 int used; 94 int used;
84 struct mutex mutex; 95 struct mutex mutex;
85 char phys[GC_MAX_DEVICES][32]; 96};
97
98struct gc_subdev {
99 unsigned int idx;
86}; 100};
87 101
88static struct gc *gc_base[3]; 102static struct gc *gc_base[3];
89 103
90static int gc_status_bit[] = { 0x40, 0x80, 0x20, 0x10, 0x08 }; 104static const int gc_status_bit[] = { 0x40, 0x80, 0x20, 0x10, 0x08 };
105
106static const char *gc_names[] = {
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};
91 111
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/* 112/*
96 * N64 support. 113 * N64 support.
97 */ 114 */
98 115
99static unsigned char gc_n64_bytes[] = { 0, 1, 13, 15, 14, 12, 10, 11, 2, 3 }; 116static 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 }; 117static const short gc_n64_btn[] = {
118 BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z,
119 BTN_TL, BTN_TR, BTN_TRIGGER, BTN_START
120};
101 121
102#define GC_N64_LENGTH 32 /* N64 bit length, not including stop bit */ 122#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 */ 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 */
104#define GC_N64_DELAY 133 /* delay between transmit request, and response ready (us) */ 132#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) */ 133#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 */ 134 /* GC_N64_DWS > 24 is known to fail */
108#define GC_N64_POWER_W 0xe2 /* power during write (transmit request) */ 135#define GC_N64_POWER_W 0xe2 /* power during write (transmit request) */
@@ -114,8 +141,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 */ 141#define GC_N64_CLOCK 0x02 /* clock bits for read */
115 142
116/* 143/*
144 * 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/*
117 * gc_n64_read_packet() reads an N64 packet. 175 * 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. 176 * Each pad uses one bit per byte. So all pads connected to this port
177 * are read in parallel.
119 */ 178 */
120 179
121static void gc_n64_read_packet(struct gc *gc, unsigned char *data) 180static void gc_n64_read_packet(struct gc *gc, unsigned char *data)
@@ -128,14 +187,13 @@ static void gc_n64_read_packet(struct gc *gc, unsigned char *data)
128 */ 187 */
129 188
130 local_irq_save(flags); 189 local_irq_save(flags);
131 for (i = 0; i < GC_N64_REQUEST_LENGTH; i++) { 190 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)); 191 gc_n64_send_stop_bit(gc, GC_N64_OUT);
133 udelay(GC_N64_DWS);
134 }
135 local_irq_restore(flags); 192 local_irq_restore(flags);
136 193
137/* 194/*
138 * Wait for the pad response to be loaded into the 33-bit register of the adapter 195 * Wait for the pad response to be loaded into the 33-bit register
196 * of the adapter.
139 */ 197 */
140 198
141 udelay(GC_N64_DELAY); 199 udelay(GC_N64_DELAY);
@@ -146,13 +204,15 @@ static void gc_n64_read_packet(struct gc *gc, unsigned char *data)
146 204
147 for (i = 0; i < GC_N64_LENGTH; i++) { 205 for (i = 0; i < GC_N64_LENGTH; i++) {
148 parport_write_data(gc->pd->port, GC_N64_POWER_R); 206 parport_write_data(gc->pd->port, GC_N64_POWER_R);
207 udelay(2);
149 data[i] = parport_read_status(gc->pd->port); 208 data[i] = parport_read_status(gc->pd->port);
150 parport_write_data(gc->pd->port, GC_N64_POWER_R | GC_N64_CLOCK); 209 parport_write_data(gc->pd->port, GC_N64_POWER_R | GC_N64_CLOCK);
151 } 210 }
152 211
153/* 212/*
154 * We must wait 200 ms here for the controller to reinitialize before the next read request. 213 * 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. 214 * the next read request. No worries as long as gc_read is polled less
215 * frequently than this.
156 */ 216 */
157 217
158} 218}
@@ -160,45 +220,112 @@ static void gc_n64_read_packet(struct gc *gc, unsigned char *data)
160static void gc_n64_process_packet(struct gc *gc) 220static void gc_n64_process_packet(struct gc *gc)
161{ 221{
162 unsigned char data[GC_N64_LENGTH]; 222 unsigned char data[GC_N64_LENGTH];
163 signed char axes[2];
164 struct input_dev *dev; 223 struct input_dev *dev;
165 int i, j, s; 224 int i, j, s;
225 signed char x, y;
166 226
167 gc_n64_read_packet(gc, data); 227 gc_n64_read_packet(gc, data);
168 228
169 for (i = 0; i < GC_MAX_DEVICES; i++) { 229 for (i = 0; i < GC_MAX_DEVICES; i++) {
170 230
171 dev = gc->dev[i]; 231 if (gc->pads[i].type != GC_N64)
172 if (!dev)
173 continue; 232 continue;
174 233
234 dev = gc->pads[i].dev;
175 s = gc_status_bit[i]; 235 s = gc_status_bit[i];
176 236
177 if (s & gc->pads[GC_N64] & ~(data[8] | data[9])) { 237 if (s & ~(data[8] | data[9])) {
178 238
179 axes[0] = axes[1] = 0; 239 x = y = 0;
180 240
181 for (j = 0; j < 8; j++) { 241 for (j = 0; j < 8; j++) {
182 if (data[23 - j] & s) 242 if (data[23 - j] & s)
183 axes[0] |= 1 << j; 243 x |= 1 << j;
184 if (data[31 - j] & s) 244 if (data[31 - j] & s)
185 axes[1] |= 1 << j; 245 y |= 1 << j;
186 } 246 }
187 247
188 input_report_abs(dev, ABS_X, axes[0]); 248 input_report_abs(dev, ABS_X, x);
189 input_report_abs(dev, ABS_Y, -axes[1]); 249 input_report_abs(dev, ABS_Y, -y);
190 250
191 input_report_abs(dev, ABS_HAT0X, !(s & data[6]) - !(s & data[7])); 251 input_report_abs(dev, ABS_HAT0X,
192 input_report_abs(dev, ABS_HAT0Y, !(s & data[4]) - !(s & data[5])); 252 !(s & data[6]) - !(s & data[7]));
253 input_report_abs(dev, ABS_HAT0Y,
254 !(s & data[4]) - !(s & data[5]));
193 255
194 for (j = 0; j < 10; j++) 256 for (j = 0; j < 10; j++)
195 input_report_key(dev, gc_n64_btn[j], s & data[gc_n64_bytes[j]]); 257 input_report_key(dev, gc_n64_btn[j],
258 s & data[gc_n64_bytes[j]]);
196 259
197 input_sync(dev); 260 input_sync(dev);
198 } 261 }
199 } 262 }
200} 263}
201 264
265static 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
202/* 329/*
203 * NES/SNES support. 330 * NES/SNES support.
204 */ 331 */
@@ -214,9 +341,11 @@ static void gc_n64_process_packet(struct gc *gc)
214#define GC_NES_CLOCK 0x01 341#define GC_NES_CLOCK 0x01
215#define GC_NES_LATCH 0x02 342#define GC_NES_LATCH 0x02
216 343
217static unsigned char gc_nes_bytes[] = { 0, 1, 2, 3 }; 344static const unsigned char gc_nes_bytes[] = { 0, 1, 2, 3 };
218static unsigned char gc_snes_bytes[] = { 8, 0, 2, 3, 9, 1, 10, 11 }; 345static 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 }; 346static const short gc_snes_btn[] = {
347 BTN_A, BTN_B, BTN_SELECT, BTN_START, BTN_X, BTN_Y, BTN_TL, BTN_TR
348};
220 349
221/* 350/*
222 * gc_nes_read_packet() reads a NES/SNES packet. 351 * gc_nes_read_packet() reads a NES/SNES packet.
@@ -244,40 +373,51 @@ static void gc_nes_read_packet(struct gc *gc, int length, unsigned char *data)
244static void gc_nes_process_packet(struct gc *gc) 373static void gc_nes_process_packet(struct gc *gc)
245{ 374{
246 unsigned char data[GC_SNESMOUSE_LENGTH]; 375 unsigned char data[GC_SNESMOUSE_LENGTH];
376 struct gc_pad *pad;
247 struct input_dev *dev; 377 struct input_dev *dev;
248 int i, j, s, len; 378 int i, j, s, len;
249 char x_rel, y_rel; 379 char x_rel, y_rel;
250 380
251 len = gc->pads[GC_SNESMOUSE] ? GC_SNESMOUSE_LENGTH : 381 len = gc->pad_count[GC_SNESMOUSE] ? GC_SNESMOUSE_LENGTH :
252 (gc->pads[GC_SNES] ? GC_SNES_LENGTH : GC_NES_LENGTH); 382 (gc->pad_count[GC_SNES] ? GC_SNES_LENGTH : GC_NES_LENGTH);
253 383
254 gc_nes_read_packet(gc, len, data); 384 gc_nes_read_packet(gc, len, data);
255 385
256 for (i = 0; i < GC_MAX_DEVICES; i++) { 386 for (i = 0; i < GC_MAX_DEVICES; i++) {
257 387
388 pad = &gc->pads[i];
258 dev = gc->dev[i]; 389 dev = gc->dev[i];
259 if (!dev)
260 continue;
261
262 s = gc_status_bit[i]; 390 s = gc_status_bit[i];
263 391
264 if (s & (gc->pads[GC_NES] | gc->pads[GC_SNES])) { 392 switch (pad->type) {
393
394 case GC_NES:
395
265 input_report_abs(dev, ABS_X, !(s & data[6]) - !(s & data[7])); 396 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])); 397 input_report_abs(dev, ABS_Y, !(s & data[4]) - !(s & data[5]));
267 }
268 398
269 if (s & gc->pads[GC_NES])
270 for (j = 0; j < 4; j++) 399 for (j = 0; j < 4; j++)
271 input_report_key(dev, gc_snes_btn[j], s & data[gc_nes_bytes[j]]); 400 input_report_key(dev, gc_snes_btn[j],
401 s & data[gc_nes_bytes[j]]);
402 input_sync(dev);
403 break;
404
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]));
272 409
273 if (s & gc->pads[GC_SNES])
274 for (j = 0; j < 8; j++) 410 for (j = 0; j < 8; j++)
275 input_report_key(dev, gc_snes_btn[j], s & data[gc_snes_bytes[j]]); 411 input_report_key(dev, gc_snes_btn[j],
412 s & data[gc_snes_bytes[j]]);
413 input_sync(dev);
414 break;
276 415
277 if (s & gc->pads[GC_SNESMOUSE]) { 416 case GC_SNESMOUSE:
278 /* 417 /*
279 * The 4 unused bits from SNES controllers appear to be ID bits 418 * The 4 unused bits from SNES controllers appear
280 * so use them to make sure iwe are dealing with a mouse. 419 * to be ID bits so use them to make sure we are
420 * dealing with a mouse.
281 * gamepad is connected. This is important since 421 * gamepad is connected. This is important since
282 * my SNES gamepad sends 1's for bits 16-31, which 422 * my SNES gamepad sends 1's for bits 16-31, which
283 * cause the mouse pointer to quickly move to the 423 * cause the mouse pointer to quickly move to the
@@ -310,9 +450,14 @@ static void gc_nes_process_packet(struct gc *gc)
310 y_rel = -y_rel; 450 y_rel = -y_rel;
311 input_report_rel(dev, REL_Y, y_rel); 451 input_report_rel(dev, REL_Y, y_rel);
312 } 452 }
453
454 input_sync(dev);
313 } 455 }
456 break;
457
458 default:
459 break;
314 } 460 }
315 input_sync(dev);
316 } 461 }
317} 462}
318 463
@@ -340,29 +485,35 @@ static void gc_multi_read_packet(struct gc *gc, int length, unsigned char *data)
340static void gc_multi_process_packet(struct gc *gc) 485static void gc_multi_process_packet(struct gc *gc)
341{ 486{
342 unsigned char data[GC_MULTI2_LENGTH]; 487 unsigned char data[GC_MULTI2_LENGTH];
488 int data_len = gc->pad_count[GC_MULTI2] ? GC_MULTI2_LENGTH : GC_MULTI_LENGTH;
489 struct gc_pad *pad;
343 struct input_dev *dev; 490 struct input_dev *dev;
344 int i, s; 491 int i, s;
345 492
346 gc_multi_read_packet(gc, gc->pads[GC_MULTI2] ? GC_MULTI2_LENGTH : GC_MULTI_LENGTH, data); 493 gc_multi_read_packet(gc, data_len, data);
347 494
348 for (i = 0; i < GC_MAX_DEVICES; i++) { 495 for (i = 0; i < GC_MAX_DEVICES; i++) {
349 496 pad = &gc->pads[i];
350 dev = gc->dev[i]; 497 dev = pad->dev;
351 if (!dev)
352 continue;
353
354 s = gc_status_bit[i]; 498 s = gc_status_bit[i];
355 499
356 if (s & (gc->pads[GC_MULTI] | gc->pads[GC_MULTI2])) { 500 switch (pad->type) {
357 input_report_abs(dev, ABS_X, !(s & data[2]) - !(s & data[3])); 501 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]); 502 input_report_key(dev, BTN_THUMB, s & data[5]);
503 /* fall through */
364 504
365 input_sync(dev); 505 case GC_MULTI:
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]));
510 input_report_key(dev, BTN_TRIGGER, s & data[4]);
511 input_sync(dev);
512 break;
513
514 default:
515 break;
516 }
366 } 517 }
367} 518}
368 519
@@ -398,30 +549,41 @@ static int gc_psx_delay = GC_PSX_DELAY;
398module_param_named(psx_delay, gc_psx_delay, uint, 0); 549module_param_named(psx_delay, gc_psx_delay, uint, 0);
399MODULE_PARM_DESC(psx_delay, "Delay when accessing Sony PSX controller (usecs)"); 550MODULE_PARM_DESC(psx_delay, "Delay when accessing Sony PSX controller (usecs)");
400 551
401static short gc_psx_abs[] = { ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_HAT0X, ABS_HAT0Y }; 552static 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, 553 ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_HAT0X, ABS_HAT0Y
403 BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR }; 554};
404static short gc_psx_ddr_btn[] = { BTN_0, BTN_1, BTN_2, BTN_3 }; 555static const short gc_psx_btn[] = {
556 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};
559static const short gc_psx_ddr_btn[] = { BTN_0, BTN_1, BTN_2, BTN_3 };
405 560
406/* 561/*
407 * gc_psx_command() writes 8bit command and reads 8bit data from 562 * gc_psx_command() writes 8bit command and reads 8bit data from
408 * the psx pad. 563 * the psx pad.
409 */ 564 */
410 565
411static void gc_psx_command(struct gc *gc, int b, unsigned char data[GC_MAX_DEVICES]) 566static void gc_psx_command(struct gc *gc, int b, unsigned char *data)
412{ 567{
568 struct parport *port = gc->pd->port;
413 int i, j, cmd, read; 569 int i, j, cmd, read;
414 570
415 for (i = 0; i < GC_MAX_DEVICES; i++) 571 memset(data, 0, GC_MAX_DEVICES);
416 data[i] = 0;
417 572
418 for (i = 0; i < GC_PSX_LENGTH; i++, b >>= 1) { 573 for (i = 0; i < GC_PSX_LENGTH; i++, b >>= 1) {
419 cmd = (b & 1) ? GC_PSX_COMMAND : 0; 574 cmd = (b & 1) ? GC_PSX_COMMAND : 0;
420 parport_write_data(gc->pd->port, cmd | GC_PSX_POWER); 575 parport_write_data(port, cmd | GC_PSX_POWER);
421 udelay(gc_psx_delay); 576 udelay(gc_psx_delay);
422 read = parport_read_status(gc->pd->port) ^ 0x80; 577
423 for (j = 0; j < GC_MAX_DEVICES; j++) 578 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; 579
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
425 parport_write_data(gc->pd->port, cmd | GC_PSX_CLOCK | GC_PSX_POWER); 587 parport_write_data(gc->pd->port, cmd | GC_PSX_CLOCK | GC_PSX_POWER);
426 udelay(gc_psx_delay); 588 udelay(gc_psx_delay);
427 } 589 }
@@ -432,31 +594,40 @@ static void gc_psx_command(struct gc *gc, int b, unsigned char data[GC_MAX_DEVIC
432 * device identifier code. 594 * device identifier code.
433 */ 595 */
434 596
435static void gc_psx_read_packet(struct gc *gc, unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES], 597static void gc_psx_read_packet(struct gc *gc,
598 unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES],
436 unsigned char id[GC_MAX_DEVICES]) 599 unsigned char id[GC_MAX_DEVICES])
437{ 600{
438 int i, j, max_len = 0; 601 int i, j, max_len = 0;
439 unsigned long flags; 602 unsigned long flags;
440 unsigned char data2[GC_MAX_DEVICES]; 603 unsigned char data2[GC_MAX_DEVICES];
441 604
442 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER); /* Select pad */ 605 /* Select pad */
606 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
443 udelay(gc_psx_delay); 607 udelay(gc_psx_delay);
444 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_POWER); /* Deselect, begin command */ 608 /* Deselect, begin command */
609 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_POWER);
445 udelay(gc_psx_delay); 610 udelay(gc_psx_delay);
446 611
447 local_irq_save(flags); 612 local_irq_save(flags);
448 613
449 gc_psx_command(gc, 0x01, data2); /* Access pad */ 614 gc_psx_command(gc, 0x01, data2); /* Access pad */
450 gc_psx_command(gc, 0x42, id); /* Get device ids */ 615 gc_psx_command(gc, 0x42, id); /* Get device ids */
451 gc_psx_command(gc, 0, data2); /* Dump status */ 616 gc_psx_command(gc, 0, data2); /* Dump status */
617
618 /* Find the longest pad */
619 for (i = 0; i < GC_MAX_DEVICES; i++) {
620 struct gc_pad *pad = &gc->pads[i];
452 621
453 for (i =0; i < GC_MAX_DEVICES; i++) /* Find the longest pad */ 622 if ((pad->type == GC_PSX || pad->type == GC_DDR) &&
454 if((gc_status_bit[i] & (gc->pads[GC_PSX] | gc->pads[GC_DDR])) 623 GC_PSX_LEN(id[i]) > max_len &&
455 && (GC_PSX_LEN(id[i]) > max_len) 624 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]); 625 max_len = GC_PSX_LEN(id[i]);
626 }
627 }
458 628
459 for (i = 0; i < max_len; i++) { /* Read in all the data */ 629 /* Read in all the data */
630 for (i = 0; i < max_len; i++) {
460 gc_psx_command(gc, 0, data2); 631 gc_psx_command(gc, 0, data2);
461 for (j = 0; j < GC_MAX_DEVICES; j++) 632 for (j = 0; j < GC_MAX_DEVICES; j++)
462 data[j][i] = data2[j]; 633 data[j][i] = data2[j];
@@ -466,86 +637,104 @@ static void gc_psx_read_packet(struct gc *gc, unsigned char data[GC_MAX_DEVICES]
466 637
467 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER); 638 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
468 639
469 for(i = 0; i < GC_MAX_DEVICES; i++) /* Set id's to the real value */ 640 /* Set id's to the real value */
641 for (i = 0; i < GC_MAX_DEVICES; i++)
470 id[i] = GC_PSX_ID(id[i]); 642 id[i] = GC_PSX_ID(id[i]);
471} 643}
472 644
473static void gc_psx_process_packet(struct gc *gc) 645static void gc_psx_report_one(struct gc_pad *pad, unsigned char psx_type,
646 unsigned char *data)
474{ 647{
475 unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES]; 648 struct input_dev *dev = pad->dev;
476 unsigned char id[GC_MAX_DEVICES]; 649 int i;
477 struct input_dev *dev;
478 int i, j;
479 650
480 gc_psx_read_packet(gc, data, id); 651 switch (psx_type) {
481 652
482 for (i = 0; i < GC_MAX_DEVICES; i++) { 653 case GC_PSX_RUMBLE:
483 654
484 dev = gc->dev[i]; 655 input_report_key(dev, BTN_THUMBL, ~data[0] & 0x04);
485 if (!dev) 656 input_report_key(dev, BTN_THUMBR, ~data[0] & 0x02);
486 continue;
487 657
488 switch (id[i]) { 658 case GC_PSX_NEGCON:
659 case GC_PSX_ANALOG:
489 660
490 case GC_PSX_RUMBLE: 661 if (pad->type == GC_DDR) {
662 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]);
491 669
492 input_report_key(dev, BTN_THUMBL, ~data[i][0] & 0x04); 670 input_report_abs(dev, ABS_X,
493 input_report_key(dev, BTN_THUMBR, ~data[i][0] & 0x02); 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);
674 }
494 675
495 case GC_PSX_NEGCON: 676 for (i = 0; i < 8; i++)
496 case GC_PSX_ANALOG: 677 input_report_key(dev, gc_psx_btn[i], ~data[1] & (1 << i));
497 678
498 if (gc->pads[GC_DDR] & gc_status_bit[i]) { 679 input_report_key(dev, BTN_START, ~data[0] & 0x08);
499 for(j = 0; j < 4; j++) 680 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 681
505 input_report_abs(dev, ABS_X, 128 + !(data[i][0] & 0x20) * 127 - !(data[i][0] & 0x80) * 128); 682 input_sync(dev);
506 input_report_abs(dev, ABS_Y, 128 + !(data[i][0] & 0x40) * 127 - !(data[i][0] & 0x10) * 128);
507 }
508 683
509 for (j = 0; j < 8; j++) 684 break;
510 input_report_key(dev, gc_psx_btn[j], ~data[i][1] & (1 << j));
511 685
512 input_report_key(dev, BTN_START, ~data[i][0] & 0x08); 686 case GC_PSX_NORMAL:
513 input_report_key(dev, BTN_SELECT, ~data[i][0] & 0x01);
514 687
515 input_sync(dev); 688 if (pad->type == GC_DDR) {
689 for (i = 0; i < 4; i++)
690 input_report_key(dev, gc_psx_ddr_btn[i],
691 ~data[0] & (0x10 << i));
692 } else {
693 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);
516 697
517 break; 698 /*
518 699 * For some reason if the extra axes are left unset
519 case GC_PSX_NORMAL: 700 * they drift.
520 if (gc->pads[GC_DDR] & gc_status_bit[i]) { 701 * for (i = 0; i < 4; i++)
521 for(j = 0; j < 4; j++) 702 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)); 703 * This needs to be debugged properly,
523 } else { 704 * 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); 705 * in input_sync()
525 input_report_abs(dev, ABS_Y, 128 + !(data[i][0] & 0x40) * 127 - !(data[i][0] & 0x10) * 128); 706 * --vojtech
526 707 */
527 /* for some reason if the extra axes are left unset they drift */ 708 }
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 709
536 for (j = 0; j < 8; j++) 710 for (i = 0; i < 8; i++)
537 input_report_key(dev, gc_psx_btn[j], ~data[i][1] & (1 << j)); 711 input_report_key(dev, gc_psx_btn[i], ~data[1] & (1 << i));
538 712
539 input_report_key(dev, BTN_START, ~data[i][0] & 0x08); 713 input_report_key(dev, BTN_START, ~data[0] & 0x08);
540 input_report_key(dev, BTN_SELECT, ~data[i][0] & 0x01); 714 input_report_key(dev, BTN_SELECT, ~data[0] & 0x01);
541 715
542 input_sync(dev); 716 input_sync(dev);
543 717
544 break; 718 break;
545 719
546 case 0: /* not a pad, ignore */ 720 default: /* not a pad, ignore */
547 break; 721 break;
548 } 722 }
723}
724
725static 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];
729 struct gc_pad *pad;
730 int i;
731
732 gc_psx_read_packet(gc, data, id);
733
734 for (i = 0; i < GC_MAX_DEVICES; i++) {
735 pad = &gc->pads[i];
736 if (pad->type == GC_PSX || pad->type == GC_DDR)
737 gc_psx_report_one(pad, id[i], data[i]);
549 } 738 }
550} 739}
551 740
@@ -561,28 +750,31 @@ static void gc_timer(unsigned long private)
561 * N64 pads - must be read first, any read confuses them for 200 us 750 * N64 pads - must be read first, any read confuses them for 200 us
562 */ 751 */
563 752
564 if (gc->pads[GC_N64]) 753 if (gc->pad_count[GC_N64])
565 gc_n64_process_packet(gc); 754 gc_n64_process_packet(gc);
566 755
567/* 756/*
568 * NES and SNES pads or mouse 757 * NES and SNES pads or mouse
569 */ 758 */
570 759
571 if (gc->pads[GC_NES] || gc->pads[GC_SNES] || gc->pads[GC_SNESMOUSE]) 760 if (gc->pad_count[GC_NES] ||
761 gc->pad_count[GC_SNES] ||
762 gc->pad_count[GC_SNESMOUSE]) {
572 gc_nes_process_packet(gc); 763 gc_nes_process_packet(gc);
764 }
573 765
574/* 766/*
575 * Multi and Multi2 joysticks 767 * Multi and Multi2 joysticks
576 */ 768 */
577 769
578 if (gc->pads[GC_MULTI] || gc->pads[GC_MULTI2]) 770 if (gc->pad_count[GC_MULTI] || gc->pad_count[GC_MULTI2])
579 gc_multi_process_packet(gc); 771 gc_multi_process_packet(gc);
580 772
581/* 773/*
582 * PSX controllers 774 * PSX controllers
583 */ 775 */
584 776
585 if (gc->pads[GC_PSX] || gc->pads[GC_DDR]) 777 if (gc->pad_count[GC_PSX] || gc->pad_count[GC_DDR])
586 gc_psx_process_packet(gc); 778 gc_psx_process_packet(gc);
587 779
588 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME); 780 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
@@ -622,25 +814,29 @@ static void gc_close(struct input_dev *dev)
622 814
623static int __init gc_setup_pad(struct gc *gc, int idx, int pad_type) 815static int __init gc_setup_pad(struct gc *gc, int idx, int pad_type)
624{ 816{
817 struct gc_pad *pad = &gc->pads[idx];
625 struct input_dev *input_dev; 818 struct input_dev *input_dev;
626 int i; 819 int i;
627 820 int err;
628 if (!pad_type)
629 return 0;
630 821
631 if (pad_type < 1 || pad_type > GC_MAX) { 822 if (pad_type < 1 || pad_type > GC_MAX) {
632 printk(KERN_WARNING "gamecon.c: Pad type %d unknown\n", pad_type); 823 pr_err("Pad type %d unknown\n", pad_type);
633 return -EINVAL; 824 return -EINVAL;
634 } 825 }
635 826
636 gc->dev[idx] = input_dev = input_allocate_device(); 827 pad->dev = input_dev = input_allocate_device();
637 if (!input_dev) { 828 if (!input_dev) {
638 printk(KERN_ERR "gamecon.c: Not enough memory for input device\n"); 829 pr_err("Not enough memory for input device\n");
639 return -ENOMEM; 830 return -ENOMEM;
640 } 831 }
641 832
833 pad->type = pad_type;
834
835 snprintf(pad->phys, sizeof(pad->phys),
836 "%s/input%d", gc->pd->port->name, idx);
837
642 input_dev->name = gc_names[pad_type]; 838 input_dev->name = gc_names[pad_type];
643 input_dev->phys = gc->phys[idx]; 839 input_dev->phys = pad->phys;
644 input_dev->id.bustype = BUS_PARPORT; 840 input_dev->id.bustype = BUS_PARPORT;
645 input_dev->id.vendor = 0x0001; 841 input_dev->id.vendor = 0x0001;
646 input_dev->id.product = pad_type; 842 input_dev->id.product = pad_type;
@@ -659,61 +855,76 @@ static int __init gc_setup_pad(struct gc *gc, int idx, int pad_type)
659 } else 855 } else
660 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL); 856 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
661 857
662 gc->pads[0] |= gc_status_bit[idx]; 858 gc->pad_count[pad_type]++;
663 gc->pads[pad_type] |= gc_status_bit[idx];
664 859
665 switch (pad_type) { 860 switch (pad_type) {
666 861
667 case GC_N64: 862 case GC_N64:
668 for (i = 0; i < 10; i++) 863 for (i = 0; i < 10; i++)
669 set_bit(gc_n64_btn[i], input_dev->keybit); 864 __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 865
705 break; 866 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 }
706 870
707 case GC_DDR: 871 err = gc_n64_init_ff(input_dev, idx);
708 for (i = 0; i < 4; i++) 872 if (err) {
709 set_bit(gc_psx_ddr_btn[i], input_dev->keybit); 873 pr_warning("Failed to initiate rumble for N64 device %d\n", idx);
710 for (i = 0; i < 12; i++) 874 goto err_free_dev;
711 set_bit(gc_psx_btn[i], input_dev->keybit); 875 }
712 876
713 break; 877 break;
878
879 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;
885
886 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;
893
894 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;
899
900 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);
906
907 break;
908
909 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);
914
915 break;
714 } 916 }
715 917
918 err = input_register_device(pad->dev);
919 if (err)
920 goto err_free_dev;
921
716 return 0; 922 return 0;
923
924err_free_dev:
925 input_free_device(pad->dev);
926 pad->dev = NULL;
927 return err;
717} 928}
718 929
719static struct gc __init *gc_probe(int parport, int *pads, int n_pads) 930static struct gc __init *gc_probe(int parport, int *pads, int n_pads)
@@ -722,52 +933,47 @@ static struct gc __init *gc_probe(int parport, int *pads, int n_pads)
722 struct parport *pp; 933 struct parport *pp;
723 struct pardevice *pd; 934 struct pardevice *pd;
724 int i; 935 int i;
936 int count = 0;
725 int err; 937 int err;
726 938
727 pp = parport_find_number(parport); 939 pp = parport_find_number(parport);
728 if (!pp) { 940 if (!pp) {
729 printk(KERN_ERR "gamecon.c: no such parport\n"); 941 pr_err("no such parport %d\n", parport);
730 err = -EINVAL; 942 err = -EINVAL;
731 goto err_out; 943 goto err_out;
732 } 944 }
733 945
734 pd = parport_register_device(pp, "gamecon", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL); 946 pd = parport_register_device(pp, "gamecon", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
735 if (!pd) { 947 if (!pd) {
736 printk(KERN_ERR "gamecon.c: parport busy already - lp.o loaded?\n"); 948 pr_err("parport busy already - lp.o loaded?\n");
737 err = -EBUSY; 949 err = -EBUSY;
738 goto err_put_pp; 950 goto err_put_pp;
739 } 951 }
740 952
741 gc = kzalloc(sizeof(struct gc), GFP_KERNEL); 953 gc = kzalloc(sizeof(struct gc), GFP_KERNEL);
742 if (!gc) { 954 if (!gc) {
743 printk(KERN_ERR "gamecon.c: Not enough memory\n"); 955 pr_err("Not enough memory\n");
744 err = -ENOMEM; 956 err = -ENOMEM;
745 goto err_unreg_pardev; 957 goto err_unreg_pardev;
746 } 958 }
747 959
748 mutex_init(&gc->mutex); 960 mutex_init(&gc->mutex);
749 gc->pd = pd; 961 gc->pd = pd;
750 init_timer(&gc->timer); 962 setup_timer(&gc->timer, gc_timer, (long) gc);
751 gc->timer.data = (long) gc;
752 gc->timer.function = gc_timer;
753 963
754 for (i = 0; i < n_pads && i < GC_MAX_DEVICES; i++) { 964 for (i = 0; i < n_pads && i < GC_MAX_DEVICES; i++) {
755 if (!pads[i]) 965 if (!pads[i])
756 continue; 966 continue;
757 967
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]); 968 err = gc_setup_pad(gc, i, pads[i]);
761 if (err) 969 if (err)
762 goto err_unreg_devs; 970 goto err_unreg_devs;
763 971
764 err = input_register_device(gc->dev[i]); 972 count++;
765 if (err)
766 goto err_free_dev;
767 } 973 }
768 974
769 if (!gc->pads[0]) { 975 if (count == 0) {
770 printk(KERN_ERR "gamecon.c: No valid devices specified\n"); 976 pr_err("No valid devices specified\n");
771 err = -EINVAL; 977 err = -EINVAL;
772 goto err_free_gc; 978 goto err_free_gc;
773 } 979 }
@@ -775,12 +981,10 @@ static struct gc __init *gc_probe(int parport, int *pads, int n_pads)
775 parport_put_port(pp); 981 parport_put_port(pp);
776 return gc; 982 return gc;
777 983
778 err_free_dev:
779 input_free_device(gc->dev[i]);
780 err_unreg_devs: 984 err_unreg_devs:
781 while (--i >= 0) 985 while (--i >= 0)
782 if (gc->dev[i]) 986 if (gc->pads[i].dev)
783 input_unregister_device(gc->dev[i]); 987 input_unregister_device(gc->pads[i].dev);
784 err_free_gc: 988 err_free_gc:
785 kfree(gc); 989 kfree(gc);
786 err_unreg_pardev: 990 err_unreg_pardev:
@@ -796,8 +1000,8 @@ static void gc_remove(struct gc *gc)
796 int i; 1000 int i;
797 1001
798 for (i = 0; i < GC_MAX_DEVICES; i++) 1002 for (i = 0; i < GC_MAX_DEVICES; i++)
799 if (gc->dev[i]) 1003 if (gc->pads[i].dev)
800 input_unregister_device(gc->dev[i]); 1004 input_unregister_device(gc->pads[i].dev);
801 parport_unregister_device(gc->pd); 1005 parport_unregister_device(gc->pd);
802 kfree(gc); 1006 kfree(gc);
803} 1007}
@@ -813,7 +1017,7 @@ static int __init gc_init(void)
813 continue; 1017 continue;
814 1018
815 if (gc_cfg[i].nargs < 2) { 1019 if (gc_cfg[i].nargs < 2) {
816 printk(KERN_ERR "gamecon.c: at least one device must be specified\n"); 1020 pr_err("at least one device must be specified\n");
817 err = -EINVAL; 1021 err = -EINVAL;
818 break; 1022 break;
819 } 1023 }
diff --git a/drivers/input/joystick/xpad.c b/drivers/input/joystick/xpad.c
index 8a28fb7846d..9b3353b404d 100644
--- a/drivers/input/joystick/xpad.c
+++ b/drivers/input/joystick/xpad.c
@@ -86,9 +86,8 @@
86 86
87/* xbox d-pads should map to buttons, as is required for DDR pads 87/* xbox d-pads should map to buttons, as is required for DDR pads
88 but we map them to axes when possible to simplify things */ 88 but we map them to axes when possible to simplify things */
89#define MAP_DPAD_TO_BUTTONS 0 89#define MAP_DPAD_TO_BUTTONS (1 << 0)
90#define MAP_DPAD_TO_AXES 1 90#define MAP_TRIGGERS_TO_BUTTONS (1 << 1)
91#define MAP_DPAD_UNKNOWN 2
92 91
93#define XTYPE_XBOX 0 92#define XTYPE_XBOX 0
94#define XTYPE_XBOX360 1 93#define XTYPE_XBOX360 1
@@ -99,57 +98,61 @@ static int dpad_to_buttons;
99module_param(dpad_to_buttons, bool, S_IRUGO); 98module_param(dpad_to_buttons, bool, S_IRUGO);
100MODULE_PARM_DESC(dpad_to_buttons, "Map D-PAD to buttons rather than axes for unknown pads"); 99MODULE_PARM_DESC(dpad_to_buttons, "Map D-PAD to buttons rather than axes for unknown pads");
101 100
101static int triggers_to_buttons;
102module_param(triggers_to_buttons, bool, S_IRUGO);
103MODULE_PARM_DESC(triggers_to_buttons, "Map triggers to buttons rather than axes for unknown pads");
104
102static const struct xpad_device { 105static const struct xpad_device {
103 u16 idVendor; 106 u16 idVendor;
104 u16 idProduct; 107 u16 idProduct;
105 char *name; 108 char *name;
106 u8 dpad_mapping; 109 u8 mapping;
107 u8 xtype; 110 u8 xtype;
108} xpad_device[] = { 111} xpad_device[] = {
109 { 0x045e, 0x0202, "Microsoft X-Box pad v1 (US)", MAP_DPAD_TO_AXES, XTYPE_XBOX }, 112 { 0x045e, 0x0202, "Microsoft X-Box pad v1 (US)", 0, XTYPE_XBOX },
110 { 0x045e, 0x0289, "Microsoft X-Box pad v2 (US)", MAP_DPAD_TO_AXES, XTYPE_XBOX }, 113 { 0x045e, 0x0289, "Microsoft X-Box pad v2 (US)", 0, XTYPE_XBOX },
111 { 0x045e, 0x0285, "Microsoft X-Box pad (Japan)", MAP_DPAD_TO_AXES, XTYPE_XBOX }, 114 { 0x045e, 0x0285, "Microsoft X-Box pad (Japan)", 0, XTYPE_XBOX },
112 { 0x045e, 0x0287, "Microsoft Xbox Controller S", MAP_DPAD_TO_AXES, XTYPE_XBOX }, 115 { 0x045e, 0x0287, "Microsoft Xbox Controller S", 0, XTYPE_XBOX },
113 { 0x045e, 0x0719, "Xbox 360 Wireless Receiver", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360W }, 116 { 0x045e, 0x0719, "Xbox 360 Wireless Receiver", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360W },
114 { 0x0c12, 0x8809, "RedOctane Xbox Dance Pad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX }, 117 { 0x0c12, 0x8809, "RedOctane Xbox Dance Pad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
115 { 0x044f, 0x0f07, "Thrustmaster, Inc. Controller", MAP_DPAD_TO_AXES, XTYPE_XBOX }, 118 { 0x044f, 0x0f07, "Thrustmaster, Inc. Controller", 0, XTYPE_XBOX },
116 { 0x046d, 0xc242, "Logitech Chillstream Controller", MAP_DPAD_TO_AXES, XTYPE_XBOX360 }, 119 { 0x046d, 0xc242, "Logitech Chillstream Controller", 0, XTYPE_XBOX360 },
117 { 0x046d, 0xca84, "Logitech Xbox Cordless Controller", MAP_DPAD_TO_AXES, XTYPE_XBOX }, 120 { 0x046d, 0xca84, "Logitech Xbox Cordless Controller", 0, XTYPE_XBOX },
118 { 0x046d, 0xca88, "Logitech Compact Controller for Xbox", MAP_DPAD_TO_AXES, XTYPE_XBOX }, 121 { 0x046d, 0xca88, "Logitech Compact Controller for Xbox", 0, XTYPE_XBOX },
119 { 0x05fd, 0x1007, "Mad Catz Controller (unverified)", MAP_DPAD_TO_AXES, XTYPE_XBOX }, 122 { 0x05fd, 0x1007, "Mad Catz Controller (unverified)", 0, XTYPE_XBOX },
120 { 0x05fd, 0x107a, "InterAct 'PowerPad Pro' X-Box pad (Germany)", MAP_DPAD_TO_AXES, XTYPE_XBOX }, 123 { 0x05fd, 0x107a, "InterAct 'PowerPad Pro' X-Box pad (Germany)", 0, XTYPE_XBOX },
121 { 0x0738, 0x4516, "Mad Catz Control Pad", MAP_DPAD_TO_AXES, XTYPE_XBOX }, 124 { 0x0738, 0x4516, "Mad Catz Control Pad", 0, XTYPE_XBOX },
122 { 0x0738, 0x4522, "Mad Catz LumiCON", MAP_DPAD_TO_AXES, XTYPE_XBOX }, 125 { 0x0738, 0x4522, "Mad Catz LumiCON", 0, XTYPE_XBOX },
123 { 0x0738, 0x4526, "Mad Catz Control Pad Pro", MAP_DPAD_TO_AXES, XTYPE_XBOX }, 126 { 0x0738, 0x4526, "Mad Catz Control Pad Pro", 0, XTYPE_XBOX },
124 { 0x0738, 0x4536, "Mad Catz MicroCON", MAP_DPAD_TO_AXES, XTYPE_XBOX }, 127 { 0x0738, 0x4536, "Mad Catz MicroCON", 0, XTYPE_XBOX },
125 { 0x0738, 0x4540, "Mad Catz Beat Pad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX }, 128 { 0x0738, 0x4540, "Mad Catz Beat Pad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
126 { 0x0738, 0x4556, "Mad Catz Lynx Wireless Controller", MAP_DPAD_TO_AXES, XTYPE_XBOX }, 129 { 0x0738, 0x4556, "Mad Catz Lynx Wireless Controller", 0, XTYPE_XBOX },
127 { 0x0738, 0x4716, "Mad Catz Wired Xbox 360 Controller", MAP_DPAD_TO_AXES, XTYPE_XBOX360 }, 130 { 0x0738, 0x4716, "Mad Catz Wired Xbox 360 Controller", 0, XTYPE_XBOX360 },
128 { 0x0738, 0x4738, "Mad Catz Wired Xbox 360 Controller (SFIV)", MAP_DPAD_TO_AXES, XTYPE_XBOX360 }, 131 { 0x0738, 0x4738, "Mad Catz Wired Xbox 360 Controller (SFIV)", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
129 { 0x0738, 0x6040, "Mad Catz Beat Pad Pro", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX }, 132 { 0x0738, 0x6040, "Mad Catz Beat Pad Pro", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
130 { 0x0c12, 0x8802, "Zeroplus Xbox Controller", MAP_DPAD_TO_AXES, XTYPE_XBOX }, 133 { 0x0c12, 0x8802, "Zeroplus Xbox Controller", 0, XTYPE_XBOX },
131 { 0x0c12, 0x880a, "Pelican Eclipse PL-2023", MAP_DPAD_TO_AXES, XTYPE_XBOX }, 134 { 0x0c12, 0x880a, "Pelican Eclipse PL-2023", 0, XTYPE_XBOX },
132 { 0x0c12, 0x8810, "Zeroplus Xbox Controller", MAP_DPAD_TO_AXES, XTYPE_XBOX }, 135 { 0x0c12, 0x8810, "Zeroplus Xbox Controller", 0, XTYPE_XBOX },
133 { 0x0c12, 0x9902, "HAMA VibraX - *FAULTY HARDWARE*", MAP_DPAD_TO_AXES, XTYPE_XBOX }, 136 { 0x0c12, 0x9902, "HAMA VibraX - *FAULTY HARDWARE*", 0, XTYPE_XBOX },
134 { 0x0e4c, 0x1097, "Radica Gamester Controller", MAP_DPAD_TO_AXES, XTYPE_XBOX }, 137 { 0x0e4c, 0x1097, "Radica Gamester Controller", 0, XTYPE_XBOX },
135 { 0x0e4c, 0x2390, "Radica Games Jtech Controller", MAP_DPAD_TO_AXES, XTYPE_XBOX }, 138 { 0x0e4c, 0x2390, "Radica Games Jtech Controller", 0, XTYPE_XBOX },
136 { 0x0e6f, 0x0003, "Logic3 Freebird wireless Controller", MAP_DPAD_TO_AXES, XTYPE_XBOX }, 139 { 0x0e6f, 0x0003, "Logic3 Freebird wireless Controller", 0, XTYPE_XBOX },
137 { 0x0e6f, 0x0005, "Eclipse wireless Controller", MAP_DPAD_TO_AXES, XTYPE_XBOX }, 140 { 0x0e6f, 0x0005, "Eclipse wireless Controller", 0, XTYPE_XBOX },
138 { 0x0e6f, 0x0006, "Edge wireless Controller", MAP_DPAD_TO_AXES, XTYPE_XBOX }, 141 { 0x0e6f, 0x0006, "Edge wireless Controller", 0, XTYPE_XBOX },
139 { 0x0e6f, 0x0006, "Pelican 'TSZ' Wired Xbox 360 Controller", MAP_DPAD_TO_AXES, XTYPE_XBOX360 }, 142 { 0x0e6f, 0x0006, "Pelican 'TSZ' Wired Xbox 360 Controller", 0, XTYPE_XBOX360 },
140 { 0x0e8f, 0x0201, "SmartJoy Frag Xpad/PS2 adaptor", MAP_DPAD_TO_AXES, XTYPE_XBOX }, 143 { 0x0e8f, 0x0201, "SmartJoy Frag Xpad/PS2 adaptor", 0, XTYPE_XBOX },
141 { 0x0f30, 0x0202, "Joytech Advanced Controller", MAP_DPAD_TO_AXES, XTYPE_XBOX }, 144 { 0x0f30, 0x0202, "Joytech Advanced Controller", 0, XTYPE_XBOX },
142 { 0x0f30, 0x8888, "BigBen XBMiniPad Controller", MAP_DPAD_TO_AXES, XTYPE_XBOX }, 145 { 0x0f30, 0x8888, "BigBen XBMiniPad Controller", 0, XTYPE_XBOX },
143 { 0x102c, 0xff0c, "Joytech Wireless Advanced Controller", MAP_DPAD_TO_AXES, XTYPE_XBOX }, 146 { 0x102c, 0xff0c, "Joytech Wireless Advanced Controller", 0, XTYPE_XBOX },
144 { 0x12ab, 0x8809, "Xbox DDR dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX }, 147 { 0x12ab, 0x8809, "Xbox DDR dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
145 { 0x1430, 0x4748, "RedOctane Guitar Hero X-plorer", MAP_DPAD_TO_AXES, XTYPE_XBOX360 }, 148 { 0x1430, 0x4748, "RedOctane Guitar Hero X-plorer", 0, XTYPE_XBOX360 },
146 { 0x1430, 0x8888, "TX6500+ Dance Pad (first generation)", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX }, 149 { 0x1430, 0x8888, "TX6500+ Dance Pad (first generation)", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
147 { 0x146b, 0x0601, "BigBen Interactive XBOX 360 Controller", MAP_DPAD_TO_AXES, XTYPE_XBOX360 }, 150 { 0x146b, 0x0601, "BigBen Interactive XBOX 360 Controller", 0, XTYPE_XBOX360 },
148 { 0x045e, 0x028e, "Microsoft X-Box 360 pad", MAP_DPAD_TO_AXES, XTYPE_XBOX360 }, 151 { 0x045e, 0x028e, "Microsoft X-Box 360 pad", 0, XTYPE_XBOX360 },
149 { 0x1bad, 0x0003, "Harmonix Rock Band Drumkit", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 }, 152 { 0x1bad, 0x0003, "Harmonix Rock Band Drumkit", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
150 { 0x0f0d, 0x0016, "Hori Real Arcade Pro.EX", MAP_DPAD_TO_AXES, XTYPE_XBOX360 }, 153 { 0x0f0d, 0x0016, "Hori Real Arcade Pro.EX", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
151 { 0xffff, 0xffff, "Chinese-made Xbox Controller", MAP_DPAD_TO_AXES, XTYPE_XBOX }, 154 { 0xffff, 0xffff, "Chinese-made Xbox Controller", 0, XTYPE_XBOX },
152 { 0x0000, 0x0000, "Generic X-Box pad", MAP_DPAD_UNKNOWN, XTYPE_UNKNOWN } 155 { 0x0000, 0x0000, "Generic X-Box pad", 0, XTYPE_UNKNOWN }
153}; 156};
154 157
155/* buttons shared with xbox and xbox360 */ 158/* buttons shared with xbox and xbox360 */
@@ -165,13 +168,20 @@ static const signed short xpad_btn[] = {
165 -1 /* terminating entry */ 168 -1 /* terminating entry */
166}; 169};
167 170
168/* only used if MAP_DPAD_TO_BUTTONS */ 171/* used when dpad is mapped to nuttons */
169static const signed short xpad_btn_pad[] = { 172static const signed short xpad_btn_pad[] = {
170 BTN_LEFT, BTN_RIGHT, /* d-pad left, right */ 173 BTN_LEFT, BTN_RIGHT, /* d-pad left, right */
171 BTN_0, BTN_1, /* d-pad up, down (XXX names??) */ 174 BTN_0, BTN_1, /* d-pad up, down (XXX names??) */
172 -1 /* terminating entry */ 175 -1 /* terminating entry */
173}; 176};
174 177
178/* used when triggers are mapped to buttons */
179static const signed short xpad_btn_triggers[] = {
180 BTN_TL2, BTN_TR2, /* triggers left/right */
181 -1
182};
183
184
175static const signed short xpad360_btn[] = { /* buttons for x360 controller */ 185static const signed short xpad360_btn[] = { /* buttons for x360 controller */
176 BTN_TL, BTN_TR, /* Button LB/RB */ 186 BTN_TL, BTN_TR, /* Button LB/RB */
177 BTN_MODE, /* The big X button */ 187 BTN_MODE, /* The big X button */
@@ -181,16 +191,21 @@ static const signed short xpad360_btn[] = { /* buttons for x360 controller */
181static const signed short xpad_abs[] = { 191static const signed short xpad_abs[] = {
182 ABS_X, ABS_Y, /* left stick */ 192 ABS_X, ABS_Y, /* left stick */
183 ABS_RX, ABS_RY, /* right stick */ 193 ABS_RX, ABS_RY, /* right stick */
184 ABS_Z, ABS_RZ, /* triggers left/right */
185 -1 /* terminating entry */ 194 -1 /* terminating entry */
186}; 195};
187 196
188/* only used if MAP_DPAD_TO_AXES */ 197/* used when dpad is mapped to axes */
189static const signed short xpad_abs_pad[] = { 198static const signed short xpad_abs_pad[] = {
190 ABS_HAT0X, ABS_HAT0Y, /* d-pad axes */ 199 ABS_HAT0X, ABS_HAT0Y, /* d-pad axes */
191 -1 /* terminating entry */ 200 -1 /* terminating entry */
192}; 201};
193 202
203/* used when triggers are mapped to axes */
204static const signed short xpad_abs_triggers[] = {
205 ABS_Z, ABS_RZ, /* triggers left/right */
206 -1
207};
208
194/* Xbox 360 has a vendor-specific class, so we cannot match it with only 209/* Xbox 360 has a vendor-specific class, so we cannot match it with only
195 * USB_INTERFACE_INFO (also specifically refused by USB subsystem), so we 210 * USB_INTERFACE_INFO (also specifically refused by USB subsystem), so we
196 * match against vendor id as well. Wired Xbox 360 devices have protocol 1, 211 * match against vendor id as well. Wired Xbox 360 devices have protocol 1,
@@ -246,7 +261,7 @@ struct usb_xpad {
246 261
247 char phys[64]; /* physical device path */ 262 char phys[64]; /* physical device path */
248 263
249 int dpad_mapping; /* map d-pad to buttons or to axes */ 264 int mapping; /* map d-pad to buttons or to axes */
250 int xtype; /* type of xbox device */ 265 int xtype; /* type of xbox device */
251}; 266};
252 267
@@ -277,20 +292,25 @@ static void xpad_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *d
277 ~(__s16) le16_to_cpup((__le16 *)(data + 18))); 292 ~(__s16) le16_to_cpup((__le16 *)(data + 18)));
278 293
279 /* triggers left/right */ 294 /* triggers left/right */
280 input_report_abs(dev, ABS_Z, data[10]); 295 if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
281 input_report_abs(dev, ABS_RZ, data[11]); 296 input_report_key(dev, BTN_TL2, data[10]);
297 input_report_key(dev, BTN_TR2, data[11]);
298 } else {
299 input_report_abs(dev, ABS_Z, data[10]);
300 input_report_abs(dev, ABS_RZ, data[11]);
301 }
282 302
283 /* digital pad */ 303 /* digital pad */
284 if (xpad->dpad_mapping == MAP_DPAD_TO_AXES) { 304 if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
285 input_report_abs(dev, ABS_HAT0X,
286 !!(data[2] & 0x08) - !!(data[2] & 0x04));
287 input_report_abs(dev, ABS_HAT0Y,
288 !!(data[2] & 0x02) - !!(data[2] & 0x01));
289 } else /* xpad->dpad_mapping == MAP_DPAD_TO_BUTTONS */ {
290 input_report_key(dev, BTN_LEFT, data[2] & 0x04); 305 input_report_key(dev, BTN_LEFT, data[2] & 0x04);
291 input_report_key(dev, BTN_RIGHT, data[2] & 0x08); 306 input_report_key(dev, BTN_RIGHT, data[2] & 0x08);
292 input_report_key(dev, BTN_0, data[2] & 0x01); /* up */ 307 input_report_key(dev, BTN_0, data[2] & 0x01); /* up */
293 input_report_key(dev, BTN_1, data[2] & 0x02); /* down */ 308 input_report_key(dev, BTN_1, data[2] & 0x02); /* down */
309 } else {
310 input_report_abs(dev, ABS_HAT0X,
311 !!(data[2] & 0x08) - !!(data[2] & 0x04));
312 input_report_abs(dev, ABS_HAT0Y,
313 !!(data[2] & 0x02) - !!(data[2] & 0x01));
294 } 314 }
295 315
296 /* start/back buttons and stick press left/right */ 316 /* start/back buttons and stick press left/right */
@@ -328,17 +348,17 @@ static void xpad360_process_packet(struct usb_xpad *xpad,
328 struct input_dev *dev = xpad->dev; 348 struct input_dev *dev = xpad->dev;
329 349
330 /* digital pad */ 350 /* digital pad */
331 if (xpad->dpad_mapping == MAP_DPAD_TO_AXES) { 351 if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
332 input_report_abs(dev, ABS_HAT0X,
333 !!(data[2] & 0x08) - !!(data[2] & 0x04));
334 input_report_abs(dev, ABS_HAT0Y,
335 !!(data[2] & 0x02) - !!(data[2] & 0x01));
336 } else if (xpad->dpad_mapping == MAP_DPAD_TO_BUTTONS) {
337 /* dpad as buttons (right, left, down, up) */ 352 /* dpad as buttons (right, left, down, up) */
338 input_report_key(dev, BTN_LEFT, data[2] & 0x04); 353 input_report_key(dev, BTN_LEFT, data[2] & 0x04);
339 input_report_key(dev, BTN_RIGHT, data[2] & 0x08); 354 input_report_key(dev, BTN_RIGHT, data[2] & 0x08);
340 input_report_key(dev, BTN_0, data[2] & 0x01); /* up */ 355 input_report_key(dev, BTN_0, data[2] & 0x01); /* up */
341 input_report_key(dev, BTN_1, data[2] & 0x02); /* down */ 356 input_report_key(dev, BTN_1, data[2] & 0x02); /* down */
357 } else {
358 input_report_abs(dev, ABS_HAT0X,
359 !!(data[2] & 0x08) - !!(data[2] & 0x04));
360 input_report_abs(dev, ABS_HAT0Y,
361 !!(data[2] & 0x02) - !!(data[2] & 0x01));
342 } 362 }
343 363
344 /* start/back buttons */ 364 /* start/back buttons */
@@ -371,8 +391,13 @@ static void xpad360_process_packet(struct usb_xpad *xpad,
371 ~(__s16) le16_to_cpup((__le16 *)(data + 12))); 391 ~(__s16) le16_to_cpup((__le16 *)(data + 12)));
372 392
373 /* triggers left/right */ 393 /* triggers left/right */
374 input_report_abs(dev, ABS_Z, data[4]); 394 if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
375 input_report_abs(dev, ABS_RZ, data[5]); 395 input_report_key(dev, BTN_TL2, data[4]);
396 input_report_key(dev, BTN_TR2, data[5]);
397 } else {
398 input_report_abs(dev, ABS_Z, data[4]);
399 input_report_abs(dev, ABS_RZ, data[5]);
400 }
376 401
377 input_sync(dev); 402 input_sync(dev);
378} 403}
@@ -505,7 +530,7 @@ static int xpad_init_output(struct usb_interface *intf, struct usb_xpad *xpad)
505 struct usb_endpoint_descriptor *ep_irq_out; 530 struct usb_endpoint_descriptor *ep_irq_out;
506 int error = -ENOMEM; 531 int error = -ENOMEM;
507 532
508 if (xpad->xtype != XTYPE_XBOX360) 533 if (xpad->xtype != XTYPE_XBOX360 && xpad->xtype != XTYPE_XBOX)
509 return 0; 534 return 0;
510 535
511 xpad->odata = usb_buffer_alloc(xpad->udev, XPAD_PKT_LEN, 536 xpad->odata = usb_buffer_alloc(xpad->udev, XPAD_PKT_LEN,
@@ -535,13 +560,13 @@ static int xpad_init_output(struct usb_interface *intf, struct usb_xpad *xpad)
535 560
536static void xpad_stop_output(struct usb_xpad *xpad) 561static void xpad_stop_output(struct usb_xpad *xpad)
537{ 562{
538 if (xpad->xtype == XTYPE_XBOX360) 563 if (xpad->xtype == XTYPE_XBOX360 || xpad->xtype == XTYPE_XBOX)
539 usb_kill_urb(xpad->irq_out); 564 usb_kill_urb(xpad->irq_out);
540} 565}
541 566
542static void xpad_deinit_output(struct usb_xpad *xpad) 567static void xpad_deinit_output(struct usb_xpad *xpad)
543{ 568{
544 if (xpad->xtype == XTYPE_XBOX360) { 569 if (xpad->xtype == XTYPE_XBOX360 || xpad->xtype == XTYPE_XBOX) {
545 usb_free_urb(xpad->irq_out); 570 usb_free_urb(xpad->irq_out);
546 usb_buffer_free(xpad->udev, XPAD_PKT_LEN, 571 usb_buffer_free(xpad->udev, XPAD_PKT_LEN,
547 xpad->odata, xpad->odata_dma); 572 xpad->odata, xpad->odata_dma);
@@ -554,24 +579,45 @@ static void xpad_stop_output(struct usb_xpad *xpad) {}
554#endif 579#endif
555 580
556#ifdef CONFIG_JOYSTICK_XPAD_FF 581#ifdef CONFIG_JOYSTICK_XPAD_FF
557static int xpad_play_effect(struct input_dev *dev, void *data, 582static int xpad_play_effect(struct input_dev *dev, void *data, struct ff_effect *effect)
558 struct ff_effect *effect)
559{ 583{
560 struct usb_xpad *xpad = input_get_drvdata(dev); 584 struct usb_xpad *xpad = input_get_drvdata(dev);
561 585
562 if (effect->type == FF_RUMBLE) { 586 if (effect->type == FF_RUMBLE) {
563 __u16 strong = effect->u.rumble.strong_magnitude; 587 __u16 strong = effect->u.rumble.strong_magnitude;
564 __u16 weak = effect->u.rumble.weak_magnitude; 588 __u16 weak = effect->u.rumble.weak_magnitude;
565 xpad->odata[0] = 0x00; 589
566 xpad->odata[1] = 0x08; 590 switch (xpad->xtype) {
567 xpad->odata[2] = 0x00; 591
568 xpad->odata[3] = strong / 256; 592 case XTYPE_XBOX:
569 xpad->odata[4] = weak / 256; 593 xpad->odata[0] = 0x00;
570 xpad->odata[5] = 0x00; 594 xpad->odata[1] = 0x06;
571 xpad->odata[6] = 0x00; 595 xpad->odata[2] = 0x00;
572 xpad->odata[7] = 0x00; 596 xpad->odata[3] = strong / 256; /* left actuator */
573 xpad->irq_out->transfer_buffer_length = 8; 597 xpad->odata[4] = 0x00;
574 usb_submit_urb(xpad->irq_out, GFP_ATOMIC); 598 xpad->odata[5] = weak / 256; /* right actuator */
599 xpad->irq_out->transfer_buffer_length = 6;
600
601 return usb_submit_urb(xpad->irq_out, GFP_ATOMIC);
602
603 case XTYPE_XBOX360:
604 xpad->odata[0] = 0x00;
605 xpad->odata[1] = 0x08;
606 xpad->odata[2] = 0x00;
607 xpad->odata[3] = strong / 256; /* left actuator? */
608 xpad->odata[4] = weak / 256; /* right actuator? */
609 xpad->odata[5] = 0x00;
610 xpad->odata[6] = 0x00;
611 xpad->odata[7] = 0x00;
612 xpad->irq_out->transfer_buffer_length = 8;
613
614 return usb_submit_urb(xpad->irq_out, GFP_ATOMIC);
615
616 default:
617 dbg("%s - rumble command sent to unsupported xpad type: %d",
618 __func__, xpad->xtype);
619 return -1;
620 }
575 } 621 }
576 622
577 return 0; 623 return 0;
@@ -579,7 +625,7 @@ static int xpad_play_effect(struct input_dev *dev, void *data,
579 625
580static int xpad_init_ff(struct usb_xpad *xpad) 626static int xpad_init_ff(struct usb_xpad *xpad)
581{ 627{
582 if (xpad->xtype != XTYPE_XBOX360) 628 if (xpad->xtype != XTYPE_XBOX360 && xpad->xtype != XTYPE_XBOX)
583 return 0; 629 return 0;
584 630
585 input_set_capability(xpad->dev, EV_FF, FF_RUMBLE); 631 input_set_capability(xpad->dev, EV_FF, FF_RUMBLE);
@@ -712,11 +758,11 @@ static void xpad_set_up_abs(struct input_dev *input_dev, signed short abs)
712 input_set_abs_params(input_dev, abs, -32768, 32767, 16, 128); 758 input_set_abs_params(input_dev, abs, -32768, 32767, 16, 128);
713 break; 759 break;
714 case ABS_Z: 760 case ABS_Z:
715 case ABS_RZ: /* the triggers */ 761 case ABS_RZ: /* the triggers (if mapped to axes) */
716 input_set_abs_params(input_dev, abs, 0, 255, 0, 0); 762 input_set_abs_params(input_dev, abs, 0, 255, 0, 0);
717 break; 763 break;
718 case ABS_HAT0X: 764 case ABS_HAT0X:
719 case ABS_HAT0Y: /* the d-pad (only if MAP_DPAD_TO_AXES) */ 765 case ABS_HAT0Y: /* the d-pad (only if dpad is mapped to axes */
720 input_set_abs_params(input_dev, abs, -1, 1, 0, 0); 766 input_set_abs_params(input_dev, abs, -1, 1, 0, 0);
721 break; 767 break;
722 } 768 }
@@ -752,10 +798,9 @@ static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id
752 goto fail2; 798 goto fail2;
753 799
754 xpad->udev = udev; 800 xpad->udev = udev;
755 xpad->dpad_mapping = xpad_device[i].dpad_mapping; 801 xpad->mapping = xpad_device[i].mapping;
756 xpad->xtype = xpad_device[i].xtype; 802 xpad->xtype = xpad_device[i].xtype;
757 if (xpad->dpad_mapping == MAP_DPAD_UNKNOWN) 803
758 xpad->dpad_mapping = !dpad_to_buttons;
759 if (xpad->xtype == XTYPE_UNKNOWN) { 804 if (xpad->xtype == XTYPE_UNKNOWN) {
760 if (intf->cur_altsetting->desc.bInterfaceClass == USB_CLASS_VENDOR_SPEC) { 805 if (intf->cur_altsetting->desc.bInterfaceClass == USB_CLASS_VENDOR_SPEC) {
761 if (intf->cur_altsetting->desc.bInterfaceProtocol == 129) 806 if (intf->cur_altsetting->desc.bInterfaceProtocol == 129)
@@ -764,7 +809,13 @@ static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id
764 xpad->xtype = XTYPE_XBOX360; 809 xpad->xtype = XTYPE_XBOX360;
765 } else 810 } else
766 xpad->xtype = XTYPE_XBOX; 811 xpad->xtype = XTYPE_XBOX;
812
813 if (dpad_to_buttons)
814 xpad->mapping |= MAP_DPAD_TO_BUTTONS;
815 if (triggers_to_buttons)
816 xpad->mapping |= MAP_TRIGGERS_TO_BUTTONS;
767 } 817 }
818
768 xpad->dev = input_dev; 819 xpad->dev = input_dev;
769 usb_make_path(udev, xpad->phys, sizeof(xpad->phys)); 820 usb_make_path(udev, xpad->phys, sizeof(xpad->phys));
770 strlcat(xpad->phys, "/input0", sizeof(xpad->phys)); 821 strlcat(xpad->phys, "/input0", sizeof(xpad->phys));
@@ -781,25 +832,37 @@ static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id
781 832
782 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); 833 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
783 834
784 /* set up buttons */ 835 /* set up standard buttons and axes */
785 for (i = 0; xpad_common_btn[i] >= 0; i++) 836 for (i = 0; xpad_common_btn[i] >= 0; i++)
786 set_bit(xpad_common_btn[i], input_dev->keybit); 837 __set_bit(xpad_common_btn[i], input_dev->keybit);
787 if ((xpad->xtype == XTYPE_XBOX360) || (xpad->xtype == XTYPE_XBOX360W))
788 for (i = 0; xpad360_btn[i] >= 0; i++)
789 set_bit(xpad360_btn[i], input_dev->keybit);
790 else
791 for (i = 0; xpad_btn[i] >= 0; i++)
792 set_bit(xpad_btn[i], input_dev->keybit);
793 if (xpad->dpad_mapping == MAP_DPAD_TO_BUTTONS)
794 for (i = 0; xpad_btn_pad[i] >= 0; i++)
795 set_bit(xpad_btn_pad[i], input_dev->keybit);
796 838
797 /* set up axes */
798 for (i = 0; xpad_abs[i] >= 0; i++) 839 for (i = 0; xpad_abs[i] >= 0; i++)
799 xpad_set_up_abs(input_dev, xpad_abs[i]); 840 xpad_set_up_abs(input_dev, xpad_abs[i]);
800 if (xpad->dpad_mapping == MAP_DPAD_TO_AXES) 841
842 /* Now set up model-specific ones */
843 if (xpad->xtype == XTYPE_XBOX360 || xpad->xtype == XTYPE_XBOX360W) {
844 for (i = 0; xpad360_btn[i] >= 0; i++)
845 __set_bit(xpad360_btn[i], input_dev->keybit);
846 } else {
847 for (i = 0; xpad_btn[i] >= 0; i++)
848 __set_bit(xpad_btn[i], input_dev->keybit);
849 }
850
851 if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
852 for (i = 0; xpad_btn_pad[i] >= 0; i++)
853 __set_bit(xpad_btn_pad[i], input_dev->keybit);
854 } else {
801 for (i = 0; xpad_abs_pad[i] >= 0; i++) 855 for (i = 0; xpad_abs_pad[i] >= 0; i++)
802 xpad_set_up_abs(input_dev, xpad_abs_pad[i]); 856 xpad_set_up_abs(input_dev, xpad_abs_pad[i]);
857 }
858
859 if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
860 for (i = 0; xpad_btn_triggers[i] >= 0; i++)
861 __set_bit(xpad_btn_triggers[i], input_dev->keybit);
862 } else {
863 for (i = 0; xpad_abs_triggers[i] >= 0; i++)
864 xpad_set_up_abs(input_dev, xpad_abs_triggers[i]);
865 }
803 866
804 error = xpad_init_output(intf, xpad); 867 error = xpad_init_output(intf, xpad);
805 if (error) 868 if (error)