]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/usb/input/xpad.c
USB: fix compiler issues with newer gcc versions
[mirror_ubuntu-artful-kernel.git] / drivers / usb / input / xpad.c
CommitLineData
1da177e4 1/*
deb8ee43 2 * X-Box gamepad - v0.0.6
1da177e4
LT
3 *
4 * Copyright (c) 2002 Marko Friedemann <mfr@bmx-chemnitz.de>
deb8ee43
DC
5 * 2005 Dominic Cerquetti <binary1230@yahoo.com>
6 * 2006 Adam Buchbinder <adam.buchbinder@gmail.com>
1da177e4
LT
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 *
23 * This driver is based on:
24 * - information from http://euc.jp/periphs/xbox-controller.ja.html
25 * - the iForce driver drivers/char/joystick/iforce.c
26 * - the skeleton-driver drivers/usb/usb-skeleton.c
27 *
28 * Thanks to:
29 * - ITO Takayuki for providing essential xpad information on his website
30 * - Vojtech Pavlik - iforce driver / input subsystem
31 * - Greg Kroah-Hartman - usb-skeleton driver
32 *
33 * TODO:
deb8ee43 34 * - fine tune axes (especially trigger axes)
1da177e4
LT
35 * - fix "analog" buttons (reported as digital now)
36 * - get rumble working
deb8ee43 37 * - need USB IDs for other dance pads
1da177e4
LT
38 *
39 * History:
40 *
41 * 2002-06-27 - 0.0.1 : first version, just said "XBOX HID controller"
42 *
43 * 2002-07-02 - 0.0.2 : basic working version
44 * - all axes and 9 of the 10 buttons work (german InterAct device)
45 * - the black button does not work
46 *
47 * 2002-07-14 - 0.0.3 : rework by Vojtech Pavlik
48 * - indentation fixes
49 * - usb + input init sequence fixes
50 *
51 * 2002-07-16 - 0.0.4 : minor changes, merge with Vojtech's v0.0.3
52 * - verified the lack of HID and report descriptors
53 * - verified that ALL buttons WORK
54 * - fixed d-pad to axes mapping
55 *
56 * 2002-07-17 - 0.0.5 : simplified d-pad handling
57 */
58
1da177e4 59#include <linux/kernel.h>
1da177e4
LT
60#include <linux/init.h>
61#include <linux/slab.h>
deb8ee43 62#include <linux/stat.h>
1da177e4 63#include <linux/module.h>
deb8ee43 64#include <linux/moduleparam.h>
1da177e4 65#include <linux/smp_lock.h>
ae0dadcf 66#include <linux/usb/input.h>
1da177e4 67
deb8ee43 68#define DRIVER_VERSION "v0.0.6"
1da177e4
LT
69#define DRIVER_AUTHOR "Marko Friedemann <mfr@bmx-chemnitz.de>"
70#define DRIVER_DESC "X-Box pad driver"
71
72#define XPAD_PKT_LEN 32
73
deb8ee43
DC
74/* xbox d-pads should map to buttons, as is required for DDR pads
75 but we map them to axes when possible to simplify things */
76#define MAP_DPAD_TO_BUTTONS 0
77#define MAP_DPAD_TO_AXES 1
78#define MAP_DPAD_UNKNOWN -1
79
80static int dpad_to_buttons;
81module_param(dpad_to_buttons, bool, S_IRUGO);
82MODULE_PARM_DESC(dpad_to_buttons, "Map D-PAD to buttons rather than axes for unknown pads");
83
4c4c9432 84static const struct xpad_device {
1da177e4
LT
85 u16 idVendor;
86 u16 idProduct;
87 char *name;
deb8ee43 88 u8 dpad_mapping;
1da177e4 89} xpad_device[] = {
deb8ee43
DC
90 { 0x045e, 0x0202, "Microsoft X-Box pad v1 (US)", MAP_DPAD_TO_AXES },
91 { 0x045e, 0x0289, "Microsoft X-Box pad v2 (US)", MAP_DPAD_TO_AXES },
92 { 0x045e, 0x0285, "Microsoft X-Box pad (Japan)", MAP_DPAD_TO_AXES },
93 { 0x05fd, 0x107a, "InterAct 'PowerPad Pro' X-Box pad (Germany)", MAP_DPAD_TO_AXES },
94 { 0x0c12, 0x8809, "RedOctane Xbox Dance Pad", MAP_DPAD_TO_BUTTONS },
95 { 0x0000, 0x0000, "Generic X-Box pad", MAP_DPAD_UNKNOWN }
1da177e4
LT
96};
97
4c4c9432 98static const signed short xpad_btn[] = {
1da177e4
LT
99 BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, /* "analog" buttons */
100 BTN_START, BTN_BACK, BTN_THUMBL, BTN_THUMBR, /* start/back/sticks */
101 -1 /* terminating entry */
102};
103
deb8ee43
DC
104/* only used if MAP_DPAD_TO_BUTTONS */
105static const signed short xpad_btn_pad[] = {
106 BTN_LEFT, BTN_RIGHT, /* d-pad left, right */
107 BTN_0, BTN_1, /* d-pad up, down (XXX names??) */
108 -1 /* terminating entry */
109};
110
4c4c9432 111static const signed short xpad_abs[] = {
1da177e4
LT
112 ABS_X, ABS_Y, /* left stick */
113 ABS_RX, ABS_RY, /* right stick */
114 ABS_Z, ABS_RZ, /* triggers left/right */
deb8ee43
DC
115 -1 /* terminating entry */
116};
117
118/* only used if MAP_DPAD_TO_AXES */
119static const signed short xpad_abs_pad[] = {
120 ABS_HAT0X, ABS_HAT0Y, /* d-pad axes */
1da177e4
LT
121 -1 /* terminating entry */
122};
123
124static struct usb_device_id xpad_table [] = {
125 { USB_INTERFACE_INFO('X', 'B', 0) }, /* X-Box USB-IF not approved class */
126 { }
127};
128
129MODULE_DEVICE_TABLE (usb, xpad_table);
130
131struct usb_xpad {
deb8ee43
DC
132 struct input_dev *dev; /* input device interface */
133 struct usb_device *udev; /* usb device */
05f091ab 134
deb8ee43
DC
135 struct urb *irq_in; /* urb for interrupt in report */
136 unsigned char *idata; /* input data */
1da177e4 137 dma_addr_t idata_dma;
05f091ab 138
deb8ee43
DC
139 char phys[65]; /* physical device path */
140
141 int dpad_mapping; /* map d-pad to buttons or to axes */
1da177e4
LT
142};
143
144/*
145 * xpad_process_packet
146 *
147 * Completes a request by converting the data into events for the
148 * input subsystem.
149 *
150 * The used report descriptor was taken from ITO Takayukis website:
151 * http://euc.jp/periphs/xbox-controller.ja.html
152 */
153
7d12e780 154static void xpad_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data)
1da177e4 155{
c5b7c7c3 156 struct input_dev *dev = xpad->dev;
1da177e4 157
1da177e4
LT
158 /* left stick */
159 input_report_abs(dev, ABS_X, (__s16) (((__s16)data[13] << 8) | data[12]));
160 input_report_abs(dev, ABS_Y, (__s16) (((__s16)data[15] << 8) | data[14]));
05f091ab 161
1da177e4
LT
162 /* right stick */
163 input_report_abs(dev, ABS_RX, (__s16) (((__s16)data[17] << 8) | data[16]));
164 input_report_abs(dev, ABS_RY, (__s16) (((__s16)data[19] << 8) | data[18]));
05f091ab 165
1da177e4
LT
166 /* triggers left/right */
167 input_report_abs(dev, ABS_Z, data[10]);
168 input_report_abs(dev, ABS_RZ, data[11]);
05f091ab 169
1da177e4 170 /* digital pad */
deb8ee43
DC
171 if (xpad->dpad_mapping == MAP_DPAD_TO_AXES) {
172 input_report_abs(dev, ABS_HAT0X, !!(data[2] & 0x08) - !!(data[2] & 0x04));
173 input_report_abs(dev, ABS_HAT0Y, !!(data[2] & 0x02) - !!(data[2] & 0x01));
174 } else /* xpad->dpad_mapping == MAP_DPAD_TO_BUTTONS */ {
175 input_report_key(dev, BTN_LEFT, data[2] & 0x04);
176 input_report_key(dev, BTN_RIGHT, data[2] & 0x08);
177 input_report_key(dev, BTN_0, data[2] & 0x01); // up
178 input_report_key(dev, BTN_1, data[2] & 0x02); // down
179 }
05f091ab 180
1da177e4 181 /* start/back buttons and stick press left/right */
deb8ee43
DC
182 input_report_key(dev, BTN_START, data[2] & 0x10);
183 input_report_key(dev, BTN_BACK, data[2] & 0x20);
184 input_report_key(dev, BTN_THUMBL, data[2] & 0x40);
185 input_report_key(dev, BTN_THUMBR, data[2] & 0x80);
05f091ab 186
1da177e4
LT
187 /* "analog" buttons A, B, X, Y */
188 input_report_key(dev, BTN_A, data[4]);
189 input_report_key(dev, BTN_B, data[5]);
190 input_report_key(dev, BTN_X, data[6]);
191 input_report_key(dev, BTN_Y, data[7]);
05f091ab 192
1da177e4
LT
193 /* "analog" buttons black, white */
194 input_report_key(dev, BTN_C, data[8]);
195 input_report_key(dev, BTN_Z, data[9]);
196
197 input_sync(dev);
198}
199
7d12e780 200static void xpad_irq_in(struct urb *urb)
1da177e4
LT
201{
202 struct usb_xpad *xpad = urb->context;
203 int retval;
05f091ab 204
1da177e4
LT
205 switch (urb->status) {
206 case 0:
207 /* success */
208 break;
209 case -ECONNRESET:
210 case -ENOENT:
211 case -ESHUTDOWN:
212 /* this urb is terminated, clean up */
213 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
214 return;
215 default:
216 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
217 goto exit;
218 }
05f091ab 219
7d12e780 220 xpad_process_packet(xpad, 0, xpad->idata);
1da177e4
LT
221
222exit:
223 retval = usb_submit_urb (urb, GFP_ATOMIC);
224 if (retval)
225 err ("%s - usb_submit_urb failed with result %d",
226 __FUNCTION__, retval);
227}
228
229static int xpad_open (struct input_dev *dev)
230{
231 struct usb_xpad *xpad = dev->private;
05f091ab 232
1da177e4 233 xpad->irq_in->dev = xpad->udev;
65cde54b 234 if (usb_submit_urb(xpad->irq_in, GFP_KERNEL))
1da177e4 235 return -EIO;
05f091ab 236
1da177e4
LT
237 return 0;
238}
239
240static void xpad_close (struct input_dev *dev)
241{
242 struct usb_xpad *xpad = dev->private;
05f091ab 243
65cde54b 244 usb_kill_urb(xpad->irq_in);
1da177e4
LT
245}
246
deb8ee43
DC
247static void xpad_set_up_abs(struct input_dev *input_dev, signed short abs)
248{
249 set_bit(abs, input_dev->absbit);
250
251 switch (abs) {
252 case ABS_X:
253 case ABS_Y:
254 case ABS_RX:
255 case ABS_RY: /* the two sticks */
256 input_set_abs_params(input_dev, abs, -32768, 32767, 16, 128);
257 break;
258 case ABS_Z:
259 case ABS_RZ: /* the triggers */
260 input_set_abs_params(input_dev, abs, 0, 255, 0, 0);
261 break;
262 case ABS_HAT0X:
263 case ABS_HAT0Y: /* the d-pad (only if MAP_DPAD_TO_AXES) */
264 input_set_abs_params(input_dev, abs, -1, 1, 0, 0);
265 break;
266 }
267}
268
1da177e4
LT
269static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id)
270{
271 struct usb_device *udev = interface_to_usbdev (intf);
c5b7c7c3
DT
272 struct usb_xpad *xpad;
273 struct input_dev *input_dev;
1da177e4 274 struct usb_endpoint_descriptor *ep_irq_in;
1da177e4 275 int i;
05f091ab 276
1da177e4
LT
277 for (i = 0; xpad_device[i].idVendor; i++) {
278 if ((le16_to_cpu(udev->descriptor.idVendor) == xpad_device[i].idVendor) &&
279 (le16_to_cpu(udev->descriptor.idProduct) == xpad_device[i].idProduct))
280 break;
281 }
05f091ab 282
c5b7c7c3
DT
283 xpad = kzalloc(sizeof(struct usb_xpad), GFP_KERNEL);
284 input_dev = input_allocate_device();
285 if (!xpad || !input_dev)
286 goto fail1;
05f091ab 287
1da177e4
LT
288 xpad->idata = usb_buffer_alloc(udev, XPAD_PKT_LEN,
289 SLAB_ATOMIC, &xpad->idata_dma);
c5b7c7c3
DT
290 if (!xpad->idata)
291 goto fail1;
1da177e4
LT
292
293 xpad->irq_in = usb_alloc_urb(0, GFP_KERNEL);
c5b7c7c3
DT
294 if (!xpad->irq_in)
295 goto fail2;
05f091ab 296
1da177e4 297 xpad->udev = udev;
deb8ee43
DC
298 xpad->dpad_mapping = xpad_device[i].dpad_mapping;
299 if (xpad->dpad_mapping == MAP_DPAD_UNKNOWN)
300 xpad->dpad_mapping = dpad_to_buttons;
c5b7c7c3
DT
301 xpad->dev = input_dev;
302 usb_make_path(udev, xpad->phys, sizeof(xpad->phys));
303 strlcat(xpad->phys, "/input0", sizeof(xpad->phys));
05f091ab 304
c5b7c7c3
DT
305 input_dev->name = xpad_device[i].name;
306 input_dev->phys = xpad->phys;
307 usb_to_input_id(udev, &input_dev->id);
308 input_dev->cdev.dev = &intf->dev;
309 input_dev->private = xpad;
310 input_dev->open = xpad_open;
311 input_dev->close = xpad_close;
05f091ab 312
c5b7c7c3 313 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
05f091ab 314
deb8ee43 315 /* set up buttons */
1da177e4 316 for (i = 0; xpad_btn[i] >= 0; i++)
c5b7c7c3 317 set_bit(xpad_btn[i], input_dev->keybit);
deb8ee43
DC
318 if (xpad->dpad_mapping == MAP_DPAD_TO_BUTTONS)
319 for (i = 0; xpad_btn_pad[i] >= 0; i++)
320 set_bit(xpad_btn_pad[i], input_dev->keybit);
05f091ab 321
deb8ee43
DC
322 /* set up axes */
323 for (i = 0; xpad_abs[i] >= 0; i++)
324 xpad_set_up_abs(input_dev, xpad_abs[i]);
325 if (xpad->dpad_mapping == MAP_DPAD_TO_AXES)
326 for (i = 0; xpad_abs_pad[i] >= 0; i++)
327 xpad_set_up_abs(input_dev, xpad_abs_pad[i]);
05f091ab 328
c5b7c7c3
DT
329 ep_irq_in = &intf->cur_altsetting->endpoint[0].desc;
330 usb_fill_int_urb(xpad->irq_in, udev,
331 usb_rcvintpipe(udev, ep_irq_in->bEndpointAddress),
332 xpad->idata, XPAD_PKT_LEN, xpad_irq_in,
333 xpad, ep_irq_in->bInterval);
334 xpad->irq_in->transfer_dma = xpad->idata_dma;
335 xpad->irq_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
05f091ab 336
c5b7c7c3 337 input_register_device(xpad->dev);
05f091ab 338
1da177e4
LT
339 usb_set_intfdata(intf, xpad);
340 return 0;
c5b7c7c3
DT
341
342fail2: usb_buffer_free(udev, XPAD_PKT_LEN, xpad->idata, xpad->idata_dma);
343fail1: input_free_device(input_dev);
344 kfree(xpad);
345 return -ENOMEM;
346
1da177e4
LT
347}
348
349static void xpad_disconnect(struct usb_interface *intf)
350{
351 struct usb_xpad *xpad = usb_get_intfdata (intf);
05f091ab 352
1da177e4
LT
353 usb_set_intfdata(intf, NULL);
354 if (xpad) {
355 usb_kill_urb(xpad->irq_in);
c5b7c7c3 356 input_unregister_device(xpad->dev);
1da177e4 357 usb_free_urb(xpad->irq_in);
deb8ee43
DC
358 usb_buffer_free(interface_to_usbdev(intf), XPAD_PKT_LEN,
359 xpad->idata, xpad->idata_dma);
1da177e4
LT
360 kfree(xpad);
361 }
362}
363
364static struct usb_driver xpad_driver = {
1da177e4
LT
365 .name = "xpad",
366 .probe = xpad_probe,
367 .disconnect = xpad_disconnect,
368 .id_table = xpad_table,
369};
370
371static int __init usb_xpad_init(void)
372{
373 int result = usb_register(&xpad_driver);
374 if (result == 0)
375 info(DRIVER_DESC ":" DRIVER_VERSION);
376 return result;
377}
378
379static void __exit usb_xpad_exit(void)
380{
381 usb_deregister(&xpad_driver);
382}
383
384module_init(usb_xpad_init);
385module_exit(usb_xpad_exit);
386
387MODULE_AUTHOR(DRIVER_AUTHOR);
388MODULE_DESCRIPTION(DRIVER_DESC);
389MODULE_LICENSE("GPL");