xpad.c 30 KB

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