2 * X-Box gamepad - v0.0.6
4 * Copyright (c) 2002 Marko Friedemann <mfr@bmx-chemnitz.de>
5 * 2005 Dominic Cerquetti <binary1230@yahoo.com>
6 * 2006 Adam Buchbinder <adam.buchbinder@gmail.com>
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.
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.
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
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
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
34 * - fine tune axes (especially trigger axes)
35 * - fix "analog" buttons (reported as digital now)
36 * - get rumble working
37 * - need USB IDs for other dance pads
41 * 2002-06-27 - 0.0.1 : first version, just said "XBOX HID controller"
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
47 * 2002-07-14 - 0.0.3 : rework by Vojtech Pavlik
49 * - usb + input init sequence fixes
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
56 * 2002-07-17 - 0.0.5 : simplified d-pad handling
59 #include <linux/kernel.h>
60 #include <linux/init.h>
61 #include <linux/slab.h>
62 #include <linux/stat.h>
63 #include <linux/module.h>
64 #include <linux/moduleparam.h>
65 #include <linux/smp_lock.h>
66 #include <linux/usb/input.h>
68 #define DRIVER_VERSION "v0.0.6"
69 #define DRIVER_AUTHOR "Marko Friedemann <mfr@bmx-chemnitz.de>"
70 #define DRIVER_DESC "X-Box pad driver"
72 #define XPAD_PKT_LEN 32
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
80 static int dpad_to_buttons;
81 module_param(dpad_to_buttons, bool, S_IRUGO);
82 MODULE_PARM_DESC(dpad_to_buttons, "Map D-PAD to buttons rather than axes for unknown pads");
84 static const struct xpad_device {
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 }
98 static const signed short xpad_btn[] = {
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 */
104 /* only used if MAP_DPAD_TO_BUTTONS */
105 static 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 */
111 static const signed short xpad_abs[] = {
112 ABS_X, ABS_Y, /* left stick */
113 ABS_RX, ABS_RY, /* right stick */
114 ABS_Z, ABS_RZ, /* triggers left/right */
115 -1 /* terminating entry */
118 /* only used if MAP_DPAD_TO_AXES */
119 static const signed short xpad_abs_pad[] = {
120 ABS_HAT0X, ABS_HAT0Y, /* d-pad axes */
121 -1 /* terminating entry */
124 static struct usb_device_id xpad_table [] = {
125 { USB_INTERFACE_INFO('X', 'B', 0) }, /* X-Box USB-IF not approved class */
129 MODULE_DEVICE_TABLE (usb, xpad_table);
132 struct input_dev *dev; /* input device interface */
133 struct usb_device *udev; /* usb device */
135 struct urb *irq_in; /* urb for interrupt in report */
136 unsigned char *idata; /* input data */
137 dma_addr_t idata_dma;
139 char phys[65]; /* physical device path */
141 int dpad_mapping; /* map d-pad to buttons or to axes */
145 * xpad_process_packet
147 * Completes a request by converting the data into events for the
150 * The used report descriptor was taken from ITO Takayukis website:
151 * http://euc.jp/periphs/xbox-controller.ja.html
154 static void xpad_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data)
156 struct input_dev *dev = xpad->dev;
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]));
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]));
166 /* triggers left/right */
167 input_report_abs(dev, ABS_Z, data[10]);
168 input_report_abs(dev, ABS_RZ, data[11]);
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
181 /* start/back buttons and stick press left/right */
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);
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]);
193 /* "analog" buttons black, white */
194 input_report_key(dev, BTN_C, data[8]);
195 input_report_key(dev, BTN_Z, data[9]);
200 static void xpad_irq_in(struct urb *urb)
202 struct usb_xpad *xpad = urb->context;
205 switch (urb->status) {
212 /* this urb is terminated, clean up */
213 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
216 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
220 xpad_process_packet(xpad, 0, xpad->idata);
223 retval = usb_submit_urb (urb, GFP_ATOMIC);
225 err ("%s - usb_submit_urb failed with result %d",
226 __FUNCTION__, retval);
229 static int xpad_open (struct input_dev *dev)
231 struct usb_xpad *xpad = dev->private;
233 xpad->irq_in->dev = xpad->udev;
234 if (usb_submit_urb(xpad->irq_in, GFP_KERNEL))
240 static void xpad_close (struct input_dev *dev)
242 struct usb_xpad *xpad = dev->private;
244 usb_kill_urb(xpad->irq_in);
247 static void xpad_set_up_abs(struct input_dev *input_dev, signed short abs)
249 set_bit(abs, input_dev->absbit);
255 case ABS_RY: /* the two sticks */
256 input_set_abs_params(input_dev, abs, -32768, 32767, 16, 128);
259 case ABS_RZ: /* the triggers */
260 input_set_abs_params(input_dev, abs, 0, 255, 0, 0);
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);
269 static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id)
271 struct usb_device *udev = interface_to_usbdev (intf);
272 struct usb_xpad *xpad;
273 struct input_dev *input_dev;
274 struct usb_endpoint_descriptor *ep_irq_in;
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))
283 xpad = kzalloc(sizeof(struct usb_xpad), GFP_KERNEL);
284 input_dev = input_allocate_device();
285 if (!xpad || !input_dev)
288 xpad->idata = usb_buffer_alloc(udev, XPAD_PKT_LEN,
289 SLAB_ATOMIC, &xpad->idata_dma);
293 xpad->irq_in = usb_alloc_urb(0, GFP_KERNEL);
298 xpad->dpad_mapping = xpad_device[i].dpad_mapping;
299 if (xpad->dpad_mapping == MAP_DPAD_UNKNOWN)
300 xpad->dpad_mapping = dpad_to_buttons;
301 xpad->dev = input_dev;
302 usb_make_path(udev, xpad->phys, sizeof(xpad->phys));
303 strlcat(xpad->phys, "/input0", sizeof(xpad->phys));
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;
313 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
316 for (i = 0; xpad_btn[i] >= 0; i++)
317 set_bit(xpad_btn[i], input_dev->keybit);
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);
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]);
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;
337 input_register_device(xpad->dev);
339 usb_set_intfdata(intf, xpad);
342 fail2: usb_buffer_free(udev, XPAD_PKT_LEN, xpad->idata, xpad->idata_dma);
343 fail1: input_free_device(input_dev);
349 static void xpad_disconnect(struct usb_interface *intf)
351 struct usb_xpad *xpad = usb_get_intfdata (intf);
353 usb_set_intfdata(intf, NULL);
355 usb_kill_urb(xpad->irq_in);
356 input_unregister_device(xpad->dev);
357 usb_free_urb(xpad->irq_in);
358 usb_buffer_free(interface_to_usbdev(intf), XPAD_PKT_LEN,
359 xpad->idata, xpad->idata_dma);
364 static struct usb_driver xpad_driver = {
367 .disconnect = xpad_disconnect,
368 .id_table = xpad_table,
371 static int __init usb_xpad_init(void)
373 int result = usb_register(&xpad_driver);
375 info(DRIVER_DESC ":" DRIVER_VERSION);
379 static void __exit usb_xpad_exit(void)
381 usb_deregister(&xpad_driver);
384 module_init(usb_xpad_init);
385 module_exit(usb_xpad_exit);
387 MODULE_AUTHOR(DRIVER_AUTHOR);
388 MODULE_DESCRIPTION(DRIVER_DESC);
389 MODULE_LICENSE("GPL");