Merge branch 'upstream' into for-linus
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / hid / usbhid / hid-core.c
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
52 static unsigned int hid_mousepoll_interval;
53 module_param_named(mousepoll, hid_mousepoll_interval, uint, 0644);
54 MODULE_PARM_DESC(mousepoll, "Polling interval of mice");
55
56 static unsigned int ignoreled;
57 module_param_named(ignoreled, ignoreled, uint, 0644);
58 MODULE_PARM_DESC(ignoreled, "Autosuspend with active leds");
59
60 /* Quirks specified at module load time */
61 static char *quirks_param[MAX_USBHID_BOOT_QUIRKS] = { [ 0 ... (MAX_USBHID_BOOT_QUIRKS - 1) ] = NULL };
62 module_param_array_named(quirks, quirks_param, charp, NULL, 0444);
63 MODULE_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 */
70 static DEFINE_MUTEX(hid_open_mut);
71
72 static void hid_io_error(struct hid_device *hid);
73 static int hid_submit_out(struct hid_device *hid);
74 static int hid_submit_ctrl(struct hid_device *hid);
75 static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid);
76
77 /* Start up the input URB */
78 static 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 */
103 static 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 */
114 static 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 */
157 static 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));
193 done:
194 spin_unlock_irqrestore(&usbhid->lock, flags);
195 }
196
197 static 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
204 static 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 dbg("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
230 static 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 dbg("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
261 static 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
321 static 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
349 static 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
410 static void hid_irq_out(struct urb *urb)
411 {
412 struct hid_device *hid = urb->context;
413 struct usbhid_device *usbhid = hid->driver_data;
414 unsigned long flags;
415 int unplug = 0;
416
417 switch (urb->status) {
418 case 0: /* success */
419 break;
420 case -ESHUTDOWN: /* unplug */
421 unplug = 1;
422 case -EILSEQ: /* protocol error or unplug */
423 case -EPROTO: /* protocol error or unplug */
424 case -ECONNRESET: /* unlink */
425 case -ENOENT:
426 break;
427 default: /* error */
428 hid_warn(urb->dev, "output irq status %d received\n",
429 urb->status);
430 }
431
432 spin_lock_irqsave(&usbhid->lock, flags);
433
434 if (unplug)
435 usbhid->outtail = usbhid->outhead;
436 else
437 usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
438
439 if (usbhid->outhead != usbhid->outtail && !hid_submit_out(hid)) {
440 /* Successfully submitted next urb in queue */
441 spin_unlock_irqrestore(&usbhid->lock, flags);
442 return;
443 }
444
445 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
446 spin_unlock_irqrestore(&usbhid->lock, flags);
447 usb_autopm_put_interface_async(usbhid->intf);
448 wake_up(&usbhid->wait);
449 }
450
451 /*
452 * Control pipe completion handler.
453 */
454
455 static void hid_ctrl(struct urb *urb)
456 {
457 struct hid_device *hid = urb->context;
458 struct usbhid_device *usbhid = hid->driver_data;
459 int unplug = 0, status = urb->status;
460
461 spin_lock(&usbhid->lock);
462
463 switch (status) {
464 case 0: /* success */
465 if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
466 hid_input_report(urb->context,
467 usbhid->ctrl[usbhid->ctrltail].report->type,
468 urb->transfer_buffer, urb->actual_length, 0);
469 break;
470 case -ESHUTDOWN: /* unplug */
471 unplug = 1;
472 case -EILSEQ: /* protocol error or unplug */
473 case -EPROTO: /* protocol error or unplug */
474 case -ECONNRESET: /* unlink */
475 case -ENOENT:
476 case -EPIPE: /* report not available */
477 break;
478 default: /* error */
479 hid_warn(urb->dev, "ctrl urb status %d received\n", status);
480 }
481
482 if (unplug)
483 usbhid->ctrltail = usbhid->ctrlhead;
484 else
485 usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
486
487 if (usbhid->ctrlhead != usbhid->ctrltail && !hid_submit_ctrl(hid)) {
488 /* Successfully submitted next urb in queue */
489 spin_unlock(&usbhid->lock);
490 return;
491 }
492
493 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
494 spin_unlock(&usbhid->lock);
495 usb_autopm_put_interface_async(usbhid->intf);
496 wake_up(&usbhid->wait);
497 }
498
499 static void __usbhid_submit_report(struct hid_device *hid, struct hid_report *report,
500 unsigned char dir)
501 {
502 int head;
503 struct usbhid_device *usbhid = hid->driver_data;
504 int len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
505
506 if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
507 return;
508
509 if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
510 if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
511 hid_warn(hid, "output queue full\n");
512 return;
513 }
514
515 usbhid->out[usbhid->outhead].raw_report = kmalloc(len, GFP_ATOMIC);
516 if (!usbhid->out[usbhid->outhead].raw_report) {
517 hid_warn(hid, "output queueing failed\n");
518 return;
519 }
520 hid_output_report(report, usbhid->out[usbhid->outhead].raw_report);
521 usbhid->out[usbhid->outhead].report = report;
522 usbhid->outhead = head;
523
524 /* Try to awake from autosuspend... */
525 if (usb_autopm_get_interface_async(usbhid->intf) < 0)
526 return;
527
528 /*
529 * But if still suspended, leave urb enqueued, don't submit.
530 * Submission will occur if/when resume() drains the queue.
531 */
532 if (test_bit(HID_REPORTED_IDLE, &usbhid->iofl))
533 return;
534
535 if (!test_and_set_bit(HID_OUT_RUNNING, &usbhid->iofl)) {
536 if (hid_submit_out(hid)) {
537 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
538 usb_autopm_put_interface_async(usbhid->intf);
539 }
540 wake_up(&usbhid->wait);
541 } else {
542 /*
543 * the queue is known to run
544 * but an earlier request may be stuck
545 * we may need to time out
546 * no race because this is called under
547 * spinlock
548 */
549 if (time_after(jiffies, usbhid->last_out + HZ * 5))
550 usb_unlink_urb(usbhid->urbout);
551 }
552 return;
553 }
554
555 if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
556 hid_warn(hid, "control queue full\n");
557 return;
558 }
559
560 if (dir == USB_DIR_OUT) {
561 usbhid->ctrl[usbhid->ctrlhead].raw_report = kmalloc(len, GFP_ATOMIC);
562 if (!usbhid->ctrl[usbhid->ctrlhead].raw_report) {
563 hid_warn(hid, "control queueing failed\n");
564 return;
565 }
566 hid_output_report(report, usbhid->ctrl[usbhid->ctrlhead].raw_report);
567 }
568 usbhid->ctrl[usbhid->ctrlhead].report = report;
569 usbhid->ctrl[usbhid->ctrlhead].dir = dir;
570 usbhid->ctrlhead = head;
571
572 /* Try to awake from autosuspend... */
573 if (usb_autopm_get_interface_async(usbhid->intf) < 0)
574 return;
575
576 /*
577 * If already suspended, leave urb enqueued, but don't submit.
578 * Submission will occur if/when resume() drains the queue.
579 */
580 if (test_bit(HID_REPORTED_IDLE, &usbhid->iofl))
581 return;
582
583 if (!test_and_set_bit(HID_CTRL_RUNNING, &usbhid->iofl)) {
584 if (hid_submit_ctrl(hid)) {
585 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
586 usb_autopm_put_interface_async(usbhid->intf);
587 }
588 wake_up(&usbhid->wait);
589 } else {
590 /*
591 * the queue is known to run
592 * but an earlier request may be stuck
593 * we may need to time out
594 * no race because this is called under
595 * spinlock
596 */
597 if (time_after(jiffies, usbhid->last_ctrl + HZ * 5))
598 usb_unlink_urb(usbhid->urbctrl);
599 }
600 }
601
602 void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
603 {
604 struct usbhid_device *usbhid = hid->driver_data;
605 unsigned long flags;
606
607 spin_lock_irqsave(&usbhid->lock, flags);
608 __usbhid_submit_report(hid, report, dir);
609 spin_unlock_irqrestore(&usbhid->lock, flags);
610 }
611 EXPORT_SYMBOL_GPL(usbhid_submit_report);
612
613 /* Workqueue routine to send requests to change LEDs */
614 static void hid_led(struct work_struct *work)
615 {
616 struct usbhid_device *usbhid =
617 container_of(work, struct usbhid_device, led_work);
618 struct hid_device *hid = usbhid->hid;
619 struct hid_field *field;
620 unsigned long flags;
621
622 field = hidinput_get_led_field(hid);
623 if (!field) {
624 hid_warn(hid, "LED event field not found\n");
625 return;
626 }
627
628 spin_lock_irqsave(&usbhid->lock, flags);
629 if (!test_bit(HID_DISCONNECTED, &usbhid->iofl)) {
630 usbhid->ledcount = hidinput_count_leds(hid);
631 hid_dbg(usbhid->hid, "New ledcount = %u\n", usbhid->ledcount);
632 __usbhid_submit_report(hid, field->report, USB_DIR_OUT);
633 }
634 spin_unlock_irqrestore(&usbhid->lock, flags);
635 }
636
637 static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
638 {
639 struct hid_device *hid = input_get_drvdata(dev);
640 struct usbhid_device *usbhid = hid->driver_data;
641 struct hid_field *field;
642 unsigned long flags;
643 int offset;
644
645 if (type == EV_FF)
646 return input_ff_event(dev, type, code, value);
647
648 if (type != EV_LED)
649 return -1;
650
651 if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
652 hid_warn(dev, "event field not found\n");
653 return -1;
654 }
655
656 spin_lock_irqsave(&usbhid->lock, flags);
657 hid_set_field(field, offset, value);
658 spin_unlock_irqrestore(&usbhid->lock, flags);
659
660 /*
661 * Defer performing requested LED action.
662 * This is more likely gather all LED changes into a single URB.
663 */
664 schedule_work(&usbhid->led_work);
665
666 return 0;
667 }
668
669 int usbhid_wait_io(struct hid_device *hid)
670 {
671 struct usbhid_device *usbhid = hid->driver_data;
672
673 if (!wait_event_timeout(usbhid->wait,
674 (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
675 !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
676 10*HZ)) {
677 dbg_hid("timeout waiting for ctrl or out queue to clear\n");
678 return -1;
679 }
680
681 return 0;
682 }
683 EXPORT_SYMBOL_GPL(usbhid_wait_io);
684
685 static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
686 {
687 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
688 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
689 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
690 }
691
692 static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
693 unsigned char type, void *buf, int size)
694 {
695 int result, retries = 4;
696
697 memset(buf, 0, size);
698
699 do {
700 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
701 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
702 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
703 retries--;
704 } while (result < size && retries);
705 return result;
706 }
707
708 int usbhid_open(struct hid_device *hid)
709 {
710 struct usbhid_device *usbhid = hid->driver_data;
711 int res = 0;
712
713 mutex_lock(&hid_open_mut);
714 if (!hid->open++) {
715 res = usb_autopm_get_interface(usbhid->intf);
716 /* the device must be awake to reliably request remote wakeup */
717 if (res < 0) {
718 hid->open--;
719 res = -EIO;
720 goto done;
721 }
722 usbhid->intf->needs_remote_wakeup = 1;
723 res = hid_start_in(hid);
724 if (res) {
725 if (res != -ENOSPC) {
726 hid_io_error(hid);
727 res = 0;
728 } else {
729 /* no use opening if resources are insufficient */
730 hid->open--;
731 res = -EBUSY;
732 usbhid->intf->needs_remote_wakeup = 0;
733 }
734 }
735 usb_autopm_put_interface(usbhid->intf);
736 }
737 done:
738 mutex_unlock(&hid_open_mut);
739 return res;
740 }
741
742 void usbhid_close(struct hid_device *hid)
743 {
744 struct usbhid_device *usbhid = hid->driver_data;
745
746 mutex_lock(&hid_open_mut);
747
748 /* protecting hid->open to make sure we don't restart
749 * data acquistion due to a resumption we no longer
750 * care about
751 */
752 spin_lock_irq(&usbhid->lock);
753 if (!--hid->open) {
754 spin_unlock_irq(&usbhid->lock);
755 hid_cancel_delayed_stuff(usbhid);
756 usb_kill_urb(usbhid->urbin);
757 usbhid->intf->needs_remote_wakeup = 0;
758 } else {
759 spin_unlock_irq(&usbhid->lock);
760 }
761 mutex_unlock(&hid_open_mut);
762 }
763
764 /*
765 * Initialize all reports
766 */
767
768 void usbhid_init_reports(struct hid_device *hid)
769 {
770 struct hid_report *report;
771 struct usbhid_device *usbhid = hid->driver_data;
772 int err, ret;
773
774 list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
775 usbhid_submit_report(hid, report, USB_DIR_IN);
776
777 list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
778 usbhid_submit_report(hid, report, USB_DIR_IN);
779
780 err = 0;
781 ret = usbhid_wait_io(hid);
782 while (ret) {
783 err |= ret;
784 if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
785 usb_kill_urb(usbhid->urbctrl);
786 if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
787 usb_kill_urb(usbhid->urbout);
788 ret = usbhid_wait_io(hid);
789 }
790
791 if (err)
792 hid_warn(hid, "timeout initializing reports\n");
793 }
794
795 /*
796 * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
797 */
798 static int hid_find_field_early(struct hid_device *hid, unsigned int page,
799 unsigned int hid_code, struct hid_field **pfield)
800 {
801 struct hid_report *report;
802 struct hid_field *field;
803 struct hid_usage *usage;
804 int i, j;
805
806 list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
807 for (i = 0; i < report->maxfield; i++) {
808 field = report->field[i];
809 for (j = 0; j < field->maxusage; j++) {
810 usage = &field->usage[j];
811 if ((usage->hid & HID_USAGE_PAGE) == page &&
812 (usage->hid & 0xFFFF) == hid_code) {
813 *pfield = field;
814 return j;
815 }
816 }
817 }
818 }
819 return -1;
820 }
821
822 void usbhid_set_leds(struct hid_device *hid)
823 {
824 struct hid_field *field;
825 int offset;
826
827 if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
828 hid_set_field(field, offset, 0);
829 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
830 }
831 }
832 EXPORT_SYMBOL_GPL(usbhid_set_leds);
833
834 /*
835 * Traverse the supplied list of reports and find the longest
836 */
837 static void hid_find_max_report(struct hid_device *hid, unsigned int type,
838 unsigned int *max)
839 {
840 struct hid_report *report;
841 unsigned int size;
842
843 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
844 size = ((report->size - 1) >> 3) + 1 + hid->report_enum[type].numbered;
845 if (*max < size)
846 *max = size;
847 }
848 }
849
850 static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
851 {
852 struct usbhid_device *usbhid = hid->driver_data;
853
854 usbhid->inbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
855 &usbhid->inbuf_dma);
856 usbhid->outbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
857 &usbhid->outbuf_dma);
858 usbhid->cr = kmalloc(sizeof(*usbhid->cr), GFP_KERNEL);
859 usbhid->ctrlbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
860 &usbhid->ctrlbuf_dma);
861 if (!usbhid->inbuf || !usbhid->outbuf || !usbhid->cr ||
862 !usbhid->ctrlbuf)
863 return -1;
864
865 return 0;
866 }
867
868 static int usbhid_get_raw_report(struct hid_device *hid,
869 unsigned char report_number, __u8 *buf, size_t count,
870 unsigned char report_type)
871 {
872 struct usbhid_device *usbhid = hid->driver_data;
873 struct usb_device *dev = hid_to_usb_dev(hid);
874 struct usb_interface *intf = usbhid->intf;
875 struct usb_host_interface *interface = intf->cur_altsetting;
876 int skipped_report_id = 0;
877 int ret;
878
879 /* Byte 0 is the report number. Report data starts at byte 1.*/
880 buf[0] = report_number;
881 if (report_number == 0x0) {
882 /* Offset the return buffer by 1, so that the report ID
883 will remain in byte 0. */
884 buf++;
885 count--;
886 skipped_report_id = 1;
887 }
888 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
889 HID_REQ_GET_REPORT,
890 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
891 ((report_type + 1) << 8) | report_number,
892 interface->desc.bInterfaceNumber, buf, count,
893 USB_CTRL_SET_TIMEOUT);
894
895 /* count also the report id */
896 if (ret > 0 && skipped_report_id)
897 ret++;
898
899 return ret;
900 }
901
902 static int usbhid_output_raw_report(struct hid_device *hid, __u8 *buf, size_t count,
903 unsigned char report_type)
904 {
905 struct usbhid_device *usbhid = hid->driver_data;
906 struct usb_device *dev = hid_to_usb_dev(hid);
907 struct usb_interface *intf = usbhid->intf;
908 struct usb_host_interface *interface = intf->cur_altsetting;
909 int ret;
910
911 if (usbhid->urbout && report_type != HID_FEATURE_REPORT) {
912 int actual_length;
913 int skipped_report_id = 0;
914
915 if (buf[0] == 0x0) {
916 /* Don't send the Report ID */
917 buf++;
918 count--;
919 skipped_report_id = 1;
920 }
921 ret = usb_interrupt_msg(dev, usbhid->urbout->pipe,
922 buf, count, &actual_length,
923 USB_CTRL_SET_TIMEOUT);
924 /* return the number of bytes transferred */
925 if (ret == 0) {
926 ret = actual_length;
927 /* count also the report id */
928 if (skipped_report_id)
929 ret++;
930 }
931 } else {
932 int skipped_report_id = 0;
933 int report_id = buf[0];
934 if (buf[0] == 0x0) {
935 /* Don't send the Report ID */
936 buf++;
937 count--;
938 skipped_report_id = 1;
939 }
940 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
941 HID_REQ_SET_REPORT,
942 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
943 ((report_type + 1) << 8) | report_id,
944 interface->desc.bInterfaceNumber, buf, count,
945 USB_CTRL_SET_TIMEOUT);
946 /* count also the report id, if this was a numbered report. */
947 if (ret > 0 && skipped_report_id)
948 ret++;
949 }
950
951 return ret;
952 }
953
954 static void usbhid_restart_queues(struct usbhid_device *usbhid)
955 {
956 if (usbhid->urbout)
957 usbhid_restart_out_queue(usbhid);
958 usbhid_restart_ctrl_queue(usbhid);
959 }
960
961 static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
962 {
963 struct usbhid_device *usbhid = hid->driver_data;
964
965 usb_free_coherent(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
966 usb_free_coherent(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
967 kfree(usbhid->cr);
968 usb_free_coherent(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
969 }
970
971 static int usbhid_parse(struct hid_device *hid)
972 {
973 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
974 struct usb_host_interface *interface = intf->cur_altsetting;
975 struct usb_device *dev = interface_to_usbdev (intf);
976 struct hid_descriptor *hdesc;
977 u32 quirks = 0;
978 unsigned int rsize = 0;
979 char *rdesc;
980 int ret, n;
981
982 quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
983 le16_to_cpu(dev->descriptor.idProduct));
984
985 if (quirks & HID_QUIRK_IGNORE)
986 return -ENODEV;
987
988 /* Many keyboards and mice don't like to be polled for reports,
989 * so we will always set the HID_QUIRK_NOGET flag for them. */
990 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
991 if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
992 interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
993 quirks |= HID_QUIRK_NOGET;
994 }
995
996 if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
997 (!interface->desc.bNumEndpoints ||
998 usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
999 dbg_hid("class descriptor not present\n");
1000 return -ENODEV;
1001 }
1002
1003 hid->version = le16_to_cpu(hdesc->bcdHID);
1004 hid->country = hdesc->bCountryCode;
1005
1006 for (n = 0; n < hdesc->bNumDescriptors; n++)
1007 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
1008 rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
1009
1010 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
1011 dbg_hid("weird size of report descriptor (%u)\n", rsize);
1012 return -EINVAL;
1013 }
1014
1015 if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
1016 dbg_hid("couldn't allocate rdesc memory\n");
1017 return -ENOMEM;
1018 }
1019
1020 hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
1021
1022 ret = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber,
1023 HID_DT_REPORT, rdesc, rsize);
1024 if (ret < 0) {
1025 dbg_hid("reading report descriptor failed\n");
1026 kfree(rdesc);
1027 goto err;
1028 }
1029
1030 ret = hid_parse_report(hid, rdesc, rsize);
1031 kfree(rdesc);
1032 if (ret) {
1033 dbg_hid("parsing report descriptor failed\n");
1034 goto err;
1035 }
1036
1037 hid->quirks |= quirks;
1038
1039 return 0;
1040 err:
1041 return ret;
1042 }
1043
1044 static int usbhid_start(struct hid_device *hid)
1045 {
1046 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
1047 struct usb_host_interface *interface = intf->cur_altsetting;
1048 struct usb_device *dev = interface_to_usbdev(intf);
1049 struct usbhid_device *usbhid = hid->driver_data;
1050 unsigned int n, insize = 0;
1051 int ret;
1052
1053 clear_bit(HID_DISCONNECTED, &usbhid->iofl);
1054
1055 usbhid->bufsize = HID_MIN_BUFFER_SIZE;
1056 hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
1057 hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
1058 hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
1059
1060 if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
1061 usbhid->bufsize = HID_MAX_BUFFER_SIZE;
1062
1063 hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
1064
1065 if (insize > HID_MAX_BUFFER_SIZE)
1066 insize = HID_MAX_BUFFER_SIZE;
1067
1068 if (hid_alloc_buffers(dev, hid)) {
1069 ret = -ENOMEM;
1070 goto fail;
1071 }
1072
1073 for (n = 0; n < interface->desc.bNumEndpoints; n++) {
1074 struct usb_endpoint_descriptor *endpoint;
1075 int pipe;
1076 int interval;
1077
1078 endpoint = &interface->endpoint[n].desc;
1079 if (!usb_endpoint_xfer_int(endpoint))
1080 continue;
1081
1082 interval = endpoint->bInterval;
1083
1084 /* Some vendors give fullspeed interval on highspeed devides */
1085 if (hid->quirks & HID_QUIRK_FULLSPEED_INTERVAL &&
1086 dev->speed == USB_SPEED_HIGH) {
1087 interval = fls(endpoint->bInterval*8);
1088 printk(KERN_INFO "%s: Fixing fullspeed to highspeed interval: %d -> %d\n",
1089 hid->name, endpoint->bInterval, interval);
1090 }
1091
1092 /* Change the polling interval of mice. */
1093 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
1094 interval = hid_mousepoll_interval;
1095
1096 ret = -ENOMEM;
1097 if (usb_endpoint_dir_in(endpoint)) {
1098 if (usbhid->urbin)
1099 continue;
1100 if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
1101 goto fail;
1102 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
1103 usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
1104 hid_irq_in, hid, interval);
1105 usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
1106 usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1107 } else {
1108 if (usbhid->urbout)
1109 continue;
1110 if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
1111 goto fail;
1112 pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
1113 usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
1114 hid_irq_out, hid, interval);
1115 usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
1116 usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1117 }
1118 }
1119
1120 usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
1121 if (!usbhid->urbctrl) {
1122 ret = -ENOMEM;
1123 goto fail;
1124 }
1125
1126 usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
1127 usbhid->ctrlbuf, 1, hid_ctrl, hid);
1128 usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
1129 usbhid->urbctrl->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1130
1131 if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
1132 usbhid_init_reports(hid);
1133
1134 set_bit(HID_STARTED, &usbhid->iofl);
1135
1136 /* Some keyboards don't work until their LEDs have been set.
1137 * Since BIOSes do set the LEDs, it must be safe for any device
1138 * that supports the keyboard boot protocol.
1139 * In addition, enable remote wakeup by default for all keyboard
1140 * devices supporting the boot protocol.
1141 */
1142 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT &&
1143 interface->desc.bInterfaceProtocol ==
1144 USB_INTERFACE_PROTOCOL_KEYBOARD) {
1145 usbhid_set_leds(hid);
1146 device_set_wakeup_enable(&dev->dev, 1);
1147 }
1148 return 0;
1149
1150 fail:
1151 usb_free_urb(usbhid->urbin);
1152 usb_free_urb(usbhid->urbout);
1153 usb_free_urb(usbhid->urbctrl);
1154 usbhid->urbin = NULL;
1155 usbhid->urbout = NULL;
1156 usbhid->urbctrl = NULL;
1157 hid_free_buffers(dev, hid);
1158 return ret;
1159 }
1160
1161 static void usbhid_stop(struct hid_device *hid)
1162 {
1163 struct usbhid_device *usbhid = hid->driver_data;
1164
1165 if (WARN_ON(!usbhid))
1166 return;
1167
1168 clear_bit(HID_STARTED, &usbhid->iofl);
1169 spin_lock_irq(&usbhid->lock); /* Sync with error and led handlers */
1170 set_bit(HID_DISCONNECTED, &usbhid->iofl);
1171 spin_unlock_irq(&usbhid->lock);
1172 usb_kill_urb(usbhid->urbin);
1173 usb_kill_urb(usbhid->urbout);
1174 usb_kill_urb(usbhid->urbctrl);
1175
1176 hid_cancel_delayed_stuff(usbhid);
1177
1178 hid->claimed = 0;
1179
1180 usb_free_urb(usbhid->urbin);
1181 usb_free_urb(usbhid->urbctrl);
1182 usb_free_urb(usbhid->urbout);
1183 usbhid->urbin = NULL; /* don't mess up next start */
1184 usbhid->urbctrl = NULL;
1185 usbhid->urbout = NULL;
1186
1187 hid_free_buffers(hid_to_usb_dev(hid), hid);
1188 }
1189
1190 static int usbhid_power(struct hid_device *hid, int lvl)
1191 {
1192 int r = 0;
1193
1194 switch (lvl) {
1195 case PM_HINT_FULLON:
1196 r = usbhid_get_power(hid);
1197 break;
1198 case PM_HINT_NORMAL:
1199 usbhid_put_power(hid);
1200 break;
1201 }
1202 return r;
1203 }
1204
1205 static struct hid_ll_driver usb_hid_driver = {
1206 .parse = usbhid_parse,
1207 .start = usbhid_start,
1208 .stop = usbhid_stop,
1209 .open = usbhid_open,
1210 .close = usbhid_close,
1211 .power = usbhid_power,
1212 .hidinput_input_event = usb_hidinput_input_event,
1213 };
1214
1215 static int usbhid_probe(struct usb_interface *intf, const struct usb_device_id *id)
1216 {
1217 struct usb_host_interface *interface = intf->cur_altsetting;
1218 struct usb_device *dev = interface_to_usbdev(intf);
1219 struct usbhid_device *usbhid;
1220 struct hid_device *hid;
1221 unsigned int n, has_in = 0;
1222 size_t len;
1223 int ret;
1224
1225 dbg_hid("HID probe called for ifnum %d\n",
1226 intf->altsetting->desc.bInterfaceNumber);
1227
1228 for (n = 0; n < interface->desc.bNumEndpoints; n++)
1229 if (usb_endpoint_is_int_in(&interface->endpoint[n].desc))
1230 has_in++;
1231 if (!has_in) {
1232 hid_err(intf, "couldn't find an input interrupt endpoint\n");
1233 return -ENODEV;
1234 }
1235
1236 hid = hid_allocate_device();
1237 if (IS_ERR(hid))
1238 return PTR_ERR(hid);
1239
1240 usb_set_intfdata(intf, hid);
1241 hid->ll_driver = &usb_hid_driver;
1242 hid->hid_get_raw_report = usbhid_get_raw_report;
1243 hid->hid_output_raw_report = usbhid_output_raw_report;
1244 hid->ff_init = hid_pidff_init;
1245 #ifdef CONFIG_USB_HIDDEV
1246 hid->hiddev_connect = hiddev_connect;
1247 hid->hiddev_disconnect = hiddev_disconnect;
1248 hid->hiddev_hid_event = hiddev_hid_event;
1249 hid->hiddev_report_event = hiddev_report_event;
1250 #endif
1251 hid->dev.parent = &intf->dev;
1252 hid->bus = BUS_USB;
1253 hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
1254 hid->product = le16_to_cpu(dev->descriptor.idProduct);
1255 hid->name[0] = 0;
1256 hid->quirks = usbhid_lookup_quirk(hid->vendor, hid->product);
1257 if (intf->cur_altsetting->desc.bInterfaceProtocol ==
1258 USB_INTERFACE_PROTOCOL_MOUSE)
1259 hid->type = HID_TYPE_USBMOUSE;
1260 else if (intf->cur_altsetting->desc.bInterfaceProtocol == 0)
1261 hid->type = HID_TYPE_USBNONE;
1262
1263 if (dev->manufacturer)
1264 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
1265
1266 if (dev->product) {
1267 if (dev->manufacturer)
1268 strlcat(hid->name, " ", sizeof(hid->name));
1269 strlcat(hid->name, dev->product, sizeof(hid->name));
1270 }
1271
1272 if (!strlen(hid->name))
1273 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
1274 le16_to_cpu(dev->descriptor.idVendor),
1275 le16_to_cpu(dev->descriptor.idProduct));
1276
1277 usb_make_path(dev, hid->phys, sizeof(hid->phys));
1278 strlcat(hid->phys, "/input", sizeof(hid->phys));
1279 len = strlen(hid->phys);
1280 if (len < sizeof(hid->phys) - 1)
1281 snprintf(hid->phys + len, sizeof(hid->phys) - len,
1282 "%d", intf->altsetting[0].desc.bInterfaceNumber);
1283
1284 if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
1285 hid->uniq[0] = 0;
1286
1287 usbhid = kzalloc(sizeof(*usbhid), GFP_KERNEL);
1288 if (usbhid == NULL) {
1289 ret = -ENOMEM;
1290 goto err;
1291 }
1292
1293 hid->driver_data = usbhid;
1294 usbhid->hid = hid;
1295 usbhid->intf = intf;
1296 usbhid->ifnum = interface->desc.bInterfaceNumber;
1297
1298 init_waitqueue_head(&usbhid->wait);
1299 INIT_WORK(&usbhid->reset_work, hid_reset);
1300 setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
1301 spin_lock_init(&usbhid->lock);
1302
1303 INIT_WORK(&usbhid->led_work, hid_led);
1304
1305 ret = hid_add_device(hid);
1306 if (ret) {
1307 if (ret != -ENODEV)
1308 hid_err(intf, "can't add hid device: %d\n", ret);
1309 goto err_free;
1310 }
1311
1312 return 0;
1313 err_free:
1314 kfree(usbhid);
1315 err:
1316 hid_destroy_device(hid);
1317 return ret;
1318 }
1319
1320 static void usbhid_disconnect(struct usb_interface *intf)
1321 {
1322 struct hid_device *hid = usb_get_intfdata(intf);
1323 struct usbhid_device *usbhid;
1324
1325 if (WARN_ON(!hid))
1326 return;
1327
1328 usbhid = hid->driver_data;
1329 hid_destroy_device(hid);
1330 kfree(usbhid);
1331 }
1332
1333 static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid)
1334 {
1335 del_timer_sync(&usbhid->io_retry);
1336 cancel_work_sync(&usbhid->reset_work);
1337 cancel_work_sync(&usbhid->led_work);
1338 }
1339
1340 static void hid_cease_io(struct usbhid_device *usbhid)
1341 {
1342 del_timer_sync(&usbhid->io_retry);
1343 usb_kill_urb(usbhid->urbin);
1344 usb_kill_urb(usbhid->urbctrl);
1345 usb_kill_urb(usbhid->urbout);
1346 }
1347
1348 /* Treat USB reset pretty much the same as suspend/resume */
1349 static int hid_pre_reset(struct usb_interface *intf)
1350 {
1351 struct hid_device *hid = usb_get_intfdata(intf);
1352 struct usbhid_device *usbhid = hid->driver_data;
1353
1354 spin_lock_irq(&usbhid->lock);
1355 set_bit(HID_RESET_PENDING, &usbhid->iofl);
1356 spin_unlock_irq(&usbhid->lock);
1357 hid_cease_io(usbhid);
1358
1359 return 0;
1360 }
1361
1362 /* Same routine used for post_reset and reset_resume */
1363 static int hid_post_reset(struct usb_interface *intf)
1364 {
1365 struct usb_device *dev = interface_to_usbdev (intf);
1366 struct hid_device *hid = usb_get_intfdata(intf);
1367 struct usbhid_device *usbhid = hid->driver_data;
1368 struct usb_host_interface *interface = intf->cur_altsetting;
1369 int status;
1370 char *rdesc;
1371
1372 /* Fetch and examine the HID report descriptor. If this
1373 * has changed, then rebind. Since usbcore's check of the
1374 * configuration descriptors passed, we already know that
1375 * the size of the HID report descriptor has not changed.
1376 */
1377 rdesc = kmalloc(hid->rsize, GFP_KERNEL);
1378 if (!rdesc) {
1379 dbg_hid("couldn't allocate rdesc memory (post_reset)\n");
1380 return 1;
1381 }
1382 status = hid_get_class_descriptor(dev,
1383 interface->desc.bInterfaceNumber,
1384 HID_DT_REPORT, rdesc, hid->rsize);
1385 if (status < 0) {
1386 dbg_hid("reading report descriptor failed (post_reset)\n");
1387 kfree(rdesc);
1388 return 1;
1389 }
1390 status = memcmp(rdesc, hid->rdesc, hid->rsize);
1391 kfree(rdesc);
1392 if (status != 0) {
1393 dbg_hid("report descriptor changed\n");
1394 return 1;
1395 }
1396
1397 spin_lock_irq(&usbhid->lock);
1398 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
1399 spin_unlock_irq(&usbhid->lock);
1400 hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
1401 status = hid_start_in(hid);
1402 if (status < 0)
1403 hid_io_error(hid);
1404 usbhid_restart_queues(usbhid);
1405
1406 return 0;
1407 }
1408
1409 int usbhid_get_power(struct hid_device *hid)
1410 {
1411 struct usbhid_device *usbhid = hid->driver_data;
1412
1413 return usb_autopm_get_interface(usbhid->intf);
1414 }
1415
1416 void usbhid_put_power(struct hid_device *hid)
1417 {
1418 struct usbhid_device *usbhid = hid->driver_data;
1419
1420 usb_autopm_put_interface(usbhid->intf);
1421 }
1422
1423
1424 #ifdef CONFIG_PM
1425 static int hid_suspend(struct usb_interface *intf, pm_message_t message)
1426 {
1427 struct hid_device *hid = usb_get_intfdata(intf);
1428 struct usbhid_device *usbhid = hid->driver_data;
1429 int status;
1430
1431 if (PMSG_IS_AUTO(message)) {
1432 spin_lock_irq(&usbhid->lock); /* Sync with error handler */
1433 if (!test_bit(HID_RESET_PENDING, &usbhid->iofl)
1434 && !test_bit(HID_CLEAR_HALT, &usbhid->iofl)
1435 && !test_bit(HID_OUT_RUNNING, &usbhid->iofl)
1436 && !test_bit(HID_CTRL_RUNNING, &usbhid->iofl)
1437 && !test_bit(HID_KEYS_PRESSED, &usbhid->iofl)
1438 && (!usbhid->ledcount || ignoreled))
1439 {
1440 set_bit(HID_REPORTED_IDLE, &usbhid->iofl);
1441 spin_unlock_irq(&usbhid->lock);
1442 if (hid->driver && hid->driver->suspend) {
1443 status = hid->driver->suspend(hid, message);
1444 if (status < 0)
1445 return status;
1446 }
1447 } else {
1448 usbhid_mark_busy(usbhid);
1449 spin_unlock_irq(&usbhid->lock);
1450 return -EBUSY;
1451 }
1452
1453 } else {
1454 if (hid->driver && hid->driver->suspend) {
1455 status = hid->driver->suspend(hid, message);
1456 if (status < 0)
1457 return status;
1458 }
1459 spin_lock_irq(&usbhid->lock);
1460 set_bit(HID_REPORTED_IDLE, &usbhid->iofl);
1461 spin_unlock_irq(&usbhid->lock);
1462 if (usbhid_wait_io(hid) < 0)
1463 return -EIO;
1464 }
1465
1466 hid_cancel_delayed_stuff(usbhid);
1467 hid_cease_io(usbhid);
1468
1469 if (PMSG_IS_AUTO(message) && test_bit(HID_KEYS_PRESSED, &usbhid->iofl)) {
1470 /* lost race against keypresses */
1471 status = hid_start_in(hid);
1472 if (status < 0)
1473 hid_io_error(hid);
1474 usbhid_mark_busy(usbhid);
1475 return -EBUSY;
1476 }
1477 dev_dbg(&intf->dev, "suspend\n");
1478 return 0;
1479 }
1480
1481 static int hid_resume(struct usb_interface *intf)
1482 {
1483 struct hid_device *hid = usb_get_intfdata (intf);
1484 struct usbhid_device *usbhid = hid->driver_data;
1485 int status;
1486
1487 if (!test_bit(HID_STARTED, &usbhid->iofl))
1488 return 0;
1489
1490 clear_bit(HID_REPORTED_IDLE, &usbhid->iofl);
1491 usbhid_mark_busy(usbhid);
1492
1493 if (test_bit(HID_CLEAR_HALT, &usbhid->iofl) ||
1494 test_bit(HID_RESET_PENDING, &usbhid->iofl))
1495 schedule_work(&usbhid->reset_work);
1496 usbhid->retry_delay = 0;
1497 status = hid_start_in(hid);
1498 if (status < 0)
1499 hid_io_error(hid);
1500 usbhid_restart_queues(usbhid);
1501
1502 if (status >= 0 && hid->driver && hid->driver->resume) {
1503 int ret = hid->driver->resume(hid);
1504 if (ret < 0)
1505 status = ret;
1506 }
1507 dev_dbg(&intf->dev, "resume status %d\n", status);
1508 return 0;
1509 }
1510
1511 static int hid_reset_resume(struct usb_interface *intf)
1512 {
1513 struct hid_device *hid = usb_get_intfdata(intf);
1514 struct usbhid_device *usbhid = hid->driver_data;
1515 int status;
1516
1517 clear_bit(HID_REPORTED_IDLE, &usbhid->iofl);
1518 status = hid_post_reset(intf);
1519 if (status >= 0 && hid->driver && hid->driver->reset_resume) {
1520 int ret = hid->driver->reset_resume(hid);
1521 if (ret < 0)
1522 status = ret;
1523 }
1524 return status;
1525 }
1526
1527 #endif /* CONFIG_PM */
1528
1529 static const struct usb_device_id hid_usb_ids[] = {
1530 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1531 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
1532 { } /* Terminating entry */
1533 };
1534
1535 MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1536
1537 static struct usb_driver hid_driver = {
1538 .name = "usbhid",
1539 .probe = usbhid_probe,
1540 .disconnect = usbhid_disconnect,
1541 #ifdef CONFIG_PM
1542 .suspend = hid_suspend,
1543 .resume = hid_resume,
1544 .reset_resume = hid_reset_resume,
1545 #endif
1546 .pre_reset = hid_pre_reset,
1547 .post_reset = hid_post_reset,
1548 .id_table = hid_usb_ids,
1549 .supports_autosuspend = 1,
1550 };
1551
1552 struct usb_interface *usbhid_find_interface(int minor)
1553 {
1554 return usb_find_interface(&hid_driver, minor);
1555 }
1556
1557 static int __init hid_init(void)
1558 {
1559 int retval = -ENOMEM;
1560
1561 retval = usbhid_quirks_init(quirks_param);
1562 if (retval)
1563 goto usbhid_quirks_init_fail;
1564 retval = usb_register(&hid_driver);
1565 if (retval)
1566 goto usb_register_fail;
1567 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
1568
1569 return 0;
1570 usb_register_fail:
1571 usbhid_quirks_exit();
1572 usbhid_quirks_init_fail:
1573 return retval;
1574 }
1575
1576 static void __exit hid_exit(void)
1577 {
1578 usb_deregister(&hid_driver);
1579 usbhid_quirks_exit();
1580 }
1581
1582 module_init(hid_init);
1583 module_exit(hid_exit);
1584
1585 MODULE_AUTHOR("Andreas Gal");
1586 MODULE_AUTHOR("Vojtech Pavlik");
1587 MODULE_AUTHOR("Jiri Kosina");
1588 MODULE_DESCRIPTION(DRIVER_DESC);
1589 MODULE_LICENSE(DRIVER_LICENSE);