xpad.c 32 KB

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