Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
bf8cb314 | 2 | * X-Box gamepad driver |
1da177e4 LT |
3 | * |
4 | * Copyright (c) 2002 Marko Friedemann <mfr@bmx-chemnitz.de> | |
d518b2b4 DC |
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> | |
deb8ee43 DC |
9 | * 2005 Dominic Cerquetti <binary1230@yahoo.com> |
10 | * 2006 Adam Buchbinder <adam.buchbinder@gmail.com> | |
c7d9f7eb | 11 | * 2007 Jan Kratochvil <honza@jikos.cz> |
7beae702 | 12 | * 2010 Christoph Fritz <chf.fritz@googlemail.com> |
1da177e4 LT |
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 | |
c7d9f7eb | 33 | * - Xbox 360 information http://www.free60.org/wiki/Gamepad |
1da177e4 LT |
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 | |
d518b2b4 | 39 | * - XBOX Linux project - extra USB id's |
1da177e4 LT |
40 | * |
41 | * TODO: | |
deb8ee43 | 42 | * - fine tune axes (especially trigger axes) |
1da177e4 LT |
43 | * - fix "analog" buttons (reported as digital now) |
44 | * - get rumble working | |
deb8ee43 | 45 | * - need USB IDs for other dance pads |
1da177e4 LT |
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 | |
d518b2b4 DC |
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 | |
bf8cb314 AH |
72 | * |
73 | * Later changes can be tracked in SCM. | |
1da177e4 LT |
74 | */ |
75 | ||
1da177e4 | 76 | #include <linux/kernel.h> |
1da177e4 LT |
77 | #include <linux/init.h> |
78 | #include <linux/slab.h> | |
deb8ee43 | 79 | #include <linux/stat.h> |
1da177e4 | 80 | #include <linux/module.h> |
ae0dadcf | 81 | #include <linux/usb/input.h> |
1da177e4 | 82 | |
1da177e4 LT |
83 | #define DRIVER_AUTHOR "Marko Friedemann <mfr@bmx-chemnitz.de>" |
84 | #define DRIVER_DESC "X-Box pad driver" | |
85 | ||
86 | #define XPAD_PKT_LEN 32 | |
87 | ||
deb8ee43 DC |
88 | /* xbox d-pads should map to buttons, as is required for DDR pads |
89 | but we map them to axes when possible to simplify things */ | |
b45d44e7 NL |
90 | #define MAP_DPAD_TO_BUTTONS (1 << 0) |
91 | #define MAP_TRIGGERS_TO_BUTTONS (1 << 1) | |
7beae702 CF |
92 | #define MAP_STICKS_TO_NULL (1 << 2) |
93 | #define DANCEPAD_MAP_CONFIG (MAP_DPAD_TO_BUTTONS | \ | |
94 | MAP_TRIGGERS_TO_BUTTONS | MAP_STICKS_TO_NULL) | |
deb8ee43 | 95 | |
c7d9f7eb JK |
96 | #define XTYPE_XBOX 0 |
97 | #define XTYPE_XBOX360 1 | |
99de0912 BM |
98 | #define XTYPE_XBOX360W 2 |
99 | #define XTYPE_UNKNOWN 3 | |
c7d9f7eb | 100 | |
90ab5ee9 | 101 | static bool dpad_to_buttons; |
deb8ee43 DC |
102 | module_param(dpad_to_buttons, bool, S_IRUGO); |
103 | MODULE_PARM_DESC(dpad_to_buttons, "Map D-PAD to buttons rather than axes for unknown pads"); | |
104 | ||
90ab5ee9 | 105 | static bool triggers_to_buttons; |
b45d44e7 NL |
106 | module_param(triggers_to_buttons, bool, S_IRUGO); |
107 | MODULE_PARM_DESC(triggers_to_buttons, "Map triggers to buttons rather than axes for unknown pads"); | |
108 | ||
90ab5ee9 | 109 | static bool sticks_to_null; |
7beae702 CF |
110 | module_param(sticks_to_null, bool, S_IRUGO); |
111 | MODULE_PARM_DESC(sticks_to_null, "Do not map sticks at all for unknown pads"); | |
112 | ||
4c4c9432 | 113 | static const struct xpad_device { |
1da177e4 LT |
114 | u16 idVendor; |
115 | u16 idProduct; | |
116 | char *name; | |
b45d44e7 | 117 | u8 mapping; |
c7d9f7eb | 118 | u8 xtype; |
1da177e4 | 119 | } xpad_device[] = { |
b45d44e7 NL |
120 | { 0x045e, 0x0202, "Microsoft X-Box pad v1 (US)", 0, XTYPE_XBOX }, |
121 | { 0x045e, 0x0289, "Microsoft X-Box pad v2 (US)", 0, XTYPE_XBOX }, | |
122 | { 0x045e, 0x0285, "Microsoft X-Box pad (Japan)", 0, XTYPE_XBOX }, | |
123 | { 0x045e, 0x0287, "Microsoft Xbox Controller S", 0, XTYPE_XBOX }, | |
99de0912 | 124 | { 0x045e, 0x0719, "Xbox 360 Wireless Receiver", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360W }, |
7beae702 | 125 | { 0x0c12, 0x8809, "RedOctane Xbox Dance Pad", DANCEPAD_MAP_CONFIG, XTYPE_XBOX }, |
b45d44e7 NL |
126 | { 0x044f, 0x0f07, "Thrustmaster, Inc. Controller", 0, XTYPE_XBOX }, |
127 | { 0x046d, 0xc242, "Logitech Chillstream Controller", 0, XTYPE_XBOX360 }, | |
128 | { 0x046d, 0xca84, "Logitech Xbox Cordless Controller", 0, XTYPE_XBOX }, | |
129 | { 0x046d, 0xca88, "Logitech Compact Controller for Xbox", 0, XTYPE_XBOX }, | |
130 | { 0x05fd, 0x1007, "Mad Catz Controller (unverified)", 0, XTYPE_XBOX }, | |
131 | { 0x05fd, 0x107a, "InterAct 'PowerPad Pro' X-Box pad (Germany)", 0, XTYPE_XBOX }, | |
132 | { 0x0738, 0x4516, "Mad Catz Control Pad", 0, XTYPE_XBOX }, | |
133 | { 0x0738, 0x4522, "Mad Catz LumiCON", 0, XTYPE_XBOX }, | |
134 | { 0x0738, 0x4526, "Mad Catz Control Pad Pro", 0, XTYPE_XBOX }, | |
135 | { 0x0738, 0x4536, "Mad Catz MicroCON", 0, XTYPE_XBOX }, | |
c7d9f7eb | 136 | { 0x0738, 0x4540, "Mad Catz Beat Pad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX }, |
b45d44e7 NL |
137 | { 0x0738, 0x4556, "Mad Catz Lynx Wireless Controller", 0, XTYPE_XBOX }, |
138 | { 0x0738, 0x4716, "Mad Catz Wired Xbox 360 Controller", 0, XTYPE_XBOX360 }, | |
139 | { 0x0738, 0x4738, "Mad Catz Wired Xbox 360 Controller (SFIV)", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 }, | |
c7d9f7eb | 140 | { 0x0738, 0x6040, "Mad Catz Beat Pad Pro", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX }, |
b45d44e7 NL |
141 | { 0x0c12, 0x8802, "Zeroplus Xbox Controller", 0, XTYPE_XBOX }, |
142 | { 0x0c12, 0x880a, "Pelican Eclipse PL-2023", 0, XTYPE_XBOX }, | |
143 | { 0x0c12, 0x8810, "Zeroplus Xbox Controller", 0, XTYPE_XBOX }, | |
144 | { 0x0c12, 0x9902, "HAMA VibraX - *FAULTY HARDWARE*", 0, XTYPE_XBOX }, | |
145 | { 0x0e4c, 0x1097, "Radica Gamester Controller", 0, XTYPE_XBOX }, | |
146 | { 0x0e4c, 0x2390, "Radica Games Jtech Controller", 0, XTYPE_XBOX }, | |
147 | { 0x0e6f, 0x0003, "Logic3 Freebird wireless Controller", 0, XTYPE_XBOX }, | |
148 | { 0x0e6f, 0x0005, "Eclipse wireless Controller", 0, XTYPE_XBOX }, | |
149 | { 0x0e6f, 0x0006, "Edge wireless Controller", 0, XTYPE_XBOX }, | |
150 | { 0x0e6f, 0x0006, "Pelican 'TSZ' Wired Xbox 360 Controller", 0, XTYPE_XBOX360 }, | |
6ac8a99b | 151 | { 0x0e6f, 0x0201, "Pelican PL-3601 'TSZ' Wired Xbox 360 Controller", 0, XTYPE_XBOX360 }, |
b45d44e7 NL |
152 | { 0x0e8f, 0x0201, "SmartJoy Frag Xpad/PS2 adaptor", 0, XTYPE_XBOX }, |
153 | { 0x0f30, 0x0202, "Joytech Advanced Controller", 0, XTYPE_XBOX }, | |
154 | { 0x0f30, 0x8888, "BigBen XBMiniPad Controller", 0, XTYPE_XBOX }, | |
155 | { 0x102c, 0xff0c, "Joytech Wireless Advanced Controller", 0, XTYPE_XBOX }, | |
c7d9f7eb | 156 | { 0x12ab, 0x8809, "Xbox DDR dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX }, |
fabadbc7 MH |
157 | { 0x12ab, 0x0004, "Honey Bee Xbox360 dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 }, |
158 | { 0x0e6f, 0x0105, "HSM3 Xbox360 dancepad", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 }, | |
b45d44e7 | 159 | { 0x1430, 0x4748, "RedOctane Guitar Hero X-plorer", 0, XTYPE_XBOX360 }, |
c7d9f7eb | 160 | { 0x1430, 0x8888, "TX6500+ Dance Pad (first generation)", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX }, |
b45d44e7 NL |
161 | { 0x146b, 0x0601, "BigBen Interactive XBOX 360 Controller", 0, XTYPE_XBOX360 }, |
162 | { 0x045e, 0x028e, "Microsoft X-Box 360 pad", 0, XTYPE_XBOX360 }, | |
fabadbc7 | 163 | { 0x1bad, 0x0002, "Harmonix Rock Band Guitar", 0, XTYPE_XBOX360 }, |
805423e8 | 164 | { 0x1bad, 0x0003, "Harmonix Rock Band Drumkit", MAP_DPAD_TO_BUTTONS, XTYPE_XBOX360 }, |
b45d44e7 | 165 | { 0x0f0d, 0x0016, "Hori Real Arcade Pro.EX", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 }, |
b326b853 | 166 | { 0x0f0d, 0x000d, "Hori Fighting Stick EX2", MAP_TRIGGERS_TO_BUTTONS, XTYPE_XBOX360 }, |
b45d44e7 NL |
167 | { 0xffff, 0xffff, "Chinese-made Xbox Controller", 0, XTYPE_XBOX }, |
168 | { 0x0000, 0x0000, "Generic X-Box pad", 0, XTYPE_UNKNOWN } | |
1da177e4 LT |
169 | }; |
170 | ||
fc55e952 AH |
171 | /* buttons shared with xbox and xbox360 */ |
172 | static const signed short xpad_common_btn[] = { | |
173 | BTN_A, BTN_B, BTN_X, BTN_Y, /* "analog" buttons */ | |
7beae702 | 174 | BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR, /* start/back/sticks */ |
1da177e4 LT |
175 | -1 /* terminating entry */ |
176 | }; | |
177 | ||
fc55e952 AH |
178 | /* original xbox controllers only */ |
179 | static const signed short xpad_btn[] = { | |
180 | BTN_C, BTN_Z, /* "analog" buttons */ | |
181 | -1 /* terminating entry */ | |
182 | }; | |
183 | ||
7beae702 | 184 | /* used when dpad is mapped to buttons */ |
deb8ee43 | 185 | static const signed short xpad_btn_pad[] = { |
7beae702 CF |
186 | BTN_TRIGGER_HAPPY1, BTN_TRIGGER_HAPPY2, /* d-pad left, right */ |
187 | BTN_TRIGGER_HAPPY3, BTN_TRIGGER_HAPPY4, /* d-pad up, down */ | |
deb8ee43 DC |
188 | -1 /* terminating entry */ |
189 | }; | |
190 | ||
b45d44e7 NL |
191 | /* used when triggers are mapped to buttons */ |
192 | static const signed short xpad_btn_triggers[] = { | |
193 | BTN_TL2, BTN_TR2, /* triggers left/right */ | |
194 | -1 | |
195 | }; | |
196 | ||
197 | ||
c7d9f7eb JK |
198 | static const signed short xpad360_btn[] = { /* buttons for x360 controller */ |
199 | BTN_TL, BTN_TR, /* Button LB/RB */ | |
200 | BTN_MODE, /* The big X button */ | |
201 | -1 | |
202 | }; | |
203 | ||
4c4c9432 | 204 | static const signed short xpad_abs[] = { |
1da177e4 LT |
205 | ABS_X, ABS_Y, /* left stick */ |
206 | ABS_RX, ABS_RY, /* right stick */ | |
deb8ee43 DC |
207 | -1 /* terminating entry */ |
208 | }; | |
209 | ||
b45d44e7 | 210 | /* used when dpad is mapped to axes */ |
deb8ee43 DC |
211 | static const signed short xpad_abs_pad[] = { |
212 | ABS_HAT0X, ABS_HAT0Y, /* d-pad axes */ | |
1da177e4 LT |
213 | -1 /* terminating entry */ |
214 | }; | |
215 | ||
b45d44e7 NL |
216 | /* used when triggers are mapped to axes */ |
217 | static const signed short xpad_abs_triggers[] = { | |
218 | ABS_Z, ABS_RZ, /* triggers left/right */ | |
219 | -1 | |
220 | }; | |
221 | ||
8a0f83ea AH |
222 | /* Xbox 360 has a vendor-specific class, so we cannot match it with only |
223 | * USB_INTERFACE_INFO (also specifically refused by USB subsystem), so we | |
99de0912 BM |
224 | * match against vendor id as well. Wired Xbox 360 devices have protocol 1, |
225 | * wireless controllers have protocol 129. */ | |
226 | #define XPAD_XBOX360_VENDOR_PROTOCOL(vend,pr) \ | |
8a0f83ea AH |
227 | .match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_INT_INFO, \ |
228 | .idVendor = (vend), \ | |
229 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, \ | |
230 | .bInterfaceSubClass = 93, \ | |
99de0912 BM |
231 | .bInterfaceProtocol = (pr) |
232 | #define XPAD_XBOX360_VENDOR(vend) \ | |
233 | { XPAD_XBOX360_VENDOR_PROTOCOL(vend,1) }, \ | |
234 | { XPAD_XBOX360_VENDOR_PROTOCOL(vend,129) } | |
8a0f83ea | 235 | |
1da177e4 LT |
236 | static struct usb_device_id xpad_table [] = { |
237 | { USB_INTERFACE_INFO('X', 'B', 0) }, /* X-Box USB-IF not approved class */ | |
99de0912 BM |
238 | XPAD_XBOX360_VENDOR(0x045e), /* Microsoft X-Box 360 controllers */ |
239 | XPAD_XBOX360_VENDOR(0x046d), /* Logitech X-Box 360 style controllers */ | |
240 | XPAD_XBOX360_VENDOR(0x0738), /* Mad Catz X-Box 360 controllers */ | |
3ffb62cb | 241 | { USB_DEVICE(0x0738, 0x4540) }, /* Mad Catz Beat Pad */ |
99de0912 | 242 | XPAD_XBOX360_VENDOR(0x0e6f), /* 0x0e6f X-Box 360 controllers */ |
fabadbc7 | 243 | XPAD_XBOX360_VENDOR(0x12ab), /* X-Box 360 dance pads */ |
99de0912 | 244 | XPAD_XBOX360_VENDOR(0x1430), /* RedOctane X-Box 360 controllers */ |
3ac91d36 | 245 | XPAD_XBOX360_VENDOR(0x146b), /* BigBen Interactive Controllers */ |
fabadbc7 | 246 | XPAD_XBOX360_VENDOR(0x1bad), /* Harminix Rock Band Guitar and Drums */ |
dadaae37 | 247 | XPAD_XBOX360_VENDOR(0x0f0d), /* Hori Controllers */ |
1da177e4 LT |
248 | { } |
249 | }; | |
250 | ||
251 | MODULE_DEVICE_TABLE (usb, xpad_table); | |
252 | ||
253 | struct usb_xpad { | |
deb8ee43 DC |
254 | struct input_dev *dev; /* input device interface */ |
255 | struct usb_device *udev; /* usb device */ | |
05f091ab | 256 | |
99de0912 BM |
257 | int pad_present; |
258 | ||
deb8ee43 DC |
259 | struct urb *irq_in; /* urb for interrupt in report */ |
260 | unsigned char *idata; /* input data */ | |
1da177e4 | 261 | dma_addr_t idata_dma; |
05f091ab | 262 | |
99de0912 BM |
263 | struct urb *bulk_out; |
264 | unsigned char *bdata; | |
265 | ||
4994cd8d | 266 | #if defined(CONFIG_JOYSTICK_XPAD_FF) || defined(CONFIG_JOYSTICK_XPAD_LEDS) |
e01a06e8 JK |
267 | struct urb *irq_out; /* urb for interrupt out report */ |
268 | unsigned char *odata; /* output data */ | |
269 | dma_addr_t odata_dma; | |
4994cd8d JK |
270 | struct mutex odata_mutex; |
271 | #endif | |
272 | ||
273 | #if defined(CONFIG_JOYSTICK_XPAD_LEDS) | |
274 | struct xpad_led *led; | |
e01a06e8 JK |
275 | #endif |
276 | ||
4994cd8d | 277 | char phys[64]; /* physical device path */ |
deb8ee43 | 278 | |
b45d44e7 | 279 | int mapping; /* map d-pad to buttons or to axes */ |
c7d9f7eb | 280 | int xtype; /* type of xbox device */ |
1da177e4 LT |
281 | }; |
282 | ||
283 | /* | |
284 | * xpad_process_packet | |
285 | * | |
286 | * Completes a request by converting the data into events for the | |
287 | * input subsystem. | |
288 | * | |
289 | * The used report descriptor was taken from ITO Takayukis website: | |
290 | * http://euc.jp/periphs/xbox-controller.ja.html | |
291 | */ | |
292 | ||
7d12e780 | 293 | static void xpad_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data) |
1da177e4 | 294 | { |
c5b7c7c3 | 295 | struct input_dev *dev = xpad->dev; |
1da177e4 | 296 | |
7beae702 CF |
297 | if (!(xpad->mapping & MAP_STICKS_TO_NULL)) { |
298 | /* left stick */ | |
299 | input_report_abs(dev, ABS_X, | |
300 | (__s16) le16_to_cpup((__le16 *)(data + 12))); | |
301 | input_report_abs(dev, ABS_Y, | |
302 | ~(__s16) le16_to_cpup((__le16 *)(data + 14))); | |
303 | ||
304 | /* right stick */ | |
305 | input_report_abs(dev, ABS_RX, | |
306 | (__s16) le16_to_cpup((__le16 *)(data + 16))); | |
307 | input_report_abs(dev, ABS_RY, | |
308 | ~(__s16) le16_to_cpup((__le16 *)(data + 18))); | |
309 | } | |
05f091ab | 310 | |
1da177e4 | 311 | /* triggers left/right */ |
b45d44e7 NL |
312 | if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) { |
313 | input_report_key(dev, BTN_TL2, data[10]); | |
314 | input_report_key(dev, BTN_TR2, data[11]); | |
315 | } else { | |
316 | input_report_abs(dev, ABS_Z, data[10]); | |
317 | input_report_abs(dev, ABS_RZ, data[11]); | |
318 | } | |
05f091ab | 319 | |
1da177e4 | 320 | /* digital pad */ |
b45d44e7 | 321 | if (xpad->mapping & MAP_DPAD_TO_BUTTONS) { |
7beae702 CF |
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); | |
b45d44e7 NL |
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)); | |
deb8ee43 | 332 | } |
05f091ab | 333 | |
1da177e4 | 334 | /* start/back buttons and stick press left/right */ |
deb8ee43 | 335 | input_report_key(dev, BTN_START, data[2] & 0x10); |
7beae702 | 336 | input_report_key(dev, BTN_SELECT, data[2] & 0x20); |
deb8ee43 DC |
337 | input_report_key(dev, BTN_THUMBL, data[2] & 0x40); |
338 | input_report_key(dev, BTN_THUMBR, data[2] & 0x80); | |
05f091ab | 339 | |
1da177e4 LT |
340 | /* "analog" buttons A, B, X, Y */ |
341 | input_report_key(dev, BTN_A, data[4]); | |
342 | input_report_key(dev, BTN_B, data[5]); | |
343 | input_report_key(dev, BTN_X, data[6]); | |
344 | input_report_key(dev, BTN_Y, data[7]); | |
05f091ab | 345 | |
1da177e4 LT |
346 | /* "analog" buttons black, white */ |
347 | input_report_key(dev, BTN_C, data[8]); | |
348 | input_report_key(dev, BTN_Z, data[9]); | |
349 | ||
350 | input_sync(dev); | |
351 | } | |
352 | ||
c7d9f7eb JK |
353 | /* |
354 | * xpad360_process_packet | |
355 | * | |
356 | * Completes a request by converting the data into events for the | |
357 | * input subsystem. It is version for xbox 360 controller | |
358 | * | |
359 | * The used report descriptor was taken from: | |
360 | * http://www.free60.org/wiki/Gamepad | |
361 | */ | |
362 | ||
20b3cdd6 DT |
363 | static void xpad360_process_packet(struct usb_xpad *xpad, |
364 | u16 cmd, unsigned char *data) | |
c7d9f7eb JK |
365 | { |
366 | struct input_dev *dev = xpad->dev; | |
367 | ||
368 | /* digital pad */ | |
b45d44e7 | 369 | if (xpad->mapping & MAP_DPAD_TO_BUTTONS) { |
7beae702 CF |
370 | /* dpad as buttons (left, right, up, down) */ |
371 | input_report_key(dev, BTN_TRIGGER_HAPPY1, data[2] & 0x04); | |
372 | input_report_key(dev, BTN_TRIGGER_HAPPY2, data[2] & 0x08); | |
373 | input_report_key(dev, BTN_TRIGGER_HAPPY3, data[2] & 0x01); | |
374 | input_report_key(dev, BTN_TRIGGER_HAPPY4, data[2] & 0x02); | |
b45d44e7 NL |
375 | } else { |
376 | input_report_abs(dev, ABS_HAT0X, | |
377 | !!(data[2] & 0x08) - !!(data[2] & 0x04)); | |
378 | input_report_abs(dev, ABS_HAT0Y, | |
379 | !!(data[2] & 0x02) - !!(data[2] & 0x01)); | |
c7d9f7eb JK |
380 | } |
381 | ||
382 | /* start/back buttons */ | |
ae91d10a | 383 | input_report_key(dev, BTN_START, data[2] & 0x10); |
7beae702 | 384 | input_report_key(dev, BTN_SELECT, data[2] & 0x20); |
c7d9f7eb JK |
385 | |
386 | /* stick press left/right */ | |
387 | input_report_key(dev, BTN_THUMBL, data[2] & 0x40); | |
388 | input_report_key(dev, BTN_THUMBR, data[2] & 0x80); | |
389 | ||
390 | /* buttons A,B,X,Y,TL,TR and MODE */ | |
ae91d10a DT |
391 | input_report_key(dev, BTN_A, data[3] & 0x10); |
392 | input_report_key(dev, BTN_B, data[3] & 0x20); | |
393 | input_report_key(dev, BTN_X, data[3] & 0x40); | |
394 | input_report_key(dev, BTN_Y, data[3] & 0x80); | |
395 | input_report_key(dev, BTN_TL, data[3] & 0x01); | |
396 | input_report_key(dev, BTN_TR, data[3] & 0x02); | |
397 | input_report_key(dev, BTN_MODE, data[3] & 0x04); | |
c7d9f7eb | 398 | |
7beae702 CF |
399 | if (!(xpad->mapping & MAP_STICKS_TO_NULL)) { |
400 | /* left stick */ | |
401 | input_report_abs(dev, ABS_X, | |
402 | (__s16) le16_to_cpup((__le16 *)(data + 6))); | |
403 | input_report_abs(dev, ABS_Y, | |
404 | ~(__s16) le16_to_cpup((__le16 *)(data + 8))); | |
405 | ||
406 | /* right stick */ | |
407 | input_report_abs(dev, ABS_RX, | |
408 | (__s16) le16_to_cpup((__le16 *)(data + 10))); | |
409 | input_report_abs(dev, ABS_RY, | |
410 | ~(__s16) le16_to_cpup((__le16 *)(data + 12))); | |
411 | } | |
c7d9f7eb JK |
412 | |
413 | /* triggers left/right */ | |
b45d44e7 NL |
414 | if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) { |
415 | input_report_key(dev, BTN_TL2, data[4]); | |
416 | input_report_key(dev, BTN_TR2, data[5]); | |
417 | } else { | |
418 | input_report_abs(dev, ABS_Z, data[4]); | |
419 | input_report_abs(dev, ABS_RZ, data[5]); | |
420 | } | |
c7d9f7eb JK |
421 | |
422 | input_sync(dev); | |
423 | } | |
424 | ||
99de0912 BM |
425 | /* |
426 | * xpad360w_process_packet | |
427 | * | |
428 | * Completes a request by converting the data into events for the | |
429 | * input subsystem. It is version for xbox 360 wireless controller. | |
430 | * | |
431 | * Byte.Bit | |
432 | * 00.1 - Status change: The controller or headset has connected/disconnected | |
433 | * Bits 01.7 and 01.6 are valid | |
434 | * 01.7 - Controller present | |
435 | * 01.6 - Headset present | |
436 | * 01.1 - Pad state (Bytes 4+) valid | |
437 | * | |
438 | */ | |
439 | ||
440 | static void xpad360w_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data) | |
441 | { | |
442 | /* Presence change */ | |
443 | if (data[0] & 0x08) { | |
444 | if (data[1] & 0x80) { | |
445 | xpad->pad_present = 1; | |
446 | usb_submit_urb(xpad->bulk_out, GFP_ATOMIC); | |
447 | } else | |
448 | xpad->pad_present = 0; | |
449 | } | |
450 | ||
451 | /* Valid pad data */ | |
452 | if (!(data[1] & 0x1)) | |
453 | return; | |
454 | ||
455 | xpad360_process_packet(xpad, cmd, &data[4]); | |
456 | } | |
457 | ||
7d12e780 | 458 | static void xpad_irq_in(struct urb *urb) |
1da177e4 LT |
459 | { |
460 | struct usb_xpad *xpad = urb->context; | |
6fc88f53 | 461 | int retval, status; |
05f091ab | 462 | |
6fc88f53 ON |
463 | status = urb->status; |
464 | ||
465 | switch (status) { | |
1da177e4 LT |
466 | case 0: |
467 | /* success */ | |
468 | break; | |
469 | case -ECONNRESET: | |
470 | case -ENOENT: | |
471 | case -ESHUTDOWN: | |
472 | /* this urb is terminated, clean up */ | |
20b3cdd6 | 473 | dbg("%s - urb shutting down with status: %d", |
ea3e6c59 | 474 | __func__, status); |
1da177e4 LT |
475 | return; |
476 | default: | |
20b3cdd6 | 477 | dbg("%s - nonzero urb status received: %d", |
ea3e6c59 | 478 | __func__, status); |
1da177e4 LT |
479 | goto exit; |
480 | } | |
05f091ab | 481 | |
99de0912 BM |
482 | switch (xpad->xtype) { |
483 | case XTYPE_XBOX360: | |
c7d9f7eb | 484 | xpad360_process_packet(xpad, 0, xpad->idata); |
99de0912 BM |
485 | break; |
486 | case XTYPE_XBOX360W: | |
487 | xpad360w_process_packet(xpad, 0, xpad->idata); | |
488 | break; | |
489 | default: | |
c7d9f7eb | 490 | xpad_process_packet(xpad, 0, xpad->idata); |
99de0912 | 491 | } |
1da177e4 LT |
492 | |
493 | exit: | |
dd38d688 | 494 | retval = usb_submit_urb(urb, GFP_ATOMIC); |
1da177e4 LT |
495 | if (retval) |
496 | err ("%s - usb_submit_urb failed with result %d", | |
ea3e6c59 | 497 | __func__, retval); |
1da177e4 LT |
498 | } |
499 | ||
20430214 DT |
500 | static void xpad_bulk_out(struct urb *urb) |
501 | { | |
502 | switch (urb->status) { | |
503 | case 0: | |
504 | /* success */ | |
505 | break; | |
506 | case -ECONNRESET: | |
507 | case -ENOENT: | |
508 | case -ESHUTDOWN: | |
509 | /* this urb is terminated, clean up */ | |
80a914dc | 510 | dbg("%s - urb shutting down with status: %d", __func__, urb->status); |
20430214 DT |
511 | break; |
512 | default: | |
80a914dc | 513 | dbg("%s - nonzero urb status received: %d", __func__, urb->status); |
20430214 DT |
514 | } |
515 | } | |
516 | ||
4994cd8d | 517 | #if defined(CONFIG_JOYSTICK_XPAD_FF) || defined(CONFIG_JOYSTICK_XPAD_LEDS) |
e01a06e8 JK |
518 | static void xpad_irq_out(struct urb *urb) |
519 | { | |
6fc88f53 ON |
520 | int retval, status; |
521 | ||
522 | status = urb->status; | |
e01a06e8 | 523 | |
6fc88f53 | 524 | switch (status) { |
70a6f2e6 | 525 | case 0: |
e01a06e8 | 526 | /* success */ |
70a6f2e6 MG |
527 | return; |
528 | ||
529 | case -ECONNRESET: | |
530 | case -ENOENT: | |
531 | case -ESHUTDOWN: | |
532 | /* this urb is terminated, clean up */ | |
533 | dbg("%s - urb shutting down with status: %d", __func__, status); | |
534 | return; | |
535 | ||
536 | default: | |
537 | dbg("%s - nonzero urb status received: %d", __func__, status); | |
538 | goto exit; | |
e01a06e8 JK |
539 | } |
540 | ||
541 | exit: | |
542 | retval = usb_submit_urb(urb, GFP_ATOMIC); | |
543 | if (retval) | |
544 | err("%s - usb_submit_urb failed with result %d", | |
ea3e6c59 | 545 | __func__, retval); |
e01a06e8 JK |
546 | } |
547 | ||
4994cd8d | 548 | static int xpad_init_output(struct usb_interface *intf, struct usb_xpad *xpad) |
e01a06e8 JK |
549 | { |
550 | struct usb_endpoint_descriptor *ep_irq_out; | |
49cc69b6 | 551 | int error; |
e01a06e8 | 552 | |
b514d4f7 | 553 | if (xpad->xtype == XTYPE_UNKNOWN) |
e01a06e8 JK |
554 | return 0; |
555 | ||
997ea58e DM |
556 | xpad->odata = usb_alloc_coherent(xpad->udev, XPAD_PKT_LEN, |
557 | GFP_KERNEL, &xpad->odata_dma); | |
49cc69b6 AL |
558 | if (!xpad->odata) { |
559 | error = -ENOMEM; | |
e01a06e8 | 560 | goto fail1; |
49cc69b6 | 561 | } |
e01a06e8 | 562 | |
4994cd8d JK |
563 | mutex_init(&xpad->odata_mutex); |
564 | ||
e01a06e8 | 565 | xpad->irq_out = usb_alloc_urb(0, GFP_KERNEL); |
49cc69b6 AL |
566 | if (!xpad->irq_out) { |
567 | error = -ENOMEM; | |
e01a06e8 | 568 | goto fail2; |
49cc69b6 | 569 | } |
e01a06e8 JK |
570 | |
571 | ep_irq_out = &intf->cur_altsetting->endpoint[1].desc; | |
572 | usb_fill_int_urb(xpad->irq_out, xpad->udev, | |
573 | usb_sndintpipe(xpad->udev, ep_irq_out->bEndpointAddress), | |
574 | xpad->odata, XPAD_PKT_LEN, | |
575 | xpad_irq_out, xpad, ep_irq_out->bInterval); | |
576 | xpad->irq_out->transfer_dma = xpad->odata_dma; | |
577 | xpad->irq_out->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | |
578 | ||
e01a06e8 JK |
579 | return 0; |
580 | ||
997ea58e | 581 | fail2: usb_free_coherent(xpad->udev, XPAD_PKT_LEN, xpad->odata, xpad->odata_dma); |
e01a06e8 JK |
582 | fail1: return error; |
583 | } | |
584 | ||
4994cd8d | 585 | static void xpad_stop_output(struct usb_xpad *xpad) |
e01a06e8 | 586 | { |
b514d4f7 | 587 | if (xpad->xtype != XTYPE_UNKNOWN) |
e01a06e8 JK |
588 | usb_kill_urb(xpad->irq_out); |
589 | } | |
590 | ||
4994cd8d | 591 | static void xpad_deinit_output(struct usb_xpad *xpad) |
e01a06e8 | 592 | { |
b514d4f7 | 593 | if (xpad->xtype != XTYPE_UNKNOWN) { |
e01a06e8 | 594 | usb_free_urb(xpad->irq_out); |
997ea58e | 595 | usb_free_coherent(xpad->udev, XPAD_PKT_LEN, |
e01a06e8 JK |
596 | xpad->odata, xpad->odata_dma); |
597 | } | |
598 | } | |
4994cd8d JK |
599 | #else |
600 | static int xpad_init_output(struct usb_interface *intf, struct usb_xpad *xpad) { return 0; } | |
601 | static void xpad_deinit_output(struct usb_xpad *xpad) {} | |
602 | static void xpad_stop_output(struct usb_xpad *xpad) {} | |
603 | #endif | |
604 | ||
605 | #ifdef CONFIG_JOYSTICK_XPAD_FF | |
12187305 | 606 | static int xpad_play_effect(struct input_dev *dev, void *data, struct ff_effect *effect) |
4994cd8d JK |
607 | { |
608 | struct usb_xpad *xpad = input_get_drvdata(dev); | |
e01a06e8 | 609 | |
4994cd8d JK |
610 | if (effect->type == FF_RUMBLE) { |
611 | __u16 strong = effect->u.rumble.strong_magnitude; | |
612 | __u16 weak = effect->u.rumble.weak_magnitude; | |
12187305 BV |
613 | |
614 | switch (xpad->xtype) { | |
615 | ||
616 | case XTYPE_XBOX: | |
617 | xpad->odata[0] = 0x00; | |
618 | xpad->odata[1] = 0x06; | |
619 | xpad->odata[2] = 0x00; | |
620 | xpad->odata[3] = strong / 256; /* left actuator */ | |
621 | xpad->odata[4] = 0x00; | |
622 | xpad->odata[5] = weak / 256; /* right actuator */ | |
623 | xpad->irq_out->transfer_buffer_length = 6; | |
624 | ||
625 | return usb_submit_urb(xpad->irq_out, GFP_ATOMIC); | |
626 | ||
627 | case XTYPE_XBOX360: | |
628 | xpad->odata[0] = 0x00; | |
629 | xpad->odata[1] = 0x08; | |
630 | xpad->odata[2] = 0x00; | |
631 | xpad->odata[3] = strong / 256; /* left actuator? */ | |
632 | xpad->odata[4] = weak / 256; /* right actuator? */ | |
633 | xpad->odata[5] = 0x00; | |
634 | xpad->odata[6] = 0x00; | |
635 | xpad->odata[7] = 0x00; | |
636 | xpad->irq_out->transfer_buffer_length = 8; | |
637 | ||
638 | return usb_submit_urb(xpad->irq_out, GFP_ATOMIC); | |
639 | ||
b514d4f7 CM |
640 | case XTYPE_XBOX360W: |
641 | xpad->odata[0] = 0x00; | |
642 | xpad->odata[1] = 0x01; | |
643 | xpad->odata[2] = 0x0F; | |
644 | xpad->odata[3] = 0xC0; | |
645 | xpad->odata[4] = 0x00; | |
646 | xpad->odata[5] = strong / 256; | |
647 | xpad->odata[6] = weak / 256; | |
648 | xpad->odata[7] = 0x00; | |
649 | xpad->odata[8] = 0x00; | |
650 | xpad->odata[9] = 0x00; | |
651 | xpad->odata[10] = 0x00; | |
652 | xpad->odata[11] = 0x00; | |
653 | xpad->irq_out->transfer_buffer_length = 12; | |
654 | ||
655 | return usb_submit_urb(xpad->irq_out, GFP_ATOMIC); | |
656 | ||
12187305 BV |
657 | default: |
658 | dbg("%s - rumble command sent to unsupported xpad type: %d", | |
659 | __func__, xpad->xtype); | |
660 | return -1; | |
661 | } | |
4994cd8d JK |
662 | } |
663 | ||
664 | return 0; | |
665 | } | |
666 | ||
667 | static int xpad_init_ff(struct usb_xpad *xpad) | |
668 | { | |
b514d4f7 | 669 | if (xpad->xtype == XTYPE_UNKNOWN) |
cfbe2010 AH |
670 | return 0; |
671 | ||
4994cd8d JK |
672 | input_set_capability(xpad->dev, EV_FF, FF_RUMBLE); |
673 | ||
674 | return input_ff_create_memless(xpad->dev, NULL, xpad_play_effect); | |
675 | } | |
676 | ||
677 | #else | |
678 | static int xpad_init_ff(struct usb_xpad *xpad) { return 0; } | |
679 | #endif | |
680 | ||
681 | #if defined(CONFIG_JOYSTICK_XPAD_LEDS) | |
682 | #include <linux/leds.h> | |
683 | ||
684 | struct xpad_led { | |
685 | char name[16]; | |
686 | struct led_classdev led_cdev; | |
687 | struct usb_xpad *xpad; | |
688 | }; | |
689 | ||
690 | static void xpad_send_led_command(struct usb_xpad *xpad, int command) | |
691 | { | |
692 | if (command >= 0 && command < 14) { | |
693 | mutex_lock(&xpad->odata_mutex); | |
694 | xpad->odata[0] = 0x01; | |
695 | xpad->odata[1] = 0x03; | |
696 | xpad->odata[2] = command; | |
04021e4e | 697 | xpad->irq_out->transfer_buffer_length = 3; |
4994cd8d JK |
698 | usb_submit_urb(xpad->irq_out, GFP_KERNEL); |
699 | mutex_unlock(&xpad->odata_mutex); | |
700 | } | |
701 | } | |
702 | ||
703 | static void xpad_led_set(struct led_classdev *led_cdev, | |
704 | enum led_brightness value) | |
705 | { | |
706 | struct xpad_led *xpad_led = container_of(led_cdev, | |
707 | struct xpad_led, led_cdev); | |
708 | ||
709 | xpad_send_led_command(xpad_led->xpad, value); | |
710 | } | |
711 | ||
712 | static int xpad_led_probe(struct usb_xpad *xpad) | |
713 | { | |
714 | static atomic_t led_seq = ATOMIC_INIT(0); | |
715 | long led_no; | |
716 | struct xpad_led *led; | |
717 | struct led_classdev *led_cdev; | |
718 | int error; | |
719 | ||
720 | if (xpad->xtype != XTYPE_XBOX360) | |
721 | return 0; | |
722 | ||
723 | xpad->led = led = kzalloc(sizeof(struct xpad_led), GFP_KERNEL); | |
724 | if (!led) | |
725 | return -ENOMEM; | |
726 | ||
727 | led_no = (long)atomic_inc_return(&led_seq) - 1; | |
728 | ||
729 | snprintf(led->name, sizeof(led->name), "xpad%ld", led_no); | |
730 | led->xpad = xpad; | |
731 | ||
732 | led_cdev = &led->led_cdev; | |
733 | led_cdev->name = led->name; | |
734 | led_cdev->brightness_set = xpad_led_set; | |
735 | ||
736 | error = led_classdev_register(&xpad->udev->dev, led_cdev); | |
737 | if (error) { | |
738 | kfree(led); | |
739 | xpad->led = NULL; | |
740 | return error; | |
741 | } | |
742 | ||
743 | /* | |
744 | * Light up the segment corresponding to controller number | |
745 | */ | |
746 | xpad_send_led_command(xpad, (led_no % 4) + 2); | |
747 | ||
748 | return 0; | |
749 | } | |
750 | ||
751 | static void xpad_led_disconnect(struct usb_xpad *xpad) | |
752 | { | |
753 | struct xpad_led *xpad_led = xpad->led; | |
754 | ||
755 | if (xpad_led) { | |
756 | led_classdev_unregister(&xpad_led->led_cdev); | |
6ff92a6d | 757 | kfree(xpad_led); |
4994cd8d JK |
758 | } |
759 | } | |
e01a06e8 | 760 | #else |
4994cd8d JK |
761 | static int xpad_led_probe(struct usb_xpad *xpad) { return 0; } |
762 | static void xpad_led_disconnect(struct usb_xpad *xpad) { } | |
e01a06e8 JK |
763 | #endif |
764 | ||
4994cd8d | 765 | |
e01a06e8 | 766 | static int xpad_open(struct input_dev *dev) |
1da177e4 | 767 | { |
7791bdae | 768 | struct usb_xpad *xpad = input_get_drvdata(dev); |
05f091ab | 769 | |
99de0912 BM |
770 | /* URB was submitted in probe */ |
771 | if(xpad->xtype == XTYPE_XBOX360W) | |
772 | return 0; | |
773 | ||
1da177e4 | 774 | xpad->irq_in->dev = xpad->udev; |
65cde54b | 775 | if (usb_submit_urb(xpad->irq_in, GFP_KERNEL)) |
1da177e4 | 776 | return -EIO; |
05f091ab | 777 | |
1da177e4 LT |
778 | return 0; |
779 | } | |
780 | ||
e01a06e8 | 781 | static void xpad_close(struct input_dev *dev) |
1da177e4 | 782 | { |
7791bdae | 783 | struct usb_xpad *xpad = input_get_drvdata(dev); |
05f091ab | 784 | |
161feb24 | 785 | if (xpad->xtype != XTYPE_XBOX360W) |
99de0912 | 786 | usb_kill_urb(xpad->irq_in); |
161feb24 | 787 | |
4994cd8d | 788 | xpad_stop_output(xpad); |
1da177e4 LT |
789 | } |
790 | ||
deb8ee43 DC |
791 | static void xpad_set_up_abs(struct input_dev *input_dev, signed short abs) |
792 | { | |
793 | set_bit(abs, input_dev->absbit); | |
794 | ||
795 | switch (abs) { | |
796 | case ABS_X: | |
797 | case ABS_Y: | |
798 | case ABS_RX: | |
799 | case ABS_RY: /* the two sticks */ | |
800 | input_set_abs_params(input_dev, abs, -32768, 32767, 16, 128); | |
801 | break; | |
802 | case ABS_Z: | |
b45d44e7 | 803 | case ABS_RZ: /* the triggers (if mapped to axes) */ |
deb8ee43 DC |
804 | input_set_abs_params(input_dev, abs, 0, 255, 0, 0); |
805 | break; | |
806 | case ABS_HAT0X: | |
b45d44e7 | 807 | case ABS_HAT0Y: /* the d-pad (only if dpad is mapped to axes */ |
deb8ee43 DC |
808 | input_set_abs_params(input_dev, abs, -1, 1, 0, 0); |
809 | break; | |
810 | } | |
811 | } | |
812 | ||
1da177e4 LT |
813 | static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id) |
814 | { | |
20b3cdd6 | 815 | struct usb_device *udev = interface_to_usbdev(intf); |
c5b7c7c3 DT |
816 | struct usb_xpad *xpad; |
817 | struct input_dev *input_dev; | |
1da177e4 | 818 | struct usb_endpoint_descriptor *ep_irq_in; |
49cc69b6 | 819 | int i, error; |
05f091ab | 820 | |
1da177e4 LT |
821 | for (i = 0; xpad_device[i].idVendor; i++) { |
822 | if ((le16_to_cpu(udev->descriptor.idVendor) == xpad_device[i].idVendor) && | |
823 | (le16_to_cpu(udev->descriptor.idProduct) == xpad_device[i].idProduct)) | |
824 | break; | |
825 | } | |
05f091ab | 826 | |
c5b7c7c3 DT |
827 | xpad = kzalloc(sizeof(struct usb_xpad), GFP_KERNEL); |
828 | input_dev = input_allocate_device(); | |
49cc69b6 AL |
829 | if (!xpad || !input_dev) { |
830 | error = -ENOMEM; | |
c5b7c7c3 | 831 | goto fail1; |
49cc69b6 | 832 | } |
05f091ab | 833 | |
997ea58e DM |
834 | xpad->idata = usb_alloc_coherent(udev, XPAD_PKT_LEN, |
835 | GFP_KERNEL, &xpad->idata_dma); | |
49cc69b6 AL |
836 | if (!xpad->idata) { |
837 | error = -ENOMEM; | |
c5b7c7c3 | 838 | goto fail1; |
49cc69b6 | 839 | } |
1da177e4 LT |
840 | |
841 | xpad->irq_in = usb_alloc_urb(0, GFP_KERNEL); | |
49cc69b6 AL |
842 | if (!xpad->irq_in) { |
843 | error = -ENOMEM; | |
c5b7c7c3 | 844 | goto fail2; |
49cc69b6 | 845 | } |
05f091ab | 846 | |
1da177e4 | 847 | xpad->udev = udev; |
b45d44e7 | 848 | xpad->mapping = xpad_device[i].mapping; |
c7d9f7eb | 849 | xpad->xtype = xpad_device[i].xtype; |
b45d44e7 | 850 | |
99de0912 BM |
851 | if (xpad->xtype == XTYPE_UNKNOWN) { |
852 | if (intf->cur_altsetting->desc.bInterfaceClass == USB_CLASS_VENDOR_SPEC) { | |
853 | if (intf->cur_altsetting->desc.bInterfaceProtocol == 129) | |
854 | xpad->xtype = XTYPE_XBOX360W; | |
855 | else | |
856 | xpad->xtype = XTYPE_XBOX360; | |
857 | } else | |
858 | xpad->xtype = XTYPE_XBOX; | |
b45d44e7 NL |
859 | |
860 | if (dpad_to_buttons) | |
861 | xpad->mapping |= MAP_DPAD_TO_BUTTONS; | |
862 | if (triggers_to_buttons) | |
863 | xpad->mapping |= MAP_TRIGGERS_TO_BUTTONS; | |
7beae702 CF |
864 | if (sticks_to_null) |
865 | xpad->mapping |= MAP_STICKS_TO_NULL; | |
99de0912 | 866 | } |
b45d44e7 | 867 | |
c5b7c7c3 DT |
868 | xpad->dev = input_dev; |
869 | usb_make_path(udev, xpad->phys, sizeof(xpad->phys)); | |
870 | strlcat(xpad->phys, "/input0", sizeof(xpad->phys)); | |
05f091ab | 871 | |
c5b7c7c3 DT |
872 | input_dev->name = xpad_device[i].name; |
873 | input_dev->phys = xpad->phys; | |
874 | usb_to_input_id(udev, &input_dev->id); | |
c0f82d57 | 875 | input_dev->dev.parent = &intf->dev; |
7791bdae DT |
876 | |
877 | input_set_drvdata(input_dev, xpad); | |
878 | ||
c5b7c7c3 DT |
879 | input_dev->open = xpad_open; |
880 | input_dev->close = xpad_close; | |
05f091ab | 881 | |
7beae702 CF |
882 | input_dev->evbit[0] = BIT_MASK(EV_KEY); |
883 | ||
884 | if (!(xpad->mapping & MAP_STICKS_TO_NULL)) { | |
885 | input_dev->evbit[0] |= BIT_MASK(EV_ABS); | |
886 | /* set up axes */ | |
887 | for (i = 0; xpad_abs[i] >= 0; i++) | |
888 | xpad_set_up_abs(input_dev, xpad_abs[i]); | |
889 | } | |
05f091ab | 890 | |
7beae702 | 891 | /* set up standard buttons */ |
fc55e952 | 892 | for (i = 0; xpad_common_btn[i] >= 0; i++) |
b45d44e7 | 893 | __set_bit(xpad_common_btn[i], input_dev->keybit); |
05f091ab | 894 | |
7beae702 | 895 | /* set up model-specific ones */ |
b45d44e7 NL |
896 | if (xpad->xtype == XTYPE_XBOX360 || xpad->xtype == XTYPE_XBOX360W) { |
897 | for (i = 0; xpad360_btn[i] >= 0; i++) | |
898 | __set_bit(xpad360_btn[i], input_dev->keybit); | |
899 | } else { | |
900 | for (i = 0; xpad_btn[i] >= 0; i++) | |
901 | __set_bit(xpad_btn[i], input_dev->keybit); | |
902 | } | |
903 | ||
904 | if (xpad->mapping & MAP_DPAD_TO_BUTTONS) { | |
905 | for (i = 0; xpad_btn_pad[i] >= 0; i++) | |
906 | __set_bit(xpad_btn_pad[i], input_dev->keybit); | |
907 | } else { | |
deb8ee43 DC |
908 | for (i = 0; xpad_abs_pad[i] >= 0; i++) |
909 | xpad_set_up_abs(input_dev, xpad_abs_pad[i]); | |
b45d44e7 NL |
910 | } |
911 | ||
912 | if (xpad->mapping & MAP_TRIGGERS_TO_BUTTONS) { | |
913 | for (i = 0; xpad_btn_triggers[i] >= 0; i++) | |
914 | __set_bit(xpad_btn_triggers[i], input_dev->keybit); | |
915 | } else { | |
916 | for (i = 0; xpad_abs_triggers[i] >= 0; i++) | |
917 | xpad_set_up_abs(input_dev, xpad_abs_triggers[i]); | |
918 | } | |
05f091ab | 919 | |
4994cd8d | 920 | error = xpad_init_output(intf, xpad); |
e01a06e8 | 921 | if (error) |
161feb24 | 922 | goto fail3; |
e01a06e8 | 923 | |
4994cd8d JK |
924 | error = xpad_init_ff(xpad); |
925 | if (error) | |
161feb24 | 926 | goto fail4; |
4994cd8d JK |
927 | |
928 | error = xpad_led_probe(xpad); | |
929 | if (error) | |
161feb24 | 930 | goto fail5; |
4994cd8d | 931 | |
c5b7c7c3 DT |
932 | ep_irq_in = &intf->cur_altsetting->endpoint[0].desc; |
933 | usb_fill_int_urb(xpad->irq_in, udev, | |
934 | usb_rcvintpipe(udev, ep_irq_in->bEndpointAddress), | |
935 | xpad->idata, XPAD_PKT_LEN, xpad_irq_in, | |
936 | xpad, ep_irq_in->bInterval); | |
937 | xpad->irq_in->transfer_dma = xpad->idata_dma; | |
938 | xpad->irq_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | |
05f091ab | 939 | |
5014186d DT |
940 | error = input_register_device(xpad->dev); |
941 | if (error) | |
161feb24 | 942 | goto fail6; |
05f091ab | 943 | |
1da177e4 | 944 | usb_set_intfdata(intf, xpad); |
99de0912 | 945 | |
99de0912 | 946 | if (xpad->xtype == XTYPE_XBOX360W) { |
99de0912 BM |
947 | /* |
948 | * Setup the message to set the LEDs on the | |
949 | * controller when it shows up | |
950 | */ | |
951 | xpad->bulk_out = usb_alloc_urb(0, GFP_KERNEL); | |
49cc69b6 AL |
952 | if (!xpad->bulk_out) { |
953 | error = -ENOMEM; | |
e3f0f0a6 | 954 | goto fail7; |
49cc69b6 | 955 | } |
99de0912 BM |
956 | |
957 | xpad->bdata = kzalloc(XPAD_PKT_LEN, GFP_KERNEL); | |
49cc69b6 AL |
958 | if (!xpad->bdata) { |
959 | error = -ENOMEM; | |
e3f0f0a6 | 960 | goto fail8; |
49cc69b6 | 961 | } |
99de0912 BM |
962 | |
963 | xpad->bdata[2] = 0x08; | |
964 | switch (intf->cur_altsetting->desc.bInterfaceNumber) { | |
965 | case 0: | |
966 | xpad->bdata[3] = 0x42; | |
967 | break; | |
968 | case 2: | |
969 | xpad->bdata[3] = 0x43; | |
970 | break; | |
971 | case 4: | |
972 | xpad->bdata[3] = 0x44; | |
973 | break; | |
974 | case 6: | |
975 | xpad->bdata[3] = 0x45; | |
976 | } | |
977 | ||
978 | ep_irq_in = &intf->cur_altsetting->endpoint[1].desc; | |
979 | usb_fill_bulk_urb(xpad->bulk_out, udev, | |
980 | usb_sndbulkpipe(udev, ep_irq_in->bEndpointAddress), | |
981 | xpad->bdata, XPAD_PKT_LEN, xpad_bulk_out, xpad); | |
e3f0f0a6 AL |
982 | |
983 | /* | |
984 | * Submit the int URB immediately rather than waiting for open | |
985 | * because we get status messages from the device whether | |
986 | * or not any controllers are attached. In fact, it's | |
987 | * exactly the message that a controller has arrived that | |
988 | * we're waiting for. | |
989 | */ | |
990 | xpad->irq_in->dev = xpad->udev; | |
991 | error = usb_submit_urb(xpad->irq_in, GFP_KERNEL); | |
992 | if (error) | |
993 | goto fail9; | |
99de0912 BM |
994 | } |
995 | ||
1da177e4 | 996 | return 0; |
c5b7c7c3 | 997 | |
e3f0f0a6 AL |
998 | fail9: kfree(xpad->bdata); |
999 | fail8: usb_free_urb(xpad->bulk_out); | |
161feb24 AL |
1000 | fail7: input_unregister_device(input_dev); |
1001 | input_dev = NULL; | |
1002 | fail6: xpad_led_disconnect(xpad); | |
1003 | fail5: if (input_dev) | |
1004 | input_ff_destroy(input_dev); | |
1005 | fail4: xpad_deinit_output(xpad); | |
1006 | fail3: usb_free_urb(xpad->irq_in); | |
997ea58e | 1007 | fail2: usb_free_coherent(udev, XPAD_PKT_LEN, xpad->idata, xpad->idata_dma); |
5014186d | 1008 | fail1: input_free_device(input_dev); |
c5b7c7c3 | 1009 | kfree(xpad); |
5014186d | 1010 | return error; |
c5b7c7c3 | 1011 | |
1da177e4 LT |
1012 | } |
1013 | ||
1014 | static void xpad_disconnect(struct usb_interface *intf) | |
1015 | { | |
1016 | struct usb_xpad *xpad = usb_get_intfdata (intf); | |
05f091ab | 1017 | |
2a059159 DT |
1018 | xpad_led_disconnect(xpad); |
1019 | input_unregister_device(xpad->dev); | |
1020 | xpad_deinit_output(xpad); | |
1021 | ||
1022 | if (xpad->xtype == XTYPE_XBOX360W) { | |
1023 | usb_kill_urb(xpad->bulk_out); | |
1024 | usb_free_urb(xpad->bulk_out); | |
1025 | usb_kill_urb(xpad->irq_in); | |
1da177e4 | 1026 | } |
2a059159 DT |
1027 | |
1028 | usb_free_urb(xpad->irq_in); | |
1029 | usb_free_coherent(xpad->udev, XPAD_PKT_LEN, | |
1030 | xpad->idata, xpad->idata_dma); | |
1031 | ||
1032 | kfree(xpad->bdata); | |
1033 | kfree(xpad); | |
1034 | ||
1035 | usb_set_intfdata(intf, NULL); | |
1da177e4 LT |
1036 | } |
1037 | ||
1038 | static struct usb_driver xpad_driver = { | |
1da177e4 LT |
1039 | .name = "xpad", |
1040 | .probe = xpad_probe, | |
1041 | .disconnect = xpad_disconnect, | |
1042 | .id_table = xpad_table, | |
1043 | }; | |
1044 | ||
08642e7c | 1045 | module_usb_driver(xpad_driver); |
1da177e4 LT |
1046 | |
1047 | MODULE_AUTHOR(DRIVER_AUTHOR); | |
1048 | MODULE_DESCRIPTION(DRIVER_DESC); | |
1049 | MODULE_LICENSE("GPL"); |