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