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 | |
b55fd23c | 7 | * Copyright (c) 2006-2007 Jiri Kosina |
1da177e4 LT |
8 | */ |
9 | ||
10 | /* | |
11 | * This program is free software; you can redistribute it and/or modify it | |
12 | * under the terms of the GNU General Public License as published by the Free | |
13 | * Software Foundation; either version 2 of the License, or (at your option) | |
14 | * any later version. | |
15 | */ | |
16 | ||
17 | #include <linux/module.h> | |
18 | #include <linux/slab.h> | |
19 | #include <linux/init.h> | |
20 | #include <linux/kernel.h> | |
1da177e4 LT |
21 | #include <linux/list.h> |
22 | #include <linux/mm.h> | |
23 | #include <linux/smp_lock.h> | |
24 | #include <linux/spinlock.h> | |
25 | #include <asm/unaligned.h> | |
26 | #include <asm/byteorder.h> | |
27 | #include <linux/input.h> | |
28 | #include <linux/wait.h> | |
29 | ||
1da177e4 LT |
30 | #include <linux/usb.h> |
31 | ||
dde5845a | 32 | #include <linux/hid.h> |
1da177e4 | 33 | #include <linux/hiddev.h> |
c080d89a | 34 | #include <linux/hid-debug.h> |
86166b7b | 35 | #include <linux/hidraw.h> |
4916b3a5 | 36 | #include "usbhid.h" |
1da177e4 LT |
37 | |
38 | /* | |
39 | * Version Information | |
40 | */ | |
41 | ||
bf0964dc | 42 | #define DRIVER_VERSION "v2.6" |
f142b3a4 | 43 | #define DRIVER_AUTHOR "Andreas Gal, Vojtech Pavlik, Jiri Kosina" |
1da177e4 LT |
44 | #define DRIVER_DESC "USB HID core driver" |
45 | #define DRIVER_LICENSE "GPL" | |
46 | ||
47 | static char *hid_types[] = {"Device", "Pointer", "Mouse", "Device", "Joystick", | |
48 | "Gamepad", "Keyboard", "Keypad", "Multi-Axis Controller"}; | |
49 | /* | |
50 | * Module parameters. | |
51 | */ | |
52 | ||
53 | static unsigned int hid_mousepoll_interval; | |
54 | module_param_named(mousepoll, hid_mousepoll_interval, uint, 0644); | |
55 | MODULE_PARM_DESC(mousepoll, "Polling interval of mice"); | |
56 | ||
876b9276 PW |
57 | /* Quirks specified at module load time */ |
58 | static char *quirks_param[MAX_USBHID_BOOT_QUIRKS] = { [ 0 ... (MAX_USBHID_BOOT_QUIRKS - 1) ] = NULL }; | |
59 | module_param_array_named(quirks, quirks_param, charp, NULL, 0444); | |
60 | MODULE_PARM_DESC(quirks, "Add/modify USB HID quirks by specifying " | |
61 | " quirks=vendorID:productID:quirks" | |
62 | " where vendorID, productID, and quirks are all in" | |
63 | " 0x-prefixed hex"); | |
ea9a4a8b JK |
64 | static char *rdesc_quirks_param[MAX_USBHID_BOOT_QUIRKS] = { [ 0 ... (MAX_USBHID_BOOT_QUIRKS - 1) ] = NULL }; |
65 | module_param_array_named(rdesc_quirks, rdesc_quirks_param, charp, NULL, 0444); | |
66 | MODULE_PARM_DESC(rdesc_quirks, "Add/modify report descriptor quirks by specifying " | |
67 | " rdesc_quirks=vendorID:productID:rdesc_quirks" | |
68 | " where vendorID, productID, and rdesc_quirks are all in" | |
69 | " 0x-prefixed hex"); | |
1da177e4 | 70 | /* |
48b4554a | 71 | * Input submission and I/O error handler. |
1da177e4 LT |
72 | */ |
73 | ||
48b4554a JK |
74 | static void hid_io_error(struct hid_device *hid); |
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 | |
48b4554a JK |
83 | spin_lock_irqsave(&usbhid->inlock, flags); |
84 | if (hid->open > 0 && !test_bit(HID_SUSPENDED, &usbhid->iofl) && | |
69626f23 | 85 | !test_bit(HID_DISCONNECTED, &usbhid->iofl) && |
48b4554a JK |
86 | !test_and_set_bit(HID_IN_RUNNING, &usbhid->iofl)) { |
87 | rc = usb_submit_urb(usbhid->urbin, GFP_ATOMIC); | |
88 | if (rc != 0) | |
89 | clear_bit(HID_IN_RUNNING, &usbhid->iofl); | |
1da177e4 | 90 | } |
48b4554a JK |
91 | spin_unlock_irqrestore(&usbhid->inlock, flags); |
92 | return rc; | |
1da177e4 LT |
93 | } |
94 | ||
48b4554a JK |
95 | /* I/O retry timer routine */ |
96 | static void hid_retry_timeout(unsigned long _hid) | |
1da177e4 | 97 | { |
48b4554a | 98 | struct hid_device *hid = (struct hid_device *) _hid; |
4916b3a5 | 99 | struct usbhid_device *usbhid = hid->driver_data; |
1da177e4 | 100 | |
48b4554a JK |
101 | dev_dbg(&usbhid->intf->dev, "retrying intr urb\n"); |
102 | if (hid_start_in(hid)) | |
103 | hid_io_error(hid); | |
1da177e4 LT |
104 | } |
105 | ||
48b4554a JK |
106 | /* Workqueue routine to reset the device or clear a halt */ |
107 | static void hid_reset(struct work_struct *work) | |
1da177e4 | 108 | { |
48b4554a JK |
109 | struct usbhid_device *usbhid = |
110 | container_of(work, struct usbhid_device, reset_work); | |
111 | struct hid_device *hid = usbhid->hid; | |
112 | int rc_lock, rc = 0; | |
1da177e4 | 113 | |
48b4554a JK |
114 | if (test_bit(HID_CLEAR_HALT, &usbhid->iofl)) { |
115 | dev_dbg(&usbhid->intf->dev, "clear halt\n"); | |
116 | rc = usb_clear_halt(hid_to_usb_dev(hid), usbhid->urbin->pipe); | |
117 | clear_bit(HID_CLEAR_HALT, &usbhid->iofl); | |
118 | hid_start_in(hid); | |
119 | } | |
1da177e4 | 120 | |
48b4554a JK |
121 | else if (test_bit(HID_RESET_PENDING, &usbhid->iofl)) { |
122 | dev_dbg(&usbhid->intf->dev, "resetting device\n"); | |
123 | rc = rc_lock = usb_lock_device_for_reset(hid_to_usb_dev(hid), usbhid->intf); | |
124 | if (rc_lock >= 0) { | |
742120c6 | 125 | rc = usb_reset_device(hid_to_usb_dev(hid)); |
48b4554a JK |
126 | if (rc_lock) |
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: | |
58037eb9 | 138 | err_hid("can't reset device, %s-%s/input%d, status %d", |
48b4554a JK |
139 | hid_to_usb_dev(hid)->bus->bus_name, |
140 | hid_to_usb_dev(hid)->devpath, | |
141 | usbhid->ifnum, rc); | |
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 | |
48b4554a | 156 | spin_lock_irqsave(&usbhid->inlock, 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: | |
186 | spin_unlock_irqrestore(&usbhid->inlock, flags); | |
1da177e4 LT |
187 | } |
188 | ||
48b4554a JK |
189 | /* |
190 | * Input interrupt completion handler. | |
191 | */ | |
854561b0 | 192 | |
48b4554a | 193 | static void hid_irq_in(struct urb *urb) |
1da177e4 | 194 | { |
48b4554a JK |
195 | struct hid_device *hid = urb->context; |
196 | struct usbhid_device *usbhid = hid->driver_data; | |
197 | int status; | |
1da177e4 | 198 | |
48b4554a JK |
199 | switch (urb->status) { |
200 | case 0: /* success */ | |
201 | usbhid->retry_delay = 0; | |
202 | hid_input_report(urb->context, HID_INPUT_REPORT, | |
203 | urb->transfer_buffer, | |
204 | urb->actual_length, 1); | |
205 | break; | |
206 | case -EPIPE: /* stall */ | |
207 | clear_bit(HID_IN_RUNNING, &usbhid->iofl); | |
208 | set_bit(HID_CLEAR_HALT, &usbhid->iofl); | |
209 | schedule_work(&usbhid->reset_work); | |
210 | return; | |
211 | case -ECONNRESET: /* unlink */ | |
212 | case -ENOENT: | |
213 | case -ESHUTDOWN: /* unplug */ | |
214 | clear_bit(HID_IN_RUNNING, &usbhid->iofl); | |
215 | return; | |
216 | case -EILSEQ: /* protocol error or unplug */ | |
217 | case -EPROTO: /* protocol error or unplug */ | |
218 | case -ETIME: /* protocol error or unplug */ | |
219 | case -ETIMEDOUT: /* Should never happen, but... */ | |
220 | clear_bit(HID_IN_RUNNING, &usbhid->iofl); | |
221 | hid_io_error(hid); | |
222 | return; | |
223 | default: /* error */ | |
224 | warn("input irq status %d received", urb->status); | |
225 | } | |
1da177e4 | 226 | |
48b4554a JK |
227 | status = usb_submit_urb(urb, GFP_ATOMIC); |
228 | if (status) { | |
229 | clear_bit(HID_IN_RUNNING, &usbhid->iofl); | |
230 | if (status != -EPERM) { | |
58037eb9 | 231 | err_hid("can't resubmit intr, %s-%s/input%d, status %d", |
48b4554a JK |
232 | hid_to_usb_dev(hid)->bus->bus_name, |
233 | hid_to_usb_dev(hid)->devpath, | |
234 | usbhid->ifnum, status); | |
235 | hid_io_error(hid); | |
236 | } | |
237 | } | |
1da177e4 LT |
238 | } |
239 | ||
48b4554a | 240 | static int hid_submit_out(struct hid_device *hid) |
1da177e4 | 241 | { |
48b4554a | 242 | struct hid_report *report; |
4916b3a5 JK |
243 | struct usbhid_device *usbhid = hid->driver_data; |
244 | ||
48b4554a | 245 | report = usbhid->out[usbhid->outtail]; |
1da177e4 | 246 | |
48b4554a JK |
247 | hid_output_report(report, usbhid->outbuf); |
248 | usbhid->urbout->transfer_buffer_length = ((report->size - 1) >> 3) + 1 + (report->id > 0); | |
249 | usbhid->urbout->dev = hid_to_usb_dev(hid); | |
1d3e2023 | 250 | |
58037eb9 | 251 | dbg_hid("submitting out urb\n"); |
d57cdcff | 252 | |
48b4554a | 253 | if (usb_submit_urb(usbhid->urbout, GFP_ATOMIC)) { |
58037eb9 | 254 | err_hid("usb_submit_urb(out) failed"); |
48b4554a JK |
255 | return -1; |
256 | } | |
1da177e4 | 257 | |
48b4554a JK |
258 | return 0; |
259 | } | |
260 | ||
261 | static int hid_submit_ctrl(struct hid_device *hid) | |
1da177e4 LT |
262 | { |
263 | struct hid_report *report; | |
48b4554a JK |
264 | unsigned char dir; |
265 | int len; | |
4916b3a5 | 266 | struct usbhid_device *usbhid = hid->driver_data; |
1da177e4 | 267 | |
48b4554a JK |
268 | report = usbhid->ctrl[usbhid->ctrltail].report; |
269 | dir = usbhid->ctrl[usbhid->ctrltail].dir; | |
1da177e4 | 270 | |
48b4554a JK |
271 | len = ((report->size - 1) >> 3) + 1 + (report->id > 0); |
272 | if (dir == USB_DIR_OUT) { | |
273 | hid_output_report(report, usbhid->ctrlbuf); | |
274 | usbhid->urbctrl->pipe = usb_sndctrlpipe(hid_to_usb_dev(hid), 0); | |
275 | usbhid->urbctrl->transfer_buffer_length = len; | |
276 | } else { | |
277 | int maxpacket, padlen; | |
1da177e4 | 278 | |
48b4554a JK |
279 | usbhid->urbctrl->pipe = usb_rcvctrlpipe(hid_to_usb_dev(hid), 0); |
280 | maxpacket = usb_maxpacket(hid_to_usb_dev(hid), usbhid->urbctrl->pipe, 0); | |
281 | if (maxpacket > 0) { | |
92c4a1b9 | 282 | padlen = DIV_ROUND_UP(len, maxpacket); |
48b4554a JK |
283 | padlen *= maxpacket; |
284 | if (padlen > usbhid->bufsize) | |
285 | padlen = usbhid->bufsize; | |
286 | } else | |
287 | padlen = 0; | |
288 | usbhid->urbctrl->transfer_buffer_length = padlen; | |
1da177e4 | 289 | } |
48b4554a | 290 | usbhid->urbctrl->dev = hid_to_usb_dev(hid); |
1da177e4 | 291 | |
48b4554a JK |
292 | usbhid->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE | dir; |
293 | usbhid->cr->bRequest = (dir == USB_DIR_OUT) ? HID_REQ_SET_REPORT : HID_REQ_GET_REPORT; | |
294 | usbhid->cr->wValue = cpu_to_le16(((report->type + 1) << 8) | report->id); | |
295 | usbhid->cr->wIndex = cpu_to_le16(usbhid->ifnum); | |
296 | usbhid->cr->wLength = cpu_to_le16(len); | |
1da177e4 | 297 | |
58037eb9 | 298 | dbg_hid("submitting ctrl urb: %s wValue=0x%04x wIndex=0x%04x wLength=%u\n", |
48b4554a JK |
299 | usbhid->cr->bRequest == HID_REQ_SET_REPORT ? "Set_Report" : "Get_Report", |
300 | usbhid->cr->wValue, usbhid->cr->wIndex, usbhid->cr->wLength); | |
1da177e4 | 301 | |
48b4554a | 302 | if (usb_submit_urb(usbhid->urbctrl, GFP_ATOMIC)) { |
58037eb9 | 303 | err_hid("usb_submit_urb(ctrl) failed"); |
48b4554a JK |
304 | return -1; |
305 | } | |
1da177e4 | 306 | |
48b4554a JK |
307 | return 0; |
308 | } | |
1da177e4 | 309 | |
48b4554a JK |
310 | /* |
311 | * Output interrupt completion handler. | |
312 | */ | |
1da177e4 | 313 | |
48b4554a JK |
314 | static void hid_irq_out(struct urb *urb) |
315 | { | |
316 | struct hid_device *hid = urb->context; | |
317 | struct usbhid_device *usbhid = hid->driver_data; | |
318 | unsigned long flags; | |
319 | int unplug = 0; | |
1da177e4 | 320 | |
48b4554a JK |
321 | switch (urb->status) { |
322 | case 0: /* success */ | |
323 | break; | |
324 | case -ESHUTDOWN: /* unplug */ | |
325 | unplug = 1; | |
326 | case -EILSEQ: /* protocol error or unplug */ | |
327 | case -EPROTO: /* protocol error or unplug */ | |
328 | case -ECONNRESET: /* unlink */ | |
329 | case -ENOENT: | |
330 | break; | |
331 | default: /* error */ | |
332 | warn("output irq status %d received", urb->status); | |
333 | } | |
1da177e4 | 334 | |
48b4554a | 335 | spin_lock_irqsave(&usbhid->outlock, flags); |
1da177e4 | 336 | |
48b4554a JK |
337 | if (unplug) |
338 | usbhid->outtail = usbhid->outhead; | |
339 | else | |
340 | usbhid->outtail = (usbhid->outtail + 1) & (HID_OUTPUT_FIFO_SIZE - 1); | |
1da177e4 | 341 | |
48b4554a JK |
342 | if (usbhid->outhead != usbhid->outtail) { |
343 | if (hid_submit_out(hid)) { | |
344 | clear_bit(HID_OUT_RUNNING, &usbhid->iofl); | |
1d1bdd20 | 345 | wake_up(&usbhid->wait); |
48b4554a JK |
346 | } |
347 | spin_unlock_irqrestore(&usbhid->outlock, flags); | |
348 | return; | |
349 | } | |
1da177e4 | 350 | |
48b4554a JK |
351 | clear_bit(HID_OUT_RUNNING, &usbhid->iofl); |
352 | spin_unlock_irqrestore(&usbhid->outlock, flags); | |
1d1bdd20 | 353 | wake_up(&usbhid->wait); |
48b4554a | 354 | } |
6345fdfd | 355 | |
48b4554a JK |
356 | /* |
357 | * Control pipe completion handler. | |
358 | */ | |
1da177e4 | 359 | |
48b4554a JK |
360 | static void hid_ctrl(struct urb *urb) |
361 | { | |
362 | struct hid_device *hid = urb->context; | |
363 | struct usbhid_device *usbhid = hid->driver_data; | |
364 | unsigned long flags; | |
365 | int unplug = 0; | |
1da177e4 | 366 | |
48b4554a | 367 | spin_lock_irqsave(&usbhid->ctrllock, flags); |
1da177e4 | 368 | |
48b4554a JK |
369 | switch (urb->status) { |
370 | case 0: /* success */ | |
371 | if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_IN) | |
372 | hid_input_report(urb->context, usbhid->ctrl[usbhid->ctrltail].report->type, | |
373 | urb->transfer_buffer, urb->actual_length, 0); | |
374 | break; | |
375 | case -ESHUTDOWN: /* unplug */ | |
376 | unplug = 1; | |
377 | case -EILSEQ: /* protocol error or unplug */ | |
378 | case -EPROTO: /* protocol error or unplug */ | |
379 | case -ECONNRESET: /* unlink */ | |
380 | case -ENOENT: | |
381 | case -EPIPE: /* report not available */ | |
382 | break; | |
383 | default: /* error */ | |
384 | warn("ctrl urb status %d received", urb->status); | |
385 | } | |
1da177e4 | 386 | |
48b4554a JK |
387 | if (unplug) |
388 | usbhid->ctrltail = usbhid->ctrlhead; | |
389 | else | |
390 | usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1); | |
1da177e4 | 391 | |
48b4554a JK |
392 | if (usbhid->ctrlhead != usbhid->ctrltail) { |
393 | if (hid_submit_ctrl(hid)) { | |
394 | clear_bit(HID_CTRL_RUNNING, &usbhid->iofl); | |
1d1bdd20 | 395 | wake_up(&usbhid->wait); |
48b4554a JK |
396 | } |
397 | spin_unlock_irqrestore(&usbhid->ctrllock, flags); | |
398 | return; | |
399 | } | |
1da177e4 | 400 | |
48b4554a JK |
401 | clear_bit(HID_CTRL_RUNNING, &usbhid->iofl); |
402 | spin_unlock_irqrestore(&usbhid->ctrllock, flags); | |
1d1bdd20 | 403 | wake_up(&usbhid->wait); |
48b4554a | 404 | } |
1da177e4 | 405 | |
48b4554a JK |
406 | void usbhid_submit_report(struct hid_device *hid, struct hid_report *report, unsigned char dir) |
407 | { | |
408 | int head; | |
409 | unsigned long flags; | |
410 | struct usbhid_device *usbhid = hid->driver_data; | |
1da177e4 | 411 | |
48b4554a JK |
412 | if ((hid->quirks & HID_QUIRK_NOGET) && dir == USB_DIR_IN) |
413 | return; | |
b857c651 | 414 | |
48b4554a | 415 | if (usbhid->urbout && dir == USB_DIR_OUT && report->type == HID_OUTPUT_REPORT) { |
1da177e4 | 416 | |
48b4554a | 417 | spin_lock_irqsave(&usbhid->outlock, flags); |
1da177e4 | 418 | |
48b4554a JK |
419 | if ((head = (usbhid->outhead + 1) & (HID_OUTPUT_FIFO_SIZE - 1)) == usbhid->outtail) { |
420 | spin_unlock_irqrestore(&usbhid->outlock, flags); | |
421 | warn("output queue full"); | |
422 | return; | |
423 | } | |
1da177e4 | 424 | |
48b4554a JK |
425 | usbhid->out[usbhid->outhead] = report; |
426 | usbhid->outhead = head; | |
4871d3be | 427 | |
48b4554a JK |
428 | if (!test_and_set_bit(HID_OUT_RUNNING, &usbhid->iofl)) |
429 | if (hid_submit_out(hid)) | |
430 | clear_bit(HID_OUT_RUNNING, &usbhid->iofl); | |
8fd6db47 | 431 | |
48b4554a JK |
432 | spin_unlock_irqrestore(&usbhid->outlock, flags); |
433 | return; | |
434 | } | |
1da177e4 | 435 | |
48b4554a | 436 | spin_lock_irqsave(&usbhid->ctrllock, flags); |
940824b0 | 437 | |
48b4554a JK |
438 | if ((head = (usbhid->ctrlhead + 1) & (HID_CONTROL_FIFO_SIZE - 1)) == usbhid->ctrltail) { |
439 | spin_unlock_irqrestore(&usbhid->ctrllock, flags); | |
440 | warn("control queue full"); | |
441 | return; | |
442 | } | |
8fd80133 | 443 | |
48b4554a JK |
444 | usbhid->ctrl[usbhid->ctrlhead].report = report; |
445 | usbhid->ctrl[usbhid->ctrlhead].dir = dir; | |
446 | usbhid->ctrlhead = head; | |
8fd80133 | 447 | |
48b4554a JK |
448 | if (!test_and_set_bit(HID_CTRL_RUNNING, &usbhid->iofl)) |
449 | if (hid_submit_ctrl(hid)) | |
450 | clear_bit(HID_CTRL_RUNNING, &usbhid->iofl); | |
931e24b9 | 451 | |
48b4554a JK |
452 | spin_unlock_irqrestore(&usbhid->ctrllock, flags); |
453 | } | |
23b0d968 | 454 | |
48b4554a JK |
455 | static int usb_hidinput_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) |
456 | { | |
e0712985 | 457 | struct hid_device *hid = input_get_drvdata(dev); |
48b4554a JK |
458 | struct hid_field *field; |
459 | int offset; | |
41ad5fba | 460 | |
48b4554a JK |
461 | if (type == EV_FF) |
462 | return input_ff_event(dev, type, code, value); | |
8d2bad87 | 463 | |
48b4554a JK |
464 | if (type != EV_LED) |
465 | return -1; | |
5556feae | 466 | |
48b4554a JK |
467 | if ((offset = hidinput_find_field(hid, type, code, &field)) == -1) { |
468 | warn("event field not found"); | |
469 | return -1; | |
470 | } | |
4a1a4d8b | 471 | |
48b4554a JK |
472 | hid_set_field(field, offset, value); |
473 | usbhid_submit_report(hid, field->report, USB_DIR_OUT); | |
1da177e4 | 474 | |
48b4554a JK |
475 | return 0; |
476 | } | |
1da177e4 | 477 | |
48b4554a JK |
478 | int usbhid_wait_io(struct hid_device *hid) |
479 | { | |
480 | struct usbhid_device *usbhid = hid->driver_data; | |
25914662 | 481 | |
1d1bdd20 JS |
482 | if (!wait_event_timeout(usbhid->wait, |
483 | (!test_bit(HID_CTRL_RUNNING, &usbhid->iofl) && | |
484 | !test_bit(HID_OUT_RUNNING, &usbhid->iofl)), | |
48b4554a | 485 | 10*HZ)) { |
58037eb9 | 486 | dbg_hid("timeout waiting for ctrl or out queue to clear\n"); |
48b4554a JK |
487 | return -1; |
488 | } | |
1da177e4 | 489 | |
48b4554a JK |
490 | return 0; |
491 | } | |
53880546 | 492 | |
48b4554a JK |
493 | static int hid_set_idle(struct usb_device *dev, int ifnum, int report, int idle) |
494 | { | |
495 | return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), | |
496 | HID_REQ_SET_IDLE, USB_TYPE_CLASS | USB_RECIP_INTERFACE, (idle << 8) | report, | |
497 | ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT); | |
498 | } | |
1da177e4 | 499 | |
48b4554a JK |
500 | static int hid_get_class_descriptor(struct usb_device *dev, int ifnum, |
501 | unsigned char type, void *buf, int size) | |
502 | { | |
503 | int result, retries = 4; | |
1da177e4 | 504 | |
48b4554a | 505 | memset(buf, 0, size); |
1da177e4 | 506 | |
48b4554a JK |
507 | do { |
508 | result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), | |
509 | USB_REQ_GET_DESCRIPTOR, USB_RECIP_INTERFACE | USB_DIR_IN, | |
510 | (type << 8), ifnum, buf, size, USB_CTRL_GET_TIMEOUT); | |
511 | retries--; | |
512 | } while (result < size && retries); | |
513 | return result; | |
514 | } | |
940824b0 | 515 | |
48b4554a JK |
516 | int usbhid_open(struct hid_device *hid) |
517 | { | |
933e3187 ON |
518 | struct usbhid_device *usbhid = hid->driver_data; |
519 | int res; | |
520 | ||
521 | if (!hid->open++) { | |
522 | res = usb_autopm_get_interface(usbhid->intf); | |
523 | if (res < 0) { | |
524 | hid->open--; | |
525 | return -EIO; | |
526 | } | |
527 | } | |
48b4554a JK |
528 | if (hid_start_in(hid)) |
529 | hid_io_error(hid); | |
530 | return 0; | |
531 | } | |
a417a21e | 532 | |
48b4554a JK |
533 | void usbhid_close(struct hid_device *hid) |
534 | { | |
535 | struct usbhid_device *usbhid = hid->driver_data; | |
eab9edd2 | 536 | |
933e3187 | 537 | if (!--hid->open) { |
48b4554a | 538 | usb_kill_urb(usbhid->urbin); |
933e3187 ON |
539 | usb_autopm_put_interface(usbhid->intf); |
540 | } | |
48b4554a | 541 | } |
1d3e2023 | 542 | |
48b4554a JK |
543 | /* |
544 | * Initialize all reports | |
545 | */ | |
41ad5fba | 546 | |
48b4554a JK |
547 | void usbhid_init_reports(struct hid_device *hid) |
548 | { | |
549 | struct hid_report *report; | |
550 | struct usbhid_device *usbhid = hid->driver_data; | |
551 | int err, ret; | |
41ad5fba | 552 | |
48b4554a JK |
553 | list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].report_list, list) |
554 | usbhid_submit_report(hid, report, USB_DIR_IN); | |
5556feae | 555 | |
48b4554a JK |
556 | list_for_each_entry(report, &hid->report_enum[HID_FEATURE_REPORT].report_list, list) |
557 | usbhid_submit_report(hid, report, USB_DIR_IN); | |
4a1a4d8b | 558 | |
48b4554a JK |
559 | err = 0; |
560 | ret = usbhid_wait_io(hid); | |
561 | while (ret) { | |
562 | err |= ret; | |
563 | if (test_bit(HID_CTRL_RUNNING, &usbhid->iofl)) | |
564 | usb_kill_urb(usbhid->urbctrl); | |
565 | if (test_bit(HID_OUT_RUNNING, &usbhid->iofl)) | |
566 | usb_kill_urb(usbhid->urbout); | |
567 | ret = usbhid_wait_io(hid); | |
568 | } | |
3f9b4076 | 569 | |
48b4554a JK |
570 | if (err) |
571 | warn("timeout initializing reports"); | |
572 | } | |
1da177e4 | 573 | |
713c8aad PZ |
574 | /* |
575 | * Reset LEDs which BIOS might have left on. For now, just NumLock (0x01). | |
576 | */ | |
577 | static int hid_find_field_early(struct hid_device *hid, unsigned int page, | |
578 | unsigned int hid_code, struct hid_field **pfield) | |
579 | { | |
580 | struct hid_report *report; | |
581 | struct hid_field *field; | |
582 | struct hid_usage *usage; | |
583 | int i, j; | |
584 | ||
585 | list_for_each_entry(report, &hid->report_enum[HID_OUTPUT_REPORT].report_list, list) { | |
586 | for (i = 0; i < report->maxfield; i++) { | |
587 | field = report->field[i]; | |
588 | for (j = 0; j < field->maxusage; j++) { | |
589 | usage = &field->usage[j]; | |
590 | if ((usage->hid & HID_USAGE_PAGE) == page && | |
591 | (usage->hid & 0xFFFF) == hid_code) { | |
592 | *pfield = field; | |
593 | return j; | |
594 | } | |
595 | } | |
596 | } | |
597 | } | |
598 | return -1; | |
599 | } | |
600 | ||
601 | static void usbhid_set_leds(struct hid_device *hid) | |
602 | { | |
603 | struct hid_field *field; | |
604 | int offset; | |
605 | ||
606 | if ((offset = hid_find_field_early(hid, HID_UP_LED, 0x01, &field)) != -1) { | |
607 | hid_set_field(field, offset, 0); | |
608 | usbhid_submit_report(hid, field->report, USB_DIR_OUT); | |
609 | } | |
610 | } | |
611 | ||
bf0964dc MH |
612 | /* |
613 | * Traverse the supplied list of reports and find the longest | |
614 | */ | |
282bfd4c JS |
615 | static void hid_find_max_report(struct hid_device *hid, unsigned int type, |
616 | unsigned int *max) | |
bf0964dc MH |
617 | { |
618 | struct hid_report *report; | |
282bfd4c | 619 | unsigned int size; |
bf0964dc MH |
620 | |
621 | list_for_each_entry(report, &hid->report_enum[type].report_list, list) { | |
622 | size = ((report->size - 1) >> 3) + 1; | |
623 | if (type == HID_INPUT_REPORT && hid->report_enum[type].numbered) | |
624 | size++; | |
625 | if (*max < size) | |
626 | *max = size; | |
627 | } | |
628 | } | |
629 | ||
1da177e4 LT |
630 | static int hid_alloc_buffers(struct usb_device *dev, struct hid_device *hid) |
631 | { | |
4916b3a5 JK |
632 | struct usbhid_device *usbhid = hid->driver_data; |
633 | ||
634 | if (!(usbhid->inbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->inbuf_dma))) | |
1da177e4 | 635 | return -1; |
4916b3a5 | 636 | if (!(usbhid->outbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->outbuf_dma))) |
1da177e4 | 637 | return -1; |
4916b3a5 | 638 | if (!(usbhid->cr = usb_buffer_alloc(dev, sizeof(*(usbhid->cr)), GFP_ATOMIC, &usbhid->cr_dma))) |
1da177e4 | 639 | return -1; |
4916b3a5 | 640 | if (!(usbhid->ctrlbuf = usb_buffer_alloc(dev, usbhid->bufsize, GFP_ATOMIC, &usbhid->ctrlbuf_dma))) |
1da177e4 LT |
641 | return -1; |
642 | ||
643 | return 0; | |
644 | } | |
645 | ||
efc493f9 JK |
646 | static int usbhid_output_raw_report(struct hid_device *hid, __u8 *buf, size_t count) |
647 | { | |
648 | struct usbhid_device *usbhid = hid->driver_data; | |
649 | struct usb_device *dev = hid_to_usb_dev(hid); | |
650 | struct usb_interface *intf = usbhid->intf; | |
651 | struct usb_host_interface *interface = intf->cur_altsetting; | |
652 | int ret; | |
653 | ||
654 | ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), | |
655 | HID_REQ_SET_REPORT, | |
656 | USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE, | |
01d7b369 | 657 | ((HID_OUTPUT_REPORT + 1) << 8) | *buf, |
efc493f9 JK |
658 | interface->desc.bInterfaceNumber, buf + 1, count - 1, |
659 | USB_CTRL_SET_TIMEOUT); | |
660 | ||
661 | /* count also the report id */ | |
662 | if (ret > 0) | |
663 | ret++; | |
664 | ||
665 | return ret; | |
666 | } | |
667 | ||
1da177e4 LT |
668 | static void hid_free_buffers(struct usb_device *dev, struct hid_device *hid) |
669 | { | |
4916b3a5 JK |
670 | struct usbhid_device *usbhid = hid->driver_data; |
671 | ||
6675c5bd DT |
672 | usb_buffer_free(dev, usbhid->bufsize, usbhid->inbuf, usbhid->inbuf_dma); |
673 | usb_buffer_free(dev, usbhid->bufsize, usbhid->outbuf, usbhid->outbuf_dma); | |
674 | usb_buffer_free(dev, sizeof(*(usbhid->cr)), usbhid->cr, usbhid->cr_dma); | |
675 | usb_buffer_free(dev, usbhid->bufsize, usbhid->ctrlbuf, usbhid->ctrlbuf_dma); | |
1da177e4 LT |
676 | } |
677 | ||
4a1a4d8b GL |
678 | /* |
679 | * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller | |
680 | * to "operational". Without this, the ps3 controller will not report any | |
681 | * events. | |
682 | */ | |
683 | static void hid_fixup_sony_ps3_controller(struct usb_device *dev, int ifnum) | |
684 | { | |
685 | int result; | |
686 | char *buf = kmalloc(18, GFP_KERNEL); | |
687 | ||
688 | if (!buf) | |
689 | return; | |
690 | ||
691 | result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), | |
692 | HID_REQ_GET_REPORT, | |
693 | USB_DIR_IN | USB_TYPE_CLASS | | |
694 | USB_RECIP_INTERFACE, | |
695 | (3 << 8) | 0xf2, ifnum, buf, 17, | |
696 | USB_CTRL_GET_TIMEOUT); | |
697 | ||
698 | if (result < 0) | |
58037eb9 | 699 | err_hid("%s failed: %d\n", __func__, result); |
4a1a4d8b GL |
700 | |
701 | kfree(buf); | |
702 | } | |
703 | ||
c500c971 | 704 | static int usbhid_start_finish(struct hid_device *hid) |
1da177e4 | 705 | { |
c500c971 JS |
706 | struct usb_interface *intf = to_usb_interface(hid->dev.parent); |
707 | char path[64], *type; | |
708 | unsigned int i; | |
709 | ||
710 | usbhid_init_reports(hid); | |
711 | hid_dump_device(hid); | |
712 | if (hid->quirks & HID_QUIRK_RESET_LEDS) | |
713 | usbhid_set_leds(hid); | |
714 | ||
715 | if (!hidinput_connect(hid)) | |
716 | hid->claimed |= HID_CLAIMED_INPUT; | |
717 | if (!hiddev_connect(hid)) | |
718 | hid->claimed |= HID_CLAIMED_HIDDEV; | |
719 | if (!hidraw_connect(hid)) | |
720 | hid->claimed |= HID_CLAIMED_HIDRAW; | |
721 | ||
722 | if (!hid->claimed) { | |
723 | printk(KERN_ERR "HID device claimed by neither input, hiddev " | |
724 | "nor hidraw\n"); | |
725 | return -ENODEV; | |
726 | } | |
727 | ||
728 | if ((hid->claimed & HID_CLAIMED_INPUT)) | |
729 | hid_ff_init(hid); | |
730 | ||
731 | if (hid->quirks & HID_QUIRK_SONY_PS3_CONTROLLER) | |
732 | hid_fixup_sony_ps3_controller(interface_to_usbdev(intf), | |
733 | intf->cur_altsetting->desc.bInterfaceNumber); | |
734 | ||
735 | printk(KERN_INFO); | |
736 | ||
737 | if (hid->claimed & HID_CLAIMED_INPUT) | |
738 | printk("input"); | |
739 | if ((hid->claimed & HID_CLAIMED_INPUT) && | |
740 | ((hid->claimed & HID_CLAIMED_HIDDEV) || | |
741 | hid->claimed & HID_CLAIMED_HIDRAW)) | |
742 | printk(","); | |
743 | if (hid->claimed & HID_CLAIMED_HIDDEV) | |
744 | printk("hiddev%d", hid->minor); | |
745 | if ((hid->claimed & HID_CLAIMED_INPUT) && | |
746 | (hid->claimed & HID_CLAIMED_HIDDEV) && | |
747 | (hid->claimed & HID_CLAIMED_HIDRAW)) | |
748 | printk(","); | |
749 | if (hid->claimed & HID_CLAIMED_HIDRAW) | |
750 | printk("hidraw%d", ((struct hidraw *)hid->hidraw)->minor); | |
751 | ||
752 | type = "Device"; | |
753 | for (i = 0; i < hid->maxcollection; i++) { | |
754 | if (hid->collection[i].type == HID_COLLECTION_APPLICATION && | |
755 | (hid->collection[i].usage & HID_USAGE_PAGE) == | |
756 | HID_UP_GENDESK && | |
757 | (hid->collection[i].usage & 0xffff) < | |
758 | ARRAY_SIZE(hid_types)) { | |
759 | type = hid_types[hid->collection[i].usage & 0xffff]; | |
760 | break; | |
761 | } | |
762 | } | |
763 | ||
764 | usb_make_path(interface_to_usbdev(intf), path, 63); | |
765 | ||
766 | printk(": USB HID v%x.%02x %s [%s] on %s\n", | |
767 | hid->version >> 8, hid->version & 0xff, type, hid->name, path); | |
768 | ||
769 | return 0; | |
770 | } | |
771 | ||
772 | static int usbhid_parse(struct hid_device *hid) | |
773 | { | |
774 | struct usb_interface *intf = to_usb_interface(hid->dev.parent); | |
1da177e4 LT |
775 | struct usb_host_interface *interface = intf->cur_altsetting; |
776 | struct usb_device *dev = interface_to_usbdev (intf); | |
777 | struct hid_descriptor *hdesc; | |
2eb5dc30 | 778 | u32 quirks = 0; |
c500c971 | 779 | unsigned int rsize = 0; |
c5b7c7c3 | 780 | char *rdesc; |
c500c971 | 781 | int ret, n; |
1da177e4 | 782 | |
2eb5dc30 PW |
783 | quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor), |
784 | le16_to_cpu(dev->descriptor.idProduct)); | |
1da177e4 | 785 | |
0f28b55d AS |
786 | /* Many keyboards and mice don't like to be polled for reports, |
787 | * so we will always set the HID_QUIRK_NOGET flag for them. */ | |
788 | if (interface->desc.bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) { | |
789 | if (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_KEYBOARD || | |
790 | interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE) | |
791 | quirks |= HID_QUIRK_NOGET; | |
792 | } | |
793 | ||
a417a21e SS |
794 | if ((quirks & HID_QUIRK_IGNORE_MOUSE) && |
795 | (interface->desc.bInterfaceProtocol == USB_INTERFACE_PROTOCOL_MOUSE)) | |
c500c971 | 796 | return -ENODEV; |
a417a21e | 797 | |
c5b7c7c3 DT |
798 | if (usb_get_extra_descriptor(interface, HID_DT_HID, &hdesc) && |
799 | (!interface->desc.bNumEndpoints || | |
800 | usb_get_extra_descriptor(&interface->endpoint[0], HID_DT_HID, &hdesc))) { | |
58037eb9 | 801 | dbg_hid("class descriptor not present\n"); |
c500c971 | 802 | return -ENODEV; |
1da177e4 LT |
803 | } |
804 | ||
c500c971 JS |
805 | hid->version = le16_to_cpu(hdesc->bcdHID); |
806 | hid->country = hdesc->bCountryCode; | |
807 | ||
1da177e4 LT |
808 | for (n = 0; n < hdesc->bNumDescriptors; n++) |
809 | if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT) | |
810 | rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength); | |
811 | ||
812 | if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) { | |
58037eb9 | 813 | dbg_hid("weird size of report descriptor (%u)\n", rsize); |
c500c971 | 814 | return -EINVAL; |
1da177e4 LT |
815 | } |
816 | ||
817 | if (!(rdesc = kmalloc(rsize, GFP_KERNEL))) { | |
58037eb9 | 818 | dbg_hid("couldn't allocate rdesc memory\n"); |
c500c971 | 819 | return -ENOMEM; |
1da177e4 LT |
820 | } |
821 | ||
854561b0 VP |
822 | hid_set_idle(dev, interface->desc.bInterfaceNumber, 0, 0); |
823 | ||
c500c971 JS |
824 | ret = hid_get_class_descriptor(dev, interface->desc.bInterfaceNumber, |
825 | HID_DT_REPORT, rdesc, rsize); | |
826 | if (ret < 0) { | |
58037eb9 | 827 | dbg_hid("reading report descriptor failed\n"); |
1da177e4 | 828 | kfree(rdesc); |
c500c971 | 829 | goto err; |
1da177e4 LT |
830 | } |
831 | ||
ea9a4a8b JK |
832 | usbhid_fixup_report_descriptor(le16_to_cpu(dev->descriptor.idVendor), |
833 | le16_to_cpu(dev->descriptor.idProduct), rdesc, | |
834 | rsize, rdesc_quirks_param); | |
66da8769 | 835 | |
58037eb9 | 836 | dbg_hid("report descriptor (size %u, read %d) = ", rsize, n); |
1da177e4 | 837 | for (n = 0; n < rsize; n++) |
58037eb9 JK |
838 | dbg_hid_line(" %02x", (unsigned char) rdesc[n]); |
839 | dbg_hid_line("\n"); | |
1da177e4 | 840 | |
c500c971 JS |
841 | ret = hid_parse_report(hid, rdesc, rsize); |
842 | kfree(rdesc); | |
843 | if (ret) { | |
58037eb9 | 844 | dbg_hid("parsing report descriptor failed\n"); |
c500c971 | 845 | goto err; |
1da177e4 LT |
846 | } |
847 | ||
1da177e4 LT |
848 | hid->quirks = quirks; |
849 | ||
c500c971 JS |
850 | return 0; |
851 | err: | |
852 | return ret; | |
853 | } | |
854 | ||
855 | static int usbhid_start(struct hid_device *hid) | |
856 | { | |
857 | struct usb_interface *intf = to_usb_interface(hid->dev.parent); | |
858 | struct usb_host_interface *interface = intf->cur_altsetting; | |
859 | struct usb_device *dev = interface_to_usbdev(intf); | |
860 | struct usbhid_device *usbhid; | |
861 | unsigned int n, insize = 0; | |
862 | int ret; | |
863 | ||
864 | WARN_ON(hid->driver_data); | |
865 | ||
866 | usbhid = kzalloc(sizeof(struct usbhid_device), GFP_KERNEL); | |
867 | if (usbhid == NULL) { | |
868 | ret = -ENOMEM; | |
869 | goto err; | |
870 | } | |
4916b3a5 JK |
871 | |
872 | hid->driver_data = usbhid; | |
873 | usbhid->hid = hid; | |
874 | ||
875 | usbhid->bufsize = HID_MIN_BUFFER_SIZE; | |
876 | hid_find_max_report(hid, HID_INPUT_REPORT, &usbhid->bufsize); | |
877 | hid_find_max_report(hid, HID_OUTPUT_REPORT, &usbhid->bufsize); | |
878 | hid_find_max_report(hid, HID_FEATURE_REPORT, &usbhid->bufsize); | |
bf0964dc | 879 | |
4916b3a5 JK |
880 | if (usbhid->bufsize > HID_MAX_BUFFER_SIZE) |
881 | usbhid->bufsize = HID_MAX_BUFFER_SIZE; | |
bf0964dc MH |
882 | |
883 | hid_find_max_report(hid, HID_INPUT_REPORT, &insize); | |
884 | ||
885 | if (insize > HID_MAX_BUFFER_SIZE) | |
886 | insize = HID_MAX_BUFFER_SIZE; | |
887 | ||
c500c971 JS |
888 | if (hid_alloc_buffers(dev, hid)) { |
889 | ret = -ENOMEM; | |
1da177e4 | 890 | goto fail; |
f345c37c PS |
891 | } |
892 | ||
1da177e4 | 893 | for (n = 0; n < interface->desc.bNumEndpoints; n++) { |
1da177e4 LT |
894 | struct usb_endpoint_descriptor *endpoint; |
895 | int pipe; | |
896 | int interval; | |
897 | ||
898 | endpoint = &interface->endpoint[n].desc; | |
899 | if ((endpoint->bmAttributes & 3) != 3) /* Not an interrupt endpoint */ | |
900 | continue; | |
901 | ||
1da177e4 | 902 | interval = endpoint->bInterval; |
1da177e4 | 903 | |
f345c37c | 904 | /* Some vendors give fullspeed interval on highspeed devides */ |
c500c971 | 905 | if (hid->quirks & HID_QUIRK_FULLSPEED_INTERVAL && |
f345c37c PS |
906 | dev->speed == USB_SPEED_HIGH) { |
907 | interval = fls(endpoint->bInterval*8); | |
908 | printk(KERN_INFO "%s: Fixing fullspeed to highspeed interval: %d -> %d\n", | |
909 | hid->name, endpoint->bInterval, interval); | |
910 | } | |
911 | ||
1da177e4 LT |
912 | /* Change the polling interval of mice. */ |
913 | if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0) | |
914 | interval = hid_mousepoll_interval; | |
05f091ab | 915 | |
c500c971 | 916 | ret = -ENOMEM; |
0f12aa03 | 917 | if (usb_endpoint_dir_in(endpoint)) { |
4916b3a5 | 918 | if (usbhid->urbin) |
1da177e4 | 919 | continue; |
4916b3a5 | 920 | if (!(usbhid->urbin = usb_alloc_urb(0, GFP_KERNEL))) |
1da177e4 LT |
921 | goto fail; |
922 | pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress); | |
4916b3a5 | 923 | usb_fill_int_urb(usbhid->urbin, dev, pipe, usbhid->inbuf, insize, |
1da177e4 | 924 | hid_irq_in, hid, interval); |
4916b3a5 JK |
925 | usbhid->urbin->transfer_dma = usbhid->inbuf_dma; |
926 | usbhid->urbin->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | |
1da177e4 | 927 | } else { |
4916b3a5 | 928 | if (usbhid->urbout) |
1da177e4 | 929 | continue; |
4916b3a5 | 930 | if (!(usbhid->urbout = usb_alloc_urb(0, GFP_KERNEL))) |
1da177e4 LT |
931 | goto fail; |
932 | pipe = usb_sndintpipe(dev, endpoint->bEndpointAddress); | |
4916b3a5 | 933 | usb_fill_int_urb(usbhid->urbout, dev, pipe, usbhid->outbuf, 0, |
1da177e4 | 934 | hid_irq_out, hid, interval); |
4916b3a5 JK |
935 | usbhid->urbout->transfer_dma = usbhid->outbuf_dma; |
936 | usbhid->urbout->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | |
1da177e4 LT |
937 | } |
938 | } | |
939 | ||
4916b3a5 | 940 | if (!usbhid->urbin) { |
58037eb9 | 941 | err_hid("couldn't find an input interrupt endpoint"); |
c500c971 | 942 | ret = -ENODEV; |
1da177e4 LT |
943 | goto fail; |
944 | } | |
945 | ||
1d1bdd20 | 946 | init_waitqueue_head(&usbhid->wait); |
4916b3a5 JK |
947 | INIT_WORK(&usbhid->reset_work, hid_reset); |
948 | setup_timer(&usbhid->io_retry, hid_retry_timeout, (unsigned long) hid); | |
aef4e266 | 949 | |
4916b3a5 JK |
950 | spin_lock_init(&usbhid->inlock); |
951 | spin_lock_init(&usbhid->outlock); | |
952 | spin_lock_init(&usbhid->ctrllock); | |
1da177e4 | 953 | |
4916b3a5 JK |
954 | usbhid->intf = intf; |
955 | usbhid->ifnum = interface->desc.bInterfaceNumber; | |
1da177e4 | 956 | |
4916b3a5 | 957 | usbhid->urbctrl = usb_alloc_urb(0, GFP_KERNEL); |
c500c971 JS |
958 | if (!usbhid->urbctrl) { |
959 | ret = -ENOMEM; | |
1da177e4 | 960 | goto fail; |
c500c971 | 961 | } |
c5b7c7c3 | 962 | |
4916b3a5 JK |
963 | usb_fill_control_urb(usbhid->urbctrl, dev, 0, (void *) usbhid->cr, |
964 | usbhid->ctrlbuf, 1, hid_ctrl, hid); | |
965 | usbhid->urbctrl->setup_dma = usbhid->cr_dma; | |
966 | usbhid->urbctrl->transfer_dma = usbhid->ctrlbuf_dma; | |
967 | usbhid->urbctrl->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP); | |
c500c971 JS |
968 | |
969 | ret = usbhid_start_finish(hid); | |
970 | if (ret) | |
971 | goto fail; | |
972 | ||
973 | return 0; | |
1da177e4 LT |
974 | |
975 | fail: | |
4916b3a5 JK |
976 | usb_free_urb(usbhid->urbin); |
977 | usb_free_urb(usbhid->urbout); | |
978 | usb_free_urb(usbhid->urbctrl); | |
22f675f3 | 979 | hid_free_buffers(dev, hid); |
cda5ecf8 | 980 | kfree(usbhid); |
c500c971 JS |
981 | err: |
982 | return ret; | |
1da177e4 LT |
983 | } |
984 | ||
c500c971 | 985 | static void usbhid_stop(struct hid_device *hid) |
1da177e4 | 986 | { |
c500c971 | 987 | struct usbhid_device *usbhid = hid->driver_data; |
1da177e4 | 988 | |
c500c971 | 989 | if (WARN_ON(!usbhid)) |
1da177e4 LT |
990 | return; |
991 | ||
4916b3a5 | 992 | spin_lock_irq(&usbhid->inlock); /* Sync with error handler */ |
69626f23 | 993 | set_bit(HID_DISCONNECTED, &usbhid->iofl); |
4916b3a5 JK |
994 | spin_unlock_irq(&usbhid->inlock); |
995 | usb_kill_urb(usbhid->urbin); | |
996 | usb_kill_urb(usbhid->urbout); | |
997 | usb_kill_urb(usbhid->urbctrl); | |
1da177e4 | 998 | |
4916b3a5 | 999 | del_timer_sync(&usbhid->io_retry); |
2fa45a4c | 1000 | cancel_work_sync(&usbhid->reset_work); |
aef4e266 | 1001 | |
1da177e4 LT |
1002 | if (hid->claimed & HID_CLAIMED_INPUT) |
1003 | hidinput_disconnect(hid); | |
1004 | if (hid->claimed & HID_CLAIMED_HIDDEV) | |
1005 | hiddev_disconnect(hid); | |
86166b7b JK |
1006 | if (hid->claimed & HID_CLAIMED_HIDRAW) |
1007 | hidraw_disconnect(hid); | |
1da177e4 | 1008 | |
c500c971 JS |
1009 | hid->claimed = 0; |
1010 | ||
4916b3a5 JK |
1011 | usb_free_urb(usbhid->urbin); |
1012 | usb_free_urb(usbhid->urbctrl); | |
1013 | usb_free_urb(usbhid->urbout); | |
1da177e4 | 1014 | |
be820975 | 1015 | hid_free_buffers(hid_to_usb_dev(hid), hid); |
22f675f3 | 1016 | kfree(usbhid); |
c500c971 | 1017 | hid->driver_data = NULL; |
1da177e4 LT |
1018 | } |
1019 | ||
c500c971 JS |
1020 | static struct hid_ll_driver usb_hid_driver = { |
1021 | .parse = usbhid_parse, | |
1022 | .start = usbhid_start, | |
1023 | .stop = usbhid_stop, | |
1024 | .open = usbhid_open, | |
1025 | .close = usbhid_close, | |
1026 | .hidinput_input_event = usb_hidinput_input_event, | |
1027 | }; | |
1028 | ||
1da177e4 LT |
1029 | static int hid_probe(struct usb_interface *intf, const struct usb_device_id *id) |
1030 | { | |
c500c971 | 1031 | struct usb_device *dev = interface_to_usbdev(intf); |
1da177e4 | 1032 | struct hid_device *hid; |
c500c971 JS |
1033 | size_t len; |
1034 | int ret; | |
1da177e4 | 1035 | |
58037eb9 | 1036 | dbg_hid("HID probe called for ifnum %d\n", |
1da177e4 LT |
1037 | intf->altsetting->desc.bInterfaceNumber); |
1038 | ||
c500c971 JS |
1039 | hid = hid_allocate_device(); |
1040 | if (IS_ERR(hid)) | |
1041 | return PTR_ERR(hid); | |
1da177e4 LT |
1042 | |
1043 | usb_set_intfdata(intf, hid); | |
c500c971 JS |
1044 | hid->ll_driver = &usb_hid_driver; |
1045 | hid->hid_output_raw_report = usbhid_output_raw_report; | |
1046 | #ifdef CONFIG_USB_HIDDEV | |
1047 | hid->hiddev_hid_event = hiddev_hid_event; | |
1048 | hid->hiddev_report_event = hiddev_report_event; | |
1049 | #endif | |
1050 | hid->dev.parent = &intf->dev; | |
1051 | hid->bus = BUS_USB; | |
1052 | hid->vendor = le16_to_cpu(dev->descriptor.idVendor); | |
1053 | hid->product = le16_to_cpu(dev->descriptor.idProduct); | |
1054 | hid->name[0] = 0; | |
1da177e4 | 1055 | |
c500c971 JS |
1056 | if (dev->manufacturer) |
1057 | strlcpy(hid->name, dev->manufacturer, sizeof(hid->name)); | |
1da177e4 | 1058 | |
c500c971 JS |
1059 | if (dev->product) { |
1060 | if (dev->manufacturer) | |
1061 | strlcat(hid->name, " ", sizeof(hid->name)); | |
1062 | strlcat(hid->name, dev->product, sizeof(hid->name)); | |
1da177e4 LT |
1063 | } |
1064 | ||
c500c971 JS |
1065 | if (!strlen(hid->name)) |
1066 | snprintf(hid->name, sizeof(hid->name), "HID %04x:%04x", | |
1067 | le16_to_cpu(dev->descriptor.idVendor), | |
1068 | le16_to_cpu(dev->descriptor.idProduct)); | |
1da177e4 | 1069 | |
c500c971 JS |
1070 | usb_make_path(dev, hid->phys, sizeof(hid->phys)); |
1071 | strlcat(hid->phys, "/input", sizeof(hid->phys)); | |
1072 | len = strlen(hid->phys); | |
1073 | if (len < sizeof(hid->phys) - 1) | |
1074 | snprintf(hid->phys + len, sizeof(hid->phys) - len, | |
1075 | "%d", intf->altsetting[0].desc.bInterfaceNumber); | |
1076 | ||
1077 | if (usb_string(dev, dev->descriptor.iSerialNumber, hid->uniq, 64) <= 0) | |
1078 | hid->uniq[0] = 0; | |
1da177e4 | 1079 | |
85cdaf52 JS |
1080 | ret = hid_add_device(hid); |
1081 | if (ret) { | |
d458a9df JS |
1082 | if (ret != -ENODEV) |
1083 | dev_err(&intf->dev, "can't add hid device: %d\n", ret); | |
c500c971 | 1084 | goto err; |
85cdaf52 | 1085 | } |
c500c971 JS |
1086 | |
1087 | return 0; | |
1088 | err: | |
1089 | hid_destroy_device(hid); | |
85cdaf52 | 1090 | return ret; |
1da177e4 LT |
1091 | } |
1092 | ||
c500c971 JS |
1093 | static void hid_disconnect(struct usb_interface *intf) |
1094 | { | |
1095 | struct hid_device *hid = usb_get_intfdata(intf); | |
1096 | ||
1097 | if (WARN_ON(!hid)) | |
1098 | return; | |
1099 | ||
1100 | hid_destroy_device(hid); | |
1101 | } | |
1102 | ||
27d72e85 | 1103 | static int hid_suspend(struct usb_interface *intf, pm_message_t message) |
1da177e4 LT |
1104 | { |
1105 | struct hid_device *hid = usb_get_intfdata (intf); | |
4916b3a5 | 1106 | struct usbhid_device *usbhid = hid->driver_data; |
1da177e4 | 1107 | |
4916b3a5 JK |
1108 | spin_lock_irq(&usbhid->inlock); /* Sync with error handler */ |
1109 | set_bit(HID_SUSPENDED, &usbhid->iofl); | |
1110 | spin_unlock_irq(&usbhid->inlock); | |
1111 | del_timer(&usbhid->io_retry); | |
1112 | usb_kill_urb(usbhid->urbin); | |
1da177e4 LT |
1113 | dev_dbg(&intf->dev, "suspend\n"); |
1114 | return 0; | |
1115 | } | |
1116 | ||
1117 | static int hid_resume(struct usb_interface *intf) | |
1118 | { | |
1119 | struct hid_device *hid = usb_get_intfdata (intf); | |
4916b3a5 | 1120 | struct usbhid_device *usbhid = hid->driver_data; |
1da177e4 LT |
1121 | int status; |
1122 | ||
4916b3a5 JK |
1123 | clear_bit(HID_SUSPENDED, &usbhid->iofl); |
1124 | usbhid->retry_delay = 0; | |
aef4e266 | 1125 | status = hid_start_in(hid); |
1da177e4 LT |
1126 | dev_dbg(&intf->dev, "resume status %d\n", status); |
1127 | return status; | |
1128 | } | |
1129 | ||
df9a1f48 | 1130 | /* Treat USB reset pretty much the same as suspend/resume */ |
f07600cf | 1131 | static int hid_pre_reset(struct usb_interface *intf) |
df9a1f48 AS |
1132 | { |
1133 | /* FIXME: What if the interface is already suspended? */ | |
1134 | hid_suspend(intf, PMSG_ON); | |
f07600cf | 1135 | return 0; |
df9a1f48 AS |
1136 | } |
1137 | ||
f07600cf AS |
1138 | /* Same routine used for post_reset and reset_resume */ |
1139 | static int hid_post_reset(struct usb_interface *intf) | |
df9a1f48 AS |
1140 | { |
1141 | struct usb_device *dev = interface_to_usbdev (intf); | |
1142 | ||
1143 | hid_set_idle(dev, intf->cur_altsetting->desc.bInterfaceNumber, 0, 0); | |
1144 | /* FIXME: Any more reinitialization needed? */ | |
1145 | ||
f07600cf | 1146 | return hid_resume(intf); |
df9a1f48 AS |
1147 | } |
1148 | ||
1da177e4 LT |
1149 | static struct usb_device_id hid_usb_ids [] = { |
1150 | { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS, | |
1151 | .bInterfaceClass = USB_INTERFACE_CLASS_HID }, | |
1152 | { } /* Terminating entry */ | |
1153 | }; | |
1154 | ||
1155 | MODULE_DEVICE_TABLE (usb, hid_usb_ids); | |
1156 | ||
1157 | static struct usb_driver hid_driver = { | |
1da177e4 LT |
1158 | .name = "usbhid", |
1159 | .probe = hid_probe, | |
1160 | .disconnect = hid_disconnect, | |
1161 | .suspend = hid_suspend, | |
1162 | .resume = hid_resume, | |
f07600cf | 1163 | .reset_resume = hid_post_reset, |
df9a1f48 AS |
1164 | .pre_reset = hid_pre_reset, |
1165 | .post_reset = hid_post_reset, | |
1da177e4 | 1166 | .id_table = hid_usb_ids, |
933e3187 | 1167 | .supports_autosuspend = 1, |
1da177e4 LT |
1168 | }; |
1169 | ||
85cdaf52 JS |
1170 | static const struct hid_device_id hid_usb_table[] = { |
1171 | { HID_USB_DEVICE(HID_ANY_ID, HID_ANY_ID) }, | |
1172 | { } | |
1173 | }; | |
1174 | ||
1175 | static struct hid_driver hid_usb_driver = { | |
1176 | .name = "generic-usb", | |
1177 | .id_table = hid_usb_table, | |
1178 | }; | |
1179 | ||
1da177e4 LT |
1180 | static int __init hid_init(void) |
1181 | { | |
1182 | int retval; | |
85cdaf52 JS |
1183 | retval = hid_register_driver(&hid_usb_driver); |
1184 | if (retval) | |
1185 | goto hid_register_fail; | |
876b9276 PW |
1186 | retval = usbhid_quirks_init(quirks_param); |
1187 | if (retval) | |
1188 | goto usbhid_quirks_init_fail; | |
1da177e4 LT |
1189 | retval = hiddev_init(); |
1190 | if (retval) | |
1191 | goto hiddev_init_fail; | |
1192 | retval = usb_register(&hid_driver); | |
1193 | if (retval) | |
1194 | goto usb_register_fail; | |
1195 | info(DRIVER_VERSION ":" DRIVER_DESC); | |
1196 | ||
1197 | return 0; | |
1198 | usb_register_fail: | |
1199 | hiddev_exit(); | |
1200 | hiddev_init_fail: | |
876b9276 PW |
1201 | usbhid_quirks_exit(); |
1202 | usbhid_quirks_init_fail: | |
85cdaf52 JS |
1203 | hid_unregister_driver(&hid_usb_driver); |
1204 | hid_register_fail: | |
1da177e4 LT |
1205 | return retval; |
1206 | } | |
1207 | ||
1208 | static void __exit hid_exit(void) | |
1209 | { | |
1210 | usb_deregister(&hid_driver); | |
1211 | hiddev_exit(); | |
876b9276 | 1212 | usbhid_quirks_exit(); |
85cdaf52 | 1213 | hid_unregister_driver(&hid_usb_driver); |
1da177e4 LT |
1214 | } |
1215 | ||
1216 | module_init(hid_init); | |
1217 | module_exit(hid_exit); | |
1218 | ||
1219 | MODULE_AUTHOR(DRIVER_AUTHOR); | |
1220 | MODULE_DESCRIPTION(DRIVER_DESC); | |
1221 | MODULE_LICENSE(DRIVER_LICENSE); |