blob: 6b9bad54070239bcf22602fd066e19adb0ac822e [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);
Alan Stern668160e2012-07-19 16:08:21 -0400334 if (raw_report) {
335 memcpy(usbhid->outbuf, raw_report,
336 usbhid->urbout->transfer_buffer_length);
337 kfree(raw_report);
338 usbhid->out[usbhid->outtail].raw_report = NULL;
339 }
Oliver Neukum68229682010-12-22 15:33:40 +0100340
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800341 dbg_hid("submitting out urb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800343 r = usb_submit_urb(usbhid->urbout, GFP_ATOMIC);
344 if (r < 0) {
345 hid_err(hid, "usb_submit_urb(out) failed: %d\n", r);
346 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800348 usbhid->last_out = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return 0;
350}
351
352static int hid_submit_ctrl(struct hid_device *hid)
353{
354 struct hid_report *report;
355 unsigned char dir;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200356 char *raw_report;
Oliver Neukum68229682010-12-22 15:33:40 +0100357 int len, r;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100358 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100360 report = usbhid->ctrl[usbhid->ctrltail].report;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200361 raw_report = usbhid->ctrl[usbhid->ctrltail].raw_report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100362 dir = usbhid->ctrl[usbhid->ctrltail].dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800364 len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
365 if (dir == USB_DIR_OUT) {
366 usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
367 usbhid->urbctrl->transfer_buffer_length = len;
Alan Stern668160e2012-07-19 16:08:21 -0400368 if (raw_report) {
369 memcpy(usbhid->ctrlbuf, raw_report, len);
370 kfree(raw_report);
371 usbhid->ctrl[usbhid->ctrltail].raw_report = NULL;
372 }
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800373 } else {
374 int maxpacket, padlen;
Oliver Neukum0361a282008-12-17 15:38:03 +0100375
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800376 usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0);
377 maxpacket = usb_maxpacket(hid_to_usb_dev(hid),
378 usbhid->urbctrl->pipe, 0);
379 if (maxpacket > 0) {
380 padlen = DIV_ROUND_UP(len, maxpacket);
381 padlen *= maxpacket;
382 if (padlen > usbhid->bufsize)
383 padlen = usbhid->bufsize;
384 } else
385 padlen = 0;
386 usbhid->urbctrl->transfer_buffer_length = padlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800388 usbhid->urbctrl->dev = hid_to_usb_dev(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800390 usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
391 usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT :
392 HID_REQ_GET_REPORT;
393 usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) |
394 report->id);
395 usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum);
396 usbhid->cr->wLength = cpu_to_le16(len);
397
398 dbg_hid("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u\n",
399 usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" :
400 "Get_Report",
401 usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength);
402
403 r = usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC);
404 if (r < 0) {
405 hid_err(hid, "usb_submit_urb(ctrl) failed: %d\n", r);
406 return r;
407 }
408 usbhid->last_ctrl = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return 0;
410}
411
412/*
413 * Output interrupt completion handler.
414 */
415
Oliver Neukum8815bb02012-04-30 09:13:46 +0200416static int irq_out_pump_restart(struct hid_device *hid)
417{
418 struct usbhid_device *usbhid = hid->driver_data;
419
420 if (usbhid->outhead != usbhid->outtail)
421 return hid_submit_out(hid);
422 else
423 return -1;
424}
425
David Howells7d12e782006-10-05 14:55:46 +0100426static void hid_irq_out(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
428 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100429 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 unsigned long flags;
431 int unplug = 0;
432
433 switch (urb->status) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200434 case 0: /* success */
435 break;
436 case -ESHUTDOWN: /* unplug */
437 unplug = 1;
438 case -EILSEQ: /* protocol error or unplug */
439 case -EPROTO: /* protocol error or unplug */
440 case -ECONNRESET: /* unlink */
441 case -ENOENT:
442 break;
443 default: /* error */
Joe Perches4291ee32010-12-09 19:29:03 -0800444 hid_warn(urb->dev, "output irq status %d received\n",
445 urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
447
Oliver Neukum0361a282008-12-17 15:38:03 +0100448 spin_lock_irqsave(&usbhid->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 if (unplug)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100451 usbhid->outtail = usbhid->outhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 else
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100453 usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Oliver Neukum8815bb02012-04-30 09:13:46 +0200455 if (!irq_out_pump_restart(hid)) {
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800456 /* Successfully submitted next urb in queue */
Oliver Neukum0361a282008-12-17 15:38:03 +0100457 spin_unlock_irqrestore(&usbhid->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return;
459 }
460
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100461 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +0100462 spin_unlock_irqrestore(&usbhid->lock, flags);
Oliver Neukum68229682010-12-22 15:33:40 +0100463 usb_autopm_put_interface_async(usbhid->intf);
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100464 wake_up(&usbhid->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
467/*
468 * Control pipe completion handler.
469 */
Oliver Neukum8815bb02012-04-30 09:13:46 +0200470static int ctrl_pump_restart(struct hid_device *hid)
471{
472 struct usbhid_device *usbhid = hid->driver_data;
473
474 if (usbhid->ctrlhead != usbhid->ctrltail)
475 return hid_submit_ctrl(hid);
476 else
477 return -1;
478}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
David Howells7d12e782006-10-05 14:55:46 +0100480static void hid_ctrl(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100483 struct usbhid_device *usbhid = hid->driver_data;
Oliver Neukum0361a282008-12-17 15:38:03 +0100484 int unplug = 0, status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Oliver Neukum0361a282008-12-17 15:38:03 +0100486 spin_lock(&usbhid->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Oliver Neukum0361a282008-12-17 15:38:03 +0100488 switch (status) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200489 case 0: /* success */
490 if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
491 hid_input_report(urb->context,
492 usbhid->ctrl[usbhid->ctrltail].report->type,
493 urb->transfer_buffer, urb->actual_length, 0);
494 break;
495 case -ESHUTDOWN: /* unplug */
496 unplug = 1;
497 case -EILSEQ: /* protocol error or unplug */
498 case -EPROTO: /* protocol error or unplug */
499 case -ECONNRESET: /* unlink */
500 case -ENOENT:
501 case -EPIPE: /* report not available */
502 break;
503 default: /* error */
Joe Perches4291ee32010-12-09 19:29:03 -0800504 hid_warn(urb->dev, "ctrl urb status %d received\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
506
507 if (unplug)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100508 usbhid->ctrltail = usbhid->ctrlhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 else
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100510 usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Oliver Neukum8815bb02012-04-30 09:13:46 +0200512 if (!ctrl_pump_restart(hid)) {
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800513 /* Successfully submitted next urb in queue */
Oliver Neukum0361a282008-12-17 15:38:03 +0100514 spin_unlock(&usbhid->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return;
516 }
517
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100518 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +0100519 spin_unlock(&usbhid->lock);
Oliver Neukum68229682010-12-22 15:33:40 +0100520 usb_autopm_put_interface_async(usbhid->intf);
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100521 wake_up(&usbhid->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
H Hartley Sweeten52cfc612009-08-17 15:37:18 -0700524static void __usbhid_submit_report(struct hid_device *hid, struct hid_report *report,
525 unsigned char dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
527 int head;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100528 struct usbhid_device *usbhid = hid->driver_data;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200529 int len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
532 return;
533
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100534 if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100535 if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
Joe Perches4291ee32010-12-09 19:29:03 -0800536 hid_warn(hid, "output queue full\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return;
538 }
539
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200540 usbhid->out[usbhid->outhead].raw_report = kmalloc(len, GFP_ATOMIC);
541 if (!usbhid->out[usbhid->outhead].raw_report) {
Joe Perches4291ee32010-12-09 19:29:03 -0800542 hid_warn(hid, "output queueing failed\n");
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200543 return;
544 }
545 hid_output_report(report, usbhid->out[usbhid->outhead].raw_report);
546 usbhid->out[usbhid->outhead].report = report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100547 usbhid->outhead = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800549 /* Try to awake from autosuspend... */
550 if (usb_autopm_get_interface_async(usbhid->intf) < 0)
551 return;
552
553 /*
554 * But if still suspended, leave urb enqueued, don't submit.
555 * Submission will occur if/when resume() drains the queue.
556 */
557 if (test_bit(HID_REPORTED_IDLE, &usbhid->iofl))
558 return;
559
Oliver Neukum858155f2010-02-12 13:02:28 +0100560 if (!test_and_set_bit(HID_OUT_RUNNING, &usbhid->iofl)) {
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800561 if (hid_submit_out(hid)) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100562 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800563 usb_autopm_put_interface_async(usbhid->intf);
564 }
565 wake_up(&usbhid->wait);
Oliver Neukum858155f2010-02-12 13:02:28 +0100566 } else {
567 /*
568 * the queue is known to run
569 * but an earlier request may be stuck
570 * we may need to time out
Oliver Neukum8815bb02012-04-30 09:13:46 +0200571 * no race because the URB is blocked under
Oliver Neukum858155f2010-02-12 13:02:28 +0100572 * spinlock
573 */
Oliver Neukum8815bb02012-04-30 09:13:46 +0200574 if (time_after(jiffies, usbhid->last_out + HZ * 5)) {
575 usb_block_urb(usbhid->urbout);
576 /* drop lock to not deadlock if the callback is called */
577 spin_unlock(&usbhid->lock);
Oliver Neukum858155f2010-02-12 13:02:28 +0100578 usb_unlink_urb(usbhid->urbout);
Oliver Neukum8815bb02012-04-30 09:13:46 +0200579 spin_lock(&usbhid->lock);
580 usb_unblock_urb(usbhid->urbout);
581 /*
582 * if the unlinking has already completed
583 * the pump will have been stopped
584 * it must be restarted now
585 */
586 if (!test_bit(HID_OUT_RUNNING, &usbhid->iofl))
587 if (!irq_out_pump_restart(hid))
588 set_bit(HID_OUT_RUNNING, &usbhid->iofl);
589
590
591 }
Oliver Neukum858155f2010-02-12 13:02:28 +0100592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return;
594 }
595
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100596 if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
Joe Perches4291ee32010-12-09 19:29:03 -0800597 hid_warn(hid, "control queue full\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return;
599 }
600
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200601 if (dir == USB_DIR_OUT) {
602 usbhid->ctrl[usbhid->ctrlhead].raw_report = kmalloc(len, GFP_ATOMIC);
603 if (!usbhid->ctrl[usbhid->ctrlhead].raw_report) {
Joe Perches4291ee32010-12-09 19:29:03 -0800604 hid_warn(hid, "control queueing failed\n");
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200605 return;
606 }
607 hid_output_report(report, usbhid->ctrl[usbhid->ctrlhead].raw_report);
608 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100609 usbhid->ctrl[usbhid->ctrlhead].report = report;
610 usbhid->ctrl[usbhid->ctrlhead].dir = dir;
611 usbhid->ctrlhead = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800613 /* Try to awake from autosuspend... */
614 if (usb_autopm_get_interface_async(usbhid->intf) < 0)
615 return;
616
617 /*
618 * If already suspended, leave urb enqueued, but don't submit.
619 * Submission will occur if/when resume() drains the queue.
620 */
621 if (test_bit(HID_REPORTED_IDLE, &usbhid->iofl))
622 return;
623
Oliver Neukum858155f2010-02-12 13:02:28 +0100624 if (!test_and_set_bit(HID_CTRL_RUNNING, &usbhid->iofl)) {
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800625 if (hid_submit_ctrl(hid)) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100626 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800627 usb_autopm_put_interface_async(usbhid->intf);
628 }
629 wake_up(&usbhid->wait);
Oliver Neukum858155f2010-02-12 13:02:28 +0100630 } else {
631 /*
632 * the queue is known to run
633 * but an earlier request may be stuck
634 * we may need to time out
Oliver Neukum8815bb02012-04-30 09:13:46 +0200635 * no race because the URB is blocked under
Oliver Neukum858155f2010-02-12 13:02:28 +0100636 * spinlock
637 */
Oliver Neukum8815bb02012-04-30 09:13:46 +0200638 if (time_after(jiffies, usbhid->last_ctrl + HZ * 5)) {
639 usb_block_urb(usbhid->urbctrl);
640 /* drop lock to not deadlock if the callback is called */
641 spin_unlock(&usbhid->lock);
Oliver Neukum858155f2010-02-12 13:02:28 +0100642 usb_unlink_urb(usbhid->urbctrl);
Oliver Neukum8815bb02012-04-30 09:13:46 +0200643 spin_lock(&usbhid->lock);
644 usb_unblock_urb(usbhid->urbctrl);
645 /*
646 * if the unlinking has already completed
647 * the pump will have been stopped
648 * it must be restarted now
649 */
650 if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
651 if (!ctrl_pump_restart(hid))
652 set_bit(HID_CTRL_RUNNING, &usbhid->iofl);
653 }
Oliver Neukum858155f2010-02-12 13:02:28 +0100654 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100655}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Oliver Neukum0361a282008-12-17 15:38:03 +0100657void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
658{
659 struct usbhid_device *usbhid = hid->driver_data;
660 unsigned long flags;
661
662 spin_lock_irqsave(&usbhid->lock, flags);
663 __usbhid_submit_report(hid, report, dir);
664 spin_unlock_irqrestore(&usbhid->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
Jiri Slaby606bd0a2008-07-04 23:06:45 +0200666EXPORT_SYMBOL_GPL(usbhid_submit_report);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Daniel Kurtz4371ea82011-11-17 19:23:50 +0800668/* Workqueue routine to send requests to change LEDs */
669static void hid_led(struct work_struct *work)
670{
671 struct usbhid_device *usbhid =
672 container_of(work, struct usbhid_device, led_work);
673 struct hid_device *hid = usbhid->hid;
674 struct hid_field *field;
675 unsigned long flags;
676
677 field = hidinput_get_led_field(hid);
678 if (!field) {
679 hid_warn(hid, "LED event field not found\n");
680 return;
681 }
682
683 spin_lock_irqsave(&usbhid->lock, flags);
684 if (!test_bit(HID_DISCONNECTED, &usbhid->iofl)) {
685 usbhid->ledcount = hidinput_count_leds(hid);
686 hid_dbg(usbhid->hid, "New ledcount = %u\n", usbhid->ledcount);
687 __usbhid_submit_report(hid, field->report, USB_DIR_OUT);
688 }
689 spin_unlock_irqrestore(&usbhid->lock, flags);
690}
691
Jiri Kosina229695e2006-12-08 18:40:53 +0100692static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
Jiri Kosinadde58452006-12-08 18:40:44 +0100693{
Dmitry Torokhove0712982007-05-09 10:17:31 +0200694 struct hid_device *hid = input_get_drvdata(dev);
Oliver Neukum0361a282008-12-17 15:38:03 +0100695 struct usbhid_device *usbhid = hid->driver_data;
Jiri Kosinadde58452006-12-08 18:40:44 +0100696 struct hid_field *field;
Oliver Neukum0361a282008-12-17 15:38:03 +0100697 unsigned long flags;
Jiri Kosinadde58452006-12-08 18:40:44 +0100698 int offset;
699
700 if (type == EV_FF)
701 return input_ff_event(dev, type, code, value);
702
703 if (type != EV_LED)
704 return -1;
705
706 if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
Joe Perches4291ee32010-12-09 19:29:03 -0800707 hid_warn(dev, "event field not found\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100708 return -1;
709 }
710
Daniel Kurtz4371ea82011-11-17 19:23:50 +0800711 spin_lock_irqsave(&usbhid->lock, flags);
Jiri Kosinadde58452006-12-08 18:40:44 +0100712 hid_set_field(field, offset, value);
Daniel Kurtz4371ea82011-11-17 19:23:50 +0800713 spin_unlock_irqrestore(&usbhid->lock, flags);
714
715 /*
716 * Defer performing requested LED action.
717 * This is more likely gather all LED changes into a single URB.
718 */
719 schedule_work(&usbhid->led_work);
Jiri Kosinadde58452006-12-08 18:40:44 +0100720
721 return 0;
722}
723
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100724int usbhid_wait_io(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100726 struct usbhid_device *usbhid = hid->driver_data;
727
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100728 if (!wait_event_timeout(usbhid->wait,
729 (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
730 !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 10*HZ)) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200732 dbg_hid("timeout waiting for ctrl or out queue to clear\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return -1;
734 }
735
736 return 0;
737}
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200738EXPORT_SYMBOL_GPL(usbhid_wait_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Vojtech Pavlik854561b2005-05-29 02:28:00 -0500740static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
741{
Vojtech Pavlik71387bd2005-05-29 02:28:14 -0500742 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
Vojtech Pavlik854561b2005-05-29 02:28:00 -0500743 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
744 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
745}
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
748 unsigned char type, void *buf, int size)
749{
750 int result, retries = 4;
751
Jiri Kosina43c7bf02007-01-26 12:58:24 +0100752 memset(buf, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 do {
755 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
756 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
757 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
758 retries--;
759 } while (result < size && retries);
760 return result;
761}
762
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100763int usbhid_open(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Oliver Neukum933e3182007-07-11 14:48:58 +0200765 struct usbhid_device *usbhid = hid->driver_data;
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200766 int res = 0;
Oliver Neukum933e3182007-07-11 14:48:58 +0200767
Oliver Neukum0361a282008-12-17 15:38:03 +0100768 mutex_lock(&hid_open_mut);
Oliver Neukum933e3182007-07-11 14:48:58 +0200769 if (!hid->open++) {
770 res = usb_autopm_get_interface(usbhid->intf);
Oliver Neukum68229682010-12-22 15:33:40 +0100771 /* the device must be awake to reliably request remote wakeup */
Oliver Neukum933e3182007-07-11 14:48:58 +0200772 if (res < 0) {
773 hid->open--;
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200774 res = -EIO;
775 goto done;
Oliver Neukum933e3182007-07-11 14:48:58 +0200776 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100777 usbhid->intf->needs_remote_wakeup = 1;
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200778 res = hid_start_in(hid);
779 if (res) {
780 if (res != -ENOSPC) {
781 hid_io_error(hid);
782 res = 0;
783 } else {
784 /* no use opening if resources are insufficient */
785 hid->open--;
786 res = -EBUSY;
787 usbhid->intf->needs_remote_wakeup = 0;
788 }
789 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100790 usb_autopm_put_interface(usbhid->intf);
Oliver Neukum933e3182007-07-11 14:48:58 +0200791 }
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200792done:
Oliver Neukum0361a282008-12-17 15:38:03 +0100793 mutex_unlock(&hid_open_mut);
Oliver Neukuma8c52b62012-03-28 13:16:19 +0200794 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100797void usbhid_close(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100799 struct usbhid_device *usbhid = hid->driver_data;
800
Oliver Neukum0361a282008-12-17 15:38:03 +0100801 mutex_lock(&hid_open_mut);
802
803 /* protecting hid->open to make sure we don't restart
804 * data acquistion due to a resumption we no longer
805 * care about
806 */
807 spin_lock_irq(&usbhid->lock);
Oliver Neukum933e3182007-07-11 14:48:58 +0200808 if (!--hid->open) {
Oliver Neukum0361a282008-12-17 15:38:03 +0100809 spin_unlock_irq(&usbhid->lock);
Oliver Neukum89092dd2009-04-29 17:12:12 +0200810 hid_cancel_delayed_stuff(usbhid);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100811 usb_kill_urb(usbhid->urbin);
Oliver Neukum0361a282008-12-17 15:38:03 +0100812 usbhid->intf->needs_remote_wakeup = 0;
813 } else {
814 spin_unlock_irq(&usbhid->lock);
Oliver Neukum933e3182007-07-11 14:48:58 +0200815 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100816 mutex_unlock(&hid_open_mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818
819/*
820 * Initialize all reports
821 */
822
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100823void usbhid_init_reports(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
825 struct hid_report *report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100826 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 int err, ret;
828
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500829 list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100830 usbhid_submit_report(hid, report, USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100833 usbhid_submit_report(hid, report, USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 err = 0;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100836 ret = usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 while (ret) {
838 err |= ret;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100839 if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
840 usb_kill_urb(usbhid->urbctrl);
841 if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
842 usb_kill_urb(usbhid->urbout);
843 ret = usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
845
846 if (err)
Joe Perches4291ee32010-12-09 19:29:03 -0800847 hid_warn(hid, "timeout initializing reports\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500850/*
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200851 * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
852 */
853static int hid_find_field_early(struct hid_device *hid, unsigned int page,
854 unsigned int hid_code, struct hid_field **pfield)
855{
856 struct hid_report *report;
857 struct hid_field *field;
858 struct hid_usage *usage;
859 int i, j;
860
861 list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
862 for (i = 0; i < report->maxfield; i++) {
863 field = report->field[i];
864 for (j = 0; j < field->maxusage; j++) {
865 usage = &field->usage[j];
866 if ((usage->hid & HID_USAGE_PAGE) == page &&
867 (usage->hid & 0xFFFF) == hid_code) {
868 *pfield = field;
869 return j;
870 }
871 }
872 }
873 }
874 return -1;
875}
876
Jiri Slaby6edfa8d2008-06-27 20:41:02 +0200877void usbhid_set_leds(struct hid_device *hid)
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200878{
879 struct hid_field *field;
880 int offset;
881
882 if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
883 hid_set_field(field, offset, 0);
884 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
885 }
886}
Jiri Slaby6edfa8d2008-06-27 20:41:02 +0200887EXPORT_SYMBOL_GPL(usbhid_set_leds);
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200888
889/*
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500890 * Traverse the supplied list of reports and find the longest
891 */
Jiri Slaby282bfd42008-03-28 17:06:41 +0100892static void hid_find_max_report(struct hid_device *hid, unsigned int type,
893 unsigned int *max)
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500894{
895 struct hid_report *report;
Jiri Slaby282bfd42008-03-28 17:06:41 +0100896 unsigned int size;
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500897
898 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
Jiri Kosinaefc7ce12008-10-17 15:01:15 +0200899 size = ((report->size - 1) >> 3) + 1 + hid->report_enum[type].numbered;
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500900 if (*max < size)
901 *max = size;
902 }
903}
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
906{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100907 struct usbhid_device *usbhid = hid->driver_data;
908
Daniel Mack997ea582010-04-12 13:17:25 +0200909 usbhid->inbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
Jiri Slaby898089d2008-11-24 16:20:06 +0100910 &usbhid->inbuf_dma);
Daniel Mack997ea582010-04-12 13:17:25 +0200911 usbhid->outbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
Jiri Slaby898089d2008-11-24 16:20:06 +0100912 &usbhid->outbuf_dma);
Alan Stern0ede76f2010-03-05 15:10:17 -0500913 usbhid->cr = kmalloc(sizeof(*usbhid->cr), GFP_KERNEL);
Daniel Mack997ea582010-04-12 13:17:25 +0200914 usbhid->ctrlbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
Jiri Slaby898089d2008-11-24 16:20:06 +0100915 &usbhid->ctrlbuf_dma);
916 if (!usbhid->inbuf || !usbhid->outbuf || !usbhid->cr ||
917 !usbhid->ctrlbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return -1;
919
920 return 0;
921}
922
Alan Ottb4dbde92011-01-18 03:04:39 -0500923static int usbhid_get_raw_report(struct hid_device *hid,
924 unsigned char report_number, __u8 *buf, size_t count,
925 unsigned char report_type)
926{
927 struct usbhid_device *usbhid = hid->driver_data;
928 struct usb_device *dev = hid_to_usb_dev(hid);
929 struct usb_interface *intf = usbhid->intf;
930 struct usb_host_interface *interface = intf->cur_altsetting;
931 int skipped_report_id = 0;
932 int ret;
933
934 /* Byte 0 is the report number. Report data starts at byte 1.*/
935 buf[0] = report_number;
936 if (report_number == 0x0) {
937 /* Offset the return buffer by 1, so that the report ID
938 will remain in byte 0. */
939 buf++;
940 count--;
941 skipped_report_id = 1;
942 }
943 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
944 HID_REQ_GET_REPORT,
945 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
946 ((report_type + 1) << 8) | report_number,
947 interface->desc.bInterfaceNumber, buf, count,
948 USB_CTRL_SET_TIMEOUT);
949
950 /* count also the report id */
951 if (ret > 0 && skipped_report_id)
952 ret++;
953
954 return ret;
955}
956
Jiri Kosinad4bfa032010-01-29 15:03:36 +0100957static int usbhid_output_raw_report(struct hid_device *hid, __u8 *buf, size_t count,
958 unsigned char report_type)
Jiri Kosinaefc493f2007-05-14 09:54:30 +0200959{
960 struct usbhid_device *usbhid = hid->driver_data;
961 struct usb_device *dev = hid_to_usb_dev(hid);
962 struct usb_interface *intf = usbhid->intf;
963 struct usb_host_interface *interface = intf->cur_altsetting;
964 int ret;
965
Alan Ottfe2c91e2010-09-22 13:19:42 +0200966 if (usbhid->urbout && report_type != HID_FEATURE_REPORT) {
Alan Otta8ab5d52010-05-16 18:07:09 -0400967 int actual_length;
968 int skipped_report_id = 0;
Alan Ott12e52722010-09-22 13:33:20 +0200969
Alan Otta8ab5d52010-05-16 18:07:09 -0400970 if (buf[0] == 0x0) {
971 /* Don't send the Report ID */
972 buf++;
973 count--;
974 skipped_report_id = 1;
975 }
976 ret = usb_interrupt_msg(dev, usbhid->urbout->pipe,
977 buf, count, &actual_length,
978 USB_CTRL_SET_TIMEOUT);
979 /* return the number of bytes transferred */
980 if (ret == 0) {
981 ret = actual_length;
982 /* count also the report id */
983 if (skipped_report_id)
984 ret++;
985 }
986 } else {
Alan Ott29129a92010-06-30 09:50:36 -0400987 int skipped_report_id = 0;
Alan Ottc29771c2010-08-17 00:44:04 -0400988 int report_id = buf[0];
Alan Ott29129a92010-06-30 09:50:36 -0400989 if (buf[0] == 0x0) {
990 /* Don't send the Report ID */
991 buf++;
992 count--;
993 skipped_report_id = 1;
994 }
Alan Otta8ab5d52010-05-16 18:07:09 -0400995 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
996 HID_REQ_SET_REPORT,
997 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
Alan Ottc29771c2010-08-17 00:44:04 -0400998 ((report_type + 1) << 8) | report_id,
Alan Ott29129a92010-06-30 09:50:36 -0400999 interface->desc.bInterfaceNumber, buf, count,
Alan Otta8ab5d52010-05-16 18:07:09 -04001000 USB_CTRL_SET_TIMEOUT);
Alan Ott29129a92010-06-30 09:50:36 -04001001 /* count also the report id, if this was a numbered report. */
1002 if (ret > 0 && skipped_report_id)
Alan Otta8ab5d52010-05-16 18:07:09 -04001003 ret++;
1004 }
Jiri Kosinaefc493f2007-05-14 09:54:30 +02001005
1006 return ret;
1007}
1008
Oliver Neukum0361a282008-12-17 15:38:03 +01001009static void usbhid_restart_queues(struct usbhid_device *usbhid)
1010{
1011 if (usbhid->urbout)
1012 usbhid_restart_out_queue(usbhid);
1013 usbhid_restart_ctrl_queue(usbhid);
1014}
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
1017{
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001018 struct usbhid_device *usbhid = hid->driver_data;
1019
Daniel Mack997ea582010-04-12 13:17:25 +02001020 usb_free_coherent(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
1021 usb_free_coherent(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
Alan Stern0ede76f2010-03-05 15:10:17 -05001022 kfree(usbhid->cr);
Daniel Mack997ea582010-04-12 13:17:25 +02001023 usb_free_coherent(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025
Jiri Slabyc500c972008-05-16 11:49:16 +02001026static int usbhid_parse(struct hid_device *hid)
1027{
1028 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 struct usb_host_interface *interface = intf->cur_altsetting;
1030 struct usb_device *dev = interface_to_usbdev (intf);
1031 struct hid_descriptor *hdesc;
Paul Walmsley2eb5dc32007-04-19 13:27:04 +02001032 u32 quirks = 0;
Jiri Slabyc500c972008-05-16 11:49:16 +02001033 unsigned int rsize = 0;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001034 char *rdesc;
Jiri Slabyc500c972008-05-16 11:49:16 +02001035 int ret, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Paul Walmsley2eb5dc32007-04-19 13:27:04 +02001037 quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
1038 le16_to_cpu(dev->descriptor.idProduct));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Jiri Kosina6f4303f2009-01-29 00:15:51 +01001040 if (quirks & HID_QUIRK_IGNORE)
1041 return -ENODEV;
1042
Alan Stern0f28b552006-05-15 14:49:04 -04001043 /* Many keyboards and mice don't like to be polled for reports,
1044 * so we will always set the HID_QUIRK_NOGET flag for them. */
1045 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
1046 if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
1047 interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
1048 quirks |= HID_QUIRK_NOGET;
1049 }
1050
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001051 if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
1052 (!interface->desc.bNumEndpoints ||
1053 usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
Jiri Kosina58037eb2007-05-30 15:07:13 +02001054 dbg_hid("class descriptor not present\n");
Jiri Slabyc500c972008-05-16 11:49:16 +02001055 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
1057
Jiri Slabyc500c972008-05-16 11:49:16 +02001058 hid->version = le16_to_cpu(hdesc->bcdHID);
1059 hid->country = hdesc->bCountryCode;
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 for (n = 0; n < hdesc->bNumDescriptors; n++)
1062 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
1063 rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
1064
1065 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
Jiri Kosina58037eb2007-05-30 15:07:13 +02001066 dbg_hid("weird size of report descriptor (%u)\n", rsize);
Jiri Slabyc500c972008-05-16 11:49:16 +02001067 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069
1070 if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
Jiri Kosina58037eb2007-05-30 15:07:13 +02001071 dbg_hid("couldn't allocate rdesc memory\n");
Jiri Slabyc500c972008-05-16 11:49:16 +02001072 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 }
1074
Vojtech Pavlik854561b2005-05-29 02:28:00 -05001075 hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
1076
Jiri Slabyc500c972008-05-16 11:49:16 +02001077 ret = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber,
1078 HID_DT_REPORT, rdesc, rsize);
1079 if (ret < 0) {
Jiri Kosina58037eb2007-05-30 15:07:13 +02001080 dbg_hid("reading report descriptor failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 kfree(rdesc);
Jiri Slabyc500c972008-05-16 11:49:16 +02001082 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
1084
Jiri Slabyc500c972008-05-16 11:49:16 +02001085 ret = hid_parse_report(hid, rdesc, rsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 kfree(rdesc);
Jiri Slabyc500c972008-05-16 11:49:16 +02001087 if (ret) {
1088 dbg_hid("parsing report descriptor failed\n");
1089 goto err;
1090 }
1091
Zoltan Karcagif5208992009-05-06 16:30:21 +02001092 hid->quirks |= quirks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Jiri Slabyc500c972008-05-16 11:49:16 +02001094 return 0;
1095err:
1096 return ret;
1097}
1098
1099static int usbhid_start(struct hid_device *hid)
1100{
1101 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
1102 struct usb_host_interface *interface = intf->cur_altsetting;
1103 struct usb_device *dev = interface_to_usbdev(intf);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001104 struct usbhid_device *usbhid = hid->driver_data;
Jiri Slabyc500c972008-05-16 11:49:16 +02001105 unsigned int n, insize = 0;
1106 int ret;
1107
Jiri Slabye3e14de2008-11-01 23:41:46 +01001108 clear_bit(HID_DISCONNECTED, &usbhid->iofl);
1109
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001110 usbhid->bufsize = HID_MIN_BUFFER_SIZE;
1111 hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
1112 hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
1113 hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
1114
1115 if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
1116 usbhid->bufsize = HID_MAX_BUFFER_SIZE;
Michael Haboustakbf0964d2005-09-05 00:12:01 -05001117
1118 hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
1119
1120 if (insize > HID_MAX_BUFFER_SIZE)
1121 insize = HID_MAX_BUFFER_SIZE;
1122
Jiri Slabyc500c972008-05-16 11:49:16 +02001123 if (hid_alloc_buffers(dev, hid)) {
1124 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 goto fail;
Pekka Sarnilaf345c372008-03-06 13:23:14 +01001126 }
1127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 for (n = 0; n < interface->desc.bNumEndpoints; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 struct usb_endpoint_descriptor *endpoint;
1130 int pipe;
1131 int interval;
1132
1133 endpoint = &interface->endpoint[n].desc;
Jiri Slaby581a2732008-11-24 16:20:08 +01001134 if (!usb_endpoint_xfer_int(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 continue;
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 interval = endpoint->bInterval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Pekka Sarnilaf345c372008-03-06 13:23:14 +01001139 /* Some vendors give fullspeed interval on highspeed devides */
Jiri Slabyc500c972008-05-16 11:49:16 +02001140 if (hid->quirks & HID_QUIRK_FULLSPEED_INTERVAL &&
Pekka Sarnilaf345c372008-03-06 13:23:14 +01001141 dev->speed == USB_SPEED_HIGH) {
1142 interval = fls(endpoint->bInterval*8);
1143 printk(KERN_INFO "%s: Fixing fullspeed to highspeed interval: %d -> %d\n",
1144 hid->name, endpoint->bInterval, interval);
1145 }
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 /* Change the polling interval of mice. */
1148 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
1149 interval = hid_mousepoll_interval;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -05001150
Jiri Slabyc500c972008-05-16 11:49:16 +02001151 ret = -ENOMEM;
Luiz Fernando N. Capitulino0f12aa02006-10-26 13:02:51 -03001152 if (usb_endpoint_dir_in(endpoint)) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001153 if (usbhid->urbin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 continue;
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001155 if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 goto fail;
1157 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001158 usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 hid_irq_in, hid, interval);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001160 usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
1161 usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 } else {
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001163 if (usbhid->urbout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 continue;
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001165 if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 goto fail;
1167 pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001168 usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 hid_irq_out, hid, interval);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001170 usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
1171 usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
1173 }
1174
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001175 usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
Jiri Slabyc500c972008-05-16 11:49:16 +02001176 if (!usbhid->urbctrl) {
1177 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 goto fail;
Jiri Slabyc500c972008-05-16 11:49:16 +02001179 }
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001180
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001181 usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
1182 usbhid->ctrlbuf, 1, hid_ctrl, hid);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001183 usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
Alan Stern0ede76f2010-03-05 15:10:17 -05001184 usbhid->urbctrl->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Jiri Slabyc500c972008-05-16 11:49:16 +02001185
Jiri Kosina5b915d92009-11-05 14:08:03 +01001186 if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
1187 usbhid_init_reports(hid);
Jiri Slaby93c10132008-06-27 00:04:24 +02001188
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001189 set_bit(HID_STARTED, &usbhid->iofl);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001190
Alan Stern08ef08e2008-10-30 23:58:51 +01001191 /* Some keyboards don't work until their LEDs have been set.
1192 * Since BIOSes do set the LEDs, it must be safe for any device
1193 * that supports the keyboard boot protocol.
Alan Stern3d615102010-04-02 13:21:58 -04001194 * In addition, enable remote wakeup by default for all keyboard
1195 * devices supporting the boot protocol.
Alan Stern08ef08e2008-10-30 23:58:51 +01001196 */
1197 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT &&
1198 interface->desc.bInterfaceProtocol ==
Alan Stern3d615102010-04-02 13:21:58 -04001199 USB_INTERFACE_PROTOCOL_KEYBOARD) {
Alan Stern08ef08e2008-10-30 23:58:51 +01001200 usbhid_set_leds(hid);
Alan Stern3d615102010-04-02 13:21:58 -04001201 device_set_wakeup_enable(&dev->dev, 1);
1202 }
Jiri Slabyc500c972008-05-16 11:49:16 +02001203 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205fail:
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001206 usb_free_urb(usbhid->urbin);
1207 usb_free_urb(usbhid->urbout);
1208 usb_free_urb(usbhid->urbctrl);
Jiri Slabye3e14de2008-11-01 23:41:46 +01001209 usbhid->urbin = NULL;
1210 usbhid->urbout = NULL;
1211 usbhid->urbctrl = NULL;
Jiri Kosina22f675f2007-08-01 12:32:27 +02001212 hid_free_buffers(dev, hid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001213 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
Jiri Slabyc500c972008-05-16 11:49:16 +02001216static void usbhid_stop(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
Jiri Slabyc500c972008-05-16 11:49:16 +02001218 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Jiri Slabyc500c972008-05-16 11:49:16 +02001220 if (WARN_ON(!usbhid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 return;
1222
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001223 clear_bit(HID_STARTED, &usbhid->iofl);
Daniel Kurtz4371ea82011-11-17 19:23:50 +08001224 spin_lock_irq(&usbhid->lock); /* Sync with error and led handlers */
Oliver Neukum69626f22008-03-31 16:27:30 +02001225 set_bit(HID_DISCONNECTED, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +01001226 spin_unlock_irq(&usbhid->lock);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001227 usb_kill_urb(usbhid->urbin);
1228 usb_kill_urb(usbhid->urbout);
1229 usb_kill_urb(usbhid->urbctrl);
1230
Oliver Neukum0361a282008-12-17 15:38:03 +01001231 hid_cancel_delayed_stuff(usbhid);
Alan Sternaef4e262006-01-31 12:58:38 -05001232
Jiri Slabyc500c972008-05-16 11:49:16 +02001233 hid->claimed = 0;
1234
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001235 usb_free_urb(usbhid->urbin);
1236 usb_free_urb(usbhid->urbctrl);
1237 usb_free_urb(usbhid->urbout);
Jiri Slabye3e14de2008-11-01 23:41:46 +01001238 usbhid->urbin = NULL; /* don't mess up next start */
1239 usbhid->urbctrl = NULL;
1240 usbhid->urbout = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Anssi Hannulabe820972007-01-19 19:28:17 +02001242 hid_free_buffers(hid_to_usb_dev(hid), hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
Oliver Neukum0361a282008-12-17 15:38:03 +01001245static int usbhid_power(struct hid_device *hid, int lvl)
1246{
1247 int r = 0;
1248
1249 switch (lvl) {
1250 case PM_HINT_FULLON:
1251 r = usbhid_get_power(hid);
1252 break;
1253 case PM_HINT_NORMAL:
1254 usbhid_put_power(hid);
1255 break;
1256 }
1257 return r;
1258}
1259
Jiri Slabyc500c972008-05-16 11:49:16 +02001260static struct hid_ll_driver usb_hid_driver = {
1261 .parse = usbhid_parse,
1262 .start = usbhid_start,
1263 .stop = usbhid_stop,
1264 .open = usbhid_open,
1265 .close = usbhid_close,
Oliver Neukum0361a282008-12-17 15:38:03 +01001266 .power = usbhid_power,
Jiri Slabyc500c972008-05-16 11:49:16 +02001267 .hidinput_input_event = usb_hidinput_input_event,
1268};
1269
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001270static int usbhid_probe(struct usb_interface *intf, const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
Jiri Slaby131d3a7a2008-11-14 12:03:47 +01001272 struct usb_host_interface *interface = intf->cur_altsetting;
Jiri Slabyc500c972008-05-16 11:49:16 +02001273 struct usb_device *dev = interface_to_usbdev(intf);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001274 struct usbhid_device *usbhid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 struct hid_device *hid;
Jiri Slaby131d3a7a2008-11-14 12:03:47 +01001276 unsigned int n, has_in = 0;
Jiri Slabyc500c972008-05-16 11:49:16 +02001277 size_t len;
1278 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Jiri Kosina58037eb2007-05-30 15:07:13 +02001280 dbg_hid("HID probe called for ifnum %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 intf->altsetting->desc.bInterfaceNumber);
1282
Jiri Slaby131d3a7a2008-11-14 12:03:47 +01001283 for (n = 0; n < interface->desc.bNumEndpoints; n++)
1284 if (usb_endpoint_is_int_in(&interface->endpoint[n].desc))
1285 has_in++;
1286 if (!has_in) {
Joe Perches4291ee32010-12-09 19:29:03 -08001287 hid_err(intf, "couldn't find an input interrupt endpoint\n");
Jiri Slaby131d3a7a2008-11-14 12:03:47 +01001288 return -ENODEV;
1289 }
1290
Jiri Slabyc500c972008-05-16 11:49:16 +02001291 hid = hid_allocate_device();
1292 if (IS_ERR(hid))
1293 return PTR_ERR(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 usb_set_intfdata(intf, hid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001296 hid->ll_driver = &usb_hid_driver;
Alan Ottb4dbde92011-01-18 03:04:39 -05001297 hid->hid_get_raw_report = usbhid_get_raw_report;
Jiri Slabyc500c972008-05-16 11:49:16 +02001298 hid->hid_output_raw_report = usbhid_output_raw_report;
Jiri Slaby76483cf2008-09-18 12:23:33 +02001299 hid->ff_init = hid_pidff_init;
Jiri Slabyc500c972008-05-16 11:49:16 +02001300#ifdef CONFIG_USB_HIDDEV
Jiri Slaby93c10132008-06-27 00:04:24 +02001301 hid->hiddev_connect = hiddev_connect;
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001302 hid->hiddev_disconnect = hiddev_disconnect;
Jiri Slabyc500c972008-05-16 11:49:16 +02001303 hid->hiddev_hid_event = hiddev_hid_event;
1304 hid->hiddev_report_event = hiddev_report_event;
1305#endif
1306 hid->dev.parent = &intf->dev;
1307 hid->bus = BUS_USB;
1308 hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
1309 hid->product = le16_to_cpu(dev->descriptor.idProduct);
1310 hid->name[0] = 0;
Bastien Nocerab5e5a372010-04-16 17:19:50 +01001311 hid->quirks = usbhid_lookup_quirk(hid->vendor, hid->product);
Jiri Slabya73a6372008-10-22 14:45:11 +02001312 if (intf->cur_altsetting->desc.bInterfaceProtocol ==
1313 USB_INTERFACE_PROTOCOL_MOUSE)
1314 hid->type = HID_TYPE_USBMOUSE;
Tomoki Sekiyama6dc14182011-05-23 15:45:44 -07001315 else if (intf->cur_altsetting->desc.bInterfaceProtocol == 0)
1316 hid->type = HID_TYPE_USBNONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Jiri Slabyc500c972008-05-16 11:49:16 +02001318 if (dev->manufacturer)
1319 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
1320
1321 if (dev->product) {
1322 if (dev->manufacturer)
1323 strlcat(hid->name, " ", sizeof(hid->name));
1324 strlcat(hid->name, dev->product, sizeof(hid->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
1326
Jiri Slabyc500c972008-05-16 11:49:16 +02001327 if (!strlen(hid->name))
1328 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
1329 le16_to_cpu(dev->descriptor.idVendor),
1330 le16_to_cpu(dev->descriptor.idProduct));
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001331
Jiri Slabyc500c972008-05-16 11:49:16 +02001332 usb_make_path(dev, hid->phys, sizeof(hid->phys));
1333 strlcat(hid->phys, "/input", sizeof(hid->phys));
1334 len = strlen(hid->phys);
1335 if (len < sizeof(hid->phys) - 1)
1336 snprintf(hid->phys + len, sizeof(hid->phys) - len,
1337 "%d", intf->altsetting[0].desc.bInterfaceNumber);
Geoff Levand4a1a4d82007-01-15 20:11:52 -08001338
Jiri Slabyc500c972008-05-16 11:49:16 +02001339 if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
1340 hid->uniq[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001342 usbhid = kzalloc(sizeof(*usbhid), GFP_KERNEL);
1343 if (usbhid == NULL) {
1344 ret = -ENOMEM;
1345 goto err;
1346 }
1347
1348 hid->driver_data = usbhid;
1349 usbhid->hid = hid;
Jiri Kosina57ab12e2010-02-17 14:25:01 +01001350 usbhid->intf = intf;
1351 usbhid->ifnum = interface->desc.bInterfaceNumber;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001352
Alan Sternfde4e2f2010-05-07 10:41:10 -04001353 init_waitqueue_head(&usbhid->wait);
1354 INIT_WORK(&usbhid->reset_work, hid_reset);
Alan Sternfde4e2f2010-05-07 10:41:10 -04001355 setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
1356 spin_lock_init(&usbhid->lock);
1357
Daniel Kurtz4371ea82011-11-17 19:23:50 +08001358 INIT_WORK(&usbhid->led_work, hid_led);
1359
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001360 ret = hid_add_device(hid);
1361 if (ret) {
Jiri Slabyd458a9d2008-05-16 11:49:20 +02001362 if (ret != -ENODEV)
Joe Perches4291ee32010-12-09 19:29:03 -08001363 hid_err(intf, "can't add hid device: %d\n", ret);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001364 goto err_free;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001365 }
Jiri Slabyc500c972008-05-16 11:49:16 +02001366
1367 return 0;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001368err_free:
1369 kfree(usbhid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001370err:
1371 hid_destroy_device(hid);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001372 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001375static void usbhid_disconnect(struct usb_interface *intf)
Jiri Slabyc500c972008-05-16 11:49:16 +02001376{
1377 struct hid_device *hid = usb_get_intfdata(intf);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001378 struct usbhid_device *usbhid;
Jiri Slabyc500c972008-05-16 11:49:16 +02001379
1380 if (WARN_ON(!hid))
1381 return;
1382
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001383 usbhid = hid->driver_data;
Jiri Slabyc500c972008-05-16 11:49:16 +02001384 hid_destroy_device(hid);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001385 kfree(usbhid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001386}
1387
Oliver Neukum0361a282008-12-17 15:38:03 +01001388static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
Oliver Neukum0361a282008-12-17 15:38:03 +01001390 del_timer_sync(&usbhid->io_retry);
Oliver Neukum0361a282008-12-17 15:38:03 +01001391 cancel_work_sync(&usbhid->reset_work);
Daniel Kurtz4371ea82011-11-17 19:23:50 +08001392 cancel_work_sync(&usbhid->led_work);
Oliver Neukum0361a282008-12-17 15:38:03 +01001393}
1394
1395static void hid_cease_io(struct usbhid_device *usbhid)
1396{
Oliver Neukumfad9fbe2011-10-13 18:21:58 +02001397 del_timer_sync(&usbhid->io_retry);
Oliver Neukum0361a282008-12-17 15:38:03 +01001398 usb_kill_urb(usbhid->urbin);
1399 usb_kill_urb(usbhid->urbctrl);
1400 usb_kill_urb(usbhid->urbout);
Oliver Neukum0361a282008-12-17 15:38:03 +01001401}
1402
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001403/* Treat USB reset pretty much the same as suspend/resume */
1404static int hid_pre_reset(struct usb_interface *intf)
1405{
1406 struct hid_device *hid = usb_get_intfdata(intf);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001407 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001409 spin_lock_irq(&usbhid->lock);
1410 set_bit(HID_RESET_PENDING, &usbhid->iofl);
1411 spin_unlock_irq(&usbhid->lock);
1412 hid_cease_io(usbhid);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001413
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001414 return 0;
1415}
1416
1417/* Same routine used for post_reset and reset_resume */
1418static int hid_post_reset(struct usb_interface *intf)
1419{
1420 struct usb_device *dev = interface_to_usbdev (intf);
1421 struct hid_device *hid = usb_get_intfdata(intf);
1422 struct usbhid_device *usbhid = hid->driver_data;
Simon Haggettdc3c78e2012-04-03 16:04:15 +01001423 struct usb_host_interface *interface = intf->cur_altsetting;
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001424 int status;
Simon Haggettdc3c78e2012-04-03 16:04:15 +01001425 char *rdesc;
1426
1427 /* Fetch and examine the HID report descriptor. If this
1428 * has changed, then rebind. Since usbcore's check of the
1429 * configuration descriptors passed, we already know that
1430 * the size of the HID report descriptor has not changed.
1431 */
1432 rdesc = kmalloc(hid->rsize, GFP_KERNEL);
1433 if (!rdesc) {
1434 dbg_hid("couldn't allocate rdesc memory (post_reset)\n");
1435 return 1;
1436 }
1437 status = hid_get_class_descriptor(dev,
1438 interface->desc.bInterfaceNumber,
1439 HID_DT_REPORT, rdesc, hid->rsize);
1440 if (status < 0) {
1441 dbg_hid("reading report descriptor failed (post_reset)\n");
1442 kfree(rdesc);
1443 return 1;
1444 }
1445 status = memcmp(rdesc, hid->rdesc, hid->rsize);
1446 kfree(rdesc);
1447 if (status != 0) {
1448 dbg_hid("report descriptor changed\n");
1449 return 1;
1450 }
Jiri Kosina70fa9f22009-06-04 15:48:38 +02001451
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001452 spin_lock_irq(&usbhid->lock);
1453 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
1454 spin_unlock_irq(&usbhid->lock);
1455 hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001456 status = hid_start_in(hid);
1457 if (status < 0)
1458 hid_io_error(hid);
1459 usbhid_restart_queues(usbhid);
1460
1461 return 0;
1462}
1463
1464int usbhid_get_power(struct hid_device *hid)
1465{
1466 struct usbhid_device *usbhid = hid->driver_data;
Jiri Kosina70fa9f22009-06-04 15:48:38 +02001467
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001468 return usb_autopm_get_interface(usbhid->intf);
1469}
1470
1471void usbhid_put_power(struct hid_device *hid)
1472{
1473 struct usbhid_device *usbhid = hid->driver_data;
Jiri Kosina70fa9f22009-06-04 15:48:38 +02001474
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001475 usb_autopm_put_interface(usbhid->intf);
1476}
1477
1478
Jiri Kosina0f6f1402009-01-19 09:17:18 +01001479#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480static int hid_suspend(struct usb_interface *intf, pm_message_t message)
1481{
Oliver Neukum0361a282008-12-17 15:38:03 +01001482 struct hid_device *hid = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 struct usbhid_device *usbhid = hid->driver_data;
Oliver Neukum0361a282008-12-17 15:38:03 +01001484 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Alan Stern5b1b0b82011-08-19 23:49:48 +02001486 if (PMSG_IS_AUTO(message)) {
Oliver Neukum0361a282008-12-17 15:38:03 +01001487 spin_lock_irq(&usbhid->lock); /* Sync with error handler */
1488 if (!test_bit(HID_RESET_PENDING, &usbhid->iofl)
1489 && !test_bit(HID_CLEAR_HALT, &usbhid->iofl)
1490 && !test_bit(HID_OUT_RUNNING, &usbhid->iofl)
1491 && !test_bit(HID_CTRL_RUNNING, &usbhid->iofl)
1492 && !test_bit(HID_KEYS_PRESSED, &usbhid->iofl)
1493 && (!usbhid->ledcount || ignoreled))
1494 {
1495 set_bit(HID_REPORTED_IDLE, &usbhid->iofl);
1496 spin_unlock_irq(&usbhid->lock);
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001497 if (hid->driver && hid->driver->suspend) {
1498 status = hid->driver->suspend(hid, message);
1499 if (status < 0)
1500 return status;
1501 }
Oliver Neukum0361a282008-12-17 15:38:03 +01001502 } else {
1503 usbhid_mark_busy(usbhid);
1504 spin_unlock_irq(&usbhid->lock);
1505 return -EBUSY;
1506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Oliver Neukum0361a282008-12-17 15:38:03 +01001508 } else {
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001509 if (hid->driver && hid->driver->suspend) {
1510 status = hid->driver->suspend(hid, message);
1511 if (status < 0)
1512 return status;
1513 }
Oliver Neukum0361a282008-12-17 15:38:03 +01001514 spin_lock_irq(&usbhid->lock);
1515 set_bit(HID_REPORTED_IDLE, &usbhid->iofl);
1516 spin_unlock_irq(&usbhid->lock);
1517 if (usbhid_wait_io(hid) < 0)
1518 return -EIO;
1519 }
1520
Oliver Neukum0361a282008-12-17 15:38:03 +01001521 hid_cancel_delayed_stuff(usbhid);
1522 hid_cease_io(usbhid);
1523
Alan Stern5b1b0b82011-08-19 23:49:48 +02001524 if (PMSG_IS_AUTO(message) && test_bit(HID_KEYS_PRESSED, &usbhid->iofl)) {
Oliver Neukum0361a282008-12-17 15:38:03 +01001525 /* lost race against keypresses */
1526 status = hid_start_in(hid);
1527 if (status < 0)
1528 hid_io_error(hid);
1529 usbhid_mark_busy(usbhid);
1530 return -EBUSY;
1531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 dev_dbg(&intf->dev, "suspend\n");
1533 return 0;
1534}
1535
1536static int hid_resume(struct usb_interface *intf)
1537{
1538 struct hid_device *hid = usb_get_intfdata (intf);
1539 struct usbhid_device *usbhid = hid->driver_data;
1540 int status;
1541
Jiri Slabyfde5be32008-11-23 12:03:20 +01001542 if (!test_bit(HID_STARTED, &usbhid->iofl))
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001543 return 0;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001544
Oliver Neukum0361a282008-12-17 15:38:03 +01001545 clear_bit(HID_REPORTED_IDLE, &usbhid->iofl);
1546 usbhid_mark_busy(usbhid);
1547
1548 if (test_bit(HID_CLEAR_HALT, &usbhid->iofl) ||
1549 test_bit(HID_RESET_PENDING, &usbhid->iofl))
1550 schedule_work(&usbhid->reset_work);
Alan Sterndf9a1f42006-06-01 13:55:28 -04001551 usbhid->retry_delay = 0;
1552 status = hid_start_in(hid);
Oliver Neukum0361a282008-12-17 15:38:03 +01001553 if (status < 0)
1554 hid_io_error(hid);
1555 usbhid_restart_queues(usbhid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001557 if (status >= 0 && hid->driver && hid->driver->resume) {
1558 int ret = hid->driver->resume(hid);
1559 if (ret < 0)
1560 status = ret;
1561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 dev_dbg(&intf->dev, "resume status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 return 0;
1564}
1565
Oliver Neukum378a0ed2009-02-18 11:46:45 +01001566static int hid_reset_resume(struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
Oliver Neukum378a0ed2009-02-18 11:46:45 +01001568 struct hid_device *hid = usb_get_intfdata(intf);
1569 struct usbhid_device *usbhid = hid->driver_data;
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001570 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Oliver Neukum378a0ed2009-02-18 11:46:45 +01001572 clear_bit(HID_REPORTED_IDLE, &usbhid->iofl);
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001573 status = hid_post_reset(intf);
1574 if (status >= 0 && hid->driver && hid->driver->reset_resume) {
1575 int ret = hid->driver->reset_resume(hid);
1576 if (ret < 0)
1577 status = ret;
1578 }
1579 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580}
1581
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001582#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Márton Némethd67dec52010-01-10 17:59:22 +01001584static const struct usb_device_id hid_usb_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1586 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
1587 { } /* Terminating entry */
1588};
1589
1590MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1591
1592static struct usb_driver hid_driver = {
1593 .name = "usbhid",
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001594 .probe = usbhid_probe,
1595 .disconnect = usbhid_disconnect,
Jiri Kosina0f6f1402009-01-19 09:17:18 +01001596#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 .suspend = hid_suspend,
1598 .resume = hid_resume,
Oliver Neukum378a0ed2009-02-18 11:46:45 +01001599 .reset_resume = hid_reset_resume,
Jiri Kosina0f6f1402009-01-19 09:17:18 +01001600#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 .pre_reset = hid_pre_reset,
1602 .post_reset = hid_post_reset,
1603 .id_table = hid_usb_ids,
Oliver Neukum933e3182007-07-11 14:48:58 +02001604 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605};
1606
Guillaume Chazarain8fe294c2010-09-12 21:32:35 +02001607struct usb_interface *usbhid_find_interface(int minor)
1608{
1609 return usb_find_interface(&hid_driver, minor);
1610}
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612static int __init hid_init(void)
1613{
Oliver Neukum0361a282008-12-17 15:38:03 +01001614 int retval = -ENOMEM;
1615
Paul Walmsley876b9272007-04-19 14:56:12 +02001616 retval = usbhid_quirks_init(quirks_param);
1617 if (retval)
1618 goto usbhid_quirks_init_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 retval = usb_register(&hid_driver);
1620 if (retval)
1621 goto usb_register_fail;
Jiri Kosinaccabcd22009-10-02 18:31:36 +02001622 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 return 0;
1625usb_register_fail:
Paul Walmsley876b9272007-04-19 14:56:12 +02001626 usbhid_quirks_exit();
1627usbhid_quirks_init_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 return retval;
1629}
1630
1631static void __exit hid_exit(void)
1632{
1633 usb_deregister(&hid_driver);
Paul Walmsley876b9272007-04-19 14:56:12 +02001634 usbhid_quirks_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635}
1636
1637module_init(hid_init);
1638module_exit(hid_exit);
1639
Jiri Kosina88adb722009-10-02 18:29:34 +02001640MODULE_AUTHOR("Andreas Gal");
1641MODULE_AUTHOR("Vojtech Pavlik");
1642MODULE_AUTHOR("Jiri Kosina");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643MODULE_DESCRIPTION(DRIVER_DESC);
1644MODULE_LICENSE(DRIVER_LICENSE);