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