Home | History | Annotate | Line # | Download | only in usb
usbdi_util.c revision 1.80
      1  1.80      maxv /*	$NetBSD: usbdi_util.c,v 1.80 2020/02/16 09:40:35 maxv 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.80      maxv __KERNEL_RCSID(0, "$NetBSD: usbdi_util.c,v 1.80 2020/02/16 09:40:35 maxv 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.78      maxv /*
    158  1.78      maxv  * Get the first 8 bytes of the device descriptor.
    159  1.78      maxv  * Do as Windows does: try to read 64 bytes -- there are devices which
    160  1.78      maxv  * recognize the initial descriptor fetch (before the control endpoint's
    161  1.78      maxv  * MaxPacketSize is known by the host) by exactly this length.
    162  1.78      maxv  */
    163  1.78      maxv usbd_status
    164  1.78      maxv usbd_get_initial_ddesc(struct usbd_device *dev, usb_device_descriptor_t *desc)
    165  1.78      maxv {
    166  1.78      maxv 	USBHIST_FUNC();
    167  1.78      maxv 	USBHIST_CALLARGS(usbdebug, "dev %#jx", (uintptr_t)dev, 0, 0, 0);
    168  1.78      maxv 	usb_device_request_t req;
    169  1.78      maxv 	char buf[64];
    170  1.78      maxv 	int res, actlen;
    171  1.78      maxv 
    172  1.78      maxv 	req.bmRequestType = UT_READ_DEVICE;
    173  1.78      maxv 	req.bRequest = UR_GET_DESCRIPTOR;
    174  1.78      maxv 	USETW2(req.wValue, UDESC_DEVICE, 0);
    175  1.78      maxv 	USETW(req.wIndex, 0);
    176  1.78      maxv 	USETW(req.wLength, 8);
    177  1.78      maxv 	res = usbd_do_request_flags(dev, &req, buf, USBD_SHORT_XFER_OK,
    178  1.78      maxv 		&actlen, USBD_DEFAULT_TIMEOUT);
    179  1.78      maxv 	if (res)
    180  1.78      maxv 		return res;
    181  1.78      maxv 	if (actlen < 8)
    182  1.78      maxv 		return USBD_SHORT_XFER;
    183  1.78      maxv 	memcpy(desc, buf, 8);
    184  1.78      maxv 	return USBD_NORMAL_COMPLETION;
    185  1.78      maxv }
    186  1.78      maxv 
    187  1.78      maxv usbd_status
    188  1.78      maxv usbd_get_string_desc(struct usbd_device *dev, int sindex, int langid,
    189  1.78      maxv     usb_string_descriptor_t *sdesc, int *sizep)
    190  1.78      maxv {
    191  1.78      maxv 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    192  1.78      maxv 	usb_device_request_t req;
    193  1.78      maxv 	usbd_status err;
    194  1.78      maxv 	int actlen;
    195  1.78      maxv 
    196  1.78      maxv 	/*
    197  1.78      maxv 	 * Pass a full-sized buffer to usbd_do_request_len().  At least
    198  1.78      maxv 	 * one device has been seen returning additional data beyond the
    199  1.78      maxv 	 * provided buffers (2-bytes written shortly after the request
    200  1.78      maxv 	 * claims to have completed and returned the 2 byte header,
    201  1.78      maxv 	 * corrupting other memory.)
    202  1.78      maxv 	 */
    203  1.78      maxv 	req.bmRequestType = UT_READ_DEVICE;
    204  1.78      maxv 	req.bRequest = UR_GET_DESCRIPTOR;
    205  1.78      maxv 	USETW2(req.wValue, UDESC_STRING, sindex);
    206  1.78      maxv 	USETW(req.wIndex, langid);
    207  1.78      maxv 	USETW(req.wLength, 2);	/* only size byte first */
    208  1.78      maxv 	err = usbd_do_request_len(dev, &req, sizeof(*sdesc), sdesc,
    209  1.78      maxv 	    USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
    210  1.78      maxv 	if (err)
    211  1.78      maxv 		return err;
    212  1.78      maxv 
    213  1.78      maxv 	if (actlen < 2)
    214  1.78      maxv 		return USBD_SHORT_XFER;
    215  1.78      maxv 
    216  1.78      maxv 	if (sdesc->bLength > sizeof(*sdesc))
    217  1.78      maxv 		return USBD_INVAL;
    218  1.78      maxv 	USETW(req.wLength, sdesc->bLength);	/* the whole string */
    219  1.78      maxv 	err = usbd_do_request_len(dev, &req, sizeof(*sdesc), sdesc,
    220  1.78      maxv 	    USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
    221  1.78      maxv 	if (err)
    222  1.78      maxv 		return err;
    223  1.78      maxv 
    224  1.78      maxv 	if (actlen != sdesc->bLength) {
    225  1.78      maxv 		DPRINTF("expected %jd, got %jd", sdesc->bLength, actlen, 0, 0);
    226  1.78      maxv 	}
    227  1.78      maxv 
    228  1.78      maxv 	*sizep = actlen;
    229  1.78      maxv 	return USBD_NORMAL_COMPLETION;
    230  1.78      maxv }
    231  1.78      maxv 
    232  1.77      maxv /* -------------------------------------------------------------------------- */
    233  1.77      maxv 
    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.77      maxv /* -------------------------------------------------------------------------- */
    298  1.77      maxv 
    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.76      maxv usbd_clear_endpoint_feature(struct usbd_device *dev, int epaddr, int sel)
    397  1.76      maxv {
    398  1.76      maxv 	USBHIST_FUNC();
    399  1.79     skrll 	USBHIST_CALLARGS(usbdebug, "dev %#jx epaddr %jd sel %jd",
    400  1.79     skrll 	    (uintptr_t)dev, epaddr, sel, 0);
    401  1.76      maxv 	usb_device_request_t req;
    402  1.76      maxv 
    403  1.76      maxv 	req.bmRequestType = UT_WRITE_ENDPOINT;
    404  1.76      maxv 	req.bRequest = UR_CLEAR_FEATURE;
    405  1.76      maxv 	USETW(req.wValue, sel);
    406  1.76      maxv 	USETW(req.wIndex, epaddr);
    407  1.76      maxv 	USETW(req.wLength, 0);
    408  1.76      maxv 	return usbd_do_request(dev, &req, 0);
    409  1.76      maxv }
    410  1.76      maxv 
    411  1.77      maxv /* -------------------------------------------------------------------------- */
    412  1.77      maxv 
    413  1.77      maxv usbd_status
    414  1.77      maxv usbd_get_config(struct usbd_device *dev, uint8_t *conf)
    415  1.77      maxv {
    416  1.77      maxv 	USBHIST_FUNC();
    417  1.77      maxv 	USBHIST_CALLARGS(usbdebug, "dev %#jx", (uintptr_t)dev, 0, 0, 0);
    418  1.77      maxv 	usb_device_request_t req;
    419  1.77      maxv 
    420  1.77      maxv 	req.bmRequestType = UT_READ_DEVICE;
    421  1.77      maxv 	req.bRequest = UR_GET_CONFIG;
    422  1.77      maxv 	USETW(req.wValue, 0);
    423  1.77      maxv 	USETW(req.wIndex, 0);
    424  1.77      maxv 	USETW(req.wLength, 1);
    425  1.77      maxv 	return usbd_do_request(dev, &req, conf);
    426  1.77      maxv }
    427  1.77      maxv 
    428  1.77      maxv usbd_status
    429  1.78      maxv usbd_set_config(struct usbd_device *dev, int conf)
    430  1.78      maxv {
    431  1.78      maxv 	USBHIST_FUNC();
    432  1.78      maxv 	USBHIST_CALLARGS(usbdebug, "dev %#jx conf %jd",
    433  1.78      maxv 	    (uintptr_t)dev, conf, 0, 0);
    434  1.78      maxv 	usb_device_request_t req;
    435  1.78      maxv 
    436  1.78      maxv 	req.bmRequestType = UT_WRITE_DEVICE;
    437  1.78      maxv 	req.bRequest = UR_SET_CONFIG;
    438  1.78      maxv 	USETW(req.wValue, conf);
    439  1.78      maxv 	USETW(req.wIndex, 0);
    440  1.78      maxv 	USETW(req.wLength, 0);
    441  1.78      maxv 	return usbd_do_request(dev, &req, 0);
    442  1.78      maxv }
    443  1.78      maxv 
    444  1.78      maxv usbd_status
    445  1.77      maxv usbd_set_address(struct usbd_device *dev, int addr)
    446  1.77      maxv {
    447  1.77      maxv 	USBHIST_FUNC();
    448  1.77      maxv 	USBHIST_CALLARGS(usbdebug, "dev %#jx addr %jd",
    449  1.77      maxv 	    (uintptr_t)dev, addr, 0, 0);
    450  1.77      maxv 	usb_device_request_t req;
    451  1.77      maxv 
    452  1.77      maxv 	req.bmRequestType = UT_WRITE_DEVICE;
    453  1.77      maxv 	req.bRequest = UR_SET_ADDRESS;
    454  1.77      maxv 	USETW(req.wValue, addr);
    455  1.77      maxv 	USETW(req.wIndex, 0);
    456  1.77      maxv 	USETW(req.wLength, 0);
    457  1.77      maxv 	return usbd_do_request(dev, &req, 0);
    458  1.77      maxv }
    459  1.77      maxv 
    460  1.77      maxv usbd_status
    461  1.77      maxv usbd_set_idle(struct usbd_interface *iface, int duration, int id)
    462  1.77      maxv {
    463  1.77      maxv 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
    464  1.77      maxv 	struct usbd_device *dev;
    465  1.77      maxv 	usb_device_request_t req;
    466  1.77      maxv 
    467  1.77      maxv 	USBHIST_FUNC();
    468  1.77      maxv 	USBHIST_CALLARGS(usbdebug, "duration %jd id %jd", duration, id, 0, 0);
    469  1.77      maxv 
    470  1.77      maxv 	if (ifd == NULL)
    471  1.77      maxv 		return USBD_IOERROR;
    472  1.77      maxv 	usbd_interface2device_handle(iface, &dev);
    473  1.77      maxv 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    474  1.77      maxv 	req.bRequest = UR_SET_IDLE;
    475  1.77      maxv 	USETW2(req.wValue, duration, id);
    476  1.77      maxv 	USETW(req.wIndex, ifd->bInterfaceNumber);
    477  1.77      maxv 	USETW(req.wLength, 0);
    478  1.77      maxv 	return usbd_do_request(dev, &req, 0);
    479  1.77      maxv }
    480  1.77      maxv 
    481  1.77      maxv /* -------------------------------------------------------------------------- */
    482  1.77      maxv 
    483  1.76      maxv 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.77      maxv /* -------------------------------------------------------------------------- */
    530  1.77      maxv 
    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.77      maxv /* -------------------------------------------------------------------------- */
    592  1.77      maxv 
    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.80      maxv /* -------------------------------------------------------------------------- */
    721  1.80      maxv 
    722  1.80      maxv void
    723  1.80      maxv usb_desc_iter_init(struct usbd_device *dev, usbd_desc_iter_t *iter)
    724  1.80      maxv {
    725  1.80      maxv 	const usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
    726  1.80      maxv 
    727  1.80      maxv 	iter->cur = (const uByte *)cd;
    728  1.80      maxv 	iter->end = (const uByte *)cd + UGETW(cd->wTotalLength);
    729  1.80      maxv }
    730  1.80      maxv 
    731  1.80      maxv const usb_descriptor_t *
    732  1.80      maxv usb_desc_iter_peek(usbd_desc_iter_t *iter)
    733  1.80      maxv {
    734  1.80      maxv 	const usb_descriptor_t *desc;
    735  1.80      maxv 
    736  1.80      maxv 	if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) {
    737  1.80      maxv 		if (iter->cur != iter->end)
    738  1.80      maxv 			printf("%s: bad descriptor\n", __func__);
    739  1.80      maxv 		return NULL;
    740  1.80      maxv 	}
    741  1.80      maxv 	desc = (const usb_descriptor_t *)iter->cur;
    742  1.80      maxv 	if (desc->bLength == 0) {
    743  1.80      maxv 		printf("%s: descriptor length = 0\n", __func__);
    744  1.80      maxv 		return NULL;
    745  1.80      maxv 	}
    746  1.80      maxv 	if (iter->cur + desc->bLength > iter->end) {
    747  1.80      maxv 		printf("%s: descriptor length too large\n", __func__);
    748  1.80      maxv 		return NULL;
    749  1.80      maxv 	}
    750  1.80      maxv 	return desc;
    751  1.80      maxv }
    752  1.80      maxv 
    753  1.80      maxv const usb_descriptor_t *
    754  1.80      maxv usb_desc_iter_next(usbd_desc_iter_t *iter)
    755  1.80      maxv {
    756  1.80      maxv 	const usb_descriptor_t *desc = usb_desc_iter_peek(iter);
    757  1.80      maxv 	if (desc == NULL)
    758  1.80      maxv 		return NULL;
    759  1.80      maxv 	iter->cur += desc->bLength;
    760  1.80      maxv 	return desc;
    761  1.80      maxv }
    762  1.80      maxv 
    763  1.80      maxv /* Return the next interface descriptor, skipping over any other
    764  1.80      maxv  * descriptors.  Returns NULL at the end or on error. */
    765  1.80      maxv const usb_interface_descriptor_t *
    766  1.80      maxv usb_desc_iter_next_interface(usbd_desc_iter_t *iter)
    767  1.80      maxv {
    768  1.80      maxv 	const usb_descriptor_t *desc;
    769  1.80      maxv 
    770  1.80      maxv 	while ((desc = usb_desc_iter_peek(iter)) != NULL &&
    771  1.80      maxv 	       desc->bDescriptorType != UDESC_INTERFACE)
    772  1.80      maxv 	{
    773  1.80      maxv 		usb_desc_iter_next(iter);
    774  1.80      maxv 	}
    775  1.80      maxv 
    776  1.80      maxv 	return (const usb_interface_descriptor_t *)usb_desc_iter_next(iter);
    777  1.80      maxv }
    778  1.80      maxv 
    779  1.80      maxv /* Returns the next non-interface descriptor, returning NULL when the
    780  1.80      maxv  * next descriptor would be an interface descriptor. */
    781  1.80      maxv const usb_descriptor_t *
    782  1.80      maxv usb_desc_iter_next_non_interface(usbd_desc_iter_t *iter)
    783  1.80      maxv {
    784  1.80      maxv 	const usb_descriptor_t *desc;
    785  1.80      maxv 
    786  1.80      maxv 	if ((desc = usb_desc_iter_peek(iter)) != NULL &&
    787  1.80      maxv 	    desc->bDescriptorType != UDESC_INTERFACE)
    788  1.80      maxv 	{
    789  1.80      maxv 		return usb_desc_iter_next(iter);
    790  1.80      maxv 	} else {
    791  1.80      maxv 		return NULL;
    792  1.80      maxv 	}
    793  1.80      maxv }
    794  1.80      maxv 
    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