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