Input: xpad - handle all variations of Mad Catz Beat Pad
[pandora-kernel.git] / drivers / input / joystick / xpad.c
1 /*
2  * X-Box gamepad driver
3  *
4  * Copyright (c) 2002 Marko Friedemann <mfr@bmx-chemnitz.de>
5  *               2004 Oliver Schwartz <Oliver.Schwartz@gmx.de>,
6  *                    Steven Toth <steve@toth.demon.co.uk>,
7  *                    Franz Lehner <franz@caos.at>,
8  *                    Ivan Hawkes <blackhawk@ivanhawkes.com>
9  *               2005 Dominic Cerquetti <binary1230@yahoo.com>
10  *               2006 Adam Buchbinder <adam.buchbinder@gmail.com>
11  *               2007 Jan Kratochvil <honza@jikos.cz>
12  *               2010 Christoph Fritz <chf.fritz@googlemail.com>
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License as
16  * published by the Free Software Foundation; either version 2 of
17  * the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27  *
28  *
29  * This driver is based on:
30  *  - information from     http://euc.jp/periphs/xbox-controller.ja.html
31  *  - the iForce driver    drivers/char/joystick/iforce.c
32  *  - the skeleton-driver  drivers/usb/usb-skeleton.c
33  *  - Xbox 360 information http://www.free60.org/wiki/Gamepad
34  *
35  * Thanks to:
36  *  - ITO Takayuki for providing essential xpad information on his website
37  *  - Vojtech Pavlik     - iforce driver / input subsystem
38  *  - Greg Kroah-Hartman - usb-skeleton driver
39  *  - XBOX Linux project - extra USB id's
40  *
41  * TODO:
42  *  - fine tune axes (especially trigger axes)
43  *  - fix "analog" buttons (reported as digital now)
44  *  - get rumble working
45  *  - need USB IDs for other dance pads
46  *
47  * History:
48  *
49  * 2002-06-27 - 0.0.1 : first version, just said "XBOX HID controller"
50  *
51  * 2002-07-02 - 0.0.2 : basic working version
52  *  - all axes and 9 of the 10 buttons work (german InterAct device)
53  *  - the black button does not work
54  *
55  * 2002-07-14 - 0.0.3 : rework by Vojtech Pavlik
56  *  - indentation fixes
57  *  - usb + input init sequence fixes
58  *
59  * 2002-07-16 - 0.0.4 : minor changes, merge with Vojtech's v0.0.3
60  *  - verified the lack of HID and report descriptors
61  *  - verified that ALL buttons WORK
62  *  - fixed d-pad to axes mapping
63  *
64  * 2002-07-17 - 0.0.5 : simplified d-pad handling
65  *
66  * 2004-10-02 - 0.0.6 : DDR pad support
67  *  - borrowed from the XBOX linux kernel
68  *  - USB id's for commonly used dance pads are present
69  *  - dance pads will map D-PAD to buttons, not axes
70  *  - pass the module paramater 'dpad_to_buttons' to force
71  *    the D-PAD to map to buttons if your pad is not detected
72  *
73  * Later changes can be tracked in SCM.
74  */
75
76 #include <linux/kernel.h>
77 #include <linux/init.h>
78 #include <linux/slab.h>
79 #include <linux/stat.h>
80 #include <linux/module.h>
81 #include <linux/usb/input.h>
82
83 #define DRIVER_AUTHOR "Marko Friedemann <mfr@bmx-chemnitz.de>"
84 #define DRIVER_DESC "X-Box pad driver"
85
86 #define XPAD_PKT_LEN 32
87
88 /* xbox d-pads should map to buttons, as is required for DDR pads
89    but we map them to axes when possible to simplify things */
90 #define MAP_DPAD_TO_BUTTONS             (1 << 0)
91 #define MAP_TRIGGERS_TO_BUTTONS         (1 << 1)
92 #define MAP_STICKS_TO_NULL              (1 << 2)
93 #define DANCEPAD_MAP_CONFIG     (MAP_DPAD_TO_BUTTONS |                  \
94                                 MAP_TRIGGERS_TO_BUTTONS | MAP_STICKS_TO_NULL)
95
96 #define XTYPE_XBOX        0
97 #define XTYPE_XBOX360     1
98 #define XTYPE_XBOX360W    2
99 #define XTYPE_UNKNOWN     3
100
101 static int dpad_to_buttons;
102 module_param(dpad_to_buttons, bool, S_IRUGO);
103 MODULE_PARM_DESC(dpad_to_buttons, "Map D-PAD to buttons rather than axes for unknown pads");
104
105 static int triggers_to_buttons;
106 module_param(triggers_to_buttons, bool, S_IRUGO);
107 MODULE_PARM_DESC(triggers_to_buttons, "Map triggers to buttons rather than axes for unknown pads");
108
109 static int sticks_to_null;
110 module_param(sticks_to_null, bool, S_IRUGO);
111 MODULE_PARM_DESC(sticks_to_null, "Do not map sticks at all for unknown pads");
112
113 static const struct xpad_device {
114         u16 idVendor;
115         u16 idProduct;
116         char *name;
117         u8 mapping;
118         u8 xtype;
119 } xpad_device[] = {
120         { 0x045e, 0x0202, "Microsoft X-Box pad v1 (US)", 0, XTYPE_XBOX },
121         { 0x045e, 0x0289, "Microsoft X-Box pad v2 (US)", 0, XTYPE_XBOX },
122         { 0x045e, 0x0285, "Microsoft X-Box pad (Japan)", 0, XTYPE_XBOX },
123         { 0x045e, 0x0287, "Microsoft Xbox Controller S", 0, XTYPE_XBOX },
124         { 0x045e, 0x0719, "Xbox 360 Wireless Receiver", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360W },
125         { 0x0c12, 0x8809, "RedOctane Xbox Dance Pad", DANCEPAD_MAP_CONFIG, XTYPE_XBOX },
126         { 0x044f, 0x0f07, "Thrustmaster, Inc. Controller", 0, XTYPE_XBOX },
127         { 0x046d, 0xc242, "Logitech Chillstream Controller", 0, XTYPE_XBOX360 },
128         { 0x046d, 0xca84, "Logitech Xbox Cordless Controller", 0, XTYPE_XBOX },
129         { 0x046d, 0xca88, "Logitech Compact Controller for Xbox", 0, XTYPE_XBOX },
130         { 0x05fd, 0x1007, "Mad Catz Controller (unverified)", 0, XTYPE_XBOX },
131         { 0x05fd, 0x107a, "InterAct 'PowerPad Pro' X-Box pad (Germany)", 0, XTYPE_XBOX },
132         { 0x0738, 0x4516, "Mad Catz Control Pad", 0, XTYPE_XBOX },
133         { 0x0738, 0x4522, "Mad Catz LumiCON", 0, XTYPE_XBOX },
134         { 0x0738, 0x4526, "Mad Catz Control Pad Pro", 0, XTYPE_XBOX },
135         { 0x0738, 0x4536, "Mad Catz MicroCON", 0, XTYPE_XBOX },
136         { 0x0738, 0x4540, "Mad Catz Beat Pad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
137         { 0x0738, 0x4556, "Mad Catz Lynx Wireless Controller", 0, XTYPE_XBOX },
138         { 0x0738, 0x4716, "Mad Catz Wired Xbox 360 Controller", 0, XTYPE_XBOX360 },
139         { 0x0738, 0x4738, "Mad Catz Wired Xbox 360 Controller (SFIV)", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
140         { 0x0738, 0x6040, "Mad Catz Beat Pad Pro", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
141         { 0x0c12, 0x8802, "Zeroplus Xbox Controller", 0, XTYPE_XBOX },
142         { 0x0c12, 0x880a, "Pelican Eclipse PL-2023", 0, XTYPE_XBOX },
143         { 0x0c12, 0x8810, "Zeroplus Xbox Controller", 0, XTYPE_XBOX },
144         { 0x0c12, 0x9902, "HAMA VibraX - *FAULTY HARDWARE*", 0, XTYPE_XBOX },
145         { 0x0e4c, 0x1097, "Radica Gamester Controller", 0, XTYPE_XBOX },
146         { 0x0e4c, 0x2390, "Radica Games Jtech Controller", 0, XTYPE_XBOX },
147         { 0x0e6f, 0x0003, "Logic3 Freebird wireless Controller", 0, XTYPE_XBOX },
148         { 0x0e6f, 0x0005, "Eclipse wireless Controller", 0, XTYPE_XBOX },
149         { 0x0e6f, 0x0006, "Edge wireless Controller", 0, XTYPE_XBOX },
150         { 0x0e6f, 0x0006, "Pelican 'TSZ' Wired Xbox 360 Controller", 0, XTYPE_XBOX360 },
151         { 0x0e6f, 0x0201, "Pelican PL-3601 'TSZ' Wired Xbox 360 Controller", 0, XTYPE_XBOX360 },
152         { 0x0e8f, 0x0201, "SmartJoy Frag Xpad/PS2 adaptor", 0, XTYPE_XBOX },
153         { 0x0f30, 0x0202, "Joytech Advanced Controller", 0, XTYPE_XBOX },
154         { 0x0f30, 0x8888, "BigBen XBMiniPad Controller", 0, XTYPE_XBOX },
155         { 0x102c, 0xff0c, "Joytech Wireless Advanced Controller", 0, XTYPE_XBOX },
156         { 0x12ab, 0x8809, "Xbox DDR dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
157         { 0x12ab, 0x0004, "Honey Bee Xbox360 dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
158         { 0x0e6f, 0x0105, "HSM3 Xbox360 dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
159         { 0x1430, 0x4748, "RedOctane Guitar Hero X-plorer", 0, XTYPE_XBOX360 },
160         { 0x1430, 0x8888, "TX6500+ Dance Pad (first generation)", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
161         { 0x146b, 0x0601, "BigBen Interactive XBOX 360 Controller", 0, XTYPE_XBOX360 },
162         { 0x045e, 0x028e, "Microsoft X-Box 360 pad", 0, XTYPE_XBOX360 },
163         { 0x1bad, 0x0002, "Harmonix Rock Band Guitar", 0, XTYPE_XBOX360 },
164         { 0x1bad, 0x0003, "Harmonix Rock Band Drumkit", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
165         { 0x0f0d, 0x0016, "Hori Real Arcade Pro.EX", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
166         { 0x0f0d, 0x000d, "Hori Fighting Stick EX2", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
167         { 0xffff, 0xffff, "Chinese-made Xbox Controller", 0, XTYPE_XBOX },
168         { 0x0000, 0x0000, "Generic X-Box pad", 0, XTYPE_UNKNOWN }
169 };
170
171 /* buttons shared with xbox and xbox360 */
172 static const signed short xpad_common_btn[] = {
173         BTN_A, BTN_B, BTN_X, BTN_Y,                     /* "analog" buttons */
174         BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR,  /* start/back/sticks */
175         -1                                              /* terminating entry */
176 };
177
178 /* original xbox controllers only */
179 static const signed short xpad_btn[] = {
180         BTN_C, BTN_Z,           /* "analog" buttons */
181         -1                      /* terminating entry */
182 };
183
184 /* used when dpad is mapped to buttons */
185 static const signed short xpad_btn_pad[] = {
186         BTN_TRIGGER_HAPPY1, BTN_TRIGGER_HAPPY2,         /* d-pad left, right */
187         BTN_TRIGGER_HAPPY3, BTN_TRIGGER_HAPPY4,         /* d-pad up, down */
188         -1                              /* terminating entry */
189 };
190
191 /* used when triggers are mapped to buttons */
192 static const signed short xpad_btn_triggers[] = {
193         BTN_TL2, BTN_TR2,               /* triggers left/right */
194         -1
195 };
196
197
198 static const signed short xpad360_btn[] = {  /* buttons for x360 controller */
199         BTN_TL, BTN_TR,         /* Button LB/RB */
200         BTN_MODE,               /* The big X button */
201         -1
202 };
203
204 static const signed short xpad_abs[] = {
205         ABS_X, ABS_Y,           /* left stick */
206         ABS_RX, ABS_RY,         /* right stick */
207         -1                      /* terminating entry */
208 };
209
210 /* used when dpad is mapped to axes */
211 static const signed short xpad_abs_pad[] = {
212         ABS_HAT0X, ABS_HAT0Y,   /* d-pad axes */
213         -1                      /* terminating entry */
214 };
215
216 /* used when triggers are mapped to axes */
217 static const signed short xpad_abs_triggers[] = {
218         ABS_Z, ABS_RZ,          /* triggers left/right */
219         -1
220 };
221
222 /* Xbox 360 has a vendor-specific class, so we cannot match it with only
223  * USB_INTERFACE_INFO (also specifically refused by USB subsystem), so we
224  * match against vendor id as well. Wired Xbox 360 devices have protocol 1,
225  * wireless controllers have protocol 129. */
226 #define XPAD_XBOX360_VENDOR_PROTOCOL(vend,pr) \
227         .match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_INT_INFO, \
228         .idVendor = (vend), \
229         .bInterfaceClass = USB_CLASS_VENDOR_SPEC, \
230         .bInterfaceSubClass = 93, \
231         .bInterfaceProtocol = (pr)
232 #define XPAD_XBOX360_VENDOR(vend) \
233         { XPAD_XBOX360_VENDOR_PROTOCOL(vend,1) }, \
234         { XPAD_XBOX360_VENDOR_PROTOCOL(vend,129) }
235
236 static struct usb_device_id xpad_table [] = {
237         { USB_INTERFACE_INFO('X', 'B', 0) },    /* X-Box USB-IF not approved class */
238         XPAD_XBOX360_VENDOR(0x045e),            /* Microsoft X-Box 360 controllers */
239         XPAD_XBOX360_VENDOR(0x046d),            /* Logitech X-Box 360 style controllers */
240         XPAD_XBOX360_VENDOR(0x0738),            /* Mad Catz X-Box 360 controllers */
241         { USB_DEVICE(0x0738, 0x4540) },         /* Mad Catz Beat Pad */
242         XPAD_XBOX360_VENDOR(0x0e6f),            /* 0x0e6f X-Box 360 controllers */
243         XPAD_XBOX360_VENDOR(0x12ab),            /* X-Box 360 dance pads */
244         XPAD_XBOX360_VENDOR(0x1430),            /* RedOctane X-Box 360 controllers */
245         XPAD_XBOX360_VENDOR(0x146b),            /* BigBen Interactive Controllers */
246         XPAD_XBOX360_VENDOR(0x1bad),            /* Harminix Rock Band Guitar and Drums */
247         XPAD_XBOX360_VENDOR(0x0f0d),            /* Hori Controllers */
248         { }
249 };
250
251 MODULE_DEVICE_TABLE (usb, xpad_table);
252
253 struct usb_xpad {
254         struct input_dev *dev;          /* input device interface */
255         struct usb_device *udev;        /* usb device */
256
257         int pad_present;
258
259         struct urb *irq_in;             /* urb for interrupt in report */
260         unsigned char *idata;           /* input data */
261         dma_addr_t idata_dma;
262
263         struct urb *bulk_out;
264         unsigned char *bdata;
265
266 #if defined(CONFIG_JOYSTICK_XPAD_FF) || defined(CONFIG_JOYSTICK_XPAD_LEDS)
267         struct urb *irq_out;            /* urb for interrupt out report */
268         unsigned char *odata;           /* output data */
269         dma_addr_t odata_dma;
270         struct mutex odata_mutex;
271 #endif
272
273 #if defined(CONFIG_JOYSTICK_XPAD_LEDS)
274         struct xpad_led *led;
275 #endif
276
277         char phys[64];                  /* physical device path */
278
279         int mapping;                    /* map d-pad to buttons or to axes */
280         int xtype;                      /* type of xbox device */
281 };
282
283 /*
284  *      xpad_process_packet
285  *
286  *      Completes a request by converting the data into events for the
287  *      input subsystem.
288  *
289  *      The used report descriptor was taken from ITO Takayukis website:
290  *       http://euc.jp/periphs/xbox-controller.ja.html
291  */
292
293 static void xpad_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data)
294 {
295         struct input_dev *dev = xpad->dev;
296
297         if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
298                 /* left stick */
299                 input_report_abs(dev, ABS_X,
300                                  (__s16) le16_to_cpup((__le16 *)(data + 12)));
301                 input_report_abs(dev, ABS_Y,
302                                  ~(__s16) le16_to_cpup((__le16 *)(data + 14)));
303
304                 /* right stick */
305                 input_report_abs(dev, ABS_RX,
306                                  (__s16) le16_to_cpup((__le16 *)(data + 16)));
307                 input_report_abs(dev, ABS_RY,
308                                  ~(__s16) le16_to_cpup((__le16 *)(data + 18)));
309         }
310
311         /* triggers left/right */
312         if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
313                 input_report_key(dev, BTN_TL2, data[10]);
314                 input_report_key(dev, BTN_TR2, data[11]);
315         } else {
316                 input_report_abs(dev, ABS_Z, data[10]);
317                 input_report_abs(dev, ABS_RZ, data[11]);
318         }
319
320         /* digital pad */
321         if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
322                 /* dpad as buttons (left, right, up, down) */
323                 input_report_key(dev, BTN_TRIGGER_HAPPY1, data[2] & 0x04);
324                 input_report_key(dev, BTN_TRIGGER_HAPPY2, data[2] & 0x08);
325                 input_report_key(dev, BTN_TRIGGER_HAPPY3, data[2] & 0x01);
326                 input_report_key(dev, BTN_TRIGGER_HAPPY4, data[2] & 0x02);
327         } else {
328                 input_report_abs(dev, ABS_HAT0X,
329                                  !!(data[2] & 0x08) - !!(data[2] & 0x04));
330                 input_report_abs(dev, ABS_HAT0Y,
331                                  !!(data[2] & 0x02) - !!(data[2] & 0x01));
332         }
333
334         /* start/back buttons and stick press left/right */
335         input_report_key(dev, BTN_START,  data[2] & 0x10);
336         input_report_key(dev, BTN_SELECT, data[2] & 0x20);
337         input_report_key(dev, BTN_THUMBL, data[2] & 0x40);
338         input_report_key(dev, BTN_THUMBR, data[2] & 0x80);
339
340         /* "analog" buttons A, B, X, Y */
341         input_report_key(dev, BTN_A, data[4]);
342         input_report_key(dev, BTN_B, data[5]);
343         input_report_key(dev, BTN_X, data[6]);
344         input_report_key(dev, BTN_Y, data[7]);
345
346         /* "analog" buttons black, white */
347         input_report_key(dev, BTN_C, data[8]);
348         input_report_key(dev, BTN_Z, data[9]);
349
350         input_sync(dev);
351 }
352
353 /*
354  *      xpad360_process_packet
355  *
356  *      Completes a request by converting the data into events for the
357  *      input subsystem. It is version for xbox 360 controller
358  *
359  *      The used report descriptor was taken from:
360  *              http://www.free60.org/wiki/Gamepad
361  */
362
363 static void xpad360_process_packet(struct usb_xpad *xpad,
364                                    u16 cmd, unsigned char *data)
365 {
366         struct input_dev *dev = xpad->dev;
367
368         /* digital pad */
369         if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
370                 /* dpad as buttons (left, right, up, down) */
371                 input_report_key(dev, BTN_TRIGGER_HAPPY1, data[2] & 0x04);
372                 input_report_key(dev, BTN_TRIGGER_HAPPY2, data[2] & 0x08);
373                 input_report_key(dev, BTN_TRIGGER_HAPPY3, data[2] & 0x01);
374                 input_report_key(dev, BTN_TRIGGER_HAPPY4, data[2] & 0x02);
375         } else {
376                 input_report_abs(dev, ABS_HAT0X,
377                                  !!(data[2] & 0x08) - !!(data[2] & 0x04));
378                 input_report_abs(dev, ABS_HAT0Y,
379                                  !!(data[2] & 0x02) - !!(data[2] & 0x01));
380         }
381
382         /* start/back buttons */
383         input_report_key(dev, BTN_START,  data[2] & 0x10);
384         input_report_key(dev, BTN_SELECT, data[2] & 0x20);
385
386         /* stick press left/right */
387         input_report_key(dev, BTN_THUMBL, data[2] & 0x40);
388         input_report_key(dev, BTN_THUMBR, data[2] & 0x80);
389
390         /* buttons A,B,X,Y,TL,TR and MODE */
391         input_report_key(dev, BTN_A,    data[3] & 0x10);
392         input_report_key(dev, BTN_B,    data[3] & 0x20);
393         input_report_key(dev, BTN_X,    data[3] & 0x40);
394         input_report_key(dev, BTN_Y,    data[3] & 0x80);
395         input_report_key(dev, BTN_TL,   data[3] & 0x01);
396         input_report_key(dev, BTN_TR,   data[3] & 0x02);
397         input_report_key(dev, BTN_MODE, data[3] & 0x04);
398
399         if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
400                 /* left stick */
401                 input_report_abs(dev, ABS_X,
402                                  (__s16) le16_to_cpup((__le16 *)(data + 6)));
403                 input_report_abs(dev, ABS_Y,
404                                  ~(__s16) le16_to_cpup((__le16 *)(data + 8)));
405
406                 /* right stick */
407                 input_report_abs(dev, ABS_RX,
408                                  (__s16) le16_to_cpup((__le16 *)(data + 10)));
409                 input_report_abs(dev, ABS_RY,
410                                  ~(__s16) le16_to_cpup((__le16 *)(data + 12)));
411         }
412
413         /* triggers left/right */
414         if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
415                 input_report_key(dev, BTN_TL2, data[4]);
416                 input_report_key(dev, BTN_TR2, data[5]);
417         } else {
418                 input_report_abs(dev, ABS_Z, data[4]);
419                 input_report_abs(dev, ABS_RZ, data[5]);
420         }
421
422         input_sync(dev);
423 }
424
425 /*
426  * xpad360w_process_packet
427  *
428  * Completes a request by converting the data into events for the
429  * input subsystem. It is version for xbox 360 wireless controller.
430  *
431  * Byte.Bit
432  * 00.1 - Status change: The controller or headset has connected/disconnected
433  *                       Bits 01.7 and 01.6 are valid
434  * 01.7 - Controller present
435  * 01.6 - Headset present
436  * 01.1 - Pad state (Bytes 4+) valid
437  *
438  */
439
440 static void xpad360w_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data)
441 {
442         /* Presence change */
443         if (data[0] & 0x08) {
444                 if (data[1] & 0x80) {
445                         xpad->pad_present = 1;
446                         usb_submit_urb(xpad->bulk_out, GFP_ATOMIC);
447                 } else
448                         xpad->pad_present = 0;
449         }
450
451         /* Valid pad data */
452         if (!(data[1] & 0x1))
453                 return;
454
455         xpad360_process_packet(xpad, cmd, &data[4]);
456 }
457
458 static void xpad_irq_in(struct urb *urb)
459 {
460         struct usb_xpad *xpad = urb->context;
461         int retval, status;
462
463         status = urb->status;
464
465         switch (status) {
466         case 0:
467                 /* success */
468                 break;
469         case -ECONNRESET:
470         case -ENOENT:
471         case -ESHUTDOWN:
472                 /* this urb is terminated, clean up */
473                 dbg("%s - urb shutting down with status: %d",
474                         __func__, status);
475                 return;
476         default:
477                 dbg("%s - nonzero urb status received: %d",
478                         __func__, status);
479                 goto exit;
480         }
481
482         switch (xpad->xtype) {
483         case XTYPE_XBOX360:
484                 xpad360_process_packet(xpad, 0, xpad->idata);
485                 break;
486         case XTYPE_XBOX360W:
487                 xpad360w_process_packet(xpad, 0, xpad->idata);
488                 break;
489         default:
490                 xpad_process_packet(xpad, 0, xpad->idata);
491         }
492
493 exit:
494         retval = usb_submit_urb(urb, GFP_ATOMIC);
495         if (retval)
496                 err ("%s - usb_submit_urb failed with result %d",
497                      __func__, retval);
498 }
499
500 static void xpad_bulk_out(struct urb *urb)
501 {
502         switch (urb->status) {
503         case 0:
504                 /* success */
505                 break;
506         case -ECONNRESET:
507         case -ENOENT:
508         case -ESHUTDOWN:
509                 /* this urb is terminated, clean up */
510                 dbg("%s - urb shutting down with status: %d", __func__, urb->status);
511                 break;
512         default:
513                 dbg("%s - nonzero urb status received: %d", __func__, urb->status);
514         }
515 }
516
517 #if defined(CONFIG_JOYSTICK_XPAD_FF) || defined(CONFIG_JOYSTICK_XPAD_LEDS)
518 static void xpad_irq_out(struct urb *urb)
519 {
520         int retval, status;
521
522         status = urb->status;
523
524         switch (status) {
525         case 0:
526                 /* success */
527                 return;
528
529         case -ECONNRESET:
530         case -ENOENT:
531         case -ESHUTDOWN:
532                 /* this urb is terminated, clean up */
533                 dbg("%s - urb shutting down with status: %d", __func__, status);
534                 return;
535
536         default:
537                 dbg("%s - nonzero urb status received: %d", __func__, status);
538                 goto exit;
539         }
540
541 exit:
542         retval = usb_submit_urb(urb, GFP_ATOMIC);
543         if (retval)
544                 err("%s - usb_submit_urb failed with result %d",
545                     __func__, retval);
546 }
547
548 static int xpad_init_output(struct usb_interface *intf, struct usb_xpad *xpad)
549 {
550         struct usb_endpoint_descriptor *ep_irq_out;
551         int error;
552
553         if (xpad->xtype == XTYPE_UNKNOWN)
554                 return 0;
555
556         xpad->odata = usb_alloc_coherent(xpad->udev, XPAD_PKT_LEN,
557                                          GFP_KERNEL, &xpad->odata_dma);
558         if (!xpad->odata) {
559                 error = -ENOMEM;
560                 goto fail1;
561         }
562
563         mutex_init(&xpad->odata_mutex);
564
565         xpad->irq_out = usb_alloc_urb(0, GFP_KERNEL);
566         if (!xpad->irq_out) {
567                 error = -ENOMEM;
568                 goto fail2;
569         }
570
571         ep_irq_out = &intf->cur_altsetting->endpoint[1].desc;
572         usb_fill_int_urb(xpad->irq_out, xpad->udev,
573                          usb_sndintpipe(xpad->udev, ep_irq_out->bEndpointAddress),
574                          xpad->odata, XPAD_PKT_LEN,
575                          xpad_irq_out, xpad, ep_irq_out->bInterval);
576         xpad->irq_out->transfer_dma = xpad->odata_dma;
577         xpad->irq_out->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
578
579         return 0;
580
581  fail2: usb_free_coherent(xpad->udev, XPAD_PKT_LEN, xpad->odata, xpad->odata_dma);
582  fail1: return error;
583 }
584
585 static void xpad_stop_output(struct usb_xpad *xpad)
586 {
587         if (xpad->xtype != XTYPE_UNKNOWN)
588                 usb_kill_urb(xpad->irq_out);
589 }
590
591 static void xpad_deinit_output(struct usb_xpad *xpad)
592 {
593         if (xpad->xtype != XTYPE_UNKNOWN) {
594                 usb_free_urb(xpad->irq_out);
595                 usb_free_coherent(xpad->udev, XPAD_PKT_LEN,
596                                 xpad->odata, xpad->odata_dma);
597         }
598 }
599 #else
600 static int xpad_init_output(struct usb_interface *intf, struct usb_xpad *xpad) { return 0; }
601 static void xpad_deinit_output(struct usb_xpad *xpad) {}
602 static void xpad_stop_output(struct usb_xpad *xpad) {}
603 #endif
604
605 #ifdef CONFIG_JOYSTICK_XPAD_FF
606 static int xpad_play_effect(struct input_dev *dev, void *data, struct ff_effect *effect)
607 {
608         struct usb_xpad *xpad = input_get_drvdata(dev);
609
610         if (effect->type == FF_RUMBLE) {
611                 __u16 strong = effect->u.rumble.strong_magnitude;
612                 __u16 weak = effect->u.rumble.weak_magnitude;
613
614                 switch (xpad->xtype) {
615
616                 case XTYPE_XBOX:
617                         xpad->odata[0] = 0x00;
618                         xpad->odata[1] = 0x06;
619                         xpad->odata[2] = 0x00;
620                         xpad->odata[3] = strong / 256;  /* left actuator */
621                         xpad->odata[4] = 0x00;
622                         xpad->odata[5] = weak / 256;    /* right actuator */
623                         xpad->irq_out->transfer_buffer_length = 6;
624
625                         return usb_submit_urb(xpad->irq_out, GFP_ATOMIC);
626
627                 case XTYPE_XBOX360:
628                         xpad->odata[0] = 0x00;
629                         xpad->odata[1] = 0x08;
630                         xpad->odata[2] = 0x00;
631                         xpad->odata[3] = strong / 256;  /* left actuator? */
632                         xpad->odata[4] = weak / 256;    /* right actuator? */
633                         xpad->odata[5] = 0x00;
634                         xpad->odata[6] = 0x00;
635                         xpad->odata[7] = 0x00;
636                         xpad->irq_out->transfer_buffer_length = 8;
637
638                         return usb_submit_urb(xpad->irq_out, GFP_ATOMIC);
639
640                 case XTYPE_XBOX360W:
641                         xpad->odata[0] = 0x00;
642                         xpad->odata[1] = 0x01;
643                         xpad->odata[2] = 0x0F;
644                         xpad->odata[3] = 0xC0;
645                         xpad->odata[4] = 0x00;
646                         xpad->odata[5] = strong / 256;
647                         xpad->odata[6] = weak / 256;
648                         xpad->odata[7] = 0x00;
649                         xpad->odata[8] = 0x00;
650                         xpad->odata[9] = 0x00;
651                         xpad->odata[10] = 0x00;
652                         xpad->odata[11] = 0x00;
653                         xpad->irq_out->transfer_buffer_length = 12;
654
655                         return usb_submit_urb(xpad->irq_out, GFP_ATOMIC);
656
657                 default:
658                         dbg("%s - rumble command sent to unsupported xpad type: %d",
659                                 __func__, xpad->xtype);
660                         return -1;
661                 }
662         }
663
664         return 0;
665 }
666
667 static int xpad_init_ff(struct usb_xpad *xpad)
668 {
669         if (xpad->xtype == XTYPE_UNKNOWN)
670                 return 0;
671
672         input_set_capability(xpad->dev, EV_FF, FF_RUMBLE);
673
674         return input_ff_create_memless(xpad->dev, NULL, xpad_play_effect);
675 }
676
677 #else
678 static int xpad_init_ff(struct usb_xpad *xpad) { return 0; }
679 #endif
680
681 #if defined(CONFIG_JOYSTICK_XPAD_LEDS)
682 #include <linux/leds.h>
683
684 struct xpad_led {
685         char name[16];
686         struct led_classdev led_cdev;
687         struct usb_xpad *xpad;
688 };
689
690 static void xpad_send_led_command(struct usb_xpad *xpad, int command)
691 {
692         if (command >= 0 && command < 14) {
693                 mutex_lock(&xpad->odata_mutex);
694                 xpad->odata[0] = 0x01;
695                 xpad->odata[1] = 0x03;
696                 xpad->odata[2] = command;
697                 xpad->irq_out->transfer_buffer_length = 3;
698                 usb_submit_urb(xpad->irq_out, GFP_KERNEL);
699                 mutex_unlock(&xpad->odata_mutex);
700         }
701 }
702
703 static void xpad_led_set(struct led_classdev *led_cdev,
704                          enum led_brightness value)
705 {
706         struct xpad_led *xpad_led = container_of(led_cdev,
707                                                  struct xpad_led, led_cdev);
708
709         xpad_send_led_command(xpad_led->xpad, value);
710 }
711
712 static int xpad_led_probe(struct usb_xpad *xpad)
713 {
714         static atomic_t led_seq = ATOMIC_INIT(0);
715         long led_no;
716         struct xpad_led *led;
717         struct led_classdev *led_cdev;
718         int error;
719
720         if (xpad->xtype != XTYPE_XBOX360)
721                 return 0;
722
723         xpad->led = led = kzalloc(sizeof(struct xpad_led), GFP_KERNEL);
724         if (!led)
725                 return -ENOMEM;
726
727         led_no = (long)atomic_inc_return(&led_seq) - 1;
728
729         snprintf(led->name, sizeof(led->name), "xpad%ld", led_no);
730         led->xpad = xpad;
731
732         led_cdev = &led->led_cdev;
733         led_cdev->name = led->name;
734         led_cdev->brightness_set = xpad_led_set;
735
736         error = led_classdev_register(&xpad->udev->dev, led_cdev);
737         if (error) {
738                 kfree(led);
739                 xpad->led = NULL;
740                 return error;
741         }
742
743         /*
744          * Light up the segment corresponding to controller number
745          */
746         xpad_send_led_command(xpad, (led_no % 4) + 2);
747
748         return 0;
749 }
750
751 static void xpad_led_disconnect(struct usb_xpad *xpad)
752 {
753         struct xpad_led *xpad_led = xpad->led;
754
755         if (xpad_led) {
756                 led_classdev_unregister(&xpad_led->led_cdev);
757                 kfree(xpad_led);
758         }
759 }
760 #else
761 static int xpad_led_probe(struct usb_xpad *xpad) { return 0; }
762 static void xpad_led_disconnect(struct usb_xpad *xpad) { }
763 #endif
764
765
766 static int xpad_open(struct input_dev *dev)
767 {
768         struct usb_xpad *xpad = input_get_drvdata(dev);
769
770         /* URB was submitted in probe */
771         if(xpad->xtype == XTYPE_XBOX360W)
772                 return 0;
773
774         xpad->irq_in->dev = xpad->udev;
775         if (usb_submit_urb(xpad->irq_in, GFP_KERNEL))
776                 return -EIO;
777
778         return 0;
779 }
780
781 static void xpad_close(struct input_dev *dev)
782 {
783         struct usb_xpad *xpad = input_get_drvdata(dev);
784
785         if (xpad->xtype != XTYPE_XBOX360W)
786                 usb_kill_urb(xpad->irq_in);
787
788         xpad_stop_output(xpad);
789 }
790
791 static void xpad_set_up_abs(struct input_dev *input_dev, signed short abs)
792 {
793         set_bit(abs, input_dev->absbit);
794
795         switch (abs) {
796         case ABS_X:
797         case ABS_Y:
798         case ABS_RX:
799         case ABS_RY:    /* the two sticks */
800                 input_set_abs_params(input_dev, abs, -32768, 32767, 16, 128);
801                 break;
802         case ABS_Z:
803         case ABS_RZ:    /* the triggers (if mapped to axes) */
804                 input_set_abs_params(input_dev, abs, 0, 255, 0, 0);
805                 break;
806         case ABS_HAT0X:
807         case ABS_HAT0Y: /* the d-pad (only if dpad is mapped to axes */
808                 input_set_abs_params(input_dev, abs, -1, 1, 0, 0);
809                 break;
810         }
811 }
812
813 static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id)
814 {
815         struct usb_device *udev = interface_to_usbdev(intf);
816         struct usb_xpad *xpad;
817         struct input_dev *input_dev;
818         struct usb_endpoint_descriptor *ep_irq_in;
819         int i, error;
820
821         for (i = 0; xpad_device[i].idVendor; i++) {
822                 if ((le16_to_cpu(udev->descriptor.idVendor) == xpad_device[i].idVendor) &&
823                     (le16_to_cpu(udev->descriptor.idProduct) == xpad_device[i].idProduct))
824                         break;
825         }
826
827         xpad = kzalloc(sizeof(struct usb_xpad), GFP_KERNEL);
828         input_dev = input_allocate_device();
829         if (!xpad || !input_dev) {
830                 error = -ENOMEM;
831                 goto fail1;
832         }
833
834         xpad->idata = usb_alloc_coherent(udev, XPAD_PKT_LEN,
835                                          GFP_KERNEL, &xpad->idata_dma);
836         if (!xpad->idata) {
837                 error = -ENOMEM;
838                 goto fail1;
839         }
840
841         xpad->irq_in = usb_alloc_urb(0, GFP_KERNEL);
842         if (!xpad->irq_in) {
843                 error = -ENOMEM;
844                 goto fail2;
845         }
846
847         xpad->udev = udev;
848         xpad->mapping = xpad_device[i].mapping;
849         xpad->xtype = xpad_device[i].xtype;
850
851         if (xpad->xtype == XTYPE_UNKNOWN) {
852                 if (intf->cur_altsetting->desc.bInterfaceClass == USB_CLASS_VENDOR_SPEC) {
853                         if (intf->cur_altsetting->desc.bInterfaceProtocol == 129)
854                                 xpad->xtype = XTYPE_XBOX360W;
855                         else
856                                 xpad->xtype = XTYPE_XBOX360;
857                 } else
858                         xpad->xtype = XTYPE_XBOX;
859
860                 if (dpad_to_buttons)
861                         xpad->mapping |= MAP_DPAD_TO_BUTTONS;
862                 if (triggers_to_buttons)
863                         xpad->mapping |= MAP_TRIGGERS_TO_BUTTONS;
864                 if (sticks_to_null)
865                         xpad->mapping |= MAP_STICKS_TO_NULL;
866         }
867
868         xpad->dev = input_dev;
869         usb_make_path(udev, xpad->phys, sizeof(xpad->phys));
870         strlcat(xpad->phys, "/input0", sizeof(xpad->phys));
871
872         input_dev->name = xpad_device[i].name;
873         input_dev->phys = xpad->phys;
874         usb_to_input_id(udev, &input_dev->id);
875         input_dev->dev.parent = &intf->dev;
876
877         input_set_drvdata(input_dev, xpad);
878
879         input_dev->open = xpad_open;
880         input_dev->close = xpad_close;
881
882         input_dev->evbit[0] = BIT_MASK(EV_KEY);
883
884         if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
885                 input_dev->evbit[0] |= BIT_MASK(EV_ABS);
886                 /* set up axes */
887                 for (i = 0; xpad_abs[i] >= 0; i++)
888                         xpad_set_up_abs(input_dev, xpad_abs[i]);
889         }
890
891         /* set up standard buttons */
892         for (i = 0; xpad_common_btn[i] >= 0; i++)
893                 __set_bit(xpad_common_btn[i], input_dev->keybit);
894
895         /* set up model-specific ones */
896         if (xpad->xtype == XTYPE_XBOX360 || xpad->xtype == XTYPE_XBOX360W) {
897                 for (i = 0; xpad360_btn[i] >= 0; i++)
898                         __set_bit(xpad360_btn[i], input_dev->keybit);
899         } else {
900                 for (i = 0; xpad_btn[i] >= 0; i++)
901                         __set_bit(xpad_btn[i], input_dev->keybit);
902         }
903
904         if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
905                 for (i = 0; xpad_btn_pad[i] >= 0; i++)
906                         __set_bit(xpad_btn_pad[i], input_dev->keybit);
907         } else {
908                 for (i = 0; xpad_abs_pad[i] >= 0; i++)
909                     xpad_set_up_abs(input_dev, xpad_abs_pad[i]);
910         }
911
912         if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
913                 for (i = 0; xpad_btn_triggers[i] >= 0; i++)
914                         __set_bit(xpad_btn_triggers[i], input_dev->keybit);
915         } else {
916                 for (i = 0; xpad_abs_triggers[i] >= 0; i++)
917                         xpad_set_up_abs(input_dev, xpad_abs_triggers[i]);
918         }
919
920         error = xpad_init_output(intf, xpad);
921         if (error)
922                 goto fail3;
923
924         error = xpad_init_ff(xpad);
925         if (error)
926                 goto fail4;
927
928         error = xpad_led_probe(xpad);
929         if (error)
930                 goto fail5;
931
932         ep_irq_in = &intf->cur_altsetting->endpoint[0].desc;
933         usb_fill_int_urb(xpad->irq_in, udev,
934                          usb_rcvintpipe(udev, ep_irq_in->bEndpointAddress),
935                          xpad->idata, XPAD_PKT_LEN, xpad_irq_in,
936                          xpad, ep_irq_in->bInterval);
937         xpad->irq_in->transfer_dma = xpad->idata_dma;
938         xpad->irq_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
939
940         error = input_register_device(xpad->dev);
941         if (error)
942                 goto fail6;
943
944         usb_set_intfdata(intf, xpad);
945
946         if (xpad->xtype == XTYPE_XBOX360W) {
947                 /*
948                  * Setup the message to set the LEDs on the
949                  * controller when it shows up
950                  */
951                 xpad->bulk_out = usb_alloc_urb(0, GFP_KERNEL);
952                 if (!xpad->bulk_out) {
953                         error = -ENOMEM;
954                         goto fail7;
955                 }
956
957                 xpad->bdata = kzalloc(XPAD_PKT_LEN, GFP_KERNEL);
958                 if (!xpad->bdata) {
959                         error = -ENOMEM;
960                         goto fail8;
961                 }
962
963                 xpad->bdata[2] = 0x08;
964                 switch (intf->cur_altsetting->desc.bInterfaceNumber) {
965                 case 0:
966                         xpad->bdata[3] = 0x42;
967                         break;
968                 case 2:
969                         xpad->bdata[3] = 0x43;
970                         break;
971                 case 4:
972                         xpad->bdata[3] = 0x44;
973                         break;
974                 case 6:
975                         xpad->bdata[3] = 0x45;
976                 }
977
978                 ep_irq_in = &intf->cur_altsetting->endpoint[1].desc;
979                 usb_fill_bulk_urb(xpad->bulk_out, udev,
980                                 usb_sndbulkpipe(udev, ep_irq_in->bEndpointAddress),
981                                 xpad->bdata, XPAD_PKT_LEN, xpad_bulk_out, xpad);
982
983                 /*
984                  * Submit the int URB immediately rather than waiting for open
985                  * because we get status messages from the device whether
986                  * or not any controllers are attached.  In fact, it's
987                  * exactly the message that a controller has arrived that
988                  * we're waiting for.
989                  */
990                 xpad->irq_in->dev = xpad->udev;
991                 error = usb_submit_urb(xpad->irq_in, GFP_KERNEL);
992                 if (error)
993                         goto fail9;
994         }
995
996         return 0;
997
998  fail9: kfree(xpad->bdata);
999  fail8: usb_free_urb(xpad->bulk_out);
1000  fail7: input_unregister_device(input_dev);
1001         input_dev = NULL;
1002  fail6: xpad_led_disconnect(xpad);
1003  fail5: if (input_dev)
1004                 input_ff_destroy(input_dev);
1005  fail4: xpad_deinit_output(xpad);
1006  fail3: usb_free_urb(xpad->irq_in);
1007  fail2: usb_free_coherent(udev, XPAD_PKT_LEN, xpad->idata, xpad->idata_dma);
1008  fail1: input_free_device(input_dev);
1009         kfree(xpad);
1010         return error;
1011
1012 }
1013
1014 static void xpad_disconnect(struct usb_interface *intf)
1015 {
1016         struct usb_xpad *xpad = usb_get_intfdata (intf);
1017
1018         xpad_led_disconnect(xpad);
1019         input_unregister_device(xpad->dev);
1020         xpad_deinit_output(xpad);
1021
1022         if (xpad->xtype == XTYPE_XBOX360W) {
1023                 usb_kill_urb(xpad->bulk_out);
1024                 usb_free_urb(xpad->bulk_out);
1025                 usb_kill_urb(xpad->irq_in);
1026         }
1027
1028         usb_free_urb(xpad->irq_in);
1029         usb_free_coherent(xpad->udev, XPAD_PKT_LEN,
1030                         xpad->idata, xpad->idata_dma);
1031
1032         kfree(xpad->bdata);
1033         kfree(xpad);
1034
1035         usb_set_intfdata(intf, NULL);
1036 }
1037
1038 static struct usb_driver xpad_driver = {
1039         .name           = "xpad",
1040         .probe          = xpad_probe,
1041         .disconnect     = xpad_disconnect,
1042         .id_table       = xpad_table,
1043 };
1044
1045 static int __init usb_xpad_init(void)
1046 {
1047         return usb_register(&xpad_driver);
1048 }
1049
1050 static void __exit usb_xpad_exit(void)
1051 {
1052         usb_deregister(&xpad_driver);
1053 }
1054
1055 module_init(usb_xpad_init);
1056 module_exit(usb_xpad_exit);
1057
1058 MODULE_AUTHOR(DRIVER_AUTHOR);
1059 MODULE_DESCRIPTION(DRIVER_DESC);
1060 MODULE_LICENSE("GPL");