xpad.c 50 KB

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