]>
Commit | Line | Data |
---|---|---|
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 | |
0361a28d | 7 | * Copyright (c) 2007-2008 Oliver Neukum |
7d39e849 | 8 | * Copyright (c) 2006-2010 Jiri Kosina |
1da177e4 LT |
9 | */ |
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> | |
1da177e4 LT |
22 | #include <linux/list.h> |
23 | #include <linux/mm.h> | |
3d5afd32 | 24 | #include <linux/mutex.h> |
1da177e4 LT |
25 | #include <linux/spinlock.h> |
26 | #include <asm/unaligned.h> | |
27 | #include <asm/byteorder.h> | |
28 | #include <linux/input.h> | |
29 | #include <linux/wait.h> | |
0361a28d | 30 | #include <linux/workqueue.h> |
dc3c78e4 | 31 | #include <linux/string.h> |
1da177e4 | 32 | |
1da177e4 LT |
33 | #include <linux/usb.h> |
34 | ||
dde5845a | 35 | #include <linux/hid.h> |
1da177e4 | 36 | #include <linux/hiddev.h> |
c080d89a | 37 | #include <linux/hid-debug.h> |
86166b7b | 38 | #include <linux/hidraw.h> |
4916b3a5 | 39 | #include "usbhid.h" |
1da177e4 LT |
40 | |
41 | /* | |
42 | * Version Information | |
43 | */ | |
44 | ||
1da177e4 | 45 | #define DRIVER_DESC "USB HID core driver" |
1da177e4 | 46 | |
1da177e4 LT |
47 | /* |
48 | * Module parameters. | |
49 | */ | |
50 | ||
51 | static unsigned int hid_mousepoll_interval; | |
52 | module_param_named(mousepoll, hid_mousepoll_interval, uint, 0644); | |
53 | MODULE_PARM_DESC(mousepoll, "Polling interval of mice"); | |
54 | ||
933bfe4d TJ |
55 | static unsigned int hid_jspoll_interval; |
56 | module_param_named(jspoll, hid_jspoll_interval, uint, 0644); | |
57 | MODULE_PARM_DESC(jspoll, "Polling interval of joysticks"); | |
58 | ||
0361a28d ON |
59 | static unsigned int ignoreled; |
60 | module_param_named(ignoreled, ignoreled, uint, 0644); | |
61 | MODULE_PARM_DESC(ignoreled, "Autosuspend with active leds"); | |
62 | ||
876b9276 | 63 | /* Quirks specified at module load time */ |
81ba9926 | 64 | static char *quirks_param[MAX_USBHID_BOOT_QUIRKS]; |
876b9276 PW |
65 | module_param_array_named(quirks, quirks_param, charp, NULL, 0444); |
66 | MODULE_PARM_DESC(quirks, "Add/modify USB HID quirks by specifying " | |
67 | " quirks=vendorID:productID:quirks" | |
68 | " where vendorID, productID, and quirks are all in" | |
69 | " 0x-prefixed hex"); | |
1da177e4 | 70 | /* |
48b4554a | 71 | * Input submission and I/O error handler. |
1da177e4 | 72 | */ |
0361a28d | 73 | static DEFINE_MUTEX(hid_open_mut); |
1da177e4 | 74 | |
48b4554a | 75 | static void hid_io_error(struct hid_device *hid); |
0361a28d ON |
76 | static int hid_submit_out(struct hid_device *hid); |
77 | static int hid_submit_ctrl(struct hid_device *hid); | |
78 | static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid); | |
48b4554a JK |
79 | |
80 | /* Start up the input URB */ | |
81 | static int hid_start_in(struct hid_device *hid) | |
1da177e4 | 82 | { |
1da177e4 | 83 | unsigned long flags; |
48b4554a JK |
84 | int rc = 0; |
85 | struct usbhid_device *usbhid = hid->driver_data; | |
1da177e4 | 86 | |
0361a28d | 87 | spin_lock_irqsave(&usbhid->lock, flags); |
0b750b3b | 88 | if ((hid->open > 0 || hid->quirks & HID_QUIRK_ALWAYS_POLL) && |
69626f23 | 89 | !test_bit(HID_DISCONNECTED, &usbhid->iofl) && |
f2b5264d | 90 | !test_bit(HID_SUSPENDED, &usbhid->iofl) && |
48b4554a JK |
91 | !test_and_set_bit(HID_IN_RUNNING, &usbhid->iofl)) { |
92 | rc = usb_submit_urb(usbhid->urbin, GFP_ATOMIC); | |
a8c52b66 | 93 | if (rc != 0) { |
48b4554a | 94 | clear_bit(HID_IN_RUNNING, &usbhid->iofl); |
a8c52b66 ON |
95 | if (rc == -ENOSPC) |
96 | set_bit(HID_NO_BANDWIDTH, &usbhid->iofl); | |
97 | } else { | |
98 | clear_bit(HID_NO_BANDWIDTH, &usbhid->iofl); | |
99 | } | |
1da177e4 | 100 | } |
0361a28d | 101 | spin_unlock_irqrestore(&usbhid->lock, flags); |
48b4554a | 102 | return rc; |
1da177e4 LT |
103 | } |
104 | ||
48b4554a JK |
105 | /* I/O retry timer routine */ |
106 | static void hid_retry_timeout(unsigned long _hid) | |
1da177e4 | 107 | { |
48b4554a | 108 | struct hid_device *hid = (struct hid_device *) _hid; |
4916b3a5 | 109 | struct usbhid_device *usbhid = hid->driver_data; |
1da177e4 | 110 | |
48b4554a JK |
111 | dev_dbg(&usbhid->intf->dev, "retrying intr urb\n"); |
112 | if (hid_start_in(hid)) | |
113 | hid_io_error(hid); | |
1da177e4 LT |
114 | } |
115 | ||
48b4554a JK |
116 | /* Workqueue routine to reset the device or clear a halt */ |
117 | static void hid_reset(struct work_struct *work) | |
1da177e4 | 118 | { |
48b4554a JK |
119 | struct usbhid_device *usbhid = |
120 | container_of(work, struct usbhid_device, reset_work); | |
121 | struct hid_device *hid = usbhid->hid; | |
8f507ef5 | 122 | int rc; |
1da177e4 | 123 | |
48b4554a JK |
124 | if (test_bit(HID_CLEAR_HALT, &usbhid->iofl)) { |
125 | dev_dbg(&usbhid->intf->dev, "clear halt\n"); | |
126 | rc = usb_clear_halt(hid_to_usb_dev(hid), usbhid->urbin->pipe); | |
127 | clear_bit(HID_CLEAR_HALT, &usbhid->iofl); | |
011b15df | 128 | if (rc == 0) { |
8f507ef5 AS |
129 | hid_start_in(hid); |
130 | } else { | |
131 | dev_dbg(&usbhid->intf->dev, | |
132 | "clear-halt failed: %d\n", rc); | |
133 | set_bit(HID_RESET_PENDING, &usbhid->iofl); | |
1da177e4 | 134 | } |
1da177e4 LT |
135 | } |
136 | ||
8f507ef5 AS |
137 | if (test_bit(HID_RESET_PENDING, &usbhid->iofl)) { |
138 | dev_dbg(&usbhid->intf->dev, "resetting device\n"); | |
139 | usb_queue_reset_device(usbhid->intf); | |
1da177e4 | 140 | } |
1da177e4 LT |
141 | } |
142 | ||
48b4554a JK |
143 | /* Main I/O error handler */ |
144 | static void hid_io_error(struct hid_device *hid) | |
dde5845a | 145 | { |
48b4554a JK |
146 | unsigned long flags; |
147 | struct usbhid_device *usbhid = hid->driver_data; | |
dde5845a | 148 | |
0361a28d | 149 | spin_lock_irqsave(&usbhid->lock, flags); |
dde5845a | 150 | |
48b4554a | 151 | /* Stop when disconnected */ |
69626f23 | 152 | if (test_bit(HID_DISCONNECTED, &usbhid->iofl)) |
48b4554a | 153 | goto done; |
dde5845a | 154 | |
5e2a55f2 AS |
155 | /* If it has been a while since the last error, we'll assume |
156 | * this a brand new error and reset the retry timeout. */ | |
157 | if (time_after(jiffies, usbhid->stop_retry + HZ/2)) | |
158 | usbhid->retry_delay = 0; | |
159 | ||
48b4554a JK |
160 | /* When an error occurs, retry at increasing intervals */ |
161 | if (usbhid->retry_delay == 0) { | |
162 | usbhid->retry_delay = 13; /* Then 26, 52, 104, 104, ... */ | |
163 | usbhid->stop_retry = jiffies + msecs_to_jiffies(1000); | |
164 | } else if (usbhid->retry_delay < 100) | |
165 | usbhid->retry_delay *= 2; | |
dde5845a | 166 | |
48b4554a | 167 | if (time_after(jiffies, usbhid->stop_retry)) { |
4916b3a5 | 168 | |
a8c52b66 | 169 | /* Retries failed, so do a port reset unless we lack bandwidth*/ |
3af4e5a9 | 170 | if (!test_bit(HID_NO_BANDWIDTH, &usbhid->iofl) |
a8c52b66 ON |
171 | && !test_and_set_bit(HID_RESET_PENDING, &usbhid->iofl)) { |
172 | ||
48b4554a JK |
173 | schedule_work(&usbhid->reset_work); |
174 | goto done; | |
175 | } | |
1da177e4 LT |
176 | } |
177 | ||
48b4554a JK |
178 | mod_timer(&usbhid->io_retry, |
179 | jiffies + msecs_to_jiffies(usbhid->retry_delay)); | |
180 | done: | |
0361a28d ON |
181 | spin_unlock_irqrestore(&usbhid->lock, flags); |
182 | } | |
183 | ||
184 | static void usbhid_mark_busy(struct usbhid_device *usbhid) | |
185 | { | |
186 | struct usb_interface *intf = usbhid->intf; | |
187 | ||
188 | usb_mark_last_busy(interface_to_usbdev(intf)); | |
189 | } | |
190 | ||
191 | static int usbhid_restart_out_queue(struct usbhid_device *usbhid) | |
192 | { | |
193 | struct hid_device *hid = usb_get_intfdata(usbhid->intf); | |
194 | int kicked; | |
f0befcd6 | 195 | int r; |
0361a28d | 196 | |
d4150c8f AS |
197 | if (!hid || test_bit(HID_RESET_PENDING, &usbhid->iofl) || |
198 | test_bit(HID_SUSPENDED, &usbhid->iofl)) | |
0361a28d ON |
199 | return 0; |
200 | ||
201 | if ((kicked = (usbhid->outhead != usbhid->outtail))) { | |
6cc203d7 | 202 | hid_dbg(hid, "Kicking head %d tail %d", usbhid->outhead, usbhid->outtail); |
f0befcd6 | 203 | |
01a7c984 | 204 | /* Try to wake up from autosuspend... */ |
f0befcd6 DK |
205 | r = usb_autopm_get_interface_async(usbhid->intf); |
206 | if (r < 0) | |
207 | return r; | |
01a7c984 AS |
208 | |
209 | /* | |
210 | * If still suspended, don't submit. Submission will | |
211 | * occur if/when resume drains the queue. | |
212 | */ | |
f2b5264d | 213 | if (test_bit(HID_SUSPENDED, &usbhid->iofl)) { |
01a7c984 AS |
214 | usb_autopm_put_interface_no_suspend(usbhid->intf); |
215 | return r; | |
216 | } | |
217 | ||
f0befcd6 DK |
218 | /* Asynchronously flush queue. */ |
219 | set_bit(HID_OUT_RUNNING, &usbhid->iofl); | |
0361a28d ON |
220 | if (hid_submit_out(hid)) { |
221 | clear_bit(HID_OUT_RUNNING, &usbhid->iofl); | |
f0befcd6 | 222 | usb_autopm_put_interface_async(usbhid->intf); |
0361a28d | 223 | } |
f0befcd6 | 224 | wake_up(&usbhid->wait); |
0361a28d ON |
225 | } |
226 | return kicked; | |
227 | } | |
228 | ||
229 | static int usbhid_restart_ctrl_queue(struct usbhid_device *usbhid) | |
230 | { | |
231 | struct hid_device *hid = usb_get_intfdata(usbhid->intf); | |
232 | int kicked; | |
f0befcd6 | 233 | int r; |
0361a28d ON |
234 | |
235 | WARN_ON(hid == NULL); | |
d4150c8f AS |
236 | if (!hid || test_bit(HID_RESET_PENDING, &usbhid->iofl) || |
237 | test_bit(HID_SUSPENDED, &usbhid->iofl)) | |
0361a28d ON |
238 | return 0; |
239 | ||
240 | if ((kicked = (usbhid->ctrlhead != usbhid->ctrltail))) { | |
6cc203d7 | 241 | hid_dbg(hid, "Kicking head %d tail %d", usbhid->ctrlhead, usbhid->ctrltail); |
f0befcd6 | 242 | |
01a7c984 | 243 | /* Try to wake up from autosuspend... */ |
f0befcd6 DK |
244 | r = usb_autopm_get_interface_async(usbhid->intf); |
245 | if (r < 0) | |
246 | return r; | |
01a7c984 AS |
247 | |
248 | /* | |
249 | * If still suspended, don't submit. Submission will | |
250 | * occur if/when resume drains the queue. | |
251 | */ | |
f2b5264d | 252 | if (test_bit(HID_SUSPENDED, &usbhid->iofl)) { |
01a7c984 AS |
253 | usb_autopm_put_interface_no_suspend(usbhid->intf); |
254 | return r; | |
255 | } | |
256 | ||
f0befcd6 DK |
257 | /* Asynchronously flush queue. */ |
258 | set_bit(HID_CTRL_RUNNING, &usbhid->iofl); | |
0361a28d ON |
259 | if (hid_submit_ctrl(hid)) { |
260 | clear_bit(HID_CTRL_RUNNING, &usbhid->iofl); | |
f0befcd6 | 261 | usb_autopm_put_interface_async(usbhid->intf); |
0361a28d | 262 | } |
f0befcd6 | 263 | wake_up(&usbhid->wait); |
0361a28d ON |
264 | } |
265 | return kicked; | |
1da177e4 LT |
266 | } |
267 | ||
48b4554a JK |
268 | /* |
269 | * Input interrupt completion handler. | |
270 | */ | |
854561b0 | 271 | |
48b4554a | 272 | static void hid_irq_in(struct urb *urb) |
1da177e4 | 273 | { |
48b4554a JK |
274 | struct hid_device *hid = urb->context; |
275 | struct usbhid_device *usbhid = hid->driver_data; | |
276 | int status; | |
1da177e4 | 277 | |
48b4554a | 278 | switch (urb->status) { |
880d29f1 JS |
279 | case 0: /* success */ |
280 | usbhid->retry_delay = 0; | |
0b750b3b JH |
281 | if ((hid->quirks & HID_QUIRK_ALWAYS_POLL) && !hid->open) |
282 | break; | |
cc8a9d79 | 283 | usbhid_mark_busy(usbhid); |
b905811a BT |
284 | if (!test_bit(HID_RESUME_RUNNING, &usbhid->iofl)) { |
285 | hid_input_report(urb->context, HID_INPUT_REPORT, | |
286 | urb->transfer_buffer, | |
287 | urb->actual_length, 1); | |
288 | /* | |
289 | * autosuspend refused while keys are pressed | |
290 | * because most keyboards don't wake up when | |
291 | * a key is released | |
292 | */ | |
293 | if (hid_check_keys_pressed(hid)) | |
294 | set_bit(HID_KEYS_PRESSED, &usbhid->iofl); | |
295 | else | |
296 | clear_bit(HID_KEYS_PRESSED, &usbhid->iofl); | |
297 | } | |
880d29f1 JS |
298 | break; |
299 | case -EPIPE: /* stall */ | |
0361a28d | 300 | usbhid_mark_busy(usbhid); |
880d29f1 JS |
301 | clear_bit(HID_IN_RUNNING, &usbhid->iofl); |
302 | set_bit(HID_CLEAR_HALT, &usbhid->iofl); | |
303 | schedule_work(&usbhid->reset_work); | |
304 | return; | |
305 | case -ECONNRESET: /* unlink */ | |
306 | case -ENOENT: | |
307 | case -ESHUTDOWN: /* unplug */ | |
308 | clear_bit(HID_IN_RUNNING, &usbhid->iofl); | |
309 | return; | |
310 | case -EILSEQ: /* protocol error or unplug */ | |
311 | case -EPROTO: /* protocol error or unplug */ | |
312 | case -ETIME: /* protocol error or unplug */ | |
313 | case -ETIMEDOUT: /* Should never happen, but... */ | |
0361a28d | 314 | usbhid_mark_busy(usbhid); |
880d29f1 JS |
315 | clear_bit(HID_IN_RUNNING, &usbhid->iofl); |
316 | hid_io_error(hid); | |
317 | return; | |
318 | default: /* error */ | |
4291ee30 JP |
319 | hid_warn(urb->dev, "input irq status %d received\n", |
320 | urb->status); | |
48b4554a | 321 | } |
1da177e4 | 322 | |
48b4554a JK |
323 | status = usb_submit_urb(urb, GFP_ATOMIC); |
324 | if (status) { | |
325 | clear_bit(HID_IN_RUNNING, &usbhid->iofl); | |
326 | if (status != -EPERM) { | |
4291ee30 JP |
327 | hid_err(hid, "can't resubmit intr, %s-%s/input%d, status %d\n", |
328 | hid_to_usb_dev(hid)->bus->bus_name, | |
329 | hid_to_usb_dev(hid)->devpath, | |
330 | usbhid->ifnum, status); | |
48b4554a JK |
331 | hid_io_error(hid); |
332 | } | |
333 | } | |
1da177e4 LT |
334 | } |
335 | ||
48b4554a | 336 | static int hid_submit_out(struct hid_device *hid) |
1da177e4 | 337 | { |
48b4554a | 338 | struct hid_report *report; |
f129ea6d | 339 | char *raw_report; |
4916b3a5 | 340 | struct usbhid_device *usbhid = hid->driver_data; |
68229689 | 341 | int r; |
4916b3a5 | 342 | |
f129ea6d AH |
343 | report = usbhid->out[usbhid->outtail].report; |
344 | raw_report = usbhid->out[usbhid->outtail].raw_report; | |
1da177e4 | 345 | |
dabb05c6 | 346 | usbhid->urbout->transfer_buffer_length = hid_report_len(report); |
f0befcd6 | 347 | usbhid->urbout->dev = hid_to_usb_dev(hid); |
668160e5 AS |
348 | if (raw_report) { |
349 | memcpy(usbhid->outbuf, raw_report, | |
350 | usbhid->urbout->transfer_buffer_length); | |
351 | kfree(raw_report); | |
352 | usbhid->out[usbhid->outtail].raw_report = NULL; | |
353 | } | |
1d3e2023 | 354 | |
f0befcd6 | 355 | dbg_hid("submitting out urb\n"); |
d57cdcff | 356 | |
f0befcd6 DK |
357 | r = usb_submit_urb(usbhid->urbout, GFP_ATOMIC); |
358 | if (r < 0) { | |
359 | hid_err(hid, "usb_submit_urb(out) failed: %d\n", r); | |
360 | return r; | |
48b4554a | 361 | } |
f0befcd6 | 362 | usbhid->last_out = jiffies; |
48b4554a JK |
363 | return 0; |
364 | } | |
365 | ||
366 | static int hid_submit_ctrl(struct hid_device *hid) | |
1da177e4 LT |
367 | { |
368 | struct hid_report *report; | |
48b4554a | 369 | unsigned char dir; |
f129ea6d | 370 | char *raw_report; |
68229689 | 371 | int len, r; |
4916b3a5 | 372 | struct usbhid_device *usbhid = hid->driver_data; |
1da177e4 | 373 | |
48b4554a | 374 | report = usbhid->ctrl[usbhid->ctrltail].report; |
f129ea6d | 375 | raw_report = usbhid->ctrl[usbhid->ctrltail].raw_report; |
48b4554a | 376 | dir = usbhid->ctrl[usbhid->ctrltail].dir; |
1da177e4 | 377 | |
f0befcd6 DK |
378 | len = ((report->size - 1) >> 3) + 1 + (report->id > 0); |
379 | if (dir == USB_DIR_OUT) { | |
380 | usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0); | |
381 | usbhid->urbctrl->transfer_buffer_length = len; | |
668160e5 AS |
382 | if (raw_report) { |
383 | memcpy(usbhid->ctrlbuf, raw_report, len); | |
384 | kfree(raw_report); | |
385 | usbhid->ctrl[usbhid->ctrltail].raw_report = NULL; | |
386 | } | |
f0befcd6 DK |
387 | } else { |
388 | int maxpacket, padlen; | |
389 | ||
390 | usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0); | |
391 | maxpacket = usb_maxpacket(hid_to_usb_dev(hid), | |
392 | usbhid->urbctrl->pipe, 0); | |
393 | if (maxpacket > 0) { | |
394 | padlen = DIV_ROUND_UP(len, maxpacket); | |
395 | padlen *= maxpacket; | |
396 | if (padlen > usbhid->bufsize) | |
397 | padlen = usbhid->bufsize; | |
398 | } else | |
399 | padlen = 0; | |
400 | usbhid->urbctrl->transfer_buffer_length = padlen; | |
48b4554a | 401 | } |
f0befcd6 DK |
402 | usbhid->urbctrl->dev = hid_to_usb_dev(hid); |
403 | ||
404 | usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir; | |
405 | usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT : | |
406 | HID_REQ_GET_REPORT; | |
407 | usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) | | |
408 | report->id); | |
409 | usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum); | |
410 | usbhid->cr->wLength = cpu_to_le16(len); | |
411 | ||
412 | dbg_hid("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u\n", | |
413 | usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" : | |
414 | "Get_Report", | |
415 | usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength); | |
416 | ||
417 | r = usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC); | |
418 | if (r < 0) { | |
419 | hid_err(hid, "usb_submit_urb(ctrl) failed: %d\n", r); | |
420 | return r; | |
421 | } | |
422 | usbhid->last_ctrl = jiffies; | |
48b4554a JK |
423 | return 0; |
424 | } | |
1da177e4 | 425 | |
48b4554a JK |
426 | /* |
427 | * Output interrupt completion handler. | |
428 | */ | |
1da177e4 | 429 | |
48b4554a JK |
430 | static void hid_irq_out(struct urb *urb) |
431 | { | |
432 | struct hid_device *hid = urb->context; | |
433 | struct usbhid_device *usbhid = hid->driver_data; | |
434 | unsigned long flags; | |
435 | int unplug = 0; | |
1da177e4 | 436 | |
48b4554a | 437 | switch (urb->status) { |
880d29f1 JS |
438 | case 0: /* success */ |
439 | break; | |
440 | case -ESHUTDOWN: /* unplug */ | |
441 | unplug = 1; | |
442 | case -EILSEQ: /* protocol error or unplug */ | |
443 | case -EPROTO: /* protocol error or unplug */ | |
444 | case -ECONNRESET: /* unlink */ | |
445 | case -ENOENT: | |
446 | break; | |
447 | default: /* error */ | |
4291ee30 JP |
448 | hid_warn(urb->dev, "output irq status %d received\n", |
449 | urb->status); | |
48b4554a | 450 | } |
1da177e4 | 451 | |
0361a28d | 452 | spin_lock_irqsave(&usbhid->lock, flags); |
1da177e4 | 453 | |
93101af3 | 454 | if (unplug) { |
48b4554a | 455 | usbhid->outtail = usbhid->outhead; |
93101af3 | 456 | } else { |
48b4554a | 457 | usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1); |
1da177e4 | 458 | |
93101af3 AS |
459 | if (usbhid->outhead != usbhid->outtail && |
460 | hid_submit_out(hid) == 0) { | |
461 | /* Successfully submitted next urb in queue */ | |
462 | spin_unlock_irqrestore(&usbhid->lock, flags); | |
463 | return; | |
464 | } | |
48b4554a | 465 | } |
1da177e4 | 466 | |
48b4554a | 467 | clear_bit(HID_OUT_RUNNING, &usbhid->iofl); |
0361a28d | 468 | spin_unlock_irqrestore(&usbhid->lock, flags); |
68229689 | 469 | usb_autopm_put_interface_async(usbhid->intf); |
1d1bdd20 | 470 | wake_up(&usbhid->wait); |
48b4554a | 471 | } |
6345fdfd | 472 | |
48b4554a JK |
473 | /* |
474 | * Control pipe completion handler. | |
475 | */ | |
1da177e4 | 476 | |
48b4554a JK |
477 | static void hid_ctrl(struct urb *urb) |
478 | { | |
479 | struct hid_device *hid = urb->context; | |
480 | struct usbhid_device *usbhid = hid->driver_data; | |
0361a28d | 481 | int unplug = 0, status = urb->status; |
1da177e4 | 482 | |
0361a28d | 483 | switch (status) { |
880d29f1 JS |
484 | case 0: /* success */ |
485 | if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN) | |
486 | hid_input_report(urb->context, | |
487 | usbhid->ctrl[usbhid->ctrltail].report->type, | |
488 | urb->transfer_buffer, urb->actual_length, 0); | |
489 | break; | |
490 | case -ESHUTDOWN: /* unplug */ | |
491 | unplug = 1; | |
492 | case -EILSEQ: /* protocol error or unplug */ | |
493 | case -EPROTO: /* protocol error or unplug */ | |
494 | case -ECONNRESET: /* unlink */ | |
495 | case -ENOENT: | |
496 | case -EPIPE: /* report not available */ | |
497 | break; | |
498 | default: /* error */ | |
4291ee30 | 499 | hid_warn(urb->dev, "ctrl urb status %d received\n", status); |
48b4554a | 500 | } |
1da177e4 | 501 | |
e470127e IAR |
502 | spin_lock(&usbhid->lock); |
503 | ||
93101af3 | 504 | if (unplug) { |
48b4554a | 505 | usbhid->ctrltail = usbhid->ctrlhead; |
93101af3 | 506 | } else { |
48b4554a | 507 | usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1); |
1da177e4 | 508 | |
93101af3 AS |
509 | if (usbhid->ctrlhead != usbhid->ctrltail && |
510 | hid_submit_ctrl(hid) == 0) { | |
511 | /* Successfully submitted next urb in queue */ | |
512 | spin_unlock(&usbhid->lock); | |
513 | return; | |
514 | } | |
48b4554a | 515 | } |
1da177e4 | 516 | |
48b4554a | 517 | clear_bit(HID_CTRL_RUNNING, &usbhid->iofl); |
0361a28d | 518 | spin_unlock(&usbhid->lock); |
68229689 | 519 | usb_autopm_put_interface_async(usbhid->intf); |
1d1bdd20 | 520 | wake_up(&usbhid->wait); |
48b4554a | 521 | } |
1da177e4 | 522 | |
52cfc61b HS |
523 | static void __usbhid_submit_report(struct hid_device *hid, struct hid_report *report, |
524 | unsigned char dir) | |
48b4554a JK |
525 | { |
526 | int head; | |
48b4554a | 527 | struct usbhid_device *usbhid = hid->driver_data; |
1da177e4 | 528 | |
46df9ded RA |
529 | if (((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN) || |
530 | test_bit(HID_DISCONNECTED, &usbhid->iofl)) | |
48b4554a | 531 | return; |
b857c651 | 532 | |
48b4554a | 533 | if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) { |
48b4554a | 534 | if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) { |
4291ee30 | 535 | hid_warn(hid, "output queue full\n"); |
48b4554a JK |
536 | return; |
537 | } | |
1da177e4 | 538 | |
27ce4050 | 539 | usbhid->out[usbhid->outhead].raw_report = hid_alloc_report_buf(report, GFP_ATOMIC); |
f129ea6d | 540 | if (!usbhid->out[usbhid->outhead].raw_report) { |
4291ee30 | 541 | hid_warn(hid, "output queueing failed\n"); |
f129ea6d AH |
542 | return; |
543 | } | |
544 | hid_output_report(report, usbhid->out[usbhid->outhead].raw_report); | |
545 | usbhid->out[usbhid->outhead].report = report; | |
48b4554a | 546 | usbhid->outhead = head; |
4871d3be | 547 | |
01a7c984 AS |
548 | /* If the queue isn't running, restart it */ |
549 | if (!test_bit(HID_OUT_RUNNING, &usbhid->iofl)) { | |
550 | usbhid_restart_out_queue(usbhid); | |
f0befcd6 | 551 | |
01a7c984 AS |
552 | /* Otherwise see if an earlier request has timed out */ |
553 | } else if (time_after(jiffies, usbhid->last_out + HZ * 5)) { | |
554 | ||
555 | /* Prevent autosuspend following the unlink */ | |
556 | usb_autopm_get_interface_no_resume(usbhid->intf); | |
f0befcd6 | 557 | |
858155fb | 558 | /* |
01a7c984 AS |
559 | * Prevent resubmission in case the URB completes |
560 | * before we can unlink it. We don't want to cancel | |
561 | * the wrong transfer! | |
858155fb | 562 | */ |
01a7c984 AS |
563 | usb_block_urb(usbhid->urbout); |
564 | ||
565 | /* Drop lock to avoid deadlock if the callback runs */ | |
566 | spin_unlock(&usbhid->lock); | |
567 | ||
568 | usb_unlink_urb(usbhid->urbout); | |
569 | spin_lock(&usbhid->lock); | |
570 | usb_unblock_urb(usbhid->urbout); | |
571 | ||
572 | /* Unlink might have stopped the queue */ | |
573 | if (!test_bit(HID_OUT_RUNNING, &usbhid->iofl)) | |
574 | usbhid_restart_out_queue(usbhid); | |
575 | ||
576 | /* Now we can allow autosuspend again */ | |
577 | usb_autopm_put_interface_async(usbhid->intf); | |
858155fb | 578 | } |
48b4554a JK |
579 | return; |
580 | } | |
1da177e4 | 581 | |
48b4554a | 582 | if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) { |
4291ee30 | 583 | hid_warn(hid, "control queue full\n"); |
48b4554a JK |
584 | return; |
585 | } | |
8fd80133 | 586 | |
f129ea6d | 587 | if (dir == USB_DIR_OUT) { |
27ce4050 | 588 | usbhid->ctrl[usbhid->ctrlhead].raw_report = hid_alloc_report_buf(report, GFP_ATOMIC); |
f129ea6d | 589 | if (!usbhid->ctrl[usbhid->ctrlhead].raw_report) { |
4291ee30 | 590 | hid_warn(hid, "control queueing failed\n"); |
f129ea6d AH |
591 | return; |
592 | } | |
593 | hid_output_report(report, usbhid->ctrl[usbhid->ctrlhead].raw_report); | |
594 | } | |
48b4554a JK |
595 | usbhid->ctrl[usbhid->ctrlhead].report = report; |
596 | usbhid->ctrl[usbhid->ctrlhead].dir = dir; | |
597 | usbhid->ctrlhead = head; | |
8fd80133 | 598 | |
01a7c984 AS |
599 | /* If the queue isn't running, restart it */ |
600 | if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl)) { | |
601 | usbhid_restart_ctrl_queue(usbhid); | |
f0befcd6 | 602 | |
01a7c984 AS |
603 | /* Otherwise see if an earlier request has timed out */ |
604 | } else if (time_after(jiffies, usbhid->last_ctrl + HZ * 5)) { | |
605 | ||
606 | /* Prevent autosuspend following the unlink */ | |
607 | usb_autopm_get_interface_no_resume(usbhid->intf); | |
f0befcd6 | 608 | |
858155fb | 609 | /* |
01a7c984 AS |
610 | * Prevent resubmission in case the URB completes |
611 | * before we can unlink it. We don't want to cancel | |
612 | * the wrong transfer! | |
858155fb | 613 | */ |
01a7c984 AS |
614 | usb_block_urb(usbhid->urbctrl); |
615 | ||
616 | /* Drop lock to avoid deadlock if the callback runs */ | |
617 | spin_unlock(&usbhid->lock); | |
618 | ||
619 | usb_unlink_urb(usbhid->urbctrl); | |
620 | spin_lock(&usbhid->lock); | |
621 | usb_unblock_urb(usbhid->urbctrl); | |
622 | ||
623 | /* Unlink might have stopped the queue */ | |
624 | if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl)) | |
625 | usbhid_restart_ctrl_queue(usbhid); | |
626 | ||
627 | /* Now we can allow autosuspend again */ | |
628 | usb_autopm_put_interface_async(usbhid->intf); | |
858155fb | 629 | } |
0361a28d | 630 | } |
931e24b9 | 631 | |
d8814272 | 632 | static void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir) |
0361a28d ON |
633 | { |
634 | struct usbhid_device *usbhid = hid->driver_data; | |
635 | unsigned long flags; | |
931e24b9 | 636 | |
0361a28d ON |
637 | spin_lock_irqsave(&usbhid->lock, flags); |
638 | __usbhid_submit_report(hid, report, dir); | |
639 | spin_unlock_irqrestore(&usbhid->lock, flags); | |
48b4554a | 640 | } |
23b0d968 | 641 | |
b7966a4d | 642 | static int usbhid_wait_io(struct hid_device *hid) |
48b4554a JK |
643 | { |
644 | struct usbhid_device *usbhid = hid->driver_data; | |
25914662 | 645 | |
1d1bdd20 JS |
646 | if (!wait_event_timeout(usbhid->wait, |
647 | (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) && | |
648 | !test_bit(HID_OUT_RUNNING, &usbhid->iofl)), | |
48b4554a | 649 | 10*HZ)) { |
58037eb9 | 650 | dbg_hid("timeout waiting for ctrl or out queue to clear\n"); |
48b4554a JK |
651 | return -1; |
652 | } | |
1da177e4 | 653 | |
48b4554a JK |
654 | return 0; |
655 | } | |
53880546 | 656 | |
48b4554a JK |
657 | static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle) |
658 | { | |
659 | return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), | |
660 | HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report, | |
661 | ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT); | |
662 | } | |
1da177e4 | 663 | |
48b4554a JK |
664 | static int hid_get_class_descriptor(struct usb_device *dev, int ifnum, |
665 | unsigned char type, void *buf, int size) | |
666 | { | |
667 | int result, retries = 4; | |
1da177e4 | 668 | |
48b4554a | 669 | memset(buf, 0, size); |
1da177e4 | 670 | |
48b4554a JK |
671 | do { |
672 | result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), | |
673 | USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN, | |
674 | (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT); | |
675 | retries--; | |
676 | } while (result < size && retries); | |
677 | return result; | |
678 | } | |
940824b0 | 679 | |
48b4554a JK |
680 | int usbhid_open(struct hid_device *hid) |
681 | { | |
933e3187 | 682 | struct usbhid_device *usbhid = hid->driver_data; |
a8c52b66 | 683 | int res = 0; |
933e3187 | 684 | |
0361a28d | 685 | mutex_lock(&hid_open_mut); |
933e3187 ON |
686 | if (!hid->open++) { |
687 | res = usb_autopm_get_interface(usbhid->intf); | |
68229689 | 688 | /* the device must be awake to reliably request remote wakeup */ |
933e3187 ON |
689 | if (res < 0) { |
690 | hid->open--; | |
a8c52b66 ON |
691 | res = -EIO; |
692 | goto done; | |
933e3187 | 693 | } |
0361a28d | 694 | usbhid->intf->needs_remote_wakeup = 1; |
b905811a | 695 | set_bit(HID_RESUME_RUNNING, &usbhid->iofl); |
a8c52b66 ON |
696 | res = hid_start_in(hid); |
697 | if (res) { | |
698 | if (res != -ENOSPC) { | |
699 | hid_io_error(hid); | |
700 | res = 0; | |
701 | } else { | |
702 | /* no use opening if resources are insufficient */ | |
703 | hid->open--; | |
704 | res = -EBUSY; | |
705 | usbhid->intf->needs_remote_wakeup = 0; | |
706 | } | |
707 | } | |
0361a28d | 708 | usb_autopm_put_interface(usbhid->intf); |
b905811a BT |
709 | |
710 | /* | |
711 | * In case events are generated while nobody was listening, | |
712 | * some are released when the device is re-opened. | |
713 | * Wait 50 msec for the queue to empty before allowing events | |
714 | * to go through hid. | |
715 | */ | |
615322f6 ON |
716 | if (res == 0 && !(hid->quirks & HID_QUIRK_ALWAYS_POLL)) |
717 | msleep(50); | |
b905811a | 718 | clear_bit(HID_RESUME_RUNNING, &usbhid->iofl); |
933e3187 | 719 | } |
a8c52b66 | 720 | done: |
0361a28d | 721 | mutex_unlock(&hid_open_mut); |
a8c52b66 | 722 | return res; |
48b4554a | 723 | } |
a417a21e | 724 | |
48b4554a JK |
725 | void usbhid_close(struct hid_device *hid) |
726 | { | |
727 | struct usbhid_device *usbhid = hid->driver_data; | |
eab9edd2 | 728 | |
0361a28d ON |
729 | mutex_lock(&hid_open_mut); |
730 | ||
731 | /* protecting hid->open to make sure we don't restart | |
732 | * data acquistion due to a resumption we no longer | |
733 | * care about | |
734 | */ | |
735 | spin_lock_irq(&usbhid->lock); | |
933e3187 | 736 | if (!--hid->open) { |
0361a28d | 737 | spin_unlock_irq(&usbhid->lock); |
89092ddd | 738 | hid_cancel_delayed_stuff(usbhid); |
0b750b3b JH |
739 | if (!(hid->quirks & HID_QUIRK_ALWAYS_POLL)) { |
740 | usb_kill_urb(usbhid->urbin); | |
741 | usbhid->intf->needs_remote_wakeup = 0; | |
742 | } | |
0361a28d ON |
743 | } else { |
744 | spin_unlock_irq(&usbhid->lock); | |
933e3187 | 745 | } |
0361a28d | 746 | mutex_unlock(&hid_open_mut); |
48b4554a | 747 | } |
1d3e2023 | 748 | |
48b4554a JK |
749 | /* |
750 | * Initialize all reports | |
751 | */ | |
41ad5fba | 752 | |
48b4554a JK |
753 | void usbhid_init_reports(struct hid_device *hid) |
754 | { | |
755 | struct hid_report *report; | |
756 | struct usbhid_device *usbhid = hid->driver_data; | |
595e9276 | 757 | struct hid_report_enum *report_enum; |
48b4554a | 758 | int err, ret; |
41ad5fba | 759 | |
9143059f BT |
760 | report_enum = &hid->report_enum[HID_INPUT_REPORT]; |
761 | list_for_each_entry(report, &report_enum->report_list, list) | |
762 | usbhid_submit_report(hid, report, USB_DIR_IN); | |
5556feae | 763 | |
595e9276 BT |
764 | report_enum = &hid->report_enum[HID_FEATURE_REPORT]; |
765 | list_for_each_entry(report, &report_enum->report_list, list) | |
48b4554a | 766 | usbhid_submit_report(hid, report, USB_DIR_IN); |
4a1a4d8b | 767 | |
48b4554a JK |
768 | err = 0; |
769 | ret = usbhid_wait_io(hid); | |
770 | while (ret) { | |
771 | err |= ret; | |
772 | if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl)) | |
773 | usb_kill_urb(usbhid->urbctrl); | |
774 | if (test_bit(HID_OUT_RUNNING, &usbhid->iofl)) | |
775 | usb_kill_urb(usbhid->urbout); | |
776 | ret = usbhid_wait_io(hid); | |
777 | } | |
3f9b4076 | 778 | |
48b4554a | 779 | if (err) |
4291ee30 | 780 | hid_warn(hid, "timeout initializing reports\n"); |
48b4554a | 781 | } |
1da177e4 | 782 | |
713c8aad PZ |
783 | /* |
784 | * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01). | |
785 | */ | |
786 | static int hid_find_field_early(struct hid_device *hid, unsigned int page, | |
787 | unsigned int hid_code, struct hid_field **pfield) | |
788 | { | |
789 | struct hid_report *report; | |
790 | struct hid_field *field; | |
791 | struct hid_usage *usage; | |
792 | int i, j; | |
793 | ||
794 | list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) { | |
795 | for (i = 0; i < report->maxfield; i++) { | |
796 | field = report->field[i]; | |
797 | for (j = 0; j < field->maxusage; j++) { | |
798 | usage = &field->usage[j]; | |
799 | if ((usage->hid & HID_USAGE_PAGE) == page && | |
800 | (usage->hid & 0xFFFF) == hid_code) { | |
801 | *pfield = field; | |
802 | return j; | |
803 | } | |
804 | } | |
805 | } | |
806 | } | |
807 | return -1; | |
808 | } | |
809 | ||
ddf64a3c | 810 | static void usbhid_set_leds(struct hid_device *hid) |
713c8aad PZ |
811 | { |
812 | struct hid_field *field; | |
813 | int offset; | |
814 | ||
815 | if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) { | |
816 | hid_set_field(field, offset, 0); | |
817 | usbhid_submit_report(hid, field->report, USB_DIR_OUT); | |
818 | } | |
819 | } | |
820 | ||
bf0964dc MH |
821 | /* |
822 | * Traverse the supplied list of reports and find the longest | |
823 | */ | |
282bfd4c JS |
824 | static void hid_find_max_report(struct hid_device *hid, unsigned int type, |
825 | unsigned int *max) | |
bf0964dc MH |
826 | { |
827 | struct hid_report *report; | |
282bfd4c | 828 | unsigned int size; |
bf0964dc MH |
829 | |
830 | list_for_each_entry(report, &hid->report_enum[type].report_list, list) { | |
efc7ce18 | 831 | size = ((report->size - 1) >> 3) + 1 + hid->report_enum[type].numbered; |
bf0964dc MH |
832 | if (*max < size) |
833 | *max = size; | |
834 | } | |
835 | } | |
836 | ||
1da177e4 LT |
837 | static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid) |
838 | { | |
4916b3a5 JK |
839 | struct usbhid_device *usbhid = hid->driver_data; |
840 | ||
997ea58e | 841 | usbhid->inbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL, |
898089d0 | 842 | &usbhid->inbuf_dma); |
997ea58e | 843 | usbhid->outbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL, |
898089d0 | 844 | &usbhid->outbuf_dma); |
0ede76fc | 845 | usbhid->cr = kmalloc(sizeof(*usbhid->cr), GFP_KERNEL); |
997ea58e | 846 | usbhid->ctrlbuf = usb_alloc_coherent(dev, usbhid->bufsize, GFP_KERNEL, |
898089d0 JS |
847 | &usbhid->ctrlbuf_dma); |
848 | if (!usbhid->inbuf || !usbhid->outbuf || !usbhid->cr || | |
849 | !usbhid->ctrlbuf) | |
1da177e4 LT |
850 | return -1; |
851 | ||
852 | return 0; | |
853 | } | |
854 | ||
b4dbde9d AO |
855 | static int usbhid_get_raw_report(struct hid_device *hid, |
856 | unsigned char report_number, __u8 *buf, size_t count, | |
857 | unsigned char report_type) | |
858 | { | |
859 | struct usbhid_device *usbhid = hid->driver_data; | |
860 | struct usb_device *dev = hid_to_usb_dev(hid); | |
861 | struct usb_interface *intf = usbhid->intf; | |
862 | struct usb_host_interface *interface = intf->cur_altsetting; | |
863 | int skipped_report_id = 0; | |
864 | int ret; | |
865 | ||
866 | /* Byte 0 is the report number. Report data starts at byte 1.*/ | |
867 | buf[0] = report_number; | |
868 | if (report_number == 0x0) { | |
869 | /* Offset the return buffer by 1, so that the report ID | |
870 | will remain in byte 0. */ | |
871 | buf++; | |
872 | count--; | |
873 | skipped_report_id = 1; | |
874 | } | |
875 | ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), | |
876 | HID_REQ_GET_REPORT, | |
877 | USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, | |
878 | ((report_type + 1) << 8) | report_number, | |
879 | interface->desc.bInterfaceNumber, buf, count, | |
880 | USB_CTRL_SET_TIMEOUT); | |
881 | ||
882 | /* count also the report id */ | |
883 | if (ret > 0 && skipped_report_id) | |
884 | ret++; | |
885 | ||
886 | return ret; | |
887 | } | |
888 | ||
975a6832 FP |
889 | static int usbhid_set_raw_report(struct hid_device *hid, unsigned int reportnum, |
890 | __u8 *buf, size_t count, unsigned char rtype) | |
891 | { | |
892 | struct usbhid_device *usbhid = hid->driver_data; | |
893 | struct usb_device *dev = hid_to_usb_dev(hid); | |
894 | struct usb_interface *intf = usbhid->intf; | |
895 | struct usb_host_interface *interface = intf->cur_altsetting; | |
896 | int ret, skipped_report_id = 0; | |
897 | ||
898 | /* Byte 0 is the report number. Report data starts at byte 1.*/ | |
e534a935 BT |
899 | if ((rtype == HID_OUTPUT_REPORT) && |
900 | (hid->quirks & HID_QUIRK_SKIP_OUTPUT_REPORT_ID)) | |
901 | buf[0] = 0; | |
902 | else | |
903 | buf[0] = reportnum; | |
904 | ||
975a6832 FP |
905 | if (buf[0] == 0x0) { |
906 | /* Don't send the Report ID */ | |
907 | buf++; | |
908 | count--; | |
909 | skipped_report_id = 1; | |
910 | } | |
911 | ||
912 | ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), | |
913 | HID_REQ_SET_REPORT, | |
914 | USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE, | |
915 | ((rtype + 1) << 8) | reportnum, | |
916 | interface->desc.bInterfaceNumber, buf, count, | |
917 | USB_CTRL_SET_TIMEOUT); | |
918 | /* count also the report id, if this was a numbered report. */ | |
919 | if (ret > 0 && skipped_report_id) | |
920 | ret++; | |
921 | ||
922 | return ret; | |
923 | } | |
924 | ||
975a6832 FP |
925 | static int usbhid_output_report(struct hid_device *hid, __u8 *buf, size_t count) |
926 | { | |
927 | struct usbhid_device *usbhid = hid->driver_data; | |
928 | struct usb_device *dev = hid_to_usb_dev(hid); | |
929 | int actual_length, skipped_report_id = 0, ret; | |
930 | ||
931 | if (!usbhid->urbout) | |
ddea1af9 | 932 | return -ENOSYS; |
975a6832 FP |
933 | |
934 | if (buf[0] == 0x0) { | |
935 | /* Don't send the Report ID */ | |
936 | buf++; | |
937 | count--; | |
938 | skipped_report_id = 1; | |
939 | } | |
940 | ||
941 | ret = usb_interrupt_msg(dev, usbhid->urbout->pipe, | |
942 | buf, count, &actual_length, | |
943 | USB_CTRL_SET_TIMEOUT); | |
944 | /* return the number of bytes transferred */ | |
945 | if (ret == 0) { | |
946 | ret = actual_length; | |
947 | /* count also the report id */ | |
948 | if (skipped_report_id) | |
949 | ret++; | |
950 | } | |
951 | ||
952 | return ret; | |
953 | } | |
954 | ||
1da177e4 LT |
955 | static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid) |
956 | { | |
4916b3a5 JK |
957 | struct usbhid_device *usbhid = hid->driver_data; |
958 | ||
997ea58e DM |
959 | usb_free_coherent(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma); |
960 | usb_free_coherent(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma); | |
0ede76fc | 961 | kfree(usbhid->cr); |
997ea58e | 962 | usb_free_coherent(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma); |
1da177e4 LT |
963 | } |
964 | ||
c500c971 JS |
965 | static int usbhid_parse(struct hid_device *hid) |
966 | { | |
967 | struct usb_interface *intf = to_usb_interface(hid->dev.parent); | |
1da177e4 LT |
968 | struct usb_host_interface *interface = intf->cur_altsetting; |
969 | struct usb_device *dev = interface_to_usbdev (intf); | |
970 | struct hid_descriptor *hdesc; | |
2eb5dc30 | 971 | u32 quirks = 0; |
c500c971 | 972 | unsigned int rsize = 0; |
c5b7c7c3 | 973 | char *rdesc; |
c500c971 | 974 | int ret, n; |
1da177e4 | 975 | |
2eb5dc30 PW |
976 | quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor), |
977 | le16_to_cpu(dev->descriptor.idProduct)); | |
1da177e4 | 978 | |
6f4303fb JK |
979 | if (quirks & HID_QUIRK_IGNORE) |
980 | return -ENODEV; | |
981 | ||
0f28b55d AS |
982 | /* Many keyboards and mice don't like to be polled for reports, |
983 | * so we will always set the HID_QUIRK_NOGET flag for them. */ | |
984 | if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) { | |
985 | if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD || | |
986 | interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE) | |
987 | quirks |= HID_QUIRK_NOGET; | |
988 | } | |
989 | ||
c5b7c7c3 DT |
990 | if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) && |
991 | (!interface->desc.bNumEndpoints || | |
992 | usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) { | |
58037eb9 | 993 | dbg_hid("class descriptor not present\n"); |
c500c971 | 994 | return -ENODEV; |
1da177e4 LT |
995 | } |
996 | ||
c500c971 JS |
997 | hid->version = le16_to_cpu(hdesc->bcdHID); |
998 | hid->country = hdesc->bCountryCode; | |
999 | ||
1da177e4 LT |
1000 | for (n = 0; n < hdesc->bNumDescriptors; n++) |
1001 | if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT) | |
1002 | rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength); | |
1003 | ||
1004 | if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) { | |
58037eb9 | 1005 | dbg_hid("weird size of report descriptor (%u)\n", rsize); |
c500c971 | 1006 | return -EINVAL; |
1da177e4 LT |
1007 | } |
1008 | ||
52150c78 JP |
1009 | rdesc = kmalloc(rsize, GFP_KERNEL); |
1010 | if (!rdesc) | |
c500c971 | 1011 | return -ENOMEM; |
1da177e4 | 1012 | |
854561b0 VP |
1013 | hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0); |
1014 | ||
c500c971 JS |
1015 | ret = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber, |
1016 | HID_DT_REPORT, rdesc, rsize); | |
1017 | if (ret < 0) { | |
58037eb9 | 1018 | dbg_hid("reading report descriptor failed\n"); |
1da177e4 | 1019 | kfree(rdesc); |
c500c971 | 1020 | goto err; |
1da177e4 LT |
1021 | } |
1022 | ||
c500c971 JS |
1023 | ret = hid_parse_report(hid, rdesc, rsize); |
1024 | kfree(rdesc); | |
1025 | if (ret) { | |
58037eb9 | 1026 | dbg_hid("parsing report descriptor failed\n"); |
c500c971 | 1027 | goto err; |
1da177e4 LT |
1028 | } |
1029 | ||
f5208997 | 1030 | hid->quirks |= quirks; |
1da177e4 | 1031 | |
c500c971 JS |
1032 | return 0; |
1033 | err: | |
1034 | return ret; | |
1035 | } | |
1036 | ||
1037 | static int usbhid_start(struct hid_device *hid) | |
1038 | { | |
1039 | struct usb_interface *intf = to_usb_interface(hid->dev.parent); | |
1040 | struct usb_host_interface *interface = intf->cur_altsetting; | |
1041 | struct usb_device *dev = interface_to_usbdev(intf); | |
3d5afd32 | 1042 | struct usbhid_device *usbhid = hid->driver_data; |
c500c971 JS |
1043 | unsigned int n, insize = 0; |
1044 | int ret; | |
1045 | ||
e3e14de5 JS |
1046 | clear_bit(HID_DISCONNECTED, &usbhid->iofl); |
1047 | ||
4916b3a5 JK |
1048 | usbhid->bufsize = HID_MIN_BUFFER_SIZE; |
1049 | hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize); | |
1050 | hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize); | |
1051 | hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize); | |
bf0964dc | 1052 | |
4916b3a5 JK |
1053 | if (usbhid->bufsize > HID_MAX_BUFFER_SIZE) |
1054 | usbhid->bufsize = HID_MAX_BUFFER_SIZE; | |
bf0964dc MH |
1055 | |
1056 | hid_find_max_report(hid, HID_INPUT_REPORT, &insize); | |
1057 | ||
1058 | if (insize > HID_MAX_BUFFER_SIZE) | |
1059 | insize = HID_MAX_BUFFER_SIZE; | |
1060 | ||
c500c971 JS |
1061 | if (hid_alloc_buffers(dev, hid)) { |
1062 | ret = -ENOMEM; | |
1da177e4 | 1063 | goto fail; |
f345c37c PS |
1064 | } |
1065 | ||
1da177e4 | 1066 | for (n = 0; n < interface->desc.bNumEndpoints; n++) { |
1da177e4 LT |
1067 | struct usb_endpoint_descriptor *endpoint; |
1068 | int pipe; | |
1069 | int interval; | |
1070 | ||
1071 | endpoint = &interface->endpoint[n].desc; | |
581a2739 | 1072 | if (!usb_endpoint_xfer_int(endpoint)) |
1da177e4 LT |
1073 | continue; |
1074 | ||
1da177e4 | 1075 | interval = endpoint->bInterval; |
1da177e4 | 1076 | |
f345c37c | 1077 | /* Some vendors give fullspeed interval on highspeed devides */ |
c500c971 | 1078 | if (hid->quirks & HID_QUIRK_FULLSPEED_INTERVAL && |
f345c37c PS |
1079 | dev->speed == USB_SPEED_HIGH) { |
1080 | interval = fls(endpoint->bInterval*8); | |
52150c78 JP |
1081 | pr_info("%s: Fixing fullspeed to highspeed interval: %d -> %d\n", |
1082 | hid->name, endpoint->bInterval, interval); | |
f345c37c PS |
1083 | } |
1084 | ||
933bfe4d TJ |
1085 | /* Change the polling interval of mice and joysticks. */ |
1086 | switch (hid->collection->usage) { | |
1087 | case HID_GD_MOUSE: | |
1088 | if (hid_mousepoll_interval > 0) | |
1089 | interval = hid_mousepoll_interval; | |
1090 | break; | |
1091 | case HID_GD_JOYSTICK: | |
1092 | if (hid_jspoll_interval > 0) | |
1093 | interval = hid_jspoll_interval; | |
1094 | break; | |
1095 | } | |
05f091ab | 1096 | |
c500c971 | 1097 | ret = -ENOMEM; |
0f12aa03 | 1098 | if (usb_endpoint_dir_in(endpoint)) { |
4916b3a5 | 1099 | if (usbhid->urbin) |
1da177e4 | 1100 | continue; |
4916b3a5 | 1101 | if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL))) |
1da177e4 LT |
1102 | goto fail; |
1103 | pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress); | |
4916b3a5 | 1104 | usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize, |
1da177e4 | 1105 | hid_irq_in, hid, interval); |
4916b3a5 JK |
1106 | usbhid->urbin->transfer_dma = usbhid->inbuf_dma; |
1107 | usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | |
1da177e4 | 1108 | } else { |
4916b3a5 | 1109 | if (usbhid->urbout) |
1da177e4 | 1110 | continue; |
4916b3a5 | 1111 | if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL))) |
1da177e4 LT |
1112 | goto fail; |
1113 | pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress); | |
4916b3a5 | 1114 | usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0, |
1da177e4 | 1115 | hid_irq_out, hid, interval); |
4916b3a5 JK |
1116 | usbhid->urbout->transfer_dma = usbhid->outbuf_dma; |
1117 | usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | |
1da177e4 LT |
1118 | } |
1119 | } | |
1120 | ||
4916b3a5 | 1121 | usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL); |
c500c971 JS |
1122 | if (!usbhid->urbctrl) { |
1123 | ret = -ENOMEM; | |
1da177e4 | 1124 | goto fail; |
c500c971 | 1125 | } |
c5b7c7c3 | 1126 | |
4916b3a5 JK |
1127 | usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr, |
1128 | usbhid->ctrlbuf, 1, hid_ctrl, hid); | |
4916b3a5 | 1129 | usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma; |
0ede76fc | 1130 | usbhid->urbctrl->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
c500c971 | 1131 | |
3d5afd32 | 1132 | set_bit(HID_STARTED, &usbhid->iofl); |
3d5afd32 | 1133 | |
0b750b3b JH |
1134 | if (hid->quirks & HID_QUIRK_ALWAYS_POLL) { |
1135 | ret = usb_autopm_get_interface(usbhid->intf); | |
1136 | if (ret) | |
1137 | goto fail; | |
1138 | usbhid->intf->needs_remote_wakeup = 1; | |
1139 | ret = hid_start_in(hid); | |
1140 | if (ret) { | |
1141 | dev_err(&hid->dev, | |
1142 | "failed to start in urb: %d\n", ret); | |
1143 | } | |
1144 | usb_autopm_put_interface(usbhid->intf); | |
1145 | } | |
1146 | ||
08ef08ee AS |
1147 | /* Some keyboards don't work until their LEDs have been set. |
1148 | * Since BIOSes do set the LEDs, it must be safe for any device | |
1149 | * that supports the keyboard boot protocol. | |
3d61510f AS |
1150 | * In addition, enable remote wakeup by default for all keyboard |
1151 | * devices supporting the boot protocol. | |
08ef08ee AS |
1152 | */ |
1153 | if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT && | |
1154 | interface->desc.bInterfaceProtocol == | |
3d61510f | 1155 | USB_INTERFACE_PROTOCOL_KEYBOARD) { |
08ef08ee | 1156 | usbhid_set_leds(hid); |
3d61510f AS |
1157 | device_set_wakeup_enable(&dev->dev, 1); |
1158 | } | |
c500c971 | 1159 | return 0; |
1da177e4 LT |
1160 | |
1161 | fail: | |
4916b3a5 JK |
1162 | usb_free_urb(usbhid->urbin); |
1163 | usb_free_urb(usbhid->urbout); | |
1164 | usb_free_urb(usbhid->urbctrl); | |
e3e14de5 JS |
1165 | usbhid->urbin = NULL; |
1166 | usbhid->urbout = NULL; | |
1167 | usbhid->urbctrl = NULL; | |
22f675f3 | 1168 | hid_free_buffers(dev, hid); |
c500c971 | 1169 | return ret; |
1da177e4 LT |
1170 | } |
1171 | ||
c500c971 | 1172 | static void usbhid_stop(struct hid_device *hid) |
1da177e4 | 1173 | { |
c500c971 | 1174 | struct usbhid_device *usbhid = hid->driver_data; |
1da177e4 | 1175 | |
c500c971 | 1176 | if (WARN_ON(!usbhid)) |
1da177e4 LT |
1177 | return; |
1178 | ||
0b750b3b JH |
1179 | if (hid->quirks & HID_QUIRK_ALWAYS_POLL) |
1180 | usbhid->intf->needs_remote_wakeup = 0; | |
1181 | ||
3d5afd32 | 1182 | clear_bit(HID_STARTED, &usbhid->iofl); |
4371ea82 | 1183 | spin_lock_irq(&usbhid->lock); /* Sync with error and led handlers */ |
69626f23 | 1184 | set_bit(HID_DISCONNECTED, &usbhid->iofl); |
0361a28d | 1185 | spin_unlock_irq(&usbhid->lock); |
4916b3a5 JK |
1186 | usb_kill_urb(usbhid->urbin); |
1187 | usb_kill_urb(usbhid->urbout); | |
1188 | usb_kill_urb(usbhid->urbctrl); | |
1da177e4 | 1189 | |
0361a28d | 1190 | hid_cancel_delayed_stuff(usbhid); |
aef4e266 | 1191 | |
c500c971 JS |
1192 | hid->claimed = 0; |
1193 | ||
4916b3a5 JK |
1194 | usb_free_urb(usbhid->urbin); |
1195 | usb_free_urb(usbhid->urbctrl); | |
1196 | usb_free_urb(usbhid->urbout); | |
e3e14de5 JS |
1197 | usbhid->urbin = NULL; /* don't mess up next start */ |
1198 | usbhid->urbctrl = NULL; | |
1199 | usbhid->urbout = NULL; | |
1da177e4 | 1200 | |
be820975 | 1201 | hid_free_buffers(hid_to_usb_dev(hid), hid); |
1da177e4 LT |
1202 | } |
1203 | ||
0361a28d ON |
1204 | static int usbhid_power(struct hid_device *hid, int lvl) |
1205 | { | |
1206 | int r = 0; | |
1207 | ||
1208 | switch (lvl) { | |
1209 | case PM_HINT_FULLON: | |
1210 | r = usbhid_get_power(hid); | |
1211 | break; | |
1212 | case PM_HINT_NORMAL: | |
1213 | usbhid_put_power(hid); | |
1214 | break; | |
1215 | } | |
1216 | return r; | |
1217 | } | |
1218 | ||
e90a6df8 HR |
1219 | static void usbhid_request(struct hid_device *hid, struct hid_report *rep, int reqtype) |
1220 | { | |
1221 | switch (reqtype) { | |
1222 | case HID_REQ_GET_REPORT: | |
1223 | usbhid_submit_report(hid, rep, USB_DIR_IN); | |
1224 | break; | |
1225 | case HID_REQ_SET_REPORT: | |
1226 | usbhid_submit_report(hid, rep, USB_DIR_OUT); | |
1227 | break; | |
1228 | } | |
1229 | } | |
1230 | ||
975a6832 FP |
1231 | static int usbhid_raw_request(struct hid_device *hid, unsigned char reportnum, |
1232 | __u8 *buf, size_t len, unsigned char rtype, | |
1233 | int reqtype) | |
1234 | { | |
1235 | switch (reqtype) { | |
1236 | case HID_REQ_GET_REPORT: | |
1237 | return usbhid_get_raw_report(hid, reportnum, buf, len, rtype); | |
1238 | case HID_REQ_SET_REPORT: | |
1239 | return usbhid_set_raw_report(hid, reportnum, buf, len, rtype); | |
1240 | default: | |
1241 | return -EIO; | |
1242 | } | |
1243 | } | |
1244 | ||
9684819b BT |
1245 | static int usbhid_idle(struct hid_device *hid, int report, int idle, |
1246 | int reqtype) | |
1247 | { | |
1248 | struct usb_device *dev = hid_to_usb_dev(hid); | |
1249 | struct usb_interface *intf = to_usb_interface(hid->dev.parent); | |
1250 | struct usb_host_interface *interface = intf->cur_altsetting; | |
1251 | int ifnum = interface->desc.bInterfaceNumber; | |
1252 | ||
1253 | if (reqtype != HID_REQ_SET_IDLE) | |
1254 | return -EINVAL; | |
1255 | ||
1256 | return hid_set_idle(dev, ifnum, report, idle); | |
1257 | } | |
1258 | ||
c500c971 JS |
1259 | static struct hid_ll_driver usb_hid_driver = { |
1260 | .parse = usbhid_parse, | |
1261 | .start = usbhid_start, | |
1262 | .stop = usbhid_stop, | |
1263 | .open = usbhid_open, | |
1264 | .close = usbhid_close, | |
0361a28d | 1265 | .power = usbhid_power, |
e90a6df8 | 1266 | .request = usbhid_request, |
3373443b | 1267 | .wait = usbhid_wait_io, |
975a6832 FP |
1268 | .raw_request = usbhid_raw_request, |
1269 | .output_report = usbhid_output_report, | |
9684819b | 1270 | .idle = usbhid_idle, |
c500c971 JS |
1271 | }; |
1272 | ||
c4c259bc | 1273 | static int usbhid_probe(struct usb_interface *intf, const struct usb_device_id *id) |
1da177e4 | 1274 | { |
131d3a7a | 1275 | struct usb_host_interface *interface = intf->cur_altsetting; |
c500c971 | 1276 | struct usb_device *dev = interface_to_usbdev(intf); |
3d5afd32 | 1277 | struct usbhid_device *usbhid; |
1da177e4 | 1278 | struct hid_device *hid; |
131d3a7a | 1279 | unsigned int n, has_in = 0; |
c500c971 JS |
1280 | size_t len; |
1281 | int ret; | |
1da177e4 | 1282 | |
58037eb9 | 1283 | dbg_hid("HID probe called for ifnum %d\n", |
1da177e4 LT |
1284 | intf->altsetting->desc.bInterfaceNumber); |
1285 | ||
131d3a7a JS |
1286 | for (n = 0; n < interface->desc.bNumEndpoints; n++) |
1287 | if (usb_endpoint_is_int_in(&interface->endpoint[n].desc)) | |
1288 | has_in++; | |
1289 | if (!has_in) { | |
4291ee30 | 1290 | hid_err(intf, "couldn't find an input interrupt endpoint\n"); |
131d3a7a JS |
1291 | return -ENODEV; |
1292 | } | |
1293 | ||
c500c971 JS |
1294 | hid = hid_allocate_device(); |
1295 | if (IS_ERR(hid)) | |
1296 | return PTR_ERR(hid); | |
1da177e4 LT |
1297 | |
1298 | usb_set_intfdata(intf, hid); | |
c500c971 | 1299 | hid->ll_driver = &usb_hid_driver; |
76483cf4 | 1300 | hid->ff_init = hid_pidff_init; |
c500c971 | 1301 | #ifdef CONFIG_USB_HIDDEV |
93c10132 | 1302 | hid->hiddev_connect = hiddev_connect; |
c4c259bc | 1303 | hid->hiddev_disconnect = hiddev_disconnect; |
c500c971 JS |
1304 | hid->hiddev_hid_event = hiddev_hid_event; |
1305 | hid->hiddev_report_event = hiddev_report_event; | |
1306 | #endif | |
1307 | hid->dev.parent = &intf->dev; | |
1308 | hid->bus = BUS_USB; | |
1309 | hid->vendor = le16_to_cpu(dev->descriptor.idVendor); | |
1310 | hid->product = le16_to_cpu(dev->descriptor.idProduct); | |
1311 | hid->name[0] = 0; | |
b5e5a37e | 1312 | hid->quirks = usbhid_lookup_quirk(hid->vendor, hid->product); |
a73a6370 JS |
1313 | if (intf->cur_altsetting->desc.bInterfaceProtocol == |
1314 | USB_INTERFACE_PROTOCOL_MOUSE) | |
1315 | hid->type = HID_TYPE_USBMOUSE; | |
6dc1418e TS |
1316 | else if (intf->cur_altsetting->desc.bInterfaceProtocol == 0) |
1317 | hid->type = HID_TYPE_USBNONE; | |
1da177e4 | 1318 | |
c500c971 JS |
1319 | if (dev->manufacturer) |
1320 | strlcpy(hid->name, dev->manufacturer, sizeof(hid->name)); | |
1da177e4 | 1321 | |
c500c971 JS |
1322 | if (dev->product) { |
1323 | if (dev->manufacturer) | |
1324 | strlcat(hid->name, " ", sizeof(hid->name)); | |
1325 | strlcat(hid->name, dev->product, sizeof(hid->name)); | |
1da177e4 LT |
1326 | } |
1327 | ||
c500c971 JS |
1328 | if (!strlen(hid->name)) |
1329 | snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x", | |
1330 | le16_to_cpu(dev->descriptor.idVendor), | |
1331 | le16_to_cpu(dev->descriptor.idProduct)); | |
1da177e4 | 1332 | |
c500c971 JS |
1333 | usb_make_path(dev, hid->phys, sizeof(hid->phys)); |
1334 | strlcat(hid->phys, "/input", sizeof(hid->phys)); | |
1335 | len = strlen(hid->phys); | |
1336 | if (len < sizeof(hid->phys) - 1) | |
1337 | snprintf(hid->phys + len, sizeof(hid->phys) - len, | |
1338 | "%d", intf->altsetting[0].desc.bInterfaceNumber); | |
1339 | ||
1340 | if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0) | |
1341 | hid->uniq[0] = 0; | |
1da177e4 | 1342 | |
3d5afd32 JS |
1343 | usbhid = kzalloc(sizeof(*usbhid), GFP_KERNEL); |
1344 | if (usbhid == NULL) { | |
1345 | ret = -ENOMEM; | |
1346 | goto err; | |
1347 | } | |
1348 | ||
1349 | hid->driver_data = usbhid; | |
1350 | usbhid->hid = hid; | |
57ab12e4 JK |
1351 | usbhid->intf = intf; |
1352 | usbhid->ifnum = interface->desc.bInterfaceNumber; | |
3d5afd32 | 1353 | |
fde4e2f7 AS |
1354 | init_waitqueue_head(&usbhid->wait); |
1355 | INIT_WORK(&usbhid->reset_work, hid_reset); | |
fde4e2f7 AS |
1356 | setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid); |
1357 | spin_lock_init(&usbhid->lock); | |
1358 | ||
85cdaf52 JS |
1359 | ret = hid_add_device(hid); |
1360 | if (ret) { | |
d458a9df | 1361 | if (ret != -ENODEV) |
4291ee30 | 1362 | hid_err(intf, "can't add hid device: %d\n", ret); |
3d5afd32 | 1363 | goto err_free; |
85cdaf52 | 1364 | } |
c500c971 JS |
1365 | |
1366 | return 0; | |
3d5afd32 JS |
1367 | err_free: |
1368 | kfree(usbhid); | |
c500c971 JS |
1369 | err: |
1370 | hid_destroy_device(hid); | |
85cdaf52 | 1371 | return ret; |
1da177e4 LT |
1372 | } |
1373 | ||
c4c259bc | 1374 | static void usbhid_disconnect(struct usb_interface *intf) |
c500c971 JS |
1375 | { |
1376 | struct hid_device *hid = usb_get_intfdata(intf); | |
3d5afd32 | 1377 | struct usbhid_device *usbhid; |
c500c971 JS |
1378 | |
1379 | if (WARN_ON(!hid)) | |
1380 | return; | |
1381 | ||
3d5afd32 | 1382 | usbhid = hid->driver_data; |
46df9ded RA |
1383 | spin_lock_irq(&usbhid->lock); /* Sync with error and led handlers */ |
1384 | set_bit(HID_DISCONNECTED, &usbhid->iofl); | |
1385 | spin_unlock_irq(&usbhid->lock); | |
c500c971 | 1386 | hid_destroy_device(hid); |
3d5afd32 | 1387 | kfree(usbhid); |
c500c971 JS |
1388 | } |
1389 | ||
0361a28d ON |
1390 | static void hid_cancel_delayed_stuff(struct usbhid_device *usbhid) |
1391 | { | |
1392 | del_timer_sync(&usbhid->io_retry); | |
0361a28d ON |
1393 | cancel_work_sync(&usbhid->reset_work); |
1394 | } | |
1395 | ||
1396 | static void hid_cease_io(struct usbhid_device *usbhid) | |
1397 | { | |
fad9fbe8 | 1398 | del_timer_sync(&usbhid->io_retry); |
0361a28d ON |
1399 | usb_kill_urb(usbhid->urbin); |
1400 | usb_kill_urb(usbhid->urbctrl); | |
1401 | usb_kill_urb(usbhid->urbout); | |
0361a28d ON |
1402 | } |
1403 | ||
972e6a99 AS |
1404 | static void hid_restart_io(struct hid_device *hid) |
1405 | { | |
1406 | struct usbhid_device *usbhid = hid->driver_data; | |
1407 | int clear_halt = test_bit(HID_CLEAR_HALT, &usbhid->iofl); | |
1408 | int reset_pending = test_bit(HID_RESET_PENDING, &usbhid->iofl); | |
1409 | ||
1410 | spin_lock_irq(&usbhid->lock); | |
1411 | clear_bit(HID_SUSPENDED, &usbhid->iofl); | |
1412 | usbhid_mark_busy(usbhid); | |
1413 | ||
1414 | if (clear_halt || reset_pending) | |
1415 | schedule_work(&usbhid->reset_work); | |
1416 | usbhid->retry_delay = 0; | |
1417 | spin_unlock_irq(&usbhid->lock); | |
1418 | ||
1419 | if (reset_pending || !test_bit(HID_STARTED, &usbhid->iofl)) | |
1420 | return; | |
1421 | ||
1422 | if (!clear_halt) { | |
1423 | if (hid_start_in(hid) < 0) | |
1424 | hid_io_error(hid); | |
1425 | } | |
1426 | ||
1427 | spin_lock_irq(&usbhid->lock); | |
1428 | if (usbhid->urbout && !test_bit(HID_OUT_RUNNING, &usbhid->iofl)) | |
1429 | usbhid_restart_out_queue(usbhid); | |
1430 | if (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl)) | |
1431 | usbhid_restart_ctrl_queue(usbhid); | |
1432 | spin_unlock_irq(&usbhid->lock); | |
1433 | } | |
1434 | ||
ae2f0074 JK |
1435 | /* Treat USB reset pretty much the same as suspend/resume */ |
1436 | static int hid_pre_reset(struct usb_interface *intf) | |
1437 | { | |
1438 | struct hid_device *hid = usb_get_intfdata(intf); | |
1439 | struct usbhid_device *usbhid = hid->driver_data; | |
1440 | ||
1441 | spin_lock_irq(&usbhid->lock); | |
1442 | set_bit(HID_RESET_PENDING, &usbhid->iofl); | |
1443 | spin_unlock_irq(&usbhid->lock); | |
1444 | hid_cease_io(usbhid); | |
1445 | ||
1446 | return 0; | |
1447 | } | |
1448 | ||
1449 | /* Same routine used for post_reset and reset_resume */ | |
1450 | static int hid_post_reset(struct usb_interface *intf) | |
1451 | { | |
1452 | struct usb_device *dev = interface_to_usbdev (intf); | |
1453 | struct hid_device *hid = usb_get_intfdata(intf); | |
1454 | struct usbhid_device *usbhid = hid->driver_data; | |
dc3c78e4 | 1455 | struct usb_host_interface *interface = intf->cur_altsetting; |
ae2f0074 | 1456 | int status; |
dc3c78e4 SH |
1457 | char *rdesc; |
1458 | ||
1459 | /* Fetch and examine the HID report descriptor. If this | |
1460 | * has changed, then rebind. Since usbcore's check of the | |
1461 | * configuration descriptors passed, we already know that | |
1462 | * the size of the HID report descriptor has not changed. | |
1463 | */ | |
86e6b77e | 1464 | rdesc = kmalloc(hid->dev_rsize, GFP_KERNEL); |
52150c78 | 1465 | if (!rdesc) |
c60fa555 | 1466 | return -ENOMEM; |
52150c78 | 1467 | |
dc3c78e4 SH |
1468 | status = hid_get_class_descriptor(dev, |
1469 | interface->desc.bInterfaceNumber, | |
86e6b77e | 1470 | HID_DT_REPORT, rdesc, hid->dev_rsize); |
dc3c78e4 SH |
1471 | if (status < 0) { |
1472 | dbg_hid("reading report descriptor failed (post_reset)\n"); | |
1473 | kfree(rdesc); | |
c60fa555 | 1474 | return status; |
dc3c78e4 | 1475 | } |
86e6b77e | 1476 | status = memcmp(rdesc, hid->dev_rdesc, hid->dev_rsize); |
dc3c78e4 SH |
1477 | kfree(rdesc); |
1478 | if (status != 0) { | |
1479 | dbg_hid("report descriptor changed\n"); | |
c60fa555 | 1480 | return -EPERM; |
dc3c78e4 | 1481 | } |
70fa9f2e | 1482 | |
972e6a99 | 1483 | /* No need to do another reset or clear a halted endpoint */ |
ae2f0074 JK |
1484 | spin_lock_irq(&usbhid->lock); |
1485 | clear_bit(HID_RESET_PENDING, &usbhid->iofl); | |
972e6a99 | 1486 | clear_bit(HID_CLEAR_HALT, &usbhid->iofl); |
ae2f0074 JK |
1487 | spin_unlock_irq(&usbhid->lock); |
1488 | hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0); | |
972e6a99 AS |
1489 | |
1490 | hid_restart_io(hid); | |
ae2f0074 JK |
1491 | |
1492 | return 0; | |
1493 | } | |
1494 | ||
1495 | int usbhid_get_power(struct hid_device *hid) | |
1496 | { | |
1497 | struct usbhid_device *usbhid = hid->driver_data; | |
70fa9f2e | 1498 | |
ae2f0074 JK |
1499 | return usb_autopm_get_interface(usbhid->intf); |
1500 | } | |
1501 | ||
1502 | void usbhid_put_power(struct hid_device *hid) | |
1503 | { | |
1504 | struct usbhid_device *usbhid = hid->driver_data; | |
70fa9f2e | 1505 | |
ae2f0074 JK |
1506 | usb_autopm_put_interface(usbhid->intf); |
1507 | } | |
1508 | ||
1509 | ||
0f6f1407 | 1510 | #ifdef CONFIG_PM |
eb055fd0 AS |
1511 | static int hid_resume_common(struct hid_device *hid, bool driver_suspended) |
1512 | { | |
972e6a99 | 1513 | int status = 0; |
eb055fd0 | 1514 | |
972e6a99 | 1515 | hid_restart_io(hid); |
eb055fd0 AS |
1516 | if (driver_suspended && hid->driver && hid->driver->resume) |
1517 | status = hid->driver->resume(hid); | |
1518 | return status; | |
1519 | } | |
1520 | ||
27d72e85 | 1521 | static int hid_suspend(struct usb_interface *intf, pm_message_t message) |
1da177e4 | 1522 | { |
0361a28d | 1523 | struct hid_device *hid = usb_get_intfdata(intf); |
4916b3a5 | 1524 | struct usbhid_device *usbhid = hid->driver_data; |
37093b70 | 1525 | int status = 0; |
eb055fd0 | 1526 | bool driver_suspended = false; |
bfde79cb | 1527 | unsigned int ledcount; |
1da177e4 | 1528 | |
5b1b0b81 | 1529 | if (PMSG_IS_AUTO(message)) { |
bfde79cb | 1530 | ledcount = hidinput_count_leds(hid); |
0361a28d ON |
1531 | spin_lock_irq(&usbhid->lock); /* Sync with error handler */ |
1532 | if (!test_bit(HID_RESET_PENDING, &usbhid->iofl) | |
1533 | && !test_bit(HID_CLEAR_HALT, &usbhid->iofl) | |
1534 | && !test_bit(HID_OUT_RUNNING, &usbhid->iofl) | |
1535 | && !test_bit(HID_CTRL_RUNNING, &usbhid->iofl) | |
1536 | && !test_bit(HID_KEYS_PRESSED, &usbhid->iofl) | |
bfde79cb | 1537 | && (!ledcount || ignoreled)) |
0361a28d | 1538 | { |
f2b5264d | 1539 | set_bit(HID_SUSPENDED, &usbhid->iofl); |
0361a28d | 1540 | spin_unlock_irq(&usbhid->lock); |
6a740aa4 BP |
1541 | if (hid->driver && hid->driver->suspend) { |
1542 | status = hid->driver->suspend(hid, message); | |
1543 | if (status < 0) | |
eb055fd0 | 1544 | goto failed; |
6a740aa4 | 1545 | } |
eb055fd0 | 1546 | driver_suspended = true; |
0361a28d ON |
1547 | } else { |
1548 | usbhid_mark_busy(usbhid); | |
1549 | spin_unlock_irq(&usbhid->lock); | |
1550 | return -EBUSY; | |
1551 | } | |
3d5afd32 | 1552 | |
0361a28d | 1553 | } else { |
37093b70 ML |
1554 | /* TODO: resume() might need to handle suspend failure */ |
1555 | if (hid->driver && hid->driver->suspend) | |
6a740aa4 | 1556 | status = hid->driver->suspend(hid, message); |
eb055fd0 | 1557 | driver_suspended = true; |
0361a28d | 1558 | spin_lock_irq(&usbhid->lock); |
f2b5264d | 1559 | set_bit(HID_SUSPENDED, &usbhid->iofl); |
0361a28d | 1560 | spin_unlock_irq(&usbhid->lock); |
37093b70 | 1561 | if (usbhid_wait_io(hid) < 0) |
eb055fd0 | 1562 | status = -EIO; |
0361a28d ON |
1563 | } |
1564 | ||
0361a28d ON |
1565 | hid_cancel_delayed_stuff(usbhid); |
1566 | hid_cease_io(usbhid); | |
1567 | ||
5b1b0b81 | 1568 | if (PMSG_IS_AUTO(message) && test_bit(HID_KEYS_PRESSED, &usbhid->iofl)) { |
0361a28d | 1569 | /* lost race against keypresses */ |
eb055fd0 AS |
1570 | status = -EBUSY; |
1571 | goto failed; | |
0361a28d | 1572 | } |
1da177e4 | 1573 | dev_dbg(&intf->dev, "suspend\n"); |
37093b70 | 1574 | return status; |
eb055fd0 AS |
1575 | |
1576 | failed: | |
1577 | hid_resume_common(hid, driver_suspended); | |
1578 | return status; | |
1da177e4 LT |
1579 | } |
1580 | ||
1581 | static int hid_resume(struct usb_interface *intf) | |
1582 | { | |
1583 | struct hid_device *hid = usb_get_intfdata (intf); | |
1584 | int status; | |
1585 | ||
eb055fd0 | 1586 | status = hid_resume_common(hid, true); |
1da177e4 | 1587 | dev_dbg(&intf->dev, "resume status %d\n", status); |
f07600cf | 1588 | return 0; |
df9a1f48 AS |
1589 | } |
1590 | ||
378a0ede | 1591 | static int hid_reset_resume(struct usb_interface *intf) |
df9a1f48 | 1592 | { |
378a0ede | 1593 | struct hid_device *hid = usb_get_intfdata(intf); |
6a740aa4 | 1594 | int status; |
df9a1f48 | 1595 | |
6a740aa4 BP |
1596 | status = hid_post_reset(intf); |
1597 | if (status >= 0 && hid->driver && hid->driver->reset_resume) { | |
1598 | int ret = hid->driver->reset_resume(hid); | |
1599 | if (ret < 0) | |
1600 | status = ret; | |
1601 | } | |
1602 | return status; | |
df9a1f48 AS |
1603 | } |
1604 | ||
ae2f0074 | 1605 | #endif /* CONFIG_PM */ |
df9a1f48 | 1606 | |
d67dec5b | 1607 | static const struct usb_device_id hid_usb_ids[] = { |
1da177e4 LT |
1608 | { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS, |
1609 | .bInterfaceClass = USB_INTERFACE_CLASS_HID }, | |
1610 | { } /* Terminating entry */ | |
1611 | }; | |
1612 | ||
1613 | MODULE_DEVICE_TABLE (usb, hid_usb_ids); | |
1614 | ||
1615 | static struct usb_driver hid_driver = { | |
1da177e4 | 1616 | .name = "usbhid", |
c4c259bc JK |
1617 | .probe = usbhid_probe, |
1618 | .disconnect = usbhid_disconnect, | |
0f6f1407 | 1619 | #ifdef CONFIG_PM |
1da177e4 LT |
1620 | .suspend = hid_suspend, |
1621 | .resume = hid_resume, | |
378a0ede | 1622 | .reset_resume = hid_reset_resume, |
0f6f1407 | 1623 | #endif |
df9a1f48 AS |
1624 | .pre_reset = hid_pre_reset, |
1625 | .post_reset = hid_post_reset, | |
1da177e4 | 1626 | .id_table = hid_usb_ids, |
933e3187 | 1627 | .supports_autosuspend = 1, |
1da177e4 LT |
1628 | }; |
1629 | ||
8fe294ca GC |
1630 | struct usb_interface *usbhid_find_interface(int minor) |
1631 | { | |
1632 | return usb_find_interface(&hid_driver, minor); | |
1633 | } | |
1634 | ||
1da177e4 LT |
1635 | static int __init hid_init(void) |
1636 | { | |
0361a28d ON |
1637 | int retval = -ENOMEM; |
1638 | ||
876b9276 PW |
1639 | retval = usbhid_quirks_init(quirks_param); |
1640 | if (retval) | |
1641 | goto usbhid_quirks_init_fail; | |
1da177e4 LT |
1642 | retval = usb_register(&hid_driver); |
1643 | if (retval) | |
1644 | goto usb_register_fail; | |
52150c78 | 1645 | pr_info(KBUILD_MODNAME ": " DRIVER_DESC "\n"); |
1da177e4 LT |
1646 | |
1647 | return 0; | |
1648 | usb_register_fail: | |
876b9276 PW |
1649 | usbhid_quirks_exit(); |
1650 | usbhid_quirks_init_fail: | |
1da177e4 LT |
1651 | return retval; |
1652 | } | |
1653 | ||
1654 | static void __exit hid_exit(void) | |
1655 | { | |
1656 | usb_deregister(&hid_driver); | |
876b9276 | 1657 | usbhid_quirks_exit(); |
1da177e4 LT |
1658 | } |
1659 | ||
1660 | module_init(hid_init); | |
1661 | module_exit(hid_exit); | |
1662 | ||
88adb72b JK |
1663 | MODULE_AUTHOR("Andreas Gal"); |
1664 | MODULE_AUTHOR("Vojtech Pavlik"); | |
1665 | MODULE_AUTHOR("Jiri Kosina"); | |
1da177e4 | 1666 | MODULE_DESCRIPTION(DRIVER_DESC); |
7021b600 | 1667 | MODULE_LICENSE("GPL"); |