blob: bf118c05a621ce3dd053504525191385d9b441af [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 Kosinab55fd232007-02-21 19:27:49 +01007 * Copyright (c) 2006-2007 Jiri Kosina
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
10/*
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
14 * any later version.
15 */
16
17#include <linux/module.h>
18#include <linux/slab.h>
19#include <linux/init.h>
20#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/list.h>
22#include <linux/mm.h>
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/usb.h>
31
Jiri Kosinadde58452006-12-08 18:40:44 +010032#include <linux/hid.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/hiddev.h>
Jiri Kosinac080d892007-01-25 11:43:31 +010034#include <linux/hid-debug.h>
Jiri Kosina4916b3a2006-12-08 18:41:03 +010035#include "usbhid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/*
38 * Version Information
39 */
40
Michael Haboustakbf0964d2005-09-05 00:12:01 -050041#define DRIVER_VERSION "v2.6"
Jiri Kosinaf142b3a2007-04-16 11:29:28 +020042#define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik, Jiri Kosina"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#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
56/*
Alan Sternaef4e262006-01-31 12:58:38 -050057 * Input submission and I/O error handler.
58 */
59
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)
64{
65 unsigned long flags;
66 int rc = 0;
Jiri Kosina4916b3a2006-12-08 18:41:03 +010067 struct usbhid_device *usbhid = hid->driver_data;
Alan Sternaef4e262006-01-31 12:58:38 -050068
Jiri Kosina4916b3a2006-12-08 18:41:03 +010069 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);
Alan Sternaef4e262006-01-31 12:58:38 -050073 if (rc != 0)
Jiri Kosina4916b3a2006-12-08 18:41:03 +010074 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
Alan Sternaef4e262006-01-31 12:58:38 -050075 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +010076 spin_unlock_irqrestore(&usbhid->inlock, flags);
Alan Sternaef4e262006-01-31 12:58:38 -050077 return rc;
78}
79
80/* I/O retry timer routine */
81static void hid_retry_timeout(unsigned long _hid)
82{
83 struct hid_device *hid = (struct hid_device *) _hid;
Jiri Kosina4916b3a2006-12-08 18:41:03 +010084 struct usbhid_device *usbhid = hid->driver_data;
Alan Sternaef4e262006-01-31 12:58:38 -050085
Jiri Kosina4916b3a2006-12-08 18:41:03 +010086 dev_dbg(&usbhid->intf->dev, "retrying intr urb\n");
Alan Sternaef4e262006-01-31 12:58:38 -050087 if (hid_start_in(hid))
88 hid_io_error(hid);
89}
90
Alan Stern6d8fc4d2006-10-18 12:35:24 -040091/* Workqueue routine to reset the device or clear a halt */
David Howellsc4028952006-11-22 14:57:56 +000092static void hid_reset(struct work_struct *work)
Alan Sternaef4e262006-01-31 12:58:38 -050093{
Jiri Kosina4916b3a2006-12-08 18:41:03 +010094 struct usbhid_device *usbhid =
95 container_of(work, struct usbhid_device, reset_work);
96 struct hid_device *hid = usbhid->hid;
Alan Stern6d8fc4d2006-10-18 12:35:24 -040097 int rc_lock, rc = 0;
Alan Sternaef4e262006-01-31 12:58:38 -050098
Jiri Kosina4916b3a2006-12-08 18:41:03 +010099 if (test_bit(HID_CLEAR_HALT, &usbhid->iofl)) {
100 dev_dbg(&usbhid->intf->dev, "clear halt\n");
Anssi Hannulabe820972007-01-19 19:28:17 +0200101 rc = usb_clear_halt(hid_to_usb_dev(hid), usbhid->urbin->pipe);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100102 clear_bit(HID_CLEAR_HALT, &usbhid->iofl);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400103 hid_start_in(hid);
Alan Sternaef4e262006-01-31 12:58:38 -0500104 }
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400105
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100106 else if (test_bit(HID_RESET_PENDING, &usbhid->iofl)) {
107 dev_dbg(&usbhid->intf->dev, "resetting device\n");
Anssi Hannulabe820972007-01-19 19:28:17 +0200108 rc = rc_lock = usb_lock_device_for_reset(hid_to_usb_dev(hid), usbhid->intf);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400109 if (rc_lock >= 0) {
Anssi Hannulabe820972007-01-19 19:28:17 +0200110 rc = usb_reset_composite_device(hid_to_usb_dev(hid), usbhid->intf);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400111 if (rc_lock)
Anssi Hannulabe820972007-01-19 19:28:17 +0200112 usb_unlock_device(hid_to_usb_dev(hid));
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400113 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100114 clear_bit(HID_RESET_PENDING, &usbhid->iofl);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400115 }
Alan Sternaef4e262006-01-31 12:58:38 -0500116
Alan Sterndf9a1f42006-06-01 13:55:28 -0400117 switch (rc) {
118 case 0:
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100119 if (!test_bit(HID_IN_RUNNING, &usbhid->iofl))
Alan Sternaef4e262006-01-31 12:58:38 -0500120 hid_io_error(hid);
Alan Sterndf9a1f42006-06-01 13:55:28 -0400121 break;
122 default:
Alan Sternaef4e262006-01-31 12:58:38 -0500123 err("can't reset device, %s-%s/input%d, status %d",
Anssi Hannulabe820972007-01-19 19:28:17 +0200124 hid_to_usb_dev(hid)->bus->bus_name,
125 hid_to_usb_dev(hid)->devpath,
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100126 usbhid->ifnum, rc);
Alan Sterndf9a1f42006-06-01 13:55:28 -0400127 /* FALLTHROUGH */
128 case -EHOSTUNREACH:
129 case -ENODEV:
130 case -EINTR:
131 break;
132 }
Alan Sternaef4e262006-01-31 12:58:38 -0500133}
134
135/* Main I/O error handler */
136static void hid_io_error(struct hid_device *hid)
137{
138 unsigned long flags;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100139 struct usbhid_device *usbhid = hid->driver_data;
Alan Sternaef4e262006-01-31 12:58:38 -0500140
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100141 spin_lock_irqsave(&usbhid->inlock, flags);
Alan Sternaef4e262006-01-31 12:58:38 -0500142
143 /* Stop when disconnected */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100144 if (usb_get_intfdata(usbhid->intf) == NULL)
Alan Sternaef4e262006-01-31 12:58:38 -0500145 goto done;
146
Alan Stern5e2a55f2007-03-20 19:03:31 +0100147 /* 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
Alan Sternaef4e262006-01-31 12:58:38 -0500152 /* When an error occurs, retry at increasing intervals */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100153 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;
Alan Sternaef4e262006-01-31 12:58:38 -0500158
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100159 if (time_after(jiffies, usbhid->stop_retry)) {
Alan Sternaef4e262006-01-31 12:58:38 -0500160
161 /* Retries failed, so do a port reset */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100162 if (!test_and_set_bit(HID_RESET_PENDING, &usbhid->iofl)) {
163 schedule_work(&usbhid->reset_work);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400164 goto done;
Alan Sternaef4e262006-01-31 12:58:38 -0500165 }
166 }
167
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100168 mod_timer(&usbhid->io_retry,
169 jiffies + msecs_to_jiffies(usbhid->retry_delay));
Alan Sternaef4e262006-01-31 12:58:38 -0500170done:
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100171 spin_unlock_irqrestore(&usbhid->inlock, flags);
Alan Sternaef4e262006-01-31 12:58:38 -0500172}
173
174/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 * Input interrupt completion handler.
176 */
177
David Howells7d12e782006-10-05 14:55:46 +0100178static void hid_irq_in(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100181 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 int status;
183
184 switch (urb->status) {
185 case 0: /* success */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100186 usbhid->retry_delay = 0;
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +0100187 hid_input_report(urb->context, HID_INPUT_REPORT,
188 urb->transfer_buffer,
189 urb->actual_length, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 break;
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400191 case -EPIPE: /* stall */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100192 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
193 set_bit(HID_CLEAR_HALT, &usbhid->iofl);
194 schedule_work(&usbhid->reset_work);
Alan Stern6d8fc4d2006-10-18 12:35:24 -0400195 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 case -ECONNRESET: /* unlink */
197 case -ENOENT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 case -ESHUTDOWN: /* unplug */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100199 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return;
Alan Sternaef4e262006-01-31 12:58:38 -0500201 case -EILSEQ: /* protocol error or unplug */
202 case -EPROTO: /* protocol error or unplug */
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700203 case -ETIME: /* protocol error or unplug */
204 case -ETIMEDOUT: /* Should never happen, but... */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100205 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
Alan Sternaef4e262006-01-31 12:58:38 -0500206 hid_io_error(hid);
207 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 default: /* error */
209 warn("input irq status %d received", urb->status);
210 }
211
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800212 status = usb_submit_urb(urb, GFP_ATOMIC);
Alan Sternaef4e262006-01-31 12:58:38 -0500213 if (status) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100214 clear_bit(HID_IN_RUNNING, &usbhid->iofl);
Alan Sternaef4e262006-01-31 12:58:38 -0500215 if (status != -EPERM) {
216 err("can't resubmit intr, %s-%s/input%d, status %d",
Anssi Hannulabe820972007-01-19 19:28:17 +0200217 hid_to_usb_dev(hid)->bus->bus_name,
218 hid_to_usb_dev(hid)->devpath,
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100219 usbhid->ifnum, status);
Alan Sternaef4e262006-01-31 12:58:38 -0500220 hid_io_error(hid);
221 }
222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225static int hid_submit_out(struct hid_device *hid)
226{
227 struct hid_report *report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100228 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100230 report = usbhid->out[usbhid->outtail];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100232 hid_output_report(report, usbhid->outbuf);
233 usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0);
Anssi Hannulabe820972007-01-19 19:28:17 +0200234 usbhid->urbout->dev = hid_to_usb_dev(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 dbg("submitting out urb");
237
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100238 if (usb_submit_urb(usbhid->urbout, GFP_ATOMIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 err("usb_submit_urb(out) failed");
240 return -1;
241 }
242
243 return 0;
244}
245
246static int hid_submit_ctrl(struct hid_device *hid)
247{
248 struct hid_report *report;
249 unsigned char dir;
250 int len;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100251 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100253 report = usbhid->ctrl[usbhid->ctrltail].report;
254 dir = usbhid->ctrl[usbhid->ctrltail].dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 len = ((report->size - 1) >> 3) + 1 + (report->id > 0);
257 if (dir == USB_DIR_OUT) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100258 hid_output_report(report, usbhid->ctrlbuf);
Anssi Hannulabe820972007-01-19 19:28:17 +0200259 usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100260 usbhid->urbctrl->transfer_buffer_length = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 } else {
262 int maxpacket, padlen;
263
Anssi Hannulabe820972007-01-19 19:28:17 +0200264 usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0);
265 maxpacket = usb_maxpacket(hid_to_usb_dev(hid), usbhid->urbctrl->pipe, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (maxpacket > 0) {
267 padlen = (len + maxpacket - 1) / maxpacket;
268 padlen *= maxpacket;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100269 if (padlen > usbhid->bufsize)
270 padlen = usbhid->bufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 } else
272 padlen = 0;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100273 usbhid->urbctrl->transfer_buffer_length = padlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
Anssi Hannulabe820972007-01-19 19:28:17 +0200275 usbhid->urbctrl->dev = hid_to_usb_dev(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100277 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 dbg("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u",
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100284 usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" : "Get_Report",
285 usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100287 if (usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 err("usb_submit_urb(ctrl) failed");
289 return -1;
290 }
291
292 return 0;
293}
294
295/*
296 * Output interrupt completion handler.
297 */
298
David Howells7d12e782006-10-05 14:55:46 +0100299static void hid_irq_out(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100302 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 unsigned long flags;
304 int unplug = 0;
305
306 switch (urb->status) {
307 case 0: /* success */
Vojtech Pavlikc4786ca2005-09-05 00:13:03 -0500308 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 case -ESHUTDOWN: /* unplug */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 unplug = 1;
Alan Sternaef4e262006-01-31 12:58:38 -0500311 case -EILSEQ: /* protocol error or unplug */
312 case -EPROTO: /* protocol error or unplug */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 case -ECONNRESET: /* unlink */
314 case -ENOENT:
315 break;
316 default: /* error */
317 warn("output irq status %d received", urb->status);
318 }
319
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100320 spin_lock_irqsave(&usbhid->outlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 if (unplug)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100323 usbhid->outtail = usbhid->outhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 else
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100325 usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100327 if (usbhid->outhead != usbhid->outtail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (hid_submit_out(hid)) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100329 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 wake_up(&hid->wait);
331 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100332 spin_unlock_irqrestore(&usbhid->outlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return;
334 }
335
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100336 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
337 spin_unlock_irqrestore(&usbhid->outlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 wake_up(&hid->wait);
339}
340
341/*
342 * Control pipe completion handler.
343 */
344
David Howells7d12e782006-10-05 14:55:46 +0100345static void hid_ctrl(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
347 struct hid_device *hid = urb->context;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100348 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 unsigned long flags;
350 int unplug = 0;
351
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100352 spin_lock_irqsave(&usbhid->ctrllock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 switch (urb->status) {
355 case 0: /* success */
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100356 if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN)
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +0100357 hid_input_report(urb->context, usbhid->ctrl[usbhid->ctrltail].report->type,
358 urb->transfer_buffer, urb->actual_length, 0);
Vojtech Pavlikc4786ca2005-09-05 00:13:03 -0500359 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 case -ESHUTDOWN: /* unplug */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 unplug = 1;
Alan Sternaef4e262006-01-31 12:58:38 -0500362 case -EILSEQ: /* protocol error or unplug */
363 case -EPROTO: /* protocol error or unplug */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 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 }
371
372 if (unplug)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100373 usbhid->ctrltail = usbhid->ctrlhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 else
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100375 usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100377 if (usbhid->ctrlhead != usbhid->ctrltail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (hid_submit_ctrl(hid)) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100379 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 wake_up(&hid->wait);
381 }
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100382 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return;
384 }
385
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100386 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
387 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 wake_up(&hid->wait);
389}
390
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100391void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 int head;
394 unsigned long flags;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100395 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN)
398 return;
399
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100400 if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100402 spin_lock_irqsave(&usbhid->outlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100404 if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) {
405 spin_unlock_irqrestore(&usbhid->outlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 warn("output queue full");
407 return;
408 }
409
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100410 usbhid->out[usbhid->outhead] = report;
411 usbhid->outhead = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100413 if (!test_and_set_bit(HID_OUT_RUNNING, &usbhid->iofl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (hid_submit_out(hid))
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100415 clear_bit(HID_OUT_RUNNING, &usbhid->iofl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100417 spin_unlock_irqrestore(&usbhid->outlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return;
419 }
420
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100421 spin_lock_irqsave(&usbhid->ctrllock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100423 if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) {
424 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 warn("control queue full");
426 return;
427 }
428
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100429 usbhid->ctrl[usbhid->ctrlhead].report = report;
430 usbhid->ctrl[usbhid->ctrlhead].dir = dir;
431 usbhid->ctrlhead = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100433 if (!test_and_set_bit(HID_CTRL_RUNNING, &usbhid->iofl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (hid_submit_ctrl(hid))
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100435 clear_bit(HID_CTRL_RUNNING, &usbhid->iofl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100437 spin_unlock_irqrestore(&usbhid->ctrllock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
Jiri Kosina229695e2006-12-08 18:40:53 +0100440static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
Jiri Kosinadde58452006-12-08 18:40:44 +0100441{
442 struct hid_device *hid = dev->private;
443 struct hid_field *field;
444 int offset;
445
446 if (type == EV_FF)
447 return input_ff_event(dev, type, code, value);
448
449 if (type != EV_LED)
450 return -1;
451
452 if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) {
453 warn("event field not found");
454 return -1;
455 }
456
457 hid_set_field(field, offset, value);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100458 usbhid_submit_report(hid, field->report, USB_DIR_OUT);
Jiri Kosinadde58452006-12-08 18:40:44 +0100459
460 return 0;
461}
462
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100463int usbhid_wait_io(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100465 struct usbhid_device *usbhid = hid->driver_data;
466
467 if (!wait_event_timeout(hid->wait, (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) &&
468 !test_bit(HID_OUT_RUNNING, &usbhid->iofl)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 10*HZ)) {
470 dbg("timeout waiting for ctrl or out queue to clear");
471 return -1;
472 }
473
474 return 0;
475}
476
Vojtech Pavlik854561b2005-05-29 02:28:00 -0500477static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle)
478{
Vojtech Pavlik71387bd2005-05-29 02:28:14 -0500479 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
Vojtech Pavlik854561b2005-05-29 02:28:00 -0500480 HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report,
481 ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
482}
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484static 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;
488
Jiri Kosina43c7bf02007-01-26 12:58:24 +0100489 memset(buf, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
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}
499
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100500int usbhid_open(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Alan Sternaef4e262006-01-31 12:58:38 -0500502 ++hid->open;
503 if (hid_start_in(hid))
504 hid_io_error(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return 0;
506}
507
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100508void usbhid_close(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100510 struct usbhid_device *usbhid = hid->driver_data;
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (!--hid->open)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100513 usb_kill_urb(usbhid->urbin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
516/*
517 * Initialize all reports
518 */
519
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100520void usbhid_init_reports(struct hid_device *hid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
522 struct hid_report *report;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100523 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 int err, ret;
525
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500526 list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100527 usbhid_submit_report(hid, report, USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100530 usbhid_submit_report(hid, report, USB_DIR_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 err = 0;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100533 ret = usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 while (ret) {
535 err |= ret;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100536 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
542
543 if (err)
Olaf Heringde289fd2006-01-06 12:45:28 +0100544 warn("timeout initializing reports");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500547/*
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200548 * 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
585/*
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500586 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid)
603{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100604 struct usbhid_device *usbhid = hid->driver_data;
605
606 if (!(usbhid->inbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->inbuf_dma)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return -1;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100608 if (!(usbhid->outbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->outbuf_dma)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return -1;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100610 if (!(usbhid->cr = usb_buffer_alloc(dev, sizeof(*(usbhid->cr)), GFP_ATOMIC, &usbhid->cr_dma)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return -1;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100612 if (!(usbhid->ctrlbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->ctrlbuf_dma)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return -1;
614
615 return 0;
616}
617
618static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid)
619{
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100620 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
Vojtech Pavlik940824b2006-01-14 00:25:39 -0500632/*
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
Geoff Levand4a1a4d82007-01-15 20:11:52 -0800646/*
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
Jiri Kosinab55fd232007-02-21 19:27:49 +0100672/*
Jiri Kosina85cbea32007-04-05 12:23:09 +0200673 * Certain Logitech keyboards send in report #3 keys which are far
Jiri Kosinab55fd232007-02-21 19:27:49 +0100674 * above the logical maximum described in descriptor. This extends
675 * the original value of 0x28c of logical maximum to 0x104d
676 */
Jiri Kosina85cbea32007-04-05 12:23:09 +0200677static void hid_fixup_logitech_descriptor(unsigned char *rdesc, int rsize)
Jiri Kosinab55fd232007-02-21 19:27:49 +0100678{
679 if (rsize >= 90 && rdesc[83] == 0x26
680 && rdesc[84] == 0x8c
681 && rdesc[85] == 0x02) {
Jiri Kosina85cbea32007-04-05 12:23:09 +0200682 info("Fixing up Logitech keyboard report descriptor");
Jiri Kosinab55fd232007-02-21 19:27:49 +0100683 rdesc[84] = rdesc[89] = 0x4d;
684 rdesc[85] = rdesc[90] = 0x10;
685 }
686}
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688static 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;
Paul Walmsley2eb5dc32007-04-19 13:27:04 +0200694 u32 quirks = 0;
Paul Walmsley4cbe7d22007-03-20 19:23:16 +0100695 unsigned rsize = 0;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500696 char *rdesc;
697 int n, len, insize = 0;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100698 struct usbhid_device *usbhid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Paul Walmsley2eb5dc32007-04-19 13:27:04 +0200700 quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
701 le16_to_cpu(dev->descriptor.idProduct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Alan Stern0f28b552006-05-15 14:49:04 -0400703 /* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (quirks & HID_QUIRK_IGNORE)
712 return NULL;
713
Soeren Sonnenburga417a212007-02-05 10:06:01 +0100714 if ((quirks & HID_QUIRK_IGNORE_MOUSE) &&
715 (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE))
716 return NULL;
717
718
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500719 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
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
Vojtech Pavlik854561b2005-05-29 02:28:00 -0500740 hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0);
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 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
Vojtech Pavlik940824b2006-01-14 00:25:39 -0500748 if ((quirks & HID_QUIRK_CYMOTION))
749 hid_fixup_cymotion_descriptor(rdesc, rsize);
750
Jiri Kosina85cbea32007-04-05 12:23:09 +0200751 if (quirks & HID_QUIRK_LOGITECH_DESCRIPTOR)
752 hid_fixup_logitech_descriptor(rdesc, rsize);
Jiri Kosinab55fd232007-02-21 19:27:49 +0100753
Jiri Kosinafdc9c562007-02-19 14:15:59 +0100754#ifdef CONFIG_HID_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 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
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100770 if (!(usbhid = kzalloc(sizeof(struct usbhid_device), GFP_KERNEL)))
771 goto fail;
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500772
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100773 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);
780
781 if (usbhid->bufsize > HID_MAX_BUFFER_SIZE)
782 usbhid->bufsize = HID_MAX_BUFFER_SIZE;
Michael Haboustakbf0964d2005-09-05 00:12:01 -0500783
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 interval = endpoint->bInterval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 /* Change the polling interval of mice. */
807 if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
808 interval = hid_mousepoll_interval;
Dmitry Torokhov05f091a2005-05-29 02:29:01 -0500809
Luiz Fernando N. Capitulino0f12aa02006-10-26 13:02:51 -0300810 if (usb_endpoint_dir_in(endpoint)) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100811 if (usbhid->urbin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 continue;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100813 if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 goto fail;
815 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100816 usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 hid_irq_in, hid, interval);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100818 usbhid->urbin->transfer_dma = usbhid->inbuf_dma;
819 usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 } else {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100821 if (usbhid->urbout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 continue;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100823 if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 goto fail;
825 pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100826 usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 hid_irq_out, hid, interval);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100828 usbhid->urbout->transfer_dma = usbhid->outbuf_dma;
829 usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831 }
832
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100833 if (!usbhid->urbin) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 err("couldn't find an input interrupt endpoint");
835 goto fail;
836 }
837
838 init_waitqueue_head(&hid->wait);
839
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100840 INIT_WORK(&usbhid->reset_work, hid_reset);
841 setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid);
Alan Sternaef4e262006-01-31 12:58:38 -0500842
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100843 spin_lock_init(&usbhid->inlock);
844 spin_lock_init(&usbhid->outlock);
845 spin_lock_init(&usbhid->ctrllock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 hid->version = le16_to_cpu(hdesc->bcdHID);
848 hid->country = hdesc->bCountryCode;
Anssi Hannulabe820972007-01-19 19:28:17 +0200849 hid->dev = &intf->dev;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100850 usbhid->intf = intf;
851 usbhid->ifnum = interface->desc.bInterfaceNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 hid->name[0] = 0;
854
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500855 if (dev->manufacturer)
856 strlcpy(hid->name, dev->manufacturer, sizeof(hid->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500858 if (dev->product) {
859 if (dev->manufacturer)
860 strlcat(hid->name, " ", sizeof(hid->name));
861 strlcat(hid->name, dev->product, sizeof(hid->name));
862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500864 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));
868
Jiri Kosina229695e2006-12-08 18:40:53 +0100869 hid->bus = BUS_USB;
Julien BLACHE9fa2ad52007-02-11 18:20:25 +0100870 hid->vendor = le16_to_cpu(dev->descriptor.idVendor);
871 hid->product = le16_to_cpu(dev->descriptor.idProduct);
Jiri Kosina229695e2006-12-08 18:40:53 +0100872
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500873 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0)
881 hid->uniq[0] = 0;
882
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100883 usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL);
884 if (!usbhid->urbctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 goto fail;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500886
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100887 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);
Jiri Kosina229695e2006-12-08 18:40:53 +0100892 hid->hidinput_input_event = usb_hidinput_input_event;
Jiri Kosina7c379142007-01-24 11:54:19 +0100893 hid->hid_open = usbhid_open;
894 hid->hid_close = usbhid_close;
Jiri Kosinaaa938f72006-12-08 18:41:10 +0100895#ifdef CONFIG_USB_HIDDEV
896 hid->hiddev_hid_event = hiddev_hid_event;
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +0100897 hid->hiddev_report_event = hiddev_report_event;
Jiri Kosinaaa938f72006-12-08 18:41:10 +0100898#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return hid;
900
901fail:
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100902 usb_free_urb(usbhid->urbin);
903 usb_free_urb(usbhid->urbout);
904 usb_free_urb(usbhid->urbctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 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);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100914 struct usbhid_device *usbhid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 if (!hid)
917 return;
918
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100919 usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100921 spin_lock_irq(&usbhid->inlock); /* Sync with error handler */
922 usb_set_intfdata(intf, NULL);
923 spin_unlock_irq(&usbhid->inlock);
924 usb_kill_urb(usbhid->urbin);
925 usb_kill_urb(usbhid->urbout);
926 usb_kill_urb(usbhid->urbctrl);
927
928 del_timer_sync(&usbhid->io_retry);
Alan Sternaef4e262006-01-31 12:58:38 -0500929 flush_scheduled_work();
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (hid->claimed & HID_CLAIMED_INPUT)
932 hidinput_disconnect(hid);
933 if (hid->claimed & HID_CLAIMED_HIDDEV)
934 hiddev_disconnect(hid);
935
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100936 usb_free_urb(usbhid->urbin);
937 usb_free_urb(usbhid->urbctrl);
938 usb_free_urb(usbhid->urbout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Anssi Hannulabe820972007-01-19 19:28:17 +0200940 hid_free_buffers(hid_to_usb_dev(hid), hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 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)))
Stelian Pop479f6ea2005-06-22 17:53:28 +0200955 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100957 usbhid_init_reports(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 hid_dump_device(hid);
Pete Zaitcev713c8aa2007-04-06 14:33:18 +0200959 if (hid->quirks & HID_QUIRK_RESET_LEDS)
960 usbhid_set_leds(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
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);
Stelian Pop479f6ea2005-06-22 17:53:28 +0200972 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
974
Anssi Hannulac4146062007-01-11 16:51:16 +0200975 if ((hid->claimed & HID_CLAIMED_INPUT))
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100976 hid_ff_init(hid);
977
Geoff Levand4a1a4d82007-01-15 20:11:52 -0800978 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 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
David Brownell27d72e82005-04-18 17:39:22 -07001009static int hid_suspend(struct usb_interface *intf, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
1011 struct hid_device *hid = usb_get_intfdata (intf);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001012 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001014 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 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);
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001026 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 int status;
1028
Jiri Kosina4916b3a2006-12-08 18:41:03 +01001029 clear_bit(HID_SUSPENDED, &usbhid->iofl);
1030 usbhid->retry_delay = 0;
Alan Sternaef4e262006-01-31 12:58:38 -05001031 status = hid_start_in(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 dev_dbg(&intf->dev, "resume status %d\n", status);
1033 return status;
1034}
1035
Alan Sterndf9a1f42006-06-01 13:55:28 -04001036/* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053static 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 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 .name = "usbhid",
1063 .probe = hid_probe,
1064 .disconnect = hid_disconnect,
1065 .suspend = hid_suspend,
1066 .resume = hid_resume,
Alan Sterndf9a1f42006-06-01 13:55:28 -04001067 .pre_reset = hid_pre_reset,
1068 .post_reset = hid_post_reset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 .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);