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