xpad.c 79 KB

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