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