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