Merge branch 'for-linus-4.12-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[GitHub/LineageOS/android_kernel_motorola_exynos9610.git] / drivers / input / joystick / xpad.c
1 /*
2 * X-Box gamepad driver
3 *
4 * Copyright (c) 2002 Marko Friedemann <mfr@bmx-chemnitz.de>
5 * 2004 Oliver Schwartz <Oliver.Schwartz@gmx.de>,
6 * Steven Toth <steve@toth.demon.co.uk>,
7 * Franz Lehner <franz@caos.at>,
8 * Ivan Hawkes <blackhawk@ivanhawkes.com>
9 * 2005 Dominic Cerquetti <binary1230@yahoo.com>
10 * 2006 Adam Buchbinder <adam.buchbinder@gmail.com>
11 * 2007 Jan Kratochvil <honza@jikos.cz>
12 * 2010 Christoph Fritz <chf.fritz@googlemail.com>
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 *
29 * This driver is based on:
30 * - information from http://euc.jp/periphs/xbox-controller.ja.html
31 * - the iForce driver drivers/char/joystick/iforce.c
32 * - the skeleton-driver drivers/usb/usb-skeleton.c
33 * - Xbox 360 information http://www.free60.org/wiki/Gamepad
34 * - Xbox One information https://github.com/quantus/xbox-one-controller-protocol
35 *
36 * Thanks to:
37 * - ITO Takayuki for providing essential xpad information on his website
38 * - Vojtech Pavlik - iforce driver / input subsystem
39 * - Greg Kroah-Hartman - usb-skeleton driver
40 * - XBOX Linux project - extra USB id's
41 * - Pekka Pöyry (quantus) - Xbox One controller reverse engineering
42 *
43 * TODO:
44 * - fine tune axes (especially trigger axes)
45 * - fix "analog" buttons (reported as digital now)
46 * - get rumble working
47 * - need USB IDs for other dance pads
48 *
49 * History:
50 *
51 * 2002-06-27 - 0.0.1 : first version, just said "XBOX HID controller"
52 *
53 * 2002-07-02 - 0.0.2 : basic working version
54 * - all axes and 9 of the 10 buttons work (german InterAct device)
55 * - the black button does not work
56 *
57 * 2002-07-14 - 0.0.3 : rework by Vojtech Pavlik
58 * - indentation fixes
59 * - usb + input init sequence fixes
60 *
61 * 2002-07-16 - 0.0.4 : minor changes, merge with Vojtech's v0.0.3
62 * - verified the lack of HID and report descriptors
63 * - verified that ALL buttons WORK
64 * - fixed d-pad to axes mapping
65 *
66 * 2002-07-17 - 0.0.5 : simplified d-pad handling
67 *
68 * 2004-10-02 - 0.0.6 : DDR pad support
69 * - borrowed from the XBOX linux kernel
70 * - USB id's for commonly used dance pads are present
71 * - dance pads will map D-PAD to buttons, not axes
72 * - pass the module paramater 'dpad_to_buttons' to force
73 * the D-PAD to map to buttons if your pad is not detected
74 *
75 * Later changes can be tracked in SCM.
76 */
77
78 #include <linux/kernel.h>
79 #include <linux/input.h>
80 #include <linux/rcupdate.h>
81 #include <linux/slab.h>
82 #include <linux/stat.h>
83 #include <linux/module.h>
84 #include <linux/usb/input.h>
85 #include <linux/usb/quirks.h>
86
87 #define DRIVER_AUTHOR "Marko Friedemann <mfr@bmx-chemnitz.de>"
88 #define DRIVER_DESC "X-Box pad driver"
89
90 #define XPAD_PKT_LEN 64
91
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 */
94 #define MAP_DPAD_TO_BUTTONS (1 << 0)
95 #define MAP_TRIGGERS_TO_BUTTONS (1 << 1)
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)
99
100 #define XTYPE_XBOX 0
101 #define XTYPE_XBOX360 1
102 #define XTYPE_XBOX360W 2
103 #define XTYPE_XBOXONE 3
104 #define XTYPE_UNKNOWN 4
105
106 static bool dpad_to_buttons;
107 module_param(dpad_to_buttons, bool, S_IRUGO);
108 MODULE_PARM_DESC(dpad_to_buttons, "Map D-PAD to buttons rather than axes for unknown pads");
109
110 static bool triggers_to_buttons;
111 module_param(triggers_to_buttons, bool, S_IRUGO);
112 MODULE_PARM_DESC(triggers_to_buttons, "Map triggers to buttons rather than axes for unknown pads");
113
114 static bool sticks_to_null;
115 module_param(sticks_to_null, bool, S_IRUGO);
116 MODULE_PARM_DESC(sticks_to_null, "Do not map sticks at all for unknown pads");
117
118 static bool auto_poweroff = true;
119 module_param(auto_poweroff, bool, S_IWUSR | S_IRUGO);
120 MODULE_PARM_DESC(auto_poweroff, "Power off wireless controllers on suspend");
121
122 static const struct xpad_device {
123 u16 idVendor;
124 u16 idProduct;
125 char *name;
126 u8 mapping;
127 u8 xtype;
128 } xpad_device[] = {
129 { 0x045e, 0x0202, "Microsoft X-Box pad v1 (US)", 0, XTYPE_XBOX },
130 { 0x045e, 0x0285, "Microsoft X-Box pad (Japan)", 0, XTYPE_XBOX },
131 { 0x045e, 0x0287, "Microsoft Xbox Controller S", 0, XTYPE_XBOX },
132 { 0x045e, 0x0289, "Microsoft X-Box pad v2 (US)", 0, XTYPE_XBOX },
133 { 0x045e, 0x028e, "Microsoft X-Box 360 pad", 0, XTYPE_XBOX360 },
134 { 0x045e, 0x02d1, "Microsoft X-Box One pad", 0, XTYPE_XBOXONE },
135 { 0x045e, 0x02dd, "Microsoft X-Box One pad (Firmware 2015)", 0, XTYPE_XBOXONE },
136 { 0x045e, 0x02e3, "Microsoft X-Box One Elite pad", 0, XTYPE_XBOXONE },
137 { 0x045e, 0x02ea, "Microsoft X-Box One S pad", 0, XTYPE_XBOXONE },
138 { 0x045e, 0x0291, "Xbox 360 Wireless Receiver (XBOX)", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360W },
139 { 0x045e, 0x0719, "Xbox 360 Wireless Receiver", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360W },
140 { 0x044f, 0x0f07, "Thrustmaster, Inc. Controller", 0, XTYPE_XBOX },
141 { 0x044f, 0xb326, "Thrustmaster Gamepad GP XID", 0, XTYPE_XBOX360 },
142 { 0x046d, 0xc21d, "Logitech Gamepad F310", 0, XTYPE_XBOX360 },
143 { 0x046d, 0xc21e, "Logitech Gamepad F510", 0, XTYPE_XBOX360 },
144 { 0x046d, 0xc21f, "Logitech Gamepad F710", 0, XTYPE_XBOX360 },
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 },
154 { 0x0738, 0x4540, "Mad Catz Beat Pad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
155 { 0x0738, 0x4556, "Mad Catz Lynx Wireless Controller", 0, XTYPE_XBOX },
156 { 0x0738, 0x4716, "Mad Catz Wired Xbox 360 Controller", 0, XTYPE_XBOX360 },
157 { 0x0738, 0x4718, "Mad Catz Street Fighter IV FightStick SE", 0, XTYPE_XBOX360 },
158 { 0x0738, 0x4726, "Mad Catz Xbox 360 Controller", 0, XTYPE_XBOX360 },
159 { 0x0738, 0x4728, "Mad Catz Street Fighter IV FightPad", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
160 { 0x0738, 0x4738, "Mad Catz Wired Xbox 360 Controller (SFIV)", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
161 { 0x0738, 0x4740, "Mad Catz Beat Pad", 0, XTYPE_XBOX360 },
162 { 0x0738, 0x4a01, "Mad Catz FightStick TE 2", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOXONE },
163 { 0x0738, 0x6040, "Mad Catz Beat Pad Pro", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
164 { 0x0738, 0xb726, "Mad Catz Xbox controller - MW2", 0, XTYPE_XBOX360 },
165 { 0x0738, 0xbeef, "Mad Catz JOYTECH NEO SE Advanced GamePad", XTYPE_XBOX360 },
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 },
169 { 0x0c12, 0x8802, "Zeroplus Xbox Controller", 0, XTYPE_XBOX },
170 { 0x0c12, 0x8809, "RedOctane Xbox Dance Pad", DANCEPAD_MAP_CONFIG, XTYPE_XBOX },
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 },
174 { 0x0d2f, 0x0002, "Andamiro Pump It Up pad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
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 },
180 { 0x0e6f, 0x0105, "HSM3 Xbox360 dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
181 { 0x0e6f, 0x0113, "Afterglow AX.1 Gamepad for Xbox 360", 0, XTYPE_XBOX360 },
182 { 0x0e6f, 0x0139, "Afterglow Prismatic Wired Controller", 0, XTYPE_XBOXONE },
183 { 0x0e6f, 0x0201, "Pelican PL-3601 'TSZ' Wired Xbox 360 Controller", 0, XTYPE_XBOX360 },
184 { 0x0e6f, 0x0213, "Afterglow Gamepad for Xbox 360", 0, XTYPE_XBOX360 },
185 { 0x0e6f, 0x021f, "Rock Candy Gamepad for Xbox 360", 0, XTYPE_XBOX360 },
186 { 0x0e6f, 0x0146, "Rock Candy Wired Controller for Xbox One", 0, XTYPE_XBOXONE },
187 { 0x0e6f, 0x0301, "Logic3 Controller", 0, XTYPE_XBOX360 },
188 { 0x0e6f, 0x0401, "Logic3 Controller", 0, XTYPE_XBOX360 },
189 { 0x0e8f, 0x0201, "SmartJoy Frag Xpad/PS2 adaptor", 0, XTYPE_XBOX },
190 { 0x0e8f, 0x3008, "Generic xbox control (dealextreme)", 0, XTYPE_XBOX },
191 { 0x0f0d, 0x000a, "Hori Co. DOA4 FightStick", 0, XTYPE_XBOX360 },
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 },
194 { 0x0f0d, 0x0067, "HORIPAD ONE", 0, XTYPE_XBOXONE },
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 },
198 { 0x12ab, 0x0004, "Honey Bee Xbox360 dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
199 { 0x12ab, 0x0301, "PDP AFTERGLOW AX.1", 0, XTYPE_XBOX360 },
200 { 0x12ab, 0x8809, "Xbox DDR dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
201 { 0x1430, 0x4748, "RedOctane Guitar Hero X-plorer", 0, XTYPE_XBOX360 },
202 { 0x1430, 0x8888, "TX6500+ Dance Pad (first generation)", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
203 { 0x146b, 0x0601, "BigBen Interactive XBOX 360 Controller", 0, XTYPE_XBOX360 },
204 { 0x1532, 0x0037, "Razer Sabertooth", 0, XTYPE_XBOX360 },
205 { 0x1532, 0x0a03, "Razer Wildcat", 0, XTYPE_XBOXONE },
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 },
210 { 0x1689, 0xfd00, "Razer Onza Tournament Edition", 0, XTYPE_XBOX360 },
211 { 0x1689, 0xfd01, "Razer Onza Classic Edition", 0, XTYPE_XBOX360 },
212 { 0x24c6, 0x542a, "Xbox ONE spectra", 0, XTYPE_XBOXONE },
213 { 0x24c6, 0x5d04, "Razer Sabertooth", 0, XTYPE_XBOX360 },
214 { 0x1bad, 0x0002, "Harmonix Rock Band Guitar", 0, XTYPE_XBOX360 },
215 { 0x1bad, 0x0003, "Harmonix Rock Band Drumkit", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
216 { 0x1bad, 0xf016, "Mad Catz Xbox 360 Controller", 0, XTYPE_XBOX360 },
217 { 0x1bad, 0xf023, "MLG Pro Circuit Controller (Xbox)", 0, XTYPE_XBOX360 },
218 { 0x1bad, 0xf028, "Street Fighter IV FightPad", 0, XTYPE_XBOX360 },
219 { 0x1bad, 0xf038, "Street Fighter IV FightStick TE", 0, XTYPE_XBOX360 },
220 { 0x1bad, 0xf900, "Harmonix Xbox 360 Controller", 0, XTYPE_XBOX360 },
221 { 0x1bad, 0xf901, "Gamestop Xbox 360 Controller", 0, XTYPE_XBOX360 },
222 { 0x1bad, 0xf903, "Tron Xbox 360 controller", 0, XTYPE_XBOX360 },
223 { 0x24c6, 0x5000, "Razer Atrox Arcade Stick", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
224 { 0x24c6, 0x5300, "PowerA MINI PROEX Controller", 0, XTYPE_XBOX360 },
225 { 0x24c6, 0x5303, "Xbox Airflo wired controller", 0, XTYPE_XBOX360 },
226 { 0x24c6, 0x541a, "PowerA Xbox One Mini Wired Controller", 0, XTYPE_XBOXONE },
227 { 0x24c6, 0x543a, "PowerA Xbox One wired controller", 0, XTYPE_XBOXONE },
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 },
232 { 0x24c6, 0x5b03, "Thrustmaster Ferrari 458 Racing Wheel", 0, XTYPE_XBOX360 },
233 { 0xffff, 0xffff, "Chinese-made Xbox Controller", 0, XTYPE_XBOX },
234 { 0x0000, 0x0000, "Generic X-Box pad", 0, XTYPE_UNKNOWN }
235 };
236
237 /* buttons shared with xbox and xbox360 */
238 static const signed short xpad_common_btn[] = {
239 BTN_A, BTN_B, BTN_X, BTN_Y, /* "analog" buttons */
240 BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR, /* start/back/sticks */
241 -1 /* terminating entry */
242 };
243
244 /* original xbox controllers only */
245 static const signed short xpad_btn[] = {
246 BTN_C, BTN_Z, /* "analog" buttons */
247 -1 /* terminating entry */
248 };
249
250 /* used when dpad is mapped to buttons */
251 static const signed short xpad_btn_pad[] = {
252 BTN_TRIGGER_HAPPY1, BTN_TRIGGER_HAPPY2, /* d-pad left, right */
253 BTN_TRIGGER_HAPPY3, BTN_TRIGGER_HAPPY4, /* d-pad up, down */
254 -1 /* terminating entry */
255 };
256
257 /* used when triggers are mapped to buttons */
258 static const signed short xpad_btn_triggers[] = {
259 BTN_TL2, BTN_TR2, /* triggers left/right */
260 -1
261 };
262
263 static 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
269 static const signed short xpad_abs[] = {
270 ABS_X, ABS_Y, /* left stick */
271 ABS_RX, ABS_RY, /* right stick */
272 -1 /* terminating entry */
273 };
274
275 /* used when dpad is mapped to axes */
276 static const signed short xpad_abs_pad[] = {
277 ABS_HAT0X, ABS_HAT0Y, /* d-pad axes */
278 -1 /* terminating entry */
279 };
280
281 /* used when triggers are mapped to axes */
282 static const signed short xpad_abs_triggers[] = {
283 ABS_Z, ABS_RZ, /* triggers left/right */
284 -1
285 };
286
287 /*
288 * Xbox 360 has a vendor-specific class, so we cannot match it with only
289 * USB_INTERFACE_INFO (also specifically refused by USB subsystem), so we
290 * match against vendor id as well. Wired Xbox 360 devices have protocol 1,
291 * wireless controllers have protocol 129.
292 */
293 #define XPAD_XBOX360_VENDOR_PROTOCOL(vend,pr) \
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, \
298 .bInterfaceProtocol = (pr)
299 #define XPAD_XBOX360_VENDOR(vend) \
300 { XPAD_XBOX360_VENDOR_PROTOCOL(vend,1) }, \
301 { XPAD_XBOX360_VENDOR_PROTOCOL(vend,129) }
302
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
313 static struct usb_device_id xpad_table[] = {
314 { USB_INTERFACE_INFO('X', 'B', 0) }, /* X-Box USB-IF not approved class */
315 XPAD_XBOX360_VENDOR(0x044f), /* Thrustmaster X-Box 360 controllers */
316 XPAD_XBOX360_VENDOR(0x045e), /* Microsoft X-Box 360 controllers */
317 XPAD_XBOXONE_VENDOR(0x045e), /* Microsoft X-Box One controllers */
318 XPAD_XBOX360_VENDOR(0x046d), /* Logitech X-Box 360 style controllers */
319 XPAD_XBOX360_VENDOR(0x0738), /* Mad Catz X-Box 360 controllers */
320 { USB_DEVICE(0x0738, 0x4540) }, /* Mad Catz Beat Pad */
321 XPAD_XBOXONE_VENDOR(0x0738), /* Mad Catz FightStick TE 2 */
322 XPAD_XBOX360_VENDOR(0x0e6f), /* 0x0e6f X-Box 360 controllers */
323 XPAD_XBOXONE_VENDOR(0x0e6f), /* 0x0e6f X-Box One controllers */
324 XPAD_XBOX360_VENDOR(0x0f0d), /* Hori Controllers */
325 XPAD_XBOXONE_VENDOR(0x0f0d), /* Hori Controllers */
326 XPAD_XBOX360_VENDOR(0x12ab), /* X-Box 360 dance pads */
327 XPAD_XBOX360_VENDOR(0x1430), /* RedOctane X-Box 360 controllers */
328 XPAD_XBOX360_VENDOR(0x146b), /* BigBen Interactive Controllers */
329 XPAD_XBOX360_VENDOR(0x1532), /* Razer Sabertooth */
330 XPAD_XBOXONE_VENDOR(0x1532), /* Razer Wildcat */
331 XPAD_XBOX360_VENDOR(0x15e4), /* Numark X-Box 360 controllers */
332 XPAD_XBOX360_VENDOR(0x162e), /* Joytech X-Box 360 controllers */
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 */
337 { }
338 };
339
340 MODULE_DEVICE_TABLE(usb, xpad_table);
341
342 struct 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 */
362 static 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 */
371 static 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 */
380 static 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 */
391 static 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
400 struct 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
413 struct usb_xpad {
414 struct input_dev *dev; /* input device interface */
415 struct input_dev __rcu *x360w_dev;
416 struct usb_device *udev; /* usb device */
417 struct usb_interface *intf; /* usb interface */
418
419 bool pad_present;
420 bool input_created;
421
422 struct urb *irq_in; /* urb for interrupt in report */
423 unsigned char *idata; /* input data */
424 dma_addr_t idata_dma;
425
426 struct urb *irq_out; /* urb for interrupt out report */
427 struct usb_anchor irq_out_anchor;
428 bool irq_out_active; /* we must not use an active URB */
429 u8 odata_serial; /* serial number for xbox one protocol */
430 unsigned char *odata; /* output data */
431 dma_addr_t odata_dma;
432 spinlock_t odata_lock;
433
434 struct xpad_output_packet out_packets[XPAD_NUM_OUT_PACKETS];
435 int last_out_packet;
436 int init_seq;
437
438 #if defined(CONFIG_JOYSTICK_XPAD_LEDS)
439 struct xpad_led *led;
440 #endif
441
442 char phys[64]; /* physical device path */
443
444 int mapping; /* map d-pad to buttons or to axes */
445 int xtype; /* type of xbox device */
446 int pad_nr; /* the order x360 pads were attached */
447 const char *name; /* name of the device */
448 struct work_struct work; /* init/remove device from callback */
449 };
450
451 static int xpad_init_input(struct usb_xpad *xpad);
452 static void xpad_deinit_input(struct usb_xpad *xpad);
453 static void xpadone_ack_mode_report(struct usb_xpad *xpad, u8 seq_num);
454
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 */
464 static void xpad_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data)
465 {
466 struct input_dev *dev = xpad->dev;
467
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 }
481
482 /* triggers left/right */
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 }
490
491 /* digital pad */
492 if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
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);
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));
503 }
504
505 /* start/back buttons and stick press left/right */
506 input_report_key(dev, BTN_START, data[2] & 0x10);
507 input_report_key(dev, BTN_SELECT, data[2] & 0x20);
508 input_report_key(dev, BTN_THUMBL, data[2] & 0x40);
509 input_report_key(dev, BTN_THUMBR, data[2] & 0x80);
510
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]);
516
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
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
534 static void xpad360_process_packet(struct usb_xpad *xpad, struct input_dev *dev,
535 u16 cmd, unsigned char *data)
536 {
537 /* valid pad data */
538 if (data[0] != 0x00)
539 return;
540
541 /* digital pad */
542 if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
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);
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) {
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));
562 }
563
564 /* start/back buttons */
565 input_report_key(dev, BTN_START, data[2] & 0x10);
566 input_report_key(dev, BTN_SELECT, data[2] & 0x20);
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 */
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);
580
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 }
594
595 /* triggers left/right */
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 }
603
604 input_sync(dev);
605 }
606
607 static 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 }
631
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 */
646 static void xpad360w_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data)
647 {
648 struct input_dev *dev;
649 bool present;
650
651 /* Presence change */
652 if (data[0] & 0x08) {
653 present = (data[1] & 0x80) != 0;
654
655 if (xpad->pad_present != present) {
656 xpad->pad_present = present;
657 schedule_work(&xpad->work);
658 }
659 }
660
661 /* Valid pad data */
662 if (data[1] != 0x1)
663 return;
664
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();
670 }
671
672 /*
673 * xpadone_process_packet
674 *
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
680 */
681 static void xpadone_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data)
682 {
683 struct input_dev *dev = xpad->dev;
684
685 /* the xbox button has its own special report */
686 if (data[0] == 0X07) {
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
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
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
765 static void xpad_irq_in(struct urb *urb)
766 {
767 struct usb_xpad *xpad = urb->context;
768 struct device *dev = &xpad->intf->dev;
769 int retval, status;
770
771 status = urb->status;
772
773 switch (status) {
774 case 0:
775 /* success */
776 break;
777 case -ECONNRESET:
778 case -ENOENT:
779 case -ESHUTDOWN:
780 /* this urb is terminated, clean up */
781 dev_dbg(dev, "%s - urb shutting down with status: %d\n",
782 __func__, status);
783 return;
784 default:
785 dev_dbg(dev, "%s - nonzero urb status received: %d\n",
786 __func__, status);
787 goto exit;
788 }
789
790 switch (xpad->xtype) {
791 case XTYPE_XBOX360:
792 xpad360_process_packet(xpad, xpad->dev, 0, xpad->idata);
793 break;
794 case XTYPE_XBOX360W:
795 xpad360w_process_packet(xpad, 0, xpad->idata);
796 break;
797 case XTYPE_XBOXONE:
798 xpadone_process_packet(xpad, 0, xpad->idata);
799 break;
800 default:
801 xpad_process_packet(xpad, 0, xpad->idata);
802 }
803
804 exit:
805 retval = usb_submit_urb(urb, GFP_ATOMIC);
806 if (retval)
807 dev_err(dev, "%s - usb_submit_urb failed with result %d\n",
808 __func__, retval);
809 }
810
811 /* Callers must hold xpad->odata_lock spinlock */
812 static 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
843 /* Callers must hold xpad->odata_lock spinlock */
844 static bool xpad_prepare_next_out_packet(struct usb_xpad *xpad)
845 {
846 struct xpad_output_packet *pkt, *packet = NULL;
847 int i;
848
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
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;
870 packet->pending = false;
871 return true;
872 }
873
874 return false;
875 }
876
877 /* Callers must hold xpad->odata_lock spinlock */
878 static 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)) {
883 usb_anchor_urb(xpad->irq_out, &xpad->irq_out_anchor);
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);
889 usb_unanchor_urb(xpad->irq_out);
890 return -EIO;
891 }
892
893 xpad->irq_out_active = true;
894 }
895
896 return 0;
897 }
898
899 static void xpad_irq_out(struct urb *urb)
900 {
901 struct usb_xpad *xpad = urb->context;
902 struct device *dev = &xpad->intf->dev;
903 int status = urb->status;
904 int error;
905 unsigned long flags;
906
907 spin_lock_irqsave(&xpad->odata_lock, flags);
908
909 switch (status) {
910 case 0:
911 /* success */
912 xpad->irq_out_active = xpad_prepare_next_out_packet(xpad);
913 break;
914
915 case -ECONNRESET:
916 case -ENOENT:
917 case -ESHUTDOWN:
918 /* this urb is terminated, clean up */
919 dev_dbg(dev, "%s - urb shutting down with status: %d\n",
920 __func__, status);
921 xpad->irq_out_active = false;
922 break;
923
924 default:
925 dev_dbg(dev, "%s - nonzero urb status received: %d\n",
926 __func__, status);
927 break;
928 }
929
930 if (xpad->irq_out_active) {
931 usb_anchor_urb(urb, &xpad->irq_out_anchor);
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);
937 usb_unanchor_urb(urb);
938 xpad->irq_out_active = false;
939 }
940 }
941
942 spin_unlock_irqrestore(&xpad->odata_lock, flags);
943 }
944
945 static int xpad_init_output(struct usb_interface *intf, struct usb_xpad *xpad,
946 struct usb_endpoint_descriptor *ep_irq_out)
947 {
948 int error;
949
950 if (xpad->xtype == XTYPE_UNKNOWN)
951 return 0;
952
953 init_usb_anchor(&xpad->irq_out_anchor);
954
955 xpad->odata = usb_alloc_coherent(xpad->udev, XPAD_PKT_LEN,
956 GFP_KERNEL, &xpad->odata_dma);
957 if (!xpad->odata)
958 return -ENOMEM;
959
960 spin_lock_init(&xpad->odata_lock);
961
962 xpad->irq_out = usb_alloc_urb(0, GFP_KERNEL);
963 if (!xpad->irq_out) {
964 error = -ENOMEM;
965 goto err_free_coherent;
966 }
967
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
975 return 0;
976
977 err_free_coherent:
978 usb_free_coherent(xpad->udev, XPAD_PKT_LEN, xpad->odata, xpad->odata_dma);
979 return error;
980 }
981
982 static void xpad_stop_output(struct usb_xpad *xpad)
983 {
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 }
992 }
993
994 static void xpad_deinit_output(struct usb_xpad *xpad)
995 {
996 if (xpad->xtype != XTYPE_UNKNOWN) {
997 usb_free_urb(xpad->irq_out);
998 usb_free_coherent(xpad->udev, XPAD_PKT_LEN,
999 xpad->odata, xpad->odata_dma);
1000 }
1001 }
1002
1003 static int xpad_inquiry_pad_presence(struct usb_xpad *xpad)
1004 {
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
1036 static int xpad_start_xbox_one(struct usb_xpad *xpad)
1037 {
1038 unsigned long flags;
1039 int retval;
1040
1041 spin_lock_irqsave(&xpad->odata_lock, flags);
1042
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;
1049 retval = xpad_try_sending_next_out_packet(xpad);
1050
1051 spin_unlock_irqrestore(&xpad->odata_lock, flags);
1052
1053 return retval;
1054 }
1055
1056 static 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
1080 #ifdef CONFIG_JOYSTICK_XPAD_FF
1081 static int xpad_play_effect(struct input_dev *dev, void *data, struct ff_effect *effect)
1082 {
1083 struct usb_xpad *xpad = input_get_drvdata(dev);
1084 struct xpad_output_packet *packet = &xpad->out_packets[XPAD_OUT_FF_IDX];
1085 __u16 strong;
1086 __u16 weak;
1087 int retval;
1088 unsigned long flags;
1089
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
1096 spin_lock_irqsave(&xpad->odata_lock, flags);
1097
1098 switch (xpad->xtype) {
1099 case XTYPE_XBOX:
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;
1108 break;
1109
1110 case XTYPE_XBOX360:
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;
1121 break;
1122
1123 case XTYPE_XBOX360W:
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;
1138 break;
1139
1140 case XTYPE_XBOXONE:
1141 packet->data[0] = 0x09; /* activate rumble */
1142 packet->data[1] = 0x00;
1143 packet->data[2] = xpad->odata_serial++;
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;
1149 packet->data[8] = strong / 512; /* left actuator */
1150 packet->data[9] = weak / 512; /* right actuator */
1151 packet->data[10] = 0xFF; /* on period */
1152 packet->data[11] = 0x00; /* off period */
1153 packet->data[12] = 0xFF; /* repeat count */
1154 packet->len = 13;
1155 packet->pending = true;
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);
1162 retval = -EINVAL;
1163 goto out;
1164 }
1165
1166 retval = xpad_try_sending_next_out_packet(xpad);
1167
1168 out:
1169 spin_unlock_irqrestore(&xpad->odata_lock, flags);
1170 return retval;
1171 }
1172
1173 static int xpad_init_ff(struct usb_xpad *xpad)
1174 {
1175 if (xpad->xtype == XTYPE_UNKNOWN)
1176 return 0;
1177
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
1184 static 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>
1189 #include <linux/idr.h>
1190
1191 static DEFINE_IDA(xpad_pad_seq);
1192
1193 struct xpad_led {
1194 char name[16];
1195 struct led_classdev led_cdev;
1196 struct usb_xpad *xpad;
1197 };
1198
1199 /**
1200 * set the LEDs on Xbox360 / Wireless Controllers
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 */
1219 static void xpad_send_led_command(struct usb_xpad *xpad, int command)
1220 {
1221 struct xpad_output_packet *packet =
1222 &xpad->out_packets[XPAD_OUT_LED_IDX];
1223 unsigned long flags;
1224
1225 command %= 16;
1226
1227 spin_lock_irqsave(&xpad->odata_lock, flags);
1228
1229 switch (xpad->xtype) {
1230 case XTYPE_XBOX360:
1231 packet->data[0] = 0x01;
1232 packet->data[1] = 0x03;
1233 packet->data[2] = command;
1234 packet->len = 3;
1235 packet->pending = true;
1236 break;
1237
1238 case XTYPE_XBOX360W:
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;
1253 break;
1254 }
1255
1256 xpad_try_sending_next_out_packet(xpad);
1257
1258 spin_unlock_irqrestore(&xpad->odata_lock, flags);
1259 }
1260
1261 /*
1262 * Light up the segment corresponding to the pad number on
1263 * Xbox 360 Controllers.
1264 */
1265 static void xpad_identify_controller(struct usb_xpad *xpad)
1266 {
1267 led_set_brightness(&xpad->led->led_cdev, (xpad->pad_nr % 4) + 2);
1268 }
1269
1270 static 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
1279 static int xpad_led_probe(struct usb_xpad *xpad)
1280 {
1281 struct xpad_led *led;
1282 struct led_classdev *led_cdev;
1283 int error;
1284
1285 if (xpad->xtype != XTYPE_XBOX360 && xpad->xtype != XTYPE_XBOX360W)
1286 return 0;
1287
1288 xpad->led = led = kzalloc(sizeof(struct xpad_led), GFP_KERNEL);
1289 if (!led)
1290 return -ENOMEM;
1291
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 }
1297
1298 snprintf(led->name, sizeof(led->name), "xpad%d", xpad->pad_nr);
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;
1304 led_cdev->flags = LED_CORE_SUSPENDRESUME;
1305
1306 error = led_classdev_register(&xpad->udev->dev, led_cdev);
1307 if (error)
1308 goto err_free_id;
1309
1310 xpad_identify_controller(xpad);
1311
1312 return 0;
1313
1314 err_free_id:
1315 ida_simple_remove(&xpad_pad_seq, xpad->pad_nr);
1316 err_free_mem:
1317 kfree(led);
1318 xpad->led = NULL;
1319 return error;
1320 }
1321
1322 static 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);
1328 ida_simple_remove(&xpad_pad_seq, xpad->pad_nr);
1329 kfree(xpad_led);
1330 }
1331 }
1332 #else
1333 static int xpad_led_probe(struct usb_xpad *xpad) { return 0; }
1334 static void xpad_led_disconnect(struct usb_xpad *xpad) { }
1335 #endif
1336
1337 static int xpad_start_input(struct usb_xpad *xpad)
1338 {
1339 int error;
1340
1341 if (usb_submit_urb(xpad->irq_in, GFP_KERNEL))
1342 return -EIO;
1343
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 }
1351
1352 return 0;
1353 }
1354
1355 static void xpad_stop_input(struct usb_xpad *xpad)
1356 {
1357 usb_kill_urb(xpad->irq_in);
1358 }
1359
1360 static 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
1390 static 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;
1397
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) {
1407 usb_kill_urb(xpad->irq_in);
1408 return error;
1409 }
1410
1411 return 0;
1412 }
1413
1414 static 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
1422 static 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
1429 static void xpad_close(struct input_dev *dev)
1430 {
1431 struct usb_xpad *xpad = input_get_drvdata(dev);
1432
1433 xpad_stop_input(xpad);
1434 }
1435
1436 static void xpad_set_up_abs(struct input_dev *input_dev, signed short abs)
1437 {
1438 struct usb_xpad *xpad = input_get_drvdata(input_dev);
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:
1449 case ABS_RZ: /* the triggers (if mapped to axes) */
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);
1454 break;
1455 case ABS_HAT0X:
1456 case ABS_HAT0Y: /* the d-pad (only if dpad is mapped to axes */
1457 input_set_abs_params(input_dev, abs, -1, 1, 0, 0);
1458 break;
1459 }
1460 }
1461
1462 static void xpad_deinit_input(struct usb_xpad *xpad)
1463 {
1464 if (xpad->input_created) {
1465 xpad->input_created = false;
1466 xpad_led_disconnect(xpad);
1467 input_unregister_device(xpad->dev);
1468 }
1469 }
1470
1471 static int xpad_init_input(struct usb_xpad *xpad)
1472 {
1473 struct input_dev *input_dev;
1474 int i, error;
1475
1476 input_dev = input_allocate_device();
1477 if (!input_dev)
1478 return -ENOMEM;
1479
1480 xpad->dev = input_dev;
1481 input_dev->name = xpad->name;
1482 input_dev->phys = xpad->phys;
1483 usb_to_input_id(xpad->udev, &input_dev->id);
1484
1485 if (xpad->xtype == XTYPE_XBOX360W) {
1486 /* x360w controllers and the receiver have different ids */
1487 input_dev->id.product = 0x02a1;
1488 }
1489
1490 input_dev->dev.parent = &xpad->intf->dev;
1491
1492 input_set_drvdata(input_dev, xpad);
1493
1494 if (xpad->xtype != XTYPE_XBOX360W) {
1495 input_dev->open = xpad_open;
1496 input_dev->close = xpad_close;
1497 }
1498
1499 __set_bit(EV_KEY, input_dev->evbit);
1500
1501 if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
1502 __set_bit(EV_ABS, input_dev->evbit);
1503 /* set up axes */
1504 for (i = 0; xpad_abs[i] >= 0; i++)
1505 xpad_set_up_abs(input_dev, xpad_abs[i]);
1506 }
1507
1508 /* set up standard buttons */
1509 for (i = 0; xpad_common_btn[i] >= 0; i++)
1510 __set_bit(xpad_common_btn[i], input_dev->keybit);
1511
1512 /* set up model-specific ones */
1513 if (xpad->xtype == XTYPE_XBOX360 || xpad->xtype == XTYPE_XBOX360W ||
1514 xpad->xtype == XTYPE_XBOXONE) {
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);
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) {
1535 for (i = 0; xpad_abs_pad[i] >= 0; i++)
1536 xpad_set_up_abs(input_dev, xpad_abs_pad[i]);
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 }
1546
1547 error = xpad_init_ff(xpad);
1548 if (error)
1549 goto err_free_input;
1550
1551 error = xpad_led_probe(xpad);
1552 if (error)
1553 goto err_destroy_ff;
1554
1555 error = input_register_device(xpad->dev);
1556 if (error)
1557 goto err_disconnect_led;
1558
1559 xpad->input_created = true;
1560 return 0;
1561
1562 err_disconnect_led:
1563 xpad_led_disconnect(xpad);
1564 err_destroy_ff:
1565 input_ff_destroy(input_dev);
1566 err_free_input:
1567 input_free_device(input_dev);
1568 return error;
1569 }
1570
1571 static 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;
1575 struct usb_endpoint_descriptor *ep_irq_in, *ep_irq_out;
1576 int i, error;
1577
1578 if (intf->cur_altsetting->desc.bNumEndpoints != 2)
1579 return -ENODEV;
1580
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
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;
1612 INIT_WORK(&xpad->work, xpad_presence_work);
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;
1618 else if (intf->cur_altsetting->desc.bInterfaceProtocol == 208)
1619 xpad->xtype = XTYPE_XBOXONE;
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
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
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;
1659 goto err_free_in_urb;
1660 }
1661
1662 error = xpad_init_output(intf, xpad, ep_irq_out);
1663 if (error)
1664 goto err_free_in_urb;
1665
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;
1672
1673 usb_set_intfdata(intf, xpad);
1674
1675 if (xpad->xtype == XTYPE_XBOX360W) {
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 */
1683 error = xpad360w_start_input(xpad);
1684 if (error)
1685 goto err_deinit_output;
1686 /*
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.
1692 */
1693 udev->quirks |= USB_QUIRK_RESET_RESUME;
1694 } else {
1695 error = xpad_init_input(xpad);
1696 if (error)
1697 goto err_deinit_output;
1698 }
1699 return 0;
1700
1701 err_deinit_output:
1702 xpad_deinit_output(xpad);
1703 err_free_in_urb:
1704 usb_free_urb(xpad->irq_in);
1705 err_free_idata:
1706 usb_free_coherent(udev, XPAD_PKT_LEN, xpad->idata, xpad->idata_dma);
1707 err_free_mem:
1708 kfree(xpad);
1709 return error;
1710 }
1711
1712 static void xpad_disconnect(struct usb_interface *intf)
1713 {
1714 struct usb_xpad *xpad = usb_get_intfdata(intf);
1715
1716 if (xpad->xtype == XTYPE_XBOX360W)
1717 xpad360w_stop_input(xpad);
1718
1719 xpad_deinit_input(xpad);
1720
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
1729 usb_free_urb(xpad->irq_in);
1730 usb_free_coherent(xpad->udev, XPAD_PKT_LEN,
1731 xpad->idata, xpad->idata_dma);
1732
1733 kfree(xpad);
1734
1735 usb_set_intfdata(intf, NULL);
1736 }
1737
1738 static 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);
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);
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
1771 static 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);
1781 if (input->users) {
1782 retval = xpad_start_input(xpad);
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 }
1791 mutex_unlock(&input->mutex);
1792 }
1793
1794 return retval;
1795 }
1796
1797 static struct usb_driver xpad_driver = {
1798 .name = "xpad",
1799 .probe = xpad_probe,
1800 .disconnect = xpad_disconnect,
1801 .suspend = xpad_suspend,
1802 .resume = xpad_resume,
1803 .reset_resume = xpad_resume,
1804 .id_table = xpad_table,
1805 };
1806
1807 module_usb_driver(xpad_driver);
1808
1809 MODULE_AUTHOR(DRIVER_AUTHOR);
1810 MODULE_DESCRIPTION(DRIVER_DESC);
1811 MODULE_LICENSE("GPL");