Home | History | Annotate | Line # | Download | only in usb
usbdi_util.c revision 1.75.2.1
      1  1.75.2.1        ad /*	$NetBSD: usbdi_util.c,v 1.75.2.1 2020/02/29 20:19:17 ad Exp $	*/
      2       1.1  augustss 
      3       1.1  augustss /*
      4      1.57       mrg  * Copyright (c) 1998, 2012 The NetBSD Foundation, Inc.
      5       1.1  augustss  * All rights reserved.
      6       1.1  augustss  *
      7       1.5  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8      1.30  augustss  * by Lennart Augustsson (lennart (at) augustsson.net) at
      9      1.57       mrg  * Carlstedt Research & Technology and Matthew R. Green (mrg (at) eterna.com.au).
     10       1.1  augustss  *
     11       1.1  augustss  * Redistribution and use in source and binary forms, with or without
     12       1.1  augustss  * modification, are permitted provided that the following conditions
     13       1.1  augustss  * are met:
     14       1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     15       1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     16       1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     17       1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     18       1.1  augustss  *    documentation and/or other materials provided with the distribution.
     19       1.1  augustss  *
     20       1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21       1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22       1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23       1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24       1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25       1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26       1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27       1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28       1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29       1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30       1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     31       1.1  augustss  */
     32      1.36     lukem 
     33      1.36     lukem #include <sys/cdefs.h>
     34  1.75.2.1        ad __KERNEL_RCSID(0, "$NetBSD: usbdi_util.c,v 1.75.2.1 2020/02/29 20:19:17 ad Exp $");
     35      1.63     skrll 
     36      1.63     skrll #ifdef _KERNEL_OPT
     37      1.63     skrll #include "opt_usb.h"
     38      1.63     skrll #endif
     39       1.1  augustss 
     40       1.1  augustss #include <sys/param.h>
     41       1.1  augustss #include <sys/systm.h>
     42       1.1  augustss #include <sys/kernel.h>
     43      1.65     skrll #include <sys/kmem.h>
     44       1.1  augustss #include <sys/proc.h>
     45      1.14  augustss #include <sys/device.h>
     46      1.57       mrg #include <sys/bus.h>
     47       1.1  augustss 
     48       1.1  augustss #include <dev/usb/usb.h>
     49       1.1  augustss #include <dev/usb/usbhid.h>
     50       1.1  augustss #include <dev/usb/usbdi.h>
     51      1.57       mrg #include <dev/usb/usbdivar.h>
     52       1.1  augustss #include <dev/usb/usbdi_util.h>
     53      1.72      manu #include <dev/usb/usb_quirks.h>
     54      1.63     skrll #include <dev/usb/usbhist.h>
     55       1.1  augustss 
     56      1.65     skrll #define	DPRINTF(FMT,A,B,C,D)	USBHIST_LOGN(usbdebug,1,FMT,A,B,C,D)
     57      1.65     skrll #define	DPRINTFN(N,FMT,A,B,C,D)	USBHIST_LOGN(usbdebug,N,FMT,A,B,C,D)
     58       1.1  augustss 
     59       1.1  augustss usbd_status
     60      1.65     skrll usbd_get_desc(struct usbd_device *dev, int type, int index, int len, void *desc)
     61       1.1  augustss {
     62       1.1  augustss 	usb_device_request_t req;
     63      1.72      manu 	usbd_status err;
     64       1.1  augustss 
     65      1.75       mrg 	USBHIST_FUNC();
     66      1.75       mrg 	USBHIST_CALLARGS(usbdebug, "type=%jd, index=%jd, len=%jd",
     67      1.75       mrg 	    type, index, len, 0);
     68      1.26  augustss 
     69      1.72      manu 	/*
     70      1.72      manu 	 * Provide hard-coded configuration descriptors
     71      1.72      manu 	 * for devices that may corrupt it. This cannot
     72      1.72      manu 	 * be done for device descriptors which are used
     73      1.72      manu 	 * to identify the device.
     74      1.72      manu 	 */
     75      1.72      manu 	if (type != UDESC_DEVICE &&
     76      1.72      manu 	    dev->ud_quirks->uq_flags & UQ_DESC_CORRUPT) {
     77      1.72      manu 		err = usbd_get_desc_fake(dev, type, index, len, desc);
     78      1.72      manu 		goto out;
     79      1.72      manu 	}
     80      1.72      manu 
     81       1.1  augustss 	req.bmRequestType = UT_READ_DEVICE;
     82       1.1  augustss 	req.bRequest = UR_GET_DESCRIPTOR;
     83       1.1  augustss 	USETW2(req.wValue, type, index);
     84       1.1  augustss 	USETW(req.wIndex, 0);
     85       1.1  augustss 	USETW(req.wLength, len);
     86      1.72      manu 	err = usbd_do_request(dev, &req, desc);
     87      1.72      manu 
     88      1.72      manu out:
     89      1.72      manu 	return err;
     90       1.1  augustss }
     91       1.1  augustss 
     92       1.1  augustss usbd_status
     93      1.65     skrll usbd_get_config_desc(struct usbd_device *dev, int confidx,
     94      1.31  augustss 		     usb_config_descriptor_t *d)
     95       1.1  augustss {
     96      1.75       mrg 	USBHIST_FUNC();
     97      1.75       mrg 	USBHIST_CALLARGS(usbdebug, "confidx=%jd", confidx, 0, 0, 0);
     98      1.23  augustss 	usbd_status err;
     99       1.9  augustss 
    100      1.40  augustss 	err = usbd_get_desc(dev, UDESC_CONFIG, confidx,
    101      1.26  augustss 			    USB_CONFIG_DESCRIPTOR_SIZE, d);
    102      1.23  augustss 	if (err)
    103      1.65     skrll 		return err;
    104       1.9  augustss 	if (d->bDescriptorType != UDESC_CONFIG) {
    105      1.70  pgoyette 		DPRINTFN(1, "confidx=%jd, bad desc len=%d type=%d",
    106      1.65     skrll 		    confidx, d->bLength, d->bDescriptorType, 0);
    107      1.65     skrll 		return USBD_INVAL;
    108       1.9  augustss 	}
    109      1.65     skrll 	return USBD_NORMAL_COMPLETION;
    110       1.1  augustss }
    111       1.1  augustss 
    112       1.1  augustss usbd_status
    113      1.65     skrll usbd_get_config_desc_full(struct usbd_device *dev, int conf, void *d, int size)
    114       1.4  augustss {
    115      1.75       mrg 	USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug, "conf=%jd", conf, 0, 0, 0);
    116      1.65     skrll 
    117      1.65     skrll 	return usbd_get_desc(dev, UDESC_CONFIG, conf, size, d);
    118       1.4  augustss }
    119       1.4  augustss 
    120       1.4  augustss usbd_status
    121      1.65     skrll usbd_get_bos_desc(struct usbd_device *dev, int confidx,
    122      1.64     skrll 		     usb_bos_descriptor_t *d)
    123      1.64     skrll {
    124      1.75       mrg 	USBHIST_FUNC();
    125      1.75       mrg 	USBHIST_CALLARGS(usbdebug, "confidx=%jd", confidx, 0, 0, 0);
    126      1.64     skrll 	usbd_status err;
    127      1.64     skrll 
    128      1.64     skrll 	err = usbd_get_desc(dev, UDESC_BOS, confidx,
    129      1.64     skrll 			    USB_BOS_DESCRIPTOR_SIZE, d);
    130      1.64     skrll 	if (err)
    131      1.69     skrll 		return err;
    132      1.64     skrll 	if (d->bDescriptorType != UDESC_BOS) {
    133      1.70  pgoyette 		DPRINTFN(1, "confidx=%jd, bad desc len=%d type=%d",
    134      1.65     skrll 		    confidx, d->bLength, d->bDescriptorType, 0);
    135      1.65     skrll 		return USBD_INVAL;
    136      1.64     skrll 	}
    137      1.65     skrll 	return USBD_NORMAL_COMPLETION;
    138      1.64     skrll }
    139      1.64     skrll 
    140      1.64     skrll usbd_status
    141      1.65     skrll usbd_get_bos_desc_full(struct usbd_device *dev, int conf, void *d, int size)
    142      1.64     skrll {
    143      1.75       mrg 	USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug, "conf=%jd", conf, 0, 0, 0);
    144      1.65     skrll 
    145      1.65     skrll 	return usbd_get_desc(dev, UDESC_BOS, conf, size, d);
    146      1.64     skrll }
    147      1.64     skrll 
    148      1.64     skrll usbd_status
    149      1.65     skrll usbd_get_device_desc(struct usbd_device *dev, usb_device_descriptor_t *d)
    150       1.1  augustss {
    151      1.65     skrll 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    152      1.65     skrll 
    153      1.69     skrll 	return usbd_get_desc(dev, UDESC_DEVICE,
    154      1.69     skrll 			     0, USB_DEVICE_DESCRIPTOR_SIZE, d);
    155       1.1  augustss }
    156       1.1  augustss 
    157  1.75.2.1        ad /*
    158  1.75.2.1        ad  * Get the first 8 bytes of the device descriptor.
    159  1.75.2.1        ad  * Do as Windows does: try to read 64 bytes -- there are devices which
    160  1.75.2.1        ad  * recognize the initial descriptor fetch (before the control endpoint's
    161  1.75.2.1        ad  * MaxPacketSize is known by the host) by exactly this length.
    162  1.75.2.1        ad  */
    163  1.75.2.1        ad usbd_status
    164  1.75.2.1        ad usbd_get_initial_ddesc(struct usbd_device *dev, usb_device_descriptor_t *desc)
    165  1.75.2.1        ad {
    166  1.75.2.1        ad 	USBHIST_FUNC();
    167  1.75.2.1        ad 	USBHIST_CALLARGS(usbdebug, "dev %#jx", (uintptr_t)dev, 0, 0, 0);
    168  1.75.2.1        ad 	usb_device_request_t req;
    169  1.75.2.1        ad 	char buf[64];
    170  1.75.2.1        ad 	int res, actlen;
    171  1.75.2.1        ad 
    172  1.75.2.1        ad 	req.bmRequestType = UT_READ_DEVICE;
    173  1.75.2.1        ad 	req.bRequest = UR_GET_DESCRIPTOR;
    174  1.75.2.1        ad 	USETW2(req.wValue, UDESC_DEVICE, 0);
    175  1.75.2.1        ad 	USETW(req.wIndex, 0);
    176  1.75.2.1        ad 	USETW(req.wLength, 8);
    177  1.75.2.1        ad 	res = usbd_do_request_flags(dev, &req, buf, USBD_SHORT_XFER_OK,
    178  1.75.2.1        ad 		&actlen, USBD_DEFAULT_TIMEOUT);
    179  1.75.2.1        ad 	if (res)
    180  1.75.2.1        ad 		return res;
    181  1.75.2.1        ad 	if (actlen < 8)
    182  1.75.2.1        ad 		return USBD_SHORT_XFER;
    183  1.75.2.1        ad 	memcpy(desc, buf, 8);
    184  1.75.2.1        ad 	return USBD_NORMAL_COMPLETION;
    185  1.75.2.1        ad }
    186  1.75.2.1        ad 
    187  1.75.2.1        ad usbd_status
    188  1.75.2.1        ad usbd_get_string_desc(struct usbd_device *dev, int sindex, int langid,
    189  1.75.2.1        ad     usb_string_descriptor_t *sdesc, int *sizep)
    190  1.75.2.1        ad {
    191  1.75.2.1        ad 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    192  1.75.2.1        ad 	usb_device_request_t req;
    193  1.75.2.1        ad 	usbd_status err;
    194  1.75.2.1        ad 	int actlen;
    195  1.75.2.1        ad 
    196  1.75.2.1        ad 	/*
    197  1.75.2.1        ad 	 * Pass a full-sized buffer to usbd_do_request_len().  At least
    198  1.75.2.1        ad 	 * one device has been seen returning additional data beyond the
    199  1.75.2.1        ad 	 * provided buffers (2-bytes written shortly after the request
    200  1.75.2.1        ad 	 * claims to have completed and returned the 2 byte header,
    201  1.75.2.1        ad 	 * corrupting other memory.)
    202  1.75.2.1        ad 	 */
    203  1.75.2.1        ad 	req.bmRequestType = UT_READ_DEVICE;
    204  1.75.2.1        ad 	req.bRequest = UR_GET_DESCRIPTOR;
    205  1.75.2.1        ad 	USETW2(req.wValue, UDESC_STRING, sindex);
    206  1.75.2.1        ad 	USETW(req.wIndex, langid);
    207  1.75.2.1        ad 	USETW(req.wLength, 2);	/* only size byte first */
    208  1.75.2.1        ad 	err = usbd_do_request_len(dev, &req, sizeof(*sdesc), sdesc,
    209  1.75.2.1        ad 	    USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
    210  1.75.2.1        ad 	if (err)
    211  1.75.2.1        ad 		return err;
    212  1.75.2.1        ad 
    213  1.75.2.1        ad 	if (actlen < 2)
    214  1.75.2.1        ad 		return USBD_SHORT_XFER;
    215  1.75.2.1        ad 
    216  1.75.2.1        ad 	if (sdesc->bLength > sizeof(*sdesc))
    217  1.75.2.1        ad 		return USBD_INVAL;
    218  1.75.2.1        ad 	USETW(req.wLength, sdesc->bLength);	/* the whole string */
    219  1.75.2.1        ad 	err = usbd_do_request_len(dev, &req, sizeof(*sdesc), sdesc,
    220  1.75.2.1        ad 	    USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
    221  1.75.2.1        ad 	if (err)
    222  1.75.2.1        ad 		return err;
    223  1.75.2.1        ad 
    224  1.75.2.1        ad 	if (actlen != sdesc->bLength) {
    225  1.75.2.1        ad 		DPRINTF("expected %jd, got %jd", sdesc->bLength, actlen, 0, 0);
    226  1.75.2.1        ad 	}
    227  1.75.2.1        ad 
    228  1.75.2.1        ad 	*sizep = actlen;
    229  1.75.2.1        ad 	return USBD_NORMAL_COMPLETION;
    230  1.75.2.1        ad }
    231  1.75.2.1        ad 
    232  1.75.2.1        ad /* -------------------------------------------------------------------------- */
    233  1.75.2.1        ad 
    234       1.1  augustss usbd_status
    235      1.65     skrll usbd_get_device_status(struct usbd_device *dev, usb_status_t *st)
    236       1.1  augustss {
    237      1.65     skrll 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    238       1.1  augustss 	usb_device_request_t req;
    239       1.1  augustss 
    240       1.1  augustss 	req.bmRequestType = UT_READ_DEVICE;
    241       1.1  augustss 	req.bRequest = UR_GET_STATUS;
    242       1.1  augustss 	USETW(req.wValue, 0);
    243       1.1  augustss 	USETW(req.wIndex, 0);
    244       1.1  augustss 	USETW(req.wLength, sizeof(usb_status_t));
    245      1.65     skrll 	return usbd_do_request(dev, &req, st);
    246      1.40  augustss }
    247       1.1  augustss 
    248       1.1  augustss usbd_status
    249      1.65     skrll usbd_get_hub_status(struct usbd_device *dev, usb_hub_status_t *st)
    250       1.1  augustss {
    251      1.75       mrg 	USBHIST_FUNC();
    252      1.75       mrg 	USBHIST_CALLARGS(usbdebug, "dev %#jx", (uintptr_t)dev, 0, 0, 0);
    253       1.1  augustss 	usb_device_request_t req;
    254       1.1  augustss 
    255       1.1  augustss 	req.bmRequestType = UT_READ_CLASS_DEVICE;
    256       1.1  augustss 	req.bRequest = UR_GET_STATUS;
    257       1.1  augustss 	USETW(req.wValue, 0);
    258       1.1  augustss 	USETW(req.wIndex, 0);
    259       1.1  augustss 	USETW(req.wLength, sizeof(usb_hub_status_t));
    260      1.65     skrll 	return usbd_do_request(dev, &req, st);
    261      1.40  augustss }
    262       1.1  augustss 
    263       1.1  augustss usbd_status
    264      1.65     skrll usbd_get_port_status(struct usbd_device *dev, int port, usb_port_status_t *ps)
    265       1.1  augustss {
    266      1.75       mrg 	USBHIST_FUNC();
    267      1.75       mrg 	USBHIST_CALLARGS(usbdebug, "dev %#jx port %jd",
    268      1.75       mrg 	    (uintptr_t)dev, port, 0, 0);
    269       1.1  augustss 	usb_device_request_t req;
    270       1.1  augustss 
    271       1.1  augustss 	req.bmRequestType = UT_READ_CLASS_OTHER;
    272       1.1  augustss 	req.bRequest = UR_GET_STATUS;
    273       1.1  augustss 	USETW(req.wValue, 0);
    274       1.1  augustss 	USETW(req.wIndex, port);
    275      1.65     skrll 	USETW(req.wLength, sizeof(*ps));
    276      1.65     skrll 	return usbd_do_request(dev, &req, ps);
    277      1.65     skrll }
    278      1.65     skrll 
    279      1.65     skrll /* USB 3.1 10.16.2.6, 10.16.2.6.3 */
    280      1.65     skrll usbd_status
    281      1.65     skrll usbd_get_port_status_ext(struct usbd_device *dev, int port,
    282      1.65     skrll     usb_port_status_ext_t *pse)
    283      1.65     skrll {
    284      1.75       mrg 	USBHIST_FUNC();
    285      1.75       mrg 	USBHIST_CALLARGS(usbdebug, "dev %#jx port %jd",
    286      1.75       mrg 	    (uintptr_t)dev, port, 0, 0);
    287      1.65     skrll 	usb_device_request_t req;
    288      1.65     skrll 
    289      1.65     skrll 	req.bmRequestType = UT_READ_CLASS_OTHER;
    290      1.65     skrll 	req.bRequest = UR_GET_STATUS;
    291      1.65     skrll 	USETW2(req.wValue, 0, UR_PST_EXT_PORT_STATUS);
    292      1.65     skrll 	USETW(req.wIndex, port);
    293      1.65     skrll 	USETW(req.wLength, sizeof(*pse));
    294      1.69     skrll 	return usbd_do_request(dev, &req, pse);
    295      1.16  augustss }
    296      1.16  augustss 
    297  1.75.2.1        ad /* -------------------------------------------------------------------------- */
    298  1.75.2.1        ad 
    299      1.16  augustss usbd_status
    300      1.65     skrll usbd_clear_hub_feature(struct usbd_device *dev, int sel)
    301      1.16  augustss {
    302      1.75       mrg 	USBHIST_FUNC();
    303      1.75       mrg 	USBHIST_CALLARGS(usbdebug, "dev %#jx sel %jd",
    304      1.75       mrg 	    (uintptr_t)dev, sel, 0, 0);
    305      1.16  augustss 	usb_device_request_t req;
    306      1.16  augustss 
    307      1.16  augustss 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
    308      1.16  augustss 	req.bRequest = UR_CLEAR_FEATURE;
    309      1.16  augustss 	USETW(req.wValue, sel);
    310      1.16  augustss 	USETW(req.wIndex, 0);
    311      1.16  augustss 	USETW(req.wLength, 0);
    312      1.65     skrll 	return usbd_do_request(dev, &req, 0);
    313      1.16  augustss }
    314      1.16  augustss 
    315      1.16  augustss usbd_status
    316      1.65     skrll usbd_set_hub_feature(struct usbd_device *dev, int sel)
    317      1.16  augustss {
    318      1.75       mrg 	USBHIST_FUNC();
    319      1.75       mrg 	USBHIST_CALLARGS(usbdebug,
    320      1.75       mrg 	    "dev %#jx sel %jd", (uintptr_t)dev, sel, 0, 0);
    321      1.16  augustss 	usb_device_request_t req;
    322      1.16  augustss 
    323      1.16  augustss 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
    324      1.16  augustss 	req.bRequest = UR_SET_FEATURE;
    325      1.16  augustss 	USETW(req.wValue, sel);
    326      1.16  augustss 	USETW(req.wIndex, 0);
    327      1.16  augustss 	USETW(req.wLength, 0);
    328      1.65     skrll 	return usbd_do_request(dev, &req, 0);
    329       1.1  augustss }
    330       1.1  augustss 
    331       1.1  augustss usbd_status
    332      1.65     skrll usbd_clear_port_feature(struct usbd_device *dev, int port, int sel)
    333       1.1  augustss {
    334      1.75       mrg 	USBHIST_FUNC();
    335      1.75       mrg 	USBHIST_CALLARGS(usbdebug, "dev %#jx port %jd sel %jd",
    336      1.75       mrg 	    (uintptr_t)dev, port, sel, 0);
    337       1.1  augustss 	usb_device_request_t req;
    338       1.1  augustss 
    339       1.1  augustss 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
    340       1.1  augustss 	req.bRequest = UR_CLEAR_FEATURE;
    341       1.1  augustss 	USETW(req.wValue, sel);
    342       1.1  augustss 	USETW(req.wIndex, port);
    343       1.1  augustss 	USETW(req.wLength, 0);
    344      1.65     skrll 	return usbd_do_request(dev, &req, 0);
    345       1.1  augustss }
    346       1.1  augustss 
    347       1.1  augustss usbd_status
    348      1.65     skrll usbd_set_port_feature(struct usbd_device *dev, int port, int sel)
    349       1.1  augustss {
    350      1.75       mrg 	USBHIST_FUNC();
    351      1.75       mrg 	USBHIST_CALLARGS(usbdebug, "dev %#jx port %jd sel %.d",
    352      1.75       mrg 	    (uintptr_t)dev, sel, 0, 0);
    353       1.1  augustss 	usb_device_request_t req;
    354       1.1  augustss 
    355       1.1  augustss 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
    356       1.1  augustss 	req.bRequest = UR_SET_FEATURE;
    357       1.1  augustss 	USETW(req.wValue, sel);
    358       1.1  augustss 	USETW(req.wIndex, port);
    359       1.1  augustss 	USETW(req.wLength, 0);
    360      1.65     skrll 	return usbd_do_request(dev, &req, 0);
    361       1.1  augustss }
    362       1.1  augustss 
    363      1.39  augustss usbd_status
    364      1.65     skrll usbd_set_port_u1_timeout(struct usbd_device *dev, int port, int timeout)
    365      1.65     skrll {
    366      1.75       mrg 	USBHIST_FUNC();
    367      1.75       mrg 	USBHIST_CALLARGS(usbdebug, "dev %#jx port %jd timeout %.d",
    368      1.75       mrg 	    (uintptr_t)dev, port, timeout, 0);
    369      1.65     skrll 	usb_device_request_t req;
    370      1.65     skrll 
    371      1.65     skrll 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
    372      1.65     skrll 	req.bRequest = UR_SET_FEATURE;
    373      1.65     skrll 	USETW(req.wValue, UHF_PORT_U1_TIMEOUT);
    374      1.65     skrll 	USETW2(req.wIndex, timeout, port);
    375      1.65     skrll 	USETW(req.wLength, 0);
    376      1.65     skrll 	return usbd_do_request(dev, &req, 0);
    377      1.65     skrll }
    378      1.65     skrll 
    379      1.65     skrll usbd_status
    380      1.65     skrll usbd_set_port_u2_timeout(struct usbd_device *dev, int port, int timeout)
    381      1.65     skrll {
    382      1.75       mrg 	USBHIST_FUNC();
    383      1.75       mrg 	USBHIST_CALLARGS(usbdebug, "dev %#jx port %jd timeout %jd",
    384      1.75       mrg 	    (uintptr_t)dev, port, timeout, 0);
    385      1.65     skrll 	usb_device_request_t req;
    386      1.65     skrll 
    387      1.65     skrll 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
    388      1.65     skrll 	req.bRequest = UR_SET_FEATURE;
    389      1.65     skrll 	USETW(req.wValue, UHF_PORT_U2_TIMEOUT);
    390      1.65     skrll 	USETW2(req.wIndex, timeout, port);
    391      1.65     skrll 	USETW(req.wLength, 0);
    392      1.65     skrll 	return usbd_do_request(dev, &req, 0);
    393      1.65     skrll }
    394      1.65     skrll 
    395      1.65     skrll usbd_status
    396  1.75.2.1        ad usbd_clear_endpoint_feature(struct usbd_device *dev, int epaddr, int sel)
    397  1.75.2.1        ad {
    398  1.75.2.1        ad 	USBHIST_FUNC();
    399  1.75.2.1        ad 	USBHIST_CALLARGS(usbdebug, "dev %#jx epaddr %jd sel %jd",
    400  1.75.2.1        ad 	    (uintptr_t)dev, epaddr, sel, 0);
    401  1.75.2.1        ad 	usb_device_request_t req;
    402  1.75.2.1        ad 
    403  1.75.2.1        ad 	req.bmRequestType = UT_WRITE_ENDPOINT;
    404  1.75.2.1        ad 	req.bRequest = UR_CLEAR_FEATURE;
    405  1.75.2.1        ad 	USETW(req.wValue, sel);
    406  1.75.2.1        ad 	USETW(req.wIndex, epaddr);
    407  1.75.2.1        ad 	USETW(req.wLength, 0);
    408  1.75.2.1        ad 	return usbd_do_request(dev, &req, 0);
    409  1.75.2.1        ad }
    410  1.75.2.1        ad 
    411  1.75.2.1        ad /* -------------------------------------------------------------------------- */
    412  1.75.2.1        ad 
    413  1.75.2.1        ad usbd_status
    414  1.75.2.1        ad usbd_get_config(struct usbd_device *dev, uint8_t *conf)
    415  1.75.2.1        ad {
    416  1.75.2.1        ad 	USBHIST_FUNC();
    417  1.75.2.1        ad 	USBHIST_CALLARGS(usbdebug, "dev %#jx", (uintptr_t)dev, 0, 0, 0);
    418  1.75.2.1        ad 	usb_device_request_t req;
    419  1.75.2.1        ad 
    420  1.75.2.1        ad 	req.bmRequestType = UT_READ_DEVICE;
    421  1.75.2.1        ad 	req.bRequest = UR_GET_CONFIG;
    422  1.75.2.1        ad 	USETW(req.wValue, 0);
    423  1.75.2.1        ad 	USETW(req.wIndex, 0);
    424  1.75.2.1        ad 	USETW(req.wLength, 1);
    425  1.75.2.1        ad 	return usbd_do_request(dev, &req, conf);
    426  1.75.2.1        ad }
    427  1.75.2.1        ad 
    428  1.75.2.1        ad usbd_status
    429  1.75.2.1        ad usbd_set_config(struct usbd_device *dev, int conf)
    430  1.75.2.1        ad {
    431  1.75.2.1        ad 	USBHIST_FUNC();
    432  1.75.2.1        ad 	USBHIST_CALLARGS(usbdebug, "dev %#jx conf %jd",
    433  1.75.2.1        ad 	    (uintptr_t)dev, conf, 0, 0);
    434  1.75.2.1        ad 	usb_device_request_t req;
    435  1.75.2.1        ad 
    436  1.75.2.1        ad 	req.bmRequestType = UT_WRITE_DEVICE;
    437  1.75.2.1        ad 	req.bRequest = UR_SET_CONFIG;
    438  1.75.2.1        ad 	USETW(req.wValue, conf);
    439  1.75.2.1        ad 	USETW(req.wIndex, 0);
    440  1.75.2.1        ad 	USETW(req.wLength, 0);
    441  1.75.2.1        ad 	return usbd_do_request(dev, &req, 0);
    442  1.75.2.1        ad }
    443  1.75.2.1        ad 
    444  1.75.2.1        ad usbd_status
    445  1.75.2.1        ad usbd_set_address(struct usbd_device *dev, int addr)
    446  1.75.2.1        ad {
    447  1.75.2.1        ad 	USBHIST_FUNC();
    448  1.75.2.1        ad 	USBHIST_CALLARGS(usbdebug, "dev %#jx addr %jd",
    449  1.75.2.1        ad 	    (uintptr_t)dev, addr, 0, 0);
    450  1.75.2.1        ad 	usb_device_request_t req;
    451  1.75.2.1        ad 
    452  1.75.2.1        ad 	req.bmRequestType = UT_WRITE_DEVICE;
    453  1.75.2.1        ad 	req.bRequest = UR_SET_ADDRESS;
    454  1.75.2.1        ad 	USETW(req.wValue, addr);
    455  1.75.2.1        ad 	USETW(req.wIndex, 0);
    456  1.75.2.1        ad 	USETW(req.wLength, 0);
    457  1.75.2.1        ad 	return usbd_do_request(dev, &req, 0);
    458  1.75.2.1        ad }
    459  1.75.2.1        ad 
    460  1.75.2.1        ad usbd_status
    461  1.75.2.1        ad usbd_set_idle(struct usbd_interface *iface, int duration, int id)
    462  1.75.2.1        ad {
    463  1.75.2.1        ad 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
    464  1.75.2.1        ad 	struct usbd_device *dev;
    465  1.75.2.1        ad 	usb_device_request_t req;
    466  1.75.2.1        ad 
    467  1.75.2.1        ad 	USBHIST_FUNC();
    468  1.75.2.1        ad 	USBHIST_CALLARGS(usbdebug, "duration %jd id %jd", duration, id, 0, 0);
    469  1.75.2.1        ad 
    470  1.75.2.1        ad 	if (ifd == NULL)
    471  1.75.2.1        ad 		return USBD_IOERROR;
    472  1.75.2.1        ad 	usbd_interface2device_handle(iface, &dev);
    473  1.75.2.1        ad 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    474  1.75.2.1        ad 	req.bRequest = UR_SET_IDLE;
    475  1.75.2.1        ad 	USETW2(req.wValue, duration, id);
    476  1.75.2.1        ad 	USETW(req.wIndex, ifd->bInterfaceNumber);
    477  1.75.2.1        ad 	USETW(req.wLength, 0);
    478  1.75.2.1        ad 	return usbd_do_request(dev, &req, 0);
    479  1.75.2.1        ad }
    480  1.75.2.1        ad 
    481  1.75.2.1        ad /* -------------------------------------------------------------------------- */
    482  1.75.2.1        ad 
    483  1.75.2.1        ad usbd_status
    484      1.65     skrll usbd_get_protocol(struct usbd_interface *iface, uint8_t *report)
    485      1.39  augustss {
    486      1.39  augustss 	usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
    487      1.65     skrll 	struct usbd_device *dev;
    488      1.39  augustss 	usb_device_request_t req;
    489      1.39  augustss 
    490      1.75       mrg 	USBHIST_FUNC();
    491      1.75       mrg 	USBHIST_CALLARGS(usbdebug, "iface=%#jx, endpt=%jd",
    492      1.75       mrg 	    (uintptr_t)iface, id->bInterfaceNumber, 0, 0);
    493      1.65     skrll 
    494      1.39  augustss 	if (id == NULL)
    495      1.65     skrll 		return USBD_IOERROR;
    496      1.68      maya 
    497      1.39  augustss 	usbd_interface2device_handle(iface, &dev);
    498      1.39  augustss 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    499      1.39  augustss 	req.bRequest = UR_GET_PROTOCOL;
    500      1.39  augustss 	USETW(req.wValue, 0);
    501      1.39  augustss 	USETW(req.wIndex, id->bInterfaceNumber);
    502      1.39  augustss 	USETW(req.wLength, 1);
    503      1.65     skrll 	return usbd_do_request(dev, &req, report);
    504      1.39  augustss }
    505       1.1  augustss 
    506       1.1  augustss usbd_status
    507      1.65     skrll usbd_set_protocol(struct usbd_interface *iface, int report)
    508       1.1  augustss {
    509       1.1  augustss 	usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
    510      1.65     skrll 	struct usbd_device *dev;
    511       1.1  augustss 	usb_device_request_t req;
    512       1.1  augustss 
    513      1.75       mrg 	USBHIST_FUNC();
    514      1.75       mrg 	USBHIST_CALLARGS(usbdebug, "iface=%#jx, report=%jd, endpt=%jd",
    515      1.75       mrg 	    (uintptr_t)iface, report, id->bInterfaceNumber, 0);
    516      1.65     skrll 
    517      1.67      maya 	if (id == NULL)
    518      1.67      maya 		return USBD_IOERROR;
    519      1.67      maya 
    520      1.37  augustss 	usbd_interface2device_handle(iface, &dev);
    521       1.1  augustss 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    522       1.1  augustss 	req.bRequest = UR_SET_PROTOCOL;
    523       1.1  augustss 	USETW(req.wValue, report);
    524       1.1  augustss 	USETW(req.wIndex, id->bInterfaceNumber);
    525       1.1  augustss 	USETW(req.wLength, 0);
    526      1.65     skrll 	return usbd_do_request(dev, &req, 0);
    527       1.1  augustss }
    528       1.1  augustss 
    529  1.75.2.1        ad /* -------------------------------------------------------------------------- */
    530  1.75.2.1        ad 
    531       1.1  augustss usbd_status
    532      1.65     skrll usbd_set_report(struct usbd_interface *iface, int type, int id, void *data,
    533      1.31  augustss 		int len)
    534       1.1  augustss {
    535       1.1  augustss 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
    536      1.65     skrll 	struct usbd_device *dev;
    537       1.1  augustss 	usb_device_request_t req;
    538       1.1  augustss 
    539      1.75       mrg 	USBHIST_FUNC();
    540      1.75       mrg 	USBHIST_CALLARGS(usbdebug, "len=%jd", len, 0, 0, 0);
    541      1.65     skrll 
    542      1.23  augustss 	if (ifd == NULL)
    543      1.65     skrll 		return USBD_IOERROR;
    544      1.37  augustss 	usbd_interface2device_handle(iface, &dev);
    545       1.1  augustss 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    546       1.1  augustss 	req.bRequest = UR_SET_REPORT;
    547       1.2  augustss 	USETW2(req.wValue, type, id);
    548       1.2  augustss 	USETW(req.wIndex, ifd->bInterfaceNumber);
    549       1.2  augustss 	USETW(req.wLength, len);
    550      1.65     skrll 	return usbd_do_request(dev, &req, data);
    551       1.3  augustss }
    552       1.3  augustss 
    553       1.3  augustss usbd_status
    554      1.65     skrll usbd_get_report(struct usbd_interface *iface, int type, int id, void *data,
    555      1.31  augustss 		int len)
    556       1.2  augustss {
    557       1.2  augustss 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
    558      1.65     skrll 	struct usbd_device *dev;
    559       1.2  augustss 	usb_device_request_t req;
    560       1.2  augustss 
    561      1.75       mrg 	USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug, "len=%jd", len, 0, 0, 0);
    562      1.65     skrll 
    563      1.28  augustss 	if (ifd == NULL)
    564      1.65     skrll 		return USBD_IOERROR;
    565      1.37  augustss 	usbd_interface2device_handle(iface, &dev);
    566       1.2  augustss 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    567       1.2  augustss 	req.bRequest = UR_GET_REPORT;
    568       1.1  augustss 	USETW2(req.wValue, type, id);
    569       1.1  augustss 	USETW(req.wIndex, ifd->bInterfaceNumber);
    570       1.1  augustss 	USETW(req.wLength, len);
    571      1.65     skrll 	return usbd_do_request(dev, &req, data);
    572       1.1  augustss }
    573       1.1  augustss 
    574       1.1  augustss usbd_status
    575      1.65     skrll usbd_get_report_descriptor(struct usbd_device *dev, int ifcno,
    576      1.31  augustss 			   int size, void *d)
    577       1.1  augustss {
    578      1.75       mrg 	USBHIST_FUNC();
    579      1.75       mrg 	USBHIST_CALLARGS(usbdebug, "dev %#jx ifcno %jd size %jd",
    580      1.75       mrg 	    (uintptr_t)dev, ifcno, size, 0);
    581       1.1  augustss 	usb_device_request_t req;
    582       1.1  augustss 
    583       1.1  augustss 	req.bmRequestType = UT_READ_INTERFACE;
    584       1.1  augustss 	req.bRequest = UR_GET_DESCRIPTOR;
    585      1.35  augustss 	USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
    586       1.6  augustss 	USETW(req.wIndex, ifcno);
    587       1.1  augustss 	USETW(req.wLength, size);
    588      1.65     skrll 	return usbd_do_request(dev, &req, d);
    589       1.1  augustss }
    590       1.1  augustss 
    591  1.75.2.1        ad /* -------------------------------------------------------------------------- */
    592  1.75.2.1        ad 
    593       1.1  augustss usb_hid_descriptor_t *
    594      1.65     skrll usbd_get_hid_descriptor(struct usbd_interface *ifc)
    595       1.1  augustss {
    596       1.1  augustss 	usb_interface_descriptor_t *idesc = usbd_get_interface_descriptor(ifc);
    597      1.65     skrll 	struct usbd_device *dev;
    598       1.1  augustss 	usb_config_descriptor_t *cdesc;
    599       1.1  augustss 	usb_hid_descriptor_t *hd;
    600       1.1  augustss 	char *p, *end;
    601       1.1  augustss 
    602      1.23  augustss 	if (idesc == NULL)
    603      1.65     skrll 		return NULL;
    604      1.41  augustss 	usbd_interface2device_handle(ifc, &dev);
    605       1.1  augustss 	cdesc = usbd_get_config_descriptor(dev);
    606       1.1  augustss 
    607       1.1  augustss 	p = (char *)idesc + idesc->bLength;
    608       1.1  augustss 	end = (char *)cdesc + UGETW(cdesc->wTotalLength);
    609       1.1  augustss 
    610       1.1  augustss 	for (; p < end; p += hd->bLength) {
    611       1.1  augustss 		hd = (usb_hid_descriptor_t *)p;
    612      1.74      maxv 		if (p + hd->bLength <= end &&
    613      1.74      maxv 		    hd->bLength >= USB_HID_DESCRIPTOR_SIZE(0) &&
    614      1.74      maxv 		    hd->bDescriptorType == UDESC_HID)
    615      1.65     skrll 			return hd;
    616       1.1  augustss 		if (hd->bDescriptorType == UDESC_INTERFACE)
    617       1.1  augustss 			break;
    618       1.1  augustss 	}
    619      1.65     skrll 	return NULL;
    620       1.1  augustss }
    621       1.1  augustss 
    622       1.1  augustss usbd_status
    623      1.65     skrll usbd_read_report_desc(struct usbd_interface *ifc, void **descp, int *sizep)
    624       1.1  augustss {
    625       1.6  augustss 	usb_interface_descriptor_t *id;
    626       1.1  augustss 	usb_hid_descriptor_t *hid;
    627      1.65     skrll 	struct usbd_device *dev;
    628      1.23  augustss 	usbd_status err;
    629       1.1  augustss 
    630      1.37  augustss 	usbd_interface2device_handle(ifc, &dev);
    631       1.6  augustss 	id = usbd_get_interface_descriptor(ifc);
    632      1.23  augustss 	if (id == NULL)
    633      1.65     skrll 		return USBD_INVAL;
    634       1.1  augustss 	hid = usbd_get_hid_descriptor(ifc);
    635      1.25  augustss 	if (hid == NULL)
    636      1.65     skrll 		return USBD_IOERROR;
    637       1.1  augustss 	*sizep = UGETW(hid->descrs[0].wDescriptorLength);
    638      1.74      maxv 	if (*sizep == 0)
    639      1.74      maxv 		return USBD_INVAL;
    640      1.65     skrll 	*descp = kmem_alloc(*sizep, KM_SLEEP);
    641      1.35  augustss 	err = usbd_get_report_descriptor(dev, id->bInterfaceNumber,
    642      1.35  augustss 					 *sizep, *descp);
    643      1.23  augustss 	if (err) {
    644      1.65     skrll 		kmem_free(*descp, *sizep);
    645      1.34  augustss 		*descp = NULL;
    646      1.65     skrll 		return err;
    647       1.1  augustss 	}
    648      1.65     skrll 	return USBD_NORMAL_COMPLETION;
    649       1.7  augustss }
    650       1.7  augustss 
    651      1.40  augustss usbd_status
    652      1.65     skrll usbd_bulk_transfer(struct usbd_xfer *xfer, struct usbd_pipe *pipe,
    653      1.65     skrll     uint16_t flags, uint32_t timeout, void *buf, uint32_t *size)
    654      1.10  augustss {
    655      1.23  augustss 	usbd_status err;
    656      1.10  augustss 
    657      1.75       mrg 	USBHIST_FUNC();
    658      1.75       mrg 	USBHIST_CALLARGS(usbdebug, "start transfer %jd bytes", *size, 0, 0, 0);
    659      1.63     skrll 
    660      1.65     skrll 	usbd_setup_xfer(xfer, 0, buf, *size, flags, timeout, NULL);
    661      1.61     skrll 	err = usbd_sync_transfer_sig(xfer);
    662      1.57       mrg 
    663      1.57       mrg 	usbd_get_xfer_status(xfer, NULL, NULL, size, NULL);
    664      1.70  pgoyette 	DPRINTFN(1, "transferred %jd", *size, 0, 0, 0);
    665      1.23  augustss 	if (err) {
    666      1.10  augustss 		usbd_clear_endpoint_stall(pipe);
    667      1.10  augustss 	}
    668      1.70  pgoyette 	USBHIST_LOG(usbdebug, "<- done xfer %#jx err %d", (uintptr_t)xfer,
    669      1.70  pgoyette 	    err, 0, 0);
    670      1.61     skrll 
    671      1.65     skrll 	return err;
    672      1.10  augustss }
    673      1.10  augustss 
    674      1.42  augustss usbd_status
    675      1.65     skrll usbd_intr_transfer(struct usbd_xfer *xfer, struct usbd_pipe *pipe,
    676      1.65     skrll     uint16_t flags, uint32_t timeout, void *buf, uint32_t *size)
    677      1.42  augustss {
    678      1.42  augustss 	usbd_status err;
    679      1.42  augustss 
    680      1.75       mrg 	USBHIST_FUNC();
    681      1.75       mrg 	USBHIST_CALLARGS(usbdebug, "start transfer %jd bytes", *size, 0, 0, 0);
    682      1.63     skrll 
    683      1.65     skrll 	usbd_setup_xfer(xfer, 0, buf, *size, flags, timeout, NULL);
    684      1.61     skrll 
    685      1.57       mrg 	err = usbd_sync_transfer_sig(xfer);
    686      1.61     skrll 
    687      1.57       mrg 	usbd_get_xfer_status(xfer, NULL, NULL, size, NULL);
    688      1.61     skrll 
    689      1.70  pgoyette 	DPRINTFN(1, "transferred %jd", *size, 0, 0, 0);
    690      1.42  augustss 	if (err) {
    691      1.42  augustss 		usbd_clear_endpoint_stall(pipe);
    692      1.42  augustss 	}
    693      1.70  pgoyette 	USBHIST_LOG(usbdebug, "<- done xfer %#jx err %jd", (uintptr_t)xfer,
    694      1.70  pgoyette 	    err, 0, 0);
    695      1.63     skrll 
    696      1.65     skrll 	return err;
    697      1.42  augustss }
    698      1.42  augustss 
    699      1.14  augustss void
    700      1.56       mrg usb_detach_waitold(device_t dv)
    701      1.14  augustss {
    702      1.75       mrg 	USBHIST_FUNC();
    703      1.75       mrg 	USBHIST_CALLARGS(usbdebug, "waiting for dv %#jx",
    704      1.75       mrg 	    (uintptr_t)dv, 0, 0, 0);
    705      1.65     skrll 
    706      1.57       mrg 	if (tsleep(dv, PZERO, "usbdet", hz * 60)) /* XXXSMP ok */
    707      1.71   msaitoh 		aprint_error_dev(dv, "usb_detach_waitold: didn't detach\n");
    708      1.65     skrll 	DPRINTFN(1, "done", 0, 0, 0, 0);
    709      1.40  augustss }
    710      1.14  augustss 
    711      1.14  augustss void
    712      1.56       mrg usb_detach_wakeupold(device_t dv)
    713      1.14  augustss {
    714      1.75       mrg 	USBHIST_FUNC();
    715      1.75       mrg 	USBHIST_CALLARGS(usbdebug, "for dv %#jx", (uintptr_t)dv, 0, 0, 0);
    716      1.65     skrll 
    717      1.57       mrg 	wakeup(dv); /* XXXSMP ok */
    718      1.40  augustss }
    719      1.38  augustss 
    720  1.75.2.1        ad /* -------------------------------------------------------------------------- */
    721  1.75.2.1        ad 
    722  1.75.2.1        ad void
    723  1.75.2.1        ad usb_desc_iter_init(struct usbd_device *dev, usbd_desc_iter_t *iter)
    724  1.75.2.1        ad {
    725  1.75.2.1        ad 	const usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
    726  1.75.2.1        ad 
    727  1.75.2.1        ad 	iter->cur = (const uByte *)cd;
    728  1.75.2.1        ad 	iter->end = (const uByte *)cd + UGETW(cd->wTotalLength);
    729  1.75.2.1        ad }
    730  1.75.2.1        ad 
    731  1.75.2.1        ad const usb_descriptor_t *
    732  1.75.2.1        ad usb_desc_iter_peek(usbd_desc_iter_t *iter)
    733  1.75.2.1        ad {
    734  1.75.2.1        ad 	const usb_descriptor_t *desc;
    735  1.75.2.1        ad 
    736  1.75.2.1        ad 	if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) {
    737  1.75.2.1        ad 		if (iter->cur != iter->end)
    738  1.75.2.1        ad 			printf("%s: bad descriptor\n", __func__);
    739  1.75.2.1        ad 		return NULL;
    740  1.75.2.1        ad 	}
    741  1.75.2.1        ad 	desc = (const usb_descriptor_t *)iter->cur;
    742  1.75.2.1        ad 	if (desc->bLength < USB_DESCRIPTOR_SIZE) {
    743  1.75.2.1        ad 		printf("%s: descriptor length too small\n", __func__);
    744  1.75.2.1        ad 		return NULL;
    745  1.75.2.1        ad 	}
    746  1.75.2.1        ad 	if (iter->cur + desc->bLength > iter->end) {
    747  1.75.2.1        ad 		printf("%s: descriptor length too large\n", __func__);
    748  1.75.2.1        ad 		return NULL;
    749  1.75.2.1        ad 	}
    750  1.75.2.1        ad 	return desc;
    751  1.75.2.1        ad }
    752  1.75.2.1        ad 
    753  1.75.2.1        ad const usb_descriptor_t *
    754  1.75.2.1        ad usb_desc_iter_next(usbd_desc_iter_t *iter)
    755  1.75.2.1        ad {
    756  1.75.2.1        ad 	const usb_descriptor_t *desc = usb_desc_iter_peek(iter);
    757  1.75.2.1        ad 	if (desc == NULL)
    758  1.75.2.1        ad 		return NULL;
    759  1.75.2.1        ad 	iter->cur += desc->bLength;
    760  1.75.2.1        ad 	return desc;
    761  1.75.2.1        ad }
    762  1.75.2.1        ad 
    763  1.75.2.1        ad /* Return the next interface descriptor, skipping over any other
    764  1.75.2.1        ad  * descriptors.  Returns NULL at the end or on error. */
    765  1.75.2.1        ad const usb_interface_descriptor_t *
    766  1.75.2.1        ad usb_desc_iter_next_interface(usbd_desc_iter_t *iter)
    767  1.75.2.1        ad {
    768  1.75.2.1        ad 	const usb_descriptor_t *desc;
    769  1.75.2.1        ad 
    770  1.75.2.1        ad 	while ((desc = usb_desc_iter_peek(iter)) != NULL &&
    771  1.75.2.1        ad 	       desc->bDescriptorType != UDESC_INTERFACE)
    772  1.75.2.1        ad 	{
    773  1.75.2.1        ad 		usb_desc_iter_next(iter);
    774  1.75.2.1        ad 	}
    775  1.75.2.1        ad 
    776  1.75.2.1        ad 	return (const usb_interface_descriptor_t *)usb_desc_iter_next(iter);
    777  1.75.2.1        ad }
    778  1.75.2.1        ad 
    779  1.75.2.1        ad /* Returns the next non-interface descriptor, returning NULL when the
    780  1.75.2.1        ad  * next descriptor would be an interface descriptor. */
    781  1.75.2.1        ad const usb_descriptor_t *
    782  1.75.2.1        ad usb_desc_iter_next_non_interface(usbd_desc_iter_t *iter)
    783  1.75.2.1        ad {
    784  1.75.2.1        ad 	const usb_descriptor_t *desc;
    785  1.75.2.1        ad 
    786  1.75.2.1        ad 	if ((desc = usb_desc_iter_peek(iter)) != NULL &&
    787  1.75.2.1        ad 	    desc->bDescriptorType != UDESC_INTERFACE)
    788  1.75.2.1        ad 	{
    789  1.75.2.1        ad 		return usb_desc_iter_next(iter);
    790  1.75.2.1        ad 	} else {
    791  1.75.2.1        ad 		return NULL;
    792  1.75.2.1        ad 	}
    793  1.75.2.1        ad }
    794  1.75.2.1        ad 
    795      1.48  drochner const usb_cdc_descriptor_t *
    796      1.65     skrll usb_find_desc(struct usbd_device *dev, int type, int subtype)
    797      1.38  augustss {
    798      1.41  augustss 	usbd_desc_iter_t iter;
    799      1.48  drochner 	const usb_cdc_descriptor_t *desc;
    800      1.41  augustss 
    801      1.41  augustss 	usb_desc_iter_init(dev, &iter);
    802      1.41  augustss 	for (;;) {
    803      1.48  drochner 		desc = (const usb_cdc_descriptor_t *)usb_desc_iter_next(&iter);
    804      1.41  augustss 		if (!desc || (desc->bDescriptorType == type &&
    805      1.48  drochner 			      (subtype == USBD_CDCSUBTYPE_ANY ||
    806      1.41  augustss 			       subtype == desc->bDescriptorSubtype)))
    807      1.41  augustss 			break;
    808      1.38  augustss 	}
    809      1.41  augustss 	return desc;
    810      1.38  augustss }
    811      1.43     itohy 
    812      1.43     itohy /* same as usb_find_desc(), but searches only in the specified interface. */
    813      1.48  drochner const usb_cdc_descriptor_t *
    814      1.65     skrll usb_find_desc_if(struct usbd_device *dev, int type, int subtype,
    815      1.43     itohy 		 usb_interface_descriptor_t *id)
    816      1.43     itohy {
    817      1.43     itohy 	usbd_desc_iter_t iter;
    818      1.48  drochner 	const usb_cdc_descriptor_t *desc;
    819      1.43     itohy 
    820      1.54  christos 	if (id == NULL)
    821      1.54  christos 		return usb_find_desc(dev, type, subtype);
    822      1.54  christos 
    823      1.43     itohy 	usb_desc_iter_init(dev, &iter);
    824      1.43     itohy 
    825      1.43     itohy 	iter.cur = (void *)id;		/* start from the interface desc */
    826      1.43     itohy 	usb_desc_iter_next(&iter);	/* and skip it */
    827      1.43     itohy 
    828      1.48  drochner 	while ((desc = (const usb_cdc_descriptor_t *)usb_desc_iter_next(&iter))
    829      1.48  drochner 	       != NULL) {
    830      1.43     itohy 		if (desc->bDescriptorType == UDESC_INTERFACE) {
    831      1.43     itohy 			/* we ran into the next interface --- not found */
    832      1.43     itohy 			return NULL;
    833      1.43     itohy 		}
    834      1.43     itohy 		if (desc->bDescriptorType == type &&
    835      1.48  drochner 		    (subtype == USBD_CDCSUBTYPE_ANY ||
    836      1.43     itohy 		     subtype == desc->bDescriptorSubtype))
    837      1.43     itohy 			break;
    838      1.43     itohy 	}
    839      1.43     itohy 	return desc;
    840      1.43     itohy }
    841