source: src/linux/universal/linux-4.4/drivers/input/joystick/xpad.c @ 31885

Last change on this file since 31885 was 31885, checked in by brainslayer, 4 months ago

update

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