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