xpad.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. *
  28. * This driver is based on:
  29. * - information from http://euc.jp/periphs/xbox-controller.ja.html
  30. * - the iForce driver drivers/char/joystick/iforce.c
  31. * - the skeleton-driver drivers/usb/usb-skeleton.c
  32. * - Xbox 360 information http://www.free60.org/wiki/Gamepad
  33. *
  34. * Thanks to:
  35. * - ITO Takayuki for providing essential xpad information on his website
  36. * - Vojtech Pavlik - iforce driver / input subsystem
  37. * - Greg Kroah-Hartman - usb-skeleton driver
  38. * - XBOX Linux project - extra USB id's
  39. *
  40. * TODO:
  41. * - fine tune axes (especially trigger axes)
  42. * - fix "analog" buttons (reported as digital now)
  43. * - get rumble working
  44. * - need USB IDs for other dance pads
  45. *
  46. * History:
  47. *
  48. * 2002-06-27 - 0.0.1 : first version, just said "XBOX HID controller"
  49. *
  50. * 2002-07-02 - 0.0.2 : basic working version
  51. * - all axes and 9 of the 10 buttons work (german InterAct device)
  52. * - the black button does not work
  53. *
  54. * 2002-07-14 - 0.0.3 : rework by Vojtech Pavlik
  55. * - indentation fixes
  56. * - usb + input init sequence fixes
  57. *
  58. * 2002-07-16 - 0.0.4 : minor changes, merge with Vojtech's v0.0.3
  59. * - verified the lack of HID and report descriptors
  60. * - verified that ALL buttons WORK
  61. * - fixed d-pad to axes mapping
  62. *
  63. * 2002-07-17 - 0.0.5 : simplified d-pad handling
  64. *
  65. * 2004-10-02 - 0.0.6 : DDR pad support
  66. * - borrowed from the XBOX linux kernel
  67. * - USB id's for commonly used dance pads are present
  68. * - dance pads will map D-PAD to buttons, not axes
  69. * - pass the module paramater 'dpad_to_buttons' to force
  70. * the D-PAD to map to buttons if your pad is not detected
  71. *
  72. * Later changes can be tracked in SCM.
  73. */
  74. #include <linux/kernel.h>
  75. #include <linux/init.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 XTYPE_XBOX 0
  88. #define XTYPE_XBOX360 1
  89. #define XTYPE_XBOX360W 2
  90. #define XTYPE_UNKNOWN 3
  91. static int dpad_to_buttons;
  92. module_param(dpad_to_buttons, bool, S_IRUGO);
  93. MODULE_PARM_DESC(dpad_to_buttons, "Map D-PAD to buttons rather than axes for unknown pads");
  94. static int triggers_to_buttons;
  95. module_param(triggers_to_buttons, bool, S_IRUGO);
  96. MODULE_PARM_DESC(triggers_to_buttons, "Map triggers to buttons rather than axes for unknown pads");
  97. static const struct xpad_device {
  98. u16 idVendor;
  99. u16 idProduct;
  100. char *name;
  101. u8 mapping;
  102. u8 xtype;
  103. } xpad_device[] = {
  104. { 0x045e, 0x0202, "Microsoft X-Box pad v1 (US)", 0, XTYPE_XBOX },
  105. { 0x045e, 0x0289, "Microsoft X-Box pad v2 (US)", 0, XTYPE_XBOX },
  106. { 0x045e, 0x0285, "Microsoft X-Box pad (Japan)", 0, XTYPE_XBOX },
  107. { 0x045e, 0x0287, "Microsoft Xbox Controller S", 0, XTYPE_XBOX },
  108. { 0x045e, 0x0719, "Xbox 360 Wireless Receiver", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360W },
  109. { 0x0c12, 0x8809, "RedOctane Xbox Dance Pad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
  110. { 0x044f, 0x0f07, "Thrustmaster, Inc. Controller", 0, XTYPE_XBOX },
  111. { 0x046d, 0xc242, "Logitech Chillstream Controller", 0, XTYPE_XBOX360 },
  112. { 0x046d, 0xca84, "Logitech Xbox Cordless Controller", 0, XTYPE_XBOX },
  113. { 0x046d, 0xca88, "Logitech Compact Controller for Xbox", 0, XTYPE_XBOX },
  114. { 0x05fd, 0x1007, "Mad Catz Controller (unverified)", 0, XTYPE_XBOX },
  115. { 0x05fd, 0x107a, "InterAct 'PowerPad Pro' X-Box pad (Germany)", 0, XTYPE_XBOX },
  116. { 0x0738, 0x4516, "Mad Catz Control Pad", 0, XTYPE_XBOX },
  117. { 0x0738, 0x4522, "Mad Catz LumiCON", 0, XTYPE_XBOX },
  118. { 0x0738, 0x4526, "Mad Catz Control Pad Pro", 0, XTYPE_XBOX },
  119. { 0x0738, 0x4536, "Mad Catz MicroCON", 0, XTYPE_XBOX },
  120. { 0x0738, 0x4540, "Mad Catz Beat Pad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
  121. { 0x0738, 0x4556, "Mad Catz Lynx Wireless Controller", 0, XTYPE_XBOX },
  122. { 0x0738, 0x4716, "Mad Catz Wired Xbox 360 Controller", 0, XTYPE_XBOX360 },
  123. { 0x0738, 0x4738, "Mad Catz Wired Xbox 360 Controller (SFIV)", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  124. { 0x0738, 0x6040, "Mad Catz Beat Pad Pro", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
  125. { 0x0c12, 0x8802, "Zeroplus Xbox Controller", 0, XTYPE_XBOX },
  126. { 0x0c12, 0x880a, "Pelican Eclipse PL-2023", 0, XTYPE_XBOX },
  127. { 0x0c12, 0x8810, "Zeroplus Xbox Controller", 0, XTYPE_XBOX },
  128. { 0x0c12, 0x9902, "HAMA VibraX - *FAULTY HARDWARE*", 0, XTYPE_XBOX },
  129. { 0x0e4c, 0x1097, "Radica Gamester Controller", 0, XTYPE_XBOX },
  130. { 0x0e4c, 0x2390, "Radica Games Jtech Controller", 0, XTYPE_XBOX },
  131. { 0x0e6f, 0x0003, "Logic3 Freebird wireless Controller", 0, XTYPE_XBOX },
  132. { 0x0e6f, 0x0005, "Eclipse wireless Controller", 0, XTYPE_XBOX },
  133. { 0x0e6f, 0x0006, "Edge wireless Controller", 0, XTYPE_XBOX },
  134. { 0x0e6f, 0x0006, "Pelican 'TSZ' Wired Xbox 360 Controller", 0, XTYPE_XBOX360 },
  135. { 0x0e8f, 0x0201, "SmartJoy Frag Xpad/PS2 adaptor", 0, XTYPE_XBOX },
  136. { 0x0f30, 0x0202, "Joytech Advanced Controller", 0, XTYPE_XBOX },
  137. { 0x0f30, 0x8888, "BigBen XBMiniPad Controller", 0, XTYPE_XBOX },
  138. { 0x102c, 0xff0c, "Joytech Wireless Advanced Controller", 0, XTYPE_XBOX },
  139. { 0x12ab, 0x8809, "Xbox DDR dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
  140. { 0x1430, 0x4748, "RedOctane Guitar Hero X-plorer", 0, XTYPE_XBOX360 },
  141. { 0x1430, 0x8888, "TX6500+ Dance Pad (first generation)", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX },
  142. { 0x146b, 0x0601, "BigBen Interactive XBOX 360 Controller", 0, XTYPE_XBOX360 },
  143. { 0x045e, 0x028e, "Microsoft X-Box 360 pad", 0, XTYPE_XBOX360 },
  144. { 0x1bad, 0x0003, "Harmonix Rock Band Drumkit", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 },
  145. { 0x0f0d, 0x0016, "Hori Real Arcade Pro.EX", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 },
  146. { 0xffff, 0xffff, "Chinese-made Xbox Controller", 0, XTYPE_XBOX },
  147. { 0x0000, 0x0000, "Generic X-Box pad", 0, XTYPE_UNKNOWN }
  148. };
  149. /* buttons shared with xbox and xbox360 */
  150. static const signed short xpad_common_btn[] = {
  151. BTN_A, BTN_B, BTN_X, BTN_Y, /* "analog" buttons */
  152. BTN_START, BTN_BACK, BTN_THUMBL, BTN_THUMBR, /* start/back/sticks */
  153. -1 /* terminating entry */
  154. };
  155. /* original xbox controllers only */
  156. static const signed short xpad_btn[] = {
  157. BTN_C, BTN_Z, /* "analog" buttons */
  158. -1 /* terminating entry */
  159. };
  160. /* used when dpad is mapped to nuttons */
  161. static const signed short xpad_btn_pad[] = {
  162. BTN_LEFT, BTN_RIGHT, /* d-pad left, right */
  163. BTN_0, BTN_1, /* d-pad up, down (XXX names??) */
  164. -1 /* terminating entry */
  165. };
  166. /* used when triggers are mapped to buttons */
  167. static const signed short xpad_btn_triggers[] = {
  168. BTN_TL2, BTN_TR2, /* triggers left/right */
  169. -1
  170. };
  171. static const signed short xpad360_btn[] = { /* buttons for x360 controller */
  172. BTN_TL, BTN_TR, /* Button LB/RB */
  173. BTN_MODE, /* The big X button */
  174. -1
  175. };
  176. static const signed short xpad_abs[] = {
  177. ABS_X, ABS_Y, /* left stick */
  178. ABS_RX, ABS_RY, /* right stick */
  179. -1 /* terminating entry */
  180. };
  181. /* used when dpad is mapped to axes */
  182. static const signed short xpad_abs_pad[] = {
  183. ABS_HAT0X, ABS_HAT0Y, /* d-pad axes */
  184. -1 /* terminating entry */
  185. };
  186. /* used when triggers are mapped to axes */
  187. static const signed short xpad_abs_triggers[] = {
  188. ABS_Z, ABS_RZ, /* triggers left/right */
  189. -1
  190. };
  191. /* Xbox 360 has a vendor-specific class, so we cannot match it with only
  192. * USB_INTERFACE_INFO (also specifically refused by USB subsystem), so we
  193. * match against vendor id as well. Wired Xbox 360 devices have protocol 1,
  194. * wireless controllers have protocol 129. */
  195. #define XPAD_XBOX360_VENDOR_PROTOCOL(vend,pr) \
  196. .match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_INT_INFO, \
  197. .idVendor = (vend), \
  198. .bInterfaceClass = USB_CLASS_VENDOR_SPEC, \
  199. .bInterfaceSubClass = 93, \
  200. .bInterfaceProtocol = (pr)
  201. #define XPAD_XBOX360_VENDOR(vend) \
  202. { XPAD_XBOX360_VENDOR_PROTOCOL(vend,1) }, \
  203. { XPAD_XBOX360_VENDOR_PROTOCOL(vend,129) }
  204. static struct usb_device_id xpad_table [] = {
  205. { USB_INTERFACE_INFO('X', 'B', 0) }, /* X-Box USB-IF not approved class */
  206. XPAD_XBOX360_VENDOR(0x045e), /* Microsoft X-Box 360 controllers */
  207. XPAD_XBOX360_VENDOR(0x046d), /* Logitech X-Box 360 style controllers */
  208. XPAD_XBOX360_VENDOR(0x0738), /* Mad Catz X-Box 360 controllers */
  209. XPAD_XBOX360_VENDOR(0x0e6f), /* 0x0e6f X-Box 360 controllers */
  210. XPAD_XBOX360_VENDOR(0x1430), /* RedOctane X-Box 360 controllers */
  211. XPAD_XBOX360_VENDOR(0x146b), /* BigBen Interactive Controllers */
  212. XPAD_XBOX360_VENDOR(0x1bad), /* Rock Band Drums */
  213. XPAD_XBOX360_VENDOR(0x0f0d), /* Hori Controllers */
  214. { }
  215. };
  216. MODULE_DEVICE_TABLE (usb, xpad_table);
  217. struct usb_xpad {
  218. struct input_dev *dev; /* input device interface */
  219. struct usb_device *udev; /* usb device */
  220. int pad_present;
  221. struct urb *irq_in; /* urb for interrupt in report */
  222. unsigned char *idata; /* input data */
  223. dma_addr_t idata_dma;
  224. struct urb *bulk_out;
  225. unsigned char *bdata;
  226. #if defined(CONFIG_JOYSTICK_XPAD_FF) || defined(CONFIG_JOYSTICK_XPAD_LEDS)
  227. struct urb *irq_out; /* urb for interrupt out report */
  228. unsigned char *odata; /* output data */
  229. dma_addr_t odata_dma;
  230. struct mutex odata_mutex;
  231. #endif
  232. #if defined(CONFIG_JOYSTICK_XPAD_LEDS)
  233. struct xpad_led *led;
  234. #endif
  235. char phys[64]; /* physical device path */
  236. int mapping; /* map d-pad to buttons or to axes */
  237. int xtype; /* type of xbox device */
  238. };
  239. /*
  240. * xpad_process_packet
  241. *
  242. * Completes a request by converting the data into events for the
  243. * input subsystem.
  244. *
  245. * The used report descriptor was taken from ITO Takayukis website:
  246. * http://euc.jp/periphs/xbox-controller.ja.html
  247. */
  248. static void xpad_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data)
  249. {
  250. struct input_dev *dev = xpad->dev;
  251. /* left stick */
  252. input_report_abs(dev, ABS_X,
  253. (__s16) le16_to_cpup((__le16 *)(data + 12)));
  254. input_report_abs(dev, ABS_Y,
  255. ~(__s16) le16_to_cpup((__le16 *)(data + 14)));
  256. /* right stick */
  257. input_report_abs(dev, ABS_RX,
  258. (__s16) le16_to_cpup((__le16 *)(data + 16)));
  259. input_report_abs(dev, ABS_RY,
  260. ~(__s16) le16_to_cpup((__le16 *)(data + 18)));
  261. /* triggers left/right */
  262. if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
  263. input_report_key(dev, BTN_TL2, data[10]);
  264. input_report_key(dev, BTN_TR2, data[11]);
  265. } else {
  266. input_report_abs(dev, ABS_Z, data[10]);
  267. input_report_abs(dev, ABS_RZ, data[11]);
  268. }
  269. /* digital pad */
  270. if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
  271. input_report_key(dev, BTN_LEFT, data[2] & 0x04);
  272. input_report_key(dev, BTN_RIGHT, data[2] & 0x08);
  273. input_report_key(dev, BTN_0, data[2] & 0x01); /* up */
  274. input_report_key(dev, BTN_1, data[2] & 0x02); /* down */
  275. } else {
  276. input_report_abs(dev, ABS_HAT0X,
  277. !!(data[2] & 0x08) - !!(data[2] & 0x04));
  278. input_report_abs(dev, ABS_HAT0Y,
  279. !!(data[2] & 0x02) - !!(data[2] & 0x01));
  280. }
  281. /* start/back buttons and stick press left/right */
  282. input_report_key(dev, BTN_START, data[2] & 0x10);
  283. input_report_key(dev, BTN_BACK, data[2] & 0x20);
  284. input_report_key(dev, BTN_THUMBL, data[2] & 0x40);
  285. input_report_key(dev, BTN_THUMBR, data[2] & 0x80);
  286. /* "analog" buttons A, B, X, Y */
  287. input_report_key(dev, BTN_A, data[4]);
  288. input_report_key(dev, BTN_B, data[5]);
  289. input_report_key(dev, BTN_X, data[6]);
  290. input_report_key(dev, BTN_Y, data[7]);
  291. /* "analog" buttons black, white */
  292. input_report_key(dev, BTN_C, data[8]);
  293. input_report_key(dev, BTN_Z, data[9]);
  294. input_sync(dev);
  295. }
  296. /*
  297. * xpad360_process_packet
  298. *
  299. * Completes a request by converting the data into events for the
  300. * input subsystem. It is version for xbox 360 controller
  301. *
  302. * The used report descriptor was taken from:
  303. * http://www.free60.org/wiki/Gamepad
  304. */
  305. static void xpad360_process_packet(struct usb_xpad *xpad,
  306. u16 cmd, unsigned char *data)
  307. {
  308. struct input_dev *dev = xpad->dev;
  309. /* digital pad */
  310. if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
  311. /* dpad as buttons (right, left, down, up) */
  312. input_report_key(dev, BTN_LEFT, data[2] & 0x04);
  313. input_report_key(dev, BTN_RIGHT, data[2] & 0x08);
  314. input_report_key(dev, BTN_0, data[2] & 0x01); /* up */
  315. input_report_key(dev, BTN_1, data[2] & 0x02); /* down */
  316. } else {
  317. input_report_abs(dev, ABS_HAT0X,
  318. !!(data[2] & 0x08) - !!(data[2] & 0x04));
  319. input_report_abs(dev, ABS_HAT0Y,
  320. !!(data[2] & 0x02) - !!(data[2] & 0x01));
  321. }
  322. /* start/back buttons */
  323. input_report_key(dev, BTN_START, data[2] & 0x10);
  324. input_report_key(dev, BTN_BACK, data[2] & 0x20);
  325. /* stick press left/right */
  326. input_report_key(dev, BTN_THUMBL, data[2] & 0x40);
  327. input_report_key(dev, BTN_THUMBR, data[2] & 0x80);
  328. /* buttons A,B,X,Y,TL,TR and MODE */
  329. input_report_key(dev, BTN_A, data[3] & 0x10);
  330. input_report_key(dev, BTN_B, data[3] & 0x20);
  331. input_report_key(dev, BTN_X, data[3] & 0x40);
  332. input_report_key(dev, BTN_Y, data[3] & 0x80);
  333. input_report_key(dev, BTN_TL, data[3] & 0x01);
  334. input_report_key(dev, BTN_TR, data[3] & 0x02);
  335. input_report_key(dev, BTN_MODE, data[3] & 0x04);
  336. /* left stick */
  337. input_report_abs(dev, ABS_X,
  338. (__s16) le16_to_cpup((__le16 *)(data + 6)));
  339. input_report_abs(dev, ABS_Y,
  340. ~(__s16) le16_to_cpup((__le16 *)(data + 8)));
  341. /* right stick */
  342. input_report_abs(dev, ABS_RX,
  343. (__s16) le16_to_cpup((__le16 *)(data + 10)));
  344. input_report_abs(dev, ABS_RY,
  345. ~(__s16) le16_to_cpup((__le16 *)(data + 12)));
  346. /* triggers left/right */
  347. if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
  348. input_report_key(dev, BTN_TL2, data[4]);
  349. input_report_key(dev, BTN_TR2, data[5]);
  350. } else {
  351. input_report_abs(dev, ABS_Z, data[4]);
  352. input_report_abs(dev, ABS_RZ, data[5]);
  353. }
  354. input_sync(dev);
  355. }
  356. /*
  357. * xpad360w_process_packet
  358. *
  359. * Completes a request by converting the data into events for the
  360. * input subsystem. It is version for xbox 360 wireless controller.
  361. *
  362. * Byte.Bit
  363. * 00.1 - Status change: The controller or headset has connected/disconnected
  364. * Bits 01.7 and 01.6 are valid
  365. * 01.7 - Controller present
  366. * 01.6 - Headset present
  367. * 01.1 - Pad state (Bytes 4+) valid
  368. *
  369. */
  370. static void xpad360w_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data)
  371. {
  372. /* Presence change */
  373. if (data[0] & 0x08) {
  374. if (data[1] & 0x80) {
  375. xpad->pad_present = 1;
  376. usb_submit_urb(xpad->bulk_out, GFP_ATOMIC);
  377. } else
  378. xpad->pad_present = 0;
  379. }
  380. /* Valid pad data */
  381. if (!(data[1] & 0x1))
  382. return;
  383. xpad360_process_packet(xpad, cmd, &data[4]);
  384. }
  385. static void xpad_irq_in(struct urb *urb)
  386. {
  387. struct usb_xpad *xpad = urb->context;
  388. int retval, status;
  389. status = urb->status;
  390. switch (status) {
  391. case 0:
  392. /* success */
  393. break;
  394. case -ECONNRESET:
  395. case -ENOENT:
  396. case -ESHUTDOWN:
  397. /* this urb is terminated, clean up */
  398. dbg("%s - urb shutting down with status: %d",
  399. __func__, status);
  400. return;
  401. default:
  402. dbg("%s - nonzero urb status received: %d",
  403. __func__, status);
  404. goto exit;
  405. }
  406. switch (xpad->xtype) {
  407. case XTYPE_XBOX360:
  408. xpad360_process_packet(xpad, 0, xpad->idata);
  409. break;
  410. case XTYPE_XBOX360W:
  411. xpad360w_process_packet(xpad, 0, xpad->idata);
  412. break;
  413. default:
  414. xpad_process_packet(xpad, 0, xpad->idata);
  415. }
  416. exit:
  417. retval = usb_submit_urb(urb, GFP_ATOMIC);
  418. if (retval)
  419. err ("%s - usb_submit_urb failed with result %d",
  420. __func__, retval);
  421. }
  422. static void xpad_bulk_out(struct urb *urb)
  423. {
  424. switch (urb->status) {
  425. case 0:
  426. /* success */
  427. break;
  428. case -ECONNRESET:
  429. case -ENOENT:
  430. case -ESHUTDOWN:
  431. /* this urb is terminated, clean up */
  432. dbg("%s - urb shutting down with status: %d", __func__, urb->status);
  433. break;
  434. default:
  435. dbg("%s - nonzero urb status received: %d", __func__, urb->status);
  436. }
  437. }
  438. #if defined(CONFIG_JOYSTICK_XPAD_FF) || defined(CONFIG_JOYSTICK_XPAD_LEDS)
  439. static void xpad_irq_out(struct urb *urb)
  440. {
  441. int retval, status;
  442. status = urb->status;
  443. switch (status) {
  444. case 0:
  445. /* success */
  446. return;
  447. case -ECONNRESET:
  448. case -ENOENT:
  449. case -ESHUTDOWN:
  450. /* this urb is terminated, clean up */
  451. dbg("%s - urb shutting down with status: %d", __func__, status);
  452. return;
  453. default:
  454. dbg("%s - nonzero urb status received: %d", __func__, status);
  455. goto exit;
  456. }
  457. exit:
  458. retval = usb_submit_urb(urb, GFP_ATOMIC);
  459. if (retval)
  460. err("%s - usb_submit_urb failed with result %d",
  461. __func__, retval);
  462. }
  463. static int xpad_init_output(struct usb_interface *intf, struct usb_xpad *xpad)
  464. {
  465. struct usb_endpoint_descriptor *ep_irq_out;
  466. int error = -ENOMEM;
  467. if (xpad->xtype != XTYPE_XBOX360 && xpad->xtype != XTYPE_XBOX)
  468. return 0;
  469. xpad->odata = usb_buffer_alloc(xpad->udev, XPAD_PKT_LEN,
  470. GFP_KERNEL, &xpad->odata_dma);
  471. if (!xpad->odata)
  472. goto fail1;
  473. mutex_init(&xpad->odata_mutex);
  474. xpad->irq_out = usb_alloc_urb(0, GFP_KERNEL);
  475. if (!xpad->irq_out)
  476. goto fail2;
  477. ep_irq_out = &intf->cur_altsetting->endpoint[1].desc;
  478. usb_fill_int_urb(xpad->irq_out, xpad->udev,
  479. usb_sndintpipe(xpad->udev, ep_irq_out->bEndpointAddress),
  480. xpad->odata, XPAD_PKT_LEN,
  481. xpad_irq_out, xpad, ep_irq_out->bInterval);
  482. xpad->irq_out->transfer_dma = xpad->odata_dma;
  483. xpad->irq_out->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  484. return 0;
  485. fail2: usb_buffer_free(xpad->udev, XPAD_PKT_LEN, xpad->odata, xpad->odata_dma);
  486. fail1: return error;
  487. }
  488. static void xpad_stop_output(struct usb_xpad *xpad)
  489. {
  490. if (xpad->xtype == XTYPE_XBOX360 || xpad->xtype == XTYPE_XBOX)
  491. usb_kill_urb(xpad->irq_out);
  492. }
  493. static void xpad_deinit_output(struct usb_xpad *xpad)
  494. {
  495. if (xpad->xtype == XTYPE_XBOX360 || xpad->xtype == XTYPE_XBOX) {
  496. usb_free_urb(xpad->irq_out);
  497. usb_buffer_free(xpad->udev, XPAD_PKT_LEN,
  498. xpad->odata, xpad->odata_dma);
  499. }
  500. }
  501. #else
  502. static int xpad_init_output(struct usb_interface *intf, struct usb_xpad *xpad) { return 0; }
  503. static void xpad_deinit_output(struct usb_xpad *xpad) {}
  504. static void xpad_stop_output(struct usb_xpad *xpad) {}
  505. #endif
  506. #ifdef CONFIG_JOYSTICK_XPAD_FF
  507. static int xpad_play_effect(struct input_dev *dev, void *data, struct ff_effect *effect)
  508. {
  509. struct usb_xpad *xpad = input_get_drvdata(dev);
  510. if (effect->type == FF_RUMBLE) {
  511. __u16 strong = effect->u.rumble.strong_magnitude;
  512. __u16 weak = effect->u.rumble.weak_magnitude;
  513. switch (xpad->xtype) {
  514. case XTYPE_XBOX:
  515. xpad->odata[0] = 0x00;
  516. xpad->odata[1] = 0x06;
  517. xpad->odata[2] = 0x00;
  518. xpad->odata[3] = strong / 256; /* left actuator */
  519. xpad->odata[4] = 0x00;
  520. xpad->odata[5] = weak / 256; /* right actuator */
  521. xpad->irq_out->transfer_buffer_length = 6;
  522. return usb_submit_urb(xpad->irq_out, GFP_ATOMIC);
  523. case XTYPE_XBOX360:
  524. xpad->odata[0] = 0x00;
  525. xpad->odata[1] = 0x08;
  526. xpad->odata[2] = 0x00;
  527. xpad->odata[3] = strong / 256; /* left actuator? */
  528. xpad->odata[4] = weak / 256; /* right actuator? */
  529. xpad->odata[5] = 0x00;
  530. xpad->odata[6] = 0x00;
  531. xpad->odata[7] = 0x00;
  532. xpad->irq_out->transfer_buffer_length = 8;
  533. return usb_submit_urb(xpad->irq_out, GFP_ATOMIC);
  534. default:
  535. dbg("%s - rumble command sent to unsupported xpad type: %d",
  536. __func__, xpad->xtype);
  537. return -1;
  538. }
  539. }
  540. return 0;
  541. }
  542. static int xpad_init_ff(struct usb_xpad *xpad)
  543. {
  544. if (xpad->xtype != XTYPE_XBOX360 && xpad->xtype != XTYPE_XBOX)
  545. return 0;
  546. input_set_capability(xpad->dev, EV_FF, FF_RUMBLE);
  547. return input_ff_create_memless(xpad->dev, NULL, xpad_play_effect);
  548. }
  549. #else
  550. static int xpad_init_ff(struct usb_xpad *xpad) { return 0; }
  551. #endif
  552. #if defined(CONFIG_JOYSTICK_XPAD_LEDS)
  553. #include <linux/leds.h>
  554. struct xpad_led {
  555. char name[16];
  556. struct led_classdev led_cdev;
  557. struct usb_xpad *xpad;
  558. };
  559. static void xpad_send_led_command(struct usb_xpad *xpad, int command)
  560. {
  561. if (command >= 0 && command < 14) {
  562. mutex_lock(&xpad->odata_mutex);
  563. xpad->odata[0] = 0x01;
  564. xpad->odata[1] = 0x03;
  565. xpad->odata[2] = command;
  566. xpad->irq_out->transfer_buffer_length = 3;
  567. usb_submit_urb(xpad->irq_out, GFP_KERNEL);
  568. mutex_unlock(&xpad->odata_mutex);
  569. }
  570. }
  571. static void xpad_led_set(struct led_classdev *led_cdev,
  572. enum led_brightness value)
  573. {
  574. struct xpad_led *xpad_led = container_of(led_cdev,
  575. struct xpad_led, led_cdev);
  576. xpad_send_led_command(xpad_led->xpad, value);
  577. }
  578. static int xpad_led_probe(struct usb_xpad *xpad)
  579. {
  580. static atomic_t led_seq = ATOMIC_INIT(0);
  581. long led_no;
  582. struct xpad_led *led;
  583. struct led_classdev *led_cdev;
  584. int error;
  585. if (xpad->xtype != XTYPE_XBOX360)
  586. return 0;
  587. xpad->led = led = kzalloc(sizeof(struct xpad_led), GFP_KERNEL);
  588. if (!led)
  589. return -ENOMEM;
  590. led_no = (long)atomic_inc_return(&led_seq) - 1;
  591. snprintf(led->name, sizeof(led->name), "xpad%ld", led_no);
  592. led->xpad = xpad;
  593. led_cdev = &led->led_cdev;
  594. led_cdev->name = led->name;
  595. led_cdev->brightness_set = xpad_led_set;
  596. error = led_classdev_register(&xpad->udev->dev, led_cdev);
  597. if (error) {
  598. kfree(led);
  599. xpad->led = NULL;
  600. return error;
  601. }
  602. /*
  603. * Light up the segment corresponding to controller number
  604. */
  605. xpad_send_led_command(xpad, (led_no % 4) + 2);
  606. return 0;
  607. }
  608. static void xpad_led_disconnect(struct usb_xpad *xpad)
  609. {
  610. struct xpad_led *xpad_led = xpad->led;
  611. if (xpad_led) {
  612. led_classdev_unregister(&xpad_led->led_cdev);
  613. kfree(xpad_led->name);
  614. }
  615. }
  616. #else
  617. static int xpad_led_probe(struct usb_xpad *xpad) { return 0; }
  618. static void xpad_led_disconnect(struct usb_xpad *xpad) { }
  619. #endif
  620. static int xpad_open(struct input_dev *dev)
  621. {
  622. struct usb_xpad *xpad = input_get_drvdata(dev);
  623. /* URB was submitted in probe */
  624. if(xpad->xtype == XTYPE_XBOX360W)
  625. return 0;
  626. xpad->irq_in->dev = xpad->udev;
  627. if (usb_submit_urb(xpad->irq_in, GFP_KERNEL))
  628. return -EIO;
  629. return 0;
  630. }
  631. static void xpad_close(struct input_dev *dev)
  632. {
  633. struct usb_xpad *xpad = input_get_drvdata(dev);
  634. if(xpad->xtype != XTYPE_XBOX360W)
  635. usb_kill_urb(xpad->irq_in);
  636. xpad_stop_output(xpad);
  637. }
  638. static void xpad_set_up_abs(struct input_dev *input_dev, signed short abs)
  639. {
  640. set_bit(abs, input_dev->absbit);
  641. switch (abs) {
  642. case ABS_X:
  643. case ABS_Y:
  644. case ABS_RX:
  645. case ABS_RY: /* the two sticks */
  646. input_set_abs_params(input_dev, abs, -32768, 32767, 16, 128);
  647. break;
  648. case ABS_Z:
  649. case ABS_RZ: /* the triggers (if mapped to axes) */
  650. input_set_abs_params(input_dev, abs, 0, 255, 0, 0);
  651. break;
  652. case ABS_HAT0X:
  653. case ABS_HAT0Y: /* the d-pad (only if dpad is mapped to axes */
  654. input_set_abs_params(input_dev, abs, -1, 1, 0, 0);
  655. break;
  656. }
  657. }
  658. static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id)
  659. {
  660. struct usb_device *udev = interface_to_usbdev(intf);
  661. struct usb_xpad *xpad;
  662. struct input_dev *input_dev;
  663. struct usb_endpoint_descriptor *ep_irq_in;
  664. int i;
  665. int error = -ENOMEM;
  666. for (i = 0; xpad_device[i].idVendor; i++) {
  667. if ((le16_to_cpu(udev->descriptor.idVendor) == xpad_device[i].idVendor) &&
  668. (le16_to_cpu(udev->descriptor.idProduct) == xpad_device[i].idProduct))
  669. break;
  670. }
  671. xpad = kzalloc(sizeof(struct usb_xpad), GFP_KERNEL);
  672. input_dev = input_allocate_device();
  673. if (!xpad || !input_dev)
  674. goto fail1;
  675. xpad->idata = usb_buffer_alloc(udev, XPAD_PKT_LEN,
  676. GFP_KERNEL, &xpad->idata_dma);
  677. if (!xpad->idata)
  678. goto fail1;
  679. xpad->irq_in = usb_alloc_urb(0, GFP_KERNEL);
  680. if (!xpad->irq_in)
  681. goto fail2;
  682. xpad->udev = udev;
  683. xpad->mapping = xpad_device[i].mapping;
  684. xpad->xtype = xpad_device[i].xtype;
  685. if (xpad->xtype == XTYPE_UNKNOWN) {
  686. if (intf->cur_altsetting->desc.bInterfaceClass == USB_CLASS_VENDOR_SPEC) {
  687. if (intf->cur_altsetting->desc.bInterfaceProtocol == 129)
  688. xpad->xtype = XTYPE_XBOX360W;
  689. else
  690. xpad->xtype = XTYPE_XBOX360;
  691. } else
  692. xpad->xtype = XTYPE_XBOX;
  693. if (dpad_to_buttons)
  694. xpad->mapping |= MAP_DPAD_TO_BUTTONS;
  695. if (triggers_to_buttons)
  696. xpad->mapping |= MAP_TRIGGERS_TO_BUTTONS;
  697. }
  698. xpad->dev = input_dev;
  699. usb_make_path(udev, xpad->phys, sizeof(xpad->phys));
  700. strlcat(xpad->phys, "/input0", sizeof(xpad->phys));
  701. input_dev->name = xpad_device[i].name;
  702. input_dev->phys = xpad->phys;
  703. usb_to_input_id(udev, &input_dev->id);
  704. input_dev->dev.parent = &intf->dev;
  705. input_set_drvdata(input_dev, xpad);
  706. input_dev->open = xpad_open;
  707. input_dev->close = xpad_close;
  708. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  709. /* set up standard buttons and axes */
  710. for (i = 0; xpad_common_btn[i] >= 0; i++)
  711. __set_bit(xpad_common_btn[i], input_dev->keybit);
  712. for (i = 0; xpad_abs[i] >= 0; i++)
  713. xpad_set_up_abs(input_dev, xpad_abs[i]);
  714. /* Now set up model-specific ones */
  715. if (xpad->xtype == XTYPE_XBOX360 || xpad->xtype == XTYPE_XBOX360W) {
  716. for (i = 0; xpad360_btn[i] >= 0; i++)
  717. __set_bit(xpad360_btn[i], input_dev->keybit);
  718. } else {
  719. for (i = 0; xpad_btn[i] >= 0; i++)
  720. __set_bit(xpad_btn[i], input_dev->keybit);
  721. }
  722. if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
  723. for (i = 0; xpad_btn_pad[i] >= 0; i++)
  724. __set_bit(xpad_btn_pad[i], input_dev->keybit);
  725. } else {
  726. for (i = 0; xpad_abs_pad[i] >= 0; i++)
  727. xpad_set_up_abs(input_dev, xpad_abs_pad[i]);
  728. }
  729. if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
  730. for (i = 0; xpad_btn_triggers[i] >= 0; i++)
  731. __set_bit(xpad_btn_triggers[i], input_dev->keybit);
  732. } else {
  733. for (i = 0; xpad_abs_triggers[i] >= 0; i++)
  734. xpad_set_up_abs(input_dev, xpad_abs_triggers[i]);
  735. }
  736. error = xpad_init_output(intf, xpad);
  737. if (error)
  738. goto fail2;
  739. error = xpad_init_ff(xpad);
  740. if (error)
  741. goto fail3;
  742. error = xpad_led_probe(xpad);
  743. if (error)
  744. goto fail3;
  745. ep_irq_in = &intf->cur_altsetting->endpoint[0].desc;
  746. usb_fill_int_urb(xpad->irq_in, udev,
  747. usb_rcvintpipe(udev, ep_irq_in->bEndpointAddress),
  748. xpad->idata, XPAD_PKT_LEN, xpad_irq_in,
  749. xpad, ep_irq_in->bInterval);
  750. xpad->irq_in->transfer_dma = xpad->idata_dma;
  751. xpad->irq_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  752. error = input_register_device(xpad->dev);
  753. if (error)
  754. goto fail4;
  755. usb_set_intfdata(intf, xpad);
  756. /*
  757. * Submit the int URB immediatly rather than waiting for open
  758. * because we get status messages from the device whether
  759. * or not any controllers are attached. In fact, it's
  760. * exactly the message that a controller has arrived that
  761. * we're waiting for.
  762. */
  763. if (xpad->xtype == XTYPE_XBOX360W) {
  764. xpad->irq_in->dev = xpad->udev;
  765. error = usb_submit_urb(xpad->irq_in, GFP_KERNEL);
  766. if (error)
  767. goto fail4;
  768. /*
  769. * Setup the message to set the LEDs on the
  770. * controller when it shows up
  771. */
  772. xpad->bulk_out = usb_alloc_urb(0, GFP_KERNEL);
  773. if(!xpad->bulk_out)
  774. goto fail5;
  775. xpad->bdata = kzalloc(XPAD_PKT_LEN, GFP_KERNEL);
  776. if(!xpad->bdata)
  777. goto fail6;
  778. xpad->bdata[2] = 0x08;
  779. switch (intf->cur_altsetting->desc.bInterfaceNumber) {
  780. case 0:
  781. xpad->bdata[3] = 0x42;
  782. break;
  783. case 2:
  784. xpad->bdata[3] = 0x43;
  785. break;
  786. case 4:
  787. xpad->bdata[3] = 0x44;
  788. break;
  789. case 6:
  790. xpad->bdata[3] = 0x45;
  791. }
  792. ep_irq_in = &intf->cur_altsetting->endpoint[1].desc;
  793. usb_fill_bulk_urb(xpad->bulk_out, udev,
  794. usb_sndbulkpipe(udev, ep_irq_in->bEndpointAddress),
  795. xpad->bdata, XPAD_PKT_LEN, xpad_bulk_out, xpad);
  796. }
  797. return 0;
  798. fail6: usb_free_urb(xpad->bulk_out);
  799. fail5: usb_kill_urb(xpad->irq_in);
  800. fail4: usb_free_urb(xpad->irq_in);
  801. fail3: xpad_deinit_output(xpad);
  802. fail2: usb_buffer_free(udev, XPAD_PKT_LEN, xpad->idata, xpad->idata_dma);
  803. fail1: input_free_device(input_dev);
  804. kfree(xpad);
  805. return error;
  806. }
  807. static void xpad_disconnect(struct usb_interface *intf)
  808. {
  809. struct usb_xpad *xpad = usb_get_intfdata (intf);
  810. usb_set_intfdata(intf, NULL);
  811. if (xpad) {
  812. xpad_led_disconnect(xpad);
  813. input_unregister_device(xpad->dev);
  814. xpad_deinit_output(xpad);
  815. if (xpad->xtype == XTYPE_XBOX360W) {
  816. usb_kill_urb(xpad->bulk_out);
  817. usb_free_urb(xpad->bulk_out);
  818. usb_kill_urb(xpad->irq_in);
  819. }
  820. usb_free_urb(xpad->irq_in);
  821. usb_buffer_free(xpad->udev, XPAD_PKT_LEN,
  822. xpad->idata, xpad->idata_dma);
  823. kfree(xpad);
  824. }
  825. }
  826. static struct usb_driver xpad_driver = {
  827. .name = "xpad",
  828. .probe = xpad_probe,
  829. .disconnect = xpad_disconnect,
  830. .id_table = xpad_table,
  831. };
  832. static int __init usb_xpad_init(void)
  833. {
  834. int result = usb_register(&xpad_driver);
  835. if (result == 0)
  836. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
  837. return result;
  838. }
  839. static void __exit usb_xpad_exit(void)
  840. {
  841. usb_deregister(&xpad_driver);
  842. }
  843. module_init(usb_xpad_init);
  844. module_exit(usb_xpad_exit);
  845. MODULE_AUTHOR(DRIVER_AUTHOR);
  846. MODULE_DESCRIPTION(DRIVER_DESC);
  847. MODULE_LICENSE("GPL");