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