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