blob: 5bf91dbad59d4181bdf23fdeda7bd7327117933b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * USB HID support for Linux
3 *
4 * Copyright (c) 1999 Andreas Gal
Michael Haboustakbf0964d2005-09-05 00:12:01 -05005 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
Oliver Neukum0361a282008-12-17 15:38:03 +01007 * Copyright (c) 2007-2008 Oliver Neukum
Jiri Kosina7d39e842010-02-02 20:46:34 +01008 * Copyright (c) 2006-2010 Jiri Kosina
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/list.h>
23#include <linux/mm.h>
Jiri Slaby3d5afd32008-10-27 12:16:15 +010024#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/spinlock.h>
26#include <asm/unaligned.h>
27#include <asm/byteorder.h>
28#include <linux/input.h>
29#include <linux/wait.h>
Oliver Neukum0361a282008-12-17 15:38:03 +010030#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/usb.h>
33
Jiri Kosinadde58452006-12-08 18:40:44 +010034#include <linux/hid.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/hiddev.h>
Jiri Kosinac080d892007-01-25 11:43:31 +010036#include <linux/hid-debug.h>
Jiri Kosina86166b72007-05-14 09:57:40 +020037#include <linux/hidraw.h>
Jiri Kosina4916b3a2006-12-08 18:41:03 +010038#include "usbhid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/*
41 * Version Information
42 */
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define DRIVER_DESC "USB HID core driver"
45#define DRIVER_LICENSE "GPL"
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/*
48 * Module parameters.
49 */
50
51static unsigned int hid_mousepoll_interval;
52module_param_named(mousepoll, hid_mousepoll_interval, uint, 0644);
53MODULE_PARM_DESC(mousepoll, "Polling interval of mice");
54
Oliver Neukum0361a282008-12-17 15:38:03 +010055static unsigned int ignoreled;
56module_param_named(ignoreled, ignoreled, uint, 0644);
57MODULE_PARM_DESC(ignoreled, "Autosuspend with active leds");
58
Paul Walmsley876b9272007-04-19 14:56:12 +020059/* Quirks specified at module load time */
60static char *quirks_param[MAX_USBHID_BOOT_QUIRKS] = { [ 0 ... (MAX_USBHID_BOOT_QUIRKS - 1) ] = NULL };
61module_param_array_named(quirks, quirks_param, charp, NULL, 0444);
62MODULE_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");
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/*
Alan Sternaef4e262006-01-31 12:58:38 -050067 * Input submission and I/O error handler.
68 */
Oliver Neukum0361a282008-12-17 15:38:03 +010069static DEFINE_MUTEX(hid_open_mut);
Alan Sternaef4e262006-01-31 12:58:38 -050070
71static void hid_io_error(struct hid_device *hid);
Oliver Neukum0361a282008-12-17 15:38:03 +010072static int hid_submit_out(struct hid_device *hid);
73static int hid_submit_ctrl(struct hid_device *hid);
74static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid);
Alan Sternaef4e262006-01-31 12:58:38 -050075
76/* Start up the input URB */
77static int hid_start_in(struct hid_device *hid)
78{
79 unsigned long flags;
80 int rc = 0;
Jiri Kosina4916b3a2006-12-08 18:41:03 +010081 struct usbhid_device *usbhid = hid->driver_data;
Alan Sternaef4e262006-01-31 12:58:38 -050082
Oliver Neukum0361a282008-12-17 15:38:03 +010083 spin_lock_irqsave(&usbhid->lock, flags);
84 if (hid->open > 0 &&
Oliver Neukum69626f22008-03-31 16:27:30 +020085 !test_bit(HID_DISCONNECTED, &usbhid->iofl) &&
Oliver Neukum0361a282008-12-17 15:38:03 +010086 !test_bit(HID_REPORTED_IDLE, &usbhid->iofl) &&
Jiri Kosina4916b3a2006-12-08 18:41:03 +010087 !test_and_set_bit(HID_IN_RUNNING, &usbhid->iofl)) {
88 rc = usb_submit_urb(usbhid->urbin, GFP_ATOMIC);
Alan Sternaef4e262006-01-31 12:58:38 -050089 if (rc != 0)
Jiri Kosina4916b3a2006-12-08 18:41:03 +010090 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
Alan Sternaef4e262006-01-31 12:58:38 -050091 }
Oliver Neukum0361a282008-12-17 15:38:03 +010092 spin_unlock_irqrestore(&usbhid->lock, flags);
Alan Sternaef4e262006-01-31 12:58:38 -050093 return rc;
94}
95
96/* I/O retry timer routine */
97static void hid_retry_timeout(unsigned long _hid)
98{
99 struct hid_device *hid = (struct hid_device *) _hid;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100100 struct usbhid_device *usbhid = hid->driver_data;
Alan Sternaef4e262006-01-31 12:58:38 -0500101
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100102 dev_dbg(&usbhid->intf->dev, "retrying intr urb\n");
Alan Sternaef4e262006-01-31 12:58:38 -0500103 if (hid_start_in(hid))
104 hid_io_error(hid);
105}
106
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400107/* Workqueue routine to reset the device or clear a halt */
David Howellsc4028952006-11-22 14:57:56 +0000108static void hid_reset(struct work_struct *work)
Alan Sternaef4e262006-01-31 12:58:38 -0500109{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100110 struct usbhid_device *usbhid =
111 container_of(work, struct usbhid_device, reset_work);
112 struct hid_device *hid = usbhid->hid;
Alan Stern011b15d2008-11-04 11:29:27 -0500113 int rc = 0;
Alan Sternaef4e262006-01-31 12:58:38 -0500114
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100115 if (test_bit(HID_CLEAR_HALT, &usbhid->iofl)) {
116 dev_dbg(&usbhid->intf->dev, "clear halt\n");
Anssi Hannulabe820972007-01-19 19:28:17 +0200117 rc = usb_clear_halt(hid_to_usb_dev(hid), usbhid->urbin->pipe);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100118 clear_bit(HID_CLEAR_HALT, &usbhid->iofl);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400119 hid_start_in(hid);
Alan Sternaef4e262006-01-31 12:58:38 -0500120 }
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400121
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100122 else if (test_bit(HID_RESET_PENDING, &usbhid->iofl)) {
123 dev_dbg(&usbhid->intf->dev, "resetting device\n");
Alan Stern011b15d2008-11-04 11:29:27 -0500124 rc = usb_lock_device_for_reset(hid_to_usb_dev(hid), usbhid->intf);
125 if (rc == 0) {
Ming Lei742120c2008-06-18 22:00:29 +0800126 rc = usb_reset_device(hid_to_usb_dev(hid));
Alan Stern011b15d2008-11-04 11:29:27 -0500127 usb_unlock_device(hid_to_usb_dev(hid));
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400128 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100129 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400130 }
Alan Sternaef4e262006-01-31 12:58:38 -0500131
Alan Sterndf9a1f42006-06-01 13:55:28 -0400132 switch (rc) {
133 case 0:
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100134 if (!test_bit(HID_IN_RUNNING, &usbhid->iofl))
Alan Sternaef4e262006-01-31 12:58:38 -0500135 hid_io_error(hid);
Alan Sterndf9a1f42006-06-01 13:55:28 -0400136 break;
137 default:
Joe Perches4291ee32010-12-09 19:29:03 -0800138 hid_err(hid, "can't reset device, %s-%s/input%d, status %d\n",
139 hid_to_usb_dev(hid)->bus->bus_name,
140 hid_to_usb_dev(hid)->devpath,
141 usbhid->ifnum, rc);
Alan Sterndf9a1f42006-06-01 13:55:28 -0400142 /* FALLTHROUGH */
143 case -EHOSTUNREACH:
144 case -ENODEV:
145 case -EINTR:
146 break;
147 }
Alan Sternaef4e262006-01-31 12:58:38 -0500148}
149
150/* Main I/O error handler */
151static void hid_io_error(struct hid_device *hid)
152{
153 unsigned long flags;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100154 struct usbhid_device *usbhid = hid->driver_data;
Alan Sternaef4e262006-01-31 12:58:38 -0500155
Oliver Neukum0361a282008-12-17 15:38:03 +0100156 spin_lock_irqsave(&usbhid->lock, flags);
Alan Sternaef4e262006-01-31 12:58:38 -0500157
158 /* Stop when disconnected */
Oliver Neukum69626f22008-03-31 16:27:30 +0200159 if (test_bit(HID_DISCONNECTED, &usbhid->iofl))
Alan Sternaef4e262006-01-31 12:58:38 -0500160 goto done;
161
Alan Stern5e2a55f2007-03-20 19:03:31 +0100162 /* If it has been a while since the last error, we'll assume
163 * this a brand new error and reset the retry timeout. */
164 if (time_after(jiffies, usbhid->stop_retry + HZ/2))
165 usbhid->retry_delay = 0;
166
Alan Sternaef4e262006-01-31 12:58:38 -0500167 /* When an error occurs, retry at increasing intervals */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100168 if (usbhid->retry_delay == 0) {
169 usbhid->retry_delay = 13; /* Then 26, 52, 104, 104, ... */
170 usbhid->stop_retry = jiffies + msecs_to_jiffies(1000);
171 } else if (usbhid->retry_delay < 100)
172 usbhid->retry_delay *= 2;
Alan Sternaef4e262006-01-31 12:58:38 -0500173
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100174 if (time_after(jiffies, usbhid->stop_retry)) {
Alan Sternaef4e262006-01-31 12:58:38 -0500175
176 /* Retries failed, so do a port reset */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100177 if (!test_and_set_bit(HID_RESET_PENDING, &usbhid->iofl)) {
178 schedule_work(&usbhid->reset_work);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400179 goto done;
Alan Sternaef4e262006-01-31 12:58:38 -0500180 }
181 }
182
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100183 mod_timer(&usbhid->io_retry,
184 jiffies + msecs_to_jiffies(usbhid->retry_delay));
Alan Sternaef4e262006-01-31 12:58:38 -0500185done:
Oliver Neukum0361a282008-12-17 15:38:03 +0100186 spin_unlock_irqrestore(&usbhid->lock, flags);
187}
188
189static void usbhid_mark_busy(struct usbhid_device *usbhid)
190{
191 struct usb_interface *intf = usbhid->intf;
192
193 usb_mark_last_busy(interface_to_usbdev(intf));
194}
195
196static int usbhid_restart_out_queue(struct usbhid_device *usbhid)
197{
198 struct hid_device *hid = usb_get_intfdata(usbhid->intf);
199 int kicked;
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800200 int r;
Oliver Neukum0361a282008-12-17 15:38:03 +0100201
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);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800207
208 r = usb_autopm_get_interface_async(usbhid->intf);
209 if (r < 0)
210 return r;
211 /* Asynchronously flush queue. */
212 set_bit(HID_OUT_RUNNING, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +0100213 if (hid_submit_out(hid)) {
214 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800215 usb_autopm_put_interface_async(usbhid->intf);
Oliver Neukum0361a282008-12-17 15:38:03 +0100216 }
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800217 wake_up(&usbhid->wait);
Oliver Neukum0361a282008-12-17 15:38:03 +0100218 }
219 return kicked;
220}
221
222static int usbhid_restart_ctrl_queue(struct usbhid_device *usbhid)
223{
224 struct hid_device *hid = usb_get_intfdata(usbhid->intf);
225 int kicked;
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800226 int r;
Oliver Neukum0361a282008-12-17 15:38:03 +0100227
228 WARN_ON(hid == NULL);
229 if (!hid)
230 return 0;
231
232 if ((kicked = (usbhid->ctrlhead != usbhid->ctrltail))) {
233 dbg("Kicking head %d tail %d", usbhid->ctrlhead, usbhid->ctrltail);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800234
235 r = usb_autopm_get_interface_async(usbhid->intf);
236 if (r < 0)
237 return r;
238 /* Asynchronously flush queue. */
239 set_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +0100240 if (hid_submit_ctrl(hid)) {
241 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800242 usb_autopm_put_interface_async(usbhid->intf);
Oliver Neukum0361a282008-12-17 15:38:03 +0100243 }
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800244 wake_up(&usbhid->wait);
Oliver Neukum0361a282008-12-17 15:38:03 +0100245 }
246 return kicked;
Alan Sternaef4e262006-01-31 12:58:38 -0500247}
248
249/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 * Input interrupt completion handler.
251 */
252
David Howells7d12e782006-10-05 14:55:46 +0100253static void hid_irq_in(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100256 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 int status;
258
259 switch (urb->status) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200260 case 0: /* success */
Oliver Neukum0361a282008-12-17 15:38:03 +0100261 usbhid_mark_busy(usbhid);
Jiri Slaby880d29f2008-06-18 23:55:41 +0200262 usbhid->retry_delay = 0;
263 hid_input_report(urb->context, HID_INPUT_REPORT,
264 urb->transfer_buffer,
265 urb->actual_length, 1);
Oliver Neukum0361a282008-12-17 15:38:03 +0100266 /*
267 * autosuspend refused while keys are pressed
268 * because most keyboards don't wake up when
269 * a key is released
270 */
271 if (hid_check_keys_pressed(hid))
272 set_bit(HID_KEYS_PRESSED, &usbhid->iofl);
273 else
274 clear_bit(HID_KEYS_PRESSED, &usbhid->iofl);
Jiri Slaby880d29f2008-06-18 23:55:41 +0200275 break;
276 case -EPIPE: /* stall */
Oliver Neukum0361a282008-12-17 15:38:03 +0100277 usbhid_mark_busy(usbhid);
Jiri Slaby880d29f2008-06-18 23:55:41 +0200278 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
279 set_bit(HID_CLEAR_HALT, &usbhid->iofl);
280 schedule_work(&usbhid->reset_work);
281 return;
282 case -ECONNRESET: /* unlink */
283 case -ENOENT:
284 case -ESHUTDOWN: /* unplug */
285 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
286 return;
287 case -EILSEQ: /* protocol error or unplug */
288 case -EPROTO: /* protocol error or unplug */
289 case -ETIME: /* protocol error or unplug */
290 case -ETIMEDOUT: /* Should never happen, but... */
Oliver Neukum0361a282008-12-17 15:38:03 +0100291 usbhid_mark_busy(usbhid);
Jiri Slaby880d29f2008-06-18 23:55:41 +0200292 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
293 hid_io_error(hid);
294 return;
295 default: /* error */
Joe Perches4291ee32010-12-09 19:29:03 -0800296 hid_warn(urb->dev, "input irq status %d received\n",
297 urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800300 status = usb_submit_urb(urb, GFP_ATOMIC);
Alan Sternaef4e262006-01-31 12:58:38 -0500301 if (status) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100302 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
Alan Sternaef4e262006-01-31 12:58:38 -0500303 if (status != -EPERM) {
Joe Perches4291ee32010-12-09 19:29:03 -0800304 hid_err(hid, "can't resubmit intr, %s-%s/input%d, status %d\n",
305 hid_to_usb_dev(hid)->bus->bus_name,
306 hid_to_usb_dev(hid)->devpath,
307 usbhid->ifnum, status);
Alan Sternaef4e262006-01-31 12:58:38 -0500308 hid_io_error(hid);
309 }
310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313static int hid_submit_out(struct hid_device *hid)
314{
315 struct hid_report *report;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200316 char *raw_report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100317 struct usbhid_device *usbhid = hid->driver_data;
Oliver Neukum68229682010-12-22 15:33:40 +0100318 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200320 report = usbhid->out[usbhid->outtail].report;
321 raw_report = usbhid->out[usbhid->outtail].raw_report;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800323 usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) +
324 1 + (report->id > 0);
325 usbhid->urbout->dev = hid_to_usb_dev(hid);
326 memcpy(usbhid->outbuf, raw_report,
327 usbhid->urbout->transfer_buffer_length);
328 kfree(raw_report);
Oliver Neukum68229682010-12-22 15:33:40 +0100329
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800330 dbg_hid("submitting out urb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800332 r = usb_submit_urb(usbhid->urbout, GFP_ATOMIC);
333 if (r < 0) {
334 hid_err(hid, "usb_submit_urb(out) failed: %d\n", r);
335 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800337 usbhid->last_out = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return 0;
339}
340
341static int hid_submit_ctrl(struct hid_device *hid)
342{
343 struct hid_report *report;
344 unsigned char dir;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200345 char *raw_report;
Oliver Neukum68229682010-12-22 15:33:40 +0100346 int len, r;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100347 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100349 report = usbhid->ctrl[usbhid->ctrltail].report;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200350 raw_report = usbhid->ctrl[usbhid->ctrltail].raw_report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100351 dir = usbhid->ctrl[usbhid->ctrltail].dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800353 len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
354 if (dir == USB_DIR_OUT) {
355 usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
356 usbhid->urbctrl->transfer_buffer_length = len;
357 memcpy(usbhid->ctrlbuf, raw_report, len);
358 kfree(raw_report);
359 } else {
360 int maxpacket, padlen;
Oliver Neukum0361a282008-12-17 15:38:03 +0100361
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800362 usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0);
363 maxpacket = usb_maxpacket(hid_to_usb_dev(hid),
364 usbhid->urbctrl->pipe, 0);
365 if (maxpacket > 0) {
366 padlen = DIV_ROUND_UP(len, maxpacket);
367 padlen *= maxpacket;
368 if (padlen > usbhid->bufsize)
369 padlen = usbhid->bufsize;
370 } else
371 padlen = 0;
372 usbhid->urbctrl->transfer_buffer_length = padlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800374 usbhid->urbctrl->dev = hid_to_usb_dev(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800376 usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
377 usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT :
378 HID_REQ_GET_REPORT;
379 usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) |
380 report->id);
381 usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum);
382 usbhid->cr->wLength = cpu_to_le16(len);
383
384 dbg_hid("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u\n",
385 usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" :
386 "Get_Report",
387 usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength);
388
389 r = usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC);
390 if (r < 0) {
391 hid_err(hid, "usb_submit_urb(ctrl) failed: %d\n", r);
392 return r;
393 }
394 usbhid->last_ctrl = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return 0;
396}
397
398/*
399 * Output interrupt completion handler.
400 */
401
David Howells7d12e782006-10-05 14:55:46 +0100402static void hid_irq_out(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100405 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 unsigned long flags;
407 int unplug = 0;
408
409 switch (urb->status) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200410 case 0: /* success */
411 break;
412 case -ESHUTDOWN: /* unplug */
413 unplug = 1;
414 case -EILSEQ: /* protocol error or unplug */
415 case -EPROTO: /* protocol error or unplug */
416 case -ECONNRESET: /* unlink */
417 case -ENOENT:
418 break;
419 default: /* error */
Joe Perches4291ee32010-12-09 19:29:03 -0800420 hid_warn(urb->dev, "output irq status %d received\n",
421 urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423
Oliver Neukum0361a282008-12-17 15:38:03 +0100424 spin_lock_irqsave(&usbhid->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 if (unplug)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100427 usbhid->outtail = usbhid->outhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 else
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100429 usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800431 if (usbhid->outhead != usbhid->outtail && !hid_submit_out(hid)) {
432 /* Successfully submitted next urb in queue */
Oliver Neukum0361a282008-12-17 15:38:03 +0100433 spin_unlock_irqrestore(&usbhid->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return;
435 }
436
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100437 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +0100438 spin_unlock_irqrestore(&usbhid->lock, flags);
Oliver Neukum68229682010-12-22 15:33:40 +0100439 usb_autopm_put_interface_async(usbhid->intf);
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100440 wake_up(&usbhid->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
443/*
444 * Control pipe completion handler.
445 */
446
David Howells7d12e782006-10-05 14:55:46 +0100447static void hid_ctrl(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100450 struct usbhid_device *usbhid = hid->driver_data;
Oliver Neukum0361a282008-12-17 15:38:03 +0100451 int unplug = 0, status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Oliver Neukum0361a282008-12-17 15:38:03 +0100453 spin_lock(&usbhid->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Oliver Neukum0361a282008-12-17 15:38:03 +0100455 switch (status) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200456 case 0: /* success */
457 if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
458 hid_input_report(urb->context,
459 usbhid->ctrl[usbhid->ctrltail].report->type,
460 urb->transfer_buffer, urb->actual_length, 0);
461 break;
462 case -ESHUTDOWN: /* unplug */
463 unplug = 1;
464 case -EILSEQ: /* protocol error or unplug */
465 case -EPROTO: /* protocol error or unplug */
466 case -ECONNRESET: /* unlink */
467 case -ENOENT:
468 case -EPIPE: /* report not available */
469 break;
470 default: /* error */
Joe Perches4291ee32010-12-09 19:29:03 -0800471 hid_warn(urb->dev, "ctrl urb status %d received\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473
474 if (unplug)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100475 usbhid->ctrltail = usbhid->ctrlhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 else
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100477 usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800479 if (usbhid->ctrlhead != usbhid->ctrltail && !hid_submit_ctrl(hid)) {
480 /* Successfully submitted next urb in queue */
Oliver Neukum0361a282008-12-17 15:38:03 +0100481 spin_unlock(&usbhid->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return;
483 }
484
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100485 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +0100486 spin_unlock(&usbhid->lock);
Oliver Neukum68229682010-12-22 15:33:40 +0100487 usb_autopm_put_interface_async(usbhid->intf);
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100488 wake_up(&usbhid->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
H Hartley Sweeten52cfc612009-08-17 15:37:18 -0700491static void __usbhid_submit_report(struct hid_device *hid, struct hid_report *report,
492 unsigned char dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 int head;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100495 struct usbhid_device *usbhid = hid->driver_data;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200496 int len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
499 return;
500
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100501 if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100502 if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
Joe Perches4291ee32010-12-09 19:29:03 -0800503 hid_warn(hid, "output queue full\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return;
505 }
506
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200507 usbhid->out[usbhid->outhead].raw_report = kmalloc(len, GFP_ATOMIC);
508 if (!usbhid->out[usbhid->outhead].raw_report) {
Joe Perches4291ee32010-12-09 19:29:03 -0800509 hid_warn(hid, "output queueing failed\n");
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200510 return;
511 }
512 hid_output_report(report, usbhid->out[usbhid->outhead].raw_report);
513 usbhid->out[usbhid->outhead].report = report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100514 usbhid->outhead = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800516 /* Try to awake from autosuspend... */
517 if (usb_autopm_get_interface_async(usbhid->intf) < 0)
518 return;
519
520 /*
521 * But if still suspended, leave urb enqueued, don't submit.
522 * Submission will occur if/when resume() drains the queue.
523 */
524 if (test_bit(HID_REPORTED_IDLE, &usbhid->iofl))
525 return;
526
Oliver Neukum858155f2010-02-12 13:02:28 +0100527 if (!test_and_set_bit(HID_OUT_RUNNING, &usbhid->iofl)) {
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800528 if (hid_submit_out(hid)) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100529 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800530 usb_autopm_put_interface_async(usbhid->intf);
531 }
532 wake_up(&usbhid->wait);
Oliver Neukum858155f2010-02-12 13:02:28 +0100533 } else {
534 /*
535 * the queue is known to run
536 * but an earlier request may be stuck
537 * we may need to time out
538 * no race because this is called under
539 * spinlock
540 */
541 if (time_after(jiffies, usbhid->last_out + HZ * 5))
542 usb_unlink_urb(usbhid->urbout);
543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return;
545 }
546
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100547 if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
Joe Perches4291ee32010-12-09 19:29:03 -0800548 hid_warn(hid, "control queue full\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return;
550 }
551
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200552 if (dir == USB_DIR_OUT) {
553 usbhid->ctrl[usbhid->ctrlhead].raw_report = kmalloc(len, GFP_ATOMIC);
554 if (!usbhid->ctrl[usbhid->ctrlhead].raw_report) {
Joe Perches4291ee32010-12-09 19:29:03 -0800555 hid_warn(hid, "control queueing failed\n");
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200556 return;
557 }
558 hid_output_report(report, usbhid->ctrl[usbhid->ctrlhead].raw_report);
559 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100560 usbhid->ctrl[usbhid->ctrlhead].report = report;
561 usbhid->ctrl[usbhid->ctrlhead].dir = dir;
562 usbhid->ctrlhead = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800564 /* Try to awake from autosuspend... */
565 if (usb_autopm_get_interface_async(usbhid->intf) < 0)
566 return;
567
568 /*
569 * If already suspended, leave urb enqueued, but don't submit.
570 * Submission will occur if/when resume() drains the queue.
571 */
572 if (test_bit(HID_REPORTED_IDLE, &usbhid->iofl))
573 return;
574
Oliver Neukum858155f2010-02-12 13:02:28 +0100575 if (!test_and_set_bit(HID_CTRL_RUNNING, &usbhid->iofl)) {
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800576 if (hid_submit_ctrl(hid)) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100577 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Daniel Kurtzf0befcd2011-11-17 19:23:49 +0800578 usb_autopm_put_interface_async(usbhid->intf);
579 }
580 wake_up(&usbhid->wait);
Oliver Neukum858155f2010-02-12 13:02:28 +0100581 } else {
582 /*
583 * the queue is known to run
584 * but an earlier request may be stuck
585 * we may need to time out
586 * no race because this is called under
587 * spinlock
588 */
589 if (time_after(jiffies, usbhid->last_ctrl + HZ * 5))
590 usb_unlink_urb(usbhid->urbctrl);
591 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100592}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Oliver Neukum0361a282008-12-17 15:38:03 +0100594void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
595{
596 struct usbhid_device *usbhid = hid->driver_data;
597 unsigned long flags;
598
599 spin_lock_irqsave(&usbhid->lock, flags);
600 __usbhid_submit_report(hid, report, dir);
601 spin_unlock_irqrestore(&usbhid->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
Jiri Slaby606bd0a2008-07-04 23:06:45 +0200603EXPORT_SYMBOL_GPL(usbhid_submit_report);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Daniel Kurtz4371ea82011-11-17 19:23:50 +0800605/* Workqueue routine to send requests to change LEDs */
606static void hid_led(struct work_struct *work)
607{
608 struct usbhid_device *usbhid =
609 container_of(work, struct usbhid_device, led_work);
610 struct hid_device *hid = usbhid->hid;
611 struct hid_field *field;
612 unsigned long flags;
613
614 field = hidinput_get_led_field(hid);
615 if (!field) {
616 hid_warn(hid, "LED event field not found\n");
617 return;
618 }
619
620 spin_lock_irqsave(&usbhid->lock, flags);
621 if (!test_bit(HID_DISCONNECTED, &usbhid->iofl)) {
622 usbhid->ledcount = hidinput_count_leds(hid);
623 hid_dbg(usbhid->hid, "New ledcount = %u\n", usbhid->ledcount);
624 __usbhid_submit_report(hid, field->report, USB_DIR_OUT);
625 }
626 spin_unlock_irqrestore(&usbhid->lock, flags);
627}
628
Jiri Kosina229695e2006-12-08 18:40:53 +0100629static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
Jiri Kosinadde58452006-12-08 18:40:44 +0100630{
Dmitry Torokhove0712982007-05-09 10:17:31 +0200631 struct hid_device *hid = input_get_drvdata(dev);
Oliver Neukum0361a282008-12-17 15:38:03 +0100632 struct usbhid_device *usbhid = hid->driver_data;
Jiri Kosinadde58452006-12-08 18:40:44 +0100633 struct hid_field *field;
Oliver Neukum0361a282008-12-17 15:38:03 +0100634 unsigned long flags;
Jiri Kosinadde58452006-12-08 18:40:44 +0100635 int offset;
636
637 if (type == EV_FF)
638 return input_ff_event(dev, type, code, value);
639
640 if (type != EV_LED)
641 return -1;
642
643 if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
Joe Perches4291ee32010-12-09 19:29:03 -0800644 hid_warn(dev, "event field not found\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100645 return -1;
646 }
647
Daniel Kurtz4371ea82011-11-17 19:23:50 +0800648 spin_lock_irqsave(&usbhid->lock, flags);
Jiri Kosinadde58452006-12-08 18:40:44 +0100649 hid_set_field(field, offset, value);
Daniel Kurtz4371ea82011-11-17 19:23:50 +0800650 spin_unlock_irqrestore(&usbhid->lock, flags);
651
652 /*
653 * Defer performing requested LED action.
654 * This is more likely gather all LED changes into a single URB.
655 */
656 schedule_work(&usbhid->led_work);
Jiri Kosinadde58452006-12-08 18:40:44 +0100657
658 return 0;
659}
660
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100661int usbhid_wait_io(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100663 struct usbhid_device *usbhid = hid->driver_data;
664
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100665 if (!wait_event_timeout(usbhid->wait,
666 (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
667 !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 10*HZ)) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200669 dbg_hid("timeout waiting for ctrl or out queue to clear\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return -1;
671 }
672
673 return 0;
674}
Bruno Prémontb8c21cf2010-03-30 22:34:30 +0200675EXPORT_SYMBOL_GPL(usbhid_wait_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Vojtech Pavlik854561b2005-05-29 02:28:00 -0500677static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
678{
Vojtech Pavlik71387bd72005-05-29 02:28:14 -0500679 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
Vojtech Pavlik854561b2005-05-29 02:28:00 -0500680 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
681 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
682}
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
685 unsigned char type, void *buf, int size)
686{
687 int result, retries = 4;
688
Jiri Kosina43c7bf02007-01-26 12:58:24 +0100689 memset(buf, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 do {
692 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
693 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
694 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
695 retries--;
696 } while (result < size && retries);
697 return result;
698}
699
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100700int usbhid_open(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Oliver Neukum933e3182007-07-11 14:48:58 +0200702 struct usbhid_device *usbhid = hid->driver_data;
703 int res;
704
Oliver Neukum0361a282008-12-17 15:38:03 +0100705 mutex_lock(&hid_open_mut);
Oliver Neukum933e3182007-07-11 14:48:58 +0200706 if (!hid->open++) {
707 res = usb_autopm_get_interface(usbhid->intf);
Oliver Neukum68229682010-12-22 15:33:40 +0100708 /* the device must be awake to reliably request remote wakeup */
Oliver Neukum933e3182007-07-11 14:48:58 +0200709 if (res < 0) {
710 hid->open--;
Oliver Neukum0361a282008-12-17 15:38:03 +0100711 mutex_unlock(&hid_open_mut);
Oliver Neukum933e3182007-07-11 14:48:58 +0200712 return -EIO;
713 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100714 usbhid->intf->needs_remote_wakeup = 1;
715 if (hid_start_in(hid))
716 hid_io_error(hid);
717
718 usb_autopm_put_interface(usbhid->intf);
Oliver Neukum933e3182007-07-11 14:48:58 +0200719 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100720 mutex_unlock(&hid_open_mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return 0;
722}
723
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100724void usbhid_close(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100726 struct usbhid_device *usbhid = hid->driver_data;
727
Oliver Neukum0361a282008-12-17 15:38:03 +0100728 mutex_lock(&hid_open_mut);
729
730 /* protecting hid->open to make sure we don't restart
731 * data acquistion due to a resumption we no longer
732 * care about
733 */
734 spin_lock_irq(&usbhid->lock);
Oliver Neukum933e3182007-07-11 14:48:58 +0200735 if (!--hid->open) {
Oliver Neukum0361a282008-12-17 15:38:03 +0100736 spin_unlock_irq(&usbhid->lock);
Oliver Neukum89092dd2009-04-29 17:12:12 +0200737 hid_cancel_delayed_stuff(usbhid);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100738 usb_kill_urb(usbhid->urbin);
Oliver Neukum0361a282008-12-17 15:38:03 +0100739 usbhid->intf->needs_remote_wakeup = 0;
740 } else {
741 spin_unlock_irq(&usbhid->lock);
Oliver Neukum933e3182007-07-11 14:48:58 +0200742 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100743 mutex_unlock(&hid_open_mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
746/*
747 * Initialize all reports
748 */
749
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100750void usbhid_init_reports(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
752 struct hid_report *report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100753 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 int err, ret;
755
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500756 list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100757 usbhid_submit_report(hid, report, USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100760 usbhid_submit_report(hid, report, USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 err = 0;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100763 ret = usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 while (ret) {
765 err |= ret;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100766 if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
767 usb_kill_urb(usbhid->urbctrl);
768 if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
769 usb_kill_urb(usbhid->urbout);
770 ret = usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
772
773 if (err)
Joe Perches4291ee32010-12-09 19:29:03 -0800774 hid_warn(hid, "timeout initializing reports\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500777/*
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200778 * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
779 */
780static int hid_find_field_early(struct hid_device *hid, unsigned int page,
781 unsigned int hid_code, struct hid_field **pfield)
782{
783 struct hid_report *report;
784 struct hid_field *field;
785 struct hid_usage *usage;
786 int i, j;
787
788 list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
789 for (i = 0; i < report->maxfield; i++) {
790 field = report->field[i];
791 for (j = 0; j < field->maxusage; j++) {
792 usage = &field->usage[j];
793 if ((usage->hid & HID_USAGE_PAGE) == page &&
794 (usage->hid & 0xFFFF) == hid_code) {
795 *pfield = field;
796 return j;
797 }
798 }
799 }
800 }
801 return -1;
802}
803
Jiri Slaby6edfa8d2008-06-27 20:41:02 +0200804void usbhid_set_leds(struct hid_device *hid)
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200805{
806 struct hid_field *field;
807 int offset;
808
809 if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
810 hid_set_field(field, offset, 0);
811 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
812 }
813}
Jiri Slaby6edfa8d2008-06-27 20:41:02 +0200814EXPORT_SYMBOL_GPL(usbhid_set_leds);
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200815
816/*
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500817 * Traverse the supplied list of reports and find the longest
818 */
Jiri Slaby282bfd42008-03-28 17:06:41 +0100819static void hid_find_max_report(struct hid_device *hid, unsigned int type,
820 unsigned int *max)
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500821{
822 struct hid_report *report;
Jiri Slaby282bfd42008-03-28 17:06:41 +0100823 unsigned int size;
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500824
825 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
Jiri Kosinaefc7ce12008-10-17 15:01:15 +0200826 size = ((report->size - 1) >> 3) + 1 + hid->report_enum[type].numbered;
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500827 if (*max < size)
828 *max = size;
829 }
830}
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
833{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100834 struct usbhid_device *usbhid = hid->driver_data;
835
Daniel Mack997ea582010-04-12 13:17:25 +0200836 usbhid->inbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
Jiri Slaby898089d2008-11-24 16:20:06 +0100837 &usbhid->inbuf_dma);
Daniel Mack997ea582010-04-12 13:17:25 +0200838 usbhid->outbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
Jiri Slaby898089d2008-11-24 16:20:06 +0100839 &usbhid->outbuf_dma);
Alan Stern0ede76f2010-03-05 15:10:17 -0500840 usbhid->cr = kmalloc(sizeof(*usbhid->cr), GFP_KERNEL);
Daniel Mack997ea582010-04-12 13:17:25 +0200841 usbhid->ctrlbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL,
Jiri Slaby898089d2008-11-24 16:20:06 +0100842 &usbhid->ctrlbuf_dma);
843 if (!usbhid->inbuf || !usbhid->outbuf || !usbhid->cr ||
844 !usbhid->ctrlbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return -1;
846
847 return 0;
848}
849
Alan Ottb4dbde92011-01-18 03:04:39 -0500850static int usbhid_get_raw_report(struct hid_device *hid,
851 unsigned char report_number, __u8 *buf, size_t count,
852 unsigned char report_type)
853{
854 struct usbhid_device *usbhid = hid->driver_data;
855 struct usb_device *dev = hid_to_usb_dev(hid);
856 struct usb_interface *intf = usbhid->intf;
857 struct usb_host_interface *interface = intf->cur_altsetting;
858 int skipped_report_id = 0;
859 int ret;
860
861 /* Byte 0 is the report number. Report data starts at byte 1.*/
862 buf[0] = report_number;
863 if (report_number == 0x0) {
864 /* Offset the return buffer by 1, so that the report ID
865 will remain in byte 0. */
866 buf++;
867 count--;
868 skipped_report_id = 1;
869 }
870 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
871 HID_REQ_GET_REPORT,
872 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
873 ((report_type + 1) << 8) | report_number,
874 interface->desc.bInterfaceNumber, buf, count,
875 USB_CTRL_SET_TIMEOUT);
876
877 /* count also the report id */
878 if (ret > 0 && skipped_report_id)
879 ret++;
880
881 return ret;
882}
883
Jiri Kosinad4bfa032010-01-29 15:03:36 +0100884static int usbhid_output_raw_report(struct hid_device *hid, __u8 *buf, size_t count,
885 unsigned char report_type)
Jiri Kosinaefc493f2007-05-14 09:54:30 +0200886{
887 struct usbhid_device *usbhid = hid->driver_data;
888 struct usb_device *dev = hid_to_usb_dev(hid);
889 struct usb_interface *intf = usbhid->intf;
890 struct usb_host_interface *interface = intf->cur_altsetting;
891 int ret;
892
Alan Ottfe2c91e2010-09-22 13:19:42 +0200893 if (usbhid->urbout && report_type != HID_FEATURE_REPORT) {
Alan Otta8ab5d52010-05-16 18:07:09 -0400894 int actual_length;
895 int skipped_report_id = 0;
Alan Ott12e52722010-09-22 13:33:20 +0200896
Alan Otta8ab5d52010-05-16 18:07:09 -0400897 if (buf[0] == 0x0) {
898 /* Don't send the Report ID */
899 buf++;
900 count--;
901 skipped_report_id = 1;
902 }
903 ret = usb_interrupt_msg(dev, usbhid->urbout->pipe,
904 buf, count, &actual_length,
905 USB_CTRL_SET_TIMEOUT);
906 /* return the number of bytes transferred */
907 if (ret == 0) {
908 ret = actual_length;
909 /* count also the report id */
910 if (skipped_report_id)
911 ret++;
912 }
913 } else {
Alan Ott29129a92010-06-30 09:50:36 -0400914 int skipped_report_id = 0;
Alan Ottc29771c2010-08-17 00:44:04 -0400915 int report_id = buf[0];
Alan Ott29129a92010-06-30 09:50:36 -0400916 if (buf[0] == 0x0) {
917 /* Don't send the Report ID */
918 buf++;
919 count--;
920 skipped_report_id = 1;
921 }
Alan Otta8ab5d52010-05-16 18:07:09 -0400922 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
923 HID_REQ_SET_REPORT,
924 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
Alan Ottc29771c2010-08-17 00:44:04 -0400925 ((report_type + 1) << 8) | report_id,
Alan Ott29129a92010-06-30 09:50:36 -0400926 interface->desc.bInterfaceNumber, buf, count,
Alan Otta8ab5d52010-05-16 18:07:09 -0400927 USB_CTRL_SET_TIMEOUT);
Alan Ott29129a92010-06-30 09:50:36 -0400928 /* count also the report id, if this was a numbered report. */
929 if (ret > 0 && skipped_report_id)
Alan Otta8ab5d52010-05-16 18:07:09 -0400930 ret++;
931 }
Jiri Kosinaefc493f2007-05-14 09:54:30 +0200932
933 return ret;
934}
935
Oliver Neukum0361a282008-12-17 15:38:03 +0100936static void usbhid_restart_queues(struct usbhid_device *usbhid)
937{
938 if (usbhid->urbout)
939 usbhid_restart_out_queue(usbhid);
940 usbhid_restart_ctrl_queue(usbhid);
941}
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
944{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100945 struct usbhid_device *usbhid = hid->driver_data;
946
Daniel Mack997ea582010-04-12 13:17:25 +0200947 usb_free_coherent(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
948 usb_free_coherent(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
Alan Stern0ede76f2010-03-05 15:10:17 -0500949 kfree(usbhid->cr);
Daniel Mack997ea582010-04-12 13:17:25 +0200950 usb_free_coherent(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
Jiri Slabyc500c972008-05-16 11:49:16 +0200953static int usbhid_parse(struct hid_device *hid)
954{
955 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 struct usb_host_interface *interface = intf->cur_altsetting;
957 struct usb_device *dev = interface_to_usbdev (intf);
958 struct hid_descriptor *hdesc;
Paul Walmsley2eb5dc32007-04-19 13:27:04 +0200959 u32 quirks = 0;
Jiri Slabyc500c972008-05-16 11:49:16 +0200960 unsigned int rsize = 0;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500961 char *rdesc;
Jiri Slabyc500c972008-05-16 11:49:16 +0200962 int ret, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Paul Walmsley2eb5dc32007-04-19 13:27:04 +0200964 quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
965 le16_to_cpu(dev->descriptor.idProduct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Jiri Kosina6f4303f2009-01-29 00:15:51 +0100967 if (quirks & HID_QUIRK_IGNORE)
968 return -ENODEV;
969
Alan Stern0f28b552006-05-15 14:49:04 -0400970 /* Many keyboards and mice don't like to be polled for reports,
971 * so we will always set the HID_QUIRK_NOGET flag for them. */
972 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
973 if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
974 interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
975 quirks |= HID_QUIRK_NOGET;
976 }
977
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500978 if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
979 (!interface->desc.bNumEndpoints ||
980 usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200981 dbg_hid("class descriptor not present\n");
Jiri Slabyc500c972008-05-16 11:49:16 +0200982 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
984
Jiri Slabyc500c972008-05-16 11:49:16 +0200985 hid->version = le16_to_cpu(hdesc->bcdHID);
986 hid->country = hdesc->bCountryCode;
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 for (n = 0; n < hdesc->bNumDescriptors; n++)
989 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
990 rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
991
992 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200993 dbg_hid("weird size of report descriptor (%u)\n", rsize);
Jiri Slabyc500c972008-05-16 11:49:16 +0200994 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
996
997 if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200998 dbg_hid("couldn't allocate rdesc memory\n");
Jiri Slabyc500c972008-05-16 11:49:16 +0200999 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
1001
Vojtech Pavlik854561b2005-05-29 02:28:00 -05001002 hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
1003
Jiri Slabyc500c972008-05-16 11:49:16 +02001004 ret = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber,
1005 HID_DT_REPORT, rdesc, rsize);
1006 if (ret < 0) {
Jiri Kosina58037eb2007-05-30 15:07:13 +02001007 dbg_hid("reading report descriptor failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 kfree(rdesc);
Jiri Slabyc500c972008-05-16 11:49:16 +02001009 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 }
1011
Jiri Slabyc500c972008-05-16 11:49:16 +02001012 ret = hid_parse_report(hid, rdesc, rsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 kfree(rdesc);
Jiri Slabyc500c972008-05-16 11:49:16 +02001014 if (ret) {
1015 dbg_hid("parsing report descriptor failed\n");
1016 goto err;
1017 }
1018
Zoltan Karcagif5208992009-05-06 16:30:21 +02001019 hid->quirks |= quirks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Jiri Slabyc500c972008-05-16 11:49:16 +02001021 return 0;
1022err:
1023 return ret;
1024}
1025
1026static int usbhid_start(struct hid_device *hid)
1027{
1028 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
1029 struct usb_host_interface *interface = intf->cur_altsetting;
1030 struct usb_device *dev = interface_to_usbdev(intf);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001031 struct usbhid_device *usbhid = hid->driver_data;
Jiri Slabyc500c972008-05-16 11:49:16 +02001032 unsigned int n, insize = 0;
1033 int ret;
1034
Jiri Slabye3e14de2008-11-01 23:41:46 +01001035 clear_bit(HID_DISCONNECTED, &usbhid->iofl);
1036
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001037 usbhid->bufsize = HID_MIN_BUFFER_SIZE;
1038 hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
1039 hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
1040 hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
1041
1042 if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
1043 usbhid->bufsize = HID_MAX_BUFFER_SIZE;
Michael Haboustakbf0964d2005-09-05 00:12:01 -05001044
1045 hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
1046
1047 if (insize > HID_MAX_BUFFER_SIZE)
1048 insize = HID_MAX_BUFFER_SIZE;
1049
Jiri Slabyc500c972008-05-16 11:49:16 +02001050 if (hid_alloc_buffers(dev, hid)) {
1051 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 goto fail;
Pekka Sarnilaf345c372008-03-06 13:23:14 +01001053 }
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 for (n = 0; n < interface->desc.bNumEndpoints; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 struct usb_endpoint_descriptor *endpoint;
1057 int pipe;
1058 int interval;
1059
1060 endpoint = &interface->endpoint[n].desc;
Jiri Slaby581a2732008-11-24 16:20:08 +01001061 if (!usb_endpoint_xfer_int(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 continue;
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 interval = endpoint->bInterval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Pekka Sarnilaf345c372008-03-06 13:23:14 +01001066 /* Some vendors give fullspeed interval on highspeed devides */
Jiri Slabyc500c972008-05-16 11:49:16 +02001067 if (hid->quirks & HID_QUIRK_FULLSPEED_INTERVAL &&
Pekka Sarnilaf345c372008-03-06 13:23:14 +01001068 dev->speed == USB_SPEED_HIGH) {
1069 interval = fls(endpoint->bInterval*8);
1070 printk(KERN_INFO "%s: Fixing fullspeed to highspeed interval: %d -> %d\n",
1071 hid->name, endpoint->bInterval, interval);
1072 }
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 /* Change the polling interval of mice. */
1075 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
1076 interval = hid_mousepoll_interval;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -05001077
Jiri Slabyc500c972008-05-16 11:49:16 +02001078 ret = -ENOMEM;
Luiz Fernando N. Capitulino0f12aa02006-10-26 13:02:51 -03001079 if (usb_endpoint_dir_in(endpoint)) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001080 if (usbhid->urbin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 continue;
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001082 if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 goto fail;
1084 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001085 usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 hid_irq_in, hid, interval);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001087 usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
1088 usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 } else {
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001090 if (usbhid->urbout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 continue;
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001092 if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 goto fail;
1094 pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001095 usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 hid_irq_out, hid, interval);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001097 usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
1098 usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
1100 }
1101
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001102 usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
Jiri Slabyc500c972008-05-16 11:49:16 +02001103 if (!usbhid->urbctrl) {
1104 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 goto fail;
Jiri Slabyc500c972008-05-16 11:49:16 +02001106 }
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -05001107
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001108 usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
1109 usbhid->ctrlbuf, 1, hid_ctrl, hid);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001110 usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
Alan Stern0ede76f2010-03-05 15:10:17 -05001111 usbhid->urbctrl->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Jiri Slabyc500c972008-05-16 11:49:16 +02001112
Jiri Kosina5b915d92009-11-05 14:08:03 +01001113 if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
1114 usbhid_init_reports(hid);
Jiri Slaby93c10132008-06-27 00:04:24 +02001115
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001116 set_bit(HID_STARTED, &usbhid->iofl);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001117
Alan Stern08ef08e2008-10-30 23:58:51 +01001118 /* Some keyboards don't work until their LEDs have been set.
1119 * Since BIOSes do set the LEDs, it must be safe for any device
1120 * that supports the keyboard boot protocol.
Alan Stern3d615102010-04-02 13:21:58 -04001121 * In addition, enable remote wakeup by default for all keyboard
1122 * devices supporting the boot protocol.
Alan Stern08ef08e2008-10-30 23:58:51 +01001123 */
1124 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT &&
1125 interface->desc.bInterfaceProtocol ==
Alan Stern3d615102010-04-02 13:21:58 -04001126 USB_INTERFACE_PROTOCOL_KEYBOARD) {
Alan Stern08ef08e2008-10-30 23:58:51 +01001127 usbhid_set_leds(hid);
Alan Stern3d615102010-04-02 13:21:58 -04001128 device_set_wakeup_enable(&dev->dev, 1);
1129 }
Jiri Slabyc500c972008-05-16 11:49:16 +02001130 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132fail:
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001133 usb_free_urb(usbhid->urbin);
1134 usb_free_urb(usbhid->urbout);
1135 usb_free_urb(usbhid->urbctrl);
Jiri Slabye3e14de2008-11-01 23:41:46 +01001136 usbhid->urbin = NULL;
1137 usbhid->urbout = NULL;
1138 usbhid->urbctrl = NULL;
Jiri Kosina22f675f2007-08-01 12:32:27 +02001139 hid_free_buffers(dev, hid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001140 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142
Jiri Slabyc500c972008-05-16 11:49:16 +02001143static void usbhid_stop(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
Jiri Slabyc500c972008-05-16 11:49:16 +02001145 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Jiri Slabyc500c972008-05-16 11:49:16 +02001147 if (WARN_ON(!usbhid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 return;
1149
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001150 clear_bit(HID_STARTED, &usbhid->iofl);
Daniel Kurtz4371ea82011-11-17 19:23:50 +08001151 spin_lock_irq(&usbhid->lock); /* Sync with error and led handlers */
Oliver Neukum69626f22008-03-31 16:27:30 +02001152 set_bit(HID_DISCONNECTED, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +01001153 spin_unlock_irq(&usbhid->lock);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001154 usb_kill_urb(usbhid->urbin);
1155 usb_kill_urb(usbhid->urbout);
1156 usb_kill_urb(usbhid->urbctrl);
1157
Oliver Neukum0361a282008-12-17 15:38:03 +01001158 hid_cancel_delayed_stuff(usbhid);
Alan Sternaef4e262006-01-31 12:58:38 -05001159
Jiri Slabyc500c972008-05-16 11:49:16 +02001160 hid->claimed = 0;
1161
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001162 usb_free_urb(usbhid->urbin);
1163 usb_free_urb(usbhid->urbctrl);
1164 usb_free_urb(usbhid->urbout);
Jiri Slabye3e14de2008-11-01 23:41:46 +01001165 usbhid->urbin = NULL; /* don't mess up next start */
1166 usbhid->urbctrl = NULL;
1167 usbhid->urbout = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Anssi Hannulabe820972007-01-19 19:28:17 +02001169 hid_free_buffers(hid_to_usb_dev(hid), hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171
Oliver Neukum0361a282008-12-17 15:38:03 +01001172static int usbhid_power(struct hid_device *hid, int lvl)
1173{
1174 int r = 0;
1175
1176 switch (lvl) {
1177 case PM_HINT_FULLON:
1178 r = usbhid_get_power(hid);
1179 break;
1180 case PM_HINT_NORMAL:
1181 usbhid_put_power(hid);
1182 break;
1183 }
1184 return r;
1185}
1186
Jiri Slabyc500c972008-05-16 11:49:16 +02001187static struct hid_ll_driver usb_hid_driver = {
1188 .parse = usbhid_parse,
1189 .start = usbhid_start,
1190 .stop = usbhid_stop,
1191 .open = usbhid_open,
1192 .close = usbhid_close,
Oliver Neukum0361a282008-12-17 15:38:03 +01001193 .power = usbhid_power,
Jiri Slabyc500c972008-05-16 11:49:16 +02001194 .hidinput_input_event = usb_hidinput_input_event,
1195};
1196
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001197static int usbhid_probe(struct usb_interface *intf, const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
Jiri Slaby131d3a7a2008-11-14 12:03:47 +01001199 struct usb_host_interface *interface = intf->cur_altsetting;
Jiri Slabyc500c972008-05-16 11:49:16 +02001200 struct usb_device *dev = interface_to_usbdev(intf);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001201 struct usbhid_device *usbhid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 struct hid_device *hid;
Jiri Slaby131d3a7a2008-11-14 12:03:47 +01001203 unsigned int n, has_in = 0;
Jiri Slabyc500c972008-05-16 11:49:16 +02001204 size_t len;
1205 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Jiri Kosina58037eb2007-05-30 15:07:13 +02001207 dbg_hid("HID probe called for ifnum %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 intf->altsetting->desc.bInterfaceNumber);
1209
Jiri Slaby131d3a7a2008-11-14 12:03:47 +01001210 for (n = 0; n < interface->desc.bNumEndpoints; n++)
1211 if (usb_endpoint_is_int_in(&interface->endpoint[n].desc))
1212 has_in++;
1213 if (!has_in) {
Joe Perches4291ee32010-12-09 19:29:03 -08001214 hid_err(intf, "couldn't find an input interrupt endpoint\n");
Jiri Slaby131d3a7a2008-11-14 12:03:47 +01001215 return -ENODEV;
1216 }
1217
Jiri Slabyc500c972008-05-16 11:49:16 +02001218 hid = hid_allocate_device();
1219 if (IS_ERR(hid))
1220 return PTR_ERR(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 usb_set_intfdata(intf, hid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001223 hid->ll_driver = &usb_hid_driver;
Alan Ottb4dbde92011-01-18 03:04:39 -05001224 hid->hid_get_raw_report = usbhid_get_raw_report;
Jiri Slabyc500c972008-05-16 11:49:16 +02001225 hid->hid_output_raw_report = usbhid_output_raw_report;
Jiri Slaby76483cf2008-09-18 12:23:33 +02001226 hid->ff_init = hid_pidff_init;
Jiri Slabyc500c972008-05-16 11:49:16 +02001227#ifdef CONFIG_USB_HIDDEV
Jiri Slaby93c10132008-06-27 00:04:24 +02001228 hid->hiddev_connect = hiddev_connect;
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001229 hid->hiddev_disconnect = hiddev_disconnect;
Jiri Slabyc500c972008-05-16 11:49:16 +02001230 hid->hiddev_hid_event = hiddev_hid_event;
1231 hid->hiddev_report_event = hiddev_report_event;
1232#endif
1233 hid->dev.parent = &intf->dev;
1234 hid->bus = BUS_USB;
1235 hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
1236 hid->product = le16_to_cpu(dev->descriptor.idProduct);
1237 hid->name[0] = 0;
Bastien Nocerab5e5a372010-04-16 17:19:50 +01001238 hid->quirks = usbhid_lookup_quirk(hid->vendor, hid->product);
Jiri Slabya73a6372008-10-22 14:45:11 +02001239 if (intf->cur_altsetting->desc.bInterfaceProtocol ==
1240 USB_INTERFACE_PROTOCOL_MOUSE)
1241 hid->type = HID_TYPE_USBMOUSE;
Tomoki Sekiyama6dc14182011-05-23 15:45:44 -07001242 else if (intf->cur_altsetting->desc.bInterfaceProtocol == 0)
1243 hid->type = HID_TYPE_USBNONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Jiri Slabyc500c972008-05-16 11:49:16 +02001245 if (dev->manufacturer)
1246 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
1247
1248 if (dev->product) {
1249 if (dev->manufacturer)
1250 strlcat(hid->name, " ", sizeof(hid->name));
1251 strlcat(hid->name, dev->product, sizeof(hid->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
1253
Jiri Slabyc500c972008-05-16 11:49:16 +02001254 if (!strlen(hid->name))
1255 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
1256 le16_to_cpu(dev->descriptor.idVendor),
1257 le16_to_cpu(dev->descriptor.idProduct));
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001258
Jiri Slabyc500c972008-05-16 11:49:16 +02001259 usb_make_path(dev, hid->phys, sizeof(hid->phys));
1260 strlcat(hid->phys, "/input", sizeof(hid->phys));
1261 len = strlen(hid->phys);
1262 if (len < sizeof(hid->phys) - 1)
1263 snprintf(hid->phys + len, sizeof(hid->phys) - len,
1264 "%d", intf->altsetting[0].desc.bInterfaceNumber);
Geoff Levand4a1a4d82007-01-15 20:11:52 -08001265
Jiri Slabyc500c972008-05-16 11:49:16 +02001266 if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
1267 hid->uniq[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001269 usbhid = kzalloc(sizeof(*usbhid), GFP_KERNEL);
1270 if (usbhid == NULL) {
1271 ret = -ENOMEM;
1272 goto err;
1273 }
1274
1275 hid->driver_data = usbhid;
1276 usbhid->hid = hid;
Jiri Kosina57ab12e2010-02-17 14:25:01 +01001277 usbhid->intf = intf;
1278 usbhid->ifnum = interface->desc.bInterfaceNumber;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001279
Alan Sternfde4e2f2010-05-07 10:41:10 -04001280 init_waitqueue_head(&usbhid->wait);
1281 INIT_WORK(&usbhid->reset_work, hid_reset);
Alan Sternfde4e2f2010-05-07 10:41:10 -04001282 setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
1283 spin_lock_init(&usbhid->lock);
1284
Daniel Kurtz4371ea82011-11-17 19:23:50 +08001285 INIT_WORK(&usbhid->led_work, hid_led);
1286
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001287 ret = hid_add_device(hid);
1288 if (ret) {
Jiri Slabyd458a9d2008-05-16 11:49:20 +02001289 if (ret != -ENODEV)
Joe Perches4291ee32010-12-09 19:29:03 -08001290 hid_err(intf, "can't add hid device: %d\n", ret);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001291 goto err_free;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001292 }
Jiri Slabyc500c972008-05-16 11:49:16 +02001293
1294 return 0;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001295err_free:
1296 kfree(usbhid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001297err:
1298 hid_destroy_device(hid);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001299 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300}
1301
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001302static void usbhid_disconnect(struct usb_interface *intf)
Jiri Slabyc500c972008-05-16 11:49:16 +02001303{
1304 struct hid_device *hid = usb_get_intfdata(intf);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001305 struct usbhid_device *usbhid;
Jiri Slabyc500c972008-05-16 11:49:16 +02001306
1307 if (WARN_ON(!hid))
1308 return;
1309
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001310 usbhid = hid->driver_data;
Jiri Slabyc500c972008-05-16 11:49:16 +02001311 hid_destroy_device(hid);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001312 kfree(usbhid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001313}
1314
Oliver Neukum0361a282008-12-17 15:38:03 +01001315static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
Oliver Neukum0361a282008-12-17 15:38:03 +01001317 del_timer_sync(&usbhid->io_retry);
Oliver Neukum0361a282008-12-17 15:38:03 +01001318 cancel_work_sync(&usbhid->reset_work);
Daniel Kurtz4371ea82011-11-17 19:23:50 +08001319 cancel_work_sync(&usbhid->led_work);
Oliver Neukum0361a282008-12-17 15:38:03 +01001320}
1321
1322static void hid_cease_io(struct usbhid_device *usbhid)
1323{
Oliver Neukumfad9fbe2011-10-13 18:21:58 +02001324 del_timer_sync(&usbhid->io_retry);
Oliver Neukum0361a282008-12-17 15:38:03 +01001325 usb_kill_urb(usbhid->urbin);
1326 usb_kill_urb(usbhid->urbctrl);
1327 usb_kill_urb(usbhid->urbout);
Oliver Neukum0361a282008-12-17 15:38:03 +01001328}
1329
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001330/* Treat USB reset pretty much the same as suspend/resume */
1331static int hid_pre_reset(struct usb_interface *intf)
1332{
1333 struct hid_device *hid = usb_get_intfdata(intf);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001334 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001336 spin_lock_irq(&usbhid->lock);
1337 set_bit(HID_RESET_PENDING, &usbhid->iofl);
1338 spin_unlock_irq(&usbhid->lock);
1339 hid_cease_io(usbhid);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001340
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001341 return 0;
1342}
1343
1344/* Same routine used for post_reset and reset_resume */
1345static int hid_post_reset(struct usb_interface *intf)
1346{
1347 struct usb_device *dev = interface_to_usbdev (intf);
1348 struct hid_device *hid = usb_get_intfdata(intf);
1349 struct usbhid_device *usbhid = hid->driver_data;
1350 int status;
Jiri Kosina70fa9f22009-06-04 15:48:38 +02001351
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001352 spin_lock_irq(&usbhid->lock);
1353 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
1354 spin_unlock_irq(&usbhid->lock);
1355 hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001356 status = hid_start_in(hid);
1357 if (status < 0)
1358 hid_io_error(hid);
1359 usbhid_restart_queues(usbhid);
1360
1361 return 0;
1362}
1363
1364int usbhid_get_power(struct hid_device *hid)
1365{
1366 struct usbhid_device *usbhid = hid->driver_data;
Jiri Kosina70fa9f22009-06-04 15:48:38 +02001367
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001368 return usb_autopm_get_interface(usbhid->intf);
1369}
1370
1371void usbhid_put_power(struct hid_device *hid)
1372{
1373 struct usbhid_device *usbhid = hid->driver_data;
Jiri Kosina70fa9f22009-06-04 15:48:38 +02001374
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001375 usb_autopm_put_interface(usbhid->intf);
1376}
1377
1378
Jiri Kosina0f6f1402009-01-19 09:17:18 +01001379#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380static int hid_suspend(struct usb_interface *intf, pm_message_t message)
1381{
Oliver Neukum0361a282008-12-17 15:38:03 +01001382 struct hid_device *hid = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 struct usbhid_device *usbhid = hid->driver_data;
Oliver Neukum0361a282008-12-17 15:38:03 +01001384 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Alan Stern5b1b0b82011-08-19 23:49:48 +02001386 if (PMSG_IS_AUTO(message)) {
Oliver Neukum0361a282008-12-17 15:38:03 +01001387 spin_lock_irq(&usbhid->lock); /* Sync with error handler */
1388 if (!test_bit(HID_RESET_PENDING, &usbhid->iofl)
1389 && !test_bit(HID_CLEAR_HALT, &usbhid->iofl)
1390 && !test_bit(HID_OUT_RUNNING, &usbhid->iofl)
1391 && !test_bit(HID_CTRL_RUNNING, &usbhid->iofl)
1392 && !test_bit(HID_KEYS_PRESSED, &usbhid->iofl)
1393 && (!usbhid->ledcount || ignoreled))
1394 {
1395 set_bit(HID_REPORTED_IDLE, &usbhid->iofl);
1396 spin_unlock_irq(&usbhid->lock);
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001397 if (hid->driver && hid->driver->suspend) {
1398 status = hid->driver->suspend(hid, message);
1399 if (status < 0)
1400 return status;
1401 }
Oliver Neukum0361a282008-12-17 15:38:03 +01001402 } else {
1403 usbhid_mark_busy(usbhid);
1404 spin_unlock_irq(&usbhid->lock);
1405 return -EBUSY;
1406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Oliver Neukum0361a282008-12-17 15:38:03 +01001408 } else {
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001409 if (hid->driver && hid->driver->suspend) {
1410 status = hid->driver->suspend(hid, message);
1411 if (status < 0)
1412 return status;
1413 }
Oliver Neukum0361a282008-12-17 15:38:03 +01001414 spin_lock_irq(&usbhid->lock);
1415 set_bit(HID_REPORTED_IDLE, &usbhid->iofl);
1416 spin_unlock_irq(&usbhid->lock);
1417 if (usbhid_wait_io(hid) < 0)
1418 return -EIO;
1419 }
1420
Oliver Neukum0361a282008-12-17 15:38:03 +01001421 hid_cancel_delayed_stuff(usbhid);
1422 hid_cease_io(usbhid);
1423
Alan Stern5b1b0b82011-08-19 23:49:48 +02001424 if (PMSG_IS_AUTO(message) && test_bit(HID_KEYS_PRESSED, &usbhid->iofl)) {
Oliver Neukum0361a282008-12-17 15:38:03 +01001425 /* lost race against keypresses */
1426 status = hid_start_in(hid);
1427 if (status < 0)
1428 hid_io_error(hid);
1429 usbhid_mark_busy(usbhid);
1430 return -EBUSY;
1431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 dev_dbg(&intf->dev, "suspend\n");
1433 return 0;
1434}
1435
1436static int hid_resume(struct usb_interface *intf)
1437{
1438 struct hid_device *hid = usb_get_intfdata (intf);
1439 struct usbhid_device *usbhid = hid->driver_data;
1440 int status;
1441
Jiri Slabyfde5be32008-11-23 12:03:20 +01001442 if (!test_bit(HID_STARTED, &usbhid->iofl))
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001443 return 0;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001444
Oliver Neukum0361a282008-12-17 15:38:03 +01001445 clear_bit(HID_REPORTED_IDLE, &usbhid->iofl);
1446 usbhid_mark_busy(usbhid);
1447
1448 if (test_bit(HID_CLEAR_HALT, &usbhid->iofl) ||
1449 test_bit(HID_RESET_PENDING, &usbhid->iofl))
1450 schedule_work(&usbhid->reset_work);
Alan Sterndf9a1f42006-06-01 13:55:28 -04001451 usbhid->retry_delay = 0;
1452 status = hid_start_in(hid);
Oliver Neukum0361a282008-12-17 15:38:03 +01001453 if (status < 0)
1454 hid_io_error(hid);
1455 usbhid_restart_queues(usbhid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001457 if (status >= 0 && hid->driver && hid->driver->resume) {
1458 int ret = hid->driver->resume(hid);
1459 if (ret < 0)
1460 status = ret;
1461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 dev_dbg(&intf->dev, "resume status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return 0;
1464}
1465
Oliver Neukum378a0ed2009-02-18 11:46:45 +01001466static int hid_reset_resume(struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
Oliver Neukum378a0ed2009-02-18 11:46:45 +01001468 struct hid_device *hid = usb_get_intfdata(intf);
1469 struct usbhid_device *usbhid = hid->driver_data;
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001470 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Oliver Neukum378a0ed2009-02-18 11:46:45 +01001472 clear_bit(HID_REPORTED_IDLE, &usbhid->iofl);
Bruno Prémont6a740aa2010-04-25 21:40:03 +02001473 status = hid_post_reset(intf);
1474 if (status >= 0 && hid->driver && hid->driver->reset_resume) {
1475 int ret = hid->driver->reset_resume(hid);
1476 if (ret < 0)
1477 status = ret;
1478 }
1479 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480}
1481
Jiri Kosinaae2f0072009-02-20 12:47:08 +01001482#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Márton Némethd67dec52010-01-10 17:59:22 +01001484static const struct usb_device_id hid_usb_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1486 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
1487 { } /* Terminating entry */
1488};
1489
1490MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1491
1492static struct usb_driver hid_driver = {
1493 .name = "usbhid",
Jiri Kosinac4c259b2009-09-15 16:27:45 +02001494 .probe = usbhid_probe,
1495 .disconnect = usbhid_disconnect,
Jiri Kosina0f6f1402009-01-19 09:17:18 +01001496#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 .suspend = hid_suspend,
1498 .resume = hid_resume,
Oliver Neukum378a0ed2009-02-18 11:46:45 +01001499 .reset_resume = hid_reset_resume,
Jiri Kosina0f6f1402009-01-19 09:17:18 +01001500#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 .pre_reset = hid_pre_reset,
1502 .post_reset = hid_post_reset,
1503 .id_table = hid_usb_ids,
Oliver Neukum933e3182007-07-11 14:48:58 +02001504 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505};
1506
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001507static const struct hid_device_id hid_usb_table[] = {
1508 { HID_USB_DEVICE(HID_ANY_ID, HID_ANY_ID) },
1509 { }
1510};
1511
Guillaume Chazarain8fe294c2010-09-12 21:32:35 +02001512struct usb_interface *usbhid_find_interface(int minor)
1513{
1514 return usb_find_interface(&hid_driver, minor);
1515}
1516
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001517static struct hid_driver hid_usb_driver = {
1518 .name = "generic-usb",
1519 .id_table = hid_usb_table,
1520};
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522static int __init hid_init(void)
1523{
Oliver Neukum0361a282008-12-17 15:38:03 +01001524 int retval = -ENOMEM;
1525
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001526 retval = hid_register_driver(&hid_usb_driver);
1527 if (retval)
1528 goto hid_register_fail;
Paul Walmsley876b9272007-04-19 14:56:12 +02001529 retval = usbhid_quirks_init(quirks_param);
1530 if (retval)
1531 goto usbhid_quirks_init_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 retval = usb_register(&hid_driver);
1533 if (retval)
1534 goto usb_register_fail;
Jiri Kosinaccabcd22009-10-02 18:31:36 +02001535 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537 return 0;
1538usb_register_fail:
Paul Walmsley876b9272007-04-19 14:56:12 +02001539 usbhid_quirks_exit();
1540usbhid_quirks_init_fail:
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001541 hid_unregister_driver(&hid_usb_driver);
1542hid_register_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 return retval;
1544}
1545
1546static void __exit hid_exit(void)
1547{
1548 usb_deregister(&hid_driver);
Paul Walmsley876b9272007-04-19 14:56:12 +02001549 usbhid_quirks_exit();
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001550 hid_unregister_driver(&hid_usb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552
1553module_init(hid_init);
1554module_exit(hid_exit);
1555
Jiri Kosina88adb722009-10-02 18:29:34 +02001556MODULE_AUTHOR("Andreas Gal");
1557MODULE_AUTHOR("Vojtech Pavlik");
1558MODULE_AUTHOR("Jiri Kosina");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559MODULE_DESCRIPTION(DRIVER_DESC);
1560MODULE_LICENSE(DRIVER_LICENSE);