xpad.c 63 KB

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