blob: ada164e1b3a1fd909c8c6a437dc2cc5cb424a845 [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
Alan Sternd4150c82012-07-19 16:08:54 -0400210 if (!hid || test_bit(HID_RESET_PENDING, &usbhid->iofl) ||
211 test_bit(HID_SUSPENDED, &usbhid->iofl))
Oliver Neukum0361a282008-12-17 15:38:03 +0100212 return 0;
213
214 if ((kicked = (usbhid->outhead != usbhid->outtail))) {
Greg Kroah-Hartman6cc203d2012-05-01 21:32:55 -0700215 hid_dbg(hid, "Kicking head %d tail %d", usbhid->outhead, usbhid->outtail);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800216
Alan Stern01a7c982012-07-19 16:08:31 -0400217 /* Try to wake up from autosuspend... */
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800218 r = usb_autopm_get_interface_async(usbhid->intf);
219 if (r < 0)
220 return r;
Alan Stern01a7c982012-07-19 16:08:31 -0400221
222 /*
223 * If still suspended, don't submit. Submission will
224 * occur if/when resume drains the queue.
225 */
Alan Sternf2b52642012-07-19 16:08:45 -0400226 if (test_bit(HID_SUSPENDED, &usbhid->iofl)) {
Alan Stern01a7c982012-07-19 16:08:31 -0400227 usb_autopm_put_interface_no_suspend(usbhid->intf);
228 return r;
229 }
230
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800231 /* Asynchronously flush queue. */
232 set_bit(HID_OUT_RUNNING, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +0100233 if (hid_submit_out(hid)) {
234 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800235 usb_autopm_put_interface_async(usbhid->intf);
Oliver Neukum0361a282008-12-17 15:38:03 +0100236 }
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800237 wake_up(&usbhid->wait);
Oliver Neukum0361a282008-12-17 15:38:03 +0100238 }
239 return kicked;
240}
241
242static int usbhid_restart_ctrl_queue(struct usbhid_device *usbhid)
243{
244 struct hid_device *hid = usb_get_intfdata(usbhid->intf);
245 int kicked;
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800246 int r;
Oliver Neukum0361a282008-12-17 15:38:03 +0100247
248 WARN_ON(hid == NULL);
Alan Sternd4150c82012-07-19 16:08:54 -0400249 if (!hid || test_bit(HID_RESET_PENDING, &usbhid->iofl) ||
250 test_bit(HID_SUSPENDED, &usbhid->iofl))
Oliver Neukum0361a282008-12-17 15:38:03 +0100251 return 0;
252
253 if ((kicked = (usbhid->ctrlhead != usbhid->ctrltail))) {
Greg Kroah-Hartman6cc203d2012-05-01 21:32:55 -0700254 hid_dbg(hid, "Kicking head %d tail %d", usbhid->ctrlhead, usbhid->ctrltail);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800255
Alan Stern01a7c982012-07-19 16:08:31 -0400256 /* Try to wake up from autosuspend... */
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800257 r = usb_autopm_get_interface_async(usbhid->intf);
258 if (r < 0)
259 return r;
Alan Stern01a7c982012-07-19 16:08:31 -0400260
261 /*
262 * If still suspended, don't submit. Submission will
263 * occur if/when resume drains the queue.
264 */
Alan Sternf2b52642012-07-19 16:08:45 -0400265 if (test_bit(HID_SUSPENDED, &usbhid->iofl)) {
Alan Stern01a7c982012-07-19 16:08:31 -0400266 usb_autopm_put_interface_no_suspend(usbhid->intf);
267 return r;
268 }
269
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800270 /* Asynchronously flush queue. */
271 set_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +0100272 if (hid_submit_ctrl(hid)) {
273 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800274 usb_autopm_put_interface_async(usbhid->intf);
Oliver Neukum0361a282008-12-17 15:38:03 +0100275 }
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800276 wake_up(&usbhid->wait);
Oliver Neukum0361a282008-12-17 15:38:03 +0100277 }
278 return kicked;
Alan Sternaef4e262006-01-31 12:58:38 -0500279}
280
281/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 * Input interrupt completion handler.
283 */
284
David Howells7d12e782006-10-05 14:55:46 +0100285static void hid_irq_in(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100288 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 int status;
290
291 switch (urb->status) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200292 case 0: /* success */
Oliver Neukum0361a282008-12-17 15:38:03 +0100293 usbhid_mark_busy(usbhid);
Jiri Slaby880d29f2008-06-18 23:55:41 +0200294 usbhid->retry_delay = 0;
295 hid_input_report(urb->context, HID_INPUT_REPORT,
296 urb->transfer_buffer,
297 urb->actual_length, 1);
Oliver Neukum0361a282008-12-17 15:38:03 +0100298 /*
299 * autosuspend refused while keys are pressed
300 * because most keyboards don't wake up when
301 * a key is released
302 */
303 if (hid_check_keys_pressed(hid))
304 set_bit(HID_KEYS_PRESSED, &usbhid->iofl);
305 else
306 clear_bit(HID_KEYS_PRESSED, &usbhid->iofl);
Jiri Slaby880d29f2008-06-18 23:55:41 +0200307 break;
308 case -EPIPE: /* stall */
Oliver Neukum0361a282008-12-17 15:38:03 +0100309 usbhid_mark_busy(usbhid);
Jiri Slaby880d29f2008-06-18 23:55:41 +0200310 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
311 set_bit(HID_CLEAR_HALT, &usbhid->iofl);
312 schedule_work(&usbhid->reset_work);
313 return;
314 case -ECONNRESET: /* unlink */
315 case -ENOENT:
316 case -ESHUTDOWN: /* unplug */
317 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
318 return;
319 case -EILSEQ: /* protocol error or unplug */
320 case -EPROTO: /* protocol error or unplug */
321 case -ETIME: /* protocol error or unplug */
322 case -ETIMEDOUT: /* Should never happen, but... */
Oliver Neukum0361a282008-12-17 15:38:03 +0100323 usbhid_mark_busy(usbhid);
Jiri Slaby880d29f2008-06-18 23:55:41 +0200324 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
325 hid_io_error(hid);
326 return;
327 default: /* error */
Joe Perches4291ee32010-12-09 19:29:03 -0800328 hid_warn(urb->dev, "input irq status %d received\n",
329 urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800332 status = usb_submit_urb(urb, GFP_ATOMIC);
Alan Sternaef4e262006-01-31 12:58:38 -0500333 if (status) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100334 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
Alan Sternaef4e262006-01-31 12:58:38 -0500335 if (status != -EPERM) {
Joe Perches4291ee32010-12-09 19:29:03 -0800336 hid_err(hid, "can't resubmit intr, %s-%s/input%d, status %d\n",
337 hid_to_usb_dev(hid)->bus->bus_name,
338 hid_to_usb_dev(hid)->devpath,
339 usbhid->ifnum, status);
Alan Sternaef4e262006-01-31 12:58:38 -0500340 hid_io_error(hid);
341 }
342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345static int hid_submit_out(struct hid_device *hid)
346{
347 struct hid_report *report;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200348 char *raw_report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100349 struct usbhid_device *usbhid = hid->driver_data;
Oliver Neukum68229682010-12-22 15:33:40 +0100350 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200352 report = usbhid->out[usbhid->outtail].report;
353 raw_report = usbhid->out[usbhid->outtail].raw_report;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800355 usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) +
356 1 + (report->id > 0);
357 usbhid->urbout->dev = hid_to_usb_dev(hid);
Alan Stern668160e2012-07-19 16:08:21 -0400358 if (raw_report) {
359 memcpy(usbhid->outbuf, raw_report,
360 usbhid->urbout->transfer_buffer_length);
361 kfree(raw_report);
362 usbhid->out[usbhid->outtail].raw_report = NULL;
363 }
Oliver Neukum68229682010-12-22 15:33:40 +0100364
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800365 dbg_hid("submitting out urb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800367 r = usb_submit_urb(usbhid->urbout, GFP_ATOMIC);
368 if (r < 0) {
369 hid_err(hid, "usb_submit_urb(out) failed: %d\n", r);
370 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800372 usbhid->last_out = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return 0;
374}
375
376static int hid_submit_ctrl(struct hid_device *hid)
377{
378 struct hid_report *report;
379 unsigned char dir;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200380 char *raw_report;
Oliver Neukum68229682010-12-22 15:33:40 +0100381 int len, r;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100382 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100384 report = usbhid->ctrl[usbhid->ctrltail].report;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200385 raw_report = usbhid->ctrl[usbhid->ctrltail].raw_report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100386 dir = usbhid->ctrl[usbhid->ctrltail].dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800388 len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
389 if (dir == USB_DIR_OUT) {
390 usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
391 usbhid->urbctrl->transfer_buffer_length = len;
Alan Stern668160e2012-07-19 16:08:21 -0400392 if (raw_report) {
393 memcpy(usbhid->ctrlbuf, raw_report, len);
394 kfree(raw_report);
395 usbhid->ctrl[usbhid->ctrltail].raw_report = NULL;
396 }
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800397 } else {
398 int maxpacket, padlen;
Oliver Neukum0361a282008-12-17 15:38:03 +0100399
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800400 usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0);
401 maxpacket = usb_maxpacket(hid_to_usb_dev(hid),
402 usbhid->urbctrl->pipe, 0);
403 if (maxpacket > 0) {
404 padlen = DIV_ROUND_UP(len, maxpacket);
405 padlen *= maxpacket;
406 if (padlen > usbhid->bufsize)
407 padlen = usbhid->bufsize;
408 } else
409 padlen = 0;
410 usbhid->urbctrl->transfer_buffer_length = padlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800412 usbhid->urbctrl->dev = hid_to_usb_dev(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800414 usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
415 usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT :
416 HID_REQ_GET_REPORT;
417 usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) |
418 report->id);
419 usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum);
420 usbhid->cr->wLength = cpu_to_le16(len);
421
422 dbg_hid("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u\n",
423 usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" :
424 "Get_Report",
425 usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength);
426
427 r = usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC);
428 if (r < 0) {
429 hid_err(hid, "usb_submit_urb(ctrl) failed: %d\n", r);
430 return r;
431 }
432 usbhid->last_ctrl = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return 0;
434}
435
436/*
437 * Output interrupt completion handler.
438 */
439
David Howells7d12e782006-10-05 14:55:46 +0100440static void hid_irq_out(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100443 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 unsigned long flags;
445 int unplug = 0;
446
447 switch (urb->status) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200448 case 0: /* success */
449 break;
450 case -ESHUTDOWN: /* unplug */
451 unplug = 1;
452 case -EILSEQ: /* protocol error or unplug */
453 case -EPROTO: /* protocol error or unplug */
454 case -ECONNRESET: /* unlink */
455 case -ENOENT:
456 break;
457 default: /* error */
Joe Perches4291ee32010-12-09 19:29:03 -0800458 hid_warn(urb->dev, "output irq status %d received\n",
459 urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461
Oliver Neukum0361a282008-12-17 15:38:03 +0100462 spin_lock_irqsave(&usbhid->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Alan Stern93101af2012-07-19 16:08:39 -0400464 if (unplug) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100465 usbhid->outtail = usbhid->outhead;
Alan Stern93101af2012-07-19 16:08:39 -0400466 } else {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100467 usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Alan Stern93101af2012-07-19 16:08:39 -0400469 if (usbhid->outhead != usbhid->outtail &&
470 hid_submit_out(hid) == 0) {
471 /* Successfully submitted next urb in queue */
472 spin_unlock_irqrestore(&usbhid->lock, flags);
473 return;
474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100477 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +0100478 spin_unlock_irqrestore(&usbhid->lock, flags);
Oliver Neukum68229682010-12-22 15:33:40 +0100479 usb_autopm_put_interface_async(usbhid->intf);
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100480 wake_up(&usbhid->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
483/*
484 * Control pipe completion handler.
485 */
486
David Howells7d12e782006-10-05 14:55:46 +0100487static void hid_ctrl(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100490 struct usbhid_device *usbhid = hid->driver_data;
Oliver Neukum0361a282008-12-17 15:38:03 +0100491 int unplug = 0, status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Oliver Neukum0361a282008-12-17 15:38:03 +0100493 spin_lock(&usbhid->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Oliver Neukum0361a282008-12-17 15:38:03 +0100495 switch (status) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200496 case 0: /* success */
497 if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
498 hid_input_report(urb->context,
499 usbhid->ctrl[usbhid->ctrltail].report->type,
500 urb->transfer_buffer, urb->actual_length, 0);
501 break;
502 case -ESHUTDOWN: /* unplug */
503 unplug = 1;
504 case -EILSEQ: /* protocol error or unplug */
505 case -EPROTO: /* protocol error or unplug */
506 case -ECONNRESET: /* unlink */
507 case -ENOENT:
508 case -EPIPE: /* report not available */
509 break;
510 default: /* error */
Joe Perches4291ee32010-12-09 19:29:03 -0800511 hid_warn(urb->dev, "ctrl urb status %d received\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513
Alan Stern93101af2012-07-19 16:08:39 -0400514 if (unplug) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100515 usbhid->ctrltail = usbhid->ctrlhead;
Alan Stern93101af2012-07-19 16:08:39 -0400516 } else {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100517 usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Alan Stern93101af2012-07-19 16:08:39 -0400519 if (usbhid->ctrlhead != usbhid->ctrltail &&
520 hid_submit_ctrl(hid) == 0) {
521 /* Successfully submitted next urb in queue */
522 spin_unlock(&usbhid->lock);
523 return;
524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100527 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +0100528 spin_unlock(&usbhid->lock);
Oliver Neukum68229682010-12-22 15:33:40 +0100529 usb_autopm_put_interface_async(usbhid->intf);
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100530 wake_up(&usbhid->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
H Hartley Sweeten52cfc61b2009-08-17 15:37:18 -0700533static void __usbhid_submit_report(struct hid_device *hid, struct hid_report *report,
534 unsigned char dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 int head;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100537 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
540 return;
541
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100542 if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100543 if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
Joe Perches4291ee32010-12-09 19:29:03 -0800544 hid_warn(hid, "output queue full\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return;
546 }
547
Jiri Kosina27ce4052013-07-10 19:56:27 +0200548 usbhid->out[usbhid->outhead].raw_report = hid_alloc_report_buf(report, GFP_ATOMIC);
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200549 if (!usbhid->out[usbhid->outhead].raw_report) {
Joe Perches4291ee32010-12-09 19:29:03 -0800550 hid_warn(hid, "output queueing failed\n");
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200551 return;
552 }
553 hid_output_report(report, usbhid->out[usbhid->outhead].raw_report);
554 usbhid->out[usbhid->outhead].report = report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100555 usbhid->outhead = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Alan Stern01a7c982012-07-19 16:08:31 -0400557 /* If the queue isn't running, restart it */
558 if (!test_bit(HID_OUT_RUNNING, &usbhid->iofl)) {
559 usbhid_restart_out_queue(usbhid);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800560
Alan Stern01a7c982012-07-19 16:08:31 -0400561 /* Otherwise see if an earlier request has timed out */
562 } else if (time_after(jiffies, usbhid->last_out + HZ * 5)) {
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800563
Alan Stern01a7c982012-07-19 16:08:31 -0400564 /* Prevent autosuspend following the unlink */
565 usb_autopm_get_interface_no_resume(usbhid->intf);
566
Oliver Neukum858155f2010-02-12 13:02:28 +0100567 /*
Alan Stern01a7c982012-07-19 16:08:31 -0400568 * Prevent resubmission in case the URB completes
569 * before we can unlink it. We don't want to cancel
570 * the wrong transfer!
Oliver Neukum858155f2010-02-12 13:02:28 +0100571 */
Alan Stern01a7c982012-07-19 16:08:31 -0400572 usb_block_urb(usbhid->urbout);
Oliver Neukum8815bb02012-04-30 09:13:46 +0200573
Alan Stern01a7c982012-07-19 16:08:31 -0400574 /* Drop lock to avoid deadlock if the callback runs */
575 spin_unlock(&usbhid->lock);
Oliver Neukum8815bb02012-04-30 09:13:46 +0200576
Alan Stern01a7c982012-07-19 16:08:31 -0400577 usb_unlink_urb(usbhid->urbout);
578 spin_lock(&usbhid->lock);
579 usb_unblock_urb(usbhid->urbout);
580
581 /* Unlink might have stopped the queue */
582 if (!test_bit(HID_OUT_RUNNING, &usbhid->iofl))
583 usbhid_restart_out_queue(usbhid);
584
585 /* Now we can allow autosuspend again */
586 usb_autopm_put_interface_async(usbhid->intf);
Oliver Neukum858155f2010-02-12 13:02:28 +0100587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return;
589 }
590
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100591 if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
Joe Perches4291ee32010-12-09 19:29:03 -0800592 hid_warn(hid, "control queue full\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return;
594 }
595
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200596 if (dir == USB_DIR_OUT) {
Jiri Kosina27ce4052013-07-10 19:56:27 +0200597 usbhid->ctrl[usbhid->ctrlhead].raw_report = hid_alloc_report_buf(report, GFP_ATOMIC);
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200598 if (!usbhid->ctrl[usbhid->ctrlhead].raw_report) {
Joe Perches4291ee32010-12-09 19:29:03 -0800599 hid_warn(hid, "control queueing failed\n");
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200600 return;
601 }
602 hid_output_report(report, usbhid->ctrl[usbhid->ctrlhead].raw_report);
603 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100604 usbhid->ctrl[usbhid->ctrlhead].report = report;
605 usbhid->ctrl[usbhid->ctrlhead].dir = dir;
606 usbhid->ctrlhead = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Alan Stern01a7c982012-07-19 16:08:31 -0400608 /* If the queue isn't running, restart it */
609 if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl)) {
610 usbhid_restart_ctrl_queue(usbhid);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800611
Alan Stern01a7c982012-07-19 16:08:31 -0400612 /* Otherwise see if an earlier request has timed out */
613 } else if (time_after(jiffies, usbhid->last_ctrl + HZ * 5)) {
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800614
Alan Stern01a7c982012-07-19 16:08:31 -0400615 /* Prevent autosuspend following the unlink */
616 usb_autopm_get_interface_no_resume(usbhid->intf);
617
Oliver Neukum858155f2010-02-12 13:02:28 +0100618 /*
Alan Stern01a7c982012-07-19 16:08:31 -0400619 * Prevent resubmission in case the URB completes
620 * before we can unlink it. We don't want to cancel
621 * the wrong transfer!
Oliver Neukum858155f2010-02-12 13:02:28 +0100622 */
Alan Stern01a7c982012-07-19 16:08:31 -0400623 usb_block_urb(usbhid->urbctrl);
624
625 /* Drop lock to avoid deadlock if the callback runs */
626 spin_unlock(&usbhid->lock);
627
628 usb_unlink_urb(usbhid->urbctrl);
629 spin_lock(&usbhid->lock);
630 usb_unblock_urb(usbhid->urbctrl);
631
632 /* Unlink might have stopped the queue */
633 if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
634 usbhid_restart_ctrl_queue(usbhid);
635
636 /* Now we can allow autosuspend again */
637 usb_autopm_put_interface_async(usbhid->intf);
Oliver Neukum858155f2010-02-12 13:02:28 +0100638 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100639}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100641static void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
Oliver Neukum0361a282008-12-17 15:38:03 +0100642{
643 struct usbhid_device *usbhid = hid->driver_data;
644 unsigned long flags;
645
646 spin_lock_irqsave(&usbhid->lock, flags);
647 __usbhid_submit_report(hid, report, dir);
648 spin_unlock_irqrestore(&usbhid->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
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
Benjamin Tissoiresb7966a42013-02-25 11:31:47 +0100707static int 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}
721
Vojtech Pavlik854561b2005-05-29 02:28:00 -0500722static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
723{
Vojtech Pavlik71387bd2005-05-29 02:28:14 -0500724 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
Vojtech Pavlik854561b2005-05-29 02:28:00 -0500725 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
726 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
727}
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
730 unsigned char type, void *buf, int size)
731{
732 int result, retries = 4;
733
Jiri Kosina43c7bf02007-01-26 12:58:24 +0100734 memset(buf, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 do {
737 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
738 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
739 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
740 retries--;
741 } while (result < size && retries);
742 return result;
743}
744
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100745int usbhid_open(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Oliver Neukum933e3182007-07-11 14:48:58 +0200747 struct usbhid_device *usbhid = hid->driver_data;
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200748 int res = 0;
Oliver Neukum933e3182007-07-11 14:48:58 +0200749
Oliver Neukum0361a282008-12-17 15:38:03 +0100750 mutex_lock(&hid_open_mut);
Oliver Neukum933e3182007-07-11 14:48:58 +0200751 if (!hid->open++) {
752 res = usb_autopm_get_interface(usbhid->intf);
Oliver Neukum68229682010-12-22 15:33:40 +0100753 /* the device must be awake to reliably request remote wakeup */
Oliver Neukum933e3182007-07-11 14:48:58 +0200754 if (res < 0) {
755 hid->open--;
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200756 res = -EIO;
757 goto done;
Oliver Neukum933e3182007-07-11 14:48:58 +0200758 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100759 usbhid->intf->needs_remote_wakeup = 1;
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200760 res = hid_start_in(hid);
761 if (res) {
762 if (res != -ENOSPC) {
763 hid_io_error(hid);
764 res = 0;
765 } else {
766 /* no use opening if resources are insufficient */
767 hid->open--;
768 res = -EBUSY;
769 usbhid->intf->needs_remote_wakeup = 0;
770 }
771 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100772 usb_autopm_put_interface(usbhid->intf);
Oliver Neukum933e3182007-07-11 14:48:58 +0200773 }
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200774done:
Oliver Neukum0361a282008-12-17 15:38:03 +0100775 mutex_unlock(&hid_open_mut);
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200776 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100779void usbhid_close(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100781 struct usbhid_device *usbhid = hid->driver_data;
782
Oliver Neukum0361a282008-12-17 15:38:03 +0100783 mutex_lock(&hid_open_mut);
784
785 /* protecting hid->open to make sure we don't restart
786 * data acquistion due to a resumption we no longer
787 * care about
788 */
789 spin_lock_irq(&usbhid->lock);
Oliver Neukum933e3182007-07-11 14:48:58 +0200790 if (!--hid->open) {
Oliver Neukum0361a282008-12-17 15:38:03 +0100791 spin_unlock_irq(&usbhid->lock);
Oliver Neukum89092dd2009-04-29 17:12:12 +0200792 hid_cancel_delayed_stuff(usbhid);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100793 usb_kill_urb(usbhid->urbin);
Oliver Neukum0361a282008-12-17 15:38:03 +0100794 usbhid->intf->needs_remote_wakeup = 0;
795 } else {
796 spin_unlock_irq(&usbhid->lock);
Oliver Neukum933e3182007-07-11 14:48:58 +0200797 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100798 mutex_unlock(&hid_open_mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
800
801/*
802 * Initialize all reports
803 */
804
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100805void usbhid_init_reports(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
807 struct hid_report *report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100808 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 int err, ret;
810
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500811 list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100812 usbhid_submit_report(hid, report, USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100815 usbhid_submit_report(hid, report, USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 err = 0;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100818 ret = usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 while (ret) {
820 err |= ret;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100821 if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
822 usb_kill_urb(usbhid->urbctrl);
823 if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
824 usb_kill_urb(usbhid->urbout);
825 ret = usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
827
828 if (err)
Joe Perches4291ee32010-12-09 19:29:03 -0800829 hid_warn(hid, "timeout initializing reports\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500832/*
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200833 * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
834 */
835static int hid_find_field_early(struct hid_device *hid, unsigned int page,
836 unsigned int hid_code, struct hid_field **pfield)
837{
838 struct hid_report *report;
839 struct hid_field *field;
840 struct hid_usage *usage;
841 int i, j;
842
843 list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
844 for (i = 0; i < report->maxfield; i++) {
845 field = report->field[i];
846 for (j = 0; j < field->maxusage; j++) {
847 usage = &field->usage[j];
848 if ((usage->hid & HID_USAGE_PAGE) == page &&
849 (usage->hid & 0xFFFF) == hid_code) {
850 *pfield = field;
851 return j;
852 }
853 }
854 }
855 }
856 return -1;
857}
858
Jiri Slaby6edfa8d2008-06-27 20:41:02 +0200859void usbhid_set_leds(struct hid_device *hid)
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200860{
861 struct hid_field *field;
862 int offset;
863
864 if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
865 hid_set_field(field, offset, 0);
866 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
867 }
868}
Jiri Slaby6edfa8d2008-06-27 20:41:02 +0200869EXPORT_SYMBOL_GPL(usbhid_set_leds);
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200870
871/*
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500872 * Traverse the supplied list of reports and find the longest
873 */
Jiri Slaby282bfd42008-03-28 17:06:41 +0100874static void hid_find_max_report(struct hid_device *hid, unsigned int type,
875 unsigned int *max)
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500876{
877 struct hid_report *report;
Jiri Slaby282bfd42008-03-28 17:06:41 +0100878 unsigned int size;
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500879
880 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
Jiri Kosinaefc7ce12008-10-17 15:01:15 +0200881 size = ((report->size - 1) >> 3) + 1 + hid->report_enum[type].numbered;
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500882 if (*max < size)
883 *max = size;
884 }
885}
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
888{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100889 struct usbhid_device *usbhid = hid->driver_data;
890
Daniel Mack997ea582010-04-12 13:17:25 +0200891 usbhid->inbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
Jiri Slaby898089d2008-11-24 16:20:06 +0100892 &usbhid->inbuf_dma);
Daniel Mack997ea582010-04-12 13:17:25 +0200893 usbhid->outbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
Jiri Slaby898089d2008-11-24 16:20:06 +0100894 &usbhid->outbuf_dma);
Alan Stern0ede76f2010-03-05 15:10:17 -0500895 usbhid->cr = kmalloc(sizeof(*usbhid->cr), GFP_KERNEL);
Daniel Mack997ea582010-04-12 13:17:25 +0200896 usbhid->ctrlbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
Jiri Slaby898089d2008-11-24 16:20:06 +0100897 &usbhid->ctrlbuf_dma);
898 if (!usbhid->inbuf || !usbhid->outbuf || !usbhid->cr ||
899 !usbhid->ctrlbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 return -1;
901
902 return 0;
903}
904
Alan Ottb4dbde92011-01-18 03:04:39 -0500905static int usbhid_get_raw_report(struct hid_device *hid,
906 unsigned char report_number, __u8 *buf, size_t count,
907 unsigned char report_type)
908{
909 struct usbhid_device *usbhid = hid->driver_data;
910 struct usb_device *dev = hid_to_usb_dev(hid);
911 struct usb_interface *intf = usbhid->intf;
912 struct usb_host_interface *interface = intf->cur_altsetting;
913 int skipped_report_id = 0;
914 int ret;
915
916 /* Byte 0 is the report number. Report data starts at byte 1.*/
917 buf[0] = report_number;
918 if (report_number == 0x0) {
919 /* Offset the return buffer by 1, so that the report ID
920 will remain in byte 0. */
921 buf++;
922 count--;
923 skipped_report_id = 1;
924 }
925 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
926 HID_REQ_GET_REPORT,
927 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
928 ((report_type + 1) << 8) | report_number,
929 interface->desc.bInterfaceNumber, buf, count,
930 USB_CTRL_SET_TIMEOUT);
931
932 /* count also the report id */
933 if (ret > 0 && skipped_report_id)
934 ret++;
935
936 return ret;
937}
938
Jiri Kosinad4bfa032010-01-29 15:03:36 +0100939static int usbhid_output_raw_report(struct hid_device *hid, __u8 *buf, size_t count,
940 unsigned char report_type)
Jiri Kosinaefc493f2007-05-14 09:54:30 +0200941{
942 struct usbhid_device *usbhid = hid->driver_data;
943 struct usb_device *dev = hid_to_usb_dev(hid);
944 struct usb_interface *intf = usbhid->intf;
945 struct usb_host_interface *interface = intf->cur_altsetting;
946 int ret;
947
Alan Ottfe2c91e2010-09-22 13:19:42 +0200948 if (usbhid->urbout && report_type != HID_FEATURE_REPORT) {
Alan Otta8ab5d52010-05-16 18:07:09 -0400949 int actual_length;
950 int skipped_report_id = 0;
Alan Ott12e52722010-09-22 13:33:20 +0200951
Alan Otta8ab5d52010-05-16 18:07:09 -0400952 if (buf[0] == 0x0) {
953 /* Don't send the Report ID */
954 buf++;
955 count--;
956 skipped_report_id = 1;
957 }
958 ret = usb_interrupt_msg(dev, usbhid->urbout->pipe,
959 buf, count, &actual_length,
960 USB_CTRL_SET_TIMEOUT);
961 /* return the number of bytes transferred */
962 if (ret == 0) {
963 ret = actual_length;
964 /* count also the report id */
965 if (skipped_report_id)
966 ret++;
967 }
968 } else {
Alan Ott29129a92010-06-30 09:50:36 -0400969 int skipped_report_id = 0;
Alan Ottc29771c2010-08-17 00:44:04 -0400970 int report_id = buf[0];
Alan Ott29129a92010-06-30 09:50:36 -0400971 if (buf[0] == 0x0) {
972 /* Don't send the Report ID */
973 buf++;
974 count--;
975 skipped_report_id = 1;
976 }
Alan Otta8ab5d52010-05-16 18:07:09 -0400977 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
978 HID_REQ_SET_REPORT,
979 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
Alan Ottc29771c2010-08-17 00:44:04 -0400980 ((report_type + 1) << 8) | report_id,
Alan Ott29129a92010-06-30 09:50:36 -0400981 interface->desc.bInterfaceNumber, buf, count,
Alan Otta8ab5d52010-05-16 18:07:09 -0400982 USB_CTRL_SET_TIMEOUT);
Alan Ott29129a92010-06-30 09:50:36 -0400983 /* count also the report id, if this was a numbered report. */
984 if (ret > 0 && skipped_report_id)
Alan Otta8ab5d52010-05-16 18:07:09 -0400985 ret++;
986 }
Jiri Kosinaefc493f2007-05-14 09:54:30 +0200987
988 return ret;
989}
990
Oliver Neukum0361a282008-12-17 15:38:03 +0100991static void usbhid_restart_queues(struct usbhid_device *usbhid)
992{
Alan Sterneb055fd2012-07-19 16:09:01 -0400993 if (usbhid->urbout && !test_bit(HID_OUT_RUNNING, &usbhid->iofl))
Oliver Neukum0361a282008-12-17 15:38:03 +0100994 usbhid_restart_out_queue(usbhid);
Alan Sterneb055fd2012-07-19 16:09:01 -0400995 if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
996 usbhid_restart_ctrl_queue(usbhid);
Oliver Neukum0361a282008-12-17 15:38:03 +0100997}
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
Henrik Rydberge90a6df2013-02-25 11:31:43 +01001243static void usbhid_request(struct hid_device *hid, struct hid_report *rep, int reqtype)
1244{
1245 switch (reqtype) {
1246 case HID_REQ_GET_REPORT:
1247 usbhid_submit_report(hid, rep, USB_DIR_IN);
1248 break;
1249 case HID_REQ_SET_REPORT:
1250 usbhid_submit_report(hid, rep, USB_DIR_OUT);
1251 break;
1252 }
1253}
1254
Benjamin Tissoires96848192013-02-27 16:38:17 +01001255static int usbhid_idle(struct hid_device *hid, int report, int idle,
1256 int reqtype)
1257{
1258 struct usb_device *dev = hid_to_usb_dev(hid);
1259 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
1260 struct usb_host_interface *interface = intf->cur_altsetting;
1261 int ifnum = interface->desc.bInterfaceNumber;
1262
1263 if (reqtype != HID_REQ_SET_IDLE)
1264 return -EINVAL;
1265
1266 return hid_set_idle(dev, ifnum, report, idle);
1267}
1268
Jiri Slabyc500c972008-05-16 11:49:16 +02001269static struct hid_ll_driver usb_hid_driver = {
1270 .parse = usbhid_parse,
1271 .start = usbhid_start,
1272 .stop = usbhid_stop,
1273 .open = usbhid_open,
1274 .close = usbhid_close,
Oliver Neukum0361a282008-12-17 15:38:03 +01001275 .power = usbhid_power,
Jiri Slabyc500c972008-05-16 11:49:16 +02001276 .hidinput_input_event = usb_hidinput_input_event,
Henrik Rydberge90a6df2013-02-25 11:31:43 +01001277 .request = usbhid_request,
Henrik Rydberg33734432013-02-25 11:31:44 +01001278 .wait = usbhid_wait_io,
Benjamin Tissoires96848192013-02-27 16:38:17 +01001279 .idle = usbhid_idle,
Jiri Slabyc500c972008-05-16 11:49:16 +02001280};
1281
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001282static int usbhid_probe(struct usb_interface *intf, const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
Jiri Slaby131d3a7a2008-11-14 12:03:47 +01001284 struct usb_host_interface *interface = intf->cur_altsetting;
Jiri Slabyc500c972008-05-16 11:49:16 +02001285 struct usb_device *dev = interface_to_usbdev(intf);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001286 struct usbhid_device *usbhid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 struct hid_device *hid;
Jiri Slaby131d3a7a2008-11-14 12:03:47 +01001288 unsigned int n, has_in = 0;
Jiri Slabyc500c972008-05-16 11:49:16 +02001289 size_t len;
1290 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Jiri Kosina58037eb2007-05-30 15:07:13 +02001292 dbg_hid("HID probe called for ifnum %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 intf->altsetting->desc.bInterfaceNumber);
1294
Jiri Slaby131d3a7a2008-11-14 12:03:47 +01001295 for (n = 0; n < interface->desc.bNumEndpoints; n++)
1296 if (usb_endpoint_is_int_in(&interface->endpoint[n].desc))
1297 has_in++;
1298 if (!has_in) {
Joe Perches4291ee32010-12-09 19:29:03 -08001299 hid_err(intf, "couldn't find an input interrupt endpoint\n");
Jiri Slaby131d3a7a2008-11-14 12:03:47 +01001300 return -ENODEV;
1301 }
1302
Jiri Slabyc500c972008-05-16 11:49:16 +02001303 hid = hid_allocate_device();
1304 if (IS_ERR(hid))
1305 return PTR_ERR(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 usb_set_intfdata(intf, hid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001308 hid->ll_driver = &usb_hid_driver;
Alan Ottb4dbde92011-01-18 03:04:39 -05001309 hid->hid_get_raw_report = usbhid_get_raw_report;
Jiri Slabyc500c972008-05-16 11:49:16 +02001310 hid->hid_output_raw_report = usbhid_output_raw_report;
Jiri Slaby76483cf2008-09-18 12:23:33 +02001311 hid->ff_init = hid_pidff_init;
Jiri Slabyc500c972008-05-16 11:49:16 +02001312#ifdef CONFIG_USB_HIDDEV
Jiri Slaby93c10132008-06-27 00:04:24 +02001313 hid->hiddev_connect = hiddev_connect;
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001314 hid->hiddev_disconnect = hiddev_disconnect;
Jiri Slabyc500c972008-05-16 11:49:16 +02001315 hid->hiddev_hid_event = hiddev_hid_event;
1316 hid->hiddev_report_event = hiddev_report_event;
1317#endif
1318 hid->dev.parent = &intf->dev;
1319 hid->bus = BUS_USB;
1320 hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
1321 hid->product = le16_to_cpu(dev->descriptor.idProduct);
1322 hid->name[0] = 0;
Bastien Nocerab5e5a372010-04-16 17:19:50 +01001323 hid->quirks = usbhid_lookup_quirk(hid->vendor, hid->product);
Jiri Slabya73a6372008-10-22 14:45:11 +02001324 if (intf->cur_altsetting->desc.bInterfaceProtocol ==
1325 USB_INTERFACE_PROTOCOL_MOUSE)
1326 hid->type = HID_TYPE_USBMOUSE;
Tomoki Sekiyama6dc14182011-05-23 15:45:44 -07001327 else if (intf->cur_altsetting->desc.bInterfaceProtocol == 0)
1328 hid->type = HID_TYPE_USBNONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Jiri Slabyc500c972008-05-16 11:49:16 +02001330 if (dev->manufacturer)
1331 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
1332
1333 if (dev->product) {
1334 if (dev->manufacturer)
1335 strlcat(hid->name, " ", sizeof(hid->name));
1336 strlcat(hid->name, dev->product, sizeof(hid->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
1338
Jiri Slabyc500c972008-05-16 11:49:16 +02001339 if (!strlen(hid->name))
1340 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
1341 le16_to_cpu(dev->descriptor.idVendor),
1342 le16_to_cpu(dev->descriptor.idProduct));
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001343
Jiri Slabyc500c972008-05-16 11:49:16 +02001344 usb_make_path(dev, hid->phys, sizeof(hid->phys));
1345 strlcat(hid->phys, "/input", sizeof(hid->phys));
1346 len = strlen(hid->phys);
1347 if (len < sizeof(hid->phys) - 1)
1348 snprintf(hid->phys + len, sizeof(hid->phys) - len,
1349 "%d", intf->altsetting[0].desc.bInterfaceNumber);
Geoff Levand4a1a4d82007-01-15 20:11:52 -08001350
Jiri Slabyc500c972008-05-16 11:49:16 +02001351 if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
1352 hid->uniq[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001354 usbhid = kzalloc(sizeof(*usbhid), GFP_KERNEL);
1355 if (usbhid == NULL) {
1356 ret = -ENOMEM;
1357 goto err;
1358 }
1359
1360 hid->driver_data = usbhid;
1361 usbhid->hid = hid;
Jiri Kosina57ab12e2010-02-17 14:25:01 +01001362 usbhid->intf = intf;
1363 usbhid->ifnum = interface->desc.bInterfaceNumber;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001364
Alan Sternfde4e2f2010-05-07 10:41:10 -04001365 init_waitqueue_head(&usbhid->wait);
1366 INIT_WORK(&usbhid->reset_work, hid_reset);
Alan Sternfde4e2f2010-05-07 10:41:10 -04001367 setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
1368 spin_lock_init(&usbhid->lock);
1369
Daniel Kurtz4371ea82011-11-17 19:23:50 +08001370 INIT_WORK(&usbhid->led_work, hid_led);
1371
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001372 ret = hid_add_device(hid);
1373 if (ret) {
Jiri Slabyd458a9d2008-05-16 11:49:20 +02001374 if (ret != -ENODEV)
Joe Perches4291ee32010-12-09 19:29:03 -08001375 hid_err(intf, "can't add hid device: %d\n", ret);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001376 goto err_free;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001377 }
Jiri Slabyc500c972008-05-16 11:49:16 +02001378
1379 return 0;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001380err_free:
1381 kfree(usbhid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001382err:
1383 hid_destroy_device(hid);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001384 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385}
1386
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001387static void usbhid_disconnect(struct usb_interface *intf)
Jiri Slabyc500c972008-05-16 11:49:16 +02001388{
1389 struct hid_device *hid = usb_get_intfdata(intf);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001390 struct usbhid_device *usbhid;
Jiri Slabyc500c972008-05-16 11:49:16 +02001391
1392 if (WARN_ON(!hid))
1393 return;
1394
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001395 usbhid = hid->driver_data;
Jiri Slabyc500c972008-05-16 11:49:16 +02001396 hid_destroy_device(hid);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001397 kfree(usbhid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001398}
1399
Oliver Neukum0361a282008-12-17 15:38:03 +01001400static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
Oliver Neukum0361a282008-12-17 15:38:03 +01001402 del_timer_sync(&usbhid->io_retry);
Oliver Neukum0361a282008-12-17 15:38:03 +01001403 cancel_work_sync(&usbhid->reset_work);
Daniel Kurtz4371ea82011-11-17 19:23:50 +08001404 cancel_work_sync(&usbhid->led_work);
Oliver Neukum0361a282008-12-17 15:38:03 +01001405}
1406
1407static void hid_cease_io(struct usbhid_device *usbhid)
1408{
Oliver Neukumfad9fbe2011-10-13 18:21:58 +02001409 del_timer_sync(&usbhid->io_retry);
Oliver Neukum0361a282008-12-17 15:38:03 +01001410 usb_kill_urb(usbhid->urbin);
1411 usb_kill_urb(usbhid->urbctrl);
1412 usb_kill_urb(usbhid->urbout);
Oliver Neukum0361a282008-12-17 15:38:03 +01001413}
1414
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001415/* Treat USB reset pretty much the same as suspend/resume */
1416static int hid_pre_reset(struct usb_interface *intf)
1417{
1418 struct hid_device *hid = usb_get_intfdata(intf);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001419 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001421 spin_lock_irq(&usbhid->lock);
1422 set_bit(HID_RESET_PENDING, &usbhid->iofl);
1423 spin_unlock_irq(&usbhid->lock);
1424 hid_cease_io(usbhid);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001425
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001426 return 0;
1427}
1428
1429/* Same routine used for post_reset and reset_resume */
1430static int hid_post_reset(struct usb_interface *intf)
1431{
1432 struct usb_device *dev = interface_to_usbdev (intf);
1433 struct hid_device *hid = usb_get_intfdata(intf);
1434 struct usbhid_device *usbhid = hid->driver_data;
Simon Haggettdc3c78e2012-04-03 16:04:15 +01001435 struct usb_host_interface *interface = intf->cur_altsetting;
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001436 int status;
Simon Haggettdc3c78e2012-04-03 16:04:15 +01001437 char *rdesc;
1438
1439 /* Fetch and examine the HID report descriptor. If this
1440 * has changed, then rebind. Since usbcore's check of the
1441 * configuration descriptors passed, we already know that
1442 * the size of the HID report descriptor has not changed.
1443 */
Kevin Daughtridge86e6b772012-09-20 12:00:32 -07001444 rdesc = kmalloc(hid->dev_rsize, GFP_KERNEL);
Simon Haggettdc3c78e2012-04-03 16:04:15 +01001445 if (!rdesc) {
1446 dbg_hid("couldn't allocate rdesc memory (post_reset)\n");
1447 return 1;
1448 }
1449 status = hid_get_class_descriptor(dev,
1450 interface->desc.bInterfaceNumber,
Kevin Daughtridge86e6b772012-09-20 12:00:32 -07001451 HID_DT_REPORT, rdesc, hid->dev_rsize);
Simon Haggettdc3c78e2012-04-03 16:04:15 +01001452 if (status < 0) {
1453 dbg_hid("reading report descriptor failed (post_reset)\n");
1454 kfree(rdesc);
1455 return 1;
1456 }
Kevin Daughtridge86e6b772012-09-20 12:00:32 -07001457 status = memcmp(rdesc, hid->dev_rdesc, hid->dev_rsize);
Simon Haggettdc3c78e2012-04-03 16:04:15 +01001458 kfree(rdesc);
1459 if (status != 0) {
1460 dbg_hid("report descriptor changed\n");
1461 return 1;
1462 }
Jiri Kosina70fa9f22009-06-04 15:48:38 +02001463
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001464 spin_lock_irq(&usbhid->lock);
1465 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
1466 spin_unlock_irq(&usbhid->lock);
1467 hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001468 status = hid_start_in(hid);
1469 if (status < 0)
1470 hid_io_error(hid);
1471 usbhid_restart_queues(usbhid);
1472
1473 return 0;
1474}
1475
1476int usbhid_get_power(struct hid_device *hid)
1477{
1478 struct usbhid_device *usbhid = hid->driver_data;
Jiri Kosina70fa9f22009-06-04 15:48:38 +02001479
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001480 return usb_autopm_get_interface(usbhid->intf);
1481}
1482
1483void usbhid_put_power(struct hid_device *hid)
1484{
1485 struct usbhid_device *usbhid = hid->driver_data;
Jiri Kosina70fa9f22009-06-04 15:48:38 +02001486
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001487 usb_autopm_put_interface(usbhid->intf);
1488}
1489
1490
Jiri Kosina0f6f1402009-01-19 09:17:18 +01001491#ifdef CONFIG_PM
Alan Sterneb055fd2012-07-19 16:09:01 -04001492static int hid_resume_common(struct hid_device *hid, bool driver_suspended)
1493{
1494 struct usbhid_device *usbhid = hid->driver_data;
1495 int status;
1496
1497 spin_lock_irq(&usbhid->lock);
1498 clear_bit(HID_SUSPENDED, &usbhid->iofl);
1499 usbhid_mark_busy(usbhid);
1500
1501 if (test_bit(HID_CLEAR_HALT, &usbhid->iofl) ||
1502 test_bit(HID_RESET_PENDING, &usbhid->iofl))
1503 schedule_work(&usbhid->reset_work);
1504 usbhid->retry_delay = 0;
1505
1506 usbhid_restart_queues(usbhid);
1507 spin_unlock_irq(&usbhid->lock);
1508
1509 status = hid_start_in(hid);
1510 if (status < 0)
1511 hid_io_error(hid);
1512
1513 if (driver_suspended && hid->driver && hid->driver->resume)
1514 status = hid->driver->resume(hid);
1515 return status;
1516}
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518static int hid_suspend(struct usb_interface *intf, pm_message_t message)
1519{
Oliver Neukum0361a282008-12-17 15:38:03 +01001520 struct hid_device *hid = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 struct usbhid_device *usbhid = hid->driver_data;
Ming Lei37093b72013-03-15 12:08:55 +08001522 int status = 0;
Alan Sterneb055fd2012-07-19 16:09:01 -04001523 bool driver_suspended = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Alan Stern5b1b0b82011-08-19 23:49:48 +02001525 if (PMSG_IS_AUTO(message)) {
Oliver Neukum0361a282008-12-17 15:38:03 +01001526 spin_lock_irq(&usbhid->lock); /* Sync with error handler */
1527 if (!test_bit(HID_RESET_PENDING, &usbhid->iofl)
1528 && !test_bit(HID_CLEAR_HALT, &usbhid->iofl)
1529 && !test_bit(HID_OUT_RUNNING, &usbhid->iofl)
1530 && !test_bit(HID_CTRL_RUNNING, &usbhid->iofl)
1531 && !test_bit(HID_KEYS_PRESSED, &usbhid->iofl)
1532 && (!usbhid->ledcount || ignoreled))
1533 {
Alan Sternf2b52642012-07-19 16:08:45 -04001534 set_bit(HID_SUSPENDED, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +01001535 spin_unlock_irq(&usbhid->lock);
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001536 if (hid->driver && hid->driver->suspend) {
1537 status = hid->driver->suspend(hid, message);
1538 if (status < 0)
Alan Sterneb055fd2012-07-19 16:09:01 -04001539 goto failed;
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001540 }
Alan Sterneb055fd2012-07-19 16:09:01 -04001541 driver_suspended = true;
Oliver Neukum0361a282008-12-17 15:38:03 +01001542 } else {
1543 usbhid_mark_busy(usbhid);
1544 spin_unlock_irq(&usbhid->lock);
1545 return -EBUSY;
1546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Oliver Neukum0361a282008-12-17 15:38:03 +01001548 } else {
Ming Lei37093b72013-03-15 12:08:55 +08001549 /* TODO: resume() might need to handle suspend failure */
1550 if (hid->driver && hid->driver->suspend)
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001551 status = hid->driver->suspend(hid, message);
Alan Sterneb055fd2012-07-19 16:09:01 -04001552 driver_suspended = true;
Oliver Neukum0361a282008-12-17 15:38:03 +01001553 spin_lock_irq(&usbhid->lock);
Alan Sternf2b52642012-07-19 16:08:45 -04001554 set_bit(HID_SUSPENDED, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +01001555 spin_unlock_irq(&usbhid->lock);
Ming Lei37093b72013-03-15 12:08:55 +08001556 if (usbhid_wait_io(hid) < 0)
Alan Sterneb055fd2012-07-19 16:09:01 -04001557 status = -EIO;
Oliver Neukum0361a282008-12-17 15:38:03 +01001558 }
1559
Oliver Neukum0361a282008-12-17 15:38:03 +01001560 hid_cancel_delayed_stuff(usbhid);
1561 hid_cease_io(usbhid);
1562
Alan Stern5b1b0b82011-08-19 23:49:48 +02001563 if (PMSG_IS_AUTO(message) && test_bit(HID_KEYS_PRESSED, &usbhid->iofl)) {
Oliver Neukum0361a282008-12-17 15:38:03 +01001564 /* lost race against keypresses */
Alan Sterneb055fd2012-07-19 16:09:01 -04001565 status = -EBUSY;
1566 goto failed;
Oliver Neukum0361a282008-12-17 15:38:03 +01001567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 dev_dbg(&intf->dev, "suspend\n");
Ming Lei37093b72013-03-15 12:08:55 +08001569 return status;
Alan Sterneb055fd2012-07-19 16:09:01 -04001570
1571 failed:
1572 hid_resume_common(hid, driver_suspended);
1573 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574}
1575
1576static int hid_resume(struct usb_interface *intf)
1577{
1578 struct hid_device *hid = usb_get_intfdata (intf);
1579 struct usbhid_device *usbhid = hid->driver_data;
1580 int status;
1581
Jiri Slabyfde5be32008-11-23 12:03:20 +01001582 if (!test_bit(HID_STARTED, &usbhid->iofl))
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001583 return 0;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001584
Alan Sterneb055fd2012-07-19 16:09:01 -04001585 status = hid_resume_common(hid, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 dev_dbg(&intf->dev, "resume status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 return 0;
1588}
1589
Oliver Neukum378a0ed2009-02-18 11:46:45 +01001590static int hid_reset_resume(struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
Oliver Neukum378a0ed2009-02-18 11:46:45 +01001592 struct hid_device *hid = usb_get_intfdata(intf);
1593 struct usbhid_device *usbhid = hid->driver_data;
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001594 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Alan Sternf2b52642012-07-19 16:08:45 -04001596 clear_bit(HID_SUSPENDED, &usbhid->iofl);
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001597 status = hid_post_reset(intf);
1598 if (status >= 0 && hid->driver && hid->driver->reset_resume) {
1599 int ret = hid->driver->reset_resume(hid);
1600 if (ret < 0)
1601 status = ret;
1602 }
1603 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604}
1605
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001606#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Márton Némethd67dec52010-01-10 17:59:22 +01001608static const struct usb_device_id hid_usb_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1610 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
1611 { } /* Terminating entry */
1612};
1613
1614MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1615
1616static struct usb_driver hid_driver = {
1617 .name = "usbhid",
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001618 .probe = usbhid_probe,
1619 .disconnect = usbhid_disconnect,
Jiri Kosina0f6f1402009-01-19 09:17:18 +01001620#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 .suspend = hid_suspend,
1622 .resume = hid_resume,
Oliver Neukum378a0ed2009-02-18 11:46:45 +01001623 .reset_resume = hid_reset_resume,
Jiri Kosina0f6f1402009-01-19 09:17:18 +01001624#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 .pre_reset = hid_pre_reset,
1626 .post_reset = hid_post_reset,
1627 .id_table = hid_usb_ids,
Oliver Neukum933e3182007-07-11 14:48:58 +02001628 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629};
1630
Guillaume Chazarain8fe294c2010-09-12 21:32:35 +02001631struct usb_interface *usbhid_find_interface(int minor)
1632{
1633 return usb_find_interface(&hid_driver, minor);
1634}
1635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636static int __init hid_init(void)
1637{
Oliver Neukum0361a282008-12-17 15:38:03 +01001638 int retval = -ENOMEM;
1639
Paul Walmsley876b9272007-04-19 14:56:12 +02001640 retval = usbhid_quirks_init(quirks_param);
1641 if (retval)
1642 goto usbhid_quirks_init_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 retval = usb_register(&hid_driver);
1644 if (retval)
1645 goto usb_register_fail;
Jiri Kosinaccabcd22009-10-02 18:31:36 +02001646 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
1648 return 0;
1649usb_register_fail:
Paul Walmsley876b9272007-04-19 14:56:12 +02001650 usbhid_quirks_exit();
1651usbhid_quirks_init_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 return retval;
1653}
1654
1655static void __exit hid_exit(void)
1656{
1657 usb_deregister(&hid_driver);
Paul Walmsley876b9272007-04-19 14:56:12 +02001658 usbhid_quirks_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}
1660
1661module_init(hid_init);
1662module_exit(hid_exit);
1663
Jiri Kosina88adb722009-10-02 18:29:34 +02001664MODULE_AUTHOR("Andreas Gal");
1665MODULE_AUTHOR("Vojtech Pavlik");
1666MODULE_AUTHOR("Jiri Kosina");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667MODULE_DESCRIPTION(DRIVER_DESC);
1668MODULE_LICENSE(DRIVER_LICENSE);