blob: 482f936fc29b0deb04b3ddb3b7fe894a60c95b57 [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) &&
Oliver Neukum0361a282008-12-17 15:38:03 +010087 !test_bit(HID_REPORTED_IDLE, &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
216 r = usb_autopm_get_interface_async(usbhid->intf);
217 if (r < 0)
218 return r;
219 /* Asynchronously flush queue. */
220 set_bit(HID_OUT_RUNNING, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +0100221 if (hid_submit_out(hid)) {
222 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800223 usb_autopm_put_interface_async(usbhid->intf);
Oliver Neukum0361a282008-12-17 15:38:03 +0100224 }
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800225 wake_up(&usbhid->wait);
Oliver Neukum0361a282008-12-17 15:38:03 +0100226 }
227 return kicked;
228}
229
230static int usbhid_restart_ctrl_queue(struct usbhid_device *usbhid)
231{
232 struct hid_device *hid = usb_get_intfdata(usbhid->intf);
233 int kicked;
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800234 int r;
Oliver Neukum0361a282008-12-17 15:38:03 +0100235
236 WARN_ON(hid == NULL);
237 if (!hid)
238 return 0;
239
240 if ((kicked = (usbhid->ctrlhead != usbhid->ctrltail))) {
Greg Kroah-Hartman6cc203d2012-05-01 21:32:55 -0700241 hid_dbg(hid, "Kicking head %d tail %d", usbhid->ctrlhead, usbhid->ctrltail);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800242
243 r = usb_autopm_get_interface_async(usbhid->intf);
244 if (r < 0)
245 return r;
246 /* Asynchronously flush queue. */
247 set_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +0100248 if (hid_submit_ctrl(hid)) {
249 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800250 usb_autopm_put_interface_async(usbhid->intf);
Oliver Neukum0361a282008-12-17 15:38:03 +0100251 }
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800252 wake_up(&usbhid->wait);
Oliver Neukum0361a282008-12-17 15:38:03 +0100253 }
254 return kicked;
Alan Sternaef4e262006-01-31 12:58:38 -0500255}
256
257/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 * Input interrupt completion handler.
259 */
260
David Howells7d12e782006-10-05 14:55:46 +0100261static void hid_irq_in(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100264 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 int status;
266
267 switch (urb->status) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200268 case 0: /* success */
Oliver Neukum0361a282008-12-17 15:38:03 +0100269 usbhid_mark_busy(usbhid);
Jiri Slaby880d29f2008-06-18 23:55:41 +0200270 usbhid->retry_delay = 0;
271 hid_input_report(urb->context, HID_INPUT_REPORT,
272 urb->transfer_buffer,
273 urb->actual_length, 1);
Oliver Neukum0361a282008-12-17 15:38:03 +0100274 /*
275 * autosuspend refused while keys are pressed
276 * because most keyboards don't wake up when
277 * a key is released
278 */
279 if (hid_check_keys_pressed(hid))
280 set_bit(HID_KEYS_PRESSED, &usbhid->iofl);
281 else
282 clear_bit(HID_KEYS_PRESSED, &usbhid->iofl);
Jiri Slaby880d29f2008-06-18 23:55:41 +0200283 break;
284 case -EPIPE: /* stall */
Oliver Neukum0361a282008-12-17 15:38:03 +0100285 usbhid_mark_busy(usbhid);
Jiri Slaby880d29f2008-06-18 23:55:41 +0200286 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
287 set_bit(HID_CLEAR_HALT, &usbhid->iofl);
288 schedule_work(&usbhid->reset_work);
289 return;
290 case -ECONNRESET: /* unlink */
291 case -ENOENT:
292 case -ESHUTDOWN: /* unplug */
293 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
294 return;
295 case -EILSEQ: /* protocol error or unplug */
296 case -EPROTO: /* protocol error or unplug */
297 case -ETIME: /* protocol error or unplug */
298 case -ETIMEDOUT: /* Should never happen, but... */
Oliver Neukum0361a282008-12-17 15:38:03 +0100299 usbhid_mark_busy(usbhid);
Jiri Slaby880d29f2008-06-18 23:55:41 +0200300 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
301 hid_io_error(hid);
302 return;
303 default: /* error */
Joe Perches4291ee32010-12-09 19:29:03 -0800304 hid_warn(urb->dev, "input irq status %d received\n",
305 urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
307
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800308 status = usb_submit_urb(urb, GFP_ATOMIC);
Alan Sternaef4e262006-01-31 12:58:38 -0500309 if (status) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100310 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
Alan Sternaef4e262006-01-31 12:58:38 -0500311 if (status != -EPERM) {
Joe Perches4291ee32010-12-09 19:29:03 -0800312 hid_err(hid, "can't resubmit intr, %s-%s/input%d, status %d\n",
313 hid_to_usb_dev(hid)->bus->bus_name,
314 hid_to_usb_dev(hid)->devpath,
315 usbhid->ifnum, status);
Alan Sternaef4e262006-01-31 12:58:38 -0500316 hid_io_error(hid);
317 }
318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321static int hid_submit_out(struct hid_device *hid)
322{
323 struct hid_report *report;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200324 char *raw_report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100325 struct usbhid_device *usbhid = hid->driver_data;
Oliver Neukum68229682010-12-22 15:33:40 +0100326 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200328 report = usbhid->out[usbhid->outtail].report;
329 raw_report = usbhid->out[usbhid->outtail].raw_report;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800331 usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) +
332 1 + (report->id > 0);
333 usbhid->urbout->dev = hid_to_usb_dev(hid);
334 memcpy(usbhid->outbuf, raw_report,
335 usbhid->urbout->transfer_buffer_length);
336 kfree(raw_report);
Oliver Neukum68229682010-12-22 15:33:40 +0100337
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800338 dbg_hid("submitting out urb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800340 r = usb_submit_urb(usbhid->urbout, GFP_ATOMIC);
341 if (r < 0) {
342 hid_err(hid, "usb_submit_urb(out) failed: %d\n", r);
343 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800345 usbhid->last_out = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return 0;
347}
348
349static int hid_submit_ctrl(struct hid_device *hid)
350{
351 struct hid_report *report;
352 unsigned char dir;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200353 char *raw_report;
Oliver Neukum68229682010-12-22 15:33:40 +0100354 int len, r;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100355 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100357 report = usbhid->ctrl[usbhid->ctrltail].report;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200358 raw_report = usbhid->ctrl[usbhid->ctrltail].raw_report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100359 dir = usbhid->ctrl[usbhid->ctrltail].dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800361 len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
362 if (dir == USB_DIR_OUT) {
363 usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
364 usbhid->urbctrl->transfer_buffer_length = len;
365 memcpy(usbhid->ctrlbuf, raw_report, len);
366 kfree(raw_report);
367 } else {
368 int maxpacket, padlen;
Oliver Neukum0361a282008-12-17 15:38:03 +0100369
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800370 usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0);
371 maxpacket = usb_maxpacket(hid_to_usb_dev(hid),
372 usbhid->urbctrl->pipe, 0);
373 if (maxpacket > 0) {
374 padlen = DIV_ROUND_UP(len, maxpacket);
375 padlen *= maxpacket;
376 if (padlen > usbhid->bufsize)
377 padlen = usbhid->bufsize;
378 } else
379 padlen = 0;
380 usbhid->urbctrl->transfer_buffer_length = padlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800382 usbhid->urbctrl->dev = hid_to_usb_dev(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800384 usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
385 usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT :
386 HID_REQ_GET_REPORT;
387 usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) |
388 report->id);
389 usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum);
390 usbhid->cr->wLength = cpu_to_le16(len);
391
392 dbg_hid("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u\n",
393 usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" :
394 "Get_Report",
395 usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength);
396
397 r = usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC);
398 if (r < 0) {
399 hid_err(hid, "usb_submit_urb(ctrl) failed: %d\n", r);
400 return r;
401 }
402 usbhid->last_ctrl = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return 0;
404}
405
406/*
407 * Output interrupt completion handler.
408 */
409
Oliver Neukum8815bb02012-04-30 09:13:46 +0200410static int irq_out_pump_restart(struct hid_device *hid)
411{
412 struct usbhid_device *usbhid = hid->driver_data;
413
414 if (usbhid->outhead != usbhid->outtail)
415 return hid_submit_out(hid);
416 else
417 return -1;
418}
419
David Howells7d12e782006-10-05 14:55:46 +0100420static void hid_irq_out(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100423 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 unsigned long flags;
425 int unplug = 0;
426
427 switch (urb->status) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200428 case 0: /* success */
429 break;
430 case -ESHUTDOWN: /* unplug */
431 unplug = 1;
432 case -EILSEQ: /* protocol error or unplug */
433 case -EPROTO: /* protocol error or unplug */
434 case -ECONNRESET: /* unlink */
435 case -ENOENT:
436 break;
437 default: /* error */
Joe Perches4291ee32010-12-09 19:29:03 -0800438 hid_warn(urb->dev, "output irq status %d received\n",
439 urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441
Oliver Neukum0361a282008-12-17 15:38:03 +0100442 spin_lock_irqsave(&usbhid->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 if (unplug)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100445 usbhid->outtail = usbhid->outhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 else
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100447 usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Oliver Neukum8815bb02012-04-30 09:13:46 +0200449 if (!irq_out_pump_restart(hid)) {
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800450 /* Successfully submitted next urb in queue */
Oliver Neukum0361a282008-12-17 15:38:03 +0100451 spin_unlock_irqrestore(&usbhid->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return;
453 }
454
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100455 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +0100456 spin_unlock_irqrestore(&usbhid->lock, flags);
Oliver Neukum68229682010-12-22 15:33:40 +0100457 usb_autopm_put_interface_async(usbhid->intf);
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100458 wake_up(&usbhid->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
461/*
462 * Control pipe completion handler.
463 */
Oliver Neukum8815bb02012-04-30 09:13:46 +0200464static int ctrl_pump_restart(struct hid_device *hid)
465{
466 struct usbhid_device *usbhid = hid->driver_data;
467
468 if (usbhid->ctrlhead != usbhid->ctrltail)
469 return hid_submit_ctrl(hid);
470 else
471 return -1;
472}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
David Howells7d12e782006-10-05 14:55:46 +0100474static void hid_ctrl(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100477 struct usbhid_device *usbhid = hid->driver_data;
Oliver Neukum0361a282008-12-17 15:38:03 +0100478 int unplug = 0, status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Oliver Neukum0361a282008-12-17 15:38:03 +0100480 spin_lock(&usbhid->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Oliver Neukum0361a282008-12-17 15:38:03 +0100482 switch (status) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200483 case 0: /* success */
484 if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
485 hid_input_report(urb->context,
486 usbhid->ctrl[usbhid->ctrltail].report->type,
487 urb->transfer_buffer, urb->actual_length, 0);
488 break;
489 case -ESHUTDOWN: /* unplug */
490 unplug = 1;
491 case -EILSEQ: /* protocol error or unplug */
492 case -EPROTO: /* protocol error or unplug */
493 case -ECONNRESET: /* unlink */
494 case -ENOENT:
495 case -EPIPE: /* report not available */
496 break;
497 default: /* error */
Joe Perches4291ee32010-12-09 19:29:03 -0800498 hid_warn(urb->dev, "ctrl urb status %d received\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
500
501 if (unplug)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100502 usbhid->ctrltail = usbhid->ctrlhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 else
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100504 usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Oliver Neukum8815bb02012-04-30 09:13:46 +0200506 if (!ctrl_pump_restart(hid)) {
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800507 /* Successfully submitted next urb in queue */
Oliver Neukum0361a282008-12-17 15:38:03 +0100508 spin_unlock(&usbhid->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return;
510 }
511
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100512 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +0100513 spin_unlock(&usbhid->lock);
Oliver Neukum68229682010-12-22 15:33:40 +0100514 usb_autopm_put_interface_async(usbhid->intf);
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100515 wake_up(&usbhid->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517
H Hartley Sweeten52cfc612009-08-17 15:37:18 -0700518static void __usbhid_submit_report(struct hid_device *hid, struct hid_report *report,
519 unsigned char dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 int head;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100522 struct usbhid_device *usbhid = hid->driver_data;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200523 int len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
526 return;
527
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100528 if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100529 if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
Joe Perches4291ee32010-12-09 19:29:03 -0800530 hid_warn(hid, "output queue full\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return;
532 }
533
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200534 usbhid->out[usbhid->outhead].raw_report = kmalloc(len, GFP_ATOMIC);
535 if (!usbhid->out[usbhid->outhead].raw_report) {
Joe Perches4291ee32010-12-09 19:29:03 -0800536 hid_warn(hid, "output queueing failed\n");
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200537 return;
538 }
539 hid_output_report(report, usbhid->out[usbhid->outhead].raw_report);
540 usbhid->out[usbhid->outhead].report = report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100541 usbhid->outhead = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800543 /* Try to awake from autosuspend... */
544 if (usb_autopm_get_interface_async(usbhid->intf) < 0)
545 return;
546
547 /*
548 * But if still suspended, leave urb enqueued, don't submit.
549 * Submission will occur if/when resume() drains the queue.
550 */
551 if (test_bit(HID_REPORTED_IDLE, &usbhid->iofl))
552 return;
553
Oliver Neukum858155f2010-02-12 13:02:28 +0100554 if (!test_and_set_bit(HID_OUT_RUNNING, &usbhid->iofl)) {
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800555 if (hid_submit_out(hid)) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100556 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800557 usb_autopm_put_interface_async(usbhid->intf);
558 }
559 wake_up(&usbhid->wait);
Oliver Neukum858155f2010-02-12 13:02:28 +0100560 } else {
561 /*
562 * the queue is known to run
563 * but an earlier request may be stuck
564 * we may need to time out
Oliver Neukum8815bb02012-04-30 09:13:46 +0200565 * no race because the URB is blocked under
Oliver Neukum858155f2010-02-12 13:02:28 +0100566 * spinlock
567 */
Oliver Neukum8815bb02012-04-30 09:13:46 +0200568 if (time_after(jiffies, usbhid->last_out + HZ * 5)) {
569 usb_block_urb(usbhid->urbout);
570 /* drop lock to not deadlock if the callback is called */
571 spin_unlock(&usbhid->lock);
Oliver Neukum858155f2010-02-12 13:02:28 +0100572 usb_unlink_urb(usbhid->urbout);
Oliver Neukum8815bb02012-04-30 09:13:46 +0200573 spin_lock(&usbhid->lock);
574 usb_unblock_urb(usbhid->urbout);
575 /*
576 * if the unlinking has already completed
577 * the pump will have been stopped
578 * it must be restarted now
579 */
580 if (!test_bit(HID_OUT_RUNNING, &usbhid->iofl))
581 if (!irq_out_pump_restart(hid))
582 set_bit(HID_OUT_RUNNING, &usbhid->iofl);
583
584
585 }
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
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800607 /* Try to awake from autosuspend... */
608 if (usb_autopm_get_interface_async(usbhid->intf) < 0)
609 return;
610
611 /*
612 * If already suspended, leave urb enqueued, but don't submit.
613 * Submission will occur if/when resume() drains the queue.
614 */
615 if (test_bit(HID_REPORTED_IDLE, &usbhid->iofl))
616 return;
617
Oliver Neukum858155f2010-02-12 13:02:28 +0100618 if (!test_and_set_bit(HID_CTRL_RUNNING, &usbhid->iofl)) {
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800619 if (hid_submit_ctrl(hid)) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100620 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800621 usb_autopm_put_interface_async(usbhid->intf);
622 }
623 wake_up(&usbhid->wait);
Oliver Neukum858155f2010-02-12 13:02:28 +0100624 } else {
625 /*
626 * the queue is known to run
627 * but an earlier request may be stuck
628 * we may need to time out
Oliver Neukum8815bb02012-04-30 09:13:46 +0200629 * no race because the URB is blocked under
Oliver Neukum858155f2010-02-12 13:02:28 +0100630 * spinlock
631 */
Oliver Neukum8815bb02012-04-30 09:13:46 +0200632 if (time_after(jiffies, usbhid->last_ctrl + HZ * 5)) {
633 usb_block_urb(usbhid->urbctrl);
634 /* drop lock to not deadlock if the callback is called */
635 spin_unlock(&usbhid->lock);
Oliver Neukum858155f2010-02-12 13:02:28 +0100636 usb_unlink_urb(usbhid->urbctrl);
Oliver Neukum8815bb02012-04-30 09:13:46 +0200637 spin_lock(&usbhid->lock);
638 usb_unblock_urb(usbhid->urbctrl);
639 /*
640 * if the unlinking has already completed
641 * the pump will have been stopped
642 * it must be restarted now
643 */
644 if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
645 if (!ctrl_pump_restart(hid))
646 set_bit(HID_CTRL_RUNNING, &usbhid->iofl);
647 }
Oliver Neukum858155f2010-02-12 13:02:28 +0100648 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100649}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Oliver Neukum0361a282008-12-17 15:38:03 +0100651void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
652{
653 struct usbhid_device *usbhid = hid->driver_data;
654 unsigned long flags;
655
656 spin_lock_irqsave(&usbhid->lock, flags);
657 __usbhid_submit_report(hid, report, dir);
658 spin_unlock_irqrestore(&usbhid->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
Jiri Slaby606bd0a2008-07-04 23:06:45 +0200660EXPORT_SYMBOL_GPL(usbhid_submit_report);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Daniel Kurtz4371ea82011-11-17 19:23:50 +0800662/* Workqueue routine to send requests to change LEDs */
663static void hid_led(struct work_struct *work)
664{
665 struct usbhid_device *usbhid =
666 container_of(work, struct usbhid_device, led_work);
667 struct hid_device *hid = usbhid->hid;
668 struct hid_field *field;
669 unsigned long flags;
670
671 field = hidinput_get_led_field(hid);
672 if (!field) {
673 hid_warn(hid, "LED event field not found\n");
674 return;
675 }
676
677 spin_lock_irqsave(&usbhid->lock, flags);
678 if (!test_bit(HID_DISCONNECTED, &usbhid->iofl)) {
679 usbhid->ledcount = hidinput_count_leds(hid);
680 hid_dbg(usbhid->hid, "New ledcount = %u\n", usbhid->ledcount);
681 __usbhid_submit_report(hid, field->report, USB_DIR_OUT);
682 }
683 spin_unlock_irqrestore(&usbhid->lock, flags);
684}
685
Jiri Kosina229695e2006-12-08 18:40:53 +0100686static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
Jiri Kosinadde58452006-12-08 18:40:44 +0100687{
Dmitry Torokhove0712982007-05-09 10:17:31 +0200688 struct hid_device *hid = input_get_drvdata(dev);
Oliver Neukum0361a282008-12-17 15:38:03 +0100689 struct usbhid_device *usbhid = hid->driver_data;
Jiri Kosinadde58452006-12-08 18:40:44 +0100690 struct hid_field *field;
Oliver Neukum0361a282008-12-17 15:38:03 +0100691 unsigned long flags;
Jiri Kosinadde58452006-12-08 18:40:44 +0100692 int offset;
693
694 if (type == EV_FF)
695 return input_ff_event(dev, type, code, value);
696
697 if (type != EV_LED)
698 return -1;
699
700 if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
Joe Perches4291ee32010-12-09 19:29:03 -0800701 hid_warn(dev, "event field not found\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100702 return -1;
703 }
704
Daniel Kurtz4371ea82011-11-17 19:23:50 +0800705 spin_lock_irqsave(&usbhid->lock, flags);
Jiri Kosinadde58452006-12-08 18:40:44 +0100706 hid_set_field(field, offset, value);
Daniel Kurtz4371ea82011-11-17 19:23:50 +0800707 spin_unlock_irqrestore(&usbhid->lock, flags);
708
709 /*
710 * Defer performing requested LED action.
711 * This is more likely gather all LED changes into a single URB.
712 */
713 schedule_work(&usbhid->led_work);
Jiri Kosinadde58452006-12-08 18:40:44 +0100714
715 return 0;
716}
717
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100718int usbhid_wait_io(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100720 struct usbhid_device *usbhid = hid->driver_data;
721
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100722 if (!wait_event_timeout(usbhid->wait,
723 (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
724 !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 10*HZ)) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200726 dbg_hid("timeout waiting for ctrl or out queue to clear\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return -1;
728 }
729
730 return 0;
731}
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200732EXPORT_SYMBOL_GPL(usbhid_wait_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Vojtech Pavlik854561b2005-05-29 02:28:00 -0500734static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
735{
Vojtech Pavlik71387bd2005-05-29 02:28:14 -0500736 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
Vojtech Pavlik854561b2005-05-29 02:28:00 -0500737 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
738 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
739}
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
742 unsigned char type, void *buf, int size)
743{
744 int result, retries = 4;
745
Jiri Kosina43c7bf02007-01-26 12:58:24 +0100746 memset(buf, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 do {
749 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
750 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
751 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
752 retries--;
753 } while (result < size && retries);
754 return result;
755}
756
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100757int usbhid_open(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Oliver Neukum933e3182007-07-11 14:48:58 +0200759 struct usbhid_device *usbhid = hid->driver_data;
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200760 int res = 0;
Oliver Neukum933e3182007-07-11 14:48:58 +0200761
Oliver Neukum0361a282008-12-17 15:38:03 +0100762 mutex_lock(&hid_open_mut);
Oliver Neukum933e3182007-07-11 14:48:58 +0200763 if (!hid->open++) {
764 res = usb_autopm_get_interface(usbhid->intf);
Oliver Neukum68229682010-12-22 15:33:40 +0100765 /* the device must be awake to reliably request remote wakeup */
Oliver Neukum933e3182007-07-11 14:48:58 +0200766 if (res < 0) {
767 hid->open--;
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200768 res = -EIO;
769 goto done;
Oliver Neukum933e3182007-07-11 14:48:58 +0200770 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100771 usbhid->intf->needs_remote_wakeup = 1;
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200772 res = hid_start_in(hid);
773 if (res) {
774 if (res != -ENOSPC) {
775 hid_io_error(hid);
776 res = 0;
777 } else {
778 /* no use opening if resources are insufficient */
779 hid->open--;
780 res = -EBUSY;
781 usbhid->intf->needs_remote_wakeup = 0;
782 }
783 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100784 usb_autopm_put_interface(usbhid->intf);
Oliver Neukum933e3182007-07-11 14:48:58 +0200785 }
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200786done:
Oliver Neukum0361a282008-12-17 15:38:03 +0100787 mutex_unlock(&hid_open_mut);
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200788 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100791void usbhid_close(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100793 struct usbhid_device *usbhid = hid->driver_data;
794
Oliver Neukum0361a282008-12-17 15:38:03 +0100795 mutex_lock(&hid_open_mut);
796
797 /* protecting hid->open to make sure we don't restart
798 * data acquistion due to a resumption we no longer
799 * care about
800 */
801 spin_lock_irq(&usbhid->lock);
Oliver Neukum933e3182007-07-11 14:48:58 +0200802 if (!--hid->open) {
Oliver Neukum0361a282008-12-17 15:38:03 +0100803 spin_unlock_irq(&usbhid->lock);
Oliver Neukum89092dd2009-04-29 17:12:12 +0200804 hid_cancel_delayed_stuff(usbhid);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100805 usb_kill_urb(usbhid->urbin);
Oliver Neukum0361a282008-12-17 15:38:03 +0100806 usbhid->intf->needs_remote_wakeup = 0;
807 } else {
808 spin_unlock_irq(&usbhid->lock);
Oliver Neukum933e3182007-07-11 14:48:58 +0200809 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100810 mutex_unlock(&hid_open_mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
813/*
814 * Initialize all reports
815 */
816
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100817void usbhid_init_reports(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
819 struct hid_report *report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100820 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 int err, ret;
822
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500823 list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100824 usbhid_submit_report(hid, report, USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100827 usbhid_submit_report(hid, report, USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 err = 0;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100830 ret = usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 while (ret) {
832 err |= ret;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100833 if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
834 usb_kill_urb(usbhid->urbctrl);
835 if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
836 usb_kill_urb(usbhid->urbout);
837 ret = usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
839
840 if (err)
Joe Perches4291ee32010-12-09 19:29:03 -0800841 hid_warn(hid, "timeout initializing reports\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500844/*
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200845 * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
846 */
847static int hid_find_field_early(struct hid_device *hid, unsigned int page,
848 unsigned int hid_code, struct hid_field **pfield)
849{
850 struct hid_report *report;
851 struct hid_field *field;
852 struct hid_usage *usage;
853 int i, j;
854
855 list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
856 for (i = 0; i < report->maxfield; i++) {
857 field = report->field[i];
858 for (j = 0; j < field->maxusage; j++) {
859 usage = &field->usage[j];
860 if ((usage->hid & HID_USAGE_PAGE) == page &&
861 (usage->hid & 0xFFFF) == hid_code) {
862 *pfield = field;
863 return j;
864 }
865 }
866 }
867 }
868 return -1;
869}
870
Jiri Slaby6edfa8d2008-06-27 20:41:02 +0200871void usbhid_set_leds(struct hid_device *hid)
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200872{
873 struct hid_field *field;
874 int offset;
875
876 if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
877 hid_set_field(field, offset, 0);
878 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
879 }
880}
Jiri Slaby6edfa8d2008-06-27 20:41:02 +0200881EXPORT_SYMBOL_GPL(usbhid_set_leds);
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200882
883/*
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500884 * Traverse the supplied list of reports and find the longest
885 */
Jiri Slaby282bfd42008-03-28 17:06:41 +0100886static void hid_find_max_report(struct hid_device *hid, unsigned int type,
887 unsigned int *max)
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500888{
889 struct hid_report *report;
Jiri Slaby282bfd42008-03-28 17:06:41 +0100890 unsigned int size;
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500891
892 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
Jiri Kosinaefc7ce12008-10-17 15:01:15 +0200893 size = ((report->size - 1) >> 3) + 1 + hid->report_enum[type].numbered;
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500894 if (*max < size)
895 *max = size;
896 }
897}
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
900{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100901 struct usbhid_device *usbhid = hid->driver_data;
902
Daniel Mack997ea582010-04-12 13:17:25 +0200903 usbhid->inbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
Jiri Slaby898089d2008-11-24 16:20:06 +0100904 &usbhid->inbuf_dma);
Daniel Mack997ea582010-04-12 13:17:25 +0200905 usbhid->outbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
Jiri Slaby898089d2008-11-24 16:20:06 +0100906 &usbhid->outbuf_dma);
Alan Stern0ede76f2010-03-05 15:10:17 -0500907 usbhid->cr = kmalloc(sizeof(*usbhid->cr), GFP_KERNEL);
Daniel Mack997ea582010-04-12 13:17:25 +0200908 usbhid->ctrlbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
Jiri Slaby898089d2008-11-24 16:20:06 +0100909 &usbhid->ctrlbuf_dma);
910 if (!usbhid->inbuf || !usbhid->outbuf || !usbhid->cr ||
911 !usbhid->ctrlbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return -1;
913
914 return 0;
915}
916
Alan Ottb4dbde92011-01-18 03:04:39 -0500917static int usbhid_get_raw_report(struct hid_device *hid,
918 unsigned char report_number, __u8 *buf, size_t count,
919 unsigned char report_type)
920{
921 struct usbhid_device *usbhid = hid->driver_data;
922 struct usb_device *dev = hid_to_usb_dev(hid);
923 struct usb_interface *intf = usbhid->intf;
924 struct usb_host_interface *interface = intf->cur_altsetting;
925 int skipped_report_id = 0;
926 int ret;
927
928 /* Byte 0 is the report number. Report data starts at byte 1.*/
929 buf[0] = report_number;
930 if (report_number == 0x0) {
931 /* Offset the return buffer by 1, so that the report ID
932 will remain in byte 0. */
933 buf++;
934 count--;
935 skipped_report_id = 1;
936 }
937 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
938 HID_REQ_GET_REPORT,
939 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
940 ((report_type + 1) << 8) | report_number,
941 interface->desc.bInterfaceNumber, buf, count,
942 USB_CTRL_SET_TIMEOUT);
943
944 /* count also the report id */
945 if (ret > 0 && skipped_report_id)
946 ret++;
947
948 return ret;
949}
950
Jiri Kosinad4bfa032010-01-29 15:03:36 +0100951static int usbhid_output_raw_report(struct hid_device *hid, __u8 *buf, size_t count,
952 unsigned char report_type)
Jiri Kosinaefc493f2007-05-14 09:54:30 +0200953{
954 struct usbhid_device *usbhid = hid->driver_data;
955 struct usb_device *dev = hid_to_usb_dev(hid);
956 struct usb_interface *intf = usbhid->intf;
957 struct usb_host_interface *interface = intf->cur_altsetting;
958 int ret;
959
Alan Ottfe2c91e2010-09-22 13:19:42 +0200960 if (usbhid->urbout && report_type != HID_FEATURE_REPORT) {
Alan Otta8ab5d52010-05-16 18:07:09 -0400961 int actual_length;
962 int skipped_report_id = 0;
Alan Ott12e52722010-09-22 13:33:20 +0200963
Alan Otta8ab5d52010-05-16 18:07:09 -0400964 if (buf[0] == 0x0) {
965 /* Don't send the Report ID */
966 buf++;
967 count--;
968 skipped_report_id = 1;
969 }
970 ret = usb_interrupt_msg(dev, usbhid->urbout->pipe,
971 buf, count, &actual_length,
972 USB_CTRL_SET_TIMEOUT);
973 /* return the number of bytes transferred */
974 if (ret == 0) {
975 ret = actual_length;
976 /* count also the report id */
977 if (skipped_report_id)
978 ret++;
979 }
980 } else {
Alan Ott29129a92010-06-30 09:50:36 -0400981 int skipped_report_id = 0;
Alan Ottc29771c2010-08-17 00:44:04 -0400982 int report_id = buf[0];
Alan Ott29129a92010-06-30 09:50:36 -0400983 if (buf[0] == 0x0) {
984 /* Don't send the Report ID */
985 buf++;
986 count--;
987 skipped_report_id = 1;
988 }
Alan Otta8ab5d52010-05-16 18:07:09 -0400989 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
990 HID_REQ_SET_REPORT,
991 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
Alan Ottc29771c2010-08-17 00:44:04 -0400992 ((report_type + 1) << 8) | report_id,
Alan Ott29129a92010-06-30 09:50:36 -0400993 interface->desc.bInterfaceNumber, buf, count,
Alan Otta8ab5d52010-05-16 18:07:09 -0400994 USB_CTRL_SET_TIMEOUT);
Alan Ott29129a92010-06-30 09:50:36 -0400995 /* count also the report id, if this was a numbered report. */
996 if (ret > 0 && skipped_report_id)
Alan Otta8ab5d52010-05-16 18:07:09 -0400997 ret++;
998 }
Jiri Kosinaefc493f2007-05-14 09:54:30 +0200999
1000 return ret;
1001}
1002
Oliver Neukum0361a282008-12-17 15:38:03 +01001003static void usbhid_restart_queues(struct usbhid_device *usbhid)
1004{
1005 if (usbhid->urbout)
1006 usbhid_restart_out_queue(usbhid);
1007 usbhid_restart_ctrl_queue(usbhid);
1008}
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
1011{
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001012 struct usbhid_device *usbhid = hid->driver_data;
1013
Daniel Mack997ea582010-04-12 13:17:25 +02001014 usb_free_coherent(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
1015 usb_free_coherent(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
Alan Stern0ede76f2010-03-05 15:10:17 -05001016 kfree(usbhid->cr);
Daniel Mack997ea582010-04-12 13:17:25 +02001017 usb_free_coherent(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
Jiri Slabyc500c972008-05-16 11:49:16 +02001020static int usbhid_parse(struct hid_device *hid)
1021{
1022 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 struct usb_host_interface *interface = intf->cur_altsetting;
1024 struct usb_device *dev = interface_to_usbdev (intf);
1025 struct hid_descriptor *hdesc;
Paul Walmsley2eb5dc32007-04-19 13:27:04 +02001026 u32 quirks = 0;
Jiri Slabyc500c972008-05-16 11:49:16 +02001027 unsigned int rsize = 0;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001028 char *rdesc;
Jiri Slabyc500c972008-05-16 11:49:16 +02001029 int ret, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Paul Walmsley2eb5dc32007-04-19 13:27:04 +02001031 quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
1032 le16_to_cpu(dev->descriptor.idProduct));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Jiri Kosina6f4303f2009-01-29 00:15:51 +01001034 if (quirks & HID_QUIRK_IGNORE)
1035 return -ENODEV;
1036
Alan Stern0f28b552006-05-15 14:49:04 -04001037 /* Many keyboards and mice don't like to be polled for reports,
1038 * so we will always set the HID_QUIRK_NOGET flag for them. */
1039 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
1040 if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
1041 interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
1042 quirks |= HID_QUIRK_NOGET;
1043 }
1044
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001045 if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
1046 (!interface->desc.bNumEndpoints ||
1047 usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
Jiri Kosina58037eb2007-05-30 15:07:13 +02001048 dbg_hid("class descriptor not present\n");
Jiri Slabyc500c972008-05-16 11:49:16 +02001049 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
1051
Jiri Slabyc500c972008-05-16 11:49:16 +02001052 hid->version = le16_to_cpu(hdesc->bcdHID);
1053 hid->country = hdesc->bCountryCode;
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 for (n = 0; n < hdesc->bNumDescriptors; n++)
1056 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
1057 rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
1058
1059 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
Jiri Kosina58037eb2007-05-30 15:07:13 +02001060 dbg_hid("weird size of report descriptor (%u)\n", rsize);
Jiri Slabyc500c972008-05-16 11:49:16 +02001061 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
1063
1064 if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
Jiri Kosina58037eb2007-05-30 15:07:13 +02001065 dbg_hid("couldn't allocate rdesc memory\n");
Jiri Slabyc500c972008-05-16 11:49:16 +02001066 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068
Vojtech Pavlik854561b2005-05-29 02:28:00 -05001069 hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
1070
Jiri Slabyc500c972008-05-16 11:49:16 +02001071 ret = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber,
1072 HID_DT_REPORT, rdesc, rsize);
1073 if (ret < 0) {
Jiri Kosina58037eb2007-05-30 15:07:13 +02001074 dbg_hid("reading report descriptor failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 kfree(rdesc);
Jiri Slabyc500c972008-05-16 11:49:16 +02001076 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
1078
Jiri Slabyc500c972008-05-16 11:49:16 +02001079 ret = hid_parse_report(hid, rdesc, rsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 kfree(rdesc);
Jiri Slabyc500c972008-05-16 11:49:16 +02001081 if (ret) {
1082 dbg_hid("parsing report descriptor failed\n");
1083 goto err;
1084 }
1085
Zoltan Karcagif5208992009-05-06 16:30:21 +02001086 hid->quirks |= quirks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Jiri Slabyc500c972008-05-16 11:49:16 +02001088 return 0;
1089err:
1090 return ret;
1091}
1092
1093static int usbhid_start(struct hid_device *hid)
1094{
1095 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
1096 struct usb_host_interface *interface = intf->cur_altsetting;
1097 struct usb_device *dev = interface_to_usbdev(intf);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001098 struct usbhid_device *usbhid = hid->driver_data;
Jiri Slabyc500c972008-05-16 11:49:16 +02001099 unsigned int n, insize = 0;
1100 int ret;
1101
Jiri Slabye3e14de2008-11-01 23:41:46 +01001102 clear_bit(HID_DISCONNECTED, &usbhid->iofl);
1103
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001104 usbhid->bufsize = HID_MIN_BUFFER_SIZE;
1105 hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
1106 hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
1107 hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
1108
1109 if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
1110 usbhid->bufsize = HID_MAX_BUFFER_SIZE;
Michael Haboustakbf0964d2005-09-05 00:12:01 -05001111
1112 hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
1113
1114 if (insize > HID_MAX_BUFFER_SIZE)
1115 insize = HID_MAX_BUFFER_SIZE;
1116
Jiri Slabyc500c972008-05-16 11:49:16 +02001117 if (hid_alloc_buffers(dev, hid)) {
1118 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 goto fail;
Pekka Sarnilaf345c372008-03-06 13:23:14 +01001120 }
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 for (n = 0; n < interface->desc.bNumEndpoints; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 struct usb_endpoint_descriptor *endpoint;
1124 int pipe;
1125 int interval;
1126
1127 endpoint = &interface->endpoint[n].desc;
Jiri Slaby581a2732008-11-24 16:20:08 +01001128 if (!usb_endpoint_xfer_int(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 continue;
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 interval = endpoint->bInterval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Pekka Sarnilaf345c372008-03-06 13:23:14 +01001133 /* Some vendors give fullspeed interval on highspeed devides */
Jiri Slabyc500c972008-05-16 11:49:16 +02001134 if (hid->quirks & HID_QUIRK_FULLSPEED_INTERVAL &&
Pekka Sarnilaf345c372008-03-06 13:23:14 +01001135 dev->speed == USB_SPEED_HIGH) {
1136 interval = fls(endpoint->bInterval*8);
1137 printk(KERN_INFO "%s: Fixing fullspeed to highspeed interval: %d -> %d\n",
1138 hid->name, endpoint->bInterval, interval);
1139 }
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 /* Change the polling interval of mice. */
1142 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
1143 interval = hid_mousepoll_interval;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -05001144
Jiri Slabyc500c972008-05-16 11:49:16 +02001145 ret = -ENOMEM;
Luiz Fernando N. Capitulino0f12aa02006-10-26 13:02:51 -03001146 if (usb_endpoint_dir_in(endpoint)) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001147 if (usbhid->urbin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 continue;
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001149 if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 goto fail;
1151 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001152 usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 hid_irq_in, hid, interval);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001154 usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
1155 usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 } else {
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001157 if (usbhid->urbout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 continue;
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001159 if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 goto fail;
1161 pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001162 usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 hid_irq_out, hid, interval);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001164 usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
1165 usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
1167 }
1168
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001169 usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
Jiri Slabyc500c972008-05-16 11:49:16 +02001170 if (!usbhid->urbctrl) {
1171 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 goto fail;
Jiri Slabyc500c972008-05-16 11:49:16 +02001173 }
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001174
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001175 usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
1176 usbhid->ctrlbuf, 1, hid_ctrl, hid);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001177 usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
Alan Stern0ede76f2010-03-05 15:10:17 -05001178 usbhid->urbctrl->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Jiri Slabyc500c972008-05-16 11:49:16 +02001179
Jiri Kosina5b915d92009-11-05 14:08:03 +01001180 if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
1181 usbhid_init_reports(hid);
Jiri Slaby93c10132008-06-27 00:04:24 +02001182
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001183 set_bit(HID_STARTED, &usbhid->iofl);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001184
Alan Stern08ef08e2008-10-30 23:58:51 +01001185 /* Some keyboards don't work until their LEDs have been set.
1186 * Since BIOSes do set the LEDs, it must be safe for any device
1187 * that supports the keyboard boot protocol.
Alan Stern3d615102010-04-02 13:21:58 -04001188 * In addition, enable remote wakeup by default for all keyboard
1189 * devices supporting the boot protocol.
Alan Stern08ef08e2008-10-30 23:58:51 +01001190 */
1191 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT &&
1192 interface->desc.bInterfaceProtocol ==
Alan Stern3d615102010-04-02 13:21:58 -04001193 USB_INTERFACE_PROTOCOL_KEYBOARD) {
Alan Stern08ef08e2008-10-30 23:58:51 +01001194 usbhid_set_leds(hid);
Alan Stern3d615102010-04-02 13:21:58 -04001195 device_set_wakeup_enable(&dev->dev, 1);
1196 }
Jiri Slabyc500c972008-05-16 11:49:16 +02001197 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199fail:
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001200 usb_free_urb(usbhid->urbin);
1201 usb_free_urb(usbhid->urbout);
1202 usb_free_urb(usbhid->urbctrl);
Jiri Slabye3e14de2008-11-01 23:41:46 +01001203 usbhid->urbin = NULL;
1204 usbhid->urbout = NULL;
1205 usbhid->urbctrl = NULL;
Jiri Kosina22f675f2007-08-01 12:32:27 +02001206 hid_free_buffers(dev, hid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001207 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
Jiri Slabyc500c972008-05-16 11:49:16 +02001210static void usbhid_stop(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Jiri Slabyc500c972008-05-16 11:49:16 +02001212 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Jiri Slabyc500c972008-05-16 11:49:16 +02001214 if (WARN_ON(!usbhid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 return;
1216
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001217 clear_bit(HID_STARTED, &usbhid->iofl);
Daniel Kurtz4371ea82011-11-17 19:23:50 +08001218 spin_lock_irq(&usbhid->lock); /* Sync with error and led handlers */
Oliver Neukum69626f22008-03-31 16:27:30 +02001219 set_bit(HID_DISCONNECTED, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +01001220 spin_unlock_irq(&usbhid->lock);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001221 usb_kill_urb(usbhid->urbin);
1222 usb_kill_urb(usbhid->urbout);
1223 usb_kill_urb(usbhid->urbctrl);
1224
Oliver Neukum0361a282008-12-17 15:38:03 +01001225 hid_cancel_delayed_stuff(usbhid);
Alan Sternaef4e262006-01-31 12:58:38 -05001226
Jiri Slabyc500c972008-05-16 11:49:16 +02001227 hid->claimed = 0;
1228
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001229 usb_free_urb(usbhid->urbin);
1230 usb_free_urb(usbhid->urbctrl);
1231 usb_free_urb(usbhid->urbout);
Jiri Slabye3e14de2008-11-01 23:41:46 +01001232 usbhid->urbin = NULL; /* don't mess up next start */
1233 usbhid->urbctrl = NULL;
1234 usbhid->urbout = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Anssi Hannulabe820972007-01-19 19:28:17 +02001236 hid_free_buffers(hid_to_usb_dev(hid), hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
Oliver Neukum0361a282008-12-17 15:38:03 +01001239static int usbhid_power(struct hid_device *hid, int lvl)
1240{
1241 int r = 0;
1242
1243 switch (lvl) {
1244 case PM_HINT_FULLON:
1245 r = usbhid_get_power(hid);
1246 break;
1247 case PM_HINT_NORMAL:
1248 usbhid_put_power(hid);
1249 break;
1250 }
1251 return r;
1252}
1253
Jiri Slabyc500c972008-05-16 11:49:16 +02001254static struct hid_ll_driver usb_hid_driver = {
1255 .parse = usbhid_parse,
1256 .start = usbhid_start,
1257 .stop = usbhid_stop,
1258 .open = usbhid_open,
1259 .close = usbhid_close,
Oliver Neukum0361a282008-12-17 15:38:03 +01001260 .power = usbhid_power,
Jiri Slabyc500c972008-05-16 11:49:16 +02001261 .hidinput_input_event = usb_hidinput_input_event,
1262};
1263
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001264static int usbhid_probe(struct usb_interface *intf, const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
Jiri Slaby131d3a7a2008-11-14 12:03:47 +01001266 struct usb_host_interface *interface = intf->cur_altsetting;
Jiri Slabyc500c972008-05-16 11:49:16 +02001267 struct usb_device *dev = interface_to_usbdev(intf);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001268 struct usbhid_device *usbhid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 struct hid_device *hid;
Jiri Slaby131d3a7a2008-11-14 12:03:47 +01001270 unsigned int n, has_in = 0;
Jiri Slabyc500c972008-05-16 11:49:16 +02001271 size_t len;
1272 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Jiri Kosina58037eb2007-05-30 15:07:13 +02001274 dbg_hid("HID probe called for ifnum %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 intf->altsetting->desc.bInterfaceNumber);
1276
Jiri Slaby131d3a7a2008-11-14 12:03:47 +01001277 for (n = 0; n < interface->desc.bNumEndpoints; n++)
1278 if (usb_endpoint_is_int_in(&interface->endpoint[n].desc))
1279 has_in++;
1280 if (!has_in) {
Joe Perches4291ee32010-12-09 19:29:03 -08001281 hid_err(intf, "couldn't find an input interrupt endpoint\n");
Jiri Slaby131d3a7a2008-11-14 12:03:47 +01001282 return -ENODEV;
1283 }
1284
Jiri Slabyc500c972008-05-16 11:49:16 +02001285 hid = hid_allocate_device();
1286 if (IS_ERR(hid))
1287 return PTR_ERR(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 usb_set_intfdata(intf, hid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001290 hid->ll_driver = &usb_hid_driver;
Alan Ottb4dbde92011-01-18 03:04:39 -05001291 hid->hid_get_raw_report = usbhid_get_raw_report;
Jiri Slabyc500c972008-05-16 11:49:16 +02001292 hid->hid_output_raw_report = usbhid_output_raw_report;
Jiri Slaby76483cf2008-09-18 12:23:33 +02001293 hid->ff_init = hid_pidff_init;
Jiri Slabyc500c972008-05-16 11:49:16 +02001294#ifdef CONFIG_USB_HIDDEV
Jiri Slaby93c10132008-06-27 00:04:24 +02001295 hid->hiddev_connect = hiddev_connect;
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001296 hid->hiddev_disconnect = hiddev_disconnect;
Jiri Slabyc500c972008-05-16 11:49:16 +02001297 hid->hiddev_hid_event = hiddev_hid_event;
1298 hid->hiddev_report_event = hiddev_report_event;
1299#endif
1300 hid->dev.parent = &intf->dev;
1301 hid->bus = BUS_USB;
1302 hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
1303 hid->product = le16_to_cpu(dev->descriptor.idProduct);
1304 hid->name[0] = 0;
Bastien Nocerab5e5a372010-04-16 17:19:50 +01001305 hid->quirks = usbhid_lookup_quirk(hid->vendor, hid->product);
Jiri Slabya73a6372008-10-22 14:45:11 +02001306 if (intf->cur_altsetting->desc.bInterfaceProtocol ==
1307 USB_INTERFACE_PROTOCOL_MOUSE)
1308 hid->type = HID_TYPE_USBMOUSE;
Tomoki Sekiyama6dc14182011-05-23 15:45:44 -07001309 else if (intf->cur_altsetting->desc.bInterfaceProtocol == 0)
1310 hid->type = HID_TYPE_USBNONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Jiri Slabyc500c972008-05-16 11:49:16 +02001312 if (dev->manufacturer)
1313 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
1314
1315 if (dev->product) {
1316 if (dev->manufacturer)
1317 strlcat(hid->name, " ", sizeof(hid->name));
1318 strlcat(hid->name, dev->product, sizeof(hid->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
1320
Jiri Slabyc500c972008-05-16 11:49:16 +02001321 if (!strlen(hid->name))
1322 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
1323 le16_to_cpu(dev->descriptor.idVendor),
1324 le16_to_cpu(dev->descriptor.idProduct));
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001325
Jiri Slabyc500c972008-05-16 11:49:16 +02001326 usb_make_path(dev, hid->phys, sizeof(hid->phys));
1327 strlcat(hid->phys, "/input", sizeof(hid->phys));
1328 len = strlen(hid->phys);
1329 if (len < sizeof(hid->phys) - 1)
1330 snprintf(hid->phys + len, sizeof(hid->phys) - len,
1331 "%d", intf->altsetting[0].desc.bInterfaceNumber);
Geoff Levand4a1a4d82007-01-15 20:11:52 -08001332
Jiri Slabyc500c972008-05-16 11:49:16 +02001333 if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
1334 hid->uniq[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001336 usbhid = kzalloc(sizeof(*usbhid), GFP_KERNEL);
1337 if (usbhid == NULL) {
1338 ret = -ENOMEM;
1339 goto err;
1340 }
1341
1342 hid->driver_data = usbhid;
1343 usbhid->hid = hid;
Jiri Kosina57ab12e2010-02-17 14:25:01 +01001344 usbhid->intf = intf;
1345 usbhid->ifnum = interface->desc.bInterfaceNumber;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001346
Alan Sternfde4e2f2010-05-07 10:41:10 -04001347 init_waitqueue_head(&usbhid->wait);
1348 INIT_WORK(&usbhid->reset_work, hid_reset);
Alan Sternfde4e2f2010-05-07 10:41:10 -04001349 setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
1350 spin_lock_init(&usbhid->lock);
1351
Daniel Kurtz4371ea82011-11-17 19:23:50 +08001352 INIT_WORK(&usbhid->led_work, hid_led);
1353
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001354 ret = hid_add_device(hid);
1355 if (ret) {
Jiri Slabyd458a9d2008-05-16 11:49:20 +02001356 if (ret != -ENODEV)
Joe Perches4291ee32010-12-09 19:29:03 -08001357 hid_err(intf, "can't add hid device: %d\n", ret);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001358 goto err_free;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001359 }
Jiri Slabyc500c972008-05-16 11:49:16 +02001360
1361 return 0;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001362err_free:
1363 kfree(usbhid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001364err:
1365 hid_destroy_device(hid);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001366 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367}
1368
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001369static void usbhid_disconnect(struct usb_interface *intf)
Jiri Slabyc500c972008-05-16 11:49:16 +02001370{
1371 struct hid_device *hid = usb_get_intfdata(intf);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001372 struct usbhid_device *usbhid;
Jiri Slabyc500c972008-05-16 11:49:16 +02001373
1374 if (WARN_ON(!hid))
1375 return;
1376
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001377 usbhid = hid->driver_data;
Jiri Slabyc500c972008-05-16 11:49:16 +02001378 hid_destroy_device(hid);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001379 kfree(usbhid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001380}
1381
Oliver Neukum0361a282008-12-17 15:38:03 +01001382static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
Oliver Neukum0361a282008-12-17 15:38:03 +01001384 del_timer_sync(&usbhid->io_retry);
Oliver Neukum0361a282008-12-17 15:38:03 +01001385 cancel_work_sync(&usbhid->reset_work);
Daniel Kurtz4371ea82011-11-17 19:23:50 +08001386 cancel_work_sync(&usbhid->led_work);
Oliver Neukum0361a282008-12-17 15:38:03 +01001387}
1388
1389static void hid_cease_io(struct usbhid_device *usbhid)
1390{
Oliver Neukumfad9fbe2011-10-13 18:21:58 +02001391 del_timer_sync(&usbhid->io_retry);
Oliver Neukum0361a282008-12-17 15:38:03 +01001392 usb_kill_urb(usbhid->urbin);
1393 usb_kill_urb(usbhid->urbctrl);
1394 usb_kill_urb(usbhid->urbout);
Oliver Neukum0361a282008-12-17 15:38:03 +01001395}
1396
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001397/* Treat USB reset pretty much the same as suspend/resume */
1398static int hid_pre_reset(struct usb_interface *intf)
1399{
1400 struct hid_device *hid = usb_get_intfdata(intf);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001401 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001403 spin_lock_irq(&usbhid->lock);
1404 set_bit(HID_RESET_PENDING, &usbhid->iofl);
1405 spin_unlock_irq(&usbhid->lock);
1406 hid_cease_io(usbhid);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001407
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001408 return 0;
1409}
1410
1411/* Same routine used for post_reset and reset_resume */
1412static int hid_post_reset(struct usb_interface *intf)
1413{
1414 struct usb_device *dev = interface_to_usbdev (intf);
1415 struct hid_device *hid = usb_get_intfdata(intf);
1416 struct usbhid_device *usbhid = hid->driver_data;
Simon Haggettdc3c78e2012-04-03 16:04:15 +01001417 struct usb_host_interface *interface = intf->cur_altsetting;
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001418 int status;
Simon Haggettdc3c78e2012-04-03 16:04:15 +01001419 char *rdesc;
1420
1421 /* Fetch and examine the HID report descriptor. If this
1422 * has changed, then rebind. Since usbcore's check of the
1423 * configuration descriptors passed, we already know that
1424 * the size of the HID report descriptor has not changed.
1425 */
1426 rdesc = kmalloc(hid->rsize, GFP_KERNEL);
1427 if (!rdesc) {
1428 dbg_hid("couldn't allocate rdesc memory (post_reset)\n");
1429 return 1;
1430 }
1431 status = hid_get_class_descriptor(dev,
1432 interface->desc.bInterfaceNumber,
1433 HID_DT_REPORT, rdesc, hid->rsize);
1434 if (status < 0) {
1435 dbg_hid("reading report descriptor failed (post_reset)\n");
1436 kfree(rdesc);
1437 return 1;
1438 }
1439 status = memcmp(rdesc, hid->rdesc, hid->rsize);
1440 kfree(rdesc);
1441 if (status != 0) {
1442 dbg_hid("report descriptor changed\n");
1443 return 1;
1444 }
Jiri Kosina70fa9f22009-06-04 15:48:38 +02001445
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001446 spin_lock_irq(&usbhid->lock);
1447 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
1448 spin_unlock_irq(&usbhid->lock);
1449 hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001450 status = hid_start_in(hid);
1451 if (status < 0)
1452 hid_io_error(hid);
1453 usbhid_restart_queues(usbhid);
1454
1455 return 0;
1456}
1457
1458int usbhid_get_power(struct hid_device *hid)
1459{
1460 struct usbhid_device *usbhid = hid->driver_data;
Jiri Kosina70fa9f22009-06-04 15:48:38 +02001461
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001462 return usb_autopm_get_interface(usbhid->intf);
1463}
1464
1465void usbhid_put_power(struct hid_device *hid)
1466{
1467 struct usbhid_device *usbhid = hid->driver_data;
Jiri Kosina70fa9f22009-06-04 15:48:38 +02001468
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001469 usb_autopm_put_interface(usbhid->intf);
1470}
1471
1472
Jiri Kosina0f6f1402009-01-19 09:17:18 +01001473#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474static int hid_suspend(struct usb_interface *intf, pm_message_t message)
1475{
Oliver Neukum0361a282008-12-17 15:38:03 +01001476 struct hid_device *hid = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 struct usbhid_device *usbhid = hid->driver_data;
Oliver Neukum0361a282008-12-17 15:38:03 +01001478 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Alan Stern5b1b0b82011-08-19 23:49:48 +02001480 if (PMSG_IS_AUTO(message)) {
Oliver Neukum0361a282008-12-17 15:38:03 +01001481 spin_lock_irq(&usbhid->lock); /* Sync with error handler */
1482 if (!test_bit(HID_RESET_PENDING, &usbhid->iofl)
1483 && !test_bit(HID_CLEAR_HALT, &usbhid->iofl)
1484 && !test_bit(HID_OUT_RUNNING, &usbhid->iofl)
1485 && !test_bit(HID_CTRL_RUNNING, &usbhid->iofl)
1486 && !test_bit(HID_KEYS_PRESSED, &usbhid->iofl)
1487 && (!usbhid->ledcount || ignoreled))
1488 {
1489 set_bit(HID_REPORTED_IDLE, &usbhid->iofl);
1490 spin_unlock_irq(&usbhid->lock);
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001491 if (hid->driver && hid->driver->suspend) {
1492 status = hid->driver->suspend(hid, message);
1493 if (status < 0)
1494 return status;
1495 }
Oliver Neukum0361a282008-12-17 15:38:03 +01001496 } else {
1497 usbhid_mark_busy(usbhid);
1498 spin_unlock_irq(&usbhid->lock);
1499 return -EBUSY;
1500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Oliver Neukum0361a282008-12-17 15:38:03 +01001502 } else {
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001503 if (hid->driver && hid->driver->suspend) {
1504 status = hid->driver->suspend(hid, message);
1505 if (status < 0)
1506 return status;
1507 }
Oliver Neukum0361a282008-12-17 15:38:03 +01001508 spin_lock_irq(&usbhid->lock);
1509 set_bit(HID_REPORTED_IDLE, &usbhid->iofl);
1510 spin_unlock_irq(&usbhid->lock);
1511 if (usbhid_wait_io(hid) < 0)
1512 return -EIO;
1513 }
1514
Oliver Neukum0361a282008-12-17 15:38:03 +01001515 hid_cancel_delayed_stuff(usbhid);
1516 hid_cease_io(usbhid);
1517
Alan Stern5b1b0b82011-08-19 23:49:48 +02001518 if (PMSG_IS_AUTO(message) && test_bit(HID_KEYS_PRESSED, &usbhid->iofl)) {
Oliver Neukum0361a282008-12-17 15:38:03 +01001519 /* lost race against keypresses */
1520 status = hid_start_in(hid);
1521 if (status < 0)
1522 hid_io_error(hid);
1523 usbhid_mark_busy(usbhid);
1524 return -EBUSY;
1525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 dev_dbg(&intf->dev, "suspend\n");
1527 return 0;
1528}
1529
1530static int hid_resume(struct usb_interface *intf)
1531{
1532 struct hid_device *hid = usb_get_intfdata (intf);
1533 struct usbhid_device *usbhid = hid->driver_data;
1534 int status;
1535
Jiri Slabyfde5be32008-11-23 12:03:20 +01001536 if (!test_bit(HID_STARTED, &usbhid->iofl))
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001537 return 0;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001538
Oliver Neukum0361a282008-12-17 15:38:03 +01001539 clear_bit(HID_REPORTED_IDLE, &usbhid->iofl);
1540 usbhid_mark_busy(usbhid);
1541
1542 if (test_bit(HID_CLEAR_HALT, &usbhid->iofl) ||
1543 test_bit(HID_RESET_PENDING, &usbhid->iofl))
1544 schedule_work(&usbhid->reset_work);
Alan Sterndf9a1f42006-06-01 13:55:28 -04001545 usbhid->retry_delay = 0;
1546 status = hid_start_in(hid);
Oliver Neukum0361a282008-12-17 15:38:03 +01001547 if (status < 0)
1548 hid_io_error(hid);
1549 usbhid_restart_queues(usbhid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001551 if (status >= 0 && hid->driver && hid->driver->resume) {
1552 int ret = hid->driver->resume(hid);
1553 if (ret < 0)
1554 status = ret;
1555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 dev_dbg(&intf->dev, "resume status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 return 0;
1558}
1559
Oliver Neukum378a0ed2009-02-18 11:46:45 +01001560static int hid_reset_resume(struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
Oliver Neukum378a0ed2009-02-18 11:46:45 +01001562 struct hid_device *hid = usb_get_intfdata(intf);
1563 struct usbhid_device *usbhid = hid->driver_data;
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001564 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Oliver Neukum378a0ed2009-02-18 11:46:45 +01001566 clear_bit(HID_REPORTED_IDLE, &usbhid->iofl);
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001567 status = hid_post_reset(intf);
1568 if (status >= 0 && hid->driver && hid->driver->reset_resume) {
1569 int ret = hid->driver->reset_resume(hid);
1570 if (ret < 0)
1571 status = ret;
1572 }
1573 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574}
1575
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001576#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Márton Némethd67dec52010-01-10 17:59:22 +01001578static const struct usb_device_id hid_usb_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1580 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
1581 { } /* Terminating entry */
1582};
1583
1584MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1585
1586static struct usb_driver hid_driver = {
1587 .name = "usbhid",
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001588 .probe = usbhid_probe,
1589 .disconnect = usbhid_disconnect,
Jiri Kosina0f6f1402009-01-19 09:17:18 +01001590#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 .suspend = hid_suspend,
1592 .resume = hid_resume,
Oliver Neukum378a0ed2009-02-18 11:46:45 +01001593 .reset_resume = hid_reset_resume,
Jiri Kosina0f6f1402009-01-19 09:17:18 +01001594#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 .pre_reset = hid_pre_reset,
1596 .post_reset = hid_post_reset,
1597 .id_table = hid_usb_ids,
Oliver Neukum933e3182007-07-11 14:48:58 +02001598 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599};
1600
Guillaume Chazarain8fe294c2010-09-12 21:32:35 +02001601struct usb_interface *usbhid_find_interface(int minor)
1602{
1603 return usb_find_interface(&hid_driver, minor);
1604}
1605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606static int __init hid_init(void)
1607{
Oliver Neukum0361a282008-12-17 15:38:03 +01001608 int retval = -ENOMEM;
1609
Paul Walmsley876b9272007-04-19 14:56:12 +02001610 retval = usbhid_quirks_init(quirks_param);
1611 if (retval)
1612 goto usbhid_quirks_init_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 retval = usb_register(&hid_driver);
1614 if (retval)
1615 goto usb_register_fail;
Jiri Kosinaccabcd22009-10-02 18:31:36 +02001616 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
1618 return 0;
1619usb_register_fail:
Paul Walmsley876b9272007-04-19 14:56:12 +02001620 usbhid_quirks_exit();
1621usbhid_quirks_init_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 return retval;
1623}
1624
1625static void __exit hid_exit(void)
1626{
1627 usb_deregister(&hid_driver);
Paul Walmsley876b9272007-04-19 14:56:12 +02001628 usbhid_quirks_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629}
1630
1631module_init(hid_init);
1632module_exit(hid_exit);
1633
Jiri Kosina88adb722009-10-02 18:29:34 +02001634MODULE_AUTHOR("Andreas Gal");
1635MODULE_AUTHOR("Vojtech Pavlik");
1636MODULE_AUTHOR("Jiri Kosina");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637MODULE_DESCRIPTION(DRIVER_DESC);
1638MODULE_LICENSE(DRIVER_LICENSE);