blob: d79100a01b0554b4579fc21c40bd3cc9d25236af [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
Oliver Neukum0361a282008-12-17 15:38:03 +01008 * Copyright (c) 2007-2008 Oliver Neukum
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11/*
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your option)
15 * any later version.
16 */
17
18#include <linux/module.h>
19#include <linux/slab.h>
20#include <linux/init.h>
21#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/list.h>
23#include <linux/mm.h>
Jiri Slaby3d5afd32008-10-27 12:16:15 +010024#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/smp_lock.h>
26#include <linux/spinlock.h>
27#include <asm/unaligned.h>
28#include <asm/byteorder.h>
29#include <linux/input.h>
30#include <linux/wait.h>
Oliver Neukum0361a282008-12-17 15:38:03 +010031#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/usb.h>
34
Jiri Kosinadde58452006-12-08 18:40:44 +010035#include <linux/hid.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/hiddev.h>
Jiri Kosinac080d892007-01-25 11:43:31 +010037#include <linux/hid-debug.h>
Jiri Kosina86166b72007-05-14 09:57:40 +020038#include <linux/hidraw.h>
Jiri Kosina4916b3a2006-12-08 18:41:03 +010039#include "usbhid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/*
42 * Version Information
43 */
44
Michael Haboustakbf0964d2005-09-05 00:12:01 -050045#define DRIVER_VERSION "v2.6"
Jiri Kosinaf142b3a42007-04-16 11:29:28 +020046#define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik, Jiri Kosina"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define DRIVER_DESC "USB HID core driver"
48#define DRIVER_LICENSE "GPL"
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/*
51 * Module parameters.
52 */
53
54static unsigned int hid_mousepoll_interval;
55module_param_named(mousepoll, hid_mousepoll_interval, uint, 0644);
56MODULE_PARM_DESC(mousepoll, "Polling interval of mice");
57
Oliver Neukum0361a282008-12-17 15:38:03 +010058static unsigned int ignoreled;
59module_param_named(ignoreled, ignoreled, uint, 0644);
60MODULE_PARM_DESC(ignoreled, "Autosuspend with active leds");
61
Paul Walmsley876b9272007-04-19 14:56:12 +020062/* Quirks specified at module load time */
63static char *quirks_param[MAX_USBHID_BOOT_QUIRKS] = { [ 0 ... (MAX_USBHID_BOOT_QUIRKS - 1) ] = NULL };
64module_param_array_named(quirks, quirks_param, charp, NULL, 0444);
65MODULE_PARM_DESC(quirks, "Add/modify USB HID quirks by specifying "
66 " quirks=vendorID:productID:quirks"
67 " where vendorID, productID, and quirks are all in"
68 " 0x-prefixed hex");
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/*
Alan Sternaef4e262006-01-31 12:58:38 -050070 * Input submission and I/O error handler.
71 */
Oliver Neukum0361a282008-12-17 15:38:03 +010072static DEFINE_MUTEX(hid_open_mut);
73static struct workqueue_struct *resumption_waker;
Alan Sternaef4e262006-01-31 12:58:38 -050074
75static void hid_io_error(struct hid_device *hid);
Oliver Neukum0361a282008-12-17 15:38:03 +010076static int hid_submit_out(struct hid_device *hid);
77static int hid_submit_ctrl(struct hid_device *hid);
78static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid);
Alan Sternaef4e262006-01-31 12:58:38 -050079
80/* Start up the input URB */
81static int hid_start_in(struct hid_device *hid)
82{
83 unsigned long flags;
84 int rc = 0;
Jiri Kosina4916b3a2006-12-08 18:41:03 +010085 struct usbhid_device *usbhid = hid->driver_data;
Alan Sternaef4e262006-01-31 12:58:38 -050086
Oliver Neukum0361a282008-12-17 15:38:03 +010087 spin_lock_irqsave(&usbhid->lock, flags);
88 if (hid->open > 0 &&
Oliver Neukum69626f22008-03-31 16:27:30 +020089 !test_bit(HID_DISCONNECTED, &usbhid->iofl) &&
Oliver Neukum0361a282008-12-17 15:38:03 +010090 !test_bit(HID_REPORTED_IDLE, &usbhid->iofl) &&
Jiri Kosina4916b3a2006-12-08 18:41:03 +010091 !test_and_set_bit(HID_IN_RUNNING, &usbhid->iofl)) {
92 rc = usb_submit_urb(usbhid->urbin, GFP_ATOMIC);
Alan Sternaef4e262006-01-31 12:58:38 -050093 if (rc != 0)
Jiri Kosina4916b3a2006-12-08 18:41:03 +010094 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
Alan Sternaef4e262006-01-31 12:58:38 -050095 }
Oliver Neukum0361a282008-12-17 15:38:03 +010096 spin_unlock_irqrestore(&usbhid->lock, flags);
Alan Sternaef4e262006-01-31 12:58:38 -050097 return rc;
98}
99
100/* I/O retry timer routine */
101static void hid_retry_timeout(unsigned long _hid)
102{
103 struct hid_device *hid = (struct hid_device *) _hid;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100104 struct usbhid_device *usbhid = hid->driver_data;
Alan Sternaef4e262006-01-31 12:58:38 -0500105
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100106 dev_dbg(&usbhid->intf->dev, "retrying intr urb\n");
Alan Sternaef4e262006-01-31 12:58:38 -0500107 if (hid_start_in(hid))
108 hid_io_error(hid);
109}
110
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400111/* Workqueue routine to reset the device or clear a halt */
David Howellsc4028952006-11-22 14:57:56 +0000112static void hid_reset(struct work_struct *work)
Alan Sternaef4e262006-01-31 12:58:38 -0500113{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100114 struct usbhid_device *usbhid =
115 container_of(work, struct usbhid_device, reset_work);
116 struct hid_device *hid = usbhid->hid;
Alan Stern011b15d2008-11-04 11:29:27 -0500117 int rc = 0;
Alan Sternaef4e262006-01-31 12:58:38 -0500118
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100119 if (test_bit(HID_CLEAR_HALT, &usbhid->iofl)) {
120 dev_dbg(&usbhid->intf->dev, "clear halt\n");
Anssi Hannulabe820972007-01-19 19:28:17 +0200121 rc = usb_clear_halt(hid_to_usb_dev(hid), usbhid->urbin->pipe);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100122 clear_bit(HID_CLEAR_HALT, &usbhid->iofl);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400123 hid_start_in(hid);
Alan Sternaef4e262006-01-31 12:58:38 -0500124 }
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400125
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100126 else if (test_bit(HID_RESET_PENDING, &usbhid->iofl)) {
127 dev_dbg(&usbhid->intf->dev, "resetting device\n");
Alan Stern011b15d2008-11-04 11:29:27 -0500128 rc = usb_lock_device_for_reset(hid_to_usb_dev(hid), usbhid->intf);
129 if (rc == 0) {
Ming Lei742120c2008-06-18 22:00:29 +0800130 rc = usb_reset_device(hid_to_usb_dev(hid));
Alan Stern011b15d2008-11-04 11:29:27 -0500131 usb_unlock_device(hid_to_usb_dev(hid));
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400132 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100133 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400134 }
Alan Sternaef4e262006-01-31 12:58:38 -0500135
Alan Sterndf9a1f42006-06-01 13:55:28 -0400136 switch (rc) {
137 case 0:
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100138 if (!test_bit(HID_IN_RUNNING, &usbhid->iofl))
Alan Sternaef4e262006-01-31 12:58:38 -0500139 hid_io_error(hid);
Alan Sterndf9a1f42006-06-01 13:55:28 -0400140 break;
141 default:
Jiri Kosina58037eb2007-05-30 15:07:13 +0200142 err_hid("can't reset device, %s-%s/input%d, status %d",
Anssi Hannulabe820972007-01-19 19:28:17 +0200143 hid_to_usb_dev(hid)->bus->bus_name,
144 hid_to_usb_dev(hid)->devpath,
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100145 usbhid->ifnum, rc);
Alan Sterndf9a1f42006-06-01 13:55:28 -0400146 /* FALLTHROUGH */
147 case -EHOSTUNREACH:
148 case -ENODEV:
149 case -EINTR:
150 break;
151 }
Alan Sternaef4e262006-01-31 12:58:38 -0500152}
153
154/* Main I/O error handler */
155static void hid_io_error(struct hid_device *hid)
156{
157 unsigned long flags;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100158 struct usbhid_device *usbhid = hid->driver_data;
Alan Sternaef4e262006-01-31 12:58:38 -0500159
Oliver Neukum0361a282008-12-17 15:38:03 +0100160 spin_lock_irqsave(&usbhid->lock, flags);
Alan Sternaef4e262006-01-31 12:58:38 -0500161
162 /* Stop when disconnected */
Oliver Neukum69626f22008-03-31 16:27:30 +0200163 if (test_bit(HID_DISCONNECTED, &usbhid->iofl))
Alan Sternaef4e262006-01-31 12:58:38 -0500164 goto done;
165
Alan Stern5e2a55f2007-03-20 19:03:31 +0100166 /* If it has been a while since the last error, we'll assume
167 * this a brand new error and reset the retry timeout. */
168 if (time_after(jiffies, usbhid->stop_retry + HZ/2))
169 usbhid->retry_delay = 0;
170
Alan Sternaef4e262006-01-31 12:58:38 -0500171 /* When an error occurs, retry at increasing intervals */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100172 if (usbhid->retry_delay == 0) {
173 usbhid->retry_delay = 13; /* Then 26, 52, 104, 104, ... */
174 usbhid->stop_retry = jiffies + msecs_to_jiffies(1000);
175 } else if (usbhid->retry_delay < 100)
176 usbhid->retry_delay *= 2;
Alan Sternaef4e262006-01-31 12:58:38 -0500177
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100178 if (time_after(jiffies, usbhid->stop_retry)) {
Alan Sternaef4e262006-01-31 12:58:38 -0500179
180 /* Retries failed, so do a port reset */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100181 if (!test_and_set_bit(HID_RESET_PENDING, &usbhid->iofl)) {
182 schedule_work(&usbhid->reset_work);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400183 goto done;
Alan Sternaef4e262006-01-31 12:58:38 -0500184 }
185 }
186
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100187 mod_timer(&usbhid->io_retry,
188 jiffies + msecs_to_jiffies(usbhid->retry_delay));
Alan Sternaef4e262006-01-31 12:58:38 -0500189done:
Oliver Neukum0361a282008-12-17 15:38:03 +0100190 spin_unlock_irqrestore(&usbhid->lock, flags);
191}
192
193static void usbhid_mark_busy(struct usbhid_device *usbhid)
194{
195 struct usb_interface *intf = usbhid->intf;
196
197 usb_mark_last_busy(interface_to_usbdev(intf));
198}
199
200static int usbhid_restart_out_queue(struct usbhid_device *usbhid)
201{
202 struct hid_device *hid = usb_get_intfdata(usbhid->intf);
203 int kicked;
204
205 if (!hid)
206 return 0;
207
208 if ((kicked = (usbhid->outhead != usbhid->outtail))) {
209 dbg("Kicking head %d tail %d", usbhid->outhead, usbhid->outtail);
210 if (hid_submit_out(hid)) {
211 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
212 wake_up(&usbhid->wait);
213 }
214 }
215 return kicked;
216}
217
218static int usbhid_restart_ctrl_queue(struct usbhid_device *usbhid)
219{
220 struct hid_device *hid = usb_get_intfdata(usbhid->intf);
221 int kicked;
222
223 WARN_ON(hid == NULL);
224 if (!hid)
225 return 0;
226
227 if ((kicked = (usbhid->ctrlhead != usbhid->ctrltail))) {
228 dbg("Kicking head %d tail %d", usbhid->ctrlhead, usbhid->ctrltail);
229 if (hid_submit_ctrl(hid)) {
230 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
231 wake_up(&usbhid->wait);
232 }
233 }
234 return kicked;
Alan Sternaef4e262006-01-31 12:58:38 -0500235}
236
237/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 * Input interrupt completion handler.
239 */
240
David Howells7d12e782006-10-05 14:55:46 +0100241static void hid_irq_in(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
243 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100244 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 int status;
246
247 switch (urb->status) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200248 case 0: /* success */
Oliver Neukum0361a282008-12-17 15:38:03 +0100249 usbhid_mark_busy(usbhid);
Jiri Slaby880d29f2008-06-18 23:55:41 +0200250 usbhid->retry_delay = 0;
251 hid_input_report(urb->context, HID_INPUT_REPORT,
252 urb->transfer_buffer,
253 urb->actual_length, 1);
Oliver Neukum0361a282008-12-17 15:38:03 +0100254 /*
255 * autosuspend refused while keys are pressed
256 * because most keyboards don't wake up when
257 * a key is released
258 */
259 if (hid_check_keys_pressed(hid))
260 set_bit(HID_KEYS_PRESSED, &usbhid->iofl);
261 else
262 clear_bit(HID_KEYS_PRESSED, &usbhid->iofl);
Jiri Slaby880d29f2008-06-18 23:55:41 +0200263 break;
264 case -EPIPE: /* stall */
Oliver Neukum0361a282008-12-17 15:38:03 +0100265 usbhid_mark_busy(usbhid);
Jiri Slaby880d29f2008-06-18 23:55:41 +0200266 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
267 set_bit(HID_CLEAR_HALT, &usbhid->iofl);
268 schedule_work(&usbhid->reset_work);
269 return;
270 case -ECONNRESET: /* unlink */
271 case -ENOENT:
272 case -ESHUTDOWN: /* unplug */
273 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
274 return;
275 case -EILSEQ: /* protocol error or unplug */
276 case -EPROTO: /* protocol error or unplug */
277 case -ETIME: /* protocol error or unplug */
278 case -ETIMEDOUT: /* Should never happen, but... */
Oliver Neukum0361a282008-12-17 15:38:03 +0100279 usbhid_mark_busy(usbhid);
Jiri Slaby880d29f2008-06-18 23:55:41 +0200280 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
281 hid_io_error(hid);
282 return;
283 default: /* error */
From: Greg Kroah-Hartman7d89fe12008-10-12 00:25:51 +0200284 dev_warn(&urb->dev->dev, "input irq status %d "
285 "received\n", urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800288 status = usb_submit_urb(urb, GFP_ATOMIC);
Alan Sternaef4e262006-01-31 12:58:38 -0500289 if (status) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100290 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
Alan Sternaef4e262006-01-31 12:58:38 -0500291 if (status != -EPERM) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200292 err_hid("can't resubmit intr, %s-%s/input%d, status %d",
Anssi Hannulabe820972007-01-19 19:28:17 +0200293 hid_to_usb_dev(hid)->bus->bus_name,
294 hid_to_usb_dev(hid)->devpath,
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100295 usbhid->ifnum, status);
Alan Sternaef4e262006-01-31 12:58:38 -0500296 hid_io_error(hid);
297 }
298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301static int hid_submit_out(struct hid_device *hid)
302{
303 struct hid_report *report;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200304 char *raw_report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100305 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200307 report = usbhid->out[usbhid->outtail].report;
308 raw_report = usbhid->out[usbhid->outtail].raw_report;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Oliver Neukum0361a282008-12-17 15:38:03 +0100310 if (!test_bit(HID_REPORTED_IDLE, &usbhid->iofl)) {
311 usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0);
312 usbhid->urbout->dev = hid_to_usb_dev(hid);
313 memcpy(usbhid->outbuf, raw_report, usbhid->urbout->transfer_buffer_length);
314 kfree(raw_report);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Oliver Neukum0361a282008-12-17 15:38:03 +0100316 dbg_hid("submitting out urb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Oliver Neukum0361a282008-12-17 15:38:03 +0100318 if (usb_submit_urb(usbhid->urbout, GFP_ATOMIC)) {
319 err_hid("usb_submit_urb(out) failed");
320 return -1;
321 }
322 } else {
323 /*
324 * queue work to wake up the device.
325 * as the work queue is freezeable, this is safe
326 * with respect to STD and STR
327 */
328 queue_work(resumption_waker, &usbhid->restart_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
330
331 return 0;
332}
333
334static int hid_submit_ctrl(struct hid_device *hid)
335{
336 struct hid_report *report;
337 unsigned char dir;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200338 char *raw_report;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 int len;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100340 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100342 report = usbhid->ctrl[usbhid->ctrltail].report;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200343 raw_report = usbhid->ctrl[usbhid->ctrltail].raw_report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100344 dir = usbhid->ctrl[usbhid->ctrltail].dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Oliver Neukum0361a282008-12-17 15:38:03 +0100346 if (!test_bit(HID_REPORTED_IDLE, &usbhid->iofl)) {
347 len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
348 if (dir == USB_DIR_OUT) {
349 usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
350 usbhid->urbctrl->transfer_buffer_length = len;
351 memcpy(usbhid->ctrlbuf, raw_report, len);
352 kfree(raw_report);
353 } else {
354 int maxpacket, padlen;
355
356 usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0);
357 maxpacket = usb_maxpacket(hid_to_usb_dev(hid), usbhid->urbctrl->pipe, 0);
358 if (maxpacket > 0) {
359 padlen = DIV_ROUND_UP(len, maxpacket);
360 padlen *= maxpacket;
361 if (padlen > usbhid->bufsize)
362 padlen = usbhid->bufsize;
363 } else
364 padlen = 0;
365 usbhid->urbctrl->transfer_buffer_length = padlen;
366 }
367 usbhid->urbctrl->dev = hid_to_usb_dev(hid);
368
369 usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
370 usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT : HID_REQ_GET_REPORT;
371 usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) | report->id);
372 usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum);
373 usbhid->cr->wLength = cpu_to_le16(len);
374
375 dbg_hid("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u\n",
376 usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" : "Get_Report",
377 usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength);
378
379 if (usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC)) {
380 err_hid("usb_submit_urb(ctrl) failed");
381 return -1;
382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 } else {
Oliver Neukum0361a282008-12-17 15:38:03 +0100384 /*
385 * queue work to wake up the device.
386 * as the work queue is freezeable, this is safe
387 * with respect to STD and STR
388 */
389 queue_work(resumption_waker, &usbhid->restart_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391
392 return 0;
393}
394
395/*
396 * Output interrupt completion handler.
397 */
398
David Howells7d12e782006-10-05 14:55:46 +0100399static void hid_irq_out(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100402 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 unsigned long flags;
404 int unplug = 0;
405
406 switch (urb->status) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200407 case 0: /* success */
408 break;
409 case -ESHUTDOWN: /* unplug */
410 unplug = 1;
411 case -EILSEQ: /* protocol error or unplug */
412 case -EPROTO: /* protocol error or unplug */
413 case -ECONNRESET: /* unlink */
414 case -ENOENT:
415 break;
416 default: /* error */
From: Greg Kroah-Hartman7d89fe12008-10-12 00:25:51 +0200417 dev_warn(&urb->dev->dev, "output irq status %d "
418 "received\n", urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420
Oliver Neukum0361a282008-12-17 15:38:03 +0100421 spin_lock_irqsave(&usbhid->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 if (unplug)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100424 usbhid->outtail = usbhid->outhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 else
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100426 usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100428 if (usbhid->outhead != usbhid->outtail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (hid_submit_out(hid)) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100430 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100431 wake_up(&usbhid->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100433 spin_unlock_irqrestore(&usbhid->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return;
435 }
436
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100437 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +0100438 spin_unlock_irqrestore(&usbhid->lock, flags);
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100439 wake_up(&usbhid->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
442/*
443 * Control pipe completion handler.
444 */
445
David Howells7d12e782006-10-05 14:55:46 +0100446static void hid_ctrl(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
448 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100449 struct usbhid_device *usbhid = hid->driver_data;
Oliver Neukum0361a282008-12-17 15:38:03 +0100450 int unplug = 0, status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Oliver Neukum0361a282008-12-17 15:38:03 +0100452 spin_lock(&usbhid->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Oliver Neukum0361a282008-12-17 15:38:03 +0100454 switch (status) {
Jiri Slaby880d29f2008-06-18 23:55:41 +0200455 case 0: /* success */
456 if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
457 hid_input_report(urb->context,
458 usbhid->ctrl[usbhid->ctrltail].report->type,
459 urb->transfer_buffer, urb->actual_length, 0);
460 break;
461 case -ESHUTDOWN: /* unplug */
462 unplug = 1;
463 case -EILSEQ: /* protocol error or unplug */
464 case -EPROTO: /* protocol error or unplug */
465 case -ECONNRESET: /* unlink */
466 case -ENOENT:
467 case -EPIPE: /* report not available */
468 break;
469 default: /* error */
From: Greg Kroah-Hartman7d89fe12008-10-12 00:25:51 +0200470 dev_warn(&urb->dev->dev, "ctrl urb status %d "
Oliver Neukum0361a282008-12-17 15:38:03 +0100471 "received\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473
474 if (unplug)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100475 usbhid->ctrltail = usbhid->ctrlhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 else
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100477 usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100479 if (usbhid->ctrlhead != usbhid->ctrltail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (hid_submit_ctrl(hid)) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100481 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100482 wake_up(&usbhid->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100484 spin_unlock(&usbhid->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return;
486 }
487
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100488 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +0100489 spin_unlock(&usbhid->lock);
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100490 wake_up(&usbhid->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
Oliver Neukum0361a282008-12-17 15:38:03 +0100493void __usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 int head;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100496 struct usbhid_device *usbhid = hid->driver_data;
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200497 int len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
500 return;
501
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100502 if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100503 if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
From: Greg Kroah-Hartman7d89fe12008-10-12 00:25:51 +0200504 dev_warn(&hid->dev, "output queue full\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return;
506 }
507
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200508 usbhid->out[usbhid->outhead].raw_report = kmalloc(len, GFP_ATOMIC);
509 if (!usbhid->out[usbhid->outhead].raw_report) {
Greg Kroah-Hartman46fcaec2008-10-15 11:30:07 -0700510 dev_warn(&hid->dev, "output queueing failed\n");
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200511 return;
512 }
513 hid_output_report(report, usbhid->out[usbhid->outhead].raw_report);
514 usbhid->out[usbhid->outhead].report = report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100515 usbhid->outhead = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100517 if (!test_and_set_bit(HID_OUT_RUNNING, &usbhid->iofl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (hid_submit_out(hid))
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100519 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return;
521 }
522
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100523 if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
From: Greg Kroah-Hartman7d89fe12008-10-12 00:25:51 +0200524 dev_warn(&hid->dev, "control queue full\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return;
526 }
527
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200528 if (dir == USB_DIR_OUT) {
529 usbhid->ctrl[usbhid->ctrlhead].raw_report = kmalloc(len, GFP_ATOMIC);
530 if (!usbhid->ctrl[usbhid->ctrlhead].raw_report) {
Greg Kroah-Hartman46fcaec2008-10-15 11:30:07 -0700531 dev_warn(&hid->dev, "control queueing failed\n");
Anssi Hannulaf129ea62008-10-04 14:44:06 +0200532 return;
533 }
534 hid_output_report(report, usbhid->ctrl[usbhid->ctrlhead].raw_report);
535 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100536 usbhid->ctrl[usbhid->ctrlhead].report = report;
537 usbhid->ctrl[usbhid->ctrlhead].dir = dir;
538 usbhid->ctrlhead = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100540 if (!test_and_set_bit(HID_CTRL_RUNNING, &usbhid->iofl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (hid_submit_ctrl(hid))
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100542 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +0100543}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Oliver Neukum0361a282008-12-17 15:38:03 +0100545void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
546{
547 struct usbhid_device *usbhid = hid->driver_data;
548 unsigned long flags;
549
550 spin_lock_irqsave(&usbhid->lock, flags);
551 __usbhid_submit_report(hid, report, dir);
552 spin_unlock_irqrestore(&usbhid->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
Jiri Slaby606bd0a2008-07-04 23:06:45 +0200554EXPORT_SYMBOL_GPL(usbhid_submit_report);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Jiri Kosina229695e2006-12-08 18:40:53 +0100556static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
Jiri Kosinadde58452006-12-08 18:40:44 +0100557{
Dmitry Torokhove0712982007-05-09 10:17:31 +0200558 struct hid_device *hid = input_get_drvdata(dev);
Oliver Neukum0361a282008-12-17 15:38:03 +0100559 struct usbhid_device *usbhid = hid->driver_data;
Jiri Kosinadde58452006-12-08 18:40:44 +0100560 struct hid_field *field;
Oliver Neukum0361a282008-12-17 15:38:03 +0100561 unsigned long flags;
Jiri Kosinadde58452006-12-08 18:40:44 +0100562 int offset;
563
564 if (type == EV_FF)
565 return input_ff_event(dev, type, code, value);
566
567 if (type != EV_LED)
568 return -1;
569
570 if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
From: Greg Kroah-Hartman7d89fe12008-10-12 00:25:51 +0200571 dev_warn(&dev->dev, "event field not found\n");
Jiri Kosinadde58452006-12-08 18:40:44 +0100572 return -1;
573 }
574
575 hid_set_field(field, offset, value);
Oliver Neukum0361a282008-12-17 15:38:03 +0100576 if (value) {
577 spin_lock_irqsave(&usbhid->lock, flags);
578 usbhid->ledcount++;
579 spin_unlock_irqrestore(&usbhid->lock, flags);
580 } else {
581 spin_lock_irqsave(&usbhid->lock, flags);
582 usbhid->ledcount--;
583 spin_unlock_irqrestore(&usbhid->lock, flags);
584 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100585 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
Jiri Kosinadde58452006-12-08 18:40:44 +0100586
587 return 0;
588}
589
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100590int usbhid_wait_io(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100592 struct usbhid_device *usbhid = hid->driver_data;
593
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100594 if (!wait_event_timeout(usbhid->wait,
595 (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
596 !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 10*HZ)) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200598 dbg_hid("timeout waiting for ctrl or out queue to clear\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return -1;
600 }
601
602 return 0;
603}
604
Vojtech Pavlik854561b2005-05-29 02:28:00 -0500605static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
606{
Vojtech Pavlik71387bd2005-05-29 02:28:14 -0500607 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
Vojtech Pavlik854561b2005-05-29 02:28:00 -0500608 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
609 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
610}
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
613 unsigned char type, void *buf, int size)
614{
615 int result, retries = 4;
616
Jiri Kosina43c7bf02007-01-26 12:58:24 +0100617 memset(buf, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 do {
620 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
621 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
622 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
623 retries--;
624 } while (result < size && retries);
625 return result;
626}
627
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100628int usbhid_open(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Oliver Neukum933e3182007-07-11 14:48:58 +0200630 struct usbhid_device *usbhid = hid->driver_data;
631 int res;
632
Oliver Neukum0361a282008-12-17 15:38:03 +0100633 mutex_lock(&hid_open_mut);
Oliver Neukum933e3182007-07-11 14:48:58 +0200634 if (!hid->open++) {
635 res = usb_autopm_get_interface(usbhid->intf);
Oliver Neukum0361a282008-12-17 15:38:03 +0100636 /* the device must be awake to reliable request remote wakeup */
Oliver Neukum933e3182007-07-11 14:48:58 +0200637 if (res < 0) {
638 hid->open--;
Oliver Neukum0361a282008-12-17 15:38:03 +0100639 mutex_unlock(&hid_open_mut);
Oliver Neukum933e3182007-07-11 14:48:58 +0200640 return -EIO;
641 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100642 usbhid->intf->needs_remote_wakeup = 1;
643 if (hid_start_in(hid))
644 hid_io_error(hid);
645
646 usb_autopm_put_interface(usbhid->intf);
Oliver Neukum933e3182007-07-11 14:48:58 +0200647 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100648 mutex_unlock(&hid_open_mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return 0;
650}
651
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100652void usbhid_close(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100654 struct usbhid_device *usbhid = hid->driver_data;
655
Oliver Neukum0361a282008-12-17 15:38:03 +0100656 mutex_lock(&hid_open_mut);
657
658 /* protecting hid->open to make sure we don't restart
659 * data acquistion due to a resumption we no longer
660 * care about
661 */
662 spin_lock_irq(&usbhid->lock);
Oliver Neukum933e3182007-07-11 14:48:58 +0200663 if (!--hid->open) {
Oliver Neukum0361a282008-12-17 15:38:03 +0100664 spin_unlock_irq(&usbhid->lock);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100665 usb_kill_urb(usbhid->urbin);
Oliver Neukum0361a282008-12-17 15:38:03 +0100666 flush_scheduled_work();
667 usbhid->intf->needs_remote_wakeup = 0;
668 } else {
669 spin_unlock_irq(&usbhid->lock);
Oliver Neukum933e3182007-07-11 14:48:58 +0200670 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100671 mutex_unlock(&hid_open_mut);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
674/*
675 * Initialize all reports
676 */
677
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100678void usbhid_init_reports(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 struct hid_report *report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100681 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 int err, ret;
683
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500684 list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100685 usbhid_submit_report(hid, report, USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100688 usbhid_submit_report(hid, report, USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 err = 0;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100691 ret = usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 while (ret) {
693 err |= ret;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100694 if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
695 usb_kill_urb(usbhid->urbctrl);
696 if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
697 usb_kill_urb(usbhid->urbout);
698 ret = usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
700
701 if (err)
From: Greg Kroah-Hartman7d89fe12008-10-12 00:25:51 +0200702 dev_warn(&hid->dev, "timeout initializing reports\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500705/*
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200706 * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
707 */
708static int hid_find_field_early(struct hid_device *hid, unsigned int page,
709 unsigned int hid_code, struct hid_field **pfield)
710{
711 struct hid_report *report;
712 struct hid_field *field;
713 struct hid_usage *usage;
714 int i, j;
715
716 list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
717 for (i = 0; i < report->maxfield; i++) {
718 field = report->field[i];
719 for (j = 0; j < field->maxusage; j++) {
720 usage = &field->usage[j];
721 if ((usage->hid & HID_USAGE_PAGE) == page &&
722 (usage->hid & 0xFFFF) == hid_code) {
723 *pfield = field;
724 return j;
725 }
726 }
727 }
728 }
729 return -1;
730}
731
Jiri Slaby6edfa8d2008-06-27 20:41:02 +0200732void usbhid_set_leds(struct hid_device *hid)
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200733{
734 struct hid_field *field;
735 int offset;
736
737 if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
738 hid_set_field(field, offset, 0);
739 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
740 }
741}
Jiri Slaby6edfa8d2008-06-27 20:41:02 +0200742EXPORT_SYMBOL_GPL(usbhid_set_leds);
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200743
744/*
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500745 * Traverse the supplied list of reports and find the longest
746 */
Jiri Slaby282bfd42008-03-28 17:06:41 +0100747static void hid_find_max_report(struct hid_device *hid, unsigned int type,
748 unsigned int *max)
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500749{
750 struct hid_report *report;
Jiri Slaby282bfd42008-03-28 17:06:41 +0100751 unsigned int size;
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500752
753 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
Jiri Kosinaefc7ce12008-10-17 15:01:15 +0200754 size = ((report->size - 1) >> 3) + 1 + hid->report_enum[type].numbered;
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500755 if (*max < size)
756 *max = size;
757 }
758}
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
761{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100762 struct usbhid_device *usbhid = hid->driver_data;
763
Jiri Slaby898089d2008-11-24 16:20:06 +0100764 usbhid->inbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_KERNEL,
765 &usbhid->inbuf_dma);
766 usbhid->outbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_KERNEL,
767 &usbhid->outbuf_dma);
768 usbhid->cr = usb_buffer_alloc(dev, sizeof(*usbhid->cr), GFP_KERNEL,
769 &usbhid->cr_dma);
770 usbhid->ctrlbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_KERNEL,
771 &usbhid->ctrlbuf_dma);
772 if (!usbhid->inbuf || !usbhid->outbuf || !usbhid->cr ||
773 !usbhid->ctrlbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return -1;
775
776 return 0;
777}
778
Jiri Kosinaefc493f2007-05-14 09:54:30 +0200779static int usbhid_output_raw_report(struct hid_device *hid, __u8 *buf, size_t count)
780{
781 struct usbhid_device *usbhid = hid->driver_data;
782 struct usb_device *dev = hid_to_usb_dev(hid);
783 struct usb_interface *intf = usbhid->intf;
784 struct usb_host_interface *interface = intf->cur_altsetting;
785 int ret;
786
787 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
788 HID_REQ_SET_REPORT,
789 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
Al Viro01d7b362008-04-28 07:00:05 +0100790 ((HID_OUTPUT_REPORT + 1) << 8) | *buf,
Jiri Kosinaefc493f2007-05-14 09:54:30 +0200791 interface->desc.bInterfaceNumber, buf + 1, count - 1,
792 USB_CTRL_SET_TIMEOUT);
793
794 /* count also the report id */
795 if (ret > 0)
796 ret++;
797
798 return ret;
799}
800
Oliver Neukum0361a282008-12-17 15:38:03 +0100801static void usbhid_restart_queues(struct usbhid_device *usbhid)
802{
803 if (usbhid->urbout)
804 usbhid_restart_out_queue(usbhid);
805 usbhid_restart_ctrl_queue(usbhid);
806}
807
808static void __usbhid_restart_queues(struct work_struct *work)
809{
810 struct usbhid_device *usbhid =
811 container_of(work, struct usbhid_device, restart_work);
812 int r;
813
814 r = usb_autopm_get_interface(usbhid->intf);
815 if (r < 0)
816 return;
817 usb_autopm_put_interface(usbhid->intf);
818}
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
821{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100822 struct usbhid_device *usbhid = hid->driver_data;
823
Dmitry Torokhov6675c5b2007-05-03 01:04:52 -0400824 usb_buffer_free(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
825 usb_buffer_free(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
826 usb_buffer_free(dev, sizeof(*(usbhid->cr)), usbhid->cr, usbhid->cr_dma);
827 usb_buffer_free(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
Jiri Slabyc500c972008-05-16 11:49:16 +0200830static int usbhid_parse(struct hid_device *hid)
831{
832 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 struct usb_host_interface *interface = intf->cur_altsetting;
834 struct usb_device *dev = interface_to_usbdev (intf);
835 struct hid_descriptor *hdesc;
Paul Walmsley2eb5dc32007-04-19 13:27:04 +0200836 u32 quirks = 0;
Jiri Slabyc500c972008-05-16 11:49:16 +0200837 unsigned int rsize = 0;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500838 char *rdesc;
Jiri Slabyc500c972008-05-16 11:49:16 +0200839 int ret, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Paul Walmsley2eb5dc32007-04-19 13:27:04 +0200841 quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
842 le16_to_cpu(dev->descriptor.idProduct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Alan Stern0f28b552006-05-15 14:49:04 -0400844 /* Many keyboards and mice don't like to be polled for reports,
845 * so we will always set the HID_QUIRK_NOGET flag for them. */
846 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
847 if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
848 interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
849 quirks |= HID_QUIRK_NOGET;
850 }
851
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500852 if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
853 (!interface->desc.bNumEndpoints ||
854 usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200855 dbg_hid("class descriptor not present\n");
Jiri Slabyc500c972008-05-16 11:49:16 +0200856 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
858
Jiri Slabyc500c972008-05-16 11:49:16 +0200859 hid->version = le16_to_cpu(hdesc->bcdHID);
860 hid->country = hdesc->bCountryCode;
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 for (n = 0; n < hdesc->bNumDescriptors; n++)
863 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
864 rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
865
866 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200867 dbg_hid("weird size of report descriptor (%u)\n", rsize);
Jiri Slabyc500c972008-05-16 11:49:16 +0200868 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
870
871 if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200872 dbg_hid("couldn't allocate rdesc memory\n");
Jiri Slabyc500c972008-05-16 11:49:16 +0200873 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
875
Vojtech Pavlik854561b2005-05-29 02:28:00 -0500876 hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
877
Jiri Slabyc500c972008-05-16 11:49:16 +0200878 ret = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber,
879 HID_DT_REPORT, rdesc, rsize);
880 if (ret < 0) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200881 dbg_hid("reading report descriptor failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 kfree(rdesc);
Jiri Slabyc500c972008-05-16 11:49:16 +0200883 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
885
Jiri Kosina58037eb2007-05-30 15:07:13 +0200886 dbg_hid("report descriptor (size %u, read %d) = ", rsize, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 for (n = 0; n < rsize; n++)
Jiri Kosina58037eb2007-05-30 15:07:13 +0200888 dbg_hid_line(" %02x", (unsigned char) rdesc[n]);
889 dbg_hid_line("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Jiri Slabyc500c972008-05-16 11:49:16 +0200891 ret = hid_parse_report(hid, rdesc, rsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 kfree(rdesc);
Jiri Slabyc500c972008-05-16 11:49:16 +0200893 if (ret) {
894 dbg_hid("parsing report descriptor failed\n");
895 goto err;
896 }
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 hid->quirks = quirks;
899
Jiri Slabyc500c972008-05-16 11:49:16 +0200900 return 0;
901err:
902 return ret;
903}
904
905static int usbhid_start(struct hid_device *hid)
906{
907 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
908 struct usb_host_interface *interface = intf->cur_altsetting;
909 struct usb_device *dev = interface_to_usbdev(intf);
Jiri Slaby3d5afd32008-10-27 12:16:15 +0100910 struct usbhid_device *usbhid = hid->driver_data;
Jiri Slabyc500c972008-05-16 11:49:16 +0200911 unsigned int n, insize = 0;
912 int ret;
913
Jiri Slabye3e14de2008-11-01 23:41:46 +0100914 clear_bit(HID_DISCONNECTED, &usbhid->iofl);
915
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100916 usbhid->bufsize = HID_MIN_BUFFER_SIZE;
917 hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
918 hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
919 hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
920
921 if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
922 usbhid->bufsize = HID_MAX_BUFFER_SIZE;
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500923
924 hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
925
926 if (insize > HID_MAX_BUFFER_SIZE)
927 insize = HID_MAX_BUFFER_SIZE;
928
Jiri Slabyc500c972008-05-16 11:49:16 +0200929 if (hid_alloc_buffers(dev, hid)) {
930 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 goto fail;
Pekka Sarnilaf345c372008-03-06 13:23:14 +0100932 }
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 for (n = 0; n < interface->desc.bNumEndpoints; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 struct usb_endpoint_descriptor *endpoint;
936 int pipe;
937 int interval;
938
939 endpoint = &interface->endpoint[n].desc;
Jiri Slaby581a2732008-11-24 16:20:08 +0100940 if (!usb_endpoint_xfer_int(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 continue;
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 interval = endpoint->bInterval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Pekka Sarnilaf345c372008-03-06 13:23:14 +0100945 /* Some vendors give fullspeed interval on highspeed devides */
Jiri Slabyc500c972008-05-16 11:49:16 +0200946 if (hid->quirks & HID_QUIRK_FULLSPEED_INTERVAL &&
Pekka Sarnilaf345c372008-03-06 13:23:14 +0100947 dev->speed == USB_SPEED_HIGH) {
948 interval = fls(endpoint->bInterval*8);
949 printk(KERN_INFO "%s: Fixing fullspeed to highspeed interval: %d -> %d\n",
950 hid->name, endpoint->bInterval, interval);
951 }
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 /* Change the polling interval of mice. */
954 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
955 interval = hid_mousepoll_interval;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500956
Jiri Slabyc500c972008-05-16 11:49:16 +0200957 ret = -ENOMEM;
Luiz Fernando N. Capitulino0f12aa02006-10-26 13:02:51 -0300958 if (usb_endpoint_dir_in(endpoint)) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100959 if (usbhid->urbin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 continue;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100961 if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 goto fail;
963 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100964 usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 hid_irq_in, hid, interval);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100966 usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
967 usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 } else {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100969 if (usbhid->urbout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 continue;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100971 if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 goto fail;
973 pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100974 usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 hid_irq_out, hid, interval);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100976 usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
977 usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
979 }
980
Jiri Slaby1d1bdd22008-03-19 21:55:04 +0100981 init_waitqueue_head(&usbhid->wait);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100982 INIT_WORK(&usbhid->reset_work, hid_reset);
Oliver Neukum0361a282008-12-17 15:38:03 +0100983 INIT_WORK(&usbhid->restart_work, __usbhid_restart_queues);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100984 setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
Alan Sternaef4e262006-01-31 12:58:38 -0500985
Oliver Neukum0361a282008-12-17 15:38:03 +0100986 spin_lock_init(&usbhid->lock);
987 spin_lock_init(&usbhid->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100989 usbhid->intf = intf;
990 usbhid->ifnum = interface->desc.bInterfaceNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100992 usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
Jiri Slabyc500c972008-05-16 11:49:16 +0200993 if (!usbhid->urbctrl) {
994 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 goto fail;
Jiri Slabyc500c972008-05-16 11:49:16 +0200996 }
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500997
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100998 usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
999 usbhid->ctrlbuf, 1, hid_ctrl, hid);
1000 usbhid->urbctrl->setup_dma = usbhid->cr_dma;
1001 usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
1002 usbhid->urbctrl->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP);
Jiri Slabyc500c972008-05-16 11:49:16 +02001003
Jiri Slaby93c10132008-06-27 00:04:24 +02001004 usbhid_init_reports(hid);
1005 hid_dump_device(hid);
1006
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001007 set_bit(HID_STARTED, &usbhid->iofl);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001008
Alan Stern08ef08e2008-10-30 23:58:51 +01001009 /* Some keyboards don't work until their LEDs have been set.
1010 * Since BIOSes do set the LEDs, it must be safe for any device
1011 * that supports the keyboard boot protocol.
1012 */
1013 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT &&
1014 interface->desc.bInterfaceProtocol ==
1015 USB_INTERFACE_PROTOCOL_KEYBOARD)
1016 usbhid_set_leds(hid);
1017
Jiri Slabyc500c972008-05-16 11:49:16 +02001018 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020fail:
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001021 usb_free_urb(usbhid->urbin);
1022 usb_free_urb(usbhid->urbout);
1023 usb_free_urb(usbhid->urbctrl);
Jiri Slabye3e14de2008-11-01 23:41:46 +01001024 usbhid->urbin = NULL;
1025 usbhid->urbout = NULL;
1026 usbhid->urbctrl = NULL;
Jiri Kosina22f675f2007-08-01 12:32:27 +02001027 hid_free_buffers(dev, hid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001028 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
Jiri Slabyc500c972008-05-16 11:49:16 +02001031static void usbhid_stop(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Jiri Slabyc500c972008-05-16 11:49:16 +02001033 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Jiri Slabyc500c972008-05-16 11:49:16 +02001035 if (WARN_ON(!usbhid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return;
1037
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001038 clear_bit(HID_STARTED, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +01001039 spin_lock_irq(&usbhid->lock); /* Sync with error handler */
Oliver Neukum69626f22008-03-31 16:27:30 +02001040 set_bit(HID_DISCONNECTED, &usbhid->iofl);
Oliver Neukum0361a282008-12-17 15:38:03 +01001041 spin_unlock_irq(&usbhid->lock);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001042 usb_kill_urb(usbhid->urbin);
1043 usb_kill_urb(usbhid->urbout);
1044 usb_kill_urb(usbhid->urbctrl);
1045
Oliver Neukum0361a282008-12-17 15:38:03 +01001046 hid_cancel_delayed_stuff(usbhid);
Alan Sternaef4e262006-01-31 12:58:38 -05001047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (hid->claimed & HID_CLAIMED_INPUT)
1049 hidinput_disconnect(hid);
1050 if (hid->claimed & HID_CLAIMED_HIDDEV)
1051 hiddev_disconnect(hid);
Jiri Kosina86166b72007-05-14 09:57:40 +02001052 if (hid->claimed & HID_CLAIMED_HIDRAW)
1053 hidraw_disconnect(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Jiri Slabyc500c972008-05-16 11:49:16 +02001055 hid->claimed = 0;
1056
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001057 usb_free_urb(usbhid->urbin);
1058 usb_free_urb(usbhid->urbctrl);
1059 usb_free_urb(usbhid->urbout);
Jiri Slabye3e14de2008-11-01 23:41:46 +01001060 usbhid->urbin = NULL; /* don't mess up next start */
1061 usbhid->urbctrl = NULL;
1062 usbhid->urbout = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Anssi Hannulabe820972007-01-19 19:28:17 +02001064 hid_free_buffers(hid_to_usb_dev(hid), hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
Oliver Neukum0361a282008-12-17 15:38:03 +01001067static int usbhid_power(struct hid_device *hid, int lvl)
1068{
1069 int r = 0;
1070
1071 switch (lvl) {
1072 case PM_HINT_FULLON:
1073 r = usbhid_get_power(hid);
1074 break;
1075 case PM_HINT_NORMAL:
1076 usbhid_put_power(hid);
1077 break;
1078 }
1079 return r;
1080}
1081
Jiri Slabyc500c972008-05-16 11:49:16 +02001082static struct hid_ll_driver usb_hid_driver = {
1083 .parse = usbhid_parse,
1084 .start = usbhid_start,
1085 .stop = usbhid_stop,
1086 .open = usbhid_open,
1087 .close = usbhid_close,
Oliver Neukum0361a282008-12-17 15:38:03 +01001088 .power = usbhid_power,
Jiri Slabyc500c972008-05-16 11:49:16 +02001089 .hidinput_input_event = usb_hidinput_input_event,
1090};
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id)
1093{
Jiri Slaby131d3a7a2008-11-14 12:03:47 +01001094 struct usb_host_interface *interface = intf->cur_altsetting;
Jiri Slabyc500c972008-05-16 11:49:16 +02001095 struct usb_device *dev = interface_to_usbdev(intf);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001096 struct usbhid_device *usbhid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 struct hid_device *hid;
Jiri Slaby131d3a7a2008-11-14 12:03:47 +01001098 unsigned int n, has_in = 0;
Jiri Slabyc500c972008-05-16 11:49:16 +02001099 size_t len;
1100 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Jiri Kosina58037eb2007-05-30 15:07:13 +02001102 dbg_hid("HID probe called for ifnum %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 intf->altsetting->desc.bInterfaceNumber);
1104
Jiri Slaby131d3a7a2008-11-14 12:03:47 +01001105 for (n = 0; n < interface->desc.bNumEndpoints; n++)
1106 if (usb_endpoint_is_int_in(&interface->endpoint[n].desc))
1107 has_in++;
1108 if (!has_in) {
1109 dev_err(&intf->dev, "couldn't find an input interrupt "
1110 "endpoint\n");
1111 return -ENODEV;
1112 }
1113
Jiri Slabyc500c972008-05-16 11:49:16 +02001114 hid = hid_allocate_device();
1115 if (IS_ERR(hid))
1116 return PTR_ERR(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 usb_set_intfdata(intf, hid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001119 hid->ll_driver = &usb_hid_driver;
1120 hid->hid_output_raw_report = usbhid_output_raw_report;
Jiri Slaby76483cf2008-09-18 12:23:33 +02001121 hid->ff_init = hid_pidff_init;
Jiri Slabyc500c972008-05-16 11:49:16 +02001122#ifdef CONFIG_USB_HIDDEV
Jiri Slaby93c10132008-06-27 00:04:24 +02001123 hid->hiddev_connect = hiddev_connect;
Jiri Slabyc500c972008-05-16 11:49:16 +02001124 hid->hiddev_hid_event = hiddev_hid_event;
1125 hid->hiddev_report_event = hiddev_report_event;
1126#endif
1127 hid->dev.parent = &intf->dev;
1128 hid->bus = BUS_USB;
1129 hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
1130 hid->product = le16_to_cpu(dev->descriptor.idProduct);
1131 hid->name[0] = 0;
Jiri Slabya73a6372008-10-22 14:45:11 +02001132 if (intf->cur_altsetting->desc.bInterfaceProtocol ==
1133 USB_INTERFACE_PROTOCOL_MOUSE)
1134 hid->type = HID_TYPE_USBMOUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Jiri Slabyc500c972008-05-16 11:49:16 +02001136 if (dev->manufacturer)
1137 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
1138
1139 if (dev->product) {
1140 if (dev->manufacturer)
1141 strlcat(hid->name, " ", sizeof(hid->name));
1142 strlcat(hid->name, dev->product, sizeof(hid->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
1144
Jiri Slabyc500c972008-05-16 11:49:16 +02001145 if (!strlen(hid->name))
1146 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
1147 le16_to_cpu(dev->descriptor.idVendor),
1148 le16_to_cpu(dev->descriptor.idProduct));
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001149
Jiri Slabyc500c972008-05-16 11:49:16 +02001150 usb_make_path(dev, hid->phys, sizeof(hid->phys));
1151 strlcat(hid->phys, "/input", sizeof(hid->phys));
1152 len = strlen(hid->phys);
1153 if (len < sizeof(hid->phys) - 1)
1154 snprintf(hid->phys + len, sizeof(hid->phys) - len,
1155 "%d", intf->altsetting[0].desc.bInterfaceNumber);
Geoff Levand4a1a4d82007-01-15 20:11:52 -08001156
Jiri Slabyc500c972008-05-16 11:49:16 +02001157 if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
1158 hid->uniq[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001160 usbhid = kzalloc(sizeof(*usbhid), GFP_KERNEL);
1161 if (usbhid == NULL) {
1162 ret = -ENOMEM;
1163 goto err;
1164 }
1165
1166 hid->driver_data = usbhid;
1167 usbhid->hid = hid;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001168
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001169 ret = hid_add_device(hid);
1170 if (ret) {
Jiri Slabyd458a9d2008-05-16 11:49:20 +02001171 if (ret != -ENODEV)
1172 dev_err(&intf->dev, "can't add hid device: %d\n", ret);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001173 goto err_free;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001174 }
Jiri Slabyc500c972008-05-16 11:49:16 +02001175
1176 return 0;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001177err_free:
1178 kfree(usbhid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001179err:
1180 hid_destroy_device(hid);
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001181 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
1183
Jiri Slabyc500c972008-05-16 11:49:16 +02001184static void hid_disconnect(struct usb_interface *intf)
1185{
1186 struct hid_device *hid = usb_get_intfdata(intf);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001187 struct usbhid_device *usbhid;
Jiri Slabyc500c972008-05-16 11:49:16 +02001188
1189 if (WARN_ON(!hid))
1190 return;
1191
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001192 usbhid = hid->driver_data;
Jiri Slabyc500c972008-05-16 11:49:16 +02001193 hid_destroy_device(hid);
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001194 kfree(usbhid);
Jiri Slabyc500c972008-05-16 11:49:16 +02001195}
1196
Oliver Neukum0361a282008-12-17 15:38:03 +01001197static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid)
1198{
1199 del_timer_sync(&usbhid->io_retry);
1200 cancel_work_sync(&usbhid->restart_work);
1201 cancel_work_sync(&usbhid->reset_work);
1202}
1203
1204static void hid_cease_io(struct usbhid_device *usbhid)
1205{
1206 del_timer(&usbhid->io_retry);
1207 usb_kill_urb(usbhid->urbin);
1208 usb_kill_urb(usbhid->urbctrl);
1209 usb_kill_urb(usbhid->urbout);
1210 flush_scheduled_work();
1211}
1212
Jiri Kosina0f6f1402009-01-19 09:17:18 +01001213#ifdef CONFIG_PM
David Brownell27d72e82005-04-18 17:39:22 -07001214static int hid_suspend(struct usb_interface *intf, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Oliver Neukum0361a282008-12-17 15:38:03 +01001216 struct hid_device *hid = usb_get_intfdata(intf);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001217 struct usbhid_device *usbhid = hid->driver_data;
Oliver Neukum0361a282008-12-17 15:38:03 +01001218 struct usb_device *udev = interface_to_usbdev(intf);
1219 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Oliver Neukum0361a282008-12-17 15:38:03 +01001221 if (udev->auto_pm) {
1222 spin_lock_irq(&usbhid->lock); /* Sync with error handler */
1223 if (!test_bit(HID_RESET_PENDING, &usbhid->iofl)
1224 && !test_bit(HID_CLEAR_HALT, &usbhid->iofl)
1225 && !test_bit(HID_OUT_RUNNING, &usbhid->iofl)
1226 && !test_bit(HID_CTRL_RUNNING, &usbhid->iofl)
1227 && !test_bit(HID_KEYS_PRESSED, &usbhid->iofl)
1228 && (!usbhid->ledcount || ignoreled))
1229 {
1230 set_bit(HID_REPORTED_IDLE, &usbhid->iofl);
1231 spin_unlock_irq(&usbhid->lock);
1232 } else {
1233 usbhid_mark_busy(usbhid);
1234 spin_unlock_irq(&usbhid->lock);
1235 return -EBUSY;
1236 }
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001237
Oliver Neukum0361a282008-12-17 15:38:03 +01001238 } else {
1239 spin_lock_irq(&usbhid->lock);
1240 set_bit(HID_REPORTED_IDLE, &usbhid->iofl);
1241 spin_unlock_irq(&usbhid->lock);
1242 if (usbhid_wait_io(hid) < 0)
1243 return -EIO;
1244 }
1245
1246 if (!ignoreled && udev->auto_pm) {
1247 spin_lock_irq(&usbhid->lock);
1248 if (test_bit(HID_LED_ON, &usbhid->iofl)) {
1249 spin_unlock_irq(&usbhid->lock);
1250 usbhid_mark_busy(usbhid);
1251 return -EBUSY;
1252 }
1253 spin_unlock_irq(&usbhid->lock);
1254 }
1255
1256 hid_cancel_delayed_stuff(usbhid);
1257 hid_cease_io(usbhid);
1258
1259 if (udev->auto_pm && test_bit(HID_KEYS_PRESSED, &usbhid->iofl)) {
1260 /* lost race against keypresses */
1261 status = hid_start_in(hid);
1262 if (status < 0)
1263 hid_io_error(hid);
1264 usbhid_mark_busy(usbhid);
1265 return -EBUSY;
1266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 dev_dbg(&intf->dev, "suspend\n");
1268 return 0;
1269}
1270
1271static int hid_resume(struct usb_interface *intf)
1272{
1273 struct hid_device *hid = usb_get_intfdata (intf);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001274 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 int status;
1276
Jiri Slabyfde5be32008-11-23 12:03:20 +01001277 if (!test_bit(HID_STARTED, &usbhid->iofl))
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001278 return 0;
Jiri Slaby3d5afd32008-10-27 12:16:15 +01001279
Oliver Neukum0361a282008-12-17 15:38:03 +01001280 clear_bit(HID_REPORTED_IDLE, &usbhid->iofl);
1281 usbhid_mark_busy(usbhid);
1282
1283 if (test_bit(HID_CLEAR_HALT, &usbhid->iofl) ||
1284 test_bit(HID_RESET_PENDING, &usbhid->iofl))
1285 schedule_work(&usbhid->reset_work);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001286 usbhid->retry_delay = 0;
Alan Sternaef4e262006-01-31 12:58:38 -05001287 status = hid_start_in(hid);
Oliver Neukum0361a282008-12-17 15:38:03 +01001288 if (status < 0)
1289 hid_io_error(hid);
1290 usbhid_restart_queues(usbhid);
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 dev_dbg(&intf->dev, "resume status %d\n", status);
Oliver Neukum0361a282008-12-17 15:38:03 +01001293 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294}
1295
Jiri Kosina0f6f1402009-01-19 09:17:18 +01001296#endif /* CONFIG_PM */
1297
Alan Sterndf9a1f42006-06-01 13:55:28 -04001298/* Treat USB reset pretty much the same as suspend/resume */
Alan Sternf07600c2007-05-30 15:38:16 -04001299static int hid_pre_reset(struct usb_interface *intf)
Alan Sterndf9a1f42006-06-01 13:55:28 -04001300{
Oliver Neukum0361a282008-12-17 15:38:03 +01001301 struct hid_device *hid = usb_get_intfdata(intf);
1302 struct usbhid_device *usbhid = hid->driver_data;
1303
1304 spin_lock_irq(&usbhid->lock);
1305 set_bit(HID_RESET_PENDING, &usbhid->iofl);
1306 spin_unlock_irq(&usbhid->lock);
1307 hid_cease_io(usbhid);
1308
Alan Sternf07600c2007-05-30 15:38:16 -04001309 return 0;
Alan Sterndf9a1f42006-06-01 13:55:28 -04001310}
1311
Alan Sternf07600c2007-05-30 15:38:16 -04001312/* Same routine used for post_reset and reset_resume */
1313static int hid_post_reset(struct usb_interface *intf)
Alan Sterndf9a1f42006-06-01 13:55:28 -04001314{
1315 struct usb_device *dev = interface_to_usbdev (intf);
Oliver Neukum0361a282008-12-17 15:38:03 +01001316 struct hid_device *hid = usb_get_intfdata(intf);
1317 struct usbhid_device *usbhid = hid->driver_data;
1318 int status;
1319
1320 spin_lock_irq(&usbhid->lock);
1321 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
1322 spin_unlock_irq(&usbhid->lock);
Alan Sterndf9a1f42006-06-01 13:55:28 -04001323 hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
1324 /* FIXME: Any more reinitialization needed? */
Oliver Neukum0361a282008-12-17 15:38:03 +01001325 status = hid_start_in(hid);
1326 if (status < 0)
1327 hid_io_error(hid);
1328 usbhid_restart_queues(usbhid);
Alan Sterndf9a1f42006-06-01 13:55:28 -04001329
Oliver Neukum0361a282008-12-17 15:38:03 +01001330 return 0;
1331}
1332
Oliver Neukum378a0ed2009-02-18 11:46:45 +01001333static int hid_reset_resume(struct usb_interface *intf)
1334{
1335 struct hid_device *hid = usb_get_intfdata(intf);
1336 struct usbhid_device *usbhid = hid->driver_data;
1337
1338 clear_bit(HID_REPORTED_IDLE, &usbhid->iofl);
1339 return hid_post_reset(intf);
1340}
1341
Oliver Neukum0361a282008-12-17 15:38:03 +01001342int usbhid_get_power(struct hid_device *hid)
1343{
1344 struct usbhid_device *usbhid = hid->driver_data;
1345
1346 return usb_autopm_get_interface(usbhid->intf);
1347}
1348
1349void usbhid_put_power(struct hid_device *hid)
1350{
1351 struct usbhid_device *usbhid = hid->driver_data;
1352
1353 usb_autopm_put_interface(usbhid->intf);
Alan Sterndf9a1f42006-06-01 13:55:28 -04001354}
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356static struct usb_device_id hid_usb_ids [] = {
1357 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1358 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
1359 { } /* Terminating entry */
1360};
1361
1362MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1363
1364static struct usb_driver hid_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 .name = "usbhid",
1366 .probe = hid_probe,
1367 .disconnect = hid_disconnect,
Jiri Kosina0f6f1402009-01-19 09:17:18 +01001368#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 .suspend = hid_suspend,
1370 .resume = hid_resume,
Oliver Neukum378a0ed2009-02-18 11:46:45 +01001371 .reset_resume = hid_reset_resume,
Jiri Kosina0f6f1402009-01-19 09:17:18 +01001372#endif
Alan Sterndf9a1f42006-06-01 13:55:28 -04001373 .pre_reset = hid_pre_reset,
1374 .post_reset = hid_post_reset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 .id_table = hid_usb_ids,
Oliver Neukum933e3182007-07-11 14:48:58 +02001376 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377};
1378
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001379static const struct hid_device_id hid_usb_table[] = {
1380 { HID_USB_DEVICE(HID_ANY_ID, HID_ANY_ID) },
1381 { }
1382};
1383
1384static struct hid_driver hid_usb_driver = {
1385 .name = "generic-usb",
1386 .id_table = hid_usb_table,
1387};
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389static int __init hid_init(void)
1390{
Oliver Neukum0361a282008-12-17 15:38:03 +01001391 int retval = -ENOMEM;
1392
1393 resumption_waker = create_freezeable_workqueue("usbhid_resumer");
1394 if (!resumption_waker)
1395 goto no_queue;
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001396 retval = hid_register_driver(&hid_usb_driver);
1397 if (retval)
1398 goto hid_register_fail;
Paul Walmsley876b9272007-04-19 14:56:12 +02001399 retval = usbhid_quirks_init(quirks_param);
1400 if (retval)
1401 goto usbhid_quirks_init_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 retval = hiddev_init();
1403 if (retval)
1404 goto hiddev_init_fail;
1405 retval = usb_register(&hid_driver);
1406 if (retval)
1407 goto usb_register_fail;
Greg Kroah-Hartmanddbe3242008-10-12 00:14:23 +02001408 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1409 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 return 0;
1412usb_register_fail:
1413 hiddev_exit();
1414hiddev_init_fail:
Paul Walmsley876b9272007-04-19 14:56:12 +02001415 usbhid_quirks_exit();
1416usbhid_quirks_init_fail:
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001417 hid_unregister_driver(&hid_usb_driver);
1418hid_register_fail:
Oliver Neukum0361a282008-12-17 15:38:03 +01001419 destroy_workqueue(resumption_waker);
1420no_queue:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 return retval;
1422}
1423
1424static void __exit hid_exit(void)
1425{
1426 usb_deregister(&hid_driver);
1427 hiddev_exit();
Paul Walmsley876b9272007-04-19 14:56:12 +02001428 usbhid_quirks_exit();
Jiri Slaby85cdaf52008-05-16 11:49:15 +02001429 hid_unregister_driver(&hid_usb_driver);
Oliver Neukum0361a282008-12-17 15:38:03 +01001430 destroy_workqueue(resumption_waker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431}
1432
1433module_init(hid_init);
1434module_exit(hid_exit);
1435
1436MODULE_AUTHOR(DRIVER_AUTHOR);
1437MODULE_DESCRIPTION(DRIVER_DESC);
1438MODULE_LICENSE(DRIVER_LICENSE);