blob: 285f0b753c251303aba544e955fed6aa993ad2f9 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#define DRIVER_AUTHOR "Marko Friedemann <mfr@bmx-chemnitz.de>"
87#define DRIVER_DESC "X-Box pad driver"
88
89#define XPAD_PKT_LEN 32
90
Dominic Cerquettideb8ee42006-10-10 14:42:48 -070091/* xbox d-pads should map to buttons, as is required for DDR pads
92 but we map them to axes when possible to simplify things */
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -080093#define MAP_DPAD_TO_BUTTONS (1 << 0)
94#define MAP_TRIGGERS_TO_BUTTONS (1 << 1)
Christoph Fritz7beae702010-07-13 09:42:33 -070095#define MAP_STICKS_TO_NULL (1 << 2)
96#define DANCEPAD_MAP_CONFIG (MAP_DPAD_TO_BUTTONS | \
97 MAP_TRIGGERS_TO_BUTTONS | MAP_STICKS_TO_NULL)
Dominic Cerquettideb8ee42006-10-10 14:42:48 -070098
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -040099#define XTYPE_XBOX 0
100#define XTYPE_XBOX360 1
Brian Magnuson99de0912008-04-03 16:19:23 -0400101#define XTYPE_XBOX360W 2
Ted Mielczarek1a48ff82014-08-08 11:21:59 -0700102#define XTYPE_XBOXONE 3
103#define XTYPE_UNKNOWN 4
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400104
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030105static bool dpad_to_buttons;
Dominic Cerquettideb8ee42006-10-10 14:42:48 -0700106module_param(dpad_to_buttons, bool, S_IRUGO);
107MODULE_PARM_DESC(dpad_to_buttons, "Map D-PAD to buttons rather than axes for unknown pads");
108
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030109static bool triggers_to_buttons;
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800110module_param(triggers_to_buttons, bool, S_IRUGO);
111MODULE_PARM_DESC(triggers_to_buttons, "Map triggers to buttons rather than axes for unknown pads");
112
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030113static bool sticks_to_null;
Christoph Fritz7beae702010-07-13 09:42:33 -0700114module_param(sticks_to_null, bool, S_IRUGO);
115MODULE_PARM_DESC(sticks_to_null, "Do not map sticks at all for unknown pads");
116
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100117static const struct xpad_device {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 u16 idVendor;
119 u16 idProduct;
120 char *name;
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800121 u8 mapping;
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400122 u8 xtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123} xpad_device[] = {
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800124 { 0x045e, 0x0202, "Microsoft X-Box pad v1 (US)", 0, XTYPE_XBOX },
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800125 { 0x045e, 0x0285, "Microsoft X-Box pad (Japan)", 0, XTYPE_XBOX },
126 { 0x045e, 0x0287, "Microsoft Xbox Controller S", 0, XTYPE_XBOX },
Guillermo A. Amaral540602a2012-12-02 23:26:18 -0800127 { 0x045e, 0x0289, "Microsoft X-Box pad v2 (US)", 0, XTYPE_XBOX },
128 { 0x045e, 0x028e, "Microsoft X-Box 360 pad", 0, XTYPE_XBOX360 },
Ted Mielczarek1a48ff82014-08-08 11:21:59 -0700129 { 0x045e, 0x02d1, "Microsoft X-Box One pad", 0, XTYPE_XBOXONE },
Erik Lundgren39a7a882015-10-06 17:04:11 -0700130 { 0x045e, 0x02dd, "Microsoft X-Box One pad (Covert Forces)", 0, XTYPE_XBOXONE },
Guillermo A. Amaral540602a2012-12-02 23:26:18 -0800131 { 0x045e, 0x0291, "Xbox 360 Wireless Receiver (XBOX)", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360W },
Brian Magnuson99de0912008-04-03 16:19:23 -0400132 { 0x045e, 0x0719, "Xbox 360 Wireless Receiver", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360W },
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800133 { 0x044f, 0x0f07, "Thrustmaster, Inc. Controller", 0, XTYPE_XBOX },
Benjamin Valentinf554f612014-09-08 14:18:40 -0700134 { 0x044f, 0xb326, "Thrustmaster Gamepad GP XID", 0, XTYPE_XBOX360 },
Petr Sebor8e2f2322014-01-02 15:35:07 -0800135 { 0x046d, 0xc21d, "Logitech Gamepad F310", 0, XTYPE_XBOX360 },
Benjamin Valentinf554f612014-09-08 14:18:40 -0700136 { 0x046d, 0xc21e, "Logitech Gamepad F510", 0, XTYPE_XBOX360 },
Petr Sebor8e2f2322014-01-02 15:35:07 -0800137 { 0x046d, 0xc21f, "Logitech Gamepad F710", 0, XTYPE_XBOX360 },
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800138 { 0x046d, 0xc242, "Logitech Chillstream Controller", 0, XTYPE_XBOX360 },
139 { 0x046d, 0xca84, "Logitech Xbox Cordless Controller", 0, XTYPE_XBOX },
140 { 0x046d, 0xca88, "Logitech Compact Controller for Xbox", 0, XTYPE_XBOX },
141 { 0x05fd, 0x1007, "Mad Catz Controller (unverified)", 0, XTYPE_XBOX },
142 { 0x05fd, 0x107a, "InterAct 'PowerPad Pro' X-Box pad (Germany)", 0, XTYPE_XBOX },
143 { 0x0738, 0x4516, "Mad Catz Control Pad", 0, XTYPE_XBOX },
144 { 0x0738, 0x4522, "Mad Catz LumiCON", 0, XTYPE_XBOX },
145 { 0x0738, 0x4526, "Mad Catz Control Pad Pro", 0, XTYPE_XBOX },
146 { 0x0738, 0x4536, "Mad Catz MicroCON", 0, XTYPE_XBOX },
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400147 { 0x0738, 0x4540, "Mad Catz Beat Pad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800148 { 0x0738, 0x4556, "Mad Catz Lynx Wireless Controller", 0, XTYPE_XBOX },
149 { 0x0738, 0x4716, "Mad Catz Wired Xbox 360 Controller", 0, XTYPE_XBOX360 },
Benjamin Valentinf554f612014-09-08 14:18:40 -0700150 { 0x0738, 0x4718, "Mad Catz Street Fighter IV FightStick SE", 0, XTYPE_XBOX360 },
151 { 0x0738, 0x4726, "Mad Catz Xbox 360 Controller", 0, XTYPE_XBOX360 },
Shawn Josephbe662272013-06-18 23:07:45 -0700152 { 0x0738, 0x4728, "Mad Catz Street Fighter IV FightPad", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800153 { 0x0738, 0x4738, "Mad Catz Wired Xbox 360 Controller (SFIV)", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
Benjamin Valentinf554f612014-09-08 14:18:40 -0700154 { 0x0738, 0x4740, "Mad Catz Beat Pad", 0, XTYPE_XBOX360 },
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400155 { 0x0738, 0x6040, "Mad Catz Beat Pad Pro", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
Benjamin Valentinf554f612014-09-08 14:18:40 -0700156 { 0x0738, 0xb726, "Mad Catz Xbox controller - MW2", 0, XTYPE_XBOX360 },
Guillermo A. Amaral540602a2012-12-02 23:26:18 -0800157 { 0x0738, 0xbeef, "Mad Catz JOYTECH NEO SE Advanced GamePad", XTYPE_XBOX360 },
Benjamin Valentinf554f612014-09-08 14:18:40 -0700158 { 0x0738, 0xcb02, "Saitek Cyborg Rumble Pad - PC/Xbox 360", 0, XTYPE_XBOX360 },
159 { 0x0738, 0xcb03, "Saitek P3200 Rumble Pad - PC/Xbox 360", 0, XTYPE_XBOX360 },
160 { 0x0738, 0xf738, "Super SFIV FightStick TE S", 0, XTYPE_XBOX360 },
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800161 { 0x0c12, 0x8802, "Zeroplus Xbox Controller", 0, XTYPE_XBOX },
Guillermo A. Amaral540602a2012-12-02 23:26:18 -0800162 { 0x0c12, 0x8809, "RedOctane Xbox Dance Pad", DANCEPAD_MAP_CONFIG, XTYPE_XBOX },
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800163 { 0x0c12, 0x880a, "Pelican Eclipse PL-2023", 0, XTYPE_XBOX },
164 { 0x0c12, 0x8810, "Zeroplus Xbox Controller", 0, XTYPE_XBOX },
165 { 0x0c12, 0x9902, "HAMA VibraX - *FAULTY HARDWARE*", 0, XTYPE_XBOX },
Yuri Khane76b8ee2012-07-11 22:12:31 -0700166 { 0x0d2f, 0x0002, "Andamiro Pump It Up pad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800167 { 0x0e4c, 0x1097, "Radica Gamester Controller", 0, XTYPE_XBOX },
168 { 0x0e4c, 0x2390, "Radica Games Jtech Controller", 0, XTYPE_XBOX },
169 { 0x0e6f, 0x0003, "Logic3 Freebird wireless Controller", 0, XTYPE_XBOX },
170 { 0x0e6f, 0x0005, "Eclipse wireless Controller", 0, XTYPE_XBOX },
171 { 0x0e6f, 0x0006, "Edge wireless Controller", 0, XTYPE_XBOX },
Guillermo A. Amaral540602a2012-12-02 23:26:18 -0800172 { 0x0e6f, 0x0105, "HSM3 Xbox360 dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
Benjamin Valentinf554f612014-09-08 14:18:40 -0700173 { 0x0e6f, 0x0113, "Afterglow AX.1 Gamepad for Xbox 360", 0, XTYPE_XBOX360 },
Christoph Fritz6ac8a992010-08-09 10:08:10 -0700174 { 0x0e6f, 0x0201, "Pelican PL-3601 'TSZ' Wired Xbox 360 Controller", 0, XTYPE_XBOX360 },
Guillermo A. Amaral540602a2012-12-02 23:26:18 -0800175 { 0x0e6f, 0x0213, "Afterglow Gamepad for Xbox 360", 0, XTYPE_XBOX360 },
Benjamin Valentinf554f612014-09-08 14:18:40 -0700176 { 0x0e6f, 0x021f, "Rock Candy Gamepad for Xbox 360", 0, XTYPE_XBOX360 },
177 { 0x0e6f, 0x0301, "Logic3 Controller", 0, XTYPE_XBOX360 },
178 { 0x0e6f, 0x0401, "Logic3 Controller", 0, XTYPE_XBOX360 },
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800179 { 0x0e8f, 0x0201, "SmartJoy Frag Xpad/PS2 adaptor", 0, XTYPE_XBOX },
Benjamin Valentinf554f612014-09-08 14:18:40 -0700180 { 0x0e8f, 0x3008, "Generic xbox control (dealextreme)", 0, XTYPE_XBOX },
181 { 0x0f0d, 0x000a, "Hori Co. DOA4 FightStick", 0, XTYPE_XBOX360 },
Guillermo A. Amaral540602a2012-12-02 23:26:18 -0800182 { 0x0f0d, 0x000d, "Hori Fighting Stick EX2", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
183 { 0x0f0d, 0x0016, "Hori Real Arcade Pro.EX", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800184 { 0x0f30, 0x0202, "Joytech Advanced Controller", 0, XTYPE_XBOX },
185 { 0x0f30, 0x8888, "BigBen XBMiniPad Controller", 0, XTYPE_XBOX },
186 { 0x102c, 0xff0c, "Joytech Wireless Advanced Controller", 0, XTYPE_XBOX },
Magnus Hörlinfabadbc2011-06-21 14:40:30 -0700187 { 0x12ab, 0x0004, "Honey Bee Xbox360 dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
Benjamin Valentinf554f612014-09-08 14:18:40 -0700188 { 0x12ab, 0x0301, "PDP AFTERGLOW AX.1", 0, XTYPE_XBOX360 },
Guillermo A. Amaral540602a2012-12-02 23:26:18 -0800189 { 0x12ab, 0x8809, "Xbox DDR dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800190 { 0x1430, 0x4748, "RedOctane Guitar Hero X-plorer", 0, XTYPE_XBOX360 },
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400191 { 0x1430, 0x8888, "TX6500+ Dance Pad (first generation)", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800192 { 0x146b, 0x0601, "BigBen Interactive XBOX 360 Controller", 0, XTYPE_XBOX360 },
Benjamin Valentinf554f612014-09-08 14:18:40 -0700193 { 0x1532, 0x0037, "Razer Sabertooth", 0, XTYPE_XBOX360 },
194 { 0x15e4, 0x3f00, "Power A Mini Pro Elite", 0, XTYPE_XBOX360 },
195 { 0x15e4, 0x3f0a, "Xbox Airflo wired controller", 0, XTYPE_XBOX360 },
196 { 0x15e4, 0x3f10, "Batarang Xbox 360 controller", 0, XTYPE_XBOX360 },
197 { 0x162e, 0xbeef, "Joytech Neo-Se Take2", 0, XTYPE_XBOX360 },
Thomaz de Oliveira dos Reisdbb48002014-01-02 15:38:45 -0800198 { 0x1689, 0xfd00, "Razer Onza Tournament Edition", 0, XTYPE_XBOX360 },
199 { 0x1689, 0xfd01, "Razer Onza Classic Edition", 0, XTYPE_XBOX360 },
Frank Razenberga7b44732014-09-08 11:32:20 -0700200 { 0x24c6, 0x5d04, "Razer Sabertooth", 0, XTYPE_XBOX360 },
Magnus Hörlinfabadbc2011-06-21 14:40:30 -0700201 { 0x1bad, 0x0002, "Harmonix Rock Band Guitar", 0, XTYPE_XBOX360 },
Corbin Simpson805423e2009-08-20 21:41:04 -0700202 { 0x1bad, 0x0003, "Harmonix Rock Band Drumkit", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
Guillermo A. Amaral540602a2012-12-02 23:26:18 -0800203 { 0x1bad, 0xf016, "Mad Catz Xbox 360 Controller", 0, XTYPE_XBOX360 },
Benjamin Valentinf554f612014-09-08 14:18:40 -0700204 { 0x1bad, 0xf023, "MLG Pro Circuit Controller (Xbox)", 0, XTYPE_XBOX360 },
Guillermo A. Amaral540602a2012-12-02 23:26:18 -0800205 { 0x1bad, 0xf028, "Street Fighter IV FightPad", 0, XTYPE_XBOX360 },
Benjamin Valentinf554f612014-09-08 14:18:40 -0700206 { 0x1bad, 0xf038, "Street Fighter IV FightStick TE", 0, XTYPE_XBOX360 },
207 { 0x1bad, 0xf900, "Harmonix Xbox 360 Controller", 0, XTYPE_XBOX360 },
Guillermo A. Amaral540602a2012-12-02 23:26:18 -0800208 { 0x1bad, 0xf901, "Gamestop Xbox 360 Controller", 0, XTYPE_XBOX360 },
209 { 0x1bad, 0xf903, "Tron Xbox 360 controller", 0, XTYPE_XBOX360 },
Dario Scarpa470446e2015-10-06 17:04:36 -0700210 { 0x24c6, 0x5000, "Razer Atrox Arcade Stick", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
Guillermo A. Amaral540602a2012-12-02 23:26:18 -0800211 { 0x24c6, 0x5300, "PowerA MINI PROEX Controller", 0, XTYPE_XBOX360 },
Benjamin Valentinf554f612014-09-08 14:18:40 -0700212 { 0x24c6, 0x5303, "Xbox Airflo wired controller", 0, XTYPE_XBOX360 },
213 { 0x24c6, 0x5500, "Hori XBOX 360 EX 2 with Turbo", 0, XTYPE_XBOX360 },
214 { 0x24c6, 0x5501, "Hori Real Arcade Pro VX-SA", 0, XTYPE_XBOX360 },
215 { 0x24c6, 0x5506, "Hori SOULCALIBUR V Stick", 0, XTYPE_XBOX360 },
216 { 0x24c6, 0x5b02, "Thrustmaster, Inc. GPX Controller", 0, XTYPE_XBOX360 },
Tommi Rantala4b546252014-10-16 14:01:43 -0700217 { 0x24c6, 0x5b03, "Thrustmaster Ferrari 458 Racing Wheel", 0, XTYPE_XBOX360 },
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800218 { 0xffff, 0xffff, "Chinese-made Xbox Controller", 0, XTYPE_XBOX },
219 { 0x0000, 0x0000, "Generic X-Box pad", 0, XTYPE_UNKNOWN }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220};
221
Anssi Hannulafc55e952008-04-03 16:18:44 -0400222/* buttons shared with xbox and xbox360 */
223static const signed short xpad_common_btn[] = {
224 BTN_A, BTN_B, BTN_X, BTN_Y, /* "analog" buttons */
Christoph Fritz7beae702010-07-13 09:42:33 -0700225 BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR, /* start/back/sticks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 -1 /* terminating entry */
227};
228
Anssi Hannulafc55e952008-04-03 16:18:44 -0400229/* original xbox controllers only */
230static const signed short xpad_btn[] = {
231 BTN_C, BTN_Z, /* "analog" buttons */
232 -1 /* terminating entry */
233};
234
Christoph Fritz7beae702010-07-13 09:42:33 -0700235/* used when dpad is mapped to buttons */
Dominic Cerquettideb8ee42006-10-10 14:42:48 -0700236static const signed short xpad_btn_pad[] = {
Christoph Fritz7beae702010-07-13 09:42:33 -0700237 BTN_TRIGGER_HAPPY1, BTN_TRIGGER_HAPPY2, /* d-pad left, right */
238 BTN_TRIGGER_HAPPY3, BTN_TRIGGER_HAPPY4, /* d-pad up, down */
Dominic Cerquettideb8ee42006-10-10 14:42:48 -0700239 -1 /* terminating entry */
240};
241
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800242/* used when triggers are mapped to buttons */
243static const signed short xpad_btn_triggers[] = {
244 BTN_TL2, BTN_TR2, /* triggers left/right */
245 -1
246};
247
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400248static const signed short xpad360_btn[] = { /* buttons for x360 controller */
249 BTN_TL, BTN_TR, /* Button LB/RB */
250 BTN_MODE, /* The big X button */
251 -1
252};
253
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100254static const signed short xpad_abs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 ABS_X, ABS_Y, /* left stick */
256 ABS_RX, ABS_RY, /* right stick */
Dominic Cerquettideb8ee42006-10-10 14:42:48 -0700257 -1 /* terminating entry */
258};
259
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800260/* used when dpad is mapped to axes */
Dominic Cerquettideb8ee42006-10-10 14:42:48 -0700261static const signed short xpad_abs_pad[] = {
262 ABS_HAT0X, ABS_HAT0Y, /* d-pad axes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 -1 /* terminating entry */
264};
265
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800266/* used when triggers are mapped to axes */
267static const signed short xpad_abs_triggers[] = {
268 ABS_Z, ABS_RZ, /* triggers left/right */
269 -1
270};
271
Ted Mielczarek1a48ff82014-08-08 11:21:59 -0700272/*
273 * Xbox 360 has a vendor-specific class, so we cannot match it with only
Anssi Hannula8a0f83e2008-04-03 16:17:52 -0400274 * USB_INTERFACE_INFO (also specifically refused by USB subsystem), so we
Brian Magnuson99de0912008-04-03 16:19:23 -0400275 * match against vendor id as well. Wired Xbox 360 devices have protocol 1,
Ted Mielczarek1a48ff82014-08-08 11:21:59 -0700276 * wireless controllers have protocol 129.
277 */
Brian Magnuson99de0912008-04-03 16:19:23 -0400278#define XPAD_XBOX360_VENDOR_PROTOCOL(vend,pr) \
Anssi Hannula8a0f83e2008-04-03 16:17:52 -0400279 .match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_INT_INFO, \
280 .idVendor = (vend), \
281 .bInterfaceClass = USB_CLASS_VENDOR_SPEC, \
282 .bInterfaceSubClass = 93, \
Brian Magnuson99de0912008-04-03 16:19:23 -0400283 .bInterfaceProtocol = (pr)
284#define XPAD_XBOX360_VENDOR(vend) \
285 { XPAD_XBOX360_VENDOR_PROTOCOL(vend,1) }, \
286 { XPAD_XBOX360_VENDOR_PROTOCOL(vend,129) }
Anssi Hannula8a0f83e2008-04-03 16:17:52 -0400287
Ted Mielczarek1a48ff82014-08-08 11:21:59 -0700288/* The Xbox One controller uses subclass 71 and protocol 208. */
289#define XPAD_XBOXONE_VENDOR_PROTOCOL(vend, pr) \
290 .match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_INT_INFO, \
291 .idVendor = (vend), \
292 .bInterfaceClass = USB_CLASS_VENDOR_SPEC, \
293 .bInterfaceSubClass = 71, \
294 .bInterfaceProtocol = (pr)
295#define XPAD_XBOXONE_VENDOR(vend) \
296 { XPAD_XBOXONE_VENDOR_PROTOCOL(vend, 208) }
297
Guillermo A. Amaral0d027962012-12-02 23:26:11 -0800298static struct usb_device_id xpad_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 { USB_INTERFACE_INFO('X', 'B', 0) }, /* X-Box USB-IF not approved class */
Tommi Rantala4dfb15c2014-10-16 14:02:07 -0700300 XPAD_XBOX360_VENDOR(0x044f), /* Thrustmaster X-Box 360 controllers */
Brian Magnuson99de0912008-04-03 16:19:23 -0400301 XPAD_XBOX360_VENDOR(0x045e), /* Microsoft X-Box 360 controllers */
Ted Mielczarek1a48ff82014-08-08 11:21:59 -0700302 XPAD_XBOXONE_VENDOR(0x045e), /* Microsoft X-Box One controllers */
Brian Magnuson99de0912008-04-03 16:19:23 -0400303 XPAD_XBOX360_VENDOR(0x046d), /* Logitech X-Box 360 style controllers */
304 XPAD_XBOX360_VENDOR(0x0738), /* Mad Catz X-Box 360 controllers */
Yuri Khan3ffb62c2012-07-11 00:49:18 -0700305 { USB_DEVICE(0x0738, 0x4540) }, /* Mad Catz Beat Pad */
Brian Magnuson99de0912008-04-03 16:19:23 -0400306 XPAD_XBOX360_VENDOR(0x0e6f), /* 0x0e6f X-Box 360 controllers */
Magnus Hörlinfabadbc2011-06-21 14:40:30 -0700307 XPAD_XBOX360_VENDOR(0x12ab), /* X-Box 360 dance pads */
Brian Magnuson99de0912008-04-03 16:19:23 -0400308 XPAD_XBOX360_VENDOR(0x1430), /* RedOctane X-Box 360 controllers */
Thomas Gruber3ac91d32009-10-05 21:43:42 -0700309 XPAD_XBOX360_VENDOR(0x146b), /* BigBen Interactive Controllers */
Magnus Hörlinfabadbc2011-06-21 14:40:30 -0700310 XPAD_XBOX360_VENDOR(0x1bad), /* Harminix Rock Band Guitar and Drums */
Ilia Katsnelsoncc71a7e2012-07-11 00:54:20 -0700311 XPAD_XBOX360_VENDOR(0x0f0d), /* Hori Controllers */
312 XPAD_XBOX360_VENDOR(0x1689), /* Razer Onza */
Guillermo A. Amaral540602a2012-12-02 23:26:18 -0800313 XPAD_XBOX360_VENDOR(0x24c6), /* PowerA Controllers */
Benjamin Valentinf554f612014-09-08 14:18:40 -0700314 XPAD_XBOX360_VENDOR(0x1532), /* Razer Sabertooth */
315 XPAD_XBOX360_VENDOR(0x15e4), /* Numark X-Box 360 controllers */
316 XPAD_XBOX360_VENDOR(0x162e), /* Joytech X-Box 360 controllers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 { }
318};
319
Guillermo A. Amaral0d027962012-12-02 23:26:11 -0800320MODULE_DEVICE_TABLE(usb, xpad_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800322struct xpad_output_packet {
323 u8 data[XPAD_PKT_LEN];
324 u8 len;
325 bool pending;
326};
327
328#define XPAD_OUT_CMD_IDX 0
329#define XPAD_OUT_FF_IDX 1
330#define XPAD_OUT_LED_IDX (1 + IS_ENABLED(CONFIG_JOYSTICK_XPAD_FF))
331#define XPAD_NUM_OUT_PACKETS (1 + \
332 IS_ENABLED(CONFIG_JOYSTICK_XPAD_FF) + \
333 IS_ENABLED(CONFIG_JOYSTICK_XPAD_LEDS))
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335struct usb_xpad {
Dominic Cerquettideb8ee42006-10-10 14:42:48 -0700336 struct input_dev *dev; /* input device interface */
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -0800337 struct input_dev __rcu *x360w_dev;
Dominic Cerquettideb8ee42006-10-10 14:42:48 -0700338 struct usb_device *udev; /* usb device */
Greg Kroah-Hartman8818e412012-05-04 15:32:53 -0700339 struct usb_interface *intf; /* usb interface */
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500340
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -0800341 bool pad_present;
342 bool input_created;
Brian Magnuson99de0912008-04-03 16:19:23 -0400343
Dominic Cerquettideb8ee42006-10-10 14:42:48 -0700344 struct urb *irq_in; /* urb for interrupt in report */
345 unsigned char *idata; /* input data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 dma_addr_t idata_dma;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500347
Jan Kratochvile01a06e2007-05-09 00:27:51 -0400348 struct urb *irq_out; /* urb for interrupt out report */
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800349 bool irq_out_active; /* we must not use an active URB */
Jan Kratochvile01a06e2007-05-09 00:27:51 -0400350 unsigned char *odata; /* output data */
351 dma_addr_t odata_dma;
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800352 spinlock_t odata_lock;
353
354 struct xpad_output_packet out_packets[XPAD_NUM_OUT_PACKETS];
355 int last_out_packet;
Jan Kratochvile01a06e2007-05-09 00:27:51 -0400356
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400357#if defined(CONFIG_JOYSTICK_XPAD_LEDS)
358 struct xpad_led *led;
359#endif
360
361 char phys[64]; /* physical device path */
Dominic Cerquettideb8ee42006-10-10 14:42:48 -0700362
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800363 int mapping; /* map d-pad to buttons or to axes */
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400364 int xtype; /* type of xbox device */
Pavel Rojtberge3b65172015-10-10 10:00:49 -0700365 int pad_nr; /* the order x360 pads were attached */
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -0700366 const char *name; /* name of the device */
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -0800367 struct work_struct work; /* init/remove device from callback */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368};
369
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -0800370static int xpad_init_input(struct usb_xpad *xpad);
371static void xpad_deinit_input(struct usb_xpad *xpad);
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373/*
374 * xpad_process_packet
375 *
376 * Completes a request by converting the data into events for the
377 * input subsystem.
378 *
379 * The used report descriptor was taken from ITO Takayukis website:
380 * http://euc.jp/periphs/xbox-controller.ja.html
381 */
David Howells7d12e782006-10-05 14:55:46 +0100382static void xpad_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500384 struct input_dev *dev = xpad->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Christoph Fritz7beae702010-07-13 09:42:33 -0700386 if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
387 /* left stick */
388 input_report_abs(dev, ABS_X,
389 (__s16) le16_to_cpup((__le16 *)(data + 12)));
390 input_report_abs(dev, ABS_Y,
391 ~(__s16) le16_to_cpup((__le16 *)(data + 14)));
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500392
Christoph Fritz7beae702010-07-13 09:42:33 -0700393 /* right stick */
394 input_report_abs(dev, ABS_RX,
395 (__s16) le16_to_cpup((__le16 *)(data + 16)));
396 input_report_abs(dev, ABS_RY,
397 ~(__s16) le16_to_cpup((__le16 *)(data + 18)));
398 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 /* triggers left/right */
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800401 if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
402 input_report_key(dev, BTN_TL2, data[10]);
403 input_report_key(dev, BTN_TR2, data[11]);
404 } else {
405 input_report_abs(dev, ABS_Z, data[10]);
406 input_report_abs(dev, ABS_RZ, data[11]);
407 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 /* digital pad */
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800410 if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
Christoph Fritz7beae702010-07-13 09:42:33 -0700411 /* dpad as buttons (left, right, up, down) */
412 input_report_key(dev, BTN_TRIGGER_HAPPY1, data[2] & 0x04);
413 input_report_key(dev, BTN_TRIGGER_HAPPY2, data[2] & 0x08);
414 input_report_key(dev, BTN_TRIGGER_HAPPY3, data[2] & 0x01);
415 input_report_key(dev, BTN_TRIGGER_HAPPY4, data[2] & 0x02);
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800416 } else {
417 input_report_abs(dev, ABS_HAT0X,
418 !!(data[2] & 0x08) - !!(data[2] & 0x04));
419 input_report_abs(dev, ABS_HAT0Y,
420 !!(data[2] & 0x02) - !!(data[2] & 0x01));
Dominic Cerquettideb8ee42006-10-10 14:42:48 -0700421 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 /* start/back buttons and stick press left/right */
Dominic Cerquettideb8ee42006-10-10 14:42:48 -0700424 input_report_key(dev, BTN_START, data[2] & 0x10);
Christoph Fritz7beae702010-07-13 09:42:33 -0700425 input_report_key(dev, BTN_SELECT, data[2] & 0x20);
Dominic Cerquettideb8ee42006-10-10 14:42:48 -0700426 input_report_key(dev, BTN_THUMBL, data[2] & 0x40);
427 input_report_key(dev, BTN_THUMBR, data[2] & 0x80);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 /* "analog" buttons A, B, X, Y */
430 input_report_key(dev, BTN_A, data[4]);
431 input_report_key(dev, BTN_B, data[5]);
432 input_report_key(dev, BTN_X, data[6]);
433 input_report_key(dev, BTN_Y, data[7]);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 /* "analog" buttons black, white */
436 input_report_key(dev, BTN_C, data[8]);
437 input_report_key(dev, BTN_Z, data[9]);
438
439 input_sync(dev);
440}
441
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400442/*
443 * xpad360_process_packet
444 *
445 * Completes a request by converting the data into events for the
446 * input subsystem. It is version for xbox 360 controller
447 *
448 * The used report descriptor was taken from:
449 * http://www.free60.org/wiki/Gamepad
450 */
451
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -0800452static void xpad360_process_packet(struct usb_xpad *xpad, struct input_dev *dev,
Dmitry Torokhov20b3cdd2007-07-18 01:20:34 -0400453 u16 cmd, unsigned char *data)
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400454{
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400455 /* digital pad */
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800456 if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
Christoph Fritz7beae702010-07-13 09:42:33 -0700457 /* dpad as buttons (left, right, up, down) */
458 input_report_key(dev, BTN_TRIGGER_HAPPY1, data[2] & 0x04);
459 input_report_key(dev, BTN_TRIGGER_HAPPY2, data[2] & 0x08);
460 input_report_key(dev, BTN_TRIGGER_HAPPY3, data[2] & 0x01);
461 input_report_key(dev, BTN_TRIGGER_HAPPY4, data[2] & 0x02);
Pavel Rojtberg5ee8bda2015-10-10 09:33:52 -0700462 }
463
464 /*
465 * This should be a simple else block. However historically
466 * xbox360w has mapped DPAD to buttons while xbox360 did not. This
467 * made no sense, but now we can not just switch back and have to
468 * support both behaviors.
469 */
470 if (!(xpad->mapping & MAP_DPAD_TO_BUTTONS) ||
471 xpad->xtype == XTYPE_XBOX360W) {
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800472 input_report_abs(dev, ABS_HAT0X,
473 !!(data[2] & 0x08) - !!(data[2] & 0x04));
474 input_report_abs(dev, ABS_HAT0Y,
475 !!(data[2] & 0x02) - !!(data[2] & 0x01));
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400476 }
477
478 /* start/back buttons */
Dmitry Torokhovae91d102007-06-14 23:49:55 -0400479 input_report_key(dev, BTN_START, data[2] & 0x10);
Christoph Fritz7beae702010-07-13 09:42:33 -0700480 input_report_key(dev, BTN_SELECT, data[2] & 0x20);
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400481
482 /* stick press left/right */
483 input_report_key(dev, BTN_THUMBL, data[2] & 0x40);
484 input_report_key(dev, BTN_THUMBR, data[2] & 0x80);
485
486 /* buttons A,B,X,Y,TL,TR and MODE */
Dmitry Torokhovae91d102007-06-14 23:49:55 -0400487 input_report_key(dev, BTN_A, data[3] & 0x10);
488 input_report_key(dev, BTN_B, data[3] & 0x20);
489 input_report_key(dev, BTN_X, data[3] & 0x40);
490 input_report_key(dev, BTN_Y, data[3] & 0x80);
491 input_report_key(dev, BTN_TL, data[3] & 0x01);
492 input_report_key(dev, BTN_TR, data[3] & 0x02);
493 input_report_key(dev, BTN_MODE, data[3] & 0x04);
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400494
Christoph Fritz7beae702010-07-13 09:42:33 -0700495 if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
496 /* left stick */
497 input_report_abs(dev, ABS_X,
498 (__s16) le16_to_cpup((__le16 *)(data + 6)));
499 input_report_abs(dev, ABS_Y,
500 ~(__s16) le16_to_cpup((__le16 *)(data + 8)));
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400501
Christoph Fritz7beae702010-07-13 09:42:33 -0700502 /* right stick */
503 input_report_abs(dev, ABS_RX,
504 (__s16) le16_to_cpup((__le16 *)(data + 10)));
505 input_report_abs(dev, ABS_RY,
506 ~(__s16) le16_to_cpup((__le16 *)(data + 12)));
507 }
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400508
509 /* triggers left/right */
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800510 if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
511 input_report_key(dev, BTN_TL2, data[4]);
512 input_report_key(dev, BTN_TR2, data[5]);
513 } else {
514 input_report_abs(dev, ABS_Z, data[4]);
515 input_report_abs(dev, ABS_RZ, data[5]);
516 }
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400517
518 input_sync(dev);
519}
520
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -0800521static void xpad_presence_work(struct work_struct *work)
522{
523 struct usb_xpad *xpad = container_of(work, struct usb_xpad, work);
524 int error;
525
526 if (xpad->pad_present) {
527 error = xpad_init_input(xpad);
528 if (error) {
529 /* complain only, not much else we can do here */
530 dev_err(&xpad->dev->dev,
531 "unable to init device: %d\n", error);
532 } else {
533 rcu_assign_pointer(xpad->x360w_dev, xpad->dev);
534 }
535 } else {
536 RCU_INIT_POINTER(xpad->x360w_dev, NULL);
537 synchronize_rcu();
538 /*
539 * Now that we are sure xpad360w_process_packet is not
540 * using input device we can get rid of it.
541 */
542 xpad_deinit_input(xpad);
543 }
544}
Pavel Rojtbergcae705b2015-06-22 14:11:30 -0700545
Brian Magnuson99de0912008-04-03 16:19:23 -0400546/*
547 * xpad360w_process_packet
548 *
549 * Completes a request by converting the data into events for the
550 * input subsystem. It is version for xbox 360 wireless controller.
551 *
552 * Byte.Bit
553 * 00.1 - Status change: The controller or headset has connected/disconnected
554 * Bits 01.7 and 01.6 are valid
555 * 01.7 - Controller present
556 * 01.6 - Headset present
557 * 01.1 - Pad state (Bytes 4+) valid
558 *
559 */
Brian Magnuson99de0912008-04-03 16:19:23 -0400560static void xpad360w_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data)
561{
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -0800562 struct input_dev *dev;
563 bool present;
564
Brian Magnuson99de0912008-04-03 16:19:23 -0400565 /* Presence change */
566 if (data[0] & 0x08) {
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -0800567 present = (data[1] & 0x80) != 0;
568
569 if (xpad->pad_present != present) {
570 xpad->pad_present = present;
571 schedule_work(&xpad->work);
572 }
Brian Magnuson99de0912008-04-03 16:19:23 -0400573 }
574
575 /* Valid pad data */
Clement Calmels93a017a2015-12-12 21:20:11 -0800576 if (data[1] != 0x1)
Brian Magnuson99de0912008-04-03 16:19:23 -0400577 return;
578
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -0800579 rcu_read_lock();
580 dev = rcu_dereference(xpad->x360w_dev);
581 if (dev)
582 xpad360_process_packet(xpad, dev, cmd, &data[4]);
583 rcu_read_unlock();
Brian Magnuson99de0912008-04-03 16:19:23 -0400584}
585
Ted Mielczarek1a48ff82014-08-08 11:21:59 -0700586/*
587 * xpadone_process_buttons
588 *
589 * Process a button update packet from an Xbox one controller.
590 */
591static void xpadone_process_buttons(struct usb_xpad *xpad,
592 struct input_dev *dev,
593 unsigned char *data)
594{
595 /* menu/view buttons */
596 input_report_key(dev, BTN_START, data[4] & 0x04);
597 input_report_key(dev, BTN_SELECT, data[4] & 0x08);
598
599 /* buttons A,B,X,Y */
600 input_report_key(dev, BTN_A, data[4] & 0x10);
601 input_report_key(dev, BTN_B, data[4] & 0x20);
602 input_report_key(dev, BTN_X, data[4] & 0x40);
603 input_report_key(dev, BTN_Y, data[4] & 0x80);
604
605 /* digital pad */
606 if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
607 /* dpad as buttons (left, right, up, down) */
608 input_report_key(dev, BTN_TRIGGER_HAPPY1, data[5] & 0x04);
609 input_report_key(dev, BTN_TRIGGER_HAPPY2, data[5] & 0x08);
610 input_report_key(dev, BTN_TRIGGER_HAPPY3, data[5] & 0x01);
611 input_report_key(dev, BTN_TRIGGER_HAPPY4, data[5] & 0x02);
612 } else {
613 input_report_abs(dev, ABS_HAT0X,
614 !!(data[5] & 0x08) - !!(data[5] & 0x04));
615 input_report_abs(dev, ABS_HAT0Y,
616 !!(data[5] & 0x02) - !!(data[5] & 0x01));
617 }
618
619 /* TL/TR */
620 input_report_key(dev, BTN_TL, data[5] & 0x10);
621 input_report_key(dev, BTN_TR, data[5] & 0x20);
622
623 /* stick press left/right */
624 input_report_key(dev, BTN_THUMBL, data[5] & 0x40);
625 input_report_key(dev, BTN_THUMBR, data[5] & 0x80);
626
627 if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
628 /* left stick */
629 input_report_abs(dev, ABS_X,
630 (__s16) le16_to_cpup((__le16 *)(data + 10)));
631 input_report_abs(dev, ABS_Y,
632 ~(__s16) le16_to_cpup((__le16 *)(data + 12)));
633
634 /* right stick */
635 input_report_abs(dev, ABS_RX,
636 (__s16) le16_to_cpup((__le16 *)(data + 14)));
637 input_report_abs(dev, ABS_RY,
638 ~(__s16) le16_to_cpup((__le16 *)(data + 16)));
639 }
640
641 /* triggers left/right */
642 if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
643 input_report_key(dev, BTN_TL2,
644 (__u16) le16_to_cpup((__le16 *)(data + 6)));
645 input_report_key(dev, BTN_TR2,
646 (__u16) le16_to_cpup((__le16 *)(data + 8)));
647 } else {
648 input_report_abs(dev, ABS_Z,
649 (__u16) le16_to_cpup((__le16 *)(data + 6)));
650 input_report_abs(dev, ABS_RZ,
651 (__u16) le16_to_cpup((__le16 *)(data + 8)));
652 }
653
654 input_sync(dev);
655}
656
657/*
658 * xpadone_process_packet
659 *
660 * Completes a request by converting the data into events for the
661 * input subsystem. This version is for the Xbox One controller.
662 *
663 * The report format was gleaned from
664 * https://github.com/kylelemons/xbox/blob/master/xbox.go
665 */
666
667static void xpadone_process_packet(struct usb_xpad *xpad,
668 u16 cmd, unsigned char *data)
669{
670 struct input_dev *dev = xpad->dev;
671
672 switch (data[0]) {
673 case 0x20:
674 xpadone_process_buttons(xpad, dev, data);
675 break;
676
677 case 0x07:
678 /* the xbox button has its own special report */
679 input_report_key(dev, BTN_MODE, data[4] & 0x01);
680 input_sync(dev);
681 break;
682 }
683}
684
David Howells7d12e782006-10-05 14:55:46 +0100685static void xpad_irq_in(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687 struct usb_xpad *xpad = urb->context;
Greg Kroah-Hartman8818e412012-05-04 15:32:53 -0700688 struct device *dev = &xpad->intf->dev;
Oliver Neukum6fc88f532008-04-03 21:40:59 +0200689 int retval, status;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500690
Oliver Neukum6fc88f532008-04-03 21:40:59 +0200691 status = urb->status;
692
693 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 case 0:
695 /* success */
696 break;
697 case -ECONNRESET:
698 case -ENOENT:
699 case -ESHUTDOWN:
700 /* this urb is terminated, clean up */
Greg Kroah-Hartmanc4f0bbc2012-05-01 21:32:59 -0700701 dev_dbg(dev, "%s - urb shutting down with status: %d\n",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400702 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return;
704 default:
Greg Kroah-Hartmanc4f0bbc2012-05-01 21:32:59 -0700705 dev_dbg(dev, "%s - nonzero urb status received: %d\n",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400706 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 goto exit;
708 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500709
Brian Magnuson99de0912008-04-03 16:19:23 -0400710 switch (xpad->xtype) {
711 case XTYPE_XBOX360:
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -0800712 xpad360_process_packet(xpad, xpad->dev, 0, xpad->idata);
Brian Magnuson99de0912008-04-03 16:19:23 -0400713 break;
714 case XTYPE_XBOX360W:
715 xpad360w_process_packet(xpad, 0, xpad->idata);
716 break;
Ted Mielczarek1a48ff82014-08-08 11:21:59 -0700717 case XTYPE_XBOXONE:
718 xpadone_process_packet(xpad, 0, xpad->idata);
719 break;
Brian Magnuson99de0912008-04-03 16:19:23 -0400720 default:
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400721 xpad_process_packet(xpad, 0, xpad->idata);
Brian Magnuson99de0912008-04-03 16:19:23 -0400722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724exit:
Dmitry Torokhovdd38d682010-01-09 00:13:36 -0800725 retval = usb_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (retval)
Greg Kroah-Hartmanc4f0bbc2012-05-01 21:32:59 -0700727 dev_err(dev, "%s - usb_submit_urb failed with result %d\n",
Greg Kroah-Hartman9cb757b2012-04-25 14:48:23 -0700728 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800731/* Callers must hold xpad->odata_lock spinlock */
732static bool xpad_prepare_next_out_packet(struct usb_xpad *xpad)
733{
734 struct xpad_output_packet *pkt, *packet = NULL;
735 int i;
736
737 for (i = 0; i < XPAD_NUM_OUT_PACKETS; i++) {
738 if (++xpad->last_out_packet >= XPAD_NUM_OUT_PACKETS)
739 xpad->last_out_packet = 0;
740
741 pkt = &xpad->out_packets[xpad->last_out_packet];
742 if (pkt->pending) {
743 dev_dbg(&xpad->intf->dev,
744 "%s - found pending output packet %d\n",
745 __func__, xpad->last_out_packet);
746 packet = pkt;
747 break;
748 }
749 }
750
751 if (packet) {
752 memcpy(xpad->odata, packet->data, packet->len);
753 xpad->irq_out->transfer_buffer_length = packet->len;
754 return true;
755 }
756
757 return false;
758}
759
760/* Callers must hold xpad->odata_lock spinlock */
761static int xpad_try_sending_next_out_packet(struct usb_xpad *xpad)
762{
763 int error;
764
765 if (!xpad->irq_out_active && xpad_prepare_next_out_packet(xpad)) {
766 error = usb_submit_urb(xpad->irq_out, GFP_ATOMIC);
767 if (error) {
768 dev_err(&xpad->intf->dev,
769 "%s - usb_submit_urb failed with result %d\n",
770 __func__, error);
771 return -EIO;
772 }
773
774 xpad->irq_out_active = true;
775 }
776
777 return 0;
778}
779
Jan Kratochvile01a06e2007-05-09 00:27:51 -0400780static void xpad_irq_out(struct urb *urb)
781{
Greg Kroah-Hartman9cb757b2012-04-25 14:48:23 -0700782 struct usb_xpad *xpad = urb->context;
Greg Kroah-Hartman8818e412012-05-04 15:32:53 -0700783 struct device *dev = &xpad->intf->dev;
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800784 int status = urb->status;
785 int error;
786 unsigned long flags;
Jan Kratochvile01a06e2007-05-09 00:27:51 -0400787
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800788 spin_lock_irqsave(&xpad->odata_lock, flags);
Oliver Neukum6fc88f532008-04-03 21:40:59 +0200789
790 switch (status) {
Michael Gruber70a6f2e2009-07-12 20:51:36 -0700791 case 0:
Jan Kratochvile01a06e2007-05-09 00:27:51 -0400792 /* success */
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800793 xpad->out_packets[xpad->last_out_packet].pending = false;
794 xpad->irq_out_active = xpad_prepare_next_out_packet(xpad);
795 break;
Michael Gruber70a6f2e2009-07-12 20:51:36 -0700796
797 case -ECONNRESET:
798 case -ENOENT:
799 case -ESHUTDOWN:
800 /* this urb is terminated, clean up */
Greg Kroah-Hartmanc4f0bbc2012-05-01 21:32:59 -0700801 dev_dbg(dev, "%s - urb shutting down with status: %d\n",
802 __func__, status);
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800803 xpad->irq_out_active = false;
804 break;
Michael Gruber70a6f2e2009-07-12 20:51:36 -0700805
806 default:
Greg Kroah-Hartmanc4f0bbc2012-05-01 21:32:59 -0700807 dev_dbg(dev, "%s - nonzero urb status received: %d\n",
808 __func__, status);
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800809 break;
Jan Kratochvile01a06e2007-05-09 00:27:51 -0400810 }
811
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800812 if (xpad->irq_out_active) {
813 error = usb_submit_urb(urb, GFP_ATOMIC);
814 if (error) {
815 dev_err(dev,
816 "%s - usb_submit_urb failed with result %d\n",
817 __func__, error);
818 xpad->irq_out_active = false;
819 }
820 }
821
822 spin_unlock_irqrestore(&xpad->odata_lock, flags);
Jan Kratochvile01a06e2007-05-09 00:27:51 -0400823}
824
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400825static int xpad_init_output(struct usb_interface *intf, struct usb_xpad *xpad)
826{
827 struct usb_endpoint_descriptor *ep_irq_out;
Ted Mielczarek1a48ff82014-08-08 11:21:59 -0700828 int ep_irq_out_idx;
Axel Lin49cc69b2010-11-11 21:39:11 -0800829 int error;
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400830
Chris Moellerb514d4f2011-07-04 19:21:59 -0700831 if (xpad->xtype == XTYPE_UNKNOWN)
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400832 return 0;
833
Daniel Mack997ea582010-04-12 13:17:25 +0200834 xpad->odata = usb_alloc_coherent(xpad->udev, XPAD_PKT_LEN,
835 GFP_KERNEL, &xpad->odata_dma);
Axel Lin49cc69b2010-11-11 21:39:11 -0800836 if (!xpad->odata) {
837 error = -ENOMEM;
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400838 goto fail1;
Axel Lin49cc69b2010-11-11 21:39:11 -0800839 }
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400840
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800841 spin_lock_init(&xpad->odata_lock);
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400842
843 xpad->irq_out = usb_alloc_urb(0, GFP_KERNEL);
Axel Lin49cc69b2010-11-11 21:39:11 -0800844 if (!xpad->irq_out) {
845 error = -ENOMEM;
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400846 goto fail2;
Axel Lin49cc69b2010-11-11 21:39:11 -0800847 }
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400848
Ted Mielczarek1a48ff82014-08-08 11:21:59 -0700849 /* Xbox One controller has in/out endpoints swapped. */
850 ep_irq_out_idx = xpad->xtype == XTYPE_XBOXONE ? 0 : 1;
851 ep_irq_out = &intf->cur_altsetting->endpoint[ep_irq_out_idx].desc;
852
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400853 usb_fill_int_urb(xpad->irq_out, xpad->udev,
854 usb_sndintpipe(xpad->udev, ep_irq_out->bEndpointAddress),
855 xpad->odata, XPAD_PKT_LEN,
856 xpad_irq_out, xpad, ep_irq_out->bInterval);
857 xpad->irq_out->transfer_dma = xpad->odata_dma;
858 xpad->irq_out->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
859
860 return 0;
861
Daniel Mack997ea582010-04-12 13:17:25 +0200862 fail2: usb_free_coherent(xpad->udev, XPAD_PKT_LEN, xpad->odata, xpad->odata_dma);
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400863 fail1: return error;
864}
865
866static void xpad_stop_output(struct usb_xpad *xpad)
867{
Chris Moellerb514d4f2011-07-04 19:21:59 -0700868 if (xpad->xtype != XTYPE_UNKNOWN)
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400869 usb_kill_urb(xpad->irq_out);
870}
871
872static void xpad_deinit_output(struct usb_xpad *xpad)
873{
Chris Moellerb514d4f2011-07-04 19:21:59 -0700874 if (xpad->xtype != XTYPE_UNKNOWN) {
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400875 usb_free_urb(xpad->irq_out);
Daniel Mack997ea582010-04-12 13:17:25 +0200876 usb_free_coherent(xpad->udev, XPAD_PKT_LEN,
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400877 xpad->odata, xpad->odata_dma);
878 }
879}
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400880
Pavel Rojtbergaba54872015-10-10 09:36:17 -0700881static int xpad_inquiry_pad_presence(struct usb_xpad *xpad)
882{
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800883 struct xpad_output_packet *packet =
884 &xpad->out_packets[XPAD_OUT_CMD_IDX];
885 unsigned long flags;
Pavel Rojtbergaba54872015-10-10 09:36:17 -0700886 int retval;
887
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800888 spin_lock_irqsave(&xpad->odata_lock, flags);
Pavel Rojtbergaba54872015-10-10 09:36:17 -0700889
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800890 packet->data[0] = 0x08;
891 packet->data[1] = 0x00;
892 packet->data[2] = 0x0F;
893 packet->data[3] = 0xC0;
894 packet->data[4] = 0x00;
895 packet->data[5] = 0x00;
896 packet->data[6] = 0x00;
897 packet->data[7] = 0x00;
898 packet->data[8] = 0x00;
899 packet->data[9] = 0x00;
900 packet->data[10] = 0x00;
901 packet->data[11] = 0x00;
902 packet->len = 12;
903 packet->pending = true;
Pavel Rojtbergaba54872015-10-10 09:36:17 -0700904
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800905 /* Reset the sequence so we send out presence first */
906 xpad->last_out_packet = -1;
907 retval = xpad_try_sending_next_out_packet(xpad);
Pavel Rojtbergaba54872015-10-10 09:36:17 -0700908
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800909 spin_unlock_irqrestore(&xpad->odata_lock, flags);
910
911 return retval;
912}
913
914static int xpad_start_xbox_one(struct usb_xpad *xpad)
915{
916 struct xpad_output_packet *packet =
917 &xpad->out_packets[XPAD_OUT_CMD_IDX];
918 unsigned long flags;
919 int retval;
920
921 spin_lock_irqsave(&xpad->odata_lock, flags);
922
923 /* Xbox one controller needs to be initialized. */
924 packet->data[0] = 0x05;
925 packet->data[1] = 0x20;
926 packet->len = 2;
927 packet->pending = true;
928
929 /* Reset the sequence so we send out start packet first */
930 xpad->last_out_packet = -1;
931 retval = xpad_try_sending_next_out_packet(xpad);
932
933 spin_unlock_irqrestore(&xpad->odata_lock, flags);
Pavel Rojtbergaba54872015-10-10 09:36:17 -0700934
935 return retval;
936}
937
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400938#ifdef CONFIG_JOYSTICK_XPAD_FF
Benjamin Valentin12187302010-01-21 20:19:06 -0800939static int xpad_play_effect(struct input_dev *dev, void *data, struct ff_effect *effect)
Jan Kratochvile01a06e2007-05-09 00:27:51 -0400940{
941 struct usb_xpad *xpad = input_get_drvdata(dev);
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800942 struct xpad_output_packet *packet = &xpad->out_packets[XPAD_OUT_FF_IDX];
Pavel Rojtberg06008152015-10-10 09:32:55 -0700943 __u16 strong;
944 __u16 weak;
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800945 int retval;
946 unsigned long flags;
Jan Kratochvile01a06e2007-05-09 00:27:51 -0400947
Pavel Rojtberg06008152015-10-10 09:32:55 -0700948 if (effect->type != FF_RUMBLE)
949 return 0;
Benjamin Valentin12187302010-01-21 20:19:06 -0800950
Pavel Rojtberg06008152015-10-10 09:32:55 -0700951 strong = effect->u.rumble.strong_magnitude;
952 weak = effect->u.rumble.weak_magnitude;
Benjamin Valentin12187302010-01-21 20:19:06 -0800953
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800954 spin_lock_irqsave(&xpad->odata_lock, flags);
955
Pavel Rojtberg06008152015-10-10 09:32:55 -0700956 switch (xpad->xtype) {
957 case XTYPE_XBOX:
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800958 packet->data[0] = 0x00;
959 packet->data[1] = 0x06;
960 packet->data[2] = 0x00;
961 packet->data[3] = strong / 256; /* left actuator */
962 packet->data[4] = 0x00;
963 packet->data[5] = weak / 256; /* right actuator */
964 packet->len = 6;
965 packet->pending = true;
Pavel Rojtberg06008152015-10-10 09:32:55 -0700966 break;
Benjamin Valentin12187302010-01-21 20:19:06 -0800967
Pavel Rojtberg06008152015-10-10 09:32:55 -0700968 case XTYPE_XBOX360:
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800969 packet->data[0] = 0x00;
970 packet->data[1] = 0x08;
971 packet->data[2] = 0x00;
972 packet->data[3] = strong / 256; /* left actuator? */
973 packet->data[4] = weak / 256; /* right actuator? */
974 packet->data[5] = 0x00;
975 packet->data[6] = 0x00;
976 packet->data[7] = 0x00;
977 packet->len = 8;
978 packet->pending = true;
Pavel Rojtberg06008152015-10-10 09:32:55 -0700979 break;
Benjamin Valentin12187302010-01-21 20:19:06 -0800980
Pavel Rojtberg06008152015-10-10 09:32:55 -0700981 case XTYPE_XBOX360W:
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800982 packet->data[0] = 0x00;
983 packet->data[1] = 0x01;
984 packet->data[2] = 0x0F;
985 packet->data[3] = 0xC0;
986 packet->data[4] = 0x00;
987 packet->data[5] = strong / 256;
988 packet->data[6] = weak / 256;
989 packet->data[7] = 0x00;
990 packet->data[8] = 0x00;
991 packet->data[9] = 0x00;
992 packet->data[10] = 0x00;
993 packet->data[11] = 0x00;
994 packet->len = 12;
995 packet->pending = true;
Pavel Rojtberg06008152015-10-10 09:32:55 -0700996 break;
Benjamin Valentin12187302010-01-21 20:19:06 -0800997
Pavel Rojtberg06008152015-10-10 09:32:55 -0700998 case XTYPE_XBOXONE:
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800999 packet->data[0] = 0x09; /* activate rumble */
1000 packet->data[1] = 0x08;
1001 packet->data[2] = 0x00;
1002 packet->data[3] = 0x08; /* continuous effect */
1003 packet->data[4] = 0x00; /* simple rumble mode */
1004 packet->data[5] = 0x03; /* L and R actuator only */
1005 packet->data[6] = 0x00; /* TODO: LT actuator */
1006 packet->data[7] = 0x00; /* TODO: RT actuator */
1007 packet->data[8] = strong / 256; /* left actuator */
1008 packet->data[9] = weak / 256; /* right actuator */
1009 packet->data[10] = 0x80; /* length of pulse */
1010 packet->data[11] = 0x00; /* stop period of pulse */
1011 packet->len = 12;
1012 packet->pending = true;
Pavel Rojtberg06008152015-10-10 09:32:55 -07001013 break;
Benjamin Valentin12187302010-01-21 20:19:06 -08001014
Pavel Rojtberg06008152015-10-10 09:32:55 -07001015 default:
1016 dev_dbg(&xpad->dev->dev,
1017 "%s - rumble command sent to unsupported xpad type: %d\n",
1018 __func__, xpad->xtype);
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001019 retval = -EINVAL;
1020 goto out;
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001021 }
1022
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001023 retval = xpad_try_sending_next_out_packet(xpad);
1024
1025out:
1026 spin_unlock_irqrestore(&xpad->odata_lock, flags);
1027 return retval;
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001028}
1029
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001030static int xpad_init_ff(struct usb_xpad *xpad)
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001031{
Ming-ting Yao Wei06049492015-04-14 16:59:11 -07001032 if (xpad->xtype == XTYPE_UNKNOWN)
Anssi Hannulacfbe2012008-04-03 16:18:57 -04001033 return 0;
1034
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001035 input_set_capability(xpad->dev, EV_FF, FF_RUMBLE);
1036
1037 return input_ff_create_memless(xpad->dev, NULL, xpad_play_effect);
1038}
1039
1040#else
1041static int xpad_init_ff(struct usb_xpad *xpad) { return 0; }
1042#endif
1043
1044#if defined(CONFIG_JOYSTICK_XPAD_LEDS)
1045#include <linux/leds.h>
Pavel Rojtberge3b65172015-10-10 10:00:49 -07001046#include <linux/idr.h>
1047
1048static DEFINE_IDA(xpad_pad_seq);
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001049
1050struct xpad_led {
1051 char name[16];
1052 struct led_classdev led_cdev;
1053 struct usb_xpad *xpad;
1054};
1055
Pierre-Loup A. Griffais75b7f052015-06-22 14:10:36 -07001056/**
Pavel Rojtberg1f6f02b2015-10-06 17:06:16 -07001057 * set the LEDs on Xbox360 / Wireless Controllers
Pierre-Loup A. Griffais75b7f052015-06-22 14:10:36 -07001058 * @param command
1059 * 0: off
1060 * 1: all blink, then previous setting
1061 * 2: 1/top-left blink, then on
1062 * 3: 2/top-right blink, then on
1063 * 4: 3/bottom-left blink, then on
1064 * 5: 4/bottom-right blink, then on
1065 * 6: 1/top-left on
1066 * 7: 2/top-right on
1067 * 8: 3/bottom-left on
1068 * 9: 4/bottom-right on
1069 * 10: rotate
1070 * 11: blink, based on previous setting
1071 * 12: slow blink, based on previous setting
1072 * 13: rotate with two lights
1073 * 14: persistent slow all blink
1074 * 15: blink once, then previous setting
1075 */
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001076static void xpad_send_led_command(struct usb_xpad *xpad, int command)
1077{
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001078 struct xpad_output_packet *packet =
1079 &xpad->out_packets[XPAD_OUT_LED_IDX];
1080 unsigned long flags;
1081
Pierre-Loup A. Griffais75b7f052015-06-22 14:10:36 -07001082 command %= 16;
1083
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001084 spin_lock_irqsave(&xpad->odata_lock, flags);
Pierre-Loup A. Griffais75b7f052015-06-22 14:10:36 -07001085
1086 switch (xpad->xtype) {
1087 case XTYPE_XBOX360:
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001088 packet->data[0] = 0x01;
1089 packet->data[1] = 0x03;
1090 packet->data[2] = command;
1091 packet->len = 3;
1092 packet->pending = true;
Pierre-Loup A. Griffais75b7f052015-06-22 14:10:36 -07001093 break;
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001094
Pierre-Loup A. Griffais75b7f052015-06-22 14:10:36 -07001095 case XTYPE_XBOX360W:
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001096 packet->data[0] = 0x00;
1097 packet->data[1] = 0x00;
1098 packet->data[2] = 0x08;
1099 packet->data[3] = 0x40 + command;
1100 packet->data[4] = 0x00;
1101 packet->data[5] = 0x00;
1102 packet->data[6] = 0x00;
1103 packet->data[7] = 0x00;
1104 packet->data[8] = 0x00;
1105 packet->data[9] = 0x00;
1106 packet->data[10] = 0x00;
1107 packet->data[11] = 0x00;
1108 packet->len = 12;
1109 packet->pending = true;
Pierre-Loup A. Griffais75b7f052015-06-22 14:10:36 -07001110 break;
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001111 }
Pierre-Loup A. Griffais75b7f052015-06-22 14:10:36 -07001112
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001113 xpad_try_sending_next_out_packet(xpad);
1114
1115 spin_unlock_irqrestore(&xpad->odata_lock, flags);
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001116}
1117
Pavel Rojtberg1f6f02b2015-10-06 17:06:16 -07001118/*
1119 * Light up the segment corresponding to the pad number on
1120 * Xbox 360 Controllers.
1121 */
Pavel Rojtbergcae705b2015-06-22 14:11:30 -07001122static void xpad_identify_controller(struct usb_xpad *xpad)
1123{
Pavel Rojtberg1f6f02b2015-10-06 17:06:16 -07001124 xpad_send_led_command(xpad, (xpad->pad_nr % 4) + 2);
Pavel Rojtbergcae705b2015-06-22 14:11:30 -07001125}
1126
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001127static void xpad_led_set(struct led_classdev *led_cdev,
1128 enum led_brightness value)
1129{
1130 struct xpad_led *xpad_led = container_of(led_cdev,
1131 struct xpad_led, led_cdev);
1132
1133 xpad_send_led_command(xpad_led->xpad, value);
1134}
1135
1136static int xpad_led_probe(struct usb_xpad *xpad)
1137{
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001138 struct xpad_led *led;
1139 struct led_classdev *led_cdev;
1140 int error;
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001141
Pierre-Loup A. Griffais75b7f052015-06-22 14:10:36 -07001142 if (xpad->xtype != XTYPE_XBOX360 && xpad->xtype != XTYPE_XBOX360W)
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001143 return 0;
1144
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001145 xpad->led = led = kzalloc(sizeof(struct xpad_led), GFP_KERNEL);
1146 if (!led)
1147 return -ENOMEM;
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001148
Pavel Rojtberge3b65172015-10-10 10:00:49 -07001149 xpad->pad_nr = ida_simple_get(&xpad_pad_seq, 0, 0, GFP_KERNEL);
1150 if (xpad->pad_nr < 0) {
1151 error = xpad->pad_nr;
1152 goto err_free_mem;
1153 }
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001154
Pavel Rojtberge3b65172015-10-10 10:00:49 -07001155 snprintf(led->name, sizeof(led->name), "xpad%d", xpad->pad_nr);
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001156 led->xpad = xpad;
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001157
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001158 led_cdev = &led->led_cdev;
1159 led_cdev->name = led->name;
1160 led_cdev->brightness_set = xpad_led_set;
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001161
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001162 error = led_classdev_register(&xpad->udev->dev, led_cdev);
Pavel Rojtberge3b65172015-10-10 10:00:49 -07001163 if (error)
1164 goto err_free_id;
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001165
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -08001166 xpad_identify_controller(xpad);
Pavel Rojtbergfbe6a312015-10-19 00:06:58 -07001167
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001168 return 0;
Pavel Rojtberge3b65172015-10-10 10:00:49 -07001169
1170err_free_id:
1171 ida_simple_remove(&xpad_pad_seq, xpad->pad_nr);
1172err_free_mem:
1173 kfree(led);
1174 xpad->led = NULL;
1175 return error;
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001176}
1177
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001178static void xpad_led_disconnect(struct usb_xpad *xpad)
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001179{
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001180 struct xpad_led *xpad_led = xpad->led;
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001181
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001182 if (xpad_led) {
1183 led_classdev_unregister(&xpad_led->led_cdev);
Pavel Rojtberge3b65172015-10-10 10:00:49 -07001184 ida_simple_remove(&xpad_pad_seq, xpad->pad_nr);
Axel Lin6ff92a62010-11-11 21:43:17 -08001185 kfree(xpad_led);
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001186 }
1187}
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001188#else
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001189static int xpad_led_probe(struct usb_xpad *xpad) { return 0; }
1190static void xpad_led_disconnect(struct usb_xpad *xpad) { }
Pavel Rojtbergcae705b2015-06-22 14:11:30 -07001191static void xpad_identify_controller(struct usb_xpad *xpad) { }
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001192#endif
1193
1194static int xpad_open(struct input_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -04001196 struct usb_xpad *xpad = input_get_drvdata(dev);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -05001197
Brian Magnuson99de0912008-04-03 16:19:23 -04001198 /* URB was submitted in probe */
Guillermo A. Amaral0d027962012-12-02 23:26:11 -08001199 if (xpad->xtype == XTYPE_XBOX360W)
Brian Magnuson99de0912008-04-03 16:19:23 -04001200 return 0;
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 xpad->irq_in->dev = xpad->udev;
Dmitry Torokhov65cde542005-05-29 02:29:38 -05001203 if (usb_submit_urb(xpad->irq_in, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 return -EIO;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -05001205
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001206 if (xpad->xtype == XTYPE_XBOXONE)
1207 return xpad_start_xbox_one(xpad);
Ted Mielczarek1a48ff82014-08-08 11:21:59 -07001208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 return 0;
1210}
1211
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001212static void xpad_close(struct input_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -04001214 struct usb_xpad *xpad = input_get_drvdata(dev);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -05001215
Axel Lin161feb22010-11-11 21:47:42 -08001216 if (xpad->xtype != XTYPE_XBOX360W)
Brian Magnuson99de0912008-04-03 16:19:23 -04001217 usb_kill_urb(xpad->irq_in);
Axel Lin161feb22010-11-11 21:47:42 -08001218
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001219 xpad_stop_output(xpad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
1221
Dominic Cerquettideb8ee42006-10-10 14:42:48 -07001222static void xpad_set_up_abs(struct input_dev *input_dev, signed short abs)
1223{
Ted Mielczarek1a48ff82014-08-08 11:21:59 -07001224 struct usb_xpad *xpad = input_get_drvdata(input_dev);
Dominic Cerquettideb8ee42006-10-10 14:42:48 -07001225 set_bit(abs, input_dev->absbit);
1226
1227 switch (abs) {
1228 case ABS_X:
1229 case ABS_Y:
1230 case ABS_RX:
1231 case ABS_RY: /* the two sticks */
1232 input_set_abs_params(input_dev, abs, -32768, 32767, 16, 128);
1233 break;
1234 case ABS_Z:
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -08001235 case ABS_RZ: /* the triggers (if mapped to axes) */
Ted Mielczarek1a48ff82014-08-08 11:21:59 -07001236 if (xpad->xtype == XTYPE_XBOXONE)
1237 input_set_abs_params(input_dev, abs, 0, 1023, 0, 0);
1238 else
1239 input_set_abs_params(input_dev, abs, 0, 255, 0, 0);
Dominic Cerquettideb8ee42006-10-10 14:42:48 -07001240 break;
1241 case ABS_HAT0X:
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -08001242 case ABS_HAT0Y: /* the d-pad (only if dpad is mapped to axes */
Dominic Cerquettideb8ee42006-10-10 14:42:48 -07001243 input_set_abs_params(input_dev, abs, -1, 1, 0, 0);
1244 break;
1245 }
1246}
1247
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001248static void xpad_deinit_input(struct usb_xpad *xpad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -08001250 if (xpad->input_created) {
1251 xpad->input_created = false;
1252 xpad_led_disconnect(xpad);
1253 input_unregister_device(xpad->dev);
1254 }
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001255}
1256
1257static int xpad_init_input(struct usb_xpad *xpad)
1258{
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001259 struct input_dev *input_dev;
Axel Lin49cc69b2010-11-11 21:39:11 -08001260 int i, error;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -05001261
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001262 input_dev = input_allocate_device();
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001263 if (!input_dev)
1264 return -ENOMEM;
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -08001265
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001266 xpad->dev = input_dev;
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001267 input_dev->name = xpad->name;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001268 input_dev->phys = xpad->phys;
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001269 usb_to_input_id(xpad->udev, &input_dev->id);
1270 input_dev->dev.parent = &xpad->intf->dev;
Dmitry Torokhov7791bda2007-04-12 01:34:39 -04001271
1272 input_set_drvdata(input_dev, xpad);
1273
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001274 input_dev->open = xpad_open;
1275 input_dev->close = xpad_close;
1276
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001277 __set_bit(EV_KEY, input_dev->evbit);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001278
Christoph Fritz7beae702010-07-13 09:42:33 -07001279 if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001280 __set_bit(EV_ABS, input_dev->evbit);
Christoph Fritz7beae702010-07-13 09:42:33 -07001281 /* set up axes */
1282 for (i = 0; xpad_abs[i] >= 0; i++)
1283 xpad_set_up_abs(input_dev, xpad_abs[i]);
1284 }
1285
1286 /* set up standard buttons */
Anssi Hannulafc55e952008-04-03 16:18:44 -04001287 for (i = 0; xpad_common_btn[i] >= 0; i++)
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -08001288 __set_bit(xpad_common_btn[i], input_dev->keybit);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001289
Christoph Fritz7beae702010-07-13 09:42:33 -07001290 /* set up model-specific ones */
Ted Mielczarek1a48ff82014-08-08 11:21:59 -07001291 if (xpad->xtype == XTYPE_XBOX360 || xpad->xtype == XTYPE_XBOX360W ||
1292 xpad->xtype == XTYPE_XBOXONE) {
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -08001293 for (i = 0; xpad360_btn[i] >= 0; i++)
1294 __set_bit(xpad360_btn[i], input_dev->keybit);
1295 } else {
1296 for (i = 0; xpad_btn[i] >= 0; i++)
1297 __set_bit(xpad_btn[i], input_dev->keybit);
1298 }
1299
1300 if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
1301 for (i = 0; xpad_btn_pad[i] >= 0; i++)
1302 __set_bit(xpad_btn_pad[i], input_dev->keybit);
Pavel Rojtberg5ee8bda2015-10-10 09:33:52 -07001303 }
1304
1305 /*
1306 * This should be a simple else block. However historically
1307 * xbox360w has mapped DPAD to buttons while xbox360 did not. This
1308 * made no sense, but now we can not just switch back and have to
1309 * support both behaviors.
1310 */
1311 if (!(xpad->mapping & MAP_DPAD_TO_BUTTONS) ||
1312 xpad->xtype == XTYPE_XBOX360W) {
Dominic Cerquettideb8ee42006-10-10 14:42:48 -07001313 for (i = 0; xpad_abs_pad[i] >= 0; i++)
Ted Mielczarek1a48ff82014-08-08 11:21:59 -07001314 xpad_set_up_abs(input_dev, xpad_abs_pad[i]);
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -08001315 }
1316
1317 if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
1318 for (i = 0; xpad_btn_triggers[i] >= 0; i++)
1319 __set_bit(xpad_btn_triggers[i], input_dev->keybit);
1320 } else {
1321 for (i = 0; xpad_abs_triggers[i] >= 0; i++)
1322 xpad_set_up_abs(input_dev, xpad_abs_triggers[i]);
1323 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -05001324
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001325 error = xpad_init_ff(xpad);
1326 if (error)
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001327 goto err_free_input;
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001328
1329 error = xpad_led_probe(xpad);
1330 if (error)
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001331 goto err_destroy_ff;
1332
1333 error = input_register_device(xpad->dev);
1334 if (error)
1335 goto err_disconnect_led;
1336
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -08001337 xpad->input_created = true;
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001338 return 0;
1339
1340err_disconnect_led:
1341 xpad_led_disconnect(xpad);
1342err_destroy_ff:
1343 input_ff_destroy(input_dev);
1344err_free_input:
1345 input_free_device(input_dev);
1346 return error;
1347}
1348
1349static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id)
1350{
1351 struct usb_device *udev = interface_to_usbdev(intf);
1352 struct usb_xpad *xpad;
1353 struct usb_endpoint_descriptor *ep_irq_in;
1354 int ep_irq_in_idx;
1355 int i, error;
1356
1357 for (i = 0; xpad_device[i].idVendor; i++) {
1358 if ((le16_to_cpu(udev->descriptor.idVendor) == xpad_device[i].idVendor) &&
1359 (le16_to_cpu(udev->descriptor.idProduct) == xpad_device[i].idProduct))
1360 break;
1361 }
1362
1363 if (xpad_device[i].xtype == XTYPE_XBOXONE &&
1364 intf->cur_altsetting->desc.bInterfaceNumber != 0) {
1365 /*
1366 * The Xbox One controller lists three interfaces all with the
1367 * same interface class, subclass and protocol. Differentiate by
1368 * interface number.
1369 */
1370 return -ENODEV;
1371 }
1372
1373 xpad = kzalloc(sizeof(struct usb_xpad), GFP_KERNEL);
1374 if (!xpad)
1375 return -ENOMEM;
1376
1377 usb_make_path(udev, xpad->phys, sizeof(xpad->phys));
1378 strlcat(xpad->phys, "/input0", sizeof(xpad->phys));
1379
1380 xpad->idata = usb_alloc_coherent(udev, XPAD_PKT_LEN,
1381 GFP_KERNEL, &xpad->idata_dma);
1382 if (!xpad->idata) {
1383 error = -ENOMEM;
1384 goto err_free_mem;
1385 }
1386
1387 xpad->irq_in = usb_alloc_urb(0, GFP_KERNEL);
1388 if (!xpad->irq_in) {
1389 error = -ENOMEM;
1390 goto err_free_idata;
1391 }
1392
1393 xpad->udev = udev;
1394 xpad->intf = intf;
1395 xpad->mapping = xpad_device[i].mapping;
1396 xpad->xtype = xpad_device[i].xtype;
1397 xpad->name = xpad_device[i].name;
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -08001398 INIT_WORK(&xpad->work, xpad_presence_work);
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001399
1400 if (xpad->xtype == XTYPE_UNKNOWN) {
1401 if (intf->cur_altsetting->desc.bInterfaceClass == USB_CLASS_VENDOR_SPEC) {
1402 if (intf->cur_altsetting->desc.bInterfaceProtocol == 129)
1403 xpad->xtype = XTYPE_XBOX360W;
1404 else
1405 xpad->xtype = XTYPE_XBOX360;
1406 } else {
1407 xpad->xtype = XTYPE_XBOX;
1408 }
1409
1410 if (dpad_to_buttons)
1411 xpad->mapping |= MAP_DPAD_TO_BUTTONS;
1412 if (triggers_to_buttons)
1413 xpad->mapping |= MAP_TRIGGERS_TO_BUTTONS;
1414 if (sticks_to_null)
1415 xpad->mapping |= MAP_STICKS_TO_NULL;
1416 }
1417
1418 error = xpad_init_output(intf, xpad);
1419 if (error)
1420 goto err_free_in_urb;
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001421
Ted Mielczarek1a48ff82014-08-08 11:21:59 -07001422 /* Xbox One controller has in/out endpoints swapped. */
1423 ep_irq_in_idx = xpad->xtype == XTYPE_XBOXONE ? 1 : 0;
1424 ep_irq_in = &intf->cur_altsetting->endpoint[ep_irq_in_idx].desc;
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 usb_fill_int_urb(xpad->irq_in, udev,
1427 usb_rcvintpipe(udev, ep_irq_in->bEndpointAddress),
1428 xpad->idata, XPAD_PKT_LEN, xpad_irq_in,
1429 xpad, ep_irq_in->bInterval);
1430 xpad->irq_in->transfer_dma = xpad->idata_dma;
1431 xpad->irq_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -05001432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 usb_set_intfdata(intf, xpad);
Brian Magnuson99de0912008-04-03 16:19:23 -04001434
Brian Magnuson99de0912008-04-03 16:19:23 -04001435 if (xpad->xtype == XTYPE_XBOX360W) {
Brian Magnuson99de0912008-04-03 16:19:23 -04001436 /*
Axel Line3f0f0a2010-11-17 23:59:34 -08001437 * Submit the int URB immediately rather than waiting for open
1438 * because we get status messages from the device whether
1439 * or not any controllers are attached. In fact, it's
1440 * exactly the message that a controller has arrived that
1441 * we're waiting for.
1442 */
1443 xpad->irq_in->dev = xpad->udev;
1444 error = usb_submit_urb(xpad->irq_in, GFP_KERNEL);
1445 if (error)
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -08001446 goto err_deinit_output;
Pavel Rojtbergaba54872015-10-10 09:36:17 -07001447
1448 /*
1449 * Send presence packet.
1450 * This will force the controller to resend connection packets.
1451 * This is useful in the case we activate the module after the
1452 * adapter has been plugged in, as it won't automatically
1453 * send us info about the controllers.
1454 */
1455 error = xpad_inquiry_pad_presence(xpad);
1456 if (error)
1457 goto err_kill_in_urb;
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -08001458 } else {
1459 error = xpad_init_input(xpad);
1460 if (error)
1461 goto err_deinit_output;
Brian Magnuson99de0912008-04-03 16:19:23 -04001462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return 0;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001464
Pavel Rojtbergaba54872015-10-10 09:36:17 -07001465err_kill_in_urb:
1466 usb_kill_urb(xpad->irq_in);
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001467err_deinit_output:
1468 xpad_deinit_output(xpad);
1469err_free_in_urb:
1470 usb_free_urb(xpad->irq_in);
1471err_free_idata:
1472 usb_free_coherent(udev, XPAD_PKT_LEN, xpad->idata, xpad->idata_dma);
1473err_free_mem:
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001474 kfree(xpad);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001475 return error;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
1478
1479static void xpad_disconnect(struct usb_interface *intf)
1480{
1481 struct usb_xpad *xpad = usb_get_intfdata (intf);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -05001482
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -08001483 if (xpad->xtype == XTYPE_XBOX360W)
Dmitry Torokhov2a059152010-11-11 21:52:18 -08001484 usb_kill_urb(xpad->irq_in);
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -08001485
1486 cancel_work_sync(&xpad->work);
1487
1488 xpad_deinit_input(xpad);
Dmitry Torokhov2a059152010-11-11 21:52:18 -08001489
1490 usb_free_urb(xpad->irq_in);
1491 usb_free_coherent(xpad->udev, XPAD_PKT_LEN,
1492 xpad->idata, xpad->idata_dma);
1493
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -08001494 xpad_deinit_output(xpad);
1495
Dmitry Torokhov2a059152010-11-11 21:52:18 -08001496 kfree(xpad);
1497
1498 usb_set_intfdata(intf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
1500
1501static struct usb_driver xpad_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 .name = "xpad",
1503 .probe = xpad_probe,
1504 .disconnect = xpad_disconnect,
1505 .id_table = xpad_table,
1506};
1507
Greg Kroah-Hartman08642e72011-11-18 09:48:31 -08001508module_usb_driver(xpad_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510MODULE_AUTHOR(DRIVER_AUTHOR);
1511MODULE_DESCRIPTION(DRIVER_DESC);
1512MODULE_LICENSE("GPL");