xpad.c 38 KB

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