]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/hid/usbhid/hid-core.c
USB HID: add support for dynamically-created quirks
[net-next-2.6.git] / drivers / hid / usbhid / hid-core.c
CommitLineData
1da177e4
LT
1/*
2 * USB HID support for Linux
3 *
4 * Copyright (c) 1999 Andreas Gal
bf0964dc
MH
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
b55fd23c 7 * Copyright (c) 2006-2007 Jiri Kosina
1da177e4
LT
8 */
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>
1da177e4
LT
21#include <linux/list.h>
22#include <linux/mm.h>
23#include <linux/smp_lock.h>
24#include <linux/spinlock.h>
25#include <asm/unaligned.h>
26#include <asm/byteorder.h>
27#include <linux/input.h>
28#include <linux/wait.h>
29
1da177e4
LT
30#include <linux/usb.h>
31
dde5845a 32#include <linux/hid.h>
1da177e4 33#include <linux/hiddev.h>
c080d89a 34#include <linux/hid-debug.h>
4916b3a5 35#include "usbhid.h"
1da177e4
LT
36
37/*
38 * Version Information
39 */
40
bf0964dc 41#define DRIVER_VERSION "v2.6"
f142b3a4 42#define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik, Jiri Kosina"
1da177e4
LT
43#define DRIVER_DESC "USB HID core driver"
44#define DRIVER_LICENSE "GPL"
45
46static char *hid_types[] = {"Device", "Pointer", "Mouse", "Device", "Joystick",
47 "Gamepad", "Keyboard", "Keypad", "Multi-Axis Controller"};
48/*
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
1da177e4 56/*
48b4554a 57 * Input submission and I/O error handler.
1da177e4
LT
58 */
59
48b4554a
JK
60static void hid_io_error(struct hid_device *hid);
61
62/* Start up the input URB */
63static int hid_start_in(struct hid_device *hid)
1da177e4 64{
1da177e4 65 unsigned long flags;
48b4554a
JK
66 int rc = 0;
67 struct usbhid_device *usbhid = hid->driver_data;
1da177e4 68
48b4554a
JK
69 spin_lock_irqsave(&usbhid->inlock, flags);
70 if (hid->open > 0 && !test_bit(HID_SUSPENDED, &usbhid->iofl) &&
71 !test_and_set_bit(HID_IN_RUNNING, &usbhid->iofl)) {
72 rc = usb_submit_urb(usbhid->urbin, GFP_ATOMIC);
73 if (rc != 0)
74 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
1da177e4 75 }
48b4554a
JK
76 spin_unlock_irqrestore(&usbhid->inlock, flags);
77 return rc;
1da177e4
LT
78}
79
48b4554a
JK
80/* I/O retry timer routine */
81static void hid_retry_timeout(unsigned long _hid)
1da177e4 82{
48b4554a 83 struct hid_device *hid = (struct hid_device *) _hid;
4916b3a5 84 struct usbhid_device *usbhid = hid->driver_data;
1da177e4 85
48b4554a
JK
86 dev_dbg(&usbhid->intf->dev, "retrying intr urb\n");
87 if (hid_start_in(hid))
88 hid_io_error(hid);
1da177e4
LT
89}
90
48b4554a
JK
91/* Workqueue routine to reset the device or clear a halt */
92static void hid_reset(struct work_struct *work)
1da177e4 93{
48b4554a
JK
94 struct usbhid_device *usbhid =
95 container_of(work, struct usbhid_device, reset_work);
96 struct hid_device *hid = usbhid->hid;
97 int rc_lock, rc = 0;
1da177e4 98
48b4554a
JK
99 if (test_bit(HID_CLEAR_HALT, &usbhid->iofl)) {
100 dev_dbg(&usbhid->intf->dev, "clear halt\n");
101 rc = usb_clear_halt(hid_to_usb_dev(hid), usbhid->urbin->pipe);
102 clear_bit(HID_CLEAR_HALT, &usbhid->iofl);
103 hid_start_in(hid);
104 }
1da177e4 105
48b4554a
JK
106 else if (test_bit(HID_RESET_PENDING, &usbhid->iofl)) {
107 dev_dbg(&usbhid->intf->dev, "resetting device\n");
108 rc = rc_lock = usb_lock_device_for_reset(hid_to_usb_dev(hid), usbhid->intf);
109 if (rc_lock >= 0) {
110 rc = usb_reset_composite_device(hid_to_usb_dev(hid), usbhid->intf);
111 if (rc_lock)
112 usb_unlock_device(hid_to_usb_dev(hid));
1da177e4 113 }
48b4554a 114 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
1da177e4
LT
115 }
116
48b4554a
JK
117 switch (rc) {
118 case 0:
119 if (!test_bit(HID_IN_RUNNING, &usbhid->iofl))
120 hid_io_error(hid);
121 break;
122 default:
123 err("can't reset device, %s-%s/input%d, status %d",
124 hid_to_usb_dev(hid)->bus->bus_name,
125 hid_to_usb_dev(hid)->devpath,
126 usbhid->ifnum, rc);
127 /* FALLTHROUGH */
128 case -EHOSTUNREACH:
129 case -ENODEV:
130 case -EINTR:
131 break;
1da177e4 132 }
1da177e4
LT
133}
134
48b4554a
JK
135/* Main I/O error handler */
136static void hid_io_error(struct hid_device *hid)
dde5845a 137{
48b4554a
JK
138 unsigned long flags;
139 struct usbhid_device *usbhid = hid->driver_data;
dde5845a 140
48b4554a 141 spin_lock_irqsave(&usbhid->inlock, flags);
dde5845a 142
48b4554a
JK
143 /* Stop when disconnected */
144 if (usb_get_intfdata(usbhid->intf) == NULL)
145 goto done;
dde5845a 146
5e2a55f2
AS
147 /* If it has been a while since the last error, we'll assume
148 * this a brand new error and reset the retry timeout. */
149 if (time_after(jiffies, usbhid->stop_retry + HZ/2))
150 usbhid->retry_delay = 0;
151
48b4554a
JK
152 /* When an error occurs, retry at increasing intervals */
153 if (usbhid->retry_delay == 0) {
154 usbhid->retry_delay = 13; /* Then 26, 52, 104, 104, ... */
155 usbhid->stop_retry = jiffies + msecs_to_jiffies(1000);
156 } else if (usbhid->retry_delay < 100)
157 usbhid->retry_delay *= 2;
dde5845a 158
48b4554a 159 if (time_after(jiffies, usbhid->stop_retry)) {
4916b3a5 160
48b4554a
JK
161 /* Retries failed, so do a port reset */
162 if (!test_and_set_bit(HID_RESET_PENDING, &usbhid->iofl)) {
163 schedule_work(&usbhid->reset_work);
164 goto done;
165 }
1da177e4
LT
166 }
167
48b4554a
JK
168 mod_timer(&usbhid->io_retry,
169 jiffies + msecs_to_jiffies(usbhid->retry_delay));
170done:
171 spin_unlock_irqrestore(&usbhid->inlock, flags);
1da177e4
LT
172}
173
48b4554a
JK
174/*
175 * Input interrupt completion handler.
176 */
854561b0 177
48b4554a 178static void hid_irq_in(struct urb *urb)
1da177e4 179{
48b4554a
JK
180 struct hid_device *hid = urb->context;
181 struct usbhid_device *usbhid = hid->driver_data;
182 int status;
1da177e4 183
48b4554a
JK
184 switch (urb->status) {
185 case 0: /* success */
186 usbhid->retry_delay = 0;
187 hid_input_report(urb->context, HID_INPUT_REPORT,
188 urb->transfer_buffer,
189 urb->actual_length, 1);
190 break;
191 case -EPIPE: /* stall */
192 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
193 set_bit(HID_CLEAR_HALT, &usbhid->iofl);
194 schedule_work(&usbhid->reset_work);
195 return;
196 case -ECONNRESET: /* unlink */
197 case -ENOENT:
198 case -ESHUTDOWN: /* unplug */
199 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
200 return;
201 case -EILSEQ: /* protocol error or unplug */
202 case -EPROTO: /* protocol error or unplug */
203 case -ETIME: /* protocol error or unplug */
204 case -ETIMEDOUT: /* Should never happen, but... */
205 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
206 hid_io_error(hid);
207 return;
208 default: /* error */
209 warn("input irq status %d received", urb->status);
210 }
1da177e4 211
48b4554a
JK
212 status = usb_submit_urb(urb, GFP_ATOMIC);
213 if (status) {
214 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
215 if (status != -EPERM) {
216 err("can't resubmit intr, %s-%s/input%d, status %d",
217 hid_to_usb_dev(hid)->bus->bus_name,
218 hid_to_usb_dev(hid)->devpath,
219 usbhid->ifnum, status);
220 hid_io_error(hid);
221 }
222 }
1da177e4
LT
223}
224
48b4554a 225static int hid_submit_out(struct hid_device *hid)
1da177e4 226{
48b4554a 227 struct hid_report *report;
4916b3a5
JK
228 struct usbhid_device *usbhid = hid->driver_data;
229
48b4554a 230 report = usbhid->out[usbhid->outtail];
1da177e4 231
48b4554a
JK
232 hid_output_report(report, usbhid->outbuf);
233 usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0);
234 usbhid->urbout->dev = hid_to_usb_dev(hid);
1d3e2023 235
48b4554a 236 dbg("submitting out urb");
d57cdcff 237
48b4554a
JK
238 if (usb_submit_urb(usbhid->urbout, GFP_ATOMIC)) {
239 err("usb_submit_urb(out) failed");
240 return -1;
241 }
1da177e4 242
48b4554a
JK
243 return 0;
244}
245
246static int hid_submit_ctrl(struct hid_device *hid)
1da177e4
LT
247{
248 struct hid_report *report;
48b4554a
JK
249 unsigned char dir;
250 int len;
4916b3a5 251 struct usbhid_device *usbhid = hid->driver_data;
1da177e4 252
48b4554a
JK
253 report = usbhid->ctrl[usbhid->ctrltail].report;
254 dir = usbhid->ctrl[usbhid->ctrltail].dir;
1da177e4 255
48b4554a
JK
256 len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
257 if (dir == USB_DIR_OUT) {
258 hid_output_report(report, usbhid->ctrlbuf);
259 usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
260 usbhid->urbctrl->transfer_buffer_length = len;
261 } else {
262 int maxpacket, padlen;
1da177e4 263
48b4554a
JK
264 usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0);
265 maxpacket = usb_maxpacket(hid_to_usb_dev(hid), usbhid->urbctrl->pipe, 0);
266 if (maxpacket > 0) {
267 padlen = (len + maxpacket - 1) / maxpacket;
268 padlen *= maxpacket;
269 if (padlen > usbhid->bufsize)
270 padlen = usbhid->bufsize;
271 } else
272 padlen = 0;
273 usbhid->urbctrl->transfer_buffer_length = padlen;
1da177e4 274 }
48b4554a 275 usbhid->urbctrl->dev = hid_to_usb_dev(hid);
1da177e4 276
48b4554a
JK
277 usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir;
278 usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT : HID_REQ_GET_REPORT;
279 usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) | report->id);
280 usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum);
281 usbhid->cr->wLength = cpu_to_le16(len);
1da177e4 282
48b4554a
JK
283 dbg("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u",
284 usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" : "Get_Report",
285 usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength);
1da177e4 286
48b4554a
JK
287 if (usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC)) {
288 err("usb_submit_urb(ctrl) failed");
289 return -1;
290 }
1da177e4 291
48b4554a
JK
292 return 0;
293}
1da177e4 294
48b4554a
JK
295/*
296 * Output interrupt completion handler.
297 */
1da177e4 298
48b4554a
JK
299static void hid_irq_out(struct urb *urb)
300{
301 struct hid_device *hid = urb->context;
302 struct usbhid_device *usbhid = hid->driver_data;
303 unsigned long flags;
304 int unplug = 0;
1da177e4 305
48b4554a
JK
306 switch (urb->status) {
307 case 0: /* success */
308 break;
309 case -ESHUTDOWN: /* unplug */
310 unplug = 1;
311 case -EILSEQ: /* protocol error or unplug */
312 case -EPROTO: /* protocol error or unplug */
313 case -ECONNRESET: /* unlink */
314 case -ENOENT:
315 break;
316 default: /* error */
317 warn("output irq status %d received", urb->status);
318 }
1da177e4 319
48b4554a 320 spin_lock_irqsave(&usbhid->outlock, flags);
1da177e4 321
48b4554a
JK
322 if (unplug)
323 usbhid->outtail = usbhid->outhead;
324 else
325 usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
1da177e4 326
48b4554a
JK
327 if (usbhid->outhead != usbhid->outtail) {
328 if (hid_submit_out(hid)) {
329 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
330 wake_up(&hid->wait);
331 }
332 spin_unlock_irqrestore(&usbhid->outlock, flags);
333 return;
334 }
1da177e4 335
48b4554a
JK
336 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
337 spin_unlock_irqrestore(&usbhid->outlock, flags);
338 wake_up(&hid->wait);
339}
6345fdfd 340
48b4554a
JK
341/*
342 * Control pipe completion handler.
343 */
1da177e4 344
48b4554a
JK
345static void hid_ctrl(struct urb *urb)
346{
347 struct hid_device *hid = urb->context;
348 struct usbhid_device *usbhid = hid->driver_data;
349 unsigned long flags;
350 int unplug = 0;
1da177e4 351
48b4554a 352 spin_lock_irqsave(&usbhid->ctrllock, flags);
1da177e4 353
48b4554a
JK
354 switch (urb->status) {
355 case 0: /* success */
356 if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
357 hid_input_report(urb->context, usbhid->ctrl[usbhid->ctrltail].report->type,
358 urb->transfer_buffer, urb->actual_length, 0);
359 break;
360 case -ESHUTDOWN: /* unplug */
361 unplug = 1;
362 case -EILSEQ: /* protocol error or unplug */
363 case -EPROTO: /* protocol error or unplug */
364 case -ECONNRESET: /* unlink */
365 case -ENOENT:
366 case -EPIPE: /* report not available */
367 break;
368 default: /* error */
369 warn("ctrl urb status %d received", urb->status);
370 }
1da177e4 371
48b4554a
JK
372 if (unplug)
373 usbhid->ctrltail = usbhid->ctrlhead;
374 else
375 usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
1da177e4 376
48b4554a
JK
377 if (usbhid->ctrlhead != usbhid->ctrltail) {
378 if (hid_submit_ctrl(hid)) {
379 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
380 wake_up(&hid->wait);
381 }
382 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
383 return;
384 }
1da177e4 385
48b4554a
JK
386 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
387 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
388 wake_up(&hid->wait);
389}
1da177e4 390
48b4554a
JK
391void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
392{
393 int head;
394 unsigned long flags;
395 struct usbhid_device *usbhid = hid->driver_data;
1da177e4 396
48b4554a
JK
397 if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
398 return;
b857c651 399
48b4554a 400 if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
1da177e4 401
48b4554a 402 spin_lock_irqsave(&usbhid->outlock, flags);
1da177e4 403
48b4554a
JK
404 if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
405 spin_unlock_irqrestore(&usbhid->outlock, flags);
406 warn("output queue full");
407 return;
408 }
1da177e4 409
48b4554a
JK
410 usbhid->out[usbhid->outhead] = report;
411 usbhid->outhead = head;
4871d3be 412
48b4554a
JK
413 if (!test_and_set_bit(HID_OUT_RUNNING, &usbhid->iofl))
414 if (hid_submit_out(hid))
415 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
8fd6db47 416
48b4554a
JK
417 spin_unlock_irqrestore(&usbhid->outlock, flags);
418 return;
419 }
1da177e4 420
48b4554a 421 spin_lock_irqsave(&usbhid->ctrllock, flags);
940824b0 422
48b4554a
JK
423 if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
424 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
425 warn("control queue full");
426 return;
427 }
8fd80133 428
48b4554a
JK
429 usbhid->ctrl[usbhid->ctrlhead].report = report;
430 usbhid->ctrl[usbhid->ctrlhead].dir = dir;
431 usbhid->ctrlhead = head;
8fd80133 432
48b4554a
JK
433 if (!test_and_set_bit(HID_CTRL_RUNNING, &usbhid->iofl))
434 if (hid_submit_ctrl(hid))
435 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
931e24b9 436
48b4554a
JK
437 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
438}
23b0d968 439
48b4554a
JK
440static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
441{
442 struct hid_device *hid = dev->private;
443 struct hid_field *field;
444 int offset;
41ad5fba 445
48b4554a
JK
446 if (type == EV_FF)
447 return input_ff_event(dev, type, code, value);
8d2bad87 448
48b4554a
JK
449 if (type != EV_LED)
450 return -1;
5556feae 451
48b4554a
JK
452 if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
453 warn("event field not found");
454 return -1;
455 }
4a1a4d8b 456
48b4554a
JK
457 hid_set_field(field, offset, value);
458 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
1da177e4 459
48b4554a
JK
460 return 0;
461}
1da177e4 462
48b4554a
JK
463int usbhid_wait_io(struct hid_device *hid)
464{
465 struct usbhid_device *usbhid = hid->driver_data;
25914662 466
48b4554a
JK
467 if (!wait_event_timeout(hid->wait, (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
468 !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
469 10*HZ)) {
470 dbg("timeout waiting for ctrl or out queue to clear");
471 return -1;
472 }
1da177e4 473
48b4554a
JK
474 return 0;
475}
53880546 476
48b4554a
JK
477static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
478{
479 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
480 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
481 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
482}
1da177e4 483
48b4554a
JK
484static int hid_get_class_descriptor(struct usb_device *dev, int ifnum,
485 unsigned char type, void *buf, int size)
486{
487 int result, retries = 4;
1da177e4 488
48b4554a 489 memset(buf, 0, size);
1da177e4 490
48b4554a
JK
491 do {
492 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
493 USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN,
494 (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT);
495 retries--;
496 } while (result < size && retries);
497 return result;
498}
940824b0 499
48b4554a
JK
500int usbhid_open(struct hid_device *hid)
501{
502 ++hid->open;
503 if (hid_start_in(hid))
504 hid_io_error(hid);
505 return 0;
506}
a417a21e 507
48b4554a
JK
508void usbhid_close(struct hid_device *hid)
509{
510 struct usbhid_device *usbhid = hid->driver_data;
eab9edd2 511
48b4554a
JK
512 if (!--hid->open)
513 usb_kill_urb(usbhid->urbin);
514}
1d3e2023 515
48b4554a
JK
516/*
517 * Initialize all reports
518 */
41ad5fba 519
48b4554a
JK
520void usbhid_init_reports(struct hid_device *hid)
521{
522 struct hid_report *report;
523 struct usbhid_device *usbhid = hid->driver_data;
524 int err, ret;
41ad5fba 525
48b4554a
JK
526 list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
527 usbhid_submit_report(hid, report, USB_DIR_IN);
5556feae 528
48b4554a
JK
529 list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
530 usbhid_submit_report(hid, report, USB_DIR_IN);
4a1a4d8b 531
48b4554a
JK
532 err = 0;
533 ret = usbhid_wait_io(hid);
534 while (ret) {
535 err |= ret;
536 if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl))
537 usb_kill_urb(usbhid->urbctrl);
538 if (test_bit(HID_OUT_RUNNING, &usbhid->iofl))
539 usb_kill_urb(usbhid->urbout);
540 ret = usbhid_wait_io(hid);
541 }
3f9b4076 542
48b4554a
JK
543 if (err)
544 warn("timeout initializing reports");
545}
1da177e4 546
713c8aad
PZ
547/*
548 * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01).
549 */
550static int hid_find_field_early(struct hid_device *hid, unsigned int page,
551 unsigned int hid_code, struct hid_field **pfield)
552{
553 struct hid_report *report;
554 struct hid_field *field;
555 struct hid_usage *usage;
556 int i, j;
557
558 list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) {
559 for (i = 0; i < report->maxfield; i++) {
560 field = report->field[i];
561 for (j = 0; j < field->maxusage; j++) {
562 usage = &field->usage[j];
563 if ((usage->hid & HID_USAGE_PAGE) == page &&
564 (usage->hid & 0xFFFF) == hid_code) {
565 *pfield = field;
566 return j;
567 }
568 }
569 }
570 }
571 return -1;
572}
573
574static void usbhid_set_leds(struct hid_device *hid)
575{
576 struct hid_field *field;
577 int offset;
578
579 if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) {
580 hid_set_field(field, offset, 0);
581 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
582 }
583}
584
bf0964dc
MH
585/*
586 * Traverse the supplied list of reports and find the longest
587 */
588static void hid_find_max_report(struct hid_device *hid, unsigned int type, int *max)
589{
590 struct hid_report *report;
591 int size;
592
593 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
594 size = ((report->size - 1) >> 3) + 1;
595 if (type == HID_INPUT_REPORT && hid->report_enum[type].numbered)
596 size++;
597 if (*max < size)
598 *max = size;
599 }
600}
601
1da177e4
LT
602static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
603{
4916b3a5
JK
604 struct usbhid_device *usbhid = hid->driver_data;
605
606 if (!(usbhid->inbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->inbuf_dma)))
1da177e4 607 return -1;
4916b3a5 608 if (!(usbhid->outbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->outbuf_dma)))
1da177e4 609 return -1;
4916b3a5 610 if (!(usbhid->cr = usb_buffer_alloc(dev, sizeof(*(usbhid->cr)), GFP_ATOMIC, &usbhid->cr_dma)))
1da177e4 611 return -1;
4916b3a5 612 if (!(usbhid->ctrlbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->ctrlbuf_dma)))
1da177e4
LT
613 return -1;
614
615 return 0;
616}
617
618static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
619{
4916b3a5
JK
620 struct usbhid_device *usbhid = hid->driver_data;
621
622 if (usbhid->inbuf)
623 usb_buffer_free(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma);
624 if (usbhid->outbuf)
625 usb_buffer_free(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma);
626 if (usbhid->cr)
627 usb_buffer_free(dev, sizeof(*(usbhid->cr)), usbhid->cr, usbhid->cr_dma);
628 if (usbhid->ctrlbuf)
629 usb_buffer_free(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma);
1da177e4
LT
630}
631
940824b0
VP
632/*
633 * Cherry Cymotion keyboard have an invalid HID report descriptor,
634 * that needs fixing before we can parse it.
635 */
636
637static void hid_fixup_cymotion_descriptor(char *rdesc, int rsize)
638{
639 if (rsize >= 17 && rdesc[11] == 0x3c && rdesc[12] == 0x02) {
640 info("Fixing up Cherry Cymotion report descriptor");
641 rdesc[11] = rdesc[16] = 0xff;
642 rdesc[12] = rdesc[17] = 0x03;
643 }
644}
645
4a1a4d8b
GL
646/*
647 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
648 * to "operational". Without this, the ps3 controller will not report any
649 * events.
650 */
651static void hid_fixup_sony_ps3_controller(struct usb_device *dev, int ifnum)
652{
653 int result;
654 char *buf = kmalloc(18, GFP_KERNEL);
655
656 if (!buf)
657 return;
658
659 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
660 HID_REQ_GET_REPORT,
661 USB_DIR_IN | USB_TYPE_CLASS |
662 USB_RECIP_INTERFACE,
663 (3 << 8) | 0xf2, ifnum, buf, 17,
664 USB_CTRL_GET_TIMEOUT);
665
666 if (result < 0)
667 err("%s failed: %d\n", __func__, result);
668
669 kfree(buf);
670}
671
b55fd23c 672/*
85cbea39 673 * Certain Logitech keyboards send in report #3 keys which are far
b55fd23c
JK
674 * above the logical maximum described in descriptor. This extends
675 * the original value of 0x28c of logical maximum to 0x104d
676 */
85cbea39 677static void hid_fixup_logitech_descriptor(unsigned char *rdesc, int rsize)
b55fd23c
JK
678{
679 if (rsize >= 90 && rdesc[83] == 0x26
680 && rdesc[84] == 0x8c
681 && rdesc[85] == 0x02) {
85cbea39 682 info("Fixing up Logitech keyboard report descriptor");
b55fd23c
JK
683 rdesc[84] = rdesc[89] = 0x4d;
684 rdesc[85] = rdesc[90] = 0x10;
685 }
686}
687
1da177e4
LT
688static struct hid_device *usb_hid_configure(struct usb_interface *intf)
689{
690 struct usb_host_interface *interface = intf->cur_altsetting;
691 struct usb_device *dev = interface_to_usbdev (intf);
692 struct hid_descriptor *hdesc;
693 struct hid_device *hid;
2eb5dc30 694 u32 quirks = 0;
4cbe7d28 695 unsigned rsize = 0;
c5b7c7c3
DT
696 char *rdesc;
697 int n, len, insize = 0;
4916b3a5 698 struct usbhid_device *usbhid;
1da177e4 699
2eb5dc30
PW
700 quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
701 le16_to_cpu(dev->descriptor.idProduct));
1da177e4 702
0f28b55d
AS
703 /* Many keyboards and mice don't like to be polled for reports,
704 * so we will always set the HID_QUIRK_NOGET flag for them. */
705 if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
706 if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD ||
707 interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)
708 quirks |= HID_QUIRK_NOGET;
709 }
710
1da177e4
LT
711 if (quirks & HID_QUIRK_IGNORE)
712 return NULL;
713
a417a21e
SS
714 if ((quirks & HID_QUIRK_IGNORE_MOUSE) &&
715 (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE))
716 return NULL;
717
718
c5b7c7c3
DT
719 if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) &&
720 (!interface->desc.bNumEndpoints ||
721 usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) {
722 dbg("class descriptor not present\n");
723 return NULL;
1da177e4
LT
724 }
725
726 for (n = 0; n < hdesc->bNumDescriptors; n++)
727 if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
728 rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
729
730 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
731 dbg("weird size of report descriptor (%u)", rsize);
732 return NULL;
733 }
734
735 if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) {
736 dbg("couldn't allocate rdesc memory");
737 return NULL;
738 }
739
854561b0
VP
740 hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
741
1da177e4
LT
742 if ((n = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber, HID_DT_REPORT, rdesc, rsize)) < 0) {
743 dbg("reading report descriptor failed");
744 kfree(rdesc);
745 return NULL;
746 }
747
940824b0
VP
748 if ((quirks & HID_QUIRK_CYMOTION))
749 hid_fixup_cymotion_descriptor(rdesc, rsize);
750
85cbea39
JK
751 if (quirks & HID_QUIRK_LOGITECH_DESCRIPTOR)
752 hid_fixup_logitech_descriptor(rdesc, rsize);
b55fd23c 753
fdc9c566 754#ifdef CONFIG_HID_DEBUG
1da177e4
LT
755 printk(KERN_DEBUG __FILE__ ": report descriptor (size %u, read %d) = ", rsize, n);
756 for (n = 0; n < rsize; n++)
757 printk(" %02x", (unsigned char) rdesc[n]);
758 printk("\n");
759#endif
760
761 if (!(hid = hid_parse_report(rdesc, n))) {
762 dbg("parsing report descriptor failed");
763 kfree(rdesc);
764 return NULL;
765 }
766
767 kfree(rdesc);
768 hid->quirks = quirks;
769
4916b3a5
JK
770 if (!(usbhid = kzalloc(sizeof(struct usbhid_device), GFP_KERNEL)))
771 goto fail;
772
773 hid->driver_data = usbhid;
774 usbhid->hid = hid;
775
776 usbhid->bufsize = HID_MIN_BUFFER_SIZE;
777 hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize);
778 hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize);
779 hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize);
bf0964dc 780
4916b3a5
JK
781 if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
782 usbhid->bufsize = HID_MAX_BUFFER_SIZE;
bf0964dc
MH
783
784 hid_find_max_report(hid, HID_INPUT_REPORT, &insize);
785
786 if (insize > HID_MAX_BUFFER_SIZE)
787 insize = HID_MAX_BUFFER_SIZE;
788
1da177e4
LT
789 if (hid_alloc_buffers(dev, hid)) {
790 hid_free_buffers(dev, hid);
791 goto fail;
792 }
793
794 for (n = 0; n < interface->desc.bNumEndpoints; n++) {
795
796 struct usb_endpoint_descriptor *endpoint;
797 int pipe;
798 int interval;
799
800 endpoint = &interface->endpoint[n].desc;
801 if ((endpoint->bmAttributes & 3) != 3) /* Not an interrupt endpoint */
802 continue;
803
1da177e4 804 interval = endpoint->bInterval;
1da177e4
LT
805
806 /* Change the polling interval of mice. */
807 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
808 interval = hid_mousepoll_interval;
05f091ab 809
0f12aa03 810 if (usb_endpoint_dir_in(endpoint)) {
4916b3a5 811 if (usbhid->urbin)
1da177e4 812 continue;
4916b3a5 813 if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
1da177e4
LT
814 goto fail;
815 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
4916b3a5 816 usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
1da177e4 817 hid_irq_in, hid, interval);
4916b3a5
JK
818 usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
819 usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1da177e4 820 } else {
4916b3a5 821 if (usbhid->urbout)
1da177e4 822 continue;
4916b3a5 823 if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
1da177e4
LT
824 goto fail;
825 pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
4916b3a5 826 usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
1da177e4 827 hid_irq_out, hid, interval);
4916b3a5
JK
828 usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
829 usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1da177e4
LT
830 }
831 }
832
4916b3a5 833 if (!usbhid->urbin) {
1da177e4
LT
834 err("couldn't find an input interrupt endpoint");
835 goto fail;
836 }
837
838 init_waitqueue_head(&hid->wait);
839
4916b3a5
JK
840 INIT_WORK(&usbhid->reset_work, hid_reset);
841 setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
aef4e266 842
4916b3a5
JK
843 spin_lock_init(&usbhid->inlock);
844 spin_lock_init(&usbhid->outlock);
845 spin_lock_init(&usbhid->ctrllock);
1da177e4
LT
846
847 hid->version = le16_to_cpu(hdesc->bcdHID);
848 hid->country = hdesc->bCountryCode;
be820975 849 hid->dev = &intf->dev;
4916b3a5
JK
850 usbhid->intf = intf;
851 usbhid->ifnum = interface->desc.bInterfaceNumber;
1da177e4
LT
852
853 hid->name[0] = 0;
854
c5b7c7c3
DT
855 if (dev->manufacturer)
856 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
857
858 if (dev->product) {
859 if (dev->manufacturer)
860 strlcat(hid->name, " ", sizeof(hid->name));
861 strlcat(hid->name, dev->product, sizeof(hid->name));
862 }
863
864 if (!strlen(hid->name))
865 snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x",
866 le16_to_cpu(dev->descriptor.idVendor),
867 le16_to_cpu(dev->descriptor.idProduct));
1da177e4 868
229695e5 869 hid->bus = BUS_USB;
9fa2ad5f
JB
870 hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
871 hid->product = le16_to_cpu(dev->descriptor.idProduct);
229695e5 872
c5b7c7c3
DT
873 usb_make_path(dev, hid->phys, sizeof(hid->phys));
874 strlcat(hid->phys, "/input", sizeof(hid->phys));
875 len = strlen(hid->phys);
876 if (len < sizeof(hid->phys) - 1)
877 snprintf(hid->phys + len, sizeof(hid->phys) - len,
878 "%d", intf->altsetting[0].desc.bInterfaceNumber);
1da177e4
LT
879
880 if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
881 hid->uniq[0] = 0;
882
4916b3a5
JK
883 usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
884 if (!usbhid->urbctrl)
1da177e4 885 goto fail;
c5b7c7c3 886
4916b3a5
JK
887 usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr,
888 usbhid->ctrlbuf, 1, hid_ctrl, hid);
889 usbhid->urbctrl->setup_dma = usbhid->cr_dma;
890 usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma;
891 usbhid->urbctrl->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP);
229695e5 892 hid->hidinput_input_event = usb_hidinput_input_event;
7c379146
JK
893 hid->hid_open = usbhid_open;
894 hid->hid_close = usbhid_close;
aa938f79
JK
895#ifdef CONFIG_USB_HIDDEV
896 hid->hiddev_hid_event = hiddev_hid_event;
aa8de2f0 897 hid->hiddev_report_event = hiddev_report_event;
aa938f79 898#endif
1da177e4
LT
899 return hid;
900
901fail:
4916b3a5
JK
902 usb_free_urb(usbhid->urbin);
903 usb_free_urb(usbhid->urbout);
904 usb_free_urb(usbhid->urbctrl);
1da177e4
LT
905 hid_free_buffers(dev, hid);
906 hid_free_device(hid);
907
908 return NULL;
909}
910
911static void hid_disconnect(struct usb_interface *intf)
912{
913 struct hid_device *hid = usb_get_intfdata (intf);
4916b3a5 914 struct usbhid_device *usbhid;
1da177e4
LT
915
916 if (!hid)
917 return;
918
4916b3a5
JK
919 usbhid = hid->driver_data;
920
921 spin_lock_irq(&usbhid->inlock); /* Sync with error handler */
1da177e4 922 usb_set_intfdata(intf, NULL);
4916b3a5
JK
923 spin_unlock_irq(&usbhid->inlock);
924 usb_kill_urb(usbhid->urbin);
925 usb_kill_urb(usbhid->urbout);
926 usb_kill_urb(usbhid->urbctrl);
1da177e4 927
4916b3a5 928 del_timer_sync(&usbhid->io_retry);
aef4e266
AS
929 flush_scheduled_work();
930
1da177e4
LT
931 if (hid->claimed & HID_CLAIMED_INPUT)
932 hidinput_disconnect(hid);
933 if (hid->claimed & HID_CLAIMED_HIDDEV)
934 hiddev_disconnect(hid);
935
4916b3a5
JK
936 usb_free_urb(usbhid->urbin);
937 usb_free_urb(usbhid->urbctrl);
938 usb_free_urb(usbhid->urbout);
1da177e4 939
be820975 940 hid_free_buffers(hid_to_usb_dev(hid), hid);
1da177e4
LT
941 hid_free_device(hid);
942}
943
944static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id)
945{
946 struct hid_device *hid;
947 char path[64];
948 int i;
949 char *c;
950
951 dbg("HID probe called for ifnum %d",
952 intf->altsetting->desc.bInterfaceNumber);
953
954 if (!(hid = usb_hid_configure(intf)))
479f6ea8 955 return -ENODEV;
1da177e4 956
4916b3a5 957 usbhid_init_reports(hid);
1da177e4 958 hid_dump_device(hid);
713c8aad
PZ
959 if (hid->quirks & HID_QUIRK_RESET_LEDS)
960 usbhid_set_leds(hid);
1da177e4
LT
961
962 if (!hidinput_connect(hid))
963 hid->claimed |= HID_CLAIMED_INPUT;
964 if (!hiddev_connect(hid))
965 hid->claimed |= HID_CLAIMED_HIDDEV;
966
967 usb_set_intfdata(intf, hid);
968
969 if (!hid->claimed) {
970 printk ("HID device not claimed by input or hiddev\n");
971 hid_disconnect(intf);
479f6ea8 972 return -ENODEV;
1da177e4
LT
973 }
974
c4146067 975 if ((hid->claimed & HID_CLAIMED_INPUT))
4916b3a5
JK
976 hid_ff_init(hid);
977
4a1a4d8b
GL
978 if (hid->quirks & HID_QUIRK_SONY_PS3_CONTROLLER)
979 hid_fixup_sony_ps3_controller(interface_to_usbdev(intf),
980 intf->cur_altsetting->desc.bInterfaceNumber);
981
1da177e4
LT
982 printk(KERN_INFO);
983
984 if (hid->claimed & HID_CLAIMED_INPUT)
985 printk("input");
986 if (hid->claimed == (HID_CLAIMED_INPUT | HID_CLAIMED_HIDDEV))
987 printk(",");
988 if (hid->claimed & HID_CLAIMED_HIDDEV)
989 printk("hiddev%d", hid->minor);
990
991 c = "Device";
992 for (i = 0; i < hid->maxcollection; i++) {
993 if (hid->collection[i].type == HID_COLLECTION_APPLICATION &&
994 (hid->collection[i].usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
995 (hid->collection[i].usage & 0xffff) < ARRAY_SIZE(hid_types)) {
996 c = hid_types[hid->collection[i].usage & 0xffff];
997 break;
998 }
999 }
1000
1001 usb_make_path(interface_to_usbdev(intf), path, 63);
1002
1003 printk(": USB HID v%x.%02x %s [%s] on %s\n",
1004 hid->version >> 8, hid->version & 0xff, c, hid->name, path);
1005
1006 return 0;
1007}
1008
27d72e85 1009static int hid_suspend(struct usb_interface *intf, pm_message_t message)
1da177e4
LT
1010{
1011 struct hid_device *hid = usb_get_intfdata (intf);
4916b3a5 1012 struct usbhid_device *usbhid = hid->driver_data;
1da177e4 1013
4916b3a5
JK
1014 spin_lock_irq(&usbhid->inlock); /* Sync with error handler */
1015 set_bit(HID_SUSPENDED, &usbhid->iofl);
1016 spin_unlock_irq(&usbhid->inlock);
1017 del_timer(&usbhid->io_retry);
1018 usb_kill_urb(usbhid->urbin);
1da177e4
LT
1019 dev_dbg(&intf->dev, "suspend\n");
1020 return 0;
1021}
1022
1023static int hid_resume(struct usb_interface *intf)
1024{
1025 struct hid_device *hid = usb_get_intfdata (intf);
4916b3a5 1026 struct usbhid_device *usbhid = hid->driver_data;
1da177e4
LT
1027 int status;
1028
4916b3a5
JK
1029 clear_bit(HID_SUSPENDED, &usbhid->iofl);
1030 usbhid->retry_delay = 0;
aef4e266 1031 status = hid_start_in(hid);
1da177e4
LT
1032 dev_dbg(&intf->dev, "resume status %d\n", status);
1033 return status;
1034}
1035
df9a1f48
AS
1036/* Treat USB reset pretty much the same as suspend/resume */
1037static void hid_pre_reset(struct usb_interface *intf)
1038{
1039 /* FIXME: What if the interface is already suspended? */
1040 hid_suspend(intf, PMSG_ON);
1041}
1042
1043static void hid_post_reset(struct usb_interface *intf)
1044{
1045 struct usb_device *dev = interface_to_usbdev (intf);
1046
1047 hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0);
1048 /* FIXME: Any more reinitialization needed? */
1049
1050 hid_resume(intf);
1051}
1052
1da177e4
LT
1053static struct usb_device_id hid_usb_ids [] = {
1054 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1055 .bInterfaceClass = USB_INTERFACE_CLASS_HID },
1056 { } /* Terminating entry */
1057};
1058
1059MODULE_DEVICE_TABLE (usb, hid_usb_ids);
1060
1061static struct usb_driver hid_driver = {
1da177e4
LT
1062 .name = "usbhid",
1063 .probe = hid_probe,
1064 .disconnect = hid_disconnect,
1065 .suspend = hid_suspend,
1066 .resume = hid_resume,
df9a1f48
AS
1067 .pre_reset = hid_pre_reset,
1068 .post_reset = hid_post_reset,
1da177e4
LT
1069 .id_table = hid_usb_ids,
1070};
1071
1072static int __init hid_init(void)
1073{
1074 int retval;
1075 retval = hiddev_init();
1076 if (retval)
1077 goto hiddev_init_fail;
1078 retval = usb_register(&hid_driver);
1079 if (retval)
1080 goto usb_register_fail;
1081 info(DRIVER_VERSION ":" DRIVER_DESC);
1082
1083 return 0;
1084usb_register_fail:
1085 hiddev_exit();
1086hiddev_init_fail:
1087 return retval;
1088}
1089
1090static void __exit hid_exit(void)
1091{
1092 usb_deregister(&hid_driver);
1093 hiddev_exit();
1094}
1095
1096module_init(hid_init);
1097module_exit(hid_exit);
1098
1099MODULE_AUTHOR(DRIVER_AUTHOR);
1100MODULE_DESCRIPTION(DRIVER_DESC);
1101MODULE_LICENSE(DRIVER_LICENSE);