xpad.c 64 KB

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