xpad.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Xbox gamepad driver
  4. *
  5. * Copyright (c) 2002 Marko Friedemann <mfr@bmx-chemnitz.de>
  6. * 2004 Oliver Schwartz <Oliver.Schwartz@gmx.de>,
  7. * Steven Toth <steve@toth.demon.co.uk>,
  8. * Franz Lehner <franz@caos.at>,
  9. * Ivan Hawkes <blackhawk@ivanhawkes.com>
  10. * 2005 Dominic Cerquetti <binary1230@yahoo.com>
  11. * 2006 Adam Buchbinder <adam.buchbinder@gmail.com>
  12. * 2007 Jan Kratochvil <honza@jikos.cz>
  13. * 2010 Christoph Fritz <chf.fritz@googlemail.com>
  14. *
  15. * This driver is based on:
  16. * - information from http://euc.jp/periphs/xbox-controller.ja.html
  17. * - the iForce driver drivers/char/joystick/iforce.c
  18. * - the skeleton-driver drivers/usb/usb-skeleton.c
  19. * - Xbox 360 information http://www.free60.org/wiki/Gamepad
  20. * - Xbox One information https://github.com/quantus/xbox-one-controller-protocol
  21. *
  22. * Thanks to:
  23. * - ITO Takayuki for providing essential xpad information on his website
  24. * - Vojtech Pavlik - iforce driver / input subsystem
  25. * - Greg Kroah-Hartman - usb-skeleton driver
  26. * - Xbox Linux project - extra USB IDs
  27. * - Pekka Pöyry (quantus) - Xbox One controller reverse-engineering
  28. *
  29. * TODO:
  30. * - fine tune axes (especially trigger axes)
  31. * - fix "analog" buttons (reported as digital now)
  32. * - get rumble working
  33. * - need USB IDs for other dance pads
  34. *
  35. * History:
  36. *
  37. * 2002-06-27 - 0.0.1 : first version, just said "XBOX HID controller"
  38. *
  39. * 2002-07-02 - 0.0.2 : basic working version
  40. * - all axes and 9 of the 10 buttons work (german InterAct device)
  41. * - the black button does not work
  42. *
  43. * 2002-07-14 - 0.0.3 : rework by Vojtech Pavlik
  44. * - indentation fixes
  45. * - usb + input init sequence fixes
  46. *
  47. * 2002-07-16 - 0.0.4 : minor changes, merge with Vojtech's v0.0.3
  48. * - verified the lack of HID and report descriptors
  49. * - verified that ALL buttons WORK
  50. * - fixed d-pad to axes mapping
  51. *
  52. * 2002-07-17 - 0.0.5 : simplified d-pad handling
  53. *
  54. * 2004-10-02 - 0.0.6 : DDR pad support
  55. * - borrowed from the Xbox Linux kernel
  56. * - USB id's for commonly used dance pads are present
  57. * - dance pads will map D-PAD to buttons, not axes
  58. * - pass the module paramater 'dpad_to_buttons' to force
  59. * the D-PAD to map to buttons if your pad is not detected
  60. *
  61. * Later changes can be tracked in SCM.
  62. */
  63. #include <linux/bits.h>
  64. #include <linux/kernel.h>
  65. #include <linux/input.h>
  66. #include <linux/rcupdate.h>
  67. #include <linux/slab.h>
  68. #include <linux/stat.h>
  69. #include <linux/module.h>
  70. #include <linux/usb/input.h>
  71. #include <linux/usb/quirks.h>
  72. #define XPAD_PKT_LEN 64
  73. /*
  74. * xbox d-pads should map to buttons, as is required for DDR pads
  75. * but we map them to axes when possible to simplify things
  76. */
  77. #define MAP_DPAD_TO_BUTTONS BIT(0)
  78. #define MAP_TRIGGERS_TO_BUTTONS BIT(1)
  79. #define MAP_STICKS_TO_NULL BIT(2)
  80. #define MAP_SHARE_BUTTON BIT(3)
  81. #define MAP_PADDLES BIT(4)
  82. #define MAP_PROFILE_BUTTON BIT(5)
  83. #define MAP_SHARE_OFFSET BIT(6)
  84. #define DANCEPAD_MAP_CONFIG (MAP_DPAD_TO_BUTTONS | \
  85. MAP_TRIGGERS_TO_BUTTONS | MAP_STICKS_TO_NULL)
  86. #define XTYPE_XBOX 0
  87. #define XTYPE_XBOX360 1
  88. #define XTYPE_XBOX360W 2
  89. #define XTYPE_XBOXONE 3
  90. #define XTYPE_UNKNOWN 4
  91. /* Send power-off packet to xpad360w after holding the mode button for this many
  92. * seconds
  93. */
  94. #define XPAD360W_POWEROFF_TIMEOUT 5
  95. #define PKT_XB 0
  96. #define PKT_XBE1 1
  97. #define PKT_XBE2_FW_OLD 2
  98. #define PKT_XBE2_FW_5_EARLY 3
  99. #define PKT_XBE2_FW_5_11 4
  100. #define FLAG_DELAY_INIT BIT(0)
  101. static bool dpad_to_buttons;
  102. module_param(dpad_to_buttons, bool, S_IRUGO);
  103. MODULE_PARM_DESC(dpad_to_buttons, "Map D-PAD to buttons rather than axes for unknown pads");
  104. static bool triggers_to_buttons;
  105. module_param(triggers_to_buttons, bool, S_IRUGO);
  106. MODULE_PARM_DESC(triggers_to_buttons, "Map triggers to buttons rather than axes for unknown pads");
  107. static bool sticks_to_null;
  108. module_param(sticks_to_null, bool, S_IRUGO);
  109. MODULE_PARM_DESC(sticks_to_null, "Do not map sticks at all for unknown pads");
  110. static bool auto_poweroff = true;
  111. module_param(auto_poweroff, bool, S_IWUSR | S_IRUGO);
  112. MODULE_PARM_DESC(auto_poweroff, "Power off wireless controllers on suspend");
  113. static const struct xpad_device {
  114. u16 idVendor;
  115. u16 idProduct;
  116. char *name;
  117. u8 mapping;
  118. u8 xtype;
  119. u8 flags;
  120. } xpad_device[] = {
  121. /* Please keep this list sorted by vendor and product ID. */
  122. { 0x0079, 0x18d4, "GPD Win 2 X-Box Controller", 0, XTYPE_XBOX360 },
  123. { 0x0351, 0x1000, "CRKD LP Blueberry Burst Pro Edition (Xbox)", 0, XTYPE_XBOX360 },
  124. { 0x0351, 0x2000, "CRKD LP Black Tribal Edition (Xbox) ", 0, XTYPE_XBOX360 },
  125. { 0x03eb, 0xff01, "Wooting One (Legacy)", 0, XTYPE_XBOX360 },
  126. { 0x03eb, 0xff02, "Wooting Two (Legacy)", 0, XTYPE_XBOX360 },
  127. { 0x03f0, 0x038D, "HyperX Clutch", 0, XTYPE_XBOX360 }, /* wired */
  128. { 0x03f0, 0x048D, "HyperX Clutch", 0, XTYPE_XBOX360 }, /* wireless */
  129. { 0x03f0, 0x0495, "HyperX Clutch Gladiate", 0, XTYPE_XBOXONE },
  130. { 0x03f0, 0x07A0, "HyperX Clutch Gladiate RGB", 0, XTYPE_XBOXONE },
  131. { 0x03f0, 0x08B6, "HyperX Clutch Gladiate", MAP_SHARE_BUTTON, XTYPE_XBOXONE }, /* v2 */
  132. { 0x03f0, 0x09B4, "HyperX Clutch Tanto", 0, XTYPE_XBOXONE },
  133. { 0x044f, 0x0f00, "Thrustmaster Wheel", 0, XTYPE_XBOX },
  134. { 0x044f, 0x0f03, "Thrustmaster Wheel", 0, XTYPE_XBOX },
  135. { 0x044f, 0x0f07, "Thrustmaster, Inc. Controller", 0, XTYPE_XBOX },
  136. { 0x044f, 0x0f10, "Thrustmaster Modena GT Wheel", 0, XTYPE_XBOX },
  137. { 0x044f, 0xb326, "Thrustmaster Gamepad GP XID", 0, XTYPE_XBOX360 },
  138. { 0x044f, 0xd01e, "ThrustMaster, Inc. ESWAP X 2 ELDEN RING EDITION", 0, XTYPE_XBOXONE },
  139. { 0x045e, 0x0202, "Microsoft X-Box pad v1 (US)", 0, XTYPE_XBOX },
  140. { 0x045e, 0x0285, "Microsoft X-Box pad (Japan)", 0, XTYPE_XBOX },
  141. { 0x045e, 0x0287, "Microsoft Xbox Controller S", 0, XTYPE_XBOX },
  142. { 0x045e, 0x0288, "Microsoft Xbox Controller S v2", 0, XTYPE_XBOX },
  143. { 0x045e, 0x0289, "Microsoft X-Box pad v2 (US)", 0, XTYPE_XBOX },
  144. { 0x045e, 0x028e, "Microsoft X-Box 360 pad", 0, XTYPE_XBOX360 },
  145. { 0x045e, 0x028f, "Microsoft X-Box 360 pad v2", 0, XTYPE_XBOX360 },
  146. { 0x045e, 0x0291, "Xbox 360 Wireless Receiver (XBOX)", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360W },
  147. { 0x045e, 0x02a9, "Xbox 360 Wireless Receiver (Unofficial)", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360W },
  148. { 0x045e, 0x02d1, "Microsoft X-Box One pad", 0, XTYPE_XBOXONE },
  149. { 0x045e, 0x02dd, "Microsoft X-Box One pad (Firmware 2015)", 0, XTYPE_XBOXONE },
  150. { 0x045e, 0x02e3, "Microsoft X-Box One Elite pad", MAP_PADDLES, XTYPE_XBOXONE },
  151. { 0x045e, 0x02ea, "Microsoft X-Box One S pad", 0, XTYPE_XBOXONE },
  152. { 0x045e, 0x0719, "Xbox 360 Wireless Receiver", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360W },
  153. { 0x045e, 0x0b00, "Microsoft X-Box One Elite 2 pad", MAP_PADDLES, XTYPE_XBOXONE },
  154. { 0x045e, 0x0b0a, "Microsoft X-Box Adaptive Controller", MAP_PROFILE_BUTTON, XTYPE_XBOXONE },
  155. { 0x045e, 0x0b12, "Microsoft Xbox Series S|X Controller", MAP_SHARE_BUTTON | MAP_SHARE_OFFSET, XTYPE_XBOXONE },
  156. { 0x046d, 0xc21d, "Logitech Gamepad F310", 0, XTYPE_XBOX360 },
  157. { 0x046d, 0xc21e, "Logitech Gamepad F510", 0, XTYPE_XBOX360 },
  158. { 0x046d, 0xc21f, "Logitech Gamepad F710", 0, XTYPE_XBOX360 },
  159. { 0x046d, 0xc242, "Logitech Chillstream Controller", 0, XTYPE_XBOX360 },
  160. { 0x046d, 0xca84, "Logitech Xbox Cordless Controller", 0, XTYPE_XBOX },
  161. { 0x046d, 0xca88, "Logitech Compact Controller for Xbox", 0, XTYPE_XBOX },
  162. { 0x046d, 0xca8a, "Logitech Precision Vibration Feedback Wheel", 0, XTYPE_XBOX },
  163. { 0x046d, 0xcaa3, "Logitech DriveFx Racing Wheel", 0, XTYPE_XBOX360 },
  164. { 0x0502, 0x1305, "Acer NGR200", 0, XTYPE_XBOX360 },
  165. { 0x056e, 0x2004, "Elecom JC-U3613M", 0, XTYPE_XBOX360 },
  166. { 0x05fd, 0x1007, "Mad Catz Controller (unverified)", 0, XTYPE_XBOX },
  167. { 0x05fd, 0x107a, "InterAct 'PowerPad Pro' X-Box pad (Germany)", 0, XTYPE_XBOX },
  168. { 0x05fe, 0x3030, "Chic Controller", 0, XTYPE_XBOX },
  169. { 0x05fe, 0x3031, "Chic Controller", 0, XTYPE_XBOX },
  170. { 0x062a, 0x0020, "Logic3 Xbox GamePad", 0, XTYPE_XBOX },
  171. { 0x062a, 0x0033, "Competition Pro Steering Wheel", 0, XTYPE_XBOX },
  172. { 0x06a3, 0x0200, "Saitek Racing Wheel", 0, XTYPE_XBOX },
  173. { 0x06a3, 0x0201, "Saitek Adrenalin", 0, XTYPE_XBOX },
  174. { 0x06a3, 0xf51a, "Saitek P3600", 0, XTYPE_XBOX360 },
  175. { 0x0738, 0x4503, "Mad Catz Racing Wheel", 0, XTYPE_XBOXONE },
  176. { 0x0738, 0x4506, "Mad Catz 4506 Wireless Controller", 0, XTYPE_XBOX },
  177. { 0x0738, 0x4516, "Mad Catz Control Pad", 0, XTYPE_XBOX },
  178. { 0x0738, 0x4520, "Mad Catz Control Pad Pro", 0, XTYPE_XBOX },
  179. { 0x0738, 0x4522, "Mad Catz LumiCON", 0, XTYPE_XBOX },
  180. { 0x0738, 0x4526, "Mad Catz Control Pad Pro", 0, XTYPE_XBOX },
  181. { 0x0738, 0x4530, "Mad Catz Universal MC2 Racing Wheel and Pedals", 0, XTYPE_XBOX },
  182. { 0x0738, 0x4536, "Mad Catz MicroCON", 0, XTYPE_XBOX },
  183. { 0x0738, 0x4540, "Mad Catz Beat Pad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
  184. { 0x0738, 0x4556, "Mad Catz Lynx Wireless Controller", 0, XTYPE_XBOX },
  185. { 0x0738, 0x4586, "Mad Catz MicroCon Wireless Controller", 0, XTYPE_XBOX },
  186. { 0x0738, 0x4588, "Mad Catz Blaster", 0, XTYPE_XBOX },
  187. { 0x0738, 0x45ff, "Mad Catz Beat Pad (w/ Handle)", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
  188. { 0x0738, 0x4716, "Mad Catz Wired Xbox 360 Controller", 0, XTYPE_XBOX360 },
  189. { 0x0738, 0x4718, "Mad Catz Street Fighter IV FightStick SE", 0, XTYPE_XBOX360 },
  190. { 0x0738, 0x4726, "Mad Catz Xbox 360 Controller", 0, XTYPE_XBOX360 },
  191. { 0x0738, 0x4728, "Mad Catz Street Fighter IV FightPad", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  192. { 0x0738, 0x4736, "Mad Catz MicroCon Gamepad", 0, XTYPE_XBOX360 },
  193. { 0x0738, 0x4738, "Mad Catz Wired Xbox 360 Controller (SFIV)", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  194. { 0x0738, 0x4740, "Mad Catz Beat Pad", 0, XTYPE_XBOX360 },
  195. { 0x0738, 0x4743, "Mad Catz Beat Pad Pro", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
  196. { 0x0738, 0x4758, "Mad Catz Arcade Game Stick", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  197. { 0x0738, 0x4a01, "Mad Catz FightStick TE 2", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOXONE },
  198. { 0x0738, 0x6040, "Mad Catz Beat Pad Pro", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
  199. { 0x0738, 0x9871, "Mad Catz Portable Drum", 0, XTYPE_XBOX360 },
  200. { 0x0738, 0xb726, "Mad Catz Xbox controller - MW2", 0, XTYPE_XBOX360 },
  201. { 0x0738, 0xb738, "Mad Catz MVC2TE Stick 2", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  202. { 0x0738, 0xbeef, "Mad Catz JOYTECH NEO SE Advanced GamePad", 0, XTYPE_XBOX360 },
  203. { 0x0738, 0xcb02, "Saitek Cyborg Rumble Pad - PC/Xbox 360", 0, XTYPE_XBOX360 },
  204. { 0x0738, 0xcb03, "Saitek P3200 Rumble Pad - PC/Xbox 360", 0, XTYPE_XBOX360 },
  205. { 0x0738, 0xcb29, "Saitek Aviator Stick AV8R02", 0, XTYPE_XBOX360 },
  206. { 0x0738, 0xf738, "Super SFIV FightStick TE S", 0, XTYPE_XBOX360 },
  207. { 0x07ff, 0xffff, "Mad Catz GamePad", 0, XTYPE_XBOX360 },
  208. { 0x0b05, 0x1a38, "ASUS ROG RAIKIRI", MAP_SHARE_BUTTON, XTYPE_XBOXONE },
  209. { 0x0b05, 0x1abb, "ASUS ROG RAIKIRI PRO", 0, XTYPE_XBOXONE },
  210. { 0x0c12, 0x0005, "Intec wireless", 0, XTYPE_XBOX },
  211. { 0x0c12, 0x8801, "Nyko Xbox Controller", 0, XTYPE_XBOX },
  212. { 0x0c12, 0x8802, "Zeroplus Xbox Controller", 0, XTYPE_XBOX },
  213. { 0x0c12, 0x8809, "RedOctane Xbox Dance Pad", DANCEPAD_MAP_CONFIG, XTYPE_XBOX },
  214. { 0x0c12, 0x880a, "Pelican Eclipse PL-2023", 0, XTYPE_XBOX },
  215. { 0x0c12, 0x8810, "Zeroplus Xbox Controller", 0, XTYPE_XBOX },
  216. { 0x0c12, 0x9902, "HAMA VibraX - *FAULTY HARDWARE*", 0, XTYPE_XBOX },
  217. { 0x0d2f, 0x0002, "Andamiro Pump It Up pad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
  218. { 0x0db0, 0x1901, "Micro Star International Xbox360 Controller for Windows", 0, XTYPE_XBOX360 },
  219. { 0x0e4c, 0x1097, "Radica Gamester Controller", 0, XTYPE_XBOX },
  220. { 0x0e4c, 0x1103, "Radica Gamester Reflex", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX },
  221. { 0x0e4c, 0x2390, "Radica Games Jtech Controller", 0, XTYPE_XBOX },
  222. { 0x0e4c, 0x3510, "Radica Gamester", 0, XTYPE_XBOX },
  223. { 0x0e6f, 0x0003, "Logic3 Freebird wireless Controller", 0, XTYPE_XBOX },
  224. { 0x0e6f, 0x0005, "Eclipse wireless Controller", 0, XTYPE_XBOX },
  225. { 0x0e6f, 0x0006, "Edge wireless Controller", 0, XTYPE_XBOX },
  226. { 0x0e6f, 0x0008, "After Glow Pro Controller", 0, XTYPE_XBOX },
  227. { 0x0e6f, 0x0105, "HSM3 Xbox360 dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
  228. { 0x0e6f, 0x0113, "Afterglow AX.1 Gamepad for Xbox 360", 0, XTYPE_XBOX360 },
  229. { 0x0e6f, 0x011f, "Rock Candy Gamepad Wired Controller", 0, XTYPE_XBOX360 },
  230. { 0x0e6f, 0x0131, "PDP EA Sports Controller", 0, XTYPE_XBOX360 },
  231. { 0x0e6f, 0x0133, "Xbox 360 Wired Controller", 0, XTYPE_XBOX360 },
  232. { 0x0e6f, 0x0139, "Afterglow Prismatic Wired Controller", 0, XTYPE_XBOXONE },
  233. { 0x0e6f, 0x013a, "PDP Xbox One Controller", 0, XTYPE_XBOXONE },
  234. { 0x0e6f, 0x0146, "Rock Candy Wired Controller for Xbox One", 0, XTYPE_XBOXONE },
  235. { 0x0e6f, 0x0147, "PDP Marvel Xbox One Controller", 0, XTYPE_XBOXONE },
  236. { 0x0e6f, 0x015c, "PDP Xbox One Arcade Stick", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOXONE },
  237. { 0x0e6f, 0x015d, "PDP Mirror's Edge Official Wired Controller for Xbox One", 0, XTYPE_XBOXONE },
  238. { 0x0e6f, 0x0161, "PDP Xbox One Controller", 0, XTYPE_XBOXONE },
  239. { 0x0e6f, 0x0162, "PDP Xbox One Controller", 0, XTYPE_XBOXONE },
  240. { 0x0e6f, 0x0163, "PDP Xbox One Controller", 0, XTYPE_XBOXONE },
  241. { 0x0e6f, 0x0164, "PDP Battlefield One", 0, XTYPE_XBOXONE },
  242. { 0x0e6f, 0x0165, "PDP Titanfall 2", 0, XTYPE_XBOXONE },
  243. { 0x0e6f, 0x0201, "Pelican PL-3601 'TSZ' Wired Xbox 360 Controller", 0, XTYPE_XBOX360 },
  244. { 0x0e6f, 0x0213, "Afterglow Gamepad for Xbox 360", 0, XTYPE_XBOX360 },
  245. { 0x0e6f, 0x021f, "Rock Candy Gamepad for Xbox 360", 0, XTYPE_XBOX360 },
  246. { 0x0e6f, 0x0246, "Rock Candy Gamepad for Xbox One 2015", 0, XTYPE_XBOXONE },
  247. { 0x0e6f, 0x02a0, "PDP Xbox One Controller", 0, XTYPE_XBOXONE },
  248. { 0x0e6f, 0x02a1, "PDP Xbox One Controller", 0, XTYPE_XBOXONE },
  249. { 0x0e6f, 0x02a2, "PDP Wired Controller for Xbox One - Crimson Red", 0, XTYPE_XBOXONE },
  250. { 0x0e6f, 0x02a4, "PDP Wired Controller for Xbox One - Stealth Series", 0, XTYPE_XBOXONE },
  251. { 0x0e6f, 0x02a6, "PDP Wired Controller for Xbox One - Camo Series", 0, XTYPE_XBOXONE },
  252. { 0x0e6f, 0x02a7, "PDP Xbox One Controller", 0, XTYPE_XBOXONE },
  253. { 0x0e6f, 0x02a8, "PDP Xbox One Controller", 0, XTYPE_XBOXONE },
  254. { 0x0e6f, 0x02ab, "PDP Controller for Xbox One", 0, XTYPE_XBOXONE },
  255. { 0x0e6f, 0x02ad, "PDP Wired Controller for Xbox One - Stealth Series", 0, XTYPE_XBOXONE },
  256. { 0x0e6f, 0x02b3, "Afterglow Prismatic Wired Controller", 0, XTYPE_XBOXONE },
  257. { 0x0e6f, 0x02b8, "Afterglow Prismatic Wired Controller", 0, XTYPE_XBOXONE },
  258. { 0x0e6f, 0x0301, "Logic3 Controller", 0, XTYPE_XBOX360 },
  259. { 0x0e6f, 0x0346, "Rock Candy Gamepad for Xbox One 2016", 0, XTYPE_XBOXONE },
  260. { 0x0e6f, 0x0401, "Logic3 Controller", 0, XTYPE_XBOX360 },
  261. { 0x0e6f, 0x0413, "Afterglow AX.1 Gamepad for Xbox 360", 0, XTYPE_XBOX360 },
  262. { 0x0e6f, 0x0501, "PDP Xbox 360 Controller", 0, XTYPE_XBOX360 },
  263. { 0x0e6f, 0xf900, "PDP Afterglow AX.1", 0, XTYPE_XBOX360 },
  264. { 0x0e8f, 0x0201, "SmartJoy Frag Xpad/PS2 adaptor", 0, XTYPE_XBOX },
  265. { 0x0e8f, 0x3008, "Generic xbox control (dealextreme)", 0, XTYPE_XBOX },
  266. { 0x0f0d, 0x000a, "Hori Co. DOA4 FightStick", 0, XTYPE_XBOX360 },
  267. { 0x0f0d, 0x000c, "Hori PadEX Turbo", 0, XTYPE_XBOX360 },
  268. { 0x0f0d, 0x000d, "Hori Fighting Stick EX2", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  269. { 0x0f0d, 0x0016, "Hori Real Arcade Pro.EX", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  270. { 0x0f0d, 0x001b, "Hori Real Arcade Pro VX", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  271. { 0x0f0d, 0x0063, "Hori Real Arcade Pro Hayabusa (USA) Xbox One", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOXONE },
  272. { 0x0f0d, 0x0067, "HORIPAD ONE", 0, XTYPE_XBOXONE },
  273. { 0x0f0d, 0x0078, "Hori Real Arcade Pro V Kai Xbox One", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOXONE },
  274. { 0x0f0d, 0x00c5, "Hori Fighting Commander ONE", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOXONE },
  275. { 0x0f0d, 0x00dc, "HORIPAD FPS for Nintendo Switch", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  276. { 0x0f0d, 0x0151, "Hori Racing Wheel Overdrive for Xbox Series X", 0, XTYPE_XBOXONE },
  277. { 0x0f0d, 0x0152, "Hori Racing Wheel Overdrive for Xbox Series X", 0, XTYPE_XBOXONE },
  278. { 0x0f0d, 0x01b2, "HORI Taiko No Tatsujin Drum Controller", MAP_SHARE_BUTTON, XTYPE_XBOXONE },
  279. { 0x0f30, 0x010b, "Philips Recoil", 0, XTYPE_XBOX },
  280. { 0x0f30, 0x0202, "Joytech Advanced Controller", 0, XTYPE_XBOX },
  281. { 0x0f30, 0x8888, "BigBen XBMiniPad Controller", 0, XTYPE_XBOX },
  282. { 0x102c, 0xff0c, "Joytech Wireless Advanced Controller", 0, XTYPE_XBOX },
  283. { 0x1038, 0x1430, "SteelSeries Stratus Duo", 0, XTYPE_XBOX360 },
  284. { 0x1038, 0x1431, "SteelSeries Stratus Duo", 0, XTYPE_XBOX360 },
  285. { 0x10f5, 0x7005, "Turtle Beach Recon Controller", 0, XTYPE_XBOXONE },
  286. { 0x10f5, 0x7008, "Turtle Beach Recon Controller", MAP_SHARE_BUTTON, XTYPE_XBOXONE },
  287. { 0x10f5, 0x7073, "Turtle Beach Stealth Ultra Controller", MAP_SHARE_BUTTON, XTYPE_XBOXONE },
  288. { 0x11c9, 0x55f0, "Nacon GC-100XF", 0, XTYPE_XBOX360 },
  289. { 0x11ff, 0x0511, "PXN V900", 0, XTYPE_XBOX360 },
  290. { 0x1209, 0x2882, "Ardwiino Controller", 0, XTYPE_XBOX360 },
  291. { 0x12ab, 0x0004, "Honey Bee Xbox360 dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
  292. { 0x12ab, 0x0301, "PDP AFTERGLOW AX.1", 0, XTYPE_XBOX360 },
  293. { 0x12ab, 0x0303, "Mortal Kombat Klassic FightStick", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  294. { 0x12ab, 0x8809, "Xbox DDR dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
  295. { 0x1430, 0x4748, "RedOctane Guitar Hero X-plorer", 0, XTYPE_XBOX360 },
  296. { 0x1430, 0x8888, "TX6500+ Dance Pad (first generation)", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
  297. { 0x1430, 0xf801, "RedOctane Controller", 0, XTYPE_XBOX360 },
  298. { 0x146b, 0x0601, "BigBen Interactive XBOX 360 Controller", 0, XTYPE_XBOX360 },
  299. { 0x146b, 0x0604, "Bigben Interactive DAIJA Arcade Stick", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  300. { 0x1532, 0x0a00, "Razer Atrox Arcade Stick", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOXONE },
  301. { 0x1532, 0x0a03, "Razer Wildcat", 0, XTYPE_XBOXONE },
  302. { 0x1532, 0x0a29, "Razer Wolverine V2", 0, XTYPE_XBOXONE },
  303. { 0x1532, 0x0a57, "Razer Wolverine V3 Pro (Wired)", 0, XTYPE_XBOX360 },
  304. { 0x1532, 0x0a59, "Razer Wolverine V3 Pro (2.4 GHz Dongle)", 0, XTYPE_XBOX360 },
  305. { 0x15e4, 0x3f00, "Power A Mini Pro Elite", 0, XTYPE_XBOX360 },
  306. { 0x15e4, 0x3f0a, "Xbox Airflo wired controller", 0, XTYPE_XBOX360 },
  307. { 0x15e4, 0x3f10, "Batarang Xbox 360 controller", 0, XTYPE_XBOX360 },
  308. { 0x162e, 0xbeef, "Joytech Neo-Se Take2", 0, XTYPE_XBOX360 },
  309. { 0x1689, 0xfd00, "Razer Onza Tournament Edition", 0, XTYPE_XBOX360 },
  310. { 0x1689, 0xfd01, "Razer Onza Classic Edition", 0, XTYPE_XBOX360 },
  311. { 0x1689, 0xfe00, "Razer Sabertooth", 0, XTYPE_XBOX360 },
  312. { 0x17ef, 0x6182, "Lenovo Legion Controller for Windows", 0, XTYPE_XBOX360 },
  313. { 0x1949, 0x041a, "Amazon Game Controller", 0, XTYPE_XBOX360 },
  314. { 0x1a86, 0xe310, "Legion Go S", 0, XTYPE_XBOX360 },
  315. { 0x1bad, 0x0002, "Harmonix Rock Band Guitar", 0, XTYPE_XBOX360 },
  316. { 0x1bad, 0x0003, "Harmonix Rock Band Drumkit", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
  317. { 0x1bad, 0x0130, "Ion Drum Rocker", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
  318. { 0x1bad, 0xf016, "Mad Catz Xbox 360 Controller", 0, XTYPE_XBOX360 },
  319. { 0x1bad, 0xf018, "Mad Catz Street Fighter IV SE Fighting Stick", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  320. { 0x1bad, 0xf019, "Mad Catz Brawlstick for Xbox 360", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  321. { 0x1bad, 0xf021, "Mad Cats Ghost Recon FS GamePad", 0, XTYPE_XBOX360 },
  322. { 0x1bad, 0xf023, "MLG Pro Circuit Controller (Xbox)", 0, XTYPE_XBOX360 },
  323. { 0x1bad, 0xf025, "Mad Catz Call Of Duty", 0, XTYPE_XBOX360 },
  324. { 0x1bad, 0xf027, "Mad Catz FPS Pro", 0, XTYPE_XBOX360 },
  325. { 0x1bad, 0xf028, "Street Fighter IV FightPad", 0, XTYPE_XBOX360 },
  326. { 0x1bad, 0xf02e, "Mad Catz Fightpad", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  327. { 0x1bad, 0xf030, "Mad Catz Xbox 360 MC2 MicroCon Racing Wheel", 0, XTYPE_XBOX360 },
  328. { 0x1bad, 0xf036, "Mad Catz MicroCon GamePad Pro", 0, XTYPE_XBOX360 },
  329. { 0x1bad, 0xf038, "Street Fighter IV FightStick TE", 0, XTYPE_XBOX360 },
  330. { 0x1bad, 0xf039, "Mad Catz MvC2 TE", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  331. { 0x1bad, 0xf03a, "Mad Catz SFxT Fightstick Pro", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  332. { 0x1bad, 0xf03d, "Street Fighter IV Arcade Stick TE - Chun Li", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  333. { 0x1bad, 0xf03e, "Mad Catz MLG FightStick TE", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  334. { 0x1bad, 0xf03f, "Mad Catz FightStick SoulCaliber", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  335. { 0x1bad, 0xf042, "Mad Catz FightStick TES+", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  336. { 0x1bad, 0xf080, "Mad Catz FightStick TE2", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  337. { 0x1bad, 0xf501, "HoriPad EX2 Turbo", 0, XTYPE_XBOX360 },
  338. { 0x1bad, 0xf502, "Hori Real Arcade Pro.VX SA", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  339. { 0x1bad, 0xf503, "Hori Fighting Stick VX", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  340. { 0x1bad, 0xf504, "Hori Real Arcade Pro. EX", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  341. { 0x1bad, 0xf505, "Hori Fighting Stick EX2B", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  342. { 0x1bad, 0xf506, "Hori Real Arcade Pro.EX Premium VLX", 0, XTYPE_XBOX360 },
  343. { 0x1bad, 0xf900, "Harmonix Xbox 360 Controller", 0, XTYPE_XBOX360 },
  344. { 0x1bad, 0xf901, "Gamestop Xbox 360 Controller", 0, XTYPE_XBOX360 },
  345. { 0x1bad, 0xf903, "Tron Xbox 360 controller", 0, XTYPE_XBOX360 },
  346. { 0x1bad, 0xf904, "PDP Versus Fighting Pad", 0, XTYPE_XBOX360 },
  347. { 0x1bad, 0xf906, "MortalKombat FightStick", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  348. { 0x1bad, 0xfa01, "MadCatz GamePad", 0, XTYPE_XBOX360 },
  349. { 0x1bad, 0xfd00, "Razer Onza TE", 0, XTYPE_XBOX360 },
  350. { 0x1bad, 0xfd01, "Razer Onza", 0, XTYPE_XBOX360 },
  351. { 0x1ee9, 0x1590, "ZOTAC Gaming Zone", 0, XTYPE_XBOX360 },
  352. { 0x20bc, 0x5134, "BETOP BTP-KP50B Xinput Dongle", 0, XTYPE_XBOX360 },
  353. { 0x20bc, 0x514a, "BETOP BTP-KP50C Xinput Dongle", 0, XTYPE_XBOX360 },
  354. { 0x20d6, 0x2001, "BDA Xbox Series X Wired Controller", 0, XTYPE_XBOXONE },
  355. { 0x20d6, 0x2009, "PowerA Enhanced Wired Controller for Xbox Series X|S", 0, XTYPE_XBOXONE },
  356. { 0x20d6, 0x2064, "PowerA Wired Controller for Xbox", MAP_SHARE_BUTTON, XTYPE_XBOXONE },
  357. { 0x20d6, 0x281f, "PowerA Wired Controller For Xbox 360", 0, XTYPE_XBOX360 },
  358. { 0x20d6, 0x400b, "PowerA FUSION Pro 4 Wired Controller", MAP_SHARE_BUTTON, XTYPE_XBOXONE },
  359. { 0x20d6, 0x890b, "PowerA MOGA XP-Ultra Controller", MAP_SHARE_BUTTON, XTYPE_XBOXONE },
  360. { 0x2345, 0xe00b, "Machenike G5 Pro Controller", 0, XTYPE_XBOX360 },
  361. { 0x24c6, 0x5000, "Razer Atrox Arcade Stick", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  362. { 0x24c6, 0x5300, "PowerA MINI PROEX Controller", 0, XTYPE_XBOX360 },
  363. { 0x24c6, 0x5303, "Xbox Airflo wired controller", 0, XTYPE_XBOX360 },
  364. { 0x24c6, 0x530a, "Xbox 360 Pro EX Controller", 0, XTYPE_XBOX360 },
  365. { 0x24c6, 0x531a, "PowerA Pro Ex", 0, XTYPE_XBOX360 },
  366. { 0x24c6, 0x5397, "FUS1ON Tournament Controller", 0, XTYPE_XBOX360 },
  367. { 0x24c6, 0x541a, "PowerA Xbox One Mini Wired Controller", 0, XTYPE_XBOXONE },
  368. { 0x24c6, 0x542a, "Xbox ONE spectra", 0, XTYPE_XBOXONE },
  369. { 0x24c6, 0x543a, "PowerA Xbox One wired controller", 0, XTYPE_XBOXONE },
  370. { 0x24c6, 0x5500, "Hori XBOX 360 EX 2 with Turbo", 0, XTYPE_XBOX360 },
  371. { 0x24c6, 0x5501, "Hori Real Arcade Pro VX-SA", 0, XTYPE_XBOX360 },
  372. { 0x24c6, 0x5502, "Hori Fighting Stick VX Alt", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  373. { 0x24c6, 0x5503, "Hori Fighting Edge", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  374. { 0x24c6, 0x5506, "Hori SOULCALIBUR V Stick", 0, XTYPE_XBOX360 },
  375. { 0x24c6, 0x550d, "Hori GEM Xbox controller", 0, XTYPE_XBOX360 },
  376. { 0x24c6, 0x550e, "Hori Real Arcade Pro V Kai 360", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  377. { 0x24c6, 0x5510, "Hori Fighting Commander ONE (Xbox 360/PC Mode)", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  378. { 0x24c6, 0x551a, "PowerA FUSION Pro Controller", 0, XTYPE_XBOXONE },
  379. { 0x24c6, 0x561a, "PowerA FUSION Controller", 0, XTYPE_XBOXONE },
  380. { 0x24c6, 0x581a, "ThrustMaster XB1 Classic Controller", 0, XTYPE_XBOXONE },
  381. { 0x24c6, 0x5b00, "ThrustMaster Ferrari 458 Racing Wheel", 0, XTYPE_XBOX360 },
  382. { 0x24c6, 0x5b02, "Thrustmaster, Inc. GPX Controller", 0, XTYPE_XBOX360 },
  383. { 0x24c6, 0x5b03, "Thrustmaster Ferrari 458 Racing Wheel", 0, XTYPE_XBOX360 },
  384. { 0x24c6, 0x5d04, "Razer Sabertooth", 0, XTYPE_XBOX360 },
  385. { 0x24c6, 0xfafe, "Rock Candy Gamepad for Xbox 360", 0, XTYPE_XBOX360 },
  386. { 0x2563, 0x058d, "OneXPlayer Gamepad", 0, XTYPE_XBOX360 },
  387. { 0x294b, 0x3303, "Snakebyte GAMEPAD BASE X", 0, XTYPE_XBOXONE },
  388. { 0x294b, 0x3404, "Snakebyte GAMEPAD RGB X", 0, XTYPE_XBOXONE },
  389. { 0x2993, 0x2001, "TECNO Pocket Go", 0, XTYPE_XBOX360 },
  390. { 0x2dc8, 0x2000, "8BitDo Pro 2 Wired Controller fox Xbox", 0, XTYPE_XBOXONE },
  391. { 0x2dc8, 0x200f, "8BitDo Ultimate 3-mode Controller for Xbox", MAP_SHARE_BUTTON, XTYPE_XBOXONE },
  392. { 0x2dc8, 0x3106, "8BitDo Ultimate Wireless / Pro 2 Wired Controller", 0, XTYPE_XBOX360 },
  393. { 0x2dc8, 0x3109, "8BitDo Ultimate Wireless Bluetooth", 0, XTYPE_XBOX360 },
  394. { 0x2dc8, 0x310a, "8BitDo Ultimate 2C Wireless Controller", 0, XTYPE_XBOX360 },
  395. { 0x2dc8, 0x310b, "8BitDo Ultimate 2 Wireless Controller", 0, XTYPE_XBOX360 },
  396. { 0x2dc8, 0x6001, "8BitDo SN30 Pro", 0, XTYPE_XBOX360 },
  397. { 0x2e24, 0x0423, "Hyperkin DuchesS Xbox One pad", MAP_SHARE_BUTTON, XTYPE_XBOXONE },
  398. { 0x2e24, 0x0652, "Hyperkin Duke X-Box One pad", 0, XTYPE_XBOXONE },
  399. { 0x2e24, 0x1688, "Hyperkin X91 X-Box One pad", 0, XTYPE_XBOXONE },
  400. { 0x2e95, 0x0504, "SCUF Gaming Controller", MAP_SHARE_BUTTON, XTYPE_XBOXONE },
  401. { 0x31e3, 0x1100, "Wooting One", 0, XTYPE_XBOX360 },
  402. { 0x31e3, 0x1200, "Wooting Two", 0, XTYPE_XBOX360 },
  403. { 0x31e3, 0x1210, "Wooting Lekker", 0, XTYPE_XBOX360 },
  404. { 0x31e3, 0x1220, "Wooting Two HE", 0, XTYPE_XBOX360 },
  405. { 0x31e3, 0x1230, "Wooting Two HE (ARM)", 0, XTYPE_XBOX360 },
  406. { 0x31e3, 0x1300, "Wooting 60HE (AVR)", 0, XTYPE_XBOX360 },
  407. { 0x31e3, 0x1310, "Wooting 60HE (ARM)", 0, XTYPE_XBOX360 },
  408. { 0x3285, 0x0603, "Nacon Pro Compact controller for Xbox", 0, XTYPE_XBOXONE },
  409. { 0x3285, 0x0607, "Nacon GC-100", 0, XTYPE_XBOX360 },
  410. { 0x3285, 0x0614, "Nacon Pro Compact", 0, XTYPE_XBOXONE },
  411. { 0x3285, 0x0646, "Nacon Pro Compact", 0, XTYPE_XBOXONE },
  412. { 0x3285, 0x0662, "Nacon Revolution5 Pro", 0, XTYPE_XBOX360 },
  413. { 0x3285, 0x0663, "Nacon Evol-X", 0, XTYPE_XBOXONE },
  414. { 0x3537, 0x1004, "GameSir T4 Kaleid", 0, XTYPE_XBOX360 },
  415. { 0x3537, 0x1010, "GameSir G7 SE", 0, XTYPE_XBOXONE },
  416. { 0x3651, 0x1000, "CRKD SG", 0, XTYPE_XBOX360 },
  417. { 0x366c, 0x0005, "ByoWave Proteus Controller", MAP_SHARE_BUTTON, XTYPE_XBOXONE, FLAG_DELAY_INIT },
  418. { 0x3767, 0x0101, "Fanatec Speedster 3 Forceshock Wheel", 0, XTYPE_XBOX },
  419. { 0x37d7, 0x2501, "Flydigi Apex 5", 0, XTYPE_XBOX360 },
  420. { 0x413d, 0x2104, "Black Shark Green Ghost Gamepad", 0, XTYPE_XBOX360 },
  421. { 0xffff, 0xffff, "Chinese-made Xbox Controller", 0, XTYPE_XBOX },
  422. { 0x0000, 0x0000, "Generic X-Box pad", 0, XTYPE_UNKNOWN }
  423. };
  424. /* buttons shared with xbox and xbox360 */
  425. static const signed short xpad_common_btn[] = {
  426. BTN_A, BTN_B, BTN_X, BTN_Y, /* "analog" buttons */
  427. BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR, /* start/back/sticks */
  428. -1 /* terminating entry */
  429. };
  430. /* original xbox controllers only */
  431. static const signed short xpad_btn[] = {
  432. BTN_C, BTN_Z, /* "analog" buttons */
  433. -1 /* terminating entry */
  434. };
  435. /* used when dpad is mapped to buttons */
  436. static const signed short xpad_btn_pad[] = {
  437. BTN_DPAD_LEFT, BTN_DPAD_RIGHT, /* d-pad left, right */
  438. BTN_DPAD_UP, BTN_DPAD_DOWN, /* d-pad up, down */
  439. -1 /* terminating entry */
  440. };
  441. /* used when triggers are mapped to buttons */
  442. static const signed short xpad_btn_triggers[] = {
  443. BTN_TL2, BTN_TR2, /* triggers left/right */
  444. -1
  445. };
  446. static const signed short xpad360_btn[] = { /* buttons for x360 controller */
  447. BTN_TL, BTN_TR, /* Button LB/RB */
  448. BTN_MODE, /* The big X button */
  449. -1
  450. };
  451. static const signed short xpad_abs[] = {
  452. ABS_X, ABS_Y, /* left stick */
  453. ABS_RX, ABS_RY, /* right stick */
  454. -1 /* terminating entry */
  455. };
  456. /* used when dpad is mapped to axes */
  457. static const signed short xpad_abs_pad[] = {
  458. ABS_HAT0X, ABS_HAT0Y, /* d-pad axes */
  459. -1 /* terminating entry */
  460. };
  461. /* used when triggers are mapped to axes */
  462. static const signed short xpad_abs_triggers[] = {
  463. ABS_Z, ABS_RZ, /* triggers left/right */
  464. -1
  465. };
  466. /* used when the controller has extra paddle buttons */
  467. static const signed short xpad_btn_paddles[] = {
  468. BTN_GRIPR, BTN_GRIPR2, /* paddle upper right, lower right */
  469. BTN_GRIPL, BTN_GRIPL2, /* paddle upper left, lower left */
  470. -1 /* terminating entry */
  471. };
  472. /*
  473. * Xbox 360 has a vendor-specific class, so we cannot match it with only
  474. * USB_INTERFACE_INFO (also specifically refused by USB subsystem), so we
  475. * match against vendor id as well. Wired Xbox 360 devices have protocol 1,
  476. * wireless controllers have protocol 129.
  477. */
  478. #define XPAD_XBOX360_VENDOR_PROTOCOL(vend, pr) \
  479. .match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_INT_INFO, \
  480. .idVendor = (vend), \
  481. .bInterfaceClass = USB_CLASS_VENDOR_SPEC, \
  482. .bInterfaceSubClass = 93, \
  483. .bInterfaceProtocol = (pr)
  484. #define XPAD_XBOX360_VENDOR(vend) \
  485. { XPAD_XBOX360_VENDOR_PROTOCOL((vend), 1) }, \
  486. { XPAD_XBOX360_VENDOR_PROTOCOL((vend), 129) }
  487. /* The Xbox One controller uses subclass 71 and protocol 208. */
  488. #define XPAD_XBOXONE_VENDOR_PROTOCOL(vend, pr) \
  489. .match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_INT_INFO, \
  490. .idVendor = (vend), \
  491. .bInterfaceClass = USB_CLASS_VENDOR_SPEC, \
  492. .bInterfaceSubClass = 71, \
  493. .bInterfaceProtocol = (pr)
  494. #define XPAD_XBOXONE_VENDOR(vend) \
  495. { XPAD_XBOXONE_VENDOR_PROTOCOL((vend), 208) }
  496. static const struct usb_device_id xpad_table[] = {
  497. /*
  498. * Please keep this list sorted by vendor ID. Note that there are 2
  499. * macros - XPAD_XBOX360_VENDOR and XPAD_XBOXONE_VENDOR.
  500. */
  501. { USB_INTERFACE_INFO('X', 'B', 0) }, /* Xbox USB-IF not-approved class */
  502. XPAD_XBOX360_VENDOR(0x0079), /* GPD Win 2 controller */
  503. XPAD_XBOX360_VENDOR(0x0351), /* CRKD Controllers */
  504. XPAD_XBOX360_VENDOR(0x03eb), /* Wooting Keyboards (Legacy) */
  505. XPAD_XBOX360_VENDOR(0x03f0), /* HP HyperX Xbox 360 controllers */
  506. XPAD_XBOXONE_VENDOR(0x03f0), /* HP HyperX Xbox One controllers */
  507. XPAD_XBOX360_VENDOR(0x044f), /* Thrustmaster Xbox 360 controllers */
  508. XPAD_XBOXONE_VENDOR(0x044f), /* Thrustmaster Xbox One controllers */
  509. XPAD_XBOX360_VENDOR(0x045e), /* Microsoft Xbox 360 controllers */
  510. XPAD_XBOXONE_VENDOR(0x045e), /* Microsoft Xbox One controllers */
  511. XPAD_XBOX360_VENDOR(0x046d), /* Logitech Xbox 360-style controllers */
  512. XPAD_XBOX360_VENDOR(0x0502), /* Acer Inc. Xbox 360 style controllers */
  513. XPAD_XBOX360_VENDOR(0x056e), /* Elecom JC-U3613M */
  514. XPAD_XBOX360_VENDOR(0x06a3), /* Saitek P3600 */
  515. XPAD_XBOX360_VENDOR(0x0738), /* Mad Catz Xbox 360 controllers */
  516. { USB_DEVICE(0x0738, 0x4540) }, /* Mad Catz Beat Pad */
  517. XPAD_XBOXONE_VENDOR(0x0738), /* Mad Catz FightStick TE 2 */
  518. XPAD_XBOX360_VENDOR(0x07ff), /* Mad Catz Gamepad */
  519. XPAD_XBOXONE_VENDOR(0x0b05), /* ASUS controllers */
  520. XPAD_XBOX360_VENDOR(0x0c12), /* Zeroplus X-Box 360 controllers */
  521. XPAD_XBOX360_VENDOR(0x0db0), /* Micro Star International X-Box 360 controllers */
  522. XPAD_XBOX360_VENDOR(0x0e6f), /* 0x0e6f Xbox 360 controllers */
  523. XPAD_XBOXONE_VENDOR(0x0e6f), /* 0x0e6f Xbox One controllers */
  524. XPAD_XBOX360_VENDOR(0x0f0d), /* Hori controllers */
  525. XPAD_XBOXONE_VENDOR(0x0f0d), /* Hori controllers */
  526. XPAD_XBOX360_VENDOR(0x1038), /* SteelSeries controllers */
  527. XPAD_XBOXONE_VENDOR(0x10f5), /* Turtle Beach Controllers */
  528. XPAD_XBOX360_VENDOR(0x11c9), /* Nacon GC100XF */
  529. XPAD_XBOX360_VENDOR(0x11ff), /* PXN V900 */
  530. XPAD_XBOX360_VENDOR(0x1209), /* Ardwiino Controllers */
  531. XPAD_XBOX360_VENDOR(0x12ab), /* Xbox 360 dance pads */
  532. XPAD_XBOX360_VENDOR(0x1430), /* RedOctane Xbox 360 controllers */
  533. XPAD_XBOX360_VENDOR(0x146b), /* Bigben Interactive controllers */
  534. XPAD_XBOX360_VENDOR(0x1532), /* Razer Sabertooth */
  535. XPAD_XBOXONE_VENDOR(0x1532), /* Razer Wildcat */
  536. XPAD_XBOX360_VENDOR(0x15e4), /* Numark Xbox 360 controllers */
  537. XPAD_XBOX360_VENDOR(0x162e), /* Joytech Xbox 360 controllers */
  538. XPAD_XBOX360_VENDOR(0x1689), /* Razer Onza */
  539. XPAD_XBOX360_VENDOR(0x17ef), /* Lenovo */
  540. XPAD_XBOX360_VENDOR(0x1949), /* Amazon controllers */
  541. XPAD_XBOX360_VENDOR(0x1a86), /* Nanjing Qinheng Microelectronics (WCH) */
  542. XPAD_XBOX360_VENDOR(0x1bad), /* Harmonix Rock Band guitar and drums */
  543. XPAD_XBOX360_VENDOR(0x1ee9), /* ZOTAC Technology Limited */
  544. XPAD_XBOX360_VENDOR(0x20bc), /* BETOP wireless dongles */
  545. XPAD_XBOX360_VENDOR(0x20d6), /* PowerA controllers */
  546. XPAD_XBOXONE_VENDOR(0x20d6), /* PowerA controllers */
  547. XPAD_XBOX360_VENDOR(0x2345), /* Machenike Controllers */
  548. XPAD_XBOX360_VENDOR(0x24c6), /* PowerA controllers */
  549. XPAD_XBOXONE_VENDOR(0x24c6), /* PowerA controllers */
  550. XPAD_XBOX360_VENDOR(0x2563), /* OneXPlayer Gamepad */
  551. XPAD_XBOX360_VENDOR(0x260d), /* Dareu H101 */
  552. XPAD_XBOXONE_VENDOR(0x294b), /* Snakebyte */
  553. XPAD_XBOX360_VENDOR(0x2993), /* TECNO Mobile */
  554. XPAD_XBOX360_VENDOR(0x2c22), /* Qanba Controllers */
  555. XPAD_XBOX360_VENDOR(0x2dc8), /* 8BitDo Controllers */
  556. XPAD_XBOXONE_VENDOR(0x2dc8), /* 8BitDo Controllers */
  557. XPAD_XBOXONE_VENDOR(0x2e24), /* Hyperkin Controllers */
  558. XPAD_XBOX360_VENDOR(0x2f24), /* GameSir Controllers */
  559. XPAD_XBOXONE_VENDOR(0x2e95), /* SCUF Gaming Controller */
  560. XPAD_XBOX360_VENDOR(0x31e3), /* Wooting Keyboards */
  561. XPAD_XBOX360_VENDOR(0x3285), /* Nacon GC-100 */
  562. XPAD_XBOXONE_VENDOR(0x3285), /* Nacon Evol-X */
  563. XPAD_XBOX360_VENDOR(0x3537), /* GameSir Controllers */
  564. XPAD_XBOXONE_VENDOR(0x3537), /* GameSir Controllers */
  565. XPAD_XBOX360_VENDOR(0x3651), /* CRKD Controllers */
  566. XPAD_XBOXONE_VENDOR(0x366c), /* ByoWave controllers */
  567. XPAD_XBOX360_VENDOR(0x37d7), /* Flydigi Controllers */
  568. XPAD_XBOX360_VENDOR(0x413d), /* Black Shark Green Ghost Controller */
  569. { }
  570. };
  571. MODULE_DEVICE_TABLE(usb, xpad_table);
  572. struct xboxone_init_packet {
  573. u16 idVendor;
  574. u16 idProduct;
  575. const u8 *data;
  576. u8 len;
  577. };
  578. #define XBOXONE_INIT_PKT(_vid, _pid, _data) \
  579. { \
  580. .idVendor = (_vid), \
  581. .idProduct = (_pid), \
  582. .data = (_data), \
  583. .len = ARRAY_SIZE(_data), \
  584. }
  585. /*
  586. * starting with xbox one, the game input protocol is used
  587. * magic numbers are taken from
  588. * - https://github.com/xpadneo/gip-dissector/blob/main/src/gip-dissector.lua
  589. * - https://github.com/medusalix/xone/blob/master/bus/protocol.c
  590. */
  591. #define GIP_CMD_ACK 0x01
  592. #define GIP_CMD_ANNOUNCE 0x02
  593. #define GIP_CMD_IDENTIFY 0x04
  594. #define GIP_CMD_POWER 0x05
  595. #define GIP_CMD_AUTHENTICATE 0x06
  596. #define GIP_CMD_VIRTUAL_KEY 0x07
  597. #define GIP_CMD_RUMBLE 0x09
  598. #define GIP_CMD_LED 0x0a
  599. #define GIP_CMD_FIRMWARE 0x0c
  600. #define GIP_CMD_INPUT 0x20
  601. #define GIP_SEQ0 0x00
  602. #define GIP_OPT_ACK 0x10
  603. #define GIP_OPT_INTERNAL 0x20
  604. /*
  605. * length of the command payload encoded with
  606. * https://en.wikipedia.org/wiki/LEB128
  607. * which is a no-op for N < 128
  608. */
  609. #define GIP_PL_LEN(N) (N)
  610. /*
  611. * payload specific defines
  612. */
  613. #define GIP_PWR_ON 0x00
  614. #define GIP_LED_ON 0x01
  615. #define GIP_MOTOR_R BIT(0)
  616. #define GIP_MOTOR_L BIT(1)
  617. #define GIP_MOTOR_RT BIT(2)
  618. #define GIP_MOTOR_LT BIT(3)
  619. #define GIP_MOTOR_ALL (GIP_MOTOR_R | GIP_MOTOR_L | GIP_MOTOR_RT | GIP_MOTOR_LT)
  620. #define GIP_WIRED_INTF_DATA 0
  621. #define GIP_WIRED_INTF_AUDIO 1
  622. /*
  623. * This packet is required for all Xbox One pads with 2015
  624. * or later firmware installed (or present from the factory).
  625. */
  626. static const u8 xboxone_power_on[] = {
  627. GIP_CMD_POWER, GIP_OPT_INTERNAL, GIP_SEQ0, GIP_PL_LEN(1), GIP_PWR_ON
  628. };
  629. /*
  630. * This packet is required for Xbox One S (0x045e:0x02ea)
  631. * and Xbox One Elite Series 2 (0x045e:0x0b00) pads to
  632. * initialize the controller that was previously used in
  633. * Bluetooth mode.
  634. */
  635. static const u8 xboxone_s_init[] = {
  636. GIP_CMD_POWER, GIP_OPT_INTERNAL, GIP_SEQ0, 0x0f, 0x06
  637. };
  638. /*
  639. * This packet is required to get additional input data
  640. * from Xbox One Elite Series 2 (0x045e:0x0b00) pads.
  641. * We mostly do this right now to get paddle data
  642. */
  643. static const u8 extra_input_packet_init[] = {
  644. 0x4d, 0x10, 0x01, 0x02, 0x07, 0x00
  645. };
  646. /*
  647. * This packet is required for the Titanfall 2 Xbox One pads
  648. * (0x0e6f:0x0165) to finish initialization and for Hori pads
  649. * (0x0f0d:0x0067) to make the analog sticks work.
  650. */
  651. static const u8 xboxone_hori_ack_id[] = {
  652. GIP_CMD_ACK, GIP_OPT_INTERNAL, GIP_SEQ0, GIP_PL_LEN(9),
  653. 0x00, GIP_CMD_IDENTIFY, GIP_OPT_INTERNAL, 0x3a, 0x00, 0x00, 0x00, 0x80, 0x00
  654. };
  655. /*
  656. * This packet is sent by default on Windows, and is required for some pads to
  657. * start sending input reports, including most (all?) of the PDP. These pads
  658. * include: (0x0e6f:0x02ab), (0x0e6f:0x02a4), (0x0e6f:0x02a6).
  659. */
  660. static const u8 xboxone_led_on[] = { GIP_CMD_LED, GIP_OPT_INTERNAL, GIP_SEQ0,
  661. GIP_PL_LEN(3), 0x00, GIP_LED_ON, 0x14 };
  662. /*
  663. * This packet is required for most (all?) of the PDP pads to start
  664. * sending input reports. These pads include: (0x0e6f:0x02ab),
  665. * (0x0e6f:0x02a4), (0x0e6f:0x02a6).
  666. */
  667. static const u8 xboxone_auth_done[] = {
  668. GIP_CMD_AUTHENTICATE, GIP_OPT_INTERNAL, GIP_SEQ0, GIP_PL_LEN(2), 0x01, 0x00
  669. };
  670. /*
  671. * A specific rumble packet is required for some PowerA pads to start
  672. * sending input reports. One of those pads is (0x24c6:0x543a).
  673. */
  674. static const u8 xboxone_rumblebegin_init[] = {
  675. GIP_CMD_RUMBLE, 0x00, GIP_SEQ0, GIP_PL_LEN(9),
  676. 0x00, GIP_MOTOR_ALL, 0x00, 0x00, 0x1D, 0x1D, 0xFF, 0x00, 0x00
  677. };
  678. /*
  679. * A rumble packet with zero FF intensity will immediately
  680. * terminate the rumbling required to init PowerA pads.
  681. * This should happen fast enough that the motors don't
  682. * spin up to enough speed to actually vibrate the gamepad.
  683. */
  684. static const u8 xboxone_rumbleend_init[] = {
  685. GIP_CMD_RUMBLE, 0x00, GIP_SEQ0, GIP_PL_LEN(9),
  686. 0x00, GIP_MOTOR_ALL, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  687. };
  688. /*
  689. * This specifies the selection of init packets that a gamepad
  690. * will be sent on init *and* the order in which they will be
  691. * sent. The correct sequence number will be added when the
  692. * packet is going to be sent.
  693. */
  694. static const struct xboxone_init_packet xboxone_init_packets[] = {
  695. XBOXONE_INIT_PKT(0x0e6f, 0x0165, xboxone_hori_ack_id),
  696. XBOXONE_INIT_PKT(0x0f0d, 0x0067, xboxone_hori_ack_id),
  697. XBOXONE_INIT_PKT(0x0000, 0x0000, xboxone_power_on),
  698. XBOXONE_INIT_PKT(0x045e, 0x02ea, xboxone_s_init),
  699. XBOXONE_INIT_PKT(0x045e, 0x0b00, xboxone_s_init),
  700. XBOXONE_INIT_PKT(0x045e, 0x0b00, extra_input_packet_init),
  701. XBOXONE_INIT_PKT(0x0000, 0x0000, xboxone_led_on),
  702. XBOXONE_INIT_PKT(0x0000, 0x0000, xboxone_auth_done),
  703. XBOXONE_INIT_PKT(0x24c6, 0x541a, xboxone_rumblebegin_init),
  704. XBOXONE_INIT_PKT(0x24c6, 0x542a, xboxone_rumblebegin_init),
  705. XBOXONE_INIT_PKT(0x24c6, 0x543a, xboxone_rumblebegin_init),
  706. XBOXONE_INIT_PKT(0x24c6, 0x541a, xboxone_rumbleend_init),
  707. XBOXONE_INIT_PKT(0x24c6, 0x542a, xboxone_rumbleend_init),
  708. XBOXONE_INIT_PKT(0x24c6, 0x543a, xboxone_rumbleend_init),
  709. };
  710. struct xpad_output_packet {
  711. u8 data[XPAD_PKT_LEN];
  712. u8 len;
  713. bool pending;
  714. };
  715. #define XPAD_OUT_CMD_IDX 0
  716. #define XPAD_OUT_FF_IDX 1
  717. #define XPAD_OUT_LED_IDX (1 + IS_ENABLED(CONFIG_JOYSTICK_XPAD_FF))
  718. #define XPAD_NUM_OUT_PACKETS (1 + \
  719. IS_ENABLED(CONFIG_JOYSTICK_XPAD_FF) + \
  720. IS_ENABLED(CONFIG_JOYSTICK_XPAD_LEDS))
  721. struct usb_xpad {
  722. struct input_dev *dev; /* input device interface */
  723. struct input_dev __rcu *x360w_dev;
  724. struct usb_device *udev; /* usb device */
  725. struct usb_interface *intf; /* usb interface */
  726. bool pad_present;
  727. bool input_created;
  728. struct urb *irq_in; /* urb for interrupt in report */
  729. unsigned char *idata; /* input data */
  730. dma_addr_t idata_dma;
  731. struct urb *irq_out; /* urb for interrupt out report */
  732. struct usb_anchor irq_out_anchor;
  733. bool irq_out_active; /* we must not use an active URB */
  734. u8 odata_serial; /* serial number for xbox one protocol */
  735. unsigned char *odata; /* output data */
  736. dma_addr_t odata_dma;
  737. spinlock_t odata_lock;
  738. struct xpad_output_packet out_packets[XPAD_NUM_OUT_PACKETS];
  739. int last_out_packet;
  740. int init_seq;
  741. #if defined(CONFIG_JOYSTICK_XPAD_LEDS)
  742. struct xpad_led *led;
  743. #endif
  744. char phys[64]; /* physical device path */
  745. int mapping; /* map d-pad to buttons or to axes */
  746. int xtype; /* type of xbox device */
  747. int packet_type; /* type of the extended packet */
  748. int pad_nr; /* the order x360 pads were attached */
  749. const char *name; /* name of the device */
  750. struct work_struct work; /* init/remove device from callback */
  751. time64_t mode_btn_down_ts;
  752. bool delay_init; /* init packets should be delayed */
  753. bool delayed_init_done;
  754. };
  755. static int xpad_init_input(struct usb_xpad *xpad);
  756. static void xpad_deinit_input(struct usb_xpad *xpad);
  757. static int xpad_start_input(struct usb_xpad *xpad);
  758. static void xpadone_ack_mode_report(struct usb_xpad *xpad, u8 seq_num);
  759. static void xpad360w_poweroff_controller(struct usb_xpad *xpad);
  760. /*
  761. * xpad_process_packet
  762. *
  763. * Completes a request by converting the data into events for the
  764. * input subsystem.
  765. *
  766. * The used report descriptor was taken from ITO Takayuki's website:
  767. * http://euc.jp/periphs/xbox-controller.ja.html
  768. */
  769. static void xpad_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data)
  770. {
  771. struct input_dev *dev = xpad->dev;
  772. if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
  773. /* left stick */
  774. input_report_abs(dev, ABS_X,
  775. (__s16) le16_to_cpup((__le16 *)(data + 12)));
  776. input_report_abs(dev, ABS_Y,
  777. ~(__s16) le16_to_cpup((__le16 *)(data + 14)));
  778. /* right stick */
  779. input_report_abs(dev, ABS_RX,
  780. (__s16) le16_to_cpup((__le16 *)(data + 16)));
  781. input_report_abs(dev, ABS_RY,
  782. ~(__s16) le16_to_cpup((__le16 *)(data + 18)));
  783. }
  784. /* triggers left/right */
  785. if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
  786. input_report_key(dev, BTN_TL2, data[10]);
  787. input_report_key(dev, BTN_TR2, data[11]);
  788. } else {
  789. input_report_abs(dev, ABS_Z, data[10]);
  790. input_report_abs(dev, ABS_RZ, data[11]);
  791. }
  792. /* digital pad */
  793. if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
  794. /* dpad as buttons (left, right, up, down) */
  795. input_report_key(dev, BTN_DPAD_LEFT, data[2] & BIT(2));
  796. input_report_key(dev, BTN_DPAD_RIGHT, data[2] & BIT(3));
  797. input_report_key(dev, BTN_DPAD_UP, data[2] & BIT(0));
  798. input_report_key(dev, BTN_DPAD_DOWN, data[2] & BIT(1));
  799. } else {
  800. input_report_abs(dev, ABS_HAT0X,
  801. !!(data[2] & 0x08) - !!(data[2] & 0x04));
  802. input_report_abs(dev, ABS_HAT0Y,
  803. !!(data[2] & 0x02) - !!(data[2] & 0x01));
  804. }
  805. /* start/back buttons and stick press left/right */
  806. input_report_key(dev, BTN_START, data[2] & BIT(4));
  807. input_report_key(dev, BTN_SELECT, data[2] & BIT(5));
  808. input_report_key(dev, BTN_THUMBL, data[2] & BIT(6));
  809. input_report_key(dev, BTN_THUMBR, data[2] & BIT(7));
  810. /* "analog" buttons A, B, X, Y */
  811. input_report_key(dev, BTN_A, data[4]);
  812. input_report_key(dev, BTN_B, data[5]);
  813. input_report_key(dev, BTN_X, data[6]);
  814. input_report_key(dev, BTN_Y, data[7]);
  815. /* "analog" buttons black, white */
  816. input_report_key(dev, BTN_C, data[8]);
  817. input_report_key(dev, BTN_Z, data[9]);
  818. input_sync(dev);
  819. }
  820. /*
  821. * xpad360_process_packet
  822. *
  823. * Completes a request by converting the data into events for the
  824. * input subsystem. It is version for xbox 360 controller
  825. *
  826. * The used report descriptor was taken from:
  827. * http://www.free60.org/wiki/Gamepad
  828. */
  829. static void xpad360_process_packet(struct usb_xpad *xpad, struct input_dev *dev,
  830. u16 cmd, unsigned char *data)
  831. {
  832. /* valid pad data */
  833. if (data[0] != 0x00)
  834. return;
  835. /* digital pad */
  836. if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
  837. /* dpad as buttons (left, right, up, down) */
  838. input_report_key(dev, BTN_DPAD_LEFT, data[2] & BIT(2));
  839. input_report_key(dev, BTN_DPAD_RIGHT, data[2] & BIT(3));
  840. input_report_key(dev, BTN_DPAD_UP, data[2] & BIT(0));
  841. input_report_key(dev, BTN_DPAD_DOWN, data[2] & BIT(1));
  842. }
  843. /*
  844. * This should be a simple else block. However historically
  845. * xbox360w has mapped DPAD to buttons while xbox360 did not. This
  846. * made no sense, but now we can not just switch back and have to
  847. * support both behaviors.
  848. */
  849. if (!(xpad->mapping & MAP_DPAD_TO_BUTTONS) ||
  850. xpad->xtype == XTYPE_XBOX360W) {
  851. input_report_abs(dev, ABS_HAT0X,
  852. !!(data[2] & 0x08) - !!(data[2] & 0x04));
  853. input_report_abs(dev, ABS_HAT0Y,
  854. !!(data[2] & 0x02) - !!(data[2] & 0x01));
  855. }
  856. /* start/back buttons */
  857. input_report_key(dev, BTN_START, data[2] & BIT(4));
  858. input_report_key(dev, BTN_SELECT, data[2] & BIT(5));
  859. /* stick press left/right */
  860. input_report_key(dev, BTN_THUMBL, data[2] & BIT(6));
  861. input_report_key(dev, BTN_THUMBR, data[2] & BIT(7));
  862. /* buttons A,B,X,Y,TL,TR and MODE */
  863. input_report_key(dev, BTN_A, data[3] & BIT(4));
  864. input_report_key(dev, BTN_B, data[3] & BIT(5));
  865. input_report_key(dev, BTN_X, data[3] & BIT(6));
  866. input_report_key(dev, BTN_Y, data[3] & BIT(7));
  867. input_report_key(dev, BTN_TL, data[3] & BIT(0));
  868. input_report_key(dev, BTN_TR, data[3] & BIT(1));
  869. input_report_key(dev, BTN_MODE, data[3] & BIT(2));
  870. if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
  871. /* left stick */
  872. input_report_abs(dev, ABS_X,
  873. (__s16) le16_to_cpup((__le16 *)(data + 6)));
  874. input_report_abs(dev, ABS_Y,
  875. ~(__s16) le16_to_cpup((__le16 *)(data + 8)));
  876. /* right stick */
  877. input_report_abs(dev, ABS_RX,
  878. (__s16) le16_to_cpup((__le16 *)(data + 10)));
  879. input_report_abs(dev, ABS_RY,
  880. ~(__s16) le16_to_cpup((__le16 *)(data + 12)));
  881. }
  882. /* triggers left/right */
  883. if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
  884. input_report_key(dev, BTN_TL2, data[4]);
  885. input_report_key(dev, BTN_TR2, data[5]);
  886. } else {
  887. input_report_abs(dev, ABS_Z, data[4]);
  888. input_report_abs(dev, ABS_RZ, data[5]);
  889. }
  890. input_sync(dev);
  891. /* XBOX360W controllers can't be turned off without driver assistance */
  892. if (xpad->xtype == XTYPE_XBOX360W) {
  893. if (xpad->mode_btn_down_ts > 0 && xpad->pad_present &&
  894. ((ktime_get_seconds() - xpad->mode_btn_down_ts) >=
  895. XPAD360W_POWEROFF_TIMEOUT)) {
  896. xpad360w_poweroff_controller(xpad);
  897. xpad->mode_btn_down_ts = 0;
  898. return;
  899. }
  900. /* mode button down/up */
  901. if (data[3] & BIT(2))
  902. xpad->mode_btn_down_ts = ktime_get_seconds();
  903. else
  904. xpad->mode_btn_down_ts = 0;
  905. }
  906. }
  907. static void xpad_presence_work(struct work_struct *work)
  908. {
  909. struct usb_xpad *xpad = container_of(work, struct usb_xpad, work);
  910. int error;
  911. if (xpad->pad_present) {
  912. error = xpad_init_input(xpad);
  913. if (error) {
  914. /* complain only, not much else we can do here */
  915. dev_err(&xpad->dev->dev,
  916. "unable to init device: %d\n", error);
  917. } else {
  918. rcu_assign_pointer(xpad->x360w_dev, xpad->dev);
  919. }
  920. } else {
  921. RCU_INIT_POINTER(xpad->x360w_dev, NULL);
  922. synchronize_rcu();
  923. /*
  924. * Now that we are sure xpad360w_process_packet is not
  925. * using input device we can get rid of it.
  926. */
  927. xpad_deinit_input(xpad);
  928. }
  929. }
  930. /*
  931. * xpad360w_process_packet
  932. *
  933. * Completes a request by converting the data into events for the
  934. * input subsystem. It is version for xbox 360 wireless controller.
  935. *
  936. * Byte.Bit
  937. * 00.1 - Status change: The controller or headset has connected/disconnected
  938. * Bits 01.7 and 01.6 are valid
  939. * 01.7 - Controller present
  940. * 01.6 - Headset present
  941. * 01.1 - Pad state (Bytes 4+) valid
  942. *
  943. */
  944. static void xpad360w_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data)
  945. {
  946. struct input_dev *dev;
  947. bool present;
  948. /* Presence change */
  949. if (data[0] & 0x08) {
  950. present = (data[1] & 0x80) != 0;
  951. if (xpad->pad_present != present) {
  952. xpad->pad_present = present;
  953. schedule_work(&xpad->work);
  954. }
  955. }
  956. /* Valid pad data */
  957. if (data[1] != 0x1)
  958. return;
  959. rcu_read_lock();
  960. dev = rcu_dereference(xpad->x360w_dev);
  961. if (dev)
  962. xpad360_process_packet(xpad, dev, cmd, &data[4]);
  963. rcu_read_unlock();
  964. }
  965. /*
  966. * xpadone_process_packet
  967. *
  968. * Completes a request by converting the data into events for the
  969. * input subsystem. This version is for the Xbox One controller.
  970. *
  971. * The report format was gleaned from
  972. * https://github.com/kylelemons/xbox/blob/master/xbox.go
  973. */
  974. static void xpadone_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data, u32 len)
  975. {
  976. struct input_dev *dev = xpad->dev;
  977. bool do_sync = false;
  978. /* the xbox button has its own special report */
  979. if (data[0] == GIP_CMD_VIRTUAL_KEY) {
  980. /*
  981. * The Xbox One S controller requires these reports to be
  982. * acked otherwise it continues sending them forever and
  983. * won't report further mode button events.
  984. */
  985. if (data[1] == (GIP_OPT_ACK | GIP_OPT_INTERNAL))
  986. xpadone_ack_mode_report(xpad, data[2]);
  987. input_report_key(dev, BTN_MODE, data[4] & GENMASK(1, 0));
  988. input_sync(dev);
  989. do_sync = true;
  990. } else if (data[0] == GIP_CMD_FIRMWARE) {
  991. /* Some packet formats force us to use this separate to poll paddle inputs */
  992. if (xpad->packet_type == PKT_XBE2_FW_5_11) {
  993. /* Mute paddles if controller is in a custom profile slot
  994. * Checked by looking at the active profile slot to
  995. * verify it's the default slot
  996. */
  997. if (data[19] != 0)
  998. data[18] = 0;
  999. /* Elite Series 2 split packet paddle bits */
  1000. input_report_key(dev, BTN_GRIPR, data[18] & BIT(0));
  1001. input_report_key(dev, BTN_GRIPR2, data[18] & BIT(1));
  1002. input_report_key(dev, BTN_GRIPL, data[18] & BIT(2));
  1003. input_report_key(dev, BTN_GRIPL2, data[18] & BIT(3));
  1004. do_sync = true;
  1005. }
  1006. } else if (data[0] == GIP_CMD_ANNOUNCE) {
  1007. int error;
  1008. if (xpad->delay_init && !xpad->delayed_init_done) {
  1009. xpad->delayed_init_done = true;
  1010. error = xpad_start_input(xpad);
  1011. if (error)
  1012. dev_warn(&xpad->dev->dev,
  1013. "unable to start delayed input: %d\n",
  1014. error);
  1015. }
  1016. } else if (data[0] == GIP_CMD_INPUT) { /* The main valid packet type for inputs */
  1017. /* menu/view buttons */
  1018. input_report_key(dev, BTN_START, data[4] & BIT(2));
  1019. input_report_key(dev, BTN_SELECT, data[4] & BIT(3));
  1020. if (xpad->mapping & MAP_SHARE_BUTTON) {
  1021. if (xpad->mapping & MAP_SHARE_OFFSET)
  1022. input_report_key(dev, KEY_RECORD, data[len - 26] & BIT(0));
  1023. else
  1024. input_report_key(dev, KEY_RECORD, data[len - 18] & BIT(0));
  1025. }
  1026. /* buttons A,B,X,Y */
  1027. input_report_key(dev, BTN_A, data[4] & BIT(4));
  1028. input_report_key(dev, BTN_B, data[4] & BIT(5));
  1029. input_report_key(dev, BTN_X, data[4] & BIT(6));
  1030. input_report_key(dev, BTN_Y, data[4] & BIT(7));
  1031. /* digital pad */
  1032. if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
  1033. /* dpad as buttons (left, right, up, down) */
  1034. input_report_key(dev, BTN_DPAD_LEFT, data[5] & BIT(2));
  1035. input_report_key(dev, BTN_DPAD_RIGHT, data[5] & BIT(3));
  1036. input_report_key(dev, BTN_DPAD_UP, data[5] & BIT(0));
  1037. input_report_key(dev, BTN_DPAD_DOWN, data[5] & BIT(1));
  1038. } else {
  1039. input_report_abs(dev, ABS_HAT0X,
  1040. !!(data[5] & 0x08) - !!(data[5] & 0x04));
  1041. input_report_abs(dev, ABS_HAT0Y,
  1042. !!(data[5] & 0x02) - !!(data[5] & 0x01));
  1043. }
  1044. /* TL/TR */
  1045. input_report_key(dev, BTN_TL, data[5] & BIT(4));
  1046. input_report_key(dev, BTN_TR, data[5] & BIT(5));
  1047. /* stick press left/right */
  1048. input_report_key(dev, BTN_THUMBL, data[5] & BIT(6));
  1049. input_report_key(dev, BTN_THUMBR, data[5] & BIT(7));
  1050. if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
  1051. /* left stick */
  1052. input_report_abs(dev, ABS_X,
  1053. (__s16) le16_to_cpup((__le16 *)(data + 10)));
  1054. input_report_abs(dev, ABS_Y,
  1055. ~(__s16) le16_to_cpup((__le16 *)(data + 12)));
  1056. /* right stick */
  1057. input_report_abs(dev, ABS_RX,
  1058. (__s16) le16_to_cpup((__le16 *)(data + 14)));
  1059. input_report_abs(dev, ABS_RY,
  1060. ~(__s16) le16_to_cpup((__le16 *)(data + 16)));
  1061. }
  1062. /* triggers left/right */
  1063. if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
  1064. input_report_key(dev, BTN_TL2,
  1065. (__u16) le16_to_cpup((__le16 *)(data + 6)));
  1066. input_report_key(dev, BTN_TR2,
  1067. (__u16) le16_to_cpup((__le16 *)(data + 8)));
  1068. } else {
  1069. input_report_abs(dev, ABS_Z,
  1070. (__u16) le16_to_cpup((__le16 *)(data + 6)));
  1071. input_report_abs(dev, ABS_RZ,
  1072. (__u16) le16_to_cpup((__le16 *)(data + 8)));
  1073. }
  1074. /* Profile button has a value of 0-3, so it is reported as an axis */
  1075. if (xpad->mapping & MAP_PROFILE_BUTTON)
  1076. input_report_abs(dev, ABS_PROFILE, data[34]);
  1077. /* paddle handling */
  1078. /* based on SDL's SDL_hidapi_xboxone.c */
  1079. if (xpad->mapping & MAP_PADDLES) {
  1080. if (xpad->packet_type == PKT_XBE1) {
  1081. /* Mute paddles if controller has a custom mapping applied.
  1082. * Checked by comparing the current mapping
  1083. * config against the factory mapping config
  1084. */
  1085. if (memcmp(&data[4], &data[18], 2) != 0)
  1086. data[32] = 0;
  1087. /* OG Elite Series Controller paddle bits */
  1088. input_report_key(dev, BTN_GRIPR, data[32] & BIT(1));
  1089. input_report_key(dev, BTN_GRIPR2, data[32] & BIT(3));
  1090. input_report_key(dev, BTN_GRIPL, data[32] & BIT(0));
  1091. input_report_key(dev, BTN_GRIPL2, data[32] & BIT(2));
  1092. } else if (xpad->packet_type == PKT_XBE2_FW_OLD) {
  1093. /* Mute paddles if controller has a custom mapping applied.
  1094. * Checked by comparing the current mapping
  1095. * config against the factory mapping config
  1096. */
  1097. if (data[19] != 0)
  1098. data[18] = 0;
  1099. /* Elite Series 2 4.x firmware paddle bits */
  1100. input_report_key(dev, BTN_GRIPR, data[18] & BIT(0));
  1101. input_report_key(dev, BTN_GRIPR2, data[18] & BIT(1));
  1102. input_report_key(dev, BTN_GRIPL, data[18] & BIT(2));
  1103. input_report_key(dev, BTN_GRIPL2, data[18] & BIT(3));
  1104. } else if (xpad->packet_type == PKT_XBE2_FW_5_EARLY) {
  1105. /* Mute paddles if controller has a custom mapping applied.
  1106. * Checked by comparing the current mapping
  1107. * config against the factory mapping config
  1108. */
  1109. if (data[23] != 0)
  1110. data[22] = 0;
  1111. /* Elite Series 2 5.x firmware paddle bits
  1112. * (before the packet was split)
  1113. */
  1114. input_report_key(dev, BTN_GRIPR, data[22] & BIT(0));
  1115. input_report_key(dev, BTN_GRIPR2, data[22] & BIT(1));
  1116. input_report_key(dev, BTN_GRIPL, data[22] & BIT(2));
  1117. input_report_key(dev, BTN_GRIPL2, data[22] & BIT(3));
  1118. }
  1119. }
  1120. do_sync = true;
  1121. }
  1122. if (do_sync)
  1123. input_sync(dev);
  1124. }
  1125. static void xpad_irq_in(struct urb *urb)
  1126. {
  1127. struct usb_xpad *xpad = urb->context;
  1128. struct device *dev = &xpad->intf->dev;
  1129. int retval, status;
  1130. status = urb->status;
  1131. switch (status) {
  1132. case 0:
  1133. /* success */
  1134. break;
  1135. case -ECONNRESET:
  1136. case -ENOENT:
  1137. case -ESHUTDOWN:
  1138. /* this urb is terminated, clean up */
  1139. dev_dbg(dev, "%s - urb shutting down with status: %d\n",
  1140. __func__, status);
  1141. return;
  1142. default:
  1143. dev_dbg(dev, "%s - nonzero urb status received: %d\n",
  1144. __func__, status);
  1145. goto exit;
  1146. }
  1147. switch (xpad->xtype) {
  1148. case XTYPE_XBOX360:
  1149. xpad360_process_packet(xpad, xpad->dev, 0, xpad->idata);
  1150. break;
  1151. case XTYPE_XBOX360W:
  1152. xpad360w_process_packet(xpad, 0, xpad->idata);
  1153. break;
  1154. case XTYPE_XBOXONE:
  1155. xpadone_process_packet(xpad, 0, xpad->idata, urb->actual_length);
  1156. break;
  1157. default:
  1158. xpad_process_packet(xpad, 0, xpad->idata);
  1159. }
  1160. exit:
  1161. retval = usb_submit_urb(urb, GFP_ATOMIC);
  1162. if (retval)
  1163. dev_err(dev, "%s - usb_submit_urb failed with result %d\n",
  1164. __func__, retval);
  1165. }
  1166. /* Callers must hold xpad->odata_lock spinlock */
  1167. static bool xpad_prepare_next_init_packet(struct usb_xpad *xpad)
  1168. {
  1169. const struct xboxone_init_packet *init_packet;
  1170. if (xpad->xtype != XTYPE_XBOXONE)
  1171. return false;
  1172. /*
  1173. * Some dongles will discard init packets if they're sent before the
  1174. * controller connects. In these cases, we need to wait until we get
  1175. * an announce packet from them to send the init packet sequence.
  1176. */
  1177. if (xpad->delay_init && !xpad->delayed_init_done)
  1178. return false;
  1179. /* Perform initialization sequence for Xbox One pads that require it */
  1180. while (xpad->init_seq < ARRAY_SIZE(xboxone_init_packets)) {
  1181. init_packet = &xboxone_init_packets[xpad->init_seq++];
  1182. if (init_packet->idVendor != 0 &&
  1183. init_packet->idVendor != xpad->dev->id.vendor)
  1184. continue;
  1185. if (init_packet->idProduct != 0 &&
  1186. init_packet->idProduct != xpad->dev->id.product)
  1187. continue;
  1188. /* This packet applies to our device, so prepare to send it */
  1189. memcpy(xpad->odata, init_packet->data, init_packet->len);
  1190. xpad->irq_out->transfer_buffer_length = init_packet->len;
  1191. /* Update packet with current sequence number */
  1192. xpad->odata[2] = xpad->odata_serial++;
  1193. return true;
  1194. }
  1195. return false;
  1196. }
  1197. /* Callers must hold xpad->odata_lock spinlock */
  1198. static bool xpad_prepare_next_out_packet(struct usb_xpad *xpad)
  1199. {
  1200. struct xpad_output_packet *pkt, *packet = NULL;
  1201. int i;
  1202. /* We may have init packets to send before we can send user commands */
  1203. if (xpad_prepare_next_init_packet(xpad))
  1204. return true;
  1205. for (i = 0; i < XPAD_NUM_OUT_PACKETS; i++) {
  1206. if (++xpad->last_out_packet >= XPAD_NUM_OUT_PACKETS)
  1207. xpad->last_out_packet = 0;
  1208. pkt = &xpad->out_packets[xpad->last_out_packet];
  1209. if (pkt->pending) {
  1210. dev_dbg(&xpad->intf->dev,
  1211. "%s - found pending output packet %d\n",
  1212. __func__, xpad->last_out_packet);
  1213. packet = pkt;
  1214. break;
  1215. }
  1216. }
  1217. if (packet) {
  1218. memcpy(xpad->odata, packet->data, packet->len);
  1219. xpad->irq_out->transfer_buffer_length = packet->len;
  1220. packet->pending = false;
  1221. return true;
  1222. }
  1223. return false;
  1224. }
  1225. /* Callers must hold xpad->odata_lock spinlock */
  1226. static int xpad_try_sending_next_out_packet(struct usb_xpad *xpad)
  1227. {
  1228. int error;
  1229. if (!xpad->irq_out_active && xpad_prepare_next_out_packet(xpad)) {
  1230. usb_anchor_urb(xpad->irq_out, &xpad->irq_out_anchor);
  1231. error = usb_submit_urb(xpad->irq_out, GFP_ATOMIC);
  1232. if (error) {
  1233. if (error != -ENODEV)
  1234. dev_err(&xpad->intf->dev,
  1235. "%s - usb_submit_urb failed with result %d\n",
  1236. __func__, error);
  1237. usb_unanchor_urb(xpad->irq_out);
  1238. return error;
  1239. }
  1240. xpad->irq_out_active = true;
  1241. }
  1242. return 0;
  1243. }
  1244. static void xpad_irq_out(struct urb *urb)
  1245. {
  1246. struct usb_xpad *xpad = urb->context;
  1247. struct device *dev = &xpad->intf->dev;
  1248. int status = urb->status;
  1249. int error;
  1250. guard(spinlock_irqsave)(&xpad->odata_lock);
  1251. switch (status) {
  1252. case 0:
  1253. /* success */
  1254. xpad->irq_out_active = xpad_prepare_next_out_packet(xpad);
  1255. break;
  1256. case -ECONNRESET:
  1257. case -ENOENT:
  1258. case -ESHUTDOWN:
  1259. /* this urb is terminated, clean up */
  1260. dev_dbg(dev, "%s - urb shutting down with status: %d\n",
  1261. __func__, status);
  1262. xpad->irq_out_active = false;
  1263. break;
  1264. default:
  1265. dev_dbg(dev, "%s - nonzero urb status received: %d\n",
  1266. __func__, status);
  1267. break;
  1268. }
  1269. if (xpad->irq_out_active) {
  1270. usb_anchor_urb(urb, &xpad->irq_out_anchor);
  1271. error = usb_submit_urb(urb, GFP_ATOMIC);
  1272. if (error) {
  1273. dev_err(dev,
  1274. "%s - usb_submit_urb failed with result %d\n",
  1275. __func__, error);
  1276. usb_unanchor_urb(urb);
  1277. xpad->irq_out_active = false;
  1278. }
  1279. }
  1280. }
  1281. static int xpad_init_output(struct usb_interface *intf, struct usb_xpad *xpad,
  1282. struct usb_endpoint_descriptor *ep_irq_out)
  1283. {
  1284. int error;
  1285. if (xpad->xtype == XTYPE_UNKNOWN)
  1286. return 0;
  1287. init_usb_anchor(&xpad->irq_out_anchor);
  1288. xpad->odata = usb_alloc_coherent(xpad->udev, XPAD_PKT_LEN,
  1289. GFP_KERNEL, &xpad->odata_dma);
  1290. if (!xpad->odata)
  1291. return -ENOMEM;
  1292. spin_lock_init(&xpad->odata_lock);
  1293. xpad->irq_out = usb_alloc_urb(0, GFP_KERNEL);
  1294. if (!xpad->irq_out) {
  1295. error = -ENOMEM;
  1296. goto err_free_coherent;
  1297. }
  1298. usb_fill_int_urb(xpad->irq_out, xpad->udev,
  1299. usb_sndintpipe(xpad->udev, ep_irq_out->bEndpointAddress),
  1300. xpad->odata, XPAD_PKT_LEN,
  1301. xpad_irq_out, xpad, ep_irq_out->bInterval);
  1302. xpad->irq_out->transfer_dma = xpad->odata_dma;
  1303. xpad->irq_out->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1304. return 0;
  1305. err_free_coherent:
  1306. usb_free_coherent(xpad->udev, XPAD_PKT_LEN, xpad->odata, xpad->odata_dma);
  1307. return error;
  1308. }
  1309. static void xpad_stop_output(struct usb_xpad *xpad)
  1310. {
  1311. if (xpad->xtype != XTYPE_UNKNOWN) {
  1312. if (!usb_wait_anchor_empty_timeout(&xpad->irq_out_anchor,
  1313. 5000)) {
  1314. dev_warn(&xpad->intf->dev,
  1315. "timed out waiting for output URB to complete, killing\n");
  1316. usb_kill_anchored_urbs(&xpad->irq_out_anchor);
  1317. }
  1318. }
  1319. }
  1320. static void xpad_deinit_output(struct usb_xpad *xpad)
  1321. {
  1322. if (xpad->xtype != XTYPE_UNKNOWN) {
  1323. usb_free_urb(xpad->irq_out);
  1324. usb_free_coherent(xpad->udev, XPAD_PKT_LEN,
  1325. xpad->odata, xpad->odata_dma);
  1326. }
  1327. }
  1328. static int xpad_inquiry_pad_presence(struct usb_xpad *xpad)
  1329. {
  1330. struct xpad_output_packet *packet =
  1331. &xpad->out_packets[XPAD_OUT_CMD_IDX];
  1332. guard(spinlock_irqsave)(&xpad->odata_lock);
  1333. packet->data[0] = 0x08;
  1334. packet->data[1] = 0x00;
  1335. packet->data[2] = 0x0F;
  1336. packet->data[3] = 0xC0;
  1337. packet->data[4] = 0x00;
  1338. packet->data[5] = 0x00;
  1339. packet->data[6] = 0x00;
  1340. packet->data[7] = 0x00;
  1341. packet->data[8] = 0x00;
  1342. packet->data[9] = 0x00;
  1343. packet->data[10] = 0x00;
  1344. packet->data[11] = 0x00;
  1345. packet->len = 12;
  1346. packet->pending = true;
  1347. /* Reset the sequence so we send out presence first */
  1348. xpad->last_out_packet = -1;
  1349. return xpad_try_sending_next_out_packet(xpad);
  1350. }
  1351. static int xpad_start_xbox_one(struct usb_xpad *xpad)
  1352. {
  1353. int error;
  1354. if (usb_ifnum_to_if(xpad->udev, GIP_WIRED_INTF_AUDIO)) {
  1355. /*
  1356. * Explicitly disable the audio interface. This is needed
  1357. * for some controllers, such as the PowerA Enhanced Wired
  1358. * Controller for Series X|S (0x20d6:0x200e) to report the
  1359. * guide button.
  1360. */
  1361. error = usb_set_interface(xpad->udev,
  1362. GIP_WIRED_INTF_AUDIO, 0);
  1363. if (error)
  1364. dev_warn(&xpad->dev->dev,
  1365. "unable to disable audio interface: %d\n",
  1366. error);
  1367. }
  1368. guard(spinlock_irqsave)(&xpad->odata_lock);
  1369. /*
  1370. * Begin the init sequence by attempting to send a packet.
  1371. * We will cycle through the init packet sequence before
  1372. * sending any packets from the output ring.
  1373. */
  1374. xpad->init_seq = 0;
  1375. return xpad_try_sending_next_out_packet(xpad);
  1376. }
  1377. static void xpadone_ack_mode_report(struct usb_xpad *xpad, u8 seq_num)
  1378. {
  1379. struct xpad_output_packet *packet =
  1380. &xpad->out_packets[XPAD_OUT_CMD_IDX];
  1381. static const u8 mode_report_ack[] = {
  1382. GIP_CMD_ACK, GIP_OPT_INTERNAL, GIP_SEQ0, GIP_PL_LEN(9),
  1383. 0x00, GIP_CMD_VIRTUAL_KEY, GIP_OPT_INTERNAL, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00
  1384. };
  1385. guard(spinlock_irqsave)(&xpad->odata_lock);
  1386. packet->len = sizeof(mode_report_ack);
  1387. memcpy(packet->data, mode_report_ack, packet->len);
  1388. packet->data[2] = seq_num;
  1389. packet->pending = true;
  1390. /* Reset the sequence so we send out the ack now */
  1391. xpad->last_out_packet = -1;
  1392. xpad_try_sending_next_out_packet(xpad);
  1393. }
  1394. #ifdef CONFIG_JOYSTICK_XPAD_FF
  1395. static int xpad_play_effect(struct input_dev *dev, void *data, struct ff_effect *effect)
  1396. {
  1397. struct usb_xpad *xpad = input_get_drvdata(dev);
  1398. struct xpad_output_packet *packet = &xpad->out_packets[XPAD_OUT_FF_IDX];
  1399. __u16 strong;
  1400. __u16 weak;
  1401. if (effect->type != FF_RUMBLE)
  1402. return 0;
  1403. strong = effect->u.rumble.strong_magnitude;
  1404. weak = effect->u.rumble.weak_magnitude;
  1405. guard(spinlock_irqsave)(&xpad->odata_lock);
  1406. switch (xpad->xtype) {
  1407. case XTYPE_XBOX:
  1408. packet->data[0] = 0x00;
  1409. packet->data[1] = 0x06;
  1410. packet->data[2] = 0x00;
  1411. packet->data[3] = strong / 256; /* left actuator */
  1412. packet->data[4] = 0x00;
  1413. packet->data[5] = weak / 256; /* right actuator */
  1414. packet->len = 6;
  1415. packet->pending = true;
  1416. break;
  1417. case XTYPE_XBOX360:
  1418. packet->data[0] = 0x00;
  1419. packet->data[1] = 0x08;
  1420. packet->data[2] = 0x00;
  1421. packet->data[3] = strong / 256; /* left actuator? */
  1422. packet->data[4] = weak / 256; /* right actuator? */
  1423. packet->data[5] = 0x00;
  1424. packet->data[6] = 0x00;
  1425. packet->data[7] = 0x00;
  1426. packet->len = 8;
  1427. packet->pending = true;
  1428. break;
  1429. case XTYPE_XBOX360W:
  1430. packet->data[0] = 0x00;
  1431. packet->data[1] = 0x01;
  1432. packet->data[2] = 0x0F;
  1433. packet->data[3] = 0xC0;
  1434. packet->data[4] = 0x00;
  1435. packet->data[5] = strong / 256;
  1436. packet->data[6] = weak / 256;
  1437. packet->data[7] = 0x00;
  1438. packet->data[8] = 0x00;
  1439. packet->data[9] = 0x00;
  1440. packet->data[10] = 0x00;
  1441. packet->data[11] = 0x00;
  1442. packet->len = 12;
  1443. packet->pending = true;
  1444. break;
  1445. case XTYPE_XBOXONE:
  1446. packet->data[0] = GIP_CMD_RUMBLE; /* activate rumble */
  1447. packet->data[1] = 0x00;
  1448. packet->data[2] = xpad->odata_serial++;
  1449. packet->data[3] = GIP_PL_LEN(9);
  1450. packet->data[4] = 0x00;
  1451. packet->data[5] = GIP_MOTOR_ALL;
  1452. packet->data[6] = 0x00; /* left trigger */
  1453. packet->data[7] = 0x00; /* right trigger */
  1454. packet->data[8] = strong / 512; /* left actuator */
  1455. packet->data[9] = weak / 512; /* right actuator */
  1456. packet->data[10] = 0xFF; /* on period */
  1457. packet->data[11] = 0x00; /* off period */
  1458. packet->data[12] = 0xFF; /* repeat count */
  1459. packet->len = 13;
  1460. packet->pending = true;
  1461. break;
  1462. default:
  1463. dev_dbg(&xpad->dev->dev,
  1464. "%s - rumble command sent to unsupported xpad type: %d\n",
  1465. __func__, xpad->xtype);
  1466. return -EINVAL;
  1467. }
  1468. return xpad_try_sending_next_out_packet(xpad);
  1469. }
  1470. static int xpad_init_ff(struct usb_xpad *xpad)
  1471. {
  1472. if (xpad->xtype == XTYPE_UNKNOWN)
  1473. return 0;
  1474. input_set_capability(xpad->dev, EV_FF, FF_RUMBLE);
  1475. return input_ff_create_memless(xpad->dev, NULL, xpad_play_effect);
  1476. }
  1477. #else
  1478. static int xpad_init_ff(struct usb_xpad *xpad) { return 0; }
  1479. #endif
  1480. #if defined(CONFIG_JOYSTICK_XPAD_LEDS)
  1481. #include <linux/leds.h>
  1482. #include <linux/idr.h>
  1483. static DEFINE_IDA(xpad_pad_seq);
  1484. struct xpad_led {
  1485. char name[16];
  1486. struct led_classdev led_cdev;
  1487. struct usb_xpad *xpad;
  1488. };
  1489. /*
  1490. * set the LEDs on Xbox 360 / Wireless Controllers
  1491. * @param command
  1492. * 0: off
  1493. * 1: all blink, then previous setting
  1494. * 2: 1/top-left blink, then on
  1495. * 3: 2/top-right blink, then on
  1496. * 4: 3/bottom-left blink, then on
  1497. * 5: 4/bottom-right blink, then on
  1498. * 6: 1/top-left on
  1499. * 7: 2/top-right on
  1500. * 8: 3/bottom-left on
  1501. * 9: 4/bottom-right on
  1502. * 10: rotate
  1503. * 11: blink, based on previous setting
  1504. * 12: slow blink, based on previous setting
  1505. * 13: rotate with two lights
  1506. * 14: persistent slow all blink
  1507. * 15: blink once, then previous setting
  1508. */
  1509. static void xpad_send_led_command(struct usb_xpad *xpad, int command)
  1510. {
  1511. struct xpad_output_packet *packet =
  1512. &xpad->out_packets[XPAD_OUT_LED_IDX];
  1513. command %= 16;
  1514. guard(spinlock_irqsave)(&xpad->odata_lock);
  1515. switch (xpad->xtype) {
  1516. case XTYPE_XBOX360:
  1517. packet->data[0] = 0x01;
  1518. packet->data[1] = 0x03;
  1519. packet->data[2] = command;
  1520. packet->len = 3;
  1521. packet->pending = true;
  1522. break;
  1523. case XTYPE_XBOX360W:
  1524. packet->data[0] = 0x00;
  1525. packet->data[1] = 0x00;
  1526. packet->data[2] = 0x08;
  1527. packet->data[3] = 0x40 + command;
  1528. packet->data[4] = 0x00;
  1529. packet->data[5] = 0x00;
  1530. packet->data[6] = 0x00;
  1531. packet->data[7] = 0x00;
  1532. packet->data[8] = 0x00;
  1533. packet->data[9] = 0x00;
  1534. packet->data[10] = 0x00;
  1535. packet->data[11] = 0x00;
  1536. packet->len = 12;
  1537. packet->pending = true;
  1538. break;
  1539. }
  1540. xpad_try_sending_next_out_packet(xpad);
  1541. }
  1542. /*
  1543. * Light up the segment corresponding to the pad number on
  1544. * Xbox 360 Controllers.
  1545. */
  1546. static void xpad_identify_controller(struct usb_xpad *xpad)
  1547. {
  1548. led_set_brightness(&xpad->led->led_cdev, (xpad->pad_nr % 4) + 2);
  1549. }
  1550. static void xpad_led_set(struct led_classdev *led_cdev,
  1551. enum led_brightness value)
  1552. {
  1553. struct xpad_led *xpad_led = container_of(led_cdev,
  1554. struct xpad_led, led_cdev);
  1555. xpad_send_led_command(xpad_led->xpad, value);
  1556. }
  1557. static int xpad_led_probe(struct usb_xpad *xpad)
  1558. {
  1559. struct xpad_led *led;
  1560. struct led_classdev *led_cdev;
  1561. int error;
  1562. if (xpad->xtype != XTYPE_XBOX360 && xpad->xtype != XTYPE_XBOX360W)
  1563. return 0;
  1564. xpad->led = led = kzalloc_obj(*led);
  1565. if (!led)
  1566. return -ENOMEM;
  1567. xpad->pad_nr = ida_alloc(&xpad_pad_seq, GFP_KERNEL);
  1568. if (xpad->pad_nr < 0) {
  1569. error = xpad->pad_nr;
  1570. goto err_free_mem;
  1571. }
  1572. snprintf(led->name, sizeof(led->name), "xpad%d", xpad->pad_nr);
  1573. led->xpad = xpad;
  1574. led_cdev = &led->led_cdev;
  1575. led_cdev->name = led->name;
  1576. led_cdev->brightness_set = xpad_led_set;
  1577. led_cdev->flags = LED_CORE_SUSPENDRESUME;
  1578. error = led_classdev_register(&xpad->udev->dev, led_cdev);
  1579. if (error)
  1580. goto err_free_id;
  1581. xpad_identify_controller(xpad);
  1582. return 0;
  1583. err_free_id:
  1584. ida_free(&xpad_pad_seq, xpad->pad_nr);
  1585. err_free_mem:
  1586. kfree(led);
  1587. xpad->led = NULL;
  1588. return error;
  1589. }
  1590. static void xpad_led_disconnect(struct usb_xpad *xpad)
  1591. {
  1592. struct xpad_led *xpad_led = xpad->led;
  1593. if (xpad_led) {
  1594. led_classdev_unregister(&xpad_led->led_cdev);
  1595. ida_free(&xpad_pad_seq, xpad->pad_nr);
  1596. kfree(xpad_led);
  1597. }
  1598. }
  1599. #else
  1600. static int xpad_led_probe(struct usb_xpad *xpad) { return 0; }
  1601. static void xpad_led_disconnect(struct usb_xpad *xpad) { }
  1602. #endif
  1603. static int xpad_start_input(struct usb_xpad *xpad)
  1604. {
  1605. int error;
  1606. if (usb_submit_urb(xpad->irq_in, GFP_KERNEL))
  1607. return -EIO;
  1608. if (xpad->xtype == XTYPE_XBOXONE) {
  1609. error = xpad_start_xbox_one(xpad);
  1610. if (error) {
  1611. usb_kill_urb(xpad->irq_in);
  1612. return error;
  1613. }
  1614. }
  1615. if (xpad->xtype == XTYPE_XBOX360) {
  1616. /*
  1617. * Some third-party controllers Xbox 360-style controllers
  1618. * require this message to finish initialization.
  1619. */
  1620. u8 dummy[20];
  1621. error = usb_control_msg_recv(xpad->udev, 0,
  1622. /* bRequest */ 0x01,
  1623. /* bmRequestType */
  1624. USB_TYPE_VENDOR | USB_DIR_IN |
  1625. USB_RECIP_INTERFACE,
  1626. /* wValue */ 0x100,
  1627. /* wIndex */ 0x00,
  1628. dummy, sizeof(dummy),
  1629. 25, GFP_KERNEL);
  1630. if (error)
  1631. dev_warn(&xpad->dev->dev,
  1632. "unable to receive magic message: %d\n",
  1633. error);
  1634. }
  1635. return 0;
  1636. }
  1637. static void xpad_stop_input(struct usb_xpad *xpad)
  1638. {
  1639. usb_kill_urb(xpad->irq_in);
  1640. }
  1641. static void xpad360w_poweroff_controller(struct usb_xpad *xpad)
  1642. {
  1643. struct xpad_output_packet *packet =
  1644. &xpad->out_packets[XPAD_OUT_CMD_IDX];
  1645. guard(spinlock_irqsave)(&xpad->odata_lock);
  1646. packet->data[0] = 0x00;
  1647. packet->data[1] = 0x00;
  1648. packet->data[2] = 0x08;
  1649. packet->data[3] = 0xC0;
  1650. packet->data[4] = 0x00;
  1651. packet->data[5] = 0x00;
  1652. packet->data[6] = 0x00;
  1653. packet->data[7] = 0x00;
  1654. packet->data[8] = 0x00;
  1655. packet->data[9] = 0x00;
  1656. packet->data[10] = 0x00;
  1657. packet->data[11] = 0x00;
  1658. packet->len = 12;
  1659. packet->pending = true;
  1660. /* Reset the sequence so we send out poweroff now */
  1661. xpad->last_out_packet = -1;
  1662. xpad_try_sending_next_out_packet(xpad);
  1663. }
  1664. static int xpad360w_start_input(struct usb_xpad *xpad)
  1665. {
  1666. int error;
  1667. error = usb_submit_urb(xpad->irq_in, GFP_KERNEL);
  1668. if (error)
  1669. return -EIO;
  1670. /*
  1671. * Send presence packet.
  1672. * This will force the controller to resend connection packets.
  1673. * This is useful in the case we activate the module after the
  1674. * adapter has been plugged in, as it won't automatically
  1675. * send us info about the controllers.
  1676. */
  1677. error = xpad_inquiry_pad_presence(xpad);
  1678. if (error) {
  1679. usb_kill_urb(xpad->irq_in);
  1680. return error;
  1681. }
  1682. return 0;
  1683. }
  1684. static void xpad360w_stop_input(struct usb_xpad *xpad)
  1685. {
  1686. usb_kill_urb(xpad->irq_in);
  1687. /* Make sure we are done with presence work if it was scheduled */
  1688. flush_work(&xpad->work);
  1689. }
  1690. static int xpad_open(struct input_dev *dev)
  1691. {
  1692. struct usb_xpad *xpad = input_get_drvdata(dev);
  1693. return xpad_start_input(xpad);
  1694. }
  1695. static void xpad_close(struct input_dev *dev)
  1696. {
  1697. struct usb_xpad *xpad = input_get_drvdata(dev);
  1698. xpad_stop_input(xpad);
  1699. }
  1700. static void xpad_set_up_abs(struct input_dev *input_dev, signed short abs)
  1701. {
  1702. struct usb_xpad *xpad = input_get_drvdata(input_dev);
  1703. switch (abs) {
  1704. case ABS_X:
  1705. case ABS_Y:
  1706. case ABS_RX:
  1707. case ABS_RY: /* the two sticks */
  1708. input_set_abs_params(input_dev, abs, -32768, 32767, 16, 128);
  1709. break;
  1710. case ABS_Z:
  1711. case ABS_RZ: /* the triggers (if mapped to axes) */
  1712. if (xpad->xtype == XTYPE_XBOXONE)
  1713. input_set_abs_params(input_dev, abs, 0, 1023, 0, 0);
  1714. else
  1715. input_set_abs_params(input_dev, abs, 0, 255, 0, 0);
  1716. break;
  1717. case ABS_HAT0X:
  1718. case ABS_HAT0Y: /* the d-pad (only if dpad is mapped to axes */
  1719. input_set_abs_params(input_dev, abs, -1, 1, 0, 0);
  1720. break;
  1721. case ABS_PROFILE: /* 4 value profile button (such as on XAC) */
  1722. input_set_abs_params(input_dev, abs, 0, 4, 0, 0);
  1723. break;
  1724. default:
  1725. input_set_abs_params(input_dev, abs, 0, 0, 0, 0);
  1726. break;
  1727. }
  1728. }
  1729. static void xpad_deinit_input(struct usb_xpad *xpad)
  1730. {
  1731. if (xpad->input_created) {
  1732. xpad->input_created = false;
  1733. xpad_led_disconnect(xpad);
  1734. input_unregister_device(xpad->dev);
  1735. }
  1736. }
  1737. static int xpad_init_input(struct usb_xpad *xpad)
  1738. {
  1739. struct input_dev *input_dev;
  1740. int i, error;
  1741. input_dev = input_allocate_device();
  1742. if (!input_dev)
  1743. return -ENOMEM;
  1744. xpad->dev = input_dev;
  1745. input_dev->name = xpad->name;
  1746. input_dev->phys = xpad->phys;
  1747. usb_to_input_id(xpad->udev, &input_dev->id);
  1748. if (xpad->xtype == XTYPE_XBOX360W) {
  1749. /* x360w controllers and the receiver have different ids */
  1750. input_dev->id.product = 0x02a1;
  1751. }
  1752. input_dev->dev.parent = &xpad->intf->dev;
  1753. input_set_drvdata(input_dev, xpad);
  1754. if (xpad->xtype != XTYPE_XBOX360W) {
  1755. input_dev->open = xpad_open;
  1756. input_dev->close = xpad_close;
  1757. }
  1758. if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
  1759. /* set up axes */
  1760. for (i = 0; xpad_abs[i] >= 0; i++)
  1761. xpad_set_up_abs(input_dev, xpad_abs[i]);
  1762. }
  1763. /* set up standard buttons */
  1764. for (i = 0; xpad_common_btn[i] >= 0; i++)
  1765. input_set_capability(input_dev, EV_KEY, xpad_common_btn[i]);
  1766. /* set up model-specific ones */
  1767. if (xpad->xtype == XTYPE_XBOX360 || xpad->xtype == XTYPE_XBOX360W ||
  1768. xpad->xtype == XTYPE_XBOXONE) {
  1769. for (i = 0; xpad360_btn[i] >= 0; i++)
  1770. input_set_capability(input_dev, EV_KEY, xpad360_btn[i]);
  1771. if (xpad->mapping & MAP_SHARE_BUTTON)
  1772. input_set_capability(input_dev, EV_KEY, KEY_RECORD);
  1773. } else {
  1774. for (i = 0; xpad_btn[i] >= 0; i++)
  1775. input_set_capability(input_dev, EV_KEY, xpad_btn[i]);
  1776. }
  1777. if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
  1778. for (i = 0; xpad_btn_pad[i] >= 0; i++)
  1779. input_set_capability(input_dev, EV_KEY,
  1780. xpad_btn_pad[i]);
  1781. }
  1782. /* set up paddles if the controller has them */
  1783. if (xpad->mapping & MAP_PADDLES) {
  1784. for (i = 0; xpad_btn_paddles[i] >= 0; i++)
  1785. input_set_capability(input_dev, EV_KEY, xpad_btn_paddles[i]);
  1786. }
  1787. /*
  1788. * This should be a simple else block. However historically
  1789. * xbox360w has mapped DPAD to buttons while xbox360 did not. This
  1790. * made no sense, but now we can not just switch back and have to
  1791. * support both behaviors.
  1792. */
  1793. if (!(xpad->mapping & MAP_DPAD_TO_BUTTONS) ||
  1794. xpad->xtype == XTYPE_XBOX360W) {
  1795. for (i = 0; xpad_abs_pad[i] >= 0; i++)
  1796. xpad_set_up_abs(input_dev, xpad_abs_pad[i]);
  1797. }
  1798. if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
  1799. for (i = 0; xpad_btn_triggers[i] >= 0; i++)
  1800. input_set_capability(input_dev, EV_KEY,
  1801. xpad_btn_triggers[i]);
  1802. } else {
  1803. for (i = 0; xpad_abs_triggers[i] >= 0; i++)
  1804. xpad_set_up_abs(input_dev, xpad_abs_triggers[i]);
  1805. }
  1806. /* setup profile button as an axis with 4 possible values */
  1807. if (xpad->mapping & MAP_PROFILE_BUTTON)
  1808. xpad_set_up_abs(input_dev, ABS_PROFILE);
  1809. error = xpad_init_ff(xpad);
  1810. if (error)
  1811. goto err_free_input;
  1812. error = xpad_led_probe(xpad);
  1813. if (error)
  1814. goto err_destroy_ff;
  1815. error = input_register_device(xpad->dev);
  1816. if (error)
  1817. goto err_disconnect_led;
  1818. xpad->input_created = true;
  1819. return 0;
  1820. err_disconnect_led:
  1821. xpad_led_disconnect(xpad);
  1822. err_destroy_ff:
  1823. input_ff_destroy(input_dev);
  1824. err_free_input:
  1825. input_free_device(input_dev);
  1826. return error;
  1827. }
  1828. static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1829. {
  1830. struct usb_device *udev = interface_to_usbdev(intf);
  1831. struct usb_xpad *xpad;
  1832. struct usb_endpoint_descriptor *ep_irq_in, *ep_irq_out;
  1833. int i, error;
  1834. if (intf->cur_altsetting->desc.bNumEndpoints != 2)
  1835. return -ENODEV;
  1836. for (i = 0; xpad_device[i].idVendor; i++) {
  1837. if ((le16_to_cpu(udev->descriptor.idVendor) == xpad_device[i].idVendor) &&
  1838. (le16_to_cpu(udev->descriptor.idProduct) == xpad_device[i].idProduct))
  1839. break;
  1840. }
  1841. xpad = kzalloc_obj(*xpad);
  1842. if (!xpad)
  1843. return -ENOMEM;
  1844. usb_make_path(udev, xpad->phys, sizeof(xpad->phys));
  1845. strlcat(xpad->phys, "/input0", sizeof(xpad->phys));
  1846. xpad->idata = usb_alloc_coherent(udev, XPAD_PKT_LEN,
  1847. GFP_KERNEL, &xpad->idata_dma);
  1848. if (!xpad->idata) {
  1849. error = -ENOMEM;
  1850. goto err_free_mem;
  1851. }
  1852. xpad->irq_in = usb_alloc_urb(0, GFP_KERNEL);
  1853. if (!xpad->irq_in) {
  1854. error = -ENOMEM;
  1855. goto err_free_idata;
  1856. }
  1857. xpad->udev = udev;
  1858. xpad->intf = intf;
  1859. xpad->mapping = xpad_device[i].mapping;
  1860. xpad->xtype = xpad_device[i].xtype;
  1861. xpad->name = xpad_device[i].name;
  1862. if (xpad_device[i].flags & FLAG_DELAY_INIT)
  1863. xpad->delay_init = true;
  1864. xpad->packet_type = PKT_XB;
  1865. INIT_WORK(&xpad->work, xpad_presence_work);
  1866. if (xpad->xtype == XTYPE_UNKNOWN) {
  1867. if (intf->cur_altsetting->desc.bInterfaceClass == USB_CLASS_VENDOR_SPEC) {
  1868. if (intf->cur_altsetting->desc.bInterfaceProtocol == 129)
  1869. xpad->xtype = XTYPE_XBOX360W;
  1870. else if (intf->cur_altsetting->desc.bInterfaceProtocol == 208)
  1871. xpad->xtype = XTYPE_XBOXONE;
  1872. else
  1873. xpad->xtype = XTYPE_XBOX360;
  1874. } else {
  1875. xpad->xtype = XTYPE_XBOX;
  1876. }
  1877. if (dpad_to_buttons)
  1878. xpad->mapping |= MAP_DPAD_TO_BUTTONS;
  1879. if (triggers_to_buttons)
  1880. xpad->mapping |= MAP_TRIGGERS_TO_BUTTONS;
  1881. if (sticks_to_null)
  1882. xpad->mapping |= MAP_STICKS_TO_NULL;
  1883. }
  1884. if (xpad->xtype == XTYPE_XBOXONE &&
  1885. intf->cur_altsetting->desc.bInterfaceNumber != GIP_WIRED_INTF_DATA) {
  1886. /*
  1887. * The Xbox One controller lists three interfaces all with the
  1888. * same interface class, subclass and protocol. Differentiate by
  1889. * interface number.
  1890. */
  1891. error = -ENODEV;
  1892. goto err_free_in_urb;
  1893. }
  1894. ep_irq_in = ep_irq_out = NULL;
  1895. for (i = 0; i < 2; i++) {
  1896. struct usb_endpoint_descriptor *ep =
  1897. &intf->cur_altsetting->endpoint[i].desc;
  1898. if (usb_endpoint_xfer_int(ep)) {
  1899. if (usb_endpoint_dir_in(ep))
  1900. ep_irq_in = ep;
  1901. else
  1902. ep_irq_out = ep;
  1903. }
  1904. }
  1905. if (!ep_irq_in || !ep_irq_out) {
  1906. error = -ENODEV;
  1907. goto err_free_in_urb;
  1908. }
  1909. error = xpad_init_output(intf, xpad, ep_irq_out);
  1910. if (error)
  1911. goto err_free_in_urb;
  1912. usb_fill_int_urb(xpad->irq_in, udev,
  1913. usb_rcvintpipe(udev, ep_irq_in->bEndpointAddress),
  1914. xpad->idata, XPAD_PKT_LEN, xpad_irq_in,
  1915. xpad, ep_irq_in->bInterval);
  1916. xpad->irq_in->transfer_dma = xpad->idata_dma;
  1917. xpad->irq_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1918. usb_set_intfdata(intf, xpad);
  1919. /* Packet type detection */
  1920. if (le16_to_cpu(udev->descriptor.idVendor) == 0x045e) { /* Microsoft controllers */
  1921. if (le16_to_cpu(udev->descriptor.idProduct) == 0x02e3) {
  1922. /* The original elite controller always uses the oldest
  1923. * type of extended packet
  1924. */
  1925. xpad->packet_type = PKT_XBE1;
  1926. } else if (le16_to_cpu(udev->descriptor.idProduct) == 0x0b00) {
  1927. /* The elite 2 controller has seen multiple packet
  1928. * revisions. These are tied to specific firmware
  1929. * versions
  1930. */
  1931. if (le16_to_cpu(udev->descriptor.bcdDevice) < 0x0500) {
  1932. /* This is the format that the Elite 2 used
  1933. * prior to the BLE update
  1934. */
  1935. xpad->packet_type = PKT_XBE2_FW_OLD;
  1936. } else if (le16_to_cpu(udev->descriptor.bcdDevice) <
  1937. 0x050b) {
  1938. /* This is the format that the Elite 2 used
  1939. * prior to the update that split the packet
  1940. */
  1941. xpad->packet_type = PKT_XBE2_FW_5_EARLY;
  1942. } else {
  1943. /* The split packet format that was introduced
  1944. * in firmware v5.11
  1945. */
  1946. xpad->packet_type = PKT_XBE2_FW_5_11;
  1947. }
  1948. }
  1949. }
  1950. if (xpad->xtype == XTYPE_XBOX360W) {
  1951. /*
  1952. * Submit the int URB immediately rather than waiting for open
  1953. * because we get status messages from the device whether
  1954. * or not any controllers are attached. In fact, it's
  1955. * exactly the message that a controller has arrived that
  1956. * we're waiting for.
  1957. */
  1958. error = xpad360w_start_input(xpad);
  1959. if (error)
  1960. goto err_deinit_output;
  1961. /*
  1962. * Wireless controllers require RESET_RESUME to work properly
  1963. * after suspend. Ideally this quirk should be in usb core
  1964. * quirk list, but we have too many vendors producing these
  1965. * controllers and we'd need to maintain 2 identical lists
  1966. * here in this driver and in usb core.
  1967. */
  1968. udev->quirks |= USB_QUIRK_RESET_RESUME;
  1969. } else {
  1970. error = xpad_init_input(xpad);
  1971. if (error)
  1972. goto err_deinit_output;
  1973. }
  1974. return 0;
  1975. err_deinit_output:
  1976. xpad_deinit_output(xpad);
  1977. err_free_in_urb:
  1978. usb_free_urb(xpad->irq_in);
  1979. err_free_idata:
  1980. usb_free_coherent(udev, XPAD_PKT_LEN, xpad->idata, xpad->idata_dma);
  1981. err_free_mem:
  1982. kfree(xpad);
  1983. return error;
  1984. }
  1985. static void xpad_disconnect(struct usb_interface *intf)
  1986. {
  1987. struct usb_xpad *xpad = usb_get_intfdata(intf);
  1988. if (xpad->xtype == XTYPE_XBOX360W)
  1989. xpad360w_stop_input(xpad);
  1990. xpad_deinit_input(xpad);
  1991. /*
  1992. * Now that both input device and LED device are gone we can
  1993. * stop output URB.
  1994. */
  1995. xpad_stop_output(xpad);
  1996. xpad_deinit_output(xpad);
  1997. usb_free_urb(xpad->irq_in);
  1998. usb_free_coherent(xpad->udev, XPAD_PKT_LEN,
  1999. xpad->idata, xpad->idata_dma);
  2000. kfree(xpad);
  2001. usb_set_intfdata(intf, NULL);
  2002. }
  2003. static int xpad_suspend(struct usb_interface *intf, pm_message_t message)
  2004. {
  2005. struct usb_xpad *xpad = usb_get_intfdata(intf);
  2006. struct input_dev *input = xpad->dev;
  2007. if (xpad->xtype == XTYPE_XBOX360W) {
  2008. /*
  2009. * Wireless controllers always listen to input so
  2010. * they are notified when controller shows up
  2011. * or goes away.
  2012. */
  2013. xpad360w_stop_input(xpad);
  2014. /*
  2015. * The wireless adapter is going off now, so the
  2016. * gamepads are going to become disconnected.
  2017. * Unless explicitly disabled, power them down
  2018. * so they don't just sit there flashing.
  2019. */
  2020. if (auto_poweroff && xpad->pad_present)
  2021. xpad360w_poweroff_controller(xpad);
  2022. } else {
  2023. guard(mutex)(&input->mutex);
  2024. if (input_device_enabled(input))
  2025. xpad_stop_input(xpad);
  2026. }
  2027. xpad_stop_output(xpad);
  2028. return 0;
  2029. }
  2030. static int xpad_resume(struct usb_interface *intf)
  2031. {
  2032. struct usb_xpad *xpad = usb_get_intfdata(intf);
  2033. struct input_dev *input = xpad->dev;
  2034. xpad->delayed_init_done = false;
  2035. if (xpad->xtype == XTYPE_XBOX360W)
  2036. return xpad360w_start_input(xpad);
  2037. guard(mutex)(&input->mutex);
  2038. if (input_device_enabled(input))
  2039. return xpad_start_input(xpad);
  2040. if (xpad->xtype == XTYPE_XBOXONE) {
  2041. /*
  2042. * Even if there are no users, we'll send Xbox One pads
  2043. * the startup sequence so they don't sit there and
  2044. * blink until somebody opens the input device again.
  2045. */
  2046. return xpad_start_xbox_one(xpad);
  2047. }
  2048. return 0;
  2049. }
  2050. static struct usb_driver xpad_driver = {
  2051. .name = "xpad",
  2052. .probe = xpad_probe,
  2053. .disconnect = xpad_disconnect,
  2054. .suspend = xpad_suspend,
  2055. .resume = xpad_resume,
  2056. .id_table = xpad_table,
  2057. };
  2058. module_usb_driver(xpad_driver);
  2059. MODULE_AUTHOR("Marko Friedemann <mfr@bmx-chemnitz.de>");
  2060. MODULE_DESCRIPTION("Xbox pad driver");
  2061. MODULE_LICENSE("GPL");