xpad.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  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/slab.h>
  79. #include <linux/stat.h>
  80. #include <linux/module.h>
  81. #include <linux/usb/input.h>
  82. #define DRIVER_AUTHOR "Marko Friedemann <mfr@bmx-chemnitz.de>"
  83. #define DRIVER_DESC "X-Box pad driver"
  84. #define XPAD_PKT_LEN 32
  85. /* xbox d-pads should map to buttons, as is required for DDR pads
  86. but we map them to axes when possible to simplify things */
  87. #define MAP_DPAD_TO_BUTTONS (1 << 0)
  88. #define MAP_TRIGGERS_TO_BUTTONS (1 << 1)
  89. #define MAP_STICKS_TO_NULL (1 << 2)
  90. #define DANCEPAD_MAP_CONFIG (MAP_DPAD_TO_BUTTONS | \
  91. MAP_TRIGGERS_TO_BUTTONS | MAP_STICKS_TO_NULL)
  92. #define XTYPE_XBOX 0
  93. #define XTYPE_XBOX360 1
  94. #define XTYPE_XBOX360W 2
  95. #define XTYPE_XBOXONE 3
  96. #define XTYPE_UNKNOWN 4
  97. static bool dpad_to_buttons;
  98. module_param(dpad_to_buttons, bool, S_IRUGO);
  99. MODULE_PARM_DESC(dpad_to_buttons, "Map D-PAD to buttons rather than axes for unknown pads");
  100. static bool triggers_to_buttons;
  101. module_param(triggers_to_buttons, bool, S_IRUGO);
  102. MODULE_PARM_DESC(triggers_to_buttons, "Map triggers to buttons rather than axes for unknown pads");
  103. static bool sticks_to_null;
  104. module_param(sticks_to_null, bool, S_IRUGO);
  105. MODULE_PARM_DESC(sticks_to_null, "Do not map sticks at all for unknown pads");
  106. static const struct xpad_device {
  107. u16 idVendor;
  108. u16 idProduct;
  109. char *name;
  110. u8 mapping;
  111. u8 xtype;
  112. } xpad_device[] = {
  113. { 0x045e, 0x0202, "Microsoft X-Box pad v1 (US)", 0, XTYPE_XBOX },
  114. { 0x045e, 0x0285, "Microsoft X-Box pad (Japan)", 0, XTYPE_XBOX },
  115. { 0x045e, 0x0287, "Microsoft Xbox Controller S", 0, XTYPE_XBOX },
  116. { 0x045e, 0x0289, "Microsoft X-Box pad v2 (US)", 0, XTYPE_XBOX },
  117. { 0x045e, 0x028e, "Microsoft X-Box 360 pad", 0, XTYPE_XBOX360 },
  118. { 0x045e, 0x02d1, "Microsoft X-Box One pad", 0, XTYPE_XBOXONE },
  119. { 0x045e, 0x0291, "Xbox 360 Wireless Receiver (XBOX)", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360W },
  120. { 0x045e, 0x0719, "Xbox 360 Wireless Receiver", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360W },
  121. { 0x044f, 0x0f07, "Thrustmaster, Inc. Controller", 0, XTYPE_XBOX },
  122. { 0x044f, 0xb326, "Thrustmaster Gamepad GP XID", 0, XTYPE_XBOX360 },
  123. { 0x046d, 0xc21d, "Logitech Gamepad F310", 0, XTYPE_XBOX360 },
  124. { 0x046d, 0xc21e, "Logitech Gamepad F510", 0, XTYPE_XBOX360 },
  125. { 0x046d, 0xc21f, "Logitech Gamepad F710", 0, XTYPE_XBOX360 },
  126. { 0x046d, 0xc242, "Logitech Chillstream Controller", 0, XTYPE_XBOX360 },
  127. { 0x046d, 0xca84, "Logitech Xbox Cordless Controller", 0, XTYPE_XBOX },
  128. { 0x046d, 0xca88, "Logitech Compact Controller for Xbox", 0, XTYPE_XBOX },
  129. { 0x05fd, 0x1007, "Mad Catz Controller (unverified)", 0, XTYPE_XBOX },
  130. { 0x05fd, 0x107a, "InterAct 'PowerPad Pro' X-Box pad (Germany)", 0, XTYPE_XBOX },
  131. { 0x0738, 0x4516, "Mad Catz Control Pad", 0, XTYPE_XBOX },
  132. { 0x0738, 0x4522, "Mad Catz LumiCON", 0, XTYPE_XBOX },
  133. { 0x0738, 0x4526, "Mad Catz Control Pad Pro", 0, XTYPE_XBOX },
  134. { 0x0738, 0x4536, "Mad Catz MicroCON", 0, XTYPE_XBOX },
  135. { 0x0738, 0x4540, "Mad Catz Beat Pad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
  136. { 0x0738, 0x4556, "Mad Catz Lynx Wireless Controller", 0, XTYPE_XBOX },
  137. { 0x0738, 0x4716, "Mad Catz Wired Xbox 360 Controller", 0, XTYPE_XBOX360 },
  138. { 0x0738, 0x4718, "Mad Catz Street Fighter IV FightStick SE", 0, XTYPE_XBOX360 },
  139. { 0x0738, 0x4726, "Mad Catz Xbox 360 Controller", 0, XTYPE_XBOX360 },
  140. { 0x0738, 0x4728, "Mad Catz Street Fighter IV FightPad", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  141. { 0x0738, 0x4738, "Mad Catz Wired Xbox 360 Controller (SFIV)", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  142. { 0x0738, 0x4740, "Mad Catz Beat Pad", 0, XTYPE_XBOX360 },
  143. { 0x0738, 0x6040, "Mad Catz Beat Pad Pro", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
  144. { 0x0738, 0xb726, "Mad Catz Xbox controller - MW2", 0, XTYPE_XBOX360 },
  145. { 0x0738, 0xbeef, "Mad Catz JOYTECH NEO SE Advanced GamePad", XTYPE_XBOX360 },
  146. { 0x0738, 0xcb02, "Saitek Cyborg Rumble Pad - PC/Xbox 360", 0, XTYPE_XBOX360 },
  147. { 0x0738, 0xcb03, "Saitek P3200 Rumble Pad - PC/Xbox 360", 0, XTYPE_XBOX360 },
  148. { 0x0738, 0xf738, "Super SFIV FightStick TE S", 0, XTYPE_XBOX360 },
  149. { 0x0c12, 0x8802, "Zeroplus Xbox Controller", 0, XTYPE_XBOX },
  150. { 0x0c12, 0x8809, "RedOctane Xbox Dance Pad", DANCEPAD_MAP_CONFIG, XTYPE_XBOX },
  151. { 0x0c12, 0x880a, "Pelican Eclipse PL-2023", 0, XTYPE_XBOX },
  152. { 0x0c12, 0x8810, "Zeroplus Xbox Controller", 0, XTYPE_XBOX },
  153. { 0x0c12, 0x9902, "HAMA VibraX - *FAULTY HARDWARE*", 0, XTYPE_XBOX },
  154. { 0x0d2f, 0x0002, "Andamiro Pump It Up pad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
  155. { 0x0e4c, 0x1097, "Radica Gamester Controller", 0, XTYPE_XBOX },
  156. { 0x0e4c, 0x2390, "Radica Games Jtech Controller", 0, XTYPE_XBOX },
  157. { 0x0e6f, 0x0003, "Logic3 Freebird wireless Controller", 0, XTYPE_XBOX },
  158. { 0x0e6f, 0x0005, "Eclipse wireless Controller", 0, XTYPE_XBOX },
  159. { 0x0e6f, 0x0006, "Edge wireless Controller", 0, XTYPE_XBOX },
  160. { 0x0e6f, 0x0105, "HSM3 Xbox360 dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
  161. { 0x0e6f, 0x0113, "Afterglow AX.1 Gamepad for Xbox 360", 0, XTYPE_XBOX360 },
  162. { 0x0e6f, 0x0201, "Pelican PL-3601 'TSZ' Wired Xbox 360 Controller", 0, XTYPE_XBOX360 },
  163. { 0x0e6f, 0x0213, "Afterglow Gamepad for Xbox 360", 0, XTYPE_XBOX360 },
  164. { 0x0e6f, 0x021f, "Rock Candy Gamepad for Xbox 360", 0, XTYPE_XBOX360 },
  165. { 0x0e6f, 0x0301, "Logic3 Controller", 0, XTYPE_XBOX360 },
  166. { 0x0e6f, 0x0401, "Logic3 Controller", 0, XTYPE_XBOX360 },
  167. { 0x0e8f, 0x0201, "SmartJoy Frag Xpad/PS2 adaptor", 0, XTYPE_XBOX },
  168. { 0x0e8f, 0x3008, "Generic xbox control (dealextreme)", 0, XTYPE_XBOX },
  169. { 0x0f0d, 0x000a, "Hori Co. DOA4 FightStick", 0, XTYPE_XBOX360 },
  170. { 0x0f0d, 0x000d, "Hori Fighting Stick EX2", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  171. { 0x0f0d, 0x0016, "Hori Real Arcade Pro.EX", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  172. { 0x0f30, 0x0202, "Joytech Advanced Controller", 0, XTYPE_XBOX },
  173. { 0x0f30, 0x8888, "BigBen XBMiniPad Controller", 0, XTYPE_XBOX },
  174. { 0x102c, 0xff0c, "Joytech Wireless Advanced Controller", 0, XTYPE_XBOX },
  175. { 0x12ab, 0x0004, "Honey Bee Xbox360 dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
  176. { 0x12ab, 0x0301, "PDP AFTERGLOW AX.1", 0, XTYPE_XBOX360 },
  177. { 0x12ab, 0x8809, "Xbox DDR dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
  178. { 0x1430, 0x4748, "RedOctane Guitar Hero X-plorer", 0, XTYPE_XBOX360 },
  179. { 0x1430, 0x8888, "TX6500+ Dance Pad (first generation)", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
  180. { 0x146b, 0x0601, "BigBen Interactive XBOX 360 Controller", 0, XTYPE_XBOX360 },
  181. { 0x1532, 0x0037, "Razer Sabertooth", 0, XTYPE_XBOX360 },
  182. { 0x15e4, 0x3f00, "Power A Mini Pro Elite", 0, XTYPE_XBOX360 },
  183. { 0x15e4, 0x3f0a, "Xbox Airflo wired controller", 0, XTYPE_XBOX360 },
  184. { 0x15e4, 0x3f10, "Batarang Xbox 360 controller", 0, XTYPE_XBOX360 },
  185. { 0x162e, 0xbeef, "Joytech Neo-Se Take2", 0, XTYPE_XBOX360 },
  186. { 0x1689, 0xfd00, "Razer Onza Tournament Edition", 0, XTYPE_XBOX360 },
  187. { 0x1689, 0xfd01, "Razer Onza Classic Edition", 0, XTYPE_XBOX360 },
  188. { 0x24c6, 0x5d04, "Razer Sabertooth", 0, XTYPE_XBOX360 },
  189. { 0x1bad, 0x0002, "Harmonix Rock Band Guitar", 0, XTYPE_XBOX360 },
  190. { 0x1bad, 0x0003, "Harmonix Rock Band Drumkit", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
  191. { 0x1bad, 0xf016, "Mad Catz Xbox 360 Controller", 0, XTYPE_XBOX360 },
  192. { 0x1bad, 0xf023, "MLG Pro Circuit Controller (Xbox)", 0, XTYPE_XBOX360 },
  193. { 0x1bad, 0xf028, "Street Fighter IV FightPad", 0, XTYPE_XBOX360 },
  194. { 0x1bad, 0xf038, "Street Fighter IV FightStick TE", 0, XTYPE_XBOX360 },
  195. { 0x1bad, 0xf900, "Harmonix Xbox 360 Controller", 0, XTYPE_XBOX360 },
  196. { 0x1bad, 0xf901, "Gamestop Xbox 360 Controller", 0, XTYPE_XBOX360 },
  197. { 0x1bad, 0xf903, "Tron Xbox 360 controller", 0, XTYPE_XBOX360 },
  198. { 0x24c6, 0x5000, "Razer Atrox Arcade Stick", 0, XTYPE_XBOX360 },
  199. { 0x24c6, 0x5300, "PowerA MINI PROEX Controller", 0, XTYPE_XBOX360 },
  200. { 0x24c6, 0x5303, "Xbox Airflo wired controller", 0, XTYPE_XBOX360 },
  201. { 0x24c6, 0x5500, "Hori XBOX 360 EX 2 with Turbo", 0, XTYPE_XBOX360 },
  202. { 0x24c6, 0x5501, "Hori Real Arcade Pro VX-SA", 0, XTYPE_XBOX360 },
  203. { 0x24c6, 0x5506, "Hori SOULCALIBUR V Stick", 0, XTYPE_XBOX360 },
  204. { 0x24c6, 0x5b02, "Thrustmaster, Inc. GPX Controller", 0, XTYPE_XBOX360 },
  205. { 0x24c6, 0x5b03, "Thrustmaster Ferrari 458 Racing Wheel", 0, XTYPE_XBOX360 },
  206. { 0xffff, 0xffff, "Chinese-made Xbox Controller", 0, XTYPE_XBOX },
  207. { 0x0000, 0x0000, "Generic X-Box pad", 0, XTYPE_UNKNOWN }
  208. };
  209. /* buttons shared with xbox and xbox360 */
  210. static const signed short xpad_common_btn[] = {
  211. BTN_A, BTN_B, BTN_X, BTN_Y, /* "analog" buttons */
  212. BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR, /* start/back/sticks */
  213. -1 /* terminating entry */
  214. };
  215. /* original xbox controllers only */
  216. static const signed short xpad_btn[] = {
  217. BTN_C, BTN_Z, /* "analog" buttons */
  218. -1 /* terminating entry */
  219. };
  220. /* used when dpad is mapped to buttons */
  221. static const signed short xpad_btn_pad[] = {
  222. BTN_TRIGGER_HAPPY1, BTN_TRIGGER_HAPPY2, /* d-pad left, right */
  223. BTN_TRIGGER_HAPPY3, BTN_TRIGGER_HAPPY4, /* d-pad up, down */
  224. -1 /* terminating entry */
  225. };
  226. /* used when triggers are mapped to buttons */
  227. static const signed short xpad_btn_triggers[] = {
  228. BTN_TL2, BTN_TR2, /* triggers left/right */
  229. -1
  230. };
  231. static const signed short xpad360_btn[] = { /* buttons for x360 controller */
  232. BTN_TL, BTN_TR, /* Button LB/RB */
  233. BTN_MODE, /* The big X button */
  234. -1
  235. };
  236. static const signed short xpad_abs[] = {
  237. ABS_X, ABS_Y, /* left stick */
  238. ABS_RX, ABS_RY, /* right stick */
  239. -1 /* terminating entry */
  240. };
  241. /* used when dpad is mapped to axes */
  242. static const signed short xpad_abs_pad[] = {
  243. ABS_HAT0X, ABS_HAT0Y, /* d-pad axes */
  244. -1 /* terminating entry */
  245. };
  246. /* used when triggers are mapped to axes */
  247. static const signed short xpad_abs_triggers[] = {
  248. ABS_Z, ABS_RZ, /* triggers left/right */
  249. -1
  250. };
  251. /*
  252. * Xbox 360 has a vendor-specific class, so we cannot match it with only
  253. * USB_INTERFACE_INFO (also specifically refused by USB subsystem), so we
  254. * match against vendor id as well. Wired Xbox 360 devices have protocol 1,
  255. * wireless controllers have protocol 129.
  256. */
  257. #define XPAD_XBOX360_VENDOR_PROTOCOL(vend,pr) \
  258. .match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_INT_INFO, \
  259. .idVendor = (vend), \
  260. .bInterfaceClass = USB_CLASS_VENDOR_SPEC, \
  261. .bInterfaceSubClass = 93, \
  262. .bInterfaceProtocol = (pr)
  263. #define XPAD_XBOX360_VENDOR(vend) \
  264. { XPAD_XBOX360_VENDOR_PROTOCOL(vend,1) }, \
  265. { XPAD_XBOX360_VENDOR_PROTOCOL(vend,129) }
  266. /* The Xbox One controller uses subclass 71 and protocol 208. */
  267. #define XPAD_XBOXONE_VENDOR_PROTOCOL(vend, pr) \
  268. .match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_INT_INFO, \
  269. .idVendor = (vend), \
  270. .bInterfaceClass = USB_CLASS_VENDOR_SPEC, \
  271. .bInterfaceSubClass = 71, \
  272. .bInterfaceProtocol = (pr)
  273. #define XPAD_XBOXONE_VENDOR(vend) \
  274. { XPAD_XBOXONE_VENDOR_PROTOCOL(vend, 208) }
  275. static struct usb_device_id xpad_table[] = {
  276. { USB_INTERFACE_INFO('X', 'B', 0) }, /* X-Box USB-IF not approved class */
  277. XPAD_XBOX360_VENDOR(0x044f), /* Thrustmaster X-Box 360 controllers */
  278. XPAD_XBOX360_VENDOR(0x045e), /* Microsoft X-Box 360 controllers */
  279. XPAD_XBOXONE_VENDOR(0x045e), /* Microsoft X-Box One controllers */
  280. XPAD_XBOX360_VENDOR(0x046d), /* Logitech X-Box 360 style controllers */
  281. XPAD_XBOX360_VENDOR(0x0738), /* Mad Catz X-Box 360 controllers */
  282. { USB_DEVICE(0x0738, 0x4540) }, /* Mad Catz Beat Pad */
  283. XPAD_XBOX360_VENDOR(0x0e6f), /* 0x0e6f X-Box 360 controllers */
  284. XPAD_XBOX360_VENDOR(0x12ab), /* X-Box 360 dance pads */
  285. XPAD_XBOX360_VENDOR(0x1430), /* RedOctane X-Box 360 controllers */
  286. XPAD_XBOX360_VENDOR(0x146b), /* BigBen Interactive Controllers */
  287. XPAD_XBOX360_VENDOR(0x1bad), /* Harminix Rock Band Guitar and Drums */
  288. XPAD_XBOX360_VENDOR(0x0f0d), /* Hori Controllers */
  289. XPAD_XBOX360_VENDOR(0x1689), /* Razer Onza */
  290. XPAD_XBOX360_VENDOR(0x24c6), /* PowerA Controllers */
  291. XPAD_XBOX360_VENDOR(0x1532), /* Razer Sabertooth */
  292. XPAD_XBOX360_VENDOR(0x15e4), /* Numark X-Box 360 controllers */
  293. XPAD_XBOX360_VENDOR(0x162e), /* Joytech X-Box 360 controllers */
  294. { }
  295. };
  296. MODULE_DEVICE_TABLE(usb, xpad_table);
  297. struct usb_xpad {
  298. struct input_dev *dev; /* input device interface */
  299. struct usb_device *udev; /* usb device */
  300. struct usb_interface *intf; /* usb interface */
  301. int pad_present;
  302. struct urb *irq_in; /* urb for interrupt in report */
  303. unsigned char *idata; /* input data */
  304. dma_addr_t idata_dma;
  305. struct urb *bulk_out;
  306. unsigned char *bdata;
  307. struct urb *irq_out; /* urb for interrupt out report */
  308. unsigned char *odata; /* output data */
  309. dma_addr_t odata_dma;
  310. struct mutex odata_mutex;
  311. #if defined(CONFIG_JOYSTICK_XPAD_LEDS)
  312. struct xpad_led *led;
  313. #endif
  314. char phys[64]; /* physical device path */
  315. int mapping; /* map d-pad to buttons or to axes */
  316. int xtype; /* type of xbox device */
  317. };
  318. /*
  319. * xpad_process_packet
  320. *
  321. * Completes a request by converting the data into events for the
  322. * input subsystem.
  323. *
  324. * The used report descriptor was taken from ITO Takayukis website:
  325. * http://euc.jp/periphs/xbox-controller.ja.html
  326. */
  327. static void xpad_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data)
  328. {
  329. struct input_dev *dev = xpad->dev;
  330. if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
  331. /* left stick */
  332. input_report_abs(dev, ABS_X,
  333. (__s16) le16_to_cpup((__le16 *)(data + 12)));
  334. input_report_abs(dev, ABS_Y,
  335. ~(__s16) le16_to_cpup((__le16 *)(data + 14)));
  336. /* right stick */
  337. input_report_abs(dev, ABS_RX,
  338. (__s16) le16_to_cpup((__le16 *)(data + 16)));
  339. input_report_abs(dev, ABS_RY,
  340. ~(__s16) le16_to_cpup((__le16 *)(data + 18)));
  341. }
  342. /* triggers left/right */
  343. if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
  344. input_report_key(dev, BTN_TL2, data[10]);
  345. input_report_key(dev, BTN_TR2, data[11]);
  346. } else {
  347. input_report_abs(dev, ABS_Z, data[10]);
  348. input_report_abs(dev, ABS_RZ, data[11]);
  349. }
  350. /* digital pad */
  351. if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
  352. /* dpad as buttons (left, right, up, down) */
  353. input_report_key(dev, BTN_TRIGGER_HAPPY1, data[2] & 0x04);
  354. input_report_key(dev, BTN_TRIGGER_HAPPY2, data[2] & 0x08);
  355. input_report_key(dev, BTN_TRIGGER_HAPPY3, data[2] & 0x01);
  356. input_report_key(dev, BTN_TRIGGER_HAPPY4, data[2] & 0x02);
  357. } else {
  358. input_report_abs(dev, ABS_HAT0X,
  359. !!(data[2] & 0x08) - !!(data[2] & 0x04));
  360. input_report_abs(dev, ABS_HAT0Y,
  361. !!(data[2] & 0x02) - !!(data[2] & 0x01));
  362. }
  363. /* start/back buttons and stick press left/right */
  364. input_report_key(dev, BTN_START, data[2] & 0x10);
  365. input_report_key(dev, BTN_SELECT, data[2] & 0x20);
  366. input_report_key(dev, BTN_THUMBL, data[2] & 0x40);
  367. input_report_key(dev, BTN_THUMBR, data[2] & 0x80);
  368. /* "analog" buttons A, B, X, Y */
  369. input_report_key(dev, BTN_A, data[4]);
  370. input_report_key(dev, BTN_B, data[5]);
  371. input_report_key(dev, BTN_X, data[6]);
  372. input_report_key(dev, BTN_Y, data[7]);
  373. /* "analog" buttons black, white */
  374. input_report_key(dev, BTN_C, data[8]);
  375. input_report_key(dev, BTN_Z, data[9]);
  376. input_sync(dev);
  377. }
  378. /*
  379. * xpad360_process_packet
  380. *
  381. * Completes a request by converting the data into events for the
  382. * input subsystem. It is version for xbox 360 controller
  383. *
  384. * The used report descriptor was taken from:
  385. * http://www.free60.org/wiki/Gamepad
  386. */
  387. static void xpad360_process_packet(struct usb_xpad *xpad,
  388. u16 cmd, unsigned char *data)
  389. {
  390. struct input_dev *dev = xpad->dev;
  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 */
  405. input_report_key(dev, BTN_START, data[2] & 0x10);
  406. input_report_key(dev, BTN_SELECT, data[2] & 0x20);
  407. /* stick press left/right */
  408. input_report_key(dev, BTN_THUMBL, data[2] & 0x40);
  409. input_report_key(dev, BTN_THUMBR, data[2] & 0x80);
  410. /* buttons A,B,X,Y,TL,TR and MODE */
  411. input_report_key(dev, BTN_A, data[3] & 0x10);
  412. input_report_key(dev, BTN_B, data[3] & 0x20);
  413. input_report_key(dev, BTN_X, data[3] & 0x40);
  414. input_report_key(dev, BTN_Y, data[3] & 0x80);
  415. input_report_key(dev, BTN_TL, data[3] & 0x01);
  416. input_report_key(dev, BTN_TR, data[3] & 0x02);
  417. input_report_key(dev, BTN_MODE, data[3] & 0x04);
  418. if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
  419. /* left stick */
  420. input_report_abs(dev, ABS_X,
  421. (__s16) le16_to_cpup((__le16 *)(data + 6)));
  422. input_report_abs(dev, ABS_Y,
  423. ~(__s16) le16_to_cpup((__le16 *)(data + 8)));
  424. /* right stick */
  425. input_report_abs(dev, ABS_RX,
  426. (__s16) le16_to_cpup((__le16 *)(data + 10)));
  427. input_report_abs(dev, ABS_RY,
  428. ~(__s16) le16_to_cpup((__le16 *)(data + 12)));
  429. }
  430. /* triggers left/right */
  431. if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
  432. input_report_key(dev, BTN_TL2, data[4]);
  433. input_report_key(dev, BTN_TR2, data[5]);
  434. } else {
  435. input_report_abs(dev, ABS_Z, data[4]);
  436. input_report_abs(dev, ABS_RZ, data[5]);
  437. }
  438. input_sync(dev);
  439. }
  440. /*
  441. * xpad360w_process_packet
  442. *
  443. * Completes a request by converting the data into events for the
  444. * input subsystem. It is version for xbox 360 wireless controller.
  445. *
  446. * Byte.Bit
  447. * 00.1 - Status change: The controller or headset has connected/disconnected
  448. * Bits 01.7 and 01.6 are valid
  449. * 01.7 - Controller present
  450. * 01.6 - Headset present
  451. * 01.1 - Pad state (Bytes 4+) valid
  452. *
  453. */
  454. static void xpad360w_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data)
  455. {
  456. /* Presence change */
  457. if (data[0] & 0x08) {
  458. if (data[1] & 0x80) {
  459. xpad->pad_present = 1;
  460. usb_submit_urb(xpad->bulk_out, GFP_ATOMIC);
  461. } else
  462. xpad->pad_present = 0;
  463. }
  464. /* Valid pad data */
  465. if (!(data[1] & 0x1))
  466. return;
  467. xpad360_process_packet(xpad, cmd, &data[4]);
  468. }
  469. /*
  470. * xpadone_process_buttons
  471. *
  472. * Process a button update packet from an Xbox one controller.
  473. */
  474. static void xpadone_process_buttons(struct usb_xpad *xpad,
  475. struct input_dev *dev,
  476. unsigned char *data)
  477. {
  478. /* menu/view buttons */
  479. input_report_key(dev, BTN_START, data[4] & 0x04);
  480. input_report_key(dev, BTN_SELECT, data[4] & 0x08);
  481. /* buttons A,B,X,Y */
  482. input_report_key(dev, BTN_A, data[4] & 0x10);
  483. input_report_key(dev, BTN_B, data[4] & 0x20);
  484. input_report_key(dev, BTN_X, data[4] & 0x40);
  485. input_report_key(dev, BTN_Y, data[4] & 0x80);
  486. /* digital pad */
  487. if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
  488. /* dpad as buttons (left, right, up, down) */
  489. input_report_key(dev, BTN_TRIGGER_HAPPY1, data[5] & 0x04);
  490. input_report_key(dev, BTN_TRIGGER_HAPPY2, data[5] & 0x08);
  491. input_report_key(dev, BTN_TRIGGER_HAPPY3, data[5] & 0x01);
  492. input_report_key(dev, BTN_TRIGGER_HAPPY4, data[5] & 0x02);
  493. } else {
  494. input_report_abs(dev, ABS_HAT0X,
  495. !!(data[5] & 0x08) - !!(data[5] & 0x04));
  496. input_report_abs(dev, ABS_HAT0Y,
  497. !!(data[5] & 0x02) - !!(data[5] & 0x01));
  498. }
  499. /* TL/TR */
  500. input_report_key(dev, BTN_TL, data[5] & 0x10);
  501. input_report_key(dev, BTN_TR, data[5] & 0x20);
  502. /* stick press left/right */
  503. input_report_key(dev, BTN_THUMBL, data[5] & 0x40);
  504. input_report_key(dev, BTN_THUMBR, data[5] & 0x80);
  505. if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
  506. /* left stick */
  507. input_report_abs(dev, ABS_X,
  508. (__s16) le16_to_cpup((__le16 *)(data + 10)));
  509. input_report_abs(dev, ABS_Y,
  510. ~(__s16) le16_to_cpup((__le16 *)(data + 12)));
  511. /* right stick */
  512. input_report_abs(dev, ABS_RX,
  513. (__s16) le16_to_cpup((__le16 *)(data + 14)));
  514. input_report_abs(dev, ABS_RY,
  515. ~(__s16) le16_to_cpup((__le16 *)(data + 16)));
  516. }
  517. /* triggers left/right */
  518. if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
  519. input_report_key(dev, BTN_TL2,
  520. (__u16) le16_to_cpup((__le16 *)(data + 6)));
  521. input_report_key(dev, BTN_TR2,
  522. (__u16) le16_to_cpup((__le16 *)(data + 8)));
  523. } else {
  524. input_report_abs(dev, ABS_Z,
  525. (__u16) le16_to_cpup((__le16 *)(data + 6)));
  526. input_report_abs(dev, ABS_RZ,
  527. (__u16) le16_to_cpup((__le16 *)(data + 8)));
  528. }
  529. input_sync(dev);
  530. }
  531. /*
  532. * xpadone_process_packet
  533. *
  534. * Completes a request by converting the data into events for the
  535. * input subsystem. This version is for the Xbox One controller.
  536. *
  537. * The report format was gleaned from
  538. * https://github.com/kylelemons/xbox/blob/master/xbox.go
  539. */
  540. static void xpadone_process_packet(struct usb_xpad *xpad,
  541. u16 cmd, unsigned char *data)
  542. {
  543. struct input_dev *dev = xpad->dev;
  544. switch (data[0]) {
  545. case 0x20:
  546. xpadone_process_buttons(xpad, dev, data);
  547. break;
  548. case 0x07:
  549. /* the xbox button has its own special report */
  550. input_report_key(dev, BTN_MODE, data[4] & 0x01);
  551. input_sync(dev);
  552. break;
  553. }
  554. }
  555. static void xpad_irq_in(struct urb *urb)
  556. {
  557. struct usb_xpad *xpad = urb->context;
  558. struct device *dev = &xpad->intf->dev;
  559. int retval, status;
  560. status = urb->status;
  561. switch (status) {
  562. case 0:
  563. /* success */
  564. break;
  565. case -ECONNRESET:
  566. case -ENOENT:
  567. case -ESHUTDOWN:
  568. /* this urb is terminated, clean up */
  569. dev_dbg(dev, "%s - urb shutting down with status: %d\n",
  570. __func__, status);
  571. return;
  572. default:
  573. dev_dbg(dev, "%s - nonzero urb status received: %d\n",
  574. __func__, status);
  575. goto exit;
  576. }
  577. switch (xpad->xtype) {
  578. case XTYPE_XBOX360:
  579. xpad360_process_packet(xpad, 0, xpad->idata);
  580. break;
  581. case XTYPE_XBOX360W:
  582. xpad360w_process_packet(xpad, 0, xpad->idata);
  583. break;
  584. case XTYPE_XBOXONE:
  585. xpadone_process_packet(xpad, 0, xpad->idata);
  586. break;
  587. default:
  588. xpad_process_packet(xpad, 0, xpad->idata);
  589. }
  590. exit:
  591. retval = usb_submit_urb(urb, GFP_ATOMIC);
  592. if (retval)
  593. dev_err(dev, "%s - usb_submit_urb failed with result %d\n",
  594. __func__, retval);
  595. }
  596. static void xpad_bulk_out(struct urb *urb)
  597. {
  598. struct usb_xpad *xpad = urb->context;
  599. struct device *dev = &xpad->intf->dev;
  600. switch (urb->status) {
  601. case 0:
  602. /* success */
  603. break;
  604. case -ECONNRESET:
  605. case -ENOENT:
  606. case -ESHUTDOWN:
  607. /* this urb is terminated, clean up */
  608. dev_dbg(dev, "%s - urb shutting down with status: %d\n",
  609. __func__, urb->status);
  610. break;
  611. default:
  612. dev_dbg(dev, "%s - nonzero urb status received: %d\n",
  613. __func__, urb->status);
  614. }
  615. }
  616. static void xpad_irq_out(struct urb *urb)
  617. {
  618. struct usb_xpad *xpad = urb->context;
  619. struct device *dev = &xpad->intf->dev;
  620. int retval, status;
  621. status = urb->status;
  622. switch (status) {
  623. case 0:
  624. /* success */
  625. return;
  626. case -ECONNRESET:
  627. case -ENOENT:
  628. case -ESHUTDOWN:
  629. /* this urb is terminated, clean up */
  630. dev_dbg(dev, "%s - urb shutting down with status: %d\n",
  631. __func__, status);
  632. return;
  633. default:
  634. dev_dbg(dev, "%s - nonzero urb status received: %d\n",
  635. __func__, status);
  636. goto exit;
  637. }
  638. exit:
  639. retval = usb_submit_urb(urb, GFP_ATOMIC);
  640. if (retval)
  641. dev_err(dev, "%s - usb_submit_urb failed with result %d\n",
  642. __func__, retval);
  643. }
  644. static int xpad_init_output(struct usb_interface *intf, struct usb_xpad *xpad)
  645. {
  646. struct usb_endpoint_descriptor *ep_irq_out;
  647. int ep_irq_out_idx;
  648. int error;
  649. if (xpad->xtype == XTYPE_UNKNOWN)
  650. return 0;
  651. xpad->odata = usb_alloc_coherent(xpad->udev, XPAD_PKT_LEN,
  652. GFP_KERNEL, &xpad->odata_dma);
  653. if (!xpad->odata) {
  654. error = -ENOMEM;
  655. goto fail1;
  656. }
  657. mutex_init(&xpad->odata_mutex);
  658. xpad->irq_out = usb_alloc_urb(0, GFP_KERNEL);
  659. if (!xpad->irq_out) {
  660. error = -ENOMEM;
  661. goto fail2;
  662. }
  663. /* Xbox One controller has in/out endpoints swapped. */
  664. ep_irq_out_idx = xpad->xtype == XTYPE_XBOXONE ? 0 : 1;
  665. ep_irq_out = &intf->cur_altsetting->endpoint[ep_irq_out_idx].desc;
  666. usb_fill_int_urb(xpad->irq_out, xpad->udev,
  667. usb_sndintpipe(xpad->udev, ep_irq_out->bEndpointAddress),
  668. xpad->odata, XPAD_PKT_LEN,
  669. xpad_irq_out, xpad, ep_irq_out->bInterval);
  670. xpad->irq_out->transfer_dma = xpad->odata_dma;
  671. xpad->irq_out->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  672. return 0;
  673. fail2: usb_free_coherent(xpad->udev, XPAD_PKT_LEN, xpad->odata, xpad->odata_dma);
  674. fail1: return error;
  675. }
  676. static void xpad_stop_output(struct usb_xpad *xpad)
  677. {
  678. if (xpad->xtype != XTYPE_UNKNOWN)
  679. usb_kill_urb(xpad->irq_out);
  680. }
  681. static void xpad_deinit_output(struct usb_xpad *xpad)
  682. {
  683. if (xpad->xtype != XTYPE_UNKNOWN) {
  684. usb_free_urb(xpad->irq_out);
  685. usb_free_coherent(xpad->udev, XPAD_PKT_LEN,
  686. xpad->odata, xpad->odata_dma);
  687. }
  688. }
  689. #ifdef CONFIG_JOYSTICK_XPAD_FF
  690. static int xpad_play_effect(struct input_dev *dev, void *data, struct ff_effect *effect)
  691. {
  692. struct usb_xpad *xpad = input_get_drvdata(dev);
  693. if (effect->type == FF_RUMBLE) {
  694. __u16 strong = effect->u.rumble.strong_magnitude;
  695. __u16 weak = effect->u.rumble.weak_magnitude;
  696. switch (xpad->xtype) {
  697. case XTYPE_XBOX:
  698. xpad->odata[0] = 0x00;
  699. xpad->odata[1] = 0x06;
  700. xpad->odata[2] = 0x00;
  701. xpad->odata[3] = strong / 256; /* left actuator */
  702. xpad->odata[4] = 0x00;
  703. xpad->odata[5] = weak / 256; /* right actuator */
  704. xpad->irq_out->transfer_buffer_length = 6;
  705. return usb_submit_urb(xpad->irq_out, GFP_ATOMIC);
  706. case XTYPE_XBOX360:
  707. xpad->odata[0] = 0x00;
  708. xpad->odata[1] = 0x08;
  709. xpad->odata[2] = 0x00;
  710. xpad->odata[3] = strong / 256; /* left actuator? */
  711. xpad->odata[4] = weak / 256; /* right actuator? */
  712. xpad->odata[5] = 0x00;
  713. xpad->odata[6] = 0x00;
  714. xpad->odata[7] = 0x00;
  715. xpad->irq_out->transfer_buffer_length = 8;
  716. return usb_submit_urb(xpad->irq_out, GFP_ATOMIC);
  717. case XTYPE_XBOX360W:
  718. xpad->odata[0] = 0x00;
  719. xpad->odata[1] = 0x01;
  720. xpad->odata[2] = 0x0F;
  721. xpad->odata[3] = 0xC0;
  722. xpad->odata[4] = 0x00;
  723. xpad->odata[5] = strong / 256;
  724. xpad->odata[6] = weak / 256;
  725. xpad->odata[7] = 0x00;
  726. xpad->odata[8] = 0x00;
  727. xpad->odata[9] = 0x00;
  728. xpad->odata[10] = 0x00;
  729. xpad->odata[11] = 0x00;
  730. xpad->irq_out->transfer_buffer_length = 12;
  731. return usb_submit_urb(xpad->irq_out, GFP_ATOMIC);
  732. case XTYPE_XBOXONE:
  733. xpad->odata[0] = 0x09; /* activate rumble */
  734. xpad->odata[1] = 0x08;
  735. xpad->odata[2] = 0x00;
  736. xpad->odata[3] = 0x08; /* continuous effect */
  737. xpad->odata[4] = 0x00; /* simple rumble mode */
  738. xpad->odata[5] = 0x03; /* L and R actuator only */
  739. xpad->odata[6] = 0x00; /* TODO: LT actuator */
  740. xpad->odata[7] = 0x00; /* TODO: RT actuator */
  741. xpad->odata[8] = strong / 256; /* left actuator */
  742. xpad->odata[9] = weak / 256; /* right actuator */
  743. xpad->odata[10] = 0x80; /* length of pulse */
  744. xpad->odata[11] = 0x00; /* stop period of pulse */
  745. xpad->irq_out->transfer_buffer_length = 12;
  746. return usb_submit_urb(xpad->irq_out, GFP_ATOMIC);
  747. default:
  748. dev_dbg(&xpad->dev->dev,
  749. "%s - rumble command sent to unsupported xpad type: %d\n",
  750. __func__, xpad->xtype);
  751. return -1;
  752. }
  753. }
  754. return 0;
  755. }
  756. static int xpad_init_ff(struct usb_xpad *xpad)
  757. {
  758. if (xpad->xtype == XTYPE_UNKNOWN)
  759. return 0;
  760. input_set_capability(xpad->dev, EV_FF, FF_RUMBLE);
  761. return input_ff_create_memless(xpad->dev, NULL, xpad_play_effect);
  762. }
  763. #else
  764. static int xpad_init_ff(struct usb_xpad *xpad) { return 0; }
  765. #endif
  766. #if defined(CONFIG_JOYSTICK_XPAD_LEDS)
  767. #include <linux/leds.h>
  768. struct xpad_led {
  769. char name[16];
  770. struct led_classdev led_cdev;
  771. struct usb_xpad *xpad;
  772. };
  773. static void xpad_send_led_command(struct usb_xpad *xpad, int command)
  774. {
  775. if (command >= 0 && command < 14) {
  776. mutex_lock(&xpad->odata_mutex);
  777. xpad->odata[0] = 0x01;
  778. xpad->odata[1] = 0x03;
  779. xpad->odata[2] = command;
  780. xpad->irq_out->transfer_buffer_length = 3;
  781. usb_submit_urb(xpad->irq_out, GFP_KERNEL);
  782. mutex_unlock(&xpad->odata_mutex);
  783. }
  784. }
  785. static void xpad_led_set(struct led_classdev *led_cdev,
  786. enum led_brightness value)
  787. {
  788. struct xpad_led *xpad_led = container_of(led_cdev,
  789. struct xpad_led, led_cdev);
  790. xpad_send_led_command(xpad_led->xpad, value);
  791. }
  792. static int xpad_led_probe(struct usb_xpad *xpad)
  793. {
  794. static atomic_t led_seq = ATOMIC_INIT(-1);
  795. unsigned long led_no;
  796. struct xpad_led *led;
  797. struct led_classdev *led_cdev;
  798. int error;
  799. if (xpad->xtype != XTYPE_XBOX360)
  800. return 0;
  801. xpad->led = led = kzalloc(sizeof(struct xpad_led), GFP_KERNEL);
  802. if (!led)
  803. return -ENOMEM;
  804. led_no = atomic_inc_return(&led_seq);
  805. snprintf(led->name, sizeof(led->name), "xpad%lu", led_no);
  806. led->xpad = xpad;
  807. led_cdev = &led->led_cdev;
  808. led_cdev->name = led->name;
  809. led_cdev->brightness_set = xpad_led_set;
  810. error = led_classdev_register(&xpad->udev->dev, led_cdev);
  811. if (error) {
  812. kfree(led);
  813. xpad->led = NULL;
  814. return error;
  815. }
  816. /*
  817. * Light up the segment corresponding to controller number
  818. */
  819. xpad_send_led_command(xpad, (led_no % 4) + 2);
  820. return 0;
  821. }
  822. static void xpad_led_disconnect(struct usb_xpad *xpad)
  823. {
  824. struct xpad_led *xpad_led = xpad->led;
  825. if (xpad_led) {
  826. led_classdev_unregister(&xpad_led->led_cdev);
  827. kfree(xpad_led);
  828. }
  829. }
  830. #else
  831. static int xpad_led_probe(struct usb_xpad *xpad) { return 0; }
  832. static void xpad_led_disconnect(struct usb_xpad *xpad) { }
  833. #endif
  834. static int xpad_open(struct input_dev *dev)
  835. {
  836. struct usb_xpad *xpad = input_get_drvdata(dev);
  837. /* URB was submitted in probe */
  838. if (xpad->xtype == XTYPE_XBOX360W)
  839. return 0;
  840. xpad->irq_in->dev = xpad->udev;
  841. if (usb_submit_urb(xpad->irq_in, GFP_KERNEL))
  842. return -EIO;
  843. if (xpad->xtype == XTYPE_XBOXONE) {
  844. /* Xbox one controller needs to be initialized. */
  845. xpad->odata[0] = 0x05;
  846. xpad->odata[1] = 0x20;
  847. xpad->irq_out->transfer_buffer_length = 2;
  848. return usb_submit_urb(xpad->irq_out, GFP_KERNEL);
  849. }
  850. return 0;
  851. }
  852. static void xpad_close(struct input_dev *dev)
  853. {
  854. struct usb_xpad *xpad = input_get_drvdata(dev);
  855. if (xpad->xtype != XTYPE_XBOX360W)
  856. usb_kill_urb(xpad->irq_in);
  857. xpad_stop_output(xpad);
  858. }
  859. static void xpad_set_up_abs(struct input_dev *input_dev, signed short abs)
  860. {
  861. struct usb_xpad *xpad = input_get_drvdata(input_dev);
  862. set_bit(abs, input_dev->absbit);
  863. switch (abs) {
  864. case ABS_X:
  865. case ABS_Y:
  866. case ABS_RX:
  867. case ABS_RY: /* the two sticks */
  868. input_set_abs_params(input_dev, abs, -32768, 32767, 16, 128);
  869. break;
  870. case ABS_Z:
  871. case ABS_RZ: /* the triggers (if mapped to axes) */
  872. if (xpad->xtype == XTYPE_XBOXONE)
  873. input_set_abs_params(input_dev, abs, 0, 1023, 0, 0);
  874. else
  875. input_set_abs_params(input_dev, abs, 0, 255, 0, 0);
  876. break;
  877. case ABS_HAT0X:
  878. case ABS_HAT0Y: /* the d-pad (only if dpad is mapped to axes */
  879. input_set_abs_params(input_dev, abs, -1, 1, 0, 0);
  880. break;
  881. }
  882. }
  883. static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id)
  884. {
  885. struct usb_device *udev = interface_to_usbdev(intf);
  886. struct usb_xpad *xpad;
  887. struct input_dev *input_dev;
  888. struct usb_endpoint_descriptor *ep_irq_in;
  889. int ep_irq_in_idx;
  890. int i, error;
  891. for (i = 0; xpad_device[i].idVendor; i++) {
  892. if ((le16_to_cpu(udev->descriptor.idVendor) == xpad_device[i].idVendor) &&
  893. (le16_to_cpu(udev->descriptor.idProduct) == xpad_device[i].idProduct))
  894. break;
  895. }
  896. if (xpad_device[i].xtype == XTYPE_XBOXONE &&
  897. intf->cur_altsetting->desc.bInterfaceNumber != 0) {
  898. /*
  899. * The Xbox One controller lists three interfaces all with the
  900. * same interface class, subclass and protocol. Differentiate by
  901. * interface number.
  902. */
  903. return -ENODEV;
  904. }
  905. xpad = kzalloc(sizeof(struct usb_xpad), GFP_KERNEL);
  906. input_dev = input_allocate_device();
  907. if (!xpad || !input_dev) {
  908. error = -ENOMEM;
  909. goto fail1;
  910. }
  911. xpad->idata = usb_alloc_coherent(udev, XPAD_PKT_LEN,
  912. GFP_KERNEL, &xpad->idata_dma);
  913. if (!xpad->idata) {
  914. error = -ENOMEM;
  915. goto fail1;
  916. }
  917. xpad->irq_in = usb_alloc_urb(0, GFP_KERNEL);
  918. if (!xpad->irq_in) {
  919. error = -ENOMEM;
  920. goto fail2;
  921. }
  922. xpad->udev = udev;
  923. xpad->intf = intf;
  924. xpad->mapping = xpad_device[i].mapping;
  925. xpad->xtype = xpad_device[i].xtype;
  926. if (xpad->xtype == XTYPE_UNKNOWN) {
  927. if (intf->cur_altsetting->desc.bInterfaceClass == USB_CLASS_VENDOR_SPEC) {
  928. if (intf->cur_altsetting->desc.bInterfaceProtocol == 129)
  929. xpad->xtype = XTYPE_XBOX360W;
  930. else
  931. xpad->xtype = XTYPE_XBOX360;
  932. } else
  933. xpad->xtype = XTYPE_XBOX;
  934. if (dpad_to_buttons)
  935. xpad->mapping |= MAP_DPAD_TO_BUTTONS;
  936. if (triggers_to_buttons)
  937. xpad->mapping |= MAP_TRIGGERS_TO_BUTTONS;
  938. if (sticks_to_null)
  939. xpad->mapping |= MAP_STICKS_TO_NULL;
  940. }
  941. xpad->dev = input_dev;
  942. usb_make_path(udev, xpad->phys, sizeof(xpad->phys));
  943. strlcat(xpad->phys, "/input0", sizeof(xpad->phys));
  944. input_dev->name = xpad_device[i].name;
  945. input_dev->phys = xpad->phys;
  946. usb_to_input_id(udev, &input_dev->id);
  947. input_dev->dev.parent = &intf->dev;
  948. input_set_drvdata(input_dev, xpad);
  949. input_dev->open = xpad_open;
  950. input_dev->close = xpad_close;
  951. input_dev->evbit[0] = BIT_MASK(EV_KEY);
  952. if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
  953. input_dev->evbit[0] |= BIT_MASK(EV_ABS);
  954. /* set up axes */
  955. for (i = 0; xpad_abs[i] >= 0; i++)
  956. xpad_set_up_abs(input_dev, xpad_abs[i]);
  957. }
  958. /* set up standard buttons */
  959. for (i = 0; xpad_common_btn[i] >= 0; i++)
  960. __set_bit(xpad_common_btn[i], input_dev->keybit);
  961. /* set up model-specific ones */
  962. if (xpad->xtype == XTYPE_XBOX360 || xpad->xtype == XTYPE_XBOX360W ||
  963. xpad->xtype == XTYPE_XBOXONE) {
  964. for (i = 0; xpad360_btn[i] >= 0; i++)
  965. __set_bit(xpad360_btn[i], input_dev->keybit);
  966. } else {
  967. for (i = 0; xpad_btn[i] >= 0; i++)
  968. __set_bit(xpad_btn[i], input_dev->keybit);
  969. }
  970. if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
  971. for (i = 0; xpad_btn_pad[i] >= 0; i++)
  972. __set_bit(xpad_btn_pad[i], input_dev->keybit);
  973. } else {
  974. for (i = 0; xpad_abs_pad[i] >= 0; i++)
  975. xpad_set_up_abs(input_dev, xpad_abs_pad[i]);
  976. }
  977. if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
  978. for (i = 0; xpad_btn_triggers[i] >= 0; i++)
  979. __set_bit(xpad_btn_triggers[i], input_dev->keybit);
  980. } else {
  981. for (i = 0; xpad_abs_triggers[i] >= 0; i++)
  982. xpad_set_up_abs(input_dev, xpad_abs_triggers[i]);
  983. }
  984. error = xpad_init_output(intf, xpad);
  985. if (error)
  986. goto fail3;
  987. error = xpad_init_ff(xpad);
  988. if (error)
  989. goto fail4;
  990. error = xpad_led_probe(xpad);
  991. if (error)
  992. goto fail5;
  993. /* Xbox One controller has in/out endpoints swapped. */
  994. ep_irq_in_idx = xpad->xtype == XTYPE_XBOXONE ? 1 : 0;
  995. ep_irq_in = &intf->cur_altsetting->endpoint[ep_irq_in_idx].desc;
  996. usb_fill_int_urb(xpad->irq_in, udev,
  997. usb_rcvintpipe(udev, ep_irq_in->bEndpointAddress),
  998. xpad->idata, XPAD_PKT_LEN, xpad_irq_in,
  999. xpad, ep_irq_in->bInterval);
  1000. xpad->irq_in->transfer_dma = xpad->idata_dma;
  1001. xpad->irq_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1002. error = input_register_device(xpad->dev);
  1003. if (error)
  1004. goto fail6;
  1005. usb_set_intfdata(intf, xpad);
  1006. if (xpad->xtype == XTYPE_XBOX360W) {
  1007. /*
  1008. * Setup the message to set the LEDs on the
  1009. * controller when it shows up
  1010. */
  1011. xpad->bulk_out = usb_alloc_urb(0, GFP_KERNEL);
  1012. if (!xpad->bulk_out) {
  1013. error = -ENOMEM;
  1014. goto fail7;
  1015. }
  1016. xpad->bdata = kzalloc(XPAD_PKT_LEN, GFP_KERNEL);
  1017. if (!xpad->bdata) {
  1018. error = -ENOMEM;
  1019. goto fail8;
  1020. }
  1021. xpad->bdata[2] = 0x08;
  1022. switch (intf->cur_altsetting->desc.bInterfaceNumber) {
  1023. case 0:
  1024. xpad->bdata[3] = 0x42;
  1025. break;
  1026. case 2:
  1027. xpad->bdata[3] = 0x43;
  1028. break;
  1029. case 4:
  1030. xpad->bdata[3] = 0x44;
  1031. break;
  1032. case 6:
  1033. xpad->bdata[3] = 0x45;
  1034. }
  1035. ep_irq_in = &intf->cur_altsetting->endpoint[1].desc;
  1036. if (usb_endpoint_is_bulk_out(ep_irq_in)) {
  1037. usb_fill_bulk_urb(xpad->bulk_out, udev,
  1038. usb_sndbulkpipe(udev,
  1039. ep_irq_in->bEndpointAddress),
  1040. xpad->bdata, XPAD_PKT_LEN,
  1041. xpad_bulk_out, xpad);
  1042. } else {
  1043. usb_fill_int_urb(xpad->bulk_out, udev,
  1044. usb_sndintpipe(udev,
  1045. ep_irq_in->bEndpointAddress),
  1046. xpad->bdata, XPAD_PKT_LEN,
  1047. xpad_bulk_out, xpad, 0);
  1048. }
  1049. /*
  1050. * Submit the int URB immediately rather than waiting for open
  1051. * because we get status messages from the device whether
  1052. * or not any controllers are attached. In fact, it's
  1053. * exactly the message that a controller has arrived that
  1054. * we're waiting for.
  1055. */
  1056. xpad->irq_in->dev = xpad->udev;
  1057. error = usb_submit_urb(xpad->irq_in, GFP_KERNEL);
  1058. if (error)
  1059. goto fail9;
  1060. }
  1061. return 0;
  1062. fail9: kfree(xpad->bdata);
  1063. fail8: usb_free_urb(xpad->bulk_out);
  1064. fail7: input_unregister_device(input_dev);
  1065. input_dev = NULL;
  1066. fail6: xpad_led_disconnect(xpad);
  1067. fail5: if (input_dev)
  1068. input_ff_destroy(input_dev);
  1069. fail4: xpad_deinit_output(xpad);
  1070. fail3: usb_free_urb(xpad->irq_in);
  1071. fail2: usb_free_coherent(udev, XPAD_PKT_LEN, xpad->idata, xpad->idata_dma);
  1072. fail1: input_free_device(input_dev);
  1073. kfree(xpad);
  1074. return error;
  1075. }
  1076. static void xpad_disconnect(struct usb_interface *intf)
  1077. {
  1078. struct usb_xpad *xpad = usb_get_intfdata (intf);
  1079. xpad_led_disconnect(xpad);
  1080. input_unregister_device(xpad->dev);
  1081. xpad_deinit_output(xpad);
  1082. if (xpad->xtype == XTYPE_XBOX360W) {
  1083. usb_kill_urb(xpad->bulk_out);
  1084. usb_free_urb(xpad->bulk_out);
  1085. usb_kill_urb(xpad->irq_in);
  1086. }
  1087. usb_free_urb(xpad->irq_in);
  1088. usb_free_coherent(xpad->udev, XPAD_PKT_LEN,
  1089. xpad->idata, xpad->idata_dma);
  1090. kfree(xpad->bdata);
  1091. kfree(xpad);
  1092. usb_set_intfdata(intf, NULL);
  1093. }
  1094. static struct usb_driver xpad_driver = {
  1095. .name = "xpad",
  1096. .probe = xpad_probe,
  1097. .disconnect = xpad_disconnect,
  1098. .id_table = xpad_table,
  1099. };
  1100. module_usb_driver(xpad_driver);
  1101. MODULE_AUTHOR(DRIVER_AUTHOR);
  1102. MODULE_DESCRIPTION(DRIVER_DESC);
  1103. MODULE_LICENSE("GPL");