blob: 2afc8617f5919ec28add338ac3197cd5c7c36998 [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 Stern6d8fc4d2006-10-18 12:35:24 -0400105 int rc_lock, 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");
Anssi Hannulabe820972007-01-19 19:28:17 +0200116 rc = rc_lock = usb_lock_device_for_reset(hid_to_usb_dev(hid), usbhid->intf);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400117 if (rc_lock >= 0) {
Ming Lei742120c2008-06-18 22:00:29 +0800118 rc = usb_reset_device(hid_to_usb_dev(hid));
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400119 if (rc_lock)
Anssi Hannulabe820972007-01-19 19:28:17 +0200120 usb_unlock_device(hid_to_usb_dev(hid));
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400121 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100122 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400123 }
Alan Sternaef4e262006-01-31 12:58:38 -0500124
Alan Sterndf9a1f42006-06-01 13:55:28 -0400125 switch (rc) {
126 case 0:
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100127 if (!test_bit(HID_IN_RUNNING, &usbhid->iofl))
Alan Sternaef4e262006-01-31 12:58:38 -0500128 hid_io_error(hid);
Alan Sterndf9a1f42006-06-01 13:55:28 -0400129 break;
130 default:
Jiri Kosina58037eb2007-05-30 15:07:13 +0200131 err_hid("can't reset device, %s-%s/input%d, status %d",
Anssi Hannulabe820972007-01-19 19:28:17 +0200132 hid_to_usb_dev(hid)->bus->bus_name,
133 hid_to_usb_dev(hid)->devpath,
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100134 usbhid->ifnum, rc);
Alan Sterndf9a1f42006-06-01 13:55:28 -0400135 /* FALLTHROUGH */
136 case -EHOSTUNREACH:
137 case -ENODEV:
138 case -EINTR:
139 break;
140 }
Alan Sternaef4e262006-01-31 12:58:38 -0500141}
142
143/* Main I/O error handler */
144static void hid_io_error(struct hid_device *hid)
145{
146 unsigned long flags;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100147 struct usbhid_device *usbhid = hid->driver_data;
Alan Sternaef4e262006-01-31 12:58:38 -0500148
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100149 spin_lock_irqsave(&usbhid->inlock, flags);
Alan Sternaef4e262006-01-31 12:58:38 -0500150
151 /* Stop when disconnected */
Oliver Neukum69626f22008-03-31 16:27:30 +0200152 if (test_bit(HID_DISCONNECTED, &usbhid->iofl))
Alan Sternaef4e262006-01-31 12:58:38 -0500153 goto done;
154
Alan Stern5e2a55f2007-03-20 19:03:31 +0100155 /* If it has been a while since the last error, we'll assume
156 * this a brand new error and reset the retry timeout. */
157 if (time_after(jiffies, usbhid->stop_retry + HZ/2))
158 usbhid->retry_delay = 0;
159
Alan Sternaef4e262006-01-31 12:58:38 -0500160 /* When an error occurs, retry at increasing intervals */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100161 if (usbhid->retry_delay == 0) {
162 usbhid->retry_delay = 13; /* Then 26, 52, 104, 104, ... */
163 usbhid->stop_retry = jiffies + msecs_to_jiffies(1000);
164 } else if (usbhid->retry_delay < 100)
165 usbhid->retry_delay *= 2;
Alan Sternaef4e262006-01-31 12:58:38 -0500166
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100167 if (time_after(jiffies, usbhid->stop_retry)) {
Alan Sternaef4e262006-01-31 12:58:38 -0500168
169 /* Retries failed, so do a port reset */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100170 if (!test_and_set_bit(HID_RESET_PENDING, &usbhid->iofl)) {
171 schedule_work(&usbhid->reset_work);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400172 goto done;
Alan Sternaef4e262006-01-31 12:58:38 -0500173 }
174 }
175
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100176 mod_timer(&usbhid->io_retry,
177 jiffies + msecs_to_jiffies(usbhid->retry_delay));
Alan Sternaef4e262006-01-31 12:58:38 -0500178done:
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100179 spin_unlock_irqrestore(&usbhid->inlock, flags);
Alan Sternaef4e262006-01-31 12:58:38 -0500180}
181
182/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * Input interrupt completion handler.
184 */
185
David Howells7d12e782006-10-05 14:55:46 +0100186static void hid_irq_in(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100189 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 int status;
191
192 switch (urb->status) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200193 case 0: /* success */
194 usbhid->retry_delay = 0;
195 hid_input_report(urb->context, HID_INPUT_REPORT,
196 urb->transfer_buffer,
197 urb->actual_length, 1);
198 break;
199 case -EPIPE: /* stall */
200 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
201 set_bit(HID_CLEAR_HALT, &usbhid->iofl);
202 schedule_work(&usbhid->reset_work);
203 return;
204 case -ECONNRESET: /* unlink */
205 case -ENOENT:
206 case -ESHUTDOWN: /* unplug */
207 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
208 return;
209 case -EILSEQ: /* protocol error or unplug */
210 case -EPROTO: /* protocol error or unplug */
211 case -ETIME: /* protocol error or unplug */
212 case -ETIMEDOUT: /* Should never happen, but... */
213 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
214 hid_io_error(hid);
215 return;
216 default: /* error */
From: Greg Kroah-Hartman7d89fe12008-10-12 00:25:51 +0200217 dev_warn(&urb->dev->dev, "input irq status %d "
218 "received\n", urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
220
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800221 status = usb_submit_urb(urb, GFP_ATOMIC);
Alan Sternaef4e262006-01-31 12:58:38 -0500222 if (status) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100223 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
Alan Sternaef4e262006-01-31 12:58:38 -0500224 if (status != -EPERM) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200225 err_hid("can't resubmit intr, %s-%s/input%d, status %d",
Anssi Hannulabe820972007-01-19 19:28:17 +0200226 hid_to_usb_dev(hid)->bus->bus_name,
227 hid_to_usb_dev(hid)->devpath,
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100228 usbhid->ifnum, status);
Alan Sternaef4e262006-01-31 12:58:38 -0500229 hid_io_error(hid);
230 }
231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234static int hid_submit_out(struct hid_device *hid)
235{
236 struct hid_report *report;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200237 char *raw_report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100238 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200240 report = usbhid->out[usbhid->outtail].report;
241 raw_report = usbhid->out[usbhid->outtail].raw_report;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100243 usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0);
Anssi Hannulabe820972007-01-19 19:28:17 +0200244 usbhid->urbout->dev = hid_to_usb_dev(hid);
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200245 memcpy(usbhid->outbuf, raw_report, usbhid->urbout->transfer_buffer_length);
246 kfree(raw_report);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Jiri Kosina58037eb2007-05-30 15:07:13 +0200248 dbg_hid("submitting out urb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100250 if (usb_submit_urb(usbhid->urbout, GFP_ATOMIC)) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200251 err_hid("usb_submit_urb(out) failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return -1;
253 }
254
255 return 0;
256}
257
258static int hid_submit_ctrl(struct hid_device *hid)
259{
260 struct hid_report *report;
261 unsigned char dir;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200262 char *raw_report;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 int len;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100264 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100266 report = usbhid->ctrl[usbhid->ctrltail].report;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200267 raw_report = usbhid->ctrl[usbhid->ctrltail].raw_report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100268 dir = usbhid->ctrl[usbhid->ctrltail].dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
271 if (dir == USB_DIR_OUT) {
Anssi Hannulabe820972007-01-19 19:28:17 +0200272 usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100273 usbhid->urbctrl->transfer_buffer_length = len;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200274 memcpy(usbhid->ctrlbuf, raw_report, len);
275 kfree(raw_report);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 } else {
277 int maxpacket, padlen;
278
Anssi Hannulabe820972007-01-19 19:28:17 +0200279 usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0);
280 maxpacket = usb_maxpacket(hid_to_usb_dev(hid), usbhid->urbctrl->pipe, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (maxpacket > 0) {
Julia Lawall92c4a1b2008-02-15 13:53:11 +0100282 padlen = DIV_ROUND_UP(len, maxpacket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 padlen *= maxpacket;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100284 if (padlen > usbhid->bufsize)
285 padlen = usbhid->bufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 } else
287 padlen = 0;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100288 usbhid->urbctrl->transfer_buffer_length = padlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
Anssi Hannulabe820972007-01-19 19:28:17 +0200290 usbhid->urbctrl->dev = hid_to_usb_dev(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100292 usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
293 usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT : HID_REQ_GET_REPORT;
294 usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) | report->id);
295 usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum);
296 usbhid->cr->wLength = cpu_to_le16(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Jiri Kosina58037eb2007-05-30 15:07:13 +0200298 dbg_hid("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u\n",
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100299 usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" : "Get_Report",
300 usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100302 if (usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC)) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200303 err_hid("usb_submit_urb(ctrl) failed");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return -1;
305 }
306
307 return 0;
308}
309
310/*
311 * Output interrupt completion handler.
312 */
313
David Howells7d12e782006-10-05 14:55:46 +0100314static void hid_irq_out(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100317 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 unsigned long flags;
319 int unplug = 0;
320
321 switch (urb->status) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200322 case 0: /* success */
323 break;
324 case -ESHUTDOWN: /* unplug */
325 unplug = 1;
326 case -EILSEQ: /* protocol error or unplug */
327 case -EPROTO: /* protocol error or unplug */
328 case -ECONNRESET: /* unlink */
329 case -ENOENT:
330 break;
331 default: /* error */
From: Greg Kroah-Hartman7d89fe12008-10-12 00:25:51 +0200332 dev_warn(&urb->dev->dev, "output irq status %d "
333 "received\n", urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100336 spin_lock_irqsave(&usbhid->outlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 if (unplug)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100339 usbhid->outtail = usbhid->outhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 else
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100341 usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100343 if (usbhid->outhead != usbhid->outtail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (hid_submit_out(hid)) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100345 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100346 wake_up(&usbhid->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100348 spin_unlock_irqrestore(&usbhid->outlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return;
350 }
351
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100352 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
353 spin_unlock_irqrestore(&usbhid->outlock, flags);
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100354 wake_up(&usbhid->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357/*
358 * Control pipe completion handler.
359 */
360
David Howells7d12e782006-10-05 14:55:46 +0100361static void hid_ctrl(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100364 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 unsigned long flags;
366 int unplug = 0;
367
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100368 spin_lock_irqsave(&usbhid->ctrllock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 switch (urb->status) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200371 case 0: /* success */
372 if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
373 hid_input_report(urb->context,
374 usbhid->ctrl[usbhid->ctrltail].report->type,
375 urb->transfer_buffer, urb->actual_length, 0);
376 break;
377 case -ESHUTDOWN: /* unplug */
378 unplug = 1;
379 case -EILSEQ: /* protocol error or unplug */
380 case -EPROTO: /* protocol error or unplug */
381 case -ECONNRESET: /* unlink */
382 case -ENOENT:
383 case -EPIPE: /* report not available */
384 break;
385 default: /* error */
From: Greg Kroah-Hartman7d89fe12008-10-12 00:25:51 +0200386 dev_warn(&urb->dev->dev, "ctrl urb status %d "
387 "received\n", urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389
390 if (unplug)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100391 usbhid->ctrltail = usbhid->ctrlhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 else
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100393 usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100395 if (usbhid->ctrlhead != usbhid->ctrltail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (hid_submit_ctrl(hid)) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100397 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100398 wake_up(&usbhid->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100400 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return;
402 }
403
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100404 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
405 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100406 wake_up(&usbhid->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100409void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 int head;
412 unsigned long flags;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100413 struct usbhid_device *usbhid = hid->driver_data;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200414 int len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
417 return;
418
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100419 if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100421 spin_lock_irqsave(&usbhid->outlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100423 if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
424 spin_unlock_irqrestore(&usbhid->outlock, flags);
From: Greg Kroah-Hartman7d89fe12008-10-12 00:25:51 +0200425 dev_warn(&hid->dev, "output queue full\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return;
427 }
428
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200429 usbhid->out[usbhid->outhead].raw_report = kmalloc(len, GFP_ATOMIC);
430 if (!usbhid->out[usbhid->outhead].raw_report) {
431 spin_unlock_irqrestore(&usbhid->outlock, flags);
Greg Kroah-Hartman46fcaec2008-10-15 11:30:07 -0700432 dev_warn(&hid->dev, "output queueing failed\n");
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200433 return;
434 }
435 hid_output_report(report, usbhid->out[usbhid->outhead].raw_report);
436 usbhid->out[usbhid->outhead].report = report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100437 usbhid->outhead = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100439 if (!test_and_set_bit(HID_OUT_RUNNING, &usbhid->iofl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (hid_submit_out(hid))
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100441 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100443 spin_unlock_irqrestore(&usbhid->outlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return;
445 }
446
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100447 spin_lock_irqsave(&usbhid->ctrllock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100449 if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
450 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
From: Greg Kroah-Hartman7d89fe12008-10-12 00:25:51 +0200451 dev_warn(&hid->dev, "control queue full\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return;
453 }
454
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200455 if (dir == USB_DIR_OUT) {
456 usbhid->ctrl[usbhid->ctrlhead].raw_report = kmalloc(len, GFP_ATOMIC);
457 if (!usbhid->ctrl[usbhid->ctrlhead].raw_report) {
458 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
Greg Kroah-Hartman46fcaec2008-10-15 11:30:07 -0700459 dev_warn(&hid->dev, "control queueing failed\n");
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200460 return;
461 }
462 hid_output_report(report, usbhid->ctrl[usbhid->ctrlhead].raw_report);
463 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100464 usbhid->ctrl[usbhid->ctrlhead].report = report;
465 usbhid->ctrl[usbhid->ctrlhead].dir = dir;
466 usbhid->ctrlhead = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100468 if (!test_and_set_bit(HID_CTRL_RUNNING, &usbhid->iofl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (hid_submit_ctrl(hid))
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100470 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100472 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
Jiri Slaby606bd0a2008-07-04 23:06:45 +0200474EXPORT_SYMBOL_GPL(usbhid_submit_report);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Jiri Kosina229695e2006-12-08 18:40:53 +0100476static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
Jiri Kosinadde58452006-12-08 18:40:44 +0100477{
Dmitry Torokhove0712982007-05-09 10:17:31 +0200478 struct hid_device *hid = input_get_drvdata(dev);
Jiri Kosinadde58452006-12-08 18:40:44 +0100479 struct hid_field *field;
480 int offset;
481
482 if (type == EV_FF)
483 return input_ff_event(dev, type, code, value);
484
485 if (type != EV_LED)
486 return -1;
487
488 if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
From: Greg Kroah-Hartman7d89fe12008-10-12 00:25:51 +0200489 dev_warn(&dev->dev, "event field not found\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100490 return -1;
491 }
492
493 hid_set_field(field, offset, value);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100494 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
Jiri Kosinadde58452006-12-08 18:40:44 +0100495
496 return 0;
497}
498
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100499int usbhid_wait_io(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100501 struct usbhid_device *usbhid = hid->driver_data;
502
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100503 if (!wait_event_timeout(usbhid->wait,
504 (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
505 !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 10*HZ)) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200507 dbg_hid("timeout waiting for ctrl or out queue to clear\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return -1;
509 }
510
511 return 0;
512}
513
Vojtech Pavlik854561b2005-05-29 02:28:00 -0500514static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
515{
Vojtech Pavlik71387bd2005-05-29 02:28:14 -0500516 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
Vojtech Pavlik854561b2005-05-29 02:28:00 -0500517 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
518 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
519}
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
522 unsigned char type, void *buf, int size)
523{
524 int result, retries = 4;
525
Jiri Kosina43c7bf02007-01-26 12:58:24 +0100526 memset(buf, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 do {
529 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
530 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
531 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
532 retries--;
533 } while (result < size && retries);
534 return result;
535}
536
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100537int usbhid_open(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Oliver Neukum933e3182007-07-11 14:48:58 +0200539 struct usbhid_device *usbhid = hid->driver_data;
540 int res;
541
542 if (!hid->open++) {
543 res = usb_autopm_get_interface(usbhid->intf);
544 if (res < 0) {
545 hid->open--;
546 return -EIO;
547 }
548 }
Alan Sternaef4e262006-01-31 12:58:38 -0500549 if (hid_start_in(hid))
550 hid_io_error(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return 0;
552}
553
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100554void usbhid_close(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100556 struct usbhid_device *usbhid = hid->driver_data;
557
Oliver Neukum933e3182007-07-11 14:48:58 +0200558 if (!--hid->open) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100559 usb_kill_urb(usbhid->urbin);
Oliver Neukum933e3182007-07-11 14:48:58 +0200560 usb_autopm_put_interface(usbhid->intf);
561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
564/*
565 * Initialize all reports
566 */
567
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100568void usbhid_init_reports(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 struct hid_report *report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100571 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 int err, ret;
573
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500574 list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100575 usbhid_submit_report(hid, report, USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100578 usbhid_submit_report(hid, report, USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 err = 0;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100581 ret = usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 while (ret) {
583 err |= ret;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100584 if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
585 usb_kill_urb(usbhid->urbctrl);
586 if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
587 usb_kill_urb(usbhid->urbout);
588 ret = usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
590
591 if (err)
From: Greg Kroah-Hartman7d89fe12008-10-12 00:25:51 +0200592 dev_warn(&hid->dev, "timeout initializing reports\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500595/*
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200596 * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
597 */
598static int hid_find_field_early(struct hid_device *hid, unsigned int page,
599 unsigned int hid_code, struct hid_field **pfield)
600{
601 struct hid_report *report;
602 struct hid_field *field;
603 struct hid_usage *usage;
604 int i, j;
605
606 list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
607 for (i = 0; i < report->maxfield; i++) {
608 field = report->field[i];
609 for (j = 0; j < field->maxusage; j++) {
610 usage = &field->usage[j];
611 if ((usage->hid & HID_USAGE_PAGE) == page &&
612 (usage->hid & 0xFFFF) == hid_code) {
613 *pfield = field;
614 return j;
615 }
616 }
617 }
618 }
619 return -1;
620}
621
Jiri Slaby6edfa8d2008-06-27 20:41:02 +0200622void usbhid_set_leds(struct hid_device *hid)
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200623{
624 struct hid_field *field;
625 int offset;
626
627 if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
628 hid_set_field(field, offset, 0);
629 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
630 }
631}
Jiri Slaby6edfa8d2008-06-27 20:41:02 +0200632EXPORT_SYMBOL_GPL(usbhid_set_leds);
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200633
634/*
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500635 * Traverse the supplied list of reports and find the longest
636 */
Jiri Slaby282bfd42008-03-28 17:06:41 +0100637static void hid_find_max_report(struct hid_device *hid, unsigned int type,
638 unsigned int *max)
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500639{
640 struct hid_report *report;
Jiri Slaby282bfd42008-03-28 17:06:41 +0100641 unsigned int size;
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500642
643 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
Jiri Kosinaefc7ce12008-10-17 15:01:15 +0200644 size = ((report->size - 1) >> 3) + 1 + hid->report_enum[type].numbered;
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500645 if (*max < size)
646 *max = size;
647 }
648}
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
651{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100652 struct usbhid_device *usbhid = hid->driver_data;
653
654 if (!(usbhid->inbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->inbuf_dma)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return -1;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100656 if (!(usbhid->outbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->outbuf_dma)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return -1;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100658 if (!(usbhid->cr = usb_buffer_alloc(dev, sizeof(*(usbhid->cr)), GFP_ATOMIC, &usbhid->cr_dma)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return -1;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100660 if (!(usbhid->ctrlbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->ctrlbuf_dma)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return -1;
662
663 return 0;
664}
665
Jiri Kosinaefc493f2007-05-14 09:54:30 +0200666static int usbhid_output_raw_report(struct hid_device *hid, __u8 *buf, size_t count)
667{
668 struct usbhid_device *usbhid = hid->driver_data;
669 struct usb_device *dev = hid_to_usb_dev(hid);
670 struct usb_interface *intf = usbhid->intf;
671 struct usb_host_interface *interface = intf->cur_altsetting;
672 int ret;
673
674 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
675 HID_REQ_SET_REPORT,
676 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
Al Viro01d7b362008-04-28 07:00:05 +0100677 ((HID_OUTPUT_REPORT + 1) << 8) | *buf,
Jiri Kosinaefc493f2007-05-14 09:54:30 +0200678 interface->desc.bInterfaceNumber, buf + 1, count - 1,
679 USB_CTRL_SET_TIMEOUT);
680
681 /* count also the report id */
682 if (ret > 0)
683 ret++;
684
685 return ret;
686}
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
689{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100690 struct usbhid_device *usbhid = hid->driver_data;
691
Dmitry Torokhov6675c5b2007-05-03 01:04:52 -0400692 usb_buffer_free(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
693 usb_buffer_free(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
694 usb_buffer_free(dev, sizeof(*(usbhid->cr)), usbhid->cr, usbhid->cr_dma);
695 usb_buffer_free(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697
Jiri Slabyc500c972008-05-16 11:49:16 +0200698static int usbhid_parse(struct hid_device *hid)
699{
700 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 struct usb_host_interface *interface = intf->cur_altsetting;
702 struct usb_device *dev = interface_to_usbdev (intf);
703 struct hid_descriptor *hdesc;
Paul Walmsley2eb5dc32007-04-19 13:27:04 +0200704 u32 quirks = 0;
Jiri Slabyc500c972008-05-16 11:49:16 +0200705 unsigned int rsize = 0;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500706 char *rdesc;
Jiri Slabyc500c972008-05-16 11:49:16 +0200707 int ret, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Paul Walmsley2eb5dc32007-04-19 13:27:04 +0200709 quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
710 le16_to_cpu(dev->descriptor.idProduct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Alan Stern0f28b552006-05-15 14:49:04 -0400712 /* Many keyboards and mice don't like to be polled for reports,
713 * so we will always set the HID_QUIRK_NOGET flag for them. */
714 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
715 if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
716 interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
717 quirks |= HID_QUIRK_NOGET;
718 }
719
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500720 if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
721 (!interface->desc.bNumEndpoints ||
722 usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200723 dbg_hid("class descriptor not present\n");
Jiri Slabyc500c972008-05-16 11:49:16 +0200724 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726
Jiri Slabyc500c972008-05-16 11:49:16 +0200727 hid->version = le16_to_cpu(hdesc->bcdHID);
728 hid->country = hdesc->bCountryCode;
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 for (n = 0; n < hdesc->bNumDescriptors; n++)
731 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
732 rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
733
734 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200735 dbg_hid("weird size of report descriptor (%u)\n", rsize);
Jiri Slabyc500c972008-05-16 11:49:16 +0200736 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
738
739 if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200740 dbg_hid("couldn't allocate rdesc memory\n");
Jiri Slabyc500c972008-05-16 11:49:16 +0200741 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743
Vojtech Pavlik854561b2005-05-29 02:28:00 -0500744 hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
745
Jiri Slabyc500c972008-05-16 11:49:16 +0200746 ret = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber,
747 HID_DT_REPORT, rdesc, rsize);
748 if (ret < 0) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200749 dbg_hid("reading report descriptor failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 kfree(rdesc);
Jiri Slabyc500c972008-05-16 11:49:16 +0200751 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753
Jiri Kosina58037eb2007-05-30 15:07:13 +0200754 dbg_hid("report descriptor (size %u, read %d) = ", rsize, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 for (n = 0; n < rsize; n++)
Jiri Kosina58037eb2007-05-30 15:07:13 +0200756 dbg_hid_line(" %02x", (unsigned char) rdesc[n]);
757 dbg_hid_line("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Jiri Slabyc500c972008-05-16 11:49:16 +0200759 ret = hid_parse_report(hid, rdesc, rsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 kfree(rdesc);
Jiri Slabyc500c972008-05-16 11:49:16 +0200761 if (ret) {
762 dbg_hid("parsing report descriptor failed\n");
763 goto err;
764 }
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 hid->quirks = quirks;
767
Jiri Slabyc500c972008-05-16 11:49:16 +0200768 return 0;
769err:
770 return ret;
771}
772
773static int usbhid_start(struct hid_device *hid)
774{
775 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
776 struct usb_host_interface *interface = intf->cur_altsetting;
777 struct usb_device *dev = interface_to_usbdev(intf);
Jiri Slaby3d5afd32008-10-27 12:16:15 +0100778 struct usbhid_device *usbhid = hid->driver_data;
Jiri Slabyc500c972008-05-16 11:49:16 +0200779 unsigned int n, insize = 0;
780 int ret;
781
Jiri Slabye3e14de2008-11-01 23:41:46 +0100782 clear_bit(HID_DISCONNECTED, &usbhid->iofl);
783
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100784 usbhid->bufsize = HID_MIN_BUFFER_SIZE;
785 hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
786 hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
787 hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
788
789 if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
790 usbhid->bufsize = HID_MAX_BUFFER_SIZE;
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500791
792 hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
793
794 if (insize > HID_MAX_BUFFER_SIZE)
795 insize = HID_MAX_BUFFER_SIZE;
796
Jiri Slabyc500c972008-05-16 11:49:16 +0200797 if (hid_alloc_buffers(dev, hid)) {
798 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 goto fail;
Pekka Sarnilaf345c372008-03-06 13:23:14 +0100800 }
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 for (n = 0; n < interface->desc.bNumEndpoints; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 struct usb_endpoint_descriptor *endpoint;
804 int pipe;
805 int interval;
806
807 endpoint = &interface->endpoint[n].desc;
808 if ((endpoint->bmAttributes & 3) != 3) /* Not an interrupt endpoint */
809 continue;
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 interval = endpoint->bInterval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Pekka Sarnilaf345c372008-03-06 13:23:14 +0100813 /* Some vendors give fullspeed interval on highspeed devides */
Jiri Slabyc500c972008-05-16 11:49:16 +0200814 if (hid->quirks & HID_QUIRK_FULLSPEED_INTERVAL &&
Pekka Sarnilaf345c372008-03-06 13:23:14 +0100815 dev->speed == USB_SPEED_HIGH) {
816 interval = fls(endpoint->bInterval*8);
817 printk(KERN_INFO "%s: Fixing fullspeed to highspeed interval: %d -> %d\n",
818 hid->name, endpoint->bInterval, interval);
819 }
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 /* Change the polling interval of mice. */
822 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
823 interval = hid_mousepoll_interval;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500824
Jiri Slabyc500c972008-05-16 11:49:16 +0200825 ret = -ENOMEM;
Luiz Fernando N. Capitulino0f12aa02006-10-26 13:02:51 -0300826 if (usb_endpoint_dir_in(endpoint)) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100827 if (usbhid->urbin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 continue;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100829 if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 goto fail;
831 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100832 usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 hid_irq_in, hid, interval);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100834 usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
835 usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 } else {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100837 if (usbhid->urbout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 continue;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100839 if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 goto fail;
841 pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100842 usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 hid_irq_out, hid, interval);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100844 usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
845 usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
847 }
848
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100849 init_waitqueue_head(&usbhid->wait);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100850 INIT_WORK(&usbhid->reset_work, hid_reset);
851 setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
Alan Sternaef4e262006-01-31 12:58:38 -0500852
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100853 spin_lock_init(&usbhid->inlock);
854 spin_lock_init(&usbhid->outlock);
855 spin_lock_init(&usbhid->ctrllock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100857 usbhid->intf = intf;
858 usbhid->ifnum = interface->desc.bInterfaceNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100860 usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
Jiri Slabyc500c972008-05-16 11:49:16 +0200861 if (!usbhid->urbctrl) {
862 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 goto fail;
Jiri Slabyc500c972008-05-16 11:49:16 +0200864 }
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500865
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100866 usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
867 usbhid->ctrlbuf, 1, hid_ctrl, hid);
868 usbhid->urbctrl->setup_dma = usbhid->cr_dma;
869 usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
870 usbhid->urbctrl->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP);
Jiri Slabyc500c972008-05-16 11:49:16 +0200871
Jiri Slaby93c10132008-06-27 00:04:24 +0200872 usbhid_init_reports(hid);
873 hid_dump_device(hid);
874
Jiri Slaby3d5afd32008-10-27 12:16:15 +0100875 set_bit(HID_STARTED, &usbhid->iofl);
Jiri Slaby3d5afd32008-10-27 12:16:15 +0100876
Jiri Slabyc500c972008-05-16 11:49:16 +0200877 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879fail:
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100880 usb_free_urb(usbhid->urbin);
881 usb_free_urb(usbhid->urbout);
882 usb_free_urb(usbhid->urbctrl);
Jiri Slabye3e14de2008-11-01 23:41:46 +0100883 usbhid->urbin = NULL;
884 usbhid->urbout = NULL;
885 usbhid->urbctrl = NULL;
Jiri Kosina22f675f2007-08-01 12:32:27 +0200886 hid_free_buffers(dev, hid);
Jiri Slabyc500c972008-05-16 11:49:16 +0200887 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
Jiri Slabyc500c972008-05-16 11:49:16 +0200890static void usbhid_stop(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Jiri Slabyc500c972008-05-16 11:49:16 +0200892 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Jiri Slabyc500c972008-05-16 11:49:16 +0200894 if (WARN_ON(!usbhid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return;
896
Jiri Slaby3d5afd32008-10-27 12:16:15 +0100897 clear_bit(HID_STARTED, &usbhid->iofl);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100898 spin_lock_irq(&usbhid->inlock); /* Sync with error handler */
Oliver Neukum69626f22008-03-31 16:27:30 +0200899 set_bit(HID_DISCONNECTED, &usbhid->iofl);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100900 spin_unlock_irq(&usbhid->inlock);
901 usb_kill_urb(usbhid->urbin);
902 usb_kill_urb(usbhid->urbout);
903 usb_kill_urb(usbhid->urbctrl);
904
905 del_timer_sync(&usbhid->io_retry);
Alan Stern2fa45a42007-05-30 11:11:12 -0400906 cancel_work_sync(&usbhid->reset_work);
Alan Sternaef4e262006-01-31 12:58:38 -0500907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if (hid->claimed & HID_CLAIMED_INPUT)
909 hidinput_disconnect(hid);
910 if (hid->claimed & HID_CLAIMED_HIDDEV)
911 hiddev_disconnect(hid);
Jiri Kosina86166b72007-05-14 09:57:40 +0200912 if (hid->claimed & HID_CLAIMED_HIDRAW)
913 hidraw_disconnect(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Jiri Slabyc500c972008-05-16 11:49:16 +0200915 hid->claimed = 0;
916
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100917 usb_free_urb(usbhid->urbin);
918 usb_free_urb(usbhid->urbctrl);
919 usb_free_urb(usbhid->urbout);
Jiri Slabye3e14de2008-11-01 23:41:46 +0100920 usbhid->urbin = NULL; /* don't mess up next start */
921 usbhid->urbctrl = NULL;
922 usbhid->urbout = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Anssi Hannulabe820972007-01-19 19:28:17 +0200924 hid_free_buffers(hid_to_usb_dev(hid), hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
Jiri Slabyc500c972008-05-16 11:49:16 +0200927static struct hid_ll_driver usb_hid_driver = {
928 .parse = usbhid_parse,
929 .start = usbhid_start,
930 .stop = usbhid_stop,
931 .open = usbhid_open,
932 .close = usbhid_close,
933 .hidinput_input_event = usb_hidinput_input_event,
934};
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id)
937{
Jiri Slaby131d3a7a2008-11-14 12:03:47 +0100938 struct usb_host_interface *interface = intf->cur_altsetting;
Jiri Slabyc500c972008-05-16 11:49:16 +0200939 struct usb_device *dev = interface_to_usbdev(intf);
Jiri Slaby3d5afd32008-10-27 12:16:15 +0100940 struct usbhid_device *usbhid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 struct hid_device *hid;
Jiri Slaby131d3a7a2008-11-14 12:03:47 +0100942 unsigned int n, has_in = 0;
Jiri Slabyc500c972008-05-16 11:49:16 +0200943 size_t len;
944 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Jiri Kosina58037eb2007-05-30 15:07:13 +0200946 dbg_hid("HID probe called for ifnum %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 intf->altsetting->desc.bInterfaceNumber);
948
Jiri Slaby131d3a7a2008-11-14 12:03:47 +0100949 for (n = 0; n < interface->desc.bNumEndpoints; n++)
950 if (usb_endpoint_is_int_in(&interface->endpoint[n].desc))
951 has_in++;
952 if (!has_in) {
953 dev_err(&intf->dev, "couldn't find an input interrupt "
954 "endpoint\n");
955 return -ENODEV;
956 }
957
Jiri Slabyc500c972008-05-16 11:49:16 +0200958 hid = hid_allocate_device();
959 if (IS_ERR(hid))
960 return PTR_ERR(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 usb_set_intfdata(intf, hid);
Jiri Slabyc500c972008-05-16 11:49:16 +0200963 hid->ll_driver = &usb_hid_driver;
964 hid->hid_output_raw_report = usbhid_output_raw_report;
Jiri Slaby76483cf2008-09-18 12:23:33 +0200965 hid->ff_init = hid_pidff_init;
Jiri Slabyc500c972008-05-16 11:49:16 +0200966#ifdef CONFIG_USB_HIDDEV
Jiri Slaby93c10132008-06-27 00:04:24 +0200967 hid->hiddev_connect = hiddev_connect;
Jiri Slabyc500c972008-05-16 11:49:16 +0200968 hid->hiddev_hid_event = hiddev_hid_event;
969 hid->hiddev_report_event = hiddev_report_event;
970#endif
971 hid->dev.parent = &intf->dev;
972 hid->bus = BUS_USB;
973 hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
974 hid->product = le16_to_cpu(dev->descriptor.idProduct);
975 hid->name[0] = 0;
Jiri Slabya73a6372008-10-22 14:45:11 +0200976 if (intf->cur_altsetting->desc.bInterfaceProtocol ==
977 USB_INTERFACE_PROTOCOL_MOUSE)
978 hid->type = HID_TYPE_USBMOUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Jiri Slabyc500c972008-05-16 11:49:16 +0200980 if (dev->manufacturer)
981 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
982
983 if (dev->product) {
984 if (dev->manufacturer)
985 strlcat(hid->name, " ", sizeof(hid->name));
986 strlcat(hid->name, dev->product, sizeof(hid->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
988
Jiri Slabyc500c972008-05-16 11:49:16 +0200989 if (!strlen(hid->name))
990 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
991 le16_to_cpu(dev->descriptor.idVendor),
992 le16_to_cpu(dev->descriptor.idProduct));
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100993
Jiri Slabyc500c972008-05-16 11:49:16 +0200994 usb_make_path(dev, hid->phys, sizeof(hid->phys));
995 strlcat(hid->phys, "/input", sizeof(hid->phys));
996 len = strlen(hid->phys);
997 if (len < sizeof(hid->phys) - 1)
998 snprintf(hid->phys + len, sizeof(hid->phys) - len,
999 "%d", intf->altsetting[0].desc.bInterfaceNumber);
Geoff Levand4a1a4d82007-01-15 20:11:52 -08001000
Jiri Slabyc500c972008-05-16 11:49:16 +02001001 if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
1002 hid->uniq[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001004 usbhid = kzalloc(sizeof(*usbhid), GFP_KERNEL);
1005 if (usbhid == NULL) {
1006 ret = -ENOMEM;
1007 goto err;
1008 }
1009
1010 hid->driver_data = usbhid;
1011 usbhid->hid = hid;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001012
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001013 ret = hid_add_device(hid);
1014 if (ret) {
Jiri Slabyd458a9d2008-05-16 11:49:20 +02001015 if (ret != -ENODEV)
1016 dev_err(&intf->dev, "can't add hid device: %d\n", ret);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001017 goto err_free;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001018 }
Jiri Slabyc500c972008-05-16 11:49:16 +02001019
1020 return 0;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001021err_free:
1022 kfree(usbhid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001023err:
1024 hid_destroy_device(hid);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001025 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027
Jiri Slabyc500c972008-05-16 11:49:16 +02001028static void hid_disconnect(struct usb_interface *intf)
1029{
1030 struct hid_device *hid = usb_get_intfdata(intf);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001031 struct usbhid_device *usbhid;
Jiri Slabyc500c972008-05-16 11:49:16 +02001032
1033 if (WARN_ON(!hid))
1034 return;
1035
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001036 usbhid = hid->driver_data;
Jiri Slabyc500c972008-05-16 11:49:16 +02001037 hid_destroy_device(hid);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001038 kfree(usbhid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001039}
1040
David Brownell27d72e82005-04-18 17:39:22 -07001041static int hid_suspend(struct usb_interface *intf, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
1043 struct hid_device *hid = usb_get_intfdata (intf);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001044 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Jiri Slabyfde5be32008-11-23 12:03:20 +01001046 if (!test_bit(HID_STARTED, &usbhid->iofl))
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001047 return 0;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001048
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001049 spin_lock_irq(&usbhid->inlock); /* Sync with error handler */
1050 set_bit(HID_SUSPENDED, &usbhid->iofl);
1051 spin_unlock_irq(&usbhid->inlock);
Jiri Slabyb1700602008-10-27 12:16:16 +01001052 del_timer_sync(&usbhid->io_retry);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001053 usb_kill_urb(usbhid->urbin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 dev_dbg(&intf->dev, "suspend\n");
1055 return 0;
1056}
1057
1058static int hid_resume(struct usb_interface *intf)
1059{
1060 struct hid_device *hid = usb_get_intfdata (intf);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001061 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 int status;
1063
Jiri Slabyfde5be32008-11-23 12:03:20 +01001064 if (!test_bit(HID_STARTED, &usbhid->iofl))
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001065 return 0;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001066
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001067 clear_bit(HID_SUSPENDED, &usbhid->iofl);
1068 usbhid->retry_delay = 0;
Alan Sternaef4e262006-01-31 12:58:38 -05001069 status = hid_start_in(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 dev_dbg(&intf->dev, "resume status %d\n", status);
1071 return status;
1072}
1073
Alan Sterndf9a1f42006-06-01 13:55:28 -04001074/* Treat USB reset pretty much the same as suspend/resume */
Alan Sternf07600c2007-05-30 15:38:16 -04001075static int hid_pre_reset(struct usb_interface *intf)
Alan Sterndf9a1f42006-06-01 13:55:28 -04001076{
1077 /* FIXME: What if the interface is already suspended? */
1078 hid_suspend(intf, PMSG_ON);
Alan Sternf07600c2007-05-30 15:38:16 -04001079 return 0;
Alan Sterndf9a1f42006-06-01 13:55:28 -04001080}
1081
Alan Sternf07600c2007-05-30 15:38:16 -04001082/* Same routine used for post_reset and reset_resume */
1083static int hid_post_reset(struct usb_interface *intf)
Alan Sterndf9a1f42006-06-01 13:55:28 -04001084{
1085 struct usb_device *dev = interface_to_usbdev (intf);
1086
1087 hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
1088 /* FIXME: Any more reinitialization needed? */
1089
Alan Sternf07600c2007-05-30 15:38:16 -04001090 return hid_resume(intf);
Alan Sterndf9a1f42006-06-01 13:55:28 -04001091}
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093static struct usb_device_id hid_usb_ids [] = {
1094 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1095 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
1096 { } /* Terminating entry */
1097};
1098
1099MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1100
1101static struct usb_driver hid_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 .name = "usbhid",
1103 .probe = hid_probe,
1104 .disconnect = hid_disconnect,
1105 .suspend = hid_suspend,
1106 .resume = hid_resume,
Alan Sternf07600c2007-05-30 15:38:16 -04001107 .reset_resume = hid_post_reset,
Alan Sterndf9a1f42006-06-01 13:55:28 -04001108 .pre_reset = hid_pre_reset,
1109 .post_reset = hid_post_reset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 .id_table = hid_usb_ids,
Oliver Neukum933e3182007-07-11 14:48:58 +02001111 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112};
1113
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001114static const struct hid_device_id hid_usb_table[] = {
1115 { HID_USB_DEVICE(HID_ANY_ID, HID_ANY_ID) },
1116 { }
1117};
1118
1119static struct hid_driver hid_usb_driver = {
1120 .name = "generic-usb",
1121 .id_table = hid_usb_table,
1122};
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124static int __init hid_init(void)
1125{
1126 int retval;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001127 retval = hid_register_driver(&hid_usb_driver);
1128 if (retval)
1129 goto hid_register_fail;
Paul Walmsley876b9272007-04-19 14:56:12 +02001130 retval = usbhid_quirks_init(quirks_param);
1131 if (retval)
1132 goto usbhid_quirks_init_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 retval = hiddev_init();
1134 if (retval)
1135 goto hiddev_init_fail;
1136 retval = usb_register(&hid_driver);
1137 if (retval)
1138 goto usb_register_fail;
Greg Kroah-Hartmanddbe3242008-10-12 00:14:23 +02001139 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1140 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 return 0;
1143usb_register_fail:
1144 hiddev_exit();
1145hiddev_init_fail:
Paul Walmsley876b9272007-04-19 14:56:12 +02001146 usbhid_quirks_exit();
1147usbhid_quirks_init_fail:
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001148 hid_unregister_driver(&hid_usb_driver);
1149hid_register_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return retval;
1151}
1152
1153static void __exit hid_exit(void)
1154{
1155 usb_deregister(&hid_driver);
1156 hiddev_exit();
Paul Walmsley876b9272007-04-19 14:56:12 +02001157 usbhid_quirks_exit();
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001158 hid_unregister_driver(&hid_usb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
1160
1161module_init(hid_init);
1162module_exit(hid_exit);
1163
1164MODULE_AUTHOR(DRIVER_AUTHOR);
1165MODULE_DESCRIPTION(DRIVER_DESC);
1166MODULE_LICENSE(DRIVER_LICENSE);