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