xpad.c 73 KB

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