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