xpad.c 62 KB

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