Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Anssi Hannula | bf8cb31 | 2008-04-03 16:19:10 -0400 | [diff] [blame] | 2 | * X-Box gamepad driver |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2002 Marko Friedemann <mfr@bmx-chemnitz.de> |
Dominic Cerquetti | d518b2b | 2006-10-20 14:51:45 -0700 | [diff] [blame] | 5 | * 2004 Oliver Schwartz <Oliver.Schwartz@gmx.de>, |
| 6 | * Steven Toth <steve@toth.demon.co.uk>, |
| 7 | * Franz Lehner <franz@caos.at>, |
| 8 | * Ivan Hawkes <blackhawk@ivanhawkes.com> |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 9 | * 2005 Dominic Cerquetti <binary1230@yahoo.com> |
| 10 | * 2006 Adam Buchbinder <adam.buchbinder@gmail.com> |
Jan Kratochvil | c7d9f7e | 2007-05-09 00:27:37 -0400 | [diff] [blame] | 11 | * 2007 Jan Kratochvil <honza@jikos.cz> |
Christoph Fritz | 7beae70 | 2010-07-13 09:42:33 -0700 | [diff] [blame] | 12 | * 2010 Christoph Fritz <chf.fritz@googlemail.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
| 14 | * This program is free software; you can redistribute it and/or |
| 15 | * modify it under the terms of the GNU General Public License as |
| 16 | * published by the Free Software Foundation; either version 2 of |
| 17 | * the License, or (at your option) any later version. |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with this program; if not, write to the Free Software |
| 26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 27 | * |
| 28 | * |
| 29 | * This driver is based on: |
| 30 | * - information from http://euc.jp/periphs/xbox-controller.ja.html |
| 31 | * - the iForce driver drivers/char/joystick/iforce.c |
| 32 | * - the skeleton-driver drivers/usb/usb-skeleton.c |
Jan Kratochvil | c7d9f7e | 2007-05-09 00:27:37 -0400 | [diff] [blame] | 33 | * - Xbox 360 information http://www.free60.org/wiki/Gamepad |
Ming-ting Yao Wei | 0604949 | 2015-04-14 16:59:11 -0700 | [diff] [blame] | 34 | * - Xbox One information https://github.com/quantus/xbox-one-controller-protocol |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | * |
| 36 | * Thanks to: |
| 37 | * - ITO Takayuki for providing essential xpad information on his website |
| 38 | * - Vojtech Pavlik - iforce driver / input subsystem |
| 39 | * - Greg Kroah-Hartman - usb-skeleton driver |
Dominic Cerquetti | d518b2b | 2006-10-20 14:51:45 -0700 | [diff] [blame] | 40 | * - XBOX Linux project - extra USB id's |
Ming-ting Yao Wei | 0604949 | 2015-04-14 16:59:11 -0700 | [diff] [blame] | 41 | * - Pekka Pöyry (quantus) - Xbox One controller reverse engineering |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | * |
| 43 | * TODO: |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 44 | * - fine tune axes (especially trigger axes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | * - fix "analog" buttons (reported as digital now) |
| 46 | * - get rumble working |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 47 | * - need USB IDs for other dance pads |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | * |
| 49 | * History: |
| 50 | * |
| 51 | * 2002-06-27 - 0.0.1 : first version, just said "XBOX HID controller" |
| 52 | * |
| 53 | * 2002-07-02 - 0.0.2 : basic working version |
| 54 | * - all axes and 9 of the 10 buttons work (german InterAct device) |
| 55 | * - the black button does not work |
| 56 | * |
| 57 | * 2002-07-14 - 0.0.3 : rework by Vojtech Pavlik |
| 58 | * - indentation fixes |
| 59 | * - usb + input init sequence fixes |
| 60 | * |
| 61 | * 2002-07-16 - 0.0.4 : minor changes, merge with Vojtech's v0.0.3 |
| 62 | * - verified the lack of HID and report descriptors |
| 63 | * - verified that ALL buttons WORK |
| 64 | * - fixed d-pad to axes mapping |
| 65 | * |
| 66 | * 2002-07-17 - 0.0.5 : simplified d-pad handling |
Dominic Cerquetti | d518b2b | 2006-10-20 14:51:45 -0700 | [diff] [blame] | 67 | * |
| 68 | * 2004-10-02 - 0.0.6 : DDR pad support |
| 69 | * - borrowed from the XBOX linux kernel |
| 70 | * - USB id's for commonly used dance pads are present |
| 71 | * - dance pads will map D-PAD to buttons, not axes |
| 72 | * - pass the module paramater 'dpad_to_buttons' to force |
| 73 | * the D-PAD to map to buttons if your pad is not detected |
Anssi Hannula | bf8cb31 | 2008-04-03 16:19:10 -0400 | [diff] [blame] | 74 | * |
| 75 | * Later changes can be tracked in SCM. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | */ |
| 77 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | #include <linux/kernel.h> |
Pierre-Loup A. Griffais | 09c8b00 | 2015-12-09 11:46:25 -0800 | [diff] [blame] | 79 | #include <linux/input.h> |
| 80 | #include <linux/rcupdate.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | #include <linux/slab.h> |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 82 | #include <linux/stat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | #include <linux/module.h> |
David Brownell | ae0dadc | 2006-06-13 10:04:34 -0700 | [diff] [blame] | 84 | #include <linux/usb/input.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | #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 Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 91 | /* 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é | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 93 | #define MAP_DPAD_TO_BUTTONS (1 << 0) |
| 94 | #define MAP_TRIGGERS_TO_BUTTONS (1 << 1) |
Christoph Fritz | 7beae70 | 2010-07-13 09:42:33 -0700 | [diff] [blame] | 95 | #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 Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 98 | |
Jan Kratochvil | c7d9f7e | 2007-05-09 00:27:37 -0400 | [diff] [blame] | 99 | #define XTYPE_XBOX 0 |
| 100 | #define XTYPE_XBOX360 1 |
Brian Magnuson | 99de091 | 2008-04-03 16:19:23 -0400 | [diff] [blame] | 101 | #define XTYPE_XBOX360W 2 |
Ted Mielczarek | 1a48ff8 | 2014-08-08 11:21:59 -0700 | [diff] [blame] | 102 | #define XTYPE_XBOXONE 3 |
| 103 | #define XTYPE_UNKNOWN 4 |
Jan Kratochvil | c7d9f7e | 2007-05-09 00:27:37 -0400 | [diff] [blame] | 104 | |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 105 | static bool dpad_to_buttons; |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 106 | module_param(dpad_to_buttons, bool, S_IRUGO); |
| 107 | MODULE_PARM_DESC(dpad_to_buttons, "Map D-PAD to buttons rather than axes for unknown pads"); |
| 108 | |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 109 | static bool triggers_to_buttons; |
Nicolas Léveillé | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 110 | module_param(triggers_to_buttons, bool, S_IRUGO); |
| 111 | MODULE_PARM_DESC(triggers_to_buttons, "Map triggers to buttons rather than axes for unknown pads"); |
| 112 | |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 113 | static bool sticks_to_null; |
Christoph Fritz | 7beae70 | 2010-07-13 09:42:33 -0700 | [diff] [blame] | 114 | module_param(sticks_to_null, bool, S_IRUGO); |
| 115 | MODULE_PARM_DESC(sticks_to_null, "Do not map sticks at all for unknown pads"); |
| 116 | |
Arjan van de Ven | 4c4c943 | 2005-11-29 09:43:42 +0100 | [diff] [blame] | 117 | static const struct xpad_device { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | u16 idVendor; |
| 119 | u16 idProduct; |
| 120 | char *name; |
Nicolas Léveillé | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 121 | u8 mapping; |
Jan Kratochvil | c7d9f7e | 2007-05-09 00:27:37 -0400 | [diff] [blame] | 122 | u8 xtype; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } xpad_device[] = { |
Nicolas Léveillé | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 124 | { 0x045e, 0x0202, "Microsoft X-Box pad v1 (US)", 0, XTYPE_XBOX }, |
Nicolas Léveillé | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 125 | { 0x045e, 0x0285, "Microsoft X-Box pad (Japan)", 0, XTYPE_XBOX }, |
| 126 | { 0x045e, 0x0287, "Microsoft Xbox Controller S", 0, XTYPE_XBOX }, |
Guillermo A. Amaral | 540602a | 2012-12-02 23:26:18 -0800 | [diff] [blame] | 127 | { 0x045e, 0x0289, "Microsoft X-Box pad v2 (US)", 0, XTYPE_XBOX }, |
| 128 | { 0x045e, 0x028e, "Microsoft X-Box 360 pad", 0, XTYPE_XBOX360 }, |
Ted Mielczarek | 1a48ff8 | 2014-08-08 11:21:59 -0700 | [diff] [blame] | 129 | { 0x045e, 0x02d1, "Microsoft X-Box One pad", 0, XTYPE_XBOXONE }, |
Erik Lundgren | 39a7a88 | 2015-10-06 17:04:11 -0700 | [diff] [blame] | 130 | { 0x045e, 0x02dd, "Microsoft X-Box One pad (Covert Forces)", 0, XTYPE_XBOXONE }, |
Guillermo A. Amaral | 540602a | 2012-12-02 23:26:18 -0800 | [diff] [blame] | 131 | { 0x045e, 0x0291, "Xbox 360 Wireless Receiver (XBOX)", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360W }, |
Brian Magnuson | 99de091 | 2008-04-03 16:19:23 -0400 | [diff] [blame] | 132 | { 0x045e, 0x0719, "Xbox 360 Wireless Receiver", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360W }, |
Nicolas Léveillé | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 133 | { 0x044f, 0x0f07, "Thrustmaster, Inc. Controller", 0, XTYPE_XBOX }, |
Benjamin Valentin | f554f61 | 2014-09-08 14:18:40 -0700 | [diff] [blame] | 134 | { 0x044f, 0xb326, "Thrustmaster Gamepad GP XID", 0, XTYPE_XBOX360 }, |
Petr Sebor | 8e2f232 | 2014-01-02 15:35:07 -0800 | [diff] [blame] | 135 | { 0x046d, 0xc21d, "Logitech Gamepad F310", 0, XTYPE_XBOX360 }, |
Benjamin Valentin | f554f61 | 2014-09-08 14:18:40 -0700 | [diff] [blame] | 136 | { 0x046d, 0xc21e, "Logitech Gamepad F510", 0, XTYPE_XBOX360 }, |
Petr Sebor | 8e2f232 | 2014-01-02 15:35:07 -0800 | [diff] [blame] | 137 | { 0x046d, 0xc21f, "Logitech Gamepad F710", 0, XTYPE_XBOX360 }, |
Nicolas Léveillé | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 138 | { 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 Kratochvil | c7d9f7e | 2007-05-09 00:27:37 -0400 | [diff] [blame] | 147 | { 0x0738, 0x4540, "Mad Catz Beat Pad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX }, |
Nicolas Léveillé | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 148 | { 0x0738, 0x4556, "Mad Catz Lynx Wireless Controller", 0, XTYPE_XBOX }, |
| 149 | { 0x0738, 0x4716, "Mad Catz Wired Xbox 360 Controller", 0, XTYPE_XBOX360 }, |
Benjamin Valentin | f554f61 | 2014-09-08 14:18:40 -0700 | [diff] [blame] | 150 | { 0x0738, 0x4718, "Mad Catz Street Fighter IV FightStick SE", 0, XTYPE_XBOX360 }, |
| 151 | { 0x0738, 0x4726, "Mad Catz Xbox 360 Controller", 0, XTYPE_XBOX360 }, |
Shawn Joseph | be66227 | 2013-06-18 23:07:45 -0700 | [diff] [blame] | 152 | { 0x0738, 0x4728, "Mad Catz Street Fighter IV FightPad", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 }, |
Nicolas Léveillé | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 153 | { 0x0738, 0x4738, "Mad Catz Wired Xbox 360 Controller (SFIV)", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 }, |
Benjamin Valentin | f554f61 | 2014-09-08 14:18:40 -0700 | [diff] [blame] | 154 | { 0x0738, 0x4740, "Mad Catz Beat Pad", 0, XTYPE_XBOX360 }, |
Jan Kratochvil | c7d9f7e | 2007-05-09 00:27:37 -0400 | [diff] [blame] | 155 | { 0x0738, 0x6040, "Mad Catz Beat Pad Pro", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX }, |
Benjamin Valentin | f554f61 | 2014-09-08 14:18:40 -0700 | [diff] [blame] | 156 | { 0x0738, 0xb726, "Mad Catz Xbox controller - MW2", 0, XTYPE_XBOX360 }, |
Guillermo A. Amaral | 540602a | 2012-12-02 23:26:18 -0800 | [diff] [blame] | 157 | { 0x0738, 0xbeef, "Mad Catz JOYTECH NEO SE Advanced GamePad", XTYPE_XBOX360 }, |
Benjamin Valentin | f554f61 | 2014-09-08 14:18:40 -0700 | [diff] [blame] | 158 | { 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é | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 161 | { 0x0c12, 0x8802, "Zeroplus Xbox Controller", 0, XTYPE_XBOX }, |
Guillermo A. Amaral | 540602a | 2012-12-02 23:26:18 -0800 | [diff] [blame] | 162 | { 0x0c12, 0x8809, "RedOctane Xbox Dance Pad", DANCEPAD_MAP_CONFIG, XTYPE_XBOX }, |
Nicolas Léveillé | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 163 | { 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 Khan | e76b8ee | 2012-07-11 22:12:31 -0700 | [diff] [blame] | 166 | { 0x0d2f, 0x0002, "Andamiro Pump It Up pad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX }, |
Nicolas Léveillé | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 167 | { 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. Amaral | 540602a | 2012-12-02 23:26:18 -0800 | [diff] [blame] | 172 | { 0x0e6f, 0x0105, "HSM3 Xbox360 dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 }, |
Benjamin Valentin | f554f61 | 2014-09-08 14:18:40 -0700 | [diff] [blame] | 173 | { 0x0e6f, 0x0113, "Afterglow AX.1 Gamepad for Xbox 360", 0, XTYPE_XBOX360 }, |
Christoph Fritz | 6ac8a99 | 2010-08-09 10:08:10 -0700 | [diff] [blame] | 174 | { 0x0e6f, 0x0201, "Pelican PL-3601 'TSZ' Wired Xbox 360 Controller", 0, XTYPE_XBOX360 }, |
Guillermo A. Amaral | 540602a | 2012-12-02 23:26:18 -0800 | [diff] [blame] | 175 | { 0x0e6f, 0x0213, "Afterglow Gamepad for Xbox 360", 0, XTYPE_XBOX360 }, |
Benjamin Valentin | f554f61 | 2014-09-08 14:18:40 -0700 | [diff] [blame] | 176 | { 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é | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 179 | { 0x0e8f, 0x0201, "SmartJoy Frag Xpad/PS2 adaptor", 0, XTYPE_XBOX }, |
Benjamin Valentin | f554f61 | 2014-09-08 14:18:40 -0700 | [diff] [blame] | 180 | { 0x0e8f, 0x3008, "Generic xbox control (dealextreme)", 0, XTYPE_XBOX }, |
| 181 | { 0x0f0d, 0x000a, "Hori Co. DOA4 FightStick", 0, XTYPE_XBOX360 }, |
Guillermo A. Amaral | 540602a | 2012-12-02 23:26:18 -0800 | [diff] [blame] | 182 | { 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é | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 184 | { 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örlin | fabadbc | 2011-06-21 14:40:30 -0700 | [diff] [blame] | 187 | { 0x12ab, 0x0004, "Honey Bee Xbox360 dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 }, |
Benjamin Valentin | f554f61 | 2014-09-08 14:18:40 -0700 | [diff] [blame] | 188 | { 0x12ab, 0x0301, "PDP AFTERGLOW AX.1", 0, XTYPE_XBOX360 }, |
Guillermo A. Amaral | 540602a | 2012-12-02 23:26:18 -0800 | [diff] [blame] | 189 | { 0x12ab, 0x8809, "Xbox DDR dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX }, |
Nicolas Léveillé | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 190 | { 0x1430, 0x4748, "RedOctane Guitar Hero X-plorer", 0, XTYPE_XBOX360 }, |
Jan Kratochvil | c7d9f7e | 2007-05-09 00:27:37 -0400 | [diff] [blame] | 191 | { 0x1430, 0x8888, "TX6500+ Dance Pad (first generation)", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX }, |
Nicolas Léveillé | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 192 | { 0x146b, 0x0601, "BigBen Interactive XBOX 360 Controller", 0, XTYPE_XBOX360 }, |
Benjamin Valentin | f554f61 | 2014-09-08 14:18:40 -0700 | [diff] [blame] | 193 | { 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 Reis | dbb4800 | 2014-01-02 15:38:45 -0800 | [diff] [blame] | 198 | { 0x1689, 0xfd00, "Razer Onza Tournament Edition", 0, XTYPE_XBOX360 }, |
| 199 | { 0x1689, 0xfd01, "Razer Onza Classic Edition", 0, XTYPE_XBOX360 }, |
Frank Razenberg | a7b4473 | 2014-09-08 11:32:20 -0700 | [diff] [blame] | 200 | { 0x24c6, 0x5d04, "Razer Sabertooth", 0, XTYPE_XBOX360 }, |
Magnus Hörlin | fabadbc | 2011-06-21 14:40:30 -0700 | [diff] [blame] | 201 | { 0x1bad, 0x0002, "Harmonix Rock Band Guitar", 0, XTYPE_XBOX360 }, |
Corbin Simpson | 805423e | 2009-08-20 21:41:04 -0700 | [diff] [blame] | 202 | { 0x1bad, 0x0003, "Harmonix Rock Band Drumkit", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 }, |
Guillermo A. Amaral | 540602a | 2012-12-02 23:26:18 -0800 | [diff] [blame] | 203 | { 0x1bad, 0xf016, "Mad Catz Xbox 360 Controller", 0, XTYPE_XBOX360 }, |
Benjamin Valentin | f554f61 | 2014-09-08 14:18:40 -0700 | [diff] [blame] | 204 | { 0x1bad, 0xf023, "MLG Pro Circuit Controller (Xbox)", 0, XTYPE_XBOX360 }, |
Guillermo A. Amaral | 540602a | 2012-12-02 23:26:18 -0800 | [diff] [blame] | 205 | { 0x1bad, 0xf028, "Street Fighter IV FightPad", 0, XTYPE_XBOX360 }, |
Benjamin Valentin | f554f61 | 2014-09-08 14:18:40 -0700 | [diff] [blame] | 206 | { 0x1bad, 0xf038, "Street Fighter IV FightStick TE", 0, XTYPE_XBOX360 }, |
| 207 | { 0x1bad, 0xf900, "Harmonix Xbox 360 Controller", 0, XTYPE_XBOX360 }, |
Guillermo A. Amaral | 540602a | 2012-12-02 23:26:18 -0800 | [diff] [blame] | 208 | { 0x1bad, 0xf901, "Gamestop Xbox 360 Controller", 0, XTYPE_XBOX360 }, |
| 209 | { 0x1bad, 0xf903, "Tron Xbox 360 controller", 0, XTYPE_XBOX360 }, |
Dario Scarpa | 470446e | 2015-10-06 17:04:36 -0700 | [diff] [blame] | 210 | { 0x24c6, 0x5000, "Razer Atrox Arcade Stick", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 }, |
Guillermo A. Amaral | 540602a | 2012-12-02 23:26:18 -0800 | [diff] [blame] | 211 | { 0x24c6, 0x5300, "PowerA MINI PROEX Controller", 0, XTYPE_XBOX360 }, |
Benjamin Valentin | f554f61 | 2014-09-08 14:18:40 -0700 | [diff] [blame] | 212 | { 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 Rantala | 4b54625 | 2014-10-16 14:01:43 -0700 | [diff] [blame] | 217 | { 0x24c6, 0x5b03, "Thrustmaster Ferrari 458 Racing Wheel", 0, XTYPE_XBOX360 }, |
Nicolas Léveillé | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 218 | { 0xffff, 0xffff, "Chinese-made Xbox Controller", 0, XTYPE_XBOX }, |
| 219 | { 0x0000, 0x0000, "Generic X-Box pad", 0, XTYPE_UNKNOWN } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | }; |
| 221 | |
Anssi Hannula | fc55e95 | 2008-04-03 16:18:44 -0400 | [diff] [blame] | 222 | /* buttons shared with xbox and xbox360 */ |
| 223 | static const signed short xpad_common_btn[] = { |
| 224 | BTN_A, BTN_B, BTN_X, BTN_Y, /* "analog" buttons */ |
Christoph Fritz | 7beae70 | 2010-07-13 09:42:33 -0700 | [diff] [blame] | 225 | BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR, /* start/back/sticks */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | -1 /* terminating entry */ |
| 227 | }; |
| 228 | |
Anssi Hannula | fc55e95 | 2008-04-03 16:18:44 -0400 | [diff] [blame] | 229 | /* original xbox controllers only */ |
| 230 | static const signed short xpad_btn[] = { |
| 231 | BTN_C, BTN_Z, /* "analog" buttons */ |
| 232 | -1 /* terminating entry */ |
| 233 | }; |
| 234 | |
Christoph Fritz | 7beae70 | 2010-07-13 09:42:33 -0700 | [diff] [blame] | 235 | /* used when dpad is mapped to buttons */ |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 236 | static const signed short xpad_btn_pad[] = { |
Christoph Fritz | 7beae70 | 2010-07-13 09:42:33 -0700 | [diff] [blame] | 237 | BTN_TRIGGER_HAPPY1, BTN_TRIGGER_HAPPY2, /* d-pad left, right */ |
| 238 | BTN_TRIGGER_HAPPY3, BTN_TRIGGER_HAPPY4, /* d-pad up, down */ |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 239 | -1 /* terminating entry */ |
| 240 | }; |
| 241 | |
Nicolas Léveillé | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 242 | /* used when triggers are mapped to buttons */ |
| 243 | static const signed short xpad_btn_triggers[] = { |
| 244 | BTN_TL2, BTN_TR2, /* triggers left/right */ |
| 245 | -1 |
| 246 | }; |
| 247 | |
Jan Kratochvil | c7d9f7e | 2007-05-09 00:27:37 -0400 | [diff] [blame] | 248 | static 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 Ven | 4c4c943 | 2005-11-29 09:43:42 +0100 | [diff] [blame] | 254 | static const signed short xpad_abs[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | ABS_X, ABS_Y, /* left stick */ |
| 256 | ABS_RX, ABS_RY, /* right stick */ |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 257 | -1 /* terminating entry */ |
| 258 | }; |
| 259 | |
Nicolas Léveillé | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 260 | /* used when dpad is mapped to axes */ |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 261 | static const signed short xpad_abs_pad[] = { |
| 262 | ABS_HAT0X, ABS_HAT0Y, /* d-pad axes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | -1 /* terminating entry */ |
| 264 | }; |
| 265 | |
Nicolas Léveillé | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 266 | /* used when triggers are mapped to axes */ |
| 267 | static const signed short xpad_abs_triggers[] = { |
| 268 | ABS_Z, ABS_RZ, /* triggers left/right */ |
| 269 | -1 |
| 270 | }; |
| 271 | |
Ted Mielczarek | 1a48ff8 | 2014-08-08 11:21:59 -0700 | [diff] [blame] | 272 | /* |
| 273 | * Xbox 360 has a vendor-specific class, so we cannot match it with only |
Anssi Hannula | 8a0f83e | 2008-04-03 16:17:52 -0400 | [diff] [blame] | 274 | * USB_INTERFACE_INFO (also specifically refused by USB subsystem), so we |
Brian Magnuson | 99de091 | 2008-04-03 16:19:23 -0400 | [diff] [blame] | 275 | * match against vendor id as well. Wired Xbox 360 devices have protocol 1, |
Ted Mielczarek | 1a48ff8 | 2014-08-08 11:21:59 -0700 | [diff] [blame] | 276 | * wireless controllers have protocol 129. |
| 277 | */ |
Brian Magnuson | 99de091 | 2008-04-03 16:19:23 -0400 | [diff] [blame] | 278 | #define XPAD_XBOX360_VENDOR_PROTOCOL(vend,pr) \ |
Anssi Hannula | 8a0f83e | 2008-04-03 16:17:52 -0400 | [diff] [blame] | 279 | .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 Magnuson | 99de091 | 2008-04-03 16:19:23 -0400 | [diff] [blame] | 283 | .bInterfaceProtocol = (pr) |
| 284 | #define XPAD_XBOX360_VENDOR(vend) \ |
| 285 | { XPAD_XBOX360_VENDOR_PROTOCOL(vend,1) }, \ |
| 286 | { XPAD_XBOX360_VENDOR_PROTOCOL(vend,129) } |
Anssi Hannula | 8a0f83e | 2008-04-03 16:17:52 -0400 | [diff] [blame] | 287 | |
Ted Mielczarek | 1a48ff8 | 2014-08-08 11:21:59 -0700 | [diff] [blame] | 288 | /* 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. Amaral | 0d02796 | 2012-12-02 23:26:11 -0800 | [diff] [blame] | 298 | static struct usb_device_id xpad_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | { USB_INTERFACE_INFO('X', 'B', 0) }, /* X-Box USB-IF not approved class */ |
Tommi Rantala | 4dfb15c | 2014-10-16 14:02:07 -0700 | [diff] [blame] | 300 | XPAD_XBOX360_VENDOR(0x044f), /* Thrustmaster X-Box 360 controllers */ |
Brian Magnuson | 99de091 | 2008-04-03 16:19:23 -0400 | [diff] [blame] | 301 | XPAD_XBOX360_VENDOR(0x045e), /* Microsoft X-Box 360 controllers */ |
Ted Mielczarek | 1a48ff8 | 2014-08-08 11:21:59 -0700 | [diff] [blame] | 302 | XPAD_XBOXONE_VENDOR(0x045e), /* Microsoft X-Box One controllers */ |
Brian Magnuson | 99de091 | 2008-04-03 16:19:23 -0400 | [diff] [blame] | 303 | XPAD_XBOX360_VENDOR(0x046d), /* Logitech X-Box 360 style controllers */ |
| 304 | XPAD_XBOX360_VENDOR(0x0738), /* Mad Catz X-Box 360 controllers */ |
Yuri Khan | 3ffb62c | 2012-07-11 00:49:18 -0700 | [diff] [blame] | 305 | { USB_DEVICE(0x0738, 0x4540) }, /* Mad Catz Beat Pad */ |
Brian Magnuson | 99de091 | 2008-04-03 16:19:23 -0400 | [diff] [blame] | 306 | XPAD_XBOX360_VENDOR(0x0e6f), /* 0x0e6f X-Box 360 controllers */ |
Magnus Hörlin | fabadbc | 2011-06-21 14:40:30 -0700 | [diff] [blame] | 307 | XPAD_XBOX360_VENDOR(0x12ab), /* X-Box 360 dance pads */ |
Brian Magnuson | 99de091 | 2008-04-03 16:19:23 -0400 | [diff] [blame] | 308 | XPAD_XBOX360_VENDOR(0x1430), /* RedOctane X-Box 360 controllers */ |
Thomas Gruber | 3ac91d3 | 2009-10-05 21:43:42 -0700 | [diff] [blame] | 309 | XPAD_XBOX360_VENDOR(0x146b), /* BigBen Interactive Controllers */ |
Magnus Hörlin | fabadbc | 2011-06-21 14:40:30 -0700 | [diff] [blame] | 310 | XPAD_XBOX360_VENDOR(0x1bad), /* Harminix Rock Band Guitar and Drums */ |
Ilia Katsnelson | cc71a7e | 2012-07-11 00:54:20 -0700 | [diff] [blame] | 311 | XPAD_XBOX360_VENDOR(0x0f0d), /* Hori Controllers */ |
| 312 | XPAD_XBOX360_VENDOR(0x1689), /* Razer Onza */ |
Guillermo A. Amaral | 540602a | 2012-12-02 23:26:18 -0800 | [diff] [blame] | 313 | XPAD_XBOX360_VENDOR(0x24c6), /* PowerA Controllers */ |
Benjamin Valentin | f554f61 | 2014-09-08 14:18:40 -0700 | [diff] [blame] | 314 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | { } |
| 318 | }; |
| 319 | |
Guillermo A. Amaral | 0d02796 | 2012-12-02 23:26:11 -0800 | [diff] [blame] | 320 | MODULE_DEVICE_TABLE(usb, xpad_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 322 | struct 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | struct usb_xpad { |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 336 | struct input_dev *dev; /* input device interface */ |
Pierre-Loup A. Griffais | 09c8b00 | 2015-12-09 11:46:25 -0800 | [diff] [blame] | 337 | struct input_dev __rcu *x360w_dev; |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 338 | struct usb_device *udev; /* usb device */ |
Greg Kroah-Hartman | 8818e41 | 2012-05-04 15:32:53 -0700 | [diff] [blame] | 339 | struct usb_interface *intf; /* usb interface */ |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 340 | |
Pierre-Loup A. Griffais | 09c8b00 | 2015-12-09 11:46:25 -0800 | [diff] [blame] | 341 | bool pad_present; |
| 342 | bool input_created; |
Brian Magnuson | 99de091 | 2008-04-03 16:19:23 -0400 | [diff] [blame] | 343 | |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 344 | struct urb *irq_in; /* urb for interrupt in report */ |
| 345 | unsigned char *idata; /* input data */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | dma_addr_t idata_dma; |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 347 | |
Jan Kratochvil | e01a06e | 2007-05-09 00:27:51 -0400 | [diff] [blame] | 348 | struct urb *irq_out; /* urb for interrupt out report */ |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 349 | bool irq_out_active; /* we must not use an active URB */ |
Jan Kratochvil | e01a06e | 2007-05-09 00:27:51 -0400 | [diff] [blame] | 350 | unsigned char *odata; /* output data */ |
Pierre-Loup A. Griffais | 2a6d752 | 2015-12-09 13:40:37 -0800 | [diff] [blame^] | 351 | u8 odata_serial; /* serial number for xbox one protocol */ |
Jan Kratochvil | e01a06e | 2007-05-09 00:27:51 -0400 | [diff] [blame] | 352 | dma_addr_t odata_dma; |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 353 | spinlock_t odata_lock; |
| 354 | |
| 355 | struct xpad_output_packet out_packets[XPAD_NUM_OUT_PACKETS]; |
| 356 | int last_out_packet; |
Jan Kratochvil | e01a06e | 2007-05-09 00:27:51 -0400 | [diff] [blame] | 357 | |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 358 | #if defined(CONFIG_JOYSTICK_XPAD_LEDS) |
| 359 | struct xpad_led *led; |
| 360 | #endif |
| 361 | |
| 362 | char phys[64]; /* physical device path */ |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 363 | |
Nicolas Léveillé | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 364 | int mapping; /* map d-pad to buttons or to axes */ |
Jan Kratochvil | c7d9f7e | 2007-05-09 00:27:37 -0400 | [diff] [blame] | 365 | int xtype; /* type of xbox device */ |
Pavel Rojtberg | e3b6517 | 2015-10-10 10:00:49 -0700 | [diff] [blame] | 366 | int pad_nr; /* the order x360 pads were attached */ |
Pierre-Loup A. Griffais | b815400 | 2015-10-10 09:34:17 -0700 | [diff] [blame] | 367 | const char *name; /* name of the device */ |
Pierre-Loup A. Griffais | 09c8b00 | 2015-12-09 11:46:25 -0800 | [diff] [blame] | 368 | struct work_struct work; /* init/remove device from callback */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | }; |
| 370 | |
Pierre-Loup A. Griffais | 09c8b00 | 2015-12-09 11:46:25 -0800 | [diff] [blame] | 371 | static int xpad_init_input(struct usb_xpad *xpad); |
| 372 | static void xpad_deinit_input(struct usb_xpad *xpad); |
| 373 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | /* |
| 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 Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 383 | static void xpad_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | { |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 385 | struct input_dev *dev = xpad->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
Christoph Fritz | 7beae70 | 2010-07-13 09:42:33 -0700 | [diff] [blame] | 387 | 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 Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 393 | |
Christoph Fritz | 7beae70 | 2010-07-13 09:42:33 -0700 | [diff] [blame] | 394 | /* 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 Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 400 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | /* triggers left/right */ |
Nicolas Léveillé | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 402 | 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 Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 409 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | /* digital pad */ |
Nicolas Léveillé | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 411 | if (xpad->mapping & MAP_DPAD_TO_BUTTONS) { |
Christoph Fritz | 7beae70 | 2010-07-13 09:42:33 -0700 | [diff] [blame] | 412 | /* 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é | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 417 | } 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 Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 422 | } |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | /* start/back buttons and stick press left/right */ |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 425 | input_report_key(dev, BTN_START, data[2] & 0x10); |
Christoph Fritz | 7beae70 | 2010-07-13 09:42:33 -0700 | [diff] [blame] | 426 | input_report_key(dev, BTN_SELECT, data[2] & 0x20); |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 427 | input_report_key(dev, BTN_THUMBL, data[2] & 0x40); |
| 428 | input_report_key(dev, BTN_THUMBR, data[2] & 0x80); |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 429 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | /* "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 Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | /* "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 Kratochvil | c7d9f7e | 2007-05-09 00:27:37 -0400 | [diff] [blame] | 443 | /* |
| 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. Griffais | 09c8b00 | 2015-12-09 11:46:25 -0800 | [diff] [blame] | 453 | static void xpad360_process_packet(struct usb_xpad *xpad, struct input_dev *dev, |
Dmitry Torokhov | 20b3cdd | 2007-07-18 01:20:34 -0400 | [diff] [blame] | 454 | u16 cmd, unsigned char *data) |
Jan Kratochvil | c7d9f7e | 2007-05-09 00:27:37 -0400 | [diff] [blame] | 455 | { |
Jan Kratochvil | c7d9f7e | 2007-05-09 00:27:37 -0400 | [diff] [blame] | 456 | /* digital pad */ |
Nicolas Léveillé | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 457 | if (xpad->mapping & MAP_DPAD_TO_BUTTONS) { |
Christoph Fritz | 7beae70 | 2010-07-13 09:42:33 -0700 | [diff] [blame] | 458 | /* 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 Rojtberg | 5ee8bda | 2015-10-10 09:33:52 -0700 | [diff] [blame] | 463 | } |
| 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é | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 473 | 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 Kratochvil | c7d9f7e | 2007-05-09 00:27:37 -0400 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /* start/back buttons */ |
Dmitry Torokhov | ae91d10 | 2007-06-14 23:49:55 -0400 | [diff] [blame] | 480 | input_report_key(dev, BTN_START, data[2] & 0x10); |
Christoph Fritz | 7beae70 | 2010-07-13 09:42:33 -0700 | [diff] [blame] | 481 | input_report_key(dev, BTN_SELECT, data[2] & 0x20); |
Jan Kratochvil | c7d9f7e | 2007-05-09 00:27:37 -0400 | [diff] [blame] | 482 | |
| 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 Torokhov | ae91d10 | 2007-06-14 23:49:55 -0400 | [diff] [blame] | 488 | 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 Kratochvil | c7d9f7e | 2007-05-09 00:27:37 -0400 | [diff] [blame] | 495 | |
Christoph Fritz | 7beae70 | 2010-07-13 09:42:33 -0700 | [diff] [blame] | 496 | 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 Kratochvil | c7d9f7e | 2007-05-09 00:27:37 -0400 | [diff] [blame] | 502 | |
Christoph Fritz | 7beae70 | 2010-07-13 09:42:33 -0700 | [diff] [blame] | 503 | /* 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 Kratochvil | c7d9f7e | 2007-05-09 00:27:37 -0400 | [diff] [blame] | 509 | |
| 510 | /* triggers left/right */ |
Nicolas Léveillé | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 511 | 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 Kratochvil | c7d9f7e | 2007-05-09 00:27:37 -0400 | [diff] [blame] | 518 | |
| 519 | input_sync(dev); |
| 520 | } |
| 521 | |
Pierre-Loup A. Griffais | 09c8b00 | 2015-12-09 11:46:25 -0800 | [diff] [blame] | 522 | static 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 Rojtberg | cae705b | 2015-06-22 14:11:30 -0700 | [diff] [blame] | 546 | |
Brian Magnuson | 99de091 | 2008-04-03 16:19:23 -0400 | [diff] [blame] | 547 | /* |
| 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 Magnuson | 99de091 | 2008-04-03 16:19:23 -0400 | [diff] [blame] | 561 | static void xpad360w_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data) |
| 562 | { |
Pierre-Loup A. Griffais | 09c8b00 | 2015-12-09 11:46:25 -0800 | [diff] [blame] | 563 | struct input_dev *dev; |
| 564 | bool present; |
| 565 | |
Brian Magnuson | 99de091 | 2008-04-03 16:19:23 -0400 | [diff] [blame] | 566 | /* Presence change */ |
| 567 | if (data[0] & 0x08) { |
Pierre-Loup A. Griffais | 09c8b00 | 2015-12-09 11:46:25 -0800 | [diff] [blame] | 568 | present = (data[1] & 0x80) != 0; |
| 569 | |
| 570 | if (xpad->pad_present != present) { |
| 571 | xpad->pad_present = present; |
| 572 | schedule_work(&xpad->work); |
| 573 | } |
Brian Magnuson | 99de091 | 2008-04-03 16:19:23 -0400 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | /* Valid pad data */ |
Clement Calmels | 93a017a | 2015-12-12 21:20:11 -0800 | [diff] [blame] | 577 | if (data[1] != 0x1) |
Brian Magnuson | 99de091 | 2008-04-03 16:19:23 -0400 | [diff] [blame] | 578 | return; |
| 579 | |
Pierre-Loup A. Griffais | 09c8b00 | 2015-12-09 11:46:25 -0800 | [diff] [blame] | 580 | 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 Magnuson | 99de091 | 2008-04-03 16:19:23 -0400 | [diff] [blame] | 585 | } |
| 586 | |
Ted Mielczarek | 1a48ff8 | 2014-08-08 11:21:59 -0700 | [diff] [blame] | 587 | /* |
| 588 | * xpadone_process_buttons |
| 589 | * |
| 590 | * Process a button update packet from an Xbox one controller. |
| 591 | */ |
| 592 | static 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 | |
| 668 | static 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 Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 686 | static void xpad_irq_in(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | { |
| 688 | struct usb_xpad *xpad = urb->context; |
Greg Kroah-Hartman | 8818e41 | 2012-05-04 15:32:53 -0700 | [diff] [blame] | 689 | struct device *dev = &xpad->intf->dev; |
Oliver Neukum | 6fc88f53 | 2008-04-03 21:40:59 +0200 | [diff] [blame] | 690 | int retval, status; |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 691 | |
Oliver Neukum | 6fc88f53 | 2008-04-03 21:40:59 +0200 | [diff] [blame] | 692 | status = urb->status; |
| 693 | |
| 694 | switch (status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | case 0: |
| 696 | /* success */ |
| 697 | break; |
| 698 | case -ECONNRESET: |
| 699 | case -ENOENT: |
| 700 | case -ESHUTDOWN: |
| 701 | /* this urb is terminated, clean up */ |
Greg Kroah-Hartman | c4f0bbc | 2012-05-01 21:32:59 -0700 | [diff] [blame] | 702 | dev_dbg(dev, "%s - urb shutting down with status: %d\n", |
Harvey Harrison | ea3e6c5 | 2008-05-05 11:36:18 -0400 | [diff] [blame] | 703 | __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | return; |
| 705 | default: |
Greg Kroah-Hartman | c4f0bbc | 2012-05-01 21:32:59 -0700 | [diff] [blame] | 706 | dev_dbg(dev, "%s - nonzero urb status received: %d\n", |
Harvey Harrison | ea3e6c5 | 2008-05-05 11:36:18 -0400 | [diff] [blame] | 707 | __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | goto exit; |
| 709 | } |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 710 | |
Brian Magnuson | 99de091 | 2008-04-03 16:19:23 -0400 | [diff] [blame] | 711 | switch (xpad->xtype) { |
| 712 | case XTYPE_XBOX360: |
Pierre-Loup A. Griffais | 09c8b00 | 2015-12-09 11:46:25 -0800 | [diff] [blame] | 713 | xpad360_process_packet(xpad, xpad->dev, 0, xpad->idata); |
Brian Magnuson | 99de091 | 2008-04-03 16:19:23 -0400 | [diff] [blame] | 714 | break; |
| 715 | case XTYPE_XBOX360W: |
| 716 | xpad360w_process_packet(xpad, 0, xpad->idata); |
| 717 | break; |
Ted Mielczarek | 1a48ff8 | 2014-08-08 11:21:59 -0700 | [diff] [blame] | 718 | case XTYPE_XBOXONE: |
| 719 | xpadone_process_packet(xpad, 0, xpad->idata); |
| 720 | break; |
Brian Magnuson | 99de091 | 2008-04-03 16:19:23 -0400 | [diff] [blame] | 721 | default: |
Jan Kratochvil | c7d9f7e | 2007-05-09 00:27:37 -0400 | [diff] [blame] | 722 | xpad_process_packet(xpad, 0, xpad->idata); |
Brian Magnuson | 99de091 | 2008-04-03 16:19:23 -0400 | [diff] [blame] | 723 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | |
| 725 | exit: |
Dmitry Torokhov | dd38d68 | 2010-01-09 00:13:36 -0800 | [diff] [blame] | 726 | retval = usb_submit_urb(urb, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | if (retval) |
Greg Kroah-Hartman | c4f0bbc | 2012-05-01 21:32:59 -0700 | [diff] [blame] | 728 | dev_err(dev, "%s - usb_submit_urb failed with result %d\n", |
Greg Kroah-Hartman | 9cb757b | 2012-04-25 14:48:23 -0700 | [diff] [blame] | 729 | __func__, retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | } |
| 731 | |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 732 | /* Callers must hold xpad->odata_lock spinlock */ |
| 733 | static 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 */ |
| 762 | static 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 Kratochvil | e01a06e | 2007-05-09 00:27:51 -0400 | [diff] [blame] | 781 | static void xpad_irq_out(struct urb *urb) |
| 782 | { |
Greg Kroah-Hartman | 9cb757b | 2012-04-25 14:48:23 -0700 | [diff] [blame] | 783 | struct usb_xpad *xpad = urb->context; |
Greg Kroah-Hartman | 8818e41 | 2012-05-04 15:32:53 -0700 | [diff] [blame] | 784 | struct device *dev = &xpad->intf->dev; |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 785 | int status = urb->status; |
| 786 | int error; |
| 787 | unsigned long flags; |
Jan Kratochvil | e01a06e | 2007-05-09 00:27:51 -0400 | [diff] [blame] | 788 | |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 789 | spin_lock_irqsave(&xpad->odata_lock, flags); |
Oliver Neukum | 6fc88f53 | 2008-04-03 21:40:59 +0200 | [diff] [blame] | 790 | |
| 791 | switch (status) { |
Michael Gruber | 70a6f2e | 2009-07-12 20:51:36 -0700 | [diff] [blame] | 792 | case 0: |
Jan Kratochvil | e01a06e | 2007-05-09 00:27:51 -0400 | [diff] [blame] | 793 | /* success */ |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 794 | xpad->out_packets[xpad->last_out_packet].pending = false; |
| 795 | xpad->irq_out_active = xpad_prepare_next_out_packet(xpad); |
| 796 | break; |
Michael Gruber | 70a6f2e | 2009-07-12 20:51:36 -0700 | [diff] [blame] | 797 | |
| 798 | case -ECONNRESET: |
| 799 | case -ENOENT: |
| 800 | case -ESHUTDOWN: |
| 801 | /* this urb is terminated, clean up */ |
Greg Kroah-Hartman | c4f0bbc | 2012-05-01 21:32:59 -0700 | [diff] [blame] | 802 | dev_dbg(dev, "%s - urb shutting down with status: %d\n", |
| 803 | __func__, status); |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 804 | xpad->irq_out_active = false; |
| 805 | break; |
Michael Gruber | 70a6f2e | 2009-07-12 20:51:36 -0700 | [diff] [blame] | 806 | |
| 807 | default: |
Greg Kroah-Hartman | c4f0bbc | 2012-05-01 21:32:59 -0700 | [diff] [blame] | 808 | dev_dbg(dev, "%s - nonzero urb status received: %d\n", |
| 809 | __func__, status); |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 810 | break; |
Jan Kratochvil | e01a06e | 2007-05-09 00:27:51 -0400 | [diff] [blame] | 811 | } |
| 812 | |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 813 | 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 Kratochvil | e01a06e | 2007-05-09 00:27:51 -0400 | [diff] [blame] | 824 | } |
| 825 | |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 826 | static int xpad_init_output(struct usb_interface *intf, struct usb_xpad *xpad) |
| 827 | { |
| 828 | struct usb_endpoint_descriptor *ep_irq_out; |
Ted Mielczarek | 1a48ff8 | 2014-08-08 11:21:59 -0700 | [diff] [blame] | 829 | int ep_irq_out_idx; |
Axel Lin | 49cc69b | 2010-11-11 21:39:11 -0800 | [diff] [blame] | 830 | int error; |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 831 | |
Chris Moeller | b514d4f | 2011-07-04 19:21:59 -0700 | [diff] [blame] | 832 | if (xpad->xtype == XTYPE_UNKNOWN) |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 833 | return 0; |
| 834 | |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 835 | xpad->odata = usb_alloc_coherent(xpad->udev, XPAD_PKT_LEN, |
| 836 | GFP_KERNEL, &xpad->odata_dma); |
Axel Lin | 49cc69b | 2010-11-11 21:39:11 -0800 | [diff] [blame] | 837 | if (!xpad->odata) { |
| 838 | error = -ENOMEM; |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 839 | goto fail1; |
Axel Lin | 49cc69b | 2010-11-11 21:39:11 -0800 | [diff] [blame] | 840 | } |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 841 | |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 842 | spin_lock_init(&xpad->odata_lock); |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 843 | |
| 844 | xpad->irq_out = usb_alloc_urb(0, GFP_KERNEL); |
Axel Lin | 49cc69b | 2010-11-11 21:39:11 -0800 | [diff] [blame] | 845 | if (!xpad->irq_out) { |
| 846 | error = -ENOMEM; |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 847 | goto fail2; |
Axel Lin | 49cc69b | 2010-11-11 21:39:11 -0800 | [diff] [blame] | 848 | } |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 849 | |
Ted Mielczarek | 1a48ff8 | 2014-08-08 11:21:59 -0700 | [diff] [blame] | 850 | /* 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 Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 854 | 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 Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 863 | fail2: usb_free_coherent(xpad->udev, XPAD_PKT_LEN, xpad->odata, xpad->odata_dma); |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 864 | fail1: return error; |
| 865 | } |
| 866 | |
| 867 | static void xpad_stop_output(struct usb_xpad *xpad) |
| 868 | { |
Chris Moeller | b514d4f | 2011-07-04 19:21:59 -0700 | [diff] [blame] | 869 | if (xpad->xtype != XTYPE_UNKNOWN) |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 870 | usb_kill_urb(xpad->irq_out); |
| 871 | } |
| 872 | |
| 873 | static void xpad_deinit_output(struct usb_xpad *xpad) |
| 874 | { |
Chris Moeller | b514d4f | 2011-07-04 19:21:59 -0700 | [diff] [blame] | 875 | if (xpad->xtype != XTYPE_UNKNOWN) { |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 876 | usb_free_urb(xpad->irq_out); |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 877 | usb_free_coherent(xpad->udev, XPAD_PKT_LEN, |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 878 | xpad->odata, xpad->odata_dma); |
| 879 | } |
| 880 | } |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 881 | |
Pavel Rojtberg | aba5487 | 2015-10-10 09:36:17 -0700 | [diff] [blame] | 882 | static int xpad_inquiry_pad_presence(struct usb_xpad *xpad) |
| 883 | { |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 884 | struct xpad_output_packet *packet = |
| 885 | &xpad->out_packets[XPAD_OUT_CMD_IDX]; |
| 886 | unsigned long flags; |
Pavel Rojtberg | aba5487 | 2015-10-10 09:36:17 -0700 | [diff] [blame] | 887 | int retval; |
| 888 | |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 889 | spin_lock_irqsave(&xpad->odata_lock, flags); |
Pavel Rojtberg | aba5487 | 2015-10-10 09:36:17 -0700 | [diff] [blame] | 890 | |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 891 | 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 Rojtberg | aba5487 | 2015-10-10 09:36:17 -0700 | [diff] [blame] | 905 | |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 906 | /* 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 Rojtberg | aba5487 | 2015-10-10 09:36:17 -0700 | [diff] [blame] | 909 | |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 910 | spin_unlock_irqrestore(&xpad->odata_lock, flags); |
| 911 | |
| 912 | return retval; |
| 913 | } |
| 914 | |
| 915 | static 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. Griffais | 2a6d752 | 2015-12-09 13:40:37 -0800 | [diff] [blame^] | 927 | 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 Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 931 | 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 Rojtberg | aba5487 | 2015-10-10 09:36:17 -0700 | [diff] [blame] | 938 | |
| 939 | return retval; |
| 940 | } |
| 941 | |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 942 | #ifdef CONFIG_JOYSTICK_XPAD_FF |
Benjamin Valentin | 1218730 | 2010-01-21 20:19:06 -0800 | [diff] [blame] | 943 | static int xpad_play_effect(struct input_dev *dev, void *data, struct ff_effect *effect) |
Jan Kratochvil | e01a06e | 2007-05-09 00:27:51 -0400 | [diff] [blame] | 944 | { |
| 945 | struct usb_xpad *xpad = input_get_drvdata(dev); |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 946 | struct xpad_output_packet *packet = &xpad->out_packets[XPAD_OUT_FF_IDX]; |
Pavel Rojtberg | 0600815 | 2015-10-10 09:32:55 -0700 | [diff] [blame] | 947 | __u16 strong; |
| 948 | __u16 weak; |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 949 | int retval; |
| 950 | unsigned long flags; |
Jan Kratochvil | e01a06e | 2007-05-09 00:27:51 -0400 | [diff] [blame] | 951 | |
Pavel Rojtberg | 0600815 | 2015-10-10 09:32:55 -0700 | [diff] [blame] | 952 | if (effect->type != FF_RUMBLE) |
| 953 | return 0; |
Benjamin Valentin | 1218730 | 2010-01-21 20:19:06 -0800 | [diff] [blame] | 954 | |
Pavel Rojtberg | 0600815 | 2015-10-10 09:32:55 -0700 | [diff] [blame] | 955 | strong = effect->u.rumble.strong_magnitude; |
| 956 | weak = effect->u.rumble.weak_magnitude; |
Benjamin Valentin | 1218730 | 2010-01-21 20:19:06 -0800 | [diff] [blame] | 957 | |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 958 | spin_lock_irqsave(&xpad->odata_lock, flags); |
| 959 | |
Pavel Rojtberg | 0600815 | 2015-10-10 09:32:55 -0700 | [diff] [blame] | 960 | switch (xpad->xtype) { |
| 961 | case XTYPE_XBOX: |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 962 | 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 Rojtberg | 0600815 | 2015-10-10 09:32:55 -0700 | [diff] [blame] | 970 | break; |
Benjamin Valentin | 1218730 | 2010-01-21 20:19:06 -0800 | [diff] [blame] | 971 | |
Pavel Rojtberg | 0600815 | 2015-10-10 09:32:55 -0700 | [diff] [blame] | 972 | case XTYPE_XBOX360: |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 973 | 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 Rojtberg | 0600815 | 2015-10-10 09:32:55 -0700 | [diff] [blame] | 983 | break; |
Benjamin Valentin | 1218730 | 2010-01-21 20:19:06 -0800 | [diff] [blame] | 984 | |
Pavel Rojtberg | 0600815 | 2015-10-10 09:32:55 -0700 | [diff] [blame] | 985 | case XTYPE_XBOX360W: |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 986 | 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 Rojtberg | 0600815 | 2015-10-10 09:32:55 -0700 | [diff] [blame] | 1000 | break; |
Benjamin Valentin | 1218730 | 2010-01-21 20:19:06 -0800 | [diff] [blame] | 1001 | |
Pavel Rojtberg | 0600815 | 2015-10-10 09:32:55 -0700 | [diff] [blame] | 1002 | case XTYPE_XBOXONE: |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 1003 | packet->data[0] = 0x09; /* activate rumble */ |
| 1004 | packet->data[1] = 0x08; |
Pierre-Loup A. Griffais | 2a6d752 | 2015-12-09 13:40:37 -0800 | [diff] [blame^] | 1005 | packet->data[2] = xpad->odata_serial++; |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 1006 | 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. Griffais | 2a6d752 | 2015-12-09 13:40:37 -0800 | [diff] [blame^] | 1011 | packet->data[8] = strong / 512; /* left actuator */ |
| 1012 | packet->data[9] = weak / 512; /* right actuator */ |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 1013 | packet->data[10] = 0x80; /* length of pulse */ |
| 1014 | packet->data[11] = 0x00; /* stop period of pulse */ |
Pierre-Loup A. Griffais | 2a6d752 | 2015-12-09 13:40:37 -0800 | [diff] [blame^] | 1015 | packet->data[12] = 0x00; |
| 1016 | packet->len = 13; |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 1017 | packet->pending = true; |
Pavel Rojtberg | 0600815 | 2015-10-10 09:32:55 -0700 | [diff] [blame] | 1018 | break; |
Benjamin Valentin | 1218730 | 2010-01-21 20:19:06 -0800 | [diff] [blame] | 1019 | |
Pavel Rojtberg | 0600815 | 2015-10-10 09:32:55 -0700 | [diff] [blame] | 1020 | default: |
| 1021 | dev_dbg(&xpad->dev->dev, |
| 1022 | "%s - rumble command sent to unsupported xpad type: %d\n", |
| 1023 | __func__, xpad->xtype); |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 1024 | retval = -EINVAL; |
| 1025 | goto out; |
Jan Kratochvil | e01a06e | 2007-05-09 00:27:51 -0400 | [diff] [blame] | 1026 | } |
| 1027 | |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 1028 | retval = xpad_try_sending_next_out_packet(xpad); |
| 1029 | |
| 1030 | out: |
| 1031 | spin_unlock_irqrestore(&xpad->odata_lock, flags); |
| 1032 | return retval; |
Jan Kratochvil | e01a06e | 2007-05-09 00:27:51 -0400 | [diff] [blame] | 1033 | } |
| 1034 | |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 1035 | static int xpad_init_ff(struct usb_xpad *xpad) |
Jan Kratochvil | e01a06e | 2007-05-09 00:27:51 -0400 | [diff] [blame] | 1036 | { |
Ming-ting Yao Wei | 0604949 | 2015-04-14 16:59:11 -0700 | [diff] [blame] | 1037 | if (xpad->xtype == XTYPE_UNKNOWN) |
Anssi Hannula | cfbe201 | 2008-04-03 16:18:57 -0400 | [diff] [blame] | 1038 | return 0; |
| 1039 | |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 1040 | 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 |
| 1046 | static 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 Rojtberg | e3b6517 | 2015-10-10 10:00:49 -0700 | [diff] [blame] | 1051 | #include <linux/idr.h> |
| 1052 | |
| 1053 | static DEFINE_IDA(xpad_pad_seq); |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 1054 | |
| 1055 | struct xpad_led { |
| 1056 | char name[16]; |
| 1057 | struct led_classdev led_cdev; |
| 1058 | struct usb_xpad *xpad; |
| 1059 | }; |
| 1060 | |
Pierre-Loup A. Griffais | 75b7f05 | 2015-06-22 14:10:36 -0700 | [diff] [blame] | 1061 | /** |
Pavel Rojtberg | 1f6f02b | 2015-10-06 17:06:16 -0700 | [diff] [blame] | 1062 | * set the LEDs on Xbox360 / Wireless Controllers |
Pierre-Loup A. Griffais | 75b7f05 | 2015-06-22 14:10:36 -0700 | [diff] [blame] | 1063 | * @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 Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 1081 | static void xpad_send_led_command(struct usb_xpad *xpad, int command) |
| 1082 | { |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 1083 | struct xpad_output_packet *packet = |
| 1084 | &xpad->out_packets[XPAD_OUT_LED_IDX]; |
| 1085 | unsigned long flags; |
| 1086 | |
Pierre-Loup A. Griffais | 75b7f05 | 2015-06-22 14:10:36 -0700 | [diff] [blame] | 1087 | command %= 16; |
| 1088 | |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 1089 | spin_lock_irqsave(&xpad->odata_lock, flags); |
Pierre-Loup A. Griffais | 75b7f05 | 2015-06-22 14:10:36 -0700 | [diff] [blame] | 1090 | |
| 1091 | switch (xpad->xtype) { |
| 1092 | case XTYPE_XBOX360: |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 1093 | 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. Griffais | 75b7f05 | 2015-06-22 14:10:36 -0700 | [diff] [blame] | 1098 | break; |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 1099 | |
Pierre-Loup A. Griffais | 75b7f05 | 2015-06-22 14:10:36 -0700 | [diff] [blame] | 1100 | case XTYPE_XBOX360W: |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 1101 | 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. Griffais | 75b7f05 | 2015-06-22 14:10:36 -0700 | [diff] [blame] | 1115 | break; |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 1116 | } |
Pierre-Loup A. Griffais | 75b7f05 | 2015-06-22 14:10:36 -0700 | [diff] [blame] | 1117 | |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 1118 | xpad_try_sending_next_out_packet(xpad); |
| 1119 | |
| 1120 | spin_unlock_irqrestore(&xpad->odata_lock, flags); |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 1121 | } |
| 1122 | |
Pavel Rojtberg | 1f6f02b | 2015-10-06 17:06:16 -0700 | [diff] [blame] | 1123 | /* |
| 1124 | * Light up the segment corresponding to the pad number on |
| 1125 | * Xbox 360 Controllers. |
| 1126 | */ |
Pavel Rojtberg | cae705b | 2015-06-22 14:11:30 -0700 | [diff] [blame] | 1127 | static void xpad_identify_controller(struct usb_xpad *xpad) |
| 1128 | { |
Pavel Rojtberg | 1f6f02b | 2015-10-06 17:06:16 -0700 | [diff] [blame] | 1129 | xpad_send_led_command(xpad, (xpad->pad_nr % 4) + 2); |
Pavel Rojtberg | cae705b | 2015-06-22 14:11:30 -0700 | [diff] [blame] | 1130 | } |
| 1131 | |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 1132 | static 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 | |
| 1141 | static int xpad_led_probe(struct usb_xpad *xpad) |
| 1142 | { |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 1143 | struct xpad_led *led; |
| 1144 | struct led_classdev *led_cdev; |
| 1145 | int error; |
Jan Kratochvil | e01a06e | 2007-05-09 00:27:51 -0400 | [diff] [blame] | 1146 | |
Pierre-Loup A. Griffais | 75b7f05 | 2015-06-22 14:10:36 -0700 | [diff] [blame] | 1147 | if (xpad->xtype != XTYPE_XBOX360 && xpad->xtype != XTYPE_XBOX360W) |
Jan Kratochvil | e01a06e | 2007-05-09 00:27:51 -0400 | [diff] [blame] | 1148 | return 0; |
| 1149 | |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 1150 | xpad->led = led = kzalloc(sizeof(struct xpad_led), GFP_KERNEL); |
| 1151 | if (!led) |
| 1152 | return -ENOMEM; |
Jan Kratochvil | e01a06e | 2007-05-09 00:27:51 -0400 | [diff] [blame] | 1153 | |
Pavel Rojtberg | e3b6517 | 2015-10-10 10:00:49 -0700 | [diff] [blame] | 1154 | 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 Kratochvil | e01a06e | 2007-05-09 00:27:51 -0400 | [diff] [blame] | 1159 | |
Pavel Rojtberg | e3b6517 | 2015-10-10 10:00:49 -0700 | [diff] [blame] | 1160 | snprintf(led->name, sizeof(led->name), "xpad%d", xpad->pad_nr); |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 1161 | led->xpad = xpad; |
Jan Kratochvil | e01a06e | 2007-05-09 00:27:51 -0400 | [diff] [blame] | 1162 | |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 1163 | led_cdev = &led->led_cdev; |
| 1164 | led_cdev->name = led->name; |
| 1165 | led_cdev->brightness_set = xpad_led_set; |
Jan Kratochvil | e01a06e | 2007-05-09 00:27:51 -0400 | [diff] [blame] | 1166 | |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 1167 | error = led_classdev_register(&xpad->udev->dev, led_cdev); |
Pavel Rojtberg | e3b6517 | 2015-10-10 10:00:49 -0700 | [diff] [blame] | 1168 | if (error) |
| 1169 | goto err_free_id; |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 1170 | |
Pierre-Loup A. Griffais | 09c8b00 | 2015-12-09 11:46:25 -0800 | [diff] [blame] | 1171 | xpad_identify_controller(xpad); |
Pavel Rojtberg | fbe6a31 | 2015-10-19 00:06:58 -0700 | [diff] [blame] | 1172 | |
Jan Kratochvil | e01a06e | 2007-05-09 00:27:51 -0400 | [diff] [blame] | 1173 | return 0; |
Pavel Rojtberg | e3b6517 | 2015-10-10 10:00:49 -0700 | [diff] [blame] | 1174 | |
| 1175 | err_free_id: |
| 1176 | ida_simple_remove(&xpad_pad_seq, xpad->pad_nr); |
| 1177 | err_free_mem: |
| 1178 | kfree(led); |
| 1179 | xpad->led = NULL; |
| 1180 | return error; |
Jan Kratochvil | e01a06e | 2007-05-09 00:27:51 -0400 | [diff] [blame] | 1181 | } |
| 1182 | |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 1183 | static void xpad_led_disconnect(struct usb_xpad *xpad) |
Jan Kratochvil | e01a06e | 2007-05-09 00:27:51 -0400 | [diff] [blame] | 1184 | { |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 1185 | struct xpad_led *xpad_led = xpad->led; |
Jan Kratochvil | e01a06e | 2007-05-09 00:27:51 -0400 | [diff] [blame] | 1186 | |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 1187 | if (xpad_led) { |
| 1188 | led_classdev_unregister(&xpad_led->led_cdev); |
Pavel Rojtberg | e3b6517 | 2015-10-10 10:00:49 -0700 | [diff] [blame] | 1189 | ida_simple_remove(&xpad_pad_seq, xpad->pad_nr); |
Axel Lin | 6ff92a6 | 2010-11-11 21:43:17 -0800 | [diff] [blame] | 1190 | kfree(xpad_led); |
Jan Kratochvil | e01a06e | 2007-05-09 00:27:51 -0400 | [diff] [blame] | 1191 | } |
| 1192 | } |
Jan Kratochvil | e01a06e | 2007-05-09 00:27:51 -0400 | [diff] [blame] | 1193 | #else |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 1194 | static int xpad_led_probe(struct usb_xpad *xpad) { return 0; } |
| 1195 | static void xpad_led_disconnect(struct usb_xpad *xpad) { } |
Pavel Rojtberg | cae705b | 2015-06-22 14:11:30 -0700 | [diff] [blame] | 1196 | static void xpad_identify_controller(struct usb_xpad *xpad) { } |
Jan Kratochvil | e01a06e | 2007-05-09 00:27:51 -0400 | [diff] [blame] | 1197 | #endif |
| 1198 | |
| 1199 | static int xpad_open(struct input_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | { |
Dmitry Torokhov | 7791bda | 2007-04-12 01:34:39 -0400 | [diff] [blame] | 1201 | struct usb_xpad *xpad = input_get_drvdata(dev); |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 1202 | |
Brian Magnuson | 99de091 | 2008-04-03 16:19:23 -0400 | [diff] [blame] | 1203 | /* URB was submitted in probe */ |
Guillermo A. Amaral | 0d02796 | 2012-12-02 23:26:11 -0800 | [diff] [blame] | 1204 | if (xpad->xtype == XTYPE_XBOX360W) |
Brian Magnuson | 99de091 | 2008-04-03 16:19:23 -0400 | [diff] [blame] | 1205 | return 0; |
| 1206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | xpad->irq_in->dev = xpad->udev; |
Dmitry Torokhov | 65cde54 | 2005-05-29 02:29:38 -0500 | [diff] [blame] | 1208 | if (usb_submit_urb(xpad->irq_in, GFP_KERNEL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | return -EIO; |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 1210 | |
Pavel Rojtberg | 7fc595f | 2015-12-09 11:57:01 -0800 | [diff] [blame] | 1211 | if (xpad->xtype == XTYPE_XBOXONE) |
| 1212 | return xpad_start_xbox_one(xpad); |
Ted Mielczarek | 1a48ff8 | 2014-08-08 11:21:59 -0700 | [diff] [blame] | 1213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | return 0; |
| 1215 | } |
| 1216 | |
Jan Kratochvil | e01a06e | 2007-05-09 00:27:51 -0400 | [diff] [blame] | 1217 | static void xpad_close(struct input_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | { |
Dmitry Torokhov | 7791bda | 2007-04-12 01:34:39 -0400 | [diff] [blame] | 1219 | struct usb_xpad *xpad = input_get_drvdata(dev); |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 1220 | |
Axel Lin | 161feb2 | 2010-11-11 21:47:42 -0800 | [diff] [blame] | 1221 | if (xpad->xtype != XTYPE_XBOX360W) |
Brian Magnuson | 99de091 | 2008-04-03 16:19:23 -0400 | [diff] [blame] | 1222 | usb_kill_urb(xpad->irq_in); |
Axel Lin | 161feb2 | 2010-11-11 21:47:42 -0800 | [diff] [blame] | 1223 | |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 1224 | xpad_stop_output(xpad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | } |
| 1226 | |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 1227 | static void xpad_set_up_abs(struct input_dev *input_dev, signed short abs) |
| 1228 | { |
Ted Mielczarek | 1a48ff8 | 2014-08-08 11:21:59 -0700 | [diff] [blame] | 1229 | struct usb_xpad *xpad = input_get_drvdata(input_dev); |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 1230 | 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é | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 1240 | case ABS_RZ: /* the triggers (if mapped to axes) */ |
Ted Mielczarek | 1a48ff8 | 2014-08-08 11:21:59 -0700 | [diff] [blame] | 1241 | 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 Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 1245 | break; |
| 1246 | case ABS_HAT0X: |
Nicolas Léveillé | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 1247 | case ABS_HAT0Y: /* the d-pad (only if dpad is mapped to axes */ |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 1248 | input_set_abs_params(input_dev, abs, -1, 1, 0, 0); |
| 1249 | break; |
| 1250 | } |
| 1251 | } |
| 1252 | |
Pierre-Loup A. Griffais | b815400 | 2015-10-10 09:34:17 -0700 | [diff] [blame] | 1253 | static void xpad_deinit_input(struct usb_xpad *xpad) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | { |
Pierre-Loup A. Griffais | 09c8b00 | 2015-12-09 11:46:25 -0800 | [diff] [blame] | 1255 | if (xpad->input_created) { |
| 1256 | xpad->input_created = false; |
| 1257 | xpad_led_disconnect(xpad); |
| 1258 | input_unregister_device(xpad->dev); |
| 1259 | } |
Pierre-Loup A. Griffais | b815400 | 2015-10-10 09:34:17 -0700 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | static int xpad_init_input(struct usb_xpad *xpad) |
| 1263 | { |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 1264 | struct input_dev *input_dev; |
Axel Lin | 49cc69b | 2010-11-11 21:39:11 -0800 | [diff] [blame] | 1265 | int i, error; |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 1266 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 1267 | input_dev = input_allocate_device(); |
Pierre-Loup A. Griffais | b815400 | 2015-10-10 09:34:17 -0700 | [diff] [blame] | 1268 | if (!input_dev) |
| 1269 | return -ENOMEM; |
Nicolas Léveillé | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 1270 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 1271 | xpad->dev = input_dev; |
Pierre-Loup A. Griffais | b815400 | 2015-10-10 09:34:17 -0700 | [diff] [blame] | 1272 | input_dev->name = xpad->name; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 1273 | input_dev->phys = xpad->phys; |
Pierre-Loup A. Griffais | b815400 | 2015-10-10 09:34:17 -0700 | [diff] [blame] | 1274 | usb_to_input_id(xpad->udev, &input_dev->id); |
| 1275 | input_dev->dev.parent = &xpad->intf->dev; |
Dmitry Torokhov | 7791bda | 2007-04-12 01:34:39 -0400 | [diff] [blame] | 1276 | |
| 1277 | input_set_drvdata(input_dev, xpad); |
| 1278 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 1279 | input_dev->open = xpad_open; |
| 1280 | input_dev->close = xpad_close; |
| 1281 | |
Pierre-Loup A. Griffais | b815400 | 2015-10-10 09:34:17 -0700 | [diff] [blame] | 1282 | __set_bit(EV_KEY, input_dev->evbit); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 1283 | |
Christoph Fritz | 7beae70 | 2010-07-13 09:42:33 -0700 | [diff] [blame] | 1284 | if (!(xpad->mapping & MAP_STICKS_TO_NULL)) { |
Pierre-Loup A. Griffais | b815400 | 2015-10-10 09:34:17 -0700 | [diff] [blame] | 1285 | __set_bit(EV_ABS, input_dev->evbit); |
Christoph Fritz | 7beae70 | 2010-07-13 09:42:33 -0700 | [diff] [blame] | 1286 | /* 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 Hannula | fc55e95 | 2008-04-03 16:18:44 -0400 | [diff] [blame] | 1292 | for (i = 0; xpad_common_btn[i] >= 0; i++) |
Nicolas Léveillé | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 1293 | __set_bit(xpad_common_btn[i], input_dev->keybit); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 1294 | |
Christoph Fritz | 7beae70 | 2010-07-13 09:42:33 -0700 | [diff] [blame] | 1295 | /* set up model-specific ones */ |
Ted Mielczarek | 1a48ff8 | 2014-08-08 11:21:59 -0700 | [diff] [blame] | 1296 | if (xpad->xtype == XTYPE_XBOX360 || xpad->xtype == XTYPE_XBOX360W || |
| 1297 | xpad->xtype == XTYPE_XBOXONE) { |
Nicolas Léveillé | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 1298 | 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 Rojtberg | 5ee8bda | 2015-10-10 09:33:52 -0700 | [diff] [blame] | 1308 | } |
| 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 Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 1318 | for (i = 0; xpad_abs_pad[i] >= 0; i++) |
Ted Mielczarek | 1a48ff8 | 2014-08-08 11:21:59 -0700 | [diff] [blame] | 1319 | xpad_set_up_abs(input_dev, xpad_abs_pad[i]); |
Nicolas Léveillé | b45d44e | 2009-12-29 20:39:05 -0800 | [diff] [blame] | 1320 | } |
| 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 Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 1329 | |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 1330 | error = xpad_init_ff(xpad); |
| 1331 | if (error) |
Pierre-Loup A. Griffais | b815400 | 2015-10-10 09:34:17 -0700 | [diff] [blame] | 1332 | goto err_free_input; |
Jan Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 1333 | |
| 1334 | error = xpad_led_probe(xpad); |
| 1335 | if (error) |
Pierre-Loup A. Griffais | b815400 | 2015-10-10 09:34:17 -0700 | [diff] [blame] | 1336 | goto err_destroy_ff; |
| 1337 | |
| 1338 | error = input_register_device(xpad->dev); |
| 1339 | if (error) |
| 1340 | goto err_disconnect_led; |
| 1341 | |
Pierre-Loup A. Griffais | 09c8b00 | 2015-12-09 11:46:25 -0800 | [diff] [blame] | 1342 | xpad->input_created = true; |
Pierre-Loup A. Griffais | b815400 | 2015-10-10 09:34:17 -0700 | [diff] [blame] | 1343 | return 0; |
| 1344 | |
| 1345 | err_disconnect_led: |
| 1346 | xpad_led_disconnect(xpad); |
| 1347 | err_destroy_ff: |
| 1348 | input_ff_destroy(input_dev); |
| 1349 | err_free_input: |
| 1350 | input_free_device(input_dev); |
| 1351 | return error; |
| 1352 | } |
| 1353 | |
| 1354 | static 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. Griffais | 09c8b00 | 2015-12-09 11:46:25 -0800 | [diff] [blame] | 1403 | INIT_WORK(&xpad->work, xpad_presence_work); |
Pierre-Loup A. Griffais | b815400 | 2015-10-10 09:34:17 -0700 | [diff] [blame] | 1404 | |
| 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 Kratochvil | 4994cd8 | 2007-07-18 00:35:40 -0400 | [diff] [blame] | 1426 | |
Ted Mielczarek | 1a48ff8 | 2014-08-08 11:21:59 -0700 | [diff] [blame] | 1427 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | 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 Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 1437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | usb_set_intfdata(intf, xpad); |
Brian Magnuson | 99de091 | 2008-04-03 16:19:23 -0400 | [diff] [blame] | 1439 | |
Brian Magnuson | 99de091 | 2008-04-03 16:19:23 -0400 | [diff] [blame] | 1440 | if (xpad->xtype == XTYPE_XBOX360W) { |
Brian Magnuson | 99de091 | 2008-04-03 16:19:23 -0400 | [diff] [blame] | 1441 | /* |
Axel Lin | e3f0f0a | 2010-11-17 23:59:34 -0800 | [diff] [blame] | 1442 | * 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. Griffais | 09c8b00 | 2015-12-09 11:46:25 -0800 | [diff] [blame] | 1451 | goto err_deinit_output; |
Pavel Rojtberg | aba5487 | 2015-10-10 09:36:17 -0700 | [diff] [blame] | 1452 | |
| 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. Griffais | 09c8b00 | 2015-12-09 11:46:25 -0800 | [diff] [blame] | 1463 | } else { |
| 1464 | error = xpad_init_input(xpad); |
| 1465 | if (error) |
| 1466 | goto err_deinit_output; |
Brian Magnuson | 99de091 | 2008-04-03 16:19:23 -0400 | [diff] [blame] | 1467 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | return 0; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 1469 | |
Pavel Rojtberg | aba5487 | 2015-10-10 09:36:17 -0700 | [diff] [blame] | 1470 | err_kill_in_urb: |
| 1471 | usb_kill_urb(xpad->irq_in); |
Pierre-Loup A. Griffais | b815400 | 2015-10-10 09:34:17 -0700 | [diff] [blame] | 1472 | err_deinit_output: |
| 1473 | xpad_deinit_output(xpad); |
| 1474 | err_free_in_urb: |
| 1475 | usb_free_urb(xpad->irq_in); |
| 1476 | err_free_idata: |
| 1477 | usb_free_coherent(udev, XPAD_PKT_LEN, xpad->idata, xpad->idata_dma); |
| 1478 | err_free_mem: |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 1479 | kfree(xpad); |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 1480 | return error; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 1481 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | static void xpad_disconnect(struct usb_interface *intf) |
| 1485 | { |
| 1486 | struct usb_xpad *xpad = usb_get_intfdata (intf); |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 1487 | |
Pierre-Loup A. Griffais | 09c8b00 | 2015-12-09 11:46:25 -0800 | [diff] [blame] | 1488 | if (xpad->xtype == XTYPE_XBOX360W) |
Dmitry Torokhov | 2a05915 | 2010-11-11 21:52:18 -0800 | [diff] [blame] | 1489 | usb_kill_urb(xpad->irq_in); |
Pierre-Loup A. Griffais | 09c8b00 | 2015-12-09 11:46:25 -0800 | [diff] [blame] | 1490 | |
| 1491 | cancel_work_sync(&xpad->work); |
| 1492 | |
| 1493 | xpad_deinit_input(xpad); |
Dmitry Torokhov | 2a05915 | 2010-11-11 21:52:18 -0800 | [diff] [blame] | 1494 | |
| 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. Griffais | 09c8b00 | 2015-12-09 11:46:25 -0800 | [diff] [blame] | 1499 | xpad_deinit_output(xpad); |
| 1500 | |
Dmitry Torokhov | 2a05915 | 2010-11-11 21:52:18 -0800 | [diff] [blame] | 1501 | kfree(xpad); |
| 1502 | |
| 1503 | usb_set_intfdata(intf, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | static struct usb_driver xpad_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | .name = "xpad", |
| 1508 | .probe = xpad_probe, |
| 1509 | .disconnect = xpad_disconnect, |
| 1510 | .id_table = xpad_table, |
| 1511 | }; |
| 1512 | |
Greg Kroah-Hartman | 08642e7 | 2011-11-18 09:48:31 -0800 | [diff] [blame] | 1513 | module_usb_driver(xpad_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | |
| 1515 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 1516 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 1517 | MODULE_LICENSE("GPL"); |