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