xpad.c 62 KB

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