xpad.c 30 KB

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