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