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