blob: aa6586043a6c367ff2b7c60225d25089306d6820 [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 */
Pierre-Loup A. Griffais2a6d7522015-12-09 13:40:37 -0800351 u8 odata_serial; /* serial number for xbox one protocol */
Jan Kratochvile01a06e2007-05-09 00:27:51 -0400352 dma_addr_t odata_dma;
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800353 spinlock_t odata_lock;
354
355 struct xpad_output_packet out_packets[XPAD_NUM_OUT_PACKETS];
356 int last_out_packet;
Jan Kratochvile01a06e2007-05-09 00:27:51 -0400357
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400358#if defined(CONFIG_JOYSTICK_XPAD_LEDS)
359 struct xpad_led *led;
360#endif
361
362 char phys[64]; /* physical device path */
Dominic Cerquettideb8ee42006-10-10 14:42:48 -0700363
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800364 int mapping; /* map d-pad to buttons or to axes */
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400365 int xtype; /* type of xbox device */
Pavel Rojtberge3b65172015-10-10 10:00:49 -0700366 int pad_nr; /* the order x360 pads were attached */
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -0700367 const char *name; /* name of the device */
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -0800368 struct work_struct work; /* init/remove device from callback */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369};
370
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -0800371static int xpad_init_input(struct usb_xpad *xpad);
372static void xpad_deinit_input(struct usb_xpad *xpad);
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374/*
375 * xpad_process_packet
376 *
377 * Completes a request by converting the data into events for the
378 * input subsystem.
379 *
380 * The used report descriptor was taken from ITO Takayukis website:
381 * http://euc.jp/periphs/xbox-controller.ja.html
382 */
David Howells7d12e782006-10-05 14:55:46 +0100383static void xpad_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500385 struct input_dev *dev = xpad->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Christoph Fritz7beae702010-07-13 09:42:33 -0700387 if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
388 /* left stick */
389 input_report_abs(dev, ABS_X,
390 (__s16) le16_to_cpup((__le16 *)(data + 12)));
391 input_report_abs(dev, ABS_Y,
392 ~(__s16) le16_to_cpup((__le16 *)(data + 14)));
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500393
Christoph Fritz7beae702010-07-13 09:42:33 -0700394 /* right stick */
395 input_report_abs(dev, ABS_RX,
396 (__s16) le16_to_cpup((__le16 *)(data + 16)));
397 input_report_abs(dev, ABS_RY,
398 ~(__s16) le16_to_cpup((__le16 *)(data + 18)));
399 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 /* triggers left/right */
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800402 if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
403 input_report_key(dev, BTN_TL2, data[10]);
404 input_report_key(dev, BTN_TR2, data[11]);
405 } else {
406 input_report_abs(dev, ABS_Z, data[10]);
407 input_report_abs(dev, ABS_RZ, data[11]);
408 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 /* digital pad */
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800411 if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
Christoph Fritz7beae702010-07-13 09:42:33 -0700412 /* dpad as buttons (left, right, up, down) */
413 input_report_key(dev, BTN_TRIGGER_HAPPY1, data[2] & 0x04);
414 input_report_key(dev, BTN_TRIGGER_HAPPY2, data[2] & 0x08);
415 input_report_key(dev, BTN_TRIGGER_HAPPY3, data[2] & 0x01);
416 input_report_key(dev, BTN_TRIGGER_HAPPY4, data[2] & 0x02);
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800417 } else {
418 input_report_abs(dev, ABS_HAT0X,
419 !!(data[2] & 0x08) - !!(data[2] & 0x04));
420 input_report_abs(dev, ABS_HAT0Y,
421 !!(data[2] & 0x02) - !!(data[2] & 0x01));
Dominic Cerquettideb8ee42006-10-10 14:42:48 -0700422 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 /* start/back buttons and stick press left/right */
Dominic Cerquettideb8ee42006-10-10 14:42:48 -0700425 input_report_key(dev, BTN_START, data[2] & 0x10);
Christoph Fritz7beae702010-07-13 09:42:33 -0700426 input_report_key(dev, BTN_SELECT, data[2] & 0x20);
Dominic Cerquettideb8ee42006-10-10 14:42:48 -0700427 input_report_key(dev, BTN_THUMBL, data[2] & 0x40);
428 input_report_key(dev, BTN_THUMBR, data[2] & 0x80);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 /* "analog" buttons A, B, X, Y */
431 input_report_key(dev, BTN_A, data[4]);
432 input_report_key(dev, BTN_B, data[5]);
433 input_report_key(dev, BTN_X, data[6]);
434 input_report_key(dev, BTN_Y, data[7]);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 /* "analog" buttons black, white */
437 input_report_key(dev, BTN_C, data[8]);
438 input_report_key(dev, BTN_Z, data[9]);
439
440 input_sync(dev);
441}
442
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400443/*
444 * xpad360_process_packet
445 *
446 * Completes a request by converting the data into events for the
447 * input subsystem. It is version for xbox 360 controller
448 *
449 * The used report descriptor was taken from:
450 * http://www.free60.org/wiki/Gamepad
451 */
452
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -0800453static void xpad360_process_packet(struct usb_xpad *xpad, struct input_dev *dev,
Dmitry Torokhov20b3cdd2007-07-18 01:20:34 -0400454 u16 cmd, unsigned char *data)
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400455{
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400456 /* digital pad */
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800457 if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
Christoph Fritz7beae702010-07-13 09:42:33 -0700458 /* dpad as buttons (left, right, up, down) */
459 input_report_key(dev, BTN_TRIGGER_HAPPY1, data[2] & 0x04);
460 input_report_key(dev, BTN_TRIGGER_HAPPY2, data[2] & 0x08);
461 input_report_key(dev, BTN_TRIGGER_HAPPY3, data[2] & 0x01);
462 input_report_key(dev, BTN_TRIGGER_HAPPY4, data[2] & 0x02);
Pavel Rojtberg5ee8bda2015-10-10 09:33:52 -0700463 }
464
465 /*
466 * This should be a simple else block. However historically
467 * xbox360w has mapped DPAD to buttons while xbox360 did not. This
468 * made no sense, but now we can not just switch back and have to
469 * support both behaviors.
470 */
471 if (!(xpad->mapping & MAP_DPAD_TO_BUTTONS) ||
472 xpad->xtype == XTYPE_XBOX360W) {
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800473 input_report_abs(dev, ABS_HAT0X,
474 !!(data[2] & 0x08) - !!(data[2] & 0x04));
475 input_report_abs(dev, ABS_HAT0Y,
476 !!(data[2] & 0x02) - !!(data[2] & 0x01));
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400477 }
478
479 /* start/back buttons */
Dmitry Torokhovae91d102007-06-14 23:49:55 -0400480 input_report_key(dev, BTN_START, data[2] & 0x10);
Christoph Fritz7beae702010-07-13 09:42:33 -0700481 input_report_key(dev, BTN_SELECT, data[2] & 0x20);
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400482
483 /* stick press left/right */
484 input_report_key(dev, BTN_THUMBL, data[2] & 0x40);
485 input_report_key(dev, BTN_THUMBR, data[2] & 0x80);
486
487 /* buttons A,B,X,Y,TL,TR and MODE */
Dmitry Torokhovae91d102007-06-14 23:49:55 -0400488 input_report_key(dev, BTN_A, data[3] & 0x10);
489 input_report_key(dev, BTN_B, data[3] & 0x20);
490 input_report_key(dev, BTN_X, data[3] & 0x40);
491 input_report_key(dev, BTN_Y, data[3] & 0x80);
492 input_report_key(dev, BTN_TL, data[3] & 0x01);
493 input_report_key(dev, BTN_TR, data[3] & 0x02);
494 input_report_key(dev, BTN_MODE, data[3] & 0x04);
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400495
Christoph Fritz7beae702010-07-13 09:42:33 -0700496 if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
497 /* left stick */
498 input_report_abs(dev, ABS_X,
499 (__s16) le16_to_cpup((__le16 *)(data + 6)));
500 input_report_abs(dev, ABS_Y,
501 ~(__s16) le16_to_cpup((__le16 *)(data + 8)));
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400502
Christoph Fritz7beae702010-07-13 09:42:33 -0700503 /* right stick */
504 input_report_abs(dev, ABS_RX,
505 (__s16) le16_to_cpup((__le16 *)(data + 10)));
506 input_report_abs(dev, ABS_RY,
507 ~(__s16) le16_to_cpup((__le16 *)(data + 12)));
508 }
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400509
510 /* triggers left/right */
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -0800511 if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
512 input_report_key(dev, BTN_TL2, data[4]);
513 input_report_key(dev, BTN_TR2, data[5]);
514 } else {
515 input_report_abs(dev, ABS_Z, data[4]);
516 input_report_abs(dev, ABS_RZ, data[5]);
517 }
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400518
519 input_sync(dev);
520}
521
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -0800522static void xpad_presence_work(struct work_struct *work)
523{
524 struct usb_xpad *xpad = container_of(work, struct usb_xpad, work);
525 int error;
526
527 if (xpad->pad_present) {
528 error = xpad_init_input(xpad);
529 if (error) {
530 /* complain only, not much else we can do here */
531 dev_err(&xpad->dev->dev,
532 "unable to init device: %d\n", error);
533 } else {
534 rcu_assign_pointer(xpad->x360w_dev, xpad->dev);
535 }
536 } else {
537 RCU_INIT_POINTER(xpad->x360w_dev, NULL);
538 synchronize_rcu();
539 /*
540 * Now that we are sure xpad360w_process_packet is not
541 * using input device we can get rid of it.
542 */
543 xpad_deinit_input(xpad);
544 }
545}
Pavel Rojtbergcae705b2015-06-22 14:11:30 -0700546
Brian Magnuson99de0912008-04-03 16:19:23 -0400547/*
548 * xpad360w_process_packet
549 *
550 * Completes a request by converting the data into events for the
551 * input subsystem. It is version for xbox 360 wireless controller.
552 *
553 * Byte.Bit
554 * 00.1 - Status change: The controller or headset has connected/disconnected
555 * Bits 01.7 and 01.6 are valid
556 * 01.7 - Controller present
557 * 01.6 - Headset present
558 * 01.1 - Pad state (Bytes 4+) valid
559 *
560 */
Brian Magnuson99de0912008-04-03 16:19:23 -0400561static void xpad360w_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data)
562{
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -0800563 struct input_dev *dev;
564 bool present;
565
Brian Magnuson99de0912008-04-03 16:19:23 -0400566 /* Presence change */
567 if (data[0] & 0x08) {
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -0800568 present = (data[1] & 0x80) != 0;
569
570 if (xpad->pad_present != present) {
571 xpad->pad_present = present;
572 schedule_work(&xpad->work);
573 }
Brian Magnuson99de0912008-04-03 16:19:23 -0400574 }
575
576 /* Valid pad data */
Clement Calmels93a017a2015-12-12 21:20:11 -0800577 if (data[1] != 0x1)
Brian Magnuson99de0912008-04-03 16:19:23 -0400578 return;
579
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -0800580 rcu_read_lock();
581 dev = rcu_dereference(xpad->x360w_dev);
582 if (dev)
583 xpad360_process_packet(xpad, dev, cmd, &data[4]);
584 rcu_read_unlock();
Brian Magnuson99de0912008-04-03 16:19:23 -0400585}
586
Ted Mielczarek1a48ff82014-08-08 11:21:59 -0700587/*
588 * xpadone_process_buttons
589 *
590 * Process a button update packet from an Xbox one controller.
591 */
592static void xpadone_process_buttons(struct usb_xpad *xpad,
593 struct input_dev *dev,
594 unsigned char *data)
595{
596 /* menu/view buttons */
597 input_report_key(dev, BTN_START, data[4] & 0x04);
598 input_report_key(dev, BTN_SELECT, data[4] & 0x08);
599
600 /* buttons A,B,X,Y */
601 input_report_key(dev, BTN_A, data[4] & 0x10);
602 input_report_key(dev, BTN_B, data[4] & 0x20);
603 input_report_key(dev, BTN_X, data[4] & 0x40);
604 input_report_key(dev, BTN_Y, data[4] & 0x80);
605
606 /* digital pad */
607 if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
608 /* dpad as buttons (left, right, up, down) */
609 input_report_key(dev, BTN_TRIGGER_HAPPY1, data[5] & 0x04);
610 input_report_key(dev, BTN_TRIGGER_HAPPY2, data[5] & 0x08);
611 input_report_key(dev, BTN_TRIGGER_HAPPY3, data[5] & 0x01);
612 input_report_key(dev, BTN_TRIGGER_HAPPY4, data[5] & 0x02);
613 } else {
614 input_report_abs(dev, ABS_HAT0X,
615 !!(data[5] & 0x08) - !!(data[5] & 0x04));
616 input_report_abs(dev, ABS_HAT0Y,
617 !!(data[5] & 0x02) - !!(data[5] & 0x01));
618 }
619
620 /* TL/TR */
621 input_report_key(dev, BTN_TL, data[5] & 0x10);
622 input_report_key(dev, BTN_TR, data[5] & 0x20);
623
624 /* stick press left/right */
625 input_report_key(dev, BTN_THUMBL, data[5] & 0x40);
626 input_report_key(dev, BTN_THUMBR, data[5] & 0x80);
627
628 if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
629 /* left stick */
630 input_report_abs(dev, ABS_X,
631 (__s16) le16_to_cpup((__le16 *)(data + 10)));
632 input_report_abs(dev, ABS_Y,
633 ~(__s16) le16_to_cpup((__le16 *)(data + 12)));
634
635 /* right stick */
636 input_report_abs(dev, ABS_RX,
637 (__s16) le16_to_cpup((__le16 *)(data + 14)));
638 input_report_abs(dev, ABS_RY,
639 ~(__s16) le16_to_cpup((__le16 *)(data + 16)));
640 }
641
642 /* triggers left/right */
643 if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
644 input_report_key(dev, BTN_TL2,
645 (__u16) le16_to_cpup((__le16 *)(data + 6)));
646 input_report_key(dev, BTN_TR2,
647 (__u16) le16_to_cpup((__le16 *)(data + 8)));
648 } else {
649 input_report_abs(dev, ABS_Z,
650 (__u16) le16_to_cpup((__le16 *)(data + 6)));
651 input_report_abs(dev, ABS_RZ,
652 (__u16) le16_to_cpup((__le16 *)(data + 8)));
653 }
654
655 input_sync(dev);
656}
657
658/*
659 * xpadone_process_packet
660 *
661 * Completes a request by converting the data into events for the
662 * input subsystem. This version is for the Xbox One controller.
663 *
664 * The report format was gleaned from
665 * https://github.com/kylelemons/xbox/blob/master/xbox.go
666 */
667
668static void xpadone_process_packet(struct usb_xpad *xpad,
669 u16 cmd, unsigned char *data)
670{
671 struct input_dev *dev = xpad->dev;
672
673 switch (data[0]) {
674 case 0x20:
675 xpadone_process_buttons(xpad, dev, data);
676 break;
677
678 case 0x07:
679 /* the xbox button has its own special report */
680 input_report_key(dev, BTN_MODE, data[4] & 0x01);
681 input_sync(dev);
682 break;
683 }
684}
685
David Howells7d12e782006-10-05 14:55:46 +0100686static void xpad_irq_in(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 struct usb_xpad *xpad = urb->context;
Greg Kroah-Hartman8818e412012-05-04 15:32:53 -0700689 struct device *dev = &xpad->intf->dev;
Oliver Neukum6fc88f532008-04-03 21:40:59 +0200690 int retval, status;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500691
Oliver Neukum6fc88f532008-04-03 21:40:59 +0200692 status = urb->status;
693
694 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 case 0:
696 /* success */
697 break;
698 case -ECONNRESET:
699 case -ENOENT:
700 case -ESHUTDOWN:
701 /* this urb is terminated, clean up */
Greg Kroah-Hartmanc4f0bbc2012-05-01 21:32:59 -0700702 dev_dbg(dev, "%s - urb shutting down with status: %d\n",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400703 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return;
705 default:
Greg Kroah-Hartmanc4f0bbc2012-05-01 21:32:59 -0700706 dev_dbg(dev, "%s - nonzero urb status received: %d\n",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400707 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 goto exit;
709 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500710
Brian Magnuson99de0912008-04-03 16:19:23 -0400711 switch (xpad->xtype) {
712 case XTYPE_XBOX360:
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -0800713 xpad360_process_packet(xpad, xpad->dev, 0, xpad->idata);
Brian Magnuson99de0912008-04-03 16:19:23 -0400714 break;
715 case XTYPE_XBOX360W:
716 xpad360w_process_packet(xpad, 0, xpad->idata);
717 break;
Ted Mielczarek1a48ff82014-08-08 11:21:59 -0700718 case XTYPE_XBOXONE:
719 xpadone_process_packet(xpad, 0, xpad->idata);
720 break;
Brian Magnuson99de0912008-04-03 16:19:23 -0400721 default:
Jan Kratochvilc7d9f7e2007-05-09 00:27:37 -0400722 xpad_process_packet(xpad, 0, xpad->idata);
Brian Magnuson99de0912008-04-03 16:19:23 -0400723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725exit:
Dmitry Torokhovdd38d682010-01-09 00:13:36 -0800726 retval = usb_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (retval)
Greg Kroah-Hartmanc4f0bbc2012-05-01 21:32:59 -0700728 dev_err(dev, "%s - usb_submit_urb failed with result %d\n",
Greg Kroah-Hartman9cb757b2012-04-25 14:48:23 -0700729 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800732/* Callers must hold xpad->odata_lock spinlock */
733static bool xpad_prepare_next_out_packet(struct usb_xpad *xpad)
734{
735 struct xpad_output_packet *pkt, *packet = NULL;
736 int i;
737
738 for (i = 0; i < XPAD_NUM_OUT_PACKETS; i++) {
739 if (++xpad->last_out_packet >= XPAD_NUM_OUT_PACKETS)
740 xpad->last_out_packet = 0;
741
742 pkt = &xpad->out_packets[xpad->last_out_packet];
743 if (pkt->pending) {
744 dev_dbg(&xpad->intf->dev,
745 "%s - found pending output packet %d\n",
746 __func__, xpad->last_out_packet);
747 packet = pkt;
748 break;
749 }
750 }
751
752 if (packet) {
753 memcpy(xpad->odata, packet->data, packet->len);
754 xpad->irq_out->transfer_buffer_length = packet->len;
755 return true;
756 }
757
758 return false;
759}
760
761/* Callers must hold xpad->odata_lock spinlock */
762static int xpad_try_sending_next_out_packet(struct usb_xpad *xpad)
763{
764 int error;
765
766 if (!xpad->irq_out_active && xpad_prepare_next_out_packet(xpad)) {
767 error = usb_submit_urb(xpad->irq_out, GFP_ATOMIC);
768 if (error) {
769 dev_err(&xpad->intf->dev,
770 "%s - usb_submit_urb failed with result %d\n",
771 __func__, error);
772 return -EIO;
773 }
774
775 xpad->irq_out_active = true;
776 }
777
778 return 0;
779}
780
Jan Kratochvile01a06e2007-05-09 00:27:51 -0400781static void xpad_irq_out(struct urb *urb)
782{
Greg Kroah-Hartman9cb757b2012-04-25 14:48:23 -0700783 struct usb_xpad *xpad = urb->context;
Greg Kroah-Hartman8818e412012-05-04 15:32:53 -0700784 struct device *dev = &xpad->intf->dev;
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800785 int status = urb->status;
786 int error;
787 unsigned long flags;
Jan Kratochvile01a06e2007-05-09 00:27:51 -0400788
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800789 spin_lock_irqsave(&xpad->odata_lock, flags);
Oliver Neukum6fc88f532008-04-03 21:40:59 +0200790
791 switch (status) {
Michael Gruber70a6f2e2009-07-12 20:51:36 -0700792 case 0:
Jan Kratochvile01a06e2007-05-09 00:27:51 -0400793 /* success */
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800794 xpad->out_packets[xpad->last_out_packet].pending = false;
795 xpad->irq_out_active = xpad_prepare_next_out_packet(xpad);
796 break;
Michael Gruber70a6f2e2009-07-12 20:51:36 -0700797
798 case -ECONNRESET:
799 case -ENOENT:
800 case -ESHUTDOWN:
801 /* this urb is terminated, clean up */
Greg Kroah-Hartmanc4f0bbc2012-05-01 21:32:59 -0700802 dev_dbg(dev, "%s - urb shutting down with status: %d\n",
803 __func__, status);
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800804 xpad->irq_out_active = false;
805 break;
Michael Gruber70a6f2e2009-07-12 20:51:36 -0700806
807 default:
Greg Kroah-Hartmanc4f0bbc2012-05-01 21:32:59 -0700808 dev_dbg(dev, "%s - nonzero urb status received: %d\n",
809 __func__, status);
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800810 break;
Jan Kratochvile01a06e2007-05-09 00:27:51 -0400811 }
812
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800813 if (xpad->irq_out_active) {
814 error = usb_submit_urb(urb, GFP_ATOMIC);
815 if (error) {
816 dev_err(dev,
817 "%s - usb_submit_urb failed with result %d\n",
818 __func__, error);
819 xpad->irq_out_active = false;
820 }
821 }
822
823 spin_unlock_irqrestore(&xpad->odata_lock, flags);
Jan Kratochvile01a06e2007-05-09 00:27:51 -0400824}
825
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400826static int xpad_init_output(struct usb_interface *intf, struct usb_xpad *xpad)
827{
828 struct usb_endpoint_descriptor *ep_irq_out;
Ted Mielczarek1a48ff82014-08-08 11:21:59 -0700829 int ep_irq_out_idx;
Axel Lin49cc69b2010-11-11 21:39:11 -0800830 int error;
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400831
Chris Moellerb514d4f2011-07-04 19:21:59 -0700832 if (xpad->xtype == XTYPE_UNKNOWN)
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400833 return 0;
834
Daniel Mack997ea582010-04-12 13:17:25 +0200835 xpad->odata = usb_alloc_coherent(xpad->udev, XPAD_PKT_LEN,
836 GFP_KERNEL, &xpad->odata_dma);
Axel Lin49cc69b2010-11-11 21:39:11 -0800837 if (!xpad->odata) {
838 error = -ENOMEM;
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400839 goto fail1;
Axel Lin49cc69b2010-11-11 21:39:11 -0800840 }
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400841
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800842 spin_lock_init(&xpad->odata_lock);
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400843
844 xpad->irq_out = usb_alloc_urb(0, GFP_KERNEL);
Axel Lin49cc69b2010-11-11 21:39:11 -0800845 if (!xpad->irq_out) {
846 error = -ENOMEM;
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400847 goto fail2;
Axel Lin49cc69b2010-11-11 21:39:11 -0800848 }
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400849
Ted Mielczarek1a48ff82014-08-08 11:21:59 -0700850 /* Xbox One controller has in/out endpoints swapped. */
851 ep_irq_out_idx = xpad->xtype == XTYPE_XBOXONE ? 0 : 1;
852 ep_irq_out = &intf->cur_altsetting->endpoint[ep_irq_out_idx].desc;
853
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400854 usb_fill_int_urb(xpad->irq_out, xpad->udev,
855 usb_sndintpipe(xpad->udev, ep_irq_out->bEndpointAddress),
856 xpad->odata, XPAD_PKT_LEN,
857 xpad_irq_out, xpad, ep_irq_out->bInterval);
858 xpad->irq_out->transfer_dma = xpad->odata_dma;
859 xpad->irq_out->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
860
861 return 0;
862
Daniel Mack997ea582010-04-12 13:17:25 +0200863 fail2: usb_free_coherent(xpad->udev, XPAD_PKT_LEN, xpad->odata, xpad->odata_dma);
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400864 fail1: return error;
865}
866
867static void xpad_stop_output(struct usb_xpad *xpad)
868{
Chris Moellerb514d4f2011-07-04 19:21:59 -0700869 if (xpad->xtype != XTYPE_UNKNOWN)
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400870 usb_kill_urb(xpad->irq_out);
871}
872
873static void xpad_deinit_output(struct usb_xpad *xpad)
874{
Chris Moellerb514d4f2011-07-04 19:21:59 -0700875 if (xpad->xtype != XTYPE_UNKNOWN) {
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400876 usb_free_urb(xpad->irq_out);
Daniel Mack997ea582010-04-12 13:17:25 +0200877 usb_free_coherent(xpad->udev, XPAD_PKT_LEN,
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400878 xpad->odata, xpad->odata_dma);
879 }
880}
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400881
Pavel Rojtbergaba54872015-10-10 09:36:17 -0700882static int xpad_inquiry_pad_presence(struct usb_xpad *xpad)
883{
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800884 struct xpad_output_packet *packet =
885 &xpad->out_packets[XPAD_OUT_CMD_IDX];
886 unsigned long flags;
Pavel Rojtbergaba54872015-10-10 09:36:17 -0700887 int retval;
888
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800889 spin_lock_irqsave(&xpad->odata_lock, flags);
Pavel Rojtbergaba54872015-10-10 09:36:17 -0700890
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800891 packet->data[0] = 0x08;
892 packet->data[1] = 0x00;
893 packet->data[2] = 0x0F;
894 packet->data[3] = 0xC0;
895 packet->data[4] = 0x00;
896 packet->data[5] = 0x00;
897 packet->data[6] = 0x00;
898 packet->data[7] = 0x00;
899 packet->data[8] = 0x00;
900 packet->data[9] = 0x00;
901 packet->data[10] = 0x00;
902 packet->data[11] = 0x00;
903 packet->len = 12;
904 packet->pending = true;
Pavel Rojtbergaba54872015-10-10 09:36:17 -0700905
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800906 /* Reset the sequence so we send out presence first */
907 xpad->last_out_packet = -1;
908 retval = xpad_try_sending_next_out_packet(xpad);
Pavel Rojtbergaba54872015-10-10 09:36:17 -0700909
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800910 spin_unlock_irqrestore(&xpad->odata_lock, flags);
911
912 return retval;
913}
914
915static int xpad_start_xbox_one(struct usb_xpad *xpad)
916{
917 struct xpad_output_packet *packet =
918 &xpad->out_packets[XPAD_OUT_CMD_IDX];
919 unsigned long flags;
920 int retval;
921
922 spin_lock_irqsave(&xpad->odata_lock, flags);
923
924 /* Xbox one controller needs to be initialized. */
925 packet->data[0] = 0x05;
926 packet->data[1] = 0x20;
Pierre-Loup A. Griffais2a6d7522015-12-09 13:40:37 -0800927 packet->data[2] = xpad->odata_serial++; /* packet serial */
928 packet->data[3] = 0x01; /* rumble bit enable? */
929 packet->data[4] = 0x00;
930 packet->len = 5;
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800931 packet->pending = true;
932
933 /* Reset the sequence so we send out start packet first */
934 xpad->last_out_packet = -1;
935 retval = xpad_try_sending_next_out_packet(xpad);
936
937 spin_unlock_irqrestore(&xpad->odata_lock, flags);
Pavel Rojtbergaba54872015-10-10 09:36:17 -0700938
939 return retval;
940}
941
Jan Kratochvil4994cd82007-07-18 00:35:40 -0400942#ifdef CONFIG_JOYSTICK_XPAD_FF
Benjamin Valentin12187302010-01-21 20:19:06 -0800943static int xpad_play_effect(struct input_dev *dev, void *data, struct ff_effect *effect)
Jan Kratochvile01a06e2007-05-09 00:27:51 -0400944{
945 struct usb_xpad *xpad = input_get_drvdata(dev);
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800946 struct xpad_output_packet *packet = &xpad->out_packets[XPAD_OUT_FF_IDX];
Pavel Rojtberg06008152015-10-10 09:32:55 -0700947 __u16 strong;
948 __u16 weak;
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800949 int retval;
950 unsigned long flags;
Jan Kratochvile01a06e2007-05-09 00:27:51 -0400951
Pavel Rojtberg06008152015-10-10 09:32:55 -0700952 if (effect->type != FF_RUMBLE)
953 return 0;
Benjamin Valentin12187302010-01-21 20:19:06 -0800954
Pavel Rojtberg06008152015-10-10 09:32:55 -0700955 strong = effect->u.rumble.strong_magnitude;
956 weak = effect->u.rumble.weak_magnitude;
Benjamin Valentin12187302010-01-21 20:19:06 -0800957
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800958 spin_lock_irqsave(&xpad->odata_lock, flags);
959
Pavel Rojtberg06008152015-10-10 09:32:55 -0700960 switch (xpad->xtype) {
961 case XTYPE_XBOX:
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800962 packet->data[0] = 0x00;
963 packet->data[1] = 0x06;
964 packet->data[2] = 0x00;
965 packet->data[3] = strong / 256; /* left actuator */
966 packet->data[4] = 0x00;
967 packet->data[5] = weak / 256; /* right actuator */
968 packet->len = 6;
969 packet->pending = true;
Pavel Rojtberg06008152015-10-10 09:32:55 -0700970 break;
Benjamin Valentin12187302010-01-21 20:19:06 -0800971
Pavel Rojtberg06008152015-10-10 09:32:55 -0700972 case XTYPE_XBOX360:
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800973 packet->data[0] = 0x00;
974 packet->data[1] = 0x08;
975 packet->data[2] = 0x00;
976 packet->data[3] = strong / 256; /* left actuator? */
977 packet->data[4] = weak / 256; /* right actuator? */
978 packet->data[5] = 0x00;
979 packet->data[6] = 0x00;
980 packet->data[7] = 0x00;
981 packet->len = 8;
982 packet->pending = true;
Pavel Rojtberg06008152015-10-10 09:32:55 -0700983 break;
Benjamin Valentin12187302010-01-21 20:19:06 -0800984
Pavel Rojtberg06008152015-10-10 09:32:55 -0700985 case XTYPE_XBOX360W:
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -0800986 packet->data[0] = 0x00;
987 packet->data[1] = 0x01;
988 packet->data[2] = 0x0F;
989 packet->data[3] = 0xC0;
990 packet->data[4] = 0x00;
991 packet->data[5] = strong / 256;
992 packet->data[6] = weak / 256;
993 packet->data[7] = 0x00;
994 packet->data[8] = 0x00;
995 packet->data[9] = 0x00;
996 packet->data[10] = 0x00;
997 packet->data[11] = 0x00;
998 packet->len = 12;
999 packet->pending = true;
Pavel Rojtberg06008152015-10-10 09:32:55 -07001000 break;
Benjamin Valentin12187302010-01-21 20:19:06 -08001001
Pavel Rojtberg06008152015-10-10 09:32:55 -07001002 case XTYPE_XBOXONE:
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001003 packet->data[0] = 0x09; /* activate rumble */
1004 packet->data[1] = 0x08;
Pierre-Loup A. Griffais2a6d7522015-12-09 13:40:37 -08001005 packet->data[2] = xpad->odata_serial++;
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001006 packet->data[3] = 0x08; /* continuous effect */
1007 packet->data[4] = 0x00; /* simple rumble mode */
1008 packet->data[5] = 0x03; /* L and R actuator only */
1009 packet->data[6] = 0x00; /* TODO: LT actuator */
1010 packet->data[7] = 0x00; /* TODO: RT actuator */
Pierre-Loup A. Griffais2a6d7522015-12-09 13:40:37 -08001011 packet->data[8] = strong / 512; /* left actuator */
1012 packet->data[9] = weak / 512; /* right actuator */
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001013 packet->data[10] = 0x80; /* length of pulse */
1014 packet->data[11] = 0x00; /* stop period of pulse */
Pierre-Loup A. Griffais2a6d7522015-12-09 13:40:37 -08001015 packet->data[12] = 0x00;
1016 packet->len = 13;
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001017 packet->pending = true;
Pavel Rojtberg06008152015-10-10 09:32:55 -07001018 break;
Benjamin Valentin12187302010-01-21 20:19:06 -08001019
Pavel Rojtberg06008152015-10-10 09:32:55 -07001020 default:
1021 dev_dbg(&xpad->dev->dev,
1022 "%s - rumble command sent to unsupported xpad type: %d\n",
1023 __func__, xpad->xtype);
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001024 retval = -EINVAL;
1025 goto out;
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001026 }
1027
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001028 retval = xpad_try_sending_next_out_packet(xpad);
1029
1030out:
1031 spin_unlock_irqrestore(&xpad->odata_lock, flags);
1032 return retval;
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001033}
1034
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001035static int xpad_init_ff(struct usb_xpad *xpad)
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001036{
Ming-ting Yao Wei06049492015-04-14 16:59:11 -07001037 if (xpad->xtype == XTYPE_UNKNOWN)
Anssi Hannulacfbe2012008-04-03 16:18:57 -04001038 return 0;
1039
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001040 input_set_capability(xpad->dev, EV_FF, FF_RUMBLE);
1041
1042 return input_ff_create_memless(xpad->dev, NULL, xpad_play_effect);
1043}
1044
1045#else
1046static int xpad_init_ff(struct usb_xpad *xpad) { return 0; }
1047#endif
1048
1049#if defined(CONFIG_JOYSTICK_XPAD_LEDS)
1050#include <linux/leds.h>
Pavel Rojtberge3b65172015-10-10 10:00:49 -07001051#include <linux/idr.h>
1052
1053static DEFINE_IDA(xpad_pad_seq);
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001054
1055struct xpad_led {
1056 char name[16];
1057 struct led_classdev led_cdev;
1058 struct usb_xpad *xpad;
1059};
1060
Pierre-Loup A. Griffais75b7f052015-06-22 14:10:36 -07001061/**
Pavel Rojtberg1f6f02b2015-10-06 17:06:16 -07001062 * set the LEDs on Xbox360 / Wireless Controllers
Pierre-Loup A. Griffais75b7f052015-06-22 14:10:36 -07001063 * @param command
1064 * 0: off
1065 * 1: all blink, then previous setting
1066 * 2: 1/top-left blink, then on
1067 * 3: 2/top-right blink, then on
1068 * 4: 3/bottom-left blink, then on
1069 * 5: 4/bottom-right blink, then on
1070 * 6: 1/top-left on
1071 * 7: 2/top-right on
1072 * 8: 3/bottom-left on
1073 * 9: 4/bottom-right on
1074 * 10: rotate
1075 * 11: blink, based on previous setting
1076 * 12: slow blink, based on previous setting
1077 * 13: rotate with two lights
1078 * 14: persistent slow all blink
1079 * 15: blink once, then previous setting
1080 */
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001081static void xpad_send_led_command(struct usb_xpad *xpad, int command)
1082{
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001083 struct xpad_output_packet *packet =
1084 &xpad->out_packets[XPAD_OUT_LED_IDX];
1085 unsigned long flags;
1086
Pierre-Loup A. Griffais75b7f052015-06-22 14:10:36 -07001087 command %= 16;
1088
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001089 spin_lock_irqsave(&xpad->odata_lock, flags);
Pierre-Loup A. Griffais75b7f052015-06-22 14:10:36 -07001090
1091 switch (xpad->xtype) {
1092 case XTYPE_XBOX360:
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001093 packet->data[0] = 0x01;
1094 packet->data[1] = 0x03;
1095 packet->data[2] = command;
1096 packet->len = 3;
1097 packet->pending = true;
Pierre-Loup A. Griffais75b7f052015-06-22 14:10:36 -07001098 break;
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001099
Pierre-Loup A. Griffais75b7f052015-06-22 14:10:36 -07001100 case XTYPE_XBOX360W:
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001101 packet->data[0] = 0x00;
1102 packet->data[1] = 0x00;
1103 packet->data[2] = 0x08;
1104 packet->data[3] = 0x40 + command;
1105 packet->data[4] = 0x00;
1106 packet->data[5] = 0x00;
1107 packet->data[6] = 0x00;
1108 packet->data[7] = 0x00;
1109 packet->data[8] = 0x00;
1110 packet->data[9] = 0x00;
1111 packet->data[10] = 0x00;
1112 packet->data[11] = 0x00;
1113 packet->len = 12;
1114 packet->pending = true;
Pierre-Loup A. Griffais75b7f052015-06-22 14:10:36 -07001115 break;
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001116 }
Pierre-Loup A. Griffais75b7f052015-06-22 14:10:36 -07001117
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001118 xpad_try_sending_next_out_packet(xpad);
1119
1120 spin_unlock_irqrestore(&xpad->odata_lock, flags);
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001121}
1122
Pavel Rojtberg1f6f02b2015-10-06 17:06:16 -07001123/*
1124 * Light up the segment corresponding to the pad number on
1125 * Xbox 360 Controllers.
1126 */
Pavel Rojtbergcae705b2015-06-22 14:11:30 -07001127static void xpad_identify_controller(struct usb_xpad *xpad)
1128{
Pavel Rojtberg1f6f02b2015-10-06 17:06:16 -07001129 xpad_send_led_command(xpad, (xpad->pad_nr % 4) + 2);
Pavel Rojtbergcae705b2015-06-22 14:11:30 -07001130}
1131
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001132static void xpad_led_set(struct led_classdev *led_cdev,
1133 enum led_brightness value)
1134{
1135 struct xpad_led *xpad_led = container_of(led_cdev,
1136 struct xpad_led, led_cdev);
1137
1138 xpad_send_led_command(xpad_led->xpad, value);
1139}
1140
1141static int xpad_led_probe(struct usb_xpad *xpad)
1142{
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001143 struct xpad_led *led;
1144 struct led_classdev *led_cdev;
1145 int error;
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001146
Pierre-Loup A. Griffais75b7f052015-06-22 14:10:36 -07001147 if (xpad->xtype != XTYPE_XBOX360 && xpad->xtype != XTYPE_XBOX360W)
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001148 return 0;
1149
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001150 xpad->led = led = kzalloc(sizeof(struct xpad_led), GFP_KERNEL);
1151 if (!led)
1152 return -ENOMEM;
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001153
Pavel Rojtberge3b65172015-10-10 10:00:49 -07001154 xpad->pad_nr = ida_simple_get(&xpad_pad_seq, 0, 0, GFP_KERNEL);
1155 if (xpad->pad_nr < 0) {
1156 error = xpad->pad_nr;
1157 goto err_free_mem;
1158 }
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001159
Pavel Rojtberge3b65172015-10-10 10:00:49 -07001160 snprintf(led->name, sizeof(led->name), "xpad%d", xpad->pad_nr);
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001161 led->xpad = xpad;
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001162
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001163 led_cdev = &led->led_cdev;
1164 led_cdev->name = led->name;
1165 led_cdev->brightness_set = xpad_led_set;
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001166
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001167 error = led_classdev_register(&xpad->udev->dev, led_cdev);
Pavel Rojtberge3b65172015-10-10 10:00:49 -07001168 if (error)
1169 goto err_free_id;
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001170
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -08001171 xpad_identify_controller(xpad);
Pavel Rojtbergfbe6a312015-10-19 00:06:58 -07001172
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001173 return 0;
Pavel Rojtberge3b65172015-10-10 10:00:49 -07001174
1175err_free_id:
1176 ida_simple_remove(&xpad_pad_seq, xpad->pad_nr);
1177err_free_mem:
1178 kfree(led);
1179 xpad->led = NULL;
1180 return error;
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001181}
1182
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001183static void xpad_led_disconnect(struct usb_xpad *xpad)
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001184{
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001185 struct xpad_led *xpad_led = xpad->led;
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001186
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001187 if (xpad_led) {
1188 led_classdev_unregister(&xpad_led->led_cdev);
Pavel Rojtberge3b65172015-10-10 10:00:49 -07001189 ida_simple_remove(&xpad_pad_seq, xpad->pad_nr);
Axel Lin6ff92a62010-11-11 21:43:17 -08001190 kfree(xpad_led);
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001191 }
1192}
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001193#else
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001194static int xpad_led_probe(struct usb_xpad *xpad) { return 0; }
1195static void xpad_led_disconnect(struct usb_xpad *xpad) { }
Pavel Rojtbergcae705b2015-06-22 14:11:30 -07001196static void xpad_identify_controller(struct usb_xpad *xpad) { }
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001197#endif
1198
1199static int xpad_open(struct input_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -04001201 struct usb_xpad *xpad = input_get_drvdata(dev);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -05001202
Brian Magnuson99de0912008-04-03 16:19:23 -04001203 /* URB was submitted in probe */
Guillermo A. Amaral0d027962012-12-02 23:26:11 -08001204 if (xpad->xtype == XTYPE_XBOX360W)
Brian Magnuson99de0912008-04-03 16:19:23 -04001205 return 0;
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 xpad->irq_in->dev = xpad->udev;
Dmitry Torokhov65cde542005-05-29 02:29:38 -05001208 if (usb_submit_urb(xpad->irq_in, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 return -EIO;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -05001210
Pavel Rojtberg7fc595f2015-12-09 11:57:01 -08001211 if (xpad->xtype == XTYPE_XBOXONE)
1212 return xpad_start_xbox_one(xpad);
Ted Mielczarek1a48ff82014-08-08 11:21:59 -07001213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 return 0;
1215}
1216
Jan Kratochvile01a06e2007-05-09 00:27:51 -04001217static void xpad_close(struct input_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -04001219 struct usb_xpad *xpad = input_get_drvdata(dev);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -05001220
Axel Lin161feb22010-11-11 21:47:42 -08001221 if (xpad->xtype != XTYPE_XBOX360W)
Brian Magnuson99de0912008-04-03 16:19:23 -04001222 usb_kill_urb(xpad->irq_in);
Axel Lin161feb22010-11-11 21:47:42 -08001223
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001224 xpad_stop_output(xpad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
Dominic Cerquettideb8ee42006-10-10 14:42:48 -07001227static void xpad_set_up_abs(struct input_dev *input_dev, signed short abs)
1228{
Ted Mielczarek1a48ff82014-08-08 11:21:59 -07001229 struct usb_xpad *xpad = input_get_drvdata(input_dev);
Dominic Cerquettideb8ee42006-10-10 14:42:48 -07001230 set_bit(abs, input_dev->absbit);
1231
1232 switch (abs) {
1233 case ABS_X:
1234 case ABS_Y:
1235 case ABS_RX:
1236 case ABS_RY: /* the two sticks */
1237 input_set_abs_params(input_dev, abs, -32768, 32767, 16, 128);
1238 break;
1239 case ABS_Z:
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -08001240 case ABS_RZ: /* the triggers (if mapped to axes) */
Ted Mielczarek1a48ff82014-08-08 11:21:59 -07001241 if (xpad->xtype == XTYPE_XBOXONE)
1242 input_set_abs_params(input_dev, abs, 0, 1023, 0, 0);
1243 else
1244 input_set_abs_params(input_dev, abs, 0, 255, 0, 0);
Dominic Cerquettideb8ee42006-10-10 14:42:48 -07001245 break;
1246 case ABS_HAT0X:
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -08001247 case ABS_HAT0Y: /* the d-pad (only if dpad is mapped to axes */
Dominic Cerquettideb8ee42006-10-10 14:42:48 -07001248 input_set_abs_params(input_dev, abs, -1, 1, 0, 0);
1249 break;
1250 }
1251}
1252
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001253static void xpad_deinit_input(struct usb_xpad *xpad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -08001255 if (xpad->input_created) {
1256 xpad->input_created = false;
1257 xpad_led_disconnect(xpad);
1258 input_unregister_device(xpad->dev);
1259 }
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001260}
1261
1262static int xpad_init_input(struct usb_xpad *xpad)
1263{
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001264 struct input_dev *input_dev;
Axel Lin49cc69b2010-11-11 21:39:11 -08001265 int i, error;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -05001266
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001267 input_dev = input_allocate_device();
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001268 if (!input_dev)
1269 return -ENOMEM;
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -08001270
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001271 xpad->dev = input_dev;
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001272 input_dev->name = xpad->name;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001273 input_dev->phys = xpad->phys;
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001274 usb_to_input_id(xpad->udev, &input_dev->id);
1275 input_dev->dev.parent = &xpad->intf->dev;
Dmitry Torokhov7791bda2007-04-12 01:34:39 -04001276
1277 input_set_drvdata(input_dev, xpad);
1278
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001279 input_dev->open = xpad_open;
1280 input_dev->close = xpad_close;
1281
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001282 __set_bit(EV_KEY, input_dev->evbit);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001283
Christoph Fritz7beae702010-07-13 09:42:33 -07001284 if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001285 __set_bit(EV_ABS, input_dev->evbit);
Christoph Fritz7beae702010-07-13 09:42:33 -07001286 /* set up axes */
1287 for (i = 0; xpad_abs[i] >= 0; i++)
1288 xpad_set_up_abs(input_dev, xpad_abs[i]);
1289 }
1290
1291 /* set up standard buttons */
Anssi Hannulafc55e952008-04-03 16:18:44 -04001292 for (i = 0; xpad_common_btn[i] >= 0; i++)
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -08001293 __set_bit(xpad_common_btn[i], input_dev->keybit);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001294
Christoph Fritz7beae702010-07-13 09:42:33 -07001295 /* set up model-specific ones */
Ted Mielczarek1a48ff82014-08-08 11:21:59 -07001296 if (xpad->xtype == XTYPE_XBOX360 || xpad->xtype == XTYPE_XBOX360W ||
1297 xpad->xtype == XTYPE_XBOXONE) {
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -08001298 for (i = 0; xpad360_btn[i] >= 0; i++)
1299 __set_bit(xpad360_btn[i], input_dev->keybit);
1300 } else {
1301 for (i = 0; xpad_btn[i] >= 0; i++)
1302 __set_bit(xpad_btn[i], input_dev->keybit);
1303 }
1304
1305 if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
1306 for (i = 0; xpad_btn_pad[i] >= 0; i++)
1307 __set_bit(xpad_btn_pad[i], input_dev->keybit);
Pavel Rojtberg5ee8bda2015-10-10 09:33:52 -07001308 }
1309
1310 /*
1311 * This should be a simple else block. However historically
1312 * xbox360w has mapped DPAD to buttons while xbox360 did not. This
1313 * made no sense, but now we can not just switch back and have to
1314 * support both behaviors.
1315 */
1316 if (!(xpad->mapping & MAP_DPAD_TO_BUTTONS) ||
1317 xpad->xtype == XTYPE_XBOX360W) {
Dominic Cerquettideb8ee42006-10-10 14:42:48 -07001318 for (i = 0; xpad_abs_pad[i] >= 0; i++)
Ted Mielczarek1a48ff82014-08-08 11:21:59 -07001319 xpad_set_up_abs(input_dev, xpad_abs_pad[i]);
Nicolas Léveilléb45d44e2009-12-29 20:39:05 -08001320 }
1321
1322 if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
1323 for (i = 0; xpad_btn_triggers[i] >= 0; i++)
1324 __set_bit(xpad_btn_triggers[i], input_dev->keybit);
1325 } else {
1326 for (i = 0; xpad_abs_triggers[i] >= 0; i++)
1327 xpad_set_up_abs(input_dev, xpad_abs_triggers[i]);
1328 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -05001329
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001330 error = xpad_init_ff(xpad);
1331 if (error)
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001332 goto err_free_input;
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001333
1334 error = xpad_led_probe(xpad);
1335 if (error)
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001336 goto err_destroy_ff;
1337
1338 error = input_register_device(xpad->dev);
1339 if (error)
1340 goto err_disconnect_led;
1341
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -08001342 xpad->input_created = true;
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001343 return 0;
1344
1345err_disconnect_led:
1346 xpad_led_disconnect(xpad);
1347err_destroy_ff:
1348 input_ff_destroy(input_dev);
1349err_free_input:
1350 input_free_device(input_dev);
1351 return error;
1352}
1353
1354static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id)
1355{
1356 struct usb_device *udev = interface_to_usbdev(intf);
1357 struct usb_xpad *xpad;
1358 struct usb_endpoint_descriptor *ep_irq_in;
1359 int ep_irq_in_idx;
1360 int i, error;
1361
1362 for (i = 0; xpad_device[i].idVendor; i++) {
1363 if ((le16_to_cpu(udev->descriptor.idVendor) == xpad_device[i].idVendor) &&
1364 (le16_to_cpu(udev->descriptor.idProduct) == xpad_device[i].idProduct))
1365 break;
1366 }
1367
1368 if (xpad_device[i].xtype == XTYPE_XBOXONE &&
1369 intf->cur_altsetting->desc.bInterfaceNumber != 0) {
1370 /*
1371 * The Xbox One controller lists three interfaces all with the
1372 * same interface class, subclass and protocol. Differentiate by
1373 * interface number.
1374 */
1375 return -ENODEV;
1376 }
1377
1378 xpad = kzalloc(sizeof(struct usb_xpad), GFP_KERNEL);
1379 if (!xpad)
1380 return -ENOMEM;
1381
1382 usb_make_path(udev, xpad->phys, sizeof(xpad->phys));
1383 strlcat(xpad->phys, "/input0", sizeof(xpad->phys));
1384
1385 xpad->idata = usb_alloc_coherent(udev, XPAD_PKT_LEN,
1386 GFP_KERNEL, &xpad->idata_dma);
1387 if (!xpad->idata) {
1388 error = -ENOMEM;
1389 goto err_free_mem;
1390 }
1391
1392 xpad->irq_in = usb_alloc_urb(0, GFP_KERNEL);
1393 if (!xpad->irq_in) {
1394 error = -ENOMEM;
1395 goto err_free_idata;
1396 }
1397
1398 xpad->udev = udev;
1399 xpad->intf = intf;
1400 xpad->mapping = xpad_device[i].mapping;
1401 xpad->xtype = xpad_device[i].xtype;
1402 xpad->name = xpad_device[i].name;
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -08001403 INIT_WORK(&xpad->work, xpad_presence_work);
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001404
1405 if (xpad->xtype == XTYPE_UNKNOWN) {
1406 if (intf->cur_altsetting->desc.bInterfaceClass == USB_CLASS_VENDOR_SPEC) {
1407 if (intf->cur_altsetting->desc.bInterfaceProtocol == 129)
1408 xpad->xtype = XTYPE_XBOX360W;
1409 else
1410 xpad->xtype = XTYPE_XBOX360;
1411 } else {
1412 xpad->xtype = XTYPE_XBOX;
1413 }
1414
1415 if (dpad_to_buttons)
1416 xpad->mapping |= MAP_DPAD_TO_BUTTONS;
1417 if (triggers_to_buttons)
1418 xpad->mapping |= MAP_TRIGGERS_TO_BUTTONS;
1419 if (sticks_to_null)
1420 xpad->mapping |= MAP_STICKS_TO_NULL;
1421 }
1422
1423 error = xpad_init_output(intf, xpad);
1424 if (error)
1425 goto err_free_in_urb;
Jan Kratochvil4994cd82007-07-18 00:35:40 -04001426
Ted Mielczarek1a48ff82014-08-08 11:21:59 -07001427 /* Xbox One controller has in/out endpoints swapped. */
1428 ep_irq_in_idx = xpad->xtype == XTYPE_XBOXONE ? 1 : 0;
1429 ep_irq_in = &intf->cur_altsetting->endpoint[ep_irq_in_idx].desc;
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 usb_fill_int_urb(xpad->irq_in, udev,
1432 usb_rcvintpipe(udev, ep_irq_in->bEndpointAddress),
1433 xpad->idata, XPAD_PKT_LEN, xpad_irq_in,
1434 xpad, ep_irq_in->bInterval);
1435 xpad->irq_in->transfer_dma = xpad->idata_dma;
1436 xpad->irq_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -05001437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 usb_set_intfdata(intf, xpad);
Brian Magnuson99de0912008-04-03 16:19:23 -04001439
Brian Magnuson99de0912008-04-03 16:19:23 -04001440 if (xpad->xtype == XTYPE_XBOX360W) {
Brian Magnuson99de0912008-04-03 16:19:23 -04001441 /*
Axel Line3f0f0a2010-11-17 23:59:34 -08001442 * Submit the int URB immediately rather than waiting for open
1443 * because we get status messages from the device whether
1444 * or not any controllers are attached. In fact, it's
1445 * exactly the message that a controller has arrived that
1446 * we're waiting for.
1447 */
1448 xpad->irq_in->dev = xpad->udev;
1449 error = usb_submit_urb(xpad->irq_in, GFP_KERNEL);
1450 if (error)
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -08001451 goto err_deinit_output;
Pavel Rojtbergaba54872015-10-10 09:36:17 -07001452
1453 /*
1454 * Send presence packet.
1455 * This will force the controller to resend connection packets.
1456 * This is useful in the case we activate the module after the
1457 * adapter has been plugged in, as it won't automatically
1458 * send us info about the controllers.
1459 */
1460 error = xpad_inquiry_pad_presence(xpad);
1461 if (error)
1462 goto err_kill_in_urb;
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -08001463 } else {
1464 error = xpad_init_input(xpad);
1465 if (error)
1466 goto err_deinit_output;
Brian Magnuson99de0912008-04-03 16:19:23 -04001467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return 0;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001469
Pavel Rojtbergaba54872015-10-10 09:36:17 -07001470err_kill_in_urb:
1471 usb_kill_urb(xpad->irq_in);
Pierre-Loup A. Griffaisb8154002015-10-10 09:34:17 -07001472err_deinit_output:
1473 xpad_deinit_output(xpad);
1474err_free_in_urb:
1475 usb_free_urb(xpad->irq_in);
1476err_free_idata:
1477 usb_free_coherent(udev, XPAD_PKT_LEN, xpad->idata, xpad->idata_dma);
1478err_free_mem:
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001479 kfree(xpad);
Dmitry Torokhov50141862007-04-12 01:33:39 -04001480 return error;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
1484static void xpad_disconnect(struct usb_interface *intf)
1485{
1486 struct usb_xpad *xpad = usb_get_intfdata (intf);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -05001487
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -08001488 if (xpad->xtype == XTYPE_XBOX360W)
Dmitry Torokhov2a059152010-11-11 21:52:18 -08001489 usb_kill_urb(xpad->irq_in);
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -08001490
1491 cancel_work_sync(&xpad->work);
1492
1493 xpad_deinit_input(xpad);
Dmitry Torokhov2a059152010-11-11 21:52:18 -08001494
1495 usb_free_urb(xpad->irq_in);
1496 usb_free_coherent(xpad->udev, XPAD_PKT_LEN,
1497 xpad->idata, xpad->idata_dma);
1498
Pierre-Loup A. Griffais09c8b002015-12-09 11:46:25 -08001499 xpad_deinit_output(xpad);
1500
Dmitry Torokhov2a059152010-11-11 21:52:18 -08001501 kfree(xpad);
1502
1503 usb_set_intfdata(intf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504}
1505
1506static struct usb_driver xpad_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 .name = "xpad",
1508 .probe = xpad_probe,
1509 .disconnect = xpad_disconnect,
1510 .id_table = xpad_table,
1511};
1512
Greg Kroah-Hartman08642e72011-11-18 09:48:31 -08001513module_usb_driver(xpad_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515MODULE_AUTHOR(DRIVER_AUTHOR);
1516MODULE_DESCRIPTION(DRIVER_DESC);
1517MODULE_LICENSE("GPL");