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