xpad.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  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. /* xbox d-pads should map to buttons, as is required for DDR pads
  89. but we map them to axes when possible to simplify things */
  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. { 0x045e, 0x0202, "Microsoft X-Box pad v1 (US)", 0, XTYPE_XBOX },
  120. { 0x045e, 0x0285, "Microsoft X-Box pad (Japan)", 0, XTYPE_XBOX },
  121. { 0x045e, 0x0287, "Microsoft Xbox Controller S", 0, XTYPE_XBOX },
  122. { 0x045e, 0x0289, "Microsoft X-Box pad v2 (US)", 0, XTYPE_XBOX },
  123. { 0x045e, 0x028e, "Microsoft X-Box 360 pad", 0, XTYPE_XBOX360 },
  124. { 0x045e, 0x02d1, "Microsoft X-Box One pad", 0, XTYPE_XBOXONE },
  125. { 0x045e, 0x02dd, "Microsoft X-Box One pad (Firmware 2015)", 0, XTYPE_XBOXONE },
  126. { 0x045e, 0x02e3, "Microsoft X-Box One Elite pad", 0, XTYPE_XBOXONE },
  127. { 0x045e, 0x02ea, "Microsoft X-Box One S pad", 0, XTYPE_XBOXONE },
  128. { 0x045e, 0x0291, "Xbox 360 Wireless Receiver (XBOX)", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360W },
  129. { 0x045e, 0x0719, "Xbox 360 Wireless Receiver", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360W },
  130. { 0x044f, 0x0f07, "Thrustmaster, Inc. Controller", 0, XTYPE_XBOX },
  131. { 0x044f, 0xb326, "Thrustmaster Gamepad GP XID", 0, XTYPE_XBOX360 },
  132. { 0x046d, 0xc21d, "Logitech Gamepad F310", 0, XTYPE_XBOX360 },
  133. { 0x046d, 0xc21e, "Logitech Gamepad F510", 0, XTYPE_XBOX360 },
  134. { 0x046d, 0xc21f, "Logitech Gamepad F710", 0, XTYPE_XBOX360 },
  135. { 0x046d, 0xc242, "Logitech Chillstream Controller", 0, XTYPE_XBOX360 },
  136. { 0x046d, 0xca84, "Logitech Xbox Cordless Controller", 0, XTYPE_XBOX },
  137. { 0x046d, 0xca88, "Logitech Compact Controller for Xbox", 0, XTYPE_XBOX },
  138. { 0x05fd, 0x1007, "Mad Catz Controller (unverified)", 0, XTYPE_XBOX },
  139. { 0x05fd, 0x107a, "InterAct 'PowerPad Pro' X-Box pad (Germany)", 0, XTYPE_XBOX },
  140. { 0x0738, 0x4516, "Mad Catz Control Pad", 0, XTYPE_XBOX },
  141. { 0x0738, 0x4522, "Mad Catz LumiCON", 0, XTYPE_XBOX },
  142. { 0x0738, 0x4526, "Mad Catz Control Pad Pro", 0, XTYPE_XBOX },
  143. { 0x0738, 0x4536, "Mad Catz MicroCON", 0, XTYPE_XBOX },
  144. { 0x0738, 0x4540, "Mad Catz Beat Pad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
  145. { 0x0738, 0x4556, "Mad Catz Lynx Wireless Controller", 0, XTYPE_XBOX },
  146. { 0x0738, 0x4716, "Mad Catz Wired Xbox 360 Controller", 0, XTYPE_XBOX360 },
  147. { 0x0738, 0x4718, "Mad Catz Street Fighter IV FightStick SE", 0, XTYPE_XBOX360 },
  148. { 0x0738, 0x4726, "Mad Catz Xbox 360 Controller", 0, XTYPE_XBOX360 },
  149. { 0x0738, 0x4728, "Mad Catz Street Fighter IV FightPad", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  150. { 0x0738, 0x4738, "Mad Catz Wired Xbox 360 Controller (SFIV)", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  151. { 0x0738, 0x4740, "Mad Catz Beat Pad", 0, XTYPE_XBOX360 },
  152. { 0x0738, 0x4a01, "Mad Catz FightStick TE 2", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOXONE },
  153. { 0x0738, 0x6040, "Mad Catz Beat Pad Pro", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
  154. { 0x0738, 0xb726, "Mad Catz Xbox controller - MW2", 0, XTYPE_XBOX360 },
  155. { 0x0738, 0xbeef, "Mad Catz JOYTECH NEO SE Advanced GamePad", XTYPE_XBOX360 },
  156. { 0x0738, 0xcb02, "Saitek Cyborg Rumble Pad - PC/Xbox 360", 0, XTYPE_XBOX360 },
  157. { 0x0738, 0xcb03, "Saitek P3200 Rumble Pad - PC/Xbox 360", 0, XTYPE_XBOX360 },
  158. { 0x0738, 0xf738, "Super SFIV FightStick TE S", 0, XTYPE_XBOX360 },
  159. { 0x0c12, 0x8802, "Zeroplus Xbox Controller", 0, XTYPE_XBOX },
  160. { 0x0c12, 0x8809, "RedOctane Xbox Dance Pad", DANCEPAD_MAP_CONFIG, XTYPE_XBOX },
  161. { 0x0c12, 0x880a, "Pelican Eclipse PL-2023", 0, XTYPE_XBOX },
  162. { 0x0c12, 0x8810, "Zeroplus Xbox Controller", 0, XTYPE_XBOX },
  163. { 0x0c12, 0x9902, "HAMA VibraX - *FAULTY HARDWARE*", 0, XTYPE_XBOX },
  164. { 0x0d2f, 0x0002, "Andamiro Pump It Up pad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
  165. { 0x0e4c, 0x1097, "Radica Gamester Controller", 0, XTYPE_XBOX },
  166. { 0x0e4c, 0x2390, "Radica Games Jtech Controller", 0, XTYPE_XBOX },
  167. { 0x0e6f, 0x0003, "Logic3 Freebird wireless Controller", 0, XTYPE_XBOX },
  168. { 0x0e6f, 0x0005, "Eclipse wireless Controller", 0, XTYPE_XBOX },
  169. { 0x0e6f, 0x0006, "Edge wireless Controller", 0, XTYPE_XBOX },
  170. { 0x0e6f, 0x0105, "HSM3 Xbox360 dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
  171. { 0x0e6f, 0x0113, "Afterglow AX.1 Gamepad for Xbox 360", 0, XTYPE_XBOX360 },
  172. { 0x0e6f, 0x0139, "Afterglow Prismatic Wired Controller", 0, XTYPE_XBOXONE },
  173. { 0x0e6f, 0x0201, "Pelican PL-3601 'TSZ' Wired Xbox 360 Controller", 0, XTYPE_XBOX360 },
  174. { 0x0e6f, 0x0213, "Afterglow Gamepad for Xbox 360", 0, XTYPE_XBOX360 },
  175. { 0x0e6f, 0x021f, "Rock Candy Gamepad for Xbox 360", 0, XTYPE_XBOX360 },
  176. { 0x0e6f, 0x0146, "Rock Candy Wired Controller for Xbox One", 0, XTYPE_XBOXONE },
  177. { 0x0e6f, 0x0301, "Logic3 Controller", 0, XTYPE_XBOX360 },
  178. { 0x0e6f, 0x0401, "Logic3 Controller", 0, XTYPE_XBOX360 },
  179. { 0x0e8f, 0x0201, "SmartJoy Frag Xpad/PS2 adaptor", 0, XTYPE_XBOX },
  180. { 0x0e8f, 0x3008, "Generic xbox control (dealextreme)", 0, XTYPE_XBOX },
  181. { 0x0f0d, 0x000a, "Hori Co. DOA4 FightStick", 0, XTYPE_XBOX360 },
  182. { 0x0f0d, 0x000d, "Hori Fighting Stick EX2", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  183. { 0x0f0d, 0x0016, "Hori Real Arcade Pro.EX", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  184. { 0x0f0d, 0x0067, "HORIPAD ONE", 0, XTYPE_XBOXONE },
  185. { 0x0f30, 0x0202, "Joytech Advanced Controller", 0, XTYPE_XBOX },
  186. { 0x0f30, 0x8888, "BigBen XBMiniPad Controller", 0, XTYPE_XBOX },
  187. { 0x102c, 0xff0c, "Joytech Wireless Advanced Controller", 0, XTYPE_XBOX },
  188. { 0x12ab, 0x0004, "Honey Bee Xbox360 dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
  189. { 0x12ab, 0x0301, "PDP AFTERGLOW AX.1", 0, XTYPE_XBOX360 },
  190. { 0x12ab, 0x8809, "Xbox DDR dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
  191. { 0x1430, 0x4748, "RedOctane Guitar Hero X-plorer", 0, XTYPE_XBOX360 },
  192. { 0x1430, 0x8888, "TX6500+ Dance Pad (first generation)", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
  193. { 0x146b, 0x0601, "BigBen Interactive XBOX 360 Controller", 0, XTYPE_XBOX360 },
  194. { 0x1532, 0x0037, "Razer Sabertooth", 0, XTYPE_XBOX360 },
  195. { 0x15e4, 0x3f00, "Power A Mini Pro Elite", 0, XTYPE_XBOX360 },
  196. { 0x15e4, 0x3f0a, "Xbox Airflo wired controller", 0, XTYPE_XBOX360 },
  197. { 0x15e4, 0x3f10, "Batarang Xbox 360 controller", 0, XTYPE_XBOX360 },
  198. { 0x162e, 0xbeef, "Joytech Neo-Se Take2", 0, XTYPE_XBOX360 },
  199. { 0x1689, 0xfd00, "Razer Onza Tournament Edition", 0, XTYPE_XBOX360 },
  200. { 0x1689, 0xfd01, "Razer Onza Classic Edition", 0, XTYPE_XBOX360 },
  201. { 0x24c6, 0x542a, "Xbox ONE spectra", 0, XTYPE_XBOXONE },
  202. { 0x24c6, 0x5d04, "Razer Sabertooth", 0, XTYPE_XBOX360 },
  203. { 0x1bad, 0x0002, "Harmonix Rock Band Guitar", 0, XTYPE_XBOX360 },
  204. { 0x1bad, 0x0003, "Harmonix Rock Band Drumkit", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
  205. { 0x1bad, 0xf016, "Mad Catz Xbox 360 Controller", 0, XTYPE_XBOX360 },
  206. { 0x1bad, 0xf023, "MLG Pro Circuit Controller (Xbox)", 0, XTYPE_XBOX360 },
  207. { 0x1bad, 0xf028, "Street Fighter IV FightPad", 0, XTYPE_XBOX360 },
  208. { 0x1bad, 0xf038, "Street Fighter IV FightStick TE", 0, XTYPE_XBOX360 },
  209. { 0x1bad, 0xf900, "Harmonix Xbox 360 Controller", 0, XTYPE_XBOX360 },
  210. { 0x1bad, 0xf901, "Gamestop Xbox 360 Controller", 0, XTYPE_XBOX360 },
  211. { 0x1bad, 0xf903, "Tron Xbox 360 controller", 0, XTYPE_XBOX360 },
  212. { 0x24c6, 0x5000, "Razer Atrox Arcade Stick", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  213. { 0x24c6, 0x5300, "PowerA MINI PROEX Controller", 0, XTYPE_XBOX360 },
  214. { 0x24c6, 0x5303, "Xbox Airflo wired controller", 0, XTYPE_XBOX360 },
  215. { 0x24c6, 0x541a, "PowerA Xbox One Mini Wired Controller", 0, XTYPE_XBOXONE },
  216. { 0x24c6, 0x543a, "PowerA Xbox One wired controller", 0, XTYPE_XBOXONE },
  217. { 0x24c6, 0x5500, "Hori XBOX 360 EX 2 with Turbo", 0, XTYPE_XBOX360 },
  218. { 0x24c6, 0x5501, "Hori Real Arcade Pro VX-SA", 0, XTYPE_XBOX360 },
  219. { 0x24c6, 0x5506, "Hori SOULCALIBUR V Stick", 0, XTYPE_XBOX360 },
  220. { 0x24c6, 0x5b02, "Thrustmaster, Inc. GPX Controller", 0, XTYPE_XBOX360 },
  221. { 0x24c6, 0x5b03, "Thrustmaster Ferrari 458 Racing Wheel", 0, XTYPE_XBOX360 },
  222. { 0xffff, 0xffff, "Chinese-made Xbox Controller", 0, XTYPE_XBOX },
  223. { 0x0000, 0x0000, "Generic X-Box pad", 0, XTYPE_UNKNOWN }
  224. };
  225. /* buttons shared with xbox and xbox360 */
  226. static const signed short xpad_common_btn[] = {
  227. BTN_A, BTN_B, BTN_X, BTN_Y, /* "analog" buttons */
  228. BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR, /* start/back/sticks */
  229. -1 /* terminating entry */
  230. };
  231. /* original xbox controllers only */
  232. static const signed short xpad_btn[] = {
  233. BTN_C, BTN_Z, /* "analog" buttons */
  234. -1 /* terminating entry */
  235. };
  236. /* used when dpad is mapped to buttons */
  237. static const signed short xpad_btn_pad[] = {
  238. BTN_TRIGGER_HAPPY1, BTN_TRIGGER_HAPPY2, /* d-pad left, right */
  239. BTN_TRIGGER_HAPPY3, BTN_TRIGGER_HAPPY4, /* d-pad up, down */
  240. -1 /* terminating entry */
  241. };
  242. /* used when triggers are mapped to buttons */
  243. static const signed short xpad_btn_triggers[] = {
  244. BTN_TL2, BTN_TR2, /* triggers left/right */
  245. -1
  246. };
  247. static const signed short xpad360_btn[] = { /* buttons for x360 controller */
  248. BTN_TL, BTN_TR, /* Button LB/RB */
  249. BTN_MODE, /* The big X button */
  250. -1
  251. };
  252. static const signed short xpad_abs[] = {
  253. ABS_X, ABS_Y, /* left stick */
  254. ABS_RX, ABS_RY, /* right stick */
  255. -1 /* terminating entry */
  256. };
  257. /* used when dpad is mapped to axes */
  258. static const signed short xpad_abs_pad[] = {
  259. ABS_HAT0X, ABS_HAT0Y, /* d-pad axes */
  260. -1 /* terminating entry */
  261. };
  262. /* used when triggers are mapped to axes */
  263. static const signed short xpad_abs_triggers[] = {
  264. ABS_Z, ABS_RZ, /* triggers left/right */
  265. -1
  266. };
  267. /*
  268. * Xbox 360 has a vendor-specific class, so we cannot match it with only
  269. * USB_INTERFACE_INFO (also specifically refused by USB subsystem), so we
  270. * match against vendor id as well. Wired Xbox 360 devices have protocol 1,
  271. * wireless controllers have protocol 129.
  272. */
  273. #define XPAD_XBOX360_VENDOR_PROTOCOL(vend,pr) \
  274. .match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_INT_INFO, \
  275. .idVendor = (vend), \
  276. .bInterfaceClass = USB_CLASS_VENDOR_SPEC, \
  277. .bInterfaceSubClass = 93, \
  278. .bInterfaceProtocol = (pr)
  279. #define XPAD_XBOX360_VENDOR(vend) \
  280. { XPAD_XBOX360_VENDOR_PROTOCOL(vend,1) }, \
  281. { XPAD_XBOX360_VENDOR_PROTOCOL(vend,129) }
  282. /* The Xbox One controller uses subclass 71 and protocol 208. */
  283. #define XPAD_XBOXONE_VENDOR_PROTOCOL(vend, pr) \
  284. .match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_INT_INFO, \
  285. .idVendor = (vend), \
  286. .bInterfaceClass = USB_CLASS_VENDOR_SPEC, \
  287. .bInterfaceSubClass = 71, \
  288. .bInterfaceProtocol = (pr)
  289. #define XPAD_XBOXONE_VENDOR(vend) \
  290. { XPAD_XBOXONE_VENDOR_PROTOCOL(vend, 208) }
  291. static struct usb_device_id xpad_table[] = {
  292. { USB_INTERFACE_INFO('X', 'B', 0) }, /* X-Box USB-IF not approved class */
  293. XPAD_XBOX360_VENDOR(0x044f), /* Thrustmaster X-Box 360 controllers */
  294. XPAD_XBOX360_VENDOR(0x045e), /* Microsoft X-Box 360 controllers */
  295. XPAD_XBOXONE_VENDOR(0x045e), /* Microsoft X-Box One controllers */
  296. XPAD_XBOX360_VENDOR(0x046d), /* Logitech X-Box 360 style controllers */
  297. XPAD_XBOX360_VENDOR(0x0738), /* Mad Catz X-Box 360 controllers */
  298. { USB_DEVICE(0x0738, 0x4540) }, /* Mad Catz Beat Pad */
  299. XPAD_XBOXONE_VENDOR(0x0738), /* Mad Catz FightStick TE 2 */
  300. XPAD_XBOX360_VENDOR(0x0e6f), /* 0x0e6f X-Box 360 controllers */
  301. XPAD_XBOXONE_VENDOR(0x0e6f), /* 0x0e6f X-Box One controllers */
  302. XPAD_XBOX360_VENDOR(0x12ab), /* X-Box 360 dance pads */
  303. XPAD_XBOX360_VENDOR(0x1430), /* RedOctane X-Box 360 controllers */
  304. XPAD_XBOX360_VENDOR(0x146b), /* BigBen Interactive Controllers */
  305. XPAD_XBOX360_VENDOR(0x1bad), /* Harminix Rock Band Guitar and Drums */
  306. XPAD_XBOX360_VENDOR(0x0f0d), /* Hori Controllers */
  307. XPAD_XBOXONE_VENDOR(0x0f0d), /* Hori Controllers */
  308. XPAD_XBOX360_VENDOR(0x1689), /* Razer Onza */
  309. XPAD_XBOX360_VENDOR(0x24c6), /* PowerA Controllers */
  310. XPAD_XBOXONE_VENDOR(0x24c6), /* PowerA Controllers */
  311. XPAD_XBOX360_VENDOR(0x1532), /* Razer Sabertooth */
  312. XPAD_XBOX360_VENDOR(0x15e4), /* Numark X-Box 360 controllers */
  313. XPAD_XBOX360_VENDOR(0x162e), /* Joytech X-Box 360 controllers */
  314. { }
  315. };
  316. MODULE_DEVICE_TABLE(usb, xpad_table);
  317. struct xpad_output_packet {
  318. u8 data[XPAD_PKT_LEN];
  319. u8 len;
  320. bool pending;
  321. };
  322. #define XPAD_OUT_CMD_IDX 0
  323. #define XPAD_OUT_FF_IDX 1
  324. #define XPAD_OUT_LED_IDX (1 + IS_ENABLED(CONFIG_JOYSTICK_XPAD_FF))
  325. #define XPAD_NUM_OUT_PACKETS (1 + \
  326. IS_ENABLED(CONFIG_JOYSTICK_XPAD_FF) + \
  327. IS_ENABLED(CONFIG_JOYSTICK_XPAD_LEDS))
  328. struct usb_xpad {
  329. struct input_dev *dev; /* input device interface */
  330. struct input_dev __rcu *x360w_dev;
  331. struct usb_device *udev; /* usb device */
  332. struct usb_interface *intf; /* usb interface */
  333. bool pad_present;
  334. bool input_created;
  335. struct urb *irq_in; /* urb for interrupt in report */
  336. unsigned char *idata; /* input data */
  337. dma_addr_t idata_dma;
  338. struct urb *irq_out; /* urb for interrupt out report */
  339. struct usb_anchor irq_out_anchor;
  340. bool irq_out_active; /* we must not use an active URB */
  341. u8 odata_serial; /* serial number for xbox one protocol */
  342. unsigned char *odata; /* output data */
  343. dma_addr_t odata_dma;
  344. spinlock_t odata_lock;
  345. struct xpad_output_packet out_packets[XPAD_NUM_OUT_PACKETS];
  346. int last_out_packet;
  347. #if defined(CONFIG_JOYSTICK_XPAD_LEDS)
  348. struct xpad_led *led;
  349. #endif
  350. char phys[64]; /* physical device path */
  351. int mapping; /* map d-pad to buttons or to axes */
  352. int xtype; /* type of xbox device */
  353. int pad_nr; /* the order x360 pads were attached */
  354. const char *name; /* name of the device */
  355. struct work_struct work; /* init/remove device from callback */
  356. };
  357. static int xpad_init_input(struct usb_xpad *xpad);
  358. static void xpad_deinit_input(struct usb_xpad *xpad);
  359. /*
  360. * xpad_process_packet
  361. *
  362. * Completes a request by converting the data into events for the
  363. * input subsystem.
  364. *
  365. * The used report descriptor was taken from ITO Takayukis website:
  366. * http://euc.jp/periphs/xbox-controller.ja.html
  367. */
  368. static void xpad_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data)
  369. {
  370. struct input_dev *dev = xpad->dev;
  371. if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
  372. /* left stick */
  373. input_report_abs(dev, ABS_X,
  374. (__s16) le16_to_cpup((__le16 *)(data + 12)));
  375. input_report_abs(dev, ABS_Y,
  376. ~(__s16) le16_to_cpup((__le16 *)(data + 14)));
  377. /* right stick */
  378. input_report_abs(dev, ABS_RX,
  379. (__s16) le16_to_cpup((__le16 *)(data + 16)));
  380. input_report_abs(dev, ABS_RY,
  381. ~(__s16) le16_to_cpup((__le16 *)(data + 18)));
  382. }
  383. /* triggers left/right */
  384. if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
  385. input_report_key(dev, BTN_TL2, data[10]);
  386. input_report_key(dev, BTN_TR2, data[11]);
  387. } else {
  388. input_report_abs(dev, ABS_Z, data[10]);
  389. input_report_abs(dev, ABS_RZ, data[11]);
  390. }
  391. /* digital pad */
  392. if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
  393. /* dpad as buttons (left, right, up, down) */
  394. input_report_key(dev, BTN_TRIGGER_HAPPY1, data[2] & 0x04);
  395. input_report_key(dev, BTN_TRIGGER_HAPPY2, data[2] & 0x08);
  396. input_report_key(dev, BTN_TRIGGER_HAPPY3, data[2] & 0x01);
  397. input_report_key(dev, BTN_TRIGGER_HAPPY4, data[2] & 0x02);
  398. } else {
  399. input_report_abs(dev, ABS_HAT0X,
  400. !!(data[2] & 0x08) - !!(data[2] & 0x04));
  401. input_report_abs(dev, ABS_HAT0Y,
  402. !!(data[2] & 0x02) - !!(data[2] & 0x01));
  403. }
  404. /* start/back buttons and stick press left/right */
  405. input_report_key(dev, BTN_START, data[2] & 0x10);
  406. input_report_key(dev, BTN_SELECT, data[2] & 0x20);
  407. input_report_key(dev, BTN_THUMBL, data[2] & 0x40);
  408. input_report_key(dev, BTN_THUMBR, data[2] & 0x80);
  409. /* "analog" buttons A, B, X, Y */
  410. input_report_key(dev, BTN_A, data[4]);
  411. input_report_key(dev, BTN_B, data[5]);
  412. input_report_key(dev, BTN_X, data[6]);
  413. input_report_key(dev, BTN_Y, data[7]);
  414. /* "analog" buttons black, white */
  415. input_report_key(dev, BTN_C, data[8]);
  416. input_report_key(dev, BTN_Z, data[9]);
  417. input_sync(dev);
  418. }
  419. /*
  420. * xpad360_process_packet
  421. *
  422. * Completes a request by converting the data into events for the
  423. * input subsystem. It is version for xbox 360 controller
  424. *
  425. * The used report descriptor was taken from:
  426. * http://www.free60.org/wiki/Gamepad
  427. */
  428. static void xpad360_process_packet(struct usb_xpad *xpad, struct input_dev *dev,
  429. u16 cmd, unsigned char *data)
  430. {
  431. /* valid pad data */
  432. if (data[0] != 0x00)
  433. return;
  434. /* digital pad */
  435. if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
  436. /* dpad as buttons (left, right, up, down) */
  437. input_report_key(dev, BTN_TRIGGER_HAPPY1, data[2] & 0x04);
  438. input_report_key(dev, BTN_TRIGGER_HAPPY2, data[2] & 0x08);
  439. input_report_key(dev, BTN_TRIGGER_HAPPY3, data[2] & 0x01);
  440. input_report_key(dev, BTN_TRIGGER_HAPPY4, data[2] & 0x02);
  441. }
  442. /*
  443. * This should be a simple else block. However historically
  444. * xbox360w has mapped DPAD to buttons while xbox360 did not. This
  445. * made no sense, but now we can not just switch back and have to
  446. * support both behaviors.
  447. */
  448. if (!(xpad->mapping & MAP_DPAD_TO_BUTTONS) ||
  449. xpad->xtype == XTYPE_XBOX360W) {
  450. input_report_abs(dev, ABS_HAT0X,
  451. !!(data[2] & 0x08) - !!(data[2] & 0x04));
  452. input_report_abs(dev, ABS_HAT0Y,
  453. !!(data[2] & 0x02) - !!(data[2] & 0x01));
  454. }
  455. /* start/back buttons */
  456. input_report_key(dev, BTN_START, data[2] & 0x10);
  457. input_report_key(dev, BTN_SELECT, data[2] & 0x20);
  458. /* stick press left/right */
  459. input_report_key(dev, BTN_THUMBL, data[2] & 0x40);
  460. input_report_key(dev, BTN_THUMBR, data[2] & 0x80);
  461. /* buttons A,B,X,Y,TL,TR and MODE */
  462. input_report_key(dev, BTN_A, data[3] & 0x10);
  463. input_report_key(dev, BTN_B, data[3] & 0x20);
  464. input_report_key(dev, BTN_X, data[3] & 0x40);
  465. input_report_key(dev, BTN_Y, data[3] & 0x80);
  466. input_report_key(dev, BTN_TL, data[3] & 0x01);
  467. input_report_key(dev, BTN_TR, data[3] & 0x02);
  468. input_report_key(dev, BTN_MODE, data[3] & 0x04);
  469. if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
  470. /* left stick */
  471. input_report_abs(dev, ABS_X,
  472. (__s16) le16_to_cpup((__le16 *)(data + 6)));
  473. input_report_abs(dev, ABS_Y,
  474. ~(__s16) le16_to_cpup((__le16 *)(data + 8)));
  475. /* right stick */
  476. input_report_abs(dev, ABS_RX,
  477. (__s16) le16_to_cpup((__le16 *)(data + 10)));
  478. input_report_abs(dev, ABS_RY,
  479. ~(__s16) le16_to_cpup((__le16 *)(data + 12)));
  480. }
  481. /* triggers left/right */
  482. if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
  483. input_report_key(dev, BTN_TL2, data[4]);
  484. input_report_key(dev, BTN_TR2, data[5]);
  485. } else {
  486. input_report_abs(dev, ABS_Z, data[4]);
  487. input_report_abs(dev, ABS_RZ, data[5]);
  488. }
  489. input_sync(dev);
  490. }
  491. static void xpad_presence_work(struct work_struct *work)
  492. {
  493. struct usb_xpad *xpad = container_of(work, struct usb_xpad, work);
  494. int error;
  495. if (xpad->pad_present) {
  496. error = xpad_init_input(xpad);
  497. if (error) {
  498. /* complain only, not much else we can do here */
  499. dev_err(&xpad->dev->dev,
  500. "unable to init device: %d\n", error);
  501. } else {
  502. rcu_assign_pointer(xpad->x360w_dev, xpad->dev);
  503. }
  504. } else {
  505. RCU_INIT_POINTER(xpad->x360w_dev, NULL);
  506. synchronize_rcu();
  507. /*
  508. * Now that we are sure xpad360w_process_packet is not
  509. * using input device we can get rid of it.
  510. */
  511. xpad_deinit_input(xpad);
  512. }
  513. }
  514. /*
  515. * xpad360w_process_packet
  516. *
  517. * Completes a request by converting the data into events for the
  518. * input subsystem. It is version for xbox 360 wireless controller.
  519. *
  520. * Byte.Bit
  521. * 00.1 - Status change: The controller or headset has connected/disconnected
  522. * Bits 01.7 and 01.6 are valid
  523. * 01.7 - Controller present
  524. * 01.6 - Headset present
  525. * 01.1 - Pad state (Bytes 4+) valid
  526. *
  527. */
  528. static void xpad360w_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data)
  529. {
  530. struct input_dev *dev;
  531. bool present;
  532. /* Presence change */
  533. if (data[0] & 0x08) {
  534. present = (data[1] & 0x80) != 0;
  535. if (xpad->pad_present != present) {
  536. xpad->pad_present = present;
  537. schedule_work(&xpad->work);
  538. }
  539. }
  540. /* Valid pad data */
  541. if (data[1] != 0x1)
  542. return;
  543. rcu_read_lock();
  544. dev = rcu_dereference(xpad->x360w_dev);
  545. if (dev)
  546. xpad360_process_packet(xpad, dev, cmd, &data[4]);
  547. rcu_read_unlock();
  548. }
  549. /*
  550. * xpadone_process_buttons
  551. *
  552. * Process a button update packet from an Xbox one controller.
  553. */
  554. static void xpadone_process_buttons(struct usb_xpad *xpad,
  555. struct input_dev *dev,
  556. unsigned char *data)
  557. {
  558. /* menu/view buttons */
  559. input_report_key(dev, BTN_START, data[4] & 0x04);
  560. input_report_key(dev, BTN_SELECT, data[4] & 0x08);
  561. /* buttons A,B,X,Y */
  562. input_report_key(dev, BTN_A, data[4] & 0x10);
  563. input_report_key(dev, BTN_B, data[4] & 0x20);
  564. input_report_key(dev, BTN_X, data[4] & 0x40);
  565. input_report_key(dev, BTN_Y, data[4] & 0x80);
  566. /* digital pad */
  567. if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
  568. /* dpad as buttons (left, right, up, down) */
  569. input_report_key(dev, BTN_TRIGGER_HAPPY1, data[5] & 0x04);
  570. input_report_key(dev, BTN_TRIGGER_HAPPY2, data[5] & 0x08);
  571. input_report_key(dev, BTN_TRIGGER_HAPPY3, data[5] & 0x01);
  572. input_report_key(dev, BTN_TRIGGER_HAPPY4, data[5] & 0x02);
  573. } else {
  574. input_report_abs(dev, ABS_HAT0X,
  575. !!(data[5] & 0x08) - !!(data[5] & 0x04));
  576. input_report_abs(dev, ABS_HAT0Y,
  577. !!(data[5] & 0x02) - !!(data[5] & 0x01));
  578. }
  579. /* TL/TR */
  580. input_report_key(dev, BTN_TL, data[5] & 0x10);
  581. input_report_key(dev, BTN_TR, data[5] & 0x20);
  582. /* stick press left/right */
  583. input_report_key(dev, BTN_THUMBL, data[5] & 0x40);
  584. input_report_key(dev, BTN_THUMBR, data[5] & 0x80);
  585. if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
  586. /* left stick */
  587. input_report_abs(dev, ABS_X,
  588. (__s16) le16_to_cpup((__le16 *)(data + 10)));
  589. input_report_abs(dev, ABS_Y,
  590. ~(__s16) le16_to_cpup((__le16 *)(data + 12)));
  591. /* right stick */
  592. input_report_abs(dev, ABS_RX,
  593. (__s16) le16_to_cpup((__le16 *)(data + 14)));
  594. input_report_abs(dev, ABS_RY,
  595. ~(__s16) le16_to_cpup((__le16 *)(data + 16)));
  596. }
  597. /* triggers left/right */
  598. if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
  599. input_report_key(dev, BTN_TL2,
  600. (__u16) le16_to_cpup((__le16 *)(data + 6)));
  601. input_report_key(dev, BTN_TR2,
  602. (__u16) le16_to_cpup((__le16 *)(data + 8)));
  603. } else {
  604. input_report_abs(dev, ABS_Z,
  605. (__u16) le16_to_cpup((__le16 *)(data + 6)));
  606. input_report_abs(dev, ABS_RZ,
  607. (__u16) le16_to_cpup((__le16 *)(data + 8)));
  608. }
  609. input_sync(dev);
  610. }
  611. /*
  612. * xpadone_process_packet
  613. *
  614. * Completes a request by converting the data into events for the
  615. * input subsystem. This version is for the Xbox One controller.
  616. *
  617. * The report format was gleaned from
  618. * https://github.com/kylelemons/xbox/blob/master/xbox.go
  619. */
  620. static void xpadone_process_packet(struct usb_xpad *xpad,
  621. u16 cmd, unsigned char *data)
  622. {
  623. struct input_dev *dev = xpad->dev;
  624. switch (data[0]) {
  625. case 0x20:
  626. xpadone_process_buttons(xpad, dev, data);
  627. break;
  628. case 0x07:
  629. /* the xbox button has its own special report */
  630. input_report_key(dev, BTN_MODE, data[4] & 0x01);
  631. input_sync(dev);
  632. break;
  633. }
  634. }
  635. static void xpad_irq_in(struct urb *urb)
  636. {
  637. struct usb_xpad *xpad = urb->context;
  638. struct device *dev = &xpad->intf->dev;
  639. int retval, status;
  640. status = urb->status;
  641. switch (status) {
  642. case 0:
  643. /* success */
  644. break;
  645. case -ECONNRESET:
  646. case -ENOENT:
  647. case -ESHUTDOWN:
  648. /* this urb is terminated, clean up */
  649. dev_dbg(dev, "%s - urb shutting down with status: %d\n",
  650. __func__, status);
  651. return;
  652. default:
  653. dev_dbg(dev, "%s - nonzero urb status received: %d\n",
  654. __func__, status);
  655. goto exit;
  656. }
  657. switch (xpad->xtype) {
  658. case XTYPE_XBOX360:
  659. xpad360_process_packet(xpad, xpad->dev, 0, xpad->idata);
  660. break;
  661. case XTYPE_XBOX360W:
  662. xpad360w_process_packet(xpad, 0, xpad->idata);
  663. break;
  664. case XTYPE_XBOXONE:
  665. xpadone_process_packet(xpad, 0, xpad->idata);
  666. break;
  667. default:
  668. xpad_process_packet(xpad, 0, xpad->idata);
  669. }
  670. exit:
  671. retval = usb_submit_urb(urb, GFP_ATOMIC);
  672. if (retval)
  673. dev_err(dev, "%s - usb_submit_urb failed with result %d\n",
  674. __func__, retval);
  675. }
  676. /* Callers must hold xpad->odata_lock spinlock */
  677. static bool xpad_prepare_next_out_packet(struct usb_xpad *xpad)
  678. {
  679. struct xpad_output_packet *pkt, *packet = NULL;
  680. int i;
  681. for (i = 0; i < XPAD_NUM_OUT_PACKETS; i++) {
  682. if (++xpad->last_out_packet >= XPAD_NUM_OUT_PACKETS)
  683. xpad->last_out_packet = 0;
  684. pkt = &xpad->out_packets[xpad->last_out_packet];
  685. if (pkt->pending) {
  686. dev_dbg(&xpad->intf->dev,
  687. "%s - found pending output packet %d\n",
  688. __func__, xpad->last_out_packet);
  689. packet = pkt;
  690. break;
  691. }
  692. }
  693. if (packet) {
  694. memcpy(xpad->odata, packet->data, packet->len);
  695. xpad->irq_out->transfer_buffer_length = packet->len;
  696. packet->pending = false;
  697. return true;
  698. }
  699. return false;
  700. }
  701. /* Callers must hold xpad->odata_lock spinlock */
  702. static int xpad_try_sending_next_out_packet(struct usb_xpad *xpad)
  703. {
  704. int error;
  705. if (!xpad->irq_out_active && xpad_prepare_next_out_packet(xpad)) {
  706. usb_anchor_urb(xpad->irq_out, &xpad->irq_out_anchor);
  707. error = usb_submit_urb(xpad->irq_out, GFP_ATOMIC);
  708. if (error) {
  709. dev_err(&xpad->intf->dev,
  710. "%s - usb_submit_urb failed with result %d\n",
  711. __func__, error);
  712. usb_unanchor_urb(xpad->irq_out);
  713. return -EIO;
  714. }
  715. xpad->irq_out_active = true;
  716. }
  717. return 0;
  718. }
  719. static void xpad_irq_out(struct urb *urb)
  720. {
  721. struct usb_xpad *xpad = urb->context;
  722. struct device *dev = &xpad->intf->dev;
  723. int status = urb->status;
  724. int error;
  725. unsigned long flags;
  726. spin_lock_irqsave(&xpad->odata_lock, flags);
  727. switch (status) {
  728. case 0:
  729. /* success */
  730. xpad->irq_out_active = xpad_prepare_next_out_packet(xpad);
  731. break;
  732. case -ECONNRESET:
  733. case -ENOENT:
  734. case -ESHUTDOWN:
  735. /* this urb is terminated, clean up */
  736. dev_dbg(dev, "%s - urb shutting down with status: %d\n",
  737. __func__, status);
  738. xpad->irq_out_active = false;
  739. break;
  740. default:
  741. dev_dbg(dev, "%s - nonzero urb status received: %d\n",
  742. __func__, status);
  743. break;
  744. }
  745. if (xpad->irq_out_active) {
  746. usb_anchor_urb(urb, &xpad->irq_out_anchor);
  747. error = usb_submit_urb(urb, GFP_ATOMIC);
  748. if (error) {
  749. dev_err(dev,
  750. "%s - usb_submit_urb failed with result %d\n",
  751. __func__, error);
  752. usb_unanchor_urb(urb);
  753. xpad->irq_out_active = false;
  754. }
  755. }
  756. spin_unlock_irqrestore(&xpad->odata_lock, flags);
  757. }
  758. static int xpad_init_output(struct usb_interface *intf, struct usb_xpad *xpad)
  759. {
  760. struct usb_endpoint_descriptor *ep_irq_out;
  761. int ep_irq_out_idx;
  762. int error;
  763. if (xpad->xtype == XTYPE_UNKNOWN)
  764. return 0;
  765. init_usb_anchor(&xpad->irq_out_anchor);
  766. xpad->odata = usb_alloc_coherent(xpad->udev, XPAD_PKT_LEN,
  767. GFP_KERNEL, &xpad->odata_dma);
  768. if (!xpad->odata) {
  769. error = -ENOMEM;
  770. goto fail1;
  771. }
  772. spin_lock_init(&xpad->odata_lock);
  773. xpad->irq_out = usb_alloc_urb(0, GFP_KERNEL);
  774. if (!xpad->irq_out) {
  775. error = -ENOMEM;
  776. goto fail2;
  777. }
  778. /* Xbox One controller has in/out endpoints swapped. */
  779. ep_irq_out_idx = xpad->xtype == XTYPE_XBOXONE ? 0 : 1;
  780. ep_irq_out = &intf->cur_altsetting->endpoint[ep_irq_out_idx].desc;
  781. usb_fill_int_urb(xpad->irq_out, xpad->udev,
  782. usb_sndintpipe(xpad->udev, ep_irq_out->bEndpointAddress),
  783. xpad->odata, XPAD_PKT_LEN,
  784. xpad_irq_out, xpad, ep_irq_out->bInterval);
  785. xpad->irq_out->transfer_dma = xpad->odata_dma;
  786. xpad->irq_out->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  787. return 0;
  788. fail2: usb_free_coherent(xpad->udev, XPAD_PKT_LEN, xpad->odata, xpad->odata_dma);
  789. fail1: return error;
  790. }
  791. static void xpad_stop_output(struct usb_xpad *xpad)
  792. {
  793. if (xpad->xtype != XTYPE_UNKNOWN) {
  794. if (!usb_wait_anchor_empty_timeout(&xpad->irq_out_anchor,
  795. 5000)) {
  796. dev_warn(&xpad->intf->dev,
  797. "timed out waiting for output URB to complete, killing\n");
  798. usb_kill_anchored_urbs(&xpad->irq_out_anchor);
  799. }
  800. }
  801. }
  802. static void xpad_deinit_output(struct usb_xpad *xpad)
  803. {
  804. if (xpad->xtype != XTYPE_UNKNOWN) {
  805. usb_free_urb(xpad->irq_out);
  806. usb_free_coherent(xpad->udev, XPAD_PKT_LEN,
  807. xpad->odata, xpad->odata_dma);
  808. }
  809. }
  810. static int xpad_inquiry_pad_presence(struct usb_xpad *xpad)
  811. {
  812. struct xpad_output_packet *packet =
  813. &xpad->out_packets[XPAD_OUT_CMD_IDX];
  814. unsigned long flags;
  815. int retval;
  816. spin_lock_irqsave(&xpad->odata_lock, flags);
  817. packet->data[0] = 0x08;
  818. packet->data[1] = 0x00;
  819. packet->data[2] = 0x0F;
  820. packet->data[3] = 0xC0;
  821. packet->data[4] = 0x00;
  822. packet->data[5] = 0x00;
  823. packet->data[6] = 0x00;
  824. packet->data[7] = 0x00;
  825. packet->data[8] = 0x00;
  826. packet->data[9] = 0x00;
  827. packet->data[10] = 0x00;
  828. packet->data[11] = 0x00;
  829. packet->len = 12;
  830. packet->pending = true;
  831. /* Reset the sequence so we send out presence first */
  832. xpad->last_out_packet = -1;
  833. retval = xpad_try_sending_next_out_packet(xpad);
  834. spin_unlock_irqrestore(&xpad->odata_lock, flags);
  835. return retval;
  836. }
  837. static int xpad_start_xbox_one(struct usb_xpad *xpad)
  838. {
  839. struct xpad_output_packet *packet =
  840. &xpad->out_packets[XPAD_OUT_CMD_IDX];
  841. unsigned long flags;
  842. int retval;
  843. spin_lock_irqsave(&xpad->odata_lock, flags);
  844. /* Xbox one controller needs to be initialized. */
  845. packet->data[0] = 0x05;
  846. packet->data[1] = 0x20;
  847. packet->data[2] = xpad->odata_serial++; /* packet serial */
  848. packet->data[3] = 0x01; /* rumble bit enable? */
  849. packet->data[4] = 0x00;
  850. packet->len = 5;
  851. packet->pending = true;
  852. /* Reset the sequence so we send out start packet first */
  853. xpad->last_out_packet = -1;
  854. retval = xpad_try_sending_next_out_packet(xpad);
  855. spin_unlock_irqrestore(&xpad->odata_lock, flags);
  856. return retval;
  857. }
  858. #ifdef CONFIG_JOYSTICK_XPAD_FF
  859. static int xpad_play_effect(struct input_dev *dev, void *data, struct ff_effect *effect)
  860. {
  861. struct usb_xpad *xpad = input_get_drvdata(dev);
  862. struct xpad_output_packet *packet = &xpad->out_packets[XPAD_OUT_FF_IDX];
  863. __u16 strong;
  864. __u16 weak;
  865. int retval;
  866. unsigned long flags;
  867. if (effect->type != FF_RUMBLE)
  868. return 0;
  869. strong = effect->u.rumble.strong_magnitude;
  870. weak = effect->u.rumble.weak_magnitude;
  871. spin_lock_irqsave(&xpad->odata_lock, flags);
  872. switch (xpad->xtype) {
  873. case XTYPE_XBOX:
  874. packet->data[0] = 0x00;
  875. packet->data[1] = 0x06;
  876. packet->data[2] = 0x00;
  877. packet->data[3] = strong / 256; /* left actuator */
  878. packet->data[4] = 0x00;
  879. packet->data[5] = weak / 256; /* right actuator */
  880. packet->len = 6;
  881. packet->pending = true;
  882. break;
  883. case XTYPE_XBOX360:
  884. packet->data[0] = 0x00;
  885. packet->data[1] = 0x08;
  886. packet->data[2] = 0x00;
  887. packet->data[3] = strong / 256; /* left actuator? */
  888. packet->data[4] = weak / 256; /* right actuator? */
  889. packet->data[5] = 0x00;
  890. packet->data[6] = 0x00;
  891. packet->data[7] = 0x00;
  892. packet->len = 8;
  893. packet->pending = true;
  894. break;
  895. case XTYPE_XBOX360W:
  896. packet->data[0] = 0x00;
  897. packet->data[1] = 0x01;
  898. packet->data[2] = 0x0F;
  899. packet->data[3] = 0xC0;
  900. packet->data[4] = 0x00;
  901. packet->data[5] = strong / 256;
  902. packet->data[6] = weak / 256;
  903. packet->data[7] = 0x00;
  904. packet->data[8] = 0x00;
  905. packet->data[9] = 0x00;
  906. packet->data[10] = 0x00;
  907. packet->data[11] = 0x00;
  908. packet->len = 12;
  909. packet->pending = true;
  910. break;
  911. case XTYPE_XBOXONE:
  912. packet->data[0] = 0x09; /* activate rumble */
  913. packet->data[1] = 0x00;
  914. packet->data[2] = xpad->odata_serial++;
  915. packet->data[3] = 0x09;
  916. packet->data[4] = 0x00;
  917. packet->data[5] = 0x0F;
  918. packet->data[6] = 0x00;
  919. packet->data[7] = 0x00;
  920. packet->data[8] = strong / 512; /* left actuator */
  921. packet->data[9] = weak / 512; /* right actuator */
  922. packet->data[10] = 0xFF; /* on period */
  923. packet->data[11] = 0x00; /* off period */
  924. packet->data[12] = 0xFF; /* repeat count */
  925. packet->len = 13;
  926. packet->pending = true;
  927. break;
  928. default:
  929. dev_dbg(&xpad->dev->dev,
  930. "%s - rumble command sent to unsupported xpad type: %d\n",
  931. __func__, xpad->xtype);
  932. retval = -EINVAL;
  933. goto out;
  934. }
  935. retval = xpad_try_sending_next_out_packet(xpad);
  936. out:
  937. spin_unlock_irqrestore(&xpad->odata_lock, flags);
  938. return retval;
  939. }
  940. static int xpad_init_ff(struct usb_xpad *xpad)
  941. {
  942. if (xpad->xtype == XTYPE_UNKNOWN)
  943. return 0;
  944. input_set_capability(xpad->dev, EV_FF, FF_RUMBLE);
  945. return input_ff_create_memless(xpad->dev, NULL, xpad_play_effect);
  946. }
  947. #else
  948. static int xpad_init_ff(struct usb_xpad *xpad) { return 0; }
  949. #endif
  950. #if defined(CONFIG_JOYSTICK_XPAD_LEDS)
  951. #include <linux/leds.h>
  952. #include <linux/idr.h>
  953. static DEFINE_IDA(xpad_pad_seq);
  954. struct xpad_led {
  955. char name[16];
  956. struct led_classdev led_cdev;
  957. struct usb_xpad *xpad;
  958. };
  959. /**
  960. * set the LEDs on Xbox360 / Wireless Controllers
  961. * @param command
  962. * 0: off
  963. * 1: all blink, then previous setting
  964. * 2: 1/top-left blink, then on
  965. * 3: 2/top-right blink, then on
  966. * 4: 3/bottom-left blink, then on
  967. * 5: 4/bottom-right blink, then on
  968. * 6: 1/top-left on
  969. * 7: 2/top-right on
  970. * 8: 3/bottom-left on
  971. * 9: 4/bottom-right on
  972. * 10: rotate
  973. * 11: blink, based on previous setting
  974. * 12: slow blink, based on previous setting
  975. * 13: rotate with two lights
  976. * 14: persistent slow all blink
  977. * 15: blink once, then previous setting
  978. */
  979. static void xpad_send_led_command(struct usb_xpad *xpad, int command)
  980. {
  981. struct xpad_output_packet *packet =
  982. &xpad->out_packets[XPAD_OUT_LED_IDX];
  983. unsigned long flags;
  984. command %= 16;
  985. spin_lock_irqsave(&xpad->odata_lock, flags);
  986. switch (xpad->xtype) {
  987. case XTYPE_XBOX360:
  988. packet->data[0] = 0x01;
  989. packet->data[1] = 0x03;
  990. packet->data[2] = command;
  991. packet->len = 3;
  992. packet->pending = true;
  993. break;
  994. case XTYPE_XBOX360W:
  995. packet->data[0] = 0x00;
  996. packet->data[1] = 0x00;
  997. packet->data[2] = 0x08;
  998. packet->data[3] = 0x40 + command;
  999. packet->data[4] = 0x00;
  1000. packet->data[5] = 0x00;
  1001. packet->data[6] = 0x00;
  1002. packet->data[7] = 0x00;
  1003. packet->data[8] = 0x00;
  1004. packet->data[9] = 0x00;
  1005. packet->data[10] = 0x00;
  1006. packet->data[11] = 0x00;
  1007. packet->len = 12;
  1008. packet->pending = true;
  1009. break;
  1010. }
  1011. xpad_try_sending_next_out_packet(xpad);
  1012. spin_unlock_irqrestore(&xpad->odata_lock, flags);
  1013. }
  1014. /*
  1015. * Light up the segment corresponding to the pad number on
  1016. * Xbox 360 Controllers.
  1017. */
  1018. static void xpad_identify_controller(struct usb_xpad *xpad)
  1019. {
  1020. led_set_brightness(&xpad->led->led_cdev, (xpad->pad_nr % 4) + 2);
  1021. }
  1022. static void xpad_led_set(struct led_classdev *led_cdev,
  1023. enum led_brightness value)
  1024. {
  1025. struct xpad_led *xpad_led = container_of(led_cdev,
  1026. struct xpad_led, led_cdev);
  1027. xpad_send_led_command(xpad_led->xpad, value);
  1028. }
  1029. static int xpad_led_probe(struct usb_xpad *xpad)
  1030. {
  1031. struct xpad_led *led;
  1032. struct led_classdev *led_cdev;
  1033. int error;
  1034. if (xpad->xtype != XTYPE_XBOX360 && xpad->xtype != XTYPE_XBOX360W)
  1035. return 0;
  1036. xpad->led = led = kzalloc(sizeof(struct xpad_led), GFP_KERNEL);
  1037. if (!led)
  1038. return -ENOMEM;
  1039. xpad->pad_nr = ida_simple_get(&xpad_pad_seq, 0, 0, GFP_KERNEL);
  1040. if (xpad->pad_nr < 0) {
  1041. error = xpad->pad_nr;
  1042. goto err_free_mem;
  1043. }
  1044. snprintf(led->name, sizeof(led->name), "xpad%d", xpad->pad_nr);
  1045. led->xpad = xpad;
  1046. led_cdev = &led->led_cdev;
  1047. led_cdev->name = led->name;
  1048. led_cdev->brightness_set = xpad_led_set;
  1049. error = led_classdev_register(&xpad->udev->dev, led_cdev);
  1050. if (error)
  1051. goto err_free_id;
  1052. xpad_identify_controller(xpad);
  1053. return 0;
  1054. err_free_id:
  1055. ida_simple_remove(&xpad_pad_seq, xpad->pad_nr);
  1056. err_free_mem:
  1057. kfree(led);
  1058. xpad->led = NULL;
  1059. return error;
  1060. }
  1061. static void xpad_led_disconnect(struct usb_xpad *xpad)
  1062. {
  1063. struct xpad_led *xpad_led = xpad->led;
  1064. if (xpad_led) {
  1065. led_classdev_unregister(&xpad_led->led_cdev);
  1066. ida_simple_remove(&xpad_pad_seq, xpad->pad_nr);
  1067. kfree(xpad_led);
  1068. }
  1069. }
  1070. #else
  1071. static int xpad_led_probe(struct usb_xpad *xpad) { return 0; }
  1072. static void xpad_led_disconnect(struct usb_xpad *xpad) { }
  1073. #endif
  1074. static int xpad_start_input(struct usb_xpad *xpad)
  1075. {
  1076. int error;
  1077. if (usb_submit_urb(xpad->irq_in, GFP_KERNEL))
  1078. return -EIO;
  1079. if (xpad->xtype == XTYPE_XBOXONE) {
  1080. error = xpad_start_xbox_one(xpad);
  1081. if (error) {
  1082. usb_kill_urb(xpad->irq_in);
  1083. return error;
  1084. }
  1085. }
  1086. return 0;
  1087. }
  1088. static void xpad_stop_input(struct usb_xpad *xpad)
  1089. {
  1090. usb_kill_urb(xpad->irq_in);
  1091. }
  1092. static void xpad360w_poweroff_controller(struct usb_xpad *xpad)
  1093. {
  1094. unsigned long flags;
  1095. struct xpad_output_packet *packet =
  1096. &xpad->out_packets[XPAD_OUT_CMD_IDX];
  1097. spin_lock_irqsave(&xpad->odata_lock, flags);
  1098. packet->data[0] = 0x00;
  1099. packet->data[1] = 0x00;
  1100. packet->data[2] = 0x08;
  1101. packet->data[3] = 0xC0;
  1102. packet->data[4] = 0x00;
  1103. packet->data[5] = 0x00;
  1104. packet->data[6] = 0x00;
  1105. packet->data[7] = 0x00;
  1106. packet->data[8] = 0x00;
  1107. packet->data[9] = 0x00;
  1108. packet->data[10] = 0x00;
  1109. packet->data[11] = 0x00;
  1110. packet->len = 12;
  1111. packet->pending = true;
  1112. /* Reset the sequence so we send out poweroff now */
  1113. xpad->last_out_packet = -1;
  1114. xpad_try_sending_next_out_packet(xpad);
  1115. spin_unlock_irqrestore(&xpad->odata_lock, flags);
  1116. }
  1117. static int xpad360w_start_input(struct usb_xpad *xpad)
  1118. {
  1119. int error;
  1120. error = usb_submit_urb(xpad->irq_in, GFP_KERNEL);
  1121. if (error)
  1122. return -EIO;
  1123. /*
  1124. * Send presence packet.
  1125. * This will force the controller to resend connection packets.
  1126. * This is useful in the case we activate the module after the
  1127. * adapter has been plugged in, as it won't automatically
  1128. * send us info about the controllers.
  1129. */
  1130. error = xpad_inquiry_pad_presence(xpad);
  1131. if (error) {
  1132. usb_kill_urb(xpad->irq_in);
  1133. return error;
  1134. }
  1135. return 0;
  1136. }
  1137. static void xpad360w_stop_input(struct usb_xpad *xpad)
  1138. {
  1139. usb_kill_urb(xpad->irq_in);
  1140. /* Make sure we are done with presence work if it was scheduled */
  1141. flush_work(&xpad->work);
  1142. }
  1143. static int xpad_open(struct input_dev *dev)
  1144. {
  1145. struct usb_xpad *xpad = input_get_drvdata(dev);
  1146. return xpad_start_input(xpad);
  1147. }
  1148. static void xpad_close(struct input_dev *dev)
  1149. {
  1150. struct usb_xpad *xpad = input_get_drvdata(dev);
  1151. xpad_stop_input(xpad);
  1152. }
  1153. static void xpad_set_up_abs(struct input_dev *input_dev, signed short abs)
  1154. {
  1155. struct usb_xpad *xpad = input_get_drvdata(input_dev);
  1156. set_bit(abs, input_dev->absbit);
  1157. switch (abs) {
  1158. case ABS_X:
  1159. case ABS_Y:
  1160. case ABS_RX:
  1161. case ABS_RY: /* the two sticks */
  1162. input_set_abs_params(input_dev, abs, -32768, 32767, 16, 128);
  1163. break;
  1164. case ABS_Z:
  1165. case ABS_RZ: /* the triggers (if mapped to axes) */
  1166. if (xpad->xtype == XTYPE_XBOXONE)
  1167. input_set_abs_params(input_dev, abs, 0, 1023, 0, 0);
  1168. else
  1169. input_set_abs_params(input_dev, abs, 0, 255, 0, 0);
  1170. break;
  1171. case ABS_HAT0X:
  1172. case ABS_HAT0Y: /* the d-pad (only if dpad is mapped to axes */
  1173. input_set_abs_params(input_dev, abs, -1, 1, 0, 0);
  1174. break;
  1175. }
  1176. }
  1177. static void xpad_deinit_input(struct usb_xpad *xpad)
  1178. {
  1179. if (xpad->input_created) {
  1180. xpad->input_created = false;
  1181. xpad_led_disconnect(xpad);
  1182. input_unregister_device(xpad->dev);
  1183. }
  1184. }
  1185. static int xpad_init_input(struct usb_xpad *xpad)
  1186. {
  1187. struct input_dev *input_dev;
  1188. int i, error;
  1189. input_dev = input_allocate_device();
  1190. if (!input_dev)
  1191. return -ENOMEM;
  1192. xpad->dev = input_dev;
  1193. input_dev->name = xpad->name;
  1194. input_dev->phys = xpad->phys;
  1195. usb_to_input_id(xpad->udev, &input_dev->id);
  1196. if (xpad->xtype == XTYPE_XBOX360W) {
  1197. /* x360w controllers and the receiver have different ids */
  1198. input_dev->id.product = 0x02a1;
  1199. }
  1200. input_dev->dev.parent = &xpad->intf->dev;
  1201. input_set_drvdata(input_dev, xpad);
  1202. if (xpad->xtype != XTYPE_XBOX360W) {
  1203. input_dev->open = xpad_open;
  1204. input_dev->close = xpad_close;
  1205. }
  1206. __set_bit(EV_KEY, input_dev->evbit);
  1207. if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
  1208. __set_bit(EV_ABS, input_dev->evbit);
  1209. /* set up axes */
  1210. for (i = 0; xpad_abs[i] >= 0; i++)
  1211. xpad_set_up_abs(input_dev, xpad_abs[i]);
  1212. }
  1213. /* set up standard buttons */
  1214. for (i = 0; xpad_common_btn[i] >= 0; i++)
  1215. __set_bit(xpad_common_btn[i], input_dev->keybit);
  1216. /* set up model-specific ones */
  1217. if (xpad->xtype == XTYPE_XBOX360 || xpad->xtype == XTYPE_XBOX360W ||
  1218. xpad->xtype == XTYPE_XBOXONE) {
  1219. for (i = 0; xpad360_btn[i] >= 0; i++)
  1220. __set_bit(xpad360_btn[i], input_dev->keybit);
  1221. } else {
  1222. for (i = 0; xpad_btn[i] >= 0; i++)
  1223. __set_bit(xpad_btn[i], input_dev->keybit);
  1224. }
  1225. if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
  1226. for (i = 0; xpad_btn_pad[i] >= 0; i++)
  1227. __set_bit(xpad_btn_pad[i], input_dev->keybit);
  1228. }
  1229. /*
  1230. * This should be a simple else block. However historically
  1231. * xbox360w has mapped DPAD to buttons while xbox360 did not. This
  1232. * made no sense, but now we can not just switch back and have to
  1233. * support both behaviors.
  1234. */
  1235. if (!(xpad->mapping & MAP_DPAD_TO_BUTTONS) ||
  1236. xpad->xtype == XTYPE_XBOX360W) {
  1237. for (i = 0; xpad_abs_pad[i] >= 0; i++)
  1238. xpad_set_up_abs(input_dev, xpad_abs_pad[i]);
  1239. }
  1240. if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
  1241. for (i = 0; xpad_btn_triggers[i] >= 0; i++)
  1242. __set_bit(xpad_btn_triggers[i], input_dev->keybit);
  1243. } else {
  1244. for (i = 0; xpad_abs_triggers[i] >= 0; i++)
  1245. xpad_set_up_abs(input_dev, xpad_abs_triggers[i]);
  1246. }
  1247. error = xpad_init_ff(xpad);
  1248. if (error)
  1249. goto err_free_input;
  1250. error = xpad_led_probe(xpad);
  1251. if (error)
  1252. goto err_destroy_ff;
  1253. error = input_register_device(xpad->dev);
  1254. if (error)
  1255. goto err_disconnect_led;
  1256. xpad->input_created = true;
  1257. return 0;
  1258. err_disconnect_led:
  1259. xpad_led_disconnect(xpad);
  1260. err_destroy_ff:
  1261. input_ff_destroy(input_dev);
  1262. err_free_input:
  1263. input_free_device(input_dev);
  1264. return error;
  1265. }
  1266. static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1267. {
  1268. struct usb_device *udev = interface_to_usbdev(intf);
  1269. struct usb_xpad *xpad;
  1270. struct usb_endpoint_descriptor *ep_irq_in;
  1271. int ep_irq_in_idx;
  1272. int i, error;
  1273. if (intf->cur_altsetting->desc.bNumEndpoints != 2)
  1274. return -ENODEV;
  1275. for (i = 0; xpad_device[i].idVendor; i++) {
  1276. if ((le16_to_cpu(udev->descriptor.idVendor) == xpad_device[i].idVendor) &&
  1277. (le16_to_cpu(udev->descriptor.idProduct) == xpad_device[i].idProduct))
  1278. break;
  1279. }
  1280. xpad = kzalloc(sizeof(struct usb_xpad), GFP_KERNEL);
  1281. if (!xpad)
  1282. return -ENOMEM;
  1283. usb_make_path(udev, xpad->phys, sizeof(xpad->phys));
  1284. strlcat(xpad->phys, "/input0", sizeof(xpad->phys));
  1285. xpad->idata = usb_alloc_coherent(udev, XPAD_PKT_LEN,
  1286. GFP_KERNEL, &xpad->idata_dma);
  1287. if (!xpad->idata) {
  1288. error = -ENOMEM;
  1289. goto err_free_mem;
  1290. }
  1291. xpad->irq_in = usb_alloc_urb(0, GFP_KERNEL);
  1292. if (!xpad->irq_in) {
  1293. error = -ENOMEM;
  1294. goto err_free_idata;
  1295. }
  1296. xpad->udev = udev;
  1297. xpad->intf = intf;
  1298. xpad->mapping = xpad_device[i].mapping;
  1299. xpad->xtype = xpad_device[i].xtype;
  1300. xpad->name = xpad_device[i].name;
  1301. INIT_WORK(&xpad->work, xpad_presence_work);
  1302. if (xpad->xtype == XTYPE_UNKNOWN) {
  1303. if (intf->cur_altsetting->desc.bInterfaceClass == USB_CLASS_VENDOR_SPEC) {
  1304. if (intf->cur_altsetting->desc.bInterfaceProtocol == 129)
  1305. xpad->xtype = XTYPE_XBOX360W;
  1306. else if (intf->cur_altsetting->desc.bInterfaceProtocol == 208)
  1307. xpad->xtype = XTYPE_XBOXONE;
  1308. else
  1309. xpad->xtype = XTYPE_XBOX360;
  1310. } else {
  1311. xpad->xtype = XTYPE_XBOX;
  1312. }
  1313. if (dpad_to_buttons)
  1314. xpad->mapping |= MAP_DPAD_TO_BUTTONS;
  1315. if (triggers_to_buttons)
  1316. xpad->mapping |= MAP_TRIGGERS_TO_BUTTONS;
  1317. if (sticks_to_null)
  1318. xpad->mapping |= MAP_STICKS_TO_NULL;
  1319. }
  1320. if (xpad->xtype == XTYPE_XBOXONE &&
  1321. intf->cur_altsetting->desc.bInterfaceNumber != 0) {
  1322. /*
  1323. * The Xbox One controller lists three interfaces all with the
  1324. * same interface class, subclass and protocol. Differentiate by
  1325. * interface number.
  1326. */
  1327. error = -ENODEV;
  1328. goto err_free_in_urb;
  1329. }
  1330. error = xpad_init_output(intf, xpad);
  1331. if (error)
  1332. goto err_free_in_urb;
  1333. /* Xbox One controller has in/out endpoints swapped. */
  1334. ep_irq_in_idx = xpad->xtype == XTYPE_XBOXONE ? 1 : 0;
  1335. ep_irq_in = &intf->cur_altsetting->endpoint[ep_irq_in_idx].desc;
  1336. usb_fill_int_urb(xpad->irq_in, udev,
  1337. usb_rcvintpipe(udev, ep_irq_in->bEndpointAddress),
  1338. xpad->idata, XPAD_PKT_LEN, xpad_irq_in,
  1339. xpad, ep_irq_in->bInterval);
  1340. xpad->irq_in->transfer_dma = xpad->idata_dma;
  1341. xpad->irq_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1342. usb_set_intfdata(intf, xpad);
  1343. if (xpad->xtype == XTYPE_XBOX360W) {
  1344. /*
  1345. * Submit the int URB immediately rather than waiting for open
  1346. * because we get status messages from the device whether
  1347. * or not any controllers are attached. In fact, it's
  1348. * exactly the message that a controller has arrived that
  1349. * we're waiting for.
  1350. */
  1351. error = xpad360w_start_input(xpad);
  1352. if (error)
  1353. goto err_deinit_output;
  1354. /*
  1355. * Wireless controllers require RESET_RESUME to work properly
  1356. * after suspend. Ideally this quirk should be in usb core
  1357. * quirk list, but we have too many vendors producing these
  1358. * controllers and we'd need to maintain 2 identical lists
  1359. * here in this driver and in usb core.
  1360. */
  1361. udev->quirks |= USB_QUIRK_RESET_RESUME;
  1362. } else {
  1363. error = xpad_init_input(xpad);
  1364. if (error)
  1365. goto err_deinit_output;
  1366. }
  1367. return 0;
  1368. err_deinit_output:
  1369. xpad_deinit_output(xpad);
  1370. err_free_in_urb:
  1371. usb_free_urb(xpad->irq_in);
  1372. err_free_idata:
  1373. usb_free_coherent(udev, XPAD_PKT_LEN, xpad->idata, xpad->idata_dma);
  1374. err_free_mem:
  1375. kfree(xpad);
  1376. return error;
  1377. }
  1378. static void xpad_disconnect(struct usb_interface *intf)
  1379. {
  1380. struct usb_xpad *xpad = usb_get_intfdata(intf);
  1381. if (xpad->xtype == XTYPE_XBOX360W)
  1382. xpad360w_stop_input(xpad);
  1383. xpad_deinit_input(xpad);
  1384. /*
  1385. * Now that both input device and LED device are gone we can
  1386. * stop output URB.
  1387. */
  1388. xpad_stop_output(xpad);
  1389. xpad_deinit_output(xpad);
  1390. usb_free_urb(xpad->irq_in);
  1391. usb_free_coherent(xpad->udev, XPAD_PKT_LEN,
  1392. xpad->idata, xpad->idata_dma);
  1393. kfree(xpad);
  1394. usb_set_intfdata(intf, NULL);
  1395. }
  1396. static int xpad_suspend(struct usb_interface *intf, pm_message_t message)
  1397. {
  1398. struct usb_xpad *xpad = usb_get_intfdata(intf);
  1399. struct input_dev *input = xpad->dev;
  1400. if (xpad->xtype == XTYPE_XBOX360W) {
  1401. /*
  1402. * Wireless controllers always listen to input so
  1403. * they are notified when controller shows up
  1404. * or goes away.
  1405. */
  1406. xpad360w_stop_input(xpad);
  1407. /*
  1408. * The wireless adapter is going off now, so the
  1409. * gamepads are going to become disconnected.
  1410. * Unless explicitly disabled, power them down
  1411. * so they don't just sit there flashing.
  1412. */
  1413. if (auto_poweroff && xpad->pad_present)
  1414. xpad360w_poweroff_controller(xpad);
  1415. } else {
  1416. mutex_lock(&input->mutex);
  1417. if (input->users)
  1418. xpad_stop_input(xpad);
  1419. mutex_unlock(&input->mutex);
  1420. }
  1421. xpad_stop_output(xpad);
  1422. return 0;
  1423. }
  1424. static int xpad_resume(struct usb_interface *intf)
  1425. {
  1426. struct usb_xpad *xpad = usb_get_intfdata(intf);
  1427. struct input_dev *input = xpad->dev;
  1428. int retval = 0;
  1429. if (xpad->xtype == XTYPE_XBOX360W) {
  1430. retval = xpad360w_start_input(xpad);
  1431. } else {
  1432. mutex_lock(&input->mutex);
  1433. if (input->users)
  1434. retval = xpad_start_input(xpad);
  1435. mutex_unlock(&input->mutex);
  1436. }
  1437. return retval;
  1438. }
  1439. static struct usb_driver xpad_driver = {
  1440. .name = "xpad",
  1441. .probe = xpad_probe,
  1442. .disconnect = xpad_disconnect,
  1443. .suspend = xpad_suspend,
  1444. .resume = xpad_resume,
  1445. .reset_resume = xpad_resume,
  1446. .id_table = xpad_table,
  1447. };
  1448. module_usb_driver(xpad_driver);
  1449. MODULE_AUTHOR(DRIVER_AUTHOR);
  1450. MODULE_DESCRIPTION(DRIVER_DESC);
  1451. MODULE_LICENSE("GPL");