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