xpad.c 32 KB

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