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