blob: 271578b85d914423d0725c2ab05b3243fc1eb04c [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 */
61static char *quirks_param[MAX_USBHID_BOOT_QUIRKS] = { [ 0 ... (MAX_USBHID_BOOT_QUIRKS - 1) ] = NULL };
62module_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
210 if (!hid)
211 return 0;
212
213 if ((kicked = (usbhid->outhead != usbhid->outtail))) {
Greg Kroah-Hartman6cc203d2012-05-01 21:32:55 -0700214 hid_dbg(hid, "Kicking head %d tail %d", usbhid->outhead, usbhid->outtail);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800215
Alan Stern01a7c982012-07-19 16:08:31 -0400216 /* Try to wake up from autosuspend... */
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800217 r = usb_autopm_get_interface_async(usbhid->intf);
218 if (r < 0)
219 return r;
Alan Stern01a7c982012-07-19 16:08:31 -0400220
221 /*
222 * If still suspended, don't submit. Submission will
223 * occur if/when resume drains the queue.
224 */
Alan Sternf2b52642012-07-19 16:08:45 -0400225 if (test_bit(HID_SUSPENDED, &usbhid->iofl)) {
Alan Stern01a7c982012-07-19 16:08:31 -0400226 usb_autopm_put_interface_no_suspend(usbhid->intf);
227 return r;
228 }
229
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800230 /* Asynchronously flush queue. */
231 set_bit(HID_OUT_RUNNING, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +0100232 if (hid_submit_out(hid)) {
233 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800234 usb_autopm_put_interface_async(usbhid->intf);
Oliver Neukum0361a282008-12-17 15:38:03 +0100235 }
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800236 wake_up(&usbhid->wait);
Oliver Neukum0361a282008-12-17 15:38:03 +0100237 }
238 return kicked;
239}
240
241static int usbhid_restart_ctrl_queue(struct usbhid_device *usbhid)
242{
243 struct hid_device *hid = usb_get_intfdata(usbhid->intf);
244 int kicked;
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800245 int r;
Oliver Neukum0361a282008-12-17 15:38:03 +0100246
247 WARN_ON(hid == NULL);
248 if (!hid)
249 return 0;
250
251 if ((kicked = (usbhid->ctrlhead != usbhid->ctrltail))) {
Greg Kroah-Hartman6cc203d2012-05-01 21:32:55 -0700252 hid_dbg(hid, "Kicking head %d tail %d", usbhid->ctrlhead, usbhid->ctrltail);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800253
Alan Stern01a7c982012-07-19 16:08:31 -0400254 /* Try to wake up from autosuspend... */
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800255 r = usb_autopm_get_interface_async(usbhid->intf);
256 if (r < 0)
257 return r;
Alan Stern01a7c982012-07-19 16:08:31 -0400258
259 /*
260 * If still suspended, don't submit. Submission will
261 * occur if/when resume drains the queue.
262 */
Alan Sternf2b52642012-07-19 16:08:45 -0400263 if (test_bit(HID_SUSPENDED, &usbhid->iofl)) {
Alan Stern01a7c982012-07-19 16:08:31 -0400264 usb_autopm_put_interface_no_suspend(usbhid->intf);
265 return r;
266 }
267
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800268 /* Asynchronously flush queue. */
269 set_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +0100270 if (hid_submit_ctrl(hid)) {
271 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800272 usb_autopm_put_interface_async(usbhid->intf);
Oliver Neukum0361a282008-12-17 15:38:03 +0100273 }
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800274 wake_up(&usbhid->wait);
Oliver Neukum0361a282008-12-17 15:38:03 +0100275 }
276 return kicked;
Alan Sternaef4e262006-01-31 12:58:38 -0500277}
278
279/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 * Input interrupt completion handler.
281 */
282
David Howells7d12e782006-10-05 14:55:46 +0100283static void hid_irq_in(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100286 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 int status;
288
289 switch (urb->status) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200290 case 0: /* success */
Oliver Neukum0361a282008-12-17 15:38:03 +0100291 usbhid_mark_busy(usbhid);
Jiri Slaby880d29f2008-06-18 23:55:41 +0200292 usbhid->retry_delay = 0;
293 hid_input_report(urb->context, HID_INPUT_REPORT,
294 urb->transfer_buffer,
295 urb->actual_length, 1);
Oliver Neukum0361a282008-12-17 15:38:03 +0100296 /*
297 * autosuspend refused while keys are pressed
298 * because most keyboards don't wake up when
299 * a key is released
300 */
301 if (hid_check_keys_pressed(hid))
302 set_bit(HID_KEYS_PRESSED, &usbhid->iofl);
303 else
304 clear_bit(HID_KEYS_PRESSED, &usbhid->iofl);
Jiri Slaby880d29f2008-06-18 23:55:41 +0200305 break;
306 case -EPIPE: /* stall */
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 set_bit(HID_CLEAR_HALT, &usbhid->iofl);
310 schedule_work(&usbhid->reset_work);
311 return;
312 case -ECONNRESET: /* unlink */
313 case -ENOENT:
314 case -ESHUTDOWN: /* unplug */
315 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
316 return;
317 case -EILSEQ: /* protocol error or unplug */
318 case -EPROTO: /* protocol error or unplug */
319 case -ETIME: /* protocol error or unplug */
320 case -ETIMEDOUT: /* Should never happen, but... */
Oliver Neukum0361a282008-12-17 15:38:03 +0100321 usbhid_mark_busy(usbhid);
Jiri Slaby880d29f2008-06-18 23:55:41 +0200322 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
323 hid_io_error(hid);
324 return;
325 default: /* error */
Joe Perches4291ee32010-12-09 19:29:03 -0800326 hid_warn(urb->dev, "input irq status %d received\n",
327 urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800330 status = usb_submit_urb(urb, GFP_ATOMIC);
Alan Sternaef4e262006-01-31 12:58:38 -0500331 if (status) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100332 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
Alan Sternaef4e262006-01-31 12:58:38 -0500333 if (status != -EPERM) {
Joe Perches4291ee32010-12-09 19:29:03 -0800334 hid_err(hid, "can't resubmit intr, %s-%s/input%d, status %d\n",
335 hid_to_usb_dev(hid)->bus->bus_name,
336 hid_to_usb_dev(hid)->devpath,
337 usbhid->ifnum, status);
Alan Sternaef4e262006-01-31 12:58:38 -0500338 hid_io_error(hid);
339 }
340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343static int hid_submit_out(struct hid_device *hid)
344{
345 struct hid_report *report;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200346 char *raw_report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100347 struct usbhid_device *usbhid = hid->driver_data;
Oliver Neukum68229682010-12-22 15:33:40 +0100348 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200350 report = usbhid->out[usbhid->outtail].report;
351 raw_report = usbhid->out[usbhid->outtail].raw_report;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800353 usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) +
354 1 + (report->id > 0);
355 usbhid->urbout->dev = hid_to_usb_dev(hid);
Alan Stern668160e2012-07-19 16:08:21 -0400356 if (raw_report) {
357 memcpy(usbhid->outbuf, raw_report,
358 usbhid->urbout->transfer_buffer_length);
359 kfree(raw_report);
360 usbhid->out[usbhid->outtail].raw_report = NULL;
361 }
Oliver Neukum68229682010-12-22 15:33:40 +0100362
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800363 dbg_hid("submitting out urb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800365 r = usb_submit_urb(usbhid->urbout, GFP_ATOMIC);
366 if (r < 0) {
367 hid_err(hid, "usb_submit_urb(out) failed: %d\n", r);
368 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800370 usbhid->last_out = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return 0;
372}
373
374static int hid_submit_ctrl(struct hid_device *hid)
375{
376 struct hid_report *report;
377 unsigned char dir;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200378 char *raw_report;
Oliver Neukum68229682010-12-22 15:33:40 +0100379 int len, r;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100380 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100382 report = usbhid->ctrl[usbhid->ctrltail].report;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200383 raw_report = usbhid->ctrl[usbhid->ctrltail].raw_report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100384 dir = usbhid->ctrl[usbhid->ctrltail].dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800386 len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
387 if (dir == USB_DIR_OUT) {
388 usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
389 usbhid->urbctrl->transfer_buffer_length = len;
Alan Stern668160e2012-07-19 16:08:21 -0400390 if (raw_report) {
391 memcpy(usbhid->ctrlbuf, raw_report, len);
392 kfree(raw_report);
393 usbhid->ctrl[usbhid->ctrltail].raw_report = NULL;
394 }
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800395 } else {
396 int maxpacket, padlen;
Oliver Neukum0361a282008-12-17 15:38:03 +0100397
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800398 usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0);
399 maxpacket = usb_maxpacket(hid_to_usb_dev(hid),
400 usbhid->urbctrl->pipe, 0);
401 if (maxpacket > 0) {
402 padlen = DIV_ROUND_UP(len, maxpacket);
403 padlen *= maxpacket;
404 if (padlen > usbhid->bufsize)
405 padlen = usbhid->bufsize;
406 } else
407 padlen = 0;
408 usbhid->urbctrl->transfer_buffer_length = padlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800410 usbhid->urbctrl->dev = hid_to_usb_dev(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800412 usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
413 usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT :
414 HID_REQ_GET_REPORT;
415 usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) |
416 report->id);
417 usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum);
418 usbhid->cr->wLength = cpu_to_le16(len);
419
420 dbg_hid("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u\n",
421 usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" :
422 "Get_Report",
423 usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength);
424
425 r = usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC);
426 if (r < 0) {
427 hid_err(hid, "usb_submit_urb(ctrl) failed: %d\n", r);
428 return r;
429 }
430 usbhid->last_ctrl = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return 0;
432}
433
434/*
435 * Output interrupt completion handler.
436 */
437
David Howells7d12e782006-10-05 14:55:46 +0100438static void hid_irq_out(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
440 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100441 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 unsigned long flags;
443 int unplug = 0;
444
445 switch (urb->status) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200446 case 0: /* success */
447 break;
448 case -ESHUTDOWN: /* unplug */
449 unplug = 1;
450 case -EILSEQ: /* protocol error or unplug */
451 case -EPROTO: /* protocol error or unplug */
452 case -ECONNRESET: /* unlink */
453 case -ENOENT:
454 break;
455 default: /* error */
Joe Perches4291ee32010-12-09 19:29:03 -0800456 hid_warn(urb->dev, "output irq status %d received\n",
457 urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459
Oliver Neukum0361a282008-12-17 15:38:03 +0100460 spin_lock_irqsave(&usbhid->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Alan Stern93101af2012-07-19 16:08:39 -0400462 if (unplug) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100463 usbhid->outtail = usbhid->outhead;
Alan Stern93101af2012-07-19 16:08:39 -0400464 } else {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100465 usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Alan Stern93101af2012-07-19 16:08:39 -0400467 if (usbhid->outhead != usbhid->outtail &&
468 hid_submit_out(hid) == 0) {
469 /* Successfully submitted next urb in queue */
470 spin_unlock_irqrestore(&usbhid->lock, flags);
471 return;
472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100475 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +0100476 spin_unlock_irqrestore(&usbhid->lock, flags);
Oliver Neukum68229682010-12-22 15:33:40 +0100477 usb_autopm_put_interface_async(usbhid->intf);
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100478 wake_up(&usbhid->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
481/*
482 * Control pipe completion handler.
483 */
484
David Howells7d12e782006-10-05 14:55:46 +0100485static void hid_ctrl(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100488 struct usbhid_device *usbhid = hid->driver_data;
Oliver Neukum0361a282008-12-17 15:38:03 +0100489 int unplug = 0, status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Oliver Neukum0361a282008-12-17 15:38:03 +0100491 spin_lock(&usbhid->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Oliver Neukum0361a282008-12-17 15:38:03 +0100493 switch (status) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200494 case 0: /* success */
495 if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
496 hid_input_report(urb->context,
497 usbhid->ctrl[usbhid->ctrltail].report->type,
498 urb->transfer_buffer, urb->actual_length, 0);
499 break;
500 case -ESHUTDOWN: /* unplug */
501 unplug = 1;
502 case -EILSEQ: /* protocol error or unplug */
503 case -EPROTO: /* protocol error or unplug */
504 case -ECONNRESET: /* unlink */
505 case -ENOENT:
506 case -EPIPE: /* report not available */
507 break;
508 default: /* error */
Joe Perches4291ee32010-12-09 19:29:03 -0800509 hid_warn(urb->dev, "ctrl urb status %d received\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511
Alan Stern93101af2012-07-19 16:08:39 -0400512 if (unplug) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100513 usbhid->ctrltail = usbhid->ctrlhead;
Alan Stern93101af2012-07-19 16:08:39 -0400514 } else {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100515 usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Alan Stern93101af2012-07-19 16:08:39 -0400517 if (usbhid->ctrlhead != usbhid->ctrltail &&
518 hid_submit_ctrl(hid) == 0) {
519 /* Successfully submitted next urb in queue */
520 spin_unlock(&usbhid->lock);
521 return;
522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
524
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100525 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +0100526 spin_unlock(&usbhid->lock);
Oliver Neukum68229682010-12-22 15:33:40 +0100527 usb_autopm_put_interface_async(usbhid->intf);
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100528 wake_up(&usbhid->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
H Hartley Sweeten52cfc61b2009-08-17 15:37:18 -0700531static void __usbhid_submit_report(struct hid_device *hid, struct hid_report *report,
532 unsigned char dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 int head;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100535 struct usbhid_device *usbhid = hid->driver_data;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200536 int len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
539 return;
540
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100541 if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100542 if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
Joe Perches4291ee32010-12-09 19:29:03 -0800543 hid_warn(hid, "output queue full\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return;
545 }
546
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200547 usbhid->out[usbhid->outhead].raw_report = kmalloc(len, GFP_ATOMIC);
548 if (!usbhid->out[usbhid->outhead].raw_report) {
Joe Perches4291ee32010-12-09 19:29:03 -0800549 hid_warn(hid, "output queueing failed\n");
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200550 return;
551 }
552 hid_output_report(report, usbhid->out[usbhid->outhead].raw_report);
553 usbhid->out[usbhid->outhead].report = report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100554 usbhid->outhead = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Alan Stern01a7c982012-07-19 16:08:31 -0400556 /* If the queue isn't running, restart it */
557 if (!test_bit(HID_OUT_RUNNING, &usbhid->iofl)) {
558 usbhid_restart_out_queue(usbhid);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800559
Alan Stern01a7c982012-07-19 16:08:31 -0400560 /* Otherwise see if an earlier request has timed out */
561 } else if (time_after(jiffies, usbhid->last_out + HZ * 5)) {
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800562
Alan Stern01a7c982012-07-19 16:08:31 -0400563 /* Prevent autosuspend following the unlink */
564 usb_autopm_get_interface_no_resume(usbhid->intf);
565
Oliver Neukum858155f2010-02-12 13:02:28 +0100566 /*
Alan Stern01a7c982012-07-19 16:08:31 -0400567 * Prevent resubmission in case the URB completes
568 * before we can unlink it. We don't want to cancel
569 * the wrong transfer!
Oliver Neukum858155f2010-02-12 13:02:28 +0100570 */
Alan Stern01a7c982012-07-19 16:08:31 -0400571 usb_block_urb(usbhid->urbout);
Oliver Neukum8815bb02012-04-30 09:13:46 +0200572
Alan Stern01a7c982012-07-19 16:08:31 -0400573 /* Drop lock to avoid deadlock if the callback runs */
574 spin_unlock(&usbhid->lock);
Oliver Neukum8815bb02012-04-30 09:13:46 +0200575
Alan Stern01a7c982012-07-19 16:08:31 -0400576 usb_unlink_urb(usbhid->urbout);
577 spin_lock(&usbhid->lock);
578 usb_unblock_urb(usbhid->urbout);
579
580 /* Unlink might have stopped the queue */
581 if (!test_bit(HID_OUT_RUNNING, &usbhid->iofl))
582 usbhid_restart_out_queue(usbhid);
583
584 /* Now we can allow autosuspend again */
585 usb_autopm_put_interface_async(usbhid->intf);
Oliver Neukum858155f2010-02-12 13:02:28 +0100586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return;
588 }
589
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100590 if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
Joe Perches4291ee32010-12-09 19:29:03 -0800591 hid_warn(hid, "control queue full\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return;
593 }
594
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200595 if (dir == USB_DIR_OUT) {
596 usbhid->ctrl[usbhid->ctrlhead].raw_report = kmalloc(len, GFP_ATOMIC);
597 if (!usbhid->ctrl[usbhid->ctrlhead].raw_report) {
Joe Perches4291ee32010-12-09 19:29:03 -0800598 hid_warn(hid, "control queueing failed\n");
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200599 return;
600 }
601 hid_output_report(report, usbhid->ctrl[usbhid->ctrlhead].raw_report);
602 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100603 usbhid->ctrl[usbhid->ctrlhead].report = report;
604 usbhid->ctrl[usbhid->ctrlhead].dir = dir;
605 usbhid->ctrlhead = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Alan Stern01a7c982012-07-19 16:08:31 -0400607 /* If the queue isn't running, restart it */
608 if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl)) {
609 usbhid_restart_ctrl_queue(usbhid);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800610
Alan Stern01a7c982012-07-19 16:08:31 -0400611 /* Otherwise see if an earlier request has timed out */
612 } else if (time_after(jiffies, usbhid->last_ctrl + HZ * 5)) {
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800613
Alan Stern01a7c982012-07-19 16:08:31 -0400614 /* Prevent autosuspend following the unlink */
615 usb_autopm_get_interface_no_resume(usbhid->intf);
616
Oliver Neukum858155f2010-02-12 13:02:28 +0100617 /*
Alan Stern01a7c982012-07-19 16:08:31 -0400618 * Prevent resubmission in case the URB completes
619 * before we can unlink it. We don't want to cancel
620 * the wrong transfer!
Oliver Neukum858155f2010-02-12 13:02:28 +0100621 */
Alan Stern01a7c982012-07-19 16:08:31 -0400622 usb_block_urb(usbhid->urbctrl);
623
624 /* Drop lock to avoid deadlock if the callback runs */
625 spin_unlock(&usbhid->lock);
626
627 usb_unlink_urb(usbhid->urbctrl);
628 spin_lock(&usbhid->lock);
629 usb_unblock_urb(usbhid->urbctrl);
630
631 /* Unlink might have stopped the queue */
632 if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
633 usbhid_restart_ctrl_queue(usbhid);
634
635 /* Now we can allow autosuspend again */
636 usb_autopm_put_interface_async(usbhid->intf);
Oliver Neukum858155f2010-02-12 13:02:28 +0100637 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100638}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Oliver Neukum0361a282008-12-17 15:38:03 +0100640void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
641{
642 struct usbhid_device *usbhid = hid->driver_data;
643 unsigned long flags;
644
645 spin_lock_irqsave(&usbhid->lock, flags);
646 __usbhid_submit_report(hid, report, dir);
647 spin_unlock_irqrestore(&usbhid->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
Jiri Slaby606bd0a2008-07-04 23:06:45 +0200649EXPORT_SYMBOL_GPL(usbhid_submit_report);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Daniel Kurtz4371ea82011-11-17 19:23:50 +0800651/* Workqueue routine to send requests to change LEDs */
652static void hid_led(struct work_struct *work)
653{
654 struct usbhid_device *usbhid =
655 container_of(work, struct usbhid_device, led_work);
656 struct hid_device *hid = usbhid->hid;
657 struct hid_field *field;
658 unsigned long flags;
659
660 field = hidinput_get_led_field(hid);
661 if (!field) {
662 hid_warn(hid, "LED event field not found\n");
663 return;
664 }
665
666 spin_lock_irqsave(&usbhid->lock, flags);
667 if (!test_bit(HID_DISCONNECTED, &usbhid->iofl)) {
668 usbhid->ledcount = hidinput_count_leds(hid);
669 hid_dbg(usbhid->hid, "New ledcount = %u\n", usbhid->ledcount);
670 __usbhid_submit_report(hid, field->report, USB_DIR_OUT);
671 }
672 spin_unlock_irqrestore(&usbhid->lock, flags);
673}
674
Jiri Kosina229695e2006-12-08 18:40:53 +0100675static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
Jiri Kosinadde58452006-12-08 18:40:44 +0100676{
Dmitry Torokhove0712982007-05-09 10:17:31 +0200677 struct hid_device *hid = input_get_drvdata(dev);
Oliver Neukum0361a282008-12-17 15:38:03 +0100678 struct usbhid_device *usbhid = hid->driver_data;
Jiri Kosinadde58452006-12-08 18:40:44 +0100679 struct hid_field *field;
Oliver Neukum0361a282008-12-17 15:38:03 +0100680 unsigned long flags;
Jiri Kosinadde58452006-12-08 18:40:44 +0100681 int offset;
682
683 if (type == EV_FF)
684 return input_ff_event(dev, type, code, value);
685
686 if (type != EV_LED)
687 return -1;
688
689 if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
Joe Perches4291ee32010-12-09 19:29:03 -0800690 hid_warn(dev, "event field not found\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100691 return -1;
692 }
693
Daniel Kurtz4371ea82011-11-17 19:23:50 +0800694 spin_lock_irqsave(&usbhid->lock, flags);
Jiri Kosinadde58452006-12-08 18:40:44 +0100695 hid_set_field(field, offset, value);
Daniel Kurtz4371ea82011-11-17 19:23:50 +0800696 spin_unlock_irqrestore(&usbhid->lock, flags);
697
698 /*
699 * Defer performing requested LED action.
700 * This is more likely gather all LED changes into a single URB.
701 */
702 schedule_work(&usbhid->led_work);
Jiri Kosinadde58452006-12-08 18:40:44 +0100703
704 return 0;
705}
706
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100707int usbhid_wait_io(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100709 struct usbhid_device *usbhid = hid->driver_data;
710
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100711 if (!wait_event_timeout(usbhid->wait,
712 (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
713 !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 10*HZ)) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200715 dbg_hid("timeout waiting for ctrl or out queue to clear\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return -1;
717 }
718
719 return 0;
720}
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200721EXPORT_SYMBOL_GPL(usbhid_wait_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Vojtech Pavlik854561b2005-05-29 02:28:00 -0500723static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
724{
Vojtech Pavlik71387bd2005-05-29 02:28:14 -0500725 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
Vojtech Pavlik854561b2005-05-29 02:28:00 -0500726 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
727 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
728}
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
731 unsigned char type, void *buf, int size)
732{
733 int result, retries = 4;
734
Jiri Kosina43c7bf02007-01-26 12:58:24 +0100735 memset(buf, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 do {
738 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
739 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
740 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
741 retries--;
742 } while (result < size && retries);
743 return result;
744}
745
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100746int usbhid_open(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Oliver Neukum933e3182007-07-11 14:48:58 +0200748 struct usbhid_device *usbhid = hid->driver_data;
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200749 int res = 0;
Oliver Neukum933e3182007-07-11 14:48:58 +0200750
Oliver Neukum0361a282008-12-17 15:38:03 +0100751 mutex_lock(&hid_open_mut);
Oliver Neukum933e3182007-07-11 14:48:58 +0200752 if (!hid->open++) {
753 res = usb_autopm_get_interface(usbhid->intf);
Oliver Neukum68229682010-12-22 15:33:40 +0100754 /* the device must be awake to reliably request remote wakeup */
Oliver Neukum933e3182007-07-11 14:48:58 +0200755 if (res < 0) {
756 hid->open--;
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200757 res = -EIO;
758 goto done;
Oliver Neukum933e3182007-07-11 14:48:58 +0200759 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100760 usbhid->intf->needs_remote_wakeup = 1;
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200761 res = hid_start_in(hid);
762 if (res) {
763 if (res != -ENOSPC) {
764 hid_io_error(hid);
765 res = 0;
766 } else {
767 /* no use opening if resources are insufficient */
768 hid->open--;
769 res = -EBUSY;
770 usbhid->intf->needs_remote_wakeup = 0;
771 }
772 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100773 usb_autopm_put_interface(usbhid->intf);
Oliver Neukum933e3182007-07-11 14:48:58 +0200774 }
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200775done:
Oliver Neukum0361a282008-12-17 15:38:03 +0100776 mutex_unlock(&hid_open_mut);
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200777 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100780void usbhid_close(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100782 struct usbhid_device *usbhid = hid->driver_data;
783
Oliver Neukum0361a282008-12-17 15:38:03 +0100784 mutex_lock(&hid_open_mut);
785
786 /* protecting hid->open to make sure we don't restart
787 * data acquistion due to a resumption we no longer
788 * care about
789 */
790 spin_lock_irq(&usbhid->lock);
Oliver Neukum933e3182007-07-11 14:48:58 +0200791 if (!--hid->open) {
Oliver Neukum0361a282008-12-17 15:38:03 +0100792 spin_unlock_irq(&usbhid->lock);
Oliver Neukum89092dd2009-04-29 17:12:12 +0200793 hid_cancel_delayed_stuff(usbhid);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100794 usb_kill_urb(usbhid->urbin);
Oliver Neukum0361a282008-12-17 15:38:03 +0100795 usbhid->intf->needs_remote_wakeup = 0;
796 } else {
797 spin_unlock_irq(&usbhid->lock);
Oliver Neukum933e3182007-07-11 14:48:58 +0200798 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100799 mutex_unlock(&hid_open_mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
802/*
803 * Initialize all reports
804 */
805
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100806void usbhid_init_reports(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 struct hid_report *report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100809 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 int err, ret;
811
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500812 list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100813 usbhid_submit_report(hid, report, USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100816 usbhid_submit_report(hid, report, USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 err = 0;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100819 ret = usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 while (ret) {
821 err |= ret;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100822 if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
823 usb_kill_urb(usbhid->urbctrl);
824 if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
825 usb_kill_urb(usbhid->urbout);
826 ret = usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828
829 if (err)
Joe Perches4291ee32010-12-09 19:29:03 -0800830 hid_warn(hid, "timeout initializing reports\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500833/*
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200834 * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
835 */
836static int hid_find_field_early(struct hid_device *hid, unsigned int page,
837 unsigned int hid_code, struct hid_field **pfield)
838{
839 struct hid_report *report;
840 struct hid_field *field;
841 struct hid_usage *usage;
842 int i, j;
843
844 list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
845 for (i = 0; i < report->maxfield; i++) {
846 field = report->field[i];
847 for (j = 0; j < field->maxusage; j++) {
848 usage = &field->usage[j];
849 if ((usage->hid & HID_USAGE_PAGE) == page &&
850 (usage->hid & 0xFFFF) == hid_code) {
851 *pfield = field;
852 return j;
853 }
854 }
855 }
856 }
857 return -1;
858}
859
Jiri Slaby6edfa8d2008-06-27 20:41:02 +0200860void usbhid_set_leds(struct hid_device *hid)
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200861{
862 struct hid_field *field;
863 int offset;
864
865 if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
866 hid_set_field(field, offset, 0);
867 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
868 }
869}
Jiri Slaby6edfa8d2008-06-27 20:41:02 +0200870EXPORT_SYMBOL_GPL(usbhid_set_leds);
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200871
872/*
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500873 * Traverse the supplied list of reports and find the longest
874 */
Jiri Slaby282bfd42008-03-28 17:06:41 +0100875static void hid_find_max_report(struct hid_device *hid, unsigned int type,
876 unsigned int *max)
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500877{
878 struct hid_report *report;
Jiri Slaby282bfd42008-03-28 17:06:41 +0100879 unsigned int size;
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500880
881 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
Jiri Kosinaefc7ce12008-10-17 15:01:15 +0200882 size = ((report->size - 1) >> 3) + 1 + hid->report_enum[type].numbered;
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500883 if (*max < size)
884 *max = size;
885 }
886}
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
889{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100890 struct usbhid_device *usbhid = hid->driver_data;
891
Daniel Mack997ea582010-04-12 13:17:25 +0200892 usbhid->inbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
Jiri Slaby898089d2008-11-24 16:20:06 +0100893 &usbhid->inbuf_dma);
Daniel Mack997ea582010-04-12 13:17:25 +0200894 usbhid->outbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
Jiri Slaby898089d2008-11-24 16:20:06 +0100895 &usbhid->outbuf_dma);
Alan Stern0ede76f2010-03-05 15:10:17 -0500896 usbhid->cr = kmalloc(sizeof(*usbhid->cr), GFP_KERNEL);
Daniel Mack997ea582010-04-12 13:17:25 +0200897 usbhid->ctrlbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
Jiri Slaby898089d2008-11-24 16:20:06 +0100898 &usbhid->ctrlbuf_dma);
899 if (!usbhid->inbuf || !usbhid->outbuf || !usbhid->cr ||
900 !usbhid->ctrlbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return -1;
902
903 return 0;
904}
905
Alan Ottb4dbde92011-01-18 03:04:39 -0500906static int usbhid_get_raw_report(struct hid_device *hid,
907 unsigned char report_number, __u8 *buf, size_t count,
908 unsigned char report_type)
909{
910 struct usbhid_device *usbhid = hid->driver_data;
911 struct usb_device *dev = hid_to_usb_dev(hid);
912 struct usb_interface *intf = usbhid->intf;
913 struct usb_host_interface *interface = intf->cur_altsetting;
914 int skipped_report_id = 0;
915 int ret;
916
917 /* Byte 0 is the report number. Report data starts at byte 1.*/
918 buf[0] = report_number;
919 if (report_number == 0x0) {
920 /* Offset the return buffer by 1, so that the report ID
921 will remain in byte 0. */
922 buf++;
923 count--;
924 skipped_report_id = 1;
925 }
926 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
927 HID_REQ_GET_REPORT,
928 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
929 ((report_type + 1) << 8) | report_number,
930 interface->desc.bInterfaceNumber, buf, count,
931 USB_CTRL_SET_TIMEOUT);
932
933 /* count also the report id */
934 if (ret > 0 && skipped_report_id)
935 ret++;
936
937 return ret;
938}
939
Jiri Kosinad4bfa032010-01-29 15:03:36 +0100940static int usbhid_output_raw_report(struct hid_device *hid, __u8 *buf, size_t count,
941 unsigned char report_type)
Jiri Kosinaefc493f2007-05-14 09:54:30 +0200942{
943 struct usbhid_device *usbhid = hid->driver_data;
944 struct usb_device *dev = hid_to_usb_dev(hid);
945 struct usb_interface *intf = usbhid->intf;
946 struct usb_host_interface *interface = intf->cur_altsetting;
947 int ret;
948
Alan Ottfe2c91e2010-09-22 13:19:42 +0200949 if (usbhid->urbout && report_type != HID_FEATURE_REPORT) {
Alan Otta8ab5d52010-05-16 18:07:09 -0400950 int actual_length;
951 int skipped_report_id = 0;
Alan Ott12e52722010-09-22 13:33:20 +0200952
Alan Otta8ab5d52010-05-16 18:07:09 -0400953 if (buf[0] == 0x0) {
954 /* Don't send the Report ID */
955 buf++;
956 count--;
957 skipped_report_id = 1;
958 }
959 ret = usb_interrupt_msg(dev, usbhid->urbout->pipe,
960 buf, count, &actual_length,
961 USB_CTRL_SET_TIMEOUT);
962 /* return the number of bytes transferred */
963 if (ret == 0) {
964 ret = actual_length;
965 /* count also the report id */
966 if (skipped_report_id)
967 ret++;
968 }
969 } else {
Alan Ott29129a92010-06-30 09:50:36 -0400970 int skipped_report_id = 0;
Alan Ottc29771c2010-08-17 00:44:04 -0400971 int report_id = buf[0];
Alan Ott29129a92010-06-30 09:50:36 -0400972 if (buf[0] == 0x0) {
973 /* Don't send the Report ID */
974 buf++;
975 count--;
976 skipped_report_id = 1;
977 }
Alan Otta8ab5d52010-05-16 18:07:09 -0400978 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
979 HID_REQ_SET_REPORT,
980 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
Alan Ottc29771c2010-08-17 00:44:04 -0400981 ((report_type + 1) << 8) | report_id,
Alan Ott29129a92010-06-30 09:50:36 -0400982 interface->desc.bInterfaceNumber, buf, count,
Alan Otta8ab5d52010-05-16 18:07:09 -0400983 USB_CTRL_SET_TIMEOUT);
Alan Ott29129a92010-06-30 09:50:36 -0400984 /* count also the report id, if this was a numbered report. */
985 if (ret > 0 && skipped_report_id)
Alan Otta8ab5d52010-05-16 18:07:09 -0400986 ret++;
987 }
Jiri Kosinaefc493f2007-05-14 09:54:30 +0200988
989 return ret;
990}
991
Oliver Neukum0361a282008-12-17 15:38:03 +0100992static void usbhid_restart_queues(struct usbhid_device *usbhid)
993{
994 if (usbhid->urbout)
995 usbhid_restart_out_queue(usbhid);
996 usbhid_restart_ctrl_queue(usbhid);
997}
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
1000{
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001001 struct usbhid_device *usbhid = hid->driver_data;
1002
Daniel Mack997ea582010-04-12 13:17:25 +02001003 usb_free_coherent(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
1004 usb_free_coherent(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
Alan Stern0ede76f2010-03-05 15:10:17 -05001005 kfree(usbhid->cr);
Daniel Mack997ea582010-04-12 13:17:25 +02001006 usb_free_coherent(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
Jiri Slabyc500c972008-05-16 11:49:16 +02001009static int usbhid_parse(struct hid_device *hid)
1010{
1011 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 struct usb_host_interface *interface = intf->cur_altsetting;
1013 struct usb_device *dev = interface_to_usbdev (intf);
1014 struct hid_descriptor *hdesc;
Paul Walmsley2eb5dc32007-04-19 13:27:04 +02001015 u32 quirks = 0;
Jiri Slabyc500c972008-05-16 11:49:16 +02001016 unsigned int rsize = 0;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001017 char *rdesc;
Jiri Slabyc500c972008-05-16 11:49:16 +02001018 int ret, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Paul Walmsley2eb5dc32007-04-19 13:27:04 +02001020 quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
1021 le16_to_cpu(dev->descriptor.idProduct));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Jiri Kosina6f4303f2009-01-29 00:15:51 +01001023 if (quirks & HID_QUIRK_IGNORE)
1024 return -ENODEV;
1025
Alan Stern0f28b552006-05-15 14:49:04 -04001026 /* Many keyboards and mice don't like to be polled for reports,
1027 * so we will always set the HID_QUIRK_NOGET flag for them. */
1028 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
1029 if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
1030 interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
1031 quirks |= HID_QUIRK_NOGET;
1032 }
1033
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001034 if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
1035 (!interface->desc.bNumEndpoints ||
1036 usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
Jiri Kosina58037eb2007-05-30 15:07:13 +02001037 dbg_hid("class descriptor not present\n");
Jiri Slabyc500c972008-05-16 11:49:16 +02001038 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 }
1040
Jiri Slabyc500c972008-05-16 11:49:16 +02001041 hid->version = le16_to_cpu(hdesc->bcdHID);
1042 hid->country = hdesc->bCountryCode;
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 for (n = 0; n < hdesc->bNumDescriptors; n++)
1045 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
1046 rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
1047
1048 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
Jiri Kosina58037eb2007-05-30 15:07:13 +02001049 dbg_hid("weird size of report descriptor (%u)\n", rsize);
Jiri Slabyc500c972008-05-16 11:49:16 +02001050 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
1052
1053 if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
Jiri Kosina58037eb2007-05-30 15:07:13 +02001054 dbg_hid("couldn't allocate rdesc memory\n");
Jiri Slabyc500c972008-05-16 11:49:16 +02001055 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
1057
Vojtech Pavlik854561b2005-05-29 02:28:00 -05001058 hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
1059
Jiri Slabyc500c972008-05-16 11:49:16 +02001060 ret = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber,
1061 HID_DT_REPORT, rdesc, rsize);
1062 if (ret < 0) {
Jiri Kosina58037eb2007-05-30 15:07:13 +02001063 dbg_hid("reading report descriptor failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 kfree(rdesc);
Jiri Slabyc500c972008-05-16 11:49:16 +02001065 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
1067
Jiri Slabyc500c972008-05-16 11:49:16 +02001068 ret = hid_parse_report(hid, rdesc, rsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 kfree(rdesc);
Jiri Slabyc500c972008-05-16 11:49:16 +02001070 if (ret) {
1071 dbg_hid("parsing report descriptor failed\n");
1072 goto err;
1073 }
1074
Zoltan Karcagif5208992009-05-06 16:30:21 +02001075 hid->quirks |= quirks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Jiri Slabyc500c972008-05-16 11:49:16 +02001077 return 0;
1078err:
1079 return ret;
1080}
1081
1082static int usbhid_start(struct hid_device *hid)
1083{
1084 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
1085 struct usb_host_interface *interface = intf->cur_altsetting;
1086 struct usb_device *dev = interface_to_usbdev(intf);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001087 struct usbhid_device *usbhid = hid->driver_data;
Jiri Slabyc500c972008-05-16 11:49:16 +02001088 unsigned int n, insize = 0;
1089 int ret;
1090
Jiri Slabye3e14de2008-11-01 23:41:46 +01001091 clear_bit(HID_DISCONNECTED, &usbhid->iofl);
1092
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001093 usbhid->bufsize = HID_MIN_BUFFER_SIZE;
1094 hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
1095 hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
1096 hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
1097
1098 if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
1099 usbhid->bufsize = HID_MAX_BUFFER_SIZE;
Michael Haboustakbf0964d2005-09-05 00:12:01 -05001100
1101 hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
1102
1103 if (insize > HID_MAX_BUFFER_SIZE)
1104 insize = HID_MAX_BUFFER_SIZE;
1105
Jiri Slabyc500c972008-05-16 11:49:16 +02001106 if (hid_alloc_buffers(dev, hid)) {
1107 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 goto fail;
Pekka Sarnilaf345c372008-03-06 13:23:14 +01001109 }
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 for (n = 0; n < interface->desc.bNumEndpoints; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 struct usb_endpoint_descriptor *endpoint;
1113 int pipe;
1114 int interval;
1115
1116 endpoint = &interface->endpoint[n].desc;
Jiri Slaby581a2732008-11-24 16:20:08 +01001117 if (!usb_endpoint_xfer_int(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 continue;
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 interval = endpoint->bInterval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Pekka Sarnilaf345c372008-03-06 13:23:14 +01001122 /* Some vendors give fullspeed interval on highspeed devides */
Jiri Slabyc500c972008-05-16 11:49:16 +02001123 if (hid->quirks & HID_QUIRK_FULLSPEED_INTERVAL &&
Pekka Sarnilaf345c372008-03-06 13:23:14 +01001124 dev->speed == USB_SPEED_HIGH) {
1125 interval = fls(endpoint->bInterval*8);
1126 printk(KERN_INFO "%s: Fixing fullspeed to highspeed interval: %d -> %d\n",
1127 hid->name, endpoint->bInterval, interval);
1128 }
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 /* Change the polling interval of mice. */
1131 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
1132 interval = hid_mousepoll_interval;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -05001133
Jiri Slabyc500c972008-05-16 11:49:16 +02001134 ret = -ENOMEM;
Luiz Fernando N. Capitulino0f12aa02006-10-26 13:02:51 -03001135 if (usb_endpoint_dir_in(endpoint)) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001136 if (usbhid->urbin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 continue;
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001138 if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 goto fail;
1140 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001141 usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 hid_irq_in, hid, interval);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001143 usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
1144 usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 } else {
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001146 if (usbhid->urbout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 continue;
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001148 if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 goto fail;
1150 pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001151 usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 hid_irq_out, hid, interval);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001153 usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
1154 usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
1156 }
1157
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001158 usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
Jiri Slabyc500c972008-05-16 11:49:16 +02001159 if (!usbhid->urbctrl) {
1160 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 goto fail;
Jiri Slabyc500c972008-05-16 11:49:16 +02001162 }
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001163
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001164 usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
1165 usbhid->ctrlbuf, 1, hid_ctrl, hid);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001166 usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
Alan Stern0ede76f2010-03-05 15:10:17 -05001167 usbhid->urbctrl->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Jiri Slabyc500c972008-05-16 11:49:16 +02001168
Jiri Kosina5b915d92009-11-05 14:08:03 +01001169 if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
1170 usbhid_init_reports(hid);
Jiri Slaby93c10132008-06-27 00:04:24 +02001171
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001172 set_bit(HID_STARTED, &usbhid->iofl);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001173
Alan Stern08ef08e2008-10-30 23:58:51 +01001174 /* Some keyboards don't work until their LEDs have been set.
1175 * Since BIOSes do set the LEDs, it must be safe for any device
1176 * that supports the keyboard boot protocol.
Alan Stern3d615102010-04-02 13:21:58 -04001177 * In addition, enable remote wakeup by default for all keyboard
1178 * devices supporting the boot protocol.
Alan Stern08ef08e2008-10-30 23:58:51 +01001179 */
1180 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT &&
1181 interface->desc.bInterfaceProtocol ==
Alan Stern3d615102010-04-02 13:21:58 -04001182 USB_INTERFACE_PROTOCOL_KEYBOARD) {
Alan Stern08ef08e2008-10-30 23:58:51 +01001183 usbhid_set_leds(hid);
Alan Stern3d615102010-04-02 13:21:58 -04001184 device_set_wakeup_enable(&dev->dev, 1);
1185 }
Jiri Slabyc500c972008-05-16 11:49:16 +02001186 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188fail:
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001189 usb_free_urb(usbhid->urbin);
1190 usb_free_urb(usbhid->urbout);
1191 usb_free_urb(usbhid->urbctrl);
Jiri Slabye3e14de2008-11-01 23:41:46 +01001192 usbhid->urbin = NULL;
1193 usbhid->urbout = NULL;
1194 usbhid->urbctrl = NULL;
Jiri Kosina22f675f2007-08-01 12:32:27 +02001195 hid_free_buffers(dev, hid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001196 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
Jiri Slabyc500c972008-05-16 11:49:16 +02001199static void usbhid_stop(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Jiri Slabyc500c972008-05-16 11:49:16 +02001201 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Jiri Slabyc500c972008-05-16 11:49:16 +02001203 if (WARN_ON(!usbhid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 return;
1205
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001206 clear_bit(HID_STARTED, &usbhid->iofl);
Daniel Kurtz4371ea82011-11-17 19:23:50 +08001207 spin_lock_irq(&usbhid->lock); /* Sync with error and led handlers */
Oliver Neukum69626f22008-03-31 16:27:30 +02001208 set_bit(HID_DISCONNECTED, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +01001209 spin_unlock_irq(&usbhid->lock);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001210 usb_kill_urb(usbhid->urbin);
1211 usb_kill_urb(usbhid->urbout);
1212 usb_kill_urb(usbhid->urbctrl);
1213
Oliver Neukum0361a282008-12-17 15:38:03 +01001214 hid_cancel_delayed_stuff(usbhid);
Alan Sternaef4e262006-01-31 12:58:38 -05001215
Jiri Slabyc500c972008-05-16 11:49:16 +02001216 hid->claimed = 0;
1217
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001218 usb_free_urb(usbhid->urbin);
1219 usb_free_urb(usbhid->urbctrl);
1220 usb_free_urb(usbhid->urbout);
Jiri Slabye3e14de2008-11-01 23:41:46 +01001221 usbhid->urbin = NULL; /* don't mess up next start */
1222 usbhid->urbctrl = NULL;
1223 usbhid->urbout = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Anssi Hannulabe820972007-01-19 19:28:17 +02001225 hid_free_buffers(hid_to_usb_dev(hid), hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226}
1227
Oliver Neukum0361a282008-12-17 15:38:03 +01001228static int usbhid_power(struct hid_device *hid, int lvl)
1229{
1230 int r = 0;
1231
1232 switch (lvl) {
1233 case PM_HINT_FULLON:
1234 r = usbhid_get_power(hid);
1235 break;
1236 case PM_HINT_NORMAL:
1237 usbhid_put_power(hid);
1238 break;
1239 }
1240 return r;
1241}
1242
Jiri Slabyc500c972008-05-16 11:49:16 +02001243static struct hid_ll_driver usb_hid_driver = {
1244 .parse = usbhid_parse,
1245 .start = usbhid_start,
1246 .stop = usbhid_stop,
1247 .open = usbhid_open,
1248 .close = usbhid_close,
Oliver Neukum0361a282008-12-17 15:38:03 +01001249 .power = usbhid_power,
Jiri Slabyc500c972008-05-16 11:49:16 +02001250 .hidinput_input_event = usb_hidinput_input_event,
1251};
1252
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001253static int usbhid_probe(struct usb_interface *intf, const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
Jiri Slaby131d3a7a2008-11-14 12:03:47 +01001255 struct usb_host_interface *interface = intf->cur_altsetting;
Jiri Slabyc500c972008-05-16 11:49:16 +02001256 struct usb_device *dev = interface_to_usbdev(intf);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001257 struct usbhid_device *usbhid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 struct hid_device *hid;
Jiri Slaby131d3a7a2008-11-14 12:03:47 +01001259 unsigned int n, has_in = 0;
Jiri Slabyc500c972008-05-16 11:49:16 +02001260 size_t len;
1261 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Jiri Kosina58037eb2007-05-30 15:07:13 +02001263 dbg_hid("HID probe called for ifnum %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 intf->altsetting->desc.bInterfaceNumber);
1265
Jiri Slaby131d3a7a2008-11-14 12:03:47 +01001266 for (n = 0; n < interface->desc.bNumEndpoints; n++)
1267 if (usb_endpoint_is_int_in(&interface->endpoint[n].desc))
1268 has_in++;
1269 if (!has_in) {
Joe Perches4291ee32010-12-09 19:29:03 -08001270 hid_err(intf, "couldn't find an input interrupt endpoint\n");
Jiri Slaby131d3a7a2008-11-14 12:03:47 +01001271 return -ENODEV;
1272 }
1273
Jiri Slabyc500c972008-05-16 11:49:16 +02001274 hid = hid_allocate_device();
1275 if (IS_ERR(hid))
1276 return PTR_ERR(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 usb_set_intfdata(intf, hid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001279 hid->ll_driver = &usb_hid_driver;
Alan Ottb4dbde92011-01-18 03:04:39 -05001280 hid->hid_get_raw_report = usbhid_get_raw_report;
Jiri Slabyc500c972008-05-16 11:49:16 +02001281 hid->hid_output_raw_report = usbhid_output_raw_report;
Jiri Slaby76483cf2008-09-18 12:23:33 +02001282 hid->ff_init = hid_pidff_init;
Jiri Slabyc500c972008-05-16 11:49:16 +02001283#ifdef CONFIG_USB_HIDDEV
Jiri Slaby93c10132008-06-27 00:04:24 +02001284 hid->hiddev_connect = hiddev_connect;
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001285 hid->hiddev_disconnect = hiddev_disconnect;
Jiri Slabyc500c972008-05-16 11:49:16 +02001286 hid->hiddev_hid_event = hiddev_hid_event;
1287 hid->hiddev_report_event = hiddev_report_event;
1288#endif
1289 hid->dev.parent = &intf->dev;
1290 hid->bus = BUS_USB;
1291 hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
1292 hid->product = le16_to_cpu(dev->descriptor.idProduct);
1293 hid->name[0] = 0;
Bastien Nocerab5e5a372010-04-16 17:19:50 +01001294 hid->quirks = usbhid_lookup_quirk(hid->vendor, hid->product);
Jiri Slabya73a6372008-10-22 14:45:11 +02001295 if (intf->cur_altsetting->desc.bInterfaceProtocol ==
1296 USB_INTERFACE_PROTOCOL_MOUSE)
1297 hid->type = HID_TYPE_USBMOUSE;
Tomoki Sekiyama6dc14182011-05-23 15:45:44 -07001298 else if (intf->cur_altsetting->desc.bInterfaceProtocol == 0)
1299 hid->type = HID_TYPE_USBNONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Jiri Slabyc500c972008-05-16 11:49:16 +02001301 if (dev->manufacturer)
1302 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
1303
1304 if (dev->product) {
1305 if (dev->manufacturer)
1306 strlcat(hid->name, " ", sizeof(hid->name));
1307 strlcat(hid->name, dev->product, sizeof(hid->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309
Jiri Slabyc500c972008-05-16 11:49:16 +02001310 if (!strlen(hid->name))
1311 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
1312 le16_to_cpu(dev->descriptor.idVendor),
1313 le16_to_cpu(dev->descriptor.idProduct));
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001314
Jiri Slabyc500c972008-05-16 11:49:16 +02001315 usb_make_path(dev, hid->phys, sizeof(hid->phys));
1316 strlcat(hid->phys, "/input", sizeof(hid->phys));
1317 len = strlen(hid->phys);
1318 if (len < sizeof(hid->phys) - 1)
1319 snprintf(hid->phys + len, sizeof(hid->phys) - len,
1320 "%d", intf->altsetting[0].desc.bInterfaceNumber);
Geoff Levand4a1a4d82007-01-15 20:11:52 -08001321
Jiri Slabyc500c972008-05-16 11:49:16 +02001322 if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
1323 hid->uniq[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001325 usbhid = kzalloc(sizeof(*usbhid), GFP_KERNEL);
1326 if (usbhid == NULL) {
1327 ret = -ENOMEM;
1328 goto err;
1329 }
1330
1331 hid->driver_data = usbhid;
1332 usbhid->hid = hid;
Jiri Kosina57ab12e2010-02-17 14:25:01 +01001333 usbhid->intf = intf;
1334 usbhid->ifnum = interface->desc.bInterfaceNumber;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001335
Alan Sternfde4e2f2010-05-07 10:41:10 -04001336 init_waitqueue_head(&usbhid->wait);
1337 INIT_WORK(&usbhid->reset_work, hid_reset);
Alan Sternfde4e2f2010-05-07 10:41:10 -04001338 setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
1339 spin_lock_init(&usbhid->lock);
1340
Daniel Kurtz4371ea82011-11-17 19:23:50 +08001341 INIT_WORK(&usbhid->led_work, hid_led);
1342
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001343 ret = hid_add_device(hid);
1344 if (ret) {
Jiri Slabyd458a9d2008-05-16 11:49:20 +02001345 if (ret != -ENODEV)
Joe Perches4291ee32010-12-09 19:29:03 -08001346 hid_err(intf, "can't add hid device: %d\n", ret);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001347 goto err_free;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001348 }
Jiri Slabyc500c972008-05-16 11:49:16 +02001349
1350 return 0;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001351err_free:
1352 kfree(usbhid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001353err:
1354 hid_destroy_device(hid);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001355 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356}
1357
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001358static void usbhid_disconnect(struct usb_interface *intf)
Jiri Slabyc500c972008-05-16 11:49:16 +02001359{
1360 struct hid_device *hid = usb_get_intfdata(intf);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001361 struct usbhid_device *usbhid;
Jiri Slabyc500c972008-05-16 11:49:16 +02001362
1363 if (WARN_ON(!hid))
1364 return;
1365
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001366 usbhid = hid->driver_data;
Jiri Slabyc500c972008-05-16 11:49:16 +02001367 hid_destroy_device(hid);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001368 kfree(usbhid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001369}
1370
Oliver Neukum0361a282008-12-17 15:38:03 +01001371static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
Oliver Neukum0361a282008-12-17 15:38:03 +01001373 del_timer_sync(&usbhid->io_retry);
Oliver Neukum0361a282008-12-17 15:38:03 +01001374 cancel_work_sync(&usbhid->reset_work);
Daniel Kurtz4371ea82011-11-17 19:23:50 +08001375 cancel_work_sync(&usbhid->led_work);
Oliver Neukum0361a282008-12-17 15:38:03 +01001376}
1377
1378static void hid_cease_io(struct usbhid_device *usbhid)
1379{
Oliver Neukumfad9fbe2011-10-13 18:21:58 +02001380 del_timer_sync(&usbhid->io_retry);
Oliver Neukum0361a282008-12-17 15:38:03 +01001381 usb_kill_urb(usbhid->urbin);
1382 usb_kill_urb(usbhid->urbctrl);
1383 usb_kill_urb(usbhid->urbout);
Oliver Neukum0361a282008-12-17 15:38:03 +01001384}
1385
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001386/* Treat USB reset pretty much the same as suspend/resume */
1387static int hid_pre_reset(struct usb_interface *intf)
1388{
1389 struct hid_device *hid = usb_get_intfdata(intf);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001390 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001392 spin_lock_irq(&usbhid->lock);
1393 set_bit(HID_RESET_PENDING, &usbhid->iofl);
1394 spin_unlock_irq(&usbhid->lock);
1395 hid_cease_io(usbhid);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001396
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001397 return 0;
1398}
1399
1400/* Same routine used for post_reset and reset_resume */
1401static int hid_post_reset(struct usb_interface *intf)
1402{
1403 struct usb_device *dev = interface_to_usbdev (intf);
1404 struct hid_device *hid = usb_get_intfdata(intf);
1405 struct usbhid_device *usbhid = hid->driver_data;
Simon Haggettdc3c78e2012-04-03 16:04:15 +01001406 struct usb_host_interface *interface = intf->cur_altsetting;
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001407 int status;
Simon Haggettdc3c78e2012-04-03 16:04:15 +01001408 char *rdesc;
1409
1410 /* Fetch and examine the HID report descriptor. If this
1411 * has changed, then rebind. Since usbcore's check of the
1412 * configuration descriptors passed, we already know that
1413 * the size of the HID report descriptor has not changed.
1414 */
1415 rdesc = kmalloc(hid->rsize, GFP_KERNEL);
1416 if (!rdesc) {
1417 dbg_hid("couldn't allocate rdesc memory (post_reset)\n");
1418 return 1;
1419 }
1420 status = hid_get_class_descriptor(dev,
1421 interface->desc.bInterfaceNumber,
1422 HID_DT_REPORT, rdesc, hid->rsize);
1423 if (status < 0) {
1424 dbg_hid("reading report descriptor failed (post_reset)\n");
1425 kfree(rdesc);
1426 return 1;
1427 }
1428 status = memcmp(rdesc, hid->rdesc, hid->rsize);
1429 kfree(rdesc);
1430 if (status != 0) {
1431 dbg_hid("report descriptor changed\n");
1432 return 1;
1433 }
Jiri Kosina70fa9f22009-06-04 15:48:38 +02001434
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001435 spin_lock_irq(&usbhid->lock);
1436 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
1437 spin_unlock_irq(&usbhid->lock);
1438 hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001439 status = hid_start_in(hid);
1440 if (status < 0)
1441 hid_io_error(hid);
1442 usbhid_restart_queues(usbhid);
1443
1444 return 0;
1445}
1446
1447int usbhid_get_power(struct hid_device *hid)
1448{
1449 struct usbhid_device *usbhid = hid->driver_data;
Jiri Kosina70fa9f22009-06-04 15:48:38 +02001450
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001451 return usb_autopm_get_interface(usbhid->intf);
1452}
1453
1454void usbhid_put_power(struct hid_device *hid)
1455{
1456 struct usbhid_device *usbhid = hid->driver_data;
Jiri Kosina70fa9f22009-06-04 15:48:38 +02001457
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001458 usb_autopm_put_interface(usbhid->intf);
1459}
1460
1461
Jiri Kosina0f6f1402009-01-19 09:17:18 +01001462#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463static int hid_suspend(struct usb_interface *intf, pm_message_t message)
1464{
Oliver Neukum0361a282008-12-17 15:38:03 +01001465 struct hid_device *hid = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 struct usbhid_device *usbhid = hid->driver_data;
Oliver Neukum0361a282008-12-17 15:38:03 +01001467 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Alan Stern5b1b0b82011-08-19 23:49:48 +02001469 if (PMSG_IS_AUTO(message)) {
Oliver Neukum0361a282008-12-17 15:38:03 +01001470 spin_lock_irq(&usbhid->lock); /* Sync with error handler */
1471 if (!test_bit(HID_RESET_PENDING, &usbhid->iofl)
1472 && !test_bit(HID_CLEAR_HALT, &usbhid->iofl)
1473 && !test_bit(HID_OUT_RUNNING, &usbhid->iofl)
1474 && !test_bit(HID_CTRL_RUNNING, &usbhid->iofl)
1475 && !test_bit(HID_KEYS_PRESSED, &usbhid->iofl)
1476 && (!usbhid->ledcount || ignoreled))
1477 {
Alan Sternf2b52642012-07-19 16:08:45 -04001478 set_bit(HID_SUSPENDED, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +01001479 spin_unlock_irq(&usbhid->lock);
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001480 if (hid->driver && hid->driver->suspend) {
1481 status = hid->driver->suspend(hid, message);
1482 if (status < 0)
1483 return status;
1484 }
Oliver Neukum0361a282008-12-17 15:38:03 +01001485 } else {
1486 usbhid_mark_busy(usbhid);
1487 spin_unlock_irq(&usbhid->lock);
1488 return -EBUSY;
1489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Oliver Neukum0361a282008-12-17 15:38:03 +01001491 } else {
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001492 if (hid->driver && hid->driver->suspend) {
1493 status = hid->driver->suspend(hid, message);
1494 if (status < 0)
1495 return status;
1496 }
Oliver Neukum0361a282008-12-17 15:38:03 +01001497 spin_lock_irq(&usbhid->lock);
Alan Sternf2b52642012-07-19 16:08:45 -04001498 set_bit(HID_SUSPENDED, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +01001499 spin_unlock_irq(&usbhid->lock);
1500 if (usbhid_wait_io(hid) < 0)
1501 return -EIO;
1502 }
1503
Oliver Neukum0361a282008-12-17 15:38:03 +01001504 hid_cancel_delayed_stuff(usbhid);
1505 hid_cease_io(usbhid);
1506
Alan Stern5b1b0b82011-08-19 23:49:48 +02001507 if (PMSG_IS_AUTO(message) && test_bit(HID_KEYS_PRESSED, &usbhid->iofl)) {
Oliver Neukum0361a282008-12-17 15:38:03 +01001508 /* lost race against keypresses */
1509 status = hid_start_in(hid);
1510 if (status < 0)
1511 hid_io_error(hid);
1512 usbhid_mark_busy(usbhid);
1513 return -EBUSY;
1514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 dev_dbg(&intf->dev, "suspend\n");
1516 return 0;
1517}
1518
1519static int hid_resume(struct usb_interface *intf)
1520{
1521 struct hid_device *hid = usb_get_intfdata (intf);
1522 struct usbhid_device *usbhid = hid->driver_data;
1523 int status;
1524
Jiri Slabyfde5be32008-11-23 12:03:20 +01001525 if (!test_bit(HID_STARTED, &usbhid->iofl))
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001526 return 0;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001527
Alan Sternf2b52642012-07-19 16:08:45 -04001528 clear_bit(HID_SUSPENDED, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +01001529 usbhid_mark_busy(usbhid);
1530
1531 if (test_bit(HID_CLEAR_HALT, &usbhid->iofl) ||
1532 test_bit(HID_RESET_PENDING, &usbhid->iofl))
1533 schedule_work(&usbhid->reset_work);
Alan Sterndf9a1f42006-06-01 13:55:28 -04001534 usbhid->retry_delay = 0;
1535 status = hid_start_in(hid);
Oliver Neukum0361a282008-12-17 15:38:03 +01001536 if (status < 0)
1537 hid_io_error(hid);
1538 usbhid_restart_queues(usbhid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001540 if (status >= 0 && hid->driver && hid->driver->resume) {
1541 int ret = hid->driver->resume(hid);
1542 if (ret < 0)
1543 status = ret;
1544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 dev_dbg(&intf->dev, "resume status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 return 0;
1547}
1548
Oliver Neukum378a0ed2009-02-18 11:46:45 +01001549static int hid_reset_resume(struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
Oliver Neukum378a0ed2009-02-18 11:46:45 +01001551 struct hid_device *hid = usb_get_intfdata(intf);
1552 struct usbhid_device *usbhid = hid->driver_data;
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001553 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Alan Sternf2b52642012-07-19 16:08:45 -04001555 clear_bit(HID_SUSPENDED, &usbhid->iofl);
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001556 status = hid_post_reset(intf);
1557 if (status >= 0 && hid->driver && hid->driver->reset_resume) {
1558 int ret = hid->driver->reset_resume(hid);
1559 if (ret < 0)
1560 status = ret;
1561 }
1562 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001565#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Márton Némethd67dec52010-01-10 17:59:22 +01001567static const struct usb_device_id hid_usb_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1569 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
1570 { } /* Terminating entry */
1571};
1572
1573MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1574
1575static struct usb_driver hid_driver = {
1576 .name = "usbhid",
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001577 .probe = usbhid_probe,
1578 .disconnect = usbhid_disconnect,
Jiri Kosina0f6f1402009-01-19 09:17:18 +01001579#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 .suspend = hid_suspend,
1581 .resume = hid_resume,
Oliver Neukum378a0ed2009-02-18 11:46:45 +01001582 .reset_resume = hid_reset_resume,
Jiri Kosina0f6f1402009-01-19 09:17:18 +01001583#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 .pre_reset = hid_pre_reset,
1585 .post_reset = hid_post_reset,
1586 .id_table = hid_usb_ids,
Oliver Neukum933e3182007-07-11 14:48:58 +02001587 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588};
1589
Guillaume Chazarain8fe294c2010-09-12 21:32:35 +02001590struct usb_interface *usbhid_find_interface(int minor)
1591{
1592 return usb_find_interface(&hid_driver, minor);
1593}
1594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595static int __init hid_init(void)
1596{
Oliver Neukum0361a282008-12-17 15:38:03 +01001597 int retval = -ENOMEM;
1598
Paul Walmsley876b9272007-04-19 14:56:12 +02001599 retval = usbhid_quirks_init(quirks_param);
1600 if (retval)
1601 goto usbhid_quirks_init_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 retval = usb_register(&hid_driver);
1603 if (retval)
1604 goto usb_register_fail;
Jiri Kosinaccabcd22009-10-02 18:31:36 +02001605 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 return 0;
1608usb_register_fail:
Paul Walmsley876b9272007-04-19 14:56:12 +02001609 usbhid_quirks_exit();
1610usbhid_quirks_init_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 return retval;
1612}
1613
1614static void __exit hid_exit(void)
1615{
1616 usb_deregister(&hid_driver);
Paul Walmsley876b9272007-04-19 14:56:12 +02001617 usbhid_quirks_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
1619
1620module_init(hid_init);
1621module_exit(hid_exit);
1622
Jiri Kosina88adb722009-10-02 18:29:34 +02001623MODULE_AUTHOR("Andreas Gal");
1624MODULE_AUTHOR("Vojtech Pavlik");
1625MODULE_AUTHOR("Jiri Kosina");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626MODULE_DESCRIPTION(DRIVER_DESC);
1627MODULE_LICENSE(DRIVER_LICENSE);