xpad.c 41 KB

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