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