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 |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 7 | * Copyright (c) 2007-2008 Oliver Neukum |
Jiri Kosina | 7d39e84 | 2010-02-02 20:46:34 +0100 | [diff] [blame] | 8 | * Copyright (c) 2006-2010 Jiri Kosina |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * This program is free software; you can redistribute it and/or modify it |
| 13 | * under the terms of the GNU General Public License as published by the Free |
| 14 | * Software Foundation; either version 2 of the License, or (at your option) |
| 15 | * any later version. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/list.h> |
| 23 | #include <linux/mm.h> |
Jiri Slaby | 3d5afd3 | 2008-10-27 12:16:15 +0100 | [diff] [blame] | 24 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/spinlock.h> |
| 26 | #include <asm/unaligned.h> |
| 27 | #include <asm/byteorder.h> |
| 28 | #include <linux/input.h> |
| 29 | #include <linux/wait.h> |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 30 | #include <linux/workqueue.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/usb.h> |
| 33 | |
Jiri Kosina | dde5845 | 2006-12-08 18:40:44 +0100 | [diff] [blame] | 34 | #include <linux/hid.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/hiddev.h> |
Jiri Kosina | c080d89 | 2007-01-25 11:43:31 +0100 | [diff] [blame] | 36 | #include <linux/hid-debug.h> |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 37 | #include <linux/hidraw.h> |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 38 | #include "usbhid.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | /* |
| 41 | * Version Information |
| 42 | */ |
| 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #define DRIVER_DESC "USB HID core driver" |
| 45 | #define DRIVER_LICENSE "GPL" |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | /* |
| 48 | * Module parameters. |
| 49 | */ |
| 50 | |
| 51 | static unsigned int hid_mousepoll_interval; |
| 52 | module_param_named(mousepoll, hid_mousepoll_interval, uint, 0644); |
| 53 | MODULE_PARM_DESC(mousepoll, "Polling interval of mice"); |
| 54 | |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 55 | static unsigned int ignoreled; |
| 56 | module_param_named(ignoreled, ignoreled, uint, 0644); |
| 57 | MODULE_PARM_DESC(ignoreled, "Autosuspend with active leds"); |
| 58 | |
Paul Walmsley | 876b927 | 2007-04-19 14:56:12 +0200 | [diff] [blame] | 59 | /* Quirks specified at module load time */ |
| 60 | static char *quirks_param[MAX_USBHID_BOOT_QUIRKS] = { [ 0 ... (MAX_USBHID_BOOT_QUIRKS - 1) ] = NULL }; |
| 61 | module_param_array_named(quirks, quirks_param, charp, NULL, 0444); |
| 62 | MODULE_PARM_DESC(quirks, "Add/modify USB HID quirks by specifying " |
| 63 | " quirks=vendorID:productID:quirks" |
| 64 | " where vendorID, productID, and quirks are all in" |
| 65 | " 0x-prefixed hex"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | /* |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 67 | * Input submission and I/O error handler. |
| 68 | */ |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 69 | static DEFINE_MUTEX(hid_open_mut); |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 70 | |
| 71 | static void hid_io_error(struct hid_device *hid); |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 72 | static int hid_submit_out(struct hid_device *hid); |
| 73 | static int hid_submit_ctrl(struct hid_device *hid); |
| 74 | static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid); |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 75 | |
| 76 | /* Start up the input URB */ |
| 77 | static int hid_start_in(struct hid_device *hid) |
| 78 | { |
| 79 | unsigned long flags; |
| 80 | int rc = 0; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 81 | struct usbhid_device *usbhid = hid->driver_data; |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 82 | |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 83 | spin_lock_irqsave(&usbhid->lock, flags); |
| 84 | if (hid->open > 0 && |
Oliver Neukum | 69626f2 | 2008-03-31 16:27:30 +0200 | [diff] [blame] | 85 | !test_bit(HID_DISCONNECTED, &usbhid->iofl) && |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 86 | !test_bit(HID_REPORTED_IDLE, &usbhid->iofl) && |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 87 | !test_and_set_bit(HID_IN_RUNNING, &usbhid->iofl)) { |
| 88 | rc = usb_submit_urb(usbhid->urbin, GFP_ATOMIC); |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 89 | if (rc != 0) |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 90 | clear_bit(HID_IN_RUNNING, &usbhid->iofl); |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 91 | } |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 92 | spin_unlock_irqrestore(&usbhid->lock, flags); |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 93 | return rc; |
| 94 | } |
| 95 | |
| 96 | /* I/O retry timer routine */ |
| 97 | static void hid_retry_timeout(unsigned long _hid) |
| 98 | { |
| 99 | struct hid_device *hid = (struct hid_device *) _hid; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 100 | struct usbhid_device *usbhid = hid->driver_data; |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 101 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 102 | dev_dbg(&usbhid->intf->dev, "retrying intr urb\n"); |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 103 | if (hid_start_in(hid)) |
| 104 | hid_io_error(hid); |
| 105 | } |
| 106 | |
Alan Stern | 6d8fc4d | 2006-10-18 12:35:24 -0400 | [diff] [blame] | 107 | /* Workqueue routine to reset the device or clear a halt */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 108 | static void hid_reset(struct work_struct *work) |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 109 | { |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 110 | struct usbhid_device *usbhid = |
| 111 | container_of(work, struct usbhid_device, reset_work); |
| 112 | struct hid_device *hid = usbhid->hid; |
Alan Stern | 011b15d | 2008-11-04 11:29:27 -0500 | [diff] [blame] | 113 | int rc = 0; |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 114 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 115 | if (test_bit(HID_CLEAR_HALT, &usbhid->iofl)) { |
| 116 | dev_dbg(&usbhid->intf->dev, "clear halt\n"); |
Anssi Hannula | be82097 | 2007-01-19 19:28:17 +0200 | [diff] [blame] | 117 | rc = usb_clear_halt(hid_to_usb_dev(hid), usbhid->urbin->pipe); |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 118 | clear_bit(HID_CLEAR_HALT, &usbhid->iofl); |
Alan Stern | 6d8fc4d | 2006-10-18 12:35:24 -0400 | [diff] [blame] | 119 | hid_start_in(hid); |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 120 | } |
Alan Stern | 6d8fc4d | 2006-10-18 12:35:24 -0400 | [diff] [blame] | 121 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 122 | else if (test_bit(HID_RESET_PENDING, &usbhid->iofl)) { |
| 123 | dev_dbg(&usbhid->intf->dev, "resetting device\n"); |
Alan Stern | 011b15d | 2008-11-04 11:29:27 -0500 | [diff] [blame] | 124 | rc = usb_lock_device_for_reset(hid_to_usb_dev(hid), usbhid->intf); |
| 125 | if (rc == 0) { |
Ming Lei | 742120c | 2008-06-18 22:00:29 +0800 | [diff] [blame] | 126 | rc = usb_reset_device(hid_to_usb_dev(hid)); |
Alan Stern | 011b15d | 2008-11-04 11:29:27 -0500 | [diff] [blame] | 127 | usb_unlock_device(hid_to_usb_dev(hid)); |
Alan Stern | 6d8fc4d | 2006-10-18 12:35:24 -0400 | [diff] [blame] | 128 | } |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 129 | clear_bit(HID_RESET_PENDING, &usbhid->iofl); |
Alan Stern | 6d8fc4d | 2006-10-18 12:35:24 -0400 | [diff] [blame] | 130 | } |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 131 | |
Alan Stern | df9a1f4 | 2006-06-01 13:55:28 -0400 | [diff] [blame] | 132 | switch (rc) { |
| 133 | case 0: |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 134 | if (!test_bit(HID_IN_RUNNING, &usbhid->iofl)) |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 135 | hid_io_error(hid); |
Alan Stern | df9a1f4 | 2006-06-01 13:55:28 -0400 | [diff] [blame] | 136 | break; |
| 137 | default: |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 138 | hid_err(hid, "can't reset device, %s-%s/input%d, status %d\n", |
| 139 | hid_to_usb_dev(hid)->bus->bus_name, |
| 140 | hid_to_usb_dev(hid)->devpath, |
| 141 | usbhid->ifnum, rc); |
Alan Stern | df9a1f4 | 2006-06-01 13:55:28 -0400 | [diff] [blame] | 142 | /* FALLTHROUGH */ |
| 143 | case -EHOSTUNREACH: |
| 144 | case -ENODEV: |
| 145 | case -EINTR: |
| 146 | break; |
| 147 | } |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /* Main I/O error handler */ |
| 151 | static void hid_io_error(struct hid_device *hid) |
| 152 | { |
| 153 | unsigned long flags; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 154 | struct usbhid_device *usbhid = hid->driver_data; |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 155 | |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 156 | spin_lock_irqsave(&usbhid->lock, flags); |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 157 | |
| 158 | /* Stop when disconnected */ |
Oliver Neukum | 69626f2 | 2008-03-31 16:27:30 +0200 | [diff] [blame] | 159 | if (test_bit(HID_DISCONNECTED, &usbhid->iofl)) |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 160 | goto done; |
| 161 | |
Alan Stern | 5e2a55f | 2007-03-20 19:03:31 +0100 | [diff] [blame] | 162 | /* If it has been a while since the last error, we'll assume |
| 163 | * this a brand new error and reset the retry timeout. */ |
| 164 | if (time_after(jiffies, usbhid->stop_retry + HZ/2)) |
| 165 | usbhid->retry_delay = 0; |
| 166 | |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 167 | /* When an error occurs, retry at increasing intervals */ |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 168 | if (usbhid->retry_delay == 0) { |
| 169 | usbhid->retry_delay = 13; /* Then 26, 52, 104, 104, ... */ |
| 170 | usbhid->stop_retry = jiffies + msecs_to_jiffies(1000); |
| 171 | } else if (usbhid->retry_delay < 100) |
| 172 | usbhid->retry_delay *= 2; |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 173 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 174 | if (time_after(jiffies, usbhid->stop_retry)) { |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 175 | |
| 176 | /* Retries failed, so do a port reset */ |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 177 | if (!test_and_set_bit(HID_RESET_PENDING, &usbhid->iofl)) { |
| 178 | schedule_work(&usbhid->reset_work); |
Alan Stern | 6d8fc4d | 2006-10-18 12:35:24 -0400 | [diff] [blame] | 179 | goto done; |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 183 | mod_timer(&usbhid->io_retry, |
| 184 | jiffies + msecs_to_jiffies(usbhid->retry_delay)); |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 185 | done: |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 186 | spin_unlock_irqrestore(&usbhid->lock, flags); |
| 187 | } |
| 188 | |
| 189 | static void usbhid_mark_busy(struct usbhid_device *usbhid) |
| 190 | { |
| 191 | struct usb_interface *intf = usbhid->intf; |
| 192 | |
| 193 | usb_mark_last_busy(interface_to_usbdev(intf)); |
| 194 | } |
| 195 | |
| 196 | static int usbhid_restart_out_queue(struct usbhid_device *usbhid) |
| 197 | { |
| 198 | struct hid_device *hid = usb_get_intfdata(usbhid->intf); |
| 199 | int kicked; |
| 200 | |
| 201 | if (!hid) |
| 202 | return 0; |
| 203 | |
| 204 | if ((kicked = (usbhid->outhead != usbhid->outtail))) { |
| 205 | dbg("Kicking head %d tail %d", usbhid->outhead, usbhid->outtail); |
| 206 | if (hid_submit_out(hid)) { |
| 207 | clear_bit(HID_OUT_RUNNING, &usbhid->iofl); |
| 208 | wake_up(&usbhid->wait); |
| 209 | } |
| 210 | } |
| 211 | return kicked; |
| 212 | } |
| 213 | |
| 214 | static int usbhid_restart_ctrl_queue(struct usbhid_device *usbhid) |
| 215 | { |
| 216 | struct hid_device *hid = usb_get_intfdata(usbhid->intf); |
| 217 | int kicked; |
| 218 | |
| 219 | WARN_ON(hid == NULL); |
| 220 | if (!hid) |
| 221 | return 0; |
| 222 | |
| 223 | if ((kicked = (usbhid->ctrlhead != usbhid->ctrltail))) { |
| 224 | dbg("Kicking head %d tail %d", usbhid->ctrlhead, usbhid->ctrltail); |
| 225 | if (hid_submit_ctrl(hid)) { |
| 226 | clear_bit(HID_CTRL_RUNNING, &usbhid->iofl); |
| 227 | wake_up(&usbhid->wait); |
| 228 | } |
| 229 | } |
| 230 | return kicked; |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | * Input interrupt completion handler. |
| 235 | */ |
| 236 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 237 | static void hid_irq_in(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | { |
| 239 | struct hid_device *hid = urb->context; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 240 | struct usbhid_device *usbhid = hid->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | int status; |
| 242 | |
| 243 | switch (urb->status) { |
Jiri Slaby | 880d29f | 2008-06-18 23:55:41 +0200 | [diff] [blame] | 244 | case 0: /* success */ |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 245 | usbhid_mark_busy(usbhid); |
Jiri Slaby | 880d29f | 2008-06-18 23:55:41 +0200 | [diff] [blame] | 246 | usbhid->retry_delay = 0; |
| 247 | hid_input_report(urb->context, HID_INPUT_REPORT, |
| 248 | urb->transfer_buffer, |
| 249 | urb->actual_length, 1); |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 250 | /* |
| 251 | * autosuspend refused while keys are pressed |
| 252 | * because most keyboards don't wake up when |
| 253 | * a key is released |
| 254 | */ |
| 255 | if (hid_check_keys_pressed(hid)) |
| 256 | set_bit(HID_KEYS_PRESSED, &usbhid->iofl); |
| 257 | else |
| 258 | clear_bit(HID_KEYS_PRESSED, &usbhid->iofl); |
Jiri Slaby | 880d29f | 2008-06-18 23:55:41 +0200 | [diff] [blame] | 259 | break; |
| 260 | case -EPIPE: /* stall */ |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 261 | usbhid_mark_busy(usbhid); |
Jiri Slaby | 880d29f | 2008-06-18 23:55:41 +0200 | [diff] [blame] | 262 | clear_bit(HID_IN_RUNNING, &usbhid->iofl); |
| 263 | set_bit(HID_CLEAR_HALT, &usbhid->iofl); |
| 264 | schedule_work(&usbhid->reset_work); |
| 265 | return; |
| 266 | case -ECONNRESET: /* unlink */ |
| 267 | case -ENOENT: |
| 268 | case -ESHUTDOWN: /* unplug */ |
| 269 | clear_bit(HID_IN_RUNNING, &usbhid->iofl); |
| 270 | return; |
| 271 | case -EILSEQ: /* protocol error or unplug */ |
| 272 | case -EPROTO: /* protocol error or unplug */ |
| 273 | case -ETIME: /* protocol error or unplug */ |
| 274 | case -ETIMEDOUT: /* Should never happen, but... */ |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 275 | usbhid_mark_busy(usbhid); |
Jiri Slaby | 880d29f | 2008-06-18 23:55:41 +0200 | [diff] [blame] | 276 | clear_bit(HID_IN_RUNNING, &usbhid->iofl); |
| 277 | hid_io_error(hid); |
| 278 | return; |
| 279 | default: /* error */ |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 280 | hid_warn(urb->dev, "input irq status %d received\n", |
| 281 | urb->status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Christoph Lameter | 54e6ecb | 2006-12-06 20:33:16 -0800 | [diff] [blame] | 284 | status = usb_submit_urb(urb, GFP_ATOMIC); |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 285 | if (status) { |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 286 | clear_bit(HID_IN_RUNNING, &usbhid->iofl); |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 287 | if (status != -EPERM) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 288 | hid_err(hid, "can't resubmit intr, %s-%s/input%d, status %d\n", |
| 289 | hid_to_usb_dev(hid)->bus->bus_name, |
| 290 | hid_to_usb_dev(hid)->devpath, |
| 291 | usbhid->ifnum, status); |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 292 | hid_io_error(hid); |
| 293 | } |
| 294 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | static int hid_submit_out(struct hid_device *hid) |
| 298 | { |
| 299 | struct hid_report *report; |
Anssi Hannula | f129ea6 | 2008-10-04 14:44:06 +0200 | [diff] [blame] | 300 | char *raw_report; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 301 | struct usbhid_device *usbhid = hid->driver_data; |
Oliver Neukum | 6822968 | 2010-12-22 15:33:40 +0100 | [diff] [blame] | 302 | int r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | |
Anssi Hannula | f129ea6 | 2008-10-04 14:44:06 +0200 | [diff] [blame] | 304 | report = usbhid->out[usbhid->outtail].report; |
| 305 | raw_report = usbhid->out[usbhid->outtail].raw_report; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
Oliver Neukum | 6822968 | 2010-12-22 15:33:40 +0100 | [diff] [blame] | 307 | r = usb_autopm_get_interface_async(usbhid->intf); |
| 308 | if (r < 0) |
| 309 | return -1; |
| 310 | |
| 311 | /* |
| 312 | * if the device hasn't been woken, we leave the output |
| 313 | * to resume() |
| 314 | */ |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 315 | if (!test_bit(HID_REPORTED_IDLE, &usbhid->iofl)) { |
| 316 | usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0); |
| 317 | usbhid->urbout->dev = hid_to_usb_dev(hid); |
| 318 | memcpy(usbhid->outbuf, raw_report, usbhid->urbout->transfer_buffer_length); |
| 319 | kfree(raw_report); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 321 | dbg_hid("submitting out urb\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 323 | if (usb_submit_urb(usbhid->urbout, GFP_ATOMIC)) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 324 | hid_err(hid, "usb_submit_urb(out) failed\n"); |
Oliver Neukum | 6822968 | 2010-12-22 15:33:40 +0100 | [diff] [blame] | 325 | usb_autopm_put_interface_async(usbhid->intf); |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 326 | return -1; |
| 327 | } |
Oliver Neukum | 858155f | 2010-02-12 13:02:28 +0100 | [diff] [blame] | 328 | usbhid->last_out = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | static int hid_submit_ctrl(struct hid_device *hid) |
| 335 | { |
| 336 | struct hid_report *report; |
| 337 | unsigned char dir; |
Anssi Hannula | f129ea6 | 2008-10-04 14:44:06 +0200 | [diff] [blame] | 338 | char *raw_report; |
Oliver Neukum | 6822968 | 2010-12-22 15:33:40 +0100 | [diff] [blame] | 339 | int len, r; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 340 | struct usbhid_device *usbhid = hid->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 342 | report = usbhid->ctrl[usbhid->ctrltail].report; |
Anssi Hannula | f129ea6 | 2008-10-04 14:44:06 +0200 | [diff] [blame] | 343 | raw_report = usbhid->ctrl[usbhid->ctrltail].raw_report; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 344 | dir = usbhid->ctrl[usbhid->ctrltail].dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
Oliver Neukum | 6822968 | 2010-12-22 15:33:40 +0100 | [diff] [blame] | 346 | r = usb_autopm_get_interface_async(usbhid->intf); |
| 347 | if (r < 0) |
| 348 | return -1; |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 349 | if (!test_bit(HID_REPORTED_IDLE, &usbhid->iofl)) { |
| 350 | len = ((report->size - 1) >> 3) + 1 + (report->id > 0); |
| 351 | if (dir == USB_DIR_OUT) { |
| 352 | usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0); |
| 353 | usbhid->urbctrl->transfer_buffer_length = len; |
| 354 | memcpy(usbhid->ctrlbuf, raw_report, len); |
| 355 | kfree(raw_report); |
| 356 | } else { |
| 357 | int maxpacket, padlen; |
| 358 | |
| 359 | usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0); |
| 360 | maxpacket = usb_maxpacket(hid_to_usb_dev(hid), usbhid->urbctrl->pipe, 0); |
| 361 | if (maxpacket > 0) { |
| 362 | padlen = DIV_ROUND_UP(len, maxpacket); |
| 363 | padlen *= maxpacket; |
| 364 | if (padlen > usbhid->bufsize) |
| 365 | padlen = usbhid->bufsize; |
| 366 | } else |
| 367 | padlen = 0; |
| 368 | usbhid->urbctrl->transfer_buffer_length = padlen; |
| 369 | } |
| 370 | usbhid->urbctrl->dev = hid_to_usb_dev(hid); |
| 371 | |
| 372 | usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir; |
| 373 | usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT : HID_REQ_GET_REPORT; |
| 374 | usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) | report->id); |
| 375 | usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum); |
| 376 | usbhid->cr->wLength = cpu_to_le16(len); |
| 377 | |
| 378 | dbg_hid("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u\n", |
| 379 | usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" : "Get_Report", |
| 380 | usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength); |
| 381 | |
| 382 | if (usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC)) { |
Oliver Neukum | 6822968 | 2010-12-22 15:33:40 +0100 | [diff] [blame] | 383 | usb_autopm_put_interface_async(usbhid->intf); |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 384 | hid_err(hid, "usb_submit_urb(ctrl) failed\n"); |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 385 | return -1; |
| 386 | } |
Oliver Neukum | 858155f | 2010-02-12 13:02:28 +0100 | [diff] [blame] | 387 | usbhid->last_ctrl = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | /* |
| 394 | * Output interrupt completion handler. |
| 395 | */ |
| 396 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 397 | static void hid_irq_out(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | { |
| 399 | struct hid_device *hid = urb->context; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 400 | struct usbhid_device *usbhid = hid->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | unsigned long flags; |
| 402 | int unplug = 0; |
| 403 | |
| 404 | switch (urb->status) { |
Jiri Slaby | 880d29f | 2008-06-18 23:55:41 +0200 | [diff] [blame] | 405 | case 0: /* success */ |
| 406 | break; |
| 407 | case -ESHUTDOWN: /* unplug */ |
| 408 | unplug = 1; |
| 409 | case -EILSEQ: /* protocol error or unplug */ |
| 410 | case -EPROTO: /* protocol error or unplug */ |
| 411 | case -ECONNRESET: /* unlink */ |
| 412 | case -ENOENT: |
| 413 | break; |
| 414 | default: /* error */ |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 415 | hid_warn(urb->dev, "output irq status %d received\n", |
| 416 | urb->status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 419 | spin_lock_irqsave(&usbhid->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
| 421 | if (unplug) |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 422 | usbhid->outtail = usbhid->outhead; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | else |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 424 | usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 426 | if (usbhid->outhead != usbhid->outtail) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | if (hid_submit_out(hid)) { |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 428 | clear_bit(HID_OUT_RUNNING, &usbhid->iofl); |
Jiri Slaby | 1d1bdd2 | 2008-03-19 21:55:04 +0100 | [diff] [blame] | 429 | wake_up(&usbhid->wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | } |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 431 | spin_unlock_irqrestore(&usbhid->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | return; |
| 433 | } |
| 434 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 435 | clear_bit(HID_OUT_RUNNING, &usbhid->iofl); |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 436 | spin_unlock_irqrestore(&usbhid->lock, flags); |
Oliver Neukum | 6822968 | 2010-12-22 15:33:40 +0100 | [diff] [blame] | 437 | usb_autopm_put_interface_async(usbhid->intf); |
Jiri Slaby | 1d1bdd2 | 2008-03-19 21:55:04 +0100 | [diff] [blame] | 438 | wake_up(&usbhid->wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | /* |
| 442 | * Control pipe completion handler. |
| 443 | */ |
| 444 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 445 | static void hid_ctrl(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | { |
| 447 | struct hid_device *hid = urb->context; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 448 | struct usbhid_device *usbhid = hid->driver_data; |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 449 | int unplug = 0, status = urb->status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 451 | spin_lock(&usbhid->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 453 | switch (status) { |
Jiri Slaby | 880d29f | 2008-06-18 23:55:41 +0200 | [diff] [blame] | 454 | case 0: /* success */ |
| 455 | if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN) |
| 456 | hid_input_report(urb->context, |
| 457 | usbhid->ctrl[usbhid->ctrltail].report->type, |
| 458 | urb->transfer_buffer, urb->actual_length, 0); |
| 459 | break; |
| 460 | case -ESHUTDOWN: /* unplug */ |
| 461 | unplug = 1; |
| 462 | case -EILSEQ: /* protocol error or unplug */ |
| 463 | case -EPROTO: /* protocol error or unplug */ |
| 464 | case -ECONNRESET: /* unlink */ |
| 465 | case -ENOENT: |
| 466 | case -EPIPE: /* report not available */ |
| 467 | break; |
| 468 | default: /* error */ |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 469 | hid_warn(urb->dev, "ctrl urb status %d received\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | if (unplug) |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 473 | usbhid->ctrltail = usbhid->ctrlhead; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | else |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 475 | usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 477 | if (usbhid->ctrlhead != usbhid->ctrltail) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | if (hid_submit_ctrl(hid)) { |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 479 | clear_bit(HID_CTRL_RUNNING, &usbhid->iofl); |
Jiri Slaby | 1d1bdd2 | 2008-03-19 21:55:04 +0100 | [diff] [blame] | 480 | wake_up(&usbhid->wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | } |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 482 | spin_unlock(&usbhid->lock); |
Oliver Neukum | 6822968 | 2010-12-22 15:33:40 +0100 | [diff] [blame] | 483 | usb_autopm_put_interface_async(usbhid->intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | return; |
| 485 | } |
| 486 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 487 | clear_bit(HID_CTRL_RUNNING, &usbhid->iofl); |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 488 | spin_unlock(&usbhid->lock); |
Oliver Neukum | 6822968 | 2010-12-22 15:33:40 +0100 | [diff] [blame] | 489 | usb_autopm_put_interface_async(usbhid->intf); |
Jiri Slaby | 1d1bdd2 | 2008-03-19 21:55:04 +0100 | [diff] [blame] | 490 | wake_up(&usbhid->wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | } |
| 492 | |
H Hartley Sweeten | 52cfc61b | 2009-08-17 15:37:18 -0700 | [diff] [blame] | 493 | static void __usbhid_submit_report(struct hid_device *hid, struct hid_report *report, |
| 494 | unsigned char dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | { |
| 496 | int head; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 497 | struct usbhid_device *usbhid = hid->driver_data; |
Anssi Hannula | f129ea6 | 2008-10-04 14:44:06 +0200 | [diff] [blame] | 498 | int len = ((report->size - 1) >> 3) + 1 + (report->id > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | |
| 500 | if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN) |
| 501 | return; |
| 502 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 503 | if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) { |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 504 | if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 505 | hid_warn(hid, "output queue full\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | return; |
| 507 | } |
| 508 | |
Anssi Hannula | f129ea6 | 2008-10-04 14:44:06 +0200 | [diff] [blame] | 509 | usbhid->out[usbhid->outhead].raw_report = kmalloc(len, GFP_ATOMIC); |
| 510 | if (!usbhid->out[usbhid->outhead].raw_report) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 511 | hid_warn(hid, "output queueing failed\n"); |
Anssi Hannula | f129ea6 | 2008-10-04 14:44:06 +0200 | [diff] [blame] | 512 | return; |
| 513 | } |
| 514 | hid_output_report(report, usbhid->out[usbhid->outhead].raw_report); |
| 515 | usbhid->out[usbhid->outhead].report = report; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 516 | usbhid->outhead = head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | |
Oliver Neukum | 858155f | 2010-02-12 13:02:28 +0100 | [diff] [blame] | 518 | if (!test_and_set_bit(HID_OUT_RUNNING, &usbhid->iofl)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | if (hid_submit_out(hid)) |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 520 | clear_bit(HID_OUT_RUNNING, &usbhid->iofl); |
Oliver Neukum | 858155f | 2010-02-12 13:02:28 +0100 | [diff] [blame] | 521 | } else { |
| 522 | /* |
| 523 | * the queue is known to run |
| 524 | * but an earlier request may be stuck |
| 525 | * we may need to time out |
| 526 | * no race because this is called under |
| 527 | * spinlock |
| 528 | */ |
| 529 | if (time_after(jiffies, usbhid->last_out + HZ * 5)) |
| 530 | usb_unlink_urb(usbhid->urbout); |
| 531 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | return; |
| 533 | } |
| 534 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 535 | if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 536 | hid_warn(hid, "control queue full\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | return; |
| 538 | } |
| 539 | |
Anssi Hannula | f129ea6 | 2008-10-04 14:44:06 +0200 | [diff] [blame] | 540 | if (dir == USB_DIR_OUT) { |
| 541 | usbhid->ctrl[usbhid->ctrlhead].raw_report = kmalloc(len, GFP_ATOMIC); |
| 542 | if (!usbhid->ctrl[usbhid->ctrlhead].raw_report) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 543 | hid_warn(hid, "control queueing failed\n"); |
Anssi Hannula | f129ea6 | 2008-10-04 14:44:06 +0200 | [diff] [blame] | 544 | return; |
| 545 | } |
| 546 | hid_output_report(report, usbhid->ctrl[usbhid->ctrlhead].raw_report); |
| 547 | } |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 548 | usbhid->ctrl[usbhid->ctrlhead].report = report; |
| 549 | usbhid->ctrl[usbhid->ctrlhead].dir = dir; |
| 550 | usbhid->ctrlhead = head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | |
Oliver Neukum | 858155f | 2010-02-12 13:02:28 +0100 | [diff] [blame] | 552 | if (!test_and_set_bit(HID_CTRL_RUNNING, &usbhid->iofl)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | if (hid_submit_ctrl(hid)) |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 554 | clear_bit(HID_CTRL_RUNNING, &usbhid->iofl); |
Oliver Neukum | 858155f | 2010-02-12 13:02:28 +0100 | [diff] [blame] | 555 | } else { |
| 556 | /* |
| 557 | * the queue is known to run |
| 558 | * but an earlier request may be stuck |
| 559 | * we may need to time out |
| 560 | * no race because this is called under |
| 561 | * spinlock |
| 562 | */ |
| 563 | if (time_after(jiffies, usbhid->last_ctrl + HZ * 5)) |
| 564 | usb_unlink_urb(usbhid->urbctrl); |
| 565 | } |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 566 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 568 | void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir) |
| 569 | { |
| 570 | struct usbhid_device *usbhid = hid->driver_data; |
| 571 | unsigned long flags; |
| 572 | |
| 573 | spin_lock_irqsave(&usbhid->lock, flags); |
| 574 | __usbhid_submit_report(hid, report, dir); |
| 575 | spin_unlock_irqrestore(&usbhid->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | } |
Jiri Slaby | 606bd0a | 2008-07-04 23:06:45 +0200 | [diff] [blame] | 577 | EXPORT_SYMBOL_GPL(usbhid_submit_report); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | |
Jiri Kosina | 229695e | 2006-12-08 18:40:53 +0100 | [diff] [blame] | 579 | 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] | 580 | { |
Dmitry Torokhov | e071298 | 2007-05-09 10:17:31 +0200 | [diff] [blame] | 581 | struct hid_device *hid = input_get_drvdata(dev); |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 582 | struct usbhid_device *usbhid = hid->driver_data; |
Jiri Kosina | dde5845 | 2006-12-08 18:40:44 +0100 | [diff] [blame] | 583 | struct hid_field *field; |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 584 | unsigned long flags; |
Jiri Kosina | dde5845 | 2006-12-08 18:40:44 +0100 | [diff] [blame] | 585 | int offset; |
| 586 | |
| 587 | if (type == EV_FF) |
| 588 | return input_ff_event(dev, type, code, value); |
| 589 | |
| 590 | if (type != EV_LED) |
| 591 | return -1; |
| 592 | |
| 593 | if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 594 | hid_warn(dev, "event field not found\n"); |
Jiri Kosina | dde5845 | 2006-12-08 18:40:44 +0100 | [diff] [blame] | 595 | return -1; |
| 596 | } |
| 597 | |
| 598 | hid_set_field(field, offset, value); |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 599 | if (value) { |
| 600 | spin_lock_irqsave(&usbhid->lock, flags); |
| 601 | usbhid->ledcount++; |
| 602 | spin_unlock_irqrestore(&usbhid->lock, flags); |
| 603 | } else { |
| 604 | spin_lock_irqsave(&usbhid->lock, flags); |
| 605 | usbhid->ledcount--; |
| 606 | spin_unlock_irqrestore(&usbhid->lock, flags); |
| 607 | } |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 608 | usbhid_submit_report(hid, field->report, USB_DIR_OUT); |
Jiri Kosina | dde5845 | 2006-12-08 18:40:44 +0100 | [diff] [blame] | 609 | |
| 610 | return 0; |
| 611 | } |
| 612 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 613 | int usbhid_wait_io(struct hid_device *hid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | { |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 615 | struct usbhid_device *usbhid = hid->driver_data; |
| 616 | |
Jiri Slaby | 1d1bdd2 | 2008-03-19 21:55:04 +0100 | [diff] [blame] | 617 | if (!wait_event_timeout(usbhid->wait, |
| 618 | (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) && |
| 619 | !test_bit(HID_OUT_RUNNING, &usbhid->iofl)), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | 10*HZ)) { |
Jiri Kosina | 58037eb | 2007-05-30 15:07:13 +0200 | [diff] [blame] | 621 | dbg_hid("timeout waiting for ctrl or out queue to clear\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | return -1; |
| 623 | } |
| 624 | |
| 625 | return 0; |
| 626 | } |
Bruno Prémont | b8c21cf | 2010-03-30 22:34:30 +0200 | [diff] [blame] | 627 | EXPORT_SYMBOL_GPL(usbhid_wait_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | |
Vojtech Pavlik | 854561b | 2005-05-29 02:28:00 -0500 | [diff] [blame] | 629 | static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle) |
| 630 | { |
Vojtech Pavlik | 71387bd | 2005-05-29 02:28:14 -0500 | [diff] [blame] | 631 | return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
Vojtech Pavlik | 854561b | 2005-05-29 02:28:00 -0500 | [diff] [blame] | 632 | HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report, |
| 633 | ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT); |
| 634 | } |
| 635 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | static int hid_get_class_descriptor(struct usb_device *dev, int ifnum, |
| 637 | unsigned char type, void *buf, int size) |
| 638 | { |
| 639 | int result, retries = 4; |
| 640 | |
Jiri Kosina | 43c7bf0 | 2007-01-26 12:58:24 +0100 | [diff] [blame] | 641 | memset(buf, 0, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | |
| 643 | do { |
| 644 | result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 645 | USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN, |
| 646 | (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT); |
| 647 | retries--; |
| 648 | } while (result < size && retries); |
| 649 | return result; |
| 650 | } |
| 651 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 652 | int usbhid_open(struct hid_device *hid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | { |
Oliver Neukum | 933e318 | 2007-07-11 14:48:58 +0200 | [diff] [blame] | 654 | struct usbhid_device *usbhid = hid->driver_data; |
| 655 | int res; |
| 656 | |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 657 | mutex_lock(&hid_open_mut); |
Oliver Neukum | 933e318 | 2007-07-11 14:48:58 +0200 | [diff] [blame] | 658 | if (!hid->open++) { |
| 659 | res = usb_autopm_get_interface(usbhid->intf); |
Oliver Neukum | 6822968 | 2010-12-22 15:33:40 +0100 | [diff] [blame] | 660 | /* the device must be awake to reliably request remote wakeup */ |
Oliver Neukum | 933e318 | 2007-07-11 14:48:58 +0200 | [diff] [blame] | 661 | if (res < 0) { |
| 662 | hid->open--; |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 663 | mutex_unlock(&hid_open_mut); |
Oliver Neukum | 933e318 | 2007-07-11 14:48:58 +0200 | [diff] [blame] | 664 | return -EIO; |
| 665 | } |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 666 | usbhid->intf->needs_remote_wakeup = 1; |
| 667 | if (hid_start_in(hid)) |
| 668 | hid_io_error(hid); |
| 669 | |
| 670 | usb_autopm_put_interface(usbhid->intf); |
Oliver Neukum | 933e318 | 2007-07-11 14:48:58 +0200 | [diff] [blame] | 671 | } |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 672 | mutex_unlock(&hid_open_mut); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | return 0; |
| 674 | } |
| 675 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 676 | void usbhid_close(struct hid_device *hid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | { |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 678 | struct usbhid_device *usbhid = hid->driver_data; |
| 679 | |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 680 | mutex_lock(&hid_open_mut); |
| 681 | |
| 682 | /* protecting hid->open to make sure we don't restart |
| 683 | * data acquistion due to a resumption we no longer |
| 684 | * care about |
| 685 | */ |
| 686 | spin_lock_irq(&usbhid->lock); |
Oliver Neukum | 933e318 | 2007-07-11 14:48:58 +0200 | [diff] [blame] | 687 | if (!--hid->open) { |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 688 | spin_unlock_irq(&usbhid->lock); |
Oliver Neukum | 89092dd | 2009-04-29 17:12:12 +0200 | [diff] [blame] | 689 | hid_cancel_delayed_stuff(usbhid); |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 690 | usb_kill_urb(usbhid->urbin); |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 691 | usbhid->intf->needs_remote_wakeup = 0; |
| 692 | } else { |
| 693 | spin_unlock_irq(&usbhid->lock); |
Oliver Neukum | 933e318 | 2007-07-11 14:48:58 +0200 | [diff] [blame] | 694 | } |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 695 | mutex_unlock(&hid_open_mut); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | /* |
| 699 | * Initialize all reports |
| 700 | */ |
| 701 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 702 | void usbhid_init_reports(struct hid_device *hid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | { |
| 704 | struct hid_report *report; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 705 | struct usbhid_device *usbhid = hid->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | int err, ret; |
| 707 | |
Michael Haboustak | bf0964d | 2005-09-05 00:12:01 -0500 | [diff] [blame] | 708 | 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] | 709 | usbhid_submit_report(hid, report, USB_DIR_IN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | |
| 711 | 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] | 712 | usbhid_submit_report(hid, report, USB_DIR_IN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | |
| 714 | err = 0; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 715 | ret = usbhid_wait_io(hid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | while (ret) { |
| 717 | err |= ret; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 718 | if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl)) |
| 719 | usb_kill_urb(usbhid->urbctrl); |
| 720 | if (test_bit(HID_OUT_RUNNING, &usbhid->iofl)) |
| 721 | usb_kill_urb(usbhid->urbout); |
| 722 | ret = usbhid_wait_io(hid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | if (err) |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 726 | hid_warn(hid, "timeout initializing reports\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | } |
| 728 | |
Michael Haboustak | bf0964d | 2005-09-05 00:12:01 -0500 | [diff] [blame] | 729 | /* |
Pete Zaitcev | 713c8aa | 2007-04-06 14:33:18 +0200 | [diff] [blame] | 730 | * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01). |
| 731 | */ |
| 732 | static int hid_find_field_early(struct hid_device *hid, unsigned int page, |
| 733 | unsigned int hid_code, struct hid_field **pfield) |
| 734 | { |
| 735 | struct hid_report *report; |
| 736 | struct hid_field *field; |
| 737 | struct hid_usage *usage; |
| 738 | int i, j; |
| 739 | |
| 740 | list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) { |
| 741 | for (i = 0; i < report->maxfield; i++) { |
| 742 | field = report->field[i]; |
| 743 | for (j = 0; j < field->maxusage; j++) { |
| 744 | usage = &field->usage[j]; |
| 745 | if ((usage->hid & HID_USAGE_PAGE) == page && |
| 746 | (usage->hid & 0xFFFF) == hid_code) { |
| 747 | *pfield = field; |
| 748 | return j; |
| 749 | } |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | return -1; |
| 754 | } |
| 755 | |
Jiri Slaby | 6edfa8d | 2008-06-27 20:41:02 +0200 | [diff] [blame] | 756 | void usbhid_set_leds(struct hid_device *hid) |
Pete Zaitcev | 713c8aa | 2007-04-06 14:33:18 +0200 | [diff] [blame] | 757 | { |
| 758 | struct hid_field *field; |
| 759 | int offset; |
| 760 | |
| 761 | if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) { |
| 762 | hid_set_field(field, offset, 0); |
| 763 | usbhid_submit_report(hid, field->report, USB_DIR_OUT); |
| 764 | } |
| 765 | } |
Jiri Slaby | 6edfa8d | 2008-06-27 20:41:02 +0200 | [diff] [blame] | 766 | EXPORT_SYMBOL_GPL(usbhid_set_leds); |
Pete Zaitcev | 713c8aa | 2007-04-06 14:33:18 +0200 | [diff] [blame] | 767 | |
| 768 | /* |
Michael Haboustak | bf0964d | 2005-09-05 00:12:01 -0500 | [diff] [blame] | 769 | * Traverse the supplied list of reports and find the longest |
| 770 | */ |
Jiri Slaby | 282bfd4 | 2008-03-28 17:06:41 +0100 | [diff] [blame] | 771 | static void hid_find_max_report(struct hid_device *hid, unsigned int type, |
| 772 | unsigned int *max) |
Michael Haboustak | bf0964d | 2005-09-05 00:12:01 -0500 | [diff] [blame] | 773 | { |
| 774 | struct hid_report *report; |
Jiri Slaby | 282bfd4 | 2008-03-28 17:06:41 +0100 | [diff] [blame] | 775 | unsigned int size; |
Michael Haboustak | bf0964d | 2005-09-05 00:12:01 -0500 | [diff] [blame] | 776 | |
| 777 | list_for_each_entry(report, &hid->report_enum[type].report_list, list) { |
Jiri Kosina | efc7ce1 | 2008-10-17 15:01:15 +0200 | [diff] [blame] | 778 | size = ((report->size - 1) >> 3) + 1 + hid->report_enum[type].numbered; |
Michael Haboustak | bf0964d | 2005-09-05 00:12:01 -0500 | [diff] [blame] | 779 | if (*max < size) |
| 780 | *max = size; |
| 781 | } |
| 782 | } |
| 783 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid) |
| 785 | { |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 786 | struct usbhid_device *usbhid = hid->driver_data; |
| 787 | |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 788 | usbhid->inbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL, |
Jiri Slaby | 898089d | 2008-11-24 16:20:06 +0100 | [diff] [blame] | 789 | &usbhid->inbuf_dma); |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 790 | usbhid->outbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL, |
Jiri Slaby | 898089d | 2008-11-24 16:20:06 +0100 | [diff] [blame] | 791 | &usbhid->outbuf_dma); |
Alan Stern | 0ede76f | 2010-03-05 15:10:17 -0500 | [diff] [blame] | 792 | usbhid->cr = kmalloc(sizeof(*usbhid->cr), GFP_KERNEL); |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 793 | usbhid->ctrlbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL, |
Jiri Slaby | 898089d | 2008-11-24 16:20:06 +0100 | [diff] [blame] | 794 | &usbhid->ctrlbuf_dma); |
| 795 | if (!usbhid->inbuf || !usbhid->outbuf || !usbhid->cr || |
| 796 | !usbhid->ctrlbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | return -1; |
| 798 | |
| 799 | return 0; |
| 800 | } |
| 801 | |
Alan Ott | b4dbde9 | 2011-01-18 03:04:39 -0500 | [diff] [blame] | 802 | static int usbhid_get_raw_report(struct hid_device *hid, |
| 803 | unsigned char report_number, __u8 *buf, size_t count, |
| 804 | unsigned char report_type) |
| 805 | { |
| 806 | struct usbhid_device *usbhid = hid->driver_data; |
| 807 | struct usb_device *dev = hid_to_usb_dev(hid); |
| 808 | struct usb_interface *intf = usbhid->intf; |
| 809 | struct usb_host_interface *interface = intf->cur_altsetting; |
| 810 | int skipped_report_id = 0; |
| 811 | int ret; |
| 812 | |
| 813 | /* Byte 0 is the report number. Report data starts at byte 1.*/ |
| 814 | buf[0] = report_number; |
| 815 | if (report_number == 0x0) { |
| 816 | /* Offset the return buffer by 1, so that the report ID |
| 817 | will remain in byte 0. */ |
| 818 | buf++; |
| 819 | count--; |
| 820 | skipped_report_id = 1; |
| 821 | } |
| 822 | ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 823 | HID_REQ_GET_REPORT, |
| 824 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
| 825 | ((report_type + 1) << 8) | report_number, |
| 826 | interface->desc.bInterfaceNumber, buf, count, |
| 827 | USB_CTRL_SET_TIMEOUT); |
| 828 | |
| 829 | /* count also the report id */ |
| 830 | if (ret > 0 && skipped_report_id) |
| 831 | ret++; |
| 832 | |
| 833 | return ret; |
| 834 | } |
| 835 | |
Jiri Kosina | d4bfa03 | 2010-01-29 15:03:36 +0100 | [diff] [blame] | 836 | static int usbhid_output_raw_report(struct hid_device *hid, __u8 *buf, size_t count, |
| 837 | unsigned char report_type) |
Jiri Kosina | efc493f | 2007-05-14 09:54:30 +0200 | [diff] [blame] | 838 | { |
| 839 | struct usbhid_device *usbhid = hid->driver_data; |
| 840 | struct usb_device *dev = hid_to_usb_dev(hid); |
| 841 | struct usb_interface *intf = usbhid->intf; |
| 842 | struct usb_host_interface *interface = intf->cur_altsetting; |
| 843 | int ret; |
| 844 | |
Alan Ott | fe2c91e | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 845 | if (usbhid->urbout && report_type != HID_FEATURE_REPORT) { |
Alan Ott | a8ab5d5 | 2010-05-16 18:07:09 -0400 | [diff] [blame] | 846 | int actual_length; |
| 847 | int skipped_report_id = 0; |
Alan Ott | 12e5272 | 2010-09-22 13:33:20 +0200 | [diff] [blame] | 848 | |
Alan Ott | a8ab5d5 | 2010-05-16 18:07:09 -0400 | [diff] [blame] | 849 | if (buf[0] == 0x0) { |
| 850 | /* Don't send the Report ID */ |
| 851 | buf++; |
| 852 | count--; |
| 853 | skipped_report_id = 1; |
| 854 | } |
| 855 | ret = usb_interrupt_msg(dev, usbhid->urbout->pipe, |
| 856 | buf, count, &actual_length, |
| 857 | USB_CTRL_SET_TIMEOUT); |
| 858 | /* return the number of bytes transferred */ |
| 859 | if (ret == 0) { |
| 860 | ret = actual_length; |
| 861 | /* count also the report id */ |
| 862 | if (skipped_report_id) |
| 863 | ret++; |
| 864 | } |
| 865 | } else { |
Alan Ott | 29129a9 | 2010-06-30 09:50:36 -0400 | [diff] [blame] | 866 | int skipped_report_id = 0; |
Alan Ott | c29771c | 2010-08-17 00:44:04 -0400 | [diff] [blame] | 867 | int report_id = buf[0]; |
Alan Ott | 29129a9 | 2010-06-30 09:50:36 -0400 | [diff] [blame] | 868 | if (buf[0] == 0x0) { |
| 869 | /* Don't send the Report ID */ |
| 870 | buf++; |
| 871 | count--; |
| 872 | skipped_report_id = 1; |
| 873 | } |
Alan Ott | a8ab5d5 | 2010-05-16 18:07:09 -0400 | [diff] [blame] | 874 | ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 875 | HID_REQ_SET_REPORT, |
| 876 | USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
Alan Ott | c29771c | 2010-08-17 00:44:04 -0400 | [diff] [blame] | 877 | ((report_type + 1) << 8) | report_id, |
Alan Ott | 29129a9 | 2010-06-30 09:50:36 -0400 | [diff] [blame] | 878 | interface->desc.bInterfaceNumber, buf, count, |
Alan Ott | a8ab5d5 | 2010-05-16 18:07:09 -0400 | [diff] [blame] | 879 | USB_CTRL_SET_TIMEOUT); |
Alan Ott | 29129a9 | 2010-06-30 09:50:36 -0400 | [diff] [blame] | 880 | /* count also the report id, if this was a numbered report. */ |
| 881 | if (ret > 0 && skipped_report_id) |
Alan Ott | a8ab5d5 | 2010-05-16 18:07:09 -0400 | [diff] [blame] | 882 | ret++; |
| 883 | } |
Jiri Kosina | efc493f | 2007-05-14 09:54:30 +0200 | [diff] [blame] | 884 | |
| 885 | return ret; |
| 886 | } |
| 887 | |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 888 | static void usbhid_restart_queues(struct usbhid_device *usbhid) |
| 889 | { |
| 890 | if (usbhid->urbout) |
| 891 | usbhid_restart_out_queue(usbhid); |
| 892 | usbhid_restart_ctrl_queue(usbhid); |
| 893 | } |
| 894 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid) |
| 896 | { |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 897 | struct usbhid_device *usbhid = hid->driver_data; |
| 898 | |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 899 | usb_free_coherent(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma); |
| 900 | usb_free_coherent(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma); |
Alan Stern | 0ede76f | 2010-03-05 15:10:17 -0500 | [diff] [blame] | 901 | kfree(usbhid->cr); |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 902 | usb_free_coherent(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | } |
| 904 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 905 | static int usbhid_parse(struct hid_device *hid) |
| 906 | { |
| 907 | struct usb_interface *intf = to_usb_interface(hid->dev.parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | struct usb_host_interface *interface = intf->cur_altsetting; |
| 909 | struct usb_device *dev = interface_to_usbdev (intf); |
| 910 | struct hid_descriptor *hdesc; |
Paul Walmsley | 2eb5dc3 | 2007-04-19 13:27:04 +0200 | [diff] [blame] | 911 | u32 quirks = 0; |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 912 | unsigned int rsize = 0; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 913 | char *rdesc; |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 914 | int ret, n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | |
Paul Walmsley | 2eb5dc3 | 2007-04-19 13:27:04 +0200 | [diff] [blame] | 916 | quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor), |
| 917 | le16_to_cpu(dev->descriptor.idProduct)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | |
Jiri Kosina | 6f4303f | 2009-01-29 00:15:51 +0100 | [diff] [blame] | 919 | if (quirks & HID_QUIRK_IGNORE) |
| 920 | return -ENODEV; |
| 921 | |
Alan Stern | 0f28b55 | 2006-05-15 14:49:04 -0400 | [diff] [blame] | 922 | /* Many keyboards and mice don't like to be polled for reports, |
| 923 | * so we will always set the HID_QUIRK_NOGET flag for them. */ |
| 924 | if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) { |
| 925 | if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD || |
| 926 | interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE) |
| 927 | quirks |= HID_QUIRK_NOGET; |
| 928 | } |
| 929 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 930 | if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) && |
| 931 | (!interface->desc.bNumEndpoints || |
| 932 | usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) { |
Jiri Kosina | 58037eb | 2007-05-30 15:07:13 +0200 | [diff] [blame] | 933 | dbg_hid("class descriptor not present\n"); |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 934 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | } |
| 936 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 937 | hid->version = le16_to_cpu(hdesc->bcdHID); |
| 938 | hid->country = hdesc->bCountryCode; |
| 939 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | for (n = 0; n < hdesc->bNumDescriptors; n++) |
| 941 | if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT) |
| 942 | rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength); |
| 943 | |
| 944 | if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) { |
Jiri Kosina | 58037eb | 2007-05-30 15:07:13 +0200 | [diff] [blame] | 945 | dbg_hid("weird size of report descriptor (%u)\n", rsize); |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 946 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) { |
Jiri Kosina | 58037eb | 2007-05-30 15:07:13 +0200 | [diff] [blame] | 950 | dbg_hid("couldn't allocate rdesc memory\n"); |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 951 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | } |
| 953 | |
Vojtech Pavlik | 854561b | 2005-05-29 02:28:00 -0500 | [diff] [blame] | 954 | hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0); |
| 955 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 956 | ret = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber, |
| 957 | HID_DT_REPORT, rdesc, rsize); |
| 958 | if (ret < 0) { |
Jiri Kosina | 58037eb | 2007-05-30 15:07:13 +0200 | [diff] [blame] | 959 | dbg_hid("reading report descriptor failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | kfree(rdesc); |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 961 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | } |
| 963 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 964 | ret = hid_parse_report(hid, rdesc, rsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | kfree(rdesc); |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 966 | if (ret) { |
| 967 | dbg_hid("parsing report descriptor failed\n"); |
| 968 | goto err; |
| 969 | } |
| 970 | |
Zoltan Karcagi | f520899 | 2009-05-06 16:30:21 +0200 | [diff] [blame] | 971 | hid->quirks |= quirks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 973 | return 0; |
| 974 | err: |
| 975 | return ret; |
| 976 | } |
| 977 | |
| 978 | static int usbhid_start(struct hid_device *hid) |
| 979 | { |
| 980 | struct usb_interface *intf = to_usb_interface(hid->dev.parent); |
| 981 | struct usb_host_interface *interface = intf->cur_altsetting; |
| 982 | struct usb_device *dev = interface_to_usbdev(intf); |
Jiri Slaby | 3d5afd3 | 2008-10-27 12:16:15 +0100 | [diff] [blame] | 983 | struct usbhid_device *usbhid = hid->driver_data; |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 984 | unsigned int n, insize = 0; |
| 985 | int ret; |
| 986 | |
Jiri Slaby | e3e14de | 2008-11-01 23:41:46 +0100 | [diff] [blame] | 987 | clear_bit(HID_DISCONNECTED, &usbhid->iofl); |
| 988 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 989 | usbhid->bufsize = HID_MIN_BUFFER_SIZE; |
| 990 | hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize); |
| 991 | hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize); |
| 992 | hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize); |
| 993 | |
| 994 | if (usbhid->bufsize > HID_MAX_BUFFER_SIZE) |
| 995 | usbhid->bufsize = HID_MAX_BUFFER_SIZE; |
Michael Haboustak | bf0964d | 2005-09-05 00:12:01 -0500 | [diff] [blame] | 996 | |
| 997 | hid_find_max_report(hid, HID_INPUT_REPORT, &insize); |
| 998 | |
| 999 | if (insize > HID_MAX_BUFFER_SIZE) |
| 1000 | insize = HID_MAX_BUFFER_SIZE; |
| 1001 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1002 | if (hid_alloc_buffers(dev, hid)) { |
| 1003 | ret = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | goto fail; |
Pekka Sarnila | f345c37 | 2008-03-06 13:23:14 +0100 | [diff] [blame] | 1005 | } |
| 1006 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | for (n = 0; n < interface->desc.bNumEndpoints; n++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | struct usb_endpoint_descriptor *endpoint; |
| 1009 | int pipe; |
| 1010 | int interval; |
| 1011 | |
| 1012 | endpoint = &interface->endpoint[n].desc; |
Jiri Slaby | 581a273 | 2008-11-24 16:20:08 +0100 | [diff] [blame] | 1013 | if (!usb_endpoint_xfer_int(endpoint)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | continue; |
| 1015 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | interval = endpoint->bInterval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | |
Pekka Sarnila | f345c37 | 2008-03-06 13:23:14 +0100 | [diff] [blame] | 1018 | /* Some vendors give fullspeed interval on highspeed devides */ |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1019 | if (hid->quirks & HID_QUIRK_FULLSPEED_INTERVAL && |
Pekka Sarnila | f345c37 | 2008-03-06 13:23:14 +0100 | [diff] [blame] | 1020 | dev->speed == USB_SPEED_HIGH) { |
| 1021 | interval = fls(endpoint->bInterval*8); |
| 1022 | printk(KERN_INFO "%s: Fixing fullspeed to highspeed interval: %d -> %d\n", |
| 1023 | hid->name, endpoint->bInterval, interval); |
| 1024 | } |
| 1025 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | /* Change the polling interval of mice. */ |
| 1027 | if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0) |
| 1028 | interval = hid_mousepoll_interval; |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 1029 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1030 | ret = -ENOMEM; |
Luiz Fernando N. Capitulino | 0f12aa0 | 2006-10-26 13:02:51 -0300 | [diff] [blame] | 1031 | if (usb_endpoint_dir_in(endpoint)) { |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 1032 | if (usbhid->urbin) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | continue; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 1034 | if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | goto fail; |
| 1036 | pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress); |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 1037 | usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | hid_irq_in, hid, interval); |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 1039 | usbhid->urbin->transfer_dma = usbhid->inbuf_dma; |
| 1040 | usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | } else { |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 1042 | if (usbhid->urbout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | continue; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 1044 | if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | goto fail; |
| 1046 | pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress); |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 1047 | usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | hid_irq_out, hid, interval); |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 1049 | usbhid->urbout->transfer_dma = usbhid->outbuf_dma; |
| 1050 | usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | } |
| 1052 | } |
| 1053 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 1054 | usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL); |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1055 | if (!usbhid->urbctrl) { |
| 1056 | ret = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | goto fail; |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1058 | } |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 1059 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 1060 | usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr, |
| 1061 | usbhid->ctrlbuf, 1, hid_ctrl, hid); |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 1062 | usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma; |
Alan Stern | 0ede76f | 2010-03-05 15:10:17 -0500 | [diff] [blame] | 1063 | usbhid->urbctrl->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1064 | |
Jiri Kosina | 5b915d9 | 2009-11-05 14:08:03 +0100 | [diff] [blame] | 1065 | if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS)) |
| 1066 | usbhid_init_reports(hid); |
Jiri Slaby | 93c1013 | 2008-06-27 00:04:24 +0200 | [diff] [blame] | 1067 | |
Jiri Slaby | 3d5afd3 | 2008-10-27 12:16:15 +0100 | [diff] [blame] | 1068 | set_bit(HID_STARTED, &usbhid->iofl); |
Jiri Slaby | 3d5afd3 | 2008-10-27 12:16:15 +0100 | [diff] [blame] | 1069 | |
Alan Stern | 08ef08e | 2008-10-30 23:58:51 +0100 | [diff] [blame] | 1070 | /* Some keyboards don't work until their LEDs have been set. |
| 1071 | * Since BIOSes do set the LEDs, it must be safe for any device |
| 1072 | * that supports the keyboard boot protocol. |
Alan Stern | 3d61510 | 2010-04-02 13:21:58 -0400 | [diff] [blame] | 1073 | * In addition, enable remote wakeup by default for all keyboard |
| 1074 | * devices supporting the boot protocol. |
Alan Stern | 08ef08e | 2008-10-30 23:58:51 +0100 | [diff] [blame] | 1075 | */ |
| 1076 | if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT && |
| 1077 | interface->desc.bInterfaceProtocol == |
Alan Stern | 3d61510 | 2010-04-02 13:21:58 -0400 | [diff] [blame] | 1078 | USB_INTERFACE_PROTOCOL_KEYBOARD) { |
Alan Stern | 08ef08e | 2008-10-30 23:58:51 +0100 | [diff] [blame] | 1079 | usbhid_set_leds(hid); |
Alan Stern | 3d61510 | 2010-04-02 13:21:58 -0400 | [diff] [blame] | 1080 | device_set_wakeup_enable(&dev->dev, 1); |
| 1081 | } |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1082 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | |
| 1084 | fail: |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 1085 | usb_free_urb(usbhid->urbin); |
| 1086 | usb_free_urb(usbhid->urbout); |
| 1087 | usb_free_urb(usbhid->urbctrl); |
Jiri Slaby | e3e14de | 2008-11-01 23:41:46 +0100 | [diff] [blame] | 1088 | usbhid->urbin = NULL; |
| 1089 | usbhid->urbout = NULL; |
| 1090 | usbhid->urbctrl = NULL; |
Jiri Kosina | 22f675f | 2007-08-01 12:32:27 +0200 | [diff] [blame] | 1091 | hid_free_buffers(dev, hid); |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1092 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1095 | static void usbhid_stop(struct hid_device *hid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | { |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1097 | struct usbhid_device *usbhid = hid->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1099 | if (WARN_ON(!usbhid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | return; |
| 1101 | |
Jiri Slaby | 3d5afd3 | 2008-10-27 12:16:15 +0100 | [diff] [blame] | 1102 | clear_bit(HID_STARTED, &usbhid->iofl); |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 1103 | spin_lock_irq(&usbhid->lock); /* Sync with error handler */ |
Oliver Neukum | 69626f2 | 2008-03-31 16:27:30 +0200 | [diff] [blame] | 1104 | set_bit(HID_DISCONNECTED, &usbhid->iofl); |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 1105 | spin_unlock_irq(&usbhid->lock); |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 1106 | usb_kill_urb(usbhid->urbin); |
| 1107 | usb_kill_urb(usbhid->urbout); |
| 1108 | usb_kill_urb(usbhid->urbctrl); |
| 1109 | |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 1110 | hid_cancel_delayed_stuff(usbhid); |
Alan Stern | aef4e26 | 2006-01-31 12:58:38 -0500 | [diff] [blame] | 1111 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1112 | hid->claimed = 0; |
| 1113 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 1114 | usb_free_urb(usbhid->urbin); |
| 1115 | usb_free_urb(usbhid->urbctrl); |
| 1116 | usb_free_urb(usbhid->urbout); |
Jiri Slaby | e3e14de | 2008-11-01 23:41:46 +0100 | [diff] [blame] | 1117 | usbhid->urbin = NULL; /* don't mess up next start */ |
| 1118 | usbhid->urbctrl = NULL; |
| 1119 | usbhid->urbout = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | |
Anssi Hannula | be82097 | 2007-01-19 19:28:17 +0200 | [diff] [blame] | 1121 | hid_free_buffers(hid_to_usb_dev(hid), hid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | } |
| 1123 | |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 1124 | static int usbhid_power(struct hid_device *hid, int lvl) |
| 1125 | { |
| 1126 | int r = 0; |
| 1127 | |
| 1128 | switch (lvl) { |
| 1129 | case PM_HINT_FULLON: |
| 1130 | r = usbhid_get_power(hid); |
| 1131 | break; |
| 1132 | case PM_HINT_NORMAL: |
| 1133 | usbhid_put_power(hid); |
| 1134 | break; |
| 1135 | } |
| 1136 | return r; |
| 1137 | } |
| 1138 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1139 | static struct hid_ll_driver usb_hid_driver = { |
| 1140 | .parse = usbhid_parse, |
| 1141 | .start = usbhid_start, |
| 1142 | .stop = usbhid_stop, |
| 1143 | .open = usbhid_open, |
| 1144 | .close = usbhid_close, |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 1145 | .power = usbhid_power, |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1146 | .hidinput_input_event = usb_hidinput_input_event, |
| 1147 | }; |
| 1148 | |
Jiri Kosina | c4c259b | 2009-09-15 16:27:45 +0200 | [diff] [blame] | 1149 | static int usbhid_probe(struct usb_interface *intf, const struct usb_device_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | { |
Jiri Slaby | 131d3a7a | 2008-11-14 12:03:47 +0100 | [diff] [blame] | 1151 | struct usb_host_interface *interface = intf->cur_altsetting; |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1152 | struct usb_device *dev = interface_to_usbdev(intf); |
Jiri Slaby | 3d5afd3 | 2008-10-27 12:16:15 +0100 | [diff] [blame] | 1153 | struct usbhid_device *usbhid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | struct hid_device *hid; |
Jiri Slaby | 131d3a7a | 2008-11-14 12:03:47 +0100 | [diff] [blame] | 1155 | unsigned int n, has_in = 0; |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1156 | size_t len; |
| 1157 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | |
Jiri Kosina | 58037eb | 2007-05-30 15:07:13 +0200 | [diff] [blame] | 1159 | dbg_hid("HID probe called for ifnum %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | intf->altsetting->desc.bInterfaceNumber); |
| 1161 | |
Jiri Slaby | 131d3a7a | 2008-11-14 12:03:47 +0100 | [diff] [blame] | 1162 | for (n = 0; n < interface->desc.bNumEndpoints; n++) |
| 1163 | if (usb_endpoint_is_int_in(&interface->endpoint[n].desc)) |
| 1164 | has_in++; |
| 1165 | if (!has_in) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 1166 | hid_err(intf, "couldn't find an input interrupt endpoint\n"); |
Jiri Slaby | 131d3a7a | 2008-11-14 12:03:47 +0100 | [diff] [blame] | 1167 | return -ENODEV; |
| 1168 | } |
| 1169 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1170 | hid = hid_allocate_device(); |
| 1171 | if (IS_ERR(hid)) |
| 1172 | return PTR_ERR(hid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | |
| 1174 | usb_set_intfdata(intf, hid); |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1175 | hid->ll_driver = &usb_hid_driver; |
Alan Ott | b4dbde9 | 2011-01-18 03:04:39 -0500 | [diff] [blame] | 1176 | hid->hid_get_raw_report = usbhid_get_raw_report; |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1177 | hid->hid_output_raw_report = usbhid_output_raw_report; |
Jiri Slaby | 76483cf | 2008-09-18 12:23:33 +0200 | [diff] [blame] | 1178 | hid->ff_init = hid_pidff_init; |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1179 | #ifdef CONFIG_USB_HIDDEV |
Jiri Slaby | 93c1013 | 2008-06-27 00:04:24 +0200 | [diff] [blame] | 1180 | hid->hiddev_connect = hiddev_connect; |
Jiri Kosina | c4c259b | 2009-09-15 16:27:45 +0200 | [diff] [blame] | 1181 | hid->hiddev_disconnect = hiddev_disconnect; |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1182 | hid->hiddev_hid_event = hiddev_hid_event; |
| 1183 | hid->hiddev_report_event = hiddev_report_event; |
| 1184 | #endif |
| 1185 | hid->dev.parent = &intf->dev; |
| 1186 | hid->bus = BUS_USB; |
| 1187 | hid->vendor = le16_to_cpu(dev->descriptor.idVendor); |
| 1188 | hid->product = le16_to_cpu(dev->descriptor.idProduct); |
| 1189 | hid->name[0] = 0; |
Bastien Nocera | b5e5a37 | 2010-04-16 17:19:50 +0100 | [diff] [blame] | 1190 | hid->quirks = usbhid_lookup_quirk(hid->vendor, hid->product); |
Jiri Slaby | a73a637 | 2008-10-22 14:45:11 +0200 | [diff] [blame] | 1191 | if (intf->cur_altsetting->desc.bInterfaceProtocol == |
| 1192 | USB_INTERFACE_PROTOCOL_MOUSE) |
| 1193 | hid->type = HID_TYPE_USBMOUSE; |
Tomoki Sekiyama | 6dc1418 | 2011-05-23 15:45:44 -0700 | [diff] [blame] | 1194 | else if (intf->cur_altsetting->desc.bInterfaceProtocol == 0) |
| 1195 | hid->type = HID_TYPE_USBNONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1197 | if (dev->manufacturer) |
| 1198 | strlcpy(hid->name, dev->manufacturer, sizeof(hid->name)); |
| 1199 | |
| 1200 | if (dev->product) { |
| 1201 | if (dev->manufacturer) |
| 1202 | strlcat(hid->name, " ", sizeof(hid->name)); |
| 1203 | strlcat(hid->name, dev->product, sizeof(hid->name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | } |
| 1205 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1206 | if (!strlen(hid->name)) |
| 1207 | snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x", |
| 1208 | le16_to_cpu(dev->descriptor.idVendor), |
| 1209 | le16_to_cpu(dev->descriptor.idProduct)); |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 1210 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1211 | usb_make_path(dev, hid->phys, sizeof(hid->phys)); |
| 1212 | strlcat(hid->phys, "/input", sizeof(hid->phys)); |
| 1213 | len = strlen(hid->phys); |
| 1214 | if (len < sizeof(hid->phys) - 1) |
| 1215 | snprintf(hid->phys + len, sizeof(hid->phys) - len, |
| 1216 | "%d", intf->altsetting[0].desc.bInterfaceNumber); |
Geoff Levand | 4a1a4d8 | 2007-01-15 20:11:52 -0800 | [diff] [blame] | 1217 | |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1218 | if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0) |
| 1219 | hid->uniq[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | |
Jiri Slaby | 3d5afd3 | 2008-10-27 12:16:15 +0100 | [diff] [blame] | 1221 | usbhid = kzalloc(sizeof(*usbhid), GFP_KERNEL); |
| 1222 | if (usbhid == NULL) { |
| 1223 | ret = -ENOMEM; |
| 1224 | goto err; |
| 1225 | } |
| 1226 | |
| 1227 | hid->driver_data = usbhid; |
| 1228 | usbhid->hid = hid; |
Jiri Kosina | 57ab12e | 2010-02-17 14:25:01 +0100 | [diff] [blame] | 1229 | usbhid->intf = intf; |
| 1230 | usbhid->ifnum = interface->desc.bInterfaceNumber; |
Jiri Slaby | 3d5afd3 | 2008-10-27 12:16:15 +0100 | [diff] [blame] | 1231 | |
Alan Stern | fde4e2f | 2010-05-07 10:41:10 -0400 | [diff] [blame] | 1232 | init_waitqueue_head(&usbhid->wait); |
| 1233 | INIT_WORK(&usbhid->reset_work, hid_reset); |
Alan Stern | fde4e2f | 2010-05-07 10:41:10 -0400 | [diff] [blame] | 1234 | setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid); |
| 1235 | spin_lock_init(&usbhid->lock); |
| 1236 | |
Jiri Slaby | 85cdaf5 | 2008-05-16 11:49:15 +0200 | [diff] [blame] | 1237 | ret = hid_add_device(hid); |
| 1238 | if (ret) { |
Jiri Slaby | d458a9d | 2008-05-16 11:49:20 +0200 | [diff] [blame] | 1239 | if (ret != -ENODEV) |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 1240 | hid_err(intf, "can't add hid device: %d\n", ret); |
Jiri Slaby | 3d5afd3 | 2008-10-27 12:16:15 +0100 | [diff] [blame] | 1241 | goto err_free; |
Jiri Slaby | 85cdaf5 | 2008-05-16 11:49:15 +0200 | [diff] [blame] | 1242 | } |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1243 | |
| 1244 | return 0; |
Jiri Slaby | 3d5afd3 | 2008-10-27 12:16:15 +0100 | [diff] [blame] | 1245 | err_free: |
| 1246 | kfree(usbhid); |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1247 | err: |
| 1248 | hid_destroy_device(hid); |
Jiri Slaby | 85cdaf5 | 2008-05-16 11:49:15 +0200 | [diff] [blame] | 1249 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | } |
| 1251 | |
Jiri Kosina | c4c259b | 2009-09-15 16:27:45 +0200 | [diff] [blame] | 1252 | static void usbhid_disconnect(struct usb_interface *intf) |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1253 | { |
| 1254 | struct hid_device *hid = usb_get_intfdata(intf); |
Jiri Slaby | 3d5afd3 | 2008-10-27 12:16:15 +0100 | [diff] [blame] | 1255 | struct usbhid_device *usbhid; |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1256 | |
| 1257 | if (WARN_ON(!hid)) |
| 1258 | return; |
| 1259 | |
Jiri Slaby | 3d5afd3 | 2008-10-27 12:16:15 +0100 | [diff] [blame] | 1260 | usbhid = hid->driver_data; |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1261 | hid_destroy_device(hid); |
Jiri Slaby | 3d5afd3 | 2008-10-27 12:16:15 +0100 | [diff] [blame] | 1262 | kfree(usbhid); |
Jiri Slaby | c500c97 | 2008-05-16 11:49:16 +0200 | [diff] [blame] | 1263 | } |
| 1264 | |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 1265 | static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | { |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 1267 | del_timer_sync(&usbhid->io_retry); |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 1268 | cancel_work_sync(&usbhid->reset_work); |
| 1269 | } |
| 1270 | |
| 1271 | static void hid_cease_io(struct usbhid_device *usbhid) |
| 1272 | { |
| 1273 | del_timer(&usbhid->io_retry); |
| 1274 | usb_kill_urb(usbhid->urbin); |
| 1275 | usb_kill_urb(usbhid->urbctrl); |
| 1276 | usb_kill_urb(usbhid->urbout); |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 1277 | } |
| 1278 | |
Jiri Kosina | ae2f007 | 2009-02-20 12:47:08 +0100 | [diff] [blame] | 1279 | /* Treat USB reset pretty much the same as suspend/resume */ |
| 1280 | static int hid_pre_reset(struct usb_interface *intf) |
| 1281 | { |
| 1282 | struct hid_device *hid = usb_get_intfdata(intf); |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 1283 | struct usbhid_device *usbhid = hid->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | |
Jiri Kosina | ae2f007 | 2009-02-20 12:47:08 +0100 | [diff] [blame] | 1285 | spin_lock_irq(&usbhid->lock); |
| 1286 | set_bit(HID_RESET_PENDING, &usbhid->iofl); |
| 1287 | spin_unlock_irq(&usbhid->lock); |
| 1288 | hid_cease_io(usbhid); |
Jiri Slaby | 3d5afd3 | 2008-10-27 12:16:15 +0100 | [diff] [blame] | 1289 | |
Jiri Kosina | ae2f007 | 2009-02-20 12:47:08 +0100 | [diff] [blame] | 1290 | return 0; |
| 1291 | } |
| 1292 | |
| 1293 | /* Same routine used for post_reset and reset_resume */ |
| 1294 | static int hid_post_reset(struct usb_interface *intf) |
| 1295 | { |
| 1296 | struct usb_device *dev = interface_to_usbdev (intf); |
| 1297 | struct hid_device *hid = usb_get_intfdata(intf); |
| 1298 | struct usbhid_device *usbhid = hid->driver_data; |
| 1299 | int status; |
Jiri Kosina | 70fa9f2 | 2009-06-04 15:48:38 +0200 | [diff] [blame] | 1300 | |
Jiri Kosina | ae2f007 | 2009-02-20 12:47:08 +0100 | [diff] [blame] | 1301 | spin_lock_irq(&usbhid->lock); |
| 1302 | clear_bit(HID_RESET_PENDING, &usbhid->iofl); |
| 1303 | spin_unlock_irq(&usbhid->lock); |
| 1304 | hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0); |
Jiri Kosina | ae2f007 | 2009-02-20 12:47:08 +0100 | [diff] [blame] | 1305 | status = hid_start_in(hid); |
| 1306 | if (status < 0) |
| 1307 | hid_io_error(hid); |
| 1308 | usbhid_restart_queues(usbhid); |
| 1309 | |
| 1310 | return 0; |
| 1311 | } |
| 1312 | |
| 1313 | int usbhid_get_power(struct hid_device *hid) |
| 1314 | { |
| 1315 | struct usbhid_device *usbhid = hid->driver_data; |
Jiri Kosina | 70fa9f2 | 2009-06-04 15:48:38 +0200 | [diff] [blame] | 1316 | |
Jiri Kosina | ae2f007 | 2009-02-20 12:47:08 +0100 | [diff] [blame] | 1317 | return usb_autopm_get_interface(usbhid->intf); |
| 1318 | } |
| 1319 | |
| 1320 | void usbhid_put_power(struct hid_device *hid) |
| 1321 | { |
| 1322 | struct usbhid_device *usbhid = hid->driver_data; |
Jiri Kosina | 70fa9f2 | 2009-06-04 15:48:38 +0200 | [diff] [blame] | 1323 | |
Jiri Kosina | ae2f007 | 2009-02-20 12:47:08 +0100 | [diff] [blame] | 1324 | usb_autopm_put_interface(usbhid->intf); |
| 1325 | } |
| 1326 | |
| 1327 | |
Jiri Kosina | 0f6f140 | 2009-01-19 09:17:18 +0100 | [diff] [blame] | 1328 | #ifdef CONFIG_PM |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | static int hid_suspend(struct usb_interface *intf, pm_message_t message) |
| 1330 | { |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 1331 | struct hid_device *hid = usb_get_intfdata(intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | struct usbhid_device *usbhid = hid->driver_data; |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 1333 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | |
Alan Stern | fb34d53 | 2009-11-13 11:53:59 -0500 | [diff] [blame] | 1335 | if (message.event & PM_EVENT_AUTO) { |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 1336 | spin_lock_irq(&usbhid->lock); /* Sync with error handler */ |
| 1337 | if (!test_bit(HID_RESET_PENDING, &usbhid->iofl) |
| 1338 | && !test_bit(HID_CLEAR_HALT, &usbhid->iofl) |
| 1339 | && !test_bit(HID_OUT_RUNNING, &usbhid->iofl) |
| 1340 | && !test_bit(HID_CTRL_RUNNING, &usbhid->iofl) |
| 1341 | && !test_bit(HID_KEYS_PRESSED, &usbhid->iofl) |
| 1342 | && (!usbhid->ledcount || ignoreled)) |
| 1343 | { |
| 1344 | set_bit(HID_REPORTED_IDLE, &usbhid->iofl); |
| 1345 | spin_unlock_irq(&usbhid->lock); |
Bruno Prémont | 6a740aa | 2010-04-25 21:40:03 +0200 | [diff] [blame] | 1346 | if (hid->driver && hid->driver->suspend) { |
| 1347 | status = hid->driver->suspend(hid, message); |
| 1348 | if (status < 0) |
| 1349 | return status; |
| 1350 | } |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 1351 | } else { |
| 1352 | usbhid_mark_busy(usbhid); |
| 1353 | spin_unlock_irq(&usbhid->lock); |
| 1354 | return -EBUSY; |
| 1355 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 1357 | } else { |
Bruno Prémont | 6a740aa | 2010-04-25 21:40:03 +0200 | [diff] [blame] | 1358 | if (hid->driver && hid->driver->suspend) { |
| 1359 | status = hid->driver->suspend(hid, message); |
| 1360 | if (status < 0) |
| 1361 | return status; |
| 1362 | } |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 1363 | spin_lock_irq(&usbhid->lock); |
| 1364 | set_bit(HID_REPORTED_IDLE, &usbhid->iofl); |
| 1365 | spin_unlock_irq(&usbhid->lock); |
| 1366 | if (usbhid_wait_io(hid) < 0) |
| 1367 | return -EIO; |
| 1368 | } |
| 1369 | |
Alan Stern | fb34d53 | 2009-11-13 11:53:59 -0500 | [diff] [blame] | 1370 | if (!ignoreled && (message.event & PM_EVENT_AUTO)) { |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 1371 | spin_lock_irq(&usbhid->lock); |
| 1372 | if (test_bit(HID_LED_ON, &usbhid->iofl)) { |
| 1373 | spin_unlock_irq(&usbhid->lock); |
| 1374 | usbhid_mark_busy(usbhid); |
| 1375 | return -EBUSY; |
| 1376 | } |
| 1377 | spin_unlock_irq(&usbhid->lock); |
| 1378 | } |
| 1379 | |
| 1380 | hid_cancel_delayed_stuff(usbhid); |
| 1381 | hid_cease_io(usbhid); |
| 1382 | |
Alan Stern | fb34d53 | 2009-11-13 11:53:59 -0500 | [diff] [blame] | 1383 | if ((message.event & PM_EVENT_AUTO) && |
| 1384 | test_bit(HID_KEYS_PRESSED, &usbhid->iofl)) { |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 1385 | /* lost race against keypresses */ |
| 1386 | status = hid_start_in(hid); |
| 1387 | if (status < 0) |
| 1388 | hid_io_error(hid); |
| 1389 | usbhid_mark_busy(usbhid); |
| 1390 | return -EBUSY; |
| 1391 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | dev_dbg(&intf->dev, "suspend\n"); |
| 1393 | return 0; |
| 1394 | } |
| 1395 | |
| 1396 | static int hid_resume(struct usb_interface *intf) |
| 1397 | { |
| 1398 | struct hid_device *hid = usb_get_intfdata (intf); |
| 1399 | struct usbhid_device *usbhid = hid->driver_data; |
| 1400 | int status; |
| 1401 | |
Jiri Slaby | fde5be3 | 2008-11-23 12:03:20 +0100 | [diff] [blame] | 1402 | if (!test_bit(HID_STARTED, &usbhid->iofl)) |
Jiri Slaby | 3d5afd3 | 2008-10-27 12:16:15 +0100 | [diff] [blame] | 1403 | return 0; |
Jiri Slaby | 3d5afd3 | 2008-10-27 12:16:15 +0100 | [diff] [blame] | 1404 | |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 1405 | clear_bit(HID_REPORTED_IDLE, &usbhid->iofl); |
| 1406 | usbhid_mark_busy(usbhid); |
| 1407 | |
| 1408 | if (test_bit(HID_CLEAR_HALT, &usbhid->iofl) || |
| 1409 | test_bit(HID_RESET_PENDING, &usbhid->iofl)) |
| 1410 | schedule_work(&usbhid->reset_work); |
Alan Stern | df9a1f4 | 2006-06-01 13:55:28 -0400 | [diff] [blame] | 1411 | usbhid->retry_delay = 0; |
| 1412 | status = hid_start_in(hid); |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 1413 | if (status < 0) |
| 1414 | hid_io_error(hid); |
| 1415 | usbhid_restart_queues(usbhid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | |
Bruno Prémont | 6a740aa | 2010-04-25 21:40:03 +0200 | [diff] [blame] | 1417 | if (status >= 0 && hid->driver && hid->driver->resume) { |
| 1418 | int ret = hid->driver->resume(hid); |
| 1419 | if (ret < 0) |
| 1420 | status = ret; |
| 1421 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | dev_dbg(&intf->dev, "resume status %d\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | return 0; |
| 1424 | } |
| 1425 | |
Oliver Neukum | 378a0ed | 2009-02-18 11:46:45 +0100 | [diff] [blame] | 1426 | static int hid_reset_resume(struct usb_interface *intf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | { |
Oliver Neukum | 378a0ed | 2009-02-18 11:46:45 +0100 | [diff] [blame] | 1428 | struct hid_device *hid = usb_get_intfdata(intf); |
| 1429 | struct usbhid_device *usbhid = hid->driver_data; |
Bruno Prémont | 6a740aa | 2010-04-25 21:40:03 +0200 | [diff] [blame] | 1430 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | |
Oliver Neukum | 378a0ed | 2009-02-18 11:46:45 +0100 | [diff] [blame] | 1432 | clear_bit(HID_REPORTED_IDLE, &usbhid->iofl); |
Bruno Prémont | 6a740aa | 2010-04-25 21:40:03 +0200 | [diff] [blame] | 1433 | status = hid_post_reset(intf); |
| 1434 | if (status >= 0 && hid->driver && hid->driver->reset_resume) { |
| 1435 | int ret = hid->driver->reset_resume(hid); |
| 1436 | if (ret < 0) |
| 1437 | status = ret; |
| 1438 | } |
| 1439 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | } |
| 1441 | |
Jiri Kosina | ae2f007 | 2009-02-20 12:47:08 +0100 | [diff] [blame] | 1442 | #endif /* CONFIG_PM */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | |
Márton Németh | d67dec5 | 2010-01-10 17:59:22 +0100 | [diff] [blame] | 1444 | static const struct usb_device_id hid_usb_ids[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS, |
| 1446 | .bInterfaceClass = USB_INTERFACE_CLASS_HID }, |
| 1447 | { } /* Terminating entry */ |
| 1448 | }; |
| 1449 | |
| 1450 | MODULE_DEVICE_TABLE (usb, hid_usb_ids); |
| 1451 | |
| 1452 | static struct usb_driver hid_driver = { |
| 1453 | .name = "usbhid", |
Jiri Kosina | c4c259b | 2009-09-15 16:27:45 +0200 | [diff] [blame] | 1454 | .probe = usbhid_probe, |
| 1455 | .disconnect = usbhid_disconnect, |
Jiri Kosina | 0f6f140 | 2009-01-19 09:17:18 +0100 | [diff] [blame] | 1456 | #ifdef CONFIG_PM |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | .suspend = hid_suspend, |
| 1458 | .resume = hid_resume, |
Oliver Neukum | 378a0ed | 2009-02-18 11:46:45 +0100 | [diff] [blame] | 1459 | .reset_resume = hid_reset_resume, |
Jiri Kosina | 0f6f140 | 2009-01-19 09:17:18 +0100 | [diff] [blame] | 1460 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | .pre_reset = hid_pre_reset, |
| 1462 | .post_reset = hid_post_reset, |
| 1463 | .id_table = hid_usb_ids, |
Oliver Neukum | 933e318 | 2007-07-11 14:48:58 +0200 | [diff] [blame] | 1464 | .supports_autosuspend = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | }; |
| 1466 | |
Jiri Slaby | 85cdaf5 | 2008-05-16 11:49:15 +0200 | [diff] [blame] | 1467 | static const struct hid_device_id hid_usb_table[] = { |
| 1468 | { HID_USB_DEVICE(HID_ANY_ID, HID_ANY_ID) }, |
| 1469 | { } |
| 1470 | }; |
| 1471 | |
Guillaume Chazarain | 8fe294c | 2010-09-12 21:32:35 +0200 | [diff] [blame] | 1472 | struct usb_interface *usbhid_find_interface(int minor) |
| 1473 | { |
| 1474 | return usb_find_interface(&hid_driver, minor); |
| 1475 | } |
| 1476 | |
Jiri Slaby | 85cdaf5 | 2008-05-16 11:49:15 +0200 | [diff] [blame] | 1477 | static struct hid_driver hid_usb_driver = { |
| 1478 | .name = "generic-usb", |
| 1479 | .id_table = hid_usb_table, |
| 1480 | }; |
| 1481 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | static int __init hid_init(void) |
| 1483 | { |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 1484 | int retval = -ENOMEM; |
| 1485 | |
Jiri Slaby | 85cdaf5 | 2008-05-16 11:49:15 +0200 | [diff] [blame] | 1486 | retval = hid_register_driver(&hid_usb_driver); |
| 1487 | if (retval) |
| 1488 | goto hid_register_fail; |
Paul Walmsley | 876b927 | 2007-04-19 14:56:12 +0200 | [diff] [blame] | 1489 | retval = usbhid_quirks_init(quirks_param); |
| 1490 | if (retval) |
| 1491 | goto usbhid_quirks_init_fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | retval = usb_register(&hid_driver); |
| 1493 | if (retval) |
| 1494 | goto usb_register_fail; |
Jiri Kosina | ccabcd2 | 2009-10-02 18:31:36 +0200 | [diff] [blame] | 1495 | printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | |
| 1497 | return 0; |
| 1498 | usb_register_fail: |
Paul Walmsley | 876b927 | 2007-04-19 14:56:12 +0200 | [diff] [blame] | 1499 | usbhid_quirks_exit(); |
| 1500 | usbhid_quirks_init_fail: |
Jiri Slaby | 85cdaf5 | 2008-05-16 11:49:15 +0200 | [diff] [blame] | 1501 | hid_unregister_driver(&hid_usb_driver); |
| 1502 | hid_register_fail: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | return retval; |
| 1504 | } |
| 1505 | |
| 1506 | static void __exit hid_exit(void) |
| 1507 | { |
| 1508 | usb_deregister(&hid_driver); |
Paul Walmsley | 876b927 | 2007-04-19 14:56:12 +0200 | [diff] [blame] | 1509 | usbhid_quirks_exit(); |
Jiri Slaby | 85cdaf5 | 2008-05-16 11:49:15 +0200 | [diff] [blame] | 1510 | hid_unregister_driver(&hid_usb_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | module_init(hid_init); |
| 1514 | module_exit(hid_exit); |
| 1515 | |
Jiri Kosina | 88adb72 | 2009-10-02 18:29:34 +0200 | [diff] [blame] | 1516 | MODULE_AUTHOR("Andreas Gal"); |
| 1517 | MODULE_AUTHOR("Vojtech Pavlik"); |
| 1518 | MODULE_AUTHOR("Jiri Kosina"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 1520 | MODULE_LICENSE(DRIVER_LICENSE); |