Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * USB HID support for Linux |
| 3 | * |
| 4 | * Copyright (c) 1999 Andreas Gal |
Michael Haboustak | bf0964d | 2005-09-05 00:12:01 -0500 | [diff] [blame] | 5 | * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz> |
| 6 | * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc |
Jiri Kosina | b55fd23 | 2007-02-21 19:27:49 +0100 | [diff] [blame] | 7 | * Copyright (c) 2006-2007 Jiri Kosina |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * This program is free software; you can redistribute it and/or modify it |
| 12 | * under the terms of the GNU General Public License as published by the Free |
| 13 | * Software Foundation; either version 2 of the License, or (at your option) |
| 14 | * any later version. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/list.h> |
| 22 | #include <linux/mm.h> |
| 23 | #include <linux/smp_lock.h> |
| 24 | #include <linux/spinlock.h> |
| 25 | #include <asm/unaligned.h> |
| 26 | #include <asm/byteorder.h> |
| 27 | #include <linux/input.h> |
| 28 | #include <linux/wait.h> |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/usb.h> |
| 31 | |
Jiri Kosina | dde5845 | 2006-12-08 18:40:44 +0100 | [diff] [blame] | 32 | #include <linux/hid.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/hiddev.h> |
Jiri Kosina | c080d89 | 2007-01-25 11:43:31 +0100 | [diff] [blame] | 34 | #include <linux/hid-debug.h> |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 35 | #include "usbhid.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | /* |
| 38 | * Version Information |
| 39 | */ |
| 40 | |
Michael Haboustak | bf0964d | 2005-09-05 00:12:01 -0500 | [diff] [blame] | 41 | #define DRIVER_VERSION "v2.6" |
Jiri Kosina | f142b3a4 | 2007-04-16 11:29:28 +0200 | [diff] [blame] | 42 | #define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik, Jiri Kosina" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #define DRIVER_DESC "USB HID core driver" |
| 44 | #define DRIVER_LICENSE "GPL" |
| 45 | |
| 46 | static char *hid_types[] = {"Device", "Pointer", "Mouse", "Device", "Joystick", |
| 47 | "Gamepad", "Keyboard", "Keypad", "Multi-Axis Controller"}; |
| 48 | /* |
| 49 | * Module parameters. |
| 50 | */ |
| 51 | |
| 52 | static unsigned int hid_mousepoll_interval; |
| 53 | module_param_named(mousepoll, hid_mousepoll_interval, uint, 0644); |
| 54 | MODULE_PARM_DESC(mousepoll, "Polling interval of mice"); |
| 55 | |
Paul Walmsley | 876b927 | 2007-04-19 14:56:12 +0200 | [diff] [blame^] | 56 | /* Quirks specified at module load time */ |
| 57 | static char *quirks_param[MAX_USBHID_BOOT_QUIRKS] = { [ 0 ... (MAX_USBHID_BOOT_QUIRKS - 1) ] = NULL }; |
| 58 | module_param_array_named(quirks, quirks_param, charp, NULL, 0444); |
| 59 | MODULE_PARM_DESC(quirks, "Add/modify USB HID quirks by specifying " |
| 60 | " quirks=vendorID:productID:quirks" |
| 61 | " where vendorID, productID, and quirks are all in" |
| 62 | " 0x-prefixed hex"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | /* |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 64 | * Input submission and I/O error handler. |
| 65 | */ |
| 66 | |
| 67 | static void hid_io_error(struct hid_device *hid); |
| 68 | |
| 69 | /* Start up the input URB */ |
| 70 | static int hid_start_in(struct hid_device *hid) |
| 71 | { |
| 72 | unsigned long flags; |
| 73 | int rc = 0; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 74 | struct usbhid_device *usbhid = hid->driver_data; |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 75 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 76 | spin_lock_irqsave(&usbhid->inlock, flags); |
| 77 | if (hid->open > 0 && !test_bit(HID_SUSPENDED, &usbhid->iofl) && |
| 78 | !test_and_set_bit(HID_IN_RUNNING, &usbhid->iofl)) { |
| 79 | rc = usb_submit_urb(usbhid->urbin, GFP_ATOMIC); |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 80 | if (rc != 0) |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 81 | clear_bit(HID_IN_RUNNING, &usbhid->iofl); |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 82 | } |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 83 | spin_unlock_irqrestore(&usbhid->inlock, flags); |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 84 | return rc; |
| 85 | } |
| 86 | |
| 87 | /* I/O retry timer routine */ |
| 88 | static void hid_retry_timeout(unsigned long _hid) |
| 89 | { |
| 90 | struct hid_device *hid = (struct hid_device *) _hid; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 91 | struct usbhid_device *usbhid = hid->driver_data; |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 92 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 93 | dev_dbg(&usbhid->intf->dev, "retrying intr urb\n"); |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 94 | if (hid_start_in(hid)) |
| 95 | hid_io_error(hid); |
| 96 | } |
| 97 | |
Alan Stern | 6d8fc4d | 2006-10-18 12:35:24 -0400 | [diff] [blame] | 98 | /* Workqueue routine to reset the device or clear a halt */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 99 | static void hid_reset(struct work_struct *work) |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 100 | { |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 101 | struct usbhid_device *usbhid = |
| 102 | container_of(work, struct usbhid_device, reset_work); |
| 103 | struct hid_device *hid = usbhid->hid; |
Alan Stern | 6d8fc4d | 2006-10-18 12:35:24 -0400 | [diff] [blame] | 104 | int rc_lock, rc = 0; |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 105 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 106 | if (test_bit(HID_CLEAR_HALT, &usbhid->iofl)) { |
| 107 | dev_dbg(&usbhid->intf->dev, "clear halt\n"); |
Anssi Hannula | be82097 | 2007-01-19 19:28:17 +0200 | [diff] [blame] | 108 | rc = usb_clear_halt(hid_to_usb_dev(hid), usbhid->urbin->pipe); |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 109 | clear_bit(HID_CLEAR_HALT, &usbhid->iofl); |
Alan Stern | 6d8fc4d | 2006-10-18 12:35:24 -0400 | [diff] [blame] | 110 | hid_start_in(hid); |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 111 | } |
Alan Stern | 6d8fc4d | 2006-10-18 12:35:24 -0400 | [diff] [blame] | 112 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 113 | else if (test_bit(HID_RESET_PENDING, &usbhid->iofl)) { |
| 114 | dev_dbg(&usbhid->intf->dev, "resetting device\n"); |
Anssi Hannula | be82097 | 2007-01-19 19:28:17 +0200 | [diff] [blame] | 115 | rc = rc_lock = usb_lock_device_for_reset(hid_to_usb_dev(hid), usbhid->intf); |
Alan Stern | 6d8fc4d | 2006-10-18 12:35:24 -0400 | [diff] [blame] | 116 | if (rc_lock >= 0) { |
Anssi Hannula | be82097 | 2007-01-19 19:28:17 +0200 | [diff] [blame] | 117 | rc = usb_reset_composite_device(hid_to_usb_dev(hid), usbhid->intf); |
Alan Stern | 6d8fc4d | 2006-10-18 12:35:24 -0400 | [diff] [blame] | 118 | if (rc_lock) |
Anssi Hannula | be82097 | 2007-01-19 19:28:17 +0200 | [diff] [blame] | 119 | usb_unlock_device(hid_to_usb_dev(hid)); |
Alan Stern | 6d8fc4d | 2006-10-18 12:35:24 -0400 | [diff] [blame] | 120 | } |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 121 | clear_bit(HID_RESET_PENDING, &usbhid->iofl); |
Alan Stern | 6d8fc4d | 2006-10-18 12:35:24 -0400 | [diff] [blame] | 122 | } |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 123 | |
Alan Stern | df9a1f4 | 2006-06-01 13:55:28 -0400 | [diff] [blame] | 124 | switch (rc) { |
| 125 | case 0: |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 126 | if (!test_bit(HID_IN_RUNNING, &usbhid->iofl)) |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 127 | hid_io_error(hid); |
Alan Stern | df9a1f4 | 2006-06-01 13:55:28 -0400 | [diff] [blame] | 128 | break; |
| 129 | default: |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 130 | err("can't reset device, %s-%s/input%d, status %d", |
Anssi Hannula | be82097 | 2007-01-19 19:28:17 +0200 | [diff] [blame] | 131 | hid_to_usb_dev(hid)->bus->bus_name, |
| 132 | hid_to_usb_dev(hid)->devpath, |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 133 | usbhid->ifnum, rc); |
Alan Stern | df9a1f4 | 2006-06-01 13:55:28 -0400 | [diff] [blame] | 134 | /* FALLTHROUGH */ |
| 135 | case -EHOSTUNREACH: |
| 136 | case -ENODEV: |
| 137 | case -EINTR: |
| 138 | break; |
| 139 | } |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /* Main I/O error handler */ |
| 143 | static void hid_io_error(struct hid_device *hid) |
| 144 | { |
| 145 | unsigned long flags; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 146 | struct usbhid_device *usbhid = hid->driver_data; |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 147 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 148 | spin_lock_irqsave(&usbhid->inlock, flags); |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 149 | |
| 150 | /* Stop when disconnected */ |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 151 | if (usb_get_intfdata(usbhid->intf) == NULL) |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 152 | goto done; |
| 153 | |
Alan Stern | 5e2a55f | 2007-03-20 19:03:31 +0100 | [diff] [blame] | 154 | /* If it has been a while since the last error, we'll assume |
| 155 | * this a brand new error and reset the retry timeout. */ |
| 156 | if (time_after(jiffies, usbhid->stop_retry + HZ/2)) |
| 157 | usbhid->retry_delay = 0; |
| 158 | |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 159 | /* When an error occurs, retry at increasing intervals */ |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 160 | if (usbhid->retry_delay == 0) { |
| 161 | usbhid->retry_delay = 13; /* Then 26, 52, 104, 104, ... */ |
| 162 | usbhid->stop_retry = jiffies + msecs_to_jiffies(1000); |
| 163 | } else if (usbhid->retry_delay < 100) |
| 164 | usbhid->retry_delay *= 2; |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 165 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 166 | if (time_after(jiffies, usbhid->stop_retry)) { |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 167 | |
| 168 | /* Retries failed, so do a port reset */ |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 169 | if (!test_and_set_bit(HID_RESET_PENDING, &usbhid->iofl)) { |
| 170 | schedule_work(&usbhid->reset_work); |
Alan Stern | 6d8fc4d | 2006-10-18 12:35:24 -0400 | [diff] [blame] | 171 | goto done; |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 175 | mod_timer(&usbhid->io_retry, |
| 176 | jiffies + msecs_to_jiffies(usbhid->retry_delay)); |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 177 | done: |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 178 | spin_unlock_irqrestore(&usbhid->inlock, flags); |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | * Input interrupt completion handler. |
| 183 | */ |
| 184 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 185 | static void hid_irq_in(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | { |
| 187 | struct hid_device *hid = urb->context; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 188 | struct usbhid_device *usbhid = hid->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | int status; |
| 190 | |
| 191 | switch (urb->status) { |
| 192 | case 0: /* success */ |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 193 | usbhid->retry_delay = 0; |
Jiri Kosina | aa8de2f | 2006-12-08 18:41:17 +0100 | [diff] [blame] | 194 | hid_input_report(urb->context, HID_INPUT_REPORT, |
| 195 | urb->transfer_buffer, |
| 196 | urb->actual_length, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | break; |
Alan Stern | 6d8fc4d | 2006-10-18 12:35:24 -0400 | [diff] [blame] | 198 | case -EPIPE: /* stall */ |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 199 | clear_bit(HID_IN_RUNNING, &usbhid->iofl); |
| 200 | set_bit(HID_CLEAR_HALT, &usbhid->iofl); |
| 201 | schedule_work(&usbhid->reset_work); |
Alan Stern | 6d8fc4d | 2006-10-18 12:35:24 -0400 | [diff] [blame] | 202 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | case -ECONNRESET: /* unlink */ |
| 204 | case -ENOENT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | case -ESHUTDOWN: /* unplug */ |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 206 | clear_bit(HID_IN_RUNNING, &usbhid->iofl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | return; |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 208 | case -EILSEQ: /* protocol error or unplug */ |
| 209 | case -EPROTO: /* protocol error or unplug */ |
Pete Zaitcev | 38e2bfc | 2006-09-18 22:49:02 -0700 | [diff] [blame] | 210 | case -ETIME: /* protocol error or unplug */ |
| 211 | case -ETIMEDOUT: /* Should never happen, but... */ |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 212 | clear_bit(HID_IN_RUNNING, &usbhid->iofl); |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 213 | hid_io_error(hid); |
| 214 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | default: /* error */ |
| 216 | warn("input irq status %d received", urb->status); |
| 217 | } |
| 218 | |
Christoph Lameter | 54e6ecb | 2006-12-06 20:33:16 -0800 | [diff] [blame] | 219 | status = usb_submit_urb(urb, GFP_ATOMIC); |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 220 | if (status) { |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 221 | clear_bit(HID_IN_RUNNING, &usbhid->iofl); |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 222 | if (status != -EPERM) { |
| 223 | err("can't resubmit intr, %s-%s/input%d, status %d", |
Anssi Hannula | be82097 | 2007-01-19 19:28:17 +0200 | [diff] [blame] | 224 | hid_to_usb_dev(hid)->bus->bus_name, |
| 225 | hid_to_usb_dev(hid)->devpath, |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 226 | usbhid->ifnum, status); |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 227 | hid_io_error(hid); |
| 228 | } |
| 229 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | static int hid_submit_out(struct hid_device *hid) |
| 233 | { |
| 234 | struct hid_report *report; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 235 | struct usbhid_device *usbhid = hid->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 237 | report = usbhid->out[usbhid->outtail]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 239 | hid_output_report(report, usbhid->outbuf); |
| 240 | usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0); |
Anssi Hannula | be82097 | 2007-01-19 19:28:17 +0200 | [diff] [blame] | 241 | usbhid->urbout->dev = hid_to_usb_dev(hid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
| 243 | dbg("submitting out urb"); |
| 244 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 245 | if (usb_submit_urb(usbhid->urbout, GFP_ATOMIC)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | err("usb_submit_urb(out) failed"); |
| 247 | return -1; |
| 248 | } |
| 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | static int hid_submit_ctrl(struct hid_device *hid) |
| 254 | { |
| 255 | struct hid_report *report; |
| 256 | unsigned char dir; |
| 257 | int len; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 258 | struct usbhid_device *usbhid = hid->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 260 | report = usbhid->ctrl[usbhid->ctrltail].report; |
| 261 | dir = usbhid->ctrl[usbhid->ctrltail].dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
| 263 | len = ((report->size - 1) >> 3) + 1 + (report->id > 0); |
| 264 | if (dir == USB_DIR_OUT) { |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 265 | hid_output_report(report, usbhid->ctrlbuf); |
Anssi Hannula | be82097 | 2007-01-19 19:28:17 +0200 | [diff] [blame] | 266 | usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0); |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 267 | usbhid->urbctrl->transfer_buffer_length = len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } else { |
| 269 | int maxpacket, padlen; |
| 270 | |
Anssi Hannula | be82097 | 2007-01-19 19:28:17 +0200 | [diff] [blame] | 271 | usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0); |
| 272 | maxpacket = usb_maxpacket(hid_to_usb_dev(hid), usbhid->urbctrl->pipe, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | if (maxpacket > 0) { |
| 274 | padlen = (len + maxpacket - 1) / maxpacket; |
| 275 | padlen *= maxpacket; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 276 | if (padlen > usbhid->bufsize) |
| 277 | padlen = usbhid->bufsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } else |
| 279 | padlen = 0; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 280 | usbhid->urbctrl->transfer_buffer_length = padlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | } |
Anssi Hannula | be82097 | 2007-01-19 19:28:17 +0200 | [diff] [blame] | 282 | usbhid->urbctrl->dev = hid_to_usb_dev(hid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 284 | usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir; |
| 285 | usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT : HID_REQ_GET_REPORT; |
| 286 | usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) | report->id); |
| 287 | usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum); |
| 288 | usbhid->cr->wLength = cpu_to_le16(len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | |
| 290 | dbg("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u", |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 291 | usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" : "Get_Report", |
| 292 | usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 294 | if (usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | err("usb_submit_urb(ctrl) failed"); |
| 296 | return -1; |
| 297 | } |
| 298 | |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | /* |
| 303 | * Output interrupt completion handler. |
| 304 | */ |
| 305 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 306 | static void hid_irq_out(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | { |
| 308 | struct hid_device *hid = urb->context; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 309 | struct usbhid_device *usbhid = hid->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | unsigned long flags; |
| 311 | int unplug = 0; |
| 312 | |
| 313 | switch (urb->status) { |
| 314 | case 0: /* success */ |
Vojtech Pavlik | c4786ca | 2005-09-05 00:13:03 -0500 | [diff] [blame] | 315 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | case -ESHUTDOWN: /* unplug */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | unplug = 1; |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 318 | case -EILSEQ: /* protocol error or unplug */ |
| 319 | case -EPROTO: /* protocol error or unplug */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | case -ECONNRESET: /* unlink */ |
| 321 | case -ENOENT: |
| 322 | break; |
| 323 | default: /* error */ |
| 324 | warn("output irq status %d received", urb->status); |
| 325 | } |
| 326 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 327 | spin_lock_irqsave(&usbhid->outlock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
| 329 | if (unplug) |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 330 | usbhid->outtail = usbhid->outhead; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | else |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 332 | usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 334 | if (usbhid->outhead != usbhid->outtail) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | if (hid_submit_out(hid)) { |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 336 | clear_bit(HID_OUT_RUNNING, &usbhid->iofl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | wake_up(&hid->wait); |
| 338 | } |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 339 | spin_unlock_irqrestore(&usbhid->outlock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | return; |
| 341 | } |
| 342 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 343 | clear_bit(HID_OUT_RUNNING, &usbhid->iofl); |
| 344 | spin_unlock_irqrestore(&usbhid->outlock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | wake_up(&hid->wait); |
| 346 | } |
| 347 | |
| 348 | /* |
| 349 | * Control pipe completion handler. |
| 350 | */ |
| 351 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 352 | static void hid_ctrl(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | { |
| 354 | struct hid_device *hid = urb->context; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 355 | struct usbhid_device *usbhid = hid->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | unsigned long flags; |
| 357 | int unplug = 0; |
| 358 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 359 | spin_lock_irqsave(&usbhid->ctrllock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | |
| 361 | switch (urb->status) { |
| 362 | case 0: /* success */ |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 363 | if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN) |
Jiri Kosina | aa8de2f | 2006-12-08 18:41:17 +0100 | [diff] [blame] | 364 | hid_input_report(urb->context, usbhid->ctrl[usbhid->ctrltail].report->type, |
| 365 | urb->transfer_buffer, urb->actual_length, 0); |
Vojtech Pavlik | c4786ca | 2005-09-05 00:13:03 -0500 | [diff] [blame] | 366 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | case -ESHUTDOWN: /* unplug */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | unplug = 1; |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 369 | case -EILSEQ: /* protocol error or unplug */ |
| 370 | case -EPROTO: /* protocol error or unplug */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | case -ECONNRESET: /* unlink */ |
| 372 | case -ENOENT: |
| 373 | case -EPIPE: /* report not available */ |
| 374 | break; |
| 375 | default: /* error */ |
| 376 | warn("ctrl urb status %d received", urb->status); |
| 377 | } |
| 378 | |
| 379 | if (unplug) |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 380 | usbhid->ctrltail = usbhid->ctrlhead; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | else |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 382 | usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 384 | if (usbhid->ctrlhead != usbhid->ctrltail) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | if (hid_submit_ctrl(hid)) { |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 386 | clear_bit(HID_CTRL_RUNNING, &usbhid->iofl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | wake_up(&hid->wait); |
| 388 | } |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 389 | spin_unlock_irqrestore(&usbhid->ctrllock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | return; |
| 391 | } |
| 392 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 393 | clear_bit(HID_CTRL_RUNNING, &usbhid->iofl); |
| 394 | spin_unlock_irqrestore(&usbhid->ctrllock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | wake_up(&hid->wait); |
| 396 | } |
| 397 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 398 | void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | { |
| 400 | int head; |
| 401 | unsigned long flags; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 402 | struct usbhid_device *usbhid = hid->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
| 404 | if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN) |
| 405 | return; |
| 406 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 407 | if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 409 | spin_lock_irqsave(&usbhid->outlock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 411 | if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) { |
| 412 | spin_unlock_irqrestore(&usbhid->outlock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | warn("output queue full"); |
| 414 | return; |
| 415 | } |
| 416 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 417 | usbhid->out[usbhid->outhead] = report; |
| 418 | usbhid->outhead = head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 420 | if (!test_and_set_bit(HID_OUT_RUNNING, &usbhid->iofl)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | if (hid_submit_out(hid)) |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 422 | clear_bit(HID_OUT_RUNNING, &usbhid->iofl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 424 | spin_unlock_irqrestore(&usbhid->outlock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | return; |
| 426 | } |
| 427 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 428 | spin_lock_irqsave(&usbhid->ctrllock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 430 | if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) { |
| 431 | spin_unlock_irqrestore(&usbhid->ctrllock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | warn("control queue full"); |
| 433 | return; |
| 434 | } |
| 435 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 436 | usbhid->ctrl[usbhid->ctrlhead].report = report; |
| 437 | usbhid->ctrl[usbhid->ctrlhead].dir = dir; |
| 438 | usbhid->ctrlhead = head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 440 | if (!test_and_set_bit(HID_CTRL_RUNNING, &usbhid->iofl)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | if (hid_submit_ctrl(hid)) |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 442 | clear_bit(HID_CTRL_RUNNING, &usbhid->iofl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 444 | spin_unlock_irqrestore(&usbhid->ctrllock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Jiri Kosina | 229695e | 2006-12-08 18:40:53 +0100 | [diff] [blame] | 447 | static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) |
Jiri Kosina | dde5845 | 2006-12-08 18:40:44 +0100 | [diff] [blame] | 448 | { |
| 449 | struct hid_device *hid = dev->private; |
| 450 | struct hid_field *field; |
| 451 | int offset; |
| 452 | |
| 453 | if (type == EV_FF) |
| 454 | return input_ff_event(dev, type, code, value); |
| 455 | |
| 456 | if (type != EV_LED) |
| 457 | return -1; |
| 458 | |
| 459 | if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) { |
| 460 | warn("event field not found"); |
| 461 | return -1; |
| 462 | } |
| 463 | |
| 464 | hid_set_field(field, offset, value); |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 465 | usbhid_submit_report(hid, field->report, USB_DIR_OUT); |
Jiri Kosina | dde5845 | 2006-12-08 18:40:44 +0100 | [diff] [blame] | 466 | |
| 467 | return 0; |
| 468 | } |
| 469 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 470 | int usbhid_wait_io(struct hid_device *hid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | { |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 472 | struct usbhid_device *usbhid = hid->driver_data; |
| 473 | |
| 474 | if (!wait_event_timeout(hid->wait, (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) && |
| 475 | !test_bit(HID_OUT_RUNNING, &usbhid->iofl)), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | 10*HZ)) { |
| 477 | dbg("timeout waiting for ctrl or out queue to clear"); |
| 478 | return -1; |
| 479 | } |
| 480 | |
| 481 | return 0; |
| 482 | } |
| 483 | |
Vojtech Pavlik | 854561b | 2005-05-29 02:28:00 -0500 | [diff] [blame] | 484 | static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle) |
| 485 | { |
Vojtech Pavlik | 71387bd | 2005-05-29 02:28:14 -0500 | [diff] [blame] | 486 | return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
Vojtech Pavlik | 854561b | 2005-05-29 02:28:00 -0500 | [diff] [blame] | 487 | HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report, |
| 488 | ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT); |
| 489 | } |
| 490 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | static int hid_get_class_descriptor(struct usb_device *dev, int ifnum, |
| 492 | unsigned char type, void *buf, int size) |
| 493 | { |
| 494 | int result, retries = 4; |
| 495 | |
Jiri Kosina | 43c7bf0 | 2007-01-26 12:58:24 +0100 | [diff] [blame] | 496 | memset(buf, 0, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | |
| 498 | do { |
| 499 | result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 500 | USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN, |
| 501 | (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT); |
| 502 | retries--; |
| 503 | } while (result < size && retries); |
| 504 | return result; |
| 505 | } |
| 506 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 507 | int usbhid_open(struct hid_device *hid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | { |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 509 | ++hid->open; |
| 510 | if (hid_start_in(hid)) |
| 511 | hid_io_error(hid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | return 0; |
| 513 | } |
| 514 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 515 | void usbhid_close(struct hid_device *hid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | { |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 517 | struct usbhid_device *usbhid = hid->driver_data; |
| 518 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | if (!--hid->open) |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 520 | usb_kill_urb(usbhid->urbin); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | /* |
| 524 | * Initialize all reports |
| 525 | */ |
| 526 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 527 | void usbhid_init_reports(struct hid_device *hid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | { |
| 529 | struct hid_report *report; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 530 | struct usbhid_device *usbhid = hid->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | int err, ret; |
| 532 | |
Michael Haboustak | bf0964d | 2005-09-05 00:12:01 -0500 | [diff] [blame] | 533 | list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list) |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 534 | usbhid_submit_report(hid, report, USB_DIR_IN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | |
| 536 | list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list) |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 537 | usbhid_submit_report(hid, report, USB_DIR_IN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | |
| 539 | err = 0; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 540 | ret = usbhid_wait_io(hid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | while (ret) { |
| 542 | err |= ret; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 543 | if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl)) |
| 544 | usb_kill_urb(usbhid->urbctrl); |
| 545 | if (test_bit(HID_OUT_RUNNING, &usbhid->iofl)) |
| 546 | usb_kill_urb(usbhid->urbout); |
| 547 | ret = usbhid_wait_io(hid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | if (err) |
Olaf Hering | de289fd | 2006-01-06 12:45:28 +0100 | [diff] [blame] | 551 | warn("timeout initializing reports"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Michael Haboustak | bf0964d | 2005-09-05 00:12:01 -0500 | [diff] [blame] | 554 | /* |
Pete Zaitcev | 713c8aa | 2007-04-06 14:33:18 +0200 | [diff] [blame] | 555 | * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01). |
| 556 | */ |
| 557 | static int hid_find_field_early(struct hid_device *hid, unsigned int page, |
| 558 | unsigned int hid_code, struct hid_field **pfield) |
| 559 | { |
| 560 | struct hid_report *report; |
| 561 | struct hid_field *field; |
| 562 | struct hid_usage *usage; |
| 563 | int i, j; |
| 564 | |
| 565 | list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) { |
| 566 | for (i = 0; i < report->maxfield; i++) { |
| 567 | field = report->field[i]; |
| 568 | for (j = 0; j < field->maxusage; j++) { |
| 569 | usage = &field->usage[j]; |
| 570 | if ((usage->hid & HID_USAGE_PAGE) == page && |
| 571 | (usage->hid & 0xFFFF) == hid_code) { |
| 572 | *pfield = field; |
| 573 | return j; |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | } |
| 578 | return -1; |
| 579 | } |
| 580 | |
| 581 | static void usbhid_set_leds(struct hid_device *hid) |
| 582 | { |
| 583 | struct hid_field *field; |
| 584 | int offset; |
| 585 | |
| 586 | if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) { |
| 587 | hid_set_field(field, offset, 0); |
| 588 | usbhid_submit_report(hid, field->report, USB_DIR_OUT); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | /* |
Michael Haboustak | bf0964d | 2005-09-05 00:12:01 -0500 | [diff] [blame] | 593 | * Traverse the supplied list of reports and find the longest |
| 594 | */ |
| 595 | static void hid_find_max_report(struct hid_device *hid, unsigned int type, int *max) |
| 596 | { |
| 597 | struct hid_report *report; |
| 598 | int size; |
| 599 | |
| 600 | list_for_each_entry(report, &hid->report_enum[type].report_list, list) { |
| 601 | size = ((report->size - 1) >> 3) + 1; |
| 602 | if (type == HID_INPUT_REPORT && hid->report_enum[type].numbered) |
| 603 | size++; |
| 604 | if (*max < size) |
| 605 | *max = size; |
| 606 | } |
| 607 | } |
| 608 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid) |
| 610 | { |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 611 | struct usbhid_device *usbhid = hid->driver_data; |
| 612 | |
| 613 | if (!(usbhid->inbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->inbuf_dma))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | return -1; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 615 | if (!(usbhid->outbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->outbuf_dma))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | return -1; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 617 | if (!(usbhid->cr = usb_buffer_alloc(dev, sizeof(*(usbhid->cr)), GFP_ATOMIC, &usbhid->cr_dma))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | return -1; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 619 | if (!(usbhid->ctrlbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->ctrlbuf_dma))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | return -1; |
| 621 | |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid) |
| 626 | { |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 627 | struct usbhid_device *usbhid = hid->driver_data; |
| 628 | |
| 629 | if (usbhid->inbuf) |
| 630 | usb_buffer_free(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma); |
| 631 | if (usbhid->outbuf) |
| 632 | usb_buffer_free(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma); |
| 633 | if (usbhid->cr) |
| 634 | usb_buffer_free(dev, sizeof(*(usbhid->cr)), usbhid->cr, usbhid->cr_dma); |
| 635 | if (usbhid->ctrlbuf) |
| 636 | usb_buffer_free(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | } |
| 638 | |
Vojtech Pavlik | 940824b | 2006-01-14 00:25:39 -0500 | [diff] [blame] | 639 | /* |
| 640 | * Cherry Cymotion keyboard have an invalid HID report descriptor, |
| 641 | * that needs fixing before we can parse it. |
| 642 | */ |
| 643 | |
| 644 | static void hid_fixup_cymotion_descriptor(char *rdesc, int rsize) |
| 645 | { |
| 646 | if (rsize >= 17 && rdesc[11] == 0x3c && rdesc[12] == 0x02) { |
| 647 | info("Fixing up Cherry Cymotion report descriptor"); |
| 648 | rdesc[11] = rdesc[16] = 0xff; |
| 649 | rdesc[12] = rdesc[17] = 0x03; |
| 650 | } |
| 651 | } |
| 652 | |
Geoff Levand | 4a1a4d8 | 2007-01-15 20:11:52 -0800 | [diff] [blame] | 653 | /* |
| 654 | * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller |
| 655 | * to "operational". Without this, the ps3 controller will not report any |
| 656 | * events. |
| 657 | */ |
| 658 | static void hid_fixup_sony_ps3_controller(struct usb_device *dev, int ifnum) |
| 659 | { |
| 660 | int result; |
| 661 | char *buf = kmalloc(18, GFP_KERNEL); |
| 662 | |
| 663 | if (!buf) |
| 664 | return; |
| 665 | |
| 666 | result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 667 | HID_REQ_GET_REPORT, |
| 668 | USB_DIR_IN | USB_TYPE_CLASS | |
| 669 | USB_RECIP_INTERFACE, |
| 670 | (3 << 8) | 0xf2, ifnum, buf, 17, |
| 671 | USB_CTRL_GET_TIMEOUT); |
| 672 | |
| 673 | if (result < 0) |
| 674 | err("%s failed: %d\n", __func__, result); |
| 675 | |
| 676 | kfree(buf); |
| 677 | } |
| 678 | |
Jiri Kosina | b55fd23 | 2007-02-21 19:27:49 +0100 | [diff] [blame] | 679 | /* |
Jiri Kosina | 85cbea3 | 2007-04-05 12:23:09 +0200 | [diff] [blame] | 680 | * Certain Logitech keyboards send in report #3 keys which are far |
Jiri Kosina | b55fd23 | 2007-02-21 19:27:49 +0100 | [diff] [blame] | 681 | * above the logical maximum described in descriptor. This extends |
| 682 | * the original value of 0x28c of logical maximum to 0x104d |
| 683 | */ |
Jiri Kosina | 85cbea3 | 2007-04-05 12:23:09 +0200 | [diff] [blame] | 684 | static void hid_fixup_logitech_descriptor(unsigned char *rdesc, int rsize) |
Jiri Kosina | b55fd23 | 2007-02-21 19:27:49 +0100 | [diff] [blame] | 685 | { |
| 686 | if (rsize >= 90 && rdesc[83] == 0x26 |
| 687 | && rdesc[84] == 0x8c |
| 688 | && rdesc[85] == 0x02) { |
Jiri Kosina | 85cbea3 | 2007-04-05 12:23:09 +0200 | [diff] [blame] | 689 | info("Fixing up Logitech keyboard report descriptor"); |
Jiri Kosina | b55fd23 | 2007-02-21 19:27:49 +0100 | [diff] [blame] | 690 | rdesc[84] = rdesc[89] = 0x4d; |
| 691 | rdesc[85] = rdesc[90] = 0x10; |
| 692 | } |
| 693 | } |
| 694 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | static struct hid_device *usb_hid_configure(struct usb_interface *intf) |
| 696 | { |
| 697 | struct usb_host_interface *interface = intf->cur_altsetting; |
| 698 | struct usb_device *dev = interface_to_usbdev (intf); |
| 699 | struct hid_descriptor *hdesc; |
| 700 | struct hid_device *hid; |
Paul Walmsley | 2eb5dc3 | 2007-04-19 13:27:04 +0200 | [diff] [blame] | 701 | u32 quirks = 0; |
Paul Walmsley | 4cbe7d2 | 2007-03-20 19:23:16 +0100 | [diff] [blame] | 702 | unsigned rsize = 0; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 703 | char *rdesc; |
| 704 | int n, len, insize = 0; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 705 | struct usbhid_device *usbhid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | |
Paul Walmsley | 2eb5dc3 | 2007-04-19 13:27:04 +0200 | [diff] [blame] | 707 | quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor), |
| 708 | le16_to_cpu(dev->descriptor.idProduct)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | |
Alan Stern | 0f28b55 | 2006-05-15 14:49:04 -0400 | [diff] [blame] | 710 | /* Many keyboards and mice don't like to be polled for reports, |
| 711 | * so we will always set the HID_QUIRK_NOGET flag for them. */ |
| 712 | if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) { |
| 713 | if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD || |
| 714 | interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE) |
| 715 | quirks |= HID_QUIRK_NOGET; |
| 716 | } |
| 717 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | if (quirks & HID_QUIRK_IGNORE) |
| 719 | return NULL; |
| 720 | |
Soeren Sonnenburg | a417a21 | 2007-02-05 10:06:01 +0100 | [diff] [blame] | 721 | if ((quirks & HID_QUIRK_IGNORE_MOUSE) && |
| 722 | (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)) |
| 723 | return NULL; |
| 724 | |
| 725 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 726 | if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) && |
| 727 | (!interface->desc.bNumEndpoints || |
| 728 | usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) { |
| 729 | dbg("class descriptor not present\n"); |
| 730 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | for (n = 0; n < hdesc->bNumDescriptors; n++) |
| 734 | if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT) |
| 735 | rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength); |
| 736 | |
| 737 | if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) { |
| 738 | dbg("weird size of report descriptor (%u)", rsize); |
| 739 | return NULL; |
| 740 | } |
| 741 | |
| 742 | if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) { |
| 743 | dbg("couldn't allocate rdesc memory"); |
| 744 | return NULL; |
| 745 | } |
| 746 | |
Vojtech Pavlik | 854561b | 2005-05-29 02:28:00 -0500 | [diff] [blame] | 747 | hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0); |
| 748 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | if ((n = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber, HID_DT_REPORT, rdesc, rsize)) < 0) { |
| 750 | dbg("reading report descriptor failed"); |
| 751 | kfree(rdesc); |
| 752 | return NULL; |
| 753 | } |
| 754 | |
Vojtech Pavlik | 940824b | 2006-01-14 00:25:39 -0500 | [diff] [blame] | 755 | if ((quirks & HID_QUIRK_CYMOTION)) |
| 756 | hid_fixup_cymotion_descriptor(rdesc, rsize); |
| 757 | |
Jiri Kosina | 85cbea3 | 2007-04-05 12:23:09 +0200 | [diff] [blame] | 758 | if (quirks & HID_QUIRK_LOGITECH_DESCRIPTOR) |
| 759 | hid_fixup_logitech_descriptor(rdesc, rsize); |
Jiri Kosina | b55fd23 | 2007-02-21 19:27:49 +0100 | [diff] [blame] | 760 | |
Jiri Kosina | fdc9c56 | 2007-02-19 14:15:59 +0100 | [diff] [blame] | 761 | #ifdef CONFIG_HID_DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | printk(KERN_DEBUG __FILE__ ": report descriptor (size %u, read %d) = ", rsize, n); |
| 763 | for (n = 0; n < rsize; n++) |
| 764 | printk(" %02x", (unsigned char) rdesc[n]); |
| 765 | printk("\n"); |
| 766 | #endif |
| 767 | |
| 768 | if (!(hid = hid_parse_report(rdesc, n))) { |
| 769 | dbg("parsing report descriptor failed"); |
| 770 | kfree(rdesc); |
| 771 | return NULL; |
| 772 | } |
| 773 | |
| 774 | kfree(rdesc); |
| 775 | hid->quirks = quirks; |
| 776 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 777 | if (!(usbhid = kzalloc(sizeof(struct usbhid_device), GFP_KERNEL))) |
| 778 | goto fail; |
Michael Haboustak | bf0964d | 2005-09-05 00:12:01 -0500 | [diff] [blame] | 779 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 780 | hid->driver_data = usbhid; |
| 781 | usbhid->hid = hid; |
| 782 | |
| 783 | usbhid->bufsize = HID_MIN_BUFFER_SIZE; |
| 784 | hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize); |
| 785 | hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize); |
| 786 | hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize); |
| 787 | |
| 788 | if (usbhid->bufsize > HID_MAX_BUFFER_SIZE) |
| 789 | usbhid->bufsize = HID_MAX_BUFFER_SIZE; |
Michael Haboustak | bf0964d | 2005-09-05 00:12:01 -0500 | [diff] [blame] | 790 | |
| 791 | hid_find_max_report(hid, HID_INPUT_REPORT, &insize); |
| 792 | |
| 793 | if (insize > HID_MAX_BUFFER_SIZE) |
| 794 | insize = HID_MAX_BUFFER_SIZE; |
| 795 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | if (hid_alloc_buffers(dev, hid)) { |
| 797 | hid_free_buffers(dev, hid); |
| 798 | goto fail; |
| 799 | } |
| 800 | |
| 801 | for (n = 0; n < interface->desc.bNumEndpoints; n++) { |
| 802 | |
| 803 | struct usb_endpoint_descriptor *endpoint; |
| 804 | int pipe; |
| 805 | int interval; |
| 806 | |
| 807 | endpoint = &interface->endpoint[n].desc; |
| 808 | if ((endpoint->bmAttributes & 3) != 3) /* Not an interrupt endpoint */ |
| 809 | continue; |
| 810 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | interval = endpoint->bInterval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | |
| 813 | /* Change the polling interval of mice. */ |
| 814 | if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0) |
| 815 | interval = hid_mousepoll_interval; |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 816 | |
Luiz Fernando N. Capitulino | 0f12aa0 | 2006-10-26 13:02:51 -0300 | [diff] [blame] | 817 | if (usb_endpoint_dir_in(endpoint)) { |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 818 | if (usbhid->urbin) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | continue; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 820 | if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | goto fail; |
| 822 | pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress); |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 823 | usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | hid_irq_in, hid, interval); |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 825 | usbhid->urbin->transfer_dma = usbhid->inbuf_dma; |
| 826 | usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | } else { |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 828 | if (usbhid->urbout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | continue; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 830 | if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | goto fail; |
| 832 | pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress); |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 833 | usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | hid_irq_out, hid, interval); |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 835 | usbhid->urbout->transfer_dma = usbhid->outbuf_dma; |
| 836 | usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | } |
| 838 | } |
| 839 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 840 | if (!usbhid->urbin) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | err("couldn't find an input interrupt endpoint"); |
| 842 | goto fail; |
| 843 | } |
| 844 | |
| 845 | init_waitqueue_head(&hid->wait); |
| 846 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 847 | INIT_WORK(&usbhid->reset_work, hid_reset); |
| 848 | setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid); |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 849 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 850 | spin_lock_init(&usbhid->inlock); |
| 851 | spin_lock_init(&usbhid->outlock); |
| 852 | spin_lock_init(&usbhid->ctrllock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | |
| 854 | hid->version = le16_to_cpu(hdesc->bcdHID); |
| 855 | hid->country = hdesc->bCountryCode; |
Anssi Hannula | be82097 | 2007-01-19 19:28:17 +0200 | [diff] [blame] | 856 | hid->dev = &intf->dev; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 857 | usbhid->intf = intf; |
| 858 | usbhid->ifnum = interface->desc.bInterfaceNumber; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | |
| 860 | hid->name[0] = 0; |
| 861 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 862 | if (dev->manufacturer) |
| 863 | strlcpy(hid->name, dev->manufacturer, sizeof(hid->name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 865 | if (dev->product) { |
| 866 | if (dev->manufacturer) |
| 867 | strlcat(hid->name, " ", sizeof(hid->name)); |
| 868 | strlcat(hid->name, dev->product, sizeof(hid->name)); |
| 869 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 871 | if (!strlen(hid->name)) |
| 872 | snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x", |
| 873 | le16_to_cpu(dev->descriptor.idVendor), |
| 874 | le16_to_cpu(dev->descriptor.idProduct)); |
| 875 | |
Jiri Kosina | 229695e | 2006-12-08 18:40:53 +0100 | [diff] [blame] | 876 | hid->bus = BUS_USB; |
Julien BLACHE | 9fa2ad5f | 2007-02-11 18:20:25 +0100 | [diff] [blame] | 877 | hid->vendor = le16_to_cpu(dev->descriptor.idVendor); |
| 878 | hid->product = le16_to_cpu(dev->descriptor.idProduct); |
Jiri Kosina | 229695e | 2006-12-08 18:40:53 +0100 | [diff] [blame] | 879 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 880 | usb_make_path(dev, hid->phys, sizeof(hid->phys)); |
| 881 | strlcat(hid->phys, "/input", sizeof(hid->phys)); |
| 882 | len = strlen(hid->phys); |
| 883 | if (len < sizeof(hid->phys) - 1) |
| 884 | snprintf(hid->phys + len, sizeof(hid->phys) - len, |
| 885 | "%d", intf->altsetting[0].desc.bInterfaceNumber); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | |
| 887 | if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0) |
| 888 | hid->uniq[0] = 0; |
| 889 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 890 | usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL); |
| 891 | if (!usbhid->urbctrl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | goto fail; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 893 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 894 | usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr, |
| 895 | usbhid->ctrlbuf, 1, hid_ctrl, hid); |
| 896 | usbhid->urbctrl->setup_dma = usbhid->cr_dma; |
| 897 | usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma; |
| 898 | usbhid->urbctrl->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP); |
Jiri Kosina | 229695e | 2006-12-08 18:40:53 +0100 | [diff] [blame] | 899 | hid->hidinput_input_event = usb_hidinput_input_event; |
Jiri Kosina | 7c37914 | 2007-01-24 11:54:19 +0100 | [diff] [blame] | 900 | hid->hid_open = usbhid_open; |
| 901 | hid->hid_close = usbhid_close; |
Jiri Kosina | aa938f7 | 2006-12-08 18:41:10 +0100 | [diff] [blame] | 902 | #ifdef CONFIG_USB_HIDDEV |
| 903 | hid->hiddev_hid_event = hiddev_hid_event; |
Jiri Kosina | aa8de2f | 2006-12-08 18:41:17 +0100 | [diff] [blame] | 904 | hid->hiddev_report_event = hiddev_report_event; |
Jiri Kosina | aa938f7 | 2006-12-08 18:41:10 +0100 | [diff] [blame] | 905 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | return hid; |
| 907 | |
| 908 | fail: |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 909 | usb_free_urb(usbhid->urbin); |
| 910 | usb_free_urb(usbhid->urbout); |
| 911 | usb_free_urb(usbhid->urbctrl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | hid_free_buffers(dev, hid); |
| 913 | hid_free_device(hid); |
| 914 | |
| 915 | return NULL; |
| 916 | } |
| 917 | |
| 918 | static void hid_disconnect(struct usb_interface *intf) |
| 919 | { |
| 920 | struct hid_device *hid = usb_get_intfdata (intf); |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 921 | struct usbhid_device *usbhid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | |
| 923 | if (!hid) |
| 924 | return; |
| 925 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 926 | usbhid = hid->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 928 | spin_lock_irq(&usbhid->inlock); /* Sync with error handler */ |
| 929 | usb_set_intfdata(intf, NULL); |
| 930 | spin_unlock_irq(&usbhid->inlock); |
| 931 | usb_kill_urb(usbhid->urbin); |
| 932 | usb_kill_urb(usbhid->urbout); |
| 933 | usb_kill_urb(usbhid->urbctrl); |
| 934 | |
| 935 | del_timer_sync(&usbhid->io_retry); |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 936 | flush_scheduled_work(); |
| 937 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | if (hid->claimed & HID_CLAIMED_INPUT) |
| 939 | hidinput_disconnect(hid); |
| 940 | if (hid->claimed & HID_CLAIMED_HIDDEV) |
| 941 | hiddev_disconnect(hid); |
| 942 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 943 | usb_free_urb(usbhid->urbin); |
| 944 | usb_free_urb(usbhid->urbctrl); |
| 945 | usb_free_urb(usbhid->urbout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | |
Anssi Hannula | be82097 | 2007-01-19 19:28:17 +0200 | [diff] [blame] | 947 | hid_free_buffers(hid_to_usb_dev(hid), hid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | hid_free_device(hid); |
| 949 | } |
| 950 | |
| 951 | static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id) |
| 952 | { |
| 953 | struct hid_device *hid; |
| 954 | char path[64]; |
| 955 | int i; |
| 956 | char *c; |
| 957 | |
| 958 | dbg("HID probe called for ifnum %d", |
| 959 | intf->altsetting->desc.bInterfaceNumber); |
| 960 | |
| 961 | if (!(hid = usb_hid_configure(intf))) |
Stelian Pop | 479f6ea | 2005-06-22 17:53:28 +0200 | [diff] [blame] | 962 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 964 | usbhid_init_reports(hid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | hid_dump_device(hid); |
Pete Zaitcev | 713c8aa | 2007-04-06 14:33:18 +0200 | [diff] [blame] | 966 | if (hid->quirks & HID_QUIRK_RESET_LEDS) |
| 967 | usbhid_set_leds(hid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | |
| 969 | if (!hidinput_connect(hid)) |
| 970 | hid->claimed |= HID_CLAIMED_INPUT; |
| 971 | if (!hiddev_connect(hid)) |
| 972 | hid->claimed |= HID_CLAIMED_HIDDEV; |
| 973 | |
| 974 | usb_set_intfdata(intf, hid); |
| 975 | |
| 976 | if (!hid->claimed) { |
| 977 | printk ("HID device not claimed by input or hiddev\n"); |
| 978 | hid_disconnect(intf); |
Stelian Pop | 479f6ea | 2005-06-22 17:53:28 +0200 | [diff] [blame] | 979 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | } |
| 981 | |
Anssi Hannula | c414606 | 2007-01-11 16:51:16 +0200 | [diff] [blame] | 982 | if ((hid->claimed & HID_CLAIMED_INPUT)) |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 983 | hid_ff_init(hid); |
| 984 | |
Geoff Levand | 4a1a4d8 | 2007-01-15 20:11:52 -0800 | [diff] [blame] | 985 | if (hid->quirks & HID_QUIRK_SONY_PS3_CONTROLLER) |
| 986 | hid_fixup_sony_ps3_controller(interface_to_usbdev(intf), |
| 987 | intf->cur_altsetting->desc.bInterfaceNumber); |
| 988 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | printk(KERN_INFO); |
| 990 | |
| 991 | if (hid->claimed & HID_CLAIMED_INPUT) |
| 992 | printk("input"); |
| 993 | if (hid->claimed == (HID_CLAIMED_INPUT | HID_CLAIMED_HIDDEV)) |
| 994 | printk(","); |
| 995 | if (hid->claimed & HID_CLAIMED_HIDDEV) |
| 996 | printk("hiddev%d", hid->minor); |
| 997 | |
| 998 | c = "Device"; |
| 999 | for (i = 0; i < hid->maxcollection; i++) { |
| 1000 | if (hid->collection[i].type == HID_COLLECTION_APPLICATION && |
| 1001 | (hid->collection[i].usage & HID_USAGE_PAGE) == HID_UP_GENDESK && |
| 1002 | (hid->collection[i].usage & 0xffff) < ARRAY_SIZE(hid_types)) { |
| 1003 | c = hid_types[hid->collection[i].usage & 0xffff]; |
| 1004 | break; |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | usb_make_path(interface_to_usbdev(intf), path, 63); |
| 1009 | |
| 1010 | printk(": USB HID v%x.%02x %s [%s] on %s\n", |
| 1011 | hid->version >> 8, hid->version & 0xff, c, hid->name, path); |
| 1012 | |
| 1013 | return 0; |
| 1014 | } |
| 1015 | |
David Brownell | 27d72e8 | 2005-04-18 17:39:22 -0700 | [diff] [blame] | 1016 | static int hid_suspend(struct usb_interface *intf, pm_message_t message) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | { |
| 1018 | struct hid_device *hid = usb_get_intfdata (intf); |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 1019 | struct usbhid_device *usbhid = hid->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 1021 | spin_lock_irq(&usbhid->inlock); /* Sync with error handler */ |
| 1022 | set_bit(HID_SUSPENDED, &usbhid->iofl); |
| 1023 | spin_unlock_irq(&usbhid->inlock); |
| 1024 | del_timer(&usbhid->io_retry); |
| 1025 | usb_kill_urb(usbhid->urbin); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | dev_dbg(&intf->dev, "suspend\n"); |
| 1027 | return 0; |
| 1028 | } |
| 1029 | |
| 1030 | static int hid_resume(struct usb_interface *intf) |
| 1031 | { |
| 1032 | struct hid_device *hid = usb_get_intfdata (intf); |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 1033 | struct usbhid_device *usbhid = hid->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | int status; |
| 1035 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 1036 | clear_bit(HID_SUSPENDED, &usbhid->iofl); |
| 1037 | usbhid->retry_delay = 0; |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 1038 | status = hid_start_in(hid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | dev_dbg(&intf->dev, "resume status %d\n", status); |
| 1040 | return status; |
| 1041 | } |
| 1042 | |
Alan Stern | df9a1f4 | 2006-06-01 13:55:28 -0400 | [diff] [blame] | 1043 | /* Treat USB reset pretty much the same as suspend/resume */ |
| 1044 | static void hid_pre_reset(struct usb_interface *intf) |
| 1045 | { |
| 1046 | /* FIXME: What if the interface is already suspended? */ |
| 1047 | hid_suspend(intf, PMSG_ON); |
| 1048 | } |
| 1049 | |
| 1050 | static void hid_post_reset(struct usb_interface *intf) |
| 1051 | { |
| 1052 | struct usb_device *dev = interface_to_usbdev (intf); |
| 1053 | |
| 1054 | hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0); |
| 1055 | /* FIXME: Any more reinitialization needed? */ |
| 1056 | |
| 1057 | hid_resume(intf); |
| 1058 | } |
| 1059 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | static struct usb_device_id hid_usb_ids [] = { |
| 1061 | { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS, |
| 1062 | .bInterfaceClass = USB_INTERFACE_CLASS_HID }, |
| 1063 | { } /* Terminating entry */ |
| 1064 | }; |
| 1065 | |
| 1066 | MODULE_DEVICE_TABLE (usb, hid_usb_ids); |
| 1067 | |
| 1068 | static struct usb_driver hid_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | .name = "usbhid", |
| 1070 | .probe = hid_probe, |
| 1071 | .disconnect = hid_disconnect, |
| 1072 | .suspend = hid_suspend, |
| 1073 | .resume = hid_resume, |
Alan Stern | df9a1f4 | 2006-06-01 13:55:28 -0400 | [diff] [blame] | 1074 | .pre_reset = hid_pre_reset, |
| 1075 | .post_reset = hid_post_reset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | .id_table = hid_usb_ids, |
| 1077 | }; |
| 1078 | |
| 1079 | static int __init hid_init(void) |
| 1080 | { |
| 1081 | int retval; |
Paul Walmsley | 876b927 | 2007-04-19 14:56:12 +0200 | [diff] [blame^] | 1082 | retval = usbhid_quirks_init(quirks_param); |
| 1083 | if (retval) |
| 1084 | goto usbhid_quirks_init_fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | retval = hiddev_init(); |
| 1086 | if (retval) |
| 1087 | goto hiddev_init_fail; |
| 1088 | retval = usb_register(&hid_driver); |
| 1089 | if (retval) |
| 1090 | goto usb_register_fail; |
| 1091 | info(DRIVER_VERSION ":" DRIVER_DESC); |
| 1092 | |
| 1093 | return 0; |
| 1094 | usb_register_fail: |
| 1095 | hiddev_exit(); |
| 1096 | hiddev_init_fail: |
Paul Walmsley | 876b927 | 2007-04-19 14:56:12 +0200 | [diff] [blame^] | 1097 | usbhid_quirks_exit(); |
| 1098 | usbhid_quirks_init_fail: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | return retval; |
| 1100 | } |
| 1101 | |
| 1102 | static void __exit hid_exit(void) |
| 1103 | { |
| 1104 | usb_deregister(&hid_driver); |
| 1105 | hiddev_exit(); |
Paul Walmsley | 876b927 | 2007-04-19 14:56:12 +0200 | [diff] [blame^] | 1106 | usbhid_quirks_exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | module_init(hid_init); |
| 1110 | module_exit(hid_exit); |
| 1111 | |
| 1112 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 1113 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 1114 | MODULE_LICENSE(DRIVER_LICENSE); |