]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/hid/usbhid/hid-core.c
HID: usbhid: update LED fields unlocked
[mirror_ubuntu-jammy-kernel.git] / drivers / hid / usbhid / hid-core.c
CommitLineData
1da177e4
LT
1/*
2 * USB HID support for Linux
3 *
4 * Copyright (c) 1999 Andreas Gal
bf0964dc
MH
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
0361a28d 7 * Copyright (c) 2007-2008 Oliver Neukum
7d39e849 8 * Copyright (c) 2006-2010 Jiri Kosina
1da177e4
LT
9 */
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>
1da177e4
LT
22#include <linux/list.h>
23#include <linux/mm.h>
3d5afd32 24#include <linux/mutex.h>
1da177e4
LT
25#include <linux/spinlock.h>
26#include <asm/unaligned.h>
27#include <asm/byteorder.h>
28#include <linux/input.h>
29#include <linux/wait.h>
0361a28d 30#include <linux/workqueue.h>
dc3c78e4 31#include <linux/string.h>
1da177e4 32
1da177e4
LT
33#include <linux/usb.h>
34
dde5845a 35#include <linux/hid.h>
1da177e4 36#include <linux/hiddev.h>
c080d89a 37#include <linux/hid-debug.h>
86166b7b 38#include <linux/hidraw.h>
4916b3a5 39#include "usbhid.h"
1da177e4
LT
40
41/*
42 * Version Information
43 */
44
1da177e4
LT
45#define DRIVER_DESC "USB HID core driver"
46#define DRIVER_LICENSE "GPL"
47
1da177e4
LT
48/*
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
0361a28d
ON
56static unsigned int ignoreled;
57module_param_named(ignoreled, ignoreled, uint, 0644);
58MODULE_PARM_DESC(ignoreled, "Autosuspend with active leds");
59
876b9276
PW
60/* 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");
1da177e4 67/*
48b4554a 68 * Input submission and I/O error handler.
1da177e4 69 */
0361a28d 70static DEFINE_MUTEX(hid_open_mut);
1da177e4 71
48b4554a 72static void hid_io_error(struct hid_device *hid);
0361a28d
ON
73static 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);
48b4554a
JK
76
77/* Start up the input URB */
78static int hid_start_in(struct hid_device *hid)
1da177e4 79{
1da177e4 80 unsigned long flags;
48b4554a
JK
81 int rc = 0;
82 struct usbhid_device *usbhid = hid->driver_data;
1da177e4 83
0361a28d
ON
84 spin_lock_irqsave(&usbhid->lock, flags);
85 if (hid->open > 0 &&
69626f23 86 !test_bit(HID_DISCONNECTED, &usbhid->iofl) &&
f2b5264d 87 !test_bit(HID_SUSPENDED, &usbhid->iofl) &&
48b4554a
JK
88 !test_and_set_bit(HID_IN_RUNNING, &usbhid->iofl)) {
89 rc = usb_submit_urb(usbhid->urbin, GFP_ATOMIC);
a8c52b66 90 if (rc != 0) {
48b4554a 91 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
a8c52b66
ON
92 if (rc == -ENOSPC)
93 set_bit(HID_NO_BANDWIDTH, &usbhid->iofl);
94 } else {
95 clear_bit(HID_NO_BANDWIDTH, &usbhid->iofl);
96 }
1da177e4 97 }
0361a28d 98 spin_unlock_irqrestore(&usbhid->lock, flags);
48b4554a 99 return rc;
1da177e4
LT
100}
101
48b4554a
JK
102/* I/O retry timer routine */
103static void hid_retry_timeout(unsigned long _hid)
1da177e4 104{
48b4554a 105 struct hid_device *hid = (struct hid_device *) _hid;
4916b3a5 106 struct usbhid_device *usbhid = hid->driver_data;
1da177e4 107
48b4554a
JK
108 dev_dbg(&usbhid->intf->dev, "retrying intr urb\n");
109 if (hid_start_in(hid))
110 hid_io_error(hid);
1da177e4
LT
111}
112
48b4554a
JK
113/* Workqueue routine to reset the device or clear a halt */
114static void hid_reset(struct work_struct *work)
1da177e4 115{
48b4554a
JK
116 struct usbhid_device *usbhid =
117 container_of(work, struct usbhid_device, reset_work);
118 struct hid_device *hid = usbhid->hid;
011b15df 119 int rc = 0;
1da177e4 120
48b4554a
JK
121 if (test_bit(HID_CLEAR_HALT, &usbhid->iofl)) {
122 dev_dbg(&usbhid->intf->dev, "clear halt\n");
123 rc = usb_clear_halt(hid_to_usb_dev(hid), usbhid->urbin->pipe);
124 clear_bit(HID_CLEAR_HALT, &usbhid->iofl);
125 hid_start_in(hid);
126 }
1da177e4 127
48b4554a
JK
128 else if (test_bit(HID_RESET_PENDING, &usbhid->iofl)) {
129 dev_dbg(&usbhid->intf->dev, "resetting device\n");
011b15df
AS
130 rc = usb_lock_device_for_reset(hid_to_usb_dev(hid), usbhid->intf);
131 if (rc == 0) {
742120c6 132 rc = usb_reset_device(hid_to_usb_dev(hid));
011b15df 133 usb_unlock_device(hid_to_usb_dev(hid));
1da177e4 134 }
48b4554a 135 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
1da177e4
LT
136 }
137
48b4554a
JK
138 switch (rc) {
139 case 0:
140 if (!test_bit(HID_IN_RUNNING, &usbhid->iofl))
141 hid_io_error(hid);
142 break;
143 default:
4291ee30
JP
144 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);
48b4554a
JK
148 /* FALLTHROUGH */
149 case -EHOSTUNREACH:
150 case -ENODEV:
151 case -EINTR:
152 break;
1da177e4 153 }
1da177e4
LT
154}
155
48b4554a
JK
156/* Main I/O error handler */
157static void hid_io_error(struct hid_device *hid)
dde5845a 158{
48b4554a
JK
159 unsigned long flags;
160 struct usbhid_device *usbhid = hid->driver_data;
dde5845a 161
0361a28d 162 spin_lock_irqsave(&usbhid->lock, flags);
dde5845a 163
48b4554a 164 /* Stop when disconnected */
69626f23 165 if (test_bit(HID_DISCONNECTED, &usbhid->iofl))
48b4554a 166 goto done;
dde5845a 167
5e2a55f2
AS
168 /* 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
48b4554a
JK
173 /* When an error occurs, retry at increasing intervals */
174 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;
dde5845a 179
48b4554a 180 if (time_after(jiffies, usbhid->stop_retry)) {
4916b3a5 181
a8c52b66
ON
182 /* 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
48b4554a
JK
186 schedule_work(&usbhid->reset_work);
187 goto done;
188 }
1da177e4
LT
189 }
190
48b4554a
JK
191 mod_timer(&usbhid->io_retry,
192 jiffies + msecs_to_jiffies(usbhid->retry_delay));
193done:
0361a28d
ON
194 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;
f0befcd6 208 int r;
0361a28d 209
d4150c8f
AS
210 if (!hid || test_bit(HID_RESET_PENDING, &usbhid->iofl) ||
211 test_bit(HID_SUSPENDED, &usbhid->iofl))
0361a28d
ON
212 return 0;
213
214 if ((kicked = (usbhid->outhead != usbhid->outtail))) {
6cc203d7 215 hid_dbg(hid, "Kicking head %d tail %d", usbhid->outhead, usbhid->outtail);
f0befcd6 216
01a7c984 217 /* Try to wake up from autosuspend... */
f0befcd6
DK
218 r = usb_autopm_get_interface_async(usbhid->intf);
219 if (r < 0)
220 return r;
01a7c984
AS
221
222 /*
223 * If still suspended, don't submit. Submission will
224 * occur if/when resume drains the queue.
225 */
f2b5264d 226 if (test_bit(HID_SUSPENDED, &usbhid->iofl)) {
01a7c984
AS
227 usb_autopm_put_interface_no_suspend(usbhid->intf);
228 return r;
229 }
230
f0befcd6
DK
231 /* Asynchronously flush queue. */
232 set_bit(HID_OUT_RUNNING, &usbhid->iofl);
0361a28d
ON
233 if (hid_submit_out(hid)) {
234 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
f0befcd6 235 usb_autopm_put_interface_async(usbhid->intf);
0361a28d 236 }
f0befcd6 237 wake_up(&usbhid->wait);
0361a28d
ON
238 }
239 return kicked;
240}
241
242static int usbhid_restart_ctrl_queue(struct usbhid_device *usbhid)
243{
244 struct hid_device *hid = usb_get_intfdata(usbhid->intf);
245 int kicked;
f0befcd6 246 int r;
0361a28d
ON
247
248 WARN_ON(hid == NULL);
d4150c8f
AS
249 if (!hid || test_bit(HID_RESET_PENDING, &usbhid->iofl) ||
250 test_bit(HID_SUSPENDED, &usbhid->iofl))
0361a28d
ON
251 return 0;
252
253 if ((kicked = (usbhid->ctrlhead != usbhid->ctrltail))) {
6cc203d7 254 hid_dbg(hid, "Kicking head %d tail %d", usbhid->ctrlhead, usbhid->ctrltail);
f0befcd6 255
01a7c984 256 /* Try to wake up from autosuspend... */
f0befcd6
DK
257 r = usb_autopm_get_interface_async(usbhid->intf);
258 if (r < 0)
259 return r;
01a7c984
AS
260
261 /*
262 * If still suspended, don't submit. Submission will
263 * occur if/when resume drains the queue.
264 */
f2b5264d 265 if (test_bit(HID_SUSPENDED, &usbhid->iofl)) {
01a7c984
AS
266 usb_autopm_put_interface_no_suspend(usbhid->intf);
267 return r;
268 }
269
f0befcd6
DK
270 /* Asynchronously flush queue. */
271 set_bit(HID_CTRL_RUNNING, &usbhid->iofl);
0361a28d
ON
272 if (hid_submit_ctrl(hid)) {
273 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
f0befcd6 274 usb_autopm_put_interface_async(usbhid->intf);
0361a28d 275 }
f0befcd6 276 wake_up(&usbhid->wait);
0361a28d
ON
277 }
278 return kicked;
1da177e4
LT
279}
280
48b4554a
JK
281/*
282 * Input interrupt completion handler.
283 */
854561b0 284
48b4554a 285static void hid_irq_in(struct urb *urb)
1da177e4 286{
48b4554a
JK
287 struct hid_device *hid = urb->context;
288 struct usbhid_device *usbhid = hid->driver_data;
289 int status;
1da177e4 290
48b4554a 291 switch (urb->status) {
880d29f1 292 case 0: /* success */
0361a28d 293 usbhid_mark_busy(usbhid);
880d29f1
JS
294 usbhid->retry_delay = 0;
295 hid_input_report(urb->context, HID_INPUT_REPORT,
296 urb->transfer_buffer,
297 urb->actual_length, 1);
0361a28d
ON
298 /*
299 * autosuspend refused while keys are pressed
300 * because most keyboards don't wake up when
301 * a key is released
302 */
303 if (hid_check_keys_pressed(hid))
304 set_bit(HID_KEYS_PRESSED, &usbhid->iofl);
305 else
306 clear_bit(HID_KEYS_PRESSED, &usbhid->iofl);
880d29f1
JS
307 break;
308 case -EPIPE: /* stall */
0361a28d 309 usbhid_mark_busy(usbhid);
880d29f1
JS
310 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
311 set_bit(HID_CLEAR_HALT, &usbhid->iofl);
312 schedule_work(&usbhid->reset_work);
313 return;
314 case -ECONNRESET: /* unlink */
315 case -ENOENT:
316 case -ESHUTDOWN: /* unplug */
317 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
318 return;
319 case -EILSEQ: /* protocol error or unplug */
320 case -EPROTO: /* protocol error or unplug */
321 case -ETIME: /* protocol error or unplug */
322 case -ETIMEDOUT: /* Should never happen, but... */
0361a28d 323 usbhid_mark_busy(usbhid);
880d29f1
JS
324 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
325 hid_io_error(hid);
326 return;
327 default: /* error */
4291ee30
JP
328 hid_warn(urb->dev, "input irq status %d received\n",
329 urb->status);
48b4554a 330 }
1da177e4 331
48b4554a
JK
332 status = usb_submit_urb(urb, GFP_ATOMIC);
333 if (status) {
334 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
335 if (status != -EPERM) {
4291ee30
JP
336 hid_err(hid, "can't resubmit intr, %s-%s/input%d, status %d\n",
337 hid_to_usb_dev(hid)->bus->bus_name,
338 hid_to_usb_dev(hid)->devpath,
339 usbhid->ifnum, status);
48b4554a
JK
340 hid_io_error(hid);
341 }
342 }
1da177e4
LT
343}
344
48b4554a 345static int hid_submit_out(struct hid_device *hid)
1da177e4 346{
48b4554a 347 struct hid_report *report;
f129ea6d 348 char *raw_report;
4916b3a5 349 struct usbhid_device *usbhid = hid->driver_data;
68229689 350 int r;
4916b3a5 351
f129ea6d
AH
352 report = usbhid->out[usbhid->outtail].report;
353 raw_report = usbhid->out[usbhid->outtail].raw_report;
1da177e4 354
f0befcd6
DK
355 usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) +
356 1 + (report->id > 0);
357 usbhid->urbout->dev = hid_to_usb_dev(hid);
668160e5
AS
358 if (raw_report) {
359 memcpy(usbhid->outbuf, raw_report,
360 usbhid->urbout->transfer_buffer_length);
361 kfree(raw_report);
362 usbhid->out[usbhid->outtail].raw_report = NULL;
363 }
1d3e2023 364
f0befcd6 365 dbg_hid("submitting out urb\n");
d57cdcff 366
f0befcd6
DK
367 r = usb_submit_urb(usbhid->urbout, GFP_ATOMIC);
368 if (r < 0) {
369 hid_err(hid, "usb_submit_urb(out) failed: %d\n", r);
370 return r;
48b4554a 371 }
f0befcd6 372 usbhid->last_out = jiffies;
48b4554a
JK
373 return 0;
374}
375
376static int hid_submit_ctrl(struct hid_device *hid)
1da177e4
LT
377{
378 struct hid_report *report;
48b4554a 379 unsigned char dir;
f129ea6d 380 char *raw_report;
68229689 381 int len, r;
4916b3a5 382 struct usbhid_device *usbhid = hid->driver_data;
1da177e4 383
48b4554a 384 report = usbhid->ctrl[usbhid->ctrltail].report;
f129ea6d 385 raw_report = usbhid->ctrl[usbhid->ctrltail].raw_report;
48b4554a 386 dir = usbhid->ctrl[usbhid->ctrltail].dir;
1da177e4 387
f0befcd6
DK
388 len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
389 if (dir == USB_DIR_OUT) {
390 usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
391 usbhid->urbctrl->transfer_buffer_length = len;
668160e5
AS
392 if (raw_report) {
393 memcpy(usbhid->ctrlbuf, raw_report, len);
394 kfree(raw_report);
395 usbhid->ctrl[usbhid->ctrltail].raw_report = NULL;
396 }
f0befcd6
DK
397 } else {
398 int maxpacket, padlen;
399
400 usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0);
401 maxpacket = usb_maxpacket(hid_to_usb_dev(hid),
402 usbhid->urbctrl->pipe, 0);
403 if (maxpacket > 0) {
404 padlen = DIV_ROUND_UP(len, maxpacket);
405 padlen *= maxpacket;
406 if (padlen > usbhid->bufsize)
407 padlen = usbhid->bufsize;
408 } else
409 padlen = 0;
410 usbhid->urbctrl->transfer_buffer_length = padlen;
48b4554a 411 }
f0befcd6
DK
412 usbhid->urbctrl->dev = hid_to_usb_dev(hid);
413
414 usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
415 usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT :
416 HID_REQ_GET_REPORT;
417 usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) |
418 report->id);
419 usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum);
420 usbhid->cr->wLength = cpu_to_le16(len);
421
422 dbg_hid("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u\n",
423 usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" :
424 "Get_Report",
425 usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength);
426
427 r = usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC);
428 if (r < 0) {
429 hid_err(hid, "usb_submit_urb(ctrl) failed: %d\n", r);
430 return r;
431 }
432 usbhid->last_ctrl = jiffies;
48b4554a
JK
433 return 0;
434}
1da177e4 435
48b4554a
JK
436/*
437 * Output interrupt completion handler.
438 */
1da177e4 439
48b4554a
JK
440static void hid_irq_out(struct urb *urb)
441{
442 struct hid_device *hid = urb->context;
443 struct usbhid_device *usbhid = hid->driver_data;
444 unsigned long flags;
445 int unplug = 0;
1da177e4 446
48b4554a 447 switch (urb->status) {
880d29f1
JS
448 case 0: /* success */
449 break;
450 case -ESHUTDOWN: /* unplug */
451 unplug = 1;
452 case -EILSEQ: /* protocol error or unplug */
453 case -EPROTO: /* protocol error or unplug */
454 case -ECONNRESET: /* unlink */
455 case -ENOENT:
456 break;
457 default: /* error */
4291ee30
JP
458 hid_warn(urb->dev, "output irq status %d received\n",
459 urb->status);
48b4554a 460 }
1da177e4 461
0361a28d 462 spin_lock_irqsave(&usbhid->lock, flags);
1da177e4 463
93101af3 464 if (unplug) {
48b4554a 465 usbhid->outtail = usbhid->outhead;
93101af3 466 } else {
48b4554a 467 usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
1da177e4 468
93101af3
AS
469 if (usbhid->outhead != usbhid->outtail &&
470 hid_submit_out(hid) == 0) {
471 /* Successfully submitted next urb in queue */
472 spin_unlock_irqrestore(&usbhid->lock, flags);
473 return;
474 }
48b4554a 475 }
1da177e4 476
48b4554a 477 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
0361a28d 478 spin_unlock_irqrestore(&usbhid->lock, flags);
68229689 479 usb_autopm_put_interface_async(usbhid->intf);
1d1bdd20 480 wake_up(&usbhid->wait);
48b4554a 481}
6345fdfd 482
48b4554a
JK
483/*
484 * Control pipe completion handler.
485 */
1da177e4 486
48b4554a
JK
487static void hid_ctrl(struct urb *urb)
488{
489 struct hid_device *hid = urb->context;
490 struct usbhid_device *usbhid = hid->driver_data;
0361a28d 491 int unplug = 0, status = urb->status;
1da177e4 492
0361a28d 493 spin_lock(&usbhid->lock);
1da177e4 494
0361a28d 495 switch (status) {
880d29f1
JS
496 case 0: /* success */
497 if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
498 hid_input_report(urb->context,
499 usbhid->ctrl[usbhid->ctrltail].report->type,
500 urb->transfer_buffer, urb->actual_length, 0);
501 break;
502 case -ESHUTDOWN: /* unplug */
503 unplug = 1;
504 case -EILSEQ: /* protocol error or unplug */
505 case -EPROTO: /* protocol error or unplug */
506 case -ECONNRESET: /* unlink */
507 case -ENOENT:
508 case -EPIPE: /* report not available */
509 break;
510 default: /* error */
4291ee30 511 hid_warn(urb->dev, "ctrl urb status %d received\n", status);
48b4554a 512 }
1da177e4 513
93101af3 514 if (unplug) {
48b4554a 515 usbhid->ctrltail = usbhid->ctrlhead;
93101af3 516 } else {
48b4554a 517 usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
1da177e4 518
93101af3
AS
519 if (usbhid->ctrlhead != usbhid->ctrltail &&
520 hid_submit_ctrl(hid) == 0) {
521 /* Successfully submitted next urb in queue */
522 spin_unlock(&usbhid->lock);
523 return;
524 }
48b4554a 525 }
1da177e4 526
48b4554a 527 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
0361a28d 528 spin_unlock(&usbhid->lock);
68229689 529 usb_autopm_put_interface_async(usbhid->intf);
1d1bdd20 530 wake_up(&usbhid->wait);
48b4554a 531}
1da177e4 532
52cfc61b
HS
533static void __usbhid_submit_report(struct hid_device *hid, struct hid_report *report,
534 unsigned char dir)
48b4554a
JK
535{
536 int head;
48b4554a 537 struct usbhid_device *usbhid = hid->driver_data;
f129ea6d 538 int len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
1da177e4 539
48b4554a
JK
540 if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
541 return;
b857c651 542
48b4554a 543 if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
48b4554a 544 if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
4291ee30 545 hid_warn(hid, "output queue full\n");
48b4554a
JK
546 return;
547 }
1da177e4 548
f129ea6d
AH
549 usbhid->out[usbhid->outhead].raw_report = kmalloc(len, GFP_ATOMIC);
550 if (!usbhid->out[usbhid->outhead].raw_report) {
4291ee30 551 hid_warn(hid, "output queueing failed\n");
f129ea6d
AH
552 return;
553 }
554 hid_output_report(report, usbhid->out[usbhid->outhead].raw_report);
555 usbhid->out[usbhid->outhead].report = report;
48b4554a 556 usbhid->outhead = head;
4871d3be 557
01a7c984
AS
558 /* If the queue isn't running, restart it */
559 if (!test_bit(HID_OUT_RUNNING, &usbhid->iofl)) {
560 usbhid_restart_out_queue(usbhid);
f0befcd6 561
01a7c984
AS
562 /* Otherwise see if an earlier request has timed out */
563 } else if (time_after(jiffies, usbhid->last_out + HZ * 5)) {
564
565 /* Prevent autosuspend following the unlink */
566 usb_autopm_get_interface_no_resume(usbhid->intf);
f0befcd6 567
858155fb 568 /*
01a7c984
AS
569 * Prevent resubmission in case the URB completes
570 * before we can unlink it. We don't want to cancel
571 * the wrong transfer!
858155fb 572 */
01a7c984
AS
573 usb_block_urb(usbhid->urbout);
574
575 /* Drop lock to avoid deadlock if the callback runs */
576 spin_unlock(&usbhid->lock);
577
578 usb_unlink_urb(usbhid->urbout);
579 spin_lock(&usbhid->lock);
580 usb_unblock_urb(usbhid->urbout);
581
582 /* Unlink might have stopped the queue */
583 if (!test_bit(HID_OUT_RUNNING, &usbhid->iofl))
584 usbhid_restart_out_queue(usbhid);
585
586 /* Now we can allow autosuspend again */
587 usb_autopm_put_interface_async(usbhid->intf);
858155fb 588 }
48b4554a
JK
589 return;
590 }
1da177e4 591
48b4554a 592 if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
4291ee30 593 hid_warn(hid, "control queue full\n");
48b4554a
JK
594 return;
595 }
8fd80133 596
f129ea6d
AH
597 if (dir == USB_DIR_OUT) {
598 usbhid->ctrl[usbhid->ctrlhead].raw_report = kmalloc(len, GFP_ATOMIC);
599 if (!usbhid->ctrl[usbhid->ctrlhead].raw_report) {
4291ee30 600 hid_warn(hid, "control queueing failed\n");
f129ea6d
AH
601 return;
602 }
603 hid_output_report(report, usbhid->ctrl[usbhid->ctrlhead].raw_report);
604 }
48b4554a
JK
605 usbhid->ctrl[usbhid->ctrlhead].report = report;
606 usbhid->ctrl[usbhid->ctrlhead].dir = dir;
607 usbhid->ctrlhead = head;
8fd80133 608
01a7c984
AS
609 /* If the queue isn't running, restart it */
610 if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl)) {
611 usbhid_restart_ctrl_queue(usbhid);
f0befcd6 612
01a7c984
AS
613 /* Otherwise see if an earlier request has timed out */
614 } else if (time_after(jiffies, usbhid->last_ctrl + HZ * 5)) {
615
616 /* Prevent autosuspend following the unlink */
617 usb_autopm_get_interface_no_resume(usbhid->intf);
f0befcd6 618
858155fb 619 /*
01a7c984
AS
620 * Prevent resubmission in case the URB completes
621 * before we can unlink it. We don't want to cancel
622 * the wrong transfer!
858155fb 623 */
01a7c984
AS
624 usb_block_urb(usbhid->urbctrl);
625
626 /* Drop lock to avoid deadlock if the callback runs */
627 spin_unlock(&usbhid->lock);
628
629 usb_unlink_urb(usbhid->urbctrl);
630 spin_lock(&usbhid->lock);
631 usb_unblock_urb(usbhid->urbctrl);
632
633 /* Unlink might have stopped the queue */
634 if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
635 usbhid_restart_ctrl_queue(usbhid);
636
637 /* Now we can allow autosuspend again */
638 usb_autopm_put_interface_async(usbhid->intf);
858155fb 639 }
0361a28d 640}
931e24b9 641
d8814272 642static void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
0361a28d
ON
643{
644 struct usbhid_device *usbhid = hid->driver_data;
645 unsigned long flags;
931e24b9 646
0361a28d
ON
647 spin_lock_irqsave(&usbhid->lock, flags);
648 __usbhid_submit_report(hid, report, dir);
649 spin_unlock_irqrestore(&usbhid->lock, flags);
48b4554a 650}
23b0d968 651
4371ea82
DK
652/* Workqueue routine to send requests to change LEDs */
653static void hid_led(struct work_struct *work)
654{
655 struct usbhid_device *usbhid =
656 container_of(work, struct usbhid_device, led_work);
657 struct hid_device *hid = usbhid->hid;
658 struct hid_field *field;
659 unsigned long flags;
660
661 field = hidinput_get_led_field(hid);
662 if (!field) {
663 hid_warn(hid, "LED event field not found\n");
664 return;
665 }
666
60682284
DH
667 /*
668 * field->report is accessed unlocked regarding HID core. So there might
669 * be another incoming SET-LED request from user-space, which changes
670 * the LED state while we assemble our outgoing buffer. However, this
671 * doesn't matter as hid_output_report() correctly converts it into a
672 * boolean value no matter what information is currently set on the LED
673 * field (even garbage). So the remote device will always get a valid
674 * request.
675 * And in case we send a wrong value, a next hid_led() worker is spawned
676 * for every SET-LED request so the following hid_led() worker will send
677 * the correct value, guaranteed!
678 */
679
4371ea82
DK
680 spin_lock_irqsave(&usbhid->lock, flags);
681 if (!test_bit(HID_DISCONNECTED, &usbhid->iofl)) {
682 usbhid->ledcount = hidinput_count_leds(hid);
683 hid_dbg(usbhid->hid, "New ledcount = %u\n", usbhid->ledcount);
684 __usbhid_submit_report(hid, field->report, USB_DIR_OUT);
685 }
686 spin_unlock_irqrestore(&usbhid->lock, flags);
687}
688
48b4554a
JK
689static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
690{
e0712985 691 struct hid_device *hid = input_get_drvdata(dev);
0361a28d 692 struct usbhid_device *usbhid = hid->driver_data;
48b4554a
JK
693 struct hid_field *field;
694 int offset;
41ad5fba 695
48b4554a
JK
696 if (type == EV_FF)
697 return input_ff_event(dev, type, code, value);
8d2bad87 698
48b4554a
JK
699 if (type != EV_LED)
700 return -1;
5556feae 701
48b4554a 702 if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
4291ee30 703 hid_warn(dev, "event field not found\n");
48b4554a
JK
704 return -1;
705 }
4a1a4d8b 706
48b4554a 707 hid_set_field(field, offset, value);
4371ea82
DK
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);
1da177e4 714
48b4554a
JK
715 return 0;
716}
1da177e4 717
b7966a4d 718static int usbhid_wait_io(struct hid_device *hid)
48b4554a
JK
719{
720 struct usbhid_device *usbhid = hid->driver_data;
25914662 721
1d1bdd20
JS
722 if (!wait_event_timeout(usbhid->wait,
723 (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
724 !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
48b4554a 725 10*HZ)) {
58037eb9 726 dbg_hid("timeout waiting for ctrl or out queue to clear\n");
48b4554a
JK
727 return -1;
728 }
1da177e4 729
48b4554a
JK
730 return 0;
731}
53880546 732
48b4554a
JK
733static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
734{
735 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
736 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
737 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
738}
1da177e4 739
48b4554a
JK
740static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
741 unsigned char type, void *buf, int size)
742{
743 int result, retries = 4;
1da177e4 744
48b4554a 745 memset(buf, 0, size);
1da177e4 746
48b4554a
JK
747 do {
748 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
749 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
750 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
751 retries--;
752 } while (result < size && retries);
753 return result;
754}
940824b0 755
48b4554a
JK
756int usbhid_open(struct hid_device *hid)
757{
933e3187 758 struct usbhid_device *usbhid = hid->driver_data;
a8c52b66 759 int res = 0;
933e3187 760
0361a28d 761 mutex_lock(&hid_open_mut);
933e3187
ON
762 if (!hid->open++) {
763 res = usb_autopm_get_interface(usbhid->intf);
68229689 764 /* the device must be awake to reliably request remote wakeup */
933e3187
ON
765 if (res < 0) {
766 hid->open--;
a8c52b66
ON
767 res = -EIO;
768 goto done;
933e3187 769 }
0361a28d 770 usbhid->intf->needs_remote_wakeup = 1;
a8c52b66
ON
771 res = hid_start_in(hid);
772 if (res) {
773 if (res != -ENOSPC) {
774 hid_io_error(hid);
775 res = 0;
776 } else {
777 /* no use opening if resources are insufficient */
778 hid->open--;
779 res = -EBUSY;
780 usbhid->intf->needs_remote_wakeup = 0;
781 }
782 }
0361a28d 783 usb_autopm_put_interface(usbhid->intf);
933e3187 784 }
a8c52b66 785done:
0361a28d 786 mutex_unlock(&hid_open_mut);
a8c52b66 787 return res;
48b4554a 788}
a417a21e 789
48b4554a
JK
790void usbhid_close(struct hid_device *hid)
791{
792 struct usbhid_device *usbhid = hid->driver_data;
eab9edd2 793
0361a28d
ON
794 mutex_lock(&hid_open_mut);
795
796 /* protecting hid->open to make sure we don't restart
797 * data acquistion due to a resumption we no longer
798 * care about
799 */
800 spin_lock_irq(&usbhid->lock);
933e3187 801 if (!--hid->open) {
0361a28d 802 spin_unlock_irq(&usbhid->lock);
89092ddd 803 hid_cancel_delayed_stuff(usbhid);
48b4554a 804 usb_kill_urb(usbhid->urbin);
0361a28d
ON
805 usbhid->intf->needs_remote_wakeup = 0;
806 } else {
807 spin_unlock_irq(&usbhid->lock);
933e3187 808 }
0361a28d 809 mutex_unlock(&hid_open_mut);
48b4554a 810}
1d3e2023 811
48b4554a
JK
812/*
813 * Initialize all reports
814 */
41ad5fba 815
48b4554a
JK
816void usbhid_init_reports(struct hid_device *hid)
817{
818 struct hid_report *report;
819 struct usbhid_device *usbhid = hid->driver_data;
820 int err, ret;
41ad5fba 821
48b4554a
JK
822 list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
823 usbhid_submit_report(hid, report, USB_DIR_IN);
5556feae 824
48b4554a
JK
825 list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
826 usbhid_submit_report(hid, report, USB_DIR_IN);
4a1a4d8b 827
48b4554a
JK
828 err = 0;
829 ret = usbhid_wait_io(hid);
830 while (ret) {
831 err |= ret;
832 if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
833 usb_kill_urb(usbhid->urbctrl);
834 if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
835 usb_kill_urb(usbhid->urbout);
836 ret = usbhid_wait_io(hid);
837 }
3f9b4076 838
48b4554a 839 if (err)
4291ee30 840 hid_warn(hid, "timeout initializing reports\n");
48b4554a 841}
1da177e4 842
713c8aad
PZ
843/*
844 * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
845 */
846static int hid_find_field_early(struct hid_device *hid, unsigned int page,
847 unsigned int hid_code, struct hid_field **pfield)
848{
849 struct hid_report *report;
850 struct hid_field *field;
851 struct hid_usage *usage;
852 int i, j;
853
854 list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
855 for (i = 0; i < report->maxfield; i++) {
856 field = report->field[i];
857 for (j = 0; j < field->maxusage; j++) {
858 usage = &field->usage[j];
859 if ((usage->hid & HID_USAGE_PAGE) == page &&
860 (usage->hid & 0xFFFF) == hid_code) {
861 *pfield = field;
862 return j;
863 }
864 }
865 }
866 }
867 return -1;
868}
869
ddf64a3c 870static void usbhid_set_leds(struct hid_device *hid)
713c8aad
PZ
871{
872 struct hid_field *field;
873 int offset;
874
875 if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
876 hid_set_field(field, offset, 0);
877 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
878 }
879}
880
bf0964dc
MH
881/*
882 * Traverse the supplied list of reports and find the longest
883 */
282bfd4c
JS
884static void hid_find_max_report(struct hid_device *hid, unsigned int type,
885 unsigned int *max)
bf0964dc
MH
886{
887 struct hid_report *report;
282bfd4c 888 unsigned int size;
bf0964dc
MH
889
890 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
efc7ce18 891 size = ((report->size - 1) >> 3) + 1 + hid->report_enum[type].numbered;
bf0964dc
MH
892 if (*max < size)
893 *max = size;
894 }
895}
896
1da177e4
LT
897static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
898{
4916b3a5
JK
899 struct usbhid_device *usbhid = hid->driver_data;
900
997ea58e 901 usbhid->inbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
898089d0 902 &usbhid->inbuf_dma);
997ea58e 903 usbhid->outbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
898089d0 904 &usbhid->outbuf_dma);
0ede76fc 905 usbhid->cr = kmalloc(sizeof(*usbhid->cr), GFP_KERNEL);
997ea58e 906 usbhid->ctrlbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
898089d0
JS
907 &usbhid->ctrlbuf_dma);
908 if (!usbhid->inbuf || !usbhid->outbuf || !usbhid->cr ||
909 !usbhid->ctrlbuf)
1da177e4
LT
910 return -1;
911
912 return 0;
913}
914
b4dbde9d
AO
915static int usbhid_get_raw_report(struct hid_device *hid,
916 unsigned char report_number, __u8 *buf, size_t count,
917 unsigned char report_type)
918{
919 struct usbhid_device *usbhid = hid->driver_data;
920 struct usb_device *dev = hid_to_usb_dev(hid);
921 struct usb_interface *intf = usbhid->intf;
922 struct usb_host_interface *interface = intf->cur_altsetting;
923 int skipped_report_id = 0;
924 int ret;
925
926 /* Byte 0 is the report number. Report data starts at byte 1.*/
927 buf[0] = report_number;
928 if (report_number == 0x0) {
929 /* Offset the return buffer by 1, so that the report ID
930 will remain in byte 0. */
931 buf++;
932 count--;
933 skipped_report_id = 1;
934 }
935 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
936 HID_REQ_GET_REPORT,
937 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
938 ((report_type + 1) << 8) | report_number,
939 interface->desc.bInterfaceNumber, buf, count,
940 USB_CTRL_SET_TIMEOUT);
941
942 /* count also the report id */
943 if (ret > 0 && skipped_report_id)
944 ret++;
945
946 return ret;
947}
948
d4bfa033
JK
949static int usbhid_output_raw_report(struct hid_device *hid, __u8 *buf, size_t count,
950 unsigned char report_type)
efc493f9
JK
951{
952 struct usbhid_device *usbhid = hid->driver_data;
953 struct usb_device *dev = hid_to_usb_dev(hid);
954 struct usb_interface *intf = usbhid->intf;
955 struct usb_host_interface *interface = intf->cur_altsetting;
956 int ret;
957
fe2c91ee 958 if (usbhid->urbout && report_type != HID_FEATURE_REPORT) {
a8ab5d58
AO
959 int actual_length;
960 int skipped_report_id = 0;
12e52725 961
a8ab5d58
AO
962 if (buf[0] == 0x0) {
963 /* Don't send the Report ID */
964 buf++;
965 count--;
966 skipped_report_id = 1;
967 }
968 ret = usb_interrupt_msg(dev, usbhid->urbout->pipe,
969 buf, count, &actual_length,
970 USB_CTRL_SET_TIMEOUT);
971 /* return the number of bytes transferred */
972 if (ret == 0) {
973 ret = actual_length;
974 /* count also the report id */
975 if (skipped_report_id)
976 ret++;
977 }
978 } else {
29129a98 979 int skipped_report_id = 0;
c29771c2 980 int report_id = buf[0];
29129a98
AO
981 if (buf[0] == 0x0) {
982 /* Don't send the Report ID */
983 buf++;
984 count--;
985 skipped_report_id = 1;
986 }
a8ab5d58
AO
987 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
988 HID_REQ_SET_REPORT,
989 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
c29771c2 990 ((report_type + 1) << 8) | report_id,
29129a98 991 interface->desc.bInterfaceNumber, buf, count,
a8ab5d58 992 USB_CTRL_SET_TIMEOUT);
29129a98
AO
993 /* count also the report id, if this was a numbered report. */
994 if (ret > 0 && skipped_report_id)
a8ab5d58
AO
995 ret++;
996 }
efc493f9
JK
997
998 return ret;
999}
1000
0361a28d
ON
1001static void usbhid_restart_queues(struct usbhid_device *usbhid)
1002{
eb055fd0 1003 if (usbhid->urbout && !test_bit(HID_OUT_RUNNING, &usbhid->iofl))
0361a28d 1004 usbhid_restart_out_queue(usbhid);
eb055fd0
AS
1005 if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
1006 usbhid_restart_ctrl_queue(usbhid);
0361a28d
ON
1007}
1008
1da177e4
LT
1009static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
1010{
4916b3a5
JK
1011 struct usbhid_device *usbhid = hid->driver_data;
1012
997ea58e
DM
1013 usb_free_coherent(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
1014 usb_free_coherent(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
0ede76fc 1015 kfree(usbhid->cr);
997ea58e 1016 usb_free_coherent(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
1da177e4
LT
1017}
1018
c500c971
JS
1019static int usbhid_parse(struct hid_device *hid)
1020{
1021 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
1da177e4
LT
1022 struct usb_host_interface *interface = intf->cur_altsetting;
1023 struct usb_device *dev = interface_to_usbdev (intf);
1024 struct hid_descriptor *hdesc;
2eb5dc30 1025 u32 quirks = 0;
c500c971 1026 unsigned int rsize = 0;
c5b7c7c3 1027 char *rdesc;
c500c971 1028 int ret, n;
1da177e4 1029
2eb5dc30
PW
1030 quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
1031 le16_to_cpu(dev->descriptor.idProduct));
1da177e4 1032
6f4303fb
JK
1033 if (quirks & HID_QUIRK_IGNORE)
1034 return -ENODEV;
1035
0f28b55d
AS
1036 /* Many keyboards and mice don't like to be polled for reports,
1037 * so we will always set the HID_QUIRK_NOGET flag for them. */
1038 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
1039 if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
1040 interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
1041 quirks |= HID_QUIRK_NOGET;
1042 }
1043
c5b7c7c3
DT
1044 if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
1045 (!interface->desc.bNumEndpoints ||
1046 usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
58037eb9 1047 dbg_hid("class descriptor not present\n");
c500c971 1048 return -ENODEV;
1da177e4
LT
1049 }
1050
c500c971
JS
1051 hid->version = le16_to_cpu(hdesc->bcdHID);
1052 hid->country = hdesc->bCountryCode;
1053
1da177e4
LT
1054 for (n = 0; n < hdesc->bNumDescriptors; n++)
1055 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
1056 rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
1057
1058 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
58037eb9 1059 dbg_hid("weird size of report descriptor (%u)\n", rsize);
c500c971 1060 return -EINVAL;
1da177e4
LT
1061 }
1062
1063 if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
58037eb9 1064 dbg_hid("couldn't allocate rdesc memory\n");
c500c971 1065 return -ENOMEM;
1da177e4
LT
1066 }
1067
854561b0
VP
1068 hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
1069
c500c971
JS
1070 ret = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber,
1071 HID_DT_REPORT, rdesc, rsize);
1072 if (ret < 0) {
58037eb9 1073 dbg_hid("reading report descriptor failed\n");
1da177e4 1074 kfree(rdesc);
c500c971 1075 goto err;
1da177e4
LT
1076 }
1077
c500c971
JS
1078 ret = hid_parse_report(hid, rdesc, rsize);
1079 kfree(rdesc);
1080 if (ret) {
58037eb9 1081 dbg_hid("parsing report descriptor failed\n");
c500c971 1082 goto err;
1da177e4
LT
1083 }
1084
f5208997 1085 hid->quirks |= quirks;
1da177e4 1086
c500c971
JS
1087 return 0;
1088err:
1089 return ret;
1090}
1091
1092static int usbhid_start(struct hid_device *hid)
1093{
1094 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
1095 struct usb_host_interface *interface = intf->cur_altsetting;
1096 struct usb_device *dev = interface_to_usbdev(intf);
3d5afd32 1097 struct usbhid_device *usbhid = hid->driver_data;
c500c971
JS
1098 unsigned int n, insize = 0;
1099 int ret;
1100
e3e14de5
JS
1101 clear_bit(HID_DISCONNECTED, &usbhid->iofl);
1102
4916b3a5
JK
1103 usbhid->bufsize = HID_MIN_BUFFER_SIZE;
1104 hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
1105 hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
1106 hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
bf0964dc 1107
4916b3a5
JK
1108 if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
1109 usbhid->bufsize = HID_MAX_BUFFER_SIZE;
bf0964dc
MH
1110
1111 hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
1112
1113 if (insize > HID_MAX_BUFFER_SIZE)
1114 insize = HID_MAX_BUFFER_SIZE;
1115
c500c971
JS
1116 if (hid_alloc_buffers(dev, hid)) {
1117 ret = -ENOMEM;
1da177e4 1118 goto fail;
f345c37c
PS
1119 }
1120
1da177e4 1121 for (n = 0; n < interface->desc.bNumEndpoints; n++) {
1da177e4
LT
1122 struct usb_endpoint_descriptor *endpoint;
1123 int pipe;
1124 int interval;
1125
1126 endpoint = &interface->endpoint[n].desc;
581a2739 1127 if (!usb_endpoint_xfer_int(endpoint))
1da177e4
LT
1128 continue;
1129
1da177e4 1130 interval = endpoint->bInterval;
1da177e4 1131
f345c37c 1132 /* Some vendors give fullspeed interval on highspeed devides */
c500c971 1133 if (hid->quirks & HID_QUIRK_FULLSPEED_INTERVAL &&
f345c37c
PS
1134 dev->speed == USB_SPEED_HIGH) {
1135 interval = fls(endpoint->bInterval*8);
1136 printk(KERN_INFO "%s: Fixing fullspeed to highspeed interval: %d -> %d\n",
1137 hid->name, endpoint->bInterval, interval);
1138 }
1139
1da177e4
LT
1140 /* Change the polling interval of mice. */
1141 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
1142 interval = hid_mousepoll_interval;
05f091ab 1143
c500c971 1144 ret = -ENOMEM;
0f12aa03 1145 if (usb_endpoint_dir_in(endpoint)) {
4916b3a5 1146 if (usbhid->urbin)
1da177e4 1147 continue;
4916b3a5 1148 if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
1da177e4
LT
1149 goto fail;
1150 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
4916b3a5 1151 usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
1da177e4 1152 hid_irq_in, hid, interval);
4916b3a5
JK
1153 usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
1154 usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1da177e4 1155 } else {
4916b3a5 1156 if (usbhid->urbout)
1da177e4 1157 continue;
4916b3a5 1158 if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
1da177e4
LT
1159 goto fail;
1160 pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
4916b3a5 1161 usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
1da177e4 1162 hid_irq_out, hid, interval);
4916b3a5
JK
1163 usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
1164 usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1da177e4
LT
1165 }
1166 }
1167
4916b3a5 1168 usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
c500c971
JS
1169 if (!usbhid->urbctrl) {
1170 ret = -ENOMEM;
1da177e4 1171 goto fail;
c500c971 1172 }
c5b7c7c3 1173
4916b3a5
JK
1174 usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
1175 usbhid->ctrlbuf, 1, hid_ctrl, hid);
4916b3a5 1176 usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
0ede76fc 1177 usbhid->urbctrl->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
c500c971 1178
5b915d9e
JK
1179 if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
1180 usbhid_init_reports(hid);
93c10132 1181
3d5afd32 1182 set_bit(HID_STARTED, &usbhid->iofl);
3d5afd32 1183
08ef08ee
AS
1184 /* Some keyboards don't work until their LEDs have been set.
1185 * Since BIOSes do set the LEDs, it must be safe for any device
1186 * that supports the keyboard boot protocol.
3d61510f
AS
1187 * In addition, enable remote wakeup by default for all keyboard
1188 * devices supporting the boot protocol.
08ef08ee
AS
1189 */
1190 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT &&
1191 interface->desc.bInterfaceProtocol ==
3d61510f 1192 USB_INTERFACE_PROTOCOL_KEYBOARD) {
08ef08ee 1193 usbhid_set_leds(hid);
3d61510f
AS
1194 device_set_wakeup_enable(&dev->dev, 1);
1195 }
c500c971 1196 return 0;
1da177e4
LT
1197
1198fail:
4916b3a5
JK
1199 usb_free_urb(usbhid->urbin);
1200 usb_free_urb(usbhid->urbout);
1201 usb_free_urb(usbhid->urbctrl);
e3e14de5
JS
1202 usbhid->urbin = NULL;
1203 usbhid->urbout = NULL;
1204 usbhid->urbctrl = NULL;
22f675f3 1205 hid_free_buffers(dev, hid);
c500c971 1206 return ret;
1da177e4
LT
1207}
1208
c500c971 1209static void usbhid_stop(struct hid_device *hid)
1da177e4 1210{
c500c971 1211 struct usbhid_device *usbhid = hid->driver_data;
1da177e4 1212
c500c971 1213 if (WARN_ON(!usbhid))
1da177e4
LT
1214 return;
1215
3d5afd32 1216 clear_bit(HID_STARTED, &usbhid->iofl);
4371ea82 1217 spin_lock_irq(&usbhid->lock); /* Sync with error and led handlers */
69626f23 1218 set_bit(HID_DISCONNECTED, &usbhid->iofl);
0361a28d 1219 spin_unlock_irq(&usbhid->lock);
4916b3a5
JK
1220 usb_kill_urb(usbhid->urbin);
1221 usb_kill_urb(usbhid->urbout);
1222 usb_kill_urb(usbhid->urbctrl);
1da177e4 1223
0361a28d 1224 hid_cancel_delayed_stuff(usbhid);
aef4e266 1225
c500c971
JS
1226 hid->claimed = 0;
1227
4916b3a5
JK
1228 usb_free_urb(usbhid->urbin);
1229 usb_free_urb(usbhid->urbctrl);
1230 usb_free_urb(usbhid->urbout);
e3e14de5
JS
1231 usbhid->urbin = NULL; /* don't mess up next start */
1232 usbhid->urbctrl = NULL;
1233 usbhid->urbout = NULL;
1da177e4 1234
be820975 1235 hid_free_buffers(hid_to_usb_dev(hid), hid);
1da177e4
LT
1236}
1237
0361a28d
ON
1238static int usbhid_power(struct hid_device *hid, int lvl)
1239{
1240 int r = 0;
1241
1242 switch (lvl) {
1243 case PM_HINT_FULLON:
1244 r = usbhid_get_power(hid);
1245 break;
1246 case PM_HINT_NORMAL:
1247 usbhid_put_power(hid);
1248 break;
1249 }
1250 return r;
1251}
1252
e90a6df8
HR
1253static void usbhid_request(struct hid_device *hid, struct hid_report *rep, int reqtype)
1254{
1255 switch (reqtype) {
1256 case HID_REQ_GET_REPORT:
1257 usbhid_submit_report(hid, rep, USB_DIR_IN);
1258 break;
1259 case HID_REQ_SET_REPORT:
1260 usbhid_submit_report(hid, rep, USB_DIR_OUT);
1261 break;
1262 }
1263}
1264
9684819b
BT
1265static int usbhid_idle(struct hid_device *hid, int report, int idle,
1266 int reqtype)
1267{
1268 struct usb_device *dev = hid_to_usb_dev(hid);
1269 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
1270 struct usb_host_interface *interface = intf->cur_altsetting;
1271 int ifnum = interface->desc.bInterfaceNumber;
1272
1273 if (reqtype != HID_REQ_SET_IDLE)
1274 return -EINVAL;
1275
1276 return hid_set_idle(dev, ifnum, report, idle);
1277}
1278
c500c971
JS
1279static struct hid_ll_driver usb_hid_driver = {
1280 .parse = usbhid_parse,
1281 .start = usbhid_start,
1282 .stop = usbhid_stop,
1283 .open = usbhid_open,
1284 .close = usbhid_close,
0361a28d 1285 .power = usbhid_power,
c500c971 1286 .hidinput_input_event = usb_hidinput_input_event,
e90a6df8 1287 .request = usbhid_request,
3373443b 1288 .wait = usbhid_wait_io,
9684819b 1289 .idle = usbhid_idle,
c500c971
JS
1290};
1291
c4c259bc 1292static int usbhid_probe(struct usb_interface *intf, const struct usb_device_id *id)
1da177e4 1293{
131d3a7a 1294 struct usb_host_interface *interface = intf->cur_altsetting;
c500c971 1295 struct usb_device *dev = interface_to_usbdev(intf);
3d5afd32 1296 struct usbhid_device *usbhid;
1da177e4 1297 struct hid_device *hid;
131d3a7a 1298 unsigned int n, has_in = 0;
c500c971
JS
1299 size_t len;
1300 int ret;
1da177e4 1301
58037eb9 1302 dbg_hid("HID probe called for ifnum %d\n",
1da177e4
LT
1303 intf->altsetting->desc.bInterfaceNumber);
1304
131d3a7a
JS
1305 for (n = 0; n < interface->desc.bNumEndpoints; n++)
1306 if (usb_endpoint_is_int_in(&interface->endpoint[n].desc))
1307 has_in++;
1308 if (!has_in) {
4291ee30 1309 hid_err(intf, "couldn't find an input interrupt endpoint\n");
131d3a7a
JS
1310 return -ENODEV;
1311 }
1312
c500c971
JS
1313 hid = hid_allocate_device();
1314 if (IS_ERR(hid))
1315 return PTR_ERR(hid);
1da177e4
LT
1316
1317 usb_set_intfdata(intf, hid);
c500c971 1318 hid->ll_driver = &usb_hid_driver;
b4dbde9d 1319 hid->hid_get_raw_report = usbhid_get_raw_report;
c500c971 1320 hid->hid_output_raw_report = usbhid_output_raw_report;
76483cf4 1321 hid->ff_init = hid_pidff_init;
c500c971 1322#ifdef CONFIG_USB_HIDDEV
93c10132 1323 hid->hiddev_connect = hiddev_connect;
c4c259bc 1324 hid->hiddev_disconnect = hiddev_disconnect;
c500c971
JS
1325 hid->hiddev_hid_event = hiddev_hid_event;
1326 hid->hiddev_report_event = hiddev_report_event;
1327#endif
1328 hid->dev.parent = &intf->dev;
1329 hid->bus = BUS_USB;
1330 hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
1331 hid->product = le16_to_cpu(dev->descriptor.idProduct);
1332 hid->name[0] = 0;
b5e5a37e 1333 hid->quirks = usbhid_lookup_quirk(hid->vendor, hid->product);
a73a6370
JS
1334 if (intf->cur_altsetting->desc.bInterfaceProtocol ==
1335 USB_INTERFACE_PROTOCOL_MOUSE)
1336 hid->type = HID_TYPE_USBMOUSE;
6dc1418e
TS
1337 else if (intf->cur_altsetting->desc.bInterfaceProtocol == 0)
1338 hid->type = HID_TYPE_USBNONE;
1da177e4 1339
c500c971
JS
1340 if (dev->manufacturer)
1341 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
1da177e4 1342
c500c971
JS
1343 if (dev->product) {
1344 if (dev->manufacturer)
1345 strlcat(hid->name, " ", sizeof(hid->name));
1346 strlcat(hid->name, dev->product, sizeof(hid->name));
1da177e4
LT
1347 }
1348
c500c971
JS
1349 if (!strlen(hid->name))
1350 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
1351 le16_to_cpu(dev->descriptor.idVendor),
1352 le16_to_cpu(dev->descriptor.idProduct));
1da177e4 1353
c500c971
JS
1354 usb_make_path(dev, hid->phys, sizeof(hid->phys));
1355 strlcat(hid->phys, "/input", sizeof(hid->phys));
1356 len = strlen(hid->phys);
1357 if (len < sizeof(hid->phys) - 1)
1358 snprintf(hid->phys + len, sizeof(hid->phys) - len,
1359 "%d", intf->altsetting[0].desc.bInterfaceNumber);
1360
1361 if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
1362 hid->uniq[0] = 0;
1da177e4 1363
3d5afd32
JS
1364 usbhid = kzalloc(sizeof(*usbhid), GFP_KERNEL);
1365 if (usbhid == NULL) {
1366 ret = -ENOMEM;
1367 goto err;
1368 }
1369
1370 hid->driver_data = usbhid;
1371 usbhid->hid = hid;
57ab12e4
JK
1372 usbhid->intf = intf;
1373 usbhid->ifnum = interface->desc.bInterfaceNumber;
3d5afd32 1374
fde4e2f7
AS
1375 init_waitqueue_head(&usbhid->wait);
1376 INIT_WORK(&usbhid->reset_work, hid_reset);
fde4e2f7
AS
1377 setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
1378 spin_lock_init(&usbhid->lock);
1379
4371ea82
DK
1380 INIT_WORK(&usbhid->led_work, hid_led);
1381
85cdaf52
JS
1382 ret = hid_add_device(hid);
1383 if (ret) {
d458a9df 1384 if (ret != -ENODEV)
4291ee30 1385 hid_err(intf, "can't add hid device: %d\n", ret);
3d5afd32 1386 goto err_free;
85cdaf52 1387 }
c500c971
JS
1388
1389 return 0;
3d5afd32
JS
1390err_free:
1391 kfree(usbhid);
c500c971
JS
1392err:
1393 hid_destroy_device(hid);
85cdaf52 1394 return ret;
1da177e4
LT
1395}
1396
c4c259bc 1397static void usbhid_disconnect(struct usb_interface *intf)
c500c971
JS
1398{
1399 struct hid_device *hid = usb_get_intfdata(intf);
3d5afd32 1400 struct usbhid_device *usbhid;
c500c971
JS
1401
1402 if (WARN_ON(!hid))
1403 return;
1404
3d5afd32 1405 usbhid = hid->driver_data;
c500c971 1406 hid_destroy_device(hid);
3d5afd32 1407 kfree(usbhid);
c500c971
JS
1408}
1409
0361a28d
ON
1410static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid)
1411{
1412 del_timer_sync(&usbhid->io_retry);
0361a28d 1413 cancel_work_sync(&usbhid->reset_work);
4371ea82 1414 cancel_work_sync(&usbhid->led_work);
0361a28d
ON
1415}
1416
1417static void hid_cease_io(struct usbhid_device *usbhid)
1418{
fad9fbe8 1419 del_timer_sync(&usbhid->io_retry);
0361a28d
ON
1420 usb_kill_urb(usbhid->urbin);
1421 usb_kill_urb(usbhid->urbctrl);
1422 usb_kill_urb(usbhid->urbout);
0361a28d
ON
1423}
1424
ae2f0074
JK
1425/* Treat USB reset pretty much the same as suspend/resume */
1426static int hid_pre_reset(struct usb_interface *intf)
1427{
1428 struct hid_device *hid = usb_get_intfdata(intf);
1429 struct usbhid_device *usbhid = hid->driver_data;
1430
1431 spin_lock_irq(&usbhid->lock);
1432 set_bit(HID_RESET_PENDING, &usbhid->iofl);
1433 spin_unlock_irq(&usbhid->lock);
1434 hid_cease_io(usbhid);
1435
1436 return 0;
1437}
1438
1439/* Same routine used for post_reset and reset_resume */
1440static int hid_post_reset(struct usb_interface *intf)
1441{
1442 struct usb_device *dev = interface_to_usbdev (intf);
1443 struct hid_device *hid = usb_get_intfdata(intf);
1444 struct usbhid_device *usbhid = hid->driver_data;
dc3c78e4 1445 struct usb_host_interface *interface = intf->cur_altsetting;
ae2f0074 1446 int status;
dc3c78e4
SH
1447 char *rdesc;
1448
1449 /* Fetch and examine the HID report descriptor. If this
1450 * has changed, then rebind. Since usbcore's check of the
1451 * configuration descriptors passed, we already know that
1452 * the size of the HID report descriptor has not changed.
1453 */
86e6b77e 1454 rdesc = kmalloc(hid->dev_rsize, GFP_KERNEL);
dc3c78e4
SH
1455 if (!rdesc) {
1456 dbg_hid("couldn't allocate rdesc memory (post_reset)\n");
1457 return 1;
1458 }
1459 status = hid_get_class_descriptor(dev,
1460 interface->desc.bInterfaceNumber,
86e6b77e 1461 HID_DT_REPORT, rdesc, hid->dev_rsize);
dc3c78e4
SH
1462 if (status < 0) {
1463 dbg_hid("reading report descriptor failed (post_reset)\n");
1464 kfree(rdesc);
1465 return 1;
1466 }
86e6b77e 1467 status = memcmp(rdesc, hid->dev_rdesc, hid->dev_rsize);
dc3c78e4
SH
1468 kfree(rdesc);
1469 if (status != 0) {
1470 dbg_hid("report descriptor changed\n");
1471 return 1;
1472 }
70fa9f2e 1473
ae2f0074
JK
1474 spin_lock_irq(&usbhid->lock);
1475 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
1476 spin_unlock_irq(&usbhid->lock);
1477 hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
ae2f0074
JK
1478 status = hid_start_in(hid);
1479 if (status < 0)
1480 hid_io_error(hid);
1481 usbhid_restart_queues(usbhid);
1482
1483 return 0;
1484}
1485
1486int usbhid_get_power(struct hid_device *hid)
1487{
1488 struct usbhid_device *usbhid = hid->driver_data;
70fa9f2e 1489
ae2f0074
JK
1490 return usb_autopm_get_interface(usbhid->intf);
1491}
1492
1493void usbhid_put_power(struct hid_device *hid)
1494{
1495 struct usbhid_device *usbhid = hid->driver_data;
70fa9f2e 1496
ae2f0074
JK
1497 usb_autopm_put_interface(usbhid->intf);
1498}
1499
1500
0f6f1407 1501#ifdef CONFIG_PM
eb055fd0
AS
1502static int hid_resume_common(struct hid_device *hid, bool driver_suspended)
1503{
1504 struct usbhid_device *usbhid = hid->driver_data;
1505 int status;
1506
1507 spin_lock_irq(&usbhid->lock);
1508 clear_bit(HID_SUSPENDED, &usbhid->iofl);
1509 usbhid_mark_busy(usbhid);
1510
1511 if (test_bit(HID_CLEAR_HALT, &usbhid->iofl) ||
1512 test_bit(HID_RESET_PENDING, &usbhid->iofl))
1513 schedule_work(&usbhid->reset_work);
1514 usbhid->retry_delay = 0;
1515
1516 usbhid_restart_queues(usbhid);
1517 spin_unlock_irq(&usbhid->lock);
1518
1519 status = hid_start_in(hid);
1520 if (status < 0)
1521 hid_io_error(hid);
1522
1523 if (driver_suspended && hid->driver && hid->driver->resume)
1524 status = hid->driver->resume(hid);
1525 return status;
1526}
1527
27d72e85 1528static int hid_suspend(struct usb_interface *intf, pm_message_t message)
1da177e4 1529{
0361a28d 1530 struct hid_device *hid = usb_get_intfdata(intf);
4916b3a5 1531 struct usbhid_device *usbhid = hid->driver_data;
37093b70 1532 int status = 0;
eb055fd0 1533 bool driver_suspended = false;
1da177e4 1534
5b1b0b81 1535 if (PMSG_IS_AUTO(message)) {
0361a28d
ON
1536 spin_lock_irq(&usbhid->lock); /* Sync with error handler */
1537 if (!test_bit(HID_RESET_PENDING, &usbhid->iofl)
1538 && !test_bit(HID_CLEAR_HALT, &usbhid->iofl)
1539 && !test_bit(HID_OUT_RUNNING, &usbhid->iofl)
1540 && !test_bit(HID_CTRL_RUNNING, &usbhid->iofl)
1541 && !test_bit(HID_KEYS_PRESSED, &usbhid->iofl)
1542 && (!usbhid->ledcount || ignoreled))
1543 {
f2b5264d 1544 set_bit(HID_SUSPENDED, &usbhid->iofl);
0361a28d 1545 spin_unlock_irq(&usbhid->lock);
6a740aa4
BP
1546 if (hid->driver && hid->driver->suspend) {
1547 status = hid->driver->suspend(hid, message);
1548 if (status < 0)
eb055fd0 1549 goto failed;
6a740aa4 1550 }
eb055fd0 1551 driver_suspended = true;
0361a28d
ON
1552 } else {
1553 usbhid_mark_busy(usbhid);
1554 spin_unlock_irq(&usbhid->lock);
1555 return -EBUSY;
1556 }
3d5afd32 1557
0361a28d 1558 } else {
37093b70
ML
1559 /* TODO: resume() might need to handle suspend failure */
1560 if (hid->driver && hid->driver->suspend)
6a740aa4 1561 status = hid->driver->suspend(hid, message);
eb055fd0 1562 driver_suspended = true;
0361a28d 1563 spin_lock_irq(&usbhid->lock);
f2b5264d 1564 set_bit(HID_SUSPENDED, &usbhid->iofl);
0361a28d 1565 spin_unlock_irq(&usbhid->lock);
37093b70 1566 if (usbhid_wait_io(hid) < 0)
eb055fd0 1567 status = -EIO;
0361a28d
ON
1568 }
1569
0361a28d
ON
1570 hid_cancel_delayed_stuff(usbhid);
1571 hid_cease_io(usbhid);
1572
5b1b0b81 1573 if (PMSG_IS_AUTO(message) && test_bit(HID_KEYS_PRESSED, &usbhid->iofl)) {
0361a28d 1574 /* lost race against keypresses */
eb055fd0
AS
1575 status = -EBUSY;
1576 goto failed;
0361a28d 1577 }
1da177e4 1578 dev_dbg(&intf->dev, "suspend\n");
37093b70 1579 return status;
eb055fd0
AS
1580
1581 failed:
1582 hid_resume_common(hid, driver_suspended);
1583 return status;
1da177e4
LT
1584}
1585
1586static int hid_resume(struct usb_interface *intf)
1587{
1588 struct hid_device *hid = usb_get_intfdata (intf);
4916b3a5 1589 struct usbhid_device *usbhid = hid->driver_data;
1da177e4
LT
1590 int status;
1591
fde5be35 1592 if (!test_bit(HID_STARTED, &usbhid->iofl))
3d5afd32 1593 return 0;
3d5afd32 1594
eb055fd0 1595 status = hid_resume_common(hid, true);
1da177e4 1596 dev_dbg(&intf->dev, "resume status %d\n", status);
f07600cf 1597 return 0;
df9a1f48
AS
1598}
1599
378a0ede 1600static int hid_reset_resume(struct usb_interface *intf)
df9a1f48 1601{
378a0ede
ON
1602 struct hid_device *hid = usb_get_intfdata(intf);
1603 struct usbhid_device *usbhid = hid->driver_data;
6a740aa4 1604 int status;
df9a1f48 1605
f2b5264d 1606 clear_bit(HID_SUSPENDED, &usbhid->iofl);
6a740aa4
BP
1607 status = hid_post_reset(intf);
1608 if (status >= 0 && hid->driver && hid->driver->reset_resume) {
1609 int ret = hid->driver->reset_resume(hid);
1610 if (ret < 0)
1611 status = ret;
1612 }
1613 return status;
df9a1f48
AS
1614}
1615
ae2f0074 1616#endif /* CONFIG_PM */
df9a1f48 1617
d67dec5b 1618static const struct usb_device_id hid_usb_ids[] = {
1da177e4
LT
1619 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1620 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
1621 { } /* Terminating entry */
1622};
1623
1624MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1625
1626static struct usb_driver hid_driver = {
1da177e4 1627 .name = "usbhid",
c4c259bc
JK
1628 .probe = usbhid_probe,
1629 .disconnect = usbhid_disconnect,
0f6f1407 1630#ifdef CONFIG_PM
1da177e4
LT
1631 .suspend = hid_suspend,
1632 .resume = hid_resume,
378a0ede 1633 .reset_resume = hid_reset_resume,
0f6f1407 1634#endif
df9a1f48
AS
1635 .pre_reset = hid_pre_reset,
1636 .post_reset = hid_post_reset,
1da177e4 1637 .id_table = hid_usb_ids,
933e3187 1638 .supports_autosuspend = 1,
1da177e4
LT
1639};
1640
8fe294ca
GC
1641struct usb_interface *usbhid_find_interface(int minor)
1642{
1643 return usb_find_interface(&hid_driver, minor);
1644}
1645
1da177e4
LT
1646static int __init hid_init(void)
1647{
0361a28d
ON
1648 int retval = -ENOMEM;
1649
876b9276
PW
1650 retval = usbhid_quirks_init(quirks_param);
1651 if (retval)
1652 goto usbhid_quirks_init_fail;
1da177e4
LT
1653 retval = usb_register(&hid_driver);
1654 if (retval)
1655 goto usb_register_fail;
ccabcd2d 1656 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
1da177e4
LT
1657
1658 return 0;
1659usb_register_fail:
876b9276
PW
1660 usbhid_quirks_exit();
1661usbhid_quirks_init_fail:
1da177e4
LT
1662 return retval;
1663}
1664
1665static void __exit hid_exit(void)
1666{
1667 usb_deregister(&hid_driver);
876b9276 1668 usbhid_quirks_exit();
1da177e4
LT
1669}
1670
1671module_init(hid_init);
1672module_exit(hid_exit);
1673
88adb72b
JK
1674MODULE_AUTHOR("Andreas Gal");
1675MODULE_AUTHOR("Vojtech Pavlik");
1676MODULE_AUTHOR("Jiri Kosina");
1da177e4
LT
1677MODULE_DESCRIPTION(DRIVER_DESC);
1678MODULE_LICENSE(DRIVER_LICENSE);