Merge branch 'next' into for-linus
[linux-2.6-block.git] / drivers / input / joystick / xpad.c
CommitLineData
1da177e4 1/*
bf8cb314 2 * X-Box gamepad driver
1da177e4
LT
3 *
4 * Copyright (c) 2002 Marko Friedemann <mfr@bmx-chemnitz.de>
d518b2b4
DC
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>
deb8ee43
DC
9 * 2005 Dominic Cerquetti <binary1230@yahoo.com>
10 * 2006 Adam Buchbinder <adam.buchbinder@gmail.com>
c7d9f7eb 11 * 2007 Jan Kratochvil <honza@jikos.cz>
7beae702 12 * 2010 Christoph Fritz <chf.fritz@googlemail.com>
1da177e4
LT
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
c7d9f7eb 33 * - Xbox 360 information http://www.free60.org/wiki/Gamepad
0604949c 34 * - Xbox One information https://github.com/quantus/xbox-one-controller-protocol
1da177e4
LT
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
d518b2b4 40 * - XBOX Linux project - extra USB id's
0604949c 41 * - Pekka Pöyry (quantus) - Xbox One controller reverse engineering
1da177e4
LT
42 *
43 * TODO:
deb8ee43 44 * - fine tune axes (especially trigger axes)
1da177e4
LT
45 * - fix "analog" buttons (reported as digital now)
46 * - get rumble working
deb8ee43 47 * - need USB IDs for other dance pads
1da177e4
LT
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
d518b2b4
DC
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
bf8cb314
AH
74 *
75 * Later changes can be tracked in SCM.
1da177e4
LT
76 */
77
1da177e4 78#include <linux/kernel.h>
09c8b00a
PLG
79#include <linux/input.h>
80#include <linux/rcupdate.h>
1da177e4 81#include <linux/slab.h>
deb8ee43 82#include <linux/stat.h>
1da177e4 83#include <linux/module.h>
ae0dadcf 84#include <linux/usb/input.h>
4220f7db 85#include <linux/usb/quirks.h>
1da177e4 86
1da177e4
LT
87#define DRIVER_AUTHOR "Marko Friedemann <mfr@bmx-chemnitz.de>"
88#define DRIVER_DESC "X-Box pad driver"
89
6f49a398 90#define XPAD_PKT_LEN 64
1da177e4 91
deb8ee43
DC
92/* xbox d-pads should map to buttons, as is required for DDR pads
93 but we map them to axes when possible to simplify things */
b45d44e7
NL
94#define MAP_DPAD_TO_BUTTONS (1 << 0)
95#define MAP_TRIGGERS_TO_BUTTONS (1 << 1)
7beae702
CF
96#define MAP_STICKS_TO_NULL (1 << 2)
97#define DANCEPAD_MAP_CONFIG (MAP_DPAD_TO_BUTTONS | \
98 MAP_TRIGGERS_TO_BUTTONS | MAP_STICKS_TO_NULL)
deb8ee43 99
c7d9f7eb
JK
100#define XTYPE_XBOX 0
101#define XTYPE_XBOX360 1
99de0912 102#define XTYPE_XBOX360W 2
1a48ff81
TM
103#define XTYPE_XBOXONE 3
104#define XTYPE_UNKNOWN 4
c7d9f7eb 105
90ab5ee9 106static bool dpad_to_buttons;
deb8ee43
DC
107module_param(dpad_to_buttons, bool, S_IRUGO);
108MODULE_PARM_DESC(dpad_to_buttons, "Map D-PAD to buttons rather than axes for unknown pads");
109
90ab5ee9 110static bool triggers_to_buttons;
b45d44e7
NL
111module_param(triggers_to_buttons, bool, S_IRUGO);
112MODULE_PARM_DESC(triggers_to_buttons, "Map triggers to buttons rather than axes for unknown pads");
113
90ab5ee9 114static bool sticks_to_null;
7beae702
CF
115module_param(sticks_to_null, bool, S_IRUGO);
116MODULE_PARM_DESC(sticks_to_null, "Do not map sticks at all for unknown pads");
117
f712a5a0
CG
118static bool auto_poweroff = true;
119module_param(auto_poweroff, bool, S_IWUSR | S_IRUGO);
120MODULE_PARM_DESC(auto_poweroff, "Power off wireless controllers on suspend");
121
4c4c9432 122static const struct xpad_device {
1da177e4
LT
123 u16 idVendor;
124 u16 idProduct;
125 char *name;
b45d44e7 126 u8 mapping;
c7d9f7eb 127 u8 xtype;
1da177e4 128} xpad_device[] = {
b45d44e7 129 { 0x045e, 0x0202, "Microsoft X-Box pad v1 (US)", 0, XTYPE_XBOX },
b45d44e7
NL
130 { 0x045e, 0x0285, "Microsoft X-Box pad (Japan)", 0, XTYPE_XBOX },
131 { 0x045e, 0x0287, "Microsoft Xbox Controller S", 0, XTYPE_XBOX },
540602a4
GA
132 { 0x045e, 0x0289, "Microsoft X-Box pad v2 (US)", 0, XTYPE_XBOX },
133 { 0x045e, 0x028e, "Microsoft X-Box 360 pad", 0, XTYPE_XBOX360 },
1a48ff81 134 { 0x045e, 0x02d1, "Microsoft X-Box One pad", 0, XTYPE_XBOXONE },
95162dc8 135 { 0x045e, 0x02dd, "Microsoft X-Box One pad (Firmware 2015)", 0, XTYPE_XBOXONE },
6f49a398 136 { 0x045e, 0x02e3, "Microsoft X-Box One Elite pad", 0, XTYPE_XBOXONE },
599b8c09 137 { 0x045e, 0x02ea, "Microsoft X-Box One S pad", 0, XTYPE_XBOXONE },
540602a4 138 { 0x045e, 0x0291, "Xbox 360 Wireless Receiver (XBOX)", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360W },
99de0912 139 { 0x045e, 0x0719, "Xbox 360 Wireless Receiver", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360W },
b45d44e7 140 { 0x044f, 0x0f07, "Thrustmaster, Inc. Controller", 0, XTYPE_XBOX },
f554f619 141 { 0x044f, 0xb326, "Thrustmaster Gamepad GP XID", 0, XTYPE_XBOX360 },
8e2f2325 142 { 0x046d, 0xc21d, "Logitech Gamepad F310", 0, XTYPE_XBOX360 },
f554f619 143 { 0x046d, 0xc21e, "Logitech Gamepad F510", 0, XTYPE_XBOX360 },
8e2f2325 144 { 0x046d, 0xc21f, "Logitech Gamepad F710", 0, XTYPE_XBOX360 },
b45d44e7
NL
145 { 0x046d, 0xc242, "Logitech Chillstream Controller", 0, XTYPE_XBOX360 },
146 { 0x046d, 0xca84, "Logitech Xbox Cordless Controller", 0, XTYPE_XBOX },
147 { 0x046d, 0xca88, "Logitech Compact Controller for Xbox", 0, XTYPE_XBOX },
148 { 0x05fd, 0x1007, "Mad Catz Controller (unverified)", 0, XTYPE_XBOX },
149 { 0x05fd, 0x107a, "InterAct 'PowerPad Pro' X-Box pad (Germany)", 0, XTYPE_XBOX },
150 { 0x0738, 0x4516, "Mad Catz Control Pad", 0, XTYPE_XBOX },
151 { 0x0738, 0x4522, "Mad Catz LumiCON", 0, XTYPE_XBOX },
152 { 0x0738, 0x4526, "Mad Catz Control Pad Pro", 0, XTYPE_XBOX },
153 { 0x0738, 0x4536, "Mad Catz MicroCON", 0, XTYPE_XBOX },
c7d9f7eb 154 { 0x0738, 0x4540, "Mad Catz Beat Pad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
b45d44e7
NL
155 { 0x0738, 0x4556, "Mad Catz Lynx Wireless Controller", 0, XTYPE_XBOX },
156 { 0x0738, 0x4716, "Mad Catz Wired Xbox 360 Controller", 0, XTYPE_XBOX360 },
f554f619
BV
157 { 0x0738, 0x4718, "Mad Catz Street Fighter IV FightStick SE", 0, XTYPE_XBOX360 },
158 { 0x0738, 0x4726, "Mad Catz Xbox 360 Controller", 0, XTYPE_XBOX360 },
be662271 159 { 0x0738, 0x4728, "Mad Catz Street Fighter IV FightPad", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
b45d44e7 160 { 0x0738, 0x4738, "Mad Catz Wired Xbox 360 Controller (SFIV)", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
f554f619 161 { 0x0738, 0x4740, "Mad Catz Beat Pad", 0, XTYPE_XBOX360 },
d63b0f0c 162 { 0x0738, 0x4a01, "Mad Catz FightStick TE 2", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOXONE },
c7d9f7eb 163 { 0x0738, 0x6040, "Mad Catz Beat Pad Pro", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
f554f619 164 { 0x0738, 0xb726, "Mad Catz Xbox controller - MW2", 0, XTYPE_XBOX360 },
540602a4 165 { 0x0738, 0xbeef, "Mad Catz JOYTECH NEO SE Advanced GamePad", XTYPE_XBOX360 },
f554f619
BV
166 { 0x0738, 0xcb02, "Saitek Cyborg Rumble Pad - PC/Xbox 360", 0, XTYPE_XBOX360 },
167 { 0x0738, 0xcb03, "Saitek P3200 Rumble Pad - PC/Xbox 360", 0, XTYPE_XBOX360 },
168 { 0x0738, 0xf738, "Super SFIV FightStick TE S", 0, XTYPE_XBOX360 },
b45d44e7 169 { 0x0c12, 0x8802, "Zeroplus Xbox Controller", 0, XTYPE_XBOX },
540602a4 170 { 0x0c12, 0x8809, "RedOctane Xbox Dance Pad", DANCEPAD_MAP_CONFIG, XTYPE_XBOX },
b45d44e7
NL
171 { 0x0c12, 0x880a, "Pelican Eclipse PL-2023", 0, XTYPE_XBOX },
172 { 0x0c12, 0x8810, "Zeroplus Xbox Controller", 0, XTYPE_XBOX },
173 { 0x0c12, 0x9902, "HAMA VibraX - *FAULTY HARDWARE*", 0, XTYPE_XBOX },
e76b8ee2 174 { 0x0d2f, 0x0002, "Andamiro Pump It Up pad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
b45d44e7
NL
175 { 0x0e4c, 0x1097, "Radica Gamester Controller", 0, XTYPE_XBOX },
176 { 0x0e4c, 0x2390, "Radica Games Jtech Controller", 0, XTYPE_XBOX },
177 { 0x0e6f, 0x0003, "Logic3 Freebird wireless Controller", 0, XTYPE_XBOX },
178 { 0x0e6f, 0x0005, "Eclipse wireless Controller", 0, XTYPE_XBOX },
179 { 0x0e6f, 0x0006, "Edge wireless Controller", 0, XTYPE_XBOX },
540602a4 180 { 0x0e6f, 0x0105, "HSM3 Xbox360 dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
f554f619 181 { 0x0e6f, 0x0113, "Afterglow AX.1 Gamepad for Xbox 360", 0, XTYPE_XBOX360 },
6538c3b2 182 { 0x0e6f, 0x0139, "Afterglow Prismatic Wired Controller", 0, XTYPE_XBOXONE },
6ac8a99b 183 { 0x0e6f, 0x0201, "Pelican PL-3601 'TSZ' Wired Xbox 360 Controller", 0, XTYPE_XBOX360 },
540602a4 184 { 0x0e6f, 0x0213, "Afterglow Gamepad for Xbox 360", 0, XTYPE_XBOX360 },
f554f619 185 { 0x0e6f, 0x021f, "Rock Candy Gamepad for Xbox 360", 0, XTYPE_XBOX360 },
6538c3b2 186 { 0x0e6f, 0x0146, "Rock Candy Wired Controller for Xbox One", 0, XTYPE_XBOXONE },
f554f619
BV
187 { 0x0e6f, 0x0301, "Logic3 Controller", 0, XTYPE_XBOX360 },
188 { 0x0e6f, 0x0401, "Logic3 Controller", 0, XTYPE_XBOX360 },
b45d44e7 189 { 0x0e8f, 0x0201, "SmartJoy Frag Xpad/PS2 adaptor", 0, XTYPE_XBOX },
f554f619
BV
190 { 0x0e8f, 0x3008, "Generic xbox control (dealextreme)", 0, XTYPE_XBOX },
191 { 0x0f0d, 0x000a, "Hori Co. DOA4 FightStick", 0, XTYPE_XBOX360 },
540602a4
GA
192 { 0x0f0d, 0x000d, "Hori Fighting Stick EX2", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
193 { 0x0f0d, 0x0016, "Hori Real Arcade Pro.EX", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
6538c3b2 194 { 0x0f0d, 0x0067, "HORIPAD ONE", 0, XTYPE_XBOXONE },
b45d44e7
NL
195 { 0x0f30, 0x0202, "Joytech Advanced Controller", 0, XTYPE_XBOX },
196 { 0x0f30, 0x8888, "BigBen XBMiniPad Controller", 0, XTYPE_XBOX },
197 { 0x102c, 0xff0c, "Joytech Wireless Advanced Controller", 0, XTYPE_XBOX },
fabadbc7 198 { 0x12ab, 0x0004, "Honey Bee Xbox360 dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
f554f619 199 { 0x12ab, 0x0301, "PDP AFTERGLOW AX.1", 0, XTYPE_XBOX360 },
540602a4 200 { 0x12ab, 0x8809, "Xbox DDR dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
b45d44e7 201 { 0x1430, 0x4748, "RedOctane Guitar Hero X-plorer", 0, XTYPE_XBOX360 },
c7d9f7eb 202 { 0x1430, 0x8888, "TX6500+ Dance Pad (first generation)", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
b45d44e7 203 { 0x146b, 0x0601, "BigBen Interactive XBOX 360 Controller", 0, XTYPE_XBOX360 },
f554f619 204 { 0x1532, 0x0037, "Razer Sabertooth", 0, XTYPE_XBOX360 },
53763668 205 { 0x1532, 0x0a03, "Razer Wildcat", 0, XTYPE_XBOXONE },
f554f619
BV
206 { 0x15e4, 0x3f00, "Power A Mini Pro Elite", 0, XTYPE_XBOX360 },
207 { 0x15e4, 0x3f0a, "Xbox Airflo wired controller", 0, XTYPE_XBOX360 },
208 { 0x15e4, 0x3f10, "Batarang Xbox 360 controller", 0, XTYPE_XBOX360 },
209 { 0x162e, 0xbeef, "Joytech Neo-Se Take2", 0, XTYPE_XBOX360 },
dbb48007
TOR
210 { 0x1689, 0xfd00, "Razer Onza Tournament Edition", 0, XTYPE_XBOX360 },
211 { 0x1689, 0xfd01, "Razer Onza Classic Edition", 0, XTYPE_XBOX360 },
6538c3b2 212 { 0x24c6, 0x542a, "Xbox ONE spectra", 0, XTYPE_XBOXONE },
a7b44738 213 { 0x24c6, 0x5d04, "Razer Sabertooth", 0, XTYPE_XBOX360 },
fabadbc7 214 { 0x1bad, 0x0002, "Harmonix Rock Band Guitar", 0, XTYPE_XBOX360 },
805423e8 215 { 0x1bad, 0x0003, "Harmonix Rock Band Drumkit", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
540602a4 216 { 0x1bad, 0xf016, "Mad Catz Xbox 360 Controller", 0, XTYPE_XBOX360 },
f554f619 217 { 0x1bad, 0xf023, "MLG Pro Circuit Controller (Xbox)", 0, XTYPE_XBOX360 },
540602a4 218 { 0x1bad, 0xf028, "Street Fighter IV FightPad", 0, XTYPE_XBOX360 },
f554f619
BV
219 { 0x1bad, 0xf038, "Street Fighter IV FightStick TE", 0, XTYPE_XBOX360 },
220 { 0x1bad, 0xf900, "Harmonix Xbox 360 Controller", 0, XTYPE_XBOX360 },
540602a4
GA
221 { 0x1bad, 0xf901, "Gamestop Xbox 360 Controller", 0, XTYPE_XBOX360 },
222 { 0x1bad, 0xf903, "Tron Xbox 360 controller", 0, XTYPE_XBOX360 },
470446ec 223 { 0x24c6, 0x5000, "Razer Atrox Arcade Stick", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
540602a4 224 { 0x24c6, 0x5300, "PowerA MINI PROEX Controller", 0, XTYPE_XBOX360 },
f554f619 225 { 0x24c6, 0x5303, "Xbox Airflo wired controller", 0, XTYPE_XBOX360 },
6538c3b2
PR
226 { 0x24c6, 0x541a, "PowerA Xbox One Mini Wired Controller", 0, XTYPE_XBOXONE },
227 { 0x24c6, 0x543a, "PowerA Xbox One wired controller", 0, XTYPE_XBOXONE },
f554f619
BV
228 { 0x24c6, 0x5500, "Hori XBOX 360 EX 2 with Turbo", 0, XTYPE_XBOX360 },
229 { 0x24c6, 0x5501, "Hori Real Arcade Pro VX-SA", 0, XTYPE_XBOX360 },
230 { 0x24c6, 0x5506, "Hori SOULCALIBUR V Stick", 0, XTYPE_XBOX360 },
231 { 0x24c6, 0x5b02, "Thrustmaster, Inc. GPX Controller", 0, XTYPE_XBOX360 },
4b546258 232 { 0x24c6, 0x5b03, "Thrustmaster Ferrari 458 Racing Wheel", 0, XTYPE_XBOX360 },
b45d44e7
NL
233 { 0xffff, 0xffff, "Chinese-made Xbox Controller", 0, XTYPE_XBOX },
234 { 0x0000, 0x0000, "Generic X-Box pad", 0, XTYPE_UNKNOWN }
1da177e4
LT
235};
236
fc55e952
AH
237/* buttons shared with xbox and xbox360 */
238static const signed short xpad_common_btn[] = {
239 BTN_A, BTN_B, BTN_X, BTN_Y, /* "analog" buttons */
7beae702 240 BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR, /* start/back/sticks */
1da177e4
LT
241 -1 /* terminating entry */
242};
243
fc55e952
AH
244/* original xbox controllers only */
245static const signed short xpad_btn[] = {
246 BTN_C, BTN_Z, /* "analog" buttons */
247 -1 /* terminating entry */
248};
249
7beae702 250/* used when dpad is mapped to buttons */
deb8ee43 251static const signed short xpad_btn_pad[] = {
7beae702
CF
252 BTN_TRIGGER_HAPPY1, BTN_TRIGGER_HAPPY2, /* d-pad left, right */
253 BTN_TRIGGER_HAPPY3, BTN_TRIGGER_HAPPY4, /* d-pad up, down */
deb8ee43
DC
254 -1 /* terminating entry */
255};
256
b45d44e7
NL
257/* used when triggers are mapped to buttons */
258static const signed short xpad_btn_triggers[] = {
259 BTN_TL2, BTN_TR2, /* triggers left/right */
260 -1
261};
262
c7d9f7eb
JK
263static const signed short xpad360_btn[] = { /* buttons for x360 controller */
264 BTN_TL, BTN_TR, /* Button LB/RB */
265 BTN_MODE, /* The big X button */
266 -1
267};
268
4c4c9432 269static const signed short xpad_abs[] = {
1da177e4
LT
270 ABS_X, ABS_Y, /* left stick */
271 ABS_RX, ABS_RY, /* right stick */
deb8ee43
DC
272 -1 /* terminating entry */
273};
274
b45d44e7 275/* used when dpad is mapped to axes */
deb8ee43
DC
276static const signed short xpad_abs_pad[] = {
277 ABS_HAT0X, ABS_HAT0Y, /* d-pad axes */
1da177e4
LT
278 -1 /* terminating entry */
279};
280
b45d44e7
NL
281/* used when triggers are mapped to axes */
282static const signed short xpad_abs_triggers[] = {
283 ABS_Z, ABS_RZ, /* triggers left/right */
284 -1
285};
286
1a48ff81
TM
287/*
288 * Xbox 360 has a vendor-specific class, so we cannot match it with only
8a0f83ea 289 * USB_INTERFACE_INFO (also specifically refused by USB subsystem), so we
99de0912 290 * match against vendor id as well. Wired Xbox 360 devices have protocol 1,
1a48ff81
TM
291 * wireless controllers have protocol 129.
292 */
99de0912 293#define XPAD_XBOX360_VENDOR_PROTOCOL(vend,pr) \
8a0f83ea
AH
294 .match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_INT_INFO, \
295 .idVendor = (vend), \
296 .bInterfaceClass = USB_CLASS_VENDOR_SPEC, \
297 .bInterfaceSubClass = 93, \
99de0912
BM
298 .bInterfaceProtocol = (pr)
299#define XPAD_XBOX360_VENDOR(vend) \
300 { XPAD_XBOX360_VENDOR_PROTOCOL(vend,1) }, \
301 { XPAD_XBOX360_VENDOR_PROTOCOL(vend,129) }
8a0f83ea 302
1a48ff81
TM
303/* The Xbox One controller uses subclass 71 and protocol 208. */
304#define XPAD_XBOXONE_VENDOR_PROTOCOL(vend, pr) \
305 .match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_INT_INFO, \
306 .idVendor = (vend), \
307 .bInterfaceClass = USB_CLASS_VENDOR_SPEC, \
308 .bInterfaceSubClass = 71, \
309 .bInterfaceProtocol = (pr)
310#define XPAD_XBOXONE_VENDOR(vend) \
311 { XPAD_XBOXONE_VENDOR_PROTOCOL(vend, 208) }
312
0d027966 313static struct usb_device_id xpad_table[] = {
1da177e4 314 { USB_INTERFACE_INFO('X', 'B', 0) }, /* X-Box USB-IF not approved class */
4dfb15cd 315 XPAD_XBOX360_VENDOR(0x044f), /* Thrustmaster X-Box 360 controllers */
99de0912 316 XPAD_XBOX360_VENDOR(0x045e), /* Microsoft X-Box 360 controllers */
1a48ff81 317 XPAD_XBOXONE_VENDOR(0x045e), /* Microsoft X-Box One controllers */
99de0912
BM
318 XPAD_XBOX360_VENDOR(0x046d), /* Logitech X-Box 360 style controllers */
319 XPAD_XBOX360_VENDOR(0x0738), /* Mad Catz X-Box 360 controllers */
3ffb62cb 320 { USB_DEVICE(0x0738, 0x4540) }, /* Mad Catz Beat Pad */
d63b0f0c 321 XPAD_XBOXONE_VENDOR(0x0738), /* Mad Catz FightStick TE 2 */
99de0912 322 XPAD_XBOX360_VENDOR(0x0e6f), /* 0x0e6f X-Box 360 controllers */
6538c3b2 323 XPAD_XBOXONE_VENDOR(0x0e6f), /* 0x0e6f X-Box One controllers */
c02fc1d9
DT
324 XPAD_XBOX360_VENDOR(0x0f0d), /* Hori Controllers */
325 XPAD_XBOXONE_VENDOR(0x0f0d), /* Hori Controllers */
fabadbc7 326 XPAD_XBOX360_VENDOR(0x12ab), /* X-Box 360 dance pads */
99de0912 327 XPAD_XBOX360_VENDOR(0x1430), /* RedOctane X-Box 360 controllers */
3ac91d36 328 XPAD_XBOX360_VENDOR(0x146b), /* BigBen Interactive Controllers */
f554f619 329 XPAD_XBOX360_VENDOR(0x1532), /* Razer Sabertooth */
53763668 330 XPAD_XBOXONE_VENDOR(0x1532), /* Razer Wildcat */
f554f619
BV
331 XPAD_XBOX360_VENDOR(0x15e4), /* Numark X-Box 360 controllers */
332 XPAD_XBOX360_VENDOR(0x162e), /* Joytech X-Box 360 controllers */
c02fc1d9
DT
333 XPAD_XBOX360_VENDOR(0x1689), /* Razer Onza */
334 XPAD_XBOX360_VENDOR(0x1bad), /* Harminix Rock Band Guitar and Drums */
335 XPAD_XBOX360_VENDOR(0x24c6), /* PowerA Controllers */
336 XPAD_XBOXONE_VENDOR(0x24c6), /* PowerA Controllers */
1da177e4
LT
337 { }
338};
339
0d027966 340MODULE_DEVICE_TABLE(usb, xpad_table);
1da177e4 341
81093c98
CG
342struct xboxone_init_packet {
343 u16 idVendor;
344 u16 idProduct;
345 const u8 *data;
346 u8 len;
347};
348
349#define XBOXONE_INIT_PKT(_vid, _pid, _data) \
350 { \
351 .idVendor = (_vid), \
352 .idProduct = (_pid), \
353 .data = (_data), \
354 .len = ARRAY_SIZE(_data), \
355 }
356
357
358/*
359 * This packet is required for all Xbox One pads with 2015
360 * or later firmware installed (or present from the factory).
361 */
362static const u8 xboxone_fw2015_init[] = {
363 0x05, 0x20, 0x00, 0x01, 0x00
364};
365
366/*
367 * This packet is required for the Titanfall 2 Xbox One pads
368 * (0x0e6f:0x0165) to finish initialization and for Hori pads
369 * (0x0f0d:0x0067) to make the analog sticks work.
370 */
371static const u8 xboxone_hori_init[] = {
372 0x01, 0x20, 0x00, 0x09, 0x00, 0x04, 0x20, 0x3a,
373 0x00, 0x00, 0x00, 0x80, 0x00
374};
375
376/*
377 * A rumble packet is required for some PowerA pads to start
378 * sending input reports. One of those pads is (0x24c6:0x543a).
379 */
380static const u8 xboxone_zerorumble_init[] = {
381 0x09, 0x00, 0x00, 0x09, 0x00, 0x0F, 0x00, 0x00,
382 0x00, 0x00, 0x00, 0x00, 0x00
383};
384
385/*
386 * This specifies the selection of init packets that a gamepad
387 * will be sent on init *and* the order in which they will be
388 * sent. The correct sequence number will be added when the
389 * packet is going to be sent.
390 */
391static const struct xboxone_init_packet xboxone_init_packets[] = {
392 XBOXONE_INIT_PKT(0x0e6f, 0x0165, xboxone_hori_init),
393 XBOXONE_INIT_PKT(0x0f0d, 0x0067, xboxone_hori_init),
394 XBOXONE_INIT_PKT(0x0000, 0x0000, xboxone_fw2015_init),
395 XBOXONE_INIT_PKT(0x24c6, 0x541a, xboxone_zerorumble_init),
396 XBOXONE_INIT_PKT(0x24c6, 0x542a, xboxone_zerorumble_init),
397 XBOXONE_INIT_PKT(0x24c6, 0x543a, xboxone_zerorumble_init),
398};
399
7fc595f4
PR
400struct xpad_output_packet {
401 u8 data[XPAD_PKT_LEN];
402 u8 len;
403 bool pending;
404};
405
406#define XPAD_OUT_CMD_IDX 0
407#define XPAD_OUT_FF_IDX 1
408#define XPAD_OUT_LED_IDX (1 + IS_ENABLED(CONFIG_JOYSTICK_XPAD_FF))
409#define XPAD_NUM_OUT_PACKETS (1 + \
410 IS_ENABLED(CONFIG_JOYSTICK_XPAD_FF) + \
411 IS_ENABLED(CONFIG_JOYSTICK_XPAD_LEDS))
412
1da177e4 413struct usb_xpad {
deb8ee43 414 struct input_dev *dev; /* input device interface */
09c8b00a 415 struct input_dev __rcu *x360w_dev;
deb8ee43 416 struct usb_device *udev; /* usb device */
8818e419 417 struct usb_interface *intf; /* usb interface */
05f091ab 418
09c8b00a
PLG
419 bool pad_present;
420 bool input_created;
99de0912 421
deb8ee43
DC
422 struct urb *irq_in; /* urb for interrupt in report */
423 unsigned char *idata; /* input data */
1da177e4 424 dma_addr_t idata_dma;
05f091ab 425
e01a06e8 426 struct urb *irq_out; /* urb for interrupt out report */
4220f7db 427 struct usb_anchor irq_out_anchor;
7fc595f4 428 bool irq_out_active; /* we must not use an active URB */
2a6d7527 429 u8 odata_serial; /* serial number for xbox one protocol */
4220f7db 430 unsigned char *odata; /* output data */
e01a06e8 431 dma_addr_t odata_dma;
7fc595f4
PR
432 spinlock_t odata_lock;
433
434 struct xpad_output_packet out_packets[XPAD_NUM_OUT_PACKETS];
435 int last_out_packet;
81093c98 436 int init_seq;
4994cd8d
JK
437
438#if defined(CONFIG_JOYSTICK_XPAD_LEDS)
439 struct xpad_led *led;
e01a06e8
JK
440#endif
441
4994cd8d 442 char phys[64]; /* physical device path */
deb8ee43 443
b45d44e7 444 int mapping; /* map d-pad to buttons or to axes */
c7d9f7eb 445 int xtype; /* type of xbox device */
e3b65174 446 int pad_nr; /* the order x360 pads were attached */
b8154002 447 const char *name; /* name of the device */
09c8b00a 448 struct work_struct work; /* init/remove device from callback */
1da177e4
LT
449};
450
09c8b00a
PLG
451static int xpad_init_input(struct usb_xpad *xpad);
452static void xpad_deinit_input(struct usb_xpad *xpad);
57b8443d 453static void xpadone_ack_mode_report(struct usb_xpad *xpad, u8 seq_num);
09c8b00a 454
1da177e4
LT
455/*
456 * xpad_process_packet
457 *
458 * Completes a request by converting the data into events for the
459 * input subsystem.
460 *
461 * The used report descriptor was taken from ITO Takayukis website:
462 * http://euc.jp/periphs/xbox-controller.ja.html
463 */
7d12e780 464static void xpad_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data)
1da177e4 465{
c5b7c7c3 466 struct input_dev *dev = xpad->dev;
1da177e4 467
7beae702
CF
468 if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
469 /* left stick */
470 input_report_abs(dev, ABS_X,
471 (__s16) le16_to_cpup((__le16 *)(data + 12)));
472 input_report_abs(dev, ABS_Y,
473 ~(__s16) le16_to_cpup((__le16 *)(data + 14)));
474
475 /* right stick */
476 input_report_abs(dev, ABS_RX,
477 (__s16) le16_to_cpup((__le16 *)(data + 16)));
478 input_report_abs(dev, ABS_RY,
479 ~(__s16) le16_to_cpup((__le16 *)(data + 18)));
480 }
05f091ab 481
1da177e4 482 /* triggers left/right */
b45d44e7
NL
483 if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
484 input_report_key(dev, BTN_TL2, data[10]);
485 input_report_key(dev, BTN_TR2, data[11]);
486 } else {
487 input_report_abs(dev, ABS_Z, data[10]);
488 input_report_abs(dev, ABS_RZ, data[11]);
489 }
05f091ab 490
1da177e4 491 /* digital pad */
b45d44e7 492 if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
7beae702
CF
493 /* dpad as buttons (left, right, up, down) */
494 input_report_key(dev, BTN_TRIGGER_HAPPY1, data[2] & 0x04);
495 input_report_key(dev, BTN_TRIGGER_HAPPY2, data[2] & 0x08);
496 input_report_key(dev, BTN_TRIGGER_HAPPY3, data[2] & 0x01);
497 input_report_key(dev, BTN_TRIGGER_HAPPY4, data[2] & 0x02);
b45d44e7
NL
498 } else {
499 input_report_abs(dev, ABS_HAT0X,
500 !!(data[2] & 0x08) - !!(data[2] & 0x04));
501 input_report_abs(dev, ABS_HAT0Y,
502 !!(data[2] & 0x02) - !!(data[2] & 0x01));
deb8ee43 503 }
05f091ab 504
1da177e4 505 /* start/back buttons and stick press left/right */
deb8ee43 506 input_report_key(dev, BTN_START, data[2] & 0x10);
7beae702 507 input_report_key(dev, BTN_SELECT, data[2] & 0x20);
deb8ee43
DC
508 input_report_key(dev, BTN_THUMBL, data[2] & 0x40);
509 input_report_key(dev, BTN_THUMBR, data[2] & 0x80);
05f091ab 510
1da177e4
LT
511 /* "analog" buttons A, B, X, Y */
512 input_report_key(dev, BTN_A, data[4]);
513 input_report_key(dev, BTN_B, data[5]);
514 input_report_key(dev, BTN_X, data[6]);
515 input_report_key(dev, BTN_Y, data[7]);
05f091ab 516
1da177e4
LT
517 /* "analog" buttons black, white */
518 input_report_key(dev, BTN_C, data[8]);
519 input_report_key(dev, BTN_Z, data[9]);
520
521 input_sync(dev);
522}
523
c7d9f7eb
JK
524/*
525 * xpad360_process_packet
526 *
527 * Completes a request by converting the data into events for the
528 * input subsystem. It is version for xbox 360 controller
529 *
530 * The used report descriptor was taken from:
531 * http://www.free60.org/wiki/Gamepad
532 */
533
09c8b00a 534static void xpad360_process_packet(struct usb_xpad *xpad, struct input_dev *dev,
20b3cdd6 535 u16 cmd, unsigned char *data)
c7d9f7eb 536{
1ff5fa3c
CG
537 /* valid pad data */
538 if (data[0] != 0x00)
539 return;
540
c7d9f7eb 541 /* digital pad */
b45d44e7 542 if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
7beae702
CF
543 /* dpad as buttons (left, right, up, down) */
544 input_report_key(dev, BTN_TRIGGER_HAPPY1, data[2] & 0x04);
545 input_report_key(dev, BTN_TRIGGER_HAPPY2, data[2] & 0x08);
546 input_report_key(dev, BTN_TRIGGER_HAPPY3, data[2] & 0x01);
547 input_report_key(dev, BTN_TRIGGER_HAPPY4, data[2] & 0x02);
5ee8bda9
PR
548 }
549
550 /*
551 * This should be a simple else block. However historically
552 * xbox360w has mapped DPAD to buttons while xbox360 did not. This
553 * made no sense, but now we can not just switch back and have to
554 * support both behaviors.
555 */
556 if (!(xpad->mapping & MAP_DPAD_TO_BUTTONS) ||
557 xpad->xtype == XTYPE_XBOX360W) {
b45d44e7
NL
558 input_report_abs(dev, ABS_HAT0X,
559 !!(data[2] & 0x08) - !!(data[2] & 0x04));
560 input_report_abs(dev, ABS_HAT0Y,
561 !!(data[2] & 0x02) - !!(data[2] & 0x01));
c7d9f7eb
JK
562 }
563
564 /* start/back buttons */
ae91d10a 565 input_report_key(dev, BTN_START, data[2] & 0x10);
7beae702 566 input_report_key(dev, BTN_SELECT, data[2] & 0x20);
c7d9f7eb
JK
567
568 /* stick press left/right */
569 input_report_key(dev, BTN_THUMBL, data[2] & 0x40);
570 input_report_key(dev, BTN_THUMBR, data[2] & 0x80);
571
572 /* buttons A,B,X,Y,TL,TR and MODE */
ae91d10a
DT
573 input_report_key(dev, BTN_A, data[3] & 0x10);
574 input_report_key(dev, BTN_B, data[3] & 0x20);
575 input_report_key(dev, BTN_X, data[3] & 0x40);
576 input_report_key(dev, BTN_Y, data[3] & 0x80);
577 input_report_key(dev, BTN_TL, data[3] & 0x01);
578 input_report_key(dev, BTN_TR, data[3] & 0x02);
579 input_report_key(dev, BTN_MODE, data[3] & 0x04);
c7d9f7eb 580
7beae702
CF
581 if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
582 /* left stick */
583 input_report_abs(dev, ABS_X,
584 (__s16) le16_to_cpup((__le16 *)(data + 6)));
585 input_report_abs(dev, ABS_Y,
586 ~(__s16) le16_to_cpup((__le16 *)(data + 8)));
587
588 /* right stick */
589 input_report_abs(dev, ABS_RX,
590 (__s16) le16_to_cpup((__le16 *)(data + 10)));
591 input_report_abs(dev, ABS_RY,
592 ~(__s16) le16_to_cpup((__le16 *)(data + 12)));
593 }
c7d9f7eb
JK
594
595 /* triggers left/right */
b45d44e7
NL
596 if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
597 input_report_key(dev, BTN_TL2, data[4]);
598 input_report_key(dev, BTN_TR2, data[5]);
599 } else {
600 input_report_abs(dev, ABS_Z, data[4]);
601 input_report_abs(dev, ABS_RZ, data[5]);
602 }
c7d9f7eb
JK
603
604 input_sync(dev);
605}
606
09c8b00a
PLG
607static void xpad_presence_work(struct work_struct *work)
608{
609 struct usb_xpad *xpad = container_of(work, struct usb_xpad, work);
610 int error;
611
612 if (xpad->pad_present) {
613 error = xpad_init_input(xpad);
614 if (error) {
615 /* complain only, not much else we can do here */
616 dev_err(&xpad->dev->dev,
617 "unable to init device: %d\n", error);
618 } else {
619 rcu_assign_pointer(xpad->x360w_dev, xpad->dev);
620 }
621 } else {
622 RCU_INIT_POINTER(xpad->x360w_dev, NULL);
623 synchronize_rcu();
624 /*
625 * Now that we are sure xpad360w_process_packet is not
626 * using input device we can get rid of it.
627 */
628 xpad_deinit_input(xpad);
629 }
630}
cae705ba 631
99de0912
BM
632/*
633 * xpad360w_process_packet
634 *
635 * Completes a request by converting the data into events for the
636 * input subsystem. It is version for xbox 360 wireless controller.
637 *
638 * Byte.Bit
639 * 00.1 - Status change: The controller or headset has connected/disconnected
640 * Bits 01.7 and 01.6 are valid
641 * 01.7 - Controller present
642 * 01.6 - Headset present
643 * 01.1 - Pad state (Bytes 4+) valid
644 *
645 */
99de0912
BM
646static void xpad360w_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data)
647{
09c8b00a
PLG
648 struct input_dev *dev;
649 bool present;
650
99de0912
BM
651 /* Presence change */
652 if (data[0] & 0x08) {
09c8b00a
PLG
653 present = (data[1] & 0x80) != 0;
654
655 if (xpad->pad_present != present) {
656 xpad->pad_present = present;
657 schedule_work(&xpad->work);
658 }
99de0912
BM
659 }
660
661 /* Valid pad data */
93a017aa 662 if (data[1] != 0x1)
99de0912
BM
663 return;
664
09c8b00a
PLG
665 rcu_read_lock();
666 dev = rcu_dereference(xpad->x360w_dev);
667 if (dev)
668 xpad360_process_packet(xpad, dev, cmd, &data[4]);
669 rcu_read_unlock();
99de0912
BM
670}
671
1a48ff81 672/*
4f88476c 673 * xpadone_process_packet
1a48ff81 674 *
4f88476c
DT
675 * Completes a request by converting the data into events for the
676 * input subsystem. This version is for the Xbox One controller.
677 *
678 * The report format was gleaned from
679 * https://github.com/kylelemons/xbox/blob/master/xbox.go
1a48ff81 680 */
4f88476c 681static void xpadone_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data)
1a48ff81 682{
4f88476c
DT
683 struct input_dev *dev = xpad->dev;
684
685 /* the xbox button has its own special report */
686 if (data[0] == 0X07) {
57b8443d
CG
687 /*
688 * The Xbox One S controller requires these reports to be
689 * acked otherwise it continues sending them forever and
690 * won't report further mode button events.
691 */
692 if (data[1] == 0x30)
693 xpadone_ack_mode_report(xpad, data[2]);
694
4f88476c
DT
695 input_report_key(dev, BTN_MODE, data[4] & 0x01);
696 input_sync(dev);
697 return;
698 }
699 /* check invalid packet */
700 else if (data[0] != 0X20)
701 return;
702
1a48ff81
TM
703 /* menu/view buttons */
704 input_report_key(dev, BTN_START, data[4] & 0x04);
705 input_report_key(dev, BTN_SELECT, data[4] & 0x08);
706
707 /* buttons A,B,X,Y */
708 input_report_key(dev, BTN_A, data[4] & 0x10);
709 input_report_key(dev, BTN_B, data[4] & 0x20);
710 input_report_key(dev, BTN_X, data[4] & 0x40);
711 input_report_key(dev, BTN_Y, data[4] & 0x80);
712
713 /* digital pad */
714 if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
715 /* dpad as buttons (left, right, up, down) */
716 input_report_key(dev, BTN_TRIGGER_HAPPY1, data[5] & 0x04);
717 input_report_key(dev, BTN_TRIGGER_HAPPY2, data[5] & 0x08);
718 input_report_key(dev, BTN_TRIGGER_HAPPY3, data[5] & 0x01);
719 input_report_key(dev, BTN_TRIGGER_HAPPY4, data[5] & 0x02);
720 } else {
721 input_report_abs(dev, ABS_HAT0X,
722 !!(data[5] & 0x08) - !!(data[5] & 0x04));
723 input_report_abs(dev, ABS_HAT0Y,
724 !!(data[5] & 0x02) - !!(data[5] & 0x01));
725 }
726
727 /* TL/TR */
728 input_report_key(dev, BTN_TL, data[5] & 0x10);
729 input_report_key(dev, BTN_TR, data[5] & 0x20);
730
731 /* stick press left/right */
732 input_report_key(dev, BTN_THUMBL, data[5] & 0x40);
733 input_report_key(dev, BTN_THUMBR, data[5] & 0x80);
734
735 if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
736 /* left stick */
737 input_report_abs(dev, ABS_X,
738 (__s16) le16_to_cpup((__le16 *)(data + 10)));
739 input_report_abs(dev, ABS_Y,
740 ~(__s16) le16_to_cpup((__le16 *)(data + 12)));
741
742 /* right stick */
743 input_report_abs(dev, ABS_RX,
744 (__s16) le16_to_cpup((__le16 *)(data + 14)));
745 input_report_abs(dev, ABS_RY,
746 ~(__s16) le16_to_cpup((__le16 *)(data + 16)));
747 }
748
749 /* triggers left/right */
750 if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
751 input_report_key(dev, BTN_TL2,
752 (__u16) le16_to_cpup((__le16 *)(data + 6)));
753 input_report_key(dev, BTN_TR2,
754 (__u16) le16_to_cpup((__le16 *)(data + 8)));
755 } else {
756 input_report_abs(dev, ABS_Z,
757 (__u16) le16_to_cpup((__le16 *)(data + 6)));
758 input_report_abs(dev, ABS_RZ,
759 (__u16) le16_to_cpup((__le16 *)(data + 8)));
760 }
761
762 input_sync(dev);
763}
764
7d12e780 765static void xpad_irq_in(struct urb *urb)
1da177e4
LT
766{
767 struct usb_xpad *xpad = urb->context;
8818e419 768 struct device *dev = &xpad->intf->dev;
6fc88f53 769 int retval, status;
05f091ab 770
6fc88f53
ON
771 status = urb->status;
772
773 switch (status) {
1da177e4
LT
774 case 0:
775 /* success */
776 break;
777 case -ECONNRESET:
778 case -ENOENT:
779 case -ESHUTDOWN:
780 /* this urb is terminated, clean up */
c4f0bbcd 781 dev_dbg(dev, "%s - urb shutting down with status: %d\n",
ea3e6c59 782 __func__, status);
1da177e4
LT
783 return;
784 default:
c4f0bbcd 785 dev_dbg(dev, "%s - nonzero urb status received: %d\n",
ea3e6c59 786 __func__, status);
1da177e4
LT
787 goto exit;
788 }
05f091ab 789
99de0912
BM
790 switch (xpad->xtype) {
791 case XTYPE_XBOX360:
09c8b00a 792 xpad360_process_packet(xpad, xpad->dev, 0, xpad->idata);
99de0912
BM
793 break;
794 case XTYPE_XBOX360W:
795 xpad360w_process_packet(xpad, 0, xpad->idata);
796 break;
1a48ff81
TM
797 case XTYPE_XBOXONE:
798 xpadone_process_packet(xpad, 0, xpad->idata);
799 break;
99de0912 800 default:
c7d9f7eb 801 xpad_process_packet(xpad, 0, xpad->idata);
99de0912 802 }
1da177e4
LT
803
804exit:
dd38d688 805 retval = usb_submit_urb(urb, GFP_ATOMIC);
1da177e4 806 if (retval)
c4f0bbcd 807 dev_err(dev, "%s - usb_submit_urb failed with result %d\n",
9cb757bf 808 __func__, retval);
1da177e4
LT
809}
810
81093c98
CG
811/* Callers must hold xpad->odata_lock spinlock */
812static bool xpad_prepare_next_init_packet(struct usb_xpad *xpad)
813{
814 const struct xboxone_init_packet *init_packet;
815
816 if (xpad->xtype != XTYPE_XBOXONE)
817 return false;
818
819 /* Perform initialization sequence for Xbox One pads that require it */
820 while (xpad->init_seq < ARRAY_SIZE(xboxone_init_packets)) {
821 init_packet = &xboxone_init_packets[xpad->init_seq++];
822
823 if (init_packet->idVendor != 0 &&
824 init_packet->idVendor != xpad->dev->id.vendor)
825 continue;
826
827 if (init_packet->idProduct != 0 &&
828 init_packet->idProduct != xpad->dev->id.product)
829 continue;
830
831 /* This packet applies to our device, so prepare to send it */
832 memcpy(xpad->odata, init_packet->data, init_packet->len);
833 xpad->irq_out->transfer_buffer_length = init_packet->len;
834
835 /* Update packet with current sequence number */
836 xpad->odata[2] = xpad->odata_serial++;
837 return true;
838 }
839
840 return false;
841}
842
7fc595f4
PR
843/* Callers must hold xpad->odata_lock spinlock */
844static bool xpad_prepare_next_out_packet(struct usb_xpad *xpad)
845{
846 struct xpad_output_packet *pkt, *packet = NULL;
847 int i;
848
81093c98
CG
849 /* We may have init packets to send before we can send user commands */
850 if (xpad_prepare_next_init_packet(xpad))
851 return true;
852
7fc595f4
PR
853 for (i = 0; i < XPAD_NUM_OUT_PACKETS; i++) {
854 if (++xpad->last_out_packet >= XPAD_NUM_OUT_PACKETS)
855 xpad->last_out_packet = 0;
856
857 pkt = &xpad->out_packets[xpad->last_out_packet];
858 if (pkt->pending) {
859 dev_dbg(&xpad->intf->dev,
860 "%s - found pending output packet %d\n",
861 __func__, xpad->last_out_packet);
862 packet = pkt;
863 break;
864 }
865 }
866
867 if (packet) {
868 memcpy(xpad->odata, packet->data, packet->len);
869 xpad->irq_out->transfer_buffer_length = packet->len;
4efc6939 870 packet->pending = false;
7fc595f4
PR
871 return true;
872 }
873
874 return false;
875}
876
877/* Callers must hold xpad->odata_lock spinlock */
878static int xpad_try_sending_next_out_packet(struct usb_xpad *xpad)
879{
880 int error;
881
882 if (!xpad->irq_out_active && xpad_prepare_next_out_packet(xpad)) {
4220f7db 883 usb_anchor_urb(xpad->irq_out, &xpad->irq_out_anchor);
7fc595f4
PR
884 error = usb_submit_urb(xpad->irq_out, GFP_ATOMIC);
885 if (error) {
886 dev_err(&xpad->intf->dev,
887 "%s - usb_submit_urb failed with result %d\n",
888 __func__, error);
4220f7db 889 usb_unanchor_urb(xpad->irq_out);
7fc595f4
PR
890 return -EIO;
891 }
892
893 xpad->irq_out_active = true;
894 }
895
896 return 0;
897}
898
e01a06e8
JK
899static void xpad_irq_out(struct urb *urb)
900{
9cb757bf 901 struct usb_xpad *xpad = urb->context;
8818e419 902 struct device *dev = &xpad->intf->dev;
7fc595f4
PR
903 int status = urb->status;
904 int error;
905 unsigned long flags;
6fc88f53 906
7fc595f4 907 spin_lock_irqsave(&xpad->odata_lock, flags);
e01a06e8 908
6fc88f53 909 switch (status) {
70a6f2e6 910 case 0:
e01a06e8 911 /* success */
7fc595f4
PR
912 xpad->irq_out_active = xpad_prepare_next_out_packet(xpad);
913 break;
70a6f2e6
MG
914
915 case -ECONNRESET:
916 case -ENOENT:
917 case -ESHUTDOWN:
918 /* this urb is terminated, clean up */
c4f0bbcd
GKH
919 dev_dbg(dev, "%s - urb shutting down with status: %d\n",
920 __func__, status);
7fc595f4
PR
921 xpad->irq_out_active = false;
922 break;
70a6f2e6
MG
923
924 default:
c4f0bbcd
GKH
925 dev_dbg(dev, "%s - nonzero urb status received: %d\n",
926 __func__, status);
7fc595f4 927 break;
e01a06e8
JK
928 }
929
7fc595f4 930 if (xpad->irq_out_active) {
4220f7db 931 usb_anchor_urb(urb, &xpad->irq_out_anchor);
7fc595f4
PR
932 error = usb_submit_urb(urb, GFP_ATOMIC);
933 if (error) {
934 dev_err(dev,
935 "%s - usb_submit_urb failed with result %d\n",
936 __func__, error);
4220f7db 937 usb_unanchor_urb(urb);
7fc595f4
PR
938 xpad->irq_out_active = false;
939 }
940 }
941
942 spin_unlock_irqrestore(&xpad->odata_lock, flags);
e01a06e8
JK
943}
944
c01b5e74
CG
945static int xpad_init_output(struct usb_interface *intf, struct usb_xpad *xpad,
946 struct usb_endpoint_descriptor *ep_irq_out)
e01a06e8 947{
49cc69b6 948 int error;
e01a06e8 949
b514d4f7 950 if (xpad->xtype == XTYPE_UNKNOWN)
e01a06e8
JK
951 return 0;
952
4220f7db
PR
953 init_usb_anchor(&xpad->irq_out_anchor);
954
997ea58e
DM
955 xpad->odata = usb_alloc_coherent(xpad->udev, XPAD_PKT_LEN,
956 GFP_KERNEL, &xpad->odata_dma);
a8c34e27
PR
957 if (!xpad->odata)
958 return -ENOMEM;
e01a06e8 959
7fc595f4 960 spin_lock_init(&xpad->odata_lock);
4994cd8d 961
e01a06e8 962 xpad->irq_out = usb_alloc_urb(0, GFP_KERNEL);
49cc69b6
AL
963 if (!xpad->irq_out) {
964 error = -ENOMEM;
a8c34e27 965 goto err_free_coherent;
49cc69b6 966 }
e01a06e8 967
e01a06e8
JK
968 usb_fill_int_urb(xpad->irq_out, xpad->udev,
969 usb_sndintpipe(xpad->udev, ep_irq_out->bEndpointAddress),
970 xpad->odata, XPAD_PKT_LEN,
971 xpad_irq_out, xpad, ep_irq_out->bInterval);
972 xpad->irq_out->transfer_dma = xpad->odata_dma;
973 xpad->irq_out->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
974
e01a06e8
JK
975 return 0;
976
a8c34e27
PR
977err_free_coherent:
978 usb_free_coherent(xpad->udev, XPAD_PKT_LEN, xpad->odata, xpad->odata_dma);
979 return error;
e01a06e8
JK
980}
981
4994cd8d 982static void xpad_stop_output(struct usb_xpad *xpad)
e01a06e8 983{
4220f7db
PR
984 if (xpad->xtype != XTYPE_UNKNOWN) {
985 if (!usb_wait_anchor_empty_timeout(&xpad->irq_out_anchor,
986 5000)) {
987 dev_warn(&xpad->intf->dev,
988 "timed out waiting for output URB to complete, killing\n");
989 usb_kill_anchored_urbs(&xpad->irq_out_anchor);
990 }
991 }
e01a06e8
JK
992}
993
4994cd8d 994static void xpad_deinit_output(struct usb_xpad *xpad)
e01a06e8 995{
b514d4f7 996 if (xpad->xtype != XTYPE_UNKNOWN) {
e01a06e8 997 usb_free_urb(xpad->irq_out);
997ea58e 998 usb_free_coherent(xpad->udev, XPAD_PKT_LEN,
e01a06e8
JK
999 xpad->odata, xpad->odata_dma);
1000 }
1001}
4994cd8d 1002
aba54876
PR
1003static int xpad_inquiry_pad_presence(struct usb_xpad *xpad)
1004{
7fc595f4
PR
1005 struct xpad_output_packet *packet =
1006 &xpad->out_packets[XPAD_OUT_CMD_IDX];
1007 unsigned long flags;
1008 int retval;
1009
1010 spin_lock_irqsave(&xpad->odata_lock, flags);
1011
1012 packet->data[0] = 0x08;
1013 packet->data[1] = 0x00;
1014 packet->data[2] = 0x0F;
1015 packet->data[3] = 0xC0;
1016 packet->data[4] = 0x00;
1017 packet->data[5] = 0x00;
1018 packet->data[6] = 0x00;
1019 packet->data[7] = 0x00;
1020 packet->data[8] = 0x00;
1021 packet->data[9] = 0x00;
1022 packet->data[10] = 0x00;
1023 packet->data[11] = 0x00;
1024 packet->len = 12;
1025 packet->pending = true;
1026
1027 /* Reset the sequence so we send out presence first */
1028 xpad->last_out_packet = -1;
1029 retval = xpad_try_sending_next_out_packet(xpad);
1030
1031 spin_unlock_irqrestore(&xpad->odata_lock, flags);
1032
1033 return retval;
1034}
1035
1036static int xpad_start_xbox_one(struct usb_xpad *xpad)
1037{
7fc595f4 1038 unsigned long flags;
aba54876
PR
1039 int retval;
1040
7fc595f4 1041 spin_lock_irqsave(&xpad->odata_lock, flags);
aba54876 1042
81093c98
CG
1043 /*
1044 * Begin the init sequence by attempting to send a packet.
1045 * We will cycle through the init packet sequence before
1046 * sending any packets from the output ring.
1047 */
1048 xpad->init_seq = 0;
7fc595f4 1049 retval = xpad_try_sending_next_out_packet(xpad);
aba54876 1050
7fc595f4 1051 spin_unlock_irqrestore(&xpad->odata_lock, flags);
aba54876
PR
1052
1053 return retval;
1054}
1055
57b8443d
CG
1056static void xpadone_ack_mode_report(struct usb_xpad *xpad, u8 seq_num)
1057{
1058 unsigned long flags;
1059 struct xpad_output_packet *packet =
1060 &xpad->out_packets[XPAD_OUT_CMD_IDX];
1061 static const u8 mode_report_ack[] = {
1062 0x01, 0x20, 0x00, 0x09, 0x00, 0x07, 0x20, 0x02,
1063 0x00, 0x00, 0x00, 0x00, 0x00
1064 };
1065
1066 spin_lock_irqsave(&xpad->odata_lock, flags);
1067
1068 packet->len = sizeof(mode_report_ack);
1069 memcpy(packet->data, mode_report_ack, packet->len);
1070 packet->data[2] = seq_num;
1071 packet->pending = true;
1072
1073 /* Reset the sequence so we send out the ack now */
1074 xpad->last_out_packet = -1;
1075 xpad_try_sending_next_out_packet(xpad);
1076
1077 spin_unlock_irqrestore(&xpad->odata_lock, flags);
1078}
1079
4994cd8d 1080#ifdef CONFIG_JOYSTICK_XPAD_FF
12187305 1081static int xpad_play_effect(struct input_dev *dev, void *data, struct ff_effect *effect)
4994cd8d
JK
1082{
1083 struct usb_xpad *xpad = input_get_drvdata(dev);
7fc595f4 1084 struct xpad_output_packet *packet = &xpad->out_packets[XPAD_OUT_FF_IDX];
06008152
PR
1085 __u16 strong;
1086 __u16 weak;
7fc595f4
PR
1087 int retval;
1088 unsigned long flags;
e01a06e8 1089
06008152
PR
1090 if (effect->type != FF_RUMBLE)
1091 return 0;
1092
1093 strong = effect->u.rumble.strong_magnitude;
1094 weak = effect->u.rumble.weak_magnitude;
1095
7fc595f4
PR
1096 spin_lock_irqsave(&xpad->odata_lock, flags);
1097
06008152
PR
1098 switch (xpad->xtype) {
1099 case XTYPE_XBOX:
7fc595f4
PR
1100 packet->data[0] = 0x00;
1101 packet->data[1] = 0x06;
1102 packet->data[2] = 0x00;
1103 packet->data[3] = strong / 256; /* left actuator */
1104 packet->data[4] = 0x00;
1105 packet->data[5] = weak / 256; /* right actuator */
1106 packet->len = 6;
1107 packet->pending = true;
06008152
PR
1108 break;
1109
1110 case XTYPE_XBOX360:
7fc595f4
PR
1111 packet->data[0] = 0x00;
1112 packet->data[1] = 0x08;
1113 packet->data[2] = 0x00;
1114 packet->data[3] = strong / 256; /* left actuator? */
1115 packet->data[4] = weak / 256; /* right actuator? */
1116 packet->data[5] = 0x00;
1117 packet->data[6] = 0x00;
1118 packet->data[7] = 0x00;
1119 packet->len = 8;
1120 packet->pending = true;
06008152
PR
1121 break;
1122
1123 case XTYPE_XBOX360W:
7fc595f4
PR
1124 packet->data[0] = 0x00;
1125 packet->data[1] = 0x01;
1126 packet->data[2] = 0x0F;
1127 packet->data[3] = 0xC0;
1128 packet->data[4] = 0x00;
1129 packet->data[5] = strong / 256;
1130 packet->data[6] = weak / 256;
1131 packet->data[7] = 0x00;
1132 packet->data[8] = 0x00;
1133 packet->data[9] = 0x00;
1134 packet->data[10] = 0x00;
1135 packet->data[11] = 0x00;
1136 packet->len = 12;
1137 packet->pending = true;
06008152
PR
1138 break;
1139
1140 case XTYPE_XBOXONE:
7fc595f4 1141 packet->data[0] = 0x09; /* activate rumble */
540c2608 1142 packet->data[1] = 0x00;
2a6d7527 1143 packet->data[2] = xpad->odata_serial++;
540c2608
CG
1144 packet->data[3] = 0x09;
1145 packet->data[4] = 0x00;
1146 packet->data[5] = 0x0F;
1147 packet->data[6] = 0x00;
1148 packet->data[7] = 0x00;
2a6d7527
PLG
1149 packet->data[8] = strong / 512; /* left actuator */
1150 packet->data[9] = weak / 512; /* right actuator */
ae3b4469
CG
1151 packet->data[10] = 0xFF; /* on period */
1152 packet->data[11] = 0x00; /* off period */
1153 packet->data[12] = 0xFF; /* repeat count */
2a6d7527 1154 packet->len = 13;
7fc595f4 1155 packet->pending = true;
06008152
PR
1156 break;
1157
1158 default:
1159 dev_dbg(&xpad->dev->dev,
1160 "%s - rumble command sent to unsupported xpad type: %d\n",
1161 __func__, xpad->xtype);
7fc595f4
PR
1162 retval = -EINVAL;
1163 goto out;
4994cd8d
JK
1164 }
1165
7fc595f4
PR
1166 retval = xpad_try_sending_next_out_packet(xpad);
1167
1168out:
1169 spin_unlock_irqrestore(&xpad->odata_lock, flags);
1170 return retval;
4994cd8d
JK
1171}
1172
1173static int xpad_init_ff(struct usb_xpad *xpad)
1174{
0604949c 1175 if (xpad->xtype == XTYPE_UNKNOWN)
cfbe2010
AH
1176 return 0;
1177
4994cd8d
JK
1178 input_set_capability(xpad->dev, EV_FF, FF_RUMBLE);
1179
1180 return input_ff_create_memless(xpad->dev, NULL, xpad_play_effect);
1181}
1182
1183#else
1184static int xpad_init_ff(struct usb_xpad *xpad) { return 0; }
1185#endif
1186
1187#if defined(CONFIG_JOYSTICK_XPAD_LEDS)
1188#include <linux/leds.h>
e3b65174
PR
1189#include <linux/idr.h>
1190
1191static DEFINE_IDA(xpad_pad_seq);
4994cd8d
JK
1192
1193struct xpad_led {
1194 char name[16];
1195 struct led_classdev led_cdev;
1196 struct usb_xpad *xpad;
1197};
1198
75b7f05d 1199/**
1f6f02b7 1200 * set the LEDs on Xbox360 / Wireless Controllers
75b7f05d
PLG
1201 * @param command
1202 * 0: off
1203 * 1: all blink, then previous setting
1204 * 2: 1/top-left blink, then on
1205 * 3: 2/top-right blink, then on
1206 * 4: 3/bottom-left blink, then on
1207 * 5: 4/bottom-right blink, then on
1208 * 6: 1/top-left on
1209 * 7: 2/top-right on
1210 * 8: 3/bottom-left on
1211 * 9: 4/bottom-right on
1212 * 10: rotate
1213 * 11: blink, based on previous setting
1214 * 12: slow blink, based on previous setting
1215 * 13: rotate with two lights
1216 * 14: persistent slow all blink
1217 * 15: blink once, then previous setting
1218 */
4994cd8d
JK
1219static void xpad_send_led_command(struct usb_xpad *xpad, int command)
1220{
7fc595f4
PR
1221 struct xpad_output_packet *packet =
1222 &xpad->out_packets[XPAD_OUT_LED_IDX];
1223 unsigned long flags;
1224
75b7f05d
PLG
1225 command %= 16;
1226
7fc595f4 1227 spin_lock_irqsave(&xpad->odata_lock, flags);
75b7f05d
PLG
1228
1229 switch (xpad->xtype) {
1230 case XTYPE_XBOX360:
7fc595f4
PR
1231 packet->data[0] = 0x01;
1232 packet->data[1] = 0x03;
1233 packet->data[2] = command;
1234 packet->len = 3;
1235 packet->pending = true;
75b7f05d 1236 break;
7fc595f4 1237
75b7f05d 1238 case XTYPE_XBOX360W:
7fc595f4
PR
1239 packet->data[0] = 0x00;
1240 packet->data[1] = 0x00;
1241 packet->data[2] = 0x08;
1242 packet->data[3] = 0x40 + command;
1243 packet->data[4] = 0x00;
1244 packet->data[5] = 0x00;
1245 packet->data[6] = 0x00;
1246 packet->data[7] = 0x00;
1247 packet->data[8] = 0x00;
1248 packet->data[9] = 0x00;
1249 packet->data[10] = 0x00;
1250 packet->data[11] = 0x00;
1251 packet->len = 12;
1252 packet->pending = true;
75b7f05d 1253 break;
4994cd8d 1254 }
75b7f05d 1255
7fc595f4
PR
1256 xpad_try_sending_next_out_packet(xpad);
1257
1258 spin_unlock_irqrestore(&xpad->odata_lock, flags);
4994cd8d
JK
1259}
1260
1f6f02b7
PR
1261/*
1262 * Light up the segment corresponding to the pad number on
1263 * Xbox 360 Controllers.
1264 */
cae705ba
PR
1265static void xpad_identify_controller(struct usb_xpad *xpad)
1266{
d9be398a 1267 led_set_brightness(&xpad->led->led_cdev, (xpad->pad_nr % 4) + 2);
cae705ba
PR
1268}
1269
4994cd8d
JK
1270static void xpad_led_set(struct led_classdev *led_cdev,
1271 enum led_brightness value)
1272{
1273 struct xpad_led *xpad_led = container_of(led_cdev,
1274 struct xpad_led, led_cdev);
1275
1276 xpad_send_led_command(xpad_led->xpad, value);
1277}
1278
1279static int xpad_led_probe(struct usb_xpad *xpad)
1280{
4994cd8d
JK
1281 struct xpad_led *led;
1282 struct led_classdev *led_cdev;
1283 int error;
1284
75b7f05d 1285 if (xpad->xtype != XTYPE_XBOX360 && xpad->xtype != XTYPE_XBOX360W)
4994cd8d
JK
1286 return 0;
1287
1288 xpad->led = led = kzalloc(sizeof(struct xpad_led), GFP_KERNEL);
1289 if (!led)
1290 return -ENOMEM;
1291
e3b65174
PR
1292 xpad->pad_nr = ida_simple_get(&xpad_pad_seq, 0, 0, GFP_KERNEL);
1293 if (xpad->pad_nr < 0) {
1294 error = xpad->pad_nr;
1295 goto err_free_mem;
1296 }
4994cd8d 1297
e3b65174 1298 snprintf(led->name, sizeof(led->name), "xpad%d", xpad->pad_nr);
4994cd8d
JK
1299 led->xpad = xpad;
1300
1301 led_cdev = &led->led_cdev;
1302 led_cdev->name = led->name;
1303 led_cdev->brightness_set = xpad_led_set;
a1fbf5bb 1304 led_cdev->flags = LED_CORE_SUSPENDRESUME;
4994cd8d
JK
1305
1306 error = led_classdev_register(&xpad->udev->dev, led_cdev);
e3b65174
PR
1307 if (error)
1308 goto err_free_id;
4994cd8d 1309
09c8b00a 1310 xpad_identify_controller(xpad);
fbe6a311 1311
4994cd8d 1312 return 0;
e3b65174
PR
1313
1314err_free_id:
1315 ida_simple_remove(&xpad_pad_seq, xpad->pad_nr);
1316err_free_mem:
1317 kfree(led);
1318 xpad->led = NULL;
1319 return error;
4994cd8d
JK
1320}
1321
1322static void xpad_led_disconnect(struct usb_xpad *xpad)
1323{
1324 struct xpad_led *xpad_led = xpad->led;
1325
1326 if (xpad_led) {
1327 led_classdev_unregister(&xpad_led->led_cdev);
e3b65174 1328 ida_simple_remove(&xpad_pad_seq, xpad->pad_nr);
6ff92a6d 1329 kfree(xpad_led);
4994cd8d
JK
1330 }
1331}
e01a06e8 1332#else
4994cd8d
JK
1333static int xpad_led_probe(struct usb_xpad *xpad) { return 0; }
1334static void xpad_led_disconnect(struct usb_xpad *xpad) { }
e01a06e8
JK
1335#endif
1336
4220f7db 1337static int xpad_start_input(struct usb_xpad *xpad)
1da177e4 1338{
4220f7db 1339 int error;
99de0912 1340
65cde54b 1341 if (usb_submit_urb(xpad->irq_in, GFP_KERNEL))
1da177e4 1342 return -EIO;
05f091ab 1343
4220f7db
PR
1344 if (xpad->xtype == XTYPE_XBOXONE) {
1345 error = xpad_start_xbox_one(xpad);
1346 if (error) {
1347 usb_kill_urb(xpad->irq_in);
1348 return error;
1349 }
1350 }
1a48ff81 1351
1da177e4
LT
1352 return 0;
1353}
1354
4220f7db 1355static void xpad_stop_input(struct usb_xpad *xpad)
1da177e4 1356{
4220f7db
PR
1357 usb_kill_urb(xpad->irq_in);
1358}
1359
f712a5a0
CG
1360static void xpad360w_poweroff_controller(struct usb_xpad *xpad)
1361{
1362 unsigned long flags;
1363 struct xpad_output_packet *packet =
1364 &xpad->out_packets[XPAD_OUT_CMD_IDX];
1365
1366 spin_lock_irqsave(&xpad->odata_lock, flags);
1367
1368 packet->data[0] = 0x00;
1369 packet->data[1] = 0x00;
1370 packet->data[2] = 0x08;
1371 packet->data[3] = 0xC0;
1372 packet->data[4] = 0x00;
1373 packet->data[5] = 0x00;
1374 packet->data[6] = 0x00;
1375 packet->data[7] = 0x00;
1376 packet->data[8] = 0x00;
1377 packet->data[9] = 0x00;
1378 packet->data[10] = 0x00;
1379 packet->data[11] = 0x00;
1380 packet->len = 12;
1381 packet->pending = true;
1382
1383 /* Reset the sequence so we send out poweroff now */
1384 xpad->last_out_packet = -1;
1385 xpad_try_sending_next_out_packet(xpad);
1386
1387 spin_unlock_irqrestore(&xpad->odata_lock, flags);
1388}
1389
4220f7db
PR
1390static int xpad360w_start_input(struct usb_xpad *xpad)
1391{
1392 int error;
1393
1394 error = usb_submit_urb(xpad->irq_in, GFP_KERNEL);
1395 if (error)
1396 return -EIO;
05f091ab 1397
4220f7db
PR
1398 /*
1399 * Send presence packet.
1400 * This will force the controller to resend connection packets.
1401 * This is useful in the case we activate the module after the
1402 * adapter has been plugged in, as it won't automatically
1403 * send us info about the controllers.
1404 */
1405 error = xpad_inquiry_pad_presence(xpad);
1406 if (error) {
99de0912 1407 usb_kill_urb(xpad->irq_in);
4220f7db
PR
1408 return error;
1409 }
161feb24 1410
4220f7db
PR
1411 return 0;
1412}
1413
1414static void xpad360w_stop_input(struct usb_xpad *xpad)
1415{
1416 usb_kill_urb(xpad->irq_in);
1417
1418 /* Make sure we are done with presence work if it was scheduled */
1419 flush_work(&xpad->work);
1420}
1421
1422static int xpad_open(struct input_dev *dev)
1423{
1424 struct usb_xpad *xpad = input_get_drvdata(dev);
1425
1426 return xpad_start_input(xpad);
1427}
1428
1429static void xpad_close(struct input_dev *dev)
1430{
1431 struct usb_xpad *xpad = input_get_drvdata(dev);
1432
1433 xpad_stop_input(xpad);
1da177e4
LT
1434}
1435
deb8ee43
DC
1436static void xpad_set_up_abs(struct input_dev *input_dev, signed short abs)
1437{
1a48ff81 1438 struct usb_xpad *xpad = input_get_drvdata(input_dev);
deb8ee43
DC
1439 set_bit(abs, input_dev->absbit);
1440
1441 switch (abs) {
1442 case ABS_X:
1443 case ABS_Y:
1444 case ABS_RX:
1445 case ABS_RY: /* the two sticks */
1446 input_set_abs_params(input_dev, abs, -32768, 32767, 16, 128);
1447 break;
1448 case ABS_Z:
b45d44e7 1449 case ABS_RZ: /* the triggers (if mapped to axes) */
1a48ff81
TM
1450 if (xpad->xtype == XTYPE_XBOXONE)
1451 input_set_abs_params(input_dev, abs, 0, 1023, 0, 0);
1452 else
1453 input_set_abs_params(input_dev, abs, 0, 255, 0, 0);
deb8ee43
DC
1454 break;
1455 case ABS_HAT0X:
b45d44e7 1456 case ABS_HAT0Y: /* the d-pad (only if dpad is mapped to axes */
deb8ee43
DC
1457 input_set_abs_params(input_dev, abs, -1, 1, 0, 0);
1458 break;
1459 }
1460}
1461
b8154002
PLG
1462static void xpad_deinit_input(struct usb_xpad *xpad)
1463{
09c8b00a
PLG
1464 if (xpad->input_created) {
1465 xpad->input_created = false;
1466 xpad_led_disconnect(xpad);
1467 input_unregister_device(xpad->dev);
1468 }
b8154002
PLG
1469}
1470
1471static int xpad_init_input(struct usb_xpad *xpad)
1da177e4 1472{
c5b7c7c3 1473 struct input_dev *input_dev;
49cc69b6 1474 int i, error;
05f091ab 1475
c5b7c7c3 1476 input_dev = input_allocate_device();
b8154002
PLG
1477 if (!input_dev)
1478 return -ENOMEM;
b45d44e7 1479
c5b7c7c3 1480 xpad->dev = input_dev;
b8154002 1481 input_dev->name = xpad->name;
c5b7c7c3 1482 input_dev->phys = xpad->phys;
b8154002 1483 usb_to_input_id(xpad->udev, &input_dev->id);
b6fc513d
PR
1484
1485 if (xpad->xtype == XTYPE_XBOX360W) {
1486 /* x360w controllers and the receiver have different ids */
1487 input_dev->id.product = 0x02a1;
1488 }
1489
b8154002 1490 input_dev->dev.parent = &xpad->intf->dev;
7791bdae
DT
1491
1492 input_set_drvdata(input_dev, xpad);
1493
4220f7db
PR
1494 if (xpad->xtype != XTYPE_XBOX360W) {
1495 input_dev->open = xpad_open;
1496 input_dev->close = xpad_close;
1497 }
05f091ab 1498
b8154002 1499 __set_bit(EV_KEY, input_dev->evbit);
7beae702
CF
1500
1501 if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
b8154002 1502 __set_bit(EV_ABS, input_dev->evbit);
7beae702
CF
1503 /* set up axes */
1504 for (i = 0; xpad_abs[i] >= 0; i++)
1505 xpad_set_up_abs(input_dev, xpad_abs[i]);
1506 }
05f091ab 1507
7beae702 1508 /* set up standard buttons */
fc55e952 1509 for (i = 0; xpad_common_btn[i] >= 0; i++)
b45d44e7 1510 __set_bit(xpad_common_btn[i], input_dev->keybit);
05f091ab 1511
7beae702 1512 /* set up model-specific ones */
1a48ff81
TM
1513 if (xpad->xtype == XTYPE_XBOX360 || xpad->xtype == XTYPE_XBOX360W ||
1514 xpad->xtype == XTYPE_XBOXONE) {
b45d44e7
NL
1515 for (i = 0; xpad360_btn[i] >= 0; i++)
1516 __set_bit(xpad360_btn[i], input_dev->keybit);
1517 } else {
1518 for (i = 0; xpad_btn[i] >= 0; i++)
1519 __set_bit(xpad_btn[i], input_dev->keybit);
1520 }
1521
1522 if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
1523 for (i = 0; xpad_btn_pad[i] >= 0; i++)
1524 __set_bit(xpad_btn_pad[i], input_dev->keybit);
5ee8bda9
PR
1525 }
1526
1527 /*
1528 * This should be a simple else block. However historically
1529 * xbox360w has mapped DPAD to buttons while xbox360 did not. This
1530 * made no sense, but now we can not just switch back and have to
1531 * support both behaviors.
1532 */
1533 if (!(xpad->mapping & MAP_DPAD_TO_BUTTONS) ||
1534 xpad->xtype == XTYPE_XBOX360W) {
deb8ee43 1535 for (i = 0; xpad_abs_pad[i] >= 0; i++)
1a48ff81 1536 xpad_set_up_abs(input_dev, xpad_abs_pad[i]);
b45d44e7
NL
1537 }
1538
1539 if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
1540 for (i = 0; xpad_btn_triggers[i] >= 0; i++)
1541 __set_bit(xpad_btn_triggers[i], input_dev->keybit);
1542 } else {
1543 for (i = 0; xpad_abs_triggers[i] >= 0; i++)
1544 xpad_set_up_abs(input_dev, xpad_abs_triggers[i]);
1545 }
05f091ab 1546
4994cd8d
JK
1547 error = xpad_init_ff(xpad);
1548 if (error)
b8154002 1549 goto err_free_input;
4994cd8d
JK
1550
1551 error = xpad_led_probe(xpad);
1552 if (error)
b8154002
PLG
1553 goto err_destroy_ff;
1554
1555 error = input_register_device(xpad->dev);
1556 if (error)
1557 goto err_disconnect_led;
1558
09c8b00a 1559 xpad->input_created = true;
b8154002
PLG
1560 return 0;
1561
1562err_disconnect_led:
1563 xpad_led_disconnect(xpad);
1564err_destroy_ff:
1565 input_ff_destroy(input_dev);
1566err_free_input:
1567 input_free_device(input_dev);
1568 return error;
1569}
1570
1571static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id)
1572{
1573 struct usb_device *udev = interface_to_usbdev(intf);
1574 struct usb_xpad *xpad;
c01b5e74 1575 struct usb_endpoint_descriptor *ep_irq_in, *ep_irq_out;
b8154002
PLG
1576 int i, error;
1577
caca925f
CG
1578 if (intf->cur_altsetting->desc.bNumEndpoints != 2)
1579 return -ENODEV;
1580
b8154002
PLG
1581 for (i = 0; xpad_device[i].idVendor; i++) {
1582 if ((le16_to_cpu(udev->descriptor.idVendor) == xpad_device[i].idVendor) &&
1583 (le16_to_cpu(udev->descriptor.idProduct) == xpad_device[i].idProduct))
1584 break;
1585 }
1586
b8154002
PLG
1587 xpad = kzalloc(sizeof(struct usb_xpad), GFP_KERNEL);
1588 if (!xpad)
1589 return -ENOMEM;
1590
1591 usb_make_path(udev, xpad->phys, sizeof(xpad->phys));
1592 strlcat(xpad->phys, "/input0", sizeof(xpad->phys));
1593
1594 xpad->idata = usb_alloc_coherent(udev, XPAD_PKT_LEN,
1595 GFP_KERNEL, &xpad->idata_dma);
1596 if (!xpad->idata) {
1597 error = -ENOMEM;
1598 goto err_free_mem;
1599 }
1600
1601 xpad->irq_in = usb_alloc_urb(0, GFP_KERNEL);
1602 if (!xpad->irq_in) {
1603 error = -ENOMEM;
1604 goto err_free_idata;
1605 }
1606
1607 xpad->udev = udev;
1608 xpad->intf = intf;
1609 xpad->mapping = xpad_device[i].mapping;
1610 xpad->xtype = xpad_device[i].xtype;
1611 xpad->name = xpad_device[i].name;
09c8b00a 1612 INIT_WORK(&xpad->work, xpad_presence_work);
b8154002
PLG
1613
1614 if (xpad->xtype == XTYPE_UNKNOWN) {
1615 if (intf->cur_altsetting->desc.bInterfaceClass == USB_CLASS_VENDOR_SPEC) {
1616 if (intf->cur_altsetting->desc.bInterfaceProtocol == 129)
1617 xpad->xtype = XTYPE_XBOX360W;
c7f14293
CG
1618 else if (intf->cur_altsetting->desc.bInterfaceProtocol == 208)
1619 xpad->xtype = XTYPE_XBOXONE;
b8154002
PLG
1620 else
1621 xpad->xtype = XTYPE_XBOX360;
1622 } else {
1623 xpad->xtype = XTYPE_XBOX;
1624 }
1625
1626 if (dpad_to_buttons)
1627 xpad->mapping |= MAP_DPAD_TO_BUTTONS;
1628 if (triggers_to_buttons)
1629 xpad->mapping |= MAP_TRIGGERS_TO_BUTTONS;
1630 if (sticks_to_null)
1631 xpad->mapping |= MAP_STICKS_TO_NULL;
1632 }
1633
c7f14293
CG
1634 if (xpad->xtype == XTYPE_XBOXONE &&
1635 intf->cur_altsetting->desc.bInterfaceNumber != 0) {
1636 /*
1637 * The Xbox One controller lists three interfaces all with the
1638 * same interface class, subclass and protocol. Differentiate by
1639 * interface number.
1640 */
1641 error = -ENODEV;
1642 goto err_free_in_urb;
1643 }
1644
c01b5e74
CG
1645 ep_irq_in = ep_irq_out = NULL;
1646
1647 for (i = 0; i < 2; i++) {
1648 struct usb_endpoint_descriptor *ep =
1649 &intf->cur_altsetting->endpoint[i].desc;
1650
1651 if (usb_endpoint_dir_in(ep))
1652 ep_irq_in = ep;
1653 else
1654 ep_irq_out = ep;
1655 }
1656
1657 if (!ep_irq_in || !ep_irq_out) {
1658 error = -ENODEV;
b8154002 1659 goto err_free_in_urb;
c01b5e74 1660 }
4994cd8d 1661
c01b5e74
CG
1662 error = xpad_init_output(intf, xpad, ep_irq_out);
1663 if (error)
1664 goto err_free_in_urb;
1a48ff81 1665
c5b7c7c3
DT
1666 usb_fill_int_urb(xpad->irq_in, udev,
1667 usb_rcvintpipe(udev, ep_irq_in->bEndpointAddress),
1668 xpad->idata, XPAD_PKT_LEN, xpad_irq_in,
1669 xpad, ep_irq_in->bInterval);
1670 xpad->irq_in->transfer_dma = xpad->idata_dma;
1671 xpad->irq_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
05f091ab 1672
1da177e4 1673 usb_set_intfdata(intf, xpad);
99de0912 1674
99de0912 1675 if (xpad->xtype == XTYPE_XBOX360W) {
e3f0f0a6
AL
1676 /*
1677 * Submit the int URB immediately rather than waiting for open
1678 * because we get status messages from the device whether
1679 * or not any controllers are attached. In fact, it's
1680 * exactly the message that a controller has arrived that
1681 * we're waiting for.
1682 */
4220f7db 1683 error = xpad360w_start_input(xpad);
e3f0f0a6 1684 if (error)
09c8b00a 1685 goto err_deinit_output;
aba54876 1686 /*
4220f7db
PR
1687 * Wireless controllers require RESET_RESUME to work properly
1688 * after suspend. Ideally this quirk should be in usb core
1689 * quirk list, but we have too many vendors producing these
1690 * controllers and we'd need to maintain 2 identical lists
1691 * here in this driver and in usb core.
aba54876 1692 */
4220f7db 1693 udev->quirks |= USB_QUIRK_RESET_RESUME;
09c8b00a
PLG
1694 } else {
1695 error = xpad_init_input(xpad);
1696 if (error)
1697 goto err_deinit_output;
99de0912 1698 }
1da177e4 1699 return 0;
c5b7c7c3 1700
b8154002
PLG
1701err_deinit_output:
1702 xpad_deinit_output(xpad);
1703err_free_in_urb:
1704 usb_free_urb(xpad->irq_in);
1705err_free_idata:
1706 usb_free_coherent(udev, XPAD_PKT_LEN, xpad->idata, xpad->idata_dma);
1707err_free_mem:
c5b7c7c3 1708 kfree(xpad);
5014186d 1709 return error;
1da177e4
LT
1710}
1711
1712static void xpad_disconnect(struct usb_interface *intf)
1713{
4220f7db 1714 struct usb_xpad *xpad = usb_get_intfdata(intf);
05f091ab 1715
09c8b00a 1716 if (xpad->xtype == XTYPE_XBOX360W)
4220f7db 1717 xpad360w_stop_input(xpad);
09c8b00a
PLG
1718
1719 xpad_deinit_input(xpad);
2a059159 1720
4220f7db
PR
1721 /*
1722 * Now that both input device and LED device are gone we can
1723 * stop output URB.
1724 */
1725 xpad_stop_output(xpad);
1726
1727 xpad_deinit_output(xpad);
1728
2a059159
DT
1729 usb_free_urb(xpad->irq_in);
1730 usb_free_coherent(xpad->udev, XPAD_PKT_LEN,
1731 xpad->idata, xpad->idata_dma);
1732
2a059159
DT
1733 kfree(xpad);
1734
1735 usb_set_intfdata(intf, NULL);
1da177e4
LT
1736}
1737
4220f7db
PR
1738static int xpad_suspend(struct usb_interface *intf, pm_message_t message)
1739{
1740 struct usb_xpad *xpad = usb_get_intfdata(intf);
1741 struct input_dev *input = xpad->dev;
1742
1743 if (xpad->xtype == XTYPE_XBOX360W) {
1744 /*
1745 * Wireless controllers always listen to input so
1746 * they are notified when controller shows up
1747 * or goes away.
1748 */
1749 xpad360w_stop_input(xpad);
f712a5a0
CG
1750
1751 /*
1752 * The wireless adapter is going off now, so the
1753 * gamepads are going to become disconnected.
1754 * Unless explicitly disabled, power them down
1755 * so they don't just sit there flashing.
1756 */
1757 if (auto_poweroff && xpad->pad_present)
1758 xpad360w_poweroff_controller(xpad);
4220f7db
PR
1759 } else {
1760 mutex_lock(&input->mutex);
1761 if (input->users)
1762 xpad_stop_input(xpad);
1763 mutex_unlock(&input->mutex);
1764 }
1765
1766 xpad_stop_output(xpad);
1767
1768 return 0;
1769}
1770
1771static int xpad_resume(struct usb_interface *intf)
1772{
1773 struct usb_xpad *xpad = usb_get_intfdata(intf);
1774 struct input_dev *input = xpad->dev;
1775 int retval = 0;
1776
1777 if (xpad->xtype == XTYPE_XBOX360W) {
1778 retval = xpad360w_start_input(xpad);
1779 } else {
1780 mutex_lock(&input->mutex);
a1fbf5bb 1781 if (input->users) {
4220f7db 1782 retval = xpad_start_input(xpad);
a1fbf5bb
CG
1783 } else if (xpad->xtype == XTYPE_XBOXONE) {
1784 /*
1785 * Even if there are no users, we'll send Xbox One pads
1786 * the startup sequence so they don't sit there and
1787 * blink until somebody opens the input device again.
1788 */
1789 retval = xpad_start_xbox_one(xpad);
1790 }
4220f7db
PR
1791 mutex_unlock(&input->mutex);
1792 }
1793
1794 return retval;
1795}
1796
1da177e4 1797static struct usb_driver xpad_driver = {
1da177e4
LT
1798 .name = "xpad",
1799 .probe = xpad_probe,
1800 .disconnect = xpad_disconnect,
4220f7db
PR
1801 .suspend = xpad_suspend,
1802 .resume = xpad_resume,
1803 .reset_resume = xpad_resume,
1da177e4
LT
1804 .id_table = xpad_table,
1805};
1806
08642e7c 1807module_usb_driver(xpad_driver);
1da177e4
LT
1808
1809MODULE_AUTHOR(DRIVER_AUTHOR);
1810MODULE_DESCRIPTION(DRIVER_DESC);
1811MODULE_LICENSE("GPL");