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