1 /* $NetBSD: usbdi_util.c,v 1.42 2004/12/03 08:53:40 augustss Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/usbdi_util.c,v 1.34 2005/03/01 08:01:22 sobomax Exp $ */
3 /* $DragonFly: src/sys/bus/usb/usbdi_util.c,v 1.14 2007/06/28 13:55:12 hasso Exp $ */
6 * Copyright (c) 1998 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Lennart Augustsson (lennart@augustsson.net) at
11 * Carlstedt Research & Technology.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the NetBSD
24 * Foundation, Inc. and its contributors.
25 * 4. Neither the name of The NetBSD Foundation nor the names of its
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/module.h>
46 #include <sys/malloc.h>
48 #include <sys/thread2.h>
50 #include <bus/usb/usb.h>
51 #include <bus/usb/usbhid.h>
53 #include <bus/usb/usbdi.h>
54 #include <bus/usb/usbdi_util.h>
57 #define DPRINTF(x) if (usbdebug) kprintf x
58 #define DPRINTFN(n,x) if (usbdebug>(n)) kprintf x
66 usbd_get_desc(usbd_device_handle dev
, int type
, int index
, int len
, void *desc
)
68 usb_device_request_t req
;
70 DPRINTFN(3,("usbd_get_desc: type=%d, index=%d, len=%d\n",
73 req
.bmRequestType
= UT_READ_DEVICE
;
74 req
.bRequest
= UR_GET_DESCRIPTOR
;
75 USETW2(req
.wValue
, type
, index
);
77 USETW(req
.wLength
, len
);
78 return (usbd_do_request(dev
, &req
, desc
));
82 usbd_get_config_desc(usbd_device_handle dev
, int confidx
,
83 usb_config_descriptor_t
*d
)
87 DPRINTFN(3,("usbd_get_config_desc: confidx=%d\n", confidx
));
88 err
= usbd_get_desc(dev
, UDESC_CONFIG
, confidx
,
89 USB_CONFIG_DESCRIPTOR_SIZE
, d
);
92 if (d
->bDescriptorType
!= UDESC_CONFIG
) {
93 DPRINTFN(-1,("usbd_get_config_desc: confidx=%d, bad desc "
95 confidx
, d
->bLength
, d
->bDescriptorType
));
98 return (USBD_NORMAL_COMPLETION
);
102 usbd_get_config_desc_full(usbd_device_handle dev
, int conf
, void *d
, int size
)
104 DPRINTFN(3,("usbd_get_config_desc_full: conf=%d\n", conf
));
105 return (usbd_get_desc(dev
, UDESC_CONFIG
, conf
, size
, d
));
109 usbd_get_device_desc(usbd_device_handle dev
, usb_device_descriptor_t
*d
)
111 DPRINTFN(3,("usbd_get_device_desc:\n"));
112 return (usbd_get_desc(dev
, UDESC_DEVICE
,
113 0, USB_DEVICE_DESCRIPTOR_SIZE
, d
));
117 usbd_get_device_status(usbd_device_handle dev
, usb_status_t
*st
)
119 usb_device_request_t req
;
121 req
.bmRequestType
= UT_READ_DEVICE
;
122 req
.bRequest
= UR_GET_STATUS
;
123 USETW(req
.wValue
, 0);
124 USETW(req
.wIndex
, 0);
125 USETW(req
.wLength
, sizeof(usb_status_t
));
126 return (usbd_do_request(dev
, &req
, st
));
130 usbd_get_hub_status(usbd_device_handle dev
, usb_hub_status_t
*st
)
132 usb_device_request_t req
;
134 req
.bmRequestType
= UT_READ_CLASS_DEVICE
;
135 req
.bRequest
= UR_GET_STATUS
;
136 USETW(req
.wValue
, 0);
137 USETW(req
.wIndex
, 0);
138 USETW(req
.wLength
, sizeof(usb_hub_status_t
));
139 return (usbd_do_request(dev
, &req
, st
));
143 usbd_set_address(usbd_device_handle dev
, int addr
)
145 usb_device_request_t req
;
147 req
.bmRequestType
= UT_WRITE_DEVICE
;
148 req
.bRequest
= UR_SET_ADDRESS
;
149 USETW(req
.wValue
, addr
);
150 USETW(req
.wIndex
, 0);
151 USETW(req
.wLength
, 0);
152 return usbd_do_request(dev
, &req
, 0);
156 usbd_get_port_status(usbd_device_handle dev
, int port
, usb_port_status_t
*ps
)
158 usb_device_request_t req
;
160 req
.bmRequestType
= UT_READ_CLASS_OTHER
;
161 req
.bRequest
= UR_GET_STATUS
;
162 USETW(req
.wValue
, 0);
163 USETW(req
.wIndex
, port
);
164 USETW(req
.wLength
, sizeof *ps
);
165 return (usbd_do_request(dev
, &req
, ps
));
169 usbd_clear_hub_feature(usbd_device_handle dev
, int sel
)
171 usb_device_request_t req
;
173 req
.bmRequestType
= UT_WRITE_CLASS_DEVICE
;
174 req
.bRequest
= UR_CLEAR_FEATURE
;
175 USETW(req
.wValue
, sel
);
176 USETW(req
.wIndex
, 0);
177 USETW(req
.wLength
, 0);
178 return (usbd_do_request(dev
, &req
, 0));
182 usbd_set_hub_feature(usbd_device_handle dev
, int sel
)
184 usb_device_request_t req
;
186 req
.bmRequestType
= UT_WRITE_CLASS_DEVICE
;
187 req
.bRequest
= UR_SET_FEATURE
;
188 USETW(req
.wValue
, sel
);
189 USETW(req
.wIndex
, 0);
190 USETW(req
.wLength
, 0);
191 return (usbd_do_request(dev
, &req
, 0));
195 usbd_clear_port_feature(usbd_device_handle dev
, int port
, int sel
)
197 usb_device_request_t req
;
199 req
.bmRequestType
= UT_WRITE_CLASS_OTHER
;
200 req
.bRequest
= UR_CLEAR_FEATURE
;
201 USETW(req
.wValue
, sel
);
202 USETW(req
.wIndex
, port
);
203 USETW(req
.wLength
, 0);
204 return (usbd_do_request(dev
, &req
, 0));
208 usbd_set_port_feature(usbd_device_handle dev
, int port
, int sel
)
210 usb_device_request_t req
;
212 req
.bmRequestType
= UT_WRITE_CLASS_OTHER
;
213 req
.bRequest
= UR_SET_FEATURE
;
214 USETW(req
.wValue
, sel
);
215 USETW(req
.wIndex
, port
);
216 USETW(req
.wLength
, 0);
217 return (usbd_do_request(dev
, &req
, 0));
221 usbd_get_protocol(usbd_interface_handle iface
, u_int8_t
*report
)
223 usb_interface_descriptor_t
*id
= usbd_get_interface_descriptor(iface
);
224 usbd_device_handle dev
;
225 usb_device_request_t req
;
227 DPRINTFN(4, ("usbd_get_protocol: iface=%p, endpt=%d\n",
228 iface
, id
->bInterfaceNumber
));
230 return (USBD_IOERROR
);
231 usbd_interface2device_handle(iface
, &dev
);
232 req
.bmRequestType
= UT_READ_CLASS_INTERFACE
;
233 req
.bRequest
= UR_GET_PROTOCOL
;
234 USETW(req
.wValue
, 0);
235 USETW(req
.wIndex
, id
->bInterfaceNumber
);
236 USETW(req
.wLength
, 1);
237 return (usbd_do_request(dev
, &req
, report
));
241 usbd_set_protocol(usbd_interface_handle iface
, int report
)
243 usb_interface_descriptor_t
*id
= usbd_get_interface_descriptor(iface
);
244 usbd_device_handle dev
;
245 usb_device_request_t req
;
247 DPRINTFN(4, ("usbd_set_protocol: iface=%p, report=%d, endpt=%d\n",
248 iface
, report
, id
->bInterfaceNumber
));
250 return (USBD_IOERROR
);
251 usbd_interface2device_handle(iface
, &dev
);
252 req
.bmRequestType
= UT_WRITE_CLASS_INTERFACE
;
253 req
.bRequest
= UR_SET_PROTOCOL
;
254 USETW(req
.wValue
, report
);
255 USETW(req
.wIndex
, id
->bInterfaceNumber
);
256 USETW(req
.wLength
, 0);
257 return (usbd_do_request(dev
, &req
, 0));
261 usbd_set_report(usbd_interface_handle iface
, int type
, int id
, void *data
,
264 usb_interface_descriptor_t
*ifd
= usbd_get_interface_descriptor(iface
);
265 usbd_device_handle dev
;
266 usb_device_request_t req
;
268 DPRINTFN(4, ("usbd_set_report: len=%d\n", len
));
270 return (USBD_IOERROR
);
271 usbd_interface2device_handle(iface
, &dev
);
272 req
.bmRequestType
= UT_WRITE_CLASS_INTERFACE
;
273 req
.bRequest
= UR_SET_REPORT
;
274 USETW2(req
.wValue
, type
, id
);
275 USETW(req
.wIndex
, ifd
->bInterfaceNumber
);
276 USETW(req
.wLength
, len
);
277 return (usbd_do_request(dev
, &req
, data
));
281 usbd_set_report_async(usbd_interface_handle iface
, int type
, int id
, void *data
,
284 usb_interface_descriptor_t
*ifd
= usbd_get_interface_descriptor(iface
);
285 usbd_device_handle dev
;
286 usb_device_request_t req
;
288 DPRINTFN(4, ("usbd_set_report_async: len=%d\n", len
));
290 return (USBD_IOERROR
);
291 usbd_interface2device_handle(iface
, &dev
);
292 req
.bmRequestType
= UT_WRITE_CLASS_INTERFACE
;
293 req
.bRequest
= UR_SET_REPORT
;
294 USETW2(req
.wValue
, type
, id
);
295 USETW(req
.wIndex
, ifd
->bInterfaceNumber
);
296 USETW(req
.wLength
, len
);
297 return (usbd_do_request_async(dev
, &req
, data
));
301 usbd_get_report(usbd_interface_handle iface
, int type
, int id
, void *data
,
304 usb_interface_descriptor_t
*ifd
= usbd_get_interface_descriptor(iface
);
305 usbd_device_handle dev
;
306 usb_device_request_t req
;
308 DPRINTFN(4, ("usbd_get_report: len=%d\n", len
));
310 return (USBD_IOERROR
);
311 usbd_interface2device_handle(iface
, &dev
);
312 req
.bmRequestType
= UT_READ_CLASS_INTERFACE
;
313 req
.bRequest
= UR_GET_REPORT
;
314 USETW2(req
.wValue
, type
, id
);
315 USETW(req
.wIndex
, ifd
->bInterfaceNumber
);
316 USETW(req
.wLength
, len
);
317 return (usbd_do_request(dev
, &req
, data
));
321 usbd_set_idle(usbd_interface_handle iface
, int duration
, int id
)
323 usb_interface_descriptor_t
*ifd
= usbd_get_interface_descriptor(iface
);
324 usbd_device_handle dev
;
325 usb_device_request_t req
;
327 DPRINTFN(4, ("usbd_set_idle: %d %d\n", duration
, id
));
329 return (USBD_IOERROR
);
330 usbd_interface2device_handle(iface
, &dev
);
331 req
.bmRequestType
= UT_WRITE_CLASS_INTERFACE
;
332 req
.bRequest
= UR_SET_IDLE
;
333 USETW2(req
.wValue
, duration
, id
);
334 USETW(req
.wIndex
, ifd
->bInterfaceNumber
);
335 USETW(req
.wLength
, 0);
336 return (usbd_do_request(dev
, &req
, 0));
340 usbd_get_report_descriptor(usbd_device_handle dev
, int ifcno
,
343 usb_device_request_t req
;
345 req
.bmRequestType
= UT_READ_INTERFACE
;
346 req
.bRequest
= UR_GET_DESCRIPTOR
;
347 USETW2(req
.wValue
, UDESC_REPORT
, 0); /* report id should be 0 */
348 USETW(req
.wIndex
, ifcno
);
349 USETW(req
.wLength
, size
);
350 return (usbd_do_request(dev
, &req
, d
));
353 usb_hid_descriptor_t
*
354 usbd_get_hid_descriptor(usbd_interface_handle ifc
)
356 usb_interface_descriptor_t
*idesc
= usbd_get_interface_descriptor(ifc
);
357 usbd_device_handle dev
;
358 usb_config_descriptor_t
*cdesc
;
359 usb_hid_descriptor_t
*hd
;
364 usbd_interface2device_handle(ifc
, &dev
);
365 cdesc
= usbd_get_config_descriptor(dev
);
367 p
= (char *)idesc
+ idesc
->bLength
;
368 end
= (char *)cdesc
+ UGETW(cdesc
->wTotalLength
);
370 for (; p
< end
; p
+= hd
->bLength
) {
371 hd
= (usb_hid_descriptor_t
*)p
;
372 if (p
+ hd
->bLength
<= end
&& hd
->bDescriptorType
== UDESC_HID
)
374 if (hd
->bDescriptorType
== UDESC_INTERFACE
)
381 usbd_read_report_desc(usbd_interface_handle ifc
, void **descp
, int *sizep
,
384 usb_interface_descriptor_t
*id
;
385 usb_hid_descriptor_t
*hid
;
386 usbd_device_handle dev
;
389 usbd_interface2device_handle(ifc
, &dev
);
390 id
= usbd_get_interface_descriptor(ifc
);
393 hid
= usbd_get_hid_descriptor(ifc
);
395 return (USBD_IOERROR
);
396 *sizep
= UGETW(hid
->descrs
[0].wDescriptorLength
);
397 *descp
= kmalloc(*sizep
, mem
, M_INTWAIT
);
398 err
= usbd_get_report_descriptor(dev
, id
->bInterfaceNumber
,
405 return (USBD_NORMAL_COMPLETION
);
409 usbd_get_config(usbd_device_handle dev
, u_int8_t
*conf
)
411 usb_device_request_t req
;
413 req
.bmRequestType
= UT_READ_DEVICE
;
414 req
.bRequest
= UR_GET_CONFIG
;
415 USETW(req
.wValue
, 0);
416 USETW(req
.wIndex
, 0);
417 USETW(req
.wLength
, 1);
418 return (usbd_do_request(dev
, &req
, conf
));
421 static void usbd_bulk_transfer_cb(usbd_xfer_handle xfer
,
422 usbd_private_handle priv
, usbd_status status
);
424 usbd_bulk_transfer_cb(usbd_xfer_handle xfer
, usbd_private_handle priv
,
431 usbd_bulk_transfer(usbd_xfer_handle xfer
, usbd_pipe_handle pipe
,
432 u_int16_t flags
, u_int32_t timeout
, void *buf
,
433 u_int32_t
*size
, char *lbl
)
438 usbd_setup_xfer(xfer
, pipe
, 0, buf
, *size
,
439 flags
, timeout
, usbd_bulk_transfer_cb
);
440 DPRINTFN(1, ("usbd_bulk_transfer: start transfer %d bytes\n", *size
));
442 err
= usbd_transfer(xfer
);
443 if (err
!= USBD_IN_PROGRESS
) {
447 error
= tsleep((caddr_t
)xfer
, PCATCH
, lbl
, 0);
450 DPRINTF(("usbd_bulk_transfer: tsleep=%d\n", error
));
451 usbd_abort_pipe(pipe
);
452 return (USBD_INTERRUPTED
);
454 usbd_get_xfer_status(xfer
, NULL
, NULL
, size
, &err
);
455 DPRINTFN(1,("usbd_bulk_transfer: transferred %d\n", *size
));
457 DPRINTF(("usbd_bulk_transfer: error=%d\n", err
));
458 usbd_clear_endpoint_stall(pipe
);
463 static void usbd_intr_transfer_cb(usbd_xfer_handle xfer
,
464 usbd_private_handle priv
, usbd_status status
);
466 usbd_intr_transfer_cb(usbd_xfer_handle xfer
, usbd_private_handle priv
,
473 usbd_intr_transfer(usbd_xfer_handle xfer
, usbd_pipe_handle pipe
,
474 u_int16_t flags
, u_int32_t timeout
, void *buf
,
475 u_int32_t
*size
, char *lbl
)
480 usbd_setup_xfer(xfer
, pipe
, 0, buf
, *size
,
481 flags
, timeout
, usbd_intr_transfer_cb
);
482 DPRINTFN(1, ("usbd_intr_transfer: start transfer %d bytes\n", *size
));
483 crit_enter(); /* don't want callback until tsleep() */
484 err
= usbd_transfer(xfer
);
485 if (err
!= USBD_IN_PROGRESS
) {
489 error
= tsleep(xfer
, PCATCH
, lbl
, 0);
492 DPRINTF(("usbd_intr_transfer: tsleep=%d\n", error
));
493 usbd_abort_pipe(pipe
);
494 return (USBD_INTERRUPTED
);
496 usbd_get_xfer_status(xfer
, NULL
, NULL
, size
, &err
);
497 DPRINTFN(1,("usbd_intr_transfer: transferred %d\n", *size
));
499 DPRINTF(("usbd_intr_transfer: error=%d\n", err
));
500 usbd_clear_endpoint_stall(pipe
);
506 usb_detach_wait(device_t dv
)
508 DPRINTF(("usb_detach_wait: waiting for %s\n", device_get_nameunit(dv
)));
509 if (tsleep(dv
, 0, "usbdet", hz
* 60))
510 kprintf("usb_detach_wait: %s didn't detach\n",
511 device_get_nameunit(dv
));
512 DPRINTF(("usb_detach_wait: %s done\n", device_get_nameunit(dv
)));
516 usb_detach_wakeup(device_t dv
)
518 DPRINTF(("usb_detach_wakeup: for %s\n", device_get_nameunit(dv
)));
522 const usb_descriptor_t
*
523 usb_find_desc(usbd_device_handle dev
, int type
, int subtype
)
525 usbd_desc_iter_t iter
;
526 const usb_descriptor_t
*desc
;
528 usb_desc_iter_init(dev
, &iter
);
530 desc
= usb_desc_iter_next(&iter
);
531 if (!desc
|| (desc
->bDescriptorType
== type
&&
532 (subtype
== USBD_SUBTYPE_ANY
||
533 subtype
== desc
->bDescriptorSubtype
)))