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