285f0b753c251303aba544e955fed6aa993ad2f9
[cascardo/linux.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  *  - Xbox One information https://github.com/quantus/xbox-one-controller-protocol
35  *
36  * Thanks to:
37  *  - ITO Takayuki for providing essential xpad information on his website
38  *  - Vojtech Pavlik     - iforce driver / input subsystem
39  *  - Greg Kroah-Hartman - usb-skeleton driver
40  *  - XBOX Linux project - extra USB id's
41  *  - Pekka Pöyry (quantus) - Xbox One controller reverse engineering
42  *
43  * TODO:
44  *  - fine tune axes (especially trigger axes)
45  *  - fix "analog" buttons (reported as digital now)
46  *  - get rumble working
47  *  - need USB IDs for other dance pads
48  *
49  * History:
50  *
51  * 2002-06-27 - 0.0.1 : first version, just said "XBOX HID controller"
52  *
53  * 2002-07-02 - 0.0.2 : basic working version
54  *  - all axes and 9 of the 10 buttons work (german InterAct device)
55  *  - the black button does not work
56  *
57  * 2002-07-14 - 0.0.3 : rework by Vojtech Pavlik
58  *  - indentation fixes
59  *  - usb + input init sequence fixes
60  *
61  * 2002-07-16 - 0.0.4 : minor changes, merge with Vojtech's v0.0.3
62  *  - verified the lack of HID and report descriptors
63  *  - verified that ALL buttons WORK
64  *  - fixed d-pad to axes mapping
65  *
66  * 2002-07-17 - 0.0.5 : simplified d-pad handling
67  *
68  * 2004-10-02 - 0.0.6 : DDR pad support
69  *  - borrowed from the XBOX linux kernel
70  *  - USB id's for commonly used dance pads are present
71  *  - dance pads will map D-PAD to buttons, not axes
72  *  - pass the module paramater 'dpad_to_buttons' to force
73  *    the D-PAD to map to buttons if your pad is not detected
74  *
75  * Later changes can be tracked in SCM.
76  */
77
78 #include <linux/kernel.h>
79 #include <linux/input.h>
80 #include <linux/rcupdate.h>
81 #include <linux/slab.h>
82 #include <linux/stat.h>
83 #include <linux/module.h>
84 #include <linux/usb/input.h>
85
86 #define DRIVER_AUTHOR "Marko Friedemann <mfr@bmx-chemnitz.de>"
87 #define DRIVER_DESC "X-Box pad driver"
88
89 #define XPAD_PKT_LEN 32
90
91 /* xbox d-pads should map to buttons, as is required for DDR pads
92    but we map them to axes when possible to simplify things */
93 #define MAP_DPAD_TO_BUTTONS             (1 << 0)
94 #define MAP_TRIGGERS_TO_BUTTONS         (1 << 1)
95 #define MAP_STICKS_TO_NULL              (1 << 2)
96 #define DANCEPAD_MAP_CONFIG     (MAP_DPAD_TO_BUTTONS |                  \
97                                 MAP_TRIGGERS_TO_BUTTONS | MAP_STICKS_TO_NULL)
98
99 #define XTYPE_XBOX        0
100 #define XTYPE_XBOX360     1
101 #define XTYPE_XBOX360W    2
102 #define XTYPE_XBOXONE     3
103 #define XTYPE_UNKNOWN     4
104
105 static bool dpad_to_buttons;
106 module_param(dpad_to_buttons, bool, S_IRUGO);
107 MODULE_PARM_DESC(dpad_to_buttons, "Map D-PAD to buttons rather than axes for unknown pads");
108
109 static bool triggers_to_buttons;
110 module_param(triggers_to_buttons, bool, S_IRUGO);
111 MODULE_PARM_DESC(triggers_to_buttons, "Map triggers to buttons rather than axes for unknown pads");
112
113 static bool sticks_to_null;
114 module_param(sticks_to_null, bool, S_IRUGO);
115 MODULE_PARM_DESC(sticks_to_null, "Do not map sticks at all for unknown pads");
116
117 static const struct xpad_device {
118         u16 idVendor;
119         u16 idProduct;
120         char *name;
121         u8 mapping;
122         u8 xtype;
123 } xpad_device[] = {
124         { 0x045e, 0x0202, "Microsoft X-Box pad v1 (US)", 0, XTYPE_XBOX },
125         { 0x045e, 0x0285, "Microsoft X-Box pad (Japan)", 0, XTYPE_XBOX },
126         { 0x045e, 0x0287, "Microsoft Xbox Controller S", 0, XTYPE_XBOX },
127         { 0x045e, 0x0289, "Microsoft X-Box pad v2 (US)", 0, XTYPE_XBOX },
128         { 0x045e, 0x028e, "Microsoft X-Box 360 pad", 0, XTYPE_XBOX360 },
129         { 0x045e, 0x02d1, "Microsoft X-Box One pad", 0, XTYPE_XBOXONE },
130         { 0x045e, 0x02dd, "Microsoft X-Box One pad (Covert Forces)", 0, XTYPE_XBOXONE },
131         { 0x045e, 0x0291, "Xbox 360 Wireless Receiver (XBOX)", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360W },
132         { 0x045e, 0x0719, "Xbox 360 Wireless Receiver", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360W },
133         { 0x044f, 0x0f07, "Thrustmaster, Inc. Controller", 0, XTYPE_XBOX },
134         { 0x044f, 0xb326, "Thrustmaster Gamepad GP XID", 0, XTYPE_XBOX360 },
135         { 0x046d, 0xc21d, "Logitech Gamepad F310", 0, XTYPE_XBOX360 },
136         { 0x046d, 0xc21e, "Logitech Gamepad F510", 0, XTYPE_XBOX360 },
137         { 0x046d, 0xc21f, "Logitech Gamepad F710", 0, XTYPE_XBOX360 },
138         { 0x046d, 0xc242, "Logitech Chillstream Controller", 0, XTYPE_XBOX360 },
139         { 0x046d, 0xca84, "Logitech Xbox Cordless Controller", 0, XTYPE_XBOX },
140         { 0x046d, 0xca88, "Logitech Compact Controller for Xbox", 0, XTYPE_XBOX },
141         { 0x05fd, 0x1007, "Mad Catz Controller (unverified)", 0, XTYPE_XBOX },
142         { 0x05fd, 0x107a, "InterAct 'PowerPad Pro' X-Box pad (Germany)", 0, XTYPE_XBOX },
143         { 0x0738, 0x4516, "Mad Catz Control Pad", 0, XTYPE_XBOX },
144         { 0x0738, 0x4522, "Mad Catz LumiCON", 0, XTYPE_XBOX },
145         { 0x0738, 0x4526, "Mad Catz Control Pad Pro", 0, XTYPE_XBOX },
146         { 0x0738, 0x4536, "Mad Catz MicroCON", 0, XTYPE_XBOX },
147         { 0x0738, 0x4540, "Mad Catz Beat Pad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
148         { 0x0738, 0x4556, "Mad Catz Lynx Wireless Controller", 0, XTYPE_XBOX },
149         { 0x0738, 0x4716, "Mad Catz Wired Xbox 360 Controller", 0, XTYPE_XBOX360 },
150         { 0x0738, 0x4718, "Mad Catz Street Fighter IV FightStick SE", 0, XTYPE_XBOX360 },
151         { 0x0738, 0x4726, "Mad Catz Xbox 360 Controller", 0, XTYPE_XBOX360 },
152         { 0x0738, 0x4728, "Mad Catz Street Fighter IV FightPad", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
153         { 0x0738, 0x4738, "Mad Catz Wired Xbox 360 Controller (SFIV)", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
154         { 0x0738, 0x4740, "Mad Catz Beat Pad", 0, XTYPE_XBOX360 },
155         { 0x0738, 0x6040, "Mad Catz Beat Pad Pro", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
156         { 0x0738, 0xb726, "Mad Catz Xbox controller - MW2", 0, XTYPE_XBOX360 },
157         { 0x0738, 0xbeef, "Mad Catz JOYTECH NEO SE Advanced GamePad", XTYPE_XBOX360 },
158         { 0x0738, 0xcb02, "Saitek Cyborg Rumble Pad - PC/Xbox 360", 0, XTYPE_XBOX360 },
159         { 0x0738, 0xcb03, "Saitek P3200 Rumble Pad - PC/Xbox 360", 0, XTYPE_XBOX360 },
160         { 0x0738, 0xf738, "Super SFIV FightStick TE S", 0, XTYPE_XBOX360 },
161         { 0x0c12, 0x8802, "Zeroplus Xbox Controller", 0, XTYPE_XBOX },
162         { 0x0c12, 0x8809, "RedOctane Xbox Dance Pad", DANCEPAD_MAP_CONFIG, XTYPE_XBOX },
163         { 0x0c12, 0x880a, "Pelican Eclipse PL-2023", 0, XTYPE_XBOX },
164         { 0x0c12, 0x8810, "Zeroplus Xbox Controller", 0, XTYPE_XBOX },
165         { 0x0c12, 0x9902, "HAMA VibraX - *FAULTY HARDWARE*", 0, XTYPE_XBOX },
166         { 0x0d2f, 0x0002, "Andamiro Pump It Up pad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
167         { 0x0e4c, 0x1097, "Radica Gamester Controller", 0, XTYPE_XBOX },
168         { 0x0e4c, 0x2390, "Radica Games Jtech Controller", 0, XTYPE_XBOX },
169         { 0x0e6f, 0x0003, "Logic3 Freebird wireless Controller", 0, XTYPE_XBOX },
170         { 0x0e6f, 0x0005, "Eclipse wireless Controller", 0, XTYPE_XBOX },
171         { 0x0e6f, 0x0006, "Edge wireless Controller", 0, XTYPE_XBOX },
172         { 0x0e6f, 0x0105, "HSM3 Xbox360 dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
173         { 0x0e6f, 0x0113, "Afterglow AX.1 Gamepad for Xbox 360", 0, XTYPE_XBOX360 },
174         { 0x0e6f, 0x0201, "Pelican PL-3601 'TSZ' Wired Xbox 360 Controller", 0, XTYPE_XBOX360 },
175         { 0x0e6f, 0x0213, "Afterglow Gamepad for Xbox 360", 0, XTYPE_XBOX360 },
176         { 0x0e6f, 0x021f, "Rock Candy Gamepad for Xbox 360", 0, XTYPE_XBOX360 },
177         { 0x0e6f, 0x0301, "Logic3 Controller", 0, XTYPE_XBOX360 },
178         { 0x0e6f, 0x0401, "Logic3 Controller", 0, XTYPE_XBOX360 },
179         { 0x0e8f, 0x0201, "SmartJoy Frag Xpad/PS2 adaptor", 0, XTYPE_XBOX },
180         { 0x0e8f, 0x3008, "Generic xbox control (dealextreme)", 0, XTYPE_XBOX },
181         { 0x0f0d, 0x000a, "Hori Co. DOA4 FightStick", 0, XTYPE_XBOX360 },
182         { 0x0f0d, 0x000d, "Hori Fighting Stick EX2", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
183         { 0x0f0d, 0x0016, "Hori Real Arcade Pro.EX", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
184         { 0x0f30, 0x0202, "Joytech Advanced Controller", 0, XTYPE_XBOX },
185         { 0x0f30, 0x8888, "BigBen XBMiniPad Controller", 0, XTYPE_XBOX },
186         { 0x102c, 0xff0c, "Joytech Wireless Advanced Controller", 0, XTYPE_XBOX },
187         { 0x12ab, 0x0004, "Honey Bee Xbox360 dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
188         { 0x12ab, 0x0301, "PDP AFTERGLOW AX.1", 0, XTYPE_XBOX360 },
189         { 0x12ab, 0x8809, "Xbox DDR dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
190         { 0x1430, 0x4748, "RedOctane Guitar Hero X-plorer", 0, XTYPE_XBOX360 },
191         { 0x1430, 0x8888, "TX6500+ Dance Pad (first generation)", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
192         { 0x146b, 0x0601, "BigBen Interactive XBOX 360 Controller", 0, XTYPE_XBOX360 },
193         { 0x1532, 0x0037, "Razer Sabertooth", 0, XTYPE_XBOX360 },
194         { 0x15e4, 0x3f00, "Power A Mini Pro Elite", 0, XTYPE_XBOX360 },
195         { 0x15e4, 0x3f0a, "Xbox Airflo wired controller", 0, XTYPE_XBOX360 },
196         { 0x15e4, 0x3f10, "Batarang Xbox 360 controller", 0, XTYPE_XBOX360 },
197         { 0x162e, 0xbeef, "Joytech Neo-Se Take2", 0, XTYPE_XBOX360 },
198         { 0x1689, 0xfd00, "Razer Onza Tournament Edition", 0, XTYPE_XBOX360 },
199         { 0x1689, 0xfd01, "Razer Onza Classic Edition", 0, XTYPE_XBOX360 },
200         { 0x24c6, 0x5d04, "Razer Sabertooth", 0, XTYPE_XBOX360 },
201         { 0x1bad, 0x0002, "Harmonix Rock Band Guitar", 0, XTYPE_XBOX360 },
202         { 0x1bad, 0x0003, "Harmonix Rock Band Drumkit", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
203         { 0x1bad, 0xf016, "Mad Catz Xbox 360 Controller", 0, XTYPE_XBOX360 },
204         { 0x1bad, 0xf023, "MLG Pro Circuit Controller (Xbox)", 0, XTYPE_XBOX360 },
205         { 0x1bad, 0xf028, "Street Fighter IV FightPad", 0, XTYPE_XBOX360 },
206         { 0x1bad, 0xf038, "Street Fighter IV FightStick TE", 0, XTYPE_XBOX360 },
207         { 0x1bad, 0xf900, "Harmonix Xbox 360 Controller", 0, XTYPE_XBOX360 },
208         { 0x1bad, 0xf901, "Gamestop Xbox 360 Controller", 0, XTYPE_XBOX360 },
209         { 0x1bad, 0xf903, "Tron Xbox 360 controller", 0, XTYPE_XBOX360 },
210         { 0x24c6, 0x5000, "Razer Atrox Arcade Stick", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
211         { 0x24c6, 0x5300, "PowerA MINI PROEX Controller", 0, XTYPE_XBOX360 },
212         { 0x24c6, 0x5303, "Xbox Airflo wired controller", 0, XTYPE_XBOX360 },
213         { 0x24c6, 0x5500, "Hori XBOX 360 EX 2 with Turbo", 0, XTYPE_XBOX360 },
214         { 0x24c6, 0x5501, "Hori Real Arcade Pro VX-SA", 0, XTYPE_XBOX360 },
215         { 0x24c6, 0x5506, "Hori SOULCALIBUR V Stick", 0, XTYPE_XBOX360 },
216         { 0x24c6, 0x5b02, "Thrustmaster, Inc. GPX Controller", 0, XTYPE_XBOX360 },
217         { 0x24c6, 0x5b03, "Thrustmaster Ferrari 458 Racing Wheel", 0, XTYPE_XBOX360 },
218         { 0xffff, 0xffff, "Chinese-made Xbox Controller", 0, XTYPE_XBOX },
219         { 0x0000, 0x0000, "Generic X-Box pad", 0, XTYPE_UNKNOWN }
220 };
221
222 /* buttons shared with xbox and xbox360 */
223 static const signed short xpad_common_btn[] = {
224         BTN_A, BTN_B, BTN_X, BTN_Y,                     /* "analog" buttons */
225         BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR,  /* start/back/sticks */
226         -1                                              /* terminating entry */
227 };
228
229 /* original xbox controllers only */
230 static const signed short xpad_btn[] = {
231         BTN_C, BTN_Z,           /* "analog" buttons */
232         -1                      /* terminating entry */
233 };
234
235 /* used when dpad is mapped to buttons */
236 static const signed short xpad_btn_pad[] = {
237         BTN_TRIGGER_HAPPY1, BTN_TRIGGER_HAPPY2,         /* d-pad left, right */
238         BTN_TRIGGER_HAPPY3, BTN_TRIGGER_HAPPY4,         /* d-pad up, down */
239         -1                              /* terminating entry */
240 };
241
242 /* used when triggers are mapped to buttons */
243 static const signed short xpad_btn_triggers[] = {
244         BTN_TL2, BTN_TR2,               /* triggers left/right */
245         -1
246 };
247
248 static const signed short xpad360_btn[] = {  /* buttons for x360 controller */
249         BTN_TL, BTN_TR,         /* Button LB/RB */
250         BTN_MODE,               /* The big X button */
251         -1
252 };
253
254 static const signed short xpad_abs[] = {
255         ABS_X, ABS_Y,           /* left stick */
256         ABS_RX, ABS_RY,         /* right stick */
257         -1                      /* terminating entry */
258 };
259
260 /* used when dpad is mapped to axes */
261 static const signed short xpad_abs_pad[] = {
262         ABS_HAT0X, ABS_HAT0Y,   /* d-pad axes */
263         -1                      /* terminating entry */
264 };
265
266 /* used when triggers are mapped to axes */
267 static const signed short xpad_abs_triggers[] = {
268         ABS_Z, ABS_RZ,          /* triggers left/right */
269         -1
270 };
271
272 /*
273  * Xbox 360 has a vendor-specific class, so we cannot match it with only
274  * USB_INTERFACE_INFO (also specifically refused by USB subsystem), so we
275  * match against vendor id as well. Wired Xbox 360 devices have protocol 1,
276  * wireless controllers have protocol 129.
277  */
278 #define XPAD_XBOX360_VENDOR_PROTOCOL(vend,pr) \
279         .match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_INT_INFO, \
280         .idVendor = (vend), \
281         .bInterfaceClass = USB_CLASS_VENDOR_SPEC, \
282         .bInterfaceSubClass = 93, \
283         .bInterfaceProtocol = (pr)
284 #define XPAD_XBOX360_VENDOR(vend) \
285         { XPAD_XBOX360_VENDOR_PROTOCOL(vend,1) }, \
286         { XPAD_XBOX360_VENDOR_PROTOCOL(vend,129) }
287
288 /* The Xbox One controller uses subclass 71 and protocol 208. */
289 #define XPAD_XBOXONE_VENDOR_PROTOCOL(vend, pr) \
290         .match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_INT_INFO, \
291         .idVendor = (vend), \
292         .bInterfaceClass = USB_CLASS_VENDOR_SPEC, \
293         .bInterfaceSubClass = 71, \
294         .bInterfaceProtocol = (pr)
295 #define XPAD_XBOXONE_VENDOR(vend) \
296         { XPAD_XBOXONE_VENDOR_PROTOCOL(vend, 208) }
297
298 static struct usb_device_id xpad_table[] = {
299         { USB_INTERFACE_INFO('X', 'B', 0) },    /* X-Box USB-IF not approved class */
300         XPAD_XBOX360_VENDOR(0x044f),            /* Thrustmaster X-Box 360 controllers */
301         XPAD_XBOX360_VENDOR(0x045e),            /* Microsoft X-Box 360 controllers */
302         XPAD_XBOXONE_VENDOR(0x045e),            /* Microsoft X-Box One controllers */
303         XPAD_XBOX360_VENDOR(0x046d),            /* Logitech X-Box 360 style controllers */
304         XPAD_XBOX360_VENDOR(0x0738),            /* Mad Catz X-Box 360 controllers */
305         { USB_DEVICE(0x0738, 0x4540) },         /* Mad Catz Beat Pad */
306         XPAD_XBOX360_VENDOR(0x0e6f),            /* 0x0e6f X-Box 360 controllers */
307         XPAD_XBOX360_VENDOR(0x12ab),            /* X-Box 360 dance pads */
308         XPAD_XBOX360_VENDOR(0x1430),            /* RedOctane X-Box 360 controllers */
309         XPAD_XBOX360_VENDOR(0x146b),            /* BigBen Interactive Controllers */
310         XPAD_XBOX360_VENDOR(0x1bad),            /* Harminix Rock Band Guitar and Drums */
311         XPAD_XBOX360_VENDOR(0x0f0d),            /* Hori Controllers */
312         XPAD_XBOX360_VENDOR(0x1689),            /* Razer Onza */
313         XPAD_XBOX360_VENDOR(0x24c6),            /* PowerA Controllers */
314         XPAD_XBOX360_VENDOR(0x1532),            /* Razer Sabertooth */
315         XPAD_XBOX360_VENDOR(0x15e4),            /* Numark X-Box 360 controllers */
316         XPAD_XBOX360_VENDOR(0x162e),            /* Joytech X-Box 360 controllers */
317         { }
318 };
319
320 MODULE_DEVICE_TABLE(usb, xpad_table);
321
322 struct xpad_output_packet {
323         u8 data[XPAD_PKT_LEN];
324         u8 len;
325         bool pending;
326 };
327
328 #define XPAD_OUT_CMD_IDX        0
329 #define XPAD_OUT_FF_IDX         1
330 #define XPAD_OUT_LED_IDX        (1 + IS_ENABLED(CONFIG_JOYSTICK_XPAD_FF))
331 #define XPAD_NUM_OUT_PACKETS    (1 + \
332                                  IS_ENABLED(CONFIG_JOYSTICK_XPAD_FF) + \
333                                  IS_ENABLED(CONFIG_JOYSTICK_XPAD_LEDS))
334
335 struct usb_xpad {
336         struct input_dev *dev;          /* input device interface */
337         struct input_dev __rcu *x360w_dev;
338         struct usb_device *udev;        /* usb device */
339         struct usb_interface *intf;     /* usb interface */
340
341         bool pad_present;
342         bool input_created;
343
344         struct urb *irq_in;             /* urb for interrupt in report */
345         unsigned char *idata;           /* input data */
346         dma_addr_t idata_dma;
347
348         struct urb *irq_out;            /* urb for interrupt out report */
349         bool irq_out_active;            /* we must not use an active URB */
350         unsigned char *odata;           /* output data */
351         dma_addr_t odata_dma;
352         spinlock_t odata_lock;
353
354         struct xpad_output_packet out_packets[XPAD_NUM_OUT_PACKETS];
355         int last_out_packet;
356
357 #if defined(CONFIG_JOYSTICK_XPAD_LEDS)
358         struct xpad_led *led;
359 #endif
360
361         char phys[64];                  /* physical device path */
362
363         int mapping;                    /* map d-pad to buttons or to axes */
364         int xtype;                      /* type of xbox device */
365         int pad_nr;                     /* the order x360 pads were attached */
366         const char *name;               /* name of the device */
367         struct work_struct work;        /* init/remove device from callback */
368 };
369
370 static int xpad_init_input(struct usb_xpad *xpad);
371 static void xpad_deinit_input(struct usb_xpad *xpad);
372
373 /*
374  *      xpad_process_packet
375  *
376  *      Completes a request by converting the data into events for the
377  *      input subsystem.
378  *
379  *      The used report descriptor was taken from ITO Takayukis website:
380  *       http://euc.jp/periphs/xbox-controller.ja.html
381  */
382 static void xpad_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data)
383 {
384         struct input_dev *dev = xpad->dev;
385
386         if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
387                 /* left stick */
388                 input_report_abs(dev, ABS_X,
389                                  (__s16) le16_to_cpup((__le16 *)(data + 12)));
390                 input_report_abs(dev, ABS_Y,
391                                  ~(__s16) le16_to_cpup((__le16 *)(data + 14)));
392
393                 /* right stick */
394                 input_report_abs(dev, ABS_RX,
395                                  (__s16) le16_to_cpup((__le16 *)(data + 16)));
396                 input_report_abs(dev, ABS_RY,
397                                  ~(__s16) le16_to_cpup((__le16 *)(data + 18)));
398         }
399
400         /* triggers left/right */
401         if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
402                 input_report_key(dev, BTN_TL2, data[10]);
403                 input_report_key(dev, BTN_TR2, data[11]);
404         } else {
405                 input_report_abs(dev, ABS_Z, data[10]);
406                 input_report_abs(dev, ABS_RZ, data[11]);
407         }
408
409         /* digital pad */
410         if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
411                 /* dpad as buttons (left, right, up, down) */
412                 input_report_key(dev, BTN_TRIGGER_HAPPY1, data[2] & 0x04);
413                 input_report_key(dev, BTN_TRIGGER_HAPPY2, data[2] & 0x08);
414                 input_report_key(dev, BTN_TRIGGER_HAPPY3, data[2] & 0x01);
415                 input_report_key(dev, BTN_TRIGGER_HAPPY4, data[2] & 0x02);
416         } else {
417                 input_report_abs(dev, ABS_HAT0X,
418                                  !!(data[2] & 0x08) - !!(data[2] & 0x04));
419                 input_report_abs(dev, ABS_HAT0Y,
420                                  !!(data[2] & 0x02) - !!(data[2] & 0x01));
421         }
422
423         /* start/back buttons and stick press left/right */
424         input_report_key(dev, BTN_START,  data[2] & 0x10);
425         input_report_key(dev, BTN_SELECT, data[2] & 0x20);
426         input_report_key(dev, BTN_THUMBL, data[2] & 0x40);
427         input_report_key(dev, BTN_THUMBR, data[2] & 0x80);
428
429         /* "analog" buttons A, B, X, Y */
430         input_report_key(dev, BTN_A, data[4]);
431         input_report_key(dev, BTN_B, data[5]);
432         input_report_key(dev, BTN_X, data[6]);
433         input_report_key(dev, BTN_Y, data[7]);
434
435         /* "analog" buttons black, white */
436         input_report_key(dev, BTN_C, data[8]);
437         input_report_key(dev, BTN_Z, data[9]);
438
439         input_sync(dev);
440 }
441
442 /*
443  *      xpad360_process_packet
444  *
445  *      Completes a request by converting the data into events for the
446  *      input subsystem. It is version for xbox 360 controller
447  *
448  *      The used report descriptor was taken from:
449  *              http://www.free60.org/wiki/Gamepad
450  */
451
452 static void xpad360_process_packet(struct usb_xpad *xpad, struct input_dev *dev,
453                                    u16 cmd, unsigned char *data)
454 {
455         /* digital pad */
456         if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
457                 /* dpad as buttons (left, right, up, down) */
458                 input_report_key(dev, BTN_TRIGGER_HAPPY1, data[2] & 0x04);
459                 input_report_key(dev, BTN_TRIGGER_HAPPY2, data[2] & 0x08);
460                 input_report_key(dev, BTN_TRIGGER_HAPPY3, data[2] & 0x01);
461                 input_report_key(dev, BTN_TRIGGER_HAPPY4, data[2] & 0x02);
462         }
463
464         /*
465          * This should be a simple else block. However historically
466          * xbox360w has mapped DPAD to buttons while xbox360 did not. This
467          * made no sense, but now we can not just switch back and have to
468          * support both behaviors.
469          */
470         if (!(xpad->mapping & MAP_DPAD_TO_BUTTONS) ||
471             xpad->xtype == XTYPE_XBOX360W) {
472                 input_report_abs(dev, ABS_HAT0X,
473                                  !!(data[2] & 0x08) - !!(data[2] & 0x04));
474                 input_report_abs(dev, ABS_HAT0Y,
475                                  !!(data[2] & 0x02) - !!(data[2] & 0x01));
476         }
477
478         /* start/back buttons */
479         input_report_key(dev, BTN_START,  data[2] & 0x10);
480         input_report_key(dev, BTN_SELECT, data[2] & 0x20);
481
482         /* stick press left/right */
483         input_report_key(dev, BTN_THUMBL, data[2] & 0x40);
484         input_report_key(dev, BTN_THUMBR, data[2] & 0x80);
485
486         /* buttons A,B,X,Y,TL,TR and MODE */
487         input_report_key(dev, BTN_A,    data[3] & 0x10);
488         input_report_key(dev, BTN_B,    data[3] & 0x20);
489         input_report_key(dev, BTN_X,    data[3] & 0x40);
490         input_report_key(dev, BTN_Y,    data[3] & 0x80);
491         input_report_key(dev, BTN_TL,   data[3] & 0x01);
492         input_report_key(dev, BTN_TR,   data[3] & 0x02);
493         input_report_key(dev, BTN_MODE, data[3] & 0x04);
494
495         if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
496                 /* left stick */
497                 input_report_abs(dev, ABS_X,
498                                  (__s16) le16_to_cpup((__le16 *)(data + 6)));
499                 input_report_abs(dev, ABS_Y,
500                                  ~(__s16) le16_to_cpup((__le16 *)(data + 8)));
501
502                 /* right stick */
503                 input_report_abs(dev, ABS_RX,
504                                  (__s16) le16_to_cpup((__le16 *)(data + 10)));
505                 input_report_abs(dev, ABS_RY,
506                                  ~(__s16) le16_to_cpup((__le16 *)(data + 12)));
507         }
508
509         /* triggers left/right */
510         if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
511                 input_report_key(dev, BTN_TL2, data[4]);
512                 input_report_key(dev, BTN_TR2, data[5]);
513         } else {
514                 input_report_abs(dev, ABS_Z, data[4]);
515                 input_report_abs(dev, ABS_RZ, data[5]);
516         }
517
518         input_sync(dev);
519 }
520
521 static void xpad_presence_work(struct work_struct *work)
522 {
523         struct usb_xpad *xpad = container_of(work, struct usb_xpad, work);
524         int error;
525
526         if (xpad->pad_present) {
527                 error = xpad_init_input(xpad);
528                 if (error) {
529                         /* complain only, not much else we can do here */
530                         dev_err(&xpad->dev->dev,
531                                 "unable to init device: %d\n", error);
532                 } else {
533                         rcu_assign_pointer(xpad->x360w_dev, xpad->dev);
534                 }
535         } else {
536                 RCU_INIT_POINTER(xpad->x360w_dev, NULL);
537                 synchronize_rcu();
538                 /*
539                  * Now that we are sure xpad360w_process_packet is not
540                  * using input device we can get rid of it.
541                  */
542                 xpad_deinit_input(xpad);
543         }
544 }
545
546 /*
547  * xpad360w_process_packet
548  *
549  * Completes a request by converting the data into events for the
550  * input subsystem. It is version for xbox 360 wireless controller.
551  *
552  * Byte.Bit
553  * 00.1 - Status change: The controller or headset has connected/disconnected
554  *                       Bits 01.7 and 01.6 are valid
555  * 01.7 - Controller present
556  * 01.6 - Headset present
557  * 01.1 - Pad state (Bytes 4+) valid
558  *
559  */
560 static void xpad360w_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data)
561 {
562         struct input_dev *dev;
563         bool present;
564
565         /* Presence change */
566         if (data[0] & 0x08) {
567                 present = (data[1] & 0x80) != 0;
568
569                 if (xpad->pad_present != present) {
570                         xpad->pad_present = present;
571                         schedule_work(&xpad->work);
572                 }
573         }
574
575         /* Valid pad data */
576         if (data[1] != 0x1)
577                 return;
578
579         rcu_read_lock();
580         dev = rcu_dereference(xpad->x360w_dev);
581         if (dev)
582                 xpad360_process_packet(xpad, dev, cmd, &data[4]);
583         rcu_read_unlock();
584 }
585
586 /*
587  *      xpadone_process_buttons
588  *
589  *      Process a button update packet from an Xbox one controller.
590  */
591 static void xpadone_process_buttons(struct usb_xpad *xpad,
592                                 struct input_dev *dev,
593                                 unsigned char *data)
594 {
595         /* menu/view buttons */
596         input_report_key(dev, BTN_START,  data[4] & 0x04);
597         input_report_key(dev, BTN_SELECT, data[4] & 0x08);
598
599         /* buttons A,B,X,Y */
600         input_report_key(dev, BTN_A,    data[4] & 0x10);
601         input_report_key(dev, BTN_B,    data[4] & 0x20);
602         input_report_key(dev, BTN_X,    data[4] & 0x40);
603         input_report_key(dev, BTN_Y,    data[4] & 0x80);
604
605         /* digital pad */
606         if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
607                 /* dpad as buttons (left, right, up, down) */
608                 input_report_key(dev, BTN_TRIGGER_HAPPY1, data[5] & 0x04);
609                 input_report_key(dev, BTN_TRIGGER_HAPPY2, data[5] & 0x08);
610                 input_report_key(dev, BTN_TRIGGER_HAPPY3, data[5] & 0x01);
611                 input_report_key(dev, BTN_TRIGGER_HAPPY4, data[5] & 0x02);
612         } else {
613                 input_report_abs(dev, ABS_HAT0X,
614                                  !!(data[5] & 0x08) - !!(data[5] & 0x04));
615                 input_report_abs(dev, ABS_HAT0Y,
616                                  !!(data[5] & 0x02) - !!(data[5] & 0x01));
617         }
618
619         /* TL/TR */
620         input_report_key(dev, BTN_TL,   data[5] & 0x10);
621         input_report_key(dev, BTN_TR,   data[5] & 0x20);
622
623         /* stick press left/right */
624         input_report_key(dev, BTN_THUMBL, data[5] & 0x40);
625         input_report_key(dev, BTN_THUMBR, data[5] & 0x80);
626
627         if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
628                 /* left stick */
629                 input_report_abs(dev, ABS_X,
630                                  (__s16) le16_to_cpup((__le16 *)(data + 10)));
631                 input_report_abs(dev, ABS_Y,
632                                  ~(__s16) le16_to_cpup((__le16 *)(data + 12)));
633
634                 /* right stick */
635                 input_report_abs(dev, ABS_RX,
636                                  (__s16) le16_to_cpup((__le16 *)(data + 14)));
637                 input_report_abs(dev, ABS_RY,
638                                  ~(__s16) le16_to_cpup((__le16 *)(data + 16)));
639         }
640
641         /* triggers left/right */
642         if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
643                 input_report_key(dev, BTN_TL2,
644                                  (__u16) le16_to_cpup((__le16 *)(data + 6)));
645                 input_report_key(dev, BTN_TR2,
646                                  (__u16) le16_to_cpup((__le16 *)(data + 8)));
647         } else {
648                 input_report_abs(dev, ABS_Z,
649                                  (__u16) le16_to_cpup((__le16 *)(data + 6)));
650                 input_report_abs(dev, ABS_RZ,
651                                  (__u16) le16_to_cpup((__le16 *)(data + 8)));
652         }
653
654         input_sync(dev);
655 }
656
657 /*
658  *      xpadone_process_packet
659  *
660  *      Completes a request by converting the data into events for the
661  *      input subsystem. This version is for the Xbox One controller.
662  *
663  *      The report format was gleaned from
664  *      https://github.com/kylelemons/xbox/blob/master/xbox.go
665  */
666
667 static void xpadone_process_packet(struct usb_xpad *xpad,
668                                 u16 cmd, unsigned char *data)
669 {
670         struct input_dev *dev = xpad->dev;
671
672         switch (data[0]) {
673         case 0x20:
674                 xpadone_process_buttons(xpad, dev, data);
675                 break;
676
677         case 0x07:
678                 /* the xbox button has its own special report */
679                 input_report_key(dev, BTN_MODE, data[4] & 0x01);
680                 input_sync(dev);
681                 break;
682         }
683 }
684
685 static void xpad_irq_in(struct urb *urb)
686 {
687         struct usb_xpad *xpad = urb->context;
688         struct device *dev = &xpad->intf->dev;
689         int retval, status;
690
691         status = urb->status;
692
693         switch (status) {
694         case 0:
695                 /* success */
696                 break;
697         case -ECONNRESET:
698         case -ENOENT:
699         case -ESHUTDOWN:
700                 /* this urb is terminated, clean up */
701                 dev_dbg(dev, "%s - urb shutting down with status: %d\n",
702                         __func__, status);
703                 return;
704         default:
705                 dev_dbg(dev, "%s - nonzero urb status received: %d\n",
706                         __func__, status);
707                 goto exit;
708         }
709
710         switch (xpad->xtype) {
711         case XTYPE_XBOX360:
712                 xpad360_process_packet(xpad, xpad->dev, 0, xpad->idata);
713                 break;
714         case XTYPE_XBOX360W:
715                 xpad360w_process_packet(xpad, 0, xpad->idata);
716                 break;
717         case XTYPE_XBOXONE:
718                 xpadone_process_packet(xpad, 0, xpad->idata);
719                 break;
720         default:
721                 xpad_process_packet(xpad, 0, xpad->idata);
722         }
723
724 exit:
725         retval = usb_submit_urb(urb, GFP_ATOMIC);
726         if (retval)
727                 dev_err(dev, "%s - usb_submit_urb failed with result %d\n",
728                         __func__, retval);
729 }
730
731 /* Callers must hold xpad->odata_lock spinlock */
732 static bool xpad_prepare_next_out_packet(struct usb_xpad *xpad)
733 {
734         struct xpad_output_packet *pkt, *packet = NULL;
735         int i;
736
737         for (i = 0; i < XPAD_NUM_OUT_PACKETS; i++) {
738                 if (++xpad->last_out_packet >= XPAD_NUM_OUT_PACKETS)
739                         xpad->last_out_packet = 0;
740
741                 pkt = &xpad->out_packets[xpad->last_out_packet];
742                 if (pkt->pending) {
743                         dev_dbg(&xpad->intf->dev,
744                                 "%s - found pending output packet %d\n",
745                                 __func__, xpad->last_out_packet);
746                         packet = pkt;
747                         break;
748                 }
749         }
750
751         if (packet) {
752                 memcpy(xpad->odata, packet->data, packet->len);
753                 xpad->irq_out->transfer_buffer_length = packet->len;
754                 return true;
755         }
756
757         return false;
758 }
759
760 /* Callers must hold xpad->odata_lock spinlock */
761 static int xpad_try_sending_next_out_packet(struct usb_xpad *xpad)
762 {
763         int error;
764
765         if (!xpad->irq_out_active && xpad_prepare_next_out_packet(xpad)) {
766                 error = usb_submit_urb(xpad->irq_out, GFP_ATOMIC);
767                 if (error) {
768                         dev_err(&xpad->intf->dev,
769                                 "%s - usb_submit_urb failed with result %d\n",
770                                 __func__, error);
771                         return -EIO;
772                 }
773
774                 xpad->irq_out_active = true;
775         }
776
777         return 0;
778 }
779
780 static void xpad_irq_out(struct urb *urb)
781 {
782         struct usb_xpad *xpad = urb->context;
783         struct device *dev = &xpad->intf->dev;
784         int status = urb->status;
785         int error;
786         unsigned long flags;
787
788         spin_lock_irqsave(&xpad->odata_lock, flags);
789
790         switch (status) {
791         case 0:
792                 /* success */
793                 xpad->out_packets[xpad->last_out_packet].pending = false;
794                 xpad->irq_out_active = xpad_prepare_next_out_packet(xpad);
795                 break;
796
797         case -ECONNRESET:
798         case -ENOENT:
799         case -ESHUTDOWN:
800                 /* this urb is terminated, clean up */
801                 dev_dbg(dev, "%s - urb shutting down with status: %d\n",
802                         __func__, status);
803                 xpad->irq_out_active = false;
804                 break;
805
806         default:
807                 dev_dbg(dev, "%s - nonzero urb status received: %d\n",
808                         __func__, status);
809                 break;
810         }
811
812         if (xpad->irq_out_active) {
813                 error = usb_submit_urb(urb, GFP_ATOMIC);
814                 if (error) {
815                         dev_err(dev,
816                                 "%s - usb_submit_urb failed with result %d\n",
817                                 __func__, error);
818                         xpad->irq_out_active = false;
819                 }
820         }
821
822         spin_unlock_irqrestore(&xpad->odata_lock, flags);
823 }
824
825 static int xpad_init_output(struct usb_interface *intf, struct usb_xpad *xpad)
826 {
827         struct usb_endpoint_descriptor *ep_irq_out;
828         int ep_irq_out_idx;
829         int error;
830
831         if (xpad->xtype == XTYPE_UNKNOWN)
832                 return 0;
833
834         xpad->odata = usb_alloc_coherent(xpad->udev, XPAD_PKT_LEN,
835                                          GFP_KERNEL, &xpad->odata_dma);
836         if (!xpad->odata) {
837                 error = -ENOMEM;
838                 goto fail1;
839         }
840
841         spin_lock_init(&xpad->odata_lock);
842
843         xpad->irq_out = usb_alloc_urb(0, GFP_KERNEL);
844         if (!xpad->irq_out) {
845                 error = -ENOMEM;
846                 goto fail2;
847         }
848
849         /* Xbox One controller has in/out endpoints swapped. */
850         ep_irq_out_idx = xpad->xtype == XTYPE_XBOXONE ? 0 : 1;
851         ep_irq_out = &intf->cur_altsetting->endpoint[ep_irq_out_idx].desc;
852
853         usb_fill_int_urb(xpad->irq_out, xpad->udev,
854                          usb_sndintpipe(xpad->udev, ep_irq_out->bEndpointAddress),
855                          xpad->odata, XPAD_PKT_LEN,
856                          xpad_irq_out, xpad, ep_irq_out->bInterval);
857         xpad->irq_out->transfer_dma = xpad->odata_dma;
858         xpad->irq_out->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
859
860         return 0;
861
862  fail2: usb_free_coherent(xpad->udev, XPAD_PKT_LEN, xpad->odata, xpad->odata_dma);
863  fail1: return error;
864 }
865
866 static void xpad_stop_output(struct usb_xpad *xpad)
867 {
868         if (xpad->xtype != XTYPE_UNKNOWN)
869                 usb_kill_urb(xpad->irq_out);
870 }
871
872 static void xpad_deinit_output(struct usb_xpad *xpad)
873 {
874         if (xpad->xtype != XTYPE_UNKNOWN) {
875                 usb_free_urb(xpad->irq_out);
876                 usb_free_coherent(xpad->udev, XPAD_PKT_LEN,
877                                 xpad->odata, xpad->odata_dma);
878         }
879 }
880
881 static int xpad_inquiry_pad_presence(struct usb_xpad *xpad)
882 {
883         struct xpad_output_packet *packet =
884                         &xpad->out_packets[XPAD_OUT_CMD_IDX];
885         unsigned long flags;
886         int retval;
887
888         spin_lock_irqsave(&xpad->odata_lock, flags);
889
890         packet->data[0] = 0x08;
891         packet->data[1] = 0x00;
892         packet->data[2] = 0x0F;
893         packet->data[3] = 0xC0;
894         packet->data[4] = 0x00;
895         packet->data[5] = 0x00;
896         packet->data[6] = 0x00;
897         packet->data[7] = 0x00;
898         packet->data[8] = 0x00;
899         packet->data[9] = 0x00;
900         packet->data[10] = 0x00;
901         packet->data[11] = 0x00;
902         packet->len = 12;
903         packet->pending = true;
904
905         /* Reset the sequence so we send out presence first */
906         xpad->last_out_packet = -1;
907         retval = xpad_try_sending_next_out_packet(xpad);
908
909         spin_unlock_irqrestore(&xpad->odata_lock, flags);
910
911         return retval;
912 }
913
914 static int xpad_start_xbox_one(struct usb_xpad *xpad)
915 {
916         struct xpad_output_packet *packet =
917                         &xpad->out_packets[XPAD_OUT_CMD_IDX];
918         unsigned long flags;
919         int retval;
920
921         spin_lock_irqsave(&xpad->odata_lock, flags);
922
923         /* Xbox one controller needs to be initialized. */
924         packet->data[0] = 0x05;
925         packet->data[1] = 0x20;
926         packet->len = 2;
927         packet->pending = true;
928
929         /* Reset the sequence so we send out start packet first */
930         xpad->last_out_packet = -1;
931         retval = xpad_try_sending_next_out_packet(xpad);
932
933         spin_unlock_irqrestore(&xpad->odata_lock, flags);
934
935         return retval;
936 }
937
938 #ifdef CONFIG_JOYSTICK_XPAD_FF
939 static int xpad_play_effect(struct input_dev *dev, void *data, struct ff_effect *effect)
940 {
941         struct usb_xpad *xpad = input_get_drvdata(dev);
942         struct xpad_output_packet *packet = &xpad->out_packets[XPAD_OUT_FF_IDX];
943         __u16 strong;
944         __u16 weak;
945         int retval;
946         unsigned long flags;
947
948         if (effect->type != FF_RUMBLE)
949                 return 0;
950
951         strong = effect->u.rumble.strong_magnitude;
952         weak = effect->u.rumble.weak_magnitude;
953
954         spin_lock_irqsave(&xpad->odata_lock, flags);
955
956         switch (xpad->xtype) {
957         case XTYPE_XBOX:
958                 packet->data[0] = 0x00;
959                 packet->data[1] = 0x06;
960                 packet->data[2] = 0x00;
961                 packet->data[3] = strong / 256; /* left actuator */
962                 packet->data[4] = 0x00;
963                 packet->data[5] = weak / 256;   /* right actuator */
964                 packet->len = 6;
965                 packet->pending = true;
966                 break;
967
968         case XTYPE_XBOX360:
969                 packet->data[0] = 0x00;
970                 packet->data[1] = 0x08;
971                 packet->data[2] = 0x00;
972                 packet->data[3] = strong / 256;  /* left actuator? */
973                 packet->data[4] = weak / 256;   /* right actuator? */
974                 packet->data[5] = 0x00;
975                 packet->data[6] = 0x00;
976                 packet->data[7] = 0x00;
977                 packet->len = 8;
978                 packet->pending = true;
979                 break;
980
981         case XTYPE_XBOX360W:
982                 packet->data[0] = 0x00;
983                 packet->data[1] = 0x01;
984                 packet->data[2] = 0x0F;
985                 packet->data[3] = 0xC0;
986                 packet->data[4] = 0x00;
987                 packet->data[5] = strong / 256;
988                 packet->data[6] = weak / 256;
989                 packet->data[7] = 0x00;
990                 packet->data[8] = 0x00;
991                 packet->data[9] = 0x00;
992                 packet->data[10] = 0x00;
993                 packet->data[11] = 0x00;
994                 packet->len = 12;
995                 packet->pending = true;
996                 break;
997
998         case XTYPE_XBOXONE:
999                 packet->data[0] = 0x09; /* activate rumble */
1000                 packet->data[1] = 0x08;
1001                 packet->data[2] = 0x00;
1002                 packet->data[3] = 0x08; /* continuous effect */
1003                 packet->data[4] = 0x00; /* simple rumble mode */
1004                 packet->data[5] = 0x03; /* L and R actuator only */
1005                 packet->data[6] = 0x00; /* TODO: LT actuator */
1006                 packet->data[7] = 0x00; /* TODO: RT actuator */
1007                 packet->data[8] = strong / 256; /* left actuator */
1008                 packet->data[9] = weak / 256;   /* right actuator */
1009                 packet->data[10] = 0x80;        /* length of pulse */
1010                 packet->data[11] = 0x00;        /* stop period of pulse */
1011                 packet->len = 12;
1012                 packet->pending = true;
1013                 break;
1014
1015         default:
1016                 dev_dbg(&xpad->dev->dev,
1017                         "%s - rumble command sent to unsupported xpad type: %d\n",
1018                         __func__, xpad->xtype);
1019                 retval = -EINVAL;
1020                 goto out;
1021         }
1022
1023         retval = xpad_try_sending_next_out_packet(xpad);
1024
1025 out:
1026         spin_unlock_irqrestore(&xpad->odata_lock, flags);
1027         return retval;
1028 }
1029
1030 static int xpad_init_ff(struct usb_xpad *xpad)
1031 {
1032         if (xpad->xtype == XTYPE_UNKNOWN)
1033                 return 0;
1034
1035         input_set_capability(xpad->dev, EV_FF, FF_RUMBLE);
1036
1037         return input_ff_create_memless(xpad->dev, NULL, xpad_play_effect);
1038 }
1039
1040 #else
1041 static int xpad_init_ff(struct usb_xpad *xpad) { return 0; }
1042 #endif
1043
1044 #if defined(CONFIG_JOYSTICK_XPAD_LEDS)
1045 #include <linux/leds.h>
1046 #include <linux/idr.h>
1047
1048 static DEFINE_IDA(xpad_pad_seq);
1049
1050 struct xpad_led {
1051         char name[16];
1052         struct led_classdev led_cdev;
1053         struct usb_xpad *xpad;
1054 };
1055
1056 /**
1057  * set the LEDs on Xbox360 / Wireless Controllers
1058  * @param command
1059  *  0: off
1060  *  1: all blink, then previous setting
1061  *  2: 1/top-left blink, then on
1062  *  3: 2/top-right blink, then on
1063  *  4: 3/bottom-left blink, then on
1064  *  5: 4/bottom-right blink, then on
1065  *  6: 1/top-left on
1066  *  7: 2/top-right on
1067  *  8: 3/bottom-left on
1068  *  9: 4/bottom-right on
1069  * 10: rotate
1070  * 11: blink, based on previous setting
1071  * 12: slow blink, based on previous setting
1072  * 13: rotate with two lights
1073  * 14: persistent slow all blink
1074  * 15: blink once, then previous setting
1075  */
1076 static void xpad_send_led_command(struct usb_xpad *xpad, int command)
1077 {
1078         struct xpad_output_packet *packet =
1079                         &xpad->out_packets[XPAD_OUT_LED_IDX];
1080         unsigned long flags;
1081
1082         command %= 16;
1083
1084         spin_lock_irqsave(&xpad->odata_lock, flags);
1085
1086         switch (xpad->xtype) {
1087         case XTYPE_XBOX360:
1088                 packet->data[0] = 0x01;
1089                 packet->data[1] = 0x03;
1090                 packet->data[2] = command;
1091                 packet->len = 3;
1092                 packet->pending = true;
1093                 break;
1094
1095         case XTYPE_XBOX360W:
1096                 packet->data[0] = 0x00;
1097                 packet->data[1] = 0x00;
1098                 packet->data[2] = 0x08;
1099                 packet->data[3] = 0x40 + command;
1100                 packet->data[4] = 0x00;
1101                 packet->data[5] = 0x00;
1102                 packet->data[6] = 0x00;
1103                 packet->data[7] = 0x00;
1104                 packet->data[8] = 0x00;
1105                 packet->data[9] = 0x00;
1106                 packet->data[10] = 0x00;
1107                 packet->data[11] = 0x00;
1108                 packet->len = 12;
1109                 packet->pending = true;
1110                 break;
1111         }
1112
1113         xpad_try_sending_next_out_packet(xpad);
1114
1115         spin_unlock_irqrestore(&xpad->odata_lock, flags);
1116 }
1117
1118 /*
1119  * Light up the segment corresponding to the pad number on
1120  * Xbox 360 Controllers.
1121  */
1122 static void xpad_identify_controller(struct usb_xpad *xpad)
1123 {
1124         xpad_send_led_command(xpad, (xpad->pad_nr % 4) + 2);
1125 }
1126
1127 static void xpad_led_set(struct led_classdev *led_cdev,
1128                          enum led_brightness value)
1129 {
1130         struct xpad_led *xpad_led = container_of(led_cdev,
1131                                                  struct xpad_led, led_cdev);
1132
1133         xpad_send_led_command(xpad_led->xpad, value);
1134 }
1135
1136 static int xpad_led_probe(struct usb_xpad *xpad)
1137 {
1138         struct xpad_led *led;
1139         struct led_classdev *led_cdev;
1140         int error;
1141
1142         if (xpad->xtype != XTYPE_XBOX360 && xpad->xtype != XTYPE_XBOX360W)
1143                 return 0;
1144
1145         xpad->led = led = kzalloc(sizeof(struct xpad_led), GFP_KERNEL);
1146         if (!led)
1147                 return -ENOMEM;
1148
1149         xpad->pad_nr = ida_simple_get(&xpad_pad_seq, 0, 0, GFP_KERNEL);
1150         if (xpad->pad_nr < 0) {
1151                 error = xpad->pad_nr;
1152                 goto err_free_mem;
1153         }
1154
1155         snprintf(led->name, sizeof(led->name), "xpad%d", xpad->pad_nr);
1156         led->xpad = xpad;
1157
1158         led_cdev = &led->led_cdev;
1159         led_cdev->name = led->name;
1160         led_cdev->brightness_set = xpad_led_set;
1161
1162         error = led_classdev_register(&xpad->udev->dev, led_cdev);
1163         if (error)
1164                 goto err_free_id;
1165
1166         xpad_identify_controller(xpad);
1167
1168         return 0;
1169
1170 err_free_id:
1171         ida_simple_remove(&xpad_pad_seq, xpad->pad_nr);
1172 err_free_mem:
1173         kfree(led);
1174         xpad->led = NULL;
1175         return error;
1176 }
1177
1178 static void xpad_led_disconnect(struct usb_xpad *xpad)
1179 {
1180         struct xpad_led *xpad_led = xpad->led;
1181
1182         if (xpad_led) {
1183                 led_classdev_unregister(&xpad_led->led_cdev);
1184                 ida_simple_remove(&xpad_pad_seq, xpad->pad_nr);
1185                 kfree(xpad_led);
1186         }
1187 }
1188 #else
1189 static int xpad_led_probe(struct usb_xpad *xpad) { return 0; }
1190 static void xpad_led_disconnect(struct usb_xpad *xpad) { }
1191 static void xpad_identify_controller(struct usb_xpad *xpad) { }
1192 #endif
1193
1194 static int xpad_open(struct input_dev *dev)
1195 {
1196         struct usb_xpad *xpad = input_get_drvdata(dev);
1197
1198         /* URB was submitted in probe */
1199         if (xpad->xtype == XTYPE_XBOX360W)
1200                 return 0;
1201
1202         xpad->irq_in->dev = xpad->udev;
1203         if (usb_submit_urb(xpad->irq_in, GFP_KERNEL))
1204                 return -EIO;
1205
1206         if (xpad->xtype == XTYPE_XBOXONE)
1207                 return xpad_start_xbox_one(xpad);
1208
1209         return 0;
1210 }
1211
1212 static void xpad_close(struct input_dev *dev)
1213 {
1214         struct usb_xpad *xpad = input_get_drvdata(dev);
1215
1216         if (xpad->xtype != XTYPE_XBOX360W)
1217                 usb_kill_urb(xpad->irq_in);
1218
1219         xpad_stop_output(xpad);
1220 }
1221
1222 static void xpad_set_up_abs(struct input_dev *input_dev, signed short abs)
1223 {
1224         struct usb_xpad *xpad = input_get_drvdata(input_dev);
1225         set_bit(abs, input_dev->absbit);
1226
1227         switch (abs) {
1228         case ABS_X:
1229         case ABS_Y:
1230         case ABS_RX:
1231         case ABS_RY:    /* the two sticks */
1232                 input_set_abs_params(input_dev, abs, -32768, 32767, 16, 128);
1233                 break;
1234         case ABS_Z:
1235         case ABS_RZ:    /* the triggers (if mapped to axes) */
1236                 if (xpad->xtype == XTYPE_XBOXONE)
1237                         input_set_abs_params(input_dev, abs, 0, 1023, 0, 0);
1238                 else
1239                         input_set_abs_params(input_dev, abs, 0, 255, 0, 0);
1240                 break;
1241         case ABS_HAT0X:
1242         case ABS_HAT0Y: /* the d-pad (only if dpad is mapped to axes */
1243                 input_set_abs_params(input_dev, abs, -1, 1, 0, 0);
1244                 break;
1245         }
1246 }
1247
1248 static void xpad_deinit_input(struct usb_xpad *xpad)
1249 {
1250         if (xpad->input_created) {
1251                 xpad->input_created = false;
1252                 xpad_led_disconnect(xpad);
1253                 input_unregister_device(xpad->dev);
1254         }
1255 }
1256
1257 static int xpad_init_input(struct usb_xpad *xpad)
1258 {
1259         struct input_dev *input_dev;
1260         int i, error;
1261
1262         input_dev = input_allocate_device();
1263         if (!input_dev)
1264                 return -ENOMEM;
1265
1266         xpad->dev = input_dev;
1267         input_dev->name = xpad->name;
1268         input_dev->phys = xpad->phys;
1269         usb_to_input_id(xpad->udev, &input_dev->id);
1270         input_dev->dev.parent = &xpad->intf->dev;
1271
1272         input_set_drvdata(input_dev, xpad);
1273
1274         input_dev->open = xpad_open;
1275         input_dev->close = xpad_close;
1276
1277         __set_bit(EV_KEY, input_dev->evbit);
1278
1279         if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
1280                 __set_bit(EV_ABS, input_dev->evbit);
1281                 /* set up axes */
1282                 for (i = 0; xpad_abs[i] >= 0; i++)
1283                         xpad_set_up_abs(input_dev, xpad_abs[i]);
1284         }
1285
1286         /* set up standard buttons */
1287         for (i = 0; xpad_common_btn[i] >= 0; i++)
1288                 __set_bit(xpad_common_btn[i], input_dev->keybit);
1289
1290         /* set up model-specific ones */
1291         if (xpad->xtype == XTYPE_XBOX360 || xpad->xtype == XTYPE_XBOX360W ||
1292             xpad->xtype == XTYPE_XBOXONE) {
1293                 for (i = 0; xpad360_btn[i] >= 0; i++)
1294                         __set_bit(xpad360_btn[i], input_dev->keybit);
1295         } else {
1296                 for (i = 0; xpad_btn[i] >= 0; i++)
1297                         __set_bit(xpad_btn[i], input_dev->keybit);
1298         }
1299
1300         if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
1301                 for (i = 0; xpad_btn_pad[i] >= 0; i++)
1302                         __set_bit(xpad_btn_pad[i], input_dev->keybit);
1303         }
1304
1305         /*
1306          * This should be a simple else block. However historically
1307          * xbox360w has mapped DPAD to buttons while xbox360 did not. This
1308          * made no sense, but now we can not just switch back and have to
1309          * support both behaviors.
1310          */
1311         if (!(xpad->mapping & MAP_DPAD_TO_BUTTONS) ||
1312             xpad->xtype == XTYPE_XBOX360W) {
1313                 for (i = 0; xpad_abs_pad[i] >= 0; i++)
1314                         xpad_set_up_abs(input_dev, xpad_abs_pad[i]);
1315         }
1316
1317         if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
1318                 for (i = 0; xpad_btn_triggers[i] >= 0; i++)
1319                         __set_bit(xpad_btn_triggers[i], input_dev->keybit);
1320         } else {
1321                 for (i = 0; xpad_abs_triggers[i] >= 0; i++)
1322                         xpad_set_up_abs(input_dev, xpad_abs_triggers[i]);
1323         }
1324
1325         error = xpad_init_ff(xpad);
1326         if (error)
1327                 goto err_free_input;
1328
1329         error = xpad_led_probe(xpad);
1330         if (error)
1331                 goto err_destroy_ff;
1332
1333         error = input_register_device(xpad->dev);
1334         if (error)
1335                 goto err_disconnect_led;
1336
1337         xpad->input_created = true;
1338         return 0;
1339
1340 err_disconnect_led:
1341         xpad_led_disconnect(xpad);
1342 err_destroy_ff:
1343         input_ff_destroy(input_dev);
1344 err_free_input:
1345         input_free_device(input_dev);
1346         return error;
1347 }
1348
1349 static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id)
1350 {
1351         struct usb_device *udev = interface_to_usbdev(intf);
1352         struct usb_xpad *xpad;
1353         struct usb_endpoint_descriptor *ep_irq_in;
1354         int ep_irq_in_idx;
1355         int i, error;
1356
1357         for (i = 0; xpad_device[i].idVendor; i++) {
1358                 if ((le16_to_cpu(udev->descriptor.idVendor) == xpad_device[i].idVendor) &&
1359                     (le16_to_cpu(udev->descriptor.idProduct) == xpad_device[i].idProduct))
1360                         break;
1361         }
1362
1363         if (xpad_device[i].xtype == XTYPE_XBOXONE &&
1364             intf->cur_altsetting->desc.bInterfaceNumber != 0) {
1365                 /*
1366                  * The Xbox One controller lists three interfaces all with the
1367                  * same interface class, subclass and protocol. Differentiate by
1368                  * interface number.
1369                  */
1370                 return -ENODEV;
1371         }
1372
1373         xpad = kzalloc(sizeof(struct usb_xpad), GFP_KERNEL);
1374         if (!xpad)
1375                 return -ENOMEM;
1376
1377         usb_make_path(udev, xpad->phys, sizeof(xpad->phys));
1378         strlcat(xpad->phys, "/input0", sizeof(xpad->phys));
1379
1380         xpad->idata = usb_alloc_coherent(udev, XPAD_PKT_LEN,
1381                                          GFP_KERNEL, &xpad->idata_dma);
1382         if (!xpad->idata) {
1383                 error = -ENOMEM;
1384                 goto err_free_mem;
1385         }
1386
1387         xpad->irq_in = usb_alloc_urb(0, GFP_KERNEL);
1388         if (!xpad->irq_in) {
1389                 error = -ENOMEM;
1390                 goto err_free_idata;
1391         }
1392
1393         xpad->udev = udev;
1394         xpad->intf = intf;
1395         xpad->mapping = xpad_device[i].mapping;
1396         xpad->xtype = xpad_device[i].xtype;
1397         xpad->name = xpad_device[i].name;
1398         INIT_WORK(&xpad->work, xpad_presence_work);
1399
1400         if (xpad->xtype == XTYPE_UNKNOWN) {
1401                 if (intf->cur_altsetting->desc.bInterfaceClass == USB_CLASS_VENDOR_SPEC) {
1402                         if (intf->cur_altsetting->desc.bInterfaceProtocol == 129)
1403                                 xpad->xtype = XTYPE_XBOX360W;
1404                         else
1405                                 xpad->xtype = XTYPE_XBOX360;
1406                 } else {
1407                         xpad->xtype = XTYPE_XBOX;
1408                 }
1409
1410                 if (dpad_to_buttons)
1411                         xpad->mapping |= MAP_DPAD_TO_BUTTONS;
1412                 if (triggers_to_buttons)
1413                         xpad->mapping |= MAP_TRIGGERS_TO_BUTTONS;
1414                 if (sticks_to_null)
1415                         xpad->mapping |= MAP_STICKS_TO_NULL;
1416         }
1417
1418         error = xpad_init_output(intf, xpad);
1419         if (error)
1420                 goto err_free_in_urb;
1421
1422         /* Xbox One controller has in/out endpoints swapped. */
1423         ep_irq_in_idx = xpad->xtype == XTYPE_XBOXONE ? 1 : 0;
1424         ep_irq_in = &intf->cur_altsetting->endpoint[ep_irq_in_idx].desc;
1425
1426         usb_fill_int_urb(xpad->irq_in, udev,
1427                          usb_rcvintpipe(udev, ep_irq_in->bEndpointAddress),
1428                          xpad->idata, XPAD_PKT_LEN, xpad_irq_in,
1429                          xpad, ep_irq_in->bInterval);
1430         xpad->irq_in->transfer_dma = xpad->idata_dma;
1431         xpad->irq_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1432
1433         usb_set_intfdata(intf, xpad);
1434
1435         if (xpad->xtype == XTYPE_XBOX360W) {
1436                 /*
1437                  * Submit the int URB immediately rather than waiting for open
1438                  * because we get status messages from the device whether
1439                  * or not any controllers are attached.  In fact, it's
1440                  * exactly the message that a controller has arrived that
1441                  * we're waiting for.
1442                  */
1443                 xpad->irq_in->dev = xpad->udev;
1444                 error = usb_submit_urb(xpad->irq_in, GFP_KERNEL);
1445                 if (error)
1446                         goto err_deinit_output;
1447
1448                 /*
1449                  * Send presence packet.
1450                  * This will force the controller to resend connection packets.
1451                  * This is useful in the case we activate the module after the
1452                  * adapter has been plugged in, as it won't automatically
1453                  * send us info about the controllers.
1454                  */
1455                 error = xpad_inquiry_pad_presence(xpad);
1456                 if (error)
1457                         goto err_kill_in_urb;
1458         } else {
1459                 error = xpad_init_input(xpad);
1460                 if (error)
1461                         goto err_deinit_output;
1462         }
1463         return 0;
1464
1465 err_kill_in_urb:
1466         usb_kill_urb(xpad->irq_in);
1467 err_deinit_output:
1468         xpad_deinit_output(xpad);
1469 err_free_in_urb:
1470         usb_free_urb(xpad->irq_in);
1471 err_free_idata:
1472         usb_free_coherent(udev, XPAD_PKT_LEN, xpad->idata, xpad->idata_dma);
1473 err_free_mem:
1474         kfree(xpad);
1475         return error;
1476
1477 }
1478
1479 static void xpad_disconnect(struct usb_interface *intf)
1480 {
1481         struct usb_xpad *xpad = usb_get_intfdata (intf);
1482
1483         if (xpad->xtype == XTYPE_XBOX360W)
1484                 usb_kill_urb(xpad->irq_in);
1485
1486         cancel_work_sync(&xpad->work);
1487
1488         xpad_deinit_input(xpad);
1489
1490         usb_free_urb(xpad->irq_in);
1491         usb_free_coherent(xpad->udev, XPAD_PKT_LEN,
1492                         xpad->idata, xpad->idata_dma);
1493
1494         xpad_deinit_output(xpad);
1495
1496         kfree(xpad);
1497
1498         usb_set_intfdata(intf, NULL);
1499 }
1500
1501 static struct usb_driver xpad_driver = {
1502         .name           = "xpad",
1503         .probe          = xpad_probe,
1504         .disconnect     = xpad_disconnect,
1505         .id_table       = xpad_table,
1506 };
1507
1508 module_usb_driver(xpad_driver);
1509
1510 MODULE_AUTHOR(DRIVER_AUTHOR);
1511 MODULE_DESCRIPTION(DRIVER_DESC);
1512 MODULE_LICENSE("GPL");