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