xpad.c 62 KB

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