xpad.c 41 KB

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