xref: /freebsd/sys/dev/usb/usb_device.c (revision af3b2549c4ba2ef00a7cbb4cb6836598bf0aefbe)
102ac6454SAndrew Thompson /* $FreeBSD$ */
202ac6454SAndrew Thompson /*-
302ac6454SAndrew Thompson  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
402ac6454SAndrew Thompson  *
502ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
602ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
702ac6454SAndrew Thompson  * are met:
802ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
902ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1002ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1102ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1202ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1302ac6454SAndrew Thompson  *
1402ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1502ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1602ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1702ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1802ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1902ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2002ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2102ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2202ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2302ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2402ac6454SAndrew Thompson  * SUCH DAMAGE.
2502ac6454SAndrew Thompson  */
2602ac6454SAndrew Thompson 
27d2b99310SHans Petter Selasky #ifdef USB_GLOBAL_INCLUDE_FILE
28d2b99310SHans Petter Selasky #include USB_GLOBAL_INCLUDE_FILE
29d2b99310SHans Petter Selasky #else
30ed6d949aSAndrew Thompson #include <sys/stdint.h>
31ed6d949aSAndrew Thompson #include <sys/stddef.h>
32ed6d949aSAndrew Thompson #include <sys/param.h>
33ed6d949aSAndrew Thompson #include <sys/queue.h>
34ed6d949aSAndrew Thompson #include <sys/types.h>
35ed6d949aSAndrew Thompson #include <sys/systm.h>
36ed6d949aSAndrew Thompson #include <sys/kernel.h>
37ed6d949aSAndrew Thompson #include <sys/bus.h>
38ed6d949aSAndrew Thompson #include <sys/module.h>
39ed6d949aSAndrew Thompson #include <sys/lock.h>
40ed6d949aSAndrew Thompson #include <sys/mutex.h>
41ed6d949aSAndrew Thompson #include <sys/condvar.h>
42ed6d949aSAndrew Thompson #include <sys/sysctl.h>
43ed6d949aSAndrew Thompson #include <sys/sx.h>
44ed6d949aSAndrew Thompson #include <sys/unistd.h>
45ed6d949aSAndrew Thompson #include <sys/callout.h>
46ed6d949aSAndrew Thompson #include <sys/malloc.h>
47ed6d949aSAndrew Thompson #include <sys/priv.h>
48ed6d949aSAndrew Thompson #include <sys/conf.h>
49ed6d949aSAndrew Thompson #include <sys/fcntl.h>
50ed6d949aSAndrew Thompson 
5102ac6454SAndrew Thompson #include <dev/usb/usb.h>
52ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
53ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
5402ac6454SAndrew Thompson #include <dev/usb/usb_ioctl.h>
55963169b4SHans Petter Selasky 
56963169b4SHans Petter Selasky #if USB_HAVE_UGEN
57963169b4SHans Petter Selasky #include <sys/sbuf.h>
58963169b4SHans Petter Selasky #endif
59963169b4SHans Petter Selasky 
6002ac6454SAndrew Thompson #include "usbdevs.h"
6102ac6454SAndrew Thompson 
62a593f6b8SAndrew Thompson #define	USB_DEBUG_VAR usb_debug
6302ac6454SAndrew Thompson 
6402ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
6502ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
6602ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
6702ac6454SAndrew Thompson #include <dev/usb/usb_device.h>
6802ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
6902ac6454SAndrew Thompson #include <dev/usb/usb_transfer.h>
7002ac6454SAndrew Thompson #include <dev/usb/usb_request.h>
7102ac6454SAndrew Thompson #include <dev/usb/usb_dynamic.h>
7202ac6454SAndrew Thompson #include <dev/usb/usb_hub.h>
7302ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
7402ac6454SAndrew Thompson #include <dev/usb/usb_msctest.h>
75bdc081c6SAndrew Thompson #if USB_HAVE_UGEN
76bdc081c6SAndrew Thompson #include <dev/usb/usb_dev.h>
7702ac6454SAndrew Thompson #include <dev/usb/usb_generic.h>
78bdc081c6SAndrew Thompson #endif
7902ac6454SAndrew Thompson 
8002ac6454SAndrew Thompson #include <dev/usb/quirk/usb_quirk.h>
8102ac6454SAndrew Thompson 
8202ac6454SAndrew Thompson #include <dev/usb/usb_controller.h>
8302ac6454SAndrew Thompson #include <dev/usb/usb_bus.h>
84d2b99310SHans Petter Selasky #endif			/* USB_GLOBAL_INCLUDE_FILE */
8502ac6454SAndrew Thompson 
8602ac6454SAndrew Thompson /* function prototypes  */
8702ac6454SAndrew Thompson 
88a593f6b8SAndrew Thompson static void	usb_init_endpoint(struct usb_device *, uint8_t,
89963169b4SHans Petter Selasky 		    struct usb_endpoint_descriptor *,
90963169b4SHans Petter Selasky 		    struct usb_endpoint_ss_comp_descriptor *,
91963169b4SHans Petter Selasky 		    struct usb_endpoint *);
92a593f6b8SAndrew Thompson static void	usb_unconfigure(struct usb_device *, uint8_t);
93a593f6b8SAndrew Thompson static void	usb_detach_device_sub(struct usb_device *, device_t *,
948427ed84SHans Petter Selasky 		    char **, uint8_t);
95a593f6b8SAndrew Thompson static uint8_t	usb_probe_and_attach_sub(struct usb_device *,
96760bc48eSAndrew Thompson 		    struct usb_attach_arg *);
97a593f6b8SAndrew Thompson static void	usb_init_attach_arg(struct usb_device *,
98760bc48eSAndrew Thompson 		    struct usb_attach_arg *);
99a593f6b8SAndrew Thompson static void	usb_suspend_resume_sub(struct usb_device *, device_t,
10002ac6454SAndrew Thompson 		    uint8_t);
101a0d53e0bSHans Petter Selasky static usb_proc_callback_t usbd_clear_stall_proc;
102963169b4SHans Petter Selasky static usb_error_t usb_config_parse(struct usb_device *, uint8_t, uint8_t);
103a593f6b8SAndrew Thompson static void	usbd_set_device_strings(struct usb_device *);
1048bb77249SHans Petter Selasky #if USB_HAVE_DEVCTL
105a593f6b8SAndrew Thompson static void	usb_notify_addq(const char *type, struct usb_device *);
1068bb77249SHans Petter Selasky #endif
1078bb77249SHans Petter Selasky #if USB_HAVE_UGEN
108a593f6b8SAndrew Thompson static void	usb_fifo_free_wrap(struct usb_device *, uint8_t, uint8_t);
109a593f6b8SAndrew Thompson static void	usb_cdev_create(struct usb_device *);
110a593f6b8SAndrew Thompson static void	usb_cdev_free(struct usb_device *);
1110f6e8f93SAndrew Thompson #endif
11202ac6454SAndrew Thompson 
11302ac6454SAndrew Thompson /* This variable is global to allow easy access to it: */
11402ac6454SAndrew Thompson 
115fa49bce5SHans Petter Selasky #ifdef	USB_TEMPLATE
116fa49bce5SHans Petter Selasky int	usb_template = USB_TEMPLATE;
117fa49bce5SHans Petter Selasky #else
118fa49bce5SHans Petter Selasky int	usb_template;
119fa49bce5SHans Petter Selasky #endif
12002ac6454SAndrew Thompson 
121*af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb, OID_AUTO, template, CTLFLAG_RWTUN,
122a593f6b8SAndrew Thompson     &usb_template, 0, "Selected USB device side template");
12302ac6454SAndrew Thompson 
1247a918edfSAndrew Thompson /* English is default language */
1257a918edfSAndrew Thompson 
1267a918edfSAndrew Thompson static int usb_lang_id = 0x0009;
1277a918edfSAndrew Thompson static int usb_lang_mask = 0x00FF;
1287a918edfSAndrew Thompson 
129*af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_id, CTLFLAG_RWTUN,
1307a918edfSAndrew Thompson     &usb_lang_id, 0, "Preferred USB language ID");
1317a918edfSAndrew Thompson 
132*af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_mask, CTLFLAG_RWTUN,
1337a918edfSAndrew Thompson     &usb_lang_mask, 0, "Preferred USB language mask");
1347a918edfSAndrew Thompson 
1354d4fa6edSAndrew Thompson static const char* statestr[USB_STATE_MAX] = {
1364d4fa6edSAndrew Thompson 	[USB_STATE_DETACHED]	= "DETACHED",
1374d4fa6edSAndrew Thompson 	[USB_STATE_ATTACHED]	= "ATTACHED",
1384d4fa6edSAndrew Thompson 	[USB_STATE_POWERED]	= "POWERED",
1394d4fa6edSAndrew Thompson 	[USB_STATE_ADDRESSED]	= "ADDRESSED",
1404d4fa6edSAndrew Thompson 	[USB_STATE_CONFIGURED]	= "CONFIGURED",
1414d4fa6edSAndrew Thompson };
1424d4fa6edSAndrew Thompson 
1434d4fa6edSAndrew Thompson const char *
144a593f6b8SAndrew Thompson usb_statestr(enum usb_dev_state state)
1454d4fa6edSAndrew Thompson {
146ec8f3127SAndrew Thompson 	return ((state < USB_STATE_MAX) ? statestr[state] : "UNKNOWN");
1474d4fa6edSAndrew Thompson }
14802ac6454SAndrew Thompson 
149ae538d85SAndrew Thompson const char *
150ae538d85SAndrew Thompson usb_get_manufacturer(struct usb_device *udev)
151ae538d85SAndrew Thompson {
152ae538d85SAndrew Thompson 	return (udev->manufacturer ? udev->manufacturer : "Unknown");
153ae538d85SAndrew Thompson }
154ae538d85SAndrew Thompson 
155ae538d85SAndrew Thompson const char *
156ae538d85SAndrew Thompson usb_get_product(struct usb_device *udev)
157ae538d85SAndrew Thompson {
158ae538d85SAndrew Thompson 	return (udev->product ? udev->product : "");
159ae538d85SAndrew Thompson }
160ae538d85SAndrew Thompson 
161ae538d85SAndrew Thompson const char *
162ae538d85SAndrew Thompson usb_get_serial(struct usb_device *udev)
163ae538d85SAndrew Thompson {
164ae538d85SAndrew Thompson 	return (udev->serial ? udev->serial : "");
165ae538d85SAndrew Thompson }
166ae538d85SAndrew Thompson 
16702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
168a593f6b8SAndrew Thompson  *	usbd_get_ep_by_addr
16902ac6454SAndrew Thompson  *
170ae60fdfbSAndrew Thompson  * This function searches for an USB ep by endpoint address and
17102ac6454SAndrew Thompson  * direction.
17202ac6454SAndrew Thompson  *
17302ac6454SAndrew Thompson  * Returns:
17402ac6454SAndrew Thompson  * NULL: Failure
17502ac6454SAndrew Thompson  * Else: Success
17602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
177ae60fdfbSAndrew Thompson struct usb_endpoint *
178a593f6b8SAndrew Thompson usbd_get_ep_by_addr(struct usb_device *udev, uint8_t ea_val)
17902ac6454SAndrew Thompson {
180ae60fdfbSAndrew Thompson 	struct usb_endpoint *ep = udev->endpoints;
181ae60fdfbSAndrew Thompson 	struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max;
18202ac6454SAndrew Thompson 	enum {
18302ac6454SAndrew Thompson 		EA_MASK = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR),
18402ac6454SAndrew Thompson 	};
18502ac6454SAndrew Thompson 
18602ac6454SAndrew Thompson 	/*
18702ac6454SAndrew Thompson 	 * According to the USB specification not all bits are used
18802ac6454SAndrew Thompson 	 * for the endpoint address. Keep defined bits only:
18902ac6454SAndrew Thompson 	 */
19002ac6454SAndrew Thompson 	ea_val &= EA_MASK;
19102ac6454SAndrew Thompson 
19202ac6454SAndrew Thompson 	/*
193ae60fdfbSAndrew Thompson 	 * Iterate accross all the USB endpoints searching for a match
19402ac6454SAndrew Thompson 	 * based on the endpoint address:
19502ac6454SAndrew Thompson 	 */
196ae60fdfbSAndrew Thompson 	for (; ep != ep_end; ep++) {
19702ac6454SAndrew Thompson 
198ae60fdfbSAndrew Thompson 		if (ep->edesc == NULL) {
19902ac6454SAndrew Thompson 			continue;
20002ac6454SAndrew Thompson 		}
20102ac6454SAndrew Thompson 		/* do the mask and check the value */
202ae60fdfbSAndrew Thompson 		if ((ep->edesc->bEndpointAddress & EA_MASK) == ea_val) {
20302ac6454SAndrew Thompson 			goto found;
20402ac6454SAndrew Thompson 		}
20502ac6454SAndrew Thompson 	}
20602ac6454SAndrew Thompson 
20702ac6454SAndrew Thompson 	/*
208ae60fdfbSAndrew Thompson 	 * The default endpoint is always present and is checked separately:
20902ac6454SAndrew Thompson 	 */
21022039494SHans Petter Selasky 	if ((udev->ctrl_ep.edesc != NULL) &&
2115b3bb704SAndrew Thompson 	    ((udev->ctrl_ep.edesc->bEndpointAddress & EA_MASK) == ea_val)) {
2125b3bb704SAndrew Thompson 		ep = &udev->ctrl_ep;
21302ac6454SAndrew Thompson 		goto found;
21402ac6454SAndrew Thompson 	}
21502ac6454SAndrew Thompson 	return (NULL);
21602ac6454SAndrew Thompson 
21702ac6454SAndrew Thompson found:
218ae60fdfbSAndrew Thompson 	return (ep);
21902ac6454SAndrew Thompson }
22002ac6454SAndrew Thompson 
22102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
222a593f6b8SAndrew Thompson  *	usbd_get_endpoint
22302ac6454SAndrew Thompson  *
224ae60fdfbSAndrew Thompson  * This function searches for an USB endpoint based on the information
225760bc48eSAndrew Thompson  * given by the passed "struct usb_config" pointer.
22602ac6454SAndrew Thompson  *
22702ac6454SAndrew Thompson  * Return values:
22802ac6454SAndrew Thompson  * NULL: No match.
229ae60fdfbSAndrew Thompson  * Else: Pointer to "struct usb_endpoint".
23002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
231ae60fdfbSAndrew Thompson struct usb_endpoint *
232a593f6b8SAndrew Thompson usbd_get_endpoint(struct usb_device *udev, uint8_t iface_index,
233760bc48eSAndrew Thompson     const struct usb_config *setup)
23402ac6454SAndrew Thompson {
235ae60fdfbSAndrew Thompson 	struct usb_endpoint *ep = udev->endpoints;
236ae60fdfbSAndrew Thompson 	struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max;
23702ac6454SAndrew Thompson 	uint8_t index = setup->ep_index;
23802ac6454SAndrew Thompson 	uint8_t ea_mask;
23902ac6454SAndrew Thompson 	uint8_t ea_val;
24002ac6454SAndrew Thompson 	uint8_t type_mask;
24102ac6454SAndrew Thompson 	uint8_t type_val;
24202ac6454SAndrew Thompson 
24302ac6454SAndrew Thompson 	DPRINTFN(10, "udev=%p iface_index=%d address=0x%x "
24402ac6454SAndrew Thompson 	    "type=0x%x dir=0x%x index=%d\n",
24502ac6454SAndrew Thompson 	    udev, iface_index, setup->endpoint,
24602ac6454SAndrew Thompson 	    setup->type, setup->direction, setup->ep_index);
24702ac6454SAndrew Thompson 
2484eae601eSAndrew Thompson 	/* check USB mode */
2494eae601eSAndrew Thompson 
250f29a0724SAndrew Thompson 	if (setup->usb_mode != USB_MODE_DUAL &&
251f29a0724SAndrew Thompson 	    udev->flags.usb_mode != setup->usb_mode) {
252ae60fdfbSAndrew Thompson 		/* wrong mode - no endpoint */
2534eae601eSAndrew Thompson 		return (NULL);
2544eae601eSAndrew Thompson 	}
2554eae601eSAndrew Thompson 
25602ac6454SAndrew Thompson 	/* setup expected endpoint direction mask and value */
25702ac6454SAndrew Thompson 
25851ec1603SAndrew Thompson 	if (setup->direction == UE_DIR_RX) {
25951ec1603SAndrew Thompson 		ea_mask = (UE_DIR_IN | UE_DIR_OUT);
260f29a0724SAndrew Thompson 		ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ?
26151ec1603SAndrew Thompson 		    UE_DIR_OUT : UE_DIR_IN;
26251ec1603SAndrew Thompson 	} else if (setup->direction == UE_DIR_TX) {
26351ec1603SAndrew Thompson 		ea_mask = (UE_DIR_IN | UE_DIR_OUT);
264f29a0724SAndrew Thompson 		ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ?
26551ec1603SAndrew Thompson 		    UE_DIR_IN : UE_DIR_OUT;
26651ec1603SAndrew Thompson 	} else if (setup->direction == UE_DIR_ANY) {
26702ac6454SAndrew Thompson 		/* match any endpoint direction */
26802ac6454SAndrew Thompson 		ea_mask = 0;
26902ac6454SAndrew Thompson 		ea_val = 0;
27002ac6454SAndrew Thompson 	} else {
27102ac6454SAndrew Thompson 		/* match the given endpoint direction */
27202ac6454SAndrew Thompson 		ea_mask = (UE_DIR_IN | UE_DIR_OUT);
27302ac6454SAndrew Thompson 		ea_val = (setup->direction & (UE_DIR_IN | UE_DIR_OUT));
27402ac6454SAndrew Thompson 	}
27502ac6454SAndrew Thompson 
27602ac6454SAndrew Thompson 	/* setup expected endpoint address */
27702ac6454SAndrew Thompson 
27802ac6454SAndrew Thompson 	if (setup->endpoint == UE_ADDR_ANY) {
27902ac6454SAndrew Thompson 		/* match any endpoint address */
28002ac6454SAndrew Thompson 	} else {
28102ac6454SAndrew Thompson 		/* match the given endpoint address */
28202ac6454SAndrew Thompson 		ea_mask |= UE_ADDR;
28302ac6454SAndrew Thompson 		ea_val |= (setup->endpoint & UE_ADDR);
28402ac6454SAndrew Thompson 	}
28502ac6454SAndrew Thompson 
28602ac6454SAndrew Thompson 	/* setup expected endpoint type */
28702ac6454SAndrew Thompson 
28802ac6454SAndrew Thompson 	if (setup->type == UE_BULK_INTR) {
28902ac6454SAndrew Thompson 		/* this will match BULK and INTERRUPT endpoints */
29002ac6454SAndrew Thompson 		type_mask = 2;
29102ac6454SAndrew Thompson 		type_val = 2;
29202ac6454SAndrew Thompson 	} else if (setup->type == UE_TYPE_ANY) {
29302ac6454SAndrew Thompson 		/* match any endpoint type */
29402ac6454SAndrew Thompson 		type_mask = 0;
29502ac6454SAndrew Thompson 		type_val = 0;
29602ac6454SAndrew Thompson 	} else {
29702ac6454SAndrew Thompson 		/* match the given endpoint type */
29802ac6454SAndrew Thompson 		type_mask = UE_XFERTYPE;
29902ac6454SAndrew Thompson 		type_val = (setup->type & UE_XFERTYPE);
30002ac6454SAndrew Thompson 	}
30102ac6454SAndrew Thompson 
30202ac6454SAndrew Thompson 	/*
303ae60fdfbSAndrew Thompson 	 * Iterate accross all the USB endpoints searching for a match
30402ac6454SAndrew Thompson 	 * based on the endpoint address. Note that we are searching
305ae60fdfbSAndrew Thompson 	 * the endpoints from the beginning of the "udev->endpoints" array.
30602ac6454SAndrew Thompson 	 */
307ae60fdfbSAndrew Thompson 	for (; ep != ep_end; ep++) {
30802ac6454SAndrew Thompson 
309ae60fdfbSAndrew Thompson 		if ((ep->edesc == NULL) ||
310ae60fdfbSAndrew Thompson 		    (ep->iface_index != iface_index)) {
31102ac6454SAndrew Thompson 			continue;
31202ac6454SAndrew Thompson 		}
31302ac6454SAndrew Thompson 		/* do the masks and check the values */
31402ac6454SAndrew Thompson 
315ae60fdfbSAndrew Thompson 		if (((ep->edesc->bEndpointAddress & ea_mask) == ea_val) &&
316ae60fdfbSAndrew Thompson 		    ((ep->edesc->bmAttributes & type_mask) == type_val)) {
31702ac6454SAndrew Thompson 			if (!index--) {
31802ac6454SAndrew Thompson 				goto found;
31902ac6454SAndrew Thompson 			}
32002ac6454SAndrew Thompson 		}
32102ac6454SAndrew Thompson 	}
32202ac6454SAndrew Thompson 
32302ac6454SAndrew Thompson 	/*
324ae60fdfbSAndrew Thompson 	 * Match against default endpoint last, so that "any endpoint", "any
325ae60fdfbSAndrew Thompson 	 * address" and "any direction" returns the first endpoint of the
32602ac6454SAndrew Thompson 	 * interface. "iface_index" and "direction" is ignored:
32702ac6454SAndrew Thompson 	 */
32822039494SHans Petter Selasky 	if ((udev->ctrl_ep.edesc != NULL) &&
3295b3bb704SAndrew Thompson 	    ((udev->ctrl_ep.edesc->bEndpointAddress & ea_mask) == ea_val) &&
3305b3bb704SAndrew Thompson 	    ((udev->ctrl_ep.edesc->bmAttributes & type_mask) == type_val) &&
33102ac6454SAndrew Thompson 	    (!index)) {
3325b3bb704SAndrew Thompson 		ep = &udev->ctrl_ep;
33302ac6454SAndrew Thompson 		goto found;
33402ac6454SAndrew Thompson 	}
33502ac6454SAndrew Thompson 	return (NULL);
33602ac6454SAndrew Thompson 
33702ac6454SAndrew Thompson found:
338ae60fdfbSAndrew Thompson 	return (ep);
33902ac6454SAndrew Thompson }
34002ac6454SAndrew Thompson 
34102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
342ed6d949aSAndrew Thompson  *	usbd_interface_count
34302ac6454SAndrew Thompson  *
34402ac6454SAndrew Thompson  * This function stores the number of USB interfaces excluding
34502ac6454SAndrew Thompson  * alternate settings, which the USB config descriptor reports into
34602ac6454SAndrew Thompson  * the unsigned 8-bit integer pointed to by "count".
34702ac6454SAndrew Thompson  *
34802ac6454SAndrew Thompson  * Returns:
34902ac6454SAndrew Thompson  *    0: Success
35002ac6454SAndrew Thompson  * Else: Failure
35102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
352e0a69b51SAndrew Thompson usb_error_t
353ed6d949aSAndrew Thompson usbd_interface_count(struct usb_device *udev, uint8_t *count)
35402ac6454SAndrew Thompson {
35502ac6454SAndrew Thompson 	if (udev->cdesc == NULL) {
35602ac6454SAndrew Thompson 		*count = 0;
35702ac6454SAndrew Thompson 		return (USB_ERR_NOT_CONFIGURED);
35802ac6454SAndrew Thompson 	}
359bdd41206SAndrew Thompson 	*count = udev->ifaces_max;
36002ac6454SAndrew Thompson 	return (USB_ERR_NORMAL_COMPLETION);
36102ac6454SAndrew Thompson }
36202ac6454SAndrew Thompson 
36302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
364a593f6b8SAndrew Thompson  *	usb_init_endpoint
36502ac6454SAndrew Thompson  *
366ae60fdfbSAndrew Thompson  * This function will initialise the USB endpoint structure pointed to by
367ae60fdfbSAndrew Thompson  * the "endpoint" argument. The structure pointed to by "endpoint" must be
368bdd41206SAndrew Thompson  * zeroed before calling this function.
36902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
37002ac6454SAndrew Thompson static void
371a593f6b8SAndrew Thompson usb_init_endpoint(struct usb_device *udev, uint8_t iface_index,
372963169b4SHans Petter Selasky     struct usb_endpoint_descriptor *edesc,
373963169b4SHans Petter Selasky     struct usb_endpoint_ss_comp_descriptor *ecomp,
374963169b4SHans Petter Selasky     struct usb_endpoint *ep)
37502ac6454SAndrew Thompson {
376e892b3feSHans Petter Selasky 	const struct usb_bus_methods *methods;
377a5cf1aaaSHans Petter Selasky 	usb_stream_t x;
378ee3e3ff5SAndrew Thompson 
379bdd41206SAndrew Thompson 	methods = udev->bus->methods;
38002ac6454SAndrew Thompson 
381ae60fdfbSAndrew Thompson 	(methods->endpoint_init) (udev, edesc, ep);
38202ac6454SAndrew Thompson 
383ae60fdfbSAndrew Thompson 	/* initialise USB endpoint structure */
384ae60fdfbSAndrew Thompson 	ep->edesc = edesc;
385963169b4SHans Petter Selasky 	ep->ecomp = ecomp;
386ae60fdfbSAndrew Thompson 	ep->iface_index = iface_index;
387a5cf1aaaSHans Petter Selasky 
388a5cf1aaaSHans Petter Selasky 	/* setup USB stream queues */
389a5cf1aaaSHans Petter Selasky 	for (x = 0; x != USB_MAX_EP_STREAMS; x++) {
390a5cf1aaaSHans Petter Selasky 		TAILQ_INIT(&ep->endpoint_q[x].head);
391a5cf1aaaSHans Petter Selasky 		ep->endpoint_q[x].command = &usbd_pipe_start;
392a5cf1aaaSHans Petter Selasky 	}
39302ac6454SAndrew Thompson 
39439307315SAndrew Thompson 	/* the pipe is not supported by the hardware */
395ae60fdfbSAndrew Thompson  	if (ep->methods == NULL)
39639307315SAndrew Thompson 		return;
39739307315SAndrew Thompson 
398a5cf1aaaSHans Petter Selasky 	/* check for SUPER-speed streams mode endpoint */
399a5cf1aaaSHans Petter Selasky 	if (udev->speed == USB_SPEED_SUPER && ecomp != NULL &&
400a5cf1aaaSHans Petter Selasky 	    (edesc->bmAttributes & UE_XFERTYPE) == UE_BULK &&
401a5cf1aaaSHans Petter Selasky 	    (UE_GET_BULK_STREAMS(ecomp->bmAttributes) != 0)) {
402a5cf1aaaSHans Petter Selasky 		usbd_set_endpoint_mode(udev, ep, USB_EP_MODE_STREAMS);
403a5cf1aaaSHans Petter Selasky 	} else {
404a5cf1aaaSHans Petter Selasky 		usbd_set_endpoint_mode(udev, ep, USB_EP_MODE_DEFAULT);
405a5cf1aaaSHans Petter Selasky 	}
406a5cf1aaaSHans Petter Selasky 
40702ac6454SAndrew Thompson 	/* clear stall, if any */
408bdd41206SAndrew Thompson 	if (methods->clear_stall != NULL) {
40902ac6454SAndrew Thompson 		USB_BUS_LOCK(udev->bus);
410ae60fdfbSAndrew Thompson 		(methods->clear_stall) (udev, ep);
41102ac6454SAndrew Thompson 		USB_BUS_UNLOCK(udev->bus);
41202ac6454SAndrew Thompson 	}
41302ac6454SAndrew Thompson }
41402ac6454SAndrew Thompson 
415bdd41206SAndrew Thompson /*-----------------------------------------------------------------------*
416a593f6b8SAndrew Thompson  *	usb_endpoint_foreach
41716589beaSAndrew Thompson  *
41816589beaSAndrew Thompson  * This function will iterate all the USB endpoints except the control
41916589beaSAndrew Thompson  * endpoint. This function is NULL safe.
42016589beaSAndrew Thompson  *
42116589beaSAndrew Thompson  * Return values:
422ae60fdfbSAndrew Thompson  * NULL: End of USB endpoints
423ae60fdfbSAndrew Thompson  * Else: Pointer to next USB endpoint
42416589beaSAndrew Thompson  *------------------------------------------------------------------------*/
425ae60fdfbSAndrew Thompson struct usb_endpoint *
426a593f6b8SAndrew Thompson usb_endpoint_foreach(struct usb_device *udev, struct usb_endpoint *ep)
42716589beaSAndrew Thompson {
428a9e26757SAndrew Thompson 	struct usb_endpoint *ep_end;
42916589beaSAndrew Thompson 
43016589beaSAndrew Thompson 	/* be NULL safe */
43116589beaSAndrew Thompson 	if (udev == NULL)
43216589beaSAndrew Thompson 		return (NULL);
43316589beaSAndrew Thompson 
434a9e26757SAndrew Thompson 	ep_end = udev->endpoints + udev->endpoints_max;
435a9e26757SAndrew Thompson 
436ae60fdfbSAndrew Thompson 	/* get next endpoint */
437ae60fdfbSAndrew Thompson 	if (ep == NULL)
438ae60fdfbSAndrew Thompson 		ep = udev->endpoints;
43916589beaSAndrew Thompson 	else
440ae60fdfbSAndrew Thompson 		ep++;
44116589beaSAndrew Thompson 
442ae60fdfbSAndrew Thompson 	/* find next allocated ep */
443ae60fdfbSAndrew Thompson 	while (ep != ep_end) {
444ae60fdfbSAndrew Thompson 		if (ep->edesc != NULL)
445ae60fdfbSAndrew Thompson 			return (ep);
446ae60fdfbSAndrew Thompson 		ep++;
44716589beaSAndrew Thompson 	}
44816589beaSAndrew Thompson 	return (NULL);
44916589beaSAndrew Thompson }
45016589beaSAndrew Thompson 
45116589beaSAndrew Thompson /*------------------------------------------------------------------------*
452d008478eSHans Petter Selasky  *	usb_wait_pending_ref_locked
453d008478eSHans Petter Selasky  *
454d008478eSHans Petter Selasky  * This function will wait for any USB references to go away before
455d008478eSHans Petter Selasky  * returning and disable further USB device refcounting on the
456d008478eSHans Petter Selasky  * specified USB device. This function is used when detaching a USB
457d008478eSHans Petter Selasky  * device.
458d008478eSHans Petter Selasky  *------------------------------------------------------------------------*/
459d008478eSHans Petter Selasky static void
460d008478eSHans Petter Selasky usb_wait_pending_ref_locked(struct usb_device *udev)
461d008478eSHans Petter Selasky {
462d008478eSHans Petter Selasky #if USB_HAVE_UGEN
463d008478eSHans Petter Selasky 	const uint16_t refcount =
464d008478eSHans Petter Selasky 	    usb_proc_is_called_from(
465d008478eSHans Petter Selasky 	    USB_BUS_EXPLORE_PROC(udev->bus)) ? 1 : 2;
466d008478eSHans Petter Selasky 
467d008478eSHans Petter Selasky 	DPRINTF("Refcount = %d\n", (int)refcount);
468d008478eSHans Petter Selasky 
469d008478eSHans Petter Selasky 	while (1) {
470d008478eSHans Petter Selasky 		/* wait for any pending references to go away */
471d008478eSHans Petter Selasky 		mtx_lock(&usb_ref_lock);
472d008478eSHans Petter Selasky 		if (udev->refcount == refcount) {
473d008478eSHans Petter Selasky 			/* prevent further refs being taken */
474d008478eSHans Petter Selasky 			udev->refcount = USB_DEV_REF_MAX;
475d008478eSHans Petter Selasky 			mtx_unlock(&usb_ref_lock);
476d008478eSHans Petter Selasky 			break;
477d008478eSHans Petter Selasky 		}
478d008478eSHans Petter Selasky 		usbd_enum_unlock(udev);
479d008478eSHans Petter Selasky 		cv_wait(&udev->ref_cv, &usb_ref_lock);
480d008478eSHans Petter Selasky 		mtx_unlock(&usb_ref_lock);
481d008478eSHans Petter Selasky 		(void) usbd_enum_lock(udev);
482d008478eSHans Petter Selasky 	}
483d008478eSHans Petter Selasky #endif
484d008478eSHans Petter Selasky }
485d008478eSHans Petter Selasky 
486d008478eSHans Petter Selasky /*------------------------------------------------------------------------*
487d008478eSHans Petter Selasky  *	usb_ref_restore_locked
488d008478eSHans Petter Selasky  *
489d008478eSHans Petter Selasky  * This function will restore the reference count value after a call
490d008478eSHans Petter Selasky  * to "usb_wait_pending_ref_locked()".
491d008478eSHans Petter Selasky  *------------------------------------------------------------------------*/
492d008478eSHans Petter Selasky static void
493d008478eSHans Petter Selasky usb_ref_restore_locked(struct usb_device *udev)
494d008478eSHans Petter Selasky {
495d008478eSHans Petter Selasky #if USB_HAVE_UGEN
496d008478eSHans Petter Selasky 	const uint16_t refcount =
497d008478eSHans Petter Selasky 	    usb_proc_is_called_from(
498d008478eSHans Petter Selasky 	    USB_BUS_EXPLORE_PROC(udev->bus)) ? 1 : 2;
499d008478eSHans Petter Selasky 
500d008478eSHans Petter Selasky 	DPRINTF("Refcount = %d\n", (int)refcount);
501d008478eSHans Petter Selasky 
502d008478eSHans Petter Selasky 	/* restore reference count and wakeup waiters, if any */
503d008478eSHans Petter Selasky 	mtx_lock(&usb_ref_lock);
504d008478eSHans Petter Selasky 	udev->refcount = refcount;
505d008478eSHans Petter Selasky 	cv_broadcast(&udev->ref_cv);
506d008478eSHans Petter Selasky 	mtx_unlock(&usb_ref_lock);
507d008478eSHans Petter Selasky #endif
508d008478eSHans Petter Selasky }
509d008478eSHans Petter Selasky 
510d008478eSHans Petter Selasky /*------------------------------------------------------------------------*
511a593f6b8SAndrew Thompson  *	usb_unconfigure
51202ac6454SAndrew Thompson  *
513ae60fdfbSAndrew Thompson  * This function will free all USB interfaces and USB endpoints belonging
51402ac6454SAndrew Thompson  * to an USB device.
515bdd41206SAndrew Thompson  *
516bdd41206SAndrew Thompson  * Flag values, see "USB_UNCFG_FLAG_XXX".
51702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
51802ac6454SAndrew Thompson static void
519a593f6b8SAndrew Thompson usb_unconfigure(struct usb_device *udev, uint8_t flag)
52002ac6454SAndrew Thompson {
521bdd41206SAndrew Thompson 	uint8_t do_unlock;
52202ac6454SAndrew Thompson 
5236950c75fSHans Petter Selasky 	/* Prevent re-enumeration */
5246950c75fSHans Petter Selasky 	do_unlock = usbd_enum_lock(udev);
525bdd41206SAndrew Thompson 
526bdd41206SAndrew Thompson 	/* detach all interface drivers */
527a593f6b8SAndrew Thompson 	usb_detach_device(udev, USB_IFACE_INDEX_ANY, flag);
528bdd41206SAndrew Thompson 
529bdd41206SAndrew Thompson #if USB_HAVE_UGEN
530bdd41206SAndrew Thompson 	/* free all FIFOs except control endpoint FIFOs */
531a593f6b8SAndrew Thompson 	usb_fifo_free_wrap(udev, USB_IFACE_INDEX_ANY, flag);
532bdd41206SAndrew Thompson 
533bdd41206SAndrew Thompson 	/*
534bdd41206SAndrew Thompson 	 * Free all cdev's, if any.
535bdd41206SAndrew Thompson 	 */
536a593f6b8SAndrew Thompson 	usb_cdev_free(udev);
537bdd41206SAndrew Thompson #endif
53802ac6454SAndrew Thompson 
539bdc081c6SAndrew Thompson #if USB_HAVE_COMPAT_LINUX
54002ac6454SAndrew Thompson 	/* free Linux compat device, if any */
541760bc48eSAndrew Thompson 	if (udev->linux_endpoint_start) {
542760bc48eSAndrew Thompson 		usb_linux_free_device(udev);
543760bc48eSAndrew Thompson 		udev->linux_endpoint_start = NULL;
54402ac6454SAndrew Thompson 	}
545bdc081c6SAndrew Thompson #endif
54602ac6454SAndrew Thompson 
547a593f6b8SAndrew Thompson 	usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_FREE);
54802ac6454SAndrew Thompson 
549ae60fdfbSAndrew Thompson 	/* free "cdesc" after "ifaces" and "endpoints", if any */
5507efaaa9aSAndrew Thompson 	if (udev->cdesc != NULL) {
551f29a0724SAndrew Thompson 		if (udev->flags.usb_mode != USB_MODE_DEVICE)
5522c79a775SHans Petter Selasky 			usbd_free_config_desc(udev, udev->cdesc);
55302ac6454SAndrew Thompson 		udev->cdesc = NULL;
55402ac6454SAndrew Thompson 	}
55502ac6454SAndrew Thompson 	/* set unconfigured state */
55602ac6454SAndrew Thompson 	udev->curr_config_no = USB_UNCONFIG_NO;
55702ac6454SAndrew Thompson 	udev->curr_config_index = USB_UNCONFIG_INDEX;
558bdd41206SAndrew Thompson 
559cb18f7d1SAlfred Perlstein 	if (do_unlock)
560cb18f7d1SAlfred Perlstein 		usbd_enum_unlock(udev);
56102ac6454SAndrew Thompson }
56202ac6454SAndrew Thompson 
56302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
564a593f6b8SAndrew Thompson  *	usbd_set_config_index
56502ac6454SAndrew Thompson  *
56602ac6454SAndrew Thompson  * This function selects configuration by index, independent of the
56702ac6454SAndrew Thompson  * actual configuration number. This function should not be used by
56802ac6454SAndrew Thompson  * USB drivers.
56902ac6454SAndrew Thompson  *
57002ac6454SAndrew Thompson  * Returns:
57102ac6454SAndrew Thompson  *    0: Success
57202ac6454SAndrew Thompson  * Else: Failure
57302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
574e0a69b51SAndrew Thompson usb_error_t
575a593f6b8SAndrew Thompson usbd_set_config_index(struct usb_device *udev, uint8_t index)
57602ac6454SAndrew Thompson {
577760bc48eSAndrew Thompson 	struct usb_status ds;
578760bc48eSAndrew Thompson 	struct usb_config_descriptor *cdp;
57902ac6454SAndrew Thompson 	uint16_t power;
58002ac6454SAndrew Thompson 	uint16_t max_power;
58102ac6454SAndrew Thompson 	uint8_t selfpowered;
58202ac6454SAndrew Thompson 	uint8_t do_unlock;
583e0a69b51SAndrew Thompson 	usb_error_t err;
58402ac6454SAndrew Thompson 
58502ac6454SAndrew Thompson 	DPRINTFN(6, "udev=%p index=%d\n", udev, index);
58602ac6454SAndrew Thompson 
5876950c75fSHans Petter Selasky 	/* Prevent re-enumeration */
5886950c75fSHans Petter Selasky 	do_unlock = usbd_enum_lock(udev);
58902ac6454SAndrew Thompson 
590d88688c7SAndrew Thompson 	usb_unconfigure(udev, 0);
59102ac6454SAndrew Thompson 
59202ac6454SAndrew Thompson 	if (index == USB_UNCONFIG_INDEX) {
59302ac6454SAndrew Thompson 		/*
59402ac6454SAndrew Thompson 		 * Leave unallocated when unconfiguring the
595a593f6b8SAndrew Thompson 		 * device. "usb_unconfigure()" will also reset
59602ac6454SAndrew Thompson 		 * the current config number and index.
59702ac6454SAndrew Thompson 		 */
598a593f6b8SAndrew Thompson 		err = usbd_req_set_config(udev, NULL, USB_UNCONFIG_NO);
599bd216778SAndrew Thompson 		if (udev->state == USB_STATE_CONFIGURED)
600a593f6b8SAndrew Thompson 			usb_set_device_state(udev, USB_STATE_ADDRESSED);
60102ac6454SAndrew Thompson 		goto done;
60202ac6454SAndrew Thompson 	}
60302ac6454SAndrew Thompson 	/* get the full config descriptor */
604f29a0724SAndrew Thompson 	if (udev->flags.usb_mode == USB_MODE_DEVICE) {
6057efaaa9aSAndrew Thompson 		/* save some memory */
606a593f6b8SAndrew Thompson 		err = usbd_req_get_descriptor_ptr(udev, &cdp,
607459d369eSAndrew Thompson 		    (UDESC_CONFIG << 8) | index);
6087efaaa9aSAndrew Thompson 	} else {
6097efaaa9aSAndrew Thompson 		/* normal request */
610a593f6b8SAndrew Thompson 		err = usbd_req_get_config_desc_full(udev,
6112c79a775SHans Petter Selasky 		    NULL, &cdp, index);
6127efaaa9aSAndrew Thompson 	}
61302ac6454SAndrew Thompson 	if (err) {
61402ac6454SAndrew Thompson 		goto done;
61502ac6454SAndrew Thompson 	}
61602ac6454SAndrew Thompson 	/* set the new config descriptor */
61702ac6454SAndrew Thompson 
61802ac6454SAndrew Thompson 	udev->cdesc = cdp;
61902ac6454SAndrew Thompson 
62002ac6454SAndrew Thompson 	/* Figure out if the device is self or bus powered. */
62102ac6454SAndrew Thompson 	selfpowered = 0;
62202ac6454SAndrew Thompson 	if ((!udev->flags.uq_bus_powered) &&
62302ac6454SAndrew Thompson 	    (cdp->bmAttributes & UC_SELF_POWERED) &&
624f29a0724SAndrew Thompson 	    (udev->flags.usb_mode == USB_MODE_HOST)) {
62502ac6454SAndrew Thompson 		/* May be self powered. */
62602ac6454SAndrew Thompson 		if (cdp->bmAttributes & UC_BUS_POWERED) {
62702ac6454SAndrew Thompson 			/* Must ask device. */
628a593f6b8SAndrew Thompson 			err = usbd_req_get_device_status(udev, NULL, &ds);
62902ac6454SAndrew Thompson 			if (err) {
63002ac6454SAndrew Thompson 				DPRINTFN(0, "could not read "
63102ac6454SAndrew Thompson 				    "device status: %s\n",
632a593f6b8SAndrew Thompson 				    usbd_errstr(err));
63302ac6454SAndrew Thompson 			} else if (UGETW(ds.wStatus) & UDS_SELF_POWERED) {
63402ac6454SAndrew Thompson 				selfpowered = 1;
63502ac6454SAndrew Thompson 			}
63602ac6454SAndrew Thompson 			DPRINTF("status=0x%04x \n",
63702ac6454SAndrew Thompson 				UGETW(ds.wStatus));
63802ac6454SAndrew Thompson 		} else
63902ac6454SAndrew Thompson 			selfpowered = 1;
64002ac6454SAndrew Thompson 	}
64102ac6454SAndrew Thompson 	DPRINTF("udev=%p cdesc=%p (addr %d) cno=%d attr=0x%02x, "
64202ac6454SAndrew Thompson 	    "selfpowered=%d, power=%d\n",
64302ac6454SAndrew Thompson 	    udev, cdp,
64491f72cedSAndrew Thompson 	    udev->address, cdp->bConfigurationValue, cdp->bmAttributes,
64502ac6454SAndrew Thompson 	    selfpowered, cdp->bMaxPower * 2);
64602ac6454SAndrew Thompson 
64702ac6454SAndrew Thompson 	/* Check if we have enough power. */
64802ac6454SAndrew Thompson 	power = cdp->bMaxPower * 2;
64902ac6454SAndrew Thompson 
65002ac6454SAndrew Thompson 	if (udev->parent_hub) {
65102ac6454SAndrew Thompson 		max_power = udev->parent_hub->hub->portpower;
65202ac6454SAndrew Thompson 	} else {
65302ac6454SAndrew Thompson 		max_power = USB_MAX_POWER;
65402ac6454SAndrew Thompson 	}
65502ac6454SAndrew Thompson 
65602ac6454SAndrew Thompson 	if (power > max_power) {
65702ac6454SAndrew Thompson 		DPRINTFN(0, "power exceeded %d > %d\n", power, max_power);
65802ac6454SAndrew Thompson 		err = USB_ERR_NO_POWER;
65902ac6454SAndrew Thompson 		goto done;
66002ac6454SAndrew Thompson 	}
66102ac6454SAndrew Thompson 	/* Only update "self_powered" in USB Host Mode */
662f29a0724SAndrew Thompson 	if (udev->flags.usb_mode == USB_MODE_HOST) {
66302ac6454SAndrew Thompson 		udev->flags.self_powered = selfpowered;
66402ac6454SAndrew Thompson 	}
66502ac6454SAndrew Thompson 	udev->power = power;
66602ac6454SAndrew Thompson 	udev->curr_config_no = cdp->bConfigurationValue;
66702ac6454SAndrew Thompson 	udev->curr_config_index = index;
668a593f6b8SAndrew Thompson 	usb_set_device_state(udev, USB_STATE_CONFIGURED);
66902ac6454SAndrew Thompson 
67002ac6454SAndrew Thompson 	/* Set the actual configuration value. */
671a593f6b8SAndrew Thompson 	err = usbd_req_set_config(udev, NULL, cdp->bConfigurationValue);
67202ac6454SAndrew Thompson 	if (err) {
67302ac6454SAndrew Thompson 		goto done;
67402ac6454SAndrew Thompson 	}
675bdd41206SAndrew Thompson 
676a593f6b8SAndrew Thompson 	err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_ALLOC);
67702ac6454SAndrew Thompson 	if (err) {
67802ac6454SAndrew Thompson 		goto done;
67902ac6454SAndrew Thompson 	}
680bdd41206SAndrew Thompson 
681a593f6b8SAndrew Thompson 	err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_INIT);
682bdd41206SAndrew Thompson 	if (err) {
683bdd41206SAndrew Thompson 		goto done;
68402ac6454SAndrew Thompson 	}
685bdd41206SAndrew Thompson 
6860f6e8f93SAndrew Thompson #if USB_HAVE_UGEN
687ee3e3ff5SAndrew Thompson 	/* create device nodes for each endpoint */
688a593f6b8SAndrew Thompson 	usb_cdev_create(udev);
6890f6e8f93SAndrew Thompson #endif
69002ac6454SAndrew Thompson 
69102ac6454SAndrew Thompson done:
692a593f6b8SAndrew Thompson 	DPRINTF("error=%s\n", usbd_errstr(err));
69302ac6454SAndrew Thompson 	if (err) {
694d88688c7SAndrew Thompson 		usb_unconfigure(udev, 0);
69502ac6454SAndrew Thompson 	}
696cb18f7d1SAlfred Perlstein 	if (do_unlock)
697cb18f7d1SAlfred Perlstein 		usbd_enum_unlock(udev);
69802ac6454SAndrew Thompson 	return (err);
69902ac6454SAndrew Thompson }
70002ac6454SAndrew Thompson 
70102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
702a593f6b8SAndrew Thompson  *	usb_config_parse
703bdd41206SAndrew Thompson  *
704ae60fdfbSAndrew Thompson  * This function will allocate and free USB interfaces and USB endpoints,
705ae60fdfbSAndrew Thompson  * parse the USB configuration structure and initialise the USB endpoints
706bdd41206SAndrew Thompson  * and interfaces. If "iface_index" is not equal to
707bdd41206SAndrew Thompson  * "USB_IFACE_INDEX_ANY" then the "cmd" parameter is the
708bdd41206SAndrew Thompson  * alternate_setting to be selected for the given interface. Else the
709bdd41206SAndrew Thompson  * "cmd" parameter is defined by "USB_CFG_XXX". "iface_index" can be
710bdd41206SAndrew Thompson  * "USB_IFACE_INDEX_ANY" or a valid USB interface index. This function
711bdd41206SAndrew Thompson  * is typically called when setting the configuration or when setting
712bdd41206SAndrew Thompson  * an alternate interface.
713bdd41206SAndrew Thompson  *
714bdd41206SAndrew Thompson  * Returns:
715bdd41206SAndrew Thompson  *    0: Success
716bdd41206SAndrew Thompson  * Else: Failure
717bdd41206SAndrew Thompson  *------------------------------------------------------------------------*/
718963169b4SHans Petter Selasky static usb_error_t
719a593f6b8SAndrew Thompson usb_config_parse(struct usb_device *udev, uint8_t iface_index, uint8_t cmd)
720bdd41206SAndrew Thompson {
721760bc48eSAndrew Thompson 	struct usb_idesc_parse_state ips;
722760bc48eSAndrew Thompson 	struct usb_interface_descriptor *id;
723760bc48eSAndrew Thompson 	struct usb_endpoint_descriptor *ed;
724760bc48eSAndrew Thompson 	struct usb_interface *iface;
725ae60fdfbSAndrew Thompson 	struct usb_endpoint *ep;
726e0a69b51SAndrew Thompson 	usb_error_t err;
727bdd41206SAndrew Thompson 	uint8_t ep_curr;
728bdd41206SAndrew Thompson 	uint8_t ep_max;
729bdd41206SAndrew Thompson 	uint8_t temp;
730bdd41206SAndrew Thompson 	uint8_t do_init;
731bdd41206SAndrew Thompson 	uint8_t alt_index;
732bdd41206SAndrew Thompson 
733bdd41206SAndrew Thompson 	if (iface_index != USB_IFACE_INDEX_ANY) {
734bdd41206SAndrew Thompson 		/* parameter overload */
735bdd41206SAndrew Thompson 		alt_index = cmd;
736bdd41206SAndrew Thompson 		cmd = USB_CFG_INIT;
737bdd41206SAndrew Thompson 	} else {
738bdd41206SAndrew Thompson 		/* not used */
739bdd41206SAndrew Thompson 		alt_index = 0;
740bdd41206SAndrew Thompson 	}
741bdd41206SAndrew Thompson 
742bdd41206SAndrew Thompson 	err = 0;
743bdd41206SAndrew Thompson 
744bdd41206SAndrew Thompson 	DPRINTFN(5, "iface_index=%d cmd=%d\n",
745bdd41206SAndrew Thompson 	    iface_index, cmd);
746bdd41206SAndrew Thompson 
747bdd41206SAndrew Thompson 	if (cmd == USB_CFG_FREE)
748bdd41206SAndrew Thompson 		goto cleanup;
749bdd41206SAndrew Thompson 
750bdd41206SAndrew Thompson 	if (cmd == USB_CFG_INIT) {
75191cd9240SAndrew Thompson 		sx_assert(&udev->enum_sx, SA_LOCKED);
752bdd41206SAndrew Thompson 
753ae60fdfbSAndrew Thompson 		/* check for in-use endpoints */
754bdd41206SAndrew Thompson 
755ae60fdfbSAndrew Thompson 		ep = udev->endpoints;
756ae60fdfbSAndrew Thompson 		ep_max = udev->endpoints_max;
757bdd41206SAndrew Thompson 		while (ep_max--) {
758ae60fdfbSAndrew Thompson 			/* look for matching endpoints */
759bdd41206SAndrew Thompson 			if ((iface_index == USB_IFACE_INDEX_ANY) ||
760ae60fdfbSAndrew Thompson 			    (iface_index == ep->iface_index)) {
761f12c6c29SAndrew Thompson 				if (ep->refcount_alloc != 0) {
762bdd41206SAndrew Thompson 					/*
763bdd41206SAndrew Thompson 					 * This typically indicates a
764bdd41206SAndrew Thompson 					 * more serious error.
765bdd41206SAndrew Thompson 					 */
766bdd41206SAndrew Thompson 					err = USB_ERR_IN_USE;
767bdd41206SAndrew Thompson 				} else {
768ae60fdfbSAndrew Thompson 					/* reset endpoint */
769ae60fdfbSAndrew Thompson 					memset(ep, 0, sizeof(*ep));
770ae60fdfbSAndrew Thompson 					/* make sure we don't zero the endpoint again */
771ae60fdfbSAndrew Thompson 					ep->iface_index = USB_IFACE_INDEX_ANY;
772bdd41206SAndrew Thompson 				}
773bdd41206SAndrew Thompson 			}
774ae60fdfbSAndrew Thompson 			ep++;
775bdd41206SAndrew Thompson 		}
776bdd41206SAndrew Thompson 
777bdd41206SAndrew Thompson 		if (err)
778bdd41206SAndrew Thompson 			return (err);
779bdd41206SAndrew Thompson 	}
780bdd41206SAndrew Thompson 
781bdd41206SAndrew Thompson 	memset(&ips, 0, sizeof(ips));
782bdd41206SAndrew Thompson 
783bdd41206SAndrew Thompson 	ep_curr = 0;
784bdd41206SAndrew Thompson 	ep_max = 0;
785bdd41206SAndrew Thompson 
786a593f6b8SAndrew Thompson 	while ((id = usb_idesc_foreach(udev->cdesc, &ips))) {
787bdd41206SAndrew Thompson 
788bdd41206SAndrew Thompson 		iface = udev->ifaces + ips.iface_index;
789bdd41206SAndrew Thompson 
790bdd41206SAndrew Thompson 		/* check for specific interface match */
791bdd41206SAndrew Thompson 
792bdd41206SAndrew Thompson 		if (cmd == USB_CFG_INIT) {
793bdd41206SAndrew Thompson 			if ((iface_index != USB_IFACE_INDEX_ANY) &&
794bdd41206SAndrew Thompson 			    (iface_index != ips.iface_index)) {
795bdd41206SAndrew Thompson 				/* wrong interface */
796bdd41206SAndrew Thompson 				do_init = 0;
797bdd41206SAndrew Thompson 			} else if (alt_index != ips.iface_index_alt) {
798bdd41206SAndrew Thompson 				/* wrong alternate setting */
799bdd41206SAndrew Thompson 				do_init = 0;
800bdd41206SAndrew Thompson 			} else {
801bdd41206SAndrew Thompson 				/* initialise interface */
802bdd41206SAndrew Thompson 				do_init = 1;
803bdd41206SAndrew Thompson 			}
804bdd41206SAndrew Thompson 		} else
805bdd41206SAndrew Thompson 			do_init = 0;
806bdd41206SAndrew Thompson 
807bdd41206SAndrew Thompson 		/* check for new interface */
808bdd41206SAndrew Thompson 		if (ips.iface_index_alt == 0) {
809bdd41206SAndrew Thompson 			/* update current number of endpoints */
810bdd41206SAndrew Thompson 			ep_curr = ep_max;
811bdd41206SAndrew Thompson 		}
812bdd41206SAndrew Thompson 		/* check for init */
813bdd41206SAndrew Thompson 		if (do_init) {
814bdd41206SAndrew Thompson 			/* setup the USB interface structure */
815bdd41206SAndrew Thompson 			iface->idesc = id;
816bdd41206SAndrew Thompson 			/* set alternate index */
817bdd41206SAndrew Thompson 			iface->alt_index = alt_index;
818d9073c1eSHans Petter Selasky 			/* set default interface parent */
819d9073c1eSHans Petter Selasky 			if (iface_index == USB_IFACE_INDEX_ANY) {
820d9073c1eSHans Petter Selasky 				iface->parent_iface_index =
821d9073c1eSHans Petter Selasky 				    USB_IFACE_INDEX_ANY;
822d9073c1eSHans Petter Selasky 			}
823bdd41206SAndrew Thompson 		}
824bdd41206SAndrew Thompson 
825bdd41206SAndrew Thompson 		DPRINTFN(5, "found idesc nendpt=%d\n", id->bNumEndpoints);
826bdd41206SAndrew Thompson 
827760bc48eSAndrew Thompson 		ed = (struct usb_endpoint_descriptor *)id;
828bdd41206SAndrew Thompson 
829bdd41206SAndrew Thompson 		temp = ep_curr;
830bdd41206SAndrew Thompson 
831bdd41206SAndrew Thompson 		/* iterate all the endpoint descriptors */
832a593f6b8SAndrew Thompson 		while ((ed = usb_edesc_foreach(udev->cdesc, ed))) {
833bdd41206SAndrew Thompson 
8345b0752bbSHans Petter Selasky 			/* check if endpoint limit has been reached */
8355b0752bbSHans Petter Selasky 			if (temp >= USB_MAX_EP_UNITS) {
8365b0752bbSHans Petter Selasky 				DPRINTF("Endpoint limit reached\n");
8375b0752bbSHans Petter Selasky 				break;
8385b0752bbSHans Petter Selasky 			}
839bdd41206SAndrew Thompson 
840ae60fdfbSAndrew Thompson 			ep = udev->endpoints + temp;
841bdd41206SAndrew Thompson 
842bdd41206SAndrew Thompson 			if (do_init) {
843963169b4SHans Petter Selasky 				void *ecomp;
844963169b4SHans Petter Selasky 
845963169b4SHans Petter Selasky 				ecomp = usb_ed_comp_foreach(udev->cdesc, (void *)ed);
846963169b4SHans Petter Selasky 				if (ecomp != NULL)
847963169b4SHans Petter Selasky 					DPRINTFN(5, "Found endpoint companion descriptor\n");
848963169b4SHans Petter Selasky 
849a593f6b8SAndrew Thompson 				usb_init_endpoint(udev,
850963169b4SHans Petter Selasky 				    ips.iface_index, ed, ecomp, ep);
851bdd41206SAndrew Thompson 			}
852bdd41206SAndrew Thompson 
853bdd41206SAndrew Thompson 			temp ++;
854bdd41206SAndrew Thompson 
855bdd41206SAndrew Thompson 			/* find maximum number of endpoints */
856bdd41206SAndrew Thompson 			if (ep_max < temp)
857bdd41206SAndrew Thompson 				ep_max = temp;
858bdd41206SAndrew Thompson 		}
859bdd41206SAndrew Thompson 	}
860bdd41206SAndrew Thompson 
861bdd41206SAndrew Thompson 	/* NOTE: It is valid to have no interfaces and no endpoints! */
862bdd41206SAndrew Thompson 
863bdd41206SAndrew Thompson 	if (cmd == USB_CFG_ALLOC) {
864bdd41206SAndrew Thompson 		udev->ifaces_max = ips.iface_index;
8655b0752bbSHans Petter Selasky #if (USB_HAVE_FIXED_IFACE == 0)
866bdd41206SAndrew Thompson 		udev->ifaces = NULL;
867bdd41206SAndrew Thompson 		if (udev->ifaces_max != 0) {
868bdd41206SAndrew Thompson 			udev->ifaces = malloc(sizeof(*iface) * udev->ifaces_max,
869bdd41206SAndrew Thompson 			        M_USB, M_WAITOK | M_ZERO);
870bdd41206SAndrew Thompson 			if (udev->ifaces == NULL) {
871bdd41206SAndrew Thompson 				err = USB_ERR_NOMEM;
872bdd41206SAndrew Thompson 				goto done;
873bdd41206SAndrew Thompson 			}
874bdd41206SAndrew Thompson 		}
8755b0752bbSHans Petter Selasky #endif
8765b0752bbSHans Petter Selasky #if (USB_HAVE_FIXED_ENDPOINT == 0)
8779469c929SAndrew Thompson 		if (ep_max != 0) {
878ae60fdfbSAndrew Thompson 			udev->endpoints = malloc(sizeof(*ep) * ep_max,
879bdd41206SAndrew Thompson 			        M_USB, M_WAITOK | M_ZERO);
880ae60fdfbSAndrew Thompson 			if (udev->endpoints == NULL) {
881bdd41206SAndrew Thompson 				err = USB_ERR_NOMEM;
882bdd41206SAndrew Thompson 				goto done;
883bdd41206SAndrew Thompson 			}
8849469c929SAndrew Thompson 		} else {
885ae60fdfbSAndrew Thompson 			udev->endpoints = NULL;
886bdd41206SAndrew Thompson 		}
8875b0752bbSHans Petter Selasky #endif
8889469c929SAndrew Thompson 		USB_BUS_LOCK(udev->bus);
889ae60fdfbSAndrew Thompson 		udev->endpoints_max = ep_max;
8909469c929SAndrew Thompson 		/* reset any ongoing clear-stall */
891ae60fdfbSAndrew Thompson 		udev->ep_curr = NULL;
8929469c929SAndrew Thompson 		USB_BUS_UNLOCK(udev->bus);
893bdd41206SAndrew Thompson 	}
8946c218933SHans Petter Selasky #if (USB_HAVE_FIXED_IFACE == 0) || (USB_HAVE_FIXED_ENDPOINT == 0)
895bdd41206SAndrew Thompson done:
8966c218933SHans Petter Selasky #endif
897bdd41206SAndrew Thompson 	if (err) {
898bdd41206SAndrew Thompson 		if (cmd == USB_CFG_ALLOC) {
899bdd41206SAndrew Thompson cleanup:
9009469c929SAndrew Thompson 			USB_BUS_LOCK(udev->bus);
901ae60fdfbSAndrew Thompson 			udev->endpoints_max = 0;
9029469c929SAndrew Thompson 			/* reset any ongoing clear-stall */
903ae60fdfbSAndrew Thompson 			udev->ep_curr = NULL;
9049469c929SAndrew Thompson 			USB_BUS_UNLOCK(udev->bus);
9059469c929SAndrew Thompson 
9065b0752bbSHans Petter Selasky #if (USB_HAVE_FIXED_IFACE == 0)
907bdd41206SAndrew Thompson 			free(udev->ifaces, M_USB);
908bdd41206SAndrew Thompson 			udev->ifaces = NULL;
9095b0752bbSHans Petter Selasky #endif
9105b0752bbSHans Petter Selasky #if (USB_HAVE_FIXED_ENDPOINT == 0)
9115b0752bbSHans Petter Selasky 			free(udev->endpoints, M_USB);
912ae60fdfbSAndrew Thompson 			udev->endpoints = NULL;
9135b0752bbSHans Petter Selasky #endif
914bdd41206SAndrew Thompson 			udev->ifaces_max = 0;
915bdd41206SAndrew Thompson 		}
916bdd41206SAndrew Thompson 	}
917bdd41206SAndrew Thompson 	return (err);
918bdd41206SAndrew Thompson }
919bdd41206SAndrew Thompson 
920bdd41206SAndrew Thompson /*------------------------------------------------------------------------*
921a593f6b8SAndrew Thompson  *	usbd_set_alt_interface_index
92202ac6454SAndrew Thompson  *
92302ac6454SAndrew Thompson  * This function will select an alternate interface index for the
92402ac6454SAndrew Thompson  * given interface index. The interface should not be in use when this
925e2805033SAndrew Thompson  * function is called. That means there should not be any open USB
926e2805033SAndrew Thompson  * transfers. Else an error is returned. If the alternate setting is
927e2805033SAndrew Thompson  * already set this function will simply return success. This function
928e2805033SAndrew Thompson  * is called in Host mode and Device mode!
92902ac6454SAndrew Thompson  *
93002ac6454SAndrew Thompson  * Returns:
93102ac6454SAndrew Thompson  *    0: Success
93202ac6454SAndrew Thompson  * Else: Failure
93302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
934e0a69b51SAndrew Thompson usb_error_t
935a593f6b8SAndrew Thompson usbd_set_alt_interface_index(struct usb_device *udev,
93602ac6454SAndrew Thompson     uint8_t iface_index, uint8_t alt_index)
93702ac6454SAndrew Thompson {
938a593f6b8SAndrew Thompson 	struct usb_interface *iface = usbd_get_iface(udev, iface_index);
939e0a69b51SAndrew Thompson 	usb_error_t err;
94002ac6454SAndrew Thompson 	uint8_t do_unlock;
94102ac6454SAndrew Thompson 
9426950c75fSHans Petter Selasky 	/* Prevent re-enumeration */
9436950c75fSHans Petter Selasky 	do_unlock = usbd_enum_lock(udev);
9446950c75fSHans Petter Selasky 
94502ac6454SAndrew Thompson 	if (iface == NULL) {
94602ac6454SAndrew Thompson 		err = USB_ERR_INVAL;
94702ac6454SAndrew Thompson 		goto done;
94802ac6454SAndrew Thompson 	}
949e2805033SAndrew Thompson 	if (iface->alt_index == alt_index) {
950e2805033SAndrew Thompson 		/*
951e2805033SAndrew Thompson 		 * Optimise away duplicate setting of
952e2805033SAndrew Thompson 		 * alternate setting in USB Host Mode!
953e2805033SAndrew Thompson 		 */
954e2805033SAndrew Thompson 		err = 0;
955e2805033SAndrew Thompson 		goto done;
956e2805033SAndrew Thompson 	}
9570f6e8f93SAndrew Thompson #if USB_HAVE_UGEN
95802ac6454SAndrew Thompson 	/*
95902ac6454SAndrew Thompson 	 * Free all generic FIFOs for this interface, except control
96002ac6454SAndrew Thompson 	 * endpoint FIFOs:
96102ac6454SAndrew Thompson 	 */
962a593f6b8SAndrew Thompson 	usb_fifo_free_wrap(udev, iface_index, 0);
9630f6e8f93SAndrew Thompson #endif
964bdd41206SAndrew Thompson 
965a593f6b8SAndrew Thompson 	err = usb_config_parse(udev, iface_index, alt_index);
96602ac6454SAndrew Thompson 	if (err) {
96702ac6454SAndrew Thompson 		goto done;
96802ac6454SAndrew Thompson 	}
969bd73b187SAlfred Perlstein 	if (iface->alt_index != alt_index) {
970bd73b187SAlfred Perlstein 		/* the alternate setting does not exist */
971bd73b187SAlfred Perlstein 		err = USB_ERR_INVAL;
972bd73b187SAlfred Perlstein 		goto done;
973bd73b187SAlfred Perlstein 	}
974bd73b187SAlfred Perlstein 
975a593f6b8SAndrew Thompson 	err = usbd_req_set_alt_interface_no(udev, NULL, iface_index,
97602ac6454SAndrew Thompson 	    iface->idesc->bAlternateSetting);
97702ac6454SAndrew Thompson 
97802ac6454SAndrew Thompson done:
979cb18f7d1SAlfred Perlstein 	if (do_unlock)
980cb18f7d1SAlfred Perlstein 		usbd_enum_unlock(udev);
98102ac6454SAndrew Thompson 	return (err);
98202ac6454SAndrew Thompson }
98302ac6454SAndrew Thompson 
98402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
985a593f6b8SAndrew Thompson  *	usbd_set_endpoint_stall
98602ac6454SAndrew Thompson  *
987963169b4SHans Petter Selasky  * This function is used to make a BULK or INTERRUPT endpoint send
988963169b4SHans Petter Selasky  * STALL tokens in USB device mode.
98902ac6454SAndrew Thompson  *
99002ac6454SAndrew Thompson  * Returns:
99102ac6454SAndrew Thompson  *    0: Success
99202ac6454SAndrew Thompson  * Else: Failure
99302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
994e0a69b51SAndrew Thompson usb_error_t
995a593f6b8SAndrew Thompson usbd_set_endpoint_stall(struct usb_device *udev, struct usb_endpoint *ep,
99602ac6454SAndrew Thompson     uint8_t do_stall)
99702ac6454SAndrew Thompson {
998760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
999a5cf1aaaSHans Petter Selasky 	usb_stream_t x;
100002ac6454SAndrew Thompson 	uint8_t et;
100102ac6454SAndrew Thompson 	uint8_t was_stalled;
100202ac6454SAndrew Thompson 
1003ae60fdfbSAndrew Thompson 	if (ep == NULL) {
100402ac6454SAndrew Thompson 		/* nothing to do */
100502ac6454SAndrew Thompson 		DPRINTF("Cannot find endpoint\n");
100602ac6454SAndrew Thompson 		/*
100702ac6454SAndrew Thompson 		 * Pretend that the clear or set stall request is
100802ac6454SAndrew Thompson 		 * successful else some USB host stacks can do
100902ac6454SAndrew Thompson 		 * strange things, especially when a control endpoint
101002ac6454SAndrew Thompson 		 * stalls.
101102ac6454SAndrew Thompson 		 */
101202ac6454SAndrew Thompson 		return (0);
101302ac6454SAndrew Thompson 	}
1014ae60fdfbSAndrew Thompson 	et = (ep->edesc->bmAttributes & UE_XFERTYPE);
101502ac6454SAndrew Thompson 
101602ac6454SAndrew Thompson 	if ((et != UE_BULK) &&
101702ac6454SAndrew Thompson 	    (et != UE_INTERRUPT)) {
101802ac6454SAndrew Thompson 		/*
101902ac6454SAndrew Thompson 	         * Should not stall control
102002ac6454SAndrew Thompson 	         * nor isochronous endpoints.
102102ac6454SAndrew Thompson 	         */
102202ac6454SAndrew Thompson 		DPRINTF("Invalid endpoint\n");
102302ac6454SAndrew Thompson 		return (0);
102402ac6454SAndrew Thompson 	}
102502ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
102602ac6454SAndrew Thompson 
102702ac6454SAndrew Thompson 	/* store current stall state */
1028ae60fdfbSAndrew Thompson 	was_stalled = ep->is_stalled;
102902ac6454SAndrew Thompson 
103002ac6454SAndrew Thompson 	/* check for no change */
103102ac6454SAndrew Thompson 	if (was_stalled && do_stall) {
1032ae60fdfbSAndrew Thompson 		/* if the endpoint is already stalled do nothing */
103302ac6454SAndrew Thompson 		USB_BUS_UNLOCK(udev->bus);
103402ac6454SAndrew Thompson 		DPRINTF("No change\n");
103502ac6454SAndrew Thompson 		return (0);
103602ac6454SAndrew Thompson 	}
103702ac6454SAndrew Thompson 	/* set stalled state */
1038ae60fdfbSAndrew Thompson 	ep->is_stalled = 1;
103902ac6454SAndrew Thompson 
104002ac6454SAndrew Thompson 	if (do_stall || (!was_stalled)) {
104102ac6454SAndrew Thompson 		if (!was_stalled) {
1042a5cf1aaaSHans Petter Selasky 			for (x = 0; x != USB_MAX_EP_STREAMS; x++) {
104302ac6454SAndrew Thompson 				/* lookup the current USB transfer, if any */
1044a5cf1aaaSHans Petter Selasky 				xfer = ep->endpoint_q[x].curr;
1045a5cf1aaaSHans Petter Selasky 				if (xfer != NULL) {
104602ac6454SAndrew Thompson 					/*
1047a5cf1aaaSHans Petter Selasky 					 * The "xfer_stall" method
1048a5cf1aaaSHans Petter Selasky 					 * will complete the USB
1049a5cf1aaaSHans Petter Selasky 					 * transfer like in case of a
1050a5cf1aaaSHans Petter Selasky 					 * timeout setting the error
1051a5cf1aaaSHans Petter Selasky 					 * code "USB_ERR_STALLED".
105202ac6454SAndrew Thompson 					 */
1053a5cf1aaaSHans Petter Selasky 					(udev->bus->methods->xfer_stall) (xfer);
1054a5cf1aaaSHans Petter Selasky 				}
1055a5cf1aaaSHans Petter Selasky 			}
1056a5cf1aaaSHans Petter Selasky 		}
1057a5cf1aaaSHans Petter Selasky 		(udev->bus->methods->set_stall) (udev, ep, &do_stall);
105802ac6454SAndrew Thompson 	}
105902ac6454SAndrew Thompson 	if (!do_stall) {
1060ae60fdfbSAndrew Thompson 		ep->toggle_next = 0;	/* reset data toggle */
1061ae60fdfbSAndrew Thompson 		ep->is_stalled = 0;	/* clear stalled state */
106202ac6454SAndrew Thompson 
1063ae60fdfbSAndrew Thompson 		(udev->bus->methods->clear_stall) (udev, ep);
106402ac6454SAndrew Thompson 
1065a5cf1aaaSHans Petter Selasky 		/* start the current or next transfer, if any */
1066a5cf1aaaSHans Petter Selasky 		for (x = 0; x != USB_MAX_EP_STREAMS; x++) {
1067a5cf1aaaSHans Petter Selasky 			usb_command_wrapper(&ep->endpoint_q[x],
1068a5cf1aaaSHans Petter Selasky 			    ep->endpoint_q[x].curr);
1069a5cf1aaaSHans Petter Selasky 		}
107002ac6454SAndrew Thompson 	}
107102ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
107202ac6454SAndrew Thompson 	return (0);
107302ac6454SAndrew Thompson }
107402ac6454SAndrew Thompson 
107502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1076a593f6b8SAndrew Thompson  *	usb_reset_iface_endpoints - used in USB device side mode
107702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1078e0a69b51SAndrew Thompson usb_error_t
1079a593f6b8SAndrew Thompson usb_reset_iface_endpoints(struct usb_device *udev, uint8_t iface_index)
108002ac6454SAndrew Thompson {
1081ae60fdfbSAndrew Thompson 	struct usb_endpoint *ep;
1082ae60fdfbSAndrew Thompson 	struct usb_endpoint *ep_end;
108302ac6454SAndrew Thompson 
1084ae60fdfbSAndrew Thompson 	ep = udev->endpoints;
1085ae60fdfbSAndrew Thompson 	ep_end = udev->endpoints + udev->endpoints_max;
108602ac6454SAndrew Thompson 
1087ae60fdfbSAndrew Thompson 	for (; ep != ep_end; ep++) {
108802ac6454SAndrew Thompson 
1089ae60fdfbSAndrew Thompson 		if ((ep->edesc == NULL) ||
1090ae60fdfbSAndrew Thompson 		    (ep->iface_index != iface_index)) {
109102ac6454SAndrew Thompson 			continue;
109202ac6454SAndrew Thompson 		}
109302ac6454SAndrew Thompson 		/* simulate a clear stall from the peer */
1094bd73b187SAlfred Perlstein 		usbd_set_endpoint_stall(udev, ep, 0);
109502ac6454SAndrew Thompson 	}
109602ac6454SAndrew Thompson 	return (0);
109702ac6454SAndrew Thompson }
109802ac6454SAndrew Thompson 
109902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1100a593f6b8SAndrew Thompson  *	usb_detach_device_sub
110102ac6454SAndrew Thompson  *
110202ac6454SAndrew Thompson  * This function will try to detach an USB device. If it fails a panic
110302ac6454SAndrew Thompson  * will result.
1104bdd41206SAndrew Thompson  *
1105bdd41206SAndrew Thompson  * Flag values, see "USB_UNCFG_FLAG_XXX".
110602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
110702ac6454SAndrew Thompson static void
1108a593f6b8SAndrew Thompson usb_detach_device_sub(struct usb_device *udev, device_t *ppdev,
11098427ed84SHans Petter Selasky     char **ppnpinfo, uint8_t flag)
111002ac6454SAndrew Thompson {
111102ac6454SAndrew Thompson 	device_t dev;
11128427ed84SHans Petter Selasky 	char *pnpinfo;
111302ac6454SAndrew Thompson 	int err;
111402ac6454SAndrew Thompson 
1115d88688c7SAndrew Thompson 	dev = *ppdev;
1116d88688c7SAndrew Thompson 	if (dev) {
111702ac6454SAndrew Thompson 		/*
111802ac6454SAndrew Thompson 		 * NOTE: It is important to clear "*ppdev" before deleting
111902ac6454SAndrew Thompson 		 * the child due to some device methods being called late
112002ac6454SAndrew Thompson 		 * during the delete process !
112102ac6454SAndrew Thompson 		 */
112202ac6454SAndrew Thompson 		*ppdev = NULL;
112302ac6454SAndrew Thompson 
1124cabe79d9SMarius Strobl 		if (!rebooting) {
112502ac6454SAndrew Thompson 			device_printf(dev, "at %s, port %d, addr %d "
112602ac6454SAndrew Thompson 			    "(disconnected)\n",
112702ac6454SAndrew Thompson 			    device_get_nameunit(udev->parent_dev),
112802ac6454SAndrew Thompson 			    udev->port_no, udev->address);
1129cabe79d9SMarius Strobl 		}
113002ac6454SAndrew Thompson 
113102ac6454SAndrew Thompson 		if (device_is_attached(dev)) {
1132ec8f3127SAndrew Thompson 			if (udev->flags.peer_suspended) {
113302ac6454SAndrew Thompson 				err = DEVICE_RESUME(dev);
113402ac6454SAndrew Thompson 				if (err) {
1135767cb2e2SAndrew Thompson 					device_printf(dev, "Resume failed\n");
113602ac6454SAndrew Thompson 				}
113702ac6454SAndrew Thompson 			}
113802ac6454SAndrew Thompson 			if (device_detach(dev)) {
113902ac6454SAndrew Thompson 				goto error;
114002ac6454SAndrew Thompson 			}
114102ac6454SAndrew Thompson 		}
114202ac6454SAndrew Thompson 		if (device_delete_child(udev->parent_dev, dev)) {
114302ac6454SAndrew Thompson 			goto error;
114402ac6454SAndrew Thompson 		}
114502ac6454SAndrew Thompson 	}
11468427ed84SHans Petter Selasky 
11478427ed84SHans Petter Selasky 	pnpinfo = *ppnpinfo;
11488427ed84SHans Petter Selasky 	if (pnpinfo != NULL) {
11498427ed84SHans Petter Selasky 		*ppnpinfo = NULL;
11508427ed84SHans Petter Selasky 		free(pnpinfo, M_USBDEV);
11518427ed84SHans Petter Selasky 	}
115202ac6454SAndrew Thompson 	return;
115302ac6454SAndrew Thompson 
115402ac6454SAndrew Thompson error:
115502ac6454SAndrew Thompson 	/* Detach is not allowed to fail in the USB world */
11568427ed84SHans Petter Selasky 	panic("usb_detach_device_sub: A USB driver would not detach\n");
115702ac6454SAndrew Thompson }
115802ac6454SAndrew Thompson 
115902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1160a593f6b8SAndrew Thompson  *	usb_detach_device
116102ac6454SAndrew Thompson  *
116202ac6454SAndrew Thompson  * The following function will detach the matching interfaces.
116302ac6454SAndrew Thompson  * This function is NULL safe.
1164bdd41206SAndrew Thompson  *
1165bdd41206SAndrew Thompson  * Flag values, see "USB_UNCFG_FLAG_XXX".
116602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
116702ac6454SAndrew Thompson void
1168a593f6b8SAndrew Thompson usb_detach_device(struct usb_device *udev, uint8_t iface_index,
1169bdd41206SAndrew Thompson     uint8_t flag)
117002ac6454SAndrew Thompson {
1171760bc48eSAndrew Thompson 	struct usb_interface *iface;
117202ac6454SAndrew Thompson 	uint8_t i;
117302ac6454SAndrew Thompson 
117402ac6454SAndrew Thompson 	if (udev == NULL) {
117502ac6454SAndrew Thompson 		/* nothing to do */
117602ac6454SAndrew Thompson 		return;
117702ac6454SAndrew Thompson 	}
117802ac6454SAndrew Thompson 	DPRINTFN(4, "udev=%p\n", udev);
117902ac6454SAndrew Thompson 
118091cd9240SAndrew Thompson 	sx_assert(&udev->enum_sx, SA_LOCKED);
118102ac6454SAndrew Thompson 
1182d008478eSHans Petter Selasky 	/* wait for pending refs to go away */
1183d008478eSHans Petter Selasky 	usb_wait_pending_ref_locked(udev);
1184d008478eSHans Petter Selasky 
118502ac6454SAndrew Thompson 	/*
118602ac6454SAndrew Thompson 	 * First detach the child to give the child's detach routine a
118702ac6454SAndrew Thompson 	 * chance to detach the sub-devices in the correct order.
118802ac6454SAndrew Thompson 	 * Then delete the child using "device_delete_child()" which
118902ac6454SAndrew Thompson 	 * will detach all sub-devices from the bottom and upwards!
119002ac6454SAndrew Thompson 	 */
119102ac6454SAndrew Thompson 	if (iface_index != USB_IFACE_INDEX_ANY) {
119202ac6454SAndrew Thompson 		i = iface_index;
119302ac6454SAndrew Thompson 		iface_index = i + 1;
119402ac6454SAndrew Thompson 	} else {
119502ac6454SAndrew Thompson 		i = 0;
119602ac6454SAndrew Thompson 		iface_index = USB_IFACE_MAX;
119702ac6454SAndrew Thompson 	}
119802ac6454SAndrew Thompson 
119902ac6454SAndrew Thompson 	/* do the detach */
120002ac6454SAndrew Thompson 
120102ac6454SAndrew Thompson 	for (; i != iface_index; i++) {
120202ac6454SAndrew Thompson 
1203a593f6b8SAndrew Thompson 		iface = usbd_get_iface(udev, i);
120402ac6454SAndrew Thompson 		if (iface == NULL) {
120502ac6454SAndrew Thompson 			/* looks like the end of the USB interfaces */
120602ac6454SAndrew Thompson 			break;
120702ac6454SAndrew Thompson 		}
12088427ed84SHans Petter Selasky 		usb_detach_device_sub(udev, &iface->subdev,
12098427ed84SHans Petter Selasky 		    &iface->pnpinfo, flag);
121002ac6454SAndrew Thompson 	}
1211d008478eSHans Petter Selasky 
1212d008478eSHans Petter Selasky 	usb_ref_restore_locked(udev);
121302ac6454SAndrew Thompson }
121402ac6454SAndrew Thompson 
121502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1216a593f6b8SAndrew Thompson  *	usb_probe_and_attach_sub
121702ac6454SAndrew Thompson  *
121802ac6454SAndrew Thompson  * Returns:
121902ac6454SAndrew Thompson  *    0: Success
122002ac6454SAndrew Thompson  * Else: Failure
122102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
122202ac6454SAndrew Thompson static uint8_t
1223a593f6b8SAndrew Thompson usb_probe_and_attach_sub(struct usb_device *udev,
1224760bc48eSAndrew Thompson     struct usb_attach_arg *uaa)
122502ac6454SAndrew Thompson {
1226760bc48eSAndrew Thompson 	struct usb_interface *iface;
122702ac6454SAndrew Thompson 	device_t dev;
122802ac6454SAndrew Thompson 	int err;
122902ac6454SAndrew Thompson 
123002ac6454SAndrew Thompson 	iface = uaa->iface;
123102ac6454SAndrew Thompson 	if (iface->parent_iface_index != USB_IFACE_INDEX_ANY) {
123202ac6454SAndrew Thompson 		/* leave interface alone */
123302ac6454SAndrew Thompson 		return (0);
123402ac6454SAndrew Thompson 	}
123502ac6454SAndrew Thompson 	dev = iface->subdev;
123602ac6454SAndrew Thompson 	if (dev) {
123702ac6454SAndrew Thompson 
123802ac6454SAndrew Thompson 		/* clean up after module unload */
123902ac6454SAndrew Thompson 
124002ac6454SAndrew Thompson 		if (device_is_attached(dev)) {
124102ac6454SAndrew Thompson 			/* already a device there */
124202ac6454SAndrew Thompson 			return (0);
124302ac6454SAndrew Thompson 		}
124402ac6454SAndrew Thompson 		/* clear "iface->subdev" as early as possible */
124502ac6454SAndrew Thompson 
124602ac6454SAndrew Thompson 		iface->subdev = NULL;
124702ac6454SAndrew Thompson 
124802ac6454SAndrew Thompson 		if (device_delete_child(udev->parent_dev, dev)) {
124902ac6454SAndrew Thompson 
125002ac6454SAndrew Thompson 			/*
125102ac6454SAndrew Thompson 			 * Panic here, else one can get a double call
125202ac6454SAndrew Thompson 			 * to device_detach().  USB devices should
125302ac6454SAndrew Thompson 			 * never fail on detach!
125402ac6454SAndrew Thompson 			 */
1255767cb2e2SAndrew Thompson 			panic("device_delete_child() failed\n");
125602ac6454SAndrew Thompson 		}
125702ac6454SAndrew Thompson 	}
125802ac6454SAndrew Thompson 	if (uaa->temp_dev == NULL) {
125902ac6454SAndrew Thompson 
126002ac6454SAndrew Thompson 		/* create a new child */
126102ac6454SAndrew Thompson 		uaa->temp_dev = device_add_child(udev->parent_dev, NULL, -1);
126202ac6454SAndrew Thompson 		if (uaa->temp_dev == NULL) {
126302ac6454SAndrew Thompson 			device_printf(udev->parent_dev,
1264767cb2e2SAndrew Thompson 			    "Device creation failed\n");
126502ac6454SAndrew Thompson 			return (1);	/* failure */
126602ac6454SAndrew Thompson 		}
126702ac6454SAndrew Thompson 		device_set_ivars(uaa->temp_dev, uaa);
126802ac6454SAndrew Thompson 		device_quiet(uaa->temp_dev);
126902ac6454SAndrew Thompson 	}
127002ac6454SAndrew Thompson 	/*
127102ac6454SAndrew Thompson 	 * Set "subdev" before probe and attach so that "devd" gets
127202ac6454SAndrew Thompson 	 * the information it needs.
127302ac6454SAndrew Thompson 	 */
127402ac6454SAndrew Thompson 	iface->subdev = uaa->temp_dev;
127502ac6454SAndrew Thompson 
127602ac6454SAndrew Thompson 	if (device_probe_and_attach(iface->subdev) == 0) {
127702ac6454SAndrew Thompson 		/*
127802ac6454SAndrew Thompson 		 * The USB attach arguments are only available during probe
127902ac6454SAndrew Thompson 		 * and attach !
128002ac6454SAndrew Thompson 		 */
128102ac6454SAndrew Thompson 		uaa->temp_dev = NULL;
128202ac6454SAndrew Thompson 		device_set_ivars(iface->subdev, NULL);
128302ac6454SAndrew Thompson 
1284ec8f3127SAndrew Thompson 		if (udev->flags.peer_suspended) {
128502ac6454SAndrew Thompson 			err = DEVICE_SUSPEND(iface->subdev);
128688b4e0abSAndrew Thompson 			if (err)
128702ac6454SAndrew Thompson 				device_printf(iface->subdev, "Suspend failed\n");
128802ac6454SAndrew Thompson 		}
128902ac6454SAndrew Thompson 		return (0);		/* success */
129002ac6454SAndrew Thompson 	} else {
129102ac6454SAndrew Thompson 		/* No USB driver found */
129202ac6454SAndrew Thompson 		iface->subdev = NULL;
129302ac6454SAndrew Thompson 	}
129402ac6454SAndrew Thompson 	return (1);			/* failure */
129502ac6454SAndrew Thompson }
129602ac6454SAndrew Thompson 
129702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1298a593f6b8SAndrew Thompson  *	usbd_set_parent_iface
129902ac6454SAndrew Thompson  *
130002ac6454SAndrew Thompson  * Using this function will lock the alternate interface setting on an
130102ac6454SAndrew Thompson  * interface. It is typically used for multi interface drivers. In USB
130202ac6454SAndrew Thompson  * device side mode it is assumed that the alternate interfaces all
130302ac6454SAndrew Thompson  * have the same endpoint descriptors. The default parent index value
130402ac6454SAndrew Thompson  * is "USB_IFACE_INDEX_ANY". Then the alternate setting value is not
130502ac6454SAndrew Thompson  * locked.
130602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
130702ac6454SAndrew Thompson void
1308a593f6b8SAndrew Thompson usbd_set_parent_iface(struct usb_device *udev, uint8_t iface_index,
130902ac6454SAndrew Thompson     uint8_t parent_index)
131002ac6454SAndrew Thompson {
1311760bc48eSAndrew Thompson 	struct usb_interface *iface;
131202ac6454SAndrew Thompson 
1313d9073c1eSHans Petter Selasky 	if (udev == NULL) {
1314d9073c1eSHans Petter Selasky 		/* nothing to do */
1315d9073c1eSHans Petter Selasky 		return;
131602ac6454SAndrew Thompson 	}
1317d9073c1eSHans Petter Selasky 	iface = usbd_get_iface(udev, iface_index);
1318d9073c1eSHans Petter Selasky 	if (iface != NULL)
1319d9073c1eSHans Petter Selasky 		iface->parent_iface_index = parent_index;
132002ac6454SAndrew Thompson }
132102ac6454SAndrew Thompson 
132202ac6454SAndrew Thompson static void
1323a593f6b8SAndrew Thompson usb_init_attach_arg(struct usb_device *udev,
1324760bc48eSAndrew Thompson     struct usb_attach_arg *uaa)
132502ac6454SAndrew Thompson {
1326d46dc4adSHans Petter Selasky 	memset(uaa, 0, sizeof(*uaa));
132702ac6454SAndrew Thompson 
132802ac6454SAndrew Thompson 	uaa->device = udev;
1329f29a0724SAndrew Thompson 	uaa->usb_mode = udev->flags.usb_mode;
133002ac6454SAndrew Thompson 	uaa->port = udev->port_no;
13312a4c6157SAndrew Thompson 	uaa->dev_state = UAA_DEV_READY;
133202ac6454SAndrew Thompson 
133302ac6454SAndrew Thompson 	uaa->info.idVendor = UGETW(udev->ddesc.idVendor);
133402ac6454SAndrew Thompson 	uaa->info.idProduct = UGETW(udev->ddesc.idProduct);
133502ac6454SAndrew Thompson 	uaa->info.bcdDevice = UGETW(udev->ddesc.bcdDevice);
133602ac6454SAndrew Thompson 	uaa->info.bDeviceClass = udev->ddesc.bDeviceClass;
133702ac6454SAndrew Thompson 	uaa->info.bDeviceSubClass = udev->ddesc.bDeviceSubClass;
133802ac6454SAndrew Thompson 	uaa->info.bDeviceProtocol = udev->ddesc.bDeviceProtocol;
133902ac6454SAndrew Thompson 	uaa->info.bConfigIndex = udev->curr_config_index;
134002ac6454SAndrew Thompson 	uaa->info.bConfigNum = udev->curr_config_no;
134102ac6454SAndrew Thompson }
134202ac6454SAndrew Thompson 
134302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1344a593f6b8SAndrew Thompson  *	usb_probe_and_attach
134502ac6454SAndrew Thompson  *
134602ac6454SAndrew Thompson  * This function is called from "uhub_explore_sub()",
1347a593f6b8SAndrew Thompson  * "usb_handle_set_config()" and "usb_handle_request()".
134802ac6454SAndrew Thompson  *
134902ac6454SAndrew Thompson  * Returns:
135002ac6454SAndrew Thompson  *    0: Success
135102ac6454SAndrew Thompson  * Else: A control transfer failed
135202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1353e0a69b51SAndrew Thompson usb_error_t
1354a593f6b8SAndrew Thompson usb_probe_and_attach(struct usb_device *udev, uint8_t iface_index)
135502ac6454SAndrew Thompson {
1356760bc48eSAndrew Thompson 	struct usb_attach_arg uaa;
1357760bc48eSAndrew Thompson 	struct usb_interface *iface;
135802ac6454SAndrew Thompson 	uint8_t i;
135902ac6454SAndrew Thompson 	uint8_t j;
136002ac6454SAndrew Thompson 	uint8_t do_unlock;
136102ac6454SAndrew Thompson 
136202ac6454SAndrew Thompson 	if (udev == NULL) {
136302ac6454SAndrew Thompson 		DPRINTF("udev == NULL\n");
136402ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
136502ac6454SAndrew Thompson 	}
13666950c75fSHans Petter Selasky 	/* Prevent re-enumeration */
13676950c75fSHans Petter Selasky 	do_unlock = usbd_enum_lock(udev);
136802ac6454SAndrew Thompson 
136902ac6454SAndrew Thompson 	if (udev->curr_config_index == USB_UNCONFIG_INDEX) {
137002ac6454SAndrew Thompson 		/* do nothing - no configuration has been set */
137102ac6454SAndrew Thompson 		goto done;
137202ac6454SAndrew Thompson 	}
137302ac6454SAndrew Thompson 	/* setup USB attach arguments */
137402ac6454SAndrew Thompson 
1375a593f6b8SAndrew Thompson 	usb_init_attach_arg(udev, &uaa);
137602ac6454SAndrew Thompson 
1377a6b60150SHans Petter Selasky 	/*
1378a6b60150SHans Petter Selasky 	 * If the whole USB device is targeted, invoke the USB event
1379a6b60150SHans Petter Selasky 	 * handler(s):
1380a6b60150SHans Petter Selasky 	 */
1381a6b60150SHans Petter Selasky 	if (iface_index == USB_IFACE_INDEX_ANY) {
1382a6b60150SHans Petter Selasky 
1383a6b60150SHans Petter Selasky 		EVENTHANDLER_INVOKE(usb_dev_configured, udev, &uaa);
1384a6b60150SHans Petter Selasky 
1385a6b60150SHans Petter Selasky 		if (uaa.dev_state != UAA_DEV_READY) {
1386a6b60150SHans Petter Selasky 			/* leave device unconfigured */
1387a6b60150SHans Petter Selasky 			usb_unconfigure(udev, 0);
1388a6b60150SHans Petter Selasky 			goto done;
1389a6b60150SHans Petter Selasky 		}
1390a6b60150SHans Petter Selasky 	}
1391a6b60150SHans Petter Selasky 
139202ac6454SAndrew Thompson 	/* Check if only one interface should be probed: */
139302ac6454SAndrew Thompson 	if (iface_index != USB_IFACE_INDEX_ANY) {
139402ac6454SAndrew Thompson 		i = iface_index;
139502ac6454SAndrew Thompson 		j = i + 1;
139602ac6454SAndrew Thompson 	} else {
139702ac6454SAndrew Thompson 		i = 0;
139802ac6454SAndrew Thompson 		j = USB_IFACE_MAX;
139902ac6454SAndrew Thompson 	}
140002ac6454SAndrew Thompson 
140102ac6454SAndrew Thompson 	/* Do the probe and attach */
140202ac6454SAndrew Thompson 	for (; i != j; i++) {
140302ac6454SAndrew Thompson 
1404a593f6b8SAndrew Thompson 		iface = usbd_get_iface(udev, i);
140502ac6454SAndrew Thompson 		if (iface == NULL) {
140602ac6454SAndrew Thompson 			/*
140702ac6454SAndrew Thompson 			 * Looks like the end of the USB
140802ac6454SAndrew Thompson 			 * interfaces !
140902ac6454SAndrew Thompson 			 */
141002ac6454SAndrew Thompson 			DPRINTFN(2, "end of interfaces "
141102ac6454SAndrew Thompson 			    "at %u\n", i);
141202ac6454SAndrew Thompson 			break;
141302ac6454SAndrew Thompson 		}
141402ac6454SAndrew Thompson 		if (iface->idesc == NULL) {
141502ac6454SAndrew Thompson 			/* no interface descriptor */
141602ac6454SAndrew Thompson 			continue;
141702ac6454SAndrew Thompson 		}
141802ac6454SAndrew Thompson 		uaa.iface = iface;
141902ac6454SAndrew Thompson 
142002ac6454SAndrew Thompson 		uaa.info.bInterfaceClass =
142102ac6454SAndrew Thompson 		    iface->idesc->bInterfaceClass;
142202ac6454SAndrew Thompson 		uaa.info.bInterfaceSubClass =
142302ac6454SAndrew Thompson 		    iface->idesc->bInterfaceSubClass;
142402ac6454SAndrew Thompson 		uaa.info.bInterfaceProtocol =
142502ac6454SAndrew Thompson 		    iface->idesc->bInterfaceProtocol;
142602ac6454SAndrew Thompson 		uaa.info.bIfaceIndex = i;
142702ac6454SAndrew Thompson 		uaa.info.bIfaceNum =
142802ac6454SAndrew Thompson 		    iface->idesc->bInterfaceNumber;
1429bd73b187SAlfred Perlstein 		uaa.driver_info = 0;	/* reset driver_info */
143002ac6454SAndrew Thompson 
143102ac6454SAndrew Thompson 		DPRINTFN(2, "iclass=%u/%u/%u iindex=%u/%u\n",
143202ac6454SAndrew Thompson 		    uaa.info.bInterfaceClass,
143302ac6454SAndrew Thompson 		    uaa.info.bInterfaceSubClass,
143402ac6454SAndrew Thompson 		    uaa.info.bInterfaceProtocol,
143502ac6454SAndrew Thompson 		    uaa.info.bIfaceIndex,
143602ac6454SAndrew Thompson 		    uaa.info.bIfaceNum);
143702ac6454SAndrew Thompson 
1438d43ffe94SHans Petter Selasky 		usb_probe_and_attach_sub(udev, &uaa);
143902ac6454SAndrew Thompson 
1440d43ffe94SHans Petter Selasky 		/*
1441d43ffe94SHans Petter Selasky 		 * Remove the leftover child, if any, to enforce that
1442d43ffe94SHans Petter Selasky 		 * a new nomatch devd event is generated for the next
1443d43ffe94SHans Petter Selasky 		 * interface if no driver is found:
1444d43ffe94SHans Petter Selasky 		 */
1445d43ffe94SHans Petter Selasky 		if (uaa.temp_dev == NULL)
1446d43ffe94SHans Petter Selasky 			continue;
1447d43ffe94SHans Petter Selasky 		if (device_delete_child(udev->parent_dev, uaa.temp_dev))
1448767cb2e2SAndrew Thompson 			DPRINTFN(0, "device delete child failed\n");
1449d43ffe94SHans Petter Selasky 		uaa.temp_dev = NULL;
145002ac6454SAndrew Thompson 	}
145102ac6454SAndrew Thompson done:
1452cb18f7d1SAlfred Perlstein 	if (do_unlock)
1453cb18f7d1SAlfred Perlstein 		usbd_enum_unlock(udev);
145402ac6454SAndrew Thompson 	return (0);
145502ac6454SAndrew Thompson }
145602ac6454SAndrew Thompson 
145702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1458a593f6b8SAndrew Thompson  *	usb_suspend_resume_sub
145902ac6454SAndrew Thompson  *
146002ac6454SAndrew Thompson  * This function is called when the suspend or resume methods should
146102ac6454SAndrew Thompson  * be executed on an USB device.
146202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
146302ac6454SAndrew Thompson static void
1464a593f6b8SAndrew Thompson usb_suspend_resume_sub(struct usb_device *udev, device_t dev, uint8_t do_suspend)
146502ac6454SAndrew Thompson {
146602ac6454SAndrew Thompson 	int err;
146702ac6454SAndrew Thompson 
146802ac6454SAndrew Thompson 	if (dev == NULL) {
146902ac6454SAndrew Thompson 		return;
147002ac6454SAndrew Thompson 	}
147102ac6454SAndrew Thompson 	if (!device_is_attached(dev)) {
147202ac6454SAndrew Thompson 		return;
147302ac6454SAndrew Thompson 	}
147402ac6454SAndrew Thompson 	if (do_suspend) {
147502ac6454SAndrew Thompson 		err = DEVICE_SUSPEND(dev);
147602ac6454SAndrew Thompson 	} else {
147702ac6454SAndrew Thompson 		err = DEVICE_RESUME(dev);
147802ac6454SAndrew Thompson 	}
147902ac6454SAndrew Thompson 	if (err) {
1480767cb2e2SAndrew Thompson 		device_printf(dev, "%s failed\n",
148102ac6454SAndrew Thompson 		    do_suspend ? "Suspend" : "Resume");
148202ac6454SAndrew Thompson 	}
148302ac6454SAndrew Thompson }
148402ac6454SAndrew Thompson 
148502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1486a593f6b8SAndrew Thompson  *	usb_suspend_resume
148702ac6454SAndrew Thompson  *
148802ac6454SAndrew Thompson  * The following function will suspend or resume the USB device.
148902ac6454SAndrew Thompson  *
149002ac6454SAndrew Thompson  * Returns:
149102ac6454SAndrew Thompson  *    0: Success
149202ac6454SAndrew Thompson  * Else: Failure
149302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1494e0a69b51SAndrew Thompson usb_error_t
1495a593f6b8SAndrew Thompson usb_suspend_resume(struct usb_device *udev, uint8_t do_suspend)
149602ac6454SAndrew Thompson {
1497760bc48eSAndrew Thompson 	struct usb_interface *iface;
149802ac6454SAndrew Thompson 	uint8_t i;
149902ac6454SAndrew Thompson 
150002ac6454SAndrew Thompson 	if (udev == NULL) {
150102ac6454SAndrew Thompson 		/* nothing to do */
150202ac6454SAndrew Thompson 		return (0);
150302ac6454SAndrew Thompson 	}
150402ac6454SAndrew Thompson 	DPRINTFN(4, "udev=%p do_suspend=%d\n", udev, do_suspend);
150502ac6454SAndrew Thompson 
15062df1e9a6SAndrew Thompson 	sx_assert(&udev->sr_sx, SA_LOCKED);
150702ac6454SAndrew Thompson 
150802ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
150902ac6454SAndrew Thompson 	/* filter the suspend events */
1510ec8f3127SAndrew Thompson 	if (udev->flags.peer_suspended == do_suspend) {
151102ac6454SAndrew Thompson 		USB_BUS_UNLOCK(udev->bus);
151202ac6454SAndrew Thompson 		/* nothing to do */
151302ac6454SAndrew Thompson 		return (0);
151402ac6454SAndrew Thompson 	}
1515ec8f3127SAndrew Thompson 	udev->flags.peer_suspended = do_suspend;
151602ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
151702ac6454SAndrew Thompson 
151802ac6454SAndrew Thompson 	/* do the suspend or resume */
151902ac6454SAndrew Thompson 
152002ac6454SAndrew Thompson 	for (i = 0; i != USB_IFACE_MAX; i++) {
152102ac6454SAndrew Thompson 
1522a593f6b8SAndrew Thompson 		iface = usbd_get_iface(udev, i);
152302ac6454SAndrew Thompson 		if (iface == NULL) {
152402ac6454SAndrew Thompson 			/* looks like the end of the USB interfaces */
152502ac6454SAndrew Thompson 			break;
152602ac6454SAndrew Thompson 		}
1527a593f6b8SAndrew Thompson 		usb_suspend_resume_sub(udev, iface->subdev, do_suspend);
152802ac6454SAndrew Thompson 	}
152902ac6454SAndrew Thompson 	return (0);
153002ac6454SAndrew Thompson }
153102ac6454SAndrew Thompson 
153202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1533a593f6b8SAndrew Thompson  *      usbd_clear_stall_proc
153402ac6454SAndrew Thompson  *
153502ac6454SAndrew Thompson  * This function performs generic USB clear stall operations.
153602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
153702ac6454SAndrew Thompson static void
1538a593f6b8SAndrew Thompson usbd_clear_stall_proc(struct usb_proc_msg *_pm)
153902ac6454SAndrew Thompson {
1540a0d53e0bSHans Petter Selasky 	struct usb_udev_msg *pm = (void *)_pm;
1541760bc48eSAndrew Thompson 	struct usb_device *udev = pm->udev;
154202ac6454SAndrew Thompson 
154302ac6454SAndrew Thompson 	/* Change lock */
154402ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
154591cd9240SAndrew Thompson 	mtx_lock(&udev->device_mtx);
154602ac6454SAndrew Thompson 
154702ac6454SAndrew Thompson 	/* Start clear stall callback */
15485b3bb704SAndrew Thompson 	usbd_transfer_start(udev->ctrl_xfer[1]);
154902ac6454SAndrew Thompson 
155002ac6454SAndrew Thompson 	/* Change lock */
155191cd9240SAndrew Thompson 	mtx_unlock(&udev->device_mtx);
155202ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
155302ac6454SAndrew Thompson }
155402ac6454SAndrew Thompson 
155502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1556a593f6b8SAndrew Thompson  *	usb_alloc_device
155702ac6454SAndrew Thompson  *
155802ac6454SAndrew Thompson  * This function allocates a new USB device. This function is called
155902ac6454SAndrew Thompson  * when a new device has been put in the powered state, but not yet in
156002ac6454SAndrew Thompson  * the addressed state. Get initial descriptor, set the address, get
156102ac6454SAndrew Thompson  * full descriptor and get strings.
156202ac6454SAndrew Thompson  *
156302ac6454SAndrew Thompson  * Return values:
156402ac6454SAndrew Thompson  *    0: Failure
156502ac6454SAndrew Thompson  * Else: Success
156602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1567760bc48eSAndrew Thompson struct usb_device *
1568a593f6b8SAndrew Thompson usb_alloc_device(device_t parent_dev, struct usb_bus *bus,
1569760bc48eSAndrew Thompson     struct usb_device *parent_hub, uint8_t depth, uint8_t port_index,
15708d2dd5ddSAndrew Thompson     uint8_t port_no, enum usb_dev_speed speed, enum usb_hc_mode mode)
157102ac6454SAndrew Thompson {
1572760bc48eSAndrew Thompson 	struct usb_attach_arg uaa;
1573760bc48eSAndrew Thompson 	struct usb_device *udev;
1574760bc48eSAndrew Thompson 	struct usb_device *adev;
1575760bc48eSAndrew Thompson 	struct usb_device *hub;
157602ac6454SAndrew Thompson 	uint8_t *scratch_ptr;
1577e0a69b51SAndrew Thompson 	usb_error_t err;
157802ac6454SAndrew Thompson 	uint8_t device_index;
15792a4c6157SAndrew Thompson 	uint8_t config_index;
15802a4c6157SAndrew Thompson 	uint8_t config_quirk;
15812a4c6157SAndrew Thompson 	uint8_t set_config_failed;
15826950c75fSHans Petter Selasky 	uint8_t do_unlock;
158302ac6454SAndrew Thompson 
158402ac6454SAndrew Thompson 	DPRINTF("parent_dev=%p, bus=%p, parent_hub=%p, depth=%u, "
1585f29a0724SAndrew Thompson 	    "port_index=%u, port_no=%u, speed=%u, usb_mode=%u\n",
158602ac6454SAndrew Thompson 	    parent_dev, bus, parent_hub, depth, port_index, port_no,
1587f29a0724SAndrew Thompson 	    speed, mode);
158802ac6454SAndrew Thompson 
158902ac6454SAndrew Thompson 	/*
159002ac6454SAndrew Thompson 	 * Find an unused device index. In USB Host mode this is the
159102ac6454SAndrew Thompson 	 * same as the device address.
159202ac6454SAndrew Thompson 	 *
159302ac6454SAndrew Thompson 	 * Device index zero is not used and device index 1 should
159402ac6454SAndrew Thompson 	 * always be the root hub.
159502ac6454SAndrew Thompson 	 */
159602ac6454SAndrew Thompson 	for (device_index = USB_ROOT_HUB_ADDR;
159702ac6454SAndrew Thompson 	    (device_index != bus->devices_max) &&
159802ac6454SAndrew Thompson 	    (bus->devices[device_index] != NULL);
159902ac6454SAndrew Thompson 	    device_index++) /* nop */;
160002ac6454SAndrew Thompson 
160102ac6454SAndrew Thompson 	if (device_index == bus->devices_max) {
160202ac6454SAndrew Thompson 		device_printf(bus->bdev,
1603767cb2e2SAndrew Thompson 		    "No free USB device index for new device\n");
160402ac6454SAndrew Thompson 		return (NULL);
160502ac6454SAndrew Thompson 	}
160602ac6454SAndrew Thompson 
160702ac6454SAndrew Thompson 	if (depth > 0x10) {
160802ac6454SAndrew Thompson 		device_printf(bus->bdev,
1609767cb2e2SAndrew Thompson 		    "Invalid device depth\n");
161002ac6454SAndrew Thompson 		return (NULL);
161102ac6454SAndrew Thompson 	}
161202ac6454SAndrew Thompson 	udev = malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO);
161302ac6454SAndrew Thompson 	if (udev == NULL) {
161402ac6454SAndrew Thompson 		return (NULL);
161502ac6454SAndrew Thompson 	}
161602ac6454SAndrew Thompson 	/* initialise our SX-lock */
161791cd9240SAndrew Thompson 	sx_init_flags(&udev->enum_sx, "USB config SX lock", SX_DUPOK);
1618a6b60150SHans Petter Selasky 	sx_init_flags(&udev->sr_sx, "USB suspend and resume SX lock", SX_NOWITNESS);
161902ac6454SAndrew Thompson 
162091cd9240SAndrew Thompson 	cv_init(&udev->ctrlreq_cv, "WCTRL");
162191cd9240SAndrew Thompson 	cv_init(&udev->ref_cv, "UGONE");
162202ac6454SAndrew Thompson 
162302ac6454SAndrew Thompson 	/* initialise our mutex */
162491cd9240SAndrew Thompson 	mtx_init(&udev->device_mtx, "USB device mutex", NULL, MTX_DEF);
162502ac6454SAndrew Thompson 
162602ac6454SAndrew Thompson 	/* initialise generic clear stall */
1627a593f6b8SAndrew Thompson 	udev->cs_msg[0].hdr.pm_callback = &usbd_clear_stall_proc;
162802ac6454SAndrew Thompson 	udev->cs_msg[0].udev = udev;
1629a593f6b8SAndrew Thompson 	udev->cs_msg[1].hdr.pm_callback = &usbd_clear_stall_proc;
163002ac6454SAndrew Thompson 	udev->cs_msg[1].udev = udev;
163102ac6454SAndrew Thompson 
163202ac6454SAndrew Thompson 	/* initialise some USB device fields */
163302ac6454SAndrew Thompson 	udev->parent_hub = parent_hub;
163402ac6454SAndrew Thompson 	udev->parent_dev = parent_dev;
163502ac6454SAndrew Thompson 	udev->port_index = port_index;
163602ac6454SAndrew Thompson 	udev->port_no = port_no;
163702ac6454SAndrew Thompson 	udev->depth = depth;
163802ac6454SAndrew Thompson 	udev->bus = bus;
163902ac6454SAndrew Thompson 	udev->address = USB_START_ADDR;	/* default value */
1640e0a69b51SAndrew Thompson 	udev->plugtime = (usb_ticks_t)ticks;
164102ac6454SAndrew Thompson 	/*
164202ac6454SAndrew Thompson 	 * We need to force the power mode to "on" because there are plenty
164302ac6454SAndrew Thompson 	 * of USB devices out there that do not work very well with
164402ac6454SAndrew Thompson 	 * automatic suspend and resume!
164502ac6454SAndrew Thompson 	 */
1646d2d71ce7SAndrew Thompson 	udev->power_mode = usbd_filter_power_mode(udev, USB_POWER_MODE_ON);
164702ac6454SAndrew Thompson 	udev->pwr_save.last_xfer_time = ticks;
164802ac6454SAndrew Thompson 	/* we are not ready yet */
164902ac6454SAndrew Thompson 	udev->refcount = 1;
165002ac6454SAndrew Thompson 
165102ac6454SAndrew Thompson 	/* set up default endpoint descriptor */
16525b3bb704SAndrew Thompson 	udev->ctrl_ep_desc.bLength = sizeof(udev->ctrl_ep_desc);
16535b3bb704SAndrew Thompson 	udev->ctrl_ep_desc.bDescriptorType = UDESC_ENDPOINT;
16545b3bb704SAndrew Thompson 	udev->ctrl_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
16555b3bb704SAndrew Thompson 	udev->ctrl_ep_desc.bmAttributes = UE_CONTROL;
16565b3bb704SAndrew Thompson 	udev->ctrl_ep_desc.wMaxPacketSize[0] = USB_MAX_IPACKET;
16575b3bb704SAndrew Thompson 	udev->ctrl_ep_desc.wMaxPacketSize[1] = 0;
16585b3bb704SAndrew Thompson 	udev->ctrl_ep_desc.bInterval = 0;
1659963169b4SHans Petter Selasky 
1660963169b4SHans Petter Selasky 	/* set up default endpoint companion descriptor */
1661963169b4SHans Petter Selasky 	udev->ctrl_ep_comp_desc.bLength = sizeof(udev->ctrl_ep_comp_desc);
1662963169b4SHans Petter Selasky 	udev->ctrl_ep_comp_desc.bDescriptorType = UDESC_ENDPOINT_SS_COMP;
1663963169b4SHans Petter Selasky 
166402ac6454SAndrew Thompson 	udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
166502ac6454SAndrew Thompson 
166602ac6454SAndrew Thompson 	udev->speed = speed;
1667f29a0724SAndrew Thompson 	udev->flags.usb_mode = mode;
166802ac6454SAndrew Thompson 
166902ac6454SAndrew Thompson 	/* search for our High Speed USB HUB, if any */
167002ac6454SAndrew Thompson 
167102ac6454SAndrew Thompson 	adev = udev;
167202ac6454SAndrew Thompson 	hub = udev->parent_hub;
167302ac6454SAndrew Thompson 
167402ac6454SAndrew Thompson 	while (hub) {
167502ac6454SAndrew Thompson 		if (hub->speed == USB_SPEED_HIGH) {
167602ac6454SAndrew Thompson 			udev->hs_hub_addr = hub->address;
1677495f25ceSAndrew Thompson 			udev->parent_hs_hub = hub;
167802ac6454SAndrew Thompson 			udev->hs_port_no = adev->port_no;
167902ac6454SAndrew Thompson 			break;
168002ac6454SAndrew Thompson 		}
168102ac6454SAndrew Thompson 		adev = hub;
168202ac6454SAndrew Thompson 		hub = hub->parent_hub;
168302ac6454SAndrew Thompson 	}
168402ac6454SAndrew Thompson 
1685ae60fdfbSAndrew Thompson 	/* init the default endpoint */
1686a593f6b8SAndrew Thompson 	usb_init_endpoint(udev, 0,
16875b3bb704SAndrew Thompson 	    &udev->ctrl_ep_desc,
1688963169b4SHans Petter Selasky 	    &udev->ctrl_ep_comp_desc,
16895b3bb704SAndrew Thompson 	    &udev->ctrl_ep);
169002ac6454SAndrew Thompson 
169102ac6454SAndrew Thompson 	/* set device index */
169202ac6454SAndrew Thompson 	udev->device_index = device_index;
169302ac6454SAndrew Thompson 
16948755859aSAndrew Thompson #if USB_HAVE_UGEN
16950f6e8f93SAndrew Thompson 	/* Create ugen name */
1696ee3e3ff5SAndrew Thompson 	snprintf(udev->ugen_name, sizeof(udev->ugen_name),
1697ee3e3ff5SAndrew Thompson 	    USB_GENERIC_NAME "%u.%u", device_get_unit(bus->bdev),
1698ee3e3ff5SAndrew Thompson 	    device_index);
16998755859aSAndrew Thompson 	LIST_INIT(&udev->pd_list);
17008755859aSAndrew Thompson 
17010f6e8f93SAndrew Thompson 	/* Create the control endpoint device */
17022ffd5fdcSHans Petter Selasky 	udev->ctrl_dev = usb_make_dev(udev, NULL, 0, 0,
17032ffd5fdcSHans Petter Selasky 	    FREAD|FWRITE, UID_ROOT, GID_OPERATOR, 0600);
17040f6e8f93SAndrew Thompson 
17050f6e8f93SAndrew Thompson 	/* Create a link from /dev/ugenX.X to the default endpoint */
17062ffd5fdcSHans Petter Selasky 	if (udev->ctrl_dev != NULL)
17072ffd5fdcSHans Petter Selasky 		make_dev_alias(udev->ctrl_dev->cdev, "%s", udev->ugen_name);
17080f6e8f93SAndrew Thompson #endif
1709963169b4SHans Petter Selasky 	/* Initialise device */
1710963169b4SHans Petter Selasky 	if (bus->methods->device_init != NULL) {
1711963169b4SHans Petter Selasky 		err = (bus->methods->device_init) (udev);
1712963169b4SHans Petter Selasky 		if (err != 0) {
1713963169b4SHans Petter Selasky 			DPRINTFN(0, "device init %d failed "
1714963169b4SHans Petter Selasky 			    "(%s, ignored)\n", device_index,
1715963169b4SHans Petter Selasky 			    usbd_errstr(err));
1716963169b4SHans Petter Selasky 			goto done;
1717963169b4SHans Petter Selasky 		}
1718963169b4SHans Petter Selasky 	}
1719963169b4SHans Petter Selasky 	/* set powered device state after device init is complete */
1720963169b4SHans Petter Selasky 	usb_set_device_state(udev, USB_STATE_POWERED);
1721963169b4SHans Petter Selasky 
1722f29a0724SAndrew Thompson 	if (udev->flags.usb_mode == USB_MODE_HOST) {
172302ac6454SAndrew Thompson 
1724a593f6b8SAndrew Thompson 		err = usbd_req_set_address(udev, NULL, device_index);
172502ac6454SAndrew Thompson 
1726963169b4SHans Petter Selasky 		/*
1727963169b4SHans Petter Selasky 		 * This is the new USB device address from now on, if
1728963169b4SHans Petter Selasky 		 * the set address request didn't set it already.
1729963169b4SHans Petter Selasky 		 */
1730963169b4SHans Petter Selasky 		if (udev->address == USB_START_ADDR)
173102ac6454SAndrew Thompson 			udev->address = device_index;
173202ac6454SAndrew Thompson 
173302ac6454SAndrew Thompson 		/*
173402ac6454SAndrew Thompson 		 * We ignore any set-address errors, hence there are
173502ac6454SAndrew Thompson 		 * buggy USB devices out there that actually receive
173602ac6454SAndrew Thompson 		 * the SETUP PID, but manage to set the address before
173702ac6454SAndrew Thompson 		 * the STATUS stage is ACK'ed. If the device responds
173802ac6454SAndrew Thompson 		 * to the subsequent get-descriptor at the new
173902ac6454SAndrew Thompson 		 * address, then we know that the set-address command
174002ac6454SAndrew Thompson 		 * was successful.
174102ac6454SAndrew Thompson 		 */
174202ac6454SAndrew Thompson 		if (err) {
174302ac6454SAndrew Thompson 			DPRINTFN(0, "set address %d failed "
174403797f33SAndrew Thompson 			    "(%s, ignored)\n", udev->address,
1745a593f6b8SAndrew Thompson 			    usbd_errstr(err));
174602ac6454SAndrew Thompson 		}
174702ac6454SAndrew Thompson 	} else {
174802ac6454SAndrew Thompson 		/* We are not self powered */
174902ac6454SAndrew Thompson 		udev->flags.self_powered = 0;
175002ac6454SAndrew Thompson 
175102ac6454SAndrew Thompson 		/* Set unconfigured state */
175202ac6454SAndrew Thompson 		udev->curr_config_no = USB_UNCONFIG_NO;
175302ac6454SAndrew Thompson 		udev->curr_config_index = USB_UNCONFIG_INDEX;
175402ac6454SAndrew Thompson 
175502ac6454SAndrew Thompson 		/* Setup USB descriptors */
1756a593f6b8SAndrew Thompson 		err = (usb_temp_setup_by_index_p) (udev, usb_template);
175702ac6454SAndrew Thompson 		if (err) {
175802ac6454SAndrew Thompson 			DPRINTFN(0, "setting up USB template failed maybe the USB "
175902ac6454SAndrew Thompson 			    "template module has not been loaded\n");
176002ac6454SAndrew Thompson 			goto done;
176102ac6454SAndrew Thompson 		}
176202ac6454SAndrew Thompson 	}
1763a593f6b8SAndrew Thompson 	usb_set_device_state(udev, USB_STATE_ADDRESSED);
176402ac6454SAndrew Thompson 
1765963169b4SHans Petter Selasky 	/* setup the device descriptor and the initial "wMaxPacketSize" */
1766963169b4SHans Petter Selasky 	err = usbd_setup_device_desc(udev, NULL);
1767963169b4SHans Petter Selasky 
1768963169b4SHans Petter Selasky 	if (err != 0) {
176923de050bSHans Petter Selasky 		/* try to enumerate two more times */
1770a593f6b8SAndrew Thompson 		err = usbd_req_re_enumerate(udev, NULL);
177123de050bSHans Petter Selasky 		if (err != 0) {
177223de050bSHans Petter Selasky 			err = usbd_req_re_enumerate(udev, NULL);
177323de050bSHans Petter Selasky 			if (err != 0) {
177402ac6454SAndrew Thompson 				goto done;
177502ac6454SAndrew Thompson 			}
177623de050bSHans Petter Selasky 		}
177723de050bSHans Petter Selasky 	}
177802ac6454SAndrew Thompson 
177902ac6454SAndrew Thompson 	/*
178002ac6454SAndrew Thompson 	 * Setup temporary USB attach args so that we can figure out some
178102ac6454SAndrew Thompson 	 * basic quirks for this device.
178202ac6454SAndrew Thompson 	 */
1783a593f6b8SAndrew Thompson 	usb_init_attach_arg(udev, &uaa);
178402ac6454SAndrew Thompson 
1785a593f6b8SAndrew Thompson 	if (usb_test_quirk(&uaa, UQ_BUS_POWERED)) {
178602ac6454SAndrew Thompson 		udev->flags.uq_bus_powered = 1;
178702ac6454SAndrew Thompson 	}
1788a593f6b8SAndrew Thompson 	if (usb_test_quirk(&uaa, UQ_NO_STRINGS)) {
178902ac6454SAndrew Thompson 		udev->flags.no_strings = 1;
179002ac6454SAndrew Thompson 	}
179102ac6454SAndrew Thompson 	/*
179202ac6454SAndrew Thompson 	 * Workaround for buggy USB devices.
179302ac6454SAndrew Thompson 	 *
179402ac6454SAndrew Thompson 	 * It appears that some string-less USB chips will crash and
179502ac6454SAndrew Thompson 	 * disappear if any attempts are made to read any string
179602ac6454SAndrew Thompson 	 * descriptors.
179702ac6454SAndrew Thompson 	 *
179802ac6454SAndrew Thompson 	 * Try to detect such chips by checking the strings in the USB
179902ac6454SAndrew Thompson 	 * device descriptor. If no strings are present there we
180002ac6454SAndrew Thompson 	 * simply disable all USB strings.
180102ac6454SAndrew Thompson 	 */
18026950c75fSHans Petter Selasky 
18036950c75fSHans Petter Selasky 	/* Protect scratch area */
18046950c75fSHans Petter Selasky 	do_unlock = usbd_enum_lock(udev);
18056950c75fSHans Petter Selasky 
18066950c75fSHans Petter Selasky 	scratch_ptr = udev->scratch.data;
180702ac6454SAndrew Thompson 
180802ac6454SAndrew Thompson 	if (udev->ddesc.iManufacturer ||
180902ac6454SAndrew Thompson 	    udev->ddesc.iProduct ||
181002ac6454SAndrew Thompson 	    udev->ddesc.iSerialNumber) {
181102ac6454SAndrew Thompson 		/* read out the language ID string */
1812a593f6b8SAndrew Thompson 		err = usbd_req_get_string_desc(udev, NULL,
1813c10e1453SAndrew Thompson 		    (char *)scratch_ptr, 4, 0, USB_LANGUAGE_TABLE);
181402ac6454SAndrew Thompson 	} else {
181502ac6454SAndrew Thompson 		err = USB_ERR_INVAL;
181602ac6454SAndrew Thompson 	}
181702ac6454SAndrew Thompson 
181802ac6454SAndrew Thompson 	if (err || (scratch_ptr[0] < 4)) {
181902ac6454SAndrew Thompson 		udev->flags.no_strings = 1;
182002ac6454SAndrew Thompson 	} else {
18217a918edfSAndrew Thompson 		uint16_t langid;
18227a918edfSAndrew Thompson 		uint16_t pref;
18237a918edfSAndrew Thompson 		uint16_t mask;
18247a918edfSAndrew Thompson 		uint8_t x;
18257a918edfSAndrew Thompson 
18267a918edfSAndrew Thompson 		/* load preferred value and mask */
18277a918edfSAndrew Thompson 		pref = usb_lang_id;
18287a918edfSAndrew Thompson 		mask = usb_lang_mask;
18297a918edfSAndrew Thompson 
18307a918edfSAndrew Thompson 		/* align length correctly */
18316950c75fSHans Petter Selasky 		scratch_ptr[0] &= ~1U;
18327a918edfSAndrew Thompson 
18337a918edfSAndrew Thompson 		/* fix compiler warning */
18347a918edfSAndrew Thompson 		langid = 0;
18357a918edfSAndrew Thompson 
18367a918edfSAndrew Thompson 		/* search for preferred language */
18377a918edfSAndrew Thompson 		for (x = 2; (x < scratch_ptr[0]); x += 2) {
18387a918edfSAndrew Thompson 			langid = UGETW(scratch_ptr + x);
18397a918edfSAndrew Thompson 			if ((langid & mask) == pref)
18407a918edfSAndrew Thompson 				break;
18417a918edfSAndrew Thompson 		}
18427a918edfSAndrew Thompson 		if (x >= scratch_ptr[0]) {
184302ac6454SAndrew Thompson 			/* pick the first language as the default */
18447a918edfSAndrew Thompson 			DPRINTFN(1, "Using first language\n");
18457a918edfSAndrew Thompson 			langid = UGETW(scratch_ptr + 2);
18467a918edfSAndrew Thompson 		}
18477a918edfSAndrew Thompson 
18487a918edfSAndrew Thompson 		DPRINTFN(1, "Language selected: 0x%04x\n", langid);
18497a918edfSAndrew Thompson 		udev->langid = langid;
185002ac6454SAndrew Thompson 	}
185102ac6454SAndrew Thompson 
18526950c75fSHans Petter Selasky 	if (do_unlock)
18536950c75fSHans Petter Selasky 		usbd_enum_unlock(udev);
18546950c75fSHans Petter Selasky 
185502ac6454SAndrew Thompson 	/* assume 100mA bus powered for now. Changed when configured. */
185602ac6454SAndrew Thompson 	udev->power = USB_MIN_POWER;
1857c86aead6SAndrew Thompson 	/* fetch the vendor and product strings from the device */
1858a593f6b8SAndrew Thompson 	usbd_set_device_strings(udev);
185902ac6454SAndrew Thompson 
18602a4c6157SAndrew Thompson 	if (udev->flags.usb_mode == USB_MODE_DEVICE) {
18612a4c6157SAndrew Thompson 		/* USB device mode setup is complete */
18622a4c6157SAndrew Thompson 		err = 0;
18632a4c6157SAndrew Thompson 		goto config_done;
18642a4c6157SAndrew Thompson 	}
186502ac6454SAndrew Thompson 
186602ac6454SAndrew Thompson 	/*
186702ac6454SAndrew Thompson 	 * Most USB devices should attach to config index 0 by
186802ac6454SAndrew Thompson 	 * default
186902ac6454SAndrew Thompson 	 */
1870a593f6b8SAndrew Thompson 	if (usb_test_quirk(&uaa, UQ_CFG_INDEX_0)) {
187102ac6454SAndrew Thompson 		config_index = 0;
187202ac6454SAndrew Thompson 		config_quirk = 1;
1873a593f6b8SAndrew Thompson 	} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_1)) {
187402ac6454SAndrew Thompson 		config_index = 1;
187502ac6454SAndrew Thompson 		config_quirk = 1;
1876a593f6b8SAndrew Thompson 	} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_2)) {
187702ac6454SAndrew Thompson 		config_index = 2;
187802ac6454SAndrew Thompson 		config_quirk = 1;
1879a593f6b8SAndrew Thompson 	} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_3)) {
188002ac6454SAndrew Thompson 		config_index = 3;
188102ac6454SAndrew Thompson 		config_quirk = 1;
1882a593f6b8SAndrew Thompson 	} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_4)) {
188302ac6454SAndrew Thompson 		config_index = 4;
188402ac6454SAndrew Thompson 		config_quirk = 1;
188502ac6454SAndrew Thompson 	} else {
188602ac6454SAndrew Thompson 		config_index = 0;
188702ac6454SAndrew Thompson 		config_quirk = 0;
188802ac6454SAndrew Thompson 	}
188902ac6454SAndrew Thompson 
18902a4c6157SAndrew Thompson 	set_config_failed = 0;
189102ac6454SAndrew Thompson repeat_set_config:
189202ac6454SAndrew Thompson 
189302ac6454SAndrew Thompson 	DPRINTF("setting config %u\n", config_index);
189402ac6454SAndrew Thompson 
189502ac6454SAndrew Thompson 	/* get the USB device configured */
1896a593f6b8SAndrew Thompson 	err = usbd_set_config_index(udev, config_index);
189702ac6454SAndrew Thompson 	if (err) {
189802ac6454SAndrew Thompson 		if (udev->ddesc.bNumConfigurations != 0) {
189902ac6454SAndrew Thompson 			if (!set_config_failed) {
190002ac6454SAndrew Thompson 				set_config_failed = 1;
190102ac6454SAndrew Thompson 				/* XXX try to re-enumerate the device */
19022a4c6157SAndrew Thompson 				err = usbd_req_re_enumerate(udev, NULL);
190302ac6454SAndrew Thompson 				if (err == 0)
190402ac6454SAndrew Thompson 					goto repeat_set_config;
190502ac6454SAndrew Thompson 			}
19062a4c6157SAndrew Thompson 			DPRINTFN(0, "Failure selecting configuration index %u:"
19072a4c6157SAndrew Thompson 			    "%s, port %u, addr %u (ignored)\n",
1908a593f6b8SAndrew Thompson 			    config_index, usbd_errstr(err), udev->port_no,
190902ac6454SAndrew Thompson 			    udev->address);
191002ac6454SAndrew Thompson 		}
191102ac6454SAndrew Thompson 		/*
19122a4c6157SAndrew Thompson 		 * Some USB devices do not have any configurations. Ignore any
19132a4c6157SAndrew Thompson 		 * set config failures!
191402ac6454SAndrew Thompson 		 */
191502ac6454SAndrew Thompson 		err = 0;
19162a4c6157SAndrew Thompson 		goto config_done;
19172a4c6157SAndrew Thompson 	}
19182a4c6157SAndrew Thompson 	if (!config_quirk && config_index + 1 < udev->ddesc.bNumConfigurations) {
191902ac6454SAndrew Thompson 		if ((udev->cdesc->bNumInterface < 2) &&
19202a4c6157SAndrew Thompson 		    usbd_get_no_descriptors(udev->cdesc, UDESC_ENDPOINT) == 0) {
19212a4c6157SAndrew Thompson 			DPRINTFN(0, "Found no endpoints, trying next config\n");
192202ac6454SAndrew Thompson 			config_index++;
192302ac6454SAndrew Thompson 			goto repeat_set_config;
192402ac6454SAndrew Thompson 		}
192515d4edd3SHans Petter Selasky #if USB_HAVE_MSCTEST
192602ac6454SAndrew Thompson 		if (config_index == 0) {
192702ac6454SAndrew Thompson 			/*
192802ac6454SAndrew Thompson 			 * Try to figure out if we have an
192902ac6454SAndrew Thompson 			 * auto-install disk there:
193002ac6454SAndrew Thompson 			 */
1931d84a79e7SAndrew Thompson 			if (usb_iface_is_cdrom(udev, 0)) {
193202ac6454SAndrew Thompson 				DPRINTFN(0, "Found possible auto-install "
193302ac6454SAndrew Thompson 				    "disk (trying next config)\n");
193402ac6454SAndrew Thompson 				config_index++;
193502ac6454SAndrew Thompson 				goto repeat_set_config;
193602ac6454SAndrew Thompson 			}
193702ac6454SAndrew Thompson 		}
193815d4edd3SHans Petter Selasky #endif
19392a4c6157SAndrew Thompson 	}
194015d4edd3SHans Petter Selasky #if USB_HAVE_MSCTEST
1941d46dc4adSHans Petter Selasky 	if (set_config_failed == 0 && config_index == 0 &&
19429157ad4bSHans Petter Selasky 	    usb_test_quirk(&uaa, UQ_MSC_NO_SYNC_CACHE) == 0 &&
19439157ad4bSHans Petter Selasky 	    usb_test_quirk(&uaa, UQ_MSC_NO_GETMAXLUN) == 0) {
1944d46dc4adSHans Petter Selasky 
1945d46dc4adSHans Petter Selasky 		/*
1946d46dc4adSHans Petter Selasky 		 * Try to figure out if there are any MSC quirks we
1947d46dc4adSHans Petter Selasky 		 * should apply automatically:
1948d46dc4adSHans Petter Selasky 		 */
1949d46dc4adSHans Petter Selasky 		err = usb_msc_auto_quirk(udev, 0);
1950d46dc4adSHans Petter Selasky 
1951d46dc4adSHans Petter Selasky 		if (err != 0) {
1952d46dc4adSHans Petter Selasky 			set_config_failed = 1;
1953d46dc4adSHans Petter Selasky 			goto repeat_set_config;
1954d46dc4adSHans Petter Selasky 		}
1955d46dc4adSHans Petter Selasky 	}
195615d4edd3SHans Petter Selasky #endif
195702ac6454SAndrew Thompson 
19582a4c6157SAndrew Thompson config_done:
195902ac6454SAndrew Thompson 	DPRINTF("new dev (addr %d), udev=%p, parent_hub=%p\n",
196002ac6454SAndrew Thompson 	    udev->address, udev, udev->parent_hub);
196102ac6454SAndrew Thompson 
196202ac6454SAndrew Thompson 	/* register our device - we are ready */
1963a593f6b8SAndrew Thompson 	usb_bus_port_set_device(bus, parent_hub ?
196402ac6454SAndrew Thompson 	    parent_hub->hub->ports + port_index : NULL, udev, device_index);
196502ac6454SAndrew Thompson 
19660f6e8f93SAndrew Thompson #if USB_HAVE_UGEN
19678755859aSAndrew Thompson 	/* Symlink the ugen device name */
1968a593f6b8SAndrew Thompson 	udev->ugen_symlink = usb_alloc_symlink(udev->ugen_name);
19698755859aSAndrew Thompson 
19708755859aSAndrew Thompson 	/* Announce device */
1971ae538d85SAndrew Thompson 	printf("%s: <%s> at %s\n", udev->ugen_name,
1972ae538d85SAndrew Thompson 	    usb_get_manufacturer(udev),
197302ac6454SAndrew Thompson 	    device_get_nameunit(udev->bus->bdev));
19748bb77249SHans Petter Selasky #endif
19757efaaa9aSAndrew Thompson 
19768bb77249SHans Petter Selasky #if USB_HAVE_DEVCTL
19773acd904dSAndrew Thompson 	usb_notify_addq("ATTACH", udev);
19788755859aSAndrew Thompson #endif
197902ac6454SAndrew Thompson done:
198002ac6454SAndrew Thompson 	if (err) {
1981d88688c7SAndrew Thompson 		/*
1982d88688c7SAndrew Thompson 		 * Free USB device and all subdevices, if any.
1983d88688c7SAndrew Thompson 		 */
1984d88688c7SAndrew Thompson 		usb_free_device(udev, 0);
198502ac6454SAndrew Thompson 		udev = NULL;
198602ac6454SAndrew Thompson 	}
198702ac6454SAndrew Thompson 	return (udev);
198802ac6454SAndrew Thompson }
198902ac6454SAndrew Thompson 
19900f6e8f93SAndrew Thompson #if USB_HAVE_UGEN
19912ffd5fdcSHans Petter Selasky struct usb_fs_privdata *
19922ffd5fdcSHans Petter Selasky usb_make_dev(struct usb_device *udev, const char *devname, int ep,
19932ffd5fdcSHans Petter Selasky     int fi, int rwmode, uid_t uid, gid_t gid, int mode)
1994ee3e3ff5SAndrew Thompson {
1995760bc48eSAndrew Thompson 	struct usb_fs_privdata* pd;
19962ffd5fdcSHans Petter Selasky 	char buffer[32];
1997ee3e3ff5SAndrew Thompson 
1998ee3e3ff5SAndrew Thompson 	/* Store information to locate ourselves again later */
1999760bc48eSAndrew Thompson 	pd = malloc(sizeof(struct usb_fs_privdata), M_USBDEV,
2000ee3e3ff5SAndrew Thompson 	    M_WAITOK | M_ZERO);
2001ee3e3ff5SAndrew Thompson 	pd->bus_index = device_get_unit(udev->bus->bdev);
2002ee3e3ff5SAndrew Thompson 	pd->dev_index = udev->device_index;
2003ee3e3ff5SAndrew Thompson 	pd->ep_addr = ep;
20042ffd5fdcSHans Petter Selasky 	pd->fifo_index = fi;
20052ffd5fdcSHans Petter Selasky 	pd->mode = rwmode;
2006ee3e3ff5SAndrew Thompson 
2007ee3e3ff5SAndrew Thompson 	/* Now, create the device itself */
20082ffd5fdcSHans Petter Selasky 	if (devname == NULL) {
20092ffd5fdcSHans Petter Selasky 		devname = buffer;
20102ffd5fdcSHans Petter Selasky 		snprintf(buffer, sizeof(buffer), USB_DEVICE_DIR "/%u.%u.%u",
2011f35aaff0SAndrew Thompson 		    pd->bus_index, pd->dev_index, pd->ep_addr);
20122ffd5fdcSHans Petter Selasky 	}
20132ffd5fdcSHans Petter Selasky 
20142ffd5fdcSHans Petter Selasky 	pd->cdev = make_dev(&usb_devsw, 0, uid, gid, mode, "%s", devname);
20152ffd5fdcSHans Petter Selasky 
20162ffd5fdcSHans Petter Selasky 	if (pd->cdev == NULL) {
20172ffd5fdcSHans Petter Selasky 		DPRINTFN(0, "Failed to create device %s\n", devname);
20182ffd5fdcSHans Petter Selasky 		free(pd, M_USBDEV);
20192ffd5fdcSHans Petter Selasky 		return (NULL);
20202ffd5fdcSHans Petter Selasky 	}
20212ffd5fdcSHans Petter Selasky 
20222ffd5fdcSHans Petter Selasky 	/* XXX setting si_drv1 and creating the device is not atomic! */
2023ee3e3ff5SAndrew Thompson 	pd->cdev->si_drv1 = pd;
2024ee3e3ff5SAndrew Thompson 
20252ffd5fdcSHans Petter Selasky 	return (pd);
20262ffd5fdcSHans Petter Selasky }
20272ffd5fdcSHans Petter Selasky 
20282ffd5fdcSHans Petter Selasky void
20292ffd5fdcSHans Petter Selasky usb_destroy_dev(struct usb_fs_privdata *pd)
20302ffd5fdcSHans Petter Selasky {
20312ffd5fdcSHans Petter Selasky 	if (pd == NULL)
20322ffd5fdcSHans Petter Selasky 		return;
20332ffd5fdcSHans Petter Selasky 
20342ffd5fdcSHans Petter Selasky 	destroy_dev(pd->cdev);
20352ffd5fdcSHans Petter Selasky 
20362ffd5fdcSHans Petter Selasky 	free(pd, M_USBDEV);
2037ee3e3ff5SAndrew Thompson }
2038ee3e3ff5SAndrew Thompson 
2039ee3e3ff5SAndrew Thompson static void
2040a593f6b8SAndrew Thompson usb_cdev_create(struct usb_device *udev)
2041ee3e3ff5SAndrew Thompson {
2042760bc48eSAndrew Thompson 	struct usb_config_descriptor *cd;
2043760bc48eSAndrew Thompson 	struct usb_endpoint_descriptor *ed;
2044760bc48eSAndrew Thompson 	struct usb_descriptor *desc;
2045760bc48eSAndrew Thompson 	struct usb_fs_privdata* pd;
2046f5f145baSAndrew Thompson 	int inmode, outmode, inmask, outmask, mode;
2047f5f145baSAndrew Thompson 	uint8_t ep;
2048ee3e3ff5SAndrew Thompson 
2049ee3e3ff5SAndrew Thompson 	KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("stale cdev entries"));
2050ee3e3ff5SAndrew Thompson 
2051ee3e3ff5SAndrew Thompson 	DPRINTFN(2, "Creating device nodes\n");
2052ee3e3ff5SAndrew Thompson 
2053a593f6b8SAndrew Thompson 	if (usbd_get_mode(udev) == USB_MODE_DEVICE) {
2054ee3e3ff5SAndrew Thompson 		inmode = FWRITE;
2055ee3e3ff5SAndrew Thompson 		outmode = FREAD;
2056ee3e3ff5SAndrew Thompson 	} else {		 /* USB_MODE_HOST */
2057ee3e3ff5SAndrew Thompson 		inmode = FREAD;
2058ee3e3ff5SAndrew Thompson 		outmode = FWRITE;
2059ee3e3ff5SAndrew Thompson 	}
2060ee3e3ff5SAndrew Thompson 
2061f5f145baSAndrew Thompson 	inmask = 0;
2062f5f145baSAndrew Thompson 	outmask = 0;
2063f5f145baSAndrew Thompson 	desc = NULL;
2064f5f145baSAndrew Thompson 
2065f5f145baSAndrew Thompson 	/*
2066f5f145baSAndrew Thompson 	 * Collect all used endpoint numbers instead of just
2067f5f145baSAndrew Thompson 	 * generating 16 static endpoints.
2068f5f145baSAndrew Thompson 	 */
2069a593f6b8SAndrew Thompson 	cd = usbd_get_config_descriptor(udev);
2070a593f6b8SAndrew Thompson 	while ((desc = usb_desc_foreach(cd, desc))) {
2071f5f145baSAndrew Thompson 		/* filter out all endpoint descriptors */
2072f5f145baSAndrew Thompson 		if ((desc->bDescriptorType == UDESC_ENDPOINT) &&
2073f5f145baSAndrew Thompson 		    (desc->bLength >= sizeof(*ed))) {
2074760bc48eSAndrew Thompson 			ed = (struct usb_endpoint_descriptor *)desc;
2075f5f145baSAndrew Thompson 
2076f5f145baSAndrew Thompson 			/* update masks */
2077f5f145baSAndrew Thompson 			ep = ed->bEndpointAddress;
2078f5f145baSAndrew Thompson 			if (UE_GET_DIR(ep)  == UE_DIR_OUT)
2079f5f145baSAndrew Thompson 				outmask |= 1 << UE_GET_ADDR(ep);
2080f5f145baSAndrew Thompson 			else
2081f5f145baSAndrew Thompson 				inmask |= 1 << UE_GET_ADDR(ep);
2082f5f145baSAndrew Thompson 		}
2083f5f145baSAndrew Thompson 	}
2084ee3e3ff5SAndrew Thompson 
2085ee3e3ff5SAndrew Thompson 	/* Create all available endpoints except EP0 */
2086ee3e3ff5SAndrew Thompson 	for (ep = 1; ep < 16; ep++) {
20872ffd5fdcSHans Petter Selasky 		mode = (inmask & (1 << ep)) ? inmode : 0;
20882ffd5fdcSHans Petter Selasky 		mode |= (outmask & (1 << ep)) ? outmode : 0;
2089ee3e3ff5SAndrew Thompson 		if (mode == 0)
2090ee3e3ff5SAndrew Thompson 			continue;	/* no IN or OUT endpoint */
2091ee3e3ff5SAndrew Thompson 
20922ffd5fdcSHans Petter Selasky 		pd = usb_make_dev(udev, NULL, ep, 0,
20932ffd5fdcSHans Petter Selasky 		    mode, UID_ROOT, GID_OPERATOR, 0600);
20942ffd5fdcSHans Petter Selasky 
20952ffd5fdcSHans Petter Selasky 		if (pd != NULL)
2096ee3e3ff5SAndrew Thompson 			LIST_INSERT_HEAD(&udev->pd_list, pd, pd_next);
2097ee3e3ff5SAndrew Thompson 	}
2098ee3e3ff5SAndrew Thompson }
2099ee3e3ff5SAndrew Thompson 
2100ee3e3ff5SAndrew Thompson static void
2101a593f6b8SAndrew Thompson usb_cdev_free(struct usb_device *udev)
2102ee3e3ff5SAndrew Thompson {
2103760bc48eSAndrew Thompson 	struct usb_fs_privdata* pd;
2104ee3e3ff5SAndrew Thompson 
2105ee3e3ff5SAndrew Thompson 	DPRINTFN(2, "Freeing device nodes\n");
2106ee3e3ff5SAndrew Thompson 
2107ee3e3ff5SAndrew Thompson 	while ((pd = LIST_FIRST(&udev->pd_list)) != NULL) {
2108ee3e3ff5SAndrew Thompson 		KASSERT(pd->cdev->si_drv1 == pd, ("privdata corrupt"));
2109ee3e3ff5SAndrew Thompson 
2110ee3e3ff5SAndrew Thompson 		LIST_REMOVE(pd, pd_next);
2111ee3e3ff5SAndrew Thompson 
21122ffd5fdcSHans Petter Selasky 		usb_destroy_dev(pd);
21132ffd5fdcSHans Petter Selasky 	}
2114ee3e3ff5SAndrew Thompson }
21150f6e8f93SAndrew Thompson #endif
2116ee3e3ff5SAndrew Thompson 
211702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
2118a593f6b8SAndrew Thompson  *	usb_free_device
211902ac6454SAndrew Thompson  *
2120d88688c7SAndrew Thompson  * This function is NULL safe and will free an USB device and its
2121d88688c7SAndrew Thompson  * children devices, if any.
2122bdd41206SAndrew Thompson  *
2123d88688c7SAndrew Thompson  * Flag values: Reserved, set to zero.
212402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
212502ac6454SAndrew Thompson void
2126a593f6b8SAndrew Thompson usb_free_device(struct usb_device *udev, uint8_t flag)
212702ac6454SAndrew Thompson {
2128760bc48eSAndrew Thompson 	struct usb_bus *bus;
2129bdd41206SAndrew Thompson 
2130bdd41206SAndrew Thompson 	if (udev == NULL)
2131bdd41206SAndrew Thompson 		return;		/* already freed */
213202ac6454SAndrew Thompson 
213302ac6454SAndrew Thompson 	DPRINTFN(4, "udev=%p port=%d\n", udev, udev->port_no);
213402ac6454SAndrew Thompson 
2135bd216778SAndrew Thompson 	bus = udev->bus;
2136f97da128SHans Petter Selasky 
2137f97da128SHans Petter Selasky 	/* set DETACHED state to prevent any further references */
2138a593f6b8SAndrew Thompson 	usb_set_device_state(udev, USB_STATE_DETACHED);
2139bdd41206SAndrew Thompson 
21408bb77249SHans Petter Selasky #if USB_HAVE_DEVCTL
21413acd904dSAndrew Thompson 	usb_notify_addq("DETACH", udev);
21428bb77249SHans Petter Selasky #endif
214302ac6454SAndrew Thompson 
21448bb77249SHans Petter Selasky #if USB_HAVE_UGEN
2145cabe79d9SMarius Strobl 	if (!rebooting) {
2146ee3e3ff5SAndrew Thompson 		printf("%s: <%s> at %s (disconnected)\n", udev->ugen_name,
2147ae538d85SAndrew Thompson 		    usb_get_manufacturer(udev), device_get_nameunit(bus->bdev));
2148cabe79d9SMarius Strobl 	}
214902ac6454SAndrew Thompson 
2150ee3e3ff5SAndrew Thompson 	/* Destroy UGEN symlink, if any */
215102ac6454SAndrew Thompson 	if (udev->ugen_symlink) {
2152a593f6b8SAndrew Thompson 		usb_free_symlink(udev->ugen_symlink);
215302ac6454SAndrew Thompson 		udev->ugen_symlink = NULL;
215402ac6454SAndrew Thompson 	}
215502ac6454SAndrew Thompson 
21562ffd5fdcSHans Petter Selasky 	usb_destroy_dev(udev->ctrl_dev);
21570f6e8f93SAndrew Thompson #endif
215802ac6454SAndrew Thompson 
2159f29a0724SAndrew Thompson 	if (udev->flags.usb_mode == USB_MODE_DEVICE) {
216002ac6454SAndrew Thompson 		/* stop receiving any control transfers (Device Side Mode) */
21615b3bb704SAndrew Thompson 		usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
216202ac6454SAndrew Thompson 	}
216302ac6454SAndrew Thompson 
2164bdd41206SAndrew Thompson 	/* the following will get the device unconfigured in software */
2165d88688c7SAndrew Thompson 	usb_unconfigure(udev, USB_UNCFG_FLAG_FREE_EP0);
216602ac6454SAndrew Thompson 
2167f97da128SHans Petter Selasky 	/* final device unregister after all character devices are closed */
2168f97da128SHans Petter Selasky 	usb_bus_port_set_device(bus, udev->parent_hub ?
2169f97da128SHans Petter Selasky 	    udev->parent_hub->hub->ports + udev->port_index : NULL,
2170f97da128SHans Petter Selasky 	    NULL, USB_ROOT_HUB_ADDR);
2171f97da128SHans Petter Selasky 
217202ac6454SAndrew Thompson 	/* unsetup any leftover default USB transfers */
21735b3bb704SAndrew Thompson 	usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
217402ac6454SAndrew Thompson 
217502ac6454SAndrew Thompson 	/* template unsetup, if any */
2176a593f6b8SAndrew Thompson 	(usb_temp_unsetup_p) (udev);
217702ac6454SAndrew Thompson 
217802ac6454SAndrew Thompson 	/*
217902ac6454SAndrew Thompson 	 * Make sure that our clear-stall messages are not queued
218002ac6454SAndrew Thompson 	 * anywhere:
218102ac6454SAndrew Thompson 	 */
218202ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
21839b3a48eeSHans Petter Selasky 	usb_proc_mwait(USB_BUS_NON_GIANT_PROC(udev->bus),
218402ac6454SAndrew Thompson 	    &udev->cs_msg[0], &udev->cs_msg[1]);
218502ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
218602ac6454SAndrew Thompson 
218791cd9240SAndrew Thompson 	sx_destroy(&udev->enum_sx);
21882df1e9a6SAndrew Thompson 	sx_destroy(&udev->sr_sx);
218902ac6454SAndrew Thompson 
219091cd9240SAndrew Thompson 	cv_destroy(&udev->ctrlreq_cv);
219191cd9240SAndrew Thompson 	cv_destroy(&udev->ref_cv);
219202ac6454SAndrew Thompson 
219391cd9240SAndrew Thompson 	mtx_destroy(&udev->device_mtx);
21948755859aSAndrew Thompson #if USB_HAVE_UGEN
2195ee3e3ff5SAndrew Thompson 	KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("leaked cdev entries"));
21968755859aSAndrew Thompson #endif
219702ac6454SAndrew Thompson 
2198963169b4SHans Petter Selasky 	/* Uninitialise device */
2199963169b4SHans Petter Selasky 	if (bus->methods->device_uninit != NULL)
2200963169b4SHans Petter Selasky 		(bus->methods->device_uninit) (udev);
2201963169b4SHans Petter Selasky 
220202ac6454SAndrew Thompson 	/* free device */
220328212de2SAndrew Thompson 	free(udev->serial, M_USB);
220428212de2SAndrew Thompson 	free(udev->manufacturer, M_USB);
220528212de2SAndrew Thompson 	free(udev->product, M_USB);
220602ac6454SAndrew Thompson 	free(udev, M_USB);
220702ac6454SAndrew Thompson }
220802ac6454SAndrew Thompson 
220902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
2210a593f6b8SAndrew Thompson  *	usbd_get_iface
221102ac6454SAndrew Thompson  *
221202ac6454SAndrew Thompson  * This function is the safe way to get the USB interface structure
221302ac6454SAndrew Thompson  * pointer by interface index.
221402ac6454SAndrew Thompson  *
221502ac6454SAndrew Thompson  * Return values:
221602ac6454SAndrew Thompson  *   NULL: Interface not present.
221702ac6454SAndrew Thompson  *   Else: Pointer to USB interface structure.
221802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
2219760bc48eSAndrew Thompson struct usb_interface *
2220a593f6b8SAndrew Thompson usbd_get_iface(struct usb_device *udev, uint8_t iface_index)
222102ac6454SAndrew Thompson {
2222760bc48eSAndrew Thompson 	struct usb_interface *iface = udev->ifaces + iface_index;
222302ac6454SAndrew Thompson 
2224bdd41206SAndrew Thompson 	if (iface_index >= udev->ifaces_max)
222502ac6454SAndrew Thompson 		return (NULL);
222602ac6454SAndrew Thompson 	return (iface);
222702ac6454SAndrew Thompson }
222802ac6454SAndrew Thompson 
222902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
22303c8d24f4SAndrew Thompson  *	usbd_find_descriptor
223102ac6454SAndrew Thompson  *
223202ac6454SAndrew Thompson  * This function will lookup the first descriptor that matches the
223302ac6454SAndrew Thompson  * criteria given by the arguments "type" and "subtype". Descriptors
223402ac6454SAndrew Thompson  * will only be searched within the interface having the index
223502ac6454SAndrew Thompson  * "iface_index".  If the "id" argument points to an USB descriptor,
223602ac6454SAndrew Thompson  * it will be skipped before the search is started. This allows
223702ac6454SAndrew Thompson  * searching for multiple descriptors using the same criteria. Else
223802ac6454SAndrew Thompson  * the search is started after the interface descriptor.
223902ac6454SAndrew Thompson  *
224002ac6454SAndrew Thompson  * Return values:
224102ac6454SAndrew Thompson  *   NULL: End of descriptors
224202ac6454SAndrew Thompson  *   Else: A descriptor matching the criteria
224302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
224402ac6454SAndrew Thompson void   *
22453c8d24f4SAndrew Thompson usbd_find_descriptor(struct usb_device *udev, void *id, uint8_t iface_index,
224602ac6454SAndrew Thompson     uint8_t type, uint8_t type_mask,
224702ac6454SAndrew Thompson     uint8_t subtype, uint8_t subtype_mask)
224802ac6454SAndrew Thompson {
2249760bc48eSAndrew Thompson 	struct usb_descriptor *desc;
2250760bc48eSAndrew Thompson 	struct usb_config_descriptor *cd;
2251760bc48eSAndrew Thompson 	struct usb_interface *iface;
225202ac6454SAndrew Thompson 
2253a593f6b8SAndrew Thompson 	cd = usbd_get_config_descriptor(udev);
225402ac6454SAndrew Thompson 	if (cd == NULL) {
225502ac6454SAndrew Thompson 		return (NULL);
225602ac6454SAndrew Thompson 	}
225702ac6454SAndrew Thompson 	if (id == NULL) {
2258a593f6b8SAndrew Thompson 		iface = usbd_get_iface(udev, iface_index);
225902ac6454SAndrew Thompson 		if (iface == NULL) {
226002ac6454SAndrew Thompson 			return (NULL);
226102ac6454SAndrew Thompson 		}
2262a593f6b8SAndrew Thompson 		id = usbd_get_interface_descriptor(iface);
226302ac6454SAndrew Thompson 		if (id == NULL) {
226402ac6454SAndrew Thompson 			return (NULL);
226502ac6454SAndrew Thompson 		}
226602ac6454SAndrew Thompson 	}
226702ac6454SAndrew Thompson 	desc = (void *)id;
226802ac6454SAndrew Thompson 
2269a593f6b8SAndrew Thompson 	while ((desc = usb_desc_foreach(cd, desc))) {
227002ac6454SAndrew Thompson 
227102ac6454SAndrew Thompson 		if (desc->bDescriptorType == UDESC_INTERFACE) {
227202ac6454SAndrew Thompson 			break;
227302ac6454SAndrew Thompson 		}
227402ac6454SAndrew Thompson 		if (((desc->bDescriptorType & type_mask) == type) &&
227502ac6454SAndrew Thompson 		    ((desc->bDescriptorSubtype & subtype_mask) == subtype)) {
227602ac6454SAndrew Thompson 			return (desc);
227702ac6454SAndrew Thompson 		}
227802ac6454SAndrew Thompson 	}
227902ac6454SAndrew Thompson 	return (NULL);
228002ac6454SAndrew Thompson }
228102ac6454SAndrew Thompson 
228202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
2283a593f6b8SAndrew Thompson  *	usb_devinfo
228402ac6454SAndrew Thompson  *
228502ac6454SAndrew Thompson  * This function will dump information from the device descriptor
228602ac6454SAndrew Thompson  * belonging to the USB device pointed to by "udev", to the string
228702ac6454SAndrew Thompson  * pointed to by "dst_ptr" having a maximum length of "dst_len" bytes
228802ac6454SAndrew Thompson  * including the terminating zero.
228902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
229002ac6454SAndrew Thompson void
2291a593f6b8SAndrew Thompson usb_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len)
229202ac6454SAndrew Thompson {
2293760bc48eSAndrew Thompson 	struct usb_device_descriptor *udd = &udev->ddesc;
229402ac6454SAndrew Thompson 	uint16_t bcdDevice;
229502ac6454SAndrew Thompson 	uint16_t bcdUSB;
229602ac6454SAndrew Thompson 
229702ac6454SAndrew Thompson 	bcdUSB = UGETW(udd->bcdUSB);
229802ac6454SAndrew Thompson 	bcdDevice = UGETW(udd->bcdDevice);
229902ac6454SAndrew Thompson 
230002ac6454SAndrew Thompson 	if (udd->bDeviceClass != 0xFF) {
230102ac6454SAndrew Thompson 		snprintf(dst_ptr, dst_len, "%s %s, class %d/%d, rev %x.%02x/"
23028755859aSAndrew Thompson 		    "%x.%02x, addr %d",
2303ae538d85SAndrew Thompson 		    usb_get_manufacturer(udev),
2304ae538d85SAndrew Thompson 		    usb_get_product(udev),
230502ac6454SAndrew Thompson 		    udd->bDeviceClass, udd->bDeviceSubClass,
230602ac6454SAndrew Thompson 		    (bcdUSB >> 8), bcdUSB & 0xFF,
230702ac6454SAndrew Thompson 		    (bcdDevice >> 8), bcdDevice & 0xFF,
230802ac6454SAndrew Thompson 		    udev->address);
230902ac6454SAndrew Thompson 	} else {
231002ac6454SAndrew Thompson 		snprintf(dst_ptr, dst_len, "%s %s, rev %x.%02x/"
23118755859aSAndrew Thompson 		    "%x.%02x, addr %d",
2312ae538d85SAndrew Thompson 		    usb_get_manufacturer(udev),
2313ae538d85SAndrew Thompson 		    usb_get_product(udev),
231402ac6454SAndrew Thompson 		    (bcdUSB >> 8), bcdUSB & 0xFF,
231502ac6454SAndrew Thompson 		    (bcdDevice >> 8), bcdDevice & 0xFF,
231602ac6454SAndrew Thompson 		    udev->address);
231702ac6454SAndrew Thompson 	}
231802ac6454SAndrew Thompson }
231902ac6454SAndrew Thompson 
2320ed6d949aSAndrew Thompson #ifdef USB_VERBOSE
232102ac6454SAndrew Thompson /*
232202ac6454SAndrew Thompson  * Descriptions of of known vendors and devices ("products").
232302ac6454SAndrew Thompson  */
232402ac6454SAndrew Thompson struct usb_knowndev {
232502ac6454SAndrew Thompson 	uint16_t vendor;
232602ac6454SAndrew Thompson 	uint16_t product;
232702ac6454SAndrew Thompson 	uint32_t flags;
232802ac6454SAndrew Thompson 	const char *vendorname;
232902ac6454SAndrew Thompson 	const char *productname;
233002ac6454SAndrew Thompson };
233102ac6454SAndrew Thompson 
233202ac6454SAndrew Thompson #define	USB_KNOWNDEV_NOPROD	0x01	/* match on vendor only */
233302ac6454SAndrew Thompson 
233402ac6454SAndrew Thompson #include "usbdevs.h"
233502ac6454SAndrew Thompson #include "usbdevs_data.h"
233602ac6454SAndrew Thompson #endif					/* USB_VERBOSE */
233702ac6454SAndrew Thompson 
233802ac6454SAndrew Thompson static void
2339a593f6b8SAndrew Thompson usbd_set_device_strings(struct usb_device *udev)
234002ac6454SAndrew Thompson {
2341760bc48eSAndrew Thompson 	struct usb_device_descriptor *udd = &udev->ddesc;
2342ed6d949aSAndrew Thompson #ifdef USB_VERBOSE
234302ac6454SAndrew Thompson 	const struct usb_knowndev *kdp;
234402ac6454SAndrew Thompson #endif
23452df1e9a6SAndrew Thompson 	char *temp_ptr;
23467a918edfSAndrew Thompson 	size_t temp_size;
234702ac6454SAndrew Thompson 	uint16_t vendor_id;
234802ac6454SAndrew Thompson 	uint16_t product_id;
23496950c75fSHans Petter Selasky 	uint8_t do_unlock;
235002ac6454SAndrew Thompson 
23516950c75fSHans Petter Selasky 	/* Protect scratch area */
23526950c75fSHans Petter Selasky 	do_unlock = usbd_enum_lock(udev);
23536950c75fSHans Petter Selasky 
23546950c75fSHans Petter Selasky 	temp_ptr = (char *)udev->scratch.data;
23556950c75fSHans Petter Selasky 	temp_size = sizeof(udev->scratch.data);
23567a918edfSAndrew Thompson 
235702ac6454SAndrew Thompson 	vendor_id = UGETW(udd->idVendor);
235802ac6454SAndrew Thompson 	product_id = UGETW(udd->idProduct);
235902ac6454SAndrew Thompson 
2360c86aead6SAndrew Thompson 	/* get serial number string */
23617a918edfSAndrew Thompson 	usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2362c86aead6SAndrew Thompson 	    udev->ddesc.iSerialNumber);
23637a918edfSAndrew Thompson 	udev->serial = strdup(temp_ptr, M_USB);
236402ac6454SAndrew Thompson 
2365c86aead6SAndrew Thompson 	/* get manufacturer string */
23667a918edfSAndrew Thompson 	usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2367c86aead6SAndrew Thompson 	    udev->ddesc.iManufacturer);
23687a918edfSAndrew Thompson 	usb_trim_spaces(temp_ptr);
23697a918edfSAndrew Thompson 	if (temp_ptr[0] != '\0')
23707a918edfSAndrew Thompson 		udev->manufacturer = strdup(temp_ptr, M_USB);
2371c86aead6SAndrew Thompson 
2372c86aead6SAndrew Thompson 	/* get product string */
23737a918edfSAndrew Thompson 	usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2374c86aead6SAndrew Thompson 	    udev->ddesc.iProduct);
23757a918edfSAndrew Thompson 	usb_trim_spaces(temp_ptr);
23767a918edfSAndrew Thompson 	if (temp_ptr[0] != '\0')
23777a918edfSAndrew Thompson 		udev->product = strdup(temp_ptr, M_USB);
2378c86aead6SAndrew Thompson 
2379ed6d949aSAndrew Thompson #ifdef USB_VERBOSE
2380c86aead6SAndrew Thompson 	if (udev->manufacturer == NULL || udev->product == NULL) {
2381c86aead6SAndrew Thompson 		for (kdp = usb_knowndevs; kdp->vendorname != NULL; kdp++) {
238202ac6454SAndrew Thompson 			if (kdp->vendor == vendor_id &&
238302ac6454SAndrew Thompson 			    (kdp->product == product_id ||
238402ac6454SAndrew Thompson 			    (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
238502ac6454SAndrew Thompson 				break;
238602ac6454SAndrew Thompson 		}
238702ac6454SAndrew Thompson 		if (kdp->vendorname != NULL) {
2388c86aead6SAndrew Thompson 			/* XXX should use pointer to knowndevs string */
2389c86aead6SAndrew Thompson 			if (udev->manufacturer == NULL) {
2390c86aead6SAndrew Thompson 				udev->manufacturer = strdup(kdp->vendorname,
2391c86aead6SAndrew Thompson 				    M_USB);
2392c86aead6SAndrew Thompson 			}
2393c86aead6SAndrew Thompson 			if (udev->product == NULL &&
2394c86aead6SAndrew Thompson 			    (kdp->flags & USB_KNOWNDEV_NOPROD) == 0) {
2395c86aead6SAndrew Thompson 				udev->product = strdup(kdp->productname,
2396c86aead6SAndrew Thompson 				    M_USB);
2397c86aead6SAndrew Thompson 			}
239802ac6454SAndrew Thompson 		}
239902ac6454SAndrew Thompson 	}
240002ac6454SAndrew Thompson #endif
2401c86aead6SAndrew Thompson 	/* Provide default strings if none were found */
2402c86aead6SAndrew Thompson 	if (udev->manufacturer == NULL) {
24037a918edfSAndrew Thompson 		snprintf(temp_ptr, temp_size, "vendor 0x%04x", vendor_id);
24047a918edfSAndrew Thompson 		udev->manufacturer = strdup(temp_ptr, M_USB);
240502ac6454SAndrew Thompson 	}
2406c86aead6SAndrew Thompson 	if (udev->product == NULL) {
24077a918edfSAndrew Thompson 		snprintf(temp_ptr, temp_size, "product 0x%04x", product_id);
24087a918edfSAndrew Thompson 		udev->product = strdup(temp_ptr, M_USB);
240902ac6454SAndrew Thompson 	}
24106950c75fSHans Petter Selasky 
24116950c75fSHans Petter Selasky 	if (do_unlock)
24126950c75fSHans Petter Selasky 		usbd_enum_unlock(udev);
241302ac6454SAndrew Thompson }
241402ac6454SAndrew Thompson 
241502ac6454SAndrew Thompson /*
241602ac6454SAndrew Thompson  * Returns:
241702ac6454SAndrew Thompson  * See: USB_MODE_XXX
241802ac6454SAndrew Thompson  */
2419f29a0724SAndrew Thompson enum usb_hc_mode
2420a593f6b8SAndrew Thompson usbd_get_mode(struct usb_device *udev)
242102ac6454SAndrew Thompson {
2422f29a0724SAndrew Thompson 	return (udev->flags.usb_mode);
242302ac6454SAndrew Thompson }
242402ac6454SAndrew Thompson 
242502ac6454SAndrew Thompson /*
242602ac6454SAndrew Thompson  * Returns:
242702ac6454SAndrew Thompson  * See: USB_SPEED_XXX
242802ac6454SAndrew Thompson  */
24298d2dd5ddSAndrew Thompson enum usb_dev_speed
2430a593f6b8SAndrew Thompson usbd_get_speed(struct usb_device *udev)
243102ac6454SAndrew Thompson {
243202ac6454SAndrew Thompson 	return (udev->speed);
243302ac6454SAndrew Thompson }
243402ac6454SAndrew Thompson 
243502ac6454SAndrew Thompson uint32_t
2436a593f6b8SAndrew Thompson usbd_get_isoc_fps(struct usb_device *udev)
243702ac6454SAndrew Thompson {
243802ac6454SAndrew Thompson 	;				/* indent fix */
243902ac6454SAndrew Thompson 	switch (udev->speed) {
244002ac6454SAndrew Thompson 	case USB_SPEED_LOW:
244102ac6454SAndrew Thompson 	case USB_SPEED_FULL:
244202ac6454SAndrew Thompson 		return (1000);
244302ac6454SAndrew Thompson 	default:
244402ac6454SAndrew Thompson 		return (8000);
244502ac6454SAndrew Thompson 	}
244602ac6454SAndrew Thompson }
244702ac6454SAndrew Thompson 
2448760bc48eSAndrew Thompson struct usb_device_descriptor *
2449a593f6b8SAndrew Thompson usbd_get_device_descriptor(struct usb_device *udev)
245002ac6454SAndrew Thompson {
245102ac6454SAndrew Thompson 	if (udev == NULL)
245202ac6454SAndrew Thompson 		return (NULL);		/* be NULL safe */
245302ac6454SAndrew Thompson 	return (&udev->ddesc);
245402ac6454SAndrew Thompson }
245502ac6454SAndrew Thompson 
2456760bc48eSAndrew Thompson struct usb_config_descriptor *
2457a593f6b8SAndrew Thompson usbd_get_config_descriptor(struct usb_device *udev)
245802ac6454SAndrew Thompson {
245902ac6454SAndrew Thompson 	if (udev == NULL)
246002ac6454SAndrew Thompson 		return (NULL);		/* be NULL safe */
246102ac6454SAndrew Thompson 	return (udev->cdesc);
246202ac6454SAndrew Thompson }
246302ac6454SAndrew Thompson 
246402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
2465a593f6b8SAndrew Thompson  *	usb_test_quirk - test a device for a given quirk
246602ac6454SAndrew Thompson  *
246702ac6454SAndrew Thompson  * Return values:
246802ac6454SAndrew Thompson  * 0: The USB device does not have the given quirk.
246902ac6454SAndrew Thompson  * Else: The USB device has the given quirk.
247002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
247102ac6454SAndrew Thompson uint8_t
2472a593f6b8SAndrew Thompson usb_test_quirk(const struct usb_attach_arg *uaa, uint16_t quirk)
247302ac6454SAndrew Thompson {
247402ac6454SAndrew Thompson 	uint8_t found;
24753f37fb62SHans Petter Selasky 	uint8_t x;
24763f37fb62SHans Petter Selasky 
24773f37fb62SHans Petter Selasky 	if (quirk == UQ_NONE)
24783f37fb62SHans Petter Selasky 		return (0);
24793f37fb62SHans Petter Selasky 
24803f37fb62SHans Petter Selasky 	/* search the automatic per device quirks first */
24813f37fb62SHans Petter Selasky 
24823f37fb62SHans Petter Selasky 	for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) {
24833f37fb62SHans Petter Selasky 		if (uaa->device->autoQuirk[x] == quirk)
24843f37fb62SHans Petter Selasky 			return (1);
24853f37fb62SHans Petter Selasky 	}
24863f37fb62SHans Petter Selasky 
24873f37fb62SHans Petter Selasky 	/* search global quirk table, if any */
248802ac6454SAndrew Thompson 
2489a593f6b8SAndrew Thompson 	found = (usb_test_quirk_p) (&uaa->info, quirk);
24903f37fb62SHans Petter Selasky 
249102ac6454SAndrew Thompson 	return (found);
249202ac6454SAndrew Thompson }
249302ac6454SAndrew Thompson 
2494760bc48eSAndrew Thompson struct usb_interface_descriptor *
2495a593f6b8SAndrew Thompson usbd_get_interface_descriptor(struct usb_interface *iface)
249602ac6454SAndrew Thompson {
249702ac6454SAndrew Thompson 	if (iface == NULL)
249802ac6454SAndrew Thompson 		return (NULL);		/* be NULL safe */
249902ac6454SAndrew Thompson 	return (iface->idesc);
250002ac6454SAndrew Thompson }
250102ac6454SAndrew Thompson 
250202ac6454SAndrew Thompson uint8_t
2503a593f6b8SAndrew Thompson usbd_get_interface_altindex(struct usb_interface *iface)
250402ac6454SAndrew Thompson {
250502ac6454SAndrew Thompson 	return (iface->alt_index);
250602ac6454SAndrew Thompson }
250702ac6454SAndrew Thompson 
250802ac6454SAndrew Thompson uint8_t
2509a593f6b8SAndrew Thompson usbd_get_bus_index(struct usb_device *udev)
251002ac6454SAndrew Thompson {
251102ac6454SAndrew Thompson 	return ((uint8_t)device_get_unit(udev->bus->bdev));
251202ac6454SAndrew Thompson }
251302ac6454SAndrew Thompson 
251402ac6454SAndrew Thompson uint8_t
2515a593f6b8SAndrew Thompson usbd_get_device_index(struct usb_device *udev)
251602ac6454SAndrew Thompson {
251702ac6454SAndrew Thompson 	return (udev->device_index);
251802ac6454SAndrew Thompson }
251902ac6454SAndrew Thompson 
25208bb77249SHans Petter Selasky #if USB_HAVE_DEVCTL
25213acd904dSAndrew Thompson static void
25223acd904dSAndrew Thompson usb_notify_addq(const char *type, struct usb_device *udev)
25233acd904dSAndrew Thompson {
25243acd904dSAndrew Thompson 	struct usb_interface *iface;
25253acd904dSAndrew Thompson 	struct sbuf *sb;
25263acd904dSAndrew Thompson 	int i;
25273acd904dSAndrew Thompson 
25283acd904dSAndrew Thompson 	/* announce the device */
25293acd904dSAndrew Thompson 	sb = sbuf_new_auto();
25303acd904dSAndrew Thompson 	sbuf_printf(sb,
25318bb77249SHans Petter Selasky #if USB_HAVE_UGEN
25328bb77249SHans Petter Selasky 	    "ugen=%s "
253302c40241SHans Petter Selasky 	    "cdev=%s "
25348bb77249SHans Petter Selasky #endif
25353acd904dSAndrew Thompson 	    "vendor=0x%04x "
25363acd904dSAndrew Thompson 	    "product=0x%04x "
25373acd904dSAndrew Thompson 	    "devclass=0x%02x "
25383acd904dSAndrew Thompson 	    "devsubclass=0x%02x "
25393acd904dSAndrew Thompson 	    "sernum=\"%s\" "
25403acd904dSAndrew Thompson 	    "release=0x%04x "
254166390216SAndrew Thompson 	    "mode=%s "
25423acd904dSAndrew Thompson 	    "port=%u "
25438bb77249SHans Petter Selasky #if USB_HAVE_UGEN
25446df3eebbSNick Hibma 	    "parent=%s"
25458bb77249SHans Petter Selasky #endif
25468bb77249SHans Petter Selasky 	    "",
25478bb77249SHans Petter Selasky #if USB_HAVE_UGEN
25483acd904dSAndrew Thompson 	    udev->ugen_name,
254902c40241SHans Petter Selasky 	    udev->ugen_name,
25508bb77249SHans Petter Selasky #endif
25513acd904dSAndrew Thompson 	    UGETW(udev->ddesc.idVendor),
25523acd904dSAndrew Thompson 	    UGETW(udev->ddesc.idProduct),
25533acd904dSAndrew Thompson 	    udev->ddesc.bDeviceClass,
25543acd904dSAndrew Thompson 	    udev->ddesc.bDeviceSubClass,
2555ae538d85SAndrew Thompson 	    usb_get_serial(udev),
25563acd904dSAndrew Thompson 	    UGETW(udev->ddesc.bcdDevice),
255766390216SAndrew Thompson 	    (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
25588bb77249SHans Petter Selasky 	    udev->port_no
25598bb77249SHans Petter Selasky #if USB_HAVE_UGEN
25608bb77249SHans Petter Selasky 	    , udev->parent_hub != NULL ?
25613acd904dSAndrew Thompson 		udev->parent_hub->ugen_name :
25628bb77249SHans Petter Selasky 		device_get_nameunit(device_get_parent(udev->bus->bdev))
25638bb77249SHans Petter Selasky #endif
25648bb77249SHans Petter Selasky 	    );
25653acd904dSAndrew Thompson 	sbuf_finish(sb);
25663acd904dSAndrew Thompson 	devctl_notify("USB", "DEVICE", type, sbuf_data(sb));
25673acd904dSAndrew Thompson 	sbuf_delete(sb);
25683acd904dSAndrew Thompson 
25693acd904dSAndrew Thompson 	/* announce each interface */
25703acd904dSAndrew Thompson 	for (i = 0; i < USB_IFACE_MAX; i++) {
25713acd904dSAndrew Thompson 		iface = usbd_get_iface(udev, i);
25723acd904dSAndrew Thompson 		if (iface == NULL)
25733acd904dSAndrew Thompson 			break;		/* end of interfaces */
25743acd904dSAndrew Thompson 		if (iface->idesc == NULL)
25753acd904dSAndrew Thompson 			continue;	/* no interface descriptor */
25763acd904dSAndrew Thompson 
25773acd904dSAndrew Thompson 		sb = sbuf_new_auto();
25783acd904dSAndrew Thompson 		sbuf_printf(sb,
25798bb77249SHans Petter Selasky #if USB_HAVE_UGEN
25808bb77249SHans Petter Selasky 		    "ugen=%s "
258102c40241SHans Petter Selasky 		    "cdev=%s "
25828bb77249SHans Petter Selasky #endif
25833acd904dSAndrew Thompson 		    "vendor=0x%04x "
25843acd904dSAndrew Thompson 		    "product=0x%04x "
25853acd904dSAndrew Thompson 		    "devclass=0x%02x "
25863acd904dSAndrew Thompson 		    "devsubclass=0x%02x "
25873acd904dSAndrew Thompson 		    "sernum=\"%s\" "
25883acd904dSAndrew Thompson 		    "release=0x%04x "
258966390216SAndrew Thompson 		    "mode=%s "
25903acd904dSAndrew Thompson 		    "interface=%d "
25913acd904dSAndrew Thompson 		    "endpoints=%d "
25923acd904dSAndrew Thompson 		    "intclass=0x%02x "
25933acd904dSAndrew Thompson 		    "intsubclass=0x%02x "
25946df3eebbSNick Hibma 		    "intprotocol=0x%02x",
25958bb77249SHans Petter Selasky #if USB_HAVE_UGEN
25963acd904dSAndrew Thompson 		    udev->ugen_name,
259702c40241SHans Petter Selasky 		    udev->ugen_name,
25988bb77249SHans Petter Selasky #endif
25993acd904dSAndrew Thompson 		    UGETW(udev->ddesc.idVendor),
26003acd904dSAndrew Thompson 		    UGETW(udev->ddesc.idProduct),
26013acd904dSAndrew Thompson 		    udev->ddesc.bDeviceClass,
26023acd904dSAndrew Thompson 		    udev->ddesc.bDeviceSubClass,
2603ae538d85SAndrew Thompson 		    usb_get_serial(udev),
26043acd904dSAndrew Thompson 		    UGETW(udev->ddesc.bcdDevice),
260566390216SAndrew Thompson 		    (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
26063acd904dSAndrew Thompson 		    iface->idesc->bInterfaceNumber,
26073acd904dSAndrew Thompson 		    iface->idesc->bNumEndpoints,
26083acd904dSAndrew Thompson 		    iface->idesc->bInterfaceClass,
26093acd904dSAndrew Thompson 		    iface->idesc->bInterfaceSubClass,
26103acd904dSAndrew Thompson 		    iface->idesc->bInterfaceProtocol);
26113acd904dSAndrew Thompson 		sbuf_finish(sb);
26123acd904dSAndrew Thompson 		devctl_notify("USB", "INTERFACE", type, sbuf_data(sb));
26133acd904dSAndrew Thompson 		sbuf_delete(sb);
26143acd904dSAndrew Thompson 	}
26153acd904dSAndrew Thompson }
26168bb77249SHans Petter Selasky #endif
261702ac6454SAndrew Thompson 
26188bb77249SHans Petter Selasky #if USB_HAVE_UGEN
261902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
2620a593f6b8SAndrew Thompson  *	usb_fifo_free_wrap
262102ac6454SAndrew Thompson  *
262202ac6454SAndrew Thompson  * This function will free the FIFOs.
262302ac6454SAndrew Thompson  *
2624bdd41206SAndrew Thompson  * Description of "flag" argument: If the USB_UNCFG_FLAG_FREE_EP0 flag
2625bdd41206SAndrew Thompson  * is set and "iface_index" is set to "USB_IFACE_INDEX_ANY", we free
2626bdd41206SAndrew Thompson  * all FIFOs. If the USB_UNCFG_FLAG_FREE_EP0 flag is not set and
2627bdd41206SAndrew Thompson  * "iface_index" is set to "USB_IFACE_INDEX_ANY", we free all non
2628bdd41206SAndrew Thompson  * control endpoint FIFOs. If "iface_index" is not set to
2629bdd41206SAndrew Thompson  * "USB_IFACE_INDEX_ANY" the flag has no effect.
263002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
263102ac6454SAndrew Thompson static void
2632a593f6b8SAndrew Thompson usb_fifo_free_wrap(struct usb_device *udev,
263302ac6454SAndrew Thompson     uint8_t iface_index, uint8_t flag)
263402ac6454SAndrew Thompson {
2635760bc48eSAndrew Thompson 	struct usb_fifo *f;
263602ac6454SAndrew Thompson 	uint16_t i;
263702ac6454SAndrew Thompson 
263802ac6454SAndrew Thompson 	/*
263902ac6454SAndrew Thompson 	 * Free any USB FIFOs on the given interface:
264002ac6454SAndrew Thompson 	 */
264102ac6454SAndrew Thompson 	for (i = 0; i != USB_FIFO_MAX; i++) {
264202ac6454SAndrew Thompson 		f = udev->fifo[i];
264302ac6454SAndrew Thompson 		if (f == NULL) {
264402ac6454SAndrew Thompson 			continue;
264502ac6454SAndrew Thompson 		}
264602ac6454SAndrew Thompson 		/* Check if the interface index matches */
264702ac6454SAndrew Thompson 		if (iface_index == f->iface_index) {
2648a593f6b8SAndrew Thompson 			if (f->methods != &usb_ugen_methods) {
264902ac6454SAndrew Thompson 				/*
265002ac6454SAndrew Thompson 				 * Don't free any non-generic FIFOs in
265102ac6454SAndrew Thompson 				 * this case.
265202ac6454SAndrew Thompson 				 */
265302ac6454SAndrew Thompson 				continue;
265402ac6454SAndrew Thompson 			}
265502ac6454SAndrew Thompson 			if ((f->dev_ep_index == 0) &&
265602ac6454SAndrew Thompson 			    (f->fs_xfer == NULL)) {
265702ac6454SAndrew Thompson 				/* no need to free this FIFO */
265802ac6454SAndrew Thompson 				continue;
265902ac6454SAndrew Thompson 			}
266002ac6454SAndrew Thompson 		} else if (iface_index == USB_IFACE_INDEX_ANY) {
2661a593f6b8SAndrew Thompson 			if ((f->methods == &usb_ugen_methods) &&
2662bdd41206SAndrew Thompson 			    (f->dev_ep_index == 0) &&
2663bdd41206SAndrew Thompson 			    (!(flag & USB_UNCFG_FLAG_FREE_EP0)) &&
266402ac6454SAndrew Thompson 			    (f->fs_xfer == NULL)) {
266502ac6454SAndrew Thompson 				/* no need to free this FIFO */
266602ac6454SAndrew Thompson 				continue;
266702ac6454SAndrew Thompson 			}
266802ac6454SAndrew Thompson 		} else {
266902ac6454SAndrew Thompson 			/* no need to free this FIFO */
267002ac6454SAndrew Thompson 			continue;
267102ac6454SAndrew Thompson 		}
2672d008478eSHans Petter Selasky 		/* wait for pending refs to go away */
2673d008478eSHans Petter Selasky 		usb_wait_pending_ref_locked(udev);
2674d008478eSHans Petter Selasky 
267502ac6454SAndrew Thompson 		/* free this FIFO */
2676a593f6b8SAndrew Thompson 		usb_fifo_free(f);
2677d008478eSHans Petter Selasky 
2678d008478eSHans Petter Selasky 		/* restore refcount */
2679d008478eSHans Petter Selasky 		usb_ref_restore_locked(udev);
268002ac6454SAndrew Thompson 	}
268102ac6454SAndrew Thompson }
26820f6e8f93SAndrew Thompson #endif
268302ac6454SAndrew Thompson 
268402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
2685a593f6b8SAndrew Thompson  *	usb_peer_can_wakeup
268602ac6454SAndrew Thompson  *
268702ac6454SAndrew Thompson  * Return values:
268802ac6454SAndrew Thompson  * 0: Peer cannot do resume signalling.
268902ac6454SAndrew Thompson  * Else: Peer can do resume signalling.
269002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
269102ac6454SAndrew Thompson uint8_t
2692a593f6b8SAndrew Thompson usb_peer_can_wakeup(struct usb_device *udev)
269302ac6454SAndrew Thompson {
2694760bc48eSAndrew Thompson 	const struct usb_config_descriptor *cdp;
269502ac6454SAndrew Thompson 
269602ac6454SAndrew Thompson 	cdp = udev->cdesc;
2697f29a0724SAndrew Thompson 	if ((cdp != NULL) && (udev->flags.usb_mode == USB_MODE_HOST)) {
269802ac6454SAndrew Thompson 		return (cdp->bmAttributes & UC_REMOTE_WAKEUP);
269902ac6454SAndrew Thompson 	}
270002ac6454SAndrew Thompson 	return (0);			/* not supported */
270102ac6454SAndrew Thompson }
2702bd216778SAndrew Thompson 
2703bd216778SAndrew Thompson void
2704a593f6b8SAndrew Thompson usb_set_device_state(struct usb_device *udev, enum usb_dev_state state)
2705bd216778SAndrew Thompson {
2706bd216778SAndrew Thompson 
2707bd216778SAndrew Thompson 	KASSERT(state < USB_STATE_MAX, ("invalid udev state"));
2708bd216778SAndrew Thompson 
2709bd216778SAndrew Thompson 	DPRINTF("udev %p state %s -> %s\n", udev,
2710a593f6b8SAndrew Thompson 	    usb_statestr(udev->state), usb_statestr(state));
2711963169b4SHans Petter Selasky 
2712f97da128SHans Petter Selasky #if USB_HAVE_UGEN
2713f97da128SHans Petter Selasky 	mtx_lock(&usb_ref_lock);
2714f97da128SHans Petter Selasky #endif
2715f97da128SHans Petter Selasky 	udev->state = state;
2716f97da128SHans Petter Selasky #if USB_HAVE_UGEN
2717f97da128SHans Petter Selasky 	mtx_unlock(&usb_ref_lock);
2718f97da128SHans Petter Selasky #endif
2719963169b4SHans Petter Selasky 	if (udev->bus->methods->device_state_change != NULL)
2720963169b4SHans Petter Selasky 		(udev->bus->methods->device_state_change) (udev);
2721963169b4SHans Petter Selasky }
2722963169b4SHans Petter Selasky 
2723963169b4SHans Petter Selasky enum usb_dev_state
2724963169b4SHans Petter Selasky usb_get_device_state(struct usb_device *udev)
2725963169b4SHans Petter Selasky {
2726963169b4SHans Petter Selasky 	if (udev == NULL)
2727963169b4SHans Petter Selasky 		return (USB_STATE_DETACHED);
2728963169b4SHans Petter Selasky 	return (udev->state);
2729bd216778SAndrew Thompson }
2730bd216778SAndrew Thompson 
2731ec8f3127SAndrew Thompson uint8_t
2732a593f6b8SAndrew Thompson usbd_device_attached(struct usb_device *udev)
2733bd216778SAndrew Thompson {
2734bd216778SAndrew Thompson 	return (udev->state > USB_STATE_DETACHED);
2735bd216778SAndrew Thompson }
2736cb18f7d1SAlfred Perlstein 
27376950c75fSHans Petter Selasky /*
27386950c75fSHans Petter Selasky  * The following function locks enumerating the given USB device. If
27396950c75fSHans Petter Selasky  * the lock is already grabbed this function returns zero. Else a
27406950c75fSHans Petter Selasky  * non-zero value is returned.
27416950c75fSHans Petter Selasky  */
27426950c75fSHans Petter Selasky uint8_t
2743cb18f7d1SAlfred Perlstein usbd_enum_lock(struct usb_device *udev)
2744cb18f7d1SAlfred Perlstein {
27456950c75fSHans Petter Selasky 	if (sx_xlocked(&udev->enum_sx))
27466950c75fSHans Petter Selasky 		return (0);
27476950c75fSHans Petter Selasky 
274891cd9240SAndrew Thompson 	sx_xlock(&udev->enum_sx);
27492df1e9a6SAndrew Thompson 	sx_xlock(&udev->sr_sx);
2750cb18f7d1SAlfred Perlstein 	/*
2751cb18f7d1SAlfred Perlstein 	 * NEWBUS LOCK NOTE: We should check if any parent SX locks
2752cb18f7d1SAlfred Perlstein 	 * are locked before locking Giant. Else the lock can be
2753cb18f7d1SAlfred Perlstein 	 * locked multiple times.
2754cb18f7d1SAlfred Perlstein 	 */
2755cb18f7d1SAlfred Perlstein 	mtx_lock(&Giant);
27566950c75fSHans Petter Selasky 	return (1);
2757cb18f7d1SAlfred Perlstein }
2758cb18f7d1SAlfred Perlstein 
2759cb18f7d1SAlfred Perlstein /* The following function unlocks enumerating the given USB device. */
2760cb18f7d1SAlfred Perlstein 
2761cb18f7d1SAlfred Perlstein void
2762cb18f7d1SAlfred Perlstein usbd_enum_unlock(struct usb_device *udev)
2763cb18f7d1SAlfred Perlstein {
2764cb18f7d1SAlfred Perlstein 	mtx_unlock(&Giant);
276591cd9240SAndrew Thompson 	sx_xunlock(&udev->enum_sx);
27662df1e9a6SAndrew Thompson 	sx_xunlock(&udev->sr_sx);
27672df1e9a6SAndrew Thompson }
27682df1e9a6SAndrew Thompson 
27692df1e9a6SAndrew Thompson /* The following function locks suspend and resume. */
27702df1e9a6SAndrew Thompson 
27712df1e9a6SAndrew Thompson void
27722df1e9a6SAndrew Thompson usbd_sr_lock(struct usb_device *udev)
27732df1e9a6SAndrew Thompson {
27742df1e9a6SAndrew Thompson 	sx_xlock(&udev->sr_sx);
27752df1e9a6SAndrew Thompson 	/*
27762df1e9a6SAndrew Thompson 	 * NEWBUS LOCK NOTE: We should check if any parent SX locks
27772df1e9a6SAndrew Thompson 	 * are locked before locking Giant. Else the lock can be
27782df1e9a6SAndrew Thompson 	 * locked multiple times.
27792df1e9a6SAndrew Thompson 	 */
27802df1e9a6SAndrew Thompson 	mtx_lock(&Giant);
27812df1e9a6SAndrew Thompson }
27822df1e9a6SAndrew Thompson 
27832df1e9a6SAndrew Thompson /* The following function unlocks suspend and resume. */
27842df1e9a6SAndrew Thompson 
27852df1e9a6SAndrew Thompson void
27862df1e9a6SAndrew Thompson usbd_sr_unlock(struct usb_device *udev)
27872df1e9a6SAndrew Thompson {
27882df1e9a6SAndrew Thompson 	mtx_unlock(&Giant);
27892df1e9a6SAndrew Thompson 	sx_xunlock(&udev->sr_sx);
2790cb18f7d1SAlfred Perlstein }
2791cb18f7d1SAlfred Perlstein 
2792cb18f7d1SAlfred Perlstein /*
2793cb18f7d1SAlfred Perlstein  * The following function checks the enumerating lock for the given
2794cb18f7d1SAlfred Perlstein  * USB device.
2795cb18f7d1SAlfred Perlstein  */
2796cb18f7d1SAlfred Perlstein 
2797cb18f7d1SAlfred Perlstein uint8_t
2798cb18f7d1SAlfred Perlstein usbd_enum_is_locked(struct usb_device *udev)
2799cb18f7d1SAlfred Perlstein {
280091cd9240SAndrew Thompson 	return (sx_xlocked(&udev->enum_sx));
2801cb18f7d1SAlfred Perlstein }
28028427ed84SHans Petter Selasky 
28038427ed84SHans Petter Selasky /*
28048427ed84SHans Petter Selasky  * The following function is used to set the per-interface specific
28058427ed84SHans Petter Selasky  * plug and play information. The string referred to by the pnpinfo
28068427ed84SHans Petter Selasky  * argument can safely be freed after calling this function. The
28078427ed84SHans Petter Selasky  * pnpinfo of an interface will be reset at device detach or when
28088427ed84SHans Petter Selasky  * passing a NULL argument to this function. This function
28098427ed84SHans Petter Selasky  * returns zero on success, else a USB_ERR_XXX failure code.
28108427ed84SHans Petter Selasky  */
28118427ed84SHans Petter Selasky 
28128427ed84SHans Petter Selasky usb_error_t
28138427ed84SHans Petter Selasky usbd_set_pnpinfo(struct usb_device *udev, uint8_t iface_index, const char *pnpinfo)
28148427ed84SHans Petter Selasky {
28158427ed84SHans Petter Selasky 	struct usb_interface *iface;
28168427ed84SHans Petter Selasky 
28178427ed84SHans Petter Selasky 	iface = usbd_get_iface(udev, iface_index);
28188427ed84SHans Petter Selasky 	if (iface == NULL)
28198427ed84SHans Petter Selasky 		return (USB_ERR_INVAL);
28208427ed84SHans Petter Selasky 
28218427ed84SHans Petter Selasky 	if (iface->pnpinfo != NULL) {
28228427ed84SHans Petter Selasky 		free(iface->pnpinfo, M_USBDEV);
28238427ed84SHans Petter Selasky 		iface->pnpinfo = NULL;
28248427ed84SHans Petter Selasky 	}
28258427ed84SHans Petter Selasky 
28268427ed84SHans Petter Selasky 	if (pnpinfo == NULL || pnpinfo[0] == 0)
28278427ed84SHans Petter Selasky 		return (0);		/* success */
28288427ed84SHans Petter Selasky 
28298427ed84SHans Petter Selasky 	iface->pnpinfo = strdup(pnpinfo, M_USBDEV);
28308427ed84SHans Petter Selasky 	if (iface->pnpinfo == NULL)
28318427ed84SHans Petter Selasky 		return (USB_ERR_NOMEM);
28328427ed84SHans Petter Selasky 
28338427ed84SHans Petter Selasky 	return (0);			/* success */
28348427ed84SHans Petter Selasky }
28358427ed84SHans Petter Selasky 
2836d46dc4adSHans Petter Selasky usb_error_t
2837d46dc4adSHans Petter Selasky usbd_add_dynamic_quirk(struct usb_device *udev, uint16_t quirk)
2838d46dc4adSHans Petter Selasky {
2839d46dc4adSHans Petter Selasky 	uint8_t x;
2840d46dc4adSHans Petter Selasky 
2841d46dc4adSHans Petter Selasky 	for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) {
28423f37fb62SHans Petter Selasky 		if (udev->autoQuirk[x] == 0 ||
28433f37fb62SHans Petter Selasky 		    udev->autoQuirk[x] == quirk) {
2844d46dc4adSHans Petter Selasky 			udev->autoQuirk[x] = quirk;
2845d46dc4adSHans Petter Selasky 			return (0);	/* success */
2846d46dc4adSHans Petter Selasky 		}
2847d46dc4adSHans Petter Selasky 	}
2848d46dc4adSHans Petter Selasky 	return (USB_ERR_NOMEM);
2849d46dc4adSHans Petter Selasky }
2850a5cf1aaaSHans Petter Selasky 
2851a5cf1aaaSHans Petter Selasky /*
2852a5cf1aaaSHans Petter Selasky  * The following function is used to select the endpoint mode. It
2853a5cf1aaaSHans Petter Selasky  * should not be called outside enumeration context.
2854a5cf1aaaSHans Petter Selasky  */
2855a5cf1aaaSHans Petter Selasky 
2856a5cf1aaaSHans Petter Selasky usb_error_t
2857a5cf1aaaSHans Petter Selasky usbd_set_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep,
2858a5cf1aaaSHans Petter Selasky     uint8_t ep_mode)
2859a5cf1aaaSHans Petter Selasky {
2860a5cf1aaaSHans Petter Selasky 	usb_error_t error;
2861f61fc4bdSHans Petter Selasky 	uint8_t do_unlock;
2862a5cf1aaaSHans Petter Selasky 
28636950c75fSHans Petter Selasky 	/* Prevent re-enumeration */
28646950c75fSHans Petter Selasky 	do_unlock = usbd_enum_lock(udev);
2865a5cf1aaaSHans Petter Selasky 
2866a5cf1aaaSHans Petter Selasky 	if (udev->bus->methods->set_endpoint_mode != NULL) {
2867a5cf1aaaSHans Petter Selasky 		error = (udev->bus->methods->set_endpoint_mode) (
2868a5cf1aaaSHans Petter Selasky 		    udev, ep, ep_mode);
2869a5cf1aaaSHans Petter Selasky 	} else if (ep_mode != USB_EP_MODE_DEFAULT) {
2870a5cf1aaaSHans Petter Selasky 		error = USB_ERR_INVAL;
2871a5cf1aaaSHans Petter Selasky 	} else {
2872a5cf1aaaSHans Petter Selasky 		error = 0;
2873a5cf1aaaSHans Petter Selasky 	}
2874a5cf1aaaSHans Petter Selasky 
2875a5cf1aaaSHans Petter Selasky 	/* only set new mode regardless of error */
2876a5cf1aaaSHans Petter Selasky 	ep->ep_mode = ep_mode;
2877a5cf1aaaSHans Petter Selasky 
2878f61fc4bdSHans Petter Selasky 	if (do_unlock)
2879f61fc4bdSHans Petter Selasky 		usbd_enum_unlock(udev);
2880a5cf1aaaSHans Petter Selasky 	return (error);
2881a5cf1aaaSHans Petter Selasky }
2882a5cf1aaaSHans Petter Selasky 
2883a5cf1aaaSHans Petter Selasky uint8_t
2884a5cf1aaaSHans Petter Selasky usbd_get_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep)
2885a5cf1aaaSHans Petter Selasky {
2886a5cf1aaaSHans Petter Selasky 	return (ep->ep_mode);
2887a5cf1aaaSHans Petter Selasky }
2888