xpad.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  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. *
  35. * Thanks to:
  36. * - ITO Takayuki for providing essential xpad information on his website
  37. * - Vojtech Pavlik - iforce driver / input subsystem
  38. * - Greg Kroah-Hartman - usb-skeleton driver
  39. * - XBOX Linux project - extra USB id's
  40. *
  41. * TODO:
  42. * - fine tune axes (especially trigger axes)
  43. * - fix "analog" buttons (reported as digital now)
  44. * - get rumble working
  45. * - need USB IDs for other dance pads
  46. *
  47. * History:
  48. *
  49. * 2002-06-27 - 0.0.1 : first version, just said "XBOX HID controller"
  50. *
  51. * 2002-07-02 - 0.0.2 : basic working version
  52. * - all axes and 9 of the 10 buttons work (german InterAct device)
  53. * - the black button does not work
  54. *
  55. * 2002-07-14 - 0.0.3 : rework by Vojtech Pavlik
  56. * - indentation fixes
  57. * - usb + input init sequence fixes
  58. *
  59. * 2002-07-16 - 0.0.4 : minor changes, merge with Vojtech's v0.0.3
  60. * - verified the lack of HID and report descriptors
  61. * - verified that ALL buttons WORK
  62. * - fixed d-pad to axes mapping
  63. *
  64. * 2002-07-17 - 0.0.5 : simplified d-pad handling
  65. *
  66. * 2004-10-02 - 0.0.6 : DDR pad support
  67. * - borrowed from the XBOX linux kernel
  68. * - USB id's for commonly used dance pads are present
  69. * - dance pads will map D-PAD to buttons, not axes
  70. * - pass the module paramater 'dpad_to_buttons' to force
  71. * the D-PAD to map to buttons if your pad is not detected
  72. *
  73. * Later changes can be tracked in SCM.
  74. */
  75. #include <linux/kernel.h>
  76. #include <linux/slab.h>
  77. #include <linux/stat.h>
  78. #include <linux/module.h>
  79. #include <linux/usb/input.h>
  80. #define DRIVER_AUTHOR "Marko Friedemann <mfr@bmx-chemnitz.de>"
  81. #define DRIVER_DESC "X-Box pad driver"
  82. #define XPAD_PKT_LEN 32
  83. /* xbox d-pads should map to buttons, as is required for DDR pads
  84. but we map them to axes when possible to simplify things */
  85. #define MAP_DPAD_TO_BUTTONS (1 << 0)
  86. #define MAP_TRIGGERS_TO_BUTTONS (1 << 1)
  87. #define MAP_STICKS_TO_NULL (1 << 2)
  88. #define DANCEPAD_MAP_CONFIG (MAP_DPAD_TO_BUTTONS | \
  89. MAP_TRIGGERS_TO_BUTTONS | MAP_STICKS_TO_NULL)
  90. #define XTYPE_XBOX 0
  91. #define XTYPE_XBOX360 1
  92. #define XTYPE_XBOX360W 2
  93. #define XTYPE_XBOXONE 3
  94. #define XTYPE_UNKNOWN 4
  95. static bool dpad_to_buttons;
  96. module_param(dpad_to_buttons, bool, S_IRUGO);
  97. MODULE_PARM_DESC(dpad_to_buttons, "Map D-PAD to buttons rather than axes for unknown pads");
  98. static bool triggers_to_buttons;
  99. module_param(triggers_to_buttons, bool, S_IRUGO);
  100. MODULE_PARM_DESC(triggers_to_buttons, "Map triggers to buttons rather than axes for unknown pads");
  101. static bool sticks_to_null;
  102. module_param(sticks_to_null, bool, S_IRUGO);
  103. MODULE_PARM_DESC(sticks_to_null, "Do not map sticks at all for unknown pads");
  104. static const struct xpad_device {
  105. u16 idVendor;
  106. u16 idProduct;
  107. char *name;
  108. u8 mapping;
  109. u8 xtype;
  110. } xpad_device[] = {
  111. { 0x045e, 0x0202, "Microsoft X-Box pad v1 (US)", 0, XTYPE_XBOX },
  112. { 0x045e, 0x0285, "Microsoft X-Box pad (Japan)", 0, XTYPE_XBOX },
  113. { 0x045e, 0x0287, "Microsoft Xbox Controller S", 0, XTYPE_XBOX },
  114. { 0x045e, 0x0289, "Microsoft X-Box pad v2 (US)", 0, XTYPE_XBOX },
  115. { 0x045e, 0x028e, "Microsoft X-Box 360 pad", 0, XTYPE_XBOX360 },
  116. { 0x045e, 0x02d1, "Microsoft X-Box One pad", 0, XTYPE_XBOXONE },
  117. { 0x045e, 0x0291, "Xbox 360 Wireless Receiver (XBOX)", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360W },
  118. { 0x045e, 0x0719, "Xbox 360 Wireless Receiver", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360W },
  119. { 0x044f, 0x0f07, "Thrustmaster, Inc. Controller", 0, XTYPE_XBOX },
  120. { 0x046d, 0xc21d, "Logitech Gamepad F310", 0, XTYPE_XBOX360 },
  121. { 0x046d, 0xc21f, "Logitech Gamepad F710", 0, XTYPE_XBOX360 },
  122. { 0x046d, 0xc242, "Logitech Chillstream Controller", 0, XTYPE_XBOX360 },
  123. { 0x046d, 0xca84, "Logitech Xbox Cordless Controller", 0, XTYPE_XBOX },
  124. { 0x046d, 0xca88, "Logitech Compact Controller for Xbox", 0, XTYPE_XBOX },
  125. { 0x05fd, 0x1007, "Mad Catz Controller (unverified)", 0, XTYPE_XBOX },
  126. { 0x05fd, 0x107a, "InterAct 'PowerPad Pro' X-Box pad (Germany)", 0, XTYPE_XBOX },
  127. { 0x0738, 0x4516, "Mad Catz Control Pad", 0, XTYPE_XBOX },
  128. { 0x0738, 0x4522, "Mad Catz LumiCON", 0, XTYPE_XBOX },
  129. { 0x0738, 0x4526, "Mad Catz Control Pad Pro", 0, XTYPE_XBOX },
  130. { 0x0738, 0x4536, "Mad Catz MicroCON", 0, XTYPE_XBOX },
  131. { 0x0738, 0x4540, "Mad Catz Beat Pad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
  132. { 0x0738, 0x4556, "Mad Catz Lynx Wireless Controller", 0, XTYPE_XBOX },
  133. { 0x0738, 0x4716, "Mad Catz Wired Xbox 360 Controller", 0, XTYPE_XBOX360 },
  134. { 0x0738, 0x4728, "Mad Catz Street Fighter IV FightPad", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  135. { 0x0738, 0x4738, "Mad Catz Wired Xbox 360 Controller (SFIV)", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  136. { 0x0738, 0x6040, "Mad Catz Beat Pad Pro", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
  137. { 0x0738, 0xbeef, "Mad Catz JOYTECH NEO SE Advanced GamePad", XTYPE_XBOX360 },
  138. { 0x0c12, 0x8802, "Zeroplus Xbox Controller", 0, XTYPE_XBOX },
  139. { 0x0c12, 0x8809, "RedOctane Xbox Dance Pad", DANCEPAD_MAP_CONFIG, XTYPE_XBOX },
  140. { 0x0c12, 0x880a, "Pelican Eclipse PL-2023", 0, XTYPE_XBOX },
  141. { 0x0c12, 0x8810, "Zeroplus Xbox Controller", 0, XTYPE_XBOX },
  142. { 0x0c12, 0x9902, "HAMA VibraX - *FAULTY HARDWARE*", 0, XTYPE_XBOX },
  143. { 0x0d2f, 0x0002, "Andamiro Pump It Up pad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
  144. { 0x0e4c, 0x1097, "Radica Gamester Controller", 0, XTYPE_XBOX },
  145. { 0x0e4c, 0x2390, "Radica Games Jtech Controller", 0, XTYPE_XBOX },
  146. { 0x0e6f, 0x0003, "Logic3 Freebird wireless Controller", 0, XTYPE_XBOX },
  147. { 0x0e6f, 0x0005, "Eclipse wireless Controller", 0, XTYPE_XBOX },
  148. { 0x0e6f, 0x0006, "Edge wireless Controller", 0, XTYPE_XBOX },
  149. { 0x0e6f, 0x0105, "HSM3 Xbox360 dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
  150. { 0x0e6f, 0x0201, "Pelican PL-3601 'TSZ' Wired Xbox 360 Controller", 0, XTYPE_XBOX360 },
  151. { 0x0e6f, 0x0213, "Afterglow Gamepad for Xbox 360", 0, XTYPE_XBOX360 },
  152. { 0x0e8f, 0x0201, "SmartJoy Frag Xpad/PS2 adaptor", 0, XTYPE_XBOX },
  153. { 0x0f0d, 0x000d, "Hori Fighting Stick EX2", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  154. { 0x0f0d, 0x0016, "Hori Real Arcade Pro.EX", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  155. { 0x0f30, 0x0202, "Joytech Advanced Controller", 0, XTYPE_XBOX },
  156. { 0x0f30, 0x8888, "BigBen XBMiniPad Controller", 0, XTYPE_XBOX },
  157. { 0x102c, 0xff0c, "Joytech Wireless Advanced Controller", 0, XTYPE_XBOX },
  158. { 0x12ab, 0x0004, "Honey Bee Xbox360 dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
  159. { 0x12ab, 0x8809, "Xbox DDR dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
  160. { 0x1430, 0x4748, "RedOctane Guitar Hero X-plorer", 0, XTYPE_XBOX360 },
  161. { 0x1430, 0x8888, "TX6500+ Dance Pad (first generation)", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
  162. { 0x146b, 0x0601, "BigBen Interactive XBOX 360 Controller", 0, XTYPE_XBOX360 },
  163. { 0x1689, 0xfd00, "Razer Onza Tournament Edition", 0, XTYPE_XBOX360 },
  164. { 0x1689, 0xfd01, "Razer Onza Classic Edition", 0, XTYPE_XBOX360 },
  165. { 0x1bad, 0x0002, "Harmonix Rock Band Guitar", 0, XTYPE_XBOX360 },
  166. { 0x1bad, 0x0003, "Harmonix Rock Band Drumkit", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
  167. { 0x1bad, 0xf016, "Mad Catz Xbox 360 Controller", 0, XTYPE_XBOX360 },
  168. { 0x1bad, 0xf028, "Street Fighter IV FightPad", 0, XTYPE_XBOX360 },
  169. { 0x1bad, 0xf901, "Gamestop Xbox 360 Controller", 0, XTYPE_XBOX360 },
  170. { 0x1bad, 0xf903, "Tron Xbox 360 controller", 0, XTYPE_XBOX360 },
  171. { 0x24c6, 0x5300, "PowerA MINI PROEX Controller", 0, XTYPE_XBOX360 },
  172. { 0xffff, 0xffff, "Chinese-made Xbox Controller", 0, XTYPE_XBOX },
  173. { 0x0000, 0x0000, "Generic X-Box pad", 0, XTYPE_UNKNOWN }
  174. };
  175. /* buttons shared with xbox and xbox360 */
  176. static const signed short xpad_common_btn[] = {
  177. BTN_A, BTN_B, BTN_X, BTN_Y, /* "analog" buttons */
  178. BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR, /* start/back/sticks */
  179. -1 /* terminating entry */
  180. };
  181. /* original xbox controllers only */
  182. static const signed short xpad_btn[] = {
  183. BTN_C, BTN_Z, /* "analog" buttons */
  184. -1 /* terminating entry */
  185. };
  186. /* used when dpad is mapped to buttons */
  187. static const signed short xpad_btn_pad[] = {
  188. BTN_TRIGGER_HAPPY1, BTN_TRIGGER_HAPPY2, /* d-pad left, right */
  189. BTN_TRIGGER_HAPPY3, BTN_TRIGGER_HAPPY4, /* d-pad up, down */
  190. -1 /* terminating entry */
  191. };
  192. /* used when triggers are mapped to buttons */
  193. static const signed short xpad_btn_triggers[] = {
  194. BTN_TL2, BTN_TR2, /* triggers left/right */
  195. -1
  196. };
  197. static const signed short xpad360_btn[] = { /* buttons for x360 controller */
  198. BTN_TL, BTN_TR, /* Button LB/RB */
  199. BTN_MODE, /* The big X button */
  200. -1
  201. };
  202. static const signed short xpad_abs[] = {
  203. ABS_X, ABS_Y, /* left stick */
  204. ABS_RX, ABS_RY, /* right stick */
  205. -1 /* terminating entry */
  206. };
  207. /* used when dpad is mapped to axes */
  208. static const signed short xpad_abs_pad[] = {
  209. ABS_HAT0X, ABS_HAT0Y, /* d-pad axes */
  210. -1 /* terminating entry */
  211. };
  212. /* used when triggers are mapped to axes */
  213. static const signed short xpad_abs_triggers[] = {
  214. ABS_Z, ABS_RZ, /* triggers left/right */
  215. -1
  216. };
  217. /*
  218. * Xbox 360 has a vendor-specific class, so we cannot match it with only
  219. * USB_INTERFACE_INFO (also specifically refused by USB subsystem), so we
  220. * match against vendor id as well. Wired Xbox 360 devices have protocol 1,
  221. * wireless controllers have protocol 129.
  222. */
  223. #define XPAD_XBOX360_VENDOR_PROTOCOL(vend,pr) \
  224. .match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_INT_INFO, \
  225. .idVendor = (vend), \
  226. .bInterfaceClass = USB_CLASS_VENDOR_SPEC, \
  227. .bInterfaceSubClass = 93, \
  228. .bInterfaceProtocol = (pr)
  229. #define XPAD_XBOX360_VENDOR(vend) \
  230. { XPAD_XBOX360_VENDOR_PROTOCOL(vend,1) }, \
  231. { XPAD_XBOX360_VENDOR_PROTOCOL(vend,129) }
  232. /* The Xbox One controller uses subclass 71 and protocol 208. */
  233. #define XPAD_XBOXONE_VENDOR_PROTOCOL(vend, pr) \
  234. .match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_INT_INFO, \
  235. .idVendor = (vend), \
  236. .bInterfaceClass = USB_CLASS_VENDOR_SPEC, \
  237. .bInterfaceSubClass = 71, \
  238. .bInterfaceProtocol = (pr)
  239. #define XPAD_XBOXONE_VENDOR(vend) \
  240. { XPAD_XBOXONE_VENDOR_PROTOCOL(vend, 208) }
  241. static struct usb_device_id xpad_table[] = {
  242. { USB_INTERFACE_INFO('X', 'B', 0) }, /* X-Box USB-IF not approved class */
  243. XPAD_XBOX360_VENDOR(0x045e), /* Microsoft X-Box 360 controllers */
  244. XPAD_XBOXONE_VENDOR(0x045e), /* Microsoft X-Box One controllers */
  245. XPAD_XBOX360_VENDOR(0x046d), /* Logitech X-Box 360 style controllers */
  246. XPAD_XBOX360_VENDOR(0x0738), /* Mad Catz X-Box 360 controllers */
  247. { USB_DEVICE(0x0738, 0x4540) }, /* Mad Catz Beat Pad */
  248. XPAD_XBOX360_VENDOR(0x0e6f), /* 0x0e6f X-Box 360 controllers */
  249. XPAD_XBOX360_VENDOR(0x12ab), /* X-Box 360 dance pads */
  250. XPAD_XBOX360_VENDOR(0x1430), /* RedOctane X-Box 360 controllers */
  251. XPAD_XBOX360_VENDOR(0x146b), /* BigBen Interactive Controllers */
  252. XPAD_XBOX360_VENDOR(0x1bad), /* Harminix Rock Band Guitar and Drums */
  253. XPAD_XBOX360_VENDOR(0x0f0d), /* Hori Controllers */
  254. XPAD_XBOX360_VENDOR(0x1689), /* Razer Onza */
  255. XPAD_XBOX360_VENDOR(0x24c6), /* PowerA Controllers */
  256. { }
  257. };
  258. MODULE_DEVICE_TABLE(usb, xpad_table);
  259. struct usb_xpad {
  260. struct input_dev *dev; /* input device interface */
  261. struct usb_device *udev; /* usb device */
  262. struct usb_interface *intf; /* usb interface */
  263. int pad_present;
  264. struct urb *irq_in; /* urb for interrupt in report */
  265. unsigned char *idata; /* input data */
  266. dma_addr_t idata_dma;
  267. struct urb *bulk_out;
  268. unsigned char *bdata;
  269. struct urb *irq_out; /* urb for interrupt out report */
  270. unsigned char *odata; /* output data */
  271. dma_addr_t odata_dma;
  272. struct mutex odata_mutex;
  273. #if defined(CONFIG_JOYSTICK_XPAD_LEDS)
  274. struct xpad_led *led;
  275. #endif
  276. char phys[64]; /* physical device path */
  277. int mapping; /* map d-pad to buttons or to axes */
  278. int xtype; /* type of xbox device */
  279. };
  280. /*
  281. * xpad_process_packet
  282. *
  283. * Completes a request by converting the data into events for the
  284. * input subsystem.
  285. *
  286. * The used report descriptor was taken from ITO Takayukis website:
  287. * http://euc.jp/periphs/xbox-controller.ja.html
  288. */
  289. static void xpad_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data)
  290. {
  291. struct input_dev *dev = xpad->dev;
  292. if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
  293. /* left stick */
  294. input_report_abs(dev, ABS_X,
  295. (__s16) le16_to_cpup((__le16 *)(data + 12)));
  296. input_report_abs(dev, ABS_Y,
  297. ~(__s16) le16_to_cpup((__le16 *)(data + 14)));
  298. /* right stick */
  299. input_report_abs(dev, ABS_RX,
  300. (__s16) le16_to_cpup((__le16 *)(data + 16)));
  301. input_report_abs(dev, ABS_RY,
  302. ~(__s16) le16_to_cpup((__le16 *)(data + 18)));
  303. }
  304. /* triggers left/right */
  305. if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
  306. input_report_key(dev, BTN_TL2, data[10]);
  307. input_report_key(dev, BTN_TR2, data[11]);
  308. } else {
  309. input_report_abs(dev, ABS_Z, data[10]);
  310. input_report_abs(dev, ABS_RZ, data[11]);
  311. }
  312. /* digital pad */
  313. if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
  314. /* dpad as buttons (left, right, up, down) */
  315. input_report_key(dev, BTN_TRIGGER_HAPPY1, data[2] & 0x04);
  316. input_report_key(dev, BTN_TRIGGER_HAPPY2, data[2] & 0x08);
  317. input_report_key(dev, BTN_TRIGGER_HAPPY3, data[2] & 0x01);
  318. input_report_key(dev, BTN_TRIGGER_HAPPY4, data[2] & 0x02);
  319. } else {
  320. input_report_abs(dev, ABS_HAT0X,
  321. !!(data[2] & 0x08) - !!(data[2] & 0x04));
  322. input_report_abs(dev, ABS_HAT0Y,
  323. !!(data[2] & 0x02) - !!(data[2] & 0x01));
  324. }
  325. /* start/back buttons and stick press left/right */
  326. input_report_key(dev, BTN_START, data[2] & 0x10);
  327. input_report_key(dev, BTN_SELECT, data[2] & 0x20);
  328. input_report_key(dev, BTN_THUMBL, data[2] & 0x40);
  329. input_report_key(dev, BTN_THUMBR, data[2] & 0x80);
  330. /* "analog" buttons A, B, X, Y */
  331. input_report_key(dev, BTN_A, data[4]);
  332. input_report_key(dev, BTN_B, data[5]);
  333. input_report_key(dev, BTN_X, data[6]);
  334. input_report_key(dev, BTN_Y, data[7]);
  335. /* "analog" buttons black, white */
  336. input_report_key(dev, BTN_C, data[8]);
  337. input_report_key(dev, BTN_Z, data[9]);
  338. input_sync(dev);
  339. }
  340. /*
  341. * xpad360_process_packet
  342. *
  343. * Completes a request by converting the data into events for the
  344. * input subsystem. It is version for xbox 360 controller
  345. *
  346. * The used report descriptor was taken from:
  347. * http://www.free60.org/wiki/Gamepad
  348. */
  349. static void xpad360_process_packet(struct usb_xpad *xpad,
  350. u16 cmd, unsigned char *data)
  351. {
  352. struct input_dev *dev = xpad->dev;
  353. /* digital pad */
  354. if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
  355. /* dpad as buttons (left, right, up, down) */
  356. input_report_key(dev, BTN_TRIGGER_HAPPY1, data[2] & 0x04);
  357. input_report_key(dev, BTN_TRIGGER_HAPPY2, data[2] & 0x08);
  358. input_report_key(dev, BTN_TRIGGER_HAPPY3, data[2] & 0x01);
  359. input_report_key(dev, BTN_TRIGGER_HAPPY4, data[2] & 0x02);
  360. } else {
  361. input_report_abs(dev, ABS_HAT0X,
  362. !!(data[2] & 0x08) - !!(data[2] & 0x04));
  363. input_report_abs(dev, ABS_HAT0Y,
  364. !!(data[2] & 0x02) - !!(data[2] & 0x01));
  365. }
  366. /* start/back buttons */
  367. input_report_key(dev, BTN_START, data[2] & 0x10);
  368. input_report_key(dev, BTN_SELECT, data[2] & 0x20);
  369. /* stick press left/right */
  370. input_report_key(dev, BTN_THUMBL, data[2] & 0x40);
  371. input_report_key(dev, BTN_THUMBR, data[2] & 0x80);
  372. /* buttons A,B,X,Y,TL,TR and MODE */
  373. input_report_key(dev, BTN_A, data[3] & 0x10);
  374. input_report_key(dev, BTN_B, data[3] & 0x20);
  375. input_report_key(dev, BTN_X, data[3] & 0x40);
  376. input_report_key(dev, BTN_Y, data[3] & 0x80);
  377. input_report_key(dev, BTN_TL, data[3] & 0x01);
  378. input_report_key(dev, BTN_TR, data[3] & 0x02);
  379. input_report_key(dev, BTN_MODE, data[3] & 0x04);
  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 + 6)));
  384. input_report_abs(dev, ABS_Y,
  385. ~(__s16) le16_to_cpup((__le16 *)(data + 8)));
  386. /* right stick */
  387. input_report_abs(dev, ABS_RX,
  388. (__s16) le16_to_cpup((__le16 *)(data + 10)));
  389. input_report_abs(dev, ABS_RY,
  390. ~(__s16) le16_to_cpup((__le16 *)(data + 12)));
  391. }
  392. /* triggers left/right */
  393. if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
  394. input_report_key(dev, BTN_TL2, data[4]);
  395. input_report_key(dev, BTN_TR2, data[5]);
  396. } else {
  397. input_report_abs(dev, ABS_Z, data[4]);
  398. input_report_abs(dev, ABS_RZ, data[5]);
  399. }
  400. input_sync(dev);
  401. }
  402. /*
  403. * xpad360w_process_packet
  404. *
  405. * Completes a request by converting the data into events for the
  406. * input subsystem. It is version for xbox 360 wireless controller.
  407. *
  408. * Byte.Bit
  409. * 00.1 - Status change: The controller or headset has connected/disconnected
  410. * Bits 01.7 and 01.6 are valid
  411. * 01.7 - Controller present
  412. * 01.6 - Headset present
  413. * 01.1 - Pad state (Bytes 4+) valid
  414. *
  415. */
  416. static void xpad360w_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data)
  417. {
  418. /* Presence change */
  419. if (data[0] & 0x08) {
  420. if (data[1] & 0x80) {
  421. xpad->pad_present = 1;
  422. usb_submit_urb(xpad->bulk_out, GFP_ATOMIC);
  423. } else
  424. xpad->pad_present = 0;
  425. }
  426. /* Valid pad data */
  427. if (!(data[1] & 0x1))
  428. return;
  429. xpad360_process_packet(xpad, cmd, &data[4]);
  430. }
  431. /*
  432. * xpadone_process_buttons
  433. *
  434. * Process a button update packet from an Xbox one controller.
  435. */
  436. static void xpadone_process_buttons(struct usb_xpad *xpad,
  437. struct input_dev *dev,
  438. unsigned char *data)
  439. {
  440. /* menu/view buttons */
  441. input_report_key(dev, BTN_START, data[4] & 0x04);
  442. input_report_key(dev, BTN_SELECT, data[4] & 0x08);
  443. /* buttons A,B,X,Y */
  444. input_report_key(dev, BTN_A, data[4] & 0x10);
  445. input_report_key(dev, BTN_B, data[4] & 0x20);
  446. input_report_key(dev, BTN_X, data[4] & 0x40);
  447. input_report_key(dev, BTN_Y, data[4] & 0x80);
  448. /* digital pad */
  449. if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
  450. /* dpad as buttons (left, right, up, down) */
  451. input_report_key(dev, BTN_TRIGGER_HAPPY1, data[5] & 0x04);
  452. input_report_key(dev, BTN_TRIGGER_HAPPY2, data[5] & 0x08);
  453. input_report_key(dev, BTN_TRIGGER_HAPPY3, data[5] & 0x01);
  454. input_report_key(dev, BTN_TRIGGER_HAPPY4, data[5] & 0x02);
  455. } else {
  456. input_report_abs(dev, ABS_HAT0X,
  457. !!(data[5] & 0x08) - !!(data[5] & 0x04));
  458. input_report_abs(dev, ABS_HAT0Y,
  459. !!(data[5] & 0x02) - !!(data[5] & 0x01));
  460. }
  461. /* TL/TR */
  462. input_report_key(dev, BTN_TL, data[5] & 0x10);
  463. input_report_key(dev, BTN_TR, data[5] & 0x20);
  464. /* stick press left/right */
  465. input_report_key(dev, BTN_THUMBL, data[5] & 0x40);
  466. input_report_key(dev, BTN_THUMBR, data[5] & 0x80);
  467. if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
  468. /* left stick */
  469. input_report_abs(dev, ABS_X,
  470. (__s16) le16_to_cpup((__le16 *)(data + 10)));
  471. input_report_abs(dev, ABS_Y,
  472. ~(__s16) le16_to_cpup((__le16 *)(data + 12)));
  473. /* right stick */
  474. input_report_abs(dev, ABS_RX,
  475. (__s16) le16_to_cpup((__le16 *)(data + 14)));
  476. input_report_abs(dev, ABS_RY,
  477. ~(__s16) le16_to_cpup((__le16 *)(data + 16)));
  478. }
  479. /* triggers left/right */
  480. if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
  481. input_report_key(dev, BTN_TL2,
  482. (__u16) le16_to_cpup((__le16 *)(data + 6)));
  483. input_report_key(dev, BTN_TR2,
  484. (__u16) le16_to_cpup((__le16 *)(data + 8)));
  485. } else {
  486. input_report_abs(dev, ABS_Z,
  487. (__u16) le16_to_cpup((__le16 *)(data + 6)));
  488. input_report_abs(dev, ABS_RZ,
  489. (__u16) le16_to_cpup((__le16 *)(data + 8)));
  490. }
  491. input_sync(dev);
  492. }
  493. /*
  494. * xpadone_process_packet
  495. *
  496. * Completes a request by converting the data into events for the
  497. * input subsystem. This version is for the Xbox One controller.
  498. *
  499. * The report format was gleaned from
  500. * https://github.com/kylelemons/xbox/blob/master/xbox.go
  501. */
  502. static void xpadone_process_packet(struct usb_xpad *xpad,
  503. u16 cmd, unsigned char *data)
  504. {
  505. struct input_dev *dev = xpad->dev;
  506. switch (data[0]) {
  507. case 0x20:
  508. xpadone_process_buttons(xpad, dev, data);
  509. break;
  510. case 0x07:
  511. /* the xbox button has its own special report */
  512. input_report_key(dev, BTN_MODE, data[4] & 0x01);
  513. input_sync(dev);
  514. break;
  515. }
  516. }
  517. static void xpad_irq_in(struct urb *urb)
  518. {
  519. struct usb_xpad *xpad = urb->context;
  520. struct device *dev = &xpad->intf->dev;
  521. int retval, status;
  522. status = urb->status;
  523. switch (status) {
  524. case 0:
  525. /* success */
  526. break;
  527. case -ECONNRESET:
  528. case -ENOENT:
  529. case -ESHUTDOWN:
  530. /* this urb is terminated, clean up */
  531. dev_dbg(dev, "%s - urb shutting down with status: %d\n",
  532. __func__, status);
  533. return;
  534. default:
  535. dev_dbg(dev, "%s - nonzero urb status received: %d\n",
  536. __func__, status);
  537. goto exit;
  538. }
  539. switch (xpad->xtype) {
  540. case XTYPE_XBOX360:
  541. xpad360_process_packet(xpad, 0, xpad->idata);
  542. break;
  543. case XTYPE_XBOX360W:
  544. xpad360w_process_packet(xpad, 0, xpad->idata);
  545. break;
  546. case XTYPE_XBOXONE:
  547. xpadone_process_packet(xpad, 0, xpad->idata);
  548. break;
  549. default:
  550. xpad_process_packet(xpad, 0, xpad->idata);
  551. }
  552. exit:
  553. retval = usb_submit_urb(urb, GFP_ATOMIC);
  554. if (retval)
  555. dev_err(dev, "%s - usb_submit_urb failed with result %d\n",
  556. __func__, retval);
  557. }
  558. static void xpad_bulk_out(struct urb *urb)
  559. {
  560. struct usb_xpad *xpad = urb->context;
  561. struct device *dev = &xpad->intf->dev;
  562. switch (urb->status) {
  563. case 0:
  564. /* success */
  565. break;
  566. case -ECONNRESET:
  567. case -ENOENT:
  568. case -ESHUTDOWN:
  569. /* this urb is terminated, clean up */
  570. dev_dbg(dev, "%s - urb shutting down with status: %d\n",
  571. __func__, urb->status);
  572. break;
  573. default:
  574. dev_dbg(dev, "%s - nonzero urb status received: %d\n",
  575. __func__, urb->status);
  576. }
  577. }
  578. static void xpad_irq_out(struct urb *urb)
  579. {
  580. struct usb_xpad *xpad = urb->context;
  581. struct device *dev = &xpad->intf->dev;
  582. int retval, status;
  583. status = urb->status;
  584. switch (status) {
  585. case 0:
  586. /* success */
  587. return;
  588. case -ECONNRESET:
  589. case -ENOENT:
  590. case -ESHUTDOWN:
  591. /* this urb is terminated, clean up */
  592. dev_dbg(dev, "%s - urb shutting down with status: %d\n",
  593. __func__, status);
  594. return;
  595. default:
  596. dev_dbg(dev, "%s - nonzero urb status received: %d\n",
  597. __func__, status);
  598. goto exit;
  599. }
  600. exit:
  601. retval = usb_submit_urb(urb, GFP_ATOMIC);
  602. if (retval)
  603. dev_err(dev, "%s - usb_submit_urb failed with result %d\n",
  604. __func__, retval);
  605. }
  606. static int xpad_init_output(struct usb_interface *intf, struct usb_xpad *xpad)
  607. {
  608. struct usb_endpoint_descriptor *ep_irq_out;
  609. int ep_irq_out_idx;
  610. int error;
  611. if (xpad->xtype == XTYPE_UNKNOWN)
  612. return 0;
  613. xpad->odata = usb_alloc_coherent(xpad->udev, XPAD_PKT_LEN,
  614. GFP_KERNEL, &xpad->odata_dma);
  615. if (!xpad->odata) {
  616. error = -ENOMEM;
  617. goto fail1;
  618. }
  619. mutex_init(&xpad->odata_mutex);
  620. xpad->irq_out = usb_alloc_urb(0, GFP_KERNEL);
  621. if (!xpad->irq_out) {
  622. error = -ENOMEM;
  623. goto fail2;
  624. }
  625. /* Xbox One controller has in/out endpoints swapped. */
  626. ep_irq_out_idx = xpad->xtype == XTYPE_XBOXONE ? 0 : 1;
  627. ep_irq_out = &intf->cur_altsetting->endpoint[ep_irq_out_idx].desc;
  628. usb_fill_int_urb(xpad->irq_out, xpad->udev,
  629. usb_sndintpipe(xpad->udev, ep_irq_out->bEndpointAddress),
  630. xpad->odata, XPAD_PKT_LEN,
  631. xpad_irq_out, xpad, ep_irq_out->bInterval);
  632. xpad->irq_out->transfer_dma = xpad->odata_dma;
  633. xpad->irq_out->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  634. return 0;
  635. fail2: usb_free_coherent(xpad->udev, XPAD_PKT_LEN, xpad->odata, xpad->odata_dma);
  636. fail1: return error;
  637. }
  638. static void xpad_stop_output(struct usb_xpad *xpad)
  639. {
  640. if (xpad->xtype != XTYPE_UNKNOWN)
  641. usb_kill_urb(xpad->irq_out);
  642. }
  643. static void xpad_deinit_output(struct usb_xpad *xpad)
  644. {
  645. if (xpad->xtype != XTYPE_UNKNOWN) {
  646. usb_free_urb(xpad->irq_out);
  647. usb_free_coherent(xpad->udev, XPAD_PKT_LEN,
  648. xpad->odata, xpad->odata_dma);
  649. }
  650. }
  651. #ifdef CONFIG_JOYSTICK_XPAD_FF
  652. static int xpad_play_effect(struct input_dev *dev, void *data, struct ff_effect *effect)
  653. {
  654. struct usb_xpad *xpad = input_get_drvdata(dev);
  655. if (effect->type == FF_RUMBLE) {
  656. __u16 strong = effect->u.rumble.strong_magnitude;
  657. __u16 weak = effect->u.rumble.weak_magnitude;
  658. switch (xpad->xtype) {
  659. case XTYPE_XBOX:
  660. xpad->odata[0] = 0x00;
  661. xpad->odata[1] = 0x06;
  662. xpad->odata[2] = 0x00;
  663. xpad->odata[3] = strong / 256; /* left actuator */
  664. xpad->odata[4] = 0x00;
  665. xpad->odata[5] = weak / 256; /* right actuator */
  666. xpad->irq_out->transfer_buffer_length = 6;
  667. return usb_submit_urb(xpad->irq_out, GFP_ATOMIC);
  668. case XTYPE_XBOX360:
  669. xpad->odata[0] = 0x00;
  670. xpad->odata[1] = 0x08;
  671. xpad->odata[2] = 0x00;
  672. xpad->odata[3] = strong / 256; /* left actuator? */
  673. xpad->odata[4] = weak / 256; /* right actuator? */
  674. xpad->odata[5] = 0x00;
  675. xpad->odata[6] = 0x00;
  676. xpad->odata[7] = 0x00;
  677. xpad->irq_out->transfer_buffer_length = 8;
  678. return usb_submit_urb(xpad->irq_out, GFP_ATOMIC);
  679. case XTYPE_XBOX360W:
  680. xpad->odata[0] = 0x00;
  681. xpad->odata[1] = 0x01;
  682. xpad->odata[2] = 0x0F;
  683. xpad->odata[3] = 0xC0;
  684. xpad->odata[4] = 0x00;
  685. xpad->odata[5] = strong / 256;
  686. xpad->odata[6] = weak / 256;
  687. xpad->odata[7] = 0x00;
  688. xpad->odata[8] = 0x00;
  689. xpad->odata[9] = 0x00;
  690. xpad->odata[10] = 0x00;
  691. xpad->odata[11] = 0x00;
  692. xpad->irq_out->transfer_buffer_length = 12;
  693. return usb_submit_urb(xpad->irq_out, GFP_ATOMIC);
  694. default:
  695. dev_dbg(&xpad->dev->dev,
  696. "%s - rumble command sent to unsupported xpad type: %d\n",
  697. __func__, xpad->xtype);
  698. return -1;
  699. }
  700. }
  701. return 0;
  702. }
  703. static int xpad_init_ff(struct usb_xpad *xpad)
  704. {
  705. if (xpad->xtype == XTYPE_UNKNOWN || xpad->xtype == XTYPE_XBOXONE)
  706. return 0;
  707. input_set_capability(xpad->dev, EV_FF, FF_RUMBLE);
  708. return input_ff_create_memless(xpad->dev, NULL, xpad_play_effect);
  709. }
  710. #else
  711. static int xpad_init_ff(struct usb_xpad *xpad) { return 0; }
  712. #endif
  713. #if defined(CONFIG_JOYSTICK_XPAD_LEDS)
  714. #include <linux/leds.h>
  715. struct xpad_led {
  716. char name[16];
  717. struct led_classdev led_cdev;
  718. struct usb_xpad *xpad;
  719. };
  720. static void xpad_send_led_command(struct usb_xpad *xpad, int command)
  721. {
  722. if (command >= 0 && command < 14) {
  723. mutex_lock(&xpad->odata_mutex);
  724. xpad->odata[0] = 0x01;
  725. xpad->odata[1] = 0x03;
  726. xpad->odata[2] = command;
  727. xpad->irq_out->transfer_buffer_length = 3;
  728. usb_submit_urb(xpad->irq_out, GFP_KERNEL);
  729. mutex_unlock(&xpad->odata_mutex);
  730. }
  731. }
  732. static void xpad_led_set(struct led_classdev *led_cdev,
  733. enum led_brightness value)
  734. {
  735. struct xpad_led *xpad_led = container_of(led_cdev,
  736. struct xpad_led, led_cdev);
  737. xpad_send_led_command(xpad_led->xpad, value);
  738. }
  739. static int xpad_led_probe(struct usb_xpad *xpad)
  740. {
  741. static atomic_t led_seq = ATOMIC_INIT(0);
  742. long led_no;
  743. struct xpad_led *led;
  744. struct led_classdev *led_cdev;
  745. int error;
  746. if (xpad->xtype != XTYPE_XBOX360)
  747. return 0;
  748. xpad->led = led = kzalloc(sizeof(struct xpad_led), GFP_KERNEL);
  749. if (!led)
  750. return -ENOMEM;
  751. led_no = (long)atomic_inc_return(&led_seq) - 1;
  752. snprintf(led->name, sizeof(led->name), "xpad%ld", led_no);
  753. led->xpad = xpad;
  754. led_cdev = &led->led_cdev;
  755. led_cdev->name = led->name;
  756. led_cdev->brightness_set = xpad_led_set;
  757. error = led_classdev_register(&xpad->udev->dev, led_cdev);
  758. if (error) {
  759. kfree(led);
  760. xpad->led = NULL;
  761. return error;
  762. }
  763. /*
  764. * Light up the segment corresponding to controller number
  765. */
  766. xpad_send_led_command(xpad, (led_no % 4) + 2);
  767. return 0;
  768. }
  769. static void xpad_led_disconnect(struct usb_xpad *xpad)
  770. {
  771. struct xpad_led *xpad_led = xpad->led;
  772. if (xpad_led) {
  773. led_classdev_unregister(&xpad_led->led_cdev);
  774. kfree(xpad_led);
  775. }
  776. }
  777. #else
  778. static int xpad_led_probe(struct usb_xpad *xpad) { return 0; }
  779. static void xpad_led_disconnect(struct usb_xpad *xpad) { }
  780. #endif
  781. static int xpad_open(struct input_dev *dev)
  782. {
  783. struct usb_xpad *xpad = input_get_drvdata(dev);
  784. /* URB was submitted in probe */
  785. if (xpad->xtype == XTYPE_XBOX360W)
  786. return 0;
  787. xpad->irq_in->dev = xpad->udev;
  788. if (usb_submit_urb(xpad->irq_in, GFP_KERNEL))
  789. return -EIO;
  790. if (xpad->xtype == XTYPE_XBOXONE) {
  791. /* Xbox one controller needs to be initialized. */
  792. xpad->odata[0] = 0x05;
  793. xpad->odata[1] = 0x20;
  794. xpad->irq_out->transfer_buffer_length = 2;
  795. return usb_submit_urb(xpad->irq_out, GFP_KERNEL);
  796. }
  797. return 0;
  798. }
  799. static void xpad_close(struct input_dev *dev)
  800. {
  801. struct usb_xpad *xpad = input_get_drvdata(dev);
  802. if (xpad->xtype != XTYPE_XBOX360W)
  803. usb_kill_urb(xpad->irq_in);
  804. xpad_stop_output(xpad);
  805. }
  806. static void xpad_set_up_abs(struct input_dev *input_dev, signed short abs)
  807. {
  808. struct usb_xpad *xpad = input_get_drvdata(input_dev);
  809. set_bit(abs, input_dev->absbit);
  810. switch (abs) {
  811. case ABS_X:
  812. case ABS_Y:
  813. case ABS_RX:
  814. case ABS_RY: /* the two sticks */
  815. input_set_abs_params(input_dev, abs, -32768, 32767, 16, 128);
  816. break;
  817. case ABS_Z:
  818. case ABS_RZ: /* the triggers (if mapped to axes) */
  819. if (xpad->xtype == XTYPE_XBOXONE)
  820. input_set_abs_params(input_dev, abs, 0, 1023, 0, 0);
  821. else
  822. input_set_abs_params(input_dev, abs, 0, 255, 0, 0);
  823. break;
  824. case ABS_HAT0X:
  825. case ABS_HAT0Y: /* the d-pad (only if dpad is mapped to axes */
  826. input_set_abs_params(input_dev, abs, -1, 1, 0, 0);
  827. break;
  828. }
  829. }
  830. static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id)
  831. {
  832. struct usb_device *udev = interface_to_usbdev(intf);
  833. struct usb_xpad *xpad;
  834. struct input_dev *input_dev;
  835. struct usb_endpoint_descriptor *ep_irq_in;
  836. int ep_irq_in_idx;
  837. int i, error;
  838. for (i = 0; xpad_device[i].idVendor; i++) {
  839. if ((le16_to_cpu(udev->descriptor.idVendor) == xpad_device[i].idVendor) &&
  840. (le16_to_cpu(udev->descriptor.idProduct) == xpad_device[i].idProduct))
  841. break;
  842. }
  843. if (xpad_device[i].xtype == XTYPE_XBOXONE &&
  844. intf->cur_altsetting->desc.bInterfaceNumber != 0) {
  845. /*
  846. * The Xbox One controller lists three interfaces all with the
  847. * same interface class, subclass and protocol. Differentiate by
  848. * interface number.
  849. */
  850. return -ENODEV;
  851. }
  852. xpad = kzalloc(sizeof(struct usb_xpad), GFP_KERNEL);
  853. input_dev = input_allocate_device();
  854. if (!xpad || !input_dev) {
  855. error = -ENOMEM;
  856. goto fail1;
  857. }
  858. xpad->idata = usb_alloc_coherent(udev, XPAD_PKT_LEN,
  859. GFP_KERNEL, &xpad->idata_dma);
  860. if (!xpad->idata) {
  861. error = -ENOMEM;
  862. goto fail1;
  863. }
  864. xpad->irq_in = usb_alloc_urb(0, GFP_KERNEL);
  865. if (!xpad->irq_in) {
  866. error = -ENOMEM;
  867. goto fail2;
  868. }
  869. xpad->udev = udev;
  870. xpad->intf = intf;
  871. xpad->mapping = xpad_device[i].mapping;
  872. xpad->xtype = xpad_device[i].xtype;
  873. if (xpad->xtype == XTYPE_UNKNOWN) {
  874. if (intf->cur_altsetting->desc.bInterfaceClass == USB_CLASS_VENDOR_SPEC) {
  875. if (intf->cur_altsetting->desc.bInterfaceProtocol == 129)
  876. xpad->xtype = XTYPE_XBOX360W;
  877. else
  878. xpad->xtype = XTYPE_XBOX360;
  879. } else
  880. xpad->xtype = XTYPE_XBOX;
  881. if (dpad_to_buttons)
  882. xpad->mapping |= MAP_DPAD_TO_BUTTONS;
  883. if (triggers_to_buttons)
  884. xpad->mapping |= MAP_TRIGGERS_TO_BUTTONS;
  885. if (sticks_to_null)
  886. xpad->mapping |= MAP_STICKS_TO_NULL;
  887. }
  888. xpad->dev = input_dev;
  889. usb_make_path(udev, xpad->phys, sizeof(xpad->phys));
  890. strlcat(xpad->phys, "/input0", sizeof(xpad->phys));
  891. input_dev->name = xpad_device[i].name;
  892. input_dev->phys = xpad->phys;
  893. usb_to_input_id(udev, &input_dev->id);
  894. input_dev->dev.parent = &intf->dev;
  895. input_set_drvdata(input_dev, xpad);
  896. input_dev->open = xpad_open;
  897. input_dev->close = xpad_close;
  898. input_dev->evbit[0] = BIT_MASK(EV_KEY);
  899. if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
  900. input_dev->evbit[0] |= BIT_MASK(EV_ABS);
  901. /* set up axes */
  902. for (i = 0; xpad_abs[i] >= 0; i++)
  903. xpad_set_up_abs(input_dev, xpad_abs[i]);
  904. }
  905. /* set up standard buttons */
  906. for (i = 0; xpad_common_btn[i] >= 0; i++)
  907. __set_bit(xpad_common_btn[i], input_dev->keybit);
  908. /* set up model-specific ones */
  909. if (xpad->xtype == XTYPE_XBOX360 || xpad->xtype == XTYPE_XBOX360W ||
  910. xpad->xtype == XTYPE_XBOXONE) {
  911. for (i = 0; xpad360_btn[i] >= 0; i++)
  912. __set_bit(xpad360_btn[i], input_dev->keybit);
  913. } else {
  914. for (i = 0; xpad_btn[i] >= 0; i++)
  915. __set_bit(xpad_btn[i], input_dev->keybit);
  916. }
  917. if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
  918. for (i = 0; xpad_btn_pad[i] >= 0; i++)
  919. __set_bit(xpad_btn_pad[i], input_dev->keybit);
  920. } else {
  921. for (i = 0; xpad_abs_pad[i] >= 0; i++)
  922. xpad_set_up_abs(input_dev, xpad_abs_pad[i]);
  923. }
  924. if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
  925. for (i = 0; xpad_btn_triggers[i] >= 0; i++)
  926. __set_bit(xpad_btn_triggers[i], input_dev->keybit);
  927. } else {
  928. for (i = 0; xpad_abs_triggers[i] >= 0; i++)
  929. xpad_set_up_abs(input_dev, xpad_abs_triggers[i]);
  930. }
  931. error = xpad_init_output(intf, xpad);
  932. if (error)
  933. goto fail3;
  934. error = xpad_init_ff(xpad);
  935. if (error)
  936. goto fail4;
  937. error = xpad_led_probe(xpad);
  938. if (error)
  939. goto fail5;
  940. /* Xbox One controller has in/out endpoints swapped. */
  941. ep_irq_in_idx = xpad->xtype == XTYPE_XBOXONE ? 1 : 0;
  942. ep_irq_in = &intf->cur_altsetting->endpoint[ep_irq_in_idx].desc;
  943. usb_fill_int_urb(xpad->irq_in, udev,
  944. usb_rcvintpipe(udev, ep_irq_in->bEndpointAddress),
  945. xpad->idata, XPAD_PKT_LEN, xpad_irq_in,
  946. xpad, ep_irq_in->bInterval);
  947. xpad->irq_in->transfer_dma = xpad->idata_dma;
  948. xpad->irq_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  949. error = input_register_device(xpad->dev);
  950. if (error)
  951. goto fail6;
  952. usb_set_intfdata(intf, xpad);
  953. if (xpad->xtype == XTYPE_XBOX360W) {
  954. /*
  955. * Setup the message to set the LEDs on the
  956. * controller when it shows up
  957. */
  958. xpad->bulk_out = usb_alloc_urb(0, GFP_KERNEL);
  959. if (!xpad->bulk_out) {
  960. error = -ENOMEM;
  961. goto fail7;
  962. }
  963. xpad->bdata = kzalloc(XPAD_PKT_LEN, GFP_KERNEL);
  964. if (!xpad->bdata) {
  965. error = -ENOMEM;
  966. goto fail8;
  967. }
  968. xpad->bdata[2] = 0x08;
  969. switch (intf->cur_altsetting->desc.bInterfaceNumber) {
  970. case 0:
  971. xpad->bdata[3] = 0x42;
  972. break;
  973. case 2:
  974. xpad->bdata[3] = 0x43;
  975. break;
  976. case 4:
  977. xpad->bdata[3] = 0x44;
  978. break;
  979. case 6:
  980. xpad->bdata[3] = 0x45;
  981. }
  982. ep_irq_in = &intf->cur_altsetting->endpoint[1].desc;
  983. usb_fill_bulk_urb(xpad->bulk_out, udev,
  984. usb_sndbulkpipe(udev, ep_irq_in->bEndpointAddress),
  985. xpad->bdata, XPAD_PKT_LEN, xpad_bulk_out, xpad);
  986. /*
  987. * Submit the int URB immediately rather than waiting for open
  988. * because we get status messages from the device whether
  989. * or not any controllers are attached. In fact, it's
  990. * exactly the message that a controller has arrived that
  991. * we're waiting for.
  992. */
  993. xpad->irq_in->dev = xpad->udev;
  994. error = usb_submit_urb(xpad->irq_in, GFP_KERNEL);
  995. if (error)
  996. goto fail9;
  997. }
  998. return 0;
  999. fail9: kfree(xpad->bdata);
  1000. fail8: usb_free_urb(xpad->bulk_out);
  1001. fail7: input_unregister_device(input_dev);
  1002. input_dev = NULL;
  1003. fail6: xpad_led_disconnect(xpad);
  1004. fail5: if (input_dev)
  1005. input_ff_destroy(input_dev);
  1006. fail4: xpad_deinit_output(xpad);
  1007. fail3: usb_free_urb(xpad->irq_in);
  1008. fail2: usb_free_coherent(udev, XPAD_PKT_LEN, xpad->idata, xpad->idata_dma);
  1009. fail1: input_free_device(input_dev);
  1010. kfree(xpad);
  1011. return error;
  1012. }
  1013. static void xpad_disconnect(struct usb_interface *intf)
  1014. {
  1015. struct usb_xpad *xpad = usb_get_intfdata (intf);
  1016. xpad_led_disconnect(xpad);
  1017. input_unregister_device(xpad->dev);
  1018. xpad_deinit_output(xpad);
  1019. if (xpad->xtype == XTYPE_XBOX360W) {
  1020. usb_kill_urb(xpad->bulk_out);
  1021. usb_free_urb(xpad->bulk_out);
  1022. usb_kill_urb(xpad->irq_in);
  1023. }
  1024. usb_free_urb(xpad->irq_in);
  1025. usb_free_coherent(xpad->udev, XPAD_PKT_LEN,
  1026. xpad->idata, xpad->idata_dma);
  1027. kfree(xpad->bdata);
  1028. kfree(xpad);
  1029. usb_set_intfdata(intf, NULL);
  1030. }
  1031. static struct usb_driver xpad_driver = {
  1032. .name = "xpad",
  1033. .probe = xpad_probe,
  1034. .disconnect = xpad_disconnect,
  1035. .id_table = xpad_table,
  1036. };
  1037. module_usb_driver(xpad_driver);
  1038. MODULE_AUTHOR(DRIVER_AUTHOR);
  1039. MODULE_DESCRIPTION(DRIVER_DESC);
  1040. MODULE_LICENSE("GPL");