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