xpad.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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 = -ENOMEM;
  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. goto fail1;
  487. mutex_init(&xpad->odata_mutex);
  488. xpad->irq_out = usb_alloc_urb(0, GFP_KERNEL);
  489. if (!xpad->irq_out)
  490. goto fail2;
  491. ep_irq_out = &intf->cur_altsetting->endpoint[1].desc;
  492. usb_fill_int_urb(xpad->irq_out, xpad->udev,
  493. usb_sndintpipe(xpad->udev, ep_irq_out->bEndpointAddress),
  494. xpad->odata, XPAD_PKT_LEN,
  495. xpad_irq_out, xpad, ep_irq_out->bInterval);
  496. xpad->irq_out->transfer_dma = xpad->odata_dma;
  497. xpad->irq_out->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  498. return 0;
  499. fail2: usb_free_coherent(xpad->udev, XPAD_PKT_LEN, xpad->odata, xpad->odata_dma);
  500. fail1: return error;
  501. }
  502. static void xpad_stop_output(struct usb_xpad *xpad)
  503. {
  504. if (xpad->xtype == XTYPE_XBOX360 || xpad->xtype == XTYPE_XBOX)
  505. usb_kill_urb(xpad->irq_out);
  506. }
  507. static void xpad_deinit_output(struct usb_xpad *xpad)
  508. {
  509. if (xpad->xtype == XTYPE_XBOX360 || xpad->xtype == XTYPE_XBOX) {
  510. usb_free_urb(xpad->irq_out);
  511. usb_free_coherent(xpad->udev, XPAD_PKT_LEN,
  512. xpad->odata, xpad->odata_dma);
  513. }
  514. }
  515. #else
  516. static int xpad_init_output(struct usb_interface *intf, struct usb_xpad *xpad) { return 0; }
  517. static void xpad_deinit_output(struct usb_xpad *xpad) {}
  518. static void xpad_stop_output(struct usb_xpad *xpad) {}
  519. #endif
  520. #ifdef CONFIG_JOYSTICK_XPAD_FF
  521. static int xpad_play_effect(struct input_dev *dev, void *data, struct ff_effect *effect)
  522. {
  523. struct usb_xpad *xpad = input_get_drvdata(dev);
  524. if (effect->type == FF_RUMBLE) {
  525. __u16 strong = effect->u.rumble.strong_magnitude;
  526. __u16 weak = effect->u.rumble.weak_magnitude;
  527. switch (xpad->xtype) {
  528. case XTYPE_XBOX:
  529. xpad->odata[0] = 0x00;
  530. xpad->odata[1] = 0x06;
  531. xpad->odata[2] = 0x00;
  532. xpad->odata[3] = strong / 256; /* left actuator */
  533. xpad->odata[4] = 0x00;
  534. xpad->odata[5] = weak / 256; /* right actuator */
  535. xpad->irq_out->transfer_buffer_length = 6;
  536. return usb_submit_urb(xpad->irq_out, GFP_ATOMIC);
  537. case XTYPE_XBOX360:
  538. xpad->odata[0] = 0x00;
  539. xpad->odata[1] = 0x08;
  540. xpad->odata[2] = 0x00;
  541. xpad->odata[3] = strong / 256; /* left actuator? */
  542. xpad->odata[4] = weak / 256; /* right actuator? */
  543. xpad->odata[5] = 0x00;
  544. xpad->odata[6] = 0x00;
  545. xpad->odata[7] = 0x00;
  546. xpad->irq_out->transfer_buffer_length = 8;
  547. return usb_submit_urb(xpad->irq_out, GFP_ATOMIC);
  548. default:
  549. dbg("%s - rumble command sent to unsupported xpad type: %d",
  550. __func__, xpad->xtype);
  551. return -1;
  552. }
  553. }
  554. return 0;
  555. }
  556. static int xpad_init_ff(struct usb_xpad *xpad)
  557. {
  558. if (xpad->xtype != XTYPE_XBOX360 && xpad->xtype != XTYPE_XBOX)
  559. return 0;
  560. input_set_capability(xpad->dev, EV_FF, FF_RUMBLE);
  561. return input_ff_create_memless(xpad->dev, NULL, xpad_play_effect);
  562. }
  563. #else
  564. static int xpad_init_ff(struct usb_xpad *xpad) { return 0; }
  565. #endif
  566. #if defined(CONFIG_JOYSTICK_XPAD_LEDS)
  567. #include <linux/leds.h>
  568. struct xpad_led {
  569. char name[16];
  570. struct led_classdev led_cdev;
  571. struct usb_xpad *xpad;
  572. };
  573. static void xpad_send_led_command(struct usb_xpad *xpad, int command)
  574. {
  575. if (command >= 0 && command < 14) {
  576. mutex_lock(&xpad->odata_mutex);
  577. xpad->odata[0] = 0x01;
  578. xpad->odata[1] = 0x03;
  579. xpad->odata[2] = command;
  580. xpad->irq_out->transfer_buffer_length = 3;
  581. usb_submit_urb(xpad->irq_out, GFP_KERNEL);
  582. mutex_unlock(&xpad->odata_mutex);
  583. }
  584. }
  585. static void xpad_led_set(struct led_classdev *led_cdev,
  586. enum led_brightness value)
  587. {
  588. struct xpad_led *xpad_led = container_of(led_cdev,
  589. struct xpad_led, led_cdev);
  590. xpad_send_led_command(xpad_led->xpad, value);
  591. }
  592. static int xpad_led_probe(struct usb_xpad *xpad)
  593. {
  594. static atomic_t led_seq = ATOMIC_INIT(0);
  595. long led_no;
  596. struct xpad_led *led;
  597. struct led_classdev *led_cdev;
  598. int error;
  599. if (xpad->xtype != XTYPE_XBOX360)
  600. return 0;
  601. xpad->led = led = kzalloc(sizeof(struct xpad_led), GFP_KERNEL);
  602. if (!led)
  603. return -ENOMEM;
  604. led_no = (long)atomic_inc_return(&led_seq) - 1;
  605. snprintf(led->name, sizeof(led->name), "xpad%ld", led_no);
  606. led->xpad = xpad;
  607. led_cdev = &led->led_cdev;
  608. led_cdev->name = led->name;
  609. led_cdev->brightness_set = xpad_led_set;
  610. error = led_classdev_register(&xpad->udev->dev, led_cdev);
  611. if (error) {
  612. kfree(led);
  613. xpad->led = NULL;
  614. return error;
  615. }
  616. /*
  617. * Light up the segment corresponding to controller number
  618. */
  619. xpad_send_led_command(xpad, (led_no % 4) + 2);
  620. return 0;
  621. }
  622. static void xpad_led_disconnect(struct usb_xpad *xpad)
  623. {
  624. struct xpad_led *xpad_led = xpad->led;
  625. if (xpad_led) {
  626. led_classdev_unregister(&xpad_led->led_cdev);
  627. kfree(xpad_led->name);
  628. }
  629. }
  630. #else
  631. static int xpad_led_probe(struct usb_xpad *xpad) { return 0; }
  632. static void xpad_led_disconnect(struct usb_xpad *xpad) { }
  633. #endif
  634. static int xpad_open(struct input_dev *dev)
  635. {
  636. struct usb_xpad *xpad = input_get_drvdata(dev);
  637. /* URB was submitted in probe */
  638. if(xpad->xtype == XTYPE_XBOX360W)
  639. return 0;
  640. xpad->irq_in->dev = xpad->udev;
  641. if (usb_submit_urb(xpad->irq_in, GFP_KERNEL))
  642. return -EIO;
  643. return 0;
  644. }
  645. static void xpad_close(struct input_dev *dev)
  646. {
  647. struct usb_xpad *xpad = input_get_drvdata(dev);
  648. if(xpad->xtype != XTYPE_XBOX360W)
  649. usb_kill_urb(xpad->irq_in);
  650. xpad_stop_output(xpad);
  651. }
  652. static void xpad_set_up_abs(struct input_dev *input_dev, signed short abs)
  653. {
  654. set_bit(abs, input_dev->absbit);
  655. switch (abs) {
  656. case ABS_X:
  657. case ABS_Y:
  658. case ABS_RX:
  659. case ABS_RY: /* the two sticks */
  660. input_set_abs_params(input_dev, abs, -32768, 32767, 16, 128);
  661. break;
  662. case ABS_Z:
  663. case ABS_RZ: /* the triggers (if mapped to axes) */
  664. input_set_abs_params(input_dev, abs, 0, 255, 0, 0);
  665. break;
  666. case ABS_HAT0X:
  667. case ABS_HAT0Y: /* the d-pad (only if dpad is mapped to axes */
  668. input_set_abs_params(input_dev, abs, -1, 1, 0, 0);
  669. break;
  670. }
  671. }
  672. static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id)
  673. {
  674. struct usb_device *udev = interface_to_usbdev(intf);
  675. struct usb_xpad *xpad;
  676. struct input_dev *input_dev;
  677. struct usb_endpoint_descriptor *ep_irq_in;
  678. int i;
  679. int error = -ENOMEM;
  680. for (i = 0; xpad_device[i].idVendor; i++) {
  681. if ((le16_to_cpu(udev->descriptor.idVendor) == xpad_device[i].idVendor) &&
  682. (le16_to_cpu(udev->descriptor.idProduct) == xpad_device[i].idProduct))
  683. break;
  684. }
  685. xpad = kzalloc(sizeof(struct usb_xpad), GFP_KERNEL);
  686. input_dev = input_allocate_device();
  687. if (!xpad || !input_dev)
  688. goto fail1;
  689. xpad->idata = usb_alloc_coherent(udev, XPAD_PKT_LEN,
  690. GFP_KERNEL, &xpad->idata_dma);
  691. if (!xpad->idata)
  692. goto fail1;
  693. xpad->irq_in = usb_alloc_urb(0, GFP_KERNEL);
  694. if (!xpad->irq_in)
  695. goto fail2;
  696. xpad->udev = udev;
  697. xpad->mapping = xpad_device[i].mapping;
  698. xpad->xtype = xpad_device[i].xtype;
  699. if (xpad->xtype == XTYPE_UNKNOWN) {
  700. if (intf->cur_altsetting->desc.bInterfaceClass == USB_CLASS_VENDOR_SPEC) {
  701. if (intf->cur_altsetting->desc.bInterfaceProtocol == 129)
  702. xpad->xtype = XTYPE_XBOX360W;
  703. else
  704. xpad->xtype = XTYPE_XBOX360;
  705. } else
  706. xpad->xtype = XTYPE_XBOX;
  707. if (dpad_to_buttons)
  708. xpad->mapping |= MAP_DPAD_TO_BUTTONS;
  709. if (triggers_to_buttons)
  710. xpad->mapping |= MAP_TRIGGERS_TO_BUTTONS;
  711. if (sticks_to_null)
  712. xpad->mapping |= MAP_STICKS_TO_NULL;
  713. }
  714. xpad->dev = input_dev;
  715. usb_make_path(udev, xpad->phys, sizeof(xpad->phys));
  716. strlcat(xpad->phys, "/input0", sizeof(xpad->phys));
  717. input_dev->name = xpad_device[i].name;
  718. input_dev->phys = xpad->phys;
  719. usb_to_input_id(udev, &input_dev->id);
  720. input_dev->dev.parent = &intf->dev;
  721. input_set_drvdata(input_dev, xpad);
  722. input_dev->open = xpad_open;
  723. input_dev->close = xpad_close;
  724. input_dev->evbit[0] = BIT_MASK(EV_KEY);
  725. if (!(xpad->mapping & MAP_STICKS_TO_NULL)) {
  726. input_dev->evbit[0] |= BIT_MASK(EV_ABS);
  727. /* set up axes */
  728. for (i = 0; xpad_abs[i] >= 0; i++)
  729. xpad_set_up_abs(input_dev, xpad_abs[i]);
  730. }
  731. /* set up standard buttons */
  732. for (i = 0; xpad_common_btn[i] >= 0; i++)
  733. __set_bit(xpad_common_btn[i], input_dev->keybit);
  734. /* set up model-specific ones */
  735. if (xpad->xtype == XTYPE_XBOX360 || xpad->xtype == XTYPE_XBOX360W) {
  736. for (i = 0; xpad360_btn[i] >= 0; i++)
  737. __set_bit(xpad360_btn[i], input_dev->keybit);
  738. } else {
  739. for (i = 0; xpad_btn[i] >= 0; i++)
  740. __set_bit(xpad_btn[i], input_dev->keybit);
  741. }
  742. if (xpad->mapping & MAP_DPAD_TO_BUTTONS) {
  743. for (i = 0; xpad_btn_pad[i] >= 0; i++)
  744. __set_bit(xpad_btn_pad[i], input_dev->keybit);
  745. } else {
  746. for (i = 0; xpad_abs_pad[i] >= 0; i++)
  747. xpad_set_up_abs(input_dev, xpad_abs_pad[i]);
  748. }
  749. if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) {
  750. for (i = 0; xpad_btn_triggers[i] >= 0; i++)
  751. __set_bit(xpad_btn_triggers[i], input_dev->keybit);
  752. } else {
  753. for (i = 0; xpad_abs_triggers[i] >= 0; i++)
  754. xpad_set_up_abs(input_dev, xpad_abs_triggers[i]);
  755. }
  756. error = xpad_init_output(intf, xpad);
  757. if (error)
  758. goto fail2;
  759. error = xpad_init_ff(xpad);
  760. if (error)
  761. goto fail3;
  762. error = xpad_led_probe(xpad);
  763. if (error)
  764. goto fail3;
  765. ep_irq_in = &intf->cur_altsetting->endpoint[0].desc;
  766. usb_fill_int_urb(xpad->irq_in, udev,
  767. usb_rcvintpipe(udev, ep_irq_in->bEndpointAddress),
  768. xpad->idata, XPAD_PKT_LEN, xpad_irq_in,
  769. xpad, ep_irq_in->bInterval);
  770. xpad->irq_in->transfer_dma = xpad->idata_dma;
  771. xpad->irq_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  772. error = input_register_device(xpad->dev);
  773. if (error)
  774. goto fail4;
  775. usb_set_intfdata(intf, xpad);
  776. /*
  777. * Submit the int URB immediatly rather than waiting for open
  778. * because we get status messages from the device whether
  779. * or not any controllers are attached. In fact, it's
  780. * exactly the message that a controller has arrived that
  781. * we're waiting for.
  782. */
  783. if (xpad->xtype == XTYPE_XBOX360W) {
  784. xpad->irq_in->dev = xpad->udev;
  785. error = usb_submit_urb(xpad->irq_in, GFP_KERNEL);
  786. if (error)
  787. goto fail4;
  788. /*
  789. * Setup the message to set the LEDs on the
  790. * controller when it shows up
  791. */
  792. xpad->bulk_out = usb_alloc_urb(0, GFP_KERNEL);
  793. if(!xpad->bulk_out)
  794. goto fail5;
  795. xpad->bdata = kzalloc(XPAD_PKT_LEN, GFP_KERNEL);
  796. if(!xpad->bdata)
  797. goto fail6;
  798. xpad->bdata[2] = 0x08;
  799. switch (intf->cur_altsetting->desc.bInterfaceNumber) {
  800. case 0:
  801. xpad->bdata[3] = 0x42;
  802. break;
  803. case 2:
  804. xpad->bdata[3] = 0x43;
  805. break;
  806. case 4:
  807. xpad->bdata[3] = 0x44;
  808. break;
  809. case 6:
  810. xpad->bdata[3] = 0x45;
  811. }
  812. ep_irq_in = &intf->cur_altsetting->endpoint[1].desc;
  813. usb_fill_bulk_urb(xpad->bulk_out, udev,
  814. usb_sndbulkpipe(udev, ep_irq_in->bEndpointAddress),
  815. xpad->bdata, XPAD_PKT_LEN, xpad_bulk_out, xpad);
  816. }
  817. return 0;
  818. fail6: usb_free_urb(xpad->bulk_out);
  819. fail5: usb_kill_urb(xpad->irq_in);
  820. fail4: usb_free_urb(xpad->irq_in);
  821. fail3: xpad_deinit_output(xpad);
  822. fail2: usb_free_coherent(udev, XPAD_PKT_LEN, xpad->idata, xpad->idata_dma);
  823. fail1: input_free_device(input_dev);
  824. kfree(xpad);
  825. return error;
  826. }
  827. static void xpad_disconnect(struct usb_interface *intf)
  828. {
  829. struct usb_xpad *xpad = usb_get_intfdata (intf);
  830. usb_set_intfdata(intf, NULL);
  831. if (xpad) {
  832. xpad_led_disconnect(xpad);
  833. input_unregister_device(xpad->dev);
  834. xpad_deinit_output(xpad);
  835. if (xpad->xtype == XTYPE_XBOX360W) {
  836. usb_kill_urb(xpad->bulk_out);
  837. usb_free_urb(xpad->bulk_out);
  838. usb_kill_urb(xpad->irq_in);
  839. }
  840. usb_free_urb(xpad->irq_in);
  841. usb_free_coherent(xpad->udev, XPAD_PKT_LEN,
  842. xpad->idata, xpad->idata_dma);
  843. kfree(xpad);
  844. }
  845. }
  846. static struct usb_driver xpad_driver = {
  847. .name = "xpad",
  848. .probe = xpad_probe,
  849. .disconnect = xpad_disconnect,
  850. .id_table = xpad_table,
  851. };
  852. static int __init usb_xpad_init(void)
  853. {
  854. int result = usb_register(&xpad_driver);
  855. if (result == 0)
  856. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
  857. return result;
  858. }
  859. static void __exit usb_xpad_exit(void)
  860. {
  861. usb_deregister(&xpad_driver);
  862. }
  863. module_init(usb_xpad_init);
  864. module_exit(usb_xpad_exit);
  865. MODULE_AUTHOR(DRIVER_AUTHOR);
  866. MODULE_DESCRIPTION(DRIVER_DESC);
  867. MODULE_LICENSE("GPL");