xpad.c 61 KB

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