blob: 79cf503e37bf14eadd227f3e9e54c1a9f488ec25 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * USB HID support for Linux
3 *
4 * Copyright (c) 1999 Andreas Gal
Michael Haboustakbf0964d2005-09-05 00:12:01 -05005 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
Oliver Neukum0361a282008-12-17 15:38:03 +01007 * Copyright (c) 2007-2008 Oliver Neukum
Jiri Kosina7d39e842010-02-02 20:46:34 +01008 * Copyright (c) 2006-2010 Jiri Kosina
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
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 Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/list.h>
23#include <linux/mm.h>
Jiri Slaby3d5afd32008-10-27 12:16:15 +010024#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#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 Neukum0361a282008-12-17 15:38:03 +010030#include <linux/workqueue.h>
Simon Haggettdc3c78e2012-04-03 16:04:15 +010031#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/usb.h>
34
Jiri Kosinadde58452006-12-08 18:40:44 +010035#include <linux/hid.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/hiddev.h>
Jiri Kosinac080d892007-01-25 11:43:31 +010037#include <linux/hid-debug.h>
Jiri Kosina86166b72007-05-14 09:57:40 +020038#include <linux/hidraw.h>
Jiri Kosina4916b3a2006-12-08 18:41:03 +010039#include "usbhid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/*
42 * Version Information
43 */
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define DRIVER_DESC "USB HID core driver"
46#define DRIVER_LICENSE "GPL"
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/*
49 * Module parameters.
50 */
51
52static unsigned int hid_mousepoll_interval;
53module_param_named(mousepoll, hid_mousepoll_interval, uint, 0644);
54MODULE_PARM_DESC(mousepoll, "Polling interval of mice");
55
Oliver Neukum0361a282008-12-17 15:38:03 +010056static unsigned int ignoreled;
57module_param_named(ignoreled, ignoreled, uint, 0644);
58MODULE_PARM_DESC(ignoreled, "Autosuspend with active leds");
59
Paul Walmsley876b9272007-04-19 14:56:12 +020060/* Quirks specified at module load time */
Mathias Krause81ba9922014-06-05 21:20:51 +020061static char *quirks_param[MAX_USBHID_BOOT_QUIRKS];
Paul Walmsley876b9272007-04-19 14:56:12 +020062module_param_array_named(quirks, quirks_param, charp, NULL, 0444);
63MODULE_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 Torvalds1da177e2005-04-16 15:20:36 -070067/*
Alan Sternaef4e262006-01-31 12:58:38 -050068 * Input submission and I/O error handler.
69 */
Oliver Neukum0361a282008-12-17 15:38:03 +010070static DEFINE_MUTEX(hid_open_mut);
Alan Sternaef4e262006-01-31 12:58:38 -050071
72static void hid_io_error(struct hid_device *hid);
Oliver Neukum0361a282008-12-17 15:38:03 +010073static int hid_submit_out(struct hid_device *hid);
74static int hid_submit_ctrl(struct hid_device *hid);
75static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid);
Alan Sternaef4e262006-01-31 12:58:38 -050076
77/* Start up the input URB */
78static int hid_start_in(struct hid_device *hid)
79{
80 unsigned long flags;
81 int rc = 0;
Jiri Kosina4916b3a2006-12-08 18:41:03 +010082 struct usbhid_device *usbhid = hid->driver_data;
Alan Sternaef4e262006-01-31 12:58:38 -050083
Oliver Neukum0361a282008-12-17 15:38:03 +010084 spin_lock_irqsave(&usbhid->lock, flags);
85 if (hid->open > 0 &&
Oliver Neukum69626f22008-03-31 16:27:30 +020086 !test_bit(HID_DISCONNECTED, &usbhid->iofl) &&
Alan Sternf2b52642012-07-19 16:08:45 -040087 !test_bit(HID_SUSPENDED, &usbhid->iofl) &&
Jiri Kosina4916b3a2006-12-08 18:41:03 +010088 !test_and_set_bit(HID_IN_RUNNING, &usbhid->iofl)) {
89 rc = usb_submit_urb(usbhid->urbin, GFP_ATOMIC);
Oliver Neukuma8c52b62012-03-28 13:16:19 +020090 if (rc != 0) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +010091 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
Oliver Neukuma8c52b62012-03-28 13:16:19 +020092 if (rc == -ENOSPC)
93 set_bit(HID_NO_BANDWIDTH, &usbhid->iofl);
94 } else {
95 clear_bit(HID_NO_BANDWIDTH, &usbhid->iofl);
96 }
Alan Sternaef4e262006-01-31 12:58:38 -050097 }
Oliver Neukum0361a282008-12-17 15:38:03 +010098 spin_unlock_irqrestore(&usbhid->lock, flags);
Alan Sternaef4e262006-01-31 12:58:38 -050099 return rc;
100}
101
102/* I/O retry timer routine */
103static void hid_retry_timeout(unsigned long _hid)
104{
105 struct hid_device *hid = (struct hid_device *) _hid;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100106 struct usbhid_device *usbhid = hid->driver_data;
Alan Sternaef4e262006-01-31 12:58:38 -0500107
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100108 dev_dbg(&usbhid->intf->dev, "retrying intr urb\n");
Alan Sternaef4e262006-01-31 12:58:38 -0500109 if (hid_start_in(hid))
110 hid_io_error(hid);
111}
112
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400113/* Workqueue routine to reset the device or clear a halt */
David Howellsc4028952006-11-22 14:57:56 +0000114static void hid_reset(struct work_struct *work)
Alan Sternaef4e262006-01-31 12:58:38 -0500115{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100116 struct usbhid_device *usbhid =
117 container_of(work, struct usbhid_device, reset_work);
118 struct hid_device *hid = usbhid->hid;
Alan Stern011b15d2008-11-04 11:29:27 -0500119 int rc = 0;
Alan Sternaef4e262006-01-31 12:58:38 -0500120
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100121 if (test_bit(HID_CLEAR_HALT, &usbhid->iofl)) {
122 dev_dbg(&usbhid->intf->dev, "clear halt\n");
Anssi Hannulabe820972007-01-19 19:28:17 +0200123 rc = usb_clear_halt(hid_to_usb_dev(hid), usbhid->urbin->pipe);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100124 clear_bit(HID_CLEAR_HALT, &usbhid->iofl);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400125 hid_start_in(hid);
Alan Sternaef4e262006-01-31 12:58:38 -0500126 }
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400127
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100128 else if (test_bit(HID_RESET_PENDING, &usbhid->iofl)) {
129 dev_dbg(&usbhid->intf->dev, "resetting device\n");
Alan Stern011b15d2008-11-04 11:29:27 -0500130 rc = usb_lock_device_for_reset(hid_to_usb_dev(hid), usbhid->intf);
131 if (rc == 0) {
Ming Lei742120c2008-06-18 22:00:29 +0800132 rc = usb_reset_device(hid_to_usb_dev(hid));
Alan Stern011b15d2008-11-04 11:29:27 -0500133 usb_unlock_device(hid_to_usb_dev(hid));
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400134 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100135 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400136 }
Alan Sternaef4e262006-01-31 12:58:38 -0500137
Alan Sterndf9a1f42006-06-01 13:55:28 -0400138 switch (rc) {
139 case 0:
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100140 if (!test_bit(HID_IN_RUNNING, &usbhid->iofl))
Alan Sternaef4e262006-01-31 12:58:38 -0500141 hid_io_error(hid);
Alan Sterndf9a1f42006-06-01 13:55:28 -0400142 break;
143 default:
Joe Perches4291ee32010-12-09 19:29:03 -0800144 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 Sterndf9a1f42006-06-01 13:55:28 -0400148 /* FALLTHROUGH */
149 case -EHOSTUNREACH:
150 case -ENODEV:
151 case -EINTR:
152 break;
153 }
Alan Sternaef4e262006-01-31 12:58:38 -0500154}
155
156/* Main I/O error handler */
157static void hid_io_error(struct hid_device *hid)
158{
159 unsigned long flags;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100160 struct usbhid_device *usbhid = hid->driver_data;
Alan Sternaef4e262006-01-31 12:58:38 -0500161
Oliver Neukum0361a282008-12-17 15:38:03 +0100162 spin_lock_irqsave(&usbhid->lock, flags);
Alan Sternaef4e262006-01-31 12:58:38 -0500163
164 /* Stop when disconnected */
Oliver Neukum69626f22008-03-31 16:27:30 +0200165 if (test_bit(HID_DISCONNECTED, &usbhid->iofl))
Alan Sternaef4e262006-01-31 12:58:38 -0500166 goto done;
167
Alan Stern5e2a55f2007-03-20 19:03:31 +0100168 /* 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 Sternaef4e262006-01-31 12:58:38 -0500173 /* When an error occurs, retry at increasing intervals */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100174 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 Sternaef4e262006-01-31 12:58:38 -0500179
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100180 if (time_after(jiffies, usbhid->stop_retry)) {
Alan Sternaef4e262006-01-31 12:58:38 -0500181
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200182 /* 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 Kosina4916b3a2006-12-08 18:41:03 +0100186 schedule_work(&usbhid->reset_work);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400187 goto done;
Alan Sternaef4e262006-01-31 12:58:38 -0500188 }
189 }
190
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100191 mod_timer(&usbhid->io_retry,
192 jiffies + msecs_to_jiffies(usbhid->retry_delay));
Alan Sternaef4e262006-01-31 12:58:38 -0500193done:
Oliver Neukum0361a282008-12-17 15:38:03 +0100194 spin_unlock_irqrestore(&usbhid->lock, flags);
195}
196
197static 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
204static int usbhid_restart_out_queue(struct usbhid_device *usbhid)
205{
206 struct hid_device *hid = usb_get_intfdata(usbhid->intf);
207 int kicked;
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800208 int r;
Oliver Neukum0361a282008-12-17 15:38:03 +0100209
Alan Sternd4150c82012-07-19 16:08:54 -0400210 if (!hid || test_bit(HID_RESET_PENDING, &usbhid->iofl) ||
211 test_bit(HID_SUSPENDED, &usbhid->iofl))
Oliver Neukum0361a282008-12-17 15:38:03 +0100212 return 0;
213
214 if ((kicked = (usbhid->outhead != usbhid->outtail))) {
Greg Kroah-Hartman6cc203d2012-05-01 21:32:55 -0700215 hid_dbg(hid, "Kicking head %d tail %d", usbhid->outhead, usbhid->outtail);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800216
Alan Stern01a7c982012-07-19 16:08:31 -0400217 /* Try to wake up from autosuspend... */
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800218 r = usb_autopm_get_interface_async(usbhid->intf);
219 if (r < 0)
220 return r;
Alan Stern01a7c982012-07-19 16:08:31 -0400221
222 /*
223 * If still suspended, don't submit. Submission will
224 * occur if/when resume drains the queue.
225 */
Alan Sternf2b52642012-07-19 16:08:45 -0400226 if (test_bit(HID_SUSPENDED, &usbhid->iofl)) {
Alan Stern01a7c982012-07-19 16:08:31 -0400227 usb_autopm_put_interface_no_suspend(usbhid->intf);
228 return r;
229 }
230
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800231 /* Asynchronously flush queue. */
232 set_bit(HID_OUT_RUNNING, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +0100233 if (hid_submit_out(hid)) {
234 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800235 usb_autopm_put_interface_async(usbhid->intf);
Oliver Neukum0361a282008-12-17 15:38:03 +0100236 }
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800237 wake_up(&usbhid->wait);
Oliver Neukum0361a282008-12-17 15:38:03 +0100238 }
239 return kicked;
240}
241
242static int usbhid_restart_ctrl_queue(struct usbhid_device *usbhid)
243{
244 struct hid_device *hid = usb_get_intfdata(usbhid->intf);
245 int kicked;
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800246 int r;
Oliver Neukum0361a282008-12-17 15:38:03 +0100247
248 WARN_ON(hid == NULL);
Alan Sternd4150c82012-07-19 16:08:54 -0400249 if (!hid || test_bit(HID_RESET_PENDING, &usbhid->iofl) ||
250 test_bit(HID_SUSPENDED, &usbhid->iofl))
Oliver Neukum0361a282008-12-17 15:38:03 +0100251 return 0;
252
253 if ((kicked = (usbhid->ctrlhead != usbhid->ctrltail))) {
Greg Kroah-Hartman6cc203d2012-05-01 21:32:55 -0700254 hid_dbg(hid, "Kicking head %d tail %d", usbhid->ctrlhead, usbhid->ctrltail);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800255
Alan Stern01a7c982012-07-19 16:08:31 -0400256 /* Try to wake up from autosuspend... */
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800257 r = usb_autopm_get_interface_async(usbhid->intf);
258 if (r < 0)
259 return r;
Alan Stern01a7c982012-07-19 16:08:31 -0400260
261 /*
262 * If still suspended, don't submit. Submission will
263 * occur if/when resume drains the queue.
264 */
Alan Sternf2b52642012-07-19 16:08:45 -0400265 if (test_bit(HID_SUSPENDED, &usbhid->iofl)) {
Alan Stern01a7c982012-07-19 16:08:31 -0400266 usb_autopm_put_interface_no_suspend(usbhid->intf);
267 return r;
268 }
269
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800270 /* Asynchronously flush queue. */
271 set_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +0100272 if (hid_submit_ctrl(hid)) {
273 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800274 usb_autopm_put_interface_async(usbhid->intf);
Oliver Neukum0361a282008-12-17 15:38:03 +0100275 }
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800276 wake_up(&usbhid->wait);
Oliver Neukum0361a282008-12-17 15:38:03 +0100277 }
278 return kicked;
Alan Sternaef4e262006-01-31 12:58:38 -0500279}
280
281/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 * Input interrupt completion handler.
283 */
284
David Howells7d12e782006-10-05 14:55:46 +0100285static void hid_irq_in(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100288 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 int status;
290
291 switch (urb->status) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200292 case 0: /* success */
Oliver Neukum0361a282008-12-17 15:38:03 +0100293 usbhid_mark_busy(usbhid);
Jiri Slaby880d29f2008-06-18 23:55:41 +0200294 usbhid->retry_delay = 0;
295 hid_input_report(urb->context, HID_INPUT_REPORT,
296 urb->transfer_buffer,
297 urb->actual_length, 1);
Oliver Neukum0361a282008-12-17 15:38:03 +0100298 /*
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 Slaby880d29f2008-06-18 23:55:41 +0200307 break;
308 case -EPIPE: /* stall */
Oliver Neukum0361a282008-12-17 15:38:03 +0100309 usbhid_mark_busy(usbhid);
Jiri Slaby880d29f2008-06-18 23:55:41 +0200310 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 Neukum0361a282008-12-17 15:38:03 +0100323 usbhid_mark_busy(usbhid);
Jiri Slaby880d29f2008-06-18 23:55:41 +0200324 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
325 hid_io_error(hid);
326 return;
327 default: /* error */
Joe Perches4291ee32010-12-09 19:29:03 -0800328 hid_warn(urb->dev, "input irq status %d received\n",
329 urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800332 status = usb_submit_urb(urb, GFP_ATOMIC);
Alan Sternaef4e262006-01-31 12:58:38 -0500333 if (status) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100334 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
Alan Sternaef4e262006-01-31 12:58:38 -0500335 if (status != -EPERM) {
Joe Perches4291ee32010-12-09 19:29:03 -0800336 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 Sternaef4e262006-01-31 12:58:38 -0500340 hid_io_error(hid);
341 }
342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345static int hid_submit_out(struct hid_device *hid)
346{
347 struct hid_report *report;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200348 char *raw_report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100349 struct usbhid_device *usbhid = hid->driver_data;
Oliver Neukum68229682010-12-22 15:33:40 +0100350 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200352 report = usbhid->out[usbhid->outtail].report;
353 raw_report = usbhid->out[usbhid->outtail].raw_report;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800355 usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) +
356 1 + (report->id > 0);
357 usbhid->urbout->dev = hid_to_usb_dev(hid);
Alan Stern668160e2012-07-19 16:08:21 -0400358 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 Neukum68229682010-12-22 15:33:40 +0100364
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800365 dbg_hid("submitting out urb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800367 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 Torvalds1da177e2005-04-16 15:20:36 -0700371 }
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800372 usbhid->last_out = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return 0;
374}
375
376static int hid_submit_ctrl(struct hid_device *hid)
377{
378 struct hid_report *report;
379 unsigned char dir;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200380 char *raw_report;
Oliver Neukum68229682010-12-22 15:33:40 +0100381 int len, r;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100382 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100384 report = usbhid->ctrl[usbhid->ctrltail].report;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200385 raw_report = usbhid->ctrl[usbhid->ctrltail].raw_report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100386 dir = usbhid->ctrl[usbhid->ctrltail].dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800388 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 Stern668160e2012-07-19 16:08:21 -0400392 if (raw_report) {
393 memcpy(usbhid->ctrlbuf, raw_report, len);
394 kfree(raw_report);
395 usbhid->ctrl[usbhid->ctrltail].raw_report = NULL;
396 }
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800397 } else {
398 int maxpacket, padlen;
Oliver Neukum0361a282008-12-17 15:38:03 +0100399
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800400 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 Torvalds1da177e2005-04-16 15:20:36 -0700411 }
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800412 usbhid->urbctrl->dev = hid_to_usb_dev(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800414 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 Torvalds1da177e2005-04-16 15:20:36 -0700433 return 0;
434}
435
436/*
437 * Output interrupt completion handler.
438 */
439
David Howells7d12e782006-10-05 14:55:46 +0100440static void hid_irq_out(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100443 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 unsigned long flags;
445 int unplug = 0;
446
447 switch (urb->status) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200448 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 Perches4291ee32010-12-09 19:29:03 -0800458 hid_warn(urb->dev, "output irq status %d received\n",
459 urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461
Oliver Neukum0361a282008-12-17 15:38:03 +0100462 spin_lock_irqsave(&usbhid->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Alan Stern93101af2012-07-19 16:08:39 -0400464 if (unplug) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100465 usbhid->outtail = usbhid->outhead;
Alan Stern93101af2012-07-19 16:08:39 -0400466 } else {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100467 usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Alan Stern93101af2012-07-19 16:08:39 -0400469 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 Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100477 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +0100478 spin_unlock_irqrestore(&usbhid->lock, flags);
Oliver Neukum68229682010-12-22 15:33:40 +0100479 usb_autopm_put_interface_async(usbhid->intf);
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100480 wake_up(&usbhid->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
483/*
484 * Control pipe completion handler.
485 */
486
David Howells7d12e782006-10-05 14:55:46 +0100487static void hid_ctrl(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100490 struct usbhid_device *usbhid = hid->driver_data;
Oliver Neukum0361a282008-12-17 15:38:03 +0100491 int unplug = 0, status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Oliver Neukum0361a282008-12-17 15:38:03 +0100493 spin_lock(&usbhid->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Oliver Neukum0361a282008-12-17 15:38:03 +0100495 switch (status) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200496 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 Perches4291ee32010-12-09 19:29:03 -0800511 hid_warn(urb->dev, "ctrl urb status %d received\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513
Alan Stern93101af2012-07-19 16:08:39 -0400514 if (unplug) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100515 usbhid->ctrltail = usbhid->ctrlhead;
Alan Stern93101af2012-07-19 16:08:39 -0400516 } else {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100517 usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Alan Stern93101af2012-07-19 16:08:39 -0400519 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 Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100527 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +0100528 spin_unlock(&usbhid->lock);
Oliver Neukum68229682010-12-22 15:33:40 +0100529 usb_autopm_put_interface_async(usbhid->intf);
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100530 wake_up(&usbhid->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
H Hartley Sweeten52cfc612009-08-17 15:37:18 -0700533static void __usbhid_submit_report(struct hid_device *hid, struct hid_report *report,
534 unsigned char dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 int head;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100537 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Reyad Attiyat46df9de2014-07-25 00:13:01 -0500539 if (((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN) ||
540 test_bit(HID_DISCONNECTED, &usbhid->iofl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return;
542
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100543 if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100544 if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
Joe Perches4291ee32010-12-09 19:29:03 -0800545 hid_warn(hid, "output queue full\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return;
547 }
548
Jiri Kosina27ce4052013-07-10 19:56:27 +0200549 usbhid->out[usbhid->outhead].raw_report = hid_alloc_report_buf(report, GFP_ATOMIC);
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200550 if (!usbhid->out[usbhid->outhead].raw_report) {
Joe Perches4291ee32010-12-09 19:29:03 -0800551 hid_warn(hid, "output queueing failed\n");
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200552 return;
553 }
554 hid_output_report(report, usbhid->out[usbhid->outhead].raw_report);
555 usbhid->out[usbhid->outhead].report = report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100556 usbhid->outhead = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Alan Stern01a7c982012-07-19 16:08:31 -0400558 /* If the queue isn't running, restart it */
559 if (!test_bit(HID_OUT_RUNNING, &usbhid->iofl)) {
560 usbhid_restart_out_queue(usbhid);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800561
Alan Stern01a7c982012-07-19 16:08:31 -0400562 /* Otherwise see if an earlier request has timed out */
563 } else if (time_after(jiffies, usbhid->last_out + HZ * 5)) {
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800564
Alan Stern01a7c982012-07-19 16:08:31 -0400565 /* Prevent autosuspend following the unlink */
566 usb_autopm_get_interface_no_resume(usbhid->intf);
567
Oliver Neukum858155f2010-02-12 13:02:28 +0100568 /*
Alan Stern01a7c982012-07-19 16:08:31 -0400569 * Prevent resubmission in case the URB completes
570 * before we can unlink it. We don't want to cancel
571 * the wrong transfer!
Oliver Neukum858155f2010-02-12 13:02:28 +0100572 */
Alan Stern01a7c982012-07-19 16:08:31 -0400573 usb_block_urb(usbhid->urbout);
Oliver Neukum8815bb02012-04-30 09:13:46 +0200574
Alan Stern01a7c982012-07-19 16:08:31 -0400575 /* Drop lock to avoid deadlock if the callback runs */
576 spin_unlock(&usbhid->lock);
Oliver Neukum8815bb02012-04-30 09:13:46 +0200577
Alan Stern01a7c982012-07-19 16:08:31 -0400578 usb_unlink_urb(usbhid->urbout);
579 spin_lock(&usbhid->lock);
580 usb_unblock_urb(usbhid->urbout);
581
582 /* Unlink might have stopped the queue */
583 if (!test_bit(HID_OUT_RUNNING, &usbhid->iofl))
584 usbhid_restart_out_queue(usbhid);
585
586 /* Now we can allow autosuspend again */
587 usb_autopm_put_interface_async(usbhid->intf);
Oliver Neukum858155f2010-02-12 13:02:28 +0100588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return;
590 }
591
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100592 if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
Joe Perches4291ee32010-12-09 19:29:03 -0800593 hid_warn(hid, "control queue full\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return;
595 }
596
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200597 if (dir == USB_DIR_OUT) {
Jiri Kosina27ce4052013-07-10 19:56:27 +0200598 usbhid->ctrl[usbhid->ctrlhead].raw_report = hid_alloc_report_buf(report, GFP_ATOMIC);
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200599 if (!usbhid->ctrl[usbhid->ctrlhead].raw_report) {
Joe Perches4291ee32010-12-09 19:29:03 -0800600 hid_warn(hid, "control queueing failed\n");
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200601 return;
602 }
603 hid_output_report(report, usbhid->ctrl[usbhid->ctrlhead].raw_report);
604 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100605 usbhid->ctrl[usbhid->ctrlhead].report = report;
606 usbhid->ctrl[usbhid->ctrlhead].dir = dir;
607 usbhid->ctrlhead = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Alan Stern01a7c982012-07-19 16:08:31 -0400609 /* If the queue isn't running, restart it */
610 if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl)) {
611 usbhid_restart_ctrl_queue(usbhid);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800612
Alan Stern01a7c982012-07-19 16:08:31 -0400613 /* Otherwise see if an earlier request has timed out */
614 } else if (time_after(jiffies, usbhid->last_ctrl + HZ * 5)) {
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800615
Alan Stern01a7c982012-07-19 16:08:31 -0400616 /* Prevent autosuspend following the unlink */
617 usb_autopm_get_interface_no_resume(usbhid->intf);
618
Oliver Neukum858155f2010-02-12 13:02:28 +0100619 /*
Alan Stern01a7c982012-07-19 16:08:31 -0400620 * Prevent resubmission in case the URB completes
621 * before we can unlink it. We don't want to cancel
622 * the wrong transfer!
Oliver Neukum858155f2010-02-12 13:02:28 +0100623 */
Alan Stern01a7c982012-07-19 16:08:31 -0400624 usb_block_urb(usbhid->urbctrl);
625
626 /* Drop lock to avoid deadlock if the callback runs */
627 spin_unlock(&usbhid->lock);
628
629 usb_unlink_urb(usbhid->urbctrl);
630 spin_lock(&usbhid->lock);
631 usb_unblock_urb(usbhid->urbctrl);
632
633 /* Unlink might have stopped the queue */
634 if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
635 usbhid_restart_ctrl_queue(usbhid);
636
637 /* Now we can allow autosuspend again */
638 usb_autopm_put_interface_async(usbhid->intf);
Oliver Neukum858155f2010-02-12 13:02:28 +0100639 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100640}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100642static void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
Oliver Neukum0361a282008-12-17 15:38:03 +0100643{
644 struct usbhid_device *usbhid = hid->driver_data;
645 unsigned long flags;
646
647 spin_lock_irqsave(&usbhid->lock, flags);
648 __usbhid_submit_report(hid, report, dir);
649 spin_unlock_irqrestore(&usbhid->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
Benjamin Tissoiresb7966a42013-02-25 11:31:47 +0100652static int usbhid_wait_io(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100654 struct usbhid_device *usbhid = hid->driver_data;
655
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100656 if (!wait_event_timeout(usbhid->wait,
657 (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
658 !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 10*HZ)) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200660 dbg_hid("timeout waiting for ctrl or out queue to clear\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return -1;
662 }
663
664 return 0;
665}
666
Vojtech Pavlik854561b2005-05-29 02:28:00 -0500667static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
668{
Vojtech Pavlik71387bd72005-05-29 02:28:14 -0500669 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
Vojtech Pavlik854561b2005-05-29 02:28:00 -0500670 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
671 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
672}
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
675 unsigned char type, void *buf, int size)
676{
677 int result, retries = 4;
678
Jiri Kosina43c7bf02007-01-26 12:58:24 +0100679 memset(buf, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 do {
682 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
683 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
684 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
685 retries--;
686 } while (result < size && retries);
687 return result;
688}
689
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100690int usbhid_open(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Oliver Neukum933e3182007-07-11 14:48:58 +0200692 struct usbhid_device *usbhid = hid->driver_data;
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200693 int res = 0;
Oliver Neukum933e3182007-07-11 14:48:58 +0200694
Oliver Neukum0361a282008-12-17 15:38:03 +0100695 mutex_lock(&hid_open_mut);
Oliver Neukum933e3182007-07-11 14:48:58 +0200696 if (!hid->open++) {
697 res = usb_autopm_get_interface(usbhid->intf);
Oliver Neukum68229682010-12-22 15:33:40 +0100698 /* the device must be awake to reliably request remote wakeup */
Oliver Neukum933e3182007-07-11 14:48:58 +0200699 if (res < 0) {
700 hid->open--;
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200701 res = -EIO;
702 goto done;
Oliver Neukum933e3182007-07-11 14:48:58 +0200703 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100704 usbhid->intf->needs_remote_wakeup = 1;
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200705 res = hid_start_in(hid);
706 if (res) {
707 if (res != -ENOSPC) {
708 hid_io_error(hid);
709 res = 0;
710 } else {
711 /* no use opening if resources are insufficient */
712 hid->open--;
713 res = -EBUSY;
714 usbhid->intf->needs_remote_wakeup = 0;
715 }
716 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100717 usb_autopm_put_interface(usbhid->intf);
Oliver Neukum933e3182007-07-11 14:48:58 +0200718 }
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200719done:
Oliver Neukum0361a282008-12-17 15:38:03 +0100720 mutex_unlock(&hid_open_mut);
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200721 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100724void usbhid_close(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100726 struct usbhid_device *usbhid = hid->driver_data;
727
Oliver Neukum0361a282008-12-17 15:38:03 +0100728 mutex_lock(&hid_open_mut);
729
730 /* protecting hid->open to make sure we don't restart
731 * data acquistion due to a resumption we no longer
732 * care about
733 */
734 spin_lock_irq(&usbhid->lock);
Oliver Neukum933e3182007-07-11 14:48:58 +0200735 if (!--hid->open) {
Oliver Neukum0361a282008-12-17 15:38:03 +0100736 spin_unlock_irq(&usbhid->lock);
Oliver Neukum89092dd2009-04-29 17:12:12 +0200737 hid_cancel_delayed_stuff(usbhid);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100738 usb_kill_urb(usbhid->urbin);
Oliver Neukum0361a282008-12-17 15:38:03 +0100739 usbhid->intf->needs_remote_wakeup = 0;
740 } else {
741 spin_unlock_irq(&usbhid->lock);
Oliver Neukum933e3182007-07-11 14:48:58 +0200742 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100743 mutex_unlock(&hid_open_mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
746/*
747 * Initialize all reports
748 */
749
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100750void usbhid_init_reports(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
752 struct hid_report *report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100753 struct usbhid_device *usbhid = hid->driver_data;
Benjamin Tissoires595e9272013-08-22 14:51:09 +0200754 struct hid_report_enum *report_enum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 int err, ret;
756
Benjamin Tissoires595e9272013-08-22 14:51:09 +0200757 if (!(hid->quirks & HID_QUIRK_NO_INIT_INPUT_REPORTS)) {
758 report_enum = &hid->report_enum[HID_INPUT_REPORT];
759 list_for_each_entry(report, &report_enum->report_list, list)
760 usbhid_submit_report(hid, report, USB_DIR_IN);
761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Benjamin Tissoires595e9272013-08-22 14:51:09 +0200763 report_enum = &hid->report_enum[HID_FEATURE_REPORT];
764 list_for_each_entry(report, &report_enum->report_list, list)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100765 usbhid_submit_report(hid, report, USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 err = 0;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100768 ret = usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 while (ret) {
770 err |= ret;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100771 if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
772 usb_kill_urb(usbhid->urbctrl);
773 if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
774 usb_kill_urb(usbhid->urbout);
775 ret = usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777
778 if (err)
Joe Perches4291ee32010-12-09 19:29:03 -0800779 hid_warn(hid, "timeout initializing reports\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500782/*
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200783 * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
784 */
785static int hid_find_field_early(struct hid_device *hid, unsigned int page,
786 unsigned int hid_code, struct hid_field **pfield)
787{
788 struct hid_report *report;
789 struct hid_field *field;
790 struct hid_usage *usage;
791 int i, j;
792
793 list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
794 for (i = 0; i < report->maxfield; i++) {
795 field = report->field[i];
796 for (j = 0; j < field->maxusage; j++) {
797 usage = &field->usage[j];
798 if ((usage->hid & HID_USAGE_PAGE) == page &&
799 (usage->hid & 0xFFFF) == hid_code) {
800 *pfield = field;
801 return j;
802 }
803 }
804 }
805 }
806 return -1;
807}
808
David Herrmannddf64a32013-07-15 19:10:10 +0200809static void usbhid_set_leds(struct hid_device *hid)
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200810{
811 struct hid_field *field;
812 int offset;
813
814 if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
815 hid_set_field(field, offset, 0);
816 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
817 }
818}
819
820/*
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500821 * Traverse the supplied list of reports and find the longest
822 */
Jiri Slaby282bfd42008-03-28 17:06:41 +0100823static void hid_find_max_report(struct hid_device *hid, unsigned int type,
824 unsigned int *max)
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500825{
826 struct hid_report *report;
Jiri Slaby282bfd42008-03-28 17:06:41 +0100827 unsigned int size;
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500828
829 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
Jiri Kosinaefc7ce12008-10-17 15:01:15 +0200830 size = ((report->size - 1) >> 3) + 1 + hid->report_enum[type].numbered;
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500831 if (*max < size)
832 *max = size;
833 }
834}
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
837{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100838 struct usbhid_device *usbhid = hid->driver_data;
839
Daniel Mack997ea582010-04-12 13:17:25 +0200840 usbhid->inbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
Jiri Slaby898089d2008-11-24 16:20:06 +0100841 &usbhid->inbuf_dma);
Daniel Mack997ea582010-04-12 13:17:25 +0200842 usbhid->outbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
Jiri Slaby898089d2008-11-24 16:20:06 +0100843 &usbhid->outbuf_dma);
Alan Stern0ede76f2010-03-05 15:10:17 -0500844 usbhid->cr = kmalloc(sizeof(*usbhid->cr), GFP_KERNEL);
Daniel Mack997ea582010-04-12 13:17:25 +0200845 usbhid->ctrlbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
Jiri Slaby898089d2008-11-24 16:20:06 +0100846 &usbhid->ctrlbuf_dma);
847 if (!usbhid->inbuf || !usbhid->outbuf || !usbhid->cr ||
848 !usbhid->ctrlbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return -1;
850
851 return 0;
852}
853
Alan Ottb4dbde92011-01-18 03:04:39 -0500854static int usbhid_get_raw_report(struct hid_device *hid,
855 unsigned char report_number, __u8 *buf, size_t count,
856 unsigned char report_type)
857{
858 struct usbhid_device *usbhid = hid->driver_data;
859 struct usb_device *dev = hid_to_usb_dev(hid);
860 struct usb_interface *intf = usbhid->intf;
861 struct usb_host_interface *interface = intf->cur_altsetting;
862 int skipped_report_id = 0;
863 int ret;
864
865 /* Byte 0 is the report number. Report data starts at byte 1.*/
866 buf[0] = report_number;
867 if (report_number == 0x0) {
868 /* Offset the return buffer by 1, so that the report ID
869 will remain in byte 0. */
870 buf++;
871 count--;
872 skipped_report_id = 1;
873 }
874 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
875 HID_REQ_GET_REPORT,
876 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
877 ((report_type + 1) << 8) | report_number,
878 interface->desc.bInterfaceNumber, buf, count,
879 USB_CTRL_SET_TIMEOUT);
880
881 /* count also the report id */
882 if (ret > 0 && skipped_report_id)
883 ret++;
884
885 return ret;
886}
887
Frank Praznik975a6832014-01-22 13:49:42 -0500888static int usbhid_set_raw_report(struct hid_device *hid, unsigned int reportnum,
889 __u8 *buf, size_t count, unsigned char rtype)
890{
891 struct usbhid_device *usbhid = hid->driver_data;
892 struct usb_device *dev = hid_to_usb_dev(hid);
893 struct usb_interface *intf = usbhid->intf;
894 struct usb_host_interface *interface = intf->cur_altsetting;
895 int ret, skipped_report_id = 0;
896
897 /* Byte 0 is the report number. Report data starts at byte 1.*/
Benjamin Tissoirese534a932014-03-08 22:52:42 -0500898 if ((rtype == HID_OUTPUT_REPORT) &&
899 (hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORT_ID))
900 buf[0] = 0;
901 else
902 buf[0] = reportnum;
903
Frank Praznik975a6832014-01-22 13:49:42 -0500904 if (buf[0] == 0x0) {
905 /* Don't send the Report ID */
906 buf++;
907 count--;
908 skipped_report_id = 1;
909 }
910
911 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
912 HID_REQ_SET_REPORT,
913 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
914 ((rtype + 1) << 8) | reportnum,
915 interface->desc.bInterfaceNumber, buf, count,
916 USB_CTRL_SET_TIMEOUT);
917 /* count also the report id, if this was a numbered report. */
918 if (ret > 0 && skipped_report_id)
919 ret++;
920
921 return ret;
922}
923
Frank Praznik975a6832014-01-22 13:49:42 -0500924static int usbhid_output_report(struct hid_device *hid, __u8 *buf, size_t count)
925{
926 struct usbhid_device *usbhid = hid->driver_data;
927 struct usb_device *dev = hid_to_usb_dev(hid);
928 int actual_length, skipped_report_id = 0, ret;
929
930 if (!usbhid->urbout)
Benjamin Tissoiresddea1af2014-02-10 12:58:51 -0500931 return -ENOSYS;
Frank Praznik975a6832014-01-22 13:49:42 -0500932
933 if (buf[0] == 0x0) {
934 /* Don't send the Report ID */
935 buf++;
936 count--;
937 skipped_report_id = 1;
938 }
939
940 ret = usb_interrupt_msg(dev, usbhid->urbout->pipe,
941 buf, count, &actual_length,
942 USB_CTRL_SET_TIMEOUT);
943 /* return the number of bytes transferred */
944 if (ret == 0) {
945 ret = actual_length;
946 /* count also the report id */
947 if (skipped_report_id)
948 ret++;
949 }
950
951 return ret;
952}
953
Oliver Neukum0361a282008-12-17 15:38:03 +0100954static void usbhid_restart_queues(struct usbhid_device *usbhid)
955{
Alan Sterneb055fd2012-07-19 16:09:01 -0400956 if (usbhid->urbout && !test_bit(HID_OUT_RUNNING, &usbhid->iofl))
Oliver Neukum0361a282008-12-17 15:38:03 +0100957 usbhid_restart_out_queue(usbhid);
Alan Sterneb055fd2012-07-19 16:09:01 -0400958 if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
959 usbhid_restart_ctrl_queue(usbhid);
Oliver Neukum0361a282008-12-17 15:38:03 +0100960}
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
963{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100964 struct usbhid_device *usbhid = hid->driver_data;
965
Daniel Mack997ea582010-04-12 13:17:25 +0200966 usb_free_coherent(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
967 usb_free_coherent(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
Alan Stern0ede76f2010-03-05 15:10:17 -0500968 kfree(usbhid->cr);
Daniel Mack997ea582010-04-12 13:17:25 +0200969 usb_free_coherent(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
Jiri Slabyc500c972008-05-16 11:49:16 +0200972static int usbhid_parse(struct hid_device *hid)
973{
974 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 struct usb_host_interface *interface = intf->cur_altsetting;
976 struct usb_device *dev = interface_to_usbdev (intf);
977 struct hid_descriptor *hdesc;
Paul Walmsley2eb5dc32007-04-19 13:27:04 +0200978 u32 quirks = 0;
Jiri Slabyc500c972008-05-16 11:49:16 +0200979 unsigned int rsize = 0;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500980 char *rdesc;
Jiri Slabyc500c972008-05-16 11:49:16 +0200981 int ret, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Paul Walmsley2eb5dc32007-04-19 13:27:04 +0200983 quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
984 le16_to_cpu(dev->descriptor.idProduct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Jiri Kosina6f4303f2009-01-29 00:15:51 +0100986 if (quirks & HID_QUIRK_IGNORE)
987 return -ENODEV;
988
Alan Stern0f28b552006-05-15 14:49:04 -0400989 /* Many keyboards and mice don't like to be polled for reports,
990 * so we will always set the HID_QUIRK_NOGET flag for them. */
991 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
992 if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
993 interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
994 quirks |= HID_QUIRK_NOGET;
995 }
996
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500997 if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
998 (!interface->desc.bNumEndpoints ||
999 usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
Jiri Kosina58037eb2007-05-30 15:07:13 +02001000 dbg_hid("class descriptor not present\n");
Jiri Slabyc500c972008-05-16 11:49:16 +02001001 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
1003
Jiri Slabyc500c972008-05-16 11:49:16 +02001004 hid->version = le16_to_cpu(hdesc->bcdHID);
1005 hid->country = hdesc->bCountryCode;
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 for (n = 0; n < hdesc->bNumDescriptors; n++)
1008 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
1009 rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
1010
1011 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
Jiri Kosina58037eb2007-05-30 15:07:13 +02001012 dbg_hid("weird size of report descriptor (%u)\n", rsize);
Jiri Slabyc500c972008-05-16 11:49:16 +02001013 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
1015
1016 if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
Jiri Kosina58037eb2007-05-30 15:07:13 +02001017 dbg_hid("couldn't allocate rdesc memory\n");
Jiri Slabyc500c972008-05-16 11:49:16 +02001018 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
1020
Vojtech Pavlik854561b2005-05-29 02:28:00 -05001021 hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
1022
Jiri Slabyc500c972008-05-16 11:49:16 +02001023 ret = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber,
1024 HID_DT_REPORT, rdesc, rsize);
1025 if (ret < 0) {
Jiri Kosina58037eb2007-05-30 15:07:13 +02001026 dbg_hid("reading report descriptor failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 kfree(rdesc);
Jiri Slabyc500c972008-05-16 11:49:16 +02001028 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 }
1030
Jiri Slabyc500c972008-05-16 11:49:16 +02001031 ret = hid_parse_report(hid, rdesc, rsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 kfree(rdesc);
Jiri Slabyc500c972008-05-16 11:49:16 +02001033 if (ret) {
1034 dbg_hid("parsing report descriptor failed\n");
1035 goto err;
1036 }
1037
Zoltan Karcagif5208992009-05-06 16:30:21 +02001038 hid->quirks |= quirks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Jiri Slabyc500c972008-05-16 11:49:16 +02001040 return 0;
1041err:
1042 return ret;
1043}
1044
1045static int usbhid_start(struct hid_device *hid)
1046{
1047 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
1048 struct usb_host_interface *interface = intf->cur_altsetting;
1049 struct usb_device *dev = interface_to_usbdev(intf);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001050 struct usbhid_device *usbhid = hid->driver_data;
Jiri Slabyc500c972008-05-16 11:49:16 +02001051 unsigned int n, insize = 0;
1052 int ret;
1053
Jiri Slabye3e14de2008-11-01 23:41:46 +01001054 clear_bit(HID_DISCONNECTED, &usbhid->iofl);
1055
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001056 usbhid->bufsize = HID_MIN_BUFFER_SIZE;
1057 hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
1058 hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
1059 hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
1060
1061 if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
1062 usbhid->bufsize = HID_MAX_BUFFER_SIZE;
Michael Haboustakbf0964d2005-09-05 00:12:01 -05001063
1064 hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
1065
1066 if (insize > HID_MAX_BUFFER_SIZE)
1067 insize = HID_MAX_BUFFER_SIZE;
1068
Jiri Slabyc500c972008-05-16 11:49:16 +02001069 if (hid_alloc_buffers(dev, hid)) {
1070 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 goto fail;
Pekka Sarnilaf345c372008-03-06 13:23:14 +01001072 }
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 for (n = 0; n < interface->desc.bNumEndpoints; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 struct usb_endpoint_descriptor *endpoint;
1076 int pipe;
1077 int interval;
1078
1079 endpoint = &interface->endpoint[n].desc;
Jiri Slaby581a2732008-11-24 16:20:08 +01001080 if (!usb_endpoint_xfer_int(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 continue;
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 interval = endpoint->bInterval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Pekka Sarnilaf345c372008-03-06 13:23:14 +01001085 /* Some vendors give fullspeed interval on highspeed devides */
Jiri Slabyc500c972008-05-16 11:49:16 +02001086 if (hid->quirks & HID_QUIRK_FULLSPEED_INTERVAL &&
Pekka Sarnilaf345c372008-03-06 13:23:14 +01001087 dev->speed == USB_SPEED_HIGH) {
1088 interval = fls(endpoint->bInterval*8);
1089 printk(KERN_INFO "%s: Fixing fullspeed to highspeed interval: %d -> %d\n",
1090 hid->name, endpoint->bInterval, interval);
1091 }
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 /* Change the polling interval of mice. */
1094 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
1095 interval = hid_mousepoll_interval;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -05001096
Jiri Slabyc500c972008-05-16 11:49:16 +02001097 ret = -ENOMEM;
Luiz Fernando N. Capitulino0f12aa02006-10-26 13:02:51 -03001098 if (usb_endpoint_dir_in(endpoint)) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001099 if (usbhid->urbin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 continue;
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001101 if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 goto fail;
1103 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001104 usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 hid_irq_in, hid, interval);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001106 usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
1107 usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 } else {
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001109 if (usbhid->urbout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 continue;
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001111 if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 goto fail;
1113 pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001114 usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 hid_irq_out, hid, interval);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001116 usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
1117 usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
1119 }
1120
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001121 usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
Jiri Slabyc500c972008-05-16 11:49:16 +02001122 if (!usbhid->urbctrl) {
1123 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 goto fail;
Jiri Slabyc500c972008-05-16 11:49:16 +02001125 }
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001126
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001127 usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
1128 usbhid->ctrlbuf, 1, hid_ctrl, hid);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001129 usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
Alan Stern0ede76f2010-03-05 15:10:17 -05001130 usbhid->urbctrl->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Jiri Slabyc500c972008-05-16 11:49:16 +02001131
Jiri Kosina5b915d92009-11-05 14:08:03 +01001132 if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
1133 usbhid_init_reports(hid);
Jiri Slaby93c10132008-06-27 00:04:24 +02001134
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001135 set_bit(HID_STARTED, &usbhid->iofl);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001136
Alan Stern08ef08e2008-10-30 23:58:51 +01001137 /* Some keyboards don't work until their LEDs have been set.
1138 * Since BIOSes do set the LEDs, it must be safe for any device
1139 * that supports the keyboard boot protocol.
Alan Stern3d615102010-04-02 13:21:58 -04001140 * In addition, enable remote wakeup by default for all keyboard
1141 * devices supporting the boot protocol.
Alan Stern08ef08e2008-10-30 23:58:51 +01001142 */
1143 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT &&
1144 interface->desc.bInterfaceProtocol ==
Alan Stern3d615102010-04-02 13:21:58 -04001145 USB_INTERFACE_PROTOCOL_KEYBOARD) {
Alan Stern08ef08e2008-10-30 23:58:51 +01001146 usbhid_set_leds(hid);
Alan Stern3d615102010-04-02 13:21:58 -04001147 device_set_wakeup_enable(&dev->dev, 1);
1148 }
Jiri Slabyc500c972008-05-16 11:49:16 +02001149 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151fail:
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001152 usb_free_urb(usbhid->urbin);
1153 usb_free_urb(usbhid->urbout);
1154 usb_free_urb(usbhid->urbctrl);
Jiri Slabye3e14de2008-11-01 23:41:46 +01001155 usbhid->urbin = NULL;
1156 usbhid->urbout = NULL;
1157 usbhid->urbctrl = NULL;
Jiri Kosina22f675f2007-08-01 12:32:27 +02001158 hid_free_buffers(dev, hid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001159 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
Jiri Slabyc500c972008-05-16 11:49:16 +02001162static void usbhid_stop(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Jiri Slabyc500c972008-05-16 11:49:16 +02001164 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Jiri Slabyc500c972008-05-16 11:49:16 +02001166 if (WARN_ON(!usbhid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 return;
1168
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001169 clear_bit(HID_STARTED, &usbhid->iofl);
Daniel Kurtz4371ea82011-11-17 19:23:50 +08001170 spin_lock_irq(&usbhid->lock); /* Sync with error and led handlers */
Oliver Neukum69626f22008-03-31 16:27:30 +02001171 set_bit(HID_DISCONNECTED, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +01001172 spin_unlock_irq(&usbhid->lock);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001173 usb_kill_urb(usbhid->urbin);
1174 usb_kill_urb(usbhid->urbout);
1175 usb_kill_urb(usbhid->urbctrl);
1176
Oliver Neukum0361a282008-12-17 15:38:03 +01001177 hid_cancel_delayed_stuff(usbhid);
Alan Sternaef4e262006-01-31 12:58:38 -05001178
Jiri Slabyc500c972008-05-16 11:49:16 +02001179 hid->claimed = 0;
1180
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001181 usb_free_urb(usbhid->urbin);
1182 usb_free_urb(usbhid->urbctrl);
1183 usb_free_urb(usbhid->urbout);
Jiri Slabye3e14de2008-11-01 23:41:46 +01001184 usbhid->urbin = NULL; /* don't mess up next start */
1185 usbhid->urbctrl = NULL;
1186 usbhid->urbout = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Anssi Hannulabe820972007-01-19 19:28:17 +02001188 hid_free_buffers(hid_to_usb_dev(hid), hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
1190
Oliver Neukum0361a282008-12-17 15:38:03 +01001191static int usbhid_power(struct hid_device *hid, int lvl)
1192{
1193 int r = 0;
1194
1195 switch (lvl) {
1196 case PM_HINT_FULLON:
1197 r = usbhid_get_power(hid);
1198 break;
1199 case PM_HINT_NORMAL:
1200 usbhid_put_power(hid);
1201 break;
1202 }
1203 return r;
1204}
1205
Henrik Rydberge90a6df2013-02-25 11:31:43 +01001206static void usbhid_request(struct hid_device *hid, struct hid_report *rep, int reqtype)
1207{
1208 switch (reqtype) {
1209 case HID_REQ_GET_REPORT:
1210 usbhid_submit_report(hid, rep, USB_DIR_IN);
1211 break;
1212 case HID_REQ_SET_REPORT:
1213 usbhid_submit_report(hid, rep, USB_DIR_OUT);
1214 break;
1215 }
1216}
1217
Frank Praznik975a6832014-01-22 13:49:42 -05001218static int usbhid_raw_request(struct hid_device *hid, unsigned char reportnum,
1219 __u8 *buf, size_t len, unsigned char rtype,
1220 int reqtype)
1221{
1222 switch (reqtype) {
1223 case HID_REQ_GET_REPORT:
1224 return usbhid_get_raw_report(hid, reportnum, buf, len, rtype);
1225 case HID_REQ_SET_REPORT:
1226 return usbhid_set_raw_report(hid, reportnum, buf, len, rtype);
1227 default:
1228 return -EIO;
1229 }
1230}
1231
Benjamin Tissoires96848192013-02-27 16:38:17 +01001232static int usbhid_idle(struct hid_device *hid, int report, int idle,
1233 int reqtype)
1234{
1235 struct usb_device *dev = hid_to_usb_dev(hid);
1236 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
1237 struct usb_host_interface *interface = intf->cur_altsetting;
1238 int ifnum = interface->desc.bInterfaceNumber;
1239
1240 if (reqtype != HID_REQ_SET_IDLE)
1241 return -EINVAL;
1242
1243 return hid_set_idle(dev, ifnum, report, idle);
1244}
1245
Jiri Slabyc500c972008-05-16 11:49:16 +02001246static struct hid_ll_driver usb_hid_driver = {
1247 .parse = usbhid_parse,
1248 .start = usbhid_start,
1249 .stop = usbhid_stop,
1250 .open = usbhid_open,
1251 .close = usbhid_close,
Oliver Neukum0361a282008-12-17 15:38:03 +01001252 .power = usbhid_power,
Henrik Rydberge90a6df2013-02-25 11:31:43 +01001253 .request = usbhid_request,
Henrik Rydberg33734432013-02-25 11:31:44 +01001254 .wait = usbhid_wait_io,
Frank Praznik975a6832014-01-22 13:49:42 -05001255 .raw_request = usbhid_raw_request,
1256 .output_report = usbhid_output_report,
Benjamin Tissoires96848192013-02-27 16:38:17 +01001257 .idle = usbhid_idle,
Jiri Slabyc500c972008-05-16 11:49:16 +02001258};
1259
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001260static int usbhid_probe(struct usb_interface *intf, const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
Jiri Slaby131d3a7a2008-11-14 12:03:47 +01001262 struct usb_host_interface *interface = intf->cur_altsetting;
Jiri Slabyc500c972008-05-16 11:49:16 +02001263 struct usb_device *dev = interface_to_usbdev(intf);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001264 struct usbhid_device *usbhid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 struct hid_device *hid;
Jiri Slaby131d3a7a2008-11-14 12:03:47 +01001266 unsigned int n, has_in = 0;
Jiri Slabyc500c972008-05-16 11:49:16 +02001267 size_t len;
1268 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Jiri Kosina58037eb2007-05-30 15:07:13 +02001270 dbg_hid("HID probe called for ifnum %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 intf->altsetting->desc.bInterfaceNumber);
1272
Jiri Slaby131d3a7a2008-11-14 12:03:47 +01001273 for (n = 0; n < interface->desc.bNumEndpoints; n++)
1274 if (usb_endpoint_is_int_in(&interface->endpoint[n].desc))
1275 has_in++;
1276 if (!has_in) {
Joe Perches4291ee32010-12-09 19:29:03 -08001277 hid_err(intf, "couldn't find an input interrupt endpoint\n");
Jiri Slaby131d3a7a2008-11-14 12:03:47 +01001278 return -ENODEV;
1279 }
1280
Jiri Slabyc500c972008-05-16 11:49:16 +02001281 hid = hid_allocate_device();
1282 if (IS_ERR(hid))
1283 return PTR_ERR(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 usb_set_intfdata(intf, hid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001286 hid->ll_driver = &usb_hid_driver;
Jiri Slaby76483cf2008-09-18 12:23:33 +02001287 hid->ff_init = hid_pidff_init;
Jiri Slabyc500c972008-05-16 11:49:16 +02001288#ifdef CONFIG_USB_HIDDEV
Jiri Slaby93c10132008-06-27 00:04:24 +02001289 hid->hiddev_connect = hiddev_connect;
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001290 hid->hiddev_disconnect = hiddev_disconnect;
Jiri Slabyc500c972008-05-16 11:49:16 +02001291 hid->hiddev_hid_event = hiddev_hid_event;
1292 hid->hiddev_report_event = hiddev_report_event;
1293#endif
1294 hid->dev.parent = &intf->dev;
1295 hid->bus = BUS_USB;
1296 hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
1297 hid->product = le16_to_cpu(dev->descriptor.idProduct);
1298 hid->name[0] = 0;
Bastien Nocerab5e5a372010-04-16 17:19:50 +01001299 hid->quirks = usbhid_lookup_quirk(hid->vendor, hid->product);
Jiri Slabya73a6372008-10-22 14:45:11 +02001300 if (intf->cur_altsetting->desc.bInterfaceProtocol ==
1301 USB_INTERFACE_PROTOCOL_MOUSE)
1302 hid->type = HID_TYPE_USBMOUSE;
Tomoki Sekiyama6dc14182011-05-23 15:45:44 -07001303 else if (intf->cur_altsetting->desc.bInterfaceProtocol == 0)
1304 hid->type = HID_TYPE_USBNONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Jiri Slabyc500c972008-05-16 11:49:16 +02001306 if (dev->manufacturer)
1307 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
1308
1309 if (dev->product) {
1310 if (dev->manufacturer)
1311 strlcat(hid->name, " ", sizeof(hid->name));
1312 strlcat(hid->name, dev->product, sizeof(hid->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 }
1314
Jiri Slabyc500c972008-05-16 11:49:16 +02001315 if (!strlen(hid->name))
1316 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
1317 le16_to_cpu(dev->descriptor.idVendor),
1318 le16_to_cpu(dev->descriptor.idProduct));
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001319
Jiri Slabyc500c972008-05-16 11:49:16 +02001320 usb_make_path(dev, hid->phys, sizeof(hid->phys));
1321 strlcat(hid->phys, "/input", sizeof(hid->phys));
1322 len = strlen(hid->phys);
1323 if (len < sizeof(hid->phys) - 1)
1324 snprintf(hid->phys + len, sizeof(hid->phys) - len,
1325 "%d", intf->altsetting[0].desc.bInterfaceNumber);
Geoff Levand4a1a4d82007-01-15 20:11:52 -08001326
Jiri Slabyc500c972008-05-16 11:49:16 +02001327 if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
1328 hid->uniq[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001330 usbhid = kzalloc(sizeof(*usbhid), GFP_KERNEL);
1331 if (usbhid == NULL) {
1332 ret = -ENOMEM;
1333 goto err;
1334 }
1335
1336 hid->driver_data = usbhid;
1337 usbhid->hid = hid;
Jiri Kosina57ab12e2010-02-17 14:25:01 +01001338 usbhid->intf = intf;
1339 usbhid->ifnum = interface->desc.bInterfaceNumber;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001340
Alan Sternfde4e2f2010-05-07 10:41:10 -04001341 init_waitqueue_head(&usbhid->wait);
1342 INIT_WORK(&usbhid->reset_work, hid_reset);
Alan Sternfde4e2f2010-05-07 10:41:10 -04001343 setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
1344 spin_lock_init(&usbhid->lock);
1345
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001346 ret = hid_add_device(hid);
1347 if (ret) {
Jiri Slabyd458a9d2008-05-16 11:49:20 +02001348 if (ret != -ENODEV)
Joe Perches4291ee32010-12-09 19:29:03 -08001349 hid_err(intf, "can't add hid device: %d\n", ret);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001350 goto err_free;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001351 }
Jiri Slabyc500c972008-05-16 11:49:16 +02001352
1353 return 0;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001354err_free:
1355 kfree(usbhid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001356err:
1357 hid_destroy_device(hid);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001358 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359}
1360
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001361static void usbhid_disconnect(struct usb_interface *intf)
Jiri Slabyc500c972008-05-16 11:49:16 +02001362{
1363 struct hid_device *hid = usb_get_intfdata(intf);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001364 struct usbhid_device *usbhid;
Jiri Slabyc500c972008-05-16 11:49:16 +02001365
1366 if (WARN_ON(!hid))
1367 return;
1368
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001369 usbhid = hid->driver_data;
Reyad Attiyat46df9de2014-07-25 00:13:01 -05001370 spin_lock_irq(&usbhid->lock); /* Sync with error and led handlers */
1371 set_bit(HID_DISCONNECTED, &usbhid->iofl);
1372 spin_unlock_irq(&usbhid->lock);
Jiri Slabyc500c972008-05-16 11:49:16 +02001373 hid_destroy_device(hid);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001374 kfree(usbhid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001375}
1376
Oliver Neukum0361a282008-12-17 15:38:03 +01001377static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
Oliver Neukum0361a282008-12-17 15:38:03 +01001379 del_timer_sync(&usbhid->io_retry);
Oliver Neukum0361a282008-12-17 15:38:03 +01001380 cancel_work_sync(&usbhid->reset_work);
1381}
1382
1383static void hid_cease_io(struct usbhid_device *usbhid)
1384{
Oliver Neukumfad9fbe2011-10-13 18:21:58 +02001385 del_timer_sync(&usbhid->io_retry);
Oliver Neukum0361a282008-12-17 15:38:03 +01001386 usb_kill_urb(usbhid->urbin);
1387 usb_kill_urb(usbhid->urbctrl);
1388 usb_kill_urb(usbhid->urbout);
Oliver Neukum0361a282008-12-17 15:38:03 +01001389}
1390
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001391/* Treat USB reset pretty much the same as suspend/resume */
1392static int hid_pre_reset(struct usb_interface *intf)
1393{
1394 struct hid_device *hid = usb_get_intfdata(intf);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001395 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001397 spin_lock_irq(&usbhid->lock);
1398 set_bit(HID_RESET_PENDING, &usbhid->iofl);
1399 spin_unlock_irq(&usbhid->lock);
1400 hid_cease_io(usbhid);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001401
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001402 return 0;
1403}
1404
1405/* Same routine used for post_reset and reset_resume */
1406static int hid_post_reset(struct usb_interface *intf)
1407{
1408 struct usb_device *dev = interface_to_usbdev (intf);
1409 struct hid_device *hid = usb_get_intfdata(intf);
1410 struct usbhid_device *usbhid = hid->driver_data;
Simon Haggettdc3c78e2012-04-03 16:04:15 +01001411 struct usb_host_interface *interface = intf->cur_altsetting;
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001412 int status;
Simon Haggettdc3c78e2012-04-03 16:04:15 +01001413 char *rdesc;
1414
1415 /* Fetch and examine the HID report descriptor. If this
1416 * has changed, then rebind. Since usbcore's check of the
1417 * configuration descriptors passed, we already know that
1418 * the size of the HID report descriptor has not changed.
1419 */
Kevin Daughtridge86e6b772012-09-20 12:00:32 -07001420 rdesc = kmalloc(hid->dev_rsize, GFP_KERNEL);
Simon Haggettdc3c78e2012-04-03 16:04:15 +01001421 if (!rdesc) {
1422 dbg_hid("couldn't allocate rdesc memory (post_reset)\n");
1423 return 1;
1424 }
1425 status = hid_get_class_descriptor(dev,
1426 interface->desc.bInterfaceNumber,
Kevin Daughtridge86e6b772012-09-20 12:00:32 -07001427 HID_DT_REPORT, rdesc, hid->dev_rsize);
Simon Haggettdc3c78e2012-04-03 16:04:15 +01001428 if (status < 0) {
1429 dbg_hid("reading report descriptor failed (post_reset)\n");
1430 kfree(rdesc);
1431 return 1;
1432 }
Kevin Daughtridge86e6b772012-09-20 12:00:32 -07001433 status = memcmp(rdesc, hid->dev_rdesc, hid->dev_rsize);
Simon Haggettdc3c78e2012-04-03 16:04:15 +01001434 kfree(rdesc);
1435 if (status != 0) {
1436 dbg_hid("report descriptor changed\n");
1437 return 1;
1438 }
Jiri Kosina70fa9f22009-06-04 15:48:38 +02001439
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001440 spin_lock_irq(&usbhid->lock);
1441 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
1442 spin_unlock_irq(&usbhid->lock);
1443 hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001444 status = hid_start_in(hid);
1445 if (status < 0)
1446 hid_io_error(hid);
1447 usbhid_restart_queues(usbhid);
1448
1449 return 0;
1450}
1451
1452int usbhid_get_power(struct hid_device *hid)
1453{
1454 struct usbhid_device *usbhid = hid->driver_data;
Jiri Kosina70fa9f22009-06-04 15:48:38 +02001455
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001456 return usb_autopm_get_interface(usbhid->intf);
1457}
1458
1459void usbhid_put_power(struct hid_device *hid)
1460{
1461 struct usbhid_device *usbhid = hid->driver_data;
Jiri Kosina70fa9f22009-06-04 15:48:38 +02001462
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001463 usb_autopm_put_interface(usbhid->intf);
1464}
1465
1466
Jiri Kosina0f6f1402009-01-19 09:17:18 +01001467#ifdef CONFIG_PM
Alan Sterneb055fd2012-07-19 16:09:01 -04001468static int hid_resume_common(struct hid_device *hid, bool driver_suspended)
1469{
1470 struct usbhid_device *usbhid = hid->driver_data;
1471 int status;
1472
1473 spin_lock_irq(&usbhid->lock);
1474 clear_bit(HID_SUSPENDED, &usbhid->iofl);
1475 usbhid_mark_busy(usbhid);
1476
1477 if (test_bit(HID_CLEAR_HALT, &usbhid->iofl) ||
1478 test_bit(HID_RESET_PENDING, &usbhid->iofl))
1479 schedule_work(&usbhid->reset_work);
1480 usbhid->retry_delay = 0;
1481
1482 usbhid_restart_queues(usbhid);
1483 spin_unlock_irq(&usbhid->lock);
1484
1485 status = hid_start_in(hid);
1486 if (status < 0)
1487 hid_io_error(hid);
1488
1489 if (driver_suspended && hid->driver && hid->driver->resume)
1490 status = hid->driver->resume(hid);
1491 return status;
1492}
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494static int hid_suspend(struct usb_interface *intf, pm_message_t message)
1495{
Oliver Neukum0361a282008-12-17 15:38:03 +01001496 struct hid_device *hid = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 struct usbhid_device *usbhid = hid->driver_data;
Ming Lei37093b72013-03-15 12:08:55 +08001498 int status = 0;
Alan Sterneb055fd2012-07-19 16:09:01 -04001499 bool driver_suspended = false;
David Herrmannbfde79c2013-07-15 19:10:13 +02001500 unsigned int ledcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Alan Stern5b1b0b82011-08-19 23:49:48 +02001502 if (PMSG_IS_AUTO(message)) {
David Herrmannbfde79c2013-07-15 19:10:13 +02001503 ledcount = hidinput_count_leds(hid);
Oliver Neukum0361a282008-12-17 15:38:03 +01001504 spin_lock_irq(&usbhid->lock); /* Sync with error handler */
1505 if (!test_bit(HID_RESET_PENDING, &usbhid->iofl)
1506 && !test_bit(HID_CLEAR_HALT, &usbhid->iofl)
1507 && !test_bit(HID_OUT_RUNNING, &usbhid->iofl)
1508 && !test_bit(HID_CTRL_RUNNING, &usbhid->iofl)
1509 && !test_bit(HID_KEYS_PRESSED, &usbhid->iofl)
David Herrmannbfde79c2013-07-15 19:10:13 +02001510 && (!ledcount || ignoreled))
Oliver Neukum0361a282008-12-17 15:38:03 +01001511 {
Alan Sternf2b52642012-07-19 16:08:45 -04001512 set_bit(HID_SUSPENDED, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +01001513 spin_unlock_irq(&usbhid->lock);
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001514 if (hid->driver && hid->driver->suspend) {
1515 status = hid->driver->suspend(hid, message);
1516 if (status < 0)
Alan Sterneb055fd2012-07-19 16:09:01 -04001517 goto failed;
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001518 }
Alan Sterneb055fd2012-07-19 16:09:01 -04001519 driver_suspended = true;
Oliver Neukum0361a282008-12-17 15:38:03 +01001520 } else {
1521 usbhid_mark_busy(usbhid);
1522 spin_unlock_irq(&usbhid->lock);
1523 return -EBUSY;
1524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Oliver Neukum0361a282008-12-17 15:38:03 +01001526 } else {
Ming Lei37093b72013-03-15 12:08:55 +08001527 /* TODO: resume() might need to handle suspend failure */
1528 if (hid->driver && hid->driver->suspend)
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001529 status = hid->driver->suspend(hid, message);
Alan Sterneb055fd2012-07-19 16:09:01 -04001530 driver_suspended = true;
Oliver Neukum0361a282008-12-17 15:38:03 +01001531 spin_lock_irq(&usbhid->lock);
Alan Sternf2b52642012-07-19 16:08:45 -04001532 set_bit(HID_SUSPENDED, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +01001533 spin_unlock_irq(&usbhid->lock);
Ming Lei37093b72013-03-15 12:08:55 +08001534 if (usbhid_wait_io(hid) < 0)
Alan Sterneb055fd2012-07-19 16:09:01 -04001535 status = -EIO;
Oliver Neukum0361a282008-12-17 15:38:03 +01001536 }
1537
Oliver Neukum0361a282008-12-17 15:38:03 +01001538 hid_cancel_delayed_stuff(usbhid);
1539 hid_cease_io(usbhid);
1540
Alan Stern5b1b0b82011-08-19 23:49:48 +02001541 if (PMSG_IS_AUTO(message) && test_bit(HID_KEYS_PRESSED, &usbhid->iofl)) {
Oliver Neukum0361a282008-12-17 15:38:03 +01001542 /* lost race against keypresses */
Alan Sterneb055fd2012-07-19 16:09:01 -04001543 status = -EBUSY;
1544 goto failed;
Oliver Neukum0361a282008-12-17 15:38:03 +01001545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 dev_dbg(&intf->dev, "suspend\n");
Ming Lei37093b72013-03-15 12:08:55 +08001547 return status;
Alan Sterneb055fd2012-07-19 16:09:01 -04001548
1549 failed:
1550 hid_resume_common(hid, driver_suspended);
1551 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552}
1553
1554static int hid_resume(struct usb_interface *intf)
1555{
1556 struct hid_device *hid = usb_get_intfdata (intf);
1557 struct usbhid_device *usbhid = hid->driver_data;
1558 int status;
1559
Jiri Slabyfde5be32008-11-23 12:03:20 +01001560 if (!test_bit(HID_STARTED, &usbhid->iofl))
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001561 return 0;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001562
Alan Sterneb055fd2012-07-19 16:09:01 -04001563 status = hid_resume_common(hid, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 dev_dbg(&intf->dev, "resume status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 return 0;
1566}
1567
Oliver Neukum378a0ed2009-02-18 11:46:45 +01001568static int hid_reset_resume(struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
Oliver Neukum378a0ed2009-02-18 11:46:45 +01001570 struct hid_device *hid = usb_get_intfdata(intf);
1571 struct usbhid_device *usbhid = hid->driver_data;
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001572 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Alan Sternf2b52642012-07-19 16:08:45 -04001574 clear_bit(HID_SUSPENDED, &usbhid->iofl);
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001575 status = hid_post_reset(intf);
1576 if (status >= 0 && hid->driver && hid->driver->reset_resume) {
1577 int ret = hid->driver->reset_resume(hid);
1578 if (ret < 0)
1579 status = ret;
1580 }
1581 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
1583
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001584#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Márton Némethd67dec52010-01-10 17:59:22 +01001586static const struct usb_device_id hid_usb_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1588 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
1589 { } /* Terminating entry */
1590};
1591
1592MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1593
1594static struct usb_driver hid_driver = {
1595 .name = "usbhid",
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001596 .probe = usbhid_probe,
1597 .disconnect = usbhid_disconnect,
Jiri Kosina0f6f1402009-01-19 09:17:18 +01001598#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 .suspend = hid_suspend,
1600 .resume = hid_resume,
Oliver Neukum378a0ed2009-02-18 11:46:45 +01001601 .reset_resume = hid_reset_resume,
Jiri Kosina0f6f1402009-01-19 09:17:18 +01001602#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 .pre_reset = hid_pre_reset,
1604 .post_reset = hid_post_reset,
1605 .id_table = hid_usb_ids,
Oliver Neukum933e3182007-07-11 14:48:58 +02001606 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607};
1608
Guillaume Chazarain8fe294c2010-09-12 21:32:35 +02001609struct usb_interface *usbhid_find_interface(int minor)
1610{
1611 return usb_find_interface(&hid_driver, minor);
1612}
1613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614static int __init hid_init(void)
1615{
Oliver Neukum0361a282008-12-17 15:38:03 +01001616 int retval = -ENOMEM;
1617
Paul Walmsley876b9272007-04-19 14:56:12 +02001618 retval = usbhid_quirks_init(quirks_param);
1619 if (retval)
1620 goto usbhid_quirks_init_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 retval = usb_register(&hid_driver);
1622 if (retval)
1623 goto usb_register_fail;
Jiri Kosinaccabcd22009-10-02 18:31:36 +02001624 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 return 0;
1627usb_register_fail:
Paul Walmsley876b9272007-04-19 14:56:12 +02001628 usbhid_quirks_exit();
1629usbhid_quirks_init_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 return retval;
1631}
1632
1633static void __exit hid_exit(void)
1634{
1635 usb_deregister(&hid_driver);
Paul Walmsley876b9272007-04-19 14:56:12 +02001636 usbhid_quirks_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637}
1638
1639module_init(hid_init);
1640module_exit(hid_exit);
1641
Jiri Kosina88adb722009-10-02 18:29:34 +02001642MODULE_AUTHOR("Andreas Gal");
1643MODULE_AUTHOR("Vojtech Pavlik");
1644MODULE_AUTHOR("Jiri Kosina");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645MODULE_DESCRIPTION(DRIVER_DESC);
1646MODULE_LICENSE(DRIVER_LICENSE);