blob: eed05a3017e52881ceabf4a2a590a52c2703de69 [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
Jiri Kosinaefc7ce12008-10-17 15:01:15 +02007 * Copyright (c) 2006-2008 Jiri Kosina
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
10/*
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
14 * any later version.
15 */
16
17#include <linux/module.h>
18#include <linux/slab.h>
19#include <linux/init.h>
20#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/list.h>
22#include <linux/mm.h>
Jiri Slaby3d5afd32008-10-27 12:16:15 +010023#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/smp_lock.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
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/usb.h>
32
Jiri Kosinadde58452006-12-08 18:40:44 +010033#include <linux/hid.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/hiddev.h>
Jiri Kosinac080d892007-01-25 11:43:31 +010035#include <linux/hid-debug.h>
Jiri Kosina86166b72007-05-14 09:57:40 +020036#include <linux/hidraw.h>
Jiri Kosina4916b3a2006-12-08 18:41:03 +010037#include "usbhid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39/*
40 * Version Information
41 */
42
Michael Haboustakbf0964d2005-09-05 00:12:01 -050043#define DRIVER_VERSION "v2.6"
Jiri Kosinaf142b3a42007-04-16 11:29:28 +020044#define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik, Jiri Kosina"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define DRIVER_DESC "USB HID core driver"
46#define DRIVER_LICENSE "GPL"
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/*
49 * Module parameters.
50 */
51
52static unsigned int hid_mousepoll_interval;
53module_param_named(mousepoll, hid_mousepoll_interval, uint, 0644);
54MODULE_PARM_DESC(mousepoll, "Polling interval of mice");
55
Paul Walmsley876b9272007-04-19 14:56:12 +020056/* Quirks specified at module load time */
57static char *quirks_param[MAX_USBHID_BOOT_QUIRKS] = { [ 0 ... (MAX_USBHID_BOOT_QUIRKS - 1) ] = NULL };
58module_param_array_named(quirks, quirks_param, charp, NULL, 0444);
59MODULE_PARM_DESC(quirks, "Add/modify USB HID quirks by specifying "
60 " quirks=vendorID:productID:quirks"
61 " where vendorID, productID, and quirks are all in"
62 " 0x-prefixed hex");
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/*
Alan Sternaef4e262006-01-31 12:58:38 -050064 * Input submission and I/O error handler.
65 */
66
67static void hid_io_error(struct hid_device *hid);
68
69/* Start up the input URB */
70static int hid_start_in(struct hid_device *hid)
71{
72 unsigned long flags;
73 int rc = 0;
Jiri Kosina4916b3a2006-12-08 18:41:03 +010074 struct usbhid_device *usbhid = hid->driver_data;
Alan Sternaef4e262006-01-31 12:58:38 -050075
Jiri Kosina4916b3a2006-12-08 18:41:03 +010076 spin_lock_irqsave(&usbhid->inlock, flags);
77 if (hid->open > 0 && !test_bit(HID_SUSPENDED, &usbhid->iofl) &&
Oliver Neukum69626f22008-03-31 16:27:30 +020078 !test_bit(HID_DISCONNECTED, &usbhid->iofl) &&
Jiri Kosina4916b3a2006-12-08 18:41:03 +010079 !test_and_set_bit(HID_IN_RUNNING, &usbhid->iofl)) {
80 rc = usb_submit_urb(usbhid->urbin, GFP_ATOMIC);
Alan Sternaef4e262006-01-31 12:58:38 -050081 if (rc != 0)
Jiri Kosina4916b3a2006-12-08 18:41:03 +010082 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
Alan Sternaef4e262006-01-31 12:58:38 -050083 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +010084 spin_unlock_irqrestore(&usbhid->inlock, flags);
Alan Sternaef4e262006-01-31 12:58:38 -050085 return rc;
86}
87
88/* I/O retry timer routine */
89static void hid_retry_timeout(unsigned long _hid)
90{
91 struct hid_device *hid = (struct hid_device *) _hid;
Jiri Kosina4916b3a2006-12-08 18:41:03 +010092 struct usbhid_device *usbhid = hid->driver_data;
Alan Sternaef4e262006-01-31 12:58:38 -050093
Jiri Kosina4916b3a2006-12-08 18:41:03 +010094 dev_dbg(&usbhid->intf->dev, "retrying intr urb\n");
Alan Sternaef4e262006-01-31 12:58:38 -050095 if (hid_start_in(hid))
96 hid_io_error(hid);
97}
98
Alan Stern6d8fc4d2006-10-18 12:35:24 -040099/* Workqueue routine to reset the device or clear a halt */
David Howellsc4028952006-11-22 14:57:56 +0000100static void hid_reset(struct work_struct *work)
Alan Sternaef4e262006-01-31 12:58:38 -0500101{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100102 struct usbhid_device *usbhid =
103 container_of(work, struct usbhid_device, reset_work);
104 struct hid_device *hid = usbhid->hid;
Alan Stern011b15d2008-11-04 11:29:27 -0500105 int rc = 0;
Alan Sternaef4e262006-01-31 12:58:38 -0500106
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100107 if (test_bit(HID_CLEAR_HALT, &usbhid->iofl)) {
108 dev_dbg(&usbhid->intf->dev, "clear halt\n");
Anssi Hannulabe820972007-01-19 19:28:17 +0200109 rc = usb_clear_halt(hid_to_usb_dev(hid), usbhid->urbin->pipe);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100110 clear_bit(HID_CLEAR_HALT, &usbhid->iofl);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400111 hid_start_in(hid);
Alan Sternaef4e262006-01-31 12:58:38 -0500112 }
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400113
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100114 else if (test_bit(HID_RESET_PENDING, &usbhid->iofl)) {
115 dev_dbg(&usbhid->intf->dev, "resetting device\n");
Alan Stern011b15d2008-11-04 11:29:27 -0500116 rc = usb_lock_device_for_reset(hid_to_usb_dev(hid), usbhid->intf);
117 if (rc == 0) {
Ming Lei742120c2008-06-18 22:00:29 +0800118 rc = usb_reset_device(hid_to_usb_dev(hid));
Alan Stern011b15d2008-11-04 11:29:27 -0500119 usb_unlock_device(hid_to_usb_dev(hid));
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400120 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100121 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400122 }
Alan Sternaef4e262006-01-31 12:58:38 -0500123
Alan Sterndf9a1f42006-06-01 13:55:28 -0400124 switch (rc) {
125 case 0:
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100126 if (!test_bit(HID_IN_RUNNING, &usbhid->iofl))
Alan Sternaef4e262006-01-31 12:58:38 -0500127 hid_io_error(hid);
Alan Sterndf9a1f42006-06-01 13:55:28 -0400128 break;
129 default:
Jiri Kosina58037eb2007-05-30 15:07:13 +0200130 err_hid("can't reset device, %s-%s/input%d, status %d",
Anssi Hannulabe820972007-01-19 19:28:17 +0200131 hid_to_usb_dev(hid)->bus->bus_name,
132 hid_to_usb_dev(hid)->devpath,
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100133 usbhid->ifnum, rc);
Alan Sterndf9a1f42006-06-01 13:55:28 -0400134 /* FALLTHROUGH */
135 case -EHOSTUNREACH:
136 case -ENODEV:
137 case -EINTR:
138 break;
139 }
Alan Sternaef4e262006-01-31 12:58:38 -0500140}
141
142/* Main I/O error handler */
143static void hid_io_error(struct hid_device *hid)
144{
145 unsigned long flags;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100146 struct usbhid_device *usbhid = hid->driver_data;
Alan Sternaef4e262006-01-31 12:58:38 -0500147
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100148 spin_lock_irqsave(&usbhid->inlock, flags);
Alan Sternaef4e262006-01-31 12:58:38 -0500149
150 /* Stop when disconnected */
Oliver Neukum69626f22008-03-31 16:27:30 +0200151 if (test_bit(HID_DISCONNECTED, &usbhid->iofl))
Alan Sternaef4e262006-01-31 12:58:38 -0500152 goto done;
153
Alan Stern5e2a55f2007-03-20 19:03:31 +0100154 /* If it has been a while since the last error, we'll assume
155 * this a brand new error and reset the retry timeout. */
156 if (time_after(jiffies, usbhid->stop_retry + HZ/2))
157 usbhid->retry_delay = 0;
158
Alan Sternaef4e262006-01-31 12:58:38 -0500159 /* When an error occurs, retry at increasing intervals */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100160 if (usbhid->retry_delay == 0) {
161 usbhid->retry_delay = 13; /* Then 26, 52, 104, 104, ... */
162 usbhid->stop_retry = jiffies + msecs_to_jiffies(1000);
163 } else if (usbhid->retry_delay < 100)
164 usbhid->retry_delay *= 2;
Alan Sternaef4e262006-01-31 12:58:38 -0500165
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100166 if (time_after(jiffies, usbhid->stop_retry)) {
Alan Sternaef4e262006-01-31 12:58:38 -0500167
168 /* Retries failed, so do a port reset */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100169 if (!test_and_set_bit(HID_RESET_PENDING, &usbhid->iofl)) {
170 schedule_work(&usbhid->reset_work);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400171 goto done;
Alan Sternaef4e262006-01-31 12:58:38 -0500172 }
173 }
174
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100175 mod_timer(&usbhid->io_retry,
176 jiffies + msecs_to_jiffies(usbhid->retry_delay));
Alan Sternaef4e262006-01-31 12:58:38 -0500177done:
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100178 spin_unlock_irqrestore(&usbhid->inlock, flags);
Alan Sternaef4e262006-01-31 12:58:38 -0500179}
180
181/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 * Input interrupt completion handler.
183 */
184
David Howells7d12e782006-10-05 14:55:46 +0100185static void hid_irq_in(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100188 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 int status;
190
191 switch (urb->status) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200192 case 0: /* success */
193 usbhid->retry_delay = 0;
194 hid_input_report(urb->context, HID_INPUT_REPORT,
195 urb->transfer_buffer,
196 urb->actual_length, 1);
197 break;
198 case -EPIPE: /* stall */
199 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
200 set_bit(HID_CLEAR_HALT, &usbhid->iofl);
201 schedule_work(&usbhid->reset_work);
202 return;
203 case -ECONNRESET: /* unlink */
204 case -ENOENT:
205 case -ESHUTDOWN: /* unplug */
206 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
207 return;
208 case -EILSEQ: /* protocol error or unplug */
209 case -EPROTO: /* protocol error or unplug */
210 case -ETIME: /* protocol error or unplug */
211 case -ETIMEDOUT: /* Should never happen, but... */
212 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
213 hid_io_error(hid);
214 return;
215 default: /* error */
From: Greg Kroah-Hartman7d89fe12008-10-12 00:25:51 +0200216 dev_warn(&urb->dev->dev, "input irq status %d "
217 "received\n", urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
219
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800220 status = usb_submit_urb(urb, GFP_ATOMIC);
Alan Sternaef4e262006-01-31 12:58:38 -0500221 if (status) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100222 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
Alan Sternaef4e262006-01-31 12:58:38 -0500223 if (status != -EPERM) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200224 err_hid("can't resubmit intr, %s-%s/input%d, status %d",
Anssi Hannulabe820972007-01-19 19:28:17 +0200225 hid_to_usb_dev(hid)->bus->bus_name,
226 hid_to_usb_dev(hid)->devpath,
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100227 usbhid->ifnum, status);
Alan Sternaef4e262006-01-31 12:58:38 -0500228 hid_io_error(hid);
229 }
230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233static int hid_submit_out(struct hid_device *hid)
234{
235 struct hid_report *report;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200236 char *raw_report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100237 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200239 report = usbhid->out[usbhid->outtail].report;
240 raw_report = usbhid->out[usbhid->outtail].raw_report;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100242 usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0);
Anssi Hannulabe820972007-01-19 19:28:17 +0200243 usbhid->urbout->dev = hid_to_usb_dev(hid);
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200244 memcpy(usbhid->outbuf, raw_report, usbhid->urbout->transfer_buffer_length);
245 kfree(raw_report);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Jiri Kosina58037eb2007-05-30 15:07:13 +0200247 dbg_hid("submitting out urb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100249 if (usb_submit_urb(usbhid->urbout, GFP_ATOMIC)) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200250 err_hid("usb_submit_urb(out) failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return -1;
252 }
253
254 return 0;
255}
256
257static int hid_submit_ctrl(struct hid_device *hid)
258{
259 struct hid_report *report;
260 unsigned char dir;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200261 char *raw_report;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 int len;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100263 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100265 report = usbhid->ctrl[usbhid->ctrltail].report;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200266 raw_report = usbhid->ctrl[usbhid->ctrltail].raw_report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100267 dir = usbhid->ctrl[usbhid->ctrltail].dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
270 if (dir == USB_DIR_OUT) {
Anssi Hannulabe820972007-01-19 19:28:17 +0200271 usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100272 usbhid->urbctrl->transfer_buffer_length = len;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200273 memcpy(usbhid->ctrlbuf, raw_report, len);
274 kfree(raw_report);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 } else {
276 int maxpacket, padlen;
277
Anssi Hannulabe820972007-01-19 19:28:17 +0200278 usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0);
279 maxpacket = usb_maxpacket(hid_to_usb_dev(hid), usbhid->urbctrl->pipe, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (maxpacket > 0) {
Julia Lawall92c4a1b2008-02-15 13:53:11 +0100281 padlen = DIV_ROUND_UP(len, maxpacket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 padlen *= maxpacket;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100283 if (padlen > usbhid->bufsize)
284 padlen = usbhid->bufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 } else
286 padlen = 0;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100287 usbhid->urbctrl->transfer_buffer_length = padlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
Anssi Hannulabe820972007-01-19 19:28:17 +0200289 usbhid->urbctrl->dev = hid_to_usb_dev(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100291 usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
292 usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT : HID_REQ_GET_REPORT;
293 usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) | report->id);
294 usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum);
295 usbhid->cr->wLength = cpu_to_le16(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Jiri Kosina58037eb2007-05-30 15:07:13 +0200297 dbg_hid("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u\n",
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100298 usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" : "Get_Report",
299 usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100301 if (usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC)) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200302 err_hid("usb_submit_urb(ctrl) failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return -1;
304 }
305
306 return 0;
307}
308
309/*
310 * Output interrupt completion handler.
311 */
312
David Howells7d12e782006-10-05 14:55:46 +0100313static void hid_irq_out(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100316 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 unsigned long flags;
318 int unplug = 0;
319
320 switch (urb->status) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200321 case 0: /* success */
322 break;
323 case -ESHUTDOWN: /* unplug */
324 unplug = 1;
325 case -EILSEQ: /* protocol error or unplug */
326 case -EPROTO: /* protocol error or unplug */
327 case -ECONNRESET: /* unlink */
328 case -ENOENT:
329 break;
330 default: /* error */
From: Greg Kroah-Hartman7d89fe12008-10-12 00:25:51 +0200331 dev_warn(&urb->dev->dev, "output irq status %d "
332 "received\n", urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
334
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100335 spin_lock_irqsave(&usbhid->outlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 if (unplug)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100338 usbhid->outtail = usbhid->outhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 else
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100340 usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100342 if (usbhid->outhead != usbhid->outtail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (hid_submit_out(hid)) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100344 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100345 wake_up(&usbhid->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100347 spin_unlock_irqrestore(&usbhid->outlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return;
349 }
350
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100351 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
352 spin_unlock_irqrestore(&usbhid->outlock, flags);
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100353 wake_up(&usbhid->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
356/*
357 * Control pipe completion handler.
358 */
359
David Howells7d12e782006-10-05 14:55:46 +0100360static void hid_ctrl(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100363 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 unsigned long flags;
365 int unplug = 0;
366
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100367 spin_lock_irqsave(&usbhid->ctrllock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 switch (urb->status) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200370 case 0: /* success */
371 if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
372 hid_input_report(urb->context,
373 usbhid->ctrl[usbhid->ctrltail].report->type,
374 urb->transfer_buffer, urb->actual_length, 0);
375 break;
376 case -ESHUTDOWN: /* unplug */
377 unplug = 1;
378 case -EILSEQ: /* protocol error or unplug */
379 case -EPROTO: /* protocol error or unplug */
380 case -ECONNRESET: /* unlink */
381 case -ENOENT:
382 case -EPIPE: /* report not available */
383 break;
384 default: /* error */
From: Greg Kroah-Hartman7d89fe12008-10-12 00:25:51 +0200385 dev_warn(&urb->dev->dev, "ctrl urb status %d "
386 "received\n", urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388
389 if (unplug)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100390 usbhid->ctrltail = usbhid->ctrlhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 else
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100392 usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100394 if (usbhid->ctrlhead != usbhid->ctrltail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (hid_submit_ctrl(hid)) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100396 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100397 wake_up(&usbhid->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100399 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return;
401 }
402
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100403 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
404 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100405 wake_up(&usbhid->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100408void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
410 int head;
411 unsigned long flags;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100412 struct usbhid_device *usbhid = hid->driver_data;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200413 int len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
416 return;
417
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100418 if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100420 spin_lock_irqsave(&usbhid->outlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100422 if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
423 spin_unlock_irqrestore(&usbhid->outlock, flags);
From: Greg Kroah-Hartman7d89fe12008-10-12 00:25:51 +0200424 dev_warn(&hid->dev, "output queue full\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return;
426 }
427
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200428 usbhid->out[usbhid->outhead].raw_report = kmalloc(len, GFP_ATOMIC);
429 if (!usbhid->out[usbhid->outhead].raw_report) {
430 spin_unlock_irqrestore(&usbhid->outlock, flags);
Greg Kroah-Hartman46fcaec2008-10-15 11:30:07 -0700431 dev_warn(&hid->dev, "output queueing failed\n");
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200432 return;
433 }
434 hid_output_report(report, usbhid->out[usbhid->outhead].raw_report);
435 usbhid->out[usbhid->outhead].report = report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100436 usbhid->outhead = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100438 if (!test_and_set_bit(HID_OUT_RUNNING, &usbhid->iofl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (hid_submit_out(hid))
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100440 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100442 spin_unlock_irqrestore(&usbhid->outlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return;
444 }
445
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100446 spin_lock_irqsave(&usbhid->ctrllock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100448 if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
449 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
From: Greg Kroah-Hartman7d89fe12008-10-12 00:25:51 +0200450 dev_warn(&hid->dev, "control queue full\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return;
452 }
453
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200454 if (dir == USB_DIR_OUT) {
455 usbhid->ctrl[usbhid->ctrlhead].raw_report = kmalloc(len, GFP_ATOMIC);
456 if (!usbhid->ctrl[usbhid->ctrlhead].raw_report) {
457 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
Greg Kroah-Hartman46fcaec2008-10-15 11:30:07 -0700458 dev_warn(&hid->dev, "control queueing failed\n");
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200459 return;
460 }
461 hid_output_report(report, usbhid->ctrl[usbhid->ctrlhead].raw_report);
462 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100463 usbhid->ctrl[usbhid->ctrlhead].report = report;
464 usbhid->ctrl[usbhid->ctrlhead].dir = dir;
465 usbhid->ctrlhead = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100467 if (!test_and_set_bit(HID_CTRL_RUNNING, &usbhid->iofl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (hid_submit_ctrl(hid))
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100469 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100471 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
Jiri Slaby606bd0a2008-07-04 23:06:45 +0200473EXPORT_SYMBOL_GPL(usbhid_submit_report);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Jiri Kosina229695e2006-12-08 18:40:53 +0100475static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
Jiri Kosinadde58452006-12-08 18:40:44 +0100476{
Dmitry Torokhove0712982007-05-09 10:17:31 +0200477 struct hid_device *hid = input_get_drvdata(dev);
Jiri Kosinadde58452006-12-08 18:40:44 +0100478 struct hid_field *field;
479 int offset;
480
481 if (type == EV_FF)
482 return input_ff_event(dev, type, code, value);
483
484 if (type != EV_LED)
485 return -1;
486
487 if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
From: Greg Kroah-Hartman7d89fe12008-10-12 00:25:51 +0200488 dev_warn(&dev->dev, "event field not found\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100489 return -1;
490 }
491
492 hid_set_field(field, offset, value);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100493 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
Jiri Kosinadde58452006-12-08 18:40:44 +0100494
495 return 0;
496}
497
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100498int usbhid_wait_io(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100500 struct usbhid_device *usbhid = hid->driver_data;
501
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100502 if (!wait_event_timeout(usbhid->wait,
503 (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
504 !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 10*HZ)) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200506 dbg_hid("timeout waiting for ctrl or out queue to clear\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return -1;
508 }
509
510 return 0;
511}
512
Vojtech Pavlik854561b2005-05-29 02:28:00 -0500513static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
514{
Vojtech Pavlik71387bd2005-05-29 02:28:14 -0500515 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
Vojtech Pavlik854561b2005-05-29 02:28:00 -0500516 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
517 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
518}
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
521 unsigned char type, void *buf, int size)
522{
523 int result, retries = 4;
524
Jiri Kosina43c7bf02007-01-26 12:58:24 +0100525 memset(buf, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 do {
528 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
529 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
530 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
531 retries--;
532 } while (result < size && retries);
533 return result;
534}
535
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100536int usbhid_open(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
Oliver Neukum933e3182007-07-11 14:48:58 +0200538 struct usbhid_device *usbhid = hid->driver_data;
539 int res;
540
541 if (!hid->open++) {
542 res = usb_autopm_get_interface(usbhid->intf);
543 if (res < 0) {
544 hid->open--;
545 return -EIO;
546 }
547 }
Alan Sternaef4e262006-01-31 12:58:38 -0500548 if (hid_start_in(hid))
549 hid_io_error(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return 0;
551}
552
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100553void usbhid_close(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100555 struct usbhid_device *usbhid = hid->driver_data;
556
Oliver Neukum933e3182007-07-11 14:48:58 +0200557 if (!--hid->open) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100558 usb_kill_urb(usbhid->urbin);
Oliver Neukum933e3182007-07-11 14:48:58 +0200559 usb_autopm_put_interface(usbhid->intf);
560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
563/*
564 * Initialize all reports
565 */
566
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100567void usbhid_init_reports(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 struct hid_report *report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100570 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 int err, ret;
572
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500573 list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100574 usbhid_submit_report(hid, report, USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100577 usbhid_submit_report(hid, report, USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 err = 0;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100580 ret = usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 while (ret) {
582 err |= ret;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100583 if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
584 usb_kill_urb(usbhid->urbctrl);
585 if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
586 usb_kill_urb(usbhid->urbout);
587 ret = usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
589
590 if (err)
From: Greg Kroah-Hartman7d89fe12008-10-12 00:25:51 +0200591 dev_warn(&hid->dev, "timeout initializing reports\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500594/*
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200595 * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
596 */
597static int hid_find_field_early(struct hid_device *hid, unsigned int page,
598 unsigned int hid_code, struct hid_field **pfield)
599{
600 struct hid_report *report;
601 struct hid_field *field;
602 struct hid_usage *usage;
603 int i, j;
604
605 list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
606 for (i = 0; i < report->maxfield; i++) {
607 field = report->field[i];
608 for (j = 0; j < field->maxusage; j++) {
609 usage = &field->usage[j];
610 if ((usage->hid & HID_USAGE_PAGE) == page &&
611 (usage->hid & 0xFFFF) == hid_code) {
612 *pfield = field;
613 return j;
614 }
615 }
616 }
617 }
618 return -1;
619}
620
Jiri Slaby6edfa8d2008-06-27 20:41:02 +0200621void usbhid_set_leds(struct hid_device *hid)
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200622{
623 struct hid_field *field;
624 int offset;
625
626 if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
627 hid_set_field(field, offset, 0);
628 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
629 }
630}
Jiri Slaby6edfa8d2008-06-27 20:41:02 +0200631EXPORT_SYMBOL_GPL(usbhid_set_leds);
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200632
633/*
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500634 * Traverse the supplied list of reports and find the longest
635 */
Jiri Slaby282bfd42008-03-28 17:06:41 +0100636static void hid_find_max_report(struct hid_device *hid, unsigned int type,
637 unsigned int *max)
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500638{
639 struct hid_report *report;
Jiri Slaby282bfd42008-03-28 17:06:41 +0100640 unsigned int size;
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500641
642 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
Jiri Kosinaefc7ce12008-10-17 15:01:15 +0200643 size = ((report->size - 1) >> 3) + 1 + hid->report_enum[type].numbered;
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500644 if (*max < size)
645 *max = size;
646 }
647}
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
650{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100651 struct usbhid_device *usbhid = hid->driver_data;
652
Jiri Slaby898089d2008-11-24 16:20:06 +0100653 usbhid->inbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_KERNEL,
654 &usbhid->inbuf_dma);
655 usbhid->outbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_KERNEL,
656 &usbhid->outbuf_dma);
657 usbhid->cr = usb_buffer_alloc(dev, sizeof(*usbhid->cr), GFP_KERNEL,
658 &usbhid->cr_dma);
659 usbhid->ctrlbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_KERNEL,
660 &usbhid->ctrlbuf_dma);
661 if (!usbhid->inbuf || !usbhid->outbuf || !usbhid->cr ||
662 !usbhid->ctrlbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return -1;
664
665 return 0;
666}
667
Jiri Kosinaefc493f2007-05-14 09:54:30 +0200668static int usbhid_output_raw_report(struct hid_device *hid, __u8 *buf, size_t count)
669{
670 struct usbhid_device *usbhid = hid->driver_data;
671 struct usb_device *dev = hid_to_usb_dev(hid);
672 struct usb_interface *intf = usbhid->intf;
673 struct usb_host_interface *interface = intf->cur_altsetting;
674 int ret;
675
676 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
677 HID_REQ_SET_REPORT,
678 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
Al Viro01d7b362008-04-28 07:00:05 +0100679 ((HID_OUTPUT_REPORT + 1) << 8) | *buf,
Jiri Kosinaefc493f2007-05-14 09:54:30 +0200680 interface->desc.bInterfaceNumber, buf + 1, count - 1,
681 USB_CTRL_SET_TIMEOUT);
682
683 /* count also the report id */
684 if (ret > 0)
685 ret++;
686
687 return ret;
688}
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
691{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100692 struct usbhid_device *usbhid = hid->driver_data;
693
Dmitry Torokhov6675c5b2007-05-03 01:04:52 -0400694 usb_buffer_free(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
695 usb_buffer_free(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
696 usb_buffer_free(dev, sizeof(*(usbhid->cr)), usbhid->cr, usbhid->cr_dma);
697 usb_buffer_free(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
Jiri Slabyc500c972008-05-16 11:49:16 +0200700static int usbhid_parse(struct hid_device *hid)
701{
702 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 struct usb_host_interface *interface = intf->cur_altsetting;
704 struct usb_device *dev = interface_to_usbdev (intf);
705 struct hid_descriptor *hdesc;
Paul Walmsley2eb5dc32007-04-19 13:27:04 +0200706 u32 quirks = 0;
Jiri Slabyc500c972008-05-16 11:49:16 +0200707 unsigned int rsize = 0;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500708 char *rdesc;
Jiri Slabyc500c972008-05-16 11:49:16 +0200709 int ret, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Paul Walmsley2eb5dc32007-04-19 13:27:04 +0200711 quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
712 le16_to_cpu(dev->descriptor.idProduct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Jiri Kosina6f4303f2009-01-29 00:15:51 +0100714 if (quirks & HID_QUIRK_IGNORE)
715 return -ENODEV;
716
Alan Stern0f28b552006-05-15 14:49:04 -0400717 /* Many keyboards and mice don't like to be polled for reports,
718 * so we will always set the HID_QUIRK_NOGET flag for them. */
719 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
720 if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
721 interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
722 quirks |= HID_QUIRK_NOGET;
723 }
724
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500725 if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
726 (!interface->desc.bNumEndpoints ||
727 usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200728 dbg_hid("class descriptor not present\n");
Jiri Slabyc500c972008-05-16 11:49:16 +0200729 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731
Jiri Slabyc500c972008-05-16 11:49:16 +0200732 hid->version = le16_to_cpu(hdesc->bcdHID);
733 hid->country = hdesc->bCountryCode;
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 for (n = 0; n < hdesc->bNumDescriptors; n++)
736 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
737 rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
738
739 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200740 dbg_hid("weird size of report descriptor (%u)\n", rsize);
Jiri Slabyc500c972008-05-16 11:49:16 +0200741 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743
744 if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200745 dbg_hid("couldn't allocate rdesc memory\n");
Jiri Slabyc500c972008-05-16 11:49:16 +0200746 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748
Vojtech Pavlik854561b2005-05-29 02:28:00 -0500749 hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
750
Jiri Slabyc500c972008-05-16 11:49:16 +0200751 ret = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber,
752 HID_DT_REPORT, rdesc, rsize);
753 if (ret < 0) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200754 dbg_hid("reading report descriptor failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 kfree(rdesc);
Jiri Slabyc500c972008-05-16 11:49:16 +0200756 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758
Jiri Kosina58037eb2007-05-30 15:07:13 +0200759 dbg_hid("report descriptor (size %u, read %d) = ", rsize, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 for (n = 0; n < rsize; n++)
Jiri Kosina58037eb2007-05-30 15:07:13 +0200761 dbg_hid_line(" %02x", (unsigned char) rdesc[n]);
762 dbg_hid_line("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Jiri Slabyc500c972008-05-16 11:49:16 +0200764 ret = hid_parse_report(hid, rdesc, rsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 kfree(rdesc);
Jiri Slabyc500c972008-05-16 11:49:16 +0200766 if (ret) {
767 dbg_hid("parsing report descriptor failed\n");
768 goto err;
769 }
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 hid->quirks = quirks;
772
Jiri Slabyc500c972008-05-16 11:49:16 +0200773 return 0;
774err:
775 return ret;
776}
777
778static int usbhid_start(struct hid_device *hid)
779{
780 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
781 struct usb_host_interface *interface = intf->cur_altsetting;
782 struct usb_device *dev = interface_to_usbdev(intf);
Jiri Slaby3d5afd32008-10-27 12:16:15 +0100783 struct usbhid_device *usbhid = hid->driver_data;
Jiri Slabyc500c972008-05-16 11:49:16 +0200784 unsigned int n, insize = 0;
785 int ret;
786
Jiri Slabye3e14de2008-11-01 23:41:46 +0100787 clear_bit(HID_DISCONNECTED, &usbhid->iofl);
788
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100789 usbhid->bufsize = HID_MIN_BUFFER_SIZE;
790 hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
791 hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
792 hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
793
794 if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
795 usbhid->bufsize = HID_MAX_BUFFER_SIZE;
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500796
797 hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
798
799 if (insize > HID_MAX_BUFFER_SIZE)
800 insize = HID_MAX_BUFFER_SIZE;
801
Jiri Slabyc500c972008-05-16 11:49:16 +0200802 if (hid_alloc_buffers(dev, hid)) {
803 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 goto fail;
Pekka Sarnilaf345c372008-03-06 13:23:14 +0100805 }
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 for (n = 0; n < interface->desc.bNumEndpoints; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 struct usb_endpoint_descriptor *endpoint;
809 int pipe;
810 int interval;
811
812 endpoint = &interface->endpoint[n].desc;
Jiri Slaby581a2732008-11-24 16:20:08 +0100813 if (!usb_endpoint_xfer_int(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 continue;
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 interval = endpoint->bInterval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Pekka Sarnilaf345c372008-03-06 13:23:14 +0100818 /* Some vendors give fullspeed interval on highspeed devides */
Jiri Slabyc500c972008-05-16 11:49:16 +0200819 if (hid->quirks & HID_QUIRK_FULLSPEED_INTERVAL &&
Pekka Sarnilaf345c372008-03-06 13:23:14 +0100820 dev->speed == USB_SPEED_HIGH) {
821 interval = fls(endpoint->bInterval*8);
822 printk(KERN_INFO "%s: Fixing fullspeed to highspeed interval: %d -> %d\n",
823 hid->name, endpoint->bInterval, interval);
824 }
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 /* Change the polling interval of mice. */
827 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
828 interval = hid_mousepoll_interval;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500829
Jiri Slabyc500c972008-05-16 11:49:16 +0200830 ret = -ENOMEM;
Luiz Fernando N. Capitulino0f12aa02006-10-26 13:02:51 -0300831 if (usb_endpoint_dir_in(endpoint)) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100832 if (usbhid->urbin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 continue;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100834 if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 goto fail;
836 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100837 usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 hid_irq_in, hid, interval);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100839 usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
840 usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 } else {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100842 if (usbhid->urbout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 continue;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100844 if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 goto fail;
846 pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100847 usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 hid_irq_out, hid, interval);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100849 usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
850 usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
852 }
853
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100854 init_waitqueue_head(&usbhid->wait);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100855 INIT_WORK(&usbhid->reset_work, hid_reset);
856 setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
Alan Sternaef4e262006-01-31 12:58:38 -0500857
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100858 spin_lock_init(&usbhid->inlock);
859 spin_lock_init(&usbhid->outlock);
860 spin_lock_init(&usbhid->ctrllock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100862 usbhid->intf = intf;
863 usbhid->ifnum = interface->desc.bInterfaceNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100865 usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
Jiri Slabyc500c972008-05-16 11:49:16 +0200866 if (!usbhid->urbctrl) {
867 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 goto fail;
Jiri Slabyc500c972008-05-16 11:49:16 +0200869 }
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500870
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100871 usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
872 usbhid->ctrlbuf, 1, hid_ctrl, hid);
873 usbhid->urbctrl->setup_dma = usbhid->cr_dma;
874 usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
875 usbhid->urbctrl->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP);
Jiri Slabyc500c972008-05-16 11:49:16 +0200876
Jiri Slaby93c10132008-06-27 00:04:24 +0200877 usbhid_init_reports(hid);
878 hid_dump_device(hid);
879
Jiri Slaby3d5afd32008-10-27 12:16:15 +0100880 set_bit(HID_STARTED, &usbhid->iofl);
Jiri Slaby3d5afd32008-10-27 12:16:15 +0100881
Alan Stern08ef08e2008-10-30 23:58:51 +0100882 /* Some keyboards don't work until their LEDs have been set.
883 * Since BIOSes do set the LEDs, it must be safe for any device
884 * that supports the keyboard boot protocol.
885 */
886 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT &&
887 interface->desc.bInterfaceProtocol ==
888 USB_INTERFACE_PROTOCOL_KEYBOARD)
889 usbhid_set_leds(hid);
890
Jiri Slabyc500c972008-05-16 11:49:16 +0200891 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893fail:
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100894 usb_free_urb(usbhid->urbin);
895 usb_free_urb(usbhid->urbout);
896 usb_free_urb(usbhid->urbctrl);
Jiri Slabye3e14de2008-11-01 23:41:46 +0100897 usbhid->urbin = NULL;
898 usbhid->urbout = NULL;
899 usbhid->urbctrl = NULL;
Jiri Kosina22f675f2007-08-01 12:32:27 +0200900 hid_free_buffers(dev, hid);
Jiri Slabyc500c972008-05-16 11:49:16 +0200901 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
Jiri Slabyc500c972008-05-16 11:49:16 +0200904static void usbhid_stop(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Jiri Slabyc500c972008-05-16 11:49:16 +0200906 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Jiri Slabyc500c972008-05-16 11:49:16 +0200908 if (WARN_ON(!usbhid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return;
910
Jiri Slaby3d5afd32008-10-27 12:16:15 +0100911 clear_bit(HID_STARTED, &usbhid->iofl);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100912 spin_lock_irq(&usbhid->inlock); /* Sync with error handler */
Oliver Neukum69626f22008-03-31 16:27:30 +0200913 set_bit(HID_DISCONNECTED, &usbhid->iofl);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100914 spin_unlock_irq(&usbhid->inlock);
915 usb_kill_urb(usbhid->urbin);
916 usb_kill_urb(usbhid->urbout);
917 usb_kill_urb(usbhid->urbctrl);
918
919 del_timer_sync(&usbhid->io_retry);
Alan Stern2fa45a42007-05-30 11:11:12 -0400920 cancel_work_sync(&usbhid->reset_work);
Alan Sternaef4e262006-01-31 12:58:38 -0500921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (hid->claimed & HID_CLAIMED_INPUT)
923 hidinput_disconnect(hid);
924 if (hid->claimed & HID_CLAIMED_HIDDEV)
925 hiddev_disconnect(hid);
Jiri Kosina86166b72007-05-14 09:57:40 +0200926 if (hid->claimed & HID_CLAIMED_HIDRAW)
927 hidraw_disconnect(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Jiri Slabyc500c972008-05-16 11:49:16 +0200929 hid->claimed = 0;
930
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100931 usb_free_urb(usbhid->urbin);
932 usb_free_urb(usbhid->urbctrl);
933 usb_free_urb(usbhid->urbout);
Jiri Slabye3e14de2008-11-01 23:41:46 +0100934 usbhid->urbin = NULL; /* don't mess up next start */
935 usbhid->urbctrl = NULL;
936 usbhid->urbout = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Anssi Hannulabe820972007-01-19 19:28:17 +0200938 hid_free_buffers(hid_to_usb_dev(hid), hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
Jiri Slabyc500c972008-05-16 11:49:16 +0200941static struct hid_ll_driver usb_hid_driver = {
942 .parse = usbhid_parse,
943 .start = usbhid_start,
944 .stop = usbhid_stop,
945 .open = usbhid_open,
946 .close = usbhid_close,
947 .hidinput_input_event = usb_hidinput_input_event,
948};
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id)
951{
Jiri Slaby131d3a7a2008-11-14 12:03:47 +0100952 struct usb_host_interface *interface = intf->cur_altsetting;
Jiri Slabyc500c972008-05-16 11:49:16 +0200953 struct usb_device *dev = interface_to_usbdev(intf);
Jiri Slaby3d5afd32008-10-27 12:16:15 +0100954 struct usbhid_device *usbhid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 struct hid_device *hid;
Jiri Slaby131d3a7a2008-11-14 12:03:47 +0100956 unsigned int n, has_in = 0;
Jiri Slabyc500c972008-05-16 11:49:16 +0200957 size_t len;
958 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Jiri Kosina58037eb2007-05-30 15:07:13 +0200960 dbg_hid("HID probe called for ifnum %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 intf->altsetting->desc.bInterfaceNumber);
962
Jiri Slaby131d3a7a2008-11-14 12:03:47 +0100963 for (n = 0; n < interface->desc.bNumEndpoints; n++)
964 if (usb_endpoint_is_int_in(&interface->endpoint[n].desc))
965 has_in++;
966 if (!has_in) {
967 dev_err(&intf->dev, "couldn't find an input interrupt "
968 "endpoint\n");
969 return -ENODEV;
970 }
971
Jiri Slabyc500c972008-05-16 11:49:16 +0200972 hid = hid_allocate_device();
973 if (IS_ERR(hid))
974 return PTR_ERR(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 usb_set_intfdata(intf, hid);
Jiri Slabyc500c972008-05-16 11:49:16 +0200977 hid->ll_driver = &usb_hid_driver;
978 hid->hid_output_raw_report = usbhid_output_raw_report;
Jiri Slaby76483cf2008-09-18 12:23:33 +0200979 hid->ff_init = hid_pidff_init;
Jiri Slabyc500c972008-05-16 11:49:16 +0200980#ifdef CONFIG_USB_HIDDEV
Jiri Slaby93c10132008-06-27 00:04:24 +0200981 hid->hiddev_connect = hiddev_connect;
Jiri Slabyc500c972008-05-16 11:49:16 +0200982 hid->hiddev_hid_event = hiddev_hid_event;
983 hid->hiddev_report_event = hiddev_report_event;
984#endif
985 hid->dev.parent = &intf->dev;
986 hid->bus = BUS_USB;
987 hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
988 hid->product = le16_to_cpu(dev->descriptor.idProduct);
989 hid->name[0] = 0;
Jiri Slabya73a6372008-10-22 14:45:11 +0200990 if (intf->cur_altsetting->desc.bInterfaceProtocol ==
991 USB_INTERFACE_PROTOCOL_MOUSE)
992 hid->type = HID_TYPE_USBMOUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Jiri Slabyc500c972008-05-16 11:49:16 +0200994 if (dev->manufacturer)
995 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
996
997 if (dev->product) {
998 if (dev->manufacturer)
999 strlcat(hid->name, " ", sizeof(hid->name));
1000 strlcat(hid->name, dev->product, sizeof(hid->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
1002
Jiri Slabyc500c972008-05-16 11:49:16 +02001003 if (!strlen(hid->name))
1004 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
1005 le16_to_cpu(dev->descriptor.idVendor),
1006 le16_to_cpu(dev->descriptor.idProduct));
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001007
Jiri Slabyc500c972008-05-16 11:49:16 +02001008 usb_make_path(dev, hid->phys, sizeof(hid->phys));
1009 strlcat(hid->phys, "/input", sizeof(hid->phys));
1010 len = strlen(hid->phys);
1011 if (len < sizeof(hid->phys) - 1)
1012 snprintf(hid->phys + len, sizeof(hid->phys) - len,
1013 "%d", intf->altsetting[0].desc.bInterfaceNumber);
Geoff Levand4a1a4d82007-01-15 20:11:52 -08001014
Jiri Slabyc500c972008-05-16 11:49:16 +02001015 if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
1016 hid->uniq[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001018 usbhid = kzalloc(sizeof(*usbhid), GFP_KERNEL);
1019 if (usbhid == NULL) {
1020 ret = -ENOMEM;
1021 goto err;
1022 }
1023
1024 hid->driver_data = usbhid;
1025 usbhid->hid = hid;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001026
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001027 ret = hid_add_device(hid);
1028 if (ret) {
Jiri Slabyd458a9d2008-05-16 11:49:20 +02001029 if (ret != -ENODEV)
1030 dev_err(&intf->dev, "can't add hid device: %d\n", ret);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001031 goto err_free;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001032 }
Jiri Slabyc500c972008-05-16 11:49:16 +02001033
1034 return 0;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001035err_free:
1036 kfree(usbhid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001037err:
1038 hid_destroy_device(hid);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001039 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
Jiri Slabyc500c972008-05-16 11:49:16 +02001042static void hid_disconnect(struct usb_interface *intf)
1043{
1044 struct hid_device *hid = usb_get_intfdata(intf);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001045 struct usbhid_device *usbhid;
Jiri Slabyc500c972008-05-16 11:49:16 +02001046
1047 if (WARN_ON(!hid))
1048 return;
1049
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001050 usbhid = hid->driver_data;
Jiri Slabyc500c972008-05-16 11:49:16 +02001051 hid_destroy_device(hid);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001052 kfree(usbhid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001053}
1054
David Brownell27d72e82005-04-18 17:39:22 -07001055static int hid_suspend(struct usb_interface *intf, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
1057 struct hid_device *hid = usb_get_intfdata (intf);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001058 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Jiri Slabyfde5be32008-11-23 12:03:20 +01001060 if (!test_bit(HID_STARTED, &usbhid->iofl))
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001061 return 0;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001062
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001063 spin_lock_irq(&usbhid->inlock); /* Sync with error handler */
1064 set_bit(HID_SUSPENDED, &usbhid->iofl);
1065 spin_unlock_irq(&usbhid->inlock);
Jiri Slabyb1700602008-10-27 12:16:16 +01001066 del_timer_sync(&usbhid->io_retry);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001067 usb_kill_urb(usbhid->urbin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 dev_dbg(&intf->dev, "suspend\n");
1069 return 0;
1070}
1071
1072static int hid_resume(struct usb_interface *intf)
1073{
1074 struct hid_device *hid = usb_get_intfdata (intf);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001075 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 int status;
1077
Jiri Slabyfde5be32008-11-23 12:03:20 +01001078 if (!test_bit(HID_STARTED, &usbhid->iofl))
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001079 return 0;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001080
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001081 clear_bit(HID_SUSPENDED, &usbhid->iofl);
1082 usbhid->retry_delay = 0;
Alan Sternaef4e262006-01-31 12:58:38 -05001083 status = hid_start_in(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 dev_dbg(&intf->dev, "resume status %d\n", status);
1085 return status;
1086}
1087
Alan Sterndf9a1f42006-06-01 13:55:28 -04001088/* Treat USB reset pretty much the same as suspend/resume */
Alan Sternf07600c2007-05-30 15:38:16 -04001089static int hid_pre_reset(struct usb_interface *intf)
Alan Sterndf9a1f42006-06-01 13:55:28 -04001090{
1091 /* FIXME: What if the interface is already suspended? */
1092 hid_suspend(intf, PMSG_ON);
Alan Sternf07600c2007-05-30 15:38:16 -04001093 return 0;
Alan Sterndf9a1f42006-06-01 13:55:28 -04001094}
1095
Alan Sternf07600c2007-05-30 15:38:16 -04001096/* Same routine used for post_reset and reset_resume */
1097static int hid_post_reset(struct usb_interface *intf)
Alan Sterndf9a1f42006-06-01 13:55:28 -04001098{
1099 struct usb_device *dev = interface_to_usbdev (intf);
1100
1101 hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
1102 /* FIXME: Any more reinitialization needed? */
1103
Alan Sternf07600c2007-05-30 15:38:16 -04001104 return hid_resume(intf);
Alan Sterndf9a1f42006-06-01 13:55:28 -04001105}
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107static struct usb_device_id hid_usb_ids [] = {
1108 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1109 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
1110 { } /* Terminating entry */
1111};
1112
1113MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1114
1115static struct usb_driver hid_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 .name = "usbhid",
1117 .probe = hid_probe,
1118 .disconnect = hid_disconnect,
1119 .suspend = hid_suspend,
1120 .resume = hid_resume,
Alan Sternf07600c2007-05-30 15:38:16 -04001121 .reset_resume = hid_post_reset,
Alan Sterndf9a1f42006-06-01 13:55:28 -04001122 .pre_reset = hid_pre_reset,
1123 .post_reset = hid_post_reset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 .id_table = hid_usb_ids,
Oliver Neukum933e3182007-07-11 14:48:58 +02001125 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126};
1127
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001128static const struct hid_device_id hid_usb_table[] = {
1129 { HID_USB_DEVICE(HID_ANY_ID, HID_ANY_ID) },
1130 { }
1131};
1132
1133static struct hid_driver hid_usb_driver = {
1134 .name = "generic-usb",
1135 .id_table = hid_usb_table,
1136};
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138static int __init hid_init(void)
1139{
1140 int retval;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001141 retval = hid_register_driver(&hid_usb_driver);
1142 if (retval)
1143 goto hid_register_fail;
Paul Walmsley876b9272007-04-19 14:56:12 +02001144 retval = usbhid_quirks_init(quirks_param);
1145 if (retval)
1146 goto usbhid_quirks_init_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 retval = hiddev_init();
1148 if (retval)
1149 goto hiddev_init_fail;
1150 retval = usb_register(&hid_driver);
1151 if (retval)
1152 goto usb_register_fail;
Greg Kroah-Hartmanddbe3242008-10-12 00:14:23 +02001153 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1154 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 return 0;
1157usb_register_fail:
1158 hiddev_exit();
1159hiddev_init_fail:
Paul Walmsley876b9272007-04-19 14:56:12 +02001160 usbhid_quirks_exit();
1161usbhid_quirks_init_fail:
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001162 hid_unregister_driver(&hid_usb_driver);
1163hid_register_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 return retval;
1165}
1166
1167static void __exit hid_exit(void)
1168{
1169 usb_deregister(&hid_driver);
1170 hiddev_exit();
Paul Walmsley876b9272007-04-19 14:56:12 +02001171 usbhid_quirks_exit();
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001172 hid_unregister_driver(&hid_usb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
1174
1175module_init(hid_init);
1176module_exit(hid_exit);
1177
1178MODULE_AUTHOR(DRIVER_AUTHOR);
1179MODULE_DESCRIPTION(DRIVER_DESC);
1180MODULE_LICENSE(DRIVER_LICENSE);