xpad.c 30 KB

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