blob: ca62a6e11846f0ba14a9b493c5524b2adeb2d813 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Anssi Hannulabf8cb312008-04-03 16:19:10 -04002 * X-Box gamepad driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (c) 2002 Marko Friedemann <mfr@bmx-chemnitz.de>
Dominic Cerquettid518b2b2006-10-20 14:51:45 -07005 * 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>
Dominic Cerquettideb8ee42006-10-10 14:42:48 -07009 * 2005 Dominic Cerquetti <binary1230@yahoo.com>
10 * 2006 Adam Buchbinder <adam.buchbinder@gmail.com>
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -040011 * 2007 Jan Kratochvil <honza@jikos.cz>
Christoph Fritz7beae702010-07-13 09:42:33 -070012 * 2010 Christoph Fritz <chf.fritz@googlemail.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
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
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -040033 * - Xbox 360 information http://www.free60.org/wiki/Gamepad
Ming-ting Yao Wei06049492015-04-14 16:59:11 -070034 * - Xbox One information https://github.com/quantus/xbox-one-controller-protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 *
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
Dominic Cerquettid518b2b2006-10-20 14:51:45 -070040 * - XBOX Linux project - extra USB id's
Ming-ting Yao Wei06049492015-04-14 16:59:11 -070041 * - Pekka Pöyry (quantus) - Xbox One controller reverse engineering
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 *
43 * TODO:
Dominic Cerquettideb8ee42006-10-10 14:42:48 -070044 * - fine tune axes (especially trigger axes)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * - fix "analog" buttons (reported as digital now)
46 * - get rumble working
Dominic Cerquettideb8ee42006-10-10 14:42:48 -070047 * - need USB IDs for other dance pads
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 *
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
Dominic Cerquettid518b2b2006-10-20 14:51:45 -070067 *
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
Anssi Hannulabf8cb312008-04-03 16:19:10 -040074 *
75 * Later changes can be tracked in SCM.
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 */
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include <linux/kernel.h>
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -080079#include <linux/input.h>
80#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/slab.h>
Dominic Cerquettideb8ee42006-10-10 14:42:48 -070082#include <linux/stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include <linux/module.h>
David Brownellae0dadc2006-06-13 10:04:34 -070084#include <linux/usb/input.h>
Pavel Rojtberg4220f7d2015-12-09 13:30:34 -080085#include <linux/usb/quirks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#define DRIVER_AUTHOR "Marko Friedemann <mfr@bmx-chemnitz.de>"
88#define DRIVER_DESC "X-Box pad driver"
89
90#define XPAD_PKT_LEN 32
91
Dominic Cerquettideb8ee42006-10-10 14:42:48 -070092/* 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 */
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -080094#define MAP_DPAD_TO_BUTTONS (1 << 0)
95#define MAP_TRIGGERS_TO_BUTTONS (1 << 1)
Christoph Fritz7beae702010-07-13 09:42:33 -070096#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)
Dominic Cerquettideb8ee42006-10-10 14:42:48 -070099
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400100#define XTYPE_XBOX 0
101#define XTYPE_XBOX360 1
Brian Magnuson99de0912008-04-03 16:19:23 -0400102#define XTYPE_XBOX360W 2
Ted Mielczarek1a48ff82014-08-08 11:21:59 -0700103#define XTYPE_XBOXONE 3
104#define XTYPE_UNKNOWN 4
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400105
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030106static bool dpad_to_buttons;
Dominic Cerquettideb8ee42006-10-10 14:42:48 -0700107module_param(dpad_to_buttons, bool, S_IRUGO);
108MODULE_PARM_DESC(dpad_to_buttons, "Map D-PAD to buttons rather than axes for unknown pads");
109
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030110static bool triggers_to_buttons;
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800111module_param(triggers_to_buttons, bool, S_IRUGO);
112MODULE_PARM_DESC(triggers_to_buttons, "Map triggers to buttons rather than axes for unknown pads");
113
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030114static bool sticks_to_null;
Christoph Fritz7beae702010-07-13 09:42:33 -0700115module_param(sticks_to_null, bool, S_IRUGO);
116MODULE_PARM_DESC(sticks_to_null, "Do not map sticks at all for unknown pads");
117
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100118static const struct xpad_device {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 u16 idVendor;
120 u16 idProduct;
121 char *name;
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800122 u8 mapping;
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400123 u8 xtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124} xpad_device[] = {
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800125 { 0x045e, 0x0202, "Microsoft X-Box pad v1 (US)", 0, XTYPE_XBOX },
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800126 { 0x045e, 0x0285, "Microsoft X-Box pad (Japan)", 0, XTYPE_XBOX },
127 { 0x045e, 0x0287, "Microsoft Xbox Controller S", 0, XTYPE_XBOX },
Guillermo A. Amaral540602a2012-12-02 23:26:18 -0800128 { 0x045e, 0x0289, "Microsoft X-Box pad v2 (US)", 0, XTYPE_XBOX },
129 { 0x045e, 0x028e, "Microsoft X-Box 360 pad", 0, XTYPE_XBOX360 },
Ted Mielczarek1a48ff82014-08-08 11:21:59 -0700130 { 0x045e, 0x02d1, "Microsoft X-Box One pad", 0, XTYPE_XBOXONE },
Pavel Rojtberg95162dc2016-01-12 14:35:51 -0800131 { 0x045e, 0x02dd, "Microsoft X-Box One pad (Firmware 2015)", 0, XTYPE_XBOXONE },
Guillermo A. Amaral540602a2012-12-02 23:26:18 -0800132 { 0x045e, 0x0291, "Xbox 360 Wireless Receiver (XBOX)", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360W },
Brian Magnuson99de0912008-04-03 16:19:23 -0400133 { 0x045e, 0x0719, "Xbox 360 Wireless Receiver", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360W },
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800134 { 0x044f, 0x0f07, "Thrustmaster, Inc. Controller", 0, XTYPE_XBOX },
Benjamin Valentinf554f612014-09-08 14:18:40 -0700135 { 0x044f, 0xb326, "Thrustmaster Gamepad GP XID", 0, XTYPE_XBOX360 },
Petr Sebor8e2f2322014-01-02 15:35:07 -0800136 { 0x046d, 0xc21d, "Logitech Gamepad F310", 0, XTYPE_XBOX360 },
Benjamin Valentinf554f612014-09-08 14:18:40 -0700137 { 0x046d, 0xc21e, "Logitech Gamepad F510", 0, XTYPE_XBOX360 },
Petr Sebor8e2f2322014-01-02 15:35:07 -0800138 { 0x046d, 0xc21f, "Logitech Gamepad F710", 0, XTYPE_XBOX360 },
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800139 { 0x046d, 0xc242, "Logitech Chillstream Controller", 0, XTYPE_XBOX360 },
140 { 0x046d, 0xca84, "Logitech Xbox Cordless Controller", 0, XTYPE_XBOX },
141 { 0x046d, 0xca88, "Logitech Compact Controller for Xbox", 0, XTYPE_XBOX },
142 { 0x05fd, 0x1007, "Mad Catz Controller (unverified)", 0, XTYPE_XBOX },
143 { 0x05fd, 0x107a, "InterAct 'PowerPad Pro' X-Box pad (Germany)", 0, XTYPE_XBOX },
144 { 0x0738, 0x4516, "Mad Catz Control Pad", 0, XTYPE_XBOX },
145 { 0x0738, 0x4522, "Mad Catz LumiCON", 0, XTYPE_XBOX },
146 { 0x0738, 0x4526, "Mad Catz Control Pad Pro", 0, XTYPE_XBOX },
147 { 0x0738, 0x4536, "Mad Catz MicroCON", 0, XTYPE_XBOX },
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400148 { 0x0738, 0x4540, "Mad Catz Beat Pad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800149 { 0x0738, 0x4556, "Mad Catz Lynx Wireless Controller", 0, XTYPE_XBOX },
150 { 0x0738, 0x4716, "Mad Catz Wired Xbox 360 Controller", 0, XTYPE_XBOX360 },
Benjamin Valentinf554f612014-09-08 14:18:40 -0700151 { 0x0738, 0x4718, "Mad Catz Street Fighter IV FightStick SE", 0, XTYPE_XBOX360 },
152 { 0x0738, 0x4726, "Mad Catz Xbox 360 Controller", 0, XTYPE_XBOX360 },
Shawn Josephbe662272013-06-18 23:07:45 -0700153 { 0x0738, 0x4728, "Mad Catz Street Fighter IV FightPad", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800154 { 0x0738, 0x4738, "Mad Catz Wired Xbox 360 Controller (SFIV)", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
Benjamin Valentinf554f612014-09-08 14:18:40 -0700155 { 0x0738, 0x4740, "Mad Catz Beat Pad", 0, XTYPE_XBOX360 },
Silvan Jegend63b0f02016-03-17 17:15:01 -0700156 { 0x0738, 0x4a01, "Mad Catz FightStick TE 2", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOXONE },
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400157 { 0x0738, 0x6040, "Mad Catz Beat Pad Pro", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
Benjamin Valentinf554f612014-09-08 14:18:40 -0700158 { 0x0738, 0xb726, "Mad Catz Xbox controller - MW2", 0, XTYPE_XBOX360 },
Guillermo A. Amaral540602a2012-12-02 23:26:18 -0800159 { 0x0738, 0xbeef, "Mad Catz JOYTECH NEO SE Advanced GamePad", XTYPE_XBOX360 },
Benjamin Valentinf554f612014-09-08 14:18:40 -0700160 { 0x0738, 0xcb02, "Saitek Cyborg Rumble Pad - PC/Xbox 360", 0, XTYPE_XBOX360 },
161 { 0x0738, 0xcb03, "Saitek P3200 Rumble Pad - PC/Xbox 360", 0, XTYPE_XBOX360 },
162 { 0x0738, 0xf738, "Super SFIV FightStick TE S", 0, XTYPE_XBOX360 },
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800163 { 0x0c12, 0x8802, "Zeroplus Xbox Controller", 0, XTYPE_XBOX },
Guillermo A. Amaral540602a2012-12-02 23:26:18 -0800164 { 0x0c12, 0x8809, "RedOctane Xbox Dance Pad", DANCEPAD_MAP_CONFIG, XTYPE_XBOX },
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800165 { 0x0c12, 0x880a, "Pelican Eclipse PL-2023", 0, XTYPE_XBOX },
166 { 0x0c12, 0x8810, "Zeroplus Xbox Controller", 0, XTYPE_XBOX },
167 { 0x0c12, 0x9902, "HAMA VibraX - *FAULTY HARDWARE*", 0, XTYPE_XBOX },
Yuri Khane76b8ee2012-07-11 22:12:31 -0700168 { 0x0d2f, 0x0002, "Andamiro Pump It Up pad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800169 { 0x0e4c, 0x1097, "Radica Gamester Controller", 0, XTYPE_XBOX },
170 { 0x0e4c, 0x2390, "Radica Games Jtech Controller", 0, XTYPE_XBOX },
171 { 0x0e6f, 0x0003, "Logic3 Freebird wireless Controller", 0, XTYPE_XBOX },
172 { 0x0e6f, 0x0005, "Eclipse wireless Controller", 0, XTYPE_XBOX },
173 { 0x0e6f, 0x0006, "Edge wireless Controller", 0, XTYPE_XBOX },
Guillermo A. Amaral540602a2012-12-02 23:26:18 -0800174 { 0x0e6f, 0x0105, "HSM3 Xbox360 dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
Benjamin Valentinf554f612014-09-08 14:18:40 -0700175 { 0x0e6f, 0x0113, "Afterglow AX.1 Gamepad for Xbox 360", 0, XTYPE_XBOX360 },
Christoph Fritz6ac8a992010-08-09 10:08:10 -0700176 { 0x0e6f, 0x0201, "Pelican PL-3601 'TSZ' Wired Xbox 360 Controller", 0, XTYPE_XBOX360 },
Guillermo A. Amaral540602a2012-12-02 23:26:18 -0800177 { 0x0e6f, 0x0213, "Afterglow Gamepad for Xbox 360", 0, XTYPE_XBOX360 },
Benjamin Valentinf554f612014-09-08 14:18:40 -0700178 { 0x0e6f, 0x021f, "Rock Candy Gamepad for Xbox 360", 0, XTYPE_XBOX360 },
179 { 0x0e6f, 0x0301, "Logic3 Controller", 0, XTYPE_XBOX360 },
180 { 0x0e6f, 0x0401, "Logic3 Controller", 0, XTYPE_XBOX360 },
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800181 { 0x0e8f, 0x0201, "SmartJoy Frag Xpad/PS2 adaptor", 0, XTYPE_XBOX },
Benjamin Valentinf554f612014-09-08 14:18:40 -0700182 { 0x0e8f, 0x3008, "Generic xbox control (dealextreme)", 0, XTYPE_XBOX },
183 { 0x0f0d, 0x000a, "Hori Co. DOA4 FightStick", 0, XTYPE_XBOX360 },
Guillermo A. Amaral540602a2012-12-02 23:26:18 -0800184 { 0x0f0d, 0x000d, "Hori Fighting Stick EX2", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
185 { 0x0f0d, 0x0016, "Hori Real Arcade Pro.EX", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800186 { 0x0f30, 0x0202, "Joytech Advanced Controller", 0, XTYPE_XBOX },
187 { 0x0f30, 0x8888, "BigBen XBMiniPad Controller", 0, XTYPE_XBOX },
188 { 0x102c, 0xff0c, "Joytech Wireless Advanced Controller", 0, XTYPE_XBOX },
Magnus Hörlinfabadbc2011-06-21 14:40:30 -0700189 { 0x12ab, 0x0004, "Honey Bee Xbox360 dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
Benjamin Valentinf554f612014-09-08 14:18:40 -0700190 { 0x12ab, 0x0301, "PDP AFTERGLOW AX.1", 0, XTYPE_XBOX360 },
Guillermo A. Amaral540602a2012-12-02 23:26:18 -0800191 { 0x12ab, 0x8809, "Xbox DDR dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800192 { 0x1430, 0x4748, "RedOctane Guitar Hero X-plorer", 0, XTYPE_XBOX360 },
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400193 { 0x1430, 0x8888, "TX6500+ Dance Pad (first generation)", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800194 { 0x146b, 0x0601, "BigBen Interactive XBOX 360 Controller", 0, XTYPE_XBOX360 },
Benjamin Valentinf554f612014-09-08 14:18:40 -0700195 { 0x1532, 0x0037, "Razer Sabertooth", 0, XTYPE_XBOX360 },
196 { 0x15e4, 0x3f00, "Power A Mini Pro Elite", 0, XTYPE_XBOX360 },
197 { 0x15e4, 0x3f0a, "Xbox Airflo wired controller", 0, XTYPE_XBOX360 },
198 { 0x15e4, 0x3f10, "Batarang Xbox 360 controller", 0, XTYPE_XBOX360 },
199 { 0x162e, 0xbeef, "Joytech Neo-Se Take2", 0, XTYPE_XBOX360 },
Thomaz de Oliveira dos Reisdbb48002014-01-02 15:38:45 -0800200 { 0x1689, 0xfd00, "Razer Onza Tournament Edition", 0, XTYPE_XBOX360 },
201 { 0x1689, 0xfd01, "Razer Onza Classic Edition", 0, XTYPE_XBOX360 },
Frank Razenberga7b44732014-09-08 11:32:20 -0700202 { 0x24c6, 0x5d04, "Razer Sabertooth", 0, XTYPE_XBOX360 },
Magnus Hörlinfabadbc2011-06-21 14:40:30 -0700203 { 0x1bad, 0x0002, "Harmonix Rock Band Guitar", 0, XTYPE_XBOX360 },
Corbin Simpson805423e2009-08-20 21:41:04 -0700204 { 0x1bad, 0x0003, "Harmonix Rock Band Drumkit", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
Guillermo A. Amaral540602a2012-12-02 23:26:18 -0800205 { 0x1bad, 0xf016, "Mad Catz Xbox 360 Controller", 0, XTYPE_XBOX360 },
Benjamin Valentinf554f612014-09-08 14:18:40 -0700206 { 0x1bad, 0xf023, "MLG Pro Circuit Controller (Xbox)", 0, XTYPE_XBOX360 },
Guillermo A. Amaral540602a2012-12-02 23:26:18 -0800207 { 0x1bad, 0xf028, "Street Fighter IV FightPad", 0, XTYPE_XBOX360 },
Benjamin Valentinf554f612014-09-08 14:18:40 -0700208 { 0x1bad, 0xf038, "Street Fighter IV FightStick TE", 0, XTYPE_XBOX360 },
209 { 0x1bad, 0xf900, "Harmonix Xbox 360 Controller", 0, XTYPE_XBOX360 },
Guillermo A. Amaral540602a2012-12-02 23:26:18 -0800210 { 0x1bad, 0xf901, "Gamestop Xbox 360 Controller", 0, XTYPE_XBOX360 },
211 { 0x1bad, 0xf903, "Tron Xbox 360 controller", 0, XTYPE_XBOX360 },
Dario Scarpa470446e2015-10-06 17:04:36 -0700212 { 0x24c6, 0x5000, "Razer Atrox Arcade Stick", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
Guillermo A. Amaral540602a2012-12-02 23:26:18 -0800213 { 0x24c6, 0x5300, "PowerA MINI PROEX Controller", 0, XTYPE_XBOX360 },
Benjamin Valentinf554f612014-09-08 14:18:40 -0700214 { 0x24c6, 0x5303, "Xbox Airflo wired controller", 0, XTYPE_XBOX360 },
215 { 0x24c6, 0x5500, "Hori XBOX 360 EX 2 with Turbo", 0, XTYPE_XBOX360 },
216 { 0x24c6, 0x5501, "Hori Real Arcade Pro VX-SA", 0, XTYPE_XBOX360 },
217 { 0x24c6, 0x5506, "Hori SOULCALIBUR V Stick", 0, XTYPE_XBOX360 },
218 { 0x24c6, 0x5b02, "Thrustmaster, Inc. GPX Controller", 0, XTYPE_XBOX360 },
Tommi Rantala4b546252014-10-16 14:01:43 -0700219 { 0x24c6, 0x5b03, "Thrustmaster Ferrari 458 Racing Wheel", 0, XTYPE_XBOX360 },
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800220 { 0xffff, 0xffff, "Chinese-made Xbox Controller", 0, XTYPE_XBOX },
221 { 0x0000, 0x0000, "Generic X-Box pad", 0, XTYPE_UNKNOWN }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222};
223
Anssi Hannulafc55e952008-04-03 16:18:44 -0400224/* buttons shared with xbox and xbox360 */
225static const signed short xpad_common_btn[] = {
226 BTN_A, BTN_B, BTN_X, BTN_Y, /* "analog" buttons */
Christoph Fritz7beae702010-07-13 09:42:33 -0700227 BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR, /* start/back/sticks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 -1 /* terminating entry */
229};
230
Anssi Hannulafc55e952008-04-03 16:18:44 -0400231/* original xbox controllers only */
232static const signed short xpad_btn[] = {
233 BTN_C, BTN_Z, /* "analog" buttons */
234 -1 /* terminating entry */
235};
236
Christoph Fritz7beae702010-07-13 09:42:33 -0700237/* used when dpad is mapped to buttons */
Dominic Cerquettideb8ee42006-10-10 14:42:48 -0700238static const signed short xpad_btn_pad[] = {
Christoph Fritz7beae702010-07-13 09:42:33 -0700239 BTN_TRIGGER_HAPPY1, BTN_TRIGGER_HAPPY2, /* d-pad left, right */
240 BTN_TRIGGER_HAPPY3, BTN_TRIGGER_HAPPY4, /* d-pad up, down */
Dominic Cerquettideb8ee42006-10-10 14:42:48 -0700241 -1 /* terminating entry */
242};
243
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800244/* used when triggers are mapped to buttons */
245static const signed short xpad_btn_triggers[] = {
246 BTN_TL2, BTN_TR2, /* triggers left/right */
247 -1
248};
249
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400250static const signed short xpad360_btn[] = { /* buttons for x360 controller */
251 BTN_TL, BTN_TR, /* Button LB/RB */
252 BTN_MODE, /* The big X button */
253 -1
254};
255
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100256static const signed short xpad_abs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 ABS_X, ABS_Y, /* left stick */
258 ABS_RX, ABS_RY, /* right stick */
Dominic Cerquettideb8ee42006-10-10 14:42:48 -0700259 -1 /* terminating entry */
260};
261
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800262/* used when dpad is mapped to axes */
Dominic Cerquettideb8ee42006-10-10 14:42:48 -0700263static const signed short xpad_abs_pad[] = {
264 ABS_HAT0X, ABS_HAT0Y, /* d-pad axes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 -1 /* terminating entry */
266};
267
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800268/* used when triggers are mapped to axes */
269static const signed short xpad_abs_triggers[] = {
270 ABS_Z, ABS_RZ, /* triggers left/right */
271 -1
272};
273
Ted Mielczarek1a48ff82014-08-08 11:21:59 -0700274/*
275 * Xbox 360 has a vendor-specific class, so we cannot match it with only
Anssi Hannula8a0f83e2008-04-03 16:17:52 -0400276 * USB_INTERFACE_INFO (also specifically refused by USB subsystem), so we
Brian Magnuson99de0912008-04-03 16:19:23 -0400277 * match against vendor id as well. Wired Xbox 360 devices have protocol 1,
Ted Mielczarek1a48ff82014-08-08 11:21:59 -0700278 * wireless controllers have protocol 129.
279 */
Brian Magnuson99de0912008-04-03 16:19:23 -0400280#define XPAD_XBOX360_VENDOR_PROTOCOL(vend,pr) \
Anssi Hannula8a0f83e2008-04-03 16:17:52 -0400281 .match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_INT_INFO, \
282 .idVendor = (vend), \
283 .bInterfaceClass = USB_CLASS_VENDOR_SPEC, \
284 .bInterfaceSubClass = 93, \
Brian Magnuson99de0912008-04-03 16:19:23 -0400285 .bInterfaceProtocol = (pr)
286#define XPAD_XBOX360_VENDOR(vend) \
287 { XPAD_XBOX360_VENDOR_PROTOCOL(vend,1) }, \
288 { XPAD_XBOX360_VENDOR_PROTOCOL(vend,129) }
Anssi Hannula8a0f83e2008-04-03 16:17:52 -0400289
Ted Mielczarek1a48ff82014-08-08 11:21:59 -0700290/* The Xbox One controller uses subclass 71 and protocol 208. */
291#define XPAD_XBOXONE_VENDOR_PROTOCOL(vend, pr) \
292 .match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_INT_INFO, \
293 .idVendor = (vend), \
294 .bInterfaceClass = USB_CLASS_VENDOR_SPEC, \
295 .bInterfaceSubClass = 71, \
296 .bInterfaceProtocol = (pr)
297#define XPAD_XBOXONE_VENDOR(vend) \
298 { XPAD_XBOXONE_VENDOR_PROTOCOL(vend, 208) }
299
Guillermo A. Amaral0d027962012-12-02 23:26:11 -0800300static struct usb_device_id xpad_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 { USB_INTERFACE_INFO('X', 'B', 0) }, /* X-Box USB-IF not approved class */
Tommi Rantala4dfb15c2014-10-16 14:02:07 -0700302 XPAD_XBOX360_VENDOR(0x044f), /* Thrustmaster X-Box 360 controllers */
Brian Magnuson99de0912008-04-03 16:19:23 -0400303 XPAD_XBOX360_VENDOR(0x045e), /* Microsoft X-Box 360 controllers */
Ted Mielczarek1a48ff82014-08-08 11:21:59 -0700304 XPAD_XBOXONE_VENDOR(0x045e), /* Microsoft X-Box One controllers */
Brian Magnuson99de0912008-04-03 16:19:23 -0400305 XPAD_XBOX360_VENDOR(0x046d), /* Logitech X-Box 360 style controllers */
306 XPAD_XBOX360_VENDOR(0x0738), /* Mad Catz X-Box 360 controllers */
Yuri Khan3ffb62c2012-07-11 00:49:18 -0700307 { USB_DEVICE(0x0738, 0x4540) }, /* Mad Catz Beat Pad */
Silvan Jegend63b0f02016-03-17 17:15:01 -0700308 XPAD_XBOXONE_VENDOR(0x0738), /* Mad Catz FightStick TE 2 */
Brian Magnuson99de0912008-04-03 16:19:23 -0400309 XPAD_XBOX360_VENDOR(0x0e6f), /* 0x0e6f X-Box 360 controllers */
Magnus Hörlinfabadbc2011-06-21 14:40:30 -0700310 XPAD_XBOX360_VENDOR(0x12ab), /* X-Box 360 dance pads */
Brian Magnuson99de0912008-04-03 16:19:23 -0400311 XPAD_XBOX360_VENDOR(0x1430), /* RedOctane X-Box 360 controllers */
Thomas Gruber3ac91d32009-10-05 21:43:42 -0700312 XPAD_XBOX360_VENDOR(0x146b), /* BigBen Interactive Controllers */
Magnus Hörlinfabadbc2011-06-21 14:40:30 -0700313 XPAD_XBOX360_VENDOR(0x1bad), /* Harminix Rock Band Guitar and Drums */
Ilia Katsnelsoncc71a7e2012-07-11 00:54:20 -0700314 XPAD_XBOX360_VENDOR(0x0f0d), /* Hori Controllers */
315 XPAD_XBOX360_VENDOR(0x1689), /* Razer Onza */
Guillermo A. Amaral540602a2012-12-02 23:26:18 -0800316 XPAD_XBOX360_VENDOR(0x24c6), /* PowerA Controllers */
Benjamin Valentinf554f612014-09-08 14:18:40 -0700317 XPAD_XBOX360_VENDOR(0x1532), /* Razer Sabertooth */
318 XPAD_XBOX360_VENDOR(0x15e4), /* Numark X-Box 360 controllers */
319 XPAD_XBOX360_VENDOR(0x162e), /* Joytech X-Box 360 controllers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 { }
321};
322
Guillermo A. Amaral0d027962012-12-02 23:26:11 -0800323MODULE_DEVICE_TABLE(usb, xpad_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800325struct xpad_output_packet {
326 u8 data[XPAD_PKT_LEN];
327 u8 len;
328 bool pending;
329};
330
331#define XPAD_OUT_CMD_IDX 0
332#define XPAD_OUT_FF_IDX 1
333#define XPAD_OUT_LED_IDX (1 + IS_ENABLED(CONFIG_JOYSTICK_XPAD_FF))
334#define XPAD_NUM_OUT_PACKETS (1 + \
335 IS_ENABLED(CONFIG_JOYSTICK_XPAD_FF) + \
336 IS_ENABLED(CONFIG_JOYSTICK_XPAD_LEDS))
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338struct usb_xpad {
Dominic Cerquettideb8ee42006-10-10 14:42:48 -0700339 struct input_dev *dev; /* input device interface */
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -0800340 struct input_dev __rcu *x360w_dev;
Dominic Cerquettideb8ee42006-10-10 14:42:48 -0700341 struct usb_device *udev; /* usb device */
Greg Kroah-Hartman8818e412012-05-04 15:32:53 -0700342 struct usb_interface *intf; /* usb interface */
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500343
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -0800344 bool pad_present;
345 bool input_created;
Brian Magnuson99de0912008-04-03 16:19:23 -0400346
Dominic Cerquettideb8ee42006-10-10 14:42:48 -0700347 struct urb *irq_in; /* urb for interrupt in report */
348 unsigned char *idata; /* input data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 dma_addr_t idata_dma;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500350
Jan Kratochvile01a06e2007-05-09 00:27:51 -0400351 struct urb *irq_out; /* urb for interrupt out report */
Pavel Rojtberg4220f7d2015-12-09 13:30:34 -0800352 struct usb_anchor irq_out_anchor;
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800353 bool irq_out_active; /* we must not use an active URB */
Pierre-Loup A. Griffais2a6d7522015-12-09 13:40:37 -0800354 u8 odata_serial; /* serial number for xbox one protocol */
Pavel Rojtberg4220f7d2015-12-09 13:30:34 -0800355 unsigned char *odata; /* output data */
Jan Kratochvile01a06e2007-05-09 00:27:51 -0400356 dma_addr_t odata_dma;
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800357 spinlock_t odata_lock;
358
359 struct xpad_output_packet out_packets[XPAD_NUM_OUT_PACKETS];
360 int last_out_packet;
Jan Kratochvile01a06e2007-05-09 00:27:51 -0400361
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400362#if defined(CONFIG_JOYSTICK_XPAD_LEDS)
363 struct xpad_led *led;
364#endif
365
366 char phys[64]; /* physical device path */
Dominic Cerquettideb8ee42006-10-10 14:42:48 -0700367
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800368 int mapping; /* map d-pad to buttons or to axes */
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400369 int xtype; /* type of xbox device */
Pavel Rojtberge3b65172015-10-10 10:00:49 -0700370 int pad_nr; /* the order x360 pads were attached */
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -0700371 const char *name; /* name of the device */
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -0800372 struct work_struct work; /* init/remove device from callback */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373};
374
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -0800375static int xpad_init_input(struct usb_xpad *xpad);
376static void xpad_deinit_input(struct usb_xpad *xpad);
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378/*
379 * xpad_process_packet
380 *
381 * Completes a request by converting the data into events for the
382 * input subsystem.
383 *
384 * The used report descriptor was taken from ITO Takayukis website:
385 * http://euc.jp/periphs/xbox-controller.ja.html
386 */
David Howells7d12e782006-10-05 14:55:46 +0100387static void xpad_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500389 struct input_dev *dev = xpad->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Christoph Fritz7beae702010-07-13 09:42:33 -0700391 if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
392 /* left stick */
393 input_report_abs(dev, ABS_X,
394 (__s16) le16_to_cpup((__le16 *)(data + 12)));
395 input_report_abs(dev, ABS_Y,
396 ~(__s16) le16_to_cpup((__le16 *)(data + 14)));
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500397
Christoph Fritz7beae702010-07-13 09:42:33 -0700398 /* right stick */
399 input_report_abs(dev, ABS_RX,
400 (__s16) le16_to_cpup((__le16 *)(data + 16)));
401 input_report_abs(dev, ABS_RY,
402 ~(__s16) le16_to_cpup((__le16 *)(data + 18)));
403 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 /* triggers left/right */
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800406 if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
407 input_report_key(dev, BTN_TL2, data[10]);
408 input_report_key(dev, BTN_TR2, data[11]);
409 } else {
410 input_report_abs(dev, ABS_Z, data[10]);
411 input_report_abs(dev, ABS_RZ, data[11]);
412 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 /* digital pad */
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800415 if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
Christoph Fritz7beae702010-07-13 09:42:33 -0700416 /* dpad as buttons (left, right, up, down) */
417 input_report_key(dev, BTN_TRIGGER_HAPPY1, data[2] & 0x04);
418 input_report_key(dev, BTN_TRIGGER_HAPPY2, data[2] & 0x08);
419 input_report_key(dev, BTN_TRIGGER_HAPPY3, data[2] & 0x01);
420 input_report_key(dev, BTN_TRIGGER_HAPPY4, data[2] & 0x02);
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800421 } else {
422 input_report_abs(dev, ABS_HAT0X,
423 !!(data[2] & 0x08) - !!(data[2] & 0x04));
424 input_report_abs(dev, ABS_HAT0Y,
425 !!(data[2] & 0x02) - !!(data[2] & 0x01));
Dominic Cerquettideb8ee42006-10-10 14:42:48 -0700426 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /* start/back buttons and stick press left/right */
Dominic Cerquettideb8ee42006-10-10 14:42:48 -0700429 input_report_key(dev, BTN_START, data[2] & 0x10);
Christoph Fritz7beae702010-07-13 09:42:33 -0700430 input_report_key(dev, BTN_SELECT, data[2] & 0x20);
Dominic Cerquettideb8ee42006-10-10 14:42:48 -0700431 input_report_key(dev, BTN_THUMBL, data[2] & 0x40);
432 input_report_key(dev, BTN_THUMBR, data[2] & 0x80);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 /* "analog" buttons A, B, X, Y */
435 input_report_key(dev, BTN_A, data[4]);
436 input_report_key(dev, BTN_B, data[5]);
437 input_report_key(dev, BTN_X, data[6]);
438 input_report_key(dev, BTN_Y, data[7]);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 /* "analog" buttons black, white */
441 input_report_key(dev, BTN_C, data[8]);
442 input_report_key(dev, BTN_Z, data[9]);
443
444 input_sync(dev);
445}
446
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400447/*
448 * xpad360_process_packet
449 *
450 * Completes a request by converting the data into events for the
451 * input subsystem. It is version for xbox 360 controller
452 *
453 * The used report descriptor was taken from:
454 * http://www.free60.org/wiki/Gamepad
455 */
456
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -0800457static void xpad360_process_packet(struct usb_xpad *xpad, struct input_dev *dev,
Dmitry Torokhov20b3cdd2007-07-18 01:20:34 -0400458 u16 cmd, unsigned char *data)
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400459{
Cameron Gutman1ff5fa32016-05-27 16:23:50 -0700460 /* valid pad data */
461 if (data[0] != 0x00)
462 return;
463
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400464 /* digital pad */
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800465 if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
Christoph Fritz7beae702010-07-13 09:42:33 -0700466 /* dpad as buttons (left, right, up, down) */
467 input_report_key(dev, BTN_TRIGGER_HAPPY1, data[2] & 0x04);
468 input_report_key(dev, BTN_TRIGGER_HAPPY2, data[2] & 0x08);
469 input_report_key(dev, BTN_TRIGGER_HAPPY3, data[2] & 0x01);
470 input_report_key(dev, BTN_TRIGGER_HAPPY4, data[2] & 0x02);
Pavel Rojtberg5ee8bda2015-10-10 09:33:52 -0700471 }
472
473 /*
474 * This should be a simple else block. However historically
475 * xbox360w has mapped DPAD to buttons while xbox360 did not. This
476 * made no sense, but now we can not just switch back and have to
477 * support both behaviors.
478 */
479 if (!(xpad->mapping & MAP_DPAD_TO_BUTTONS) ||
480 xpad->xtype == XTYPE_XBOX360W) {
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800481 input_report_abs(dev, ABS_HAT0X,
482 !!(data[2] & 0x08) - !!(data[2] & 0x04));
483 input_report_abs(dev, ABS_HAT0Y,
484 !!(data[2] & 0x02) - !!(data[2] & 0x01));
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400485 }
486
487 /* start/back buttons */
Dmitry Torokhovae91d102007-06-14 23:49:55 -0400488 input_report_key(dev, BTN_START, data[2] & 0x10);
Christoph Fritz7beae702010-07-13 09:42:33 -0700489 input_report_key(dev, BTN_SELECT, data[2] & 0x20);
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400490
491 /* stick press left/right */
492 input_report_key(dev, BTN_THUMBL, data[2] & 0x40);
493 input_report_key(dev, BTN_THUMBR, data[2] & 0x80);
494
495 /* buttons A,B,X,Y,TL,TR and MODE */
Dmitry Torokhovae91d102007-06-14 23:49:55 -0400496 input_report_key(dev, BTN_A, data[3] & 0x10);
497 input_report_key(dev, BTN_B, data[3] & 0x20);
498 input_report_key(dev, BTN_X, data[3] & 0x40);
499 input_report_key(dev, BTN_Y, data[3] & 0x80);
500 input_report_key(dev, BTN_TL, data[3] & 0x01);
501 input_report_key(dev, BTN_TR, data[3] & 0x02);
502 input_report_key(dev, BTN_MODE, data[3] & 0x04);
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400503
Christoph Fritz7beae702010-07-13 09:42:33 -0700504 if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
505 /* left stick */
506 input_report_abs(dev, ABS_X,
507 (__s16) le16_to_cpup((__le16 *)(data + 6)));
508 input_report_abs(dev, ABS_Y,
509 ~(__s16) le16_to_cpup((__le16 *)(data + 8)));
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400510
Christoph Fritz7beae702010-07-13 09:42:33 -0700511 /* right stick */
512 input_report_abs(dev, ABS_RX,
513 (__s16) le16_to_cpup((__le16 *)(data + 10)));
514 input_report_abs(dev, ABS_RY,
515 ~(__s16) le16_to_cpup((__le16 *)(data + 12)));
516 }
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400517
518 /* triggers left/right */
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800519 if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
520 input_report_key(dev, BTN_TL2, data[4]);
521 input_report_key(dev, BTN_TR2, data[5]);
522 } else {
523 input_report_abs(dev, ABS_Z, data[4]);
524 input_report_abs(dev, ABS_RZ, data[5]);
525 }
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400526
527 input_sync(dev);
528}
529
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -0800530static void xpad_presence_work(struct work_struct *work)
531{
532 struct usb_xpad *xpad = container_of(work, struct usb_xpad, work);
533 int error;
534
535 if (xpad->pad_present) {
536 error = xpad_init_input(xpad);
537 if (error) {
538 /* complain only, not much else we can do here */
539 dev_err(&xpad->dev->dev,
540 "unable to init device: %d\n", error);
541 } else {
542 rcu_assign_pointer(xpad->x360w_dev, xpad->dev);
543 }
544 } else {
545 RCU_INIT_POINTER(xpad->x360w_dev, NULL);
546 synchronize_rcu();
547 /*
548 * Now that we are sure xpad360w_process_packet is not
549 * using input device we can get rid of it.
550 */
551 xpad_deinit_input(xpad);
552 }
553}
Pavel Rojtbergcae705b2015-06-22 14:11:30 -0700554
Brian Magnuson99de0912008-04-03 16:19:23 -0400555/*
556 * xpad360w_process_packet
557 *
558 * Completes a request by converting the data into events for the
559 * input subsystem. It is version for xbox 360 wireless controller.
560 *
561 * Byte.Bit
562 * 00.1 - Status change: The controller or headset has connected/disconnected
563 * Bits 01.7 and 01.6 are valid
564 * 01.7 - Controller present
565 * 01.6 - Headset present
566 * 01.1 - Pad state (Bytes 4+) valid
567 *
568 */
Brian Magnuson99de0912008-04-03 16:19:23 -0400569static void xpad360w_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data)
570{
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -0800571 struct input_dev *dev;
572 bool present;
573
Brian Magnuson99de0912008-04-03 16:19:23 -0400574 /* Presence change */
575 if (data[0] & 0x08) {
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -0800576 present = (data[1] & 0x80) != 0;
577
578 if (xpad->pad_present != present) {
579 xpad->pad_present = present;
580 schedule_work(&xpad->work);
581 }
Brian Magnuson99de0912008-04-03 16:19:23 -0400582 }
583
584 /* Valid pad data */
Clement Calmels93a017aa22015-12-12 21:20:11 -0800585 if (data[1] != 0x1)
Brian Magnuson99de0912008-04-03 16:19:23 -0400586 return;
587
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -0800588 rcu_read_lock();
589 dev = rcu_dereference(xpad->x360w_dev);
590 if (dev)
591 xpad360_process_packet(xpad, dev, cmd, &data[4]);
592 rcu_read_unlock();
Brian Magnuson99de0912008-04-03 16:19:23 -0400593}
594
Ted Mielczarek1a48ff82014-08-08 11:21:59 -0700595/*
596 * xpadone_process_buttons
597 *
598 * Process a button update packet from an Xbox one controller.
599 */
600static void xpadone_process_buttons(struct usb_xpad *xpad,
601 struct input_dev *dev,
602 unsigned char *data)
603{
604 /* menu/view buttons */
605 input_report_key(dev, BTN_START, data[4] & 0x04);
606 input_report_key(dev, BTN_SELECT, data[4] & 0x08);
607
608 /* buttons A,B,X,Y */
609 input_report_key(dev, BTN_A, data[4] & 0x10);
610 input_report_key(dev, BTN_B, data[4] & 0x20);
611 input_report_key(dev, BTN_X, data[4] & 0x40);
612 input_report_key(dev, BTN_Y, data[4] & 0x80);
613
614 /* digital pad */
615 if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
616 /* dpad as buttons (left, right, up, down) */
617 input_report_key(dev, BTN_TRIGGER_HAPPY1, data[5] & 0x04);
618 input_report_key(dev, BTN_TRIGGER_HAPPY2, data[5] & 0x08);
619 input_report_key(dev, BTN_TRIGGER_HAPPY3, data[5] & 0x01);
620 input_report_key(dev, BTN_TRIGGER_HAPPY4, data[5] & 0x02);
621 } else {
622 input_report_abs(dev, ABS_HAT0X,
623 !!(data[5] & 0x08) - !!(data[5] & 0x04));
624 input_report_abs(dev, ABS_HAT0Y,
625 !!(data[5] & 0x02) - !!(data[5] & 0x01));
626 }
627
628 /* TL/TR */
629 input_report_key(dev, BTN_TL, data[5] & 0x10);
630 input_report_key(dev, BTN_TR, data[5] & 0x20);
631
632 /* stick press left/right */
633 input_report_key(dev, BTN_THUMBL, data[5] & 0x40);
634 input_report_key(dev, BTN_THUMBR, data[5] & 0x80);
635
636 if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
637 /* left stick */
638 input_report_abs(dev, ABS_X,
639 (__s16) le16_to_cpup((__le16 *)(data + 10)));
640 input_report_abs(dev, ABS_Y,
641 ~(__s16) le16_to_cpup((__le16 *)(data + 12)));
642
643 /* right stick */
644 input_report_abs(dev, ABS_RX,
645 (__s16) le16_to_cpup((__le16 *)(data + 14)));
646 input_report_abs(dev, ABS_RY,
647 ~(__s16) le16_to_cpup((__le16 *)(data + 16)));
648 }
649
650 /* triggers left/right */
651 if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
652 input_report_key(dev, BTN_TL2,
653 (__u16) le16_to_cpup((__le16 *)(data + 6)));
654 input_report_key(dev, BTN_TR2,
655 (__u16) le16_to_cpup((__le16 *)(data + 8)));
656 } else {
657 input_report_abs(dev, ABS_Z,
658 (__u16) le16_to_cpup((__le16 *)(data + 6)));
659 input_report_abs(dev, ABS_RZ,
660 (__u16) le16_to_cpup((__le16 *)(data + 8)));
661 }
662
663 input_sync(dev);
664}
665
666/*
667 * xpadone_process_packet
668 *
669 * Completes a request by converting the data into events for the
670 * input subsystem. This version is for the Xbox One controller.
671 *
672 * The report format was gleaned from
673 * https://github.com/kylelemons/xbox/blob/master/xbox.go
674 */
675
676static void xpadone_process_packet(struct usb_xpad *xpad,
677 u16 cmd, unsigned char *data)
678{
679 struct input_dev *dev = xpad->dev;
680
681 switch (data[0]) {
682 case 0x20:
683 xpadone_process_buttons(xpad, dev, data);
684 break;
685
686 case 0x07:
687 /* the xbox button has its own special report */
688 input_report_key(dev, BTN_MODE, data[4] & 0x01);
689 input_sync(dev);
690 break;
691 }
692}
693
David Howells7d12e782006-10-05 14:55:46 +0100694static void xpad_irq_in(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
696 struct usb_xpad *xpad = urb->context;
Greg Kroah-Hartman8818e412012-05-04 15:32:53 -0700697 struct device *dev = &xpad->intf->dev;
Oliver Neukum6fc88f532008-04-03 21:40:59 +0200698 int retval, status;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500699
Oliver Neukum6fc88f532008-04-03 21:40:59 +0200700 status = urb->status;
701
702 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 case 0:
704 /* success */
705 break;
706 case -ECONNRESET:
707 case -ENOENT:
708 case -ESHUTDOWN:
709 /* this urb is terminated, clean up */
Greg Kroah-Hartmanc4f0bbc2012-05-01 21:32:59 -0700710 dev_dbg(dev, "%s - urb shutting down with status: %d\n",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400711 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return;
713 default:
Greg Kroah-Hartmanc4f0bbc2012-05-01 21:32:59 -0700714 dev_dbg(dev, "%s - nonzero urb status received: %d\n",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400715 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 goto exit;
717 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500718
Brian Magnuson99de0912008-04-03 16:19:23 -0400719 switch (xpad->xtype) {
720 case XTYPE_XBOX360:
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -0800721 xpad360_process_packet(xpad, xpad->dev, 0, xpad->idata);
Brian Magnuson99de0912008-04-03 16:19:23 -0400722 break;
723 case XTYPE_XBOX360W:
724 xpad360w_process_packet(xpad, 0, xpad->idata);
725 break;
Ted Mielczarek1a48ff82014-08-08 11:21:59 -0700726 case XTYPE_XBOXONE:
727 xpadone_process_packet(xpad, 0, xpad->idata);
728 break;
Brian Magnuson99de0912008-04-03 16:19:23 -0400729 default:
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400730 xpad_process_packet(xpad, 0, xpad->idata);
Brian Magnuson99de0912008-04-03 16:19:23 -0400731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733exit:
Dmitry Torokhovdd38d682010-01-09 00:13:36 -0800734 retval = usb_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (retval)
Greg Kroah-Hartmanc4f0bbc2012-05-01 21:32:59 -0700736 dev_err(dev, "%s - usb_submit_urb failed with result %d\n",
Greg Kroah-Hartman9cb757b2012-04-25 14:48:23 -0700737 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800740/* Callers must hold xpad->odata_lock spinlock */
741static bool xpad_prepare_next_out_packet(struct usb_xpad *xpad)
742{
743 struct xpad_output_packet *pkt, *packet = NULL;
744 int i;
745
746 for (i = 0; i < XPAD_NUM_OUT_PACKETS; i++) {
747 if (++xpad->last_out_packet >= XPAD_NUM_OUT_PACKETS)
748 xpad->last_out_packet = 0;
749
750 pkt = &xpad->out_packets[xpad->last_out_packet];
751 if (pkt->pending) {
752 dev_dbg(&xpad->intf->dev,
753 "%s - found pending output packet %d\n",
754 __func__, xpad->last_out_packet);
755 packet = pkt;
756 break;
757 }
758 }
759
760 if (packet) {
761 memcpy(xpad->odata, packet->data, packet->len);
762 xpad->irq_out->transfer_buffer_length = packet->len;
Pavel Rojtberg4efc6932016-05-27 16:22:25 -0700763 packet->pending = false;
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800764 return true;
765 }
766
767 return false;
768}
769
770/* Callers must hold xpad->odata_lock spinlock */
771static int xpad_try_sending_next_out_packet(struct usb_xpad *xpad)
772{
773 int error;
774
775 if (!xpad->irq_out_active && xpad_prepare_next_out_packet(xpad)) {
Pavel Rojtberg4220f7d2015-12-09 13:30:34 -0800776 usb_anchor_urb(xpad->irq_out, &xpad->irq_out_anchor);
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800777 error = usb_submit_urb(xpad->irq_out, GFP_ATOMIC);
778 if (error) {
779 dev_err(&xpad->intf->dev,
780 "%s - usb_submit_urb failed with result %d\n",
781 __func__, error);
Pavel Rojtberg4220f7d2015-12-09 13:30:34 -0800782 usb_unanchor_urb(xpad->irq_out);
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800783 return -EIO;
784 }
785
786 xpad->irq_out_active = true;
787 }
788
789 return 0;
790}
791
Jan Kratochvile01a06e2007-05-09 00:27:51 -0400792static void xpad_irq_out(struct urb *urb)
793{
Greg Kroah-Hartman9cb757b2012-04-25 14:48:23 -0700794 struct usb_xpad *xpad = urb->context;
Greg Kroah-Hartman8818e412012-05-04 15:32:53 -0700795 struct device *dev = &xpad->intf->dev;
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800796 int status = urb->status;
797 int error;
798 unsigned long flags;
Jan Kratochvile01a06e2007-05-09 00:27:51 -0400799
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800800 spin_lock_irqsave(&xpad->odata_lock, flags);
Oliver Neukum6fc88f532008-04-03 21:40:59 +0200801
802 switch (status) {
Michael Gruber70a6f2e2009-07-12 20:51:36 -0700803 case 0:
Jan Kratochvile01a06e2007-05-09 00:27:51 -0400804 /* success */
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800805 xpad->irq_out_active = xpad_prepare_next_out_packet(xpad);
806 break;
Michael Gruber70a6f2e2009-07-12 20:51:36 -0700807
808 case -ECONNRESET:
809 case -ENOENT:
810 case -ESHUTDOWN:
811 /* this urb is terminated, clean up */
Greg Kroah-Hartmanc4f0bbc2012-05-01 21:32:59 -0700812 dev_dbg(dev, "%s - urb shutting down with status: %d\n",
813 __func__, status);
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800814 xpad->irq_out_active = false;
815 break;
Michael Gruber70a6f2e2009-07-12 20:51:36 -0700816
817 default:
Greg Kroah-Hartmanc4f0bbc2012-05-01 21:32:59 -0700818 dev_dbg(dev, "%s - nonzero urb status received: %d\n",
819 __func__, status);
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800820 break;
Jan Kratochvile01a06e2007-05-09 00:27:51 -0400821 }
822
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800823 if (xpad->irq_out_active) {
Pavel Rojtberg4220f7d2015-12-09 13:30:34 -0800824 usb_anchor_urb(urb, &xpad->irq_out_anchor);
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800825 error = usb_submit_urb(urb, GFP_ATOMIC);
826 if (error) {
827 dev_err(dev,
828 "%s - usb_submit_urb failed with result %d\n",
829 __func__, error);
Pavel Rojtberg4220f7d2015-12-09 13:30:34 -0800830 usb_unanchor_urb(urb);
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800831 xpad->irq_out_active = false;
832 }
833 }
834
835 spin_unlock_irqrestore(&xpad->odata_lock, flags);
Jan Kratochvile01a06e2007-05-09 00:27:51 -0400836}
837
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400838static int xpad_init_output(struct usb_interface *intf, struct usb_xpad *xpad)
839{
840 struct usb_endpoint_descriptor *ep_irq_out;
Ted Mielczarek1a48ff82014-08-08 11:21:59 -0700841 int ep_irq_out_idx;
Axel Lin49cc69b2010-11-11 21:39:11 -0800842 int error;
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400843
Chris Moellerb514d4f2011-07-04 19:21:59 -0700844 if (xpad->xtype == XTYPE_UNKNOWN)
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400845 return 0;
846
Pavel Rojtberg4220f7d2015-12-09 13:30:34 -0800847 init_usb_anchor(&xpad->irq_out_anchor);
848
Daniel Mack997ea582010-04-12 13:17:25 +0200849 xpad->odata = usb_alloc_coherent(xpad->udev, XPAD_PKT_LEN,
850 GFP_KERNEL, &xpad->odata_dma);
Axel Lin49cc69b2010-11-11 21:39:11 -0800851 if (!xpad->odata) {
852 error = -ENOMEM;
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400853 goto fail1;
Axel Lin49cc69b2010-11-11 21:39:11 -0800854 }
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400855
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800856 spin_lock_init(&xpad->odata_lock);
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400857
858 xpad->irq_out = usb_alloc_urb(0, GFP_KERNEL);
Axel Lin49cc69b2010-11-11 21:39:11 -0800859 if (!xpad->irq_out) {
860 error = -ENOMEM;
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400861 goto fail2;
Axel Lin49cc69b2010-11-11 21:39:11 -0800862 }
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400863
Ted Mielczarek1a48ff82014-08-08 11:21:59 -0700864 /* Xbox One controller has in/out endpoints swapped. */
865 ep_irq_out_idx = xpad->xtype == XTYPE_XBOXONE ? 0 : 1;
866 ep_irq_out = &intf->cur_altsetting->endpoint[ep_irq_out_idx].desc;
867
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400868 usb_fill_int_urb(xpad->irq_out, xpad->udev,
869 usb_sndintpipe(xpad->udev, ep_irq_out->bEndpointAddress),
870 xpad->odata, XPAD_PKT_LEN,
871 xpad_irq_out, xpad, ep_irq_out->bInterval);
872 xpad->irq_out->transfer_dma = xpad->odata_dma;
873 xpad->irq_out->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
874
875 return 0;
876
Daniel Mack997ea582010-04-12 13:17:25 +0200877 fail2: usb_free_coherent(xpad->udev, XPAD_PKT_LEN, xpad->odata, xpad->odata_dma);
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400878 fail1: return error;
879}
880
881static void xpad_stop_output(struct usb_xpad *xpad)
882{
Pavel Rojtberg4220f7d2015-12-09 13:30:34 -0800883 if (xpad->xtype != XTYPE_UNKNOWN) {
884 if (!usb_wait_anchor_empty_timeout(&xpad->irq_out_anchor,
885 5000)) {
886 dev_warn(&xpad->intf->dev,
887 "timed out waiting for output URB to complete, killing\n");
888 usb_kill_anchored_urbs(&xpad->irq_out_anchor);
889 }
890 }
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400891}
892
893static void xpad_deinit_output(struct usb_xpad *xpad)
894{
Chris Moellerb514d4f2011-07-04 19:21:59 -0700895 if (xpad->xtype != XTYPE_UNKNOWN) {
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400896 usb_free_urb(xpad->irq_out);
Daniel Mack997ea582010-04-12 13:17:25 +0200897 usb_free_coherent(xpad->udev, XPAD_PKT_LEN,
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400898 xpad->odata, xpad->odata_dma);
899 }
900}
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400901
Pavel Rojtbergaba54872015-10-10 09:36:17 -0700902static int xpad_inquiry_pad_presence(struct usb_xpad *xpad)
903{
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800904 struct xpad_output_packet *packet =
905 &xpad->out_packets[XPAD_OUT_CMD_IDX];
906 unsigned long flags;
Pavel Rojtbergaba54872015-10-10 09:36:17 -0700907 int retval;
908
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800909 spin_lock_irqsave(&xpad->odata_lock, flags);
Pavel Rojtbergaba54872015-10-10 09:36:17 -0700910
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800911 packet->data[0] = 0x08;
912 packet->data[1] = 0x00;
913 packet->data[2] = 0x0F;
914 packet->data[3] = 0xC0;
915 packet->data[4] = 0x00;
916 packet->data[5] = 0x00;
917 packet->data[6] = 0x00;
918 packet->data[7] = 0x00;
919 packet->data[8] = 0x00;
920 packet->data[9] = 0x00;
921 packet->data[10] = 0x00;
922 packet->data[11] = 0x00;
923 packet->len = 12;
924 packet->pending = true;
Pavel Rojtbergaba54872015-10-10 09:36:17 -0700925
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800926 /* Reset the sequence so we send out presence first */
927 xpad->last_out_packet = -1;
928 retval = xpad_try_sending_next_out_packet(xpad);
Pavel Rojtbergaba54872015-10-10 09:36:17 -0700929
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800930 spin_unlock_irqrestore(&xpad->odata_lock, flags);
931
932 return retval;
933}
934
935static int xpad_start_xbox_one(struct usb_xpad *xpad)
936{
937 struct xpad_output_packet *packet =
938 &xpad->out_packets[XPAD_OUT_CMD_IDX];
939 unsigned long flags;
940 int retval;
941
942 spin_lock_irqsave(&xpad->odata_lock, flags);
943
944 /* Xbox one controller needs to be initialized. */
945 packet->data[0] = 0x05;
946 packet->data[1] = 0x20;
Pierre-Loup A. Griffais2a6d7522015-12-09 13:40:37 -0800947 packet->data[2] = xpad->odata_serial++; /* packet serial */
948 packet->data[3] = 0x01; /* rumble bit enable? */
949 packet->data[4] = 0x00;
950 packet->len = 5;
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800951 packet->pending = true;
952
953 /* Reset the sequence so we send out start packet first */
954 xpad->last_out_packet = -1;
955 retval = xpad_try_sending_next_out_packet(xpad);
956
957 spin_unlock_irqrestore(&xpad->odata_lock, flags);
Pavel Rojtbergaba54872015-10-10 09:36:17 -0700958
959 return retval;
960}
961
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400962#ifdef CONFIG_JOYSTICK_XPAD_FF
Benjamin Valentin12187302010-01-21 20:19:06 -0800963static int xpad_play_effect(struct input_dev *dev, void *data, struct ff_effect *effect)
Jan Kratochvile01a06e2007-05-09 00:27:51 -0400964{
965 struct usb_xpad *xpad = input_get_drvdata(dev);
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800966 struct xpad_output_packet *packet = &xpad->out_packets[XPAD_OUT_FF_IDX];
Pavel Rojtberg06008152015-10-10 09:32:55 -0700967 __u16 strong;
968 __u16 weak;
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800969 int retval;
970 unsigned long flags;
Jan Kratochvile01a06e2007-05-09 00:27:51 -0400971
Pavel Rojtberg06008152015-10-10 09:32:55 -0700972 if (effect->type != FF_RUMBLE)
973 return 0;
Benjamin Valentin12187302010-01-21 20:19:06 -0800974
Pavel Rojtberg06008152015-10-10 09:32:55 -0700975 strong = effect->u.rumble.strong_magnitude;
976 weak = effect->u.rumble.weak_magnitude;
Benjamin Valentin12187302010-01-21 20:19:06 -0800977
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800978 spin_lock_irqsave(&xpad->odata_lock, flags);
979
Pavel Rojtberg06008152015-10-10 09:32:55 -0700980 switch (xpad->xtype) {
981 case XTYPE_XBOX:
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800982 packet->data[0] = 0x00;
983 packet->data[1] = 0x06;
984 packet->data[2] = 0x00;
985 packet->data[3] = strong / 256; /* left actuator */
986 packet->data[4] = 0x00;
987 packet->data[5] = weak / 256; /* right actuator */
988 packet->len = 6;
989 packet->pending = true;
Pavel Rojtberg06008152015-10-10 09:32:55 -0700990 break;
Benjamin Valentin12187302010-01-21 20:19:06 -0800991
Pavel Rojtberg06008152015-10-10 09:32:55 -0700992 case XTYPE_XBOX360:
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800993 packet->data[0] = 0x00;
994 packet->data[1] = 0x08;
995 packet->data[2] = 0x00;
996 packet->data[3] = strong / 256; /* left actuator? */
997 packet->data[4] = weak / 256; /* right actuator? */
998 packet->data[5] = 0x00;
999 packet->data[6] = 0x00;
1000 packet->data[7] = 0x00;
1001 packet->len = 8;
1002 packet->pending = true;
Pavel Rojtberg06008152015-10-10 09:32:55 -07001003 break;
Benjamin Valentin12187302010-01-21 20:19:06 -08001004
Pavel Rojtberg06008152015-10-10 09:32:55 -07001005 case XTYPE_XBOX360W:
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001006 packet->data[0] = 0x00;
1007 packet->data[1] = 0x01;
1008 packet->data[2] = 0x0F;
1009 packet->data[3] = 0xC0;
1010 packet->data[4] = 0x00;
1011 packet->data[5] = strong / 256;
1012 packet->data[6] = weak / 256;
1013 packet->data[7] = 0x00;
1014 packet->data[8] = 0x00;
1015 packet->data[9] = 0x00;
1016 packet->data[10] = 0x00;
1017 packet->data[11] = 0x00;
1018 packet->len = 12;
1019 packet->pending = true;
Pavel Rojtberg06008152015-10-10 09:32:55 -07001020 break;
Benjamin Valentin12187302010-01-21 20:19:06 -08001021
Pavel Rojtberg06008152015-10-10 09:32:55 -07001022 case XTYPE_XBOXONE:
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001023 packet->data[0] = 0x09; /* activate rumble */
1024 packet->data[1] = 0x08;
Pierre-Loup A. Griffais2a6d7522015-12-09 13:40:37 -08001025 packet->data[2] = xpad->odata_serial++;
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001026 packet->data[3] = 0x08; /* continuous effect */
1027 packet->data[4] = 0x00; /* simple rumble mode */
1028 packet->data[5] = 0x03; /* L and R actuator only */
1029 packet->data[6] = 0x00; /* TODO: LT actuator */
1030 packet->data[7] = 0x00; /* TODO: RT actuator */
Pierre-Loup A. Griffais2a6d7522015-12-09 13:40:37 -08001031 packet->data[8] = strong / 512; /* left actuator */
1032 packet->data[9] = weak / 512; /* right actuator */
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001033 packet->data[10] = 0x80; /* length of pulse */
1034 packet->data[11] = 0x00; /* stop period of pulse */
Pierre-Loup A. Griffais2a6d7522015-12-09 13:40:37 -08001035 packet->data[12] = 0x00;
1036 packet->len = 13;
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001037 packet->pending = true;
Pavel Rojtberg06008152015-10-10 09:32:55 -07001038 break;
Benjamin Valentin12187302010-01-21 20:19:06 -08001039
Pavel Rojtberg06008152015-10-10 09:32:55 -07001040 default:
1041 dev_dbg(&xpad->dev->dev,
1042 "%s - rumble command sent to unsupported xpad type: %d\n",
1043 __func__, xpad->xtype);
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001044 retval = -EINVAL;
1045 goto out;
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001046 }
1047
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001048 retval = xpad_try_sending_next_out_packet(xpad);
1049
1050out:
1051 spin_unlock_irqrestore(&xpad->odata_lock, flags);
1052 return retval;
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001053}
1054
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001055static int xpad_init_ff(struct usb_xpad *xpad)
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001056{
Ming-ting Yao Wei06049492015-04-14 16:59:11 -07001057 if (xpad->xtype == XTYPE_UNKNOWN)
Anssi Hannulacfbe2012008-04-03 16:18:57 -04001058 return 0;
1059
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001060 input_set_capability(xpad->dev, EV_FF, FF_RUMBLE);
1061
1062 return input_ff_create_memless(xpad->dev, NULL, xpad_play_effect);
1063}
1064
1065#else
1066static int xpad_init_ff(struct usb_xpad *xpad) { return 0; }
1067#endif
1068
1069#if defined(CONFIG_JOYSTICK_XPAD_LEDS)
1070#include <linux/leds.h>
Pavel Rojtberge3b65172015-10-10 10:00:49 -07001071#include <linux/idr.h>
1072
1073static DEFINE_IDA(xpad_pad_seq);
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001074
1075struct xpad_led {
1076 char name[16];
1077 struct led_classdev led_cdev;
1078 struct usb_xpad *xpad;
1079};
1080
Pierre-Loup A. Griffais75b7f052015-06-22 14:10:36 -07001081/**
Pavel Rojtberg1f6f02b2015-10-06 17:06:16 -07001082 * set the LEDs on Xbox360 / Wireless Controllers
Pierre-Loup A. Griffais75b7f052015-06-22 14:10:36 -07001083 * @param command
1084 * 0: off
1085 * 1: all blink, then previous setting
1086 * 2: 1/top-left blink, then on
1087 * 3: 2/top-right blink, then on
1088 * 4: 3/bottom-left blink, then on
1089 * 5: 4/bottom-right blink, then on
1090 * 6: 1/top-left on
1091 * 7: 2/top-right on
1092 * 8: 3/bottom-left on
1093 * 9: 4/bottom-right on
1094 * 10: rotate
1095 * 11: blink, based on previous setting
1096 * 12: slow blink, based on previous setting
1097 * 13: rotate with two lights
1098 * 14: persistent slow all blink
1099 * 15: blink once, then previous setting
1100 */
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001101static void xpad_send_led_command(struct usb_xpad *xpad, int command)
1102{
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001103 struct xpad_output_packet *packet =
1104 &xpad->out_packets[XPAD_OUT_LED_IDX];
1105 unsigned long flags;
1106
Pierre-Loup A. Griffais75b7f052015-06-22 14:10:36 -07001107 command %= 16;
1108
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001109 spin_lock_irqsave(&xpad->odata_lock, flags);
Pierre-Loup A. Griffais75b7f052015-06-22 14:10:36 -07001110
1111 switch (xpad->xtype) {
1112 case XTYPE_XBOX360:
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001113 packet->data[0] = 0x01;
1114 packet->data[1] = 0x03;
1115 packet->data[2] = command;
1116 packet->len = 3;
1117 packet->pending = true;
Pierre-Loup A. Griffais75b7f052015-06-22 14:10:36 -07001118 break;
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001119
Pierre-Loup A. Griffais75b7f052015-06-22 14:10:36 -07001120 case XTYPE_XBOX360W:
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001121 packet->data[0] = 0x00;
1122 packet->data[1] = 0x00;
1123 packet->data[2] = 0x08;
1124 packet->data[3] = 0x40 + command;
1125 packet->data[4] = 0x00;
1126 packet->data[5] = 0x00;
1127 packet->data[6] = 0x00;
1128 packet->data[7] = 0x00;
1129 packet->data[8] = 0x00;
1130 packet->data[9] = 0x00;
1131 packet->data[10] = 0x00;
1132 packet->data[11] = 0x00;
1133 packet->len = 12;
1134 packet->pending = true;
Pierre-Loup A. Griffais75b7f052015-06-22 14:10:36 -07001135 break;
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001136 }
Pierre-Loup A. Griffais75b7f052015-06-22 14:10:36 -07001137
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001138 xpad_try_sending_next_out_packet(xpad);
1139
1140 spin_unlock_irqrestore(&xpad->odata_lock, flags);
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001141}
1142
Pavel Rojtberg1f6f02b2015-10-06 17:06:16 -07001143/*
1144 * Light up the segment corresponding to the pad number on
1145 * Xbox 360 Controllers.
1146 */
Pavel Rojtbergcae705b2015-06-22 14:11:30 -07001147static void xpad_identify_controller(struct usb_xpad *xpad)
1148{
Dmitry Torokhovd9be3982015-12-16 14:24:58 -08001149 led_set_brightness(&xpad->led->led_cdev, (xpad->pad_nr % 4) + 2);
Pavel Rojtbergcae705b2015-06-22 14:11:30 -07001150}
1151
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001152static void xpad_led_set(struct led_classdev *led_cdev,
1153 enum led_brightness value)
1154{
1155 struct xpad_led *xpad_led = container_of(led_cdev,
1156 struct xpad_led, led_cdev);
1157
1158 xpad_send_led_command(xpad_led->xpad, value);
1159}
1160
1161static int xpad_led_probe(struct usb_xpad *xpad)
1162{
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001163 struct xpad_led *led;
1164 struct led_classdev *led_cdev;
1165 int error;
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001166
Pierre-Loup A. Griffais75b7f052015-06-22 14:10:36 -07001167 if (xpad->xtype != XTYPE_XBOX360 && xpad->xtype != XTYPE_XBOX360W)
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001168 return 0;
1169
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001170 xpad->led = led = kzalloc(sizeof(struct xpad_led), GFP_KERNEL);
1171 if (!led)
1172 return -ENOMEM;
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001173
Pavel Rojtberge3b65172015-10-10 10:00:49 -07001174 xpad->pad_nr = ida_simple_get(&xpad_pad_seq, 0, 0, GFP_KERNEL);
1175 if (xpad->pad_nr < 0) {
1176 error = xpad->pad_nr;
1177 goto err_free_mem;
1178 }
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001179
Pavel Rojtberge3b65172015-10-10 10:00:49 -07001180 snprintf(led->name, sizeof(led->name), "xpad%d", xpad->pad_nr);
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001181 led->xpad = xpad;
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001182
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001183 led_cdev = &led->led_cdev;
1184 led_cdev->name = led->name;
1185 led_cdev->brightness_set = xpad_led_set;
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001186
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001187 error = led_classdev_register(&xpad->udev->dev, led_cdev);
Pavel Rojtberge3b65172015-10-10 10:00:49 -07001188 if (error)
1189 goto err_free_id;
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001190
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -08001191 xpad_identify_controller(xpad);
Pavel Rojtbergfbe6a312015-10-19 00:06:58 -07001192
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001193 return 0;
Pavel Rojtberge3b65172015-10-10 10:00:49 -07001194
1195err_free_id:
1196 ida_simple_remove(&xpad_pad_seq, xpad->pad_nr);
1197err_free_mem:
1198 kfree(led);
1199 xpad->led = NULL;
1200 return error;
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001201}
1202
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001203static void xpad_led_disconnect(struct usb_xpad *xpad)
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001204{
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001205 struct xpad_led *xpad_led = xpad->led;
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001206
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001207 if (xpad_led) {
1208 led_classdev_unregister(&xpad_led->led_cdev);
Pavel Rojtberge3b65172015-10-10 10:00:49 -07001209 ida_simple_remove(&xpad_pad_seq, xpad->pad_nr);
Axel Lin6ff92a62010-11-11 21:43:17 -08001210 kfree(xpad_led);
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001211 }
1212}
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001213#else
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001214static int xpad_led_probe(struct usb_xpad *xpad) { return 0; }
1215static void xpad_led_disconnect(struct usb_xpad *xpad) { }
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001216#endif
1217
Pavel Rojtberg4220f7d2015-12-09 13:30:34 -08001218static int xpad_start_input(struct usb_xpad *xpad)
1219{
1220 int error;
1221
1222 if (usb_submit_urb(xpad->irq_in, GFP_KERNEL))
1223 return -EIO;
1224
1225 if (xpad->xtype == XTYPE_XBOXONE) {
1226 error = xpad_start_xbox_one(xpad);
1227 if (error) {
1228 usb_kill_urb(xpad->irq_in);
1229 return error;
1230 }
1231 }
1232
1233 return 0;
1234}
1235
1236static void xpad_stop_input(struct usb_xpad *xpad)
1237{
1238 usb_kill_urb(xpad->irq_in);
1239}
1240
1241static int xpad360w_start_input(struct usb_xpad *xpad)
1242{
1243 int error;
1244
1245 error = usb_submit_urb(xpad->irq_in, GFP_KERNEL);
1246 if (error)
1247 return -EIO;
1248
1249 /*
1250 * Send presence packet.
1251 * This will force the controller to resend connection packets.
1252 * This is useful in the case we activate the module after the
1253 * adapter has been plugged in, as it won't automatically
1254 * send us info about the controllers.
1255 */
1256 error = xpad_inquiry_pad_presence(xpad);
1257 if (error) {
1258 usb_kill_urb(xpad->irq_in);
1259 return error;
1260 }
1261
1262 return 0;
1263}
1264
1265static void xpad360w_stop_input(struct usb_xpad *xpad)
1266{
1267 usb_kill_urb(xpad->irq_in);
1268
1269 /* Make sure we are done with presence work if it was scheduled */
1270 flush_work(&xpad->work);
1271}
1272
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001273static int xpad_open(struct input_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -04001275 struct usb_xpad *xpad = input_get_drvdata(dev);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -05001276
Pavel Rojtberg4220f7d2015-12-09 13:30:34 -08001277 return xpad_start_input(xpad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001280static void xpad_close(struct input_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -04001282 struct usb_xpad *xpad = input_get_drvdata(dev);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -05001283
Pavel Rojtberg4220f7d2015-12-09 13:30:34 -08001284 xpad_stop_input(xpad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
Dominic Cerquettideb8ee42006-10-10 14:42:48 -07001287static void xpad_set_up_abs(struct input_dev *input_dev, signed short abs)
1288{
Ted Mielczarek1a48ff82014-08-08 11:21:59 -07001289 struct usb_xpad *xpad = input_get_drvdata(input_dev);
Dominic Cerquettideb8ee42006-10-10 14:42:48 -07001290 set_bit(abs, input_dev->absbit);
1291
1292 switch (abs) {
1293 case ABS_X:
1294 case ABS_Y:
1295 case ABS_RX:
1296 case ABS_RY: /* the two sticks */
1297 input_set_abs_params(input_dev, abs, -32768, 32767, 16, 128);
1298 break;
1299 case ABS_Z:
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -08001300 case ABS_RZ: /* the triggers (if mapped to axes) */
Ted Mielczarek1a48ff82014-08-08 11:21:59 -07001301 if (xpad->xtype == XTYPE_XBOXONE)
1302 input_set_abs_params(input_dev, abs, 0, 1023, 0, 0);
1303 else
1304 input_set_abs_params(input_dev, abs, 0, 255, 0, 0);
Dominic Cerquettideb8ee42006-10-10 14:42:48 -07001305 break;
1306 case ABS_HAT0X:
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -08001307 case ABS_HAT0Y: /* the d-pad (only if dpad is mapped to axes */
Dominic Cerquettideb8ee42006-10-10 14:42:48 -07001308 input_set_abs_params(input_dev, abs, -1, 1, 0, 0);
1309 break;
1310 }
1311}
1312
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001313static void xpad_deinit_input(struct usb_xpad *xpad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -08001315 if (xpad->input_created) {
1316 xpad->input_created = false;
1317 xpad_led_disconnect(xpad);
1318 input_unregister_device(xpad->dev);
1319 }
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001320}
1321
1322static int xpad_init_input(struct usb_xpad *xpad)
1323{
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001324 struct input_dev *input_dev;
Axel Lin49cc69b2010-11-11 21:39:11 -08001325 int i, error;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -05001326
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001327 input_dev = input_allocate_device();
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001328 if (!input_dev)
1329 return -ENOMEM;
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -08001330
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001331 xpad->dev = input_dev;
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001332 input_dev->name = xpad->name;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001333 input_dev->phys = xpad->phys;
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001334 usb_to_input_id(xpad->udev, &input_dev->id);
1335 input_dev->dev.parent = &xpad->intf->dev;
Dmitry Torokhov7791bda2007-04-12 01:34:39 -04001336
1337 input_set_drvdata(input_dev, xpad);
1338
Pavel Rojtberg4220f7d2015-12-09 13:30:34 -08001339 if (xpad->xtype != XTYPE_XBOX360W) {
1340 input_dev->open = xpad_open;
1341 input_dev->close = xpad_close;
1342 }
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001343
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001344 __set_bit(EV_KEY, input_dev->evbit);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001345
Christoph Fritz7beae702010-07-13 09:42:33 -07001346 if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001347 __set_bit(EV_ABS, input_dev->evbit);
Christoph Fritz7beae702010-07-13 09:42:33 -07001348 /* set up axes */
1349 for (i = 0; xpad_abs[i] >= 0; i++)
1350 xpad_set_up_abs(input_dev, xpad_abs[i]);
1351 }
1352
1353 /* set up standard buttons */
Anssi Hannulafc55e952008-04-03 16:18:44 -04001354 for (i = 0; xpad_common_btn[i] >= 0; i++)
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -08001355 __set_bit(xpad_common_btn[i], input_dev->keybit);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001356
Christoph Fritz7beae702010-07-13 09:42:33 -07001357 /* set up model-specific ones */
Ted Mielczarek1a48ff82014-08-08 11:21:59 -07001358 if (xpad->xtype == XTYPE_XBOX360 || xpad->xtype == XTYPE_XBOX360W ||
1359 xpad->xtype == XTYPE_XBOXONE) {
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -08001360 for (i = 0; xpad360_btn[i] >= 0; i++)
1361 __set_bit(xpad360_btn[i], input_dev->keybit);
1362 } else {
1363 for (i = 0; xpad_btn[i] >= 0; i++)
1364 __set_bit(xpad_btn[i], input_dev->keybit);
1365 }
1366
1367 if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
1368 for (i = 0; xpad_btn_pad[i] >= 0; i++)
1369 __set_bit(xpad_btn_pad[i], input_dev->keybit);
Pavel Rojtberg5ee8bda2015-10-10 09:33:52 -07001370 }
1371
1372 /*
1373 * This should be a simple else block. However historically
1374 * xbox360w has mapped DPAD to buttons while xbox360 did not. This
1375 * made no sense, but now we can not just switch back and have to
1376 * support both behaviors.
1377 */
1378 if (!(xpad->mapping & MAP_DPAD_TO_BUTTONS) ||
1379 xpad->xtype == XTYPE_XBOX360W) {
Dominic Cerquettideb8ee42006-10-10 14:42:48 -07001380 for (i = 0; xpad_abs_pad[i] >= 0; i++)
Ted Mielczarek1a48ff82014-08-08 11:21:59 -07001381 xpad_set_up_abs(input_dev, xpad_abs_pad[i]);
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -08001382 }
1383
1384 if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
1385 for (i = 0; xpad_btn_triggers[i] >= 0; i++)
1386 __set_bit(xpad_btn_triggers[i], input_dev->keybit);
1387 } else {
1388 for (i = 0; xpad_abs_triggers[i] >= 0; i++)
1389 xpad_set_up_abs(input_dev, xpad_abs_triggers[i]);
1390 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -05001391
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001392 error = xpad_init_ff(xpad);
1393 if (error)
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001394 goto err_free_input;
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001395
1396 error = xpad_led_probe(xpad);
1397 if (error)
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001398 goto err_destroy_ff;
1399
1400 error = input_register_device(xpad->dev);
1401 if (error)
1402 goto err_disconnect_led;
1403
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -08001404 xpad->input_created = true;
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001405 return 0;
1406
1407err_disconnect_led:
1408 xpad_led_disconnect(xpad);
1409err_destroy_ff:
1410 input_ff_destroy(input_dev);
1411err_free_input:
1412 input_free_device(input_dev);
1413 return error;
1414}
1415
1416static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id)
1417{
1418 struct usb_device *udev = interface_to_usbdev(intf);
1419 struct usb_xpad *xpad;
1420 struct usb_endpoint_descriptor *ep_irq_in;
1421 int ep_irq_in_idx;
1422 int i, error;
1423
1424 for (i = 0; xpad_device[i].idVendor; i++) {
1425 if ((le16_to_cpu(udev->descriptor.idVendor) == xpad_device[i].idVendor) &&
1426 (le16_to_cpu(udev->descriptor.idProduct) == xpad_device[i].idProduct))
1427 break;
1428 }
1429
1430 if (xpad_device[i].xtype == XTYPE_XBOXONE &&
1431 intf->cur_altsetting->desc.bInterfaceNumber != 0) {
1432 /*
1433 * The Xbox One controller lists three interfaces all with the
1434 * same interface class, subclass and protocol. Differentiate by
1435 * interface number.
1436 */
1437 return -ENODEV;
1438 }
1439
1440 xpad = kzalloc(sizeof(struct usb_xpad), GFP_KERNEL);
1441 if (!xpad)
1442 return -ENOMEM;
1443
1444 usb_make_path(udev, xpad->phys, sizeof(xpad->phys));
1445 strlcat(xpad->phys, "/input0", sizeof(xpad->phys));
1446
1447 xpad->idata = usb_alloc_coherent(udev, XPAD_PKT_LEN,
1448 GFP_KERNEL, &xpad->idata_dma);
1449 if (!xpad->idata) {
1450 error = -ENOMEM;
1451 goto err_free_mem;
1452 }
1453
1454 xpad->irq_in = usb_alloc_urb(0, GFP_KERNEL);
1455 if (!xpad->irq_in) {
1456 error = -ENOMEM;
1457 goto err_free_idata;
1458 }
1459
1460 xpad->udev = udev;
1461 xpad->intf = intf;
1462 xpad->mapping = xpad_device[i].mapping;
1463 xpad->xtype = xpad_device[i].xtype;
1464 xpad->name = xpad_device[i].name;
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -08001465 INIT_WORK(&xpad->work, xpad_presence_work);
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001466
1467 if (xpad->xtype == XTYPE_UNKNOWN) {
1468 if (intf->cur_altsetting->desc.bInterfaceClass == USB_CLASS_VENDOR_SPEC) {
1469 if (intf->cur_altsetting->desc.bInterfaceProtocol == 129)
1470 xpad->xtype = XTYPE_XBOX360W;
1471 else
1472 xpad->xtype = XTYPE_XBOX360;
1473 } else {
1474 xpad->xtype = XTYPE_XBOX;
1475 }
1476
1477 if (dpad_to_buttons)
1478 xpad->mapping |= MAP_DPAD_TO_BUTTONS;
1479 if (triggers_to_buttons)
1480 xpad->mapping |= MAP_TRIGGERS_TO_BUTTONS;
1481 if (sticks_to_null)
1482 xpad->mapping |= MAP_STICKS_TO_NULL;
1483 }
1484
1485 error = xpad_init_output(intf, xpad);
1486 if (error)
1487 goto err_free_in_urb;
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001488
Ted Mielczarek1a48ff82014-08-08 11:21:59 -07001489 /* Xbox One controller has in/out endpoints swapped. */
1490 ep_irq_in_idx = xpad->xtype == XTYPE_XBOXONE ? 1 : 0;
1491 ep_irq_in = &intf->cur_altsetting->endpoint[ep_irq_in_idx].desc;
1492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 usb_fill_int_urb(xpad->irq_in, udev,
1494 usb_rcvintpipe(udev, ep_irq_in->bEndpointAddress),
1495 xpad->idata, XPAD_PKT_LEN, xpad_irq_in,
1496 xpad, ep_irq_in->bInterval);
1497 xpad->irq_in->transfer_dma = xpad->idata_dma;
1498 xpad->irq_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -05001499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 usb_set_intfdata(intf, xpad);
Brian Magnuson99de0912008-04-03 16:19:23 -04001501
Brian Magnuson99de0912008-04-03 16:19:23 -04001502 if (xpad->xtype == XTYPE_XBOX360W) {
Brian Magnuson99de0912008-04-03 16:19:23 -04001503 /*
Axel Line3f0f0a2010-11-17 23:59:34 -08001504 * Submit the int URB immediately rather than waiting for open
1505 * because we get status messages from the device whether
1506 * or not any controllers are attached. In fact, it's
1507 * exactly the message that a controller has arrived that
1508 * we're waiting for.
1509 */
Pavel Rojtberg4220f7d2015-12-09 13:30:34 -08001510 error = xpad360w_start_input(xpad);
Axel Line3f0f0a2010-11-17 23:59:34 -08001511 if (error)
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -08001512 goto err_deinit_output;
Pavel Rojtbergaba54872015-10-10 09:36:17 -07001513 /*
Pavel Rojtberg4220f7d2015-12-09 13:30:34 -08001514 * Wireless controllers require RESET_RESUME to work properly
1515 * after suspend. Ideally this quirk should be in usb core
1516 * quirk list, but we have too many vendors producing these
1517 * controllers and we'd need to maintain 2 identical lists
1518 * here in this driver and in usb core.
Pavel Rojtbergaba54872015-10-10 09:36:17 -07001519 */
Pavel Rojtberg4220f7d2015-12-09 13:30:34 -08001520 udev->quirks |= USB_QUIRK_RESET_RESUME;
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -08001521 } else {
1522 error = xpad_init_input(xpad);
1523 if (error)
1524 goto err_deinit_output;
Brian Magnuson99de0912008-04-03 16:19:23 -04001525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 return 0;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001527
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001528err_deinit_output:
1529 xpad_deinit_output(xpad);
1530err_free_in_urb:
1531 usb_free_urb(xpad->irq_in);
1532err_free_idata:
1533 usb_free_coherent(udev, XPAD_PKT_LEN, xpad->idata, xpad->idata_dma);
1534err_free_mem:
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001535 kfree(xpad);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001536 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537}
1538
1539static void xpad_disconnect(struct usb_interface *intf)
1540{
Pavel Rojtberg4220f7d2015-12-09 13:30:34 -08001541 struct usb_xpad *xpad = usb_get_intfdata(intf);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -05001542
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -08001543 if (xpad->xtype == XTYPE_XBOX360W)
Pavel Rojtberg4220f7d2015-12-09 13:30:34 -08001544 xpad360w_stop_input(xpad);
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -08001545
1546 xpad_deinit_input(xpad);
Dmitry Torokhov2a059152010-11-11 21:52:18 -08001547
Pavel Rojtberg4220f7d2015-12-09 13:30:34 -08001548 /*
1549 * Now that both input device and LED device are gone we can
1550 * stop output URB.
1551 */
1552 xpad_stop_output(xpad);
1553
1554 xpad_deinit_output(xpad);
1555
Dmitry Torokhov2a059152010-11-11 21:52:18 -08001556 usb_free_urb(xpad->irq_in);
1557 usb_free_coherent(xpad->udev, XPAD_PKT_LEN,
1558 xpad->idata, xpad->idata_dma);
1559
Dmitry Torokhov2a059152010-11-11 21:52:18 -08001560 kfree(xpad);
1561
1562 usb_set_intfdata(intf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564
Pavel Rojtberg4220f7d2015-12-09 13:30:34 -08001565static int xpad_suspend(struct usb_interface *intf, pm_message_t message)
1566{
1567 struct usb_xpad *xpad = usb_get_intfdata(intf);
1568 struct input_dev *input = xpad->dev;
1569
1570 if (xpad->xtype == XTYPE_XBOX360W) {
1571 /*
1572 * Wireless controllers always listen to input so
1573 * they are notified when controller shows up
1574 * or goes away.
1575 */
1576 xpad360w_stop_input(xpad);
1577 } else {
1578 mutex_lock(&input->mutex);
1579 if (input->users)
1580 xpad_stop_input(xpad);
1581 mutex_unlock(&input->mutex);
1582 }
1583
1584 xpad_stop_output(xpad);
1585
1586 return 0;
1587}
1588
1589static int xpad_resume(struct usb_interface *intf)
1590{
1591 struct usb_xpad *xpad = usb_get_intfdata(intf);
1592 struct input_dev *input = xpad->dev;
1593 int retval = 0;
1594
1595 if (xpad->xtype == XTYPE_XBOX360W) {
1596 retval = xpad360w_start_input(xpad);
1597 } else {
1598 mutex_lock(&input->mutex);
1599 if (input->users)
1600 retval = xpad_start_input(xpad);
1601 mutex_unlock(&input->mutex);
1602 }
1603
1604 return retval;
1605}
1606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607static struct usb_driver xpad_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 .name = "xpad",
1609 .probe = xpad_probe,
1610 .disconnect = xpad_disconnect,
Pavel Rojtberg4220f7d2015-12-09 13:30:34 -08001611 .suspend = xpad_suspend,
1612 .resume = xpad_resume,
1613 .reset_resume = xpad_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 .id_table = xpad_table,
1615};
1616
Greg Kroah-Hartman08642e72011-11-18 09:48:31 -08001617module_usb_driver(xpad_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619MODULE_AUTHOR(DRIVER_AUTHOR);
1620MODULE_DESCRIPTION(DRIVER_DESC);
1621MODULE_LICENSE("GPL");