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