Home | History | Annotate | Line # | Download | only in usb
usbdi_util.c revision 1.63.2.11
      1 /*	$NetBSD: usbdi_util.c,v 1.63.2.11 2015/09/29 11:38:29 skrll Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998, 2012 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Lennart Augustsson (lennart (at) augustsson.net) at
      9  * Carlstedt Research & Technology and Matthew R. Green (mrg (at) eterna.com.au).
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: usbdi_util.c,v 1.63.2.11 2015/09/29 11:38:29 skrll Exp $");
     35 
     36 #ifdef _KERNEL_OPT
     37 #include "opt_usb.h"
     38 #endif
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/kernel.h>
     43 #include <sys/kmem.h>
     44 #include <sys/proc.h>
     45 #include <sys/device.h>
     46 #include <sys/bus.h>
     47 
     48 #include <dev/usb/usb.h>
     49 #include <dev/usb/usbhid.h>
     50 #include <dev/usb/usbdi.h>
     51 #include <dev/usb/usbdivar.h>
     52 #include <dev/usb/usbdi_util.h>
     53 #include <dev/usb/usbhist.h>
     54 
     55 #define	DPRINTFN(N,FMT,A,B,C,D)	USBHIST_LOGN(usbdebug,N,FMT,A,B,C,D)
     56 
     57 usbd_status
     58 usbd_get_desc(struct usbd_device *dev, int type, int index, int len, void *desc)
     59 {
     60 	usb_device_request_t req;
     61 
     62 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
     63 
     64 	DPRINTFN(3,"type=%d, index=%d, len=%d", type, index, len, 0);
     65 
     66 	req.bmRequestType = UT_READ_DEVICE;
     67 	req.bRequest = UR_GET_DESCRIPTOR;
     68 	USETW2(req.wValue, type, index);
     69 	USETW(req.wIndex, 0);
     70 	USETW(req.wLength, len);
     71 	return usbd_do_request(dev, &req, desc);
     72 }
     73 
     74 usbd_status
     75 usbd_get_config_desc(struct usbd_device *dev, int confidx,
     76 		     usb_config_descriptor_t *d)
     77 {
     78 	usbd_status err;
     79 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
     80 
     81 	DPRINTFN(3, "confidx=%d", confidx, 0, 0, 0);
     82 	err = usbd_get_desc(dev, UDESC_CONFIG, confidx,
     83 			    USB_CONFIG_DESCRIPTOR_SIZE, d);
     84 	if (err)
     85 		return err;
     86 	if (d->bDescriptorType != UDESC_CONFIG) {
     87 		DPRINTFN(1, "confidx=%d, bad desc len=%d type=%d",
     88 		    confidx, d->bLength, d->bDescriptorType, 0);
     89 		return USBD_INVAL;
     90 	}
     91 	return USBD_NORMAL_COMPLETION;
     92 }
     93 
     94 usbd_status
     95 usbd_get_config_desc_full(struct usbd_device *dev, int conf, void *d, int size)
     96 {
     97 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
     98 
     99 	DPRINTFN(3, "conf=%d", conf, 0, 0, 0);
    100 	return usbd_get_desc(dev, UDESC_CONFIG, conf, size, d);
    101 }
    102 
    103 usbd_status
    104 usbd_get_bos_desc(struct usbd_device *dev, int confidx,
    105 		     usb_bos_descriptor_t *d)
    106 {
    107 	usbd_status err;
    108 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    109 
    110 	DPRINTFN(3, "confidx=%d", confidx, 0, 0, 0);
    111 	err = usbd_get_desc(dev, UDESC_BOS, confidx,
    112 			    USB_BOS_DESCRIPTOR_SIZE, d);
    113 	if (err)
    114 		return (err);
    115 	if (d->bDescriptorType != UDESC_BOS) {
    116 		DPRINTFN(1, "confidx=%d, bad desc len=%d type=%d",
    117 		    confidx, d->bLength, d->bDescriptorType, 0);
    118 		return USBD_INVAL;
    119 	}
    120 	return USBD_NORMAL_COMPLETION;
    121 }
    122 
    123 usbd_status
    124 usbd_get_bos_desc_full(struct usbd_device *dev, int conf, void *d, int size)
    125 {
    126 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    127 
    128 	DPRINTFN(3, "conf=%d", conf, 0, 0, 0);
    129 	return usbd_get_desc(dev, UDESC_BOS, conf, size, d);
    130 }
    131 
    132 usbd_status
    133 usbd_get_device_desc(struct usbd_device *dev, usb_device_descriptor_t *d)
    134 {
    135 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    136 
    137 	return (usbd_get_desc(dev, UDESC_DEVICE,
    138 			     0, USB_DEVICE_DESCRIPTOR_SIZE, d));
    139 }
    140 
    141 usbd_status
    142 usbd_get_device_status(struct usbd_device *dev, usb_status_t *st)
    143 {
    144 	usb_device_request_t req;
    145 
    146 	req.bmRequestType = UT_READ_DEVICE;
    147 	req.bRequest = UR_GET_STATUS;
    148 	USETW(req.wValue, 0);
    149 	USETW(req.wIndex, 0);
    150 	USETW(req.wLength, sizeof(usb_status_t));
    151 	return usbd_do_request(dev, &req, st);
    152 }
    153 
    154 usbd_status
    155 usbd_get_hub_status(struct usbd_device *dev, usb_hub_status_t *st)
    156 {
    157 	usb_device_request_t req;
    158 
    159 	req.bmRequestType = UT_READ_CLASS_DEVICE;
    160 	req.bRequest = UR_GET_STATUS;
    161 	USETW(req.wValue, 0);
    162 	USETW(req.wIndex, 0);
    163 	USETW(req.wLength, sizeof(usb_hub_status_t));
    164 	return usbd_do_request(dev, &req, st);
    165 }
    166 
    167 usbd_status
    168 usbd_set_address(struct usbd_device *dev, int addr)
    169 {
    170 	usb_device_request_t req;
    171 
    172 	req.bmRequestType = UT_WRITE_DEVICE;
    173 	req.bRequest = UR_SET_ADDRESS;
    174 	USETW(req.wValue, addr);
    175 	USETW(req.wIndex, 0);
    176 	USETW(req.wLength, 0);
    177 	return usbd_do_request(dev, &req, 0);
    178 }
    179 
    180 usbd_status
    181 usbd_get_port_status(struct usbd_device *dev, int port, usb_port_status_t *ps)
    182 {
    183 	usb_device_request_t req;
    184 
    185 	req.bmRequestType = UT_READ_CLASS_OTHER;
    186 	req.bRequest = UR_GET_STATUS;
    187 	USETW(req.wValue, 0);
    188 	USETW(req.wIndex, port);
    189 	USETW(req.wLength, sizeof(*ps));
    190 	return usbd_do_request(dev, &req, ps);
    191 }
    192 
    193 /* USB 3.1 10.16.2.6, 10.16.2.6.3 */
    194 usbd_status
    195 usbd_get_port_status_ext(struct usbd_device *dev, int port,
    196     usb_port_status_ext_t *pse)
    197 {
    198 	usb_device_request_t req;
    199 
    200 	req.bmRequestType = UT_READ_CLASS_OTHER;
    201 	req.bRequest = UR_GET_STATUS;
    202 	USETW2(req.wValue, 0, UR_PST_EXT_PORT_STATUS);
    203 	USETW(req.wIndex, port);
    204 	USETW(req.wLength, sizeof(*pse));
    205 	return (usbd_do_request(dev, &req, pse));
    206 }
    207 
    208 usbd_status
    209 usbd_clear_hub_feature(struct usbd_device *dev, int sel)
    210 {
    211 	usb_device_request_t req;
    212 
    213 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
    214 	req.bRequest = UR_CLEAR_FEATURE;
    215 	USETW(req.wValue, sel);
    216 	USETW(req.wIndex, 0);
    217 	USETW(req.wLength, 0);
    218 	return usbd_do_request(dev, &req, 0);
    219 }
    220 
    221 usbd_status
    222 usbd_set_hub_feature(struct usbd_device *dev, int sel)
    223 {
    224 	usb_device_request_t req;
    225 
    226 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
    227 	req.bRequest = UR_SET_FEATURE;
    228 	USETW(req.wValue, sel);
    229 	USETW(req.wIndex, 0);
    230 	USETW(req.wLength, 0);
    231 	return usbd_do_request(dev, &req, 0);
    232 }
    233 
    234 usbd_status
    235 usbd_clear_port_feature(struct usbd_device *dev, int port, int sel)
    236 {
    237 	usb_device_request_t req;
    238 
    239 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
    240 	req.bRequest = UR_CLEAR_FEATURE;
    241 	USETW(req.wValue, sel);
    242 	USETW(req.wIndex, port);
    243 	USETW(req.wLength, 0);
    244 	return usbd_do_request(dev, &req, 0);
    245 }
    246 
    247 usbd_status
    248 usbd_set_port_feature(struct usbd_device *dev, int port, int sel)
    249 {
    250 	usb_device_request_t req;
    251 
    252 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
    253 	req.bRequest = UR_SET_FEATURE;
    254 	USETW(req.wValue, sel);
    255 	USETW(req.wIndex, port);
    256 	USETW(req.wLength, 0);
    257 	return usbd_do_request(dev, &req, 0);
    258 }
    259 
    260 usbd_status
    261 usbd_set_port_u1_timeout(struct usbd_device *dev, int port, int timeout)
    262 {
    263 	usb_device_request_t req;
    264 
    265 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
    266 	req.bRequest = UR_SET_FEATURE;
    267 	USETW(req.wValue, UHF_PORT_U1_TIMEOUT);
    268 	USETW2(req.wIndex, timeout, port);
    269 	USETW(req.wLength, 0);
    270 	return usbd_do_request(dev, &req, 0);
    271 }
    272 
    273 usbd_status
    274 usbd_set_port_u2_timeout(struct usbd_device *dev, int port, int timeout)
    275 {
    276 	usb_device_request_t req;
    277 
    278 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
    279 	req.bRequest = UR_SET_FEATURE;
    280 	USETW(req.wValue, UHF_PORT_U2_TIMEOUT);
    281 	USETW2(req.wIndex, timeout, port);
    282 	USETW(req.wLength, 0);
    283 	return usbd_do_request(dev, &req, 0);
    284 }
    285 
    286 usbd_status
    287 usbd_get_protocol(struct usbd_interface *iface, uint8_t *report)
    288 {
    289 	usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
    290 	struct usbd_device *dev;
    291 	usb_device_request_t req;
    292 
    293 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    294 
    295 	DPRINTFN(4, "iface=%p, endpt=%d", iface, id->bInterfaceNumber, 0, 0);
    296 	if (id == NULL)
    297 		return USBD_IOERROR;
    298 	usbd_interface2device_handle(iface, &dev);
    299 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    300 	req.bRequest = UR_GET_PROTOCOL;
    301 	USETW(req.wValue, 0);
    302 	USETW(req.wIndex, id->bInterfaceNumber);
    303 	USETW(req.wLength, 1);
    304 	return usbd_do_request(dev, &req, report);
    305 }
    306 
    307 usbd_status
    308 usbd_set_protocol(struct usbd_interface *iface, int report)
    309 {
    310 	usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
    311 	struct usbd_device *dev;
    312 	usb_device_request_t req;
    313 
    314 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    315 
    316 	DPRINTFN(4, "iface=%p, report=%d, endpt=%d", iface, report,
    317 	     id->bInterfaceNumber, 0);
    318 	if (id == NULL)
    319 		return USBD_IOERROR;
    320 	usbd_interface2device_handle(iface, &dev);
    321 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    322 	req.bRequest = UR_SET_PROTOCOL;
    323 	USETW(req.wValue, report);
    324 	USETW(req.wIndex, id->bInterfaceNumber);
    325 	USETW(req.wLength, 0);
    326 	return usbd_do_request(dev, &req, 0);
    327 }
    328 
    329 usbd_status
    330 usbd_set_report(struct usbd_interface *iface, int type, int id, void *data,
    331 		int len)
    332 {
    333 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
    334 	struct usbd_device *dev;
    335 	usb_device_request_t req;
    336 
    337 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    338 
    339 	DPRINTFN(4, "len=%d", len, 0, 0, 0);
    340 	if (ifd == NULL)
    341 		return USBD_IOERROR;
    342 	usbd_interface2device_handle(iface, &dev);
    343 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    344 	req.bRequest = UR_SET_REPORT;
    345 	USETW2(req.wValue, type, id);
    346 	USETW(req.wIndex, ifd->bInterfaceNumber);
    347 	USETW(req.wLength, len);
    348 	return usbd_do_request(dev, &req, data);
    349 }
    350 
    351 usbd_status
    352 usbd_get_report(struct usbd_interface *iface, int type, int id, void *data,
    353 		int len)
    354 {
    355 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
    356 	struct usbd_device *dev;
    357 	usb_device_request_t req;
    358 
    359 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    360 
    361 	DPRINTFN(4, "len=%d", len, 0, 0, 0);
    362 	if (ifd == NULL)
    363 		return USBD_IOERROR;
    364 	usbd_interface2device_handle(iface, &dev);
    365 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    366 	req.bRequest = UR_GET_REPORT;
    367 	USETW2(req.wValue, type, id);
    368 	USETW(req.wIndex, ifd->bInterfaceNumber);
    369 	USETW(req.wLength, len);
    370 	return usbd_do_request(dev, &req, data);
    371 }
    372 
    373 usbd_status
    374 usbd_set_idle(struct usbd_interface *iface, int duration, int id)
    375 {
    376 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
    377 	struct usbd_device *dev;
    378 	usb_device_request_t req;
    379 
    380 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    381 
    382 	DPRINTFN(4, "duration %d id %d", duration, id, 0, 0);
    383 	if (ifd == NULL)
    384 		return USBD_IOERROR;
    385 	usbd_interface2device_handle(iface, &dev);
    386 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    387 	req.bRequest = UR_SET_IDLE;
    388 	USETW2(req.wValue, duration, id);
    389 	USETW(req.wIndex, ifd->bInterfaceNumber);
    390 	USETW(req.wLength, 0);
    391 	return usbd_do_request(dev, &req, 0);
    392 }
    393 
    394 usbd_status
    395 usbd_get_report_descriptor(struct usbd_device *dev, int ifcno,
    396 			   int size, void *d)
    397 {
    398 	usb_device_request_t req;
    399 
    400 	req.bmRequestType = UT_READ_INTERFACE;
    401 	req.bRequest = UR_GET_DESCRIPTOR;
    402 	USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
    403 	USETW(req.wIndex, ifcno);
    404 	USETW(req.wLength, size);
    405 	return usbd_do_request(dev, &req, d);
    406 }
    407 
    408 usb_hid_descriptor_t *
    409 usbd_get_hid_descriptor(struct usbd_interface *ifc)
    410 {
    411 	usb_interface_descriptor_t *idesc = usbd_get_interface_descriptor(ifc);
    412 	struct usbd_device *dev;
    413 	usb_config_descriptor_t *cdesc;
    414 	usb_hid_descriptor_t *hd;
    415 	char *p, *end;
    416 
    417 	if (idesc == NULL)
    418 		return NULL;
    419 	usbd_interface2device_handle(ifc, &dev);
    420 	cdesc = usbd_get_config_descriptor(dev);
    421 
    422 	p = (char *)idesc + idesc->bLength;
    423 	end = (char *)cdesc + UGETW(cdesc->wTotalLength);
    424 
    425 	for (; p < end; p += hd->bLength) {
    426 		hd = (usb_hid_descriptor_t *)p;
    427 		if (p + hd->bLength <= end && hd->bDescriptorType == UDESC_HID)
    428 			return hd;
    429 		if (hd->bDescriptorType == UDESC_INTERFACE)
    430 			break;
    431 	}
    432 	return NULL;
    433 }
    434 
    435 usbd_status
    436 usbd_read_report_desc(struct usbd_interface *ifc, void **descp, int *sizep)
    437 {
    438 	usb_interface_descriptor_t *id;
    439 	usb_hid_descriptor_t *hid;
    440 	struct usbd_device *dev;
    441 	usbd_status err;
    442 
    443 	usbd_interface2device_handle(ifc, &dev);
    444 	id = usbd_get_interface_descriptor(ifc);
    445 	if (id == NULL)
    446 		return USBD_INVAL;
    447 	hid = usbd_get_hid_descriptor(ifc);
    448 	if (hid == NULL)
    449 		return USBD_IOERROR;
    450 	*sizep = UGETW(hid->descrs[0].wDescriptorLength);
    451 	*descp = kmem_alloc(*sizep, KM_SLEEP);
    452 	if (*descp == NULL)
    453 		return USBD_NOMEM;
    454 	err = usbd_get_report_descriptor(dev, id->bInterfaceNumber,
    455 					 *sizep, *descp);
    456 	if (err) {
    457 		kmem_free(*descp, *sizep);
    458 		*descp = NULL;
    459 		return err;
    460 	}
    461 	return USBD_NORMAL_COMPLETION;
    462 }
    463 
    464 usbd_status
    465 usbd_get_config(struct usbd_device *dev, uint8_t *conf)
    466 {
    467 	usb_device_request_t req;
    468 
    469 	req.bmRequestType = UT_READ_DEVICE;
    470 	req.bRequest = UR_GET_CONFIG;
    471 	USETW(req.wValue, 0);
    472 	USETW(req.wIndex, 0);
    473 	USETW(req.wLength, 1);
    474 	return usbd_do_request(dev, &req, conf);
    475 }
    476 
    477 usbd_status
    478 usbd_bulk_transfer(struct usbd_xfer *xfer, struct usbd_pipe *pipe,
    479 		   uint16_t flags, uint32_t timeout, void *buf,
    480 		   uint32_t *size)
    481 {
    482 	usbd_status err;
    483 
    484 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    485 
    486 	usbd_setup_xfer(xfer, pipe, 0, buf, *size, flags, timeout, NULL);
    487 	DPRINTFN(1, "start transfer %d bytes", *size, 0, 0, 0);
    488 	err = usbd_sync_transfer_sig(xfer);
    489 
    490 	usbd_get_xfer_status(xfer, NULL, NULL, size, NULL);
    491 	DPRINTFN(1, "transferred %d", *size, 0, 0, 0);
    492 	if (err) {
    493 		usbd_clear_endpoint_stall(pipe);
    494 	}
    495 	USBHIST_LOG(usbdebug, "<- done xfer %p err %d", xfer, err, 0, 0);
    496 
    497 	return err;
    498 }
    499 
    500 usbd_status
    501 usbd_intr_transfer(struct usbd_xfer *xfer, struct usbd_pipe *pipe,
    502 		   uint16_t flags, uint32_t timeout, void *buf,
    503 		   uint32_t *size)
    504 {
    505 	usbd_status err;
    506 
    507 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    508 
    509 	usbd_setup_xfer(xfer, pipe, 0, buf, *size, flags, timeout, NULL);
    510 
    511 	DPRINTFN(1, "start transfer %d bytes", *size, 0, 0, 0);
    512 	err = usbd_sync_transfer_sig(xfer);
    513 
    514 	usbd_get_xfer_status(xfer, NULL, NULL, size, NULL);
    515 
    516 	DPRINTFN(1, "transferred %d", *size, 0, 0, 0);
    517 	if (err) {
    518 		usbd_clear_endpoint_stall(pipe);
    519 	}
    520 	USBHIST_LOG(usbdebug, "<- done xfer %p err %d", xfer, err, 0, 0);
    521 
    522 	return err;
    523 }
    524 
    525 void
    526 usb_detach_wait(device_t dv, kcondvar_t *cv, kmutex_t *lock)
    527 {
    528 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    529 
    530 	DPRINTFN(1, "waiting for dv %p", dv, 0, 0, 0);
    531 	if (cv_timedwait(cv, lock, hz * 60))	// dv, PZERO, "usbdet", hz * 60
    532 		printf("usb_detach_wait: %s didn't detach\n",
    533 			device_xname(dv));
    534 	DPRINTFN(1, "done", 0, 0, 0, 0);
    535 }
    536 
    537 void
    538 usb_detach_broadcast(device_t dv, kcondvar_t *cv)
    539 {
    540 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    541 
    542 	DPRINTFN(1, "for dv %p", dv, 0, 0, 0);
    543 	cv_broadcast(cv);
    544 }
    545 
    546 void
    547 usb_detach_waitold(device_t dv)
    548 {
    549 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    550 
    551 	DPRINTFN(1, "waiting for dv %p", dv, 0, 0, 0);
    552 	if (tsleep(dv, PZERO, "usbdet", hz * 60)) /* XXXSMP ok */
    553 		printf("usb_detach_waitold: %s didn't detach\n",
    554 			device_xname(dv));
    555 	DPRINTFN(1, "done", 0, 0, 0, 0);
    556 }
    557 
    558 void
    559 usb_detach_wakeupold(device_t dv)
    560 {
    561 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    562 
    563 	DPRINTFN(1, "for dv %p", dv, 0, 0, 0);
    564 	wakeup(dv); /* XXXSMP ok */
    565 }
    566 
    567 const usb_cdc_descriptor_t *
    568 usb_find_desc(struct usbd_device *dev, int type, int subtype)
    569 {
    570 	usbd_desc_iter_t iter;
    571 	const usb_cdc_descriptor_t *desc;
    572 
    573 	usb_desc_iter_init(dev, &iter);
    574 	for (;;) {
    575 		desc = (const usb_cdc_descriptor_t *)usb_desc_iter_next(&iter);
    576 		if (!desc || (desc->bDescriptorType == type &&
    577 			      (subtype == USBD_CDCSUBTYPE_ANY ||
    578 			       subtype == desc->bDescriptorSubtype)))
    579 			break;
    580 	}
    581 	return desc;
    582 }
    583 
    584 /* same as usb_find_desc(), but searches only in the specified interface. */
    585 const usb_cdc_descriptor_t *
    586 usb_find_desc_if(struct usbd_device *dev, int type, int subtype,
    587 		 usb_interface_descriptor_t *id)
    588 {
    589 	usbd_desc_iter_t iter;
    590 	const usb_cdc_descriptor_t *desc;
    591 
    592 	if (id == NULL)
    593 		return usb_find_desc(dev, type, subtype);
    594 
    595 	usb_desc_iter_init(dev, &iter);
    596 
    597 	iter.cur = (void *)id;		/* start from the interface desc */
    598 	usb_desc_iter_next(&iter);	/* and skip it */
    599 
    600 	while ((desc = (const usb_cdc_descriptor_t *)usb_desc_iter_next(&iter))
    601 	       != NULL) {
    602 		if (desc->bDescriptorType == UDESC_INTERFACE) {
    603 			/* we ran into the next interface --- not found */
    604 			return NULL;
    605 		}
    606 		if (desc->bDescriptorType == type &&
    607 		    (subtype == USBD_CDCSUBTYPE_ANY ||
    608 		     subtype == desc->bDescriptorSubtype))
    609 			break;
    610 	}
    611 	return desc;
    612 }
    613