Merge branch 'we21-fix' of git://git.kernel.org/pub/scm/linux/kernel/git/linville...
[pandora-kernel.git] / drivers / usb / input / xpad.c
1 /*
2  * X-Box gamepad - v0.0.6
3  *
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>
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:
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
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
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>
67
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"
71
72 #define XPAD_PKT_LEN 32
73
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
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");
83
84 static const struct xpad_device {
85         u16 idVendor;
86         u16 idProduct;
87         char *name;
88         u8 dpad_mapping;
89 } 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 }
96 };
97
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 */
102 };
103
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 */
109 };
110
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 */
116 };
117
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 */
122 };
123
124 static struct usb_device_id xpad_table [] = {
125         { USB_INTERFACE_INFO('X', 'B', 0) },    /* X-Box USB-IF not approved class */
126         { }
127 };
128
129 MODULE_DEVICE_TABLE (usb, xpad_table);
130
131 struct usb_xpad {
132         struct input_dev *dev;          /* input device interface */
133         struct usb_device *udev;        /* usb device */
134
135         struct urb *irq_in;             /* urb for interrupt in report */
136         unsigned char *idata;           /* input data */
137         dma_addr_t idata_dma;
138
139         char phys[65];                  /* physical device path */
140
141         int dpad_mapping;               /* map d-pad to buttons or to axes */
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
154 static void xpad_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data)
155 {
156         struct input_dev *dev = xpad->dev;
157
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]));
161
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]));
165
166         /* triggers left/right */
167         input_report_abs(dev, ABS_Z, data[10]);
168         input_report_abs(dev, ABS_RZ, data[11]);
169
170         /* digital pad */
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         }
180
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);
186
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]);
192
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
200 static void xpad_irq_in(struct urb *urb)
201 {
202         struct usb_xpad *xpad = urb->context;
203         int retval;
204
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         }
219
220         xpad_process_packet(xpad, 0, xpad->idata);
221
222 exit:
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
229 static int xpad_open (struct input_dev *dev)
230 {
231         struct usb_xpad *xpad = dev->private;
232
233         xpad->irq_in->dev = xpad->udev;
234         if (usb_submit_urb(xpad->irq_in, GFP_KERNEL))
235                 return -EIO;
236
237         return 0;
238 }
239
240 static void xpad_close (struct input_dev *dev)
241 {
242         struct usb_xpad *xpad = dev->private;
243
244         usb_kill_urb(xpad->irq_in);
245 }
246
247 static 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
269 static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id)
270 {
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;
275         int i;
276
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         }
282
283         xpad = kzalloc(sizeof(struct usb_xpad), GFP_KERNEL);
284         input_dev = input_allocate_device();
285         if (!xpad || !input_dev)
286                 goto fail1;
287
288         xpad->idata = usb_buffer_alloc(udev, XPAD_PKT_LEN,
289                                        SLAB_ATOMIC, &xpad->idata_dma);
290         if (!xpad->idata)
291                 goto fail1;
292
293         xpad->irq_in = usb_alloc_urb(0, GFP_KERNEL);
294         if (!xpad->irq_in)
295                 goto fail2;
296
297         xpad->udev = udev;
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));
304
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;
312
313         input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
314
315         /* set up buttons */
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);
321
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]);
328
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;
336
337         input_register_device(xpad->dev);
338
339         usb_set_intfdata(intf, xpad);
340         return 0;
341
342 fail2:  usb_buffer_free(udev, XPAD_PKT_LEN, xpad->idata, xpad->idata_dma);
343 fail1:  input_free_device(input_dev);
344         kfree(xpad);
345         return -ENOMEM;
346
347 }
348
349 static void xpad_disconnect(struct usb_interface *intf)
350 {
351         struct usb_xpad *xpad = usb_get_intfdata (intf);
352
353         usb_set_intfdata(intf, NULL);
354         if (xpad) {
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);
360                 kfree(xpad);
361         }
362 }
363
364 static struct usb_driver xpad_driver = {
365         .name           = "xpad",
366         .probe          = xpad_probe,
367         .disconnect     = xpad_disconnect,
368         .id_table       = xpad_table,
369 };
370
371 static 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
379 static void __exit usb_xpad_exit(void)
380 {
381         usb_deregister(&xpad_driver);
382 }
383
384 module_init(usb_xpad_init);
385 module_exit(usb_xpad_exit);
386
387 MODULE_AUTHOR(DRIVER_AUTHOR);
388 MODULE_DESCRIPTION(DRIVER_DESC);
389 MODULE_LICENSE("GPL");