Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 2 | * X-Box gamepad - v0.0.6 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2002 Marko Friedemann <mfr@bmx-chemnitz.de> |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 5 | * 2005 Dominic Cerquetti <binary1230@yahoo.com> |
| 6 | * 2006 Adam Buchbinder <adam.buchbinder@gmail.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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: |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 34 | * - fine tune axes (especially trigger axes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | * - fix "analog" buttons (reported as digital now) |
| 36 | * - get rumble working |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 37 | * - need USB IDs for other dance pads |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #include <linux/init.h> |
| 61 | #include <linux/slab.h> |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 62 | #include <linux/stat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | #include <linux/module.h> |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 64 | #include <linux/moduleparam.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | #include <linux/smp_lock.h> |
David Brownell | ae0dadc | 2006-06-13 10:04:34 -0700 | [diff] [blame] | 66 | #include <linux/usb/input.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 68 | #define DRIVER_VERSION "v0.0.6" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 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 | |
Arjan van de Ven | 4c4c943 | 2005-11-29 09:43:42 +0100 | [diff] [blame] | 84 | static const struct xpad_device { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | u16 idVendor; |
| 86 | u16 idProduct; |
| 87 | char *name; |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 88 | u8 dpad_mapping; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } xpad_device[] = { |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 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 } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
Arjan van de Ven | 4c4c943 | 2005-11-29 09:43:42 +0100 | [diff] [blame] | 98 | static const signed short xpad_btn[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 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 | |
Arjan van de Ven | 4c4c943 | 2005-11-29 09:43:42 +0100 | [diff] [blame] | 111 | static const signed short xpad_abs[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | ABS_X, ABS_Y, /* left stick */ |
| 113 | ABS_RX, ABS_RY, /* right stick */ |
| 114 | ABS_Z, ABS_RZ, /* triggers left/right */ |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 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 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 { |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 132 | struct input_dev *dev; /* input device interface */ |
| 133 | struct usb_device *udev; /* usb device */ |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 134 | |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 135 | struct urb *irq_in; /* urb for interrupt in report */ |
| 136 | unsigned char *idata; /* input data */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | dma_addr_t idata_dma; |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 138 | |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 139 | char phys[65]; /* physical device path */ |
| 140 | |
| 141 | int dpad_mapping; /* map d-pad to buttons or to axes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 154 | static void xpad_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | { |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 156 | struct input_dev *dev = xpad->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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])); |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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])); |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | /* triggers left/right */ |
| 167 | input_report_abs(dev, ABS_Z, data[10]); |
| 168 | input_report_abs(dev, ABS_RZ, data[11]); |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | /* digital pad */ |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 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 | } |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | /* start/back buttons and stick press left/right */ |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 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); |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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]); |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 200 | static void xpad_irq_in(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | { |
| 202 | struct usb_xpad *xpad = urb->context; |
| 203 | int retval; |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 219 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 220 | xpad_process_packet(xpad, 0, xpad->idata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | xpad->irq_in->dev = xpad->udev; |
Dmitry Torokhov | 65cde54 | 2005-05-29 02:29:38 -0500 | [diff] [blame] | 234 | if (usb_submit_urb(xpad->irq_in, GFP_KERNEL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | return -EIO; |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | static void xpad_close (struct input_dev *dev) |
| 241 | { |
| 242 | struct usb_xpad *xpad = dev->private; |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 243 | |
Dmitry Torokhov | 65cde54 | 2005-05-29 02:29:38 -0500 | [diff] [blame] | 244 | usb_kill_urb(xpad->irq_in); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 272 | struct usb_xpad *xpad; |
| 273 | struct input_dev *input_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | struct usb_endpoint_descriptor *ep_irq_in; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | int i; |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 282 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 283 | xpad = kzalloc(sizeof(struct usb_xpad), GFP_KERNEL); |
| 284 | input_dev = input_allocate_device(); |
| 285 | if (!xpad || !input_dev) |
| 286 | goto fail1; |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 287 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | xpad->idata = usb_buffer_alloc(udev, XPAD_PKT_LEN, |
| 289 | SLAB_ATOMIC, &xpad->idata_dma); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 290 | if (!xpad->idata) |
| 291 | goto fail1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
| 293 | xpad->irq_in = usb_alloc_urb(0, GFP_KERNEL); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 294 | if (!xpad->irq_in) |
| 295 | goto fail2; |
| 296 | |
| 297 | xpad->udev = udev; |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 298 | xpad->dpad_mapping = xpad_device[i].dpad_mapping; |
| 299 | if (xpad->dpad_mapping == MAP_DPAD_UNKNOWN) |
| 300 | xpad->dpad_mapping = dpad_to_buttons; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 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 | |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 315 | /* set up buttons */ |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 316 | for (i = 0; xpad_btn[i] >= 0; i++) |
| 317 | set_bit(xpad_btn[i], input_dev->keybit); |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 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); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 321 | |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 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]); |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 328 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | ep_irq_in = &intf->cur_altsetting->endpoint[0].desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 336 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 337 | input_register_device(xpad->dev); |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 338 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | usb_set_intfdata(intf, xpad); |
| 340 | return 0; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | static void xpad_disconnect(struct usb_interface *intf) |
| 350 | { |
| 351 | struct usb_xpad *xpad = usb_get_intfdata (intf); |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 352 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | usb_set_intfdata(intf, NULL); |
| 354 | if (xpad) { |
| 355 | usb_kill_urb(xpad->irq_in); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 356 | input_unregister_device(xpad->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | usb_free_urb(xpad->irq_in); |
Dominic Cerquetti | deb8ee4 | 2006-10-10 14:42:48 -0700 | [diff] [blame] | 358 | usb_buffer_free(interface_to_usbdev(intf), XPAD_PKT_LEN, |
| 359 | xpad->idata, xpad->idata_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | kfree(xpad); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | static struct usb_driver xpad_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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"); |