blob: 80c50763b3f821cfe17aca989b50224eb8629b24 [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 Stern8f507ef2014-09-02 11:39:15 -0400119 int rc;
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 Stern011b15d2008-11-04 11:29:27 -0500125 if (rc == 0) {
Alan Stern8f507ef2014-09-02 11:39:15 -0400126 hid_start_in(hid);
127 } else {
128 dev_dbg(&usbhid->intf->dev,
129 "clear-halt failed: %d\n", rc);
130 set_bit(HID_RESET_PENDING, &usbhid->iofl);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400131 }
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400132 }
Alan Sternaef4e262006-01-31 12:58:38 -0500133
Alan Stern8f507ef2014-09-02 11:39:15 -0400134 if (test_bit(HID_RESET_PENDING, &usbhid->iofl)) {
135 dev_dbg(&usbhid->intf->dev, "resetting device\n");
136 usb_queue_reset_device(usbhid->intf);
Alan Sterndf9a1f42006-06-01 13:55:28 -0400137 }
Alan Sternaef4e262006-01-31 12:58:38 -0500138}
139
140/* Main I/O error handler */
141static void hid_io_error(struct hid_device *hid)
142{
143 unsigned long flags;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100144 struct usbhid_device *usbhid = hid->driver_data;
Alan Sternaef4e262006-01-31 12:58:38 -0500145
Oliver Neukum0361a282008-12-17 15:38:03 +0100146 spin_lock_irqsave(&usbhid->lock, flags);
Alan Sternaef4e262006-01-31 12:58:38 -0500147
148 /* Stop when disconnected */
Oliver Neukum69626f22008-03-31 16:27:30 +0200149 if (test_bit(HID_DISCONNECTED, &usbhid->iofl))
Alan Sternaef4e262006-01-31 12:58:38 -0500150 goto done;
151
Alan Stern5e2a55f2007-03-20 19:03:31 +0100152 /* If it has been a while since the last error, we'll assume
153 * this a brand new error and reset the retry timeout. */
154 if (time_after(jiffies, usbhid->stop_retry + HZ/2))
155 usbhid->retry_delay = 0;
156
Alan Sternaef4e262006-01-31 12:58:38 -0500157 /* When an error occurs, retry at increasing intervals */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100158 if (usbhid->retry_delay == 0) {
159 usbhid->retry_delay = 13; /* Then 26, 52, 104, 104, ... */
160 usbhid->stop_retry = jiffies + msecs_to_jiffies(1000);
161 } else if (usbhid->retry_delay < 100)
162 usbhid->retry_delay *= 2;
Alan Sternaef4e262006-01-31 12:58:38 -0500163
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100164 if (time_after(jiffies, usbhid->stop_retry)) {
Alan Sternaef4e262006-01-31 12:58:38 -0500165
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200166 /* Retries failed, so do a port reset unless we lack bandwidth*/
167 if (test_bit(HID_NO_BANDWIDTH, &usbhid->iofl)
168 && !test_and_set_bit(HID_RESET_PENDING, &usbhid->iofl)) {
169
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100170 schedule_work(&usbhid->reset_work);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400171 goto done;
Alan Sternaef4e262006-01-31 12:58:38 -0500172 }
173 }
174
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100175 mod_timer(&usbhid->io_retry,
176 jiffies + msecs_to_jiffies(usbhid->retry_delay));
Alan Sternaef4e262006-01-31 12:58:38 -0500177done:
Oliver Neukum0361a282008-12-17 15:38:03 +0100178 spin_unlock_irqrestore(&usbhid->lock, flags);
179}
180
181static void usbhid_mark_busy(struct usbhid_device *usbhid)
182{
183 struct usb_interface *intf = usbhid->intf;
184
185 usb_mark_last_busy(interface_to_usbdev(intf));
186}
187
188static int usbhid_restart_out_queue(struct usbhid_device *usbhid)
189{
190 struct hid_device *hid = usb_get_intfdata(usbhid->intf);
191 int kicked;
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800192 int r;
Oliver Neukum0361a282008-12-17 15:38:03 +0100193
Alan Sternd4150c82012-07-19 16:08:54 -0400194 if (!hid || test_bit(HID_RESET_PENDING, &usbhid->iofl) ||
195 test_bit(HID_SUSPENDED, &usbhid->iofl))
Oliver Neukum0361a282008-12-17 15:38:03 +0100196 return 0;
197
198 if ((kicked = (usbhid->outhead != usbhid->outtail))) {
Greg Kroah-Hartman6cc203d2012-05-01 21:32:55 -0700199 hid_dbg(hid, "Kicking head %d tail %d", usbhid->outhead, usbhid->outtail);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800200
Alan Stern01a7c982012-07-19 16:08:31 -0400201 /* Try to wake up from autosuspend... */
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800202 r = usb_autopm_get_interface_async(usbhid->intf);
203 if (r < 0)
204 return r;
Alan Stern01a7c982012-07-19 16:08:31 -0400205
206 /*
207 * If still suspended, don't submit. Submission will
208 * occur if/when resume drains the queue.
209 */
Alan Sternf2b52642012-07-19 16:08:45 -0400210 if (test_bit(HID_SUSPENDED, &usbhid->iofl)) {
Alan Stern01a7c982012-07-19 16:08:31 -0400211 usb_autopm_put_interface_no_suspend(usbhid->intf);
212 return r;
213 }
214
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800215 /* Asynchronously flush queue. */
216 set_bit(HID_OUT_RUNNING, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +0100217 if (hid_submit_out(hid)) {
218 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800219 usb_autopm_put_interface_async(usbhid->intf);
Oliver Neukum0361a282008-12-17 15:38:03 +0100220 }
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800221 wake_up(&usbhid->wait);
Oliver Neukum0361a282008-12-17 15:38:03 +0100222 }
223 return kicked;
224}
225
226static int usbhid_restart_ctrl_queue(struct usbhid_device *usbhid)
227{
228 struct hid_device *hid = usb_get_intfdata(usbhid->intf);
229 int kicked;
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800230 int r;
Oliver Neukum0361a282008-12-17 15:38:03 +0100231
232 WARN_ON(hid == NULL);
Alan Sternd4150c82012-07-19 16:08:54 -0400233 if (!hid || test_bit(HID_RESET_PENDING, &usbhid->iofl) ||
234 test_bit(HID_SUSPENDED, &usbhid->iofl))
Oliver Neukum0361a282008-12-17 15:38:03 +0100235 return 0;
236
237 if ((kicked = (usbhid->ctrlhead != usbhid->ctrltail))) {
Greg Kroah-Hartman6cc203d2012-05-01 21:32:55 -0700238 hid_dbg(hid, "Kicking head %d tail %d", usbhid->ctrlhead, usbhid->ctrltail);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800239
Alan Stern01a7c982012-07-19 16:08:31 -0400240 /* Try to wake up from autosuspend... */
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800241 r = usb_autopm_get_interface_async(usbhid->intf);
242 if (r < 0)
243 return r;
Alan Stern01a7c982012-07-19 16:08:31 -0400244
245 /*
246 * If still suspended, don't submit. Submission will
247 * occur if/when resume drains the queue.
248 */
Alan Sternf2b52642012-07-19 16:08:45 -0400249 if (test_bit(HID_SUSPENDED, &usbhid->iofl)) {
Alan Stern01a7c982012-07-19 16:08:31 -0400250 usb_autopm_put_interface_no_suspend(usbhid->intf);
251 return r;
252 }
253
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800254 /* Asynchronously flush queue. */
255 set_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +0100256 if (hid_submit_ctrl(hid)) {
257 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800258 usb_autopm_put_interface_async(usbhid->intf);
Oliver Neukum0361a282008-12-17 15:38:03 +0100259 }
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800260 wake_up(&usbhid->wait);
Oliver Neukum0361a282008-12-17 15:38:03 +0100261 }
262 return kicked;
Alan Sternaef4e262006-01-31 12:58:38 -0500263}
264
265/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 * Input interrupt completion handler.
267 */
268
David Howells7d12e782006-10-05 14:55:46 +0100269static void hid_irq_in(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100272 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 int status;
274
275 switch (urb->status) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200276 case 0: /* success */
Oliver Neukum0361a282008-12-17 15:38:03 +0100277 usbhid_mark_busy(usbhid);
Jiri Slaby880d29f2008-06-18 23:55:41 +0200278 usbhid->retry_delay = 0;
279 hid_input_report(urb->context, HID_INPUT_REPORT,
280 urb->transfer_buffer,
281 urb->actual_length, 1);
Oliver Neukum0361a282008-12-17 15:38:03 +0100282 /*
283 * autosuspend refused while keys are pressed
284 * because most keyboards don't wake up when
285 * a key is released
286 */
287 if (hid_check_keys_pressed(hid))
288 set_bit(HID_KEYS_PRESSED, &usbhid->iofl);
289 else
290 clear_bit(HID_KEYS_PRESSED, &usbhid->iofl);
Jiri Slaby880d29f2008-06-18 23:55:41 +0200291 break;
292 case -EPIPE: /* stall */
Oliver Neukum0361a282008-12-17 15:38:03 +0100293 usbhid_mark_busy(usbhid);
Jiri Slaby880d29f2008-06-18 23:55:41 +0200294 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
295 set_bit(HID_CLEAR_HALT, &usbhid->iofl);
296 schedule_work(&usbhid->reset_work);
297 return;
298 case -ECONNRESET: /* unlink */
299 case -ENOENT:
300 case -ESHUTDOWN: /* unplug */
301 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
302 return;
303 case -EILSEQ: /* protocol error or unplug */
304 case -EPROTO: /* protocol error or unplug */
305 case -ETIME: /* protocol error or unplug */
306 case -ETIMEDOUT: /* Should never happen, but... */
Oliver Neukum0361a282008-12-17 15:38:03 +0100307 usbhid_mark_busy(usbhid);
Jiri Slaby880d29f2008-06-18 23:55:41 +0200308 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
309 hid_io_error(hid);
310 return;
311 default: /* error */
Joe Perches4291ee32010-12-09 19:29:03 -0800312 hid_warn(urb->dev, "input irq status %d received\n",
313 urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
315
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800316 status = usb_submit_urb(urb, GFP_ATOMIC);
Alan Sternaef4e262006-01-31 12:58:38 -0500317 if (status) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100318 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
Alan Sternaef4e262006-01-31 12:58:38 -0500319 if (status != -EPERM) {
Joe Perches4291ee32010-12-09 19:29:03 -0800320 hid_err(hid, "can't resubmit intr, %s-%s/input%d, status %d\n",
321 hid_to_usb_dev(hid)->bus->bus_name,
322 hid_to_usb_dev(hid)->devpath,
323 usbhid->ifnum, status);
Alan Sternaef4e262006-01-31 12:58:38 -0500324 hid_io_error(hid);
325 }
326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329static int hid_submit_out(struct hid_device *hid)
330{
331 struct hid_report *report;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200332 char *raw_report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100333 struct usbhid_device *usbhid = hid->driver_data;
Oliver Neukum68229682010-12-22 15:33:40 +0100334 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200336 report = usbhid->out[usbhid->outtail].report;
337 raw_report = usbhid->out[usbhid->outtail].raw_report;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800339 usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) +
340 1 + (report->id > 0);
341 usbhid->urbout->dev = hid_to_usb_dev(hid);
Alan Stern668160e2012-07-19 16:08:21 -0400342 if (raw_report) {
343 memcpy(usbhid->outbuf, raw_report,
344 usbhid->urbout->transfer_buffer_length);
345 kfree(raw_report);
346 usbhid->out[usbhid->outtail].raw_report = NULL;
347 }
Oliver Neukum68229682010-12-22 15:33:40 +0100348
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800349 dbg_hid("submitting out urb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800351 r = usb_submit_urb(usbhid->urbout, GFP_ATOMIC);
352 if (r < 0) {
353 hid_err(hid, "usb_submit_urb(out) failed: %d\n", r);
354 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800356 usbhid->last_out = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return 0;
358}
359
360static int hid_submit_ctrl(struct hid_device *hid)
361{
362 struct hid_report *report;
363 unsigned char dir;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200364 char *raw_report;
Oliver Neukum68229682010-12-22 15:33:40 +0100365 int len, r;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100366 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100368 report = usbhid->ctrl[usbhid->ctrltail].report;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200369 raw_report = usbhid->ctrl[usbhid->ctrltail].raw_report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100370 dir = usbhid->ctrl[usbhid->ctrltail].dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800372 len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
373 if (dir == USB_DIR_OUT) {
374 usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
375 usbhid->urbctrl->transfer_buffer_length = len;
Alan Stern668160e2012-07-19 16:08:21 -0400376 if (raw_report) {
377 memcpy(usbhid->ctrlbuf, raw_report, len);
378 kfree(raw_report);
379 usbhid->ctrl[usbhid->ctrltail].raw_report = NULL;
380 }
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800381 } else {
382 int maxpacket, padlen;
Oliver Neukum0361a282008-12-17 15:38:03 +0100383
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800384 usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0);
385 maxpacket = usb_maxpacket(hid_to_usb_dev(hid),
386 usbhid->urbctrl->pipe, 0);
387 if (maxpacket > 0) {
388 padlen = DIV_ROUND_UP(len, maxpacket);
389 padlen *= maxpacket;
390 if (padlen > usbhid->bufsize)
391 padlen = usbhid->bufsize;
392 } else
393 padlen = 0;
394 usbhid->urbctrl->transfer_buffer_length = padlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800396 usbhid->urbctrl->dev = hid_to_usb_dev(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800398 usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
399 usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT :
400 HID_REQ_GET_REPORT;
401 usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) |
402 report->id);
403 usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum);
404 usbhid->cr->wLength = cpu_to_le16(len);
405
406 dbg_hid("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u\n",
407 usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" :
408 "Get_Report",
409 usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength);
410
411 r = usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC);
412 if (r < 0) {
413 hid_err(hid, "usb_submit_urb(ctrl) failed: %d\n", r);
414 return r;
415 }
416 usbhid->last_ctrl = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return 0;
418}
419
420/*
421 * Output interrupt completion handler.
422 */
423
David Howells7d12e782006-10-05 14:55:46 +0100424static void hid_irq_out(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100427 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 unsigned long flags;
429 int unplug = 0;
430
431 switch (urb->status) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200432 case 0: /* success */
433 break;
434 case -ESHUTDOWN: /* unplug */
435 unplug = 1;
436 case -EILSEQ: /* protocol error or unplug */
437 case -EPROTO: /* protocol error or unplug */
438 case -ECONNRESET: /* unlink */
439 case -ENOENT:
440 break;
441 default: /* error */
Joe Perches4291ee32010-12-09 19:29:03 -0800442 hid_warn(urb->dev, "output irq status %d received\n",
443 urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445
Oliver Neukum0361a282008-12-17 15:38:03 +0100446 spin_lock_irqsave(&usbhid->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Alan Stern93101af2012-07-19 16:08:39 -0400448 if (unplug) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100449 usbhid->outtail = usbhid->outhead;
Alan Stern93101af2012-07-19 16:08:39 -0400450 } else {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100451 usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Alan Stern93101af2012-07-19 16:08:39 -0400453 if (usbhid->outhead != usbhid->outtail &&
454 hid_submit_out(hid) == 0) {
455 /* Successfully submitted next urb in queue */
456 spin_unlock_irqrestore(&usbhid->lock, flags);
457 return;
458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100461 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +0100462 spin_unlock_irqrestore(&usbhid->lock, flags);
Oliver Neukum68229682010-12-22 15:33:40 +0100463 usb_autopm_put_interface_async(usbhid->intf);
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100464 wake_up(&usbhid->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
467/*
468 * Control pipe completion handler.
469 */
470
David Howells7d12e782006-10-05 14:55:46 +0100471static void hid_ctrl(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100474 struct usbhid_device *usbhid = hid->driver_data;
Oliver Neukum0361a282008-12-17 15:38:03 +0100475 int unplug = 0, status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Oliver Neukum0361a282008-12-17 15:38:03 +0100477 spin_lock(&usbhid->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Oliver Neukum0361a282008-12-17 15:38:03 +0100479 switch (status) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200480 case 0: /* success */
481 if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
482 hid_input_report(urb->context,
483 usbhid->ctrl[usbhid->ctrltail].report->type,
484 urb->transfer_buffer, urb->actual_length, 0);
485 break;
486 case -ESHUTDOWN: /* unplug */
487 unplug = 1;
488 case -EILSEQ: /* protocol error or unplug */
489 case -EPROTO: /* protocol error or unplug */
490 case -ECONNRESET: /* unlink */
491 case -ENOENT:
492 case -EPIPE: /* report not available */
493 break;
494 default: /* error */
Joe Perches4291ee32010-12-09 19:29:03 -0800495 hid_warn(urb->dev, "ctrl urb status %d received\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497
Alan Stern93101af2012-07-19 16:08:39 -0400498 if (unplug) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100499 usbhid->ctrltail = usbhid->ctrlhead;
Alan Stern93101af2012-07-19 16:08:39 -0400500 } else {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100501 usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Alan Stern93101af2012-07-19 16:08:39 -0400503 if (usbhid->ctrlhead != usbhid->ctrltail &&
504 hid_submit_ctrl(hid) == 0) {
505 /* Successfully submitted next urb in queue */
506 spin_unlock(&usbhid->lock);
507 return;
508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100511 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +0100512 spin_unlock(&usbhid->lock);
Oliver Neukum68229682010-12-22 15:33:40 +0100513 usb_autopm_put_interface_async(usbhid->intf);
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100514 wake_up(&usbhid->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
H Hartley Sweeten52cfc61b2009-08-17 15:37:18 -0700517static void __usbhid_submit_report(struct hid_device *hid, struct hid_report *report,
518 unsigned char dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 int head;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100521 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Reyad Attiyat46df9de2014-07-25 00:13:01 -0500523 if (((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN) ||
524 test_bit(HID_DISCONNECTED, &usbhid->iofl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return;
526
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100527 if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100528 if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
Joe Perches4291ee32010-12-09 19:29:03 -0800529 hid_warn(hid, "output queue full\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return;
531 }
532
Jiri Kosina27ce4052013-07-10 19:56:27 +0200533 usbhid->out[usbhid->outhead].raw_report = hid_alloc_report_buf(report, GFP_ATOMIC);
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200534 if (!usbhid->out[usbhid->outhead].raw_report) {
Joe Perches4291ee32010-12-09 19:29:03 -0800535 hid_warn(hid, "output queueing failed\n");
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200536 return;
537 }
538 hid_output_report(report, usbhid->out[usbhid->outhead].raw_report);
539 usbhid->out[usbhid->outhead].report = report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100540 usbhid->outhead = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Alan Stern01a7c982012-07-19 16:08:31 -0400542 /* If the queue isn't running, restart it */
543 if (!test_bit(HID_OUT_RUNNING, &usbhid->iofl)) {
544 usbhid_restart_out_queue(usbhid);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800545
Alan Stern01a7c982012-07-19 16:08:31 -0400546 /* Otherwise see if an earlier request has timed out */
547 } else if (time_after(jiffies, usbhid->last_out + HZ * 5)) {
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800548
Alan Stern01a7c982012-07-19 16:08:31 -0400549 /* Prevent autosuspend following the unlink */
550 usb_autopm_get_interface_no_resume(usbhid->intf);
551
Oliver Neukum858155f2010-02-12 13:02:28 +0100552 /*
Alan Stern01a7c982012-07-19 16:08:31 -0400553 * Prevent resubmission in case the URB completes
554 * before we can unlink it. We don't want to cancel
555 * the wrong transfer!
Oliver Neukum858155f2010-02-12 13:02:28 +0100556 */
Alan Stern01a7c982012-07-19 16:08:31 -0400557 usb_block_urb(usbhid->urbout);
Oliver Neukum8815bb02012-04-30 09:13:46 +0200558
Alan Stern01a7c982012-07-19 16:08:31 -0400559 /* Drop lock to avoid deadlock if the callback runs */
560 spin_unlock(&usbhid->lock);
Oliver Neukum8815bb02012-04-30 09:13:46 +0200561
Alan Stern01a7c982012-07-19 16:08:31 -0400562 usb_unlink_urb(usbhid->urbout);
563 spin_lock(&usbhid->lock);
564 usb_unblock_urb(usbhid->urbout);
565
566 /* Unlink might have stopped the queue */
567 if (!test_bit(HID_OUT_RUNNING, &usbhid->iofl))
568 usbhid_restart_out_queue(usbhid);
569
570 /* Now we can allow autosuspend again */
571 usb_autopm_put_interface_async(usbhid->intf);
Oliver Neukum858155f2010-02-12 13:02:28 +0100572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return;
574 }
575
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100576 if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
Joe Perches4291ee32010-12-09 19:29:03 -0800577 hid_warn(hid, "control queue full\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return;
579 }
580
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200581 if (dir == USB_DIR_OUT) {
Jiri Kosina27ce4052013-07-10 19:56:27 +0200582 usbhid->ctrl[usbhid->ctrlhead].raw_report = hid_alloc_report_buf(report, GFP_ATOMIC);
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200583 if (!usbhid->ctrl[usbhid->ctrlhead].raw_report) {
Joe Perches4291ee32010-12-09 19:29:03 -0800584 hid_warn(hid, "control queueing failed\n");
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200585 return;
586 }
587 hid_output_report(report, usbhid->ctrl[usbhid->ctrlhead].raw_report);
588 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100589 usbhid->ctrl[usbhid->ctrlhead].report = report;
590 usbhid->ctrl[usbhid->ctrlhead].dir = dir;
591 usbhid->ctrlhead = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Alan Stern01a7c982012-07-19 16:08:31 -0400593 /* If the queue isn't running, restart it */
594 if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl)) {
595 usbhid_restart_ctrl_queue(usbhid);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800596
Alan Stern01a7c982012-07-19 16:08:31 -0400597 /* Otherwise see if an earlier request has timed out */
598 } else if (time_after(jiffies, usbhid->last_ctrl + HZ * 5)) {
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800599
Alan Stern01a7c982012-07-19 16:08:31 -0400600 /* Prevent autosuspend following the unlink */
601 usb_autopm_get_interface_no_resume(usbhid->intf);
602
Oliver Neukum858155f2010-02-12 13:02:28 +0100603 /*
Alan Stern01a7c982012-07-19 16:08:31 -0400604 * Prevent resubmission in case the URB completes
605 * before we can unlink it. We don't want to cancel
606 * the wrong transfer!
Oliver Neukum858155f2010-02-12 13:02:28 +0100607 */
Alan Stern01a7c982012-07-19 16:08:31 -0400608 usb_block_urb(usbhid->urbctrl);
609
610 /* Drop lock to avoid deadlock if the callback runs */
611 spin_unlock(&usbhid->lock);
612
613 usb_unlink_urb(usbhid->urbctrl);
614 spin_lock(&usbhid->lock);
615 usb_unblock_urb(usbhid->urbctrl);
616
617 /* Unlink might have stopped the queue */
618 if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
619 usbhid_restart_ctrl_queue(usbhid);
620
621 /* Now we can allow autosuspend again */
622 usb_autopm_put_interface_async(usbhid->intf);
Oliver Neukum858155f2010-02-12 13:02:28 +0100623 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100624}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100626static void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
Oliver Neukum0361a282008-12-17 15:38:03 +0100627{
628 struct usbhid_device *usbhid = hid->driver_data;
629 unsigned long flags;
630
631 spin_lock_irqsave(&usbhid->lock, flags);
632 __usbhid_submit_report(hid, report, dir);
633 spin_unlock_irqrestore(&usbhid->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
635
Benjamin Tissoiresb7966a42013-02-25 11:31:47 +0100636static int usbhid_wait_io(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100638 struct usbhid_device *usbhid = hid->driver_data;
639
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100640 if (!wait_event_timeout(usbhid->wait,
641 (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
642 !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 10*HZ)) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200644 dbg_hid("timeout waiting for ctrl or out queue to clear\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return -1;
646 }
647
648 return 0;
649}
650
Vojtech Pavlik854561b2005-05-29 02:28:00 -0500651static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
652{
Vojtech Pavlik71387bd2005-05-29 02:28:14 -0500653 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
Vojtech Pavlik854561b2005-05-29 02:28:00 -0500654 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
655 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
656}
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
659 unsigned char type, void *buf, int size)
660{
661 int result, retries = 4;
662
Jiri Kosina43c7bf02007-01-26 12:58:24 +0100663 memset(buf, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 do {
666 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
667 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
668 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
669 retries--;
670 } while (result < size && retries);
671 return result;
672}
673
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100674int usbhid_open(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Oliver Neukum933e3182007-07-11 14:48:58 +0200676 struct usbhid_device *usbhid = hid->driver_data;
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200677 int res = 0;
Oliver Neukum933e3182007-07-11 14:48:58 +0200678
Oliver Neukum0361a282008-12-17 15:38:03 +0100679 mutex_lock(&hid_open_mut);
Oliver Neukum933e3182007-07-11 14:48:58 +0200680 if (!hid->open++) {
681 res = usb_autopm_get_interface(usbhid->intf);
Oliver Neukum68229682010-12-22 15:33:40 +0100682 /* the device must be awake to reliably request remote wakeup */
Oliver Neukum933e3182007-07-11 14:48:58 +0200683 if (res < 0) {
684 hid->open--;
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200685 res = -EIO;
686 goto done;
Oliver Neukum933e3182007-07-11 14:48:58 +0200687 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100688 usbhid->intf->needs_remote_wakeup = 1;
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200689 res = hid_start_in(hid);
690 if (res) {
691 if (res != -ENOSPC) {
692 hid_io_error(hid);
693 res = 0;
694 } else {
695 /* no use opening if resources are insufficient */
696 hid->open--;
697 res = -EBUSY;
698 usbhid->intf->needs_remote_wakeup = 0;
699 }
700 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100701 usb_autopm_put_interface(usbhid->intf);
Oliver Neukum933e3182007-07-11 14:48:58 +0200702 }
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200703done:
Oliver Neukum0361a282008-12-17 15:38:03 +0100704 mutex_unlock(&hid_open_mut);
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200705 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100708void usbhid_close(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100710 struct usbhid_device *usbhid = hid->driver_data;
711
Oliver Neukum0361a282008-12-17 15:38:03 +0100712 mutex_lock(&hid_open_mut);
713
714 /* protecting hid->open to make sure we don't restart
715 * data acquistion due to a resumption we no longer
716 * care about
717 */
718 spin_lock_irq(&usbhid->lock);
Oliver Neukum933e3182007-07-11 14:48:58 +0200719 if (!--hid->open) {
Oliver Neukum0361a282008-12-17 15:38:03 +0100720 spin_unlock_irq(&usbhid->lock);
Oliver Neukum89092dd2009-04-29 17:12:12 +0200721 hid_cancel_delayed_stuff(usbhid);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100722 usb_kill_urb(usbhid->urbin);
Oliver Neukum0361a282008-12-17 15:38:03 +0100723 usbhid->intf->needs_remote_wakeup = 0;
724 } else {
725 spin_unlock_irq(&usbhid->lock);
Oliver Neukum933e3182007-07-11 14:48:58 +0200726 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100727 mutex_unlock(&hid_open_mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
730/*
731 * Initialize all reports
732 */
733
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100734void usbhid_init_reports(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
736 struct hid_report *report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100737 struct usbhid_device *usbhid = hid->driver_data;
Benjamin Tissoires595e9272013-08-22 14:51:09 +0200738 struct hid_report_enum *report_enum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 int err, ret;
740
Benjamin Tissoires595e9272013-08-22 14:51:09 +0200741 if (!(hid->quirks & HID_QUIRK_NO_INIT_INPUT_REPORTS)) {
742 report_enum = &hid->report_enum[HID_INPUT_REPORT];
743 list_for_each_entry(report, &report_enum->report_list, list)
744 usbhid_submit_report(hid, report, USB_DIR_IN);
745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Benjamin Tissoires595e9272013-08-22 14:51:09 +0200747 report_enum = &hid->report_enum[HID_FEATURE_REPORT];
748 list_for_each_entry(report, &report_enum->report_list, list)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100749 usbhid_submit_report(hid, report, USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 err = 0;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100752 ret = usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 while (ret) {
754 err |= ret;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100755 if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
756 usb_kill_urb(usbhid->urbctrl);
757 if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
758 usb_kill_urb(usbhid->urbout);
759 ret = usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
761
762 if (err)
Joe Perches4291ee32010-12-09 19:29:03 -0800763 hid_warn(hid, "timeout initializing reports\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500766/*
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200767 * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
768 */
769static int hid_find_field_early(struct hid_device *hid, unsigned int page,
770 unsigned int hid_code, struct hid_field **pfield)
771{
772 struct hid_report *report;
773 struct hid_field *field;
774 struct hid_usage *usage;
775 int i, j;
776
777 list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
778 for (i = 0; i < report->maxfield; i++) {
779 field = report->field[i];
780 for (j = 0; j < field->maxusage; j++) {
781 usage = &field->usage[j];
782 if ((usage->hid & HID_USAGE_PAGE) == page &&
783 (usage->hid & 0xFFFF) == hid_code) {
784 *pfield = field;
785 return j;
786 }
787 }
788 }
789 }
790 return -1;
791}
792
David Herrmannddf64a32013-07-15 19:10:10 +0200793static void usbhid_set_leds(struct hid_device *hid)
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200794{
795 struct hid_field *field;
796 int offset;
797
798 if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
799 hid_set_field(field, offset, 0);
800 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
801 }
802}
803
804/*
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500805 * Traverse the supplied list of reports and find the longest
806 */
Jiri Slaby282bfd42008-03-28 17:06:41 +0100807static void hid_find_max_report(struct hid_device *hid, unsigned int type,
808 unsigned int *max)
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500809{
810 struct hid_report *report;
Jiri Slaby282bfd42008-03-28 17:06:41 +0100811 unsigned int size;
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500812
813 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
Jiri Kosinaefc7ce12008-10-17 15:01:15 +0200814 size = ((report->size - 1) >> 3) + 1 + hid->report_enum[type].numbered;
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500815 if (*max < size)
816 *max = size;
817 }
818}
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
821{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100822 struct usbhid_device *usbhid = hid->driver_data;
823
Daniel Mack997ea582010-04-12 13:17:25 +0200824 usbhid->inbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
Jiri Slaby898089d2008-11-24 16:20:06 +0100825 &usbhid->inbuf_dma);
Daniel Mack997ea582010-04-12 13:17:25 +0200826 usbhid->outbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
Jiri Slaby898089d2008-11-24 16:20:06 +0100827 &usbhid->outbuf_dma);
Alan Stern0ede76f2010-03-05 15:10:17 -0500828 usbhid->cr = kmalloc(sizeof(*usbhid->cr), GFP_KERNEL);
Daniel Mack997ea582010-04-12 13:17:25 +0200829 usbhid->ctrlbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
Jiri Slaby898089d2008-11-24 16:20:06 +0100830 &usbhid->ctrlbuf_dma);
831 if (!usbhid->inbuf || !usbhid->outbuf || !usbhid->cr ||
832 !usbhid->ctrlbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return -1;
834
835 return 0;
836}
837
Alan Ottb4dbde92011-01-18 03:04:39 -0500838static int usbhid_get_raw_report(struct hid_device *hid,
839 unsigned char report_number, __u8 *buf, size_t count,
840 unsigned char report_type)
841{
842 struct usbhid_device *usbhid = hid->driver_data;
843 struct usb_device *dev = hid_to_usb_dev(hid);
844 struct usb_interface *intf = usbhid->intf;
845 struct usb_host_interface *interface = intf->cur_altsetting;
846 int skipped_report_id = 0;
847 int ret;
848
849 /* Byte 0 is the report number. Report data starts at byte 1.*/
850 buf[0] = report_number;
851 if (report_number == 0x0) {
852 /* Offset the return buffer by 1, so that the report ID
853 will remain in byte 0. */
854 buf++;
855 count--;
856 skipped_report_id = 1;
857 }
858 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
859 HID_REQ_GET_REPORT,
860 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
861 ((report_type + 1) << 8) | report_number,
862 interface->desc.bInterfaceNumber, buf, count,
863 USB_CTRL_SET_TIMEOUT);
864
865 /* count also the report id */
866 if (ret > 0 && skipped_report_id)
867 ret++;
868
869 return ret;
870}
871
Frank Praznik975a6832014-01-22 13:49:42 -0500872static int usbhid_set_raw_report(struct hid_device *hid, unsigned int reportnum,
873 __u8 *buf, size_t count, unsigned char rtype)
874{
875 struct usbhid_device *usbhid = hid->driver_data;
876 struct usb_device *dev = hid_to_usb_dev(hid);
877 struct usb_interface *intf = usbhid->intf;
878 struct usb_host_interface *interface = intf->cur_altsetting;
879 int ret, skipped_report_id = 0;
880
881 /* Byte 0 is the report number. Report data starts at byte 1.*/
Benjamin Tissoirese534a932014-03-08 22:52:42 -0500882 if ((rtype == HID_OUTPUT_REPORT) &&
883 (hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORT_ID))
884 buf[0] = 0;
885 else
886 buf[0] = reportnum;
887
Frank Praznik975a6832014-01-22 13:49:42 -0500888 if (buf[0] == 0x0) {
889 /* Don't send the Report ID */
890 buf++;
891 count--;
892 skipped_report_id = 1;
893 }
894
895 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
896 HID_REQ_SET_REPORT,
897 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
898 ((rtype + 1) << 8) | reportnum,
899 interface->desc.bInterfaceNumber, buf, count,
900 USB_CTRL_SET_TIMEOUT);
901 /* count also the report id, if this was a numbered report. */
902 if (ret > 0 && skipped_report_id)
903 ret++;
904
905 return ret;
906}
907
Frank Praznik975a6832014-01-22 13:49:42 -0500908static int usbhid_output_report(struct hid_device *hid, __u8 *buf, size_t count)
909{
910 struct usbhid_device *usbhid = hid->driver_data;
911 struct usb_device *dev = hid_to_usb_dev(hid);
912 int actual_length, skipped_report_id = 0, ret;
913
914 if (!usbhid->urbout)
Benjamin Tissoiresddea1af2014-02-10 12:58:51 -0500915 return -ENOSYS;
Frank Praznik975a6832014-01-22 13:49:42 -0500916
917 if (buf[0] == 0x0) {
918 /* Don't send the Report ID */
919 buf++;
920 count--;
921 skipped_report_id = 1;
922 }
923
924 ret = usb_interrupt_msg(dev, usbhid->urbout->pipe,
925 buf, count, &actual_length,
926 USB_CTRL_SET_TIMEOUT);
927 /* return the number of bytes transferred */
928 if (ret == 0) {
929 ret = actual_length;
930 /* count also the report id */
931 if (skipped_report_id)
932 ret++;
933 }
934
935 return ret;
936}
937
Oliver Neukum0361a282008-12-17 15:38:03 +0100938static void usbhid_restart_queues(struct usbhid_device *usbhid)
939{
Alan Sterneb055fd2012-07-19 16:09:01 -0400940 if (usbhid->urbout && !test_bit(HID_OUT_RUNNING, &usbhid->iofl))
Oliver Neukum0361a282008-12-17 15:38:03 +0100941 usbhid_restart_out_queue(usbhid);
Alan Sterneb055fd2012-07-19 16:09:01 -0400942 if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
943 usbhid_restart_ctrl_queue(usbhid);
Oliver Neukum0361a282008-12-17 15:38:03 +0100944}
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
947{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100948 struct usbhid_device *usbhid = hid->driver_data;
949
Daniel Mack997ea582010-04-12 13:17:25 +0200950 usb_free_coherent(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
951 usb_free_coherent(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
Alan Stern0ede76f2010-03-05 15:10:17 -0500952 kfree(usbhid->cr);
Daniel Mack997ea582010-04-12 13:17:25 +0200953 usb_free_coherent(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954}
955
Jiri Slabyc500c972008-05-16 11:49:16 +0200956static int usbhid_parse(struct hid_device *hid)
957{
958 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 struct usb_host_interface *interface = intf->cur_altsetting;
960 struct usb_device *dev = interface_to_usbdev (intf);
961 struct hid_descriptor *hdesc;
Paul Walmsley2eb5dc32007-04-19 13:27:04 +0200962 u32 quirks = 0;
Jiri Slabyc500c972008-05-16 11:49:16 +0200963 unsigned int rsize = 0;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500964 char *rdesc;
Jiri Slabyc500c972008-05-16 11:49:16 +0200965 int ret, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Paul Walmsley2eb5dc32007-04-19 13:27:04 +0200967 quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
968 le16_to_cpu(dev->descriptor.idProduct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Jiri Kosina6f4303f2009-01-29 00:15:51 +0100970 if (quirks & HID_QUIRK_IGNORE)
971 return -ENODEV;
972
Alan Stern0f28b552006-05-15 14:49:04 -0400973 /* Many keyboards and mice don't like to be polled for reports,
974 * so we will always set the HID_QUIRK_NOGET flag for them. */
975 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
976 if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
977 interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
978 quirks |= HID_QUIRK_NOGET;
979 }
980
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500981 if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
982 (!interface->desc.bNumEndpoints ||
983 usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200984 dbg_hid("class descriptor not present\n");
Jiri Slabyc500c972008-05-16 11:49:16 +0200985 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
987
Jiri Slabyc500c972008-05-16 11:49:16 +0200988 hid->version = le16_to_cpu(hdesc->bcdHID);
989 hid->country = hdesc->bCountryCode;
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 for (n = 0; n < hdesc->bNumDescriptors; n++)
992 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
993 rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
994
995 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200996 dbg_hid("weird size of report descriptor (%u)\n", rsize);
Jiri Slabyc500c972008-05-16 11:49:16 +0200997 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
999
1000 if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
Jiri Kosina58037eb2007-05-30 15:07:13 +02001001 dbg_hid("couldn't allocate rdesc memory\n");
Jiri Slabyc500c972008-05-16 11:49:16 +02001002 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
1004
Vojtech Pavlik854561b2005-05-29 02:28:00 -05001005 hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
1006
Jiri Slabyc500c972008-05-16 11:49:16 +02001007 ret = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber,
1008 HID_DT_REPORT, rdesc, rsize);
1009 if (ret < 0) {
Jiri Kosina58037eb2007-05-30 15:07:13 +02001010 dbg_hid("reading report descriptor failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 kfree(rdesc);
Jiri Slabyc500c972008-05-16 11:49:16 +02001012 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
1014
Jiri Slabyc500c972008-05-16 11:49:16 +02001015 ret = hid_parse_report(hid, rdesc, rsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 kfree(rdesc);
Jiri Slabyc500c972008-05-16 11:49:16 +02001017 if (ret) {
1018 dbg_hid("parsing report descriptor failed\n");
1019 goto err;
1020 }
1021
Zoltan Karcagif5208992009-05-06 16:30:21 +02001022 hid->quirks |= quirks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Jiri Slabyc500c972008-05-16 11:49:16 +02001024 return 0;
1025err:
1026 return ret;
1027}
1028
1029static int usbhid_start(struct hid_device *hid)
1030{
1031 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
1032 struct usb_host_interface *interface = intf->cur_altsetting;
1033 struct usb_device *dev = interface_to_usbdev(intf);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001034 struct usbhid_device *usbhid = hid->driver_data;
Jiri Slabyc500c972008-05-16 11:49:16 +02001035 unsigned int n, insize = 0;
1036 int ret;
1037
Jiri Slabye3e14de2008-11-01 23:41:46 +01001038 clear_bit(HID_DISCONNECTED, &usbhid->iofl);
1039
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001040 usbhid->bufsize = HID_MIN_BUFFER_SIZE;
1041 hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
1042 hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
1043 hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
1044
1045 if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
1046 usbhid->bufsize = HID_MAX_BUFFER_SIZE;
Michael Haboustakbf0964d2005-09-05 00:12:01 -05001047
1048 hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
1049
1050 if (insize > HID_MAX_BUFFER_SIZE)
1051 insize = HID_MAX_BUFFER_SIZE;
1052
Jiri Slabyc500c972008-05-16 11:49:16 +02001053 if (hid_alloc_buffers(dev, hid)) {
1054 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 goto fail;
Pekka Sarnilaf345c372008-03-06 13:23:14 +01001056 }
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 for (n = 0; n < interface->desc.bNumEndpoints; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 struct usb_endpoint_descriptor *endpoint;
1060 int pipe;
1061 int interval;
1062
1063 endpoint = &interface->endpoint[n].desc;
Jiri Slaby581a2732008-11-24 16:20:08 +01001064 if (!usb_endpoint_xfer_int(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 continue;
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 interval = endpoint->bInterval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Pekka Sarnilaf345c372008-03-06 13:23:14 +01001069 /* Some vendors give fullspeed interval on highspeed devides */
Jiri Slabyc500c972008-05-16 11:49:16 +02001070 if (hid->quirks & HID_QUIRK_FULLSPEED_INTERVAL &&
Pekka Sarnilaf345c372008-03-06 13:23:14 +01001071 dev->speed == USB_SPEED_HIGH) {
1072 interval = fls(endpoint->bInterval*8);
1073 printk(KERN_INFO "%s: Fixing fullspeed to highspeed interval: %d -> %d\n",
1074 hid->name, endpoint->bInterval, interval);
1075 }
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 /* Change the polling interval of mice. */
1078 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
1079 interval = hid_mousepoll_interval;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -05001080
Jiri Slabyc500c972008-05-16 11:49:16 +02001081 ret = -ENOMEM;
Luiz Fernando N. Capitulino0f12aa02006-10-26 13:02:51 -03001082 if (usb_endpoint_dir_in(endpoint)) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001083 if (usbhid->urbin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 continue;
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001085 if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 goto fail;
1087 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001088 usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 hid_irq_in, hid, interval);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001090 usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
1091 usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 } else {
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001093 if (usbhid->urbout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 continue;
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001095 if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 goto fail;
1097 pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001098 usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 hid_irq_out, hid, interval);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001100 usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
1101 usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
1103 }
1104
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001105 usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
Jiri Slabyc500c972008-05-16 11:49:16 +02001106 if (!usbhid->urbctrl) {
1107 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 goto fail;
Jiri Slabyc500c972008-05-16 11:49:16 +02001109 }
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001110
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001111 usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
1112 usbhid->ctrlbuf, 1, hid_ctrl, hid);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001113 usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
Alan Stern0ede76f2010-03-05 15:10:17 -05001114 usbhid->urbctrl->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Jiri Slabyc500c972008-05-16 11:49:16 +02001115
Jiri Kosina5b915d92009-11-05 14:08:03 +01001116 if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
1117 usbhid_init_reports(hid);
Jiri Slaby93c10132008-06-27 00:04:24 +02001118
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001119 set_bit(HID_STARTED, &usbhid->iofl);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001120
Alan Stern08ef08e2008-10-30 23:58:51 +01001121 /* Some keyboards don't work until their LEDs have been set.
1122 * Since BIOSes do set the LEDs, it must be safe for any device
1123 * that supports the keyboard boot protocol.
Alan Stern3d615102010-04-02 13:21:58 -04001124 * In addition, enable remote wakeup by default for all keyboard
1125 * devices supporting the boot protocol.
Alan Stern08ef08e2008-10-30 23:58:51 +01001126 */
1127 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT &&
1128 interface->desc.bInterfaceProtocol ==
Alan Stern3d615102010-04-02 13:21:58 -04001129 USB_INTERFACE_PROTOCOL_KEYBOARD) {
Alan Stern08ef08e2008-10-30 23:58:51 +01001130 usbhid_set_leds(hid);
Alan Stern3d615102010-04-02 13:21:58 -04001131 device_set_wakeup_enable(&dev->dev, 1);
1132 }
Jiri Slabyc500c972008-05-16 11:49:16 +02001133 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135fail:
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001136 usb_free_urb(usbhid->urbin);
1137 usb_free_urb(usbhid->urbout);
1138 usb_free_urb(usbhid->urbctrl);
Jiri Slabye3e14de2008-11-01 23:41:46 +01001139 usbhid->urbin = NULL;
1140 usbhid->urbout = NULL;
1141 usbhid->urbctrl = NULL;
Jiri Kosina22f675f2007-08-01 12:32:27 +02001142 hid_free_buffers(dev, hid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001143 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
Jiri Slabyc500c972008-05-16 11:49:16 +02001146static void usbhid_stop(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Jiri Slabyc500c972008-05-16 11:49:16 +02001148 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Jiri Slabyc500c972008-05-16 11:49:16 +02001150 if (WARN_ON(!usbhid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return;
1152
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001153 clear_bit(HID_STARTED, &usbhid->iofl);
Daniel Kurtz4371ea82011-11-17 19:23:50 +08001154 spin_lock_irq(&usbhid->lock); /* Sync with error and led handlers */
Oliver Neukum69626f22008-03-31 16:27:30 +02001155 set_bit(HID_DISCONNECTED, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +01001156 spin_unlock_irq(&usbhid->lock);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001157 usb_kill_urb(usbhid->urbin);
1158 usb_kill_urb(usbhid->urbout);
1159 usb_kill_urb(usbhid->urbctrl);
1160
Oliver Neukum0361a282008-12-17 15:38:03 +01001161 hid_cancel_delayed_stuff(usbhid);
Alan Sternaef4e262006-01-31 12:58:38 -05001162
Jiri Slabyc500c972008-05-16 11:49:16 +02001163 hid->claimed = 0;
1164
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001165 usb_free_urb(usbhid->urbin);
1166 usb_free_urb(usbhid->urbctrl);
1167 usb_free_urb(usbhid->urbout);
Jiri Slabye3e14de2008-11-01 23:41:46 +01001168 usbhid->urbin = NULL; /* don't mess up next start */
1169 usbhid->urbctrl = NULL;
1170 usbhid->urbout = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Anssi Hannulabe820972007-01-19 19:28:17 +02001172 hid_free_buffers(hid_to_usb_dev(hid), hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
1174
Oliver Neukum0361a282008-12-17 15:38:03 +01001175static int usbhid_power(struct hid_device *hid, int lvl)
1176{
1177 int r = 0;
1178
1179 switch (lvl) {
1180 case PM_HINT_FULLON:
1181 r = usbhid_get_power(hid);
1182 break;
1183 case PM_HINT_NORMAL:
1184 usbhid_put_power(hid);
1185 break;
1186 }
1187 return r;
1188}
1189
Henrik Rydberge90a6df2013-02-25 11:31:43 +01001190static void usbhid_request(struct hid_device *hid, struct hid_report *rep, int reqtype)
1191{
1192 switch (reqtype) {
1193 case HID_REQ_GET_REPORT:
1194 usbhid_submit_report(hid, rep, USB_DIR_IN);
1195 break;
1196 case HID_REQ_SET_REPORT:
1197 usbhid_submit_report(hid, rep, USB_DIR_OUT);
1198 break;
1199 }
1200}
1201
Frank Praznik975a6832014-01-22 13:49:42 -05001202static int usbhid_raw_request(struct hid_device *hid, unsigned char reportnum,
1203 __u8 *buf, size_t len, unsigned char rtype,
1204 int reqtype)
1205{
1206 switch (reqtype) {
1207 case HID_REQ_GET_REPORT:
1208 return usbhid_get_raw_report(hid, reportnum, buf, len, rtype);
1209 case HID_REQ_SET_REPORT:
1210 return usbhid_set_raw_report(hid, reportnum, buf, len, rtype);
1211 default:
1212 return -EIO;
1213 }
1214}
1215
Benjamin Tissoires96848192013-02-27 16:38:17 +01001216static int usbhid_idle(struct hid_device *hid, int report, int idle,
1217 int reqtype)
1218{
1219 struct usb_device *dev = hid_to_usb_dev(hid);
1220 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
1221 struct usb_host_interface *interface = intf->cur_altsetting;
1222 int ifnum = interface->desc.bInterfaceNumber;
1223
1224 if (reqtype != HID_REQ_SET_IDLE)
1225 return -EINVAL;
1226
1227 return hid_set_idle(dev, ifnum, report, idle);
1228}
1229
Jiri Slabyc500c972008-05-16 11:49:16 +02001230static struct hid_ll_driver usb_hid_driver = {
1231 .parse = usbhid_parse,
1232 .start = usbhid_start,
1233 .stop = usbhid_stop,
1234 .open = usbhid_open,
1235 .close = usbhid_close,
Oliver Neukum0361a282008-12-17 15:38:03 +01001236 .power = usbhid_power,
Henrik Rydberge90a6df2013-02-25 11:31:43 +01001237 .request = usbhid_request,
Henrik Rydberg33734432013-02-25 11:31:44 +01001238 .wait = usbhid_wait_io,
Frank Praznik975a6832014-01-22 13:49:42 -05001239 .raw_request = usbhid_raw_request,
1240 .output_report = usbhid_output_report,
Benjamin Tissoires96848192013-02-27 16:38:17 +01001241 .idle = usbhid_idle,
Jiri Slabyc500c972008-05-16 11:49:16 +02001242};
1243
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001244static int usbhid_probe(struct usb_interface *intf, const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
Jiri Slaby131d3a7a2008-11-14 12:03:47 +01001246 struct usb_host_interface *interface = intf->cur_altsetting;
Jiri Slabyc500c972008-05-16 11:49:16 +02001247 struct usb_device *dev = interface_to_usbdev(intf);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001248 struct usbhid_device *usbhid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 struct hid_device *hid;
Jiri Slaby131d3a7a2008-11-14 12:03:47 +01001250 unsigned int n, has_in = 0;
Jiri Slabyc500c972008-05-16 11:49:16 +02001251 size_t len;
1252 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Jiri Kosina58037eb2007-05-30 15:07:13 +02001254 dbg_hid("HID probe called for ifnum %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 intf->altsetting->desc.bInterfaceNumber);
1256
Jiri Slaby131d3a7a2008-11-14 12:03:47 +01001257 for (n = 0; n < interface->desc.bNumEndpoints; n++)
1258 if (usb_endpoint_is_int_in(&interface->endpoint[n].desc))
1259 has_in++;
1260 if (!has_in) {
Joe Perches4291ee32010-12-09 19:29:03 -08001261 hid_err(intf, "couldn't find an input interrupt endpoint\n");
Jiri Slaby131d3a7a2008-11-14 12:03:47 +01001262 return -ENODEV;
1263 }
1264
Jiri Slabyc500c972008-05-16 11:49:16 +02001265 hid = hid_allocate_device();
1266 if (IS_ERR(hid))
1267 return PTR_ERR(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 usb_set_intfdata(intf, hid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001270 hid->ll_driver = &usb_hid_driver;
Jiri Slaby76483cf2008-09-18 12:23:33 +02001271 hid->ff_init = hid_pidff_init;
Jiri Slabyc500c972008-05-16 11:49:16 +02001272#ifdef CONFIG_USB_HIDDEV
Jiri Slaby93c10132008-06-27 00:04:24 +02001273 hid->hiddev_connect = hiddev_connect;
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001274 hid->hiddev_disconnect = hiddev_disconnect;
Jiri Slabyc500c972008-05-16 11:49:16 +02001275 hid->hiddev_hid_event = hiddev_hid_event;
1276 hid->hiddev_report_event = hiddev_report_event;
1277#endif
1278 hid->dev.parent = &intf->dev;
1279 hid->bus = BUS_USB;
1280 hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
1281 hid->product = le16_to_cpu(dev->descriptor.idProduct);
1282 hid->name[0] = 0;
Bastien Nocerab5e5a372010-04-16 17:19:50 +01001283 hid->quirks = usbhid_lookup_quirk(hid->vendor, hid->product);
Jiri Slabya73a6372008-10-22 14:45:11 +02001284 if (intf->cur_altsetting->desc.bInterfaceProtocol ==
1285 USB_INTERFACE_PROTOCOL_MOUSE)
1286 hid->type = HID_TYPE_USBMOUSE;
Tomoki Sekiyama6dc14182011-05-23 15:45:44 -07001287 else if (intf->cur_altsetting->desc.bInterfaceProtocol == 0)
1288 hid->type = HID_TYPE_USBNONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Jiri Slabyc500c972008-05-16 11:49:16 +02001290 if (dev->manufacturer)
1291 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
1292
1293 if (dev->product) {
1294 if (dev->manufacturer)
1295 strlcat(hid->name, " ", sizeof(hid->name));
1296 strlcat(hid->name, dev->product, sizeof(hid->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
1298
Jiri Slabyc500c972008-05-16 11:49:16 +02001299 if (!strlen(hid->name))
1300 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
1301 le16_to_cpu(dev->descriptor.idVendor),
1302 le16_to_cpu(dev->descriptor.idProduct));
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001303
Jiri Slabyc500c972008-05-16 11:49:16 +02001304 usb_make_path(dev, hid->phys, sizeof(hid->phys));
1305 strlcat(hid->phys, "/input", sizeof(hid->phys));
1306 len = strlen(hid->phys);
1307 if (len < sizeof(hid->phys) - 1)
1308 snprintf(hid->phys + len, sizeof(hid->phys) - len,
1309 "%d", intf->altsetting[0].desc.bInterfaceNumber);
Geoff Levand4a1a4d82007-01-15 20:11:52 -08001310
Jiri Slabyc500c972008-05-16 11:49:16 +02001311 if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
1312 hid->uniq[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001314 usbhid = kzalloc(sizeof(*usbhid), GFP_KERNEL);
1315 if (usbhid == NULL) {
1316 ret = -ENOMEM;
1317 goto err;
1318 }
1319
1320 hid->driver_data = usbhid;
1321 usbhid->hid = hid;
Jiri Kosina57ab12e2010-02-17 14:25:01 +01001322 usbhid->intf = intf;
1323 usbhid->ifnum = interface->desc.bInterfaceNumber;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001324
Alan Sternfde4e2f2010-05-07 10:41:10 -04001325 init_waitqueue_head(&usbhid->wait);
1326 INIT_WORK(&usbhid->reset_work, hid_reset);
Alan Sternfde4e2f2010-05-07 10:41:10 -04001327 setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
1328 spin_lock_init(&usbhid->lock);
1329
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001330 ret = hid_add_device(hid);
1331 if (ret) {
Jiri Slabyd458a9d2008-05-16 11:49:20 +02001332 if (ret != -ENODEV)
Joe Perches4291ee32010-12-09 19:29:03 -08001333 hid_err(intf, "can't add hid device: %d\n", ret);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001334 goto err_free;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001335 }
Jiri Slabyc500c972008-05-16 11:49:16 +02001336
1337 return 0;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001338err_free:
1339 kfree(usbhid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001340err:
1341 hid_destroy_device(hid);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001342 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
1344
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001345static void usbhid_disconnect(struct usb_interface *intf)
Jiri Slabyc500c972008-05-16 11:49:16 +02001346{
1347 struct hid_device *hid = usb_get_intfdata(intf);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001348 struct usbhid_device *usbhid;
Jiri Slabyc500c972008-05-16 11:49:16 +02001349
1350 if (WARN_ON(!hid))
1351 return;
1352
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001353 usbhid = hid->driver_data;
Reyad Attiyat46df9de2014-07-25 00:13:01 -05001354 spin_lock_irq(&usbhid->lock); /* Sync with error and led handlers */
1355 set_bit(HID_DISCONNECTED, &usbhid->iofl);
1356 spin_unlock_irq(&usbhid->lock);
Jiri Slabyc500c972008-05-16 11:49:16 +02001357 hid_destroy_device(hid);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001358 kfree(usbhid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001359}
1360
Oliver Neukum0361a282008-12-17 15:38:03 +01001361static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
Oliver Neukum0361a282008-12-17 15:38:03 +01001363 del_timer_sync(&usbhid->io_retry);
Oliver Neukum0361a282008-12-17 15:38:03 +01001364 cancel_work_sync(&usbhid->reset_work);
1365}
1366
1367static void hid_cease_io(struct usbhid_device *usbhid)
1368{
Oliver Neukumfad9fbe2011-10-13 18:21:58 +02001369 del_timer_sync(&usbhid->io_retry);
Oliver Neukum0361a282008-12-17 15:38:03 +01001370 usb_kill_urb(usbhid->urbin);
1371 usb_kill_urb(usbhid->urbctrl);
1372 usb_kill_urb(usbhid->urbout);
Oliver Neukum0361a282008-12-17 15:38:03 +01001373}
1374
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001375/* Treat USB reset pretty much the same as suspend/resume */
1376static int hid_pre_reset(struct usb_interface *intf)
1377{
1378 struct hid_device *hid = usb_get_intfdata(intf);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001379 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001381 spin_lock_irq(&usbhid->lock);
1382 set_bit(HID_RESET_PENDING, &usbhid->iofl);
1383 spin_unlock_irq(&usbhid->lock);
1384 hid_cease_io(usbhid);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001385
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001386 return 0;
1387}
1388
1389/* Same routine used for post_reset and reset_resume */
1390static int hid_post_reset(struct usb_interface *intf)
1391{
1392 struct usb_device *dev = interface_to_usbdev (intf);
1393 struct hid_device *hid = usb_get_intfdata(intf);
1394 struct usbhid_device *usbhid = hid->driver_data;
Simon Haggettdc3c78e2012-04-03 16:04:15 +01001395 struct usb_host_interface *interface = intf->cur_altsetting;
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001396 int status;
Simon Haggettdc3c78e2012-04-03 16:04:15 +01001397 char *rdesc;
1398
1399 /* Fetch and examine the HID report descriptor. If this
1400 * has changed, then rebind. Since usbcore's check of the
1401 * configuration descriptors passed, we already know that
1402 * the size of the HID report descriptor has not changed.
1403 */
Kevin Daughtridge86e6b772012-09-20 12:00:32 -07001404 rdesc = kmalloc(hid->dev_rsize, GFP_KERNEL);
Simon Haggettdc3c78e2012-04-03 16:04:15 +01001405 if (!rdesc) {
1406 dbg_hid("couldn't allocate rdesc memory (post_reset)\n");
1407 return 1;
1408 }
1409 status = hid_get_class_descriptor(dev,
1410 interface->desc.bInterfaceNumber,
Kevin Daughtridge86e6b772012-09-20 12:00:32 -07001411 HID_DT_REPORT, rdesc, hid->dev_rsize);
Simon Haggettdc3c78e2012-04-03 16:04:15 +01001412 if (status < 0) {
1413 dbg_hid("reading report descriptor failed (post_reset)\n");
1414 kfree(rdesc);
1415 return 1;
1416 }
Kevin Daughtridge86e6b772012-09-20 12:00:32 -07001417 status = memcmp(rdesc, hid->dev_rdesc, hid->dev_rsize);
Simon Haggettdc3c78e2012-04-03 16:04:15 +01001418 kfree(rdesc);
1419 if (status != 0) {
1420 dbg_hid("report descriptor changed\n");
1421 return 1;
1422 }
Jiri Kosina70fa9f22009-06-04 15:48:38 +02001423
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001424 spin_lock_irq(&usbhid->lock);
1425 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
1426 spin_unlock_irq(&usbhid->lock);
1427 hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001428 status = hid_start_in(hid);
1429 if (status < 0)
1430 hid_io_error(hid);
1431 usbhid_restart_queues(usbhid);
1432
1433 return 0;
1434}
1435
1436int usbhid_get_power(struct hid_device *hid)
1437{
1438 struct usbhid_device *usbhid = hid->driver_data;
Jiri Kosina70fa9f22009-06-04 15:48:38 +02001439
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001440 return usb_autopm_get_interface(usbhid->intf);
1441}
1442
1443void usbhid_put_power(struct hid_device *hid)
1444{
1445 struct usbhid_device *usbhid = hid->driver_data;
Jiri Kosina70fa9f22009-06-04 15:48:38 +02001446
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001447 usb_autopm_put_interface(usbhid->intf);
1448}
1449
1450
Jiri Kosina0f6f1402009-01-19 09:17:18 +01001451#ifdef CONFIG_PM
Alan Sterneb055fd2012-07-19 16:09:01 -04001452static int hid_resume_common(struct hid_device *hid, bool driver_suspended)
1453{
1454 struct usbhid_device *usbhid = hid->driver_data;
1455 int status;
1456
1457 spin_lock_irq(&usbhid->lock);
1458 clear_bit(HID_SUSPENDED, &usbhid->iofl);
1459 usbhid_mark_busy(usbhid);
1460
1461 if (test_bit(HID_CLEAR_HALT, &usbhid->iofl) ||
1462 test_bit(HID_RESET_PENDING, &usbhid->iofl))
1463 schedule_work(&usbhid->reset_work);
1464 usbhid->retry_delay = 0;
1465
1466 usbhid_restart_queues(usbhid);
1467 spin_unlock_irq(&usbhid->lock);
1468
1469 status = hid_start_in(hid);
1470 if (status < 0)
1471 hid_io_error(hid);
1472
1473 if (driver_suspended && hid->driver && hid->driver->resume)
1474 status = hid->driver->resume(hid);
1475 return status;
1476}
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478static int hid_suspend(struct usb_interface *intf, pm_message_t message)
1479{
Oliver Neukum0361a282008-12-17 15:38:03 +01001480 struct hid_device *hid = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 struct usbhid_device *usbhid = hid->driver_data;
Ming Lei37093b72013-03-15 12:08:55 +08001482 int status = 0;
Alan Sterneb055fd2012-07-19 16:09:01 -04001483 bool driver_suspended = false;
David Herrmannbfde79c2013-07-15 19:10:13 +02001484 unsigned int ledcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Alan Stern5b1b0b82011-08-19 23:49:48 +02001486 if (PMSG_IS_AUTO(message)) {
David Herrmannbfde79c2013-07-15 19:10:13 +02001487 ledcount = hidinput_count_leds(hid);
Oliver Neukum0361a282008-12-17 15:38:03 +01001488 spin_lock_irq(&usbhid->lock); /* Sync with error handler */
1489 if (!test_bit(HID_RESET_PENDING, &usbhid->iofl)
1490 && !test_bit(HID_CLEAR_HALT, &usbhid->iofl)
1491 && !test_bit(HID_OUT_RUNNING, &usbhid->iofl)
1492 && !test_bit(HID_CTRL_RUNNING, &usbhid->iofl)
1493 && !test_bit(HID_KEYS_PRESSED, &usbhid->iofl)
David Herrmannbfde79c2013-07-15 19:10:13 +02001494 && (!ledcount || ignoreled))
Oliver Neukum0361a282008-12-17 15:38:03 +01001495 {
Alan Sternf2b52642012-07-19 16:08:45 -04001496 set_bit(HID_SUSPENDED, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +01001497 spin_unlock_irq(&usbhid->lock);
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001498 if (hid->driver && hid->driver->suspend) {
1499 status = hid->driver->suspend(hid, message);
1500 if (status < 0)
Alan Sterneb055fd2012-07-19 16:09:01 -04001501 goto failed;
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001502 }
Alan Sterneb055fd2012-07-19 16:09:01 -04001503 driver_suspended = true;
Oliver Neukum0361a282008-12-17 15:38:03 +01001504 } else {
1505 usbhid_mark_busy(usbhid);
1506 spin_unlock_irq(&usbhid->lock);
1507 return -EBUSY;
1508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Oliver Neukum0361a282008-12-17 15:38:03 +01001510 } else {
Ming Lei37093b72013-03-15 12:08:55 +08001511 /* TODO: resume() might need to handle suspend failure */
1512 if (hid->driver && hid->driver->suspend)
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001513 status = hid->driver->suspend(hid, message);
Alan Sterneb055fd2012-07-19 16:09:01 -04001514 driver_suspended = true;
Oliver Neukum0361a282008-12-17 15:38:03 +01001515 spin_lock_irq(&usbhid->lock);
Alan Sternf2b52642012-07-19 16:08:45 -04001516 set_bit(HID_SUSPENDED, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +01001517 spin_unlock_irq(&usbhid->lock);
Ming Lei37093b72013-03-15 12:08:55 +08001518 if (usbhid_wait_io(hid) < 0)
Alan Sterneb055fd2012-07-19 16:09:01 -04001519 status = -EIO;
Oliver Neukum0361a282008-12-17 15:38:03 +01001520 }
1521
Oliver Neukum0361a282008-12-17 15:38:03 +01001522 hid_cancel_delayed_stuff(usbhid);
1523 hid_cease_io(usbhid);
1524
Alan Stern5b1b0b82011-08-19 23:49:48 +02001525 if (PMSG_IS_AUTO(message) && test_bit(HID_KEYS_PRESSED, &usbhid->iofl)) {
Oliver Neukum0361a282008-12-17 15:38:03 +01001526 /* lost race against keypresses */
Alan Sterneb055fd2012-07-19 16:09:01 -04001527 status = -EBUSY;
1528 goto failed;
Oliver Neukum0361a282008-12-17 15:38:03 +01001529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 dev_dbg(&intf->dev, "suspend\n");
Ming Lei37093b72013-03-15 12:08:55 +08001531 return status;
Alan Sterneb055fd2012-07-19 16:09:01 -04001532
1533 failed:
1534 hid_resume_common(hid, driver_suspended);
1535 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
1538static int hid_resume(struct usb_interface *intf)
1539{
1540 struct hid_device *hid = usb_get_intfdata (intf);
1541 struct usbhid_device *usbhid = hid->driver_data;
1542 int status;
1543
Jiri Slabyfde5be32008-11-23 12:03:20 +01001544 if (!test_bit(HID_STARTED, &usbhid->iofl))
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001545 return 0;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001546
Alan Sterneb055fd2012-07-19 16:09:01 -04001547 status = hid_resume_common(hid, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 dev_dbg(&intf->dev, "resume status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 return 0;
1550}
1551
Oliver Neukum378a0ed2009-02-18 11:46:45 +01001552static int hid_reset_resume(struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
Oliver Neukum378a0ed2009-02-18 11:46:45 +01001554 struct hid_device *hid = usb_get_intfdata(intf);
1555 struct usbhid_device *usbhid = hid->driver_data;
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001556 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Alan Sternf2b52642012-07-19 16:08:45 -04001558 clear_bit(HID_SUSPENDED, &usbhid->iofl);
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001559 status = hid_post_reset(intf);
1560 if (status >= 0 && hid->driver && hid->driver->reset_resume) {
1561 int ret = hid->driver->reset_resume(hid);
1562 if (ret < 0)
1563 status = ret;
1564 }
1565 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566}
1567
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001568#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Márton Némethd67dec52010-01-10 17:59:22 +01001570static const struct usb_device_id hid_usb_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1572 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
1573 { } /* Terminating entry */
1574};
1575
1576MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1577
1578static struct usb_driver hid_driver = {
1579 .name = "usbhid",
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001580 .probe = usbhid_probe,
1581 .disconnect = usbhid_disconnect,
Jiri Kosina0f6f1402009-01-19 09:17:18 +01001582#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 .suspend = hid_suspend,
1584 .resume = hid_resume,
Oliver Neukum378a0ed2009-02-18 11:46:45 +01001585 .reset_resume = hid_reset_resume,
Jiri Kosina0f6f1402009-01-19 09:17:18 +01001586#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 .pre_reset = hid_pre_reset,
1588 .post_reset = hid_post_reset,
1589 .id_table = hid_usb_ids,
Oliver Neukum933e3182007-07-11 14:48:58 +02001590 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591};
1592
Guillaume Chazarain8fe294c2010-09-12 21:32:35 +02001593struct usb_interface *usbhid_find_interface(int minor)
1594{
1595 return usb_find_interface(&hid_driver, minor);
1596}
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598static int __init hid_init(void)
1599{
Oliver Neukum0361a282008-12-17 15:38:03 +01001600 int retval = -ENOMEM;
1601
Paul Walmsley876b9272007-04-19 14:56:12 +02001602 retval = usbhid_quirks_init(quirks_param);
1603 if (retval)
1604 goto usbhid_quirks_init_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 retval = usb_register(&hid_driver);
1606 if (retval)
1607 goto usb_register_fail;
Jiri Kosinaccabcd22009-10-02 18:31:36 +02001608 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 return 0;
1611usb_register_fail:
Paul Walmsley876b9272007-04-19 14:56:12 +02001612 usbhid_quirks_exit();
1613usbhid_quirks_init_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 return retval;
1615}
1616
1617static void __exit hid_exit(void)
1618{
1619 usb_deregister(&hid_driver);
Paul Walmsley876b9272007-04-19 14:56:12 +02001620 usbhid_quirks_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621}
1622
1623module_init(hid_init);
1624module_exit(hid_exit);
1625
Jiri Kosina88adb722009-10-02 18:29:34 +02001626MODULE_AUTHOR("Andreas Gal");
1627MODULE_AUTHOR("Vojtech Pavlik");
1628MODULE_AUTHOR("Jiri Kosina");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629MODULE_DESCRIPTION(DRIVER_DESC);
1630MODULE_LICENSE(DRIVER_LICENSE);