xref: /freebsd/sys/dev/usb/usb_device.c (revision 415bcd89)
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 
121af3b2549SHans 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 
129af3b2549SHans 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 
132af3b2549SHans 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 /*------------------------------------------------------------------------*
452b78e84d1SHans Petter Selasky  *	usb_wait_pending_refs
453d008478eSHans Petter Selasky  *
454d008478eSHans Petter Selasky  * This function will wait for any USB references to go away before
455b78e84d1SHans Petter Selasky  * returning. This function is used before freeing a USB device.
456d008478eSHans Petter Selasky  *------------------------------------------------------------------------*/
457d008478eSHans Petter Selasky static void
458b78e84d1SHans Petter Selasky usb_wait_pending_refs(struct usb_device *udev)
459d008478eSHans Petter Selasky {
460d008478eSHans Petter Selasky #if USB_HAVE_UGEN
461b78e84d1SHans Petter Selasky 	DPRINTF("Refcount = %d\n", (int)udev->refcount);
462d008478eSHans Petter Selasky 
463b78e84d1SHans Petter Selasky 	mtx_lock(&usb_ref_lock);
464b78e84d1SHans Petter Selasky 	udev->refcount--;
465d008478eSHans Petter Selasky 	while (1) {
466d008478eSHans Petter Selasky 		/* wait for any pending references to go away */
467b78e84d1SHans Petter Selasky 		if (udev->refcount == 0) {
468b78e84d1SHans Petter Selasky 			/* prevent further refs being taken, if any */
469d008478eSHans Petter Selasky 			udev->refcount = USB_DEV_REF_MAX;
470d008478eSHans Petter Selasky 			break;
471d008478eSHans Petter Selasky 		}
472d008478eSHans Petter Selasky 		cv_wait(&udev->ref_cv, &usb_ref_lock);
473d008478eSHans Petter Selasky 	}
474d008478eSHans Petter Selasky 	mtx_unlock(&usb_ref_lock);
475d008478eSHans Petter Selasky #endif
476d008478eSHans Petter Selasky }
477d008478eSHans Petter Selasky 
478d008478eSHans Petter Selasky /*------------------------------------------------------------------------*
479a593f6b8SAndrew Thompson  *	usb_unconfigure
48002ac6454SAndrew Thompson  *
481ae60fdfbSAndrew Thompson  * This function will free all USB interfaces and USB endpoints belonging
48202ac6454SAndrew Thompson  * to an USB device.
483bdd41206SAndrew Thompson  *
484bdd41206SAndrew Thompson  * Flag values, see "USB_UNCFG_FLAG_XXX".
48502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
48602ac6454SAndrew Thompson static void
487a593f6b8SAndrew Thompson usb_unconfigure(struct usb_device *udev, uint8_t flag)
48802ac6454SAndrew Thompson {
489bdd41206SAndrew Thompson 	uint8_t do_unlock;
49002ac6454SAndrew Thompson 
4916950c75fSHans Petter Selasky 	/* Prevent re-enumeration */
4926950c75fSHans Petter Selasky 	do_unlock = usbd_enum_lock(udev);
493bdd41206SAndrew Thompson 
494bdd41206SAndrew Thompson 	/* detach all interface drivers */
495a593f6b8SAndrew Thompson 	usb_detach_device(udev, USB_IFACE_INDEX_ANY, flag);
496bdd41206SAndrew Thompson 
497bdd41206SAndrew Thompson #if USB_HAVE_UGEN
498bdd41206SAndrew Thompson 	/* free all FIFOs except control endpoint FIFOs */
499a593f6b8SAndrew Thompson 	usb_fifo_free_wrap(udev, USB_IFACE_INDEX_ANY, flag);
500bdd41206SAndrew Thompson 
501bdd41206SAndrew Thompson 	/*
502bdd41206SAndrew Thompson 	 * Free all cdev's, if any.
503bdd41206SAndrew Thompson 	 */
504a593f6b8SAndrew Thompson 	usb_cdev_free(udev);
505bdd41206SAndrew Thompson #endif
50602ac6454SAndrew Thompson 
507bdc081c6SAndrew Thompson #if USB_HAVE_COMPAT_LINUX
50802ac6454SAndrew Thompson 	/* free Linux compat device, if any */
509760bc48eSAndrew Thompson 	if (udev->linux_endpoint_start) {
510760bc48eSAndrew Thompson 		usb_linux_free_device(udev);
511760bc48eSAndrew Thompson 		udev->linux_endpoint_start = NULL;
51202ac6454SAndrew Thompson 	}
513bdc081c6SAndrew Thompson #endif
51402ac6454SAndrew Thompson 
515a593f6b8SAndrew Thompson 	usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_FREE);
51602ac6454SAndrew Thompson 
517ae60fdfbSAndrew Thompson 	/* free "cdesc" after "ifaces" and "endpoints", if any */
5187efaaa9aSAndrew Thompson 	if (udev->cdesc != NULL) {
519f29a0724SAndrew Thompson 		if (udev->flags.usb_mode != USB_MODE_DEVICE)
5202c79a775SHans Petter Selasky 			usbd_free_config_desc(udev, udev->cdesc);
52102ac6454SAndrew Thompson 		udev->cdesc = NULL;
52202ac6454SAndrew Thompson 	}
52302ac6454SAndrew Thompson 	/* set unconfigured state */
52402ac6454SAndrew Thompson 	udev->curr_config_no = USB_UNCONFIG_NO;
52502ac6454SAndrew Thompson 	udev->curr_config_index = USB_UNCONFIG_INDEX;
526bdd41206SAndrew Thompson 
527cb18f7d1SAlfred Perlstein 	if (do_unlock)
528cb18f7d1SAlfred Perlstein 		usbd_enum_unlock(udev);
52902ac6454SAndrew Thompson }
53002ac6454SAndrew Thompson 
53102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
532a593f6b8SAndrew Thompson  *	usbd_set_config_index
53302ac6454SAndrew Thompson  *
53402ac6454SAndrew Thompson  * This function selects configuration by index, independent of the
53502ac6454SAndrew Thompson  * actual configuration number. This function should not be used by
53602ac6454SAndrew Thompson  * USB drivers.
53702ac6454SAndrew Thompson  *
53802ac6454SAndrew Thompson  * Returns:
53902ac6454SAndrew Thompson  *    0: Success
54002ac6454SAndrew Thompson  * Else: Failure
54102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
542e0a69b51SAndrew Thompson usb_error_t
543a593f6b8SAndrew Thompson usbd_set_config_index(struct usb_device *udev, uint8_t index)
54402ac6454SAndrew Thompson {
545760bc48eSAndrew Thompson 	struct usb_status ds;
546760bc48eSAndrew Thompson 	struct usb_config_descriptor *cdp;
54702ac6454SAndrew Thompson 	uint16_t power;
54802ac6454SAndrew Thompson 	uint16_t max_power;
54902ac6454SAndrew Thompson 	uint8_t selfpowered;
55002ac6454SAndrew Thompson 	uint8_t do_unlock;
551e0a69b51SAndrew Thompson 	usb_error_t err;
55202ac6454SAndrew Thompson 
55302ac6454SAndrew Thompson 	DPRINTFN(6, "udev=%p index=%d\n", udev, index);
55402ac6454SAndrew Thompson 
5556950c75fSHans Petter Selasky 	/* Prevent re-enumeration */
5566950c75fSHans Petter Selasky 	do_unlock = usbd_enum_lock(udev);
55702ac6454SAndrew Thompson 
558d88688c7SAndrew Thompson 	usb_unconfigure(udev, 0);
55902ac6454SAndrew Thompson 
56002ac6454SAndrew Thompson 	if (index == USB_UNCONFIG_INDEX) {
56102ac6454SAndrew Thompson 		/*
56202ac6454SAndrew Thompson 		 * Leave unallocated when unconfiguring the
563a593f6b8SAndrew Thompson 		 * device. "usb_unconfigure()" will also reset
56402ac6454SAndrew Thompson 		 * the current config number and index.
56502ac6454SAndrew Thompson 		 */
566a593f6b8SAndrew Thompson 		err = usbd_req_set_config(udev, NULL, USB_UNCONFIG_NO);
567bd216778SAndrew Thompson 		if (udev->state == USB_STATE_CONFIGURED)
568a593f6b8SAndrew Thompson 			usb_set_device_state(udev, USB_STATE_ADDRESSED);
56902ac6454SAndrew Thompson 		goto done;
57002ac6454SAndrew Thompson 	}
57102ac6454SAndrew Thompson 	/* get the full config descriptor */
572f29a0724SAndrew Thompson 	if (udev->flags.usb_mode == USB_MODE_DEVICE) {
5737efaaa9aSAndrew Thompson 		/* save some memory */
574a593f6b8SAndrew Thompson 		err = usbd_req_get_descriptor_ptr(udev, &cdp,
575459d369eSAndrew Thompson 		    (UDESC_CONFIG << 8) | index);
5767efaaa9aSAndrew Thompson 	} else {
5777efaaa9aSAndrew Thompson 		/* normal request */
578a593f6b8SAndrew Thompson 		err = usbd_req_get_config_desc_full(udev,
5792c79a775SHans Petter Selasky 		    NULL, &cdp, index);
5807efaaa9aSAndrew Thompson 	}
58102ac6454SAndrew Thompson 	if (err) {
58202ac6454SAndrew Thompson 		goto done;
58302ac6454SAndrew Thompson 	}
58402ac6454SAndrew Thompson 	/* set the new config descriptor */
58502ac6454SAndrew Thompson 
58602ac6454SAndrew Thompson 	udev->cdesc = cdp;
58702ac6454SAndrew Thompson 
58802ac6454SAndrew Thompson 	/* Figure out if the device is self or bus powered. */
58902ac6454SAndrew Thompson 	selfpowered = 0;
59002ac6454SAndrew Thompson 	if ((!udev->flags.uq_bus_powered) &&
59102ac6454SAndrew Thompson 	    (cdp->bmAttributes & UC_SELF_POWERED) &&
592f29a0724SAndrew Thompson 	    (udev->flags.usb_mode == USB_MODE_HOST)) {
59302ac6454SAndrew Thompson 		/* May be self powered. */
59402ac6454SAndrew Thompson 		if (cdp->bmAttributes & UC_BUS_POWERED) {
59502ac6454SAndrew Thompson 			/* Must ask device. */
596a593f6b8SAndrew Thompson 			err = usbd_req_get_device_status(udev, NULL, &ds);
59702ac6454SAndrew Thompson 			if (err) {
59802ac6454SAndrew Thompson 				DPRINTFN(0, "could not read "
59902ac6454SAndrew Thompson 				    "device status: %s\n",
600a593f6b8SAndrew Thompson 				    usbd_errstr(err));
60102ac6454SAndrew Thompson 			} else if (UGETW(ds.wStatus) & UDS_SELF_POWERED) {
60202ac6454SAndrew Thompson 				selfpowered = 1;
60302ac6454SAndrew Thompson 			}
60402ac6454SAndrew Thompson 			DPRINTF("status=0x%04x \n",
60502ac6454SAndrew Thompson 				UGETW(ds.wStatus));
60602ac6454SAndrew Thompson 		} else
60702ac6454SAndrew Thompson 			selfpowered = 1;
60802ac6454SAndrew Thompson 	}
60902ac6454SAndrew Thompson 	DPRINTF("udev=%p cdesc=%p (addr %d) cno=%d attr=0x%02x, "
61002ac6454SAndrew Thompson 	    "selfpowered=%d, power=%d\n",
61102ac6454SAndrew Thompson 	    udev, cdp,
61291f72cedSAndrew Thompson 	    udev->address, cdp->bConfigurationValue, cdp->bmAttributes,
61302ac6454SAndrew Thompson 	    selfpowered, cdp->bMaxPower * 2);
61402ac6454SAndrew Thompson 
61502ac6454SAndrew Thompson 	/* Check if we have enough power. */
61602ac6454SAndrew Thompson 	power = cdp->bMaxPower * 2;
61702ac6454SAndrew Thompson 
61802ac6454SAndrew Thompson 	if (udev->parent_hub) {
61902ac6454SAndrew Thompson 		max_power = udev->parent_hub->hub->portpower;
62002ac6454SAndrew Thompson 	} else {
62102ac6454SAndrew Thompson 		max_power = USB_MAX_POWER;
62202ac6454SAndrew Thompson 	}
62302ac6454SAndrew Thompson 
62402ac6454SAndrew Thompson 	if (power > max_power) {
62502ac6454SAndrew Thompson 		DPRINTFN(0, "power exceeded %d > %d\n", power, max_power);
62602ac6454SAndrew Thompson 		err = USB_ERR_NO_POWER;
62702ac6454SAndrew Thompson 		goto done;
62802ac6454SAndrew Thompson 	}
62902ac6454SAndrew Thompson 	/* Only update "self_powered" in USB Host Mode */
630f29a0724SAndrew Thompson 	if (udev->flags.usb_mode == USB_MODE_HOST) {
63102ac6454SAndrew Thompson 		udev->flags.self_powered = selfpowered;
63202ac6454SAndrew Thompson 	}
63302ac6454SAndrew Thompson 	udev->power = power;
63402ac6454SAndrew Thompson 	udev->curr_config_no = cdp->bConfigurationValue;
63502ac6454SAndrew Thompson 	udev->curr_config_index = index;
636a593f6b8SAndrew Thompson 	usb_set_device_state(udev, USB_STATE_CONFIGURED);
63702ac6454SAndrew Thompson 
63802ac6454SAndrew Thompson 	/* Set the actual configuration value. */
639a593f6b8SAndrew Thompson 	err = usbd_req_set_config(udev, NULL, cdp->bConfigurationValue);
64002ac6454SAndrew Thompson 	if (err) {
64102ac6454SAndrew Thompson 		goto done;
64202ac6454SAndrew Thompson 	}
643bdd41206SAndrew Thompson 
644a593f6b8SAndrew Thompson 	err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_ALLOC);
64502ac6454SAndrew Thompson 	if (err) {
64602ac6454SAndrew Thompson 		goto done;
64702ac6454SAndrew Thompson 	}
648bdd41206SAndrew Thompson 
649a593f6b8SAndrew Thompson 	err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_INIT);
650bdd41206SAndrew Thompson 	if (err) {
651bdd41206SAndrew Thompson 		goto done;
65202ac6454SAndrew Thompson 	}
653bdd41206SAndrew Thompson 
6540f6e8f93SAndrew Thompson #if USB_HAVE_UGEN
655ee3e3ff5SAndrew Thompson 	/* create device nodes for each endpoint */
656a593f6b8SAndrew Thompson 	usb_cdev_create(udev);
6570f6e8f93SAndrew Thompson #endif
65802ac6454SAndrew Thompson 
65902ac6454SAndrew Thompson done:
660a593f6b8SAndrew Thompson 	DPRINTF("error=%s\n", usbd_errstr(err));
66102ac6454SAndrew Thompson 	if (err) {
662d88688c7SAndrew Thompson 		usb_unconfigure(udev, 0);
66302ac6454SAndrew Thompson 	}
664cb18f7d1SAlfred Perlstein 	if (do_unlock)
665cb18f7d1SAlfred Perlstein 		usbd_enum_unlock(udev);
66602ac6454SAndrew Thompson 	return (err);
66702ac6454SAndrew Thompson }
66802ac6454SAndrew Thompson 
66902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
670a593f6b8SAndrew Thompson  *	usb_config_parse
671bdd41206SAndrew Thompson  *
672ae60fdfbSAndrew Thompson  * This function will allocate and free USB interfaces and USB endpoints,
673ae60fdfbSAndrew Thompson  * parse the USB configuration structure and initialise the USB endpoints
674bdd41206SAndrew Thompson  * and interfaces. If "iface_index" is not equal to
675bdd41206SAndrew Thompson  * "USB_IFACE_INDEX_ANY" then the "cmd" parameter is the
676bdd41206SAndrew Thompson  * alternate_setting to be selected for the given interface. Else the
677bdd41206SAndrew Thompson  * "cmd" parameter is defined by "USB_CFG_XXX". "iface_index" can be
678bdd41206SAndrew Thompson  * "USB_IFACE_INDEX_ANY" or a valid USB interface index. This function
679bdd41206SAndrew Thompson  * is typically called when setting the configuration or when setting
680bdd41206SAndrew Thompson  * an alternate interface.
681bdd41206SAndrew Thompson  *
682bdd41206SAndrew Thompson  * Returns:
683bdd41206SAndrew Thompson  *    0: Success
684bdd41206SAndrew Thompson  * Else: Failure
685bdd41206SAndrew Thompson  *------------------------------------------------------------------------*/
686963169b4SHans Petter Selasky static usb_error_t
687a593f6b8SAndrew Thompson usb_config_parse(struct usb_device *udev, uint8_t iface_index, uint8_t cmd)
688bdd41206SAndrew Thompson {
689760bc48eSAndrew Thompson 	struct usb_idesc_parse_state ips;
690760bc48eSAndrew Thompson 	struct usb_interface_descriptor *id;
691760bc48eSAndrew Thompson 	struct usb_endpoint_descriptor *ed;
692760bc48eSAndrew Thompson 	struct usb_interface *iface;
693ae60fdfbSAndrew Thompson 	struct usb_endpoint *ep;
694e0a69b51SAndrew Thompson 	usb_error_t err;
695bdd41206SAndrew Thompson 	uint8_t ep_curr;
696bdd41206SAndrew Thompson 	uint8_t ep_max;
697bdd41206SAndrew Thompson 	uint8_t temp;
698bdd41206SAndrew Thompson 	uint8_t do_init;
699bdd41206SAndrew Thompson 	uint8_t alt_index;
700bdd41206SAndrew Thompson 
701bdd41206SAndrew Thompson 	if (iface_index != USB_IFACE_INDEX_ANY) {
702bdd41206SAndrew Thompson 		/* parameter overload */
703bdd41206SAndrew Thompson 		alt_index = cmd;
704bdd41206SAndrew Thompson 		cmd = USB_CFG_INIT;
705bdd41206SAndrew Thompson 	} else {
706bdd41206SAndrew Thompson 		/* not used */
707bdd41206SAndrew Thompson 		alt_index = 0;
708bdd41206SAndrew Thompson 	}
709bdd41206SAndrew Thompson 
710bdd41206SAndrew Thompson 	err = 0;
711bdd41206SAndrew Thompson 
712bdd41206SAndrew Thompson 	DPRINTFN(5, "iface_index=%d cmd=%d\n",
713bdd41206SAndrew Thompson 	    iface_index, cmd);
714bdd41206SAndrew Thompson 
715bdd41206SAndrew Thompson 	if (cmd == USB_CFG_FREE)
716bdd41206SAndrew Thompson 		goto cleanup;
717bdd41206SAndrew Thompson 
718bdd41206SAndrew Thompson 	if (cmd == USB_CFG_INIT) {
71991cd9240SAndrew Thompson 		sx_assert(&udev->enum_sx, SA_LOCKED);
720bdd41206SAndrew Thompson 
721ae60fdfbSAndrew Thompson 		/* check for in-use endpoints */
722bdd41206SAndrew Thompson 
723ae60fdfbSAndrew Thompson 		ep = udev->endpoints;
724ae60fdfbSAndrew Thompson 		ep_max = udev->endpoints_max;
725bdd41206SAndrew Thompson 		while (ep_max--) {
726ae60fdfbSAndrew Thompson 			/* look for matching endpoints */
727bdd41206SAndrew Thompson 			if ((iface_index == USB_IFACE_INDEX_ANY) ||
728ae60fdfbSAndrew Thompson 			    (iface_index == ep->iface_index)) {
729f12c6c29SAndrew Thompson 				if (ep->refcount_alloc != 0) {
730bdd41206SAndrew Thompson 					/*
731bdd41206SAndrew Thompson 					 * This typically indicates a
732bdd41206SAndrew Thompson 					 * more serious error.
733bdd41206SAndrew Thompson 					 */
734bdd41206SAndrew Thompson 					err = USB_ERR_IN_USE;
735bdd41206SAndrew Thompson 				} else {
736ae60fdfbSAndrew Thompson 					/* reset endpoint */
737ae60fdfbSAndrew Thompson 					memset(ep, 0, sizeof(*ep));
738ae60fdfbSAndrew Thompson 					/* make sure we don't zero the endpoint again */
739ae60fdfbSAndrew Thompson 					ep->iface_index = USB_IFACE_INDEX_ANY;
740bdd41206SAndrew Thompson 				}
741bdd41206SAndrew Thompson 			}
742ae60fdfbSAndrew Thompson 			ep++;
743bdd41206SAndrew Thompson 		}
744bdd41206SAndrew Thompson 
745bdd41206SAndrew Thompson 		if (err)
746bdd41206SAndrew Thompson 			return (err);
747bdd41206SAndrew Thompson 	}
748bdd41206SAndrew Thompson 
749bdd41206SAndrew Thompson 	memset(&ips, 0, sizeof(ips));
750bdd41206SAndrew Thompson 
751bdd41206SAndrew Thompson 	ep_curr = 0;
752bdd41206SAndrew Thompson 	ep_max = 0;
753bdd41206SAndrew Thompson 
754a593f6b8SAndrew Thompson 	while ((id = usb_idesc_foreach(udev->cdesc, &ips))) {
755bdd41206SAndrew Thompson 
756bdd41206SAndrew Thompson 		iface = udev->ifaces + ips.iface_index;
757bdd41206SAndrew Thompson 
758bdd41206SAndrew Thompson 		/* check for specific interface match */
759bdd41206SAndrew Thompson 
760bdd41206SAndrew Thompson 		if (cmd == USB_CFG_INIT) {
761bdd41206SAndrew Thompson 			if ((iface_index != USB_IFACE_INDEX_ANY) &&
762bdd41206SAndrew Thompson 			    (iface_index != ips.iface_index)) {
763bdd41206SAndrew Thompson 				/* wrong interface */
764bdd41206SAndrew Thompson 				do_init = 0;
765bdd41206SAndrew Thompson 			} else if (alt_index != ips.iface_index_alt) {
766bdd41206SAndrew Thompson 				/* wrong alternate setting */
767bdd41206SAndrew Thompson 				do_init = 0;
768bdd41206SAndrew Thompson 			} else {
769bdd41206SAndrew Thompson 				/* initialise interface */
770bdd41206SAndrew Thompson 				do_init = 1;
771bdd41206SAndrew Thompson 			}
772bdd41206SAndrew Thompson 		} else
773bdd41206SAndrew Thompson 			do_init = 0;
774bdd41206SAndrew Thompson 
775bdd41206SAndrew Thompson 		/* check for new interface */
776bdd41206SAndrew Thompson 		if (ips.iface_index_alt == 0) {
777bdd41206SAndrew Thompson 			/* update current number of endpoints */
778bdd41206SAndrew Thompson 			ep_curr = ep_max;
779bdd41206SAndrew Thompson 		}
780bdd41206SAndrew Thompson 		/* check for init */
781bdd41206SAndrew Thompson 		if (do_init) {
782bdd41206SAndrew Thompson 			/* setup the USB interface structure */
783bdd41206SAndrew Thompson 			iface->idesc = id;
784bdd41206SAndrew Thompson 			/* set alternate index */
785bdd41206SAndrew Thompson 			iface->alt_index = alt_index;
786d9073c1eSHans Petter Selasky 			/* set default interface parent */
787d9073c1eSHans Petter Selasky 			if (iface_index == USB_IFACE_INDEX_ANY) {
788d9073c1eSHans Petter Selasky 				iface->parent_iface_index =
789d9073c1eSHans Petter Selasky 				    USB_IFACE_INDEX_ANY;
790d9073c1eSHans Petter Selasky 			}
791bdd41206SAndrew Thompson 		}
792bdd41206SAndrew Thompson 
793bdd41206SAndrew Thompson 		DPRINTFN(5, "found idesc nendpt=%d\n", id->bNumEndpoints);
794bdd41206SAndrew Thompson 
795760bc48eSAndrew Thompson 		ed = (struct usb_endpoint_descriptor *)id;
796bdd41206SAndrew Thompson 
797bdd41206SAndrew Thompson 		temp = ep_curr;
798bdd41206SAndrew Thompson 
799bdd41206SAndrew Thompson 		/* iterate all the endpoint descriptors */
800a593f6b8SAndrew Thompson 		while ((ed = usb_edesc_foreach(udev->cdesc, ed))) {
801bdd41206SAndrew Thompson 
8025b0752bbSHans Petter Selasky 			/* check if endpoint limit has been reached */
8035b0752bbSHans Petter Selasky 			if (temp >= USB_MAX_EP_UNITS) {
8045b0752bbSHans Petter Selasky 				DPRINTF("Endpoint limit reached\n");
8055b0752bbSHans Petter Selasky 				break;
8065b0752bbSHans Petter Selasky 			}
807bdd41206SAndrew Thompson 
808ae60fdfbSAndrew Thompson 			ep = udev->endpoints + temp;
809bdd41206SAndrew Thompson 
810bdd41206SAndrew Thompson 			if (do_init) {
811963169b4SHans Petter Selasky 				void *ecomp;
812963169b4SHans Petter Selasky 
813963169b4SHans Petter Selasky 				ecomp = usb_ed_comp_foreach(udev->cdesc, (void *)ed);
814963169b4SHans Petter Selasky 				if (ecomp != NULL)
815963169b4SHans Petter Selasky 					DPRINTFN(5, "Found endpoint companion descriptor\n");
816963169b4SHans Petter Selasky 
817a593f6b8SAndrew Thompson 				usb_init_endpoint(udev,
818963169b4SHans Petter Selasky 				    ips.iface_index, ed, ecomp, ep);
819bdd41206SAndrew Thompson 			}
820bdd41206SAndrew Thompson 
821bdd41206SAndrew Thompson 			temp ++;
822bdd41206SAndrew Thompson 
823bdd41206SAndrew Thompson 			/* find maximum number of endpoints */
824bdd41206SAndrew Thompson 			if (ep_max < temp)
825bdd41206SAndrew Thompson 				ep_max = temp;
826bdd41206SAndrew Thompson 		}
827bdd41206SAndrew Thompson 	}
828bdd41206SAndrew Thompson 
829bdd41206SAndrew Thompson 	/* NOTE: It is valid to have no interfaces and no endpoints! */
830bdd41206SAndrew Thompson 
831bdd41206SAndrew Thompson 	if (cmd == USB_CFG_ALLOC) {
832bdd41206SAndrew Thompson 		udev->ifaces_max = ips.iface_index;
8335b0752bbSHans Petter Selasky #if (USB_HAVE_FIXED_IFACE == 0)
834bdd41206SAndrew Thompson 		udev->ifaces = NULL;
835bdd41206SAndrew Thompson 		if (udev->ifaces_max != 0) {
836bdd41206SAndrew Thompson 			udev->ifaces = malloc(sizeof(*iface) * udev->ifaces_max,
837bdd41206SAndrew Thompson 			        M_USB, M_WAITOK | M_ZERO);
838bdd41206SAndrew Thompson 			if (udev->ifaces == NULL) {
839bdd41206SAndrew Thompson 				err = USB_ERR_NOMEM;
840bdd41206SAndrew Thompson 				goto done;
841bdd41206SAndrew Thompson 			}
842bdd41206SAndrew Thompson 		}
8435b0752bbSHans Petter Selasky #endif
8445b0752bbSHans Petter Selasky #if (USB_HAVE_FIXED_ENDPOINT == 0)
8459469c929SAndrew Thompson 		if (ep_max != 0) {
846ae60fdfbSAndrew Thompson 			udev->endpoints = malloc(sizeof(*ep) * ep_max,
847bdd41206SAndrew Thompson 			        M_USB, M_WAITOK | M_ZERO);
848ae60fdfbSAndrew Thompson 			if (udev->endpoints == NULL) {
849bdd41206SAndrew Thompson 				err = USB_ERR_NOMEM;
850bdd41206SAndrew Thompson 				goto done;
851bdd41206SAndrew Thompson 			}
8529469c929SAndrew Thompson 		} else {
853ae60fdfbSAndrew Thompson 			udev->endpoints = NULL;
854bdd41206SAndrew Thompson 		}
8555b0752bbSHans Petter Selasky #endif
8569469c929SAndrew Thompson 		USB_BUS_LOCK(udev->bus);
857ae60fdfbSAndrew Thompson 		udev->endpoints_max = ep_max;
8589469c929SAndrew Thompson 		/* reset any ongoing clear-stall */
859ae60fdfbSAndrew Thompson 		udev->ep_curr = NULL;
8609469c929SAndrew Thompson 		USB_BUS_UNLOCK(udev->bus);
861bdd41206SAndrew Thompson 	}
8626c218933SHans Petter Selasky #if (USB_HAVE_FIXED_IFACE == 0) || (USB_HAVE_FIXED_ENDPOINT == 0)
863bdd41206SAndrew Thompson done:
8646c218933SHans Petter Selasky #endif
865bdd41206SAndrew Thompson 	if (err) {
866bdd41206SAndrew Thompson 		if (cmd == USB_CFG_ALLOC) {
867bdd41206SAndrew Thompson cleanup:
8689469c929SAndrew Thompson 			USB_BUS_LOCK(udev->bus);
869ae60fdfbSAndrew Thompson 			udev->endpoints_max = 0;
8709469c929SAndrew Thompson 			/* reset any ongoing clear-stall */
871ae60fdfbSAndrew Thompson 			udev->ep_curr = NULL;
8729469c929SAndrew Thompson 			USB_BUS_UNLOCK(udev->bus);
8739469c929SAndrew Thompson 
8745b0752bbSHans Petter Selasky #if (USB_HAVE_FIXED_IFACE == 0)
875bdd41206SAndrew Thompson 			free(udev->ifaces, M_USB);
876bdd41206SAndrew Thompson 			udev->ifaces = NULL;
8775b0752bbSHans Petter Selasky #endif
8785b0752bbSHans Petter Selasky #if (USB_HAVE_FIXED_ENDPOINT == 0)
8795b0752bbSHans Petter Selasky 			free(udev->endpoints, M_USB);
880ae60fdfbSAndrew Thompson 			udev->endpoints = NULL;
8815b0752bbSHans Petter Selasky #endif
882bdd41206SAndrew Thompson 			udev->ifaces_max = 0;
883bdd41206SAndrew Thompson 		}
884bdd41206SAndrew Thompson 	}
885bdd41206SAndrew Thompson 	return (err);
886bdd41206SAndrew Thompson }
887bdd41206SAndrew Thompson 
888bdd41206SAndrew Thompson /*------------------------------------------------------------------------*
889a593f6b8SAndrew Thompson  *	usbd_set_alt_interface_index
89002ac6454SAndrew Thompson  *
89102ac6454SAndrew Thompson  * This function will select an alternate interface index for the
89202ac6454SAndrew Thompson  * given interface index. The interface should not be in use when this
893e2805033SAndrew Thompson  * function is called. That means there should not be any open USB
894e2805033SAndrew Thompson  * transfers. Else an error is returned. If the alternate setting is
895e2805033SAndrew Thompson  * already set this function will simply return success. This function
896e2805033SAndrew Thompson  * is called in Host mode and Device mode!
89702ac6454SAndrew Thompson  *
89802ac6454SAndrew Thompson  * Returns:
89902ac6454SAndrew Thompson  *    0: Success
90002ac6454SAndrew Thompson  * Else: Failure
90102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
902e0a69b51SAndrew Thompson usb_error_t
903a593f6b8SAndrew Thompson usbd_set_alt_interface_index(struct usb_device *udev,
90402ac6454SAndrew Thompson     uint8_t iface_index, uint8_t alt_index)
90502ac6454SAndrew Thompson {
906a593f6b8SAndrew Thompson 	struct usb_interface *iface = usbd_get_iface(udev, iface_index);
907e0a69b51SAndrew Thompson 	usb_error_t err;
90802ac6454SAndrew Thompson 	uint8_t do_unlock;
90902ac6454SAndrew Thompson 
9106950c75fSHans Petter Selasky 	/* Prevent re-enumeration */
9116950c75fSHans Petter Selasky 	do_unlock = usbd_enum_lock(udev);
9126950c75fSHans Petter Selasky 
91302ac6454SAndrew Thompson 	if (iface == NULL) {
91402ac6454SAndrew Thompson 		err = USB_ERR_INVAL;
91502ac6454SAndrew Thompson 		goto done;
91602ac6454SAndrew Thompson 	}
917e2805033SAndrew Thompson 	if (iface->alt_index == alt_index) {
918e2805033SAndrew Thompson 		/*
919e2805033SAndrew Thompson 		 * Optimise away duplicate setting of
920e2805033SAndrew Thompson 		 * alternate setting in USB Host Mode!
921e2805033SAndrew Thompson 		 */
922e2805033SAndrew Thompson 		err = 0;
923e2805033SAndrew Thompson 		goto done;
924e2805033SAndrew Thompson 	}
9250f6e8f93SAndrew Thompson #if USB_HAVE_UGEN
92602ac6454SAndrew Thompson 	/*
92702ac6454SAndrew Thompson 	 * Free all generic FIFOs for this interface, except control
92802ac6454SAndrew Thompson 	 * endpoint FIFOs:
92902ac6454SAndrew Thompson 	 */
930a593f6b8SAndrew Thompson 	usb_fifo_free_wrap(udev, iface_index, 0);
9310f6e8f93SAndrew Thompson #endif
932bdd41206SAndrew Thompson 
933a593f6b8SAndrew Thompson 	err = usb_config_parse(udev, iface_index, alt_index);
93402ac6454SAndrew Thompson 	if (err) {
93502ac6454SAndrew Thompson 		goto done;
93602ac6454SAndrew Thompson 	}
937bd73b187SAlfred Perlstein 	if (iface->alt_index != alt_index) {
938bd73b187SAlfred Perlstein 		/* the alternate setting does not exist */
939bd73b187SAlfred Perlstein 		err = USB_ERR_INVAL;
940bd73b187SAlfred Perlstein 		goto done;
941bd73b187SAlfred Perlstein 	}
942bd73b187SAlfred Perlstein 
943a593f6b8SAndrew Thompson 	err = usbd_req_set_alt_interface_no(udev, NULL, iface_index,
94402ac6454SAndrew Thompson 	    iface->idesc->bAlternateSetting);
94502ac6454SAndrew Thompson 
94602ac6454SAndrew Thompson done:
947cb18f7d1SAlfred Perlstein 	if (do_unlock)
948cb18f7d1SAlfred Perlstein 		usbd_enum_unlock(udev);
94902ac6454SAndrew Thompson 	return (err);
95002ac6454SAndrew Thompson }
95102ac6454SAndrew Thompson 
95202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
953a593f6b8SAndrew Thompson  *	usbd_set_endpoint_stall
95402ac6454SAndrew Thompson  *
955963169b4SHans Petter Selasky  * This function is used to make a BULK or INTERRUPT endpoint send
956963169b4SHans Petter Selasky  * STALL tokens in USB device mode.
95702ac6454SAndrew Thompson  *
95802ac6454SAndrew Thompson  * Returns:
95902ac6454SAndrew Thompson  *    0: Success
96002ac6454SAndrew Thompson  * Else: Failure
96102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
962e0a69b51SAndrew Thompson usb_error_t
963a593f6b8SAndrew Thompson usbd_set_endpoint_stall(struct usb_device *udev, struct usb_endpoint *ep,
96402ac6454SAndrew Thompson     uint8_t do_stall)
96502ac6454SAndrew Thompson {
966760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
967a5cf1aaaSHans Petter Selasky 	usb_stream_t x;
96802ac6454SAndrew Thompson 	uint8_t et;
96902ac6454SAndrew Thompson 	uint8_t was_stalled;
97002ac6454SAndrew Thompson 
971ae60fdfbSAndrew Thompson 	if (ep == NULL) {
97202ac6454SAndrew Thompson 		/* nothing to do */
97302ac6454SAndrew Thompson 		DPRINTF("Cannot find endpoint\n");
97402ac6454SAndrew Thompson 		/*
97502ac6454SAndrew Thompson 		 * Pretend that the clear or set stall request is
97602ac6454SAndrew Thompson 		 * successful else some USB host stacks can do
97702ac6454SAndrew Thompson 		 * strange things, especially when a control endpoint
97802ac6454SAndrew Thompson 		 * stalls.
97902ac6454SAndrew Thompson 		 */
98002ac6454SAndrew Thompson 		return (0);
98102ac6454SAndrew Thompson 	}
982ae60fdfbSAndrew Thompson 	et = (ep->edesc->bmAttributes & UE_XFERTYPE);
98302ac6454SAndrew Thompson 
98402ac6454SAndrew Thompson 	if ((et != UE_BULK) &&
98502ac6454SAndrew Thompson 	    (et != UE_INTERRUPT)) {
98602ac6454SAndrew Thompson 		/*
98702ac6454SAndrew Thompson 	         * Should not stall control
98802ac6454SAndrew Thompson 	         * nor isochronous endpoints.
98902ac6454SAndrew Thompson 	         */
99002ac6454SAndrew Thompson 		DPRINTF("Invalid endpoint\n");
99102ac6454SAndrew Thompson 		return (0);
99202ac6454SAndrew Thompson 	}
99302ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
99402ac6454SAndrew Thompson 
99502ac6454SAndrew Thompson 	/* store current stall state */
996ae60fdfbSAndrew Thompson 	was_stalled = ep->is_stalled;
99702ac6454SAndrew Thompson 
99802ac6454SAndrew Thompson 	/* check for no change */
99902ac6454SAndrew Thompson 	if (was_stalled && do_stall) {
1000ae60fdfbSAndrew Thompson 		/* if the endpoint is already stalled do nothing */
100102ac6454SAndrew Thompson 		USB_BUS_UNLOCK(udev->bus);
100202ac6454SAndrew Thompson 		DPRINTF("No change\n");
100302ac6454SAndrew Thompson 		return (0);
100402ac6454SAndrew Thompson 	}
100502ac6454SAndrew Thompson 	/* set stalled state */
1006ae60fdfbSAndrew Thompson 	ep->is_stalled = 1;
100702ac6454SAndrew Thompson 
100802ac6454SAndrew Thompson 	if (do_stall || (!was_stalled)) {
100902ac6454SAndrew Thompson 		if (!was_stalled) {
1010a5cf1aaaSHans Petter Selasky 			for (x = 0; x != USB_MAX_EP_STREAMS; x++) {
101102ac6454SAndrew Thompson 				/* lookup the current USB transfer, if any */
1012a5cf1aaaSHans Petter Selasky 				xfer = ep->endpoint_q[x].curr;
1013a5cf1aaaSHans Petter Selasky 				if (xfer != NULL) {
101402ac6454SAndrew Thompson 					/*
1015a5cf1aaaSHans Petter Selasky 					 * The "xfer_stall" method
1016a5cf1aaaSHans Petter Selasky 					 * will complete the USB
1017a5cf1aaaSHans Petter Selasky 					 * transfer like in case of a
1018a5cf1aaaSHans Petter Selasky 					 * timeout setting the error
1019a5cf1aaaSHans Petter Selasky 					 * code "USB_ERR_STALLED".
102002ac6454SAndrew Thompson 					 */
1021a5cf1aaaSHans Petter Selasky 					(udev->bus->methods->xfer_stall) (xfer);
1022a5cf1aaaSHans Petter Selasky 				}
1023a5cf1aaaSHans Petter Selasky 			}
1024a5cf1aaaSHans Petter Selasky 		}
1025a5cf1aaaSHans Petter Selasky 		(udev->bus->methods->set_stall) (udev, ep, &do_stall);
102602ac6454SAndrew Thompson 	}
102702ac6454SAndrew Thompson 	if (!do_stall) {
1028ae60fdfbSAndrew Thompson 		ep->toggle_next = 0;	/* reset data toggle */
1029ae60fdfbSAndrew Thompson 		ep->is_stalled = 0;	/* clear stalled state */
103002ac6454SAndrew Thompson 
1031ae60fdfbSAndrew Thompson 		(udev->bus->methods->clear_stall) (udev, ep);
103202ac6454SAndrew Thompson 
1033a5cf1aaaSHans Petter Selasky 		/* start the current or next transfer, if any */
1034a5cf1aaaSHans Petter Selasky 		for (x = 0; x != USB_MAX_EP_STREAMS; x++) {
1035a5cf1aaaSHans Petter Selasky 			usb_command_wrapper(&ep->endpoint_q[x],
1036a5cf1aaaSHans Petter Selasky 			    ep->endpoint_q[x].curr);
1037a5cf1aaaSHans Petter Selasky 		}
103802ac6454SAndrew Thompson 	}
103902ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
104002ac6454SAndrew Thompson 	return (0);
104102ac6454SAndrew Thompson }
104202ac6454SAndrew Thompson 
104302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1044a593f6b8SAndrew Thompson  *	usb_reset_iface_endpoints - used in USB device side mode
104502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1046e0a69b51SAndrew Thompson usb_error_t
1047a593f6b8SAndrew Thompson usb_reset_iface_endpoints(struct usb_device *udev, uint8_t iface_index)
104802ac6454SAndrew Thompson {
1049ae60fdfbSAndrew Thompson 	struct usb_endpoint *ep;
1050ae60fdfbSAndrew Thompson 	struct usb_endpoint *ep_end;
105102ac6454SAndrew Thompson 
1052ae60fdfbSAndrew Thompson 	ep = udev->endpoints;
1053ae60fdfbSAndrew Thompson 	ep_end = udev->endpoints + udev->endpoints_max;
105402ac6454SAndrew Thompson 
1055ae60fdfbSAndrew Thompson 	for (; ep != ep_end; ep++) {
105602ac6454SAndrew Thompson 
1057ae60fdfbSAndrew Thompson 		if ((ep->edesc == NULL) ||
1058ae60fdfbSAndrew Thompson 		    (ep->iface_index != iface_index)) {
105902ac6454SAndrew Thompson 			continue;
106002ac6454SAndrew Thompson 		}
106102ac6454SAndrew Thompson 		/* simulate a clear stall from the peer */
1062bd73b187SAlfred Perlstein 		usbd_set_endpoint_stall(udev, ep, 0);
106302ac6454SAndrew Thompson 	}
106402ac6454SAndrew Thompson 	return (0);
106502ac6454SAndrew Thompson }
106602ac6454SAndrew Thompson 
106702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1068a593f6b8SAndrew Thompson  *	usb_detach_device_sub
106902ac6454SAndrew Thompson  *
107002ac6454SAndrew Thompson  * This function will try to detach an USB device. If it fails a panic
107102ac6454SAndrew Thompson  * will result.
1072bdd41206SAndrew Thompson  *
1073bdd41206SAndrew Thompson  * Flag values, see "USB_UNCFG_FLAG_XXX".
107402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
107502ac6454SAndrew Thompson static void
1076a593f6b8SAndrew Thompson usb_detach_device_sub(struct usb_device *udev, device_t *ppdev,
10778427ed84SHans Petter Selasky     char **ppnpinfo, uint8_t flag)
107802ac6454SAndrew Thompson {
107902ac6454SAndrew Thompson 	device_t dev;
10808427ed84SHans Petter Selasky 	char *pnpinfo;
108102ac6454SAndrew Thompson 	int err;
108202ac6454SAndrew Thompson 
1083d88688c7SAndrew Thompson 	dev = *ppdev;
1084d88688c7SAndrew Thompson 	if (dev) {
108502ac6454SAndrew Thompson 		/*
108602ac6454SAndrew Thompson 		 * NOTE: It is important to clear "*ppdev" before deleting
108702ac6454SAndrew Thompson 		 * the child due to some device methods being called late
108802ac6454SAndrew Thompson 		 * during the delete process !
108902ac6454SAndrew Thompson 		 */
109002ac6454SAndrew Thompson 		*ppdev = NULL;
109102ac6454SAndrew Thompson 
1092cabe79d9SMarius Strobl 		if (!rebooting) {
109302ac6454SAndrew Thompson 			device_printf(dev, "at %s, port %d, addr %d "
109402ac6454SAndrew Thompson 			    "(disconnected)\n",
109502ac6454SAndrew Thompson 			    device_get_nameunit(udev->parent_dev),
109602ac6454SAndrew Thompson 			    udev->port_no, udev->address);
1097cabe79d9SMarius Strobl 		}
109802ac6454SAndrew Thompson 
109902ac6454SAndrew Thompson 		if (device_is_attached(dev)) {
1100ec8f3127SAndrew Thompson 			if (udev->flags.peer_suspended) {
110102ac6454SAndrew Thompson 				err = DEVICE_RESUME(dev);
110202ac6454SAndrew Thompson 				if (err) {
1103767cb2e2SAndrew Thompson 					device_printf(dev, "Resume failed\n");
110402ac6454SAndrew Thompson 				}
110502ac6454SAndrew Thompson 			}
110602ac6454SAndrew Thompson 			if (device_detach(dev)) {
110702ac6454SAndrew Thompson 				goto error;
110802ac6454SAndrew Thompson 			}
110902ac6454SAndrew Thompson 		}
111002ac6454SAndrew Thompson 		if (device_delete_child(udev->parent_dev, dev)) {
111102ac6454SAndrew Thompson 			goto error;
111202ac6454SAndrew Thompson 		}
111302ac6454SAndrew Thompson 	}
11148427ed84SHans Petter Selasky 
11158427ed84SHans Petter Selasky 	pnpinfo = *ppnpinfo;
11168427ed84SHans Petter Selasky 	if (pnpinfo != NULL) {
11178427ed84SHans Petter Selasky 		*ppnpinfo = NULL;
11188427ed84SHans Petter Selasky 		free(pnpinfo, M_USBDEV);
11198427ed84SHans Petter Selasky 	}
112002ac6454SAndrew Thompson 	return;
112102ac6454SAndrew Thompson 
112202ac6454SAndrew Thompson error:
112302ac6454SAndrew Thompson 	/* Detach is not allowed to fail in the USB world */
11248427ed84SHans Petter Selasky 	panic("usb_detach_device_sub: A USB driver would not detach\n");
112502ac6454SAndrew Thompson }
112602ac6454SAndrew Thompson 
112702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1128a593f6b8SAndrew Thompson  *	usb_detach_device
112902ac6454SAndrew Thompson  *
113002ac6454SAndrew Thompson  * The following function will detach the matching interfaces.
113102ac6454SAndrew Thompson  * This function is NULL safe.
1132bdd41206SAndrew Thompson  *
1133bdd41206SAndrew Thompson  * Flag values, see "USB_UNCFG_FLAG_XXX".
113402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
113502ac6454SAndrew Thompson void
1136a593f6b8SAndrew Thompson usb_detach_device(struct usb_device *udev, uint8_t iface_index,
1137bdd41206SAndrew Thompson     uint8_t flag)
113802ac6454SAndrew Thompson {
1139760bc48eSAndrew Thompson 	struct usb_interface *iface;
114002ac6454SAndrew Thompson 	uint8_t i;
114102ac6454SAndrew Thompson 
114202ac6454SAndrew Thompson 	if (udev == NULL) {
114302ac6454SAndrew Thompson 		/* nothing to do */
114402ac6454SAndrew Thompson 		return;
114502ac6454SAndrew Thompson 	}
114602ac6454SAndrew Thompson 	DPRINTFN(4, "udev=%p\n", udev);
114702ac6454SAndrew Thompson 
114891cd9240SAndrew Thompson 	sx_assert(&udev->enum_sx, SA_LOCKED);
114902ac6454SAndrew Thompson 
115002ac6454SAndrew Thompson 	/*
115102ac6454SAndrew Thompson 	 * First detach the child to give the child's detach routine a
115202ac6454SAndrew Thompson 	 * chance to detach the sub-devices in the correct order.
115302ac6454SAndrew Thompson 	 * Then delete the child using "device_delete_child()" which
115402ac6454SAndrew Thompson 	 * will detach all sub-devices from the bottom and upwards!
115502ac6454SAndrew Thompson 	 */
115602ac6454SAndrew Thompson 	if (iface_index != USB_IFACE_INDEX_ANY) {
115702ac6454SAndrew Thompson 		i = iface_index;
115802ac6454SAndrew Thompson 		iface_index = i + 1;
115902ac6454SAndrew Thompson 	} else {
116002ac6454SAndrew Thompson 		i = 0;
116102ac6454SAndrew Thompson 		iface_index = USB_IFACE_MAX;
116202ac6454SAndrew Thompson 	}
116302ac6454SAndrew Thompson 
116402ac6454SAndrew Thompson 	/* do the detach */
116502ac6454SAndrew Thompson 
116602ac6454SAndrew Thompson 	for (; i != iface_index; i++) {
116702ac6454SAndrew Thompson 
1168a593f6b8SAndrew Thompson 		iface = usbd_get_iface(udev, i);
116902ac6454SAndrew Thompson 		if (iface == NULL) {
117002ac6454SAndrew Thompson 			/* looks like the end of the USB interfaces */
117102ac6454SAndrew Thompson 			break;
117202ac6454SAndrew Thompson 		}
11738427ed84SHans Petter Selasky 		usb_detach_device_sub(udev, &iface->subdev,
11748427ed84SHans Petter Selasky 		    &iface->pnpinfo, flag);
117502ac6454SAndrew Thompson 	}
117602ac6454SAndrew Thompson }
117702ac6454SAndrew Thompson 
117802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1179a593f6b8SAndrew Thompson  *	usb_probe_and_attach_sub
118002ac6454SAndrew Thompson  *
118102ac6454SAndrew Thompson  * Returns:
118202ac6454SAndrew Thompson  *    0: Success
118302ac6454SAndrew Thompson  * Else: Failure
118402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
118502ac6454SAndrew Thompson static uint8_t
1186a593f6b8SAndrew Thompson usb_probe_and_attach_sub(struct usb_device *udev,
1187760bc48eSAndrew Thompson     struct usb_attach_arg *uaa)
118802ac6454SAndrew Thompson {
1189760bc48eSAndrew Thompson 	struct usb_interface *iface;
119002ac6454SAndrew Thompson 	device_t dev;
119102ac6454SAndrew Thompson 	int err;
119202ac6454SAndrew Thompson 
119302ac6454SAndrew Thompson 	iface = uaa->iface;
119402ac6454SAndrew Thompson 	if (iface->parent_iface_index != USB_IFACE_INDEX_ANY) {
119502ac6454SAndrew Thompson 		/* leave interface alone */
119602ac6454SAndrew Thompson 		return (0);
119702ac6454SAndrew Thompson 	}
119802ac6454SAndrew Thompson 	dev = iface->subdev;
119902ac6454SAndrew Thompson 	if (dev) {
120002ac6454SAndrew Thompson 
120102ac6454SAndrew Thompson 		/* clean up after module unload */
120202ac6454SAndrew Thompson 
120302ac6454SAndrew Thompson 		if (device_is_attached(dev)) {
120402ac6454SAndrew Thompson 			/* already a device there */
120502ac6454SAndrew Thompson 			return (0);
120602ac6454SAndrew Thompson 		}
120702ac6454SAndrew Thompson 		/* clear "iface->subdev" as early as possible */
120802ac6454SAndrew Thompson 
120902ac6454SAndrew Thompson 		iface->subdev = NULL;
121002ac6454SAndrew Thompson 
121102ac6454SAndrew Thompson 		if (device_delete_child(udev->parent_dev, dev)) {
121202ac6454SAndrew Thompson 
121302ac6454SAndrew Thompson 			/*
121402ac6454SAndrew Thompson 			 * Panic here, else one can get a double call
121502ac6454SAndrew Thompson 			 * to device_detach().  USB devices should
121602ac6454SAndrew Thompson 			 * never fail on detach!
121702ac6454SAndrew Thompson 			 */
1218767cb2e2SAndrew Thompson 			panic("device_delete_child() failed\n");
121902ac6454SAndrew Thompson 		}
122002ac6454SAndrew Thompson 	}
122102ac6454SAndrew Thompson 	if (uaa->temp_dev == NULL) {
122202ac6454SAndrew Thompson 
122302ac6454SAndrew Thompson 		/* create a new child */
122402ac6454SAndrew Thompson 		uaa->temp_dev = device_add_child(udev->parent_dev, NULL, -1);
122502ac6454SAndrew Thompson 		if (uaa->temp_dev == NULL) {
122602ac6454SAndrew Thompson 			device_printf(udev->parent_dev,
1227767cb2e2SAndrew Thompson 			    "Device creation failed\n");
122802ac6454SAndrew Thompson 			return (1);	/* failure */
122902ac6454SAndrew Thompson 		}
123002ac6454SAndrew Thompson 		device_set_ivars(uaa->temp_dev, uaa);
123102ac6454SAndrew Thompson 		device_quiet(uaa->temp_dev);
123202ac6454SAndrew Thompson 	}
123302ac6454SAndrew Thompson 	/*
123402ac6454SAndrew Thompson 	 * Set "subdev" before probe and attach so that "devd" gets
123502ac6454SAndrew Thompson 	 * the information it needs.
123602ac6454SAndrew Thompson 	 */
123702ac6454SAndrew Thompson 	iface->subdev = uaa->temp_dev;
123802ac6454SAndrew Thompson 
123902ac6454SAndrew Thompson 	if (device_probe_and_attach(iface->subdev) == 0) {
124002ac6454SAndrew Thompson 		/*
124102ac6454SAndrew Thompson 		 * The USB attach arguments are only available during probe
124202ac6454SAndrew Thompson 		 * and attach !
124302ac6454SAndrew Thompson 		 */
124402ac6454SAndrew Thompson 		uaa->temp_dev = NULL;
124502ac6454SAndrew Thompson 		device_set_ivars(iface->subdev, NULL);
124602ac6454SAndrew Thompson 
1247ec8f3127SAndrew Thompson 		if (udev->flags.peer_suspended) {
124802ac6454SAndrew Thompson 			err = DEVICE_SUSPEND(iface->subdev);
124988b4e0abSAndrew Thompson 			if (err)
125002ac6454SAndrew Thompson 				device_printf(iface->subdev, "Suspend failed\n");
125102ac6454SAndrew Thompson 		}
125202ac6454SAndrew Thompson 		return (0);		/* success */
125302ac6454SAndrew Thompson 	} else {
125402ac6454SAndrew Thompson 		/* No USB driver found */
125502ac6454SAndrew Thompson 		iface->subdev = NULL;
125602ac6454SAndrew Thompson 	}
125702ac6454SAndrew Thompson 	return (1);			/* failure */
125802ac6454SAndrew Thompson }
125902ac6454SAndrew Thompson 
126002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1261a593f6b8SAndrew Thompson  *	usbd_set_parent_iface
126202ac6454SAndrew Thompson  *
126302ac6454SAndrew Thompson  * Using this function will lock the alternate interface setting on an
126402ac6454SAndrew Thompson  * interface. It is typically used for multi interface drivers. In USB
126502ac6454SAndrew Thompson  * device side mode it is assumed that the alternate interfaces all
126602ac6454SAndrew Thompson  * have the same endpoint descriptors. The default parent index value
126702ac6454SAndrew Thompson  * is "USB_IFACE_INDEX_ANY". Then the alternate setting value is not
126802ac6454SAndrew Thompson  * locked.
126902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
127002ac6454SAndrew Thompson void
1271a593f6b8SAndrew Thompson usbd_set_parent_iface(struct usb_device *udev, uint8_t iface_index,
127202ac6454SAndrew Thompson     uint8_t parent_index)
127302ac6454SAndrew Thompson {
1274760bc48eSAndrew Thompson 	struct usb_interface *iface;
127502ac6454SAndrew Thompson 
1276d9073c1eSHans Petter Selasky 	if (udev == NULL) {
1277d9073c1eSHans Petter Selasky 		/* nothing to do */
1278d9073c1eSHans Petter Selasky 		return;
127902ac6454SAndrew Thompson 	}
1280d9073c1eSHans Petter Selasky 	iface = usbd_get_iface(udev, iface_index);
1281d9073c1eSHans Petter Selasky 	if (iface != NULL)
1282d9073c1eSHans Petter Selasky 		iface->parent_iface_index = parent_index;
128302ac6454SAndrew Thompson }
128402ac6454SAndrew Thompson 
128502ac6454SAndrew Thompson static void
1286a593f6b8SAndrew Thompson usb_init_attach_arg(struct usb_device *udev,
1287760bc48eSAndrew Thompson     struct usb_attach_arg *uaa)
128802ac6454SAndrew Thompson {
1289d46dc4adSHans Petter Selasky 	memset(uaa, 0, sizeof(*uaa));
129002ac6454SAndrew Thompson 
129102ac6454SAndrew Thompson 	uaa->device = udev;
1292f29a0724SAndrew Thompson 	uaa->usb_mode = udev->flags.usb_mode;
129302ac6454SAndrew Thompson 	uaa->port = udev->port_no;
12942a4c6157SAndrew Thompson 	uaa->dev_state = UAA_DEV_READY;
129502ac6454SAndrew Thompson 
129602ac6454SAndrew Thompson 	uaa->info.idVendor = UGETW(udev->ddesc.idVendor);
129702ac6454SAndrew Thompson 	uaa->info.idProduct = UGETW(udev->ddesc.idProduct);
129802ac6454SAndrew Thompson 	uaa->info.bcdDevice = UGETW(udev->ddesc.bcdDevice);
129902ac6454SAndrew Thompson 	uaa->info.bDeviceClass = udev->ddesc.bDeviceClass;
130002ac6454SAndrew Thompson 	uaa->info.bDeviceSubClass = udev->ddesc.bDeviceSubClass;
130102ac6454SAndrew Thompson 	uaa->info.bDeviceProtocol = udev->ddesc.bDeviceProtocol;
130202ac6454SAndrew Thompson 	uaa->info.bConfigIndex = udev->curr_config_index;
130302ac6454SAndrew Thompson 	uaa->info.bConfigNum = udev->curr_config_no;
130402ac6454SAndrew Thompson }
130502ac6454SAndrew Thompson 
130602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1307a593f6b8SAndrew Thompson  *	usb_probe_and_attach
130802ac6454SAndrew Thompson  *
130902ac6454SAndrew Thompson  * This function is called from "uhub_explore_sub()",
1310a593f6b8SAndrew Thompson  * "usb_handle_set_config()" and "usb_handle_request()".
131102ac6454SAndrew Thompson  *
131202ac6454SAndrew Thompson  * Returns:
131302ac6454SAndrew Thompson  *    0: Success
131402ac6454SAndrew Thompson  * Else: A control transfer failed
131502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1316e0a69b51SAndrew Thompson usb_error_t
1317a593f6b8SAndrew Thompson usb_probe_and_attach(struct usb_device *udev, uint8_t iface_index)
131802ac6454SAndrew Thompson {
1319760bc48eSAndrew Thompson 	struct usb_attach_arg uaa;
1320760bc48eSAndrew Thompson 	struct usb_interface *iface;
132102ac6454SAndrew Thompson 	uint8_t i;
132202ac6454SAndrew Thompson 	uint8_t j;
132302ac6454SAndrew Thompson 	uint8_t do_unlock;
132402ac6454SAndrew Thompson 
132502ac6454SAndrew Thompson 	if (udev == NULL) {
132602ac6454SAndrew Thompson 		DPRINTF("udev == NULL\n");
132702ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
132802ac6454SAndrew Thompson 	}
13296950c75fSHans Petter Selasky 	/* Prevent re-enumeration */
13306950c75fSHans Petter Selasky 	do_unlock = usbd_enum_lock(udev);
133102ac6454SAndrew Thompson 
133202ac6454SAndrew Thompson 	if (udev->curr_config_index == USB_UNCONFIG_INDEX) {
133302ac6454SAndrew Thompson 		/* do nothing - no configuration has been set */
133402ac6454SAndrew Thompson 		goto done;
133502ac6454SAndrew Thompson 	}
133602ac6454SAndrew Thompson 	/* setup USB attach arguments */
133702ac6454SAndrew Thompson 
1338a593f6b8SAndrew Thompson 	usb_init_attach_arg(udev, &uaa);
133902ac6454SAndrew Thompson 
1340a6b60150SHans Petter Selasky 	/*
1341a6b60150SHans Petter Selasky 	 * If the whole USB device is targeted, invoke the USB event
1342a6b60150SHans Petter Selasky 	 * handler(s):
1343a6b60150SHans Petter Selasky 	 */
1344a6b60150SHans Petter Selasky 	if (iface_index == USB_IFACE_INDEX_ANY) {
1345a6b60150SHans Petter Selasky 
1346415bcd89SHans Petter Selasky 		if (usb_test_quirk(&uaa, UQ_MSC_DYMO_EJECT) != 0 &&
1347415bcd89SHans Petter Selasky 		    usb_dymo_eject(udev, 0) == 0) {
1348415bcd89SHans Petter Selasky 			/* success, mark the udev as disappearing */
1349415bcd89SHans Petter Selasky 			uaa.dev_state = UAA_DEV_EJECTING;
1350415bcd89SHans Petter Selasky 		}
1351415bcd89SHans Petter Selasky 
1352a6b60150SHans Petter Selasky 		EVENTHANDLER_INVOKE(usb_dev_configured, udev, &uaa);
1353a6b60150SHans Petter Selasky 
1354a6b60150SHans Petter Selasky 		if (uaa.dev_state != UAA_DEV_READY) {
1355a6b60150SHans Petter Selasky 			/* leave device unconfigured */
1356a6b60150SHans Petter Selasky 			usb_unconfigure(udev, 0);
1357a6b60150SHans Petter Selasky 			goto done;
1358a6b60150SHans Petter Selasky 		}
1359a6b60150SHans Petter Selasky 	}
1360a6b60150SHans Petter Selasky 
136102ac6454SAndrew Thompson 	/* Check if only one interface should be probed: */
136202ac6454SAndrew Thompson 	if (iface_index != USB_IFACE_INDEX_ANY) {
136302ac6454SAndrew Thompson 		i = iface_index;
136402ac6454SAndrew Thompson 		j = i + 1;
136502ac6454SAndrew Thompson 	} else {
136602ac6454SAndrew Thompson 		i = 0;
136702ac6454SAndrew Thompson 		j = USB_IFACE_MAX;
136802ac6454SAndrew Thompson 	}
136902ac6454SAndrew Thompson 
137002ac6454SAndrew Thompson 	/* Do the probe and attach */
137102ac6454SAndrew Thompson 	for (; i != j; i++) {
137202ac6454SAndrew Thompson 
1373a593f6b8SAndrew Thompson 		iface = usbd_get_iface(udev, i);
137402ac6454SAndrew Thompson 		if (iface == NULL) {
137502ac6454SAndrew Thompson 			/*
137602ac6454SAndrew Thompson 			 * Looks like the end of the USB
137702ac6454SAndrew Thompson 			 * interfaces !
137802ac6454SAndrew Thompson 			 */
137902ac6454SAndrew Thompson 			DPRINTFN(2, "end of interfaces "
138002ac6454SAndrew Thompson 			    "at %u\n", i);
138102ac6454SAndrew Thompson 			break;
138202ac6454SAndrew Thompson 		}
138302ac6454SAndrew Thompson 		if (iface->idesc == NULL) {
138402ac6454SAndrew Thompson 			/* no interface descriptor */
138502ac6454SAndrew Thompson 			continue;
138602ac6454SAndrew Thompson 		}
138702ac6454SAndrew Thompson 		uaa.iface = iface;
138802ac6454SAndrew Thompson 
138902ac6454SAndrew Thompson 		uaa.info.bInterfaceClass =
139002ac6454SAndrew Thompson 		    iface->idesc->bInterfaceClass;
139102ac6454SAndrew Thompson 		uaa.info.bInterfaceSubClass =
139202ac6454SAndrew Thompson 		    iface->idesc->bInterfaceSubClass;
139302ac6454SAndrew Thompson 		uaa.info.bInterfaceProtocol =
139402ac6454SAndrew Thompson 		    iface->idesc->bInterfaceProtocol;
139502ac6454SAndrew Thompson 		uaa.info.bIfaceIndex = i;
139602ac6454SAndrew Thompson 		uaa.info.bIfaceNum =
139702ac6454SAndrew Thompson 		    iface->idesc->bInterfaceNumber;
1398bd73b187SAlfred Perlstein 		uaa.driver_info = 0;	/* reset driver_info */
139902ac6454SAndrew Thompson 
140002ac6454SAndrew Thompson 		DPRINTFN(2, "iclass=%u/%u/%u iindex=%u/%u\n",
140102ac6454SAndrew Thompson 		    uaa.info.bInterfaceClass,
140202ac6454SAndrew Thompson 		    uaa.info.bInterfaceSubClass,
140302ac6454SAndrew Thompson 		    uaa.info.bInterfaceProtocol,
140402ac6454SAndrew Thompson 		    uaa.info.bIfaceIndex,
140502ac6454SAndrew Thompson 		    uaa.info.bIfaceNum);
140602ac6454SAndrew Thompson 
1407d43ffe94SHans Petter Selasky 		usb_probe_and_attach_sub(udev, &uaa);
140802ac6454SAndrew Thompson 
1409d43ffe94SHans Petter Selasky 		/*
1410d43ffe94SHans Petter Selasky 		 * Remove the leftover child, if any, to enforce that
1411d43ffe94SHans Petter Selasky 		 * a new nomatch devd event is generated for the next
1412d43ffe94SHans Petter Selasky 		 * interface if no driver is found:
1413d43ffe94SHans Petter Selasky 		 */
1414d43ffe94SHans Petter Selasky 		if (uaa.temp_dev == NULL)
1415d43ffe94SHans Petter Selasky 			continue;
1416d43ffe94SHans Petter Selasky 		if (device_delete_child(udev->parent_dev, uaa.temp_dev))
1417767cb2e2SAndrew Thompson 			DPRINTFN(0, "device delete child failed\n");
1418d43ffe94SHans Petter Selasky 		uaa.temp_dev = NULL;
141902ac6454SAndrew Thompson 	}
142002ac6454SAndrew Thompson done:
1421cb18f7d1SAlfred Perlstein 	if (do_unlock)
1422cb18f7d1SAlfred Perlstein 		usbd_enum_unlock(udev);
142302ac6454SAndrew Thompson 	return (0);
142402ac6454SAndrew Thompson }
142502ac6454SAndrew Thompson 
142602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1427a593f6b8SAndrew Thompson  *	usb_suspend_resume_sub
142802ac6454SAndrew Thompson  *
142902ac6454SAndrew Thompson  * This function is called when the suspend or resume methods should
143002ac6454SAndrew Thompson  * be executed on an USB device.
143102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
143202ac6454SAndrew Thompson static void
1433a593f6b8SAndrew Thompson usb_suspend_resume_sub(struct usb_device *udev, device_t dev, uint8_t do_suspend)
143402ac6454SAndrew Thompson {
143502ac6454SAndrew Thompson 	int err;
143602ac6454SAndrew Thompson 
143702ac6454SAndrew Thompson 	if (dev == NULL) {
143802ac6454SAndrew Thompson 		return;
143902ac6454SAndrew Thompson 	}
144002ac6454SAndrew Thompson 	if (!device_is_attached(dev)) {
144102ac6454SAndrew Thompson 		return;
144202ac6454SAndrew Thompson 	}
144302ac6454SAndrew Thompson 	if (do_suspend) {
144402ac6454SAndrew Thompson 		err = DEVICE_SUSPEND(dev);
144502ac6454SAndrew Thompson 	} else {
144602ac6454SAndrew Thompson 		err = DEVICE_RESUME(dev);
144702ac6454SAndrew Thompson 	}
144802ac6454SAndrew Thompson 	if (err) {
1449767cb2e2SAndrew Thompson 		device_printf(dev, "%s failed\n",
145002ac6454SAndrew Thompson 		    do_suspend ? "Suspend" : "Resume");
145102ac6454SAndrew Thompson 	}
145202ac6454SAndrew Thompson }
145302ac6454SAndrew Thompson 
145402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1455a593f6b8SAndrew Thompson  *	usb_suspend_resume
145602ac6454SAndrew Thompson  *
145702ac6454SAndrew Thompson  * The following function will suspend or resume the USB device.
145802ac6454SAndrew Thompson  *
145902ac6454SAndrew Thompson  * Returns:
146002ac6454SAndrew Thompson  *    0: Success
146102ac6454SAndrew Thompson  * Else: Failure
146202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1463e0a69b51SAndrew Thompson usb_error_t
1464a593f6b8SAndrew Thompson usb_suspend_resume(struct usb_device *udev, uint8_t do_suspend)
146502ac6454SAndrew Thompson {
1466760bc48eSAndrew Thompson 	struct usb_interface *iface;
146702ac6454SAndrew Thompson 	uint8_t i;
146802ac6454SAndrew Thompson 
146902ac6454SAndrew Thompson 	if (udev == NULL) {
147002ac6454SAndrew Thompson 		/* nothing to do */
147102ac6454SAndrew Thompson 		return (0);
147202ac6454SAndrew Thompson 	}
147302ac6454SAndrew Thompson 	DPRINTFN(4, "udev=%p do_suspend=%d\n", udev, do_suspend);
147402ac6454SAndrew Thompson 
14752df1e9a6SAndrew Thompson 	sx_assert(&udev->sr_sx, SA_LOCKED);
147602ac6454SAndrew Thompson 
147702ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
147802ac6454SAndrew Thompson 	/* filter the suspend events */
1479ec8f3127SAndrew Thompson 	if (udev->flags.peer_suspended == do_suspend) {
148002ac6454SAndrew Thompson 		USB_BUS_UNLOCK(udev->bus);
148102ac6454SAndrew Thompson 		/* nothing to do */
148202ac6454SAndrew Thompson 		return (0);
148302ac6454SAndrew Thompson 	}
1484ec8f3127SAndrew Thompson 	udev->flags.peer_suspended = do_suspend;
148502ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
148602ac6454SAndrew Thompson 
148702ac6454SAndrew Thompson 	/* do the suspend or resume */
148802ac6454SAndrew Thompson 
148902ac6454SAndrew Thompson 	for (i = 0; i != USB_IFACE_MAX; i++) {
149002ac6454SAndrew Thompson 
1491a593f6b8SAndrew Thompson 		iface = usbd_get_iface(udev, i);
149202ac6454SAndrew Thompson 		if (iface == NULL) {
149302ac6454SAndrew Thompson 			/* looks like the end of the USB interfaces */
149402ac6454SAndrew Thompson 			break;
149502ac6454SAndrew Thompson 		}
1496a593f6b8SAndrew Thompson 		usb_suspend_resume_sub(udev, iface->subdev, do_suspend);
149702ac6454SAndrew Thompson 	}
149802ac6454SAndrew Thompson 	return (0);
149902ac6454SAndrew Thompson }
150002ac6454SAndrew Thompson 
150102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1502a593f6b8SAndrew Thompson  *      usbd_clear_stall_proc
150302ac6454SAndrew Thompson  *
150402ac6454SAndrew Thompson  * This function performs generic USB clear stall operations.
150502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
150602ac6454SAndrew Thompson static void
1507a593f6b8SAndrew Thompson usbd_clear_stall_proc(struct usb_proc_msg *_pm)
150802ac6454SAndrew Thompson {
1509a0d53e0bSHans Petter Selasky 	struct usb_udev_msg *pm = (void *)_pm;
1510760bc48eSAndrew Thompson 	struct usb_device *udev = pm->udev;
151102ac6454SAndrew Thompson 
151202ac6454SAndrew Thompson 	/* Change lock */
151302ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
151491cd9240SAndrew Thompson 	mtx_lock(&udev->device_mtx);
151502ac6454SAndrew Thompson 
151602ac6454SAndrew Thompson 	/* Start clear stall callback */
15175b3bb704SAndrew Thompson 	usbd_transfer_start(udev->ctrl_xfer[1]);
151802ac6454SAndrew Thompson 
151902ac6454SAndrew Thompson 	/* Change lock */
152091cd9240SAndrew Thompson 	mtx_unlock(&udev->device_mtx);
152102ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
152202ac6454SAndrew Thompson }
152302ac6454SAndrew Thompson 
152402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1525a593f6b8SAndrew Thompson  *	usb_alloc_device
152602ac6454SAndrew Thompson  *
152702ac6454SAndrew Thompson  * This function allocates a new USB device. This function is called
152802ac6454SAndrew Thompson  * when a new device has been put in the powered state, but not yet in
152902ac6454SAndrew Thompson  * the addressed state. Get initial descriptor, set the address, get
153002ac6454SAndrew Thompson  * full descriptor and get strings.
153102ac6454SAndrew Thompson  *
153202ac6454SAndrew Thompson  * Return values:
153302ac6454SAndrew Thompson  *    0: Failure
153402ac6454SAndrew Thompson  * Else: Success
153502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1536760bc48eSAndrew Thompson struct usb_device *
1537a593f6b8SAndrew Thompson usb_alloc_device(device_t parent_dev, struct usb_bus *bus,
1538760bc48eSAndrew Thompson     struct usb_device *parent_hub, uint8_t depth, uint8_t port_index,
15398d2dd5ddSAndrew Thompson     uint8_t port_no, enum usb_dev_speed speed, enum usb_hc_mode mode)
154002ac6454SAndrew Thompson {
1541760bc48eSAndrew Thompson 	struct usb_attach_arg uaa;
1542760bc48eSAndrew Thompson 	struct usb_device *udev;
1543760bc48eSAndrew Thompson 	struct usb_device *adev;
1544760bc48eSAndrew Thompson 	struct usb_device *hub;
154502ac6454SAndrew Thompson 	uint8_t *scratch_ptr;
1546e0a69b51SAndrew Thompson 	usb_error_t err;
154702ac6454SAndrew Thompson 	uint8_t device_index;
15482a4c6157SAndrew Thompson 	uint8_t config_index;
15492a4c6157SAndrew Thompson 	uint8_t config_quirk;
15502a4c6157SAndrew Thompson 	uint8_t set_config_failed;
15516950c75fSHans Petter Selasky 	uint8_t do_unlock;
155202ac6454SAndrew Thompson 
155302ac6454SAndrew Thompson 	DPRINTF("parent_dev=%p, bus=%p, parent_hub=%p, depth=%u, "
1554f29a0724SAndrew Thompson 	    "port_index=%u, port_no=%u, speed=%u, usb_mode=%u\n",
155502ac6454SAndrew Thompson 	    parent_dev, bus, parent_hub, depth, port_index, port_no,
1556f29a0724SAndrew Thompson 	    speed, mode);
155702ac6454SAndrew Thompson 
155802ac6454SAndrew Thompson 	/*
155902ac6454SAndrew Thompson 	 * Find an unused device index. In USB Host mode this is the
156002ac6454SAndrew Thompson 	 * same as the device address.
156102ac6454SAndrew Thompson 	 *
156202ac6454SAndrew Thompson 	 * Device index zero is not used and device index 1 should
156302ac6454SAndrew Thompson 	 * always be the root hub.
156402ac6454SAndrew Thompson 	 */
156502ac6454SAndrew Thompson 	for (device_index = USB_ROOT_HUB_ADDR;
156602ac6454SAndrew Thompson 	    (device_index != bus->devices_max) &&
156702ac6454SAndrew Thompson 	    (bus->devices[device_index] != NULL);
156802ac6454SAndrew Thompson 	    device_index++) /* nop */;
156902ac6454SAndrew Thompson 
157002ac6454SAndrew Thompson 	if (device_index == bus->devices_max) {
157102ac6454SAndrew Thompson 		device_printf(bus->bdev,
1572767cb2e2SAndrew Thompson 		    "No free USB device index for new device\n");
157302ac6454SAndrew Thompson 		return (NULL);
157402ac6454SAndrew Thompson 	}
157502ac6454SAndrew Thompson 
157602ac6454SAndrew Thompson 	if (depth > 0x10) {
157702ac6454SAndrew Thompson 		device_printf(bus->bdev,
1578767cb2e2SAndrew Thompson 		    "Invalid device depth\n");
157902ac6454SAndrew Thompson 		return (NULL);
158002ac6454SAndrew Thompson 	}
158102ac6454SAndrew Thompson 	udev = malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO);
158202ac6454SAndrew Thompson 	if (udev == NULL) {
158302ac6454SAndrew Thompson 		return (NULL);
158402ac6454SAndrew Thompson 	}
158502ac6454SAndrew Thompson 	/* initialise our SX-lock */
158691cd9240SAndrew Thompson 	sx_init_flags(&udev->enum_sx, "USB config SX lock", SX_DUPOK);
1587a6b60150SHans Petter Selasky 	sx_init_flags(&udev->sr_sx, "USB suspend and resume SX lock", SX_NOWITNESS);
158802ac6454SAndrew Thompson 
158991cd9240SAndrew Thompson 	cv_init(&udev->ctrlreq_cv, "WCTRL");
159091cd9240SAndrew Thompson 	cv_init(&udev->ref_cv, "UGONE");
159102ac6454SAndrew Thompson 
159202ac6454SAndrew Thompson 	/* initialise our mutex */
159391cd9240SAndrew Thompson 	mtx_init(&udev->device_mtx, "USB device mutex", NULL, MTX_DEF);
159402ac6454SAndrew Thompson 
159502ac6454SAndrew Thompson 	/* initialise generic clear stall */
1596a593f6b8SAndrew Thompson 	udev->cs_msg[0].hdr.pm_callback = &usbd_clear_stall_proc;
159702ac6454SAndrew Thompson 	udev->cs_msg[0].udev = udev;
1598a593f6b8SAndrew Thompson 	udev->cs_msg[1].hdr.pm_callback = &usbd_clear_stall_proc;
159902ac6454SAndrew Thompson 	udev->cs_msg[1].udev = udev;
160002ac6454SAndrew Thompson 
160102ac6454SAndrew Thompson 	/* initialise some USB device fields */
160202ac6454SAndrew Thompson 	udev->parent_hub = parent_hub;
160302ac6454SAndrew Thompson 	udev->parent_dev = parent_dev;
160402ac6454SAndrew Thompson 	udev->port_index = port_index;
160502ac6454SAndrew Thompson 	udev->port_no = port_no;
160602ac6454SAndrew Thompson 	udev->depth = depth;
160702ac6454SAndrew Thompson 	udev->bus = bus;
160802ac6454SAndrew Thompson 	udev->address = USB_START_ADDR;	/* default value */
1609e0a69b51SAndrew Thompson 	udev->plugtime = (usb_ticks_t)ticks;
161002ac6454SAndrew Thompson 	/*
161102ac6454SAndrew Thompson 	 * We need to force the power mode to "on" because there are plenty
161202ac6454SAndrew Thompson 	 * of USB devices out there that do not work very well with
161302ac6454SAndrew Thompson 	 * automatic suspend and resume!
161402ac6454SAndrew Thompson 	 */
1615d2d71ce7SAndrew Thompson 	udev->power_mode = usbd_filter_power_mode(udev, USB_POWER_MODE_ON);
161602ac6454SAndrew Thompson 	udev->pwr_save.last_xfer_time = ticks;
161702ac6454SAndrew Thompson 	/* we are not ready yet */
161802ac6454SAndrew Thompson 	udev->refcount = 1;
161902ac6454SAndrew Thompson 
162002ac6454SAndrew Thompson 	/* set up default endpoint descriptor */
16215b3bb704SAndrew Thompson 	udev->ctrl_ep_desc.bLength = sizeof(udev->ctrl_ep_desc);
16225b3bb704SAndrew Thompson 	udev->ctrl_ep_desc.bDescriptorType = UDESC_ENDPOINT;
16235b3bb704SAndrew Thompson 	udev->ctrl_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
16245b3bb704SAndrew Thompson 	udev->ctrl_ep_desc.bmAttributes = UE_CONTROL;
16255b3bb704SAndrew Thompson 	udev->ctrl_ep_desc.wMaxPacketSize[0] = USB_MAX_IPACKET;
16265b3bb704SAndrew Thompson 	udev->ctrl_ep_desc.wMaxPacketSize[1] = 0;
16275b3bb704SAndrew Thompson 	udev->ctrl_ep_desc.bInterval = 0;
1628963169b4SHans Petter Selasky 
1629963169b4SHans Petter Selasky 	/* set up default endpoint companion descriptor */
1630963169b4SHans Petter Selasky 	udev->ctrl_ep_comp_desc.bLength = sizeof(udev->ctrl_ep_comp_desc);
1631963169b4SHans Petter Selasky 	udev->ctrl_ep_comp_desc.bDescriptorType = UDESC_ENDPOINT_SS_COMP;
1632963169b4SHans Petter Selasky 
163302ac6454SAndrew Thompson 	udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
163402ac6454SAndrew Thompson 
163502ac6454SAndrew Thompson 	udev->speed = speed;
1636f29a0724SAndrew Thompson 	udev->flags.usb_mode = mode;
163702ac6454SAndrew Thompson 
163802ac6454SAndrew Thompson 	/* search for our High Speed USB HUB, if any */
163902ac6454SAndrew Thompson 
164002ac6454SAndrew Thompson 	adev = udev;
164102ac6454SAndrew Thompson 	hub = udev->parent_hub;
164202ac6454SAndrew Thompson 
164302ac6454SAndrew Thompson 	while (hub) {
164402ac6454SAndrew Thompson 		if (hub->speed == USB_SPEED_HIGH) {
164502ac6454SAndrew Thompson 			udev->hs_hub_addr = hub->address;
1646495f25ceSAndrew Thompson 			udev->parent_hs_hub = hub;
164702ac6454SAndrew Thompson 			udev->hs_port_no = adev->port_no;
164802ac6454SAndrew Thompson 			break;
164902ac6454SAndrew Thompson 		}
165002ac6454SAndrew Thompson 		adev = hub;
165102ac6454SAndrew Thompson 		hub = hub->parent_hub;
165202ac6454SAndrew Thompson 	}
165302ac6454SAndrew Thompson 
1654ae60fdfbSAndrew Thompson 	/* init the default endpoint */
1655a593f6b8SAndrew Thompson 	usb_init_endpoint(udev, 0,
16565b3bb704SAndrew Thompson 	    &udev->ctrl_ep_desc,
1657963169b4SHans Petter Selasky 	    &udev->ctrl_ep_comp_desc,
16585b3bb704SAndrew Thompson 	    &udev->ctrl_ep);
165902ac6454SAndrew Thompson 
166002ac6454SAndrew Thompson 	/* set device index */
166102ac6454SAndrew Thompson 	udev->device_index = device_index;
166202ac6454SAndrew Thompson 
16638755859aSAndrew Thompson #if USB_HAVE_UGEN
16640f6e8f93SAndrew Thompson 	/* Create ugen name */
1665ee3e3ff5SAndrew Thompson 	snprintf(udev->ugen_name, sizeof(udev->ugen_name),
1666ee3e3ff5SAndrew Thompson 	    USB_GENERIC_NAME "%u.%u", device_get_unit(bus->bdev),
1667ee3e3ff5SAndrew Thompson 	    device_index);
16688755859aSAndrew Thompson 	LIST_INIT(&udev->pd_list);
16698755859aSAndrew Thompson 
16700f6e8f93SAndrew Thompson 	/* Create the control endpoint device */
16712ffd5fdcSHans Petter Selasky 	udev->ctrl_dev = usb_make_dev(udev, NULL, 0, 0,
16722ffd5fdcSHans Petter Selasky 	    FREAD|FWRITE, UID_ROOT, GID_OPERATOR, 0600);
16730f6e8f93SAndrew Thompson 
16740f6e8f93SAndrew Thompson 	/* Create a link from /dev/ugenX.X to the default endpoint */
16752ffd5fdcSHans Petter Selasky 	if (udev->ctrl_dev != NULL)
16762ffd5fdcSHans Petter Selasky 		make_dev_alias(udev->ctrl_dev->cdev, "%s", udev->ugen_name);
16770f6e8f93SAndrew Thompson #endif
1678963169b4SHans Petter Selasky 	/* Initialise device */
1679963169b4SHans Petter Selasky 	if (bus->methods->device_init != NULL) {
1680963169b4SHans Petter Selasky 		err = (bus->methods->device_init) (udev);
1681963169b4SHans Petter Selasky 		if (err != 0) {
1682963169b4SHans Petter Selasky 			DPRINTFN(0, "device init %d failed "
1683963169b4SHans Petter Selasky 			    "(%s, ignored)\n", device_index,
1684963169b4SHans Petter Selasky 			    usbd_errstr(err));
1685963169b4SHans Petter Selasky 			goto done;
1686963169b4SHans Petter Selasky 		}
1687963169b4SHans Petter Selasky 	}
1688963169b4SHans Petter Selasky 	/* set powered device state after device init is complete */
1689963169b4SHans Petter Selasky 	usb_set_device_state(udev, USB_STATE_POWERED);
1690963169b4SHans Petter Selasky 
1691f29a0724SAndrew Thompson 	if (udev->flags.usb_mode == USB_MODE_HOST) {
169202ac6454SAndrew Thompson 
1693a593f6b8SAndrew Thompson 		err = usbd_req_set_address(udev, NULL, device_index);
169402ac6454SAndrew Thompson 
1695963169b4SHans Petter Selasky 		/*
1696963169b4SHans Petter Selasky 		 * This is the new USB device address from now on, if
1697963169b4SHans Petter Selasky 		 * the set address request didn't set it already.
1698963169b4SHans Petter Selasky 		 */
1699963169b4SHans Petter Selasky 		if (udev->address == USB_START_ADDR)
170002ac6454SAndrew Thompson 			udev->address = device_index;
170102ac6454SAndrew Thompson 
170202ac6454SAndrew Thompson 		/*
170302ac6454SAndrew Thompson 		 * We ignore any set-address errors, hence there are
170402ac6454SAndrew Thompson 		 * buggy USB devices out there that actually receive
170502ac6454SAndrew Thompson 		 * the SETUP PID, but manage to set the address before
170602ac6454SAndrew Thompson 		 * the STATUS stage is ACK'ed. If the device responds
170702ac6454SAndrew Thompson 		 * to the subsequent get-descriptor at the new
170802ac6454SAndrew Thompson 		 * address, then we know that the set-address command
170902ac6454SAndrew Thompson 		 * was successful.
171002ac6454SAndrew Thompson 		 */
171102ac6454SAndrew Thompson 		if (err) {
171202ac6454SAndrew Thompson 			DPRINTFN(0, "set address %d failed "
171303797f33SAndrew Thompson 			    "(%s, ignored)\n", udev->address,
1714a593f6b8SAndrew Thompson 			    usbd_errstr(err));
171502ac6454SAndrew Thompson 		}
171602ac6454SAndrew Thompson 	} else {
171702ac6454SAndrew Thompson 		/* We are not self powered */
171802ac6454SAndrew Thompson 		udev->flags.self_powered = 0;
171902ac6454SAndrew Thompson 
172002ac6454SAndrew Thompson 		/* Set unconfigured state */
172102ac6454SAndrew Thompson 		udev->curr_config_no = USB_UNCONFIG_NO;
172202ac6454SAndrew Thompson 		udev->curr_config_index = USB_UNCONFIG_INDEX;
172302ac6454SAndrew Thompson 
172402ac6454SAndrew Thompson 		/* Setup USB descriptors */
1725a593f6b8SAndrew Thompson 		err = (usb_temp_setup_by_index_p) (udev, usb_template);
172602ac6454SAndrew Thompson 		if (err) {
172702ac6454SAndrew Thompson 			DPRINTFN(0, "setting up USB template failed maybe the USB "
172802ac6454SAndrew Thompson 			    "template module has not been loaded\n");
172902ac6454SAndrew Thompson 			goto done;
173002ac6454SAndrew Thompson 		}
173102ac6454SAndrew Thompson 	}
1732a593f6b8SAndrew Thompson 	usb_set_device_state(udev, USB_STATE_ADDRESSED);
173302ac6454SAndrew Thompson 
1734963169b4SHans Petter Selasky 	/* setup the device descriptor and the initial "wMaxPacketSize" */
1735963169b4SHans Petter Selasky 	err = usbd_setup_device_desc(udev, NULL);
1736963169b4SHans Petter Selasky 
1737963169b4SHans Petter Selasky 	if (err != 0) {
173823de050bSHans Petter Selasky 		/* try to enumerate two more times */
1739a593f6b8SAndrew Thompson 		err = usbd_req_re_enumerate(udev, NULL);
174023de050bSHans Petter Selasky 		if (err != 0) {
174123de050bSHans Petter Selasky 			err = usbd_req_re_enumerate(udev, NULL);
174223de050bSHans Petter Selasky 			if (err != 0) {
174302ac6454SAndrew Thompson 				goto done;
174402ac6454SAndrew Thompson 			}
174523de050bSHans Petter Selasky 		}
174623de050bSHans Petter Selasky 	}
174702ac6454SAndrew Thompson 
174802ac6454SAndrew Thompson 	/*
174902ac6454SAndrew Thompson 	 * Setup temporary USB attach args so that we can figure out some
175002ac6454SAndrew Thompson 	 * basic quirks for this device.
175102ac6454SAndrew Thompson 	 */
1752a593f6b8SAndrew Thompson 	usb_init_attach_arg(udev, &uaa);
175302ac6454SAndrew Thompson 
1754a593f6b8SAndrew Thompson 	if (usb_test_quirk(&uaa, UQ_BUS_POWERED)) {
175502ac6454SAndrew Thompson 		udev->flags.uq_bus_powered = 1;
175602ac6454SAndrew Thompson 	}
1757a593f6b8SAndrew Thompson 	if (usb_test_quirk(&uaa, UQ_NO_STRINGS)) {
175802ac6454SAndrew Thompson 		udev->flags.no_strings = 1;
175902ac6454SAndrew Thompson 	}
176002ac6454SAndrew Thompson 	/*
176102ac6454SAndrew Thompson 	 * Workaround for buggy USB devices.
176202ac6454SAndrew Thompson 	 *
176302ac6454SAndrew Thompson 	 * It appears that some string-less USB chips will crash and
176402ac6454SAndrew Thompson 	 * disappear if any attempts are made to read any string
176502ac6454SAndrew Thompson 	 * descriptors.
176602ac6454SAndrew Thompson 	 *
176702ac6454SAndrew Thompson 	 * Try to detect such chips by checking the strings in the USB
176802ac6454SAndrew Thompson 	 * device descriptor. If no strings are present there we
176902ac6454SAndrew Thompson 	 * simply disable all USB strings.
177002ac6454SAndrew Thompson 	 */
17716950c75fSHans Petter Selasky 
17726950c75fSHans Petter Selasky 	/* Protect scratch area */
17736950c75fSHans Petter Selasky 	do_unlock = usbd_enum_lock(udev);
17746950c75fSHans Petter Selasky 
17756950c75fSHans Petter Selasky 	scratch_ptr = udev->scratch.data;
177602ac6454SAndrew Thompson 
177702ac6454SAndrew Thompson 	if (udev->ddesc.iManufacturer ||
177802ac6454SAndrew Thompson 	    udev->ddesc.iProduct ||
177902ac6454SAndrew Thompson 	    udev->ddesc.iSerialNumber) {
178002ac6454SAndrew Thompson 		/* read out the language ID string */
1781a593f6b8SAndrew Thompson 		err = usbd_req_get_string_desc(udev, NULL,
1782c10e1453SAndrew Thompson 		    (char *)scratch_ptr, 4, 0, USB_LANGUAGE_TABLE);
178302ac6454SAndrew Thompson 	} else {
178402ac6454SAndrew Thompson 		err = USB_ERR_INVAL;
178502ac6454SAndrew Thompson 	}
178602ac6454SAndrew Thompson 
178702ac6454SAndrew Thompson 	if (err || (scratch_ptr[0] < 4)) {
178802ac6454SAndrew Thompson 		udev->flags.no_strings = 1;
178902ac6454SAndrew Thompson 	} else {
17907a918edfSAndrew Thompson 		uint16_t langid;
17917a918edfSAndrew Thompson 		uint16_t pref;
17927a918edfSAndrew Thompson 		uint16_t mask;
17937a918edfSAndrew Thompson 		uint8_t x;
17947a918edfSAndrew Thompson 
17957a918edfSAndrew Thompson 		/* load preferred value and mask */
17967a918edfSAndrew Thompson 		pref = usb_lang_id;
17977a918edfSAndrew Thompson 		mask = usb_lang_mask;
17987a918edfSAndrew Thompson 
17997a918edfSAndrew Thompson 		/* align length correctly */
18006950c75fSHans Petter Selasky 		scratch_ptr[0] &= ~1U;
18017a918edfSAndrew Thompson 
18027a918edfSAndrew Thompson 		/* fix compiler warning */
18037a918edfSAndrew Thompson 		langid = 0;
18047a918edfSAndrew Thompson 
18057a918edfSAndrew Thompson 		/* search for preferred language */
18067a918edfSAndrew Thompson 		for (x = 2; (x < scratch_ptr[0]); x += 2) {
18077a918edfSAndrew Thompson 			langid = UGETW(scratch_ptr + x);
18087a918edfSAndrew Thompson 			if ((langid & mask) == pref)
18097a918edfSAndrew Thompson 				break;
18107a918edfSAndrew Thompson 		}
18117a918edfSAndrew Thompson 		if (x >= scratch_ptr[0]) {
181202ac6454SAndrew Thompson 			/* pick the first language as the default */
18137a918edfSAndrew Thompson 			DPRINTFN(1, "Using first language\n");
18147a918edfSAndrew Thompson 			langid = UGETW(scratch_ptr + 2);
18157a918edfSAndrew Thompson 		}
18167a918edfSAndrew Thompson 
18177a918edfSAndrew Thompson 		DPRINTFN(1, "Language selected: 0x%04x\n", langid);
18187a918edfSAndrew Thompson 		udev->langid = langid;
181902ac6454SAndrew Thompson 	}
182002ac6454SAndrew Thompson 
18216950c75fSHans Petter Selasky 	if (do_unlock)
18226950c75fSHans Petter Selasky 		usbd_enum_unlock(udev);
18236950c75fSHans Petter Selasky 
182402ac6454SAndrew Thompson 	/* assume 100mA bus powered for now. Changed when configured. */
182502ac6454SAndrew Thompson 	udev->power = USB_MIN_POWER;
1826c86aead6SAndrew Thompson 	/* fetch the vendor and product strings from the device */
1827a593f6b8SAndrew Thompson 	usbd_set_device_strings(udev);
182802ac6454SAndrew Thompson 
18292a4c6157SAndrew Thompson 	if (udev->flags.usb_mode == USB_MODE_DEVICE) {
18302a4c6157SAndrew Thompson 		/* USB device mode setup is complete */
18312a4c6157SAndrew Thompson 		err = 0;
18322a4c6157SAndrew Thompson 		goto config_done;
18332a4c6157SAndrew Thompson 	}
183402ac6454SAndrew Thompson 
183502ac6454SAndrew Thompson 	/*
183602ac6454SAndrew Thompson 	 * Most USB devices should attach to config index 0 by
183702ac6454SAndrew Thompson 	 * default
183802ac6454SAndrew Thompson 	 */
1839a593f6b8SAndrew Thompson 	if (usb_test_quirk(&uaa, UQ_CFG_INDEX_0)) {
184002ac6454SAndrew Thompson 		config_index = 0;
184102ac6454SAndrew Thompson 		config_quirk = 1;
1842a593f6b8SAndrew Thompson 	} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_1)) {
184302ac6454SAndrew Thompson 		config_index = 1;
184402ac6454SAndrew Thompson 		config_quirk = 1;
1845a593f6b8SAndrew Thompson 	} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_2)) {
184602ac6454SAndrew Thompson 		config_index = 2;
184702ac6454SAndrew Thompson 		config_quirk = 1;
1848a593f6b8SAndrew Thompson 	} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_3)) {
184902ac6454SAndrew Thompson 		config_index = 3;
185002ac6454SAndrew Thompson 		config_quirk = 1;
1851a593f6b8SAndrew Thompson 	} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_4)) {
185202ac6454SAndrew Thompson 		config_index = 4;
185302ac6454SAndrew Thompson 		config_quirk = 1;
185402ac6454SAndrew Thompson 	} else {
185502ac6454SAndrew Thompson 		config_index = 0;
185602ac6454SAndrew Thompson 		config_quirk = 0;
185702ac6454SAndrew Thompson 	}
185802ac6454SAndrew Thompson 
18592a4c6157SAndrew Thompson 	set_config_failed = 0;
186002ac6454SAndrew Thompson repeat_set_config:
186102ac6454SAndrew Thompson 
186202ac6454SAndrew Thompson 	DPRINTF("setting config %u\n", config_index);
186302ac6454SAndrew Thompson 
186402ac6454SAndrew Thompson 	/* get the USB device configured */
1865a593f6b8SAndrew Thompson 	err = usbd_set_config_index(udev, config_index);
186602ac6454SAndrew Thompson 	if (err) {
186702ac6454SAndrew Thompson 		if (udev->ddesc.bNumConfigurations != 0) {
186802ac6454SAndrew Thompson 			if (!set_config_failed) {
186902ac6454SAndrew Thompson 				set_config_failed = 1;
187002ac6454SAndrew Thompson 				/* XXX try to re-enumerate the device */
18712a4c6157SAndrew Thompson 				err = usbd_req_re_enumerate(udev, NULL);
187202ac6454SAndrew Thompson 				if (err == 0)
187302ac6454SAndrew Thompson 					goto repeat_set_config;
187402ac6454SAndrew Thompson 			}
18752a4c6157SAndrew Thompson 			DPRINTFN(0, "Failure selecting configuration index %u:"
18762a4c6157SAndrew Thompson 			    "%s, port %u, addr %u (ignored)\n",
1877a593f6b8SAndrew Thompson 			    config_index, usbd_errstr(err), udev->port_no,
187802ac6454SAndrew Thompson 			    udev->address);
187902ac6454SAndrew Thompson 		}
188002ac6454SAndrew Thompson 		/*
18812a4c6157SAndrew Thompson 		 * Some USB devices do not have any configurations. Ignore any
18822a4c6157SAndrew Thompson 		 * set config failures!
188302ac6454SAndrew Thompson 		 */
188402ac6454SAndrew Thompson 		err = 0;
18852a4c6157SAndrew Thompson 		goto config_done;
18862a4c6157SAndrew Thompson 	}
18872a4c6157SAndrew Thompson 	if (!config_quirk && config_index + 1 < udev->ddesc.bNumConfigurations) {
188802ac6454SAndrew Thompson 		if ((udev->cdesc->bNumInterface < 2) &&
18892a4c6157SAndrew Thompson 		    usbd_get_no_descriptors(udev->cdesc, UDESC_ENDPOINT) == 0) {
18902a4c6157SAndrew Thompson 			DPRINTFN(0, "Found no endpoints, trying next config\n");
189102ac6454SAndrew Thompson 			config_index++;
189202ac6454SAndrew Thompson 			goto repeat_set_config;
189302ac6454SAndrew Thompson 		}
189415d4edd3SHans Petter Selasky #if USB_HAVE_MSCTEST
189502ac6454SAndrew Thompson 		if (config_index == 0) {
189602ac6454SAndrew Thompson 			/*
189702ac6454SAndrew Thompson 			 * Try to figure out if we have an
189802ac6454SAndrew Thompson 			 * auto-install disk there:
189902ac6454SAndrew Thompson 			 */
1900d84a79e7SAndrew Thompson 			if (usb_iface_is_cdrom(udev, 0)) {
190102ac6454SAndrew Thompson 				DPRINTFN(0, "Found possible auto-install "
190202ac6454SAndrew Thompson 				    "disk (trying next config)\n");
190302ac6454SAndrew Thompson 				config_index++;
190402ac6454SAndrew Thompson 				goto repeat_set_config;
190502ac6454SAndrew Thompson 			}
190602ac6454SAndrew Thompson 		}
190715d4edd3SHans Petter Selasky #endif
19082a4c6157SAndrew Thompson 	}
190915d4edd3SHans Petter Selasky #if USB_HAVE_MSCTEST
1910d46dc4adSHans Petter Selasky 	if (set_config_failed == 0 && config_index == 0 &&
19119157ad4bSHans Petter Selasky 	    usb_test_quirk(&uaa, UQ_MSC_NO_SYNC_CACHE) == 0 &&
19129157ad4bSHans Petter Selasky 	    usb_test_quirk(&uaa, UQ_MSC_NO_GETMAXLUN) == 0) {
1913d46dc4adSHans Petter Selasky 
1914d46dc4adSHans Petter Selasky 		/*
1915d46dc4adSHans Petter Selasky 		 * Try to figure out if there are any MSC quirks we
1916d46dc4adSHans Petter Selasky 		 * should apply automatically:
1917d46dc4adSHans Petter Selasky 		 */
1918d46dc4adSHans Petter Selasky 		err = usb_msc_auto_quirk(udev, 0);
1919d46dc4adSHans Petter Selasky 
1920d46dc4adSHans Petter Selasky 		if (err != 0) {
1921d46dc4adSHans Petter Selasky 			set_config_failed = 1;
1922d46dc4adSHans Petter Selasky 			goto repeat_set_config;
1923d46dc4adSHans Petter Selasky 		}
1924d46dc4adSHans Petter Selasky 	}
192515d4edd3SHans Petter Selasky #endif
192602ac6454SAndrew Thompson 
19272a4c6157SAndrew Thompson config_done:
192802ac6454SAndrew Thompson 	DPRINTF("new dev (addr %d), udev=%p, parent_hub=%p\n",
192902ac6454SAndrew Thompson 	    udev->address, udev, udev->parent_hub);
193002ac6454SAndrew Thompson 
193102ac6454SAndrew Thompson 	/* register our device - we are ready */
1932a593f6b8SAndrew Thompson 	usb_bus_port_set_device(bus, parent_hub ?
193302ac6454SAndrew Thompson 	    parent_hub->hub->ports + port_index : NULL, udev, device_index);
193402ac6454SAndrew Thompson 
19350f6e8f93SAndrew Thompson #if USB_HAVE_UGEN
19368755859aSAndrew Thompson 	/* Symlink the ugen device name */
1937a593f6b8SAndrew Thompson 	udev->ugen_symlink = usb_alloc_symlink(udev->ugen_name);
19388755859aSAndrew Thompson 
19398755859aSAndrew Thompson 	/* Announce device */
1940ae538d85SAndrew Thompson 	printf("%s: <%s> at %s\n", udev->ugen_name,
1941ae538d85SAndrew Thompson 	    usb_get_manufacturer(udev),
194202ac6454SAndrew Thompson 	    device_get_nameunit(udev->bus->bdev));
19438bb77249SHans Petter Selasky #endif
19447efaaa9aSAndrew Thompson 
19458bb77249SHans Petter Selasky #if USB_HAVE_DEVCTL
19463acd904dSAndrew Thompson 	usb_notify_addq("ATTACH", udev);
19478755859aSAndrew Thompson #endif
194802ac6454SAndrew Thompson done:
194902ac6454SAndrew Thompson 	if (err) {
1950d88688c7SAndrew Thompson 		/*
1951d88688c7SAndrew Thompson 		 * Free USB device and all subdevices, if any.
1952d88688c7SAndrew Thompson 		 */
1953d88688c7SAndrew Thompson 		usb_free_device(udev, 0);
195402ac6454SAndrew Thompson 		udev = NULL;
195502ac6454SAndrew Thompson 	}
195602ac6454SAndrew Thompson 	return (udev);
195702ac6454SAndrew Thompson }
195802ac6454SAndrew Thompson 
19590f6e8f93SAndrew Thompson #if USB_HAVE_UGEN
19602ffd5fdcSHans Petter Selasky struct usb_fs_privdata *
19612ffd5fdcSHans Petter Selasky usb_make_dev(struct usb_device *udev, const char *devname, int ep,
19622ffd5fdcSHans Petter Selasky     int fi, int rwmode, uid_t uid, gid_t gid, int mode)
1963ee3e3ff5SAndrew Thompson {
1964760bc48eSAndrew Thompson 	struct usb_fs_privdata* pd;
19652ffd5fdcSHans Petter Selasky 	char buffer[32];
1966ee3e3ff5SAndrew Thompson 
1967ee3e3ff5SAndrew Thompson 	/* Store information to locate ourselves again later */
1968760bc48eSAndrew Thompson 	pd = malloc(sizeof(struct usb_fs_privdata), M_USBDEV,
1969ee3e3ff5SAndrew Thompson 	    M_WAITOK | M_ZERO);
1970ee3e3ff5SAndrew Thompson 	pd->bus_index = device_get_unit(udev->bus->bdev);
1971ee3e3ff5SAndrew Thompson 	pd->dev_index = udev->device_index;
1972ee3e3ff5SAndrew Thompson 	pd->ep_addr = ep;
19732ffd5fdcSHans Petter Selasky 	pd->fifo_index = fi;
19742ffd5fdcSHans Petter Selasky 	pd->mode = rwmode;
1975ee3e3ff5SAndrew Thompson 
1976ee3e3ff5SAndrew Thompson 	/* Now, create the device itself */
19772ffd5fdcSHans Petter Selasky 	if (devname == NULL) {
19782ffd5fdcSHans Petter Selasky 		devname = buffer;
19792ffd5fdcSHans Petter Selasky 		snprintf(buffer, sizeof(buffer), USB_DEVICE_DIR "/%u.%u.%u",
1980f35aaff0SAndrew Thompson 		    pd->bus_index, pd->dev_index, pd->ep_addr);
19812ffd5fdcSHans Petter Selasky 	}
19822ffd5fdcSHans Petter Selasky 
19832ffd5fdcSHans Petter Selasky 	pd->cdev = make_dev(&usb_devsw, 0, uid, gid, mode, "%s", devname);
19842ffd5fdcSHans Petter Selasky 
19852ffd5fdcSHans Petter Selasky 	if (pd->cdev == NULL) {
19862ffd5fdcSHans Petter Selasky 		DPRINTFN(0, "Failed to create device %s\n", devname);
19872ffd5fdcSHans Petter Selasky 		free(pd, M_USBDEV);
19882ffd5fdcSHans Petter Selasky 		return (NULL);
19892ffd5fdcSHans Petter Selasky 	}
19902ffd5fdcSHans Petter Selasky 
19912ffd5fdcSHans Petter Selasky 	/* XXX setting si_drv1 and creating the device is not atomic! */
1992ee3e3ff5SAndrew Thompson 	pd->cdev->si_drv1 = pd;
1993ee3e3ff5SAndrew Thompson 
19942ffd5fdcSHans Petter Selasky 	return (pd);
19952ffd5fdcSHans Petter Selasky }
19962ffd5fdcSHans Petter Selasky 
19972ffd5fdcSHans Petter Selasky void
1998b78e84d1SHans Petter Selasky usb_destroy_dev_sync(struct usb_fs_privdata *pd)
19992ffd5fdcSHans Petter Selasky {
2000b78e84d1SHans Petter Selasky 	DPRINTFN(1, "Destroying device at ugen%d.%d\n",
2001b78e84d1SHans Petter Selasky 	    pd->bus_index, pd->dev_index);
20022ffd5fdcSHans Petter Selasky 
2003b78e84d1SHans Petter Selasky 	/*
2004b78e84d1SHans Petter Selasky 	 * Destroy character device synchronously. After this
2005b78e84d1SHans Petter Selasky 	 * all system calls are returned. Can block.
2006b78e84d1SHans Petter Selasky 	 */
20072ffd5fdcSHans Petter Selasky 	destroy_dev(pd->cdev);
20082ffd5fdcSHans Petter Selasky 
20092ffd5fdcSHans Petter Selasky 	free(pd, M_USBDEV);
2010ee3e3ff5SAndrew Thompson }
2011ee3e3ff5SAndrew Thompson 
2012b78e84d1SHans Petter Selasky void
2013b78e84d1SHans Petter Selasky usb_destroy_dev(struct usb_fs_privdata *pd)
2014b78e84d1SHans Petter Selasky {
2015b78e84d1SHans Petter Selasky 	struct usb_bus *bus;
2016b78e84d1SHans Petter Selasky 
2017b78e84d1SHans Petter Selasky 	if (pd == NULL)
2018b78e84d1SHans Petter Selasky 		return;
2019b78e84d1SHans Petter Selasky 
2020b78e84d1SHans Petter Selasky 	mtx_lock(&usb_ref_lock);
2021b78e84d1SHans Petter Selasky 	bus = devclass_get_softc(usb_devclass_ptr, pd->bus_index);
2022b78e84d1SHans Petter Selasky 	mtx_unlock(&usb_ref_lock);
2023b78e84d1SHans Petter Selasky 
2024b78e84d1SHans Petter Selasky 	if (bus == NULL) {
2025b78e84d1SHans Petter Selasky 		usb_destroy_dev_sync(pd);
2026b78e84d1SHans Petter Selasky 		return;
2027b78e84d1SHans Petter Selasky 	}
2028b78e84d1SHans Petter Selasky 
202907dbde67SHans Petter Selasky 	/* make sure we can re-use the device name */
203007dbde67SHans Petter Selasky 	delist_dev(pd->cdev);
203107dbde67SHans Petter Selasky 
2032b78e84d1SHans Petter Selasky 	USB_BUS_LOCK(bus);
2033b78e84d1SHans Petter Selasky 	LIST_INSERT_HEAD(&bus->pd_cleanup_list, pd, pd_next);
2034b78e84d1SHans Petter Selasky 	/* get cleanup going */
2035b78e84d1SHans Petter Selasky 	usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
2036b78e84d1SHans Petter Selasky 	    &bus->cleanup_msg[0], &bus->cleanup_msg[1]);
2037b78e84d1SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
2038b78e84d1SHans Petter Selasky }
2039b78e84d1SHans Petter Selasky 
2040ee3e3ff5SAndrew Thompson static void
2041a593f6b8SAndrew Thompson usb_cdev_create(struct usb_device *udev)
2042ee3e3ff5SAndrew Thompson {
2043760bc48eSAndrew Thompson 	struct usb_config_descriptor *cd;
2044760bc48eSAndrew Thompson 	struct usb_endpoint_descriptor *ed;
2045760bc48eSAndrew Thompson 	struct usb_descriptor *desc;
2046760bc48eSAndrew Thompson 	struct usb_fs_privdata* pd;
2047f5f145baSAndrew Thompson 	int inmode, outmode, inmask, outmask, mode;
2048f5f145baSAndrew Thompson 	uint8_t ep;
2049ee3e3ff5SAndrew Thompson 
2050ee3e3ff5SAndrew Thompson 	KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("stale cdev entries"));
2051ee3e3ff5SAndrew Thompson 
2052ee3e3ff5SAndrew Thompson 	DPRINTFN(2, "Creating device nodes\n");
2053ee3e3ff5SAndrew Thompson 
2054a593f6b8SAndrew Thompson 	if (usbd_get_mode(udev) == USB_MODE_DEVICE) {
2055ee3e3ff5SAndrew Thompson 		inmode = FWRITE;
2056ee3e3ff5SAndrew Thompson 		outmode = FREAD;
2057ee3e3ff5SAndrew Thompson 	} else {		 /* USB_MODE_HOST */
2058ee3e3ff5SAndrew Thompson 		inmode = FREAD;
2059ee3e3ff5SAndrew Thompson 		outmode = FWRITE;
2060ee3e3ff5SAndrew Thompson 	}
2061ee3e3ff5SAndrew Thompson 
2062f5f145baSAndrew Thompson 	inmask = 0;
2063f5f145baSAndrew Thompson 	outmask = 0;
2064f5f145baSAndrew Thompson 	desc = NULL;
2065f5f145baSAndrew Thompson 
2066f5f145baSAndrew Thompson 	/*
2067f5f145baSAndrew Thompson 	 * Collect all used endpoint numbers instead of just
2068f5f145baSAndrew Thompson 	 * generating 16 static endpoints.
2069f5f145baSAndrew Thompson 	 */
2070a593f6b8SAndrew Thompson 	cd = usbd_get_config_descriptor(udev);
2071a593f6b8SAndrew Thompson 	while ((desc = usb_desc_foreach(cd, desc))) {
2072f5f145baSAndrew Thompson 		/* filter out all endpoint descriptors */
2073f5f145baSAndrew Thompson 		if ((desc->bDescriptorType == UDESC_ENDPOINT) &&
2074f5f145baSAndrew Thompson 		    (desc->bLength >= sizeof(*ed))) {
2075760bc48eSAndrew Thompson 			ed = (struct usb_endpoint_descriptor *)desc;
2076f5f145baSAndrew Thompson 
2077f5f145baSAndrew Thompson 			/* update masks */
2078f5f145baSAndrew Thompson 			ep = ed->bEndpointAddress;
2079f5f145baSAndrew Thompson 			if (UE_GET_DIR(ep)  == UE_DIR_OUT)
2080f5f145baSAndrew Thompson 				outmask |= 1 << UE_GET_ADDR(ep);
2081f5f145baSAndrew Thompson 			else
2082f5f145baSAndrew Thompson 				inmask |= 1 << UE_GET_ADDR(ep);
2083f5f145baSAndrew Thompson 		}
2084f5f145baSAndrew Thompson 	}
2085ee3e3ff5SAndrew Thompson 
2086ee3e3ff5SAndrew Thompson 	/* Create all available endpoints except EP0 */
2087ee3e3ff5SAndrew Thompson 	for (ep = 1; ep < 16; ep++) {
20882ffd5fdcSHans Petter Selasky 		mode = (inmask & (1 << ep)) ? inmode : 0;
20892ffd5fdcSHans Petter Selasky 		mode |= (outmask & (1 << ep)) ? outmode : 0;
2090ee3e3ff5SAndrew Thompson 		if (mode == 0)
2091ee3e3ff5SAndrew Thompson 			continue;	/* no IN or OUT endpoint */
2092ee3e3ff5SAndrew Thompson 
20932ffd5fdcSHans Petter Selasky 		pd = usb_make_dev(udev, NULL, ep, 0,
20942ffd5fdcSHans Petter Selasky 		    mode, UID_ROOT, GID_OPERATOR, 0600);
20952ffd5fdcSHans Petter Selasky 
20962ffd5fdcSHans Petter Selasky 		if (pd != NULL)
2097ee3e3ff5SAndrew Thompson 			LIST_INSERT_HEAD(&udev->pd_list, pd, pd_next);
2098ee3e3ff5SAndrew Thompson 	}
2099ee3e3ff5SAndrew Thompson }
2100ee3e3ff5SAndrew Thompson 
2101ee3e3ff5SAndrew Thompson static void
2102a593f6b8SAndrew Thompson usb_cdev_free(struct usb_device *udev)
2103ee3e3ff5SAndrew Thompson {
2104760bc48eSAndrew Thompson 	struct usb_fs_privdata* pd;
2105ee3e3ff5SAndrew Thompson 
2106ee3e3ff5SAndrew Thompson 	DPRINTFN(2, "Freeing device nodes\n");
2107ee3e3ff5SAndrew Thompson 
2108ee3e3ff5SAndrew Thompson 	while ((pd = LIST_FIRST(&udev->pd_list)) != NULL) {
2109ee3e3ff5SAndrew Thompson 		KASSERT(pd->cdev->si_drv1 == pd, ("privdata corrupt"));
2110ee3e3ff5SAndrew Thompson 
2111ee3e3ff5SAndrew Thompson 		LIST_REMOVE(pd, pd_next);
2112ee3e3ff5SAndrew Thompson 
21132ffd5fdcSHans Petter Selasky 		usb_destroy_dev(pd);
21142ffd5fdcSHans Petter Selasky 	}
2115ee3e3ff5SAndrew Thompson }
21160f6e8f93SAndrew Thompson #endif
2117ee3e3ff5SAndrew Thompson 
211802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
2119a593f6b8SAndrew Thompson  *	usb_free_device
212002ac6454SAndrew Thompson  *
2121d88688c7SAndrew Thompson  * This function is NULL safe and will free an USB device and its
2122d88688c7SAndrew Thompson  * children devices, if any.
2123bdd41206SAndrew Thompson  *
2124d88688c7SAndrew Thompson  * Flag values: Reserved, set to zero.
212502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
212602ac6454SAndrew Thompson void
2127a593f6b8SAndrew Thompson usb_free_device(struct usb_device *udev, uint8_t flag)
212802ac6454SAndrew Thompson {
2129760bc48eSAndrew Thompson 	struct usb_bus *bus;
2130bdd41206SAndrew Thompson 
2131bdd41206SAndrew Thompson 	if (udev == NULL)
2132bdd41206SAndrew Thompson 		return;		/* already freed */
213302ac6454SAndrew Thompson 
213402ac6454SAndrew Thompson 	DPRINTFN(4, "udev=%p port=%d\n", udev, udev->port_no);
213502ac6454SAndrew Thompson 
2136bd216778SAndrew Thompson 	bus = udev->bus;
2137f97da128SHans Petter Selasky 
2138f97da128SHans Petter Selasky 	/* set DETACHED state to prevent any further references */
2139a593f6b8SAndrew Thompson 	usb_set_device_state(udev, USB_STATE_DETACHED);
2140bdd41206SAndrew Thompson 
21418bb77249SHans Petter Selasky #if USB_HAVE_DEVCTL
21423acd904dSAndrew Thompson 	usb_notify_addq("DETACH", udev);
21438bb77249SHans Petter Selasky #endif
214402ac6454SAndrew Thompson 
21458bb77249SHans Petter Selasky #if USB_HAVE_UGEN
2146cabe79d9SMarius Strobl 	if (!rebooting) {
2147ee3e3ff5SAndrew Thompson 		printf("%s: <%s> at %s (disconnected)\n", udev->ugen_name,
2148ae538d85SAndrew Thompson 		    usb_get_manufacturer(udev), device_get_nameunit(bus->bdev));
2149cabe79d9SMarius Strobl 	}
215002ac6454SAndrew Thompson 
2151ee3e3ff5SAndrew Thompson 	/* Destroy UGEN symlink, if any */
215202ac6454SAndrew Thompson 	if (udev->ugen_symlink) {
2153a593f6b8SAndrew Thompson 		usb_free_symlink(udev->ugen_symlink);
215402ac6454SAndrew Thompson 		udev->ugen_symlink = NULL;
215502ac6454SAndrew Thompson 	}
215602ac6454SAndrew Thompson 
21572ffd5fdcSHans Petter Selasky 	usb_destroy_dev(udev->ctrl_dev);
21580f6e8f93SAndrew Thompson #endif
215902ac6454SAndrew Thompson 
2160f29a0724SAndrew Thompson 	if (udev->flags.usb_mode == USB_MODE_DEVICE) {
216102ac6454SAndrew Thompson 		/* stop receiving any control transfers (Device Side Mode) */
21625b3bb704SAndrew Thompson 		usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
216302ac6454SAndrew Thompson 	}
216402ac6454SAndrew Thompson 
2165bdd41206SAndrew Thompson 	/* the following will get the device unconfigured in software */
2166d88688c7SAndrew Thompson 	usb_unconfigure(udev, USB_UNCFG_FLAG_FREE_EP0);
216702ac6454SAndrew Thompson 
2168f97da128SHans Petter Selasky 	/* final device unregister after all character devices are closed */
2169f97da128SHans Petter Selasky 	usb_bus_port_set_device(bus, udev->parent_hub ?
2170f97da128SHans Petter Selasky 	    udev->parent_hub->hub->ports + udev->port_index : NULL,
2171f97da128SHans Petter Selasky 	    NULL, USB_ROOT_HUB_ADDR);
2172f97da128SHans Petter Selasky 
217302ac6454SAndrew Thompson 	/* unsetup any leftover default USB transfers */
21745b3bb704SAndrew Thompson 	usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
217502ac6454SAndrew Thompson 
217602ac6454SAndrew Thompson 	/* template unsetup, if any */
2177a593f6b8SAndrew Thompson 	(usb_temp_unsetup_p) (udev);
217802ac6454SAndrew Thompson 
217902ac6454SAndrew Thompson 	/*
218002ac6454SAndrew Thompson 	 * Make sure that our clear-stall messages are not queued
218102ac6454SAndrew Thompson 	 * anywhere:
218202ac6454SAndrew Thompson 	 */
218302ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
21849b3a48eeSHans Petter Selasky 	usb_proc_mwait(USB_BUS_NON_GIANT_PROC(udev->bus),
218502ac6454SAndrew Thompson 	    &udev->cs_msg[0], &udev->cs_msg[1]);
218602ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
218702ac6454SAndrew Thompson 
2188b78e84d1SHans Petter Selasky 	/* wait for all references to go away */
2189b78e84d1SHans Petter Selasky 	usb_wait_pending_refs(udev);
2190b78e84d1SHans Petter Selasky 
219191cd9240SAndrew Thompson 	sx_destroy(&udev->enum_sx);
21922df1e9a6SAndrew Thompson 	sx_destroy(&udev->sr_sx);
219302ac6454SAndrew Thompson 
219491cd9240SAndrew Thompson 	cv_destroy(&udev->ctrlreq_cv);
219591cd9240SAndrew Thompson 	cv_destroy(&udev->ref_cv);
219602ac6454SAndrew Thompson 
219791cd9240SAndrew Thompson 	mtx_destroy(&udev->device_mtx);
21988755859aSAndrew Thompson #if USB_HAVE_UGEN
2199ee3e3ff5SAndrew Thompson 	KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("leaked cdev entries"));
22008755859aSAndrew Thompson #endif
220102ac6454SAndrew Thompson 
2202963169b4SHans Petter Selasky 	/* Uninitialise device */
2203963169b4SHans Petter Selasky 	if (bus->methods->device_uninit != NULL)
2204963169b4SHans Petter Selasky 		(bus->methods->device_uninit) (udev);
2205963169b4SHans Petter Selasky 
220602ac6454SAndrew Thompson 	/* free device */
220728212de2SAndrew Thompson 	free(udev->serial, M_USB);
220828212de2SAndrew Thompson 	free(udev->manufacturer, M_USB);
220928212de2SAndrew Thompson 	free(udev->product, M_USB);
221002ac6454SAndrew Thompson 	free(udev, M_USB);
221102ac6454SAndrew Thompson }
221202ac6454SAndrew Thompson 
221302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
2214a593f6b8SAndrew Thompson  *	usbd_get_iface
221502ac6454SAndrew Thompson  *
221602ac6454SAndrew Thompson  * This function is the safe way to get the USB interface structure
221702ac6454SAndrew Thompson  * pointer by interface index.
221802ac6454SAndrew Thompson  *
221902ac6454SAndrew Thompson  * Return values:
222002ac6454SAndrew Thompson  *   NULL: Interface not present.
222102ac6454SAndrew Thompson  *   Else: Pointer to USB interface structure.
222202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
2223760bc48eSAndrew Thompson struct usb_interface *
2224a593f6b8SAndrew Thompson usbd_get_iface(struct usb_device *udev, uint8_t iface_index)
222502ac6454SAndrew Thompson {
2226760bc48eSAndrew Thompson 	struct usb_interface *iface = udev->ifaces + iface_index;
222702ac6454SAndrew Thompson 
2228bdd41206SAndrew Thompson 	if (iface_index >= udev->ifaces_max)
222902ac6454SAndrew Thompson 		return (NULL);
223002ac6454SAndrew Thompson 	return (iface);
223102ac6454SAndrew Thompson }
223202ac6454SAndrew Thompson 
223302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
22343c8d24f4SAndrew Thompson  *	usbd_find_descriptor
223502ac6454SAndrew Thompson  *
223602ac6454SAndrew Thompson  * This function will lookup the first descriptor that matches the
223702ac6454SAndrew Thompson  * criteria given by the arguments "type" and "subtype". Descriptors
223802ac6454SAndrew Thompson  * will only be searched within the interface having the index
223902ac6454SAndrew Thompson  * "iface_index".  If the "id" argument points to an USB descriptor,
224002ac6454SAndrew Thompson  * it will be skipped before the search is started. This allows
224102ac6454SAndrew Thompson  * searching for multiple descriptors using the same criteria. Else
224202ac6454SAndrew Thompson  * the search is started after the interface descriptor.
224302ac6454SAndrew Thompson  *
224402ac6454SAndrew Thompson  * Return values:
224502ac6454SAndrew Thompson  *   NULL: End of descriptors
224602ac6454SAndrew Thompson  *   Else: A descriptor matching the criteria
224702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
224802ac6454SAndrew Thompson void   *
22493c8d24f4SAndrew Thompson usbd_find_descriptor(struct usb_device *udev, void *id, uint8_t iface_index,
225002ac6454SAndrew Thompson     uint8_t type, uint8_t type_mask,
225102ac6454SAndrew Thompson     uint8_t subtype, uint8_t subtype_mask)
225202ac6454SAndrew Thompson {
2253760bc48eSAndrew Thompson 	struct usb_descriptor *desc;
2254760bc48eSAndrew Thompson 	struct usb_config_descriptor *cd;
2255760bc48eSAndrew Thompson 	struct usb_interface *iface;
225602ac6454SAndrew Thompson 
2257a593f6b8SAndrew Thompson 	cd = usbd_get_config_descriptor(udev);
225802ac6454SAndrew Thompson 	if (cd == NULL) {
225902ac6454SAndrew Thompson 		return (NULL);
226002ac6454SAndrew Thompson 	}
226102ac6454SAndrew Thompson 	if (id == NULL) {
2262a593f6b8SAndrew Thompson 		iface = usbd_get_iface(udev, iface_index);
226302ac6454SAndrew Thompson 		if (iface == NULL) {
226402ac6454SAndrew Thompson 			return (NULL);
226502ac6454SAndrew Thompson 		}
2266a593f6b8SAndrew Thompson 		id = usbd_get_interface_descriptor(iface);
226702ac6454SAndrew Thompson 		if (id == NULL) {
226802ac6454SAndrew Thompson 			return (NULL);
226902ac6454SAndrew Thompson 		}
227002ac6454SAndrew Thompson 	}
227102ac6454SAndrew Thompson 	desc = (void *)id;
227202ac6454SAndrew Thompson 
2273a593f6b8SAndrew Thompson 	while ((desc = usb_desc_foreach(cd, desc))) {
227402ac6454SAndrew Thompson 
227502ac6454SAndrew Thompson 		if (desc->bDescriptorType == UDESC_INTERFACE) {
227602ac6454SAndrew Thompson 			break;
227702ac6454SAndrew Thompson 		}
227802ac6454SAndrew Thompson 		if (((desc->bDescriptorType & type_mask) == type) &&
227902ac6454SAndrew Thompson 		    ((desc->bDescriptorSubtype & subtype_mask) == subtype)) {
228002ac6454SAndrew Thompson 			return (desc);
228102ac6454SAndrew Thompson 		}
228202ac6454SAndrew Thompson 	}
228302ac6454SAndrew Thompson 	return (NULL);
228402ac6454SAndrew Thompson }
228502ac6454SAndrew Thompson 
228602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
2287a593f6b8SAndrew Thompson  *	usb_devinfo
228802ac6454SAndrew Thompson  *
228902ac6454SAndrew Thompson  * This function will dump information from the device descriptor
229002ac6454SAndrew Thompson  * belonging to the USB device pointed to by "udev", to the string
229102ac6454SAndrew Thompson  * pointed to by "dst_ptr" having a maximum length of "dst_len" bytes
229202ac6454SAndrew Thompson  * including the terminating zero.
229302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
229402ac6454SAndrew Thompson void
2295a593f6b8SAndrew Thompson usb_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len)
229602ac6454SAndrew Thompson {
2297760bc48eSAndrew Thompson 	struct usb_device_descriptor *udd = &udev->ddesc;
229802ac6454SAndrew Thompson 	uint16_t bcdDevice;
229902ac6454SAndrew Thompson 	uint16_t bcdUSB;
230002ac6454SAndrew Thompson 
230102ac6454SAndrew Thompson 	bcdUSB = UGETW(udd->bcdUSB);
230202ac6454SAndrew Thompson 	bcdDevice = UGETW(udd->bcdDevice);
230302ac6454SAndrew Thompson 
230402ac6454SAndrew Thompson 	if (udd->bDeviceClass != 0xFF) {
230502ac6454SAndrew Thompson 		snprintf(dst_ptr, dst_len, "%s %s, class %d/%d, rev %x.%02x/"
23068755859aSAndrew Thompson 		    "%x.%02x, addr %d",
2307ae538d85SAndrew Thompson 		    usb_get_manufacturer(udev),
2308ae538d85SAndrew Thompson 		    usb_get_product(udev),
230902ac6454SAndrew Thompson 		    udd->bDeviceClass, udd->bDeviceSubClass,
231002ac6454SAndrew Thompson 		    (bcdUSB >> 8), bcdUSB & 0xFF,
231102ac6454SAndrew Thompson 		    (bcdDevice >> 8), bcdDevice & 0xFF,
231202ac6454SAndrew Thompson 		    udev->address);
231302ac6454SAndrew Thompson 	} else {
231402ac6454SAndrew Thompson 		snprintf(dst_ptr, dst_len, "%s %s, rev %x.%02x/"
23158755859aSAndrew Thompson 		    "%x.%02x, addr %d",
2316ae538d85SAndrew Thompson 		    usb_get_manufacturer(udev),
2317ae538d85SAndrew Thompson 		    usb_get_product(udev),
231802ac6454SAndrew Thompson 		    (bcdUSB >> 8), bcdUSB & 0xFF,
231902ac6454SAndrew Thompson 		    (bcdDevice >> 8), bcdDevice & 0xFF,
232002ac6454SAndrew Thompson 		    udev->address);
232102ac6454SAndrew Thompson 	}
232202ac6454SAndrew Thompson }
232302ac6454SAndrew Thompson 
2324ed6d949aSAndrew Thompson #ifdef USB_VERBOSE
232502ac6454SAndrew Thompson /*
232602ac6454SAndrew Thompson  * Descriptions of of known vendors and devices ("products").
232702ac6454SAndrew Thompson  */
232802ac6454SAndrew Thompson struct usb_knowndev {
232902ac6454SAndrew Thompson 	uint16_t vendor;
233002ac6454SAndrew Thompson 	uint16_t product;
233102ac6454SAndrew Thompson 	uint32_t flags;
233202ac6454SAndrew Thompson 	const char *vendorname;
233302ac6454SAndrew Thompson 	const char *productname;
233402ac6454SAndrew Thompson };
233502ac6454SAndrew Thompson 
233602ac6454SAndrew Thompson #define	USB_KNOWNDEV_NOPROD	0x01	/* match on vendor only */
233702ac6454SAndrew Thompson 
233802ac6454SAndrew Thompson #include "usbdevs.h"
233902ac6454SAndrew Thompson #include "usbdevs_data.h"
234002ac6454SAndrew Thompson #endif					/* USB_VERBOSE */
234102ac6454SAndrew Thompson 
234202ac6454SAndrew Thompson static void
2343a593f6b8SAndrew Thompson usbd_set_device_strings(struct usb_device *udev)
234402ac6454SAndrew Thompson {
2345760bc48eSAndrew Thompson 	struct usb_device_descriptor *udd = &udev->ddesc;
2346ed6d949aSAndrew Thompson #ifdef USB_VERBOSE
234702ac6454SAndrew Thompson 	const struct usb_knowndev *kdp;
234802ac6454SAndrew Thompson #endif
23492df1e9a6SAndrew Thompson 	char *temp_ptr;
23507a918edfSAndrew Thompson 	size_t temp_size;
235102ac6454SAndrew Thompson 	uint16_t vendor_id;
235202ac6454SAndrew Thompson 	uint16_t product_id;
23536950c75fSHans Petter Selasky 	uint8_t do_unlock;
235402ac6454SAndrew Thompson 
23556950c75fSHans Petter Selasky 	/* Protect scratch area */
23566950c75fSHans Petter Selasky 	do_unlock = usbd_enum_lock(udev);
23576950c75fSHans Petter Selasky 
23586950c75fSHans Petter Selasky 	temp_ptr = (char *)udev->scratch.data;
23596950c75fSHans Petter Selasky 	temp_size = sizeof(udev->scratch.data);
23607a918edfSAndrew Thompson 
236102ac6454SAndrew Thompson 	vendor_id = UGETW(udd->idVendor);
236202ac6454SAndrew Thompson 	product_id = UGETW(udd->idProduct);
236302ac6454SAndrew Thompson 
2364c86aead6SAndrew Thompson 	/* get serial number string */
23657a918edfSAndrew Thompson 	usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2366c86aead6SAndrew Thompson 	    udev->ddesc.iSerialNumber);
23677a918edfSAndrew Thompson 	udev->serial = strdup(temp_ptr, M_USB);
236802ac6454SAndrew Thompson 
2369c86aead6SAndrew Thompson 	/* get manufacturer string */
23707a918edfSAndrew Thompson 	usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2371c86aead6SAndrew Thompson 	    udev->ddesc.iManufacturer);
23727a918edfSAndrew Thompson 	usb_trim_spaces(temp_ptr);
23737a918edfSAndrew Thompson 	if (temp_ptr[0] != '\0')
23747a918edfSAndrew Thompson 		udev->manufacturer = strdup(temp_ptr, M_USB);
2375c86aead6SAndrew Thompson 
2376c86aead6SAndrew Thompson 	/* get product string */
23777a918edfSAndrew Thompson 	usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2378c86aead6SAndrew Thompson 	    udev->ddesc.iProduct);
23797a918edfSAndrew Thompson 	usb_trim_spaces(temp_ptr);
23807a918edfSAndrew Thompson 	if (temp_ptr[0] != '\0')
23817a918edfSAndrew Thompson 		udev->product = strdup(temp_ptr, M_USB);
2382c86aead6SAndrew Thompson 
2383ed6d949aSAndrew Thompson #ifdef USB_VERBOSE
2384c86aead6SAndrew Thompson 	if (udev->manufacturer == NULL || udev->product == NULL) {
2385c86aead6SAndrew Thompson 		for (kdp = usb_knowndevs; kdp->vendorname != NULL; kdp++) {
238602ac6454SAndrew Thompson 			if (kdp->vendor == vendor_id &&
238702ac6454SAndrew Thompson 			    (kdp->product == product_id ||
238802ac6454SAndrew Thompson 			    (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
238902ac6454SAndrew Thompson 				break;
239002ac6454SAndrew Thompson 		}
239102ac6454SAndrew Thompson 		if (kdp->vendorname != NULL) {
2392c86aead6SAndrew Thompson 			/* XXX should use pointer to knowndevs string */
2393c86aead6SAndrew Thompson 			if (udev->manufacturer == NULL) {
2394c86aead6SAndrew Thompson 				udev->manufacturer = strdup(kdp->vendorname,
2395c86aead6SAndrew Thompson 				    M_USB);
2396c86aead6SAndrew Thompson 			}
2397c86aead6SAndrew Thompson 			if (udev->product == NULL &&
2398c86aead6SAndrew Thompson 			    (kdp->flags & USB_KNOWNDEV_NOPROD) == 0) {
2399c86aead6SAndrew Thompson 				udev->product = strdup(kdp->productname,
2400c86aead6SAndrew Thompson 				    M_USB);
2401c86aead6SAndrew Thompson 			}
240202ac6454SAndrew Thompson 		}
240302ac6454SAndrew Thompson 	}
240402ac6454SAndrew Thompson #endif
2405c86aead6SAndrew Thompson 	/* Provide default strings if none were found */
2406c86aead6SAndrew Thompson 	if (udev->manufacturer == NULL) {
24077a918edfSAndrew Thompson 		snprintf(temp_ptr, temp_size, "vendor 0x%04x", vendor_id);
24087a918edfSAndrew Thompson 		udev->manufacturer = strdup(temp_ptr, M_USB);
240902ac6454SAndrew Thompson 	}
2410c86aead6SAndrew Thompson 	if (udev->product == NULL) {
24117a918edfSAndrew Thompson 		snprintf(temp_ptr, temp_size, "product 0x%04x", product_id);
24127a918edfSAndrew Thompson 		udev->product = strdup(temp_ptr, M_USB);
241302ac6454SAndrew Thompson 	}
24146950c75fSHans Petter Selasky 
24156950c75fSHans Petter Selasky 	if (do_unlock)
24166950c75fSHans Petter Selasky 		usbd_enum_unlock(udev);
241702ac6454SAndrew Thompson }
241802ac6454SAndrew Thompson 
241902ac6454SAndrew Thompson /*
242002ac6454SAndrew Thompson  * Returns:
242102ac6454SAndrew Thompson  * See: USB_MODE_XXX
242202ac6454SAndrew Thompson  */
2423f29a0724SAndrew Thompson enum usb_hc_mode
2424a593f6b8SAndrew Thompson usbd_get_mode(struct usb_device *udev)
242502ac6454SAndrew Thompson {
2426f29a0724SAndrew Thompson 	return (udev->flags.usb_mode);
242702ac6454SAndrew Thompson }
242802ac6454SAndrew Thompson 
242902ac6454SAndrew Thompson /*
243002ac6454SAndrew Thompson  * Returns:
243102ac6454SAndrew Thompson  * See: USB_SPEED_XXX
243202ac6454SAndrew Thompson  */
24338d2dd5ddSAndrew Thompson enum usb_dev_speed
2434a593f6b8SAndrew Thompson usbd_get_speed(struct usb_device *udev)
243502ac6454SAndrew Thompson {
243602ac6454SAndrew Thompson 	return (udev->speed);
243702ac6454SAndrew Thompson }
243802ac6454SAndrew Thompson 
243902ac6454SAndrew Thompson uint32_t
2440a593f6b8SAndrew Thompson usbd_get_isoc_fps(struct usb_device *udev)
244102ac6454SAndrew Thompson {
244202ac6454SAndrew Thompson 	;				/* indent fix */
244302ac6454SAndrew Thompson 	switch (udev->speed) {
244402ac6454SAndrew Thompson 	case USB_SPEED_LOW:
244502ac6454SAndrew Thompson 	case USB_SPEED_FULL:
244602ac6454SAndrew Thompson 		return (1000);
244702ac6454SAndrew Thompson 	default:
244802ac6454SAndrew Thompson 		return (8000);
244902ac6454SAndrew Thompson 	}
245002ac6454SAndrew Thompson }
245102ac6454SAndrew Thompson 
2452760bc48eSAndrew Thompson struct usb_device_descriptor *
2453a593f6b8SAndrew Thompson usbd_get_device_descriptor(struct usb_device *udev)
245402ac6454SAndrew Thompson {
245502ac6454SAndrew Thompson 	if (udev == NULL)
245602ac6454SAndrew Thompson 		return (NULL);		/* be NULL safe */
245702ac6454SAndrew Thompson 	return (&udev->ddesc);
245802ac6454SAndrew Thompson }
245902ac6454SAndrew Thompson 
2460760bc48eSAndrew Thompson struct usb_config_descriptor *
2461a593f6b8SAndrew Thompson usbd_get_config_descriptor(struct usb_device *udev)
246202ac6454SAndrew Thompson {
246302ac6454SAndrew Thompson 	if (udev == NULL)
246402ac6454SAndrew Thompson 		return (NULL);		/* be NULL safe */
246502ac6454SAndrew Thompson 	return (udev->cdesc);
246602ac6454SAndrew Thompson }
246702ac6454SAndrew Thompson 
246802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
2469a593f6b8SAndrew Thompson  *	usb_test_quirk - test a device for a given quirk
247002ac6454SAndrew Thompson  *
247102ac6454SAndrew Thompson  * Return values:
247202ac6454SAndrew Thompson  * 0: The USB device does not have the given quirk.
247302ac6454SAndrew Thompson  * Else: The USB device has the given quirk.
247402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
247502ac6454SAndrew Thompson uint8_t
2476a593f6b8SAndrew Thompson usb_test_quirk(const struct usb_attach_arg *uaa, uint16_t quirk)
247702ac6454SAndrew Thompson {
247802ac6454SAndrew Thompson 	uint8_t found;
24793f37fb62SHans Petter Selasky 	uint8_t x;
24803f37fb62SHans Petter Selasky 
24813f37fb62SHans Petter Selasky 	if (quirk == UQ_NONE)
24823f37fb62SHans Petter Selasky 		return (0);
24833f37fb62SHans Petter Selasky 
24843f37fb62SHans Petter Selasky 	/* search the automatic per device quirks first */
24853f37fb62SHans Petter Selasky 
24863f37fb62SHans Petter Selasky 	for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) {
24873f37fb62SHans Petter Selasky 		if (uaa->device->autoQuirk[x] == quirk)
24883f37fb62SHans Petter Selasky 			return (1);
24893f37fb62SHans Petter Selasky 	}
24903f37fb62SHans Petter Selasky 
24913f37fb62SHans Petter Selasky 	/* search global quirk table, if any */
249202ac6454SAndrew Thompson 
2493a593f6b8SAndrew Thompson 	found = (usb_test_quirk_p) (&uaa->info, quirk);
24943f37fb62SHans Petter Selasky 
249502ac6454SAndrew Thompson 	return (found);
249602ac6454SAndrew Thompson }
249702ac6454SAndrew Thompson 
2498760bc48eSAndrew Thompson struct usb_interface_descriptor *
2499a593f6b8SAndrew Thompson usbd_get_interface_descriptor(struct usb_interface *iface)
250002ac6454SAndrew Thompson {
250102ac6454SAndrew Thompson 	if (iface == NULL)
250202ac6454SAndrew Thompson 		return (NULL);		/* be NULL safe */
250302ac6454SAndrew Thompson 	return (iface->idesc);
250402ac6454SAndrew Thompson }
250502ac6454SAndrew Thompson 
250602ac6454SAndrew Thompson uint8_t
2507a593f6b8SAndrew Thompson usbd_get_interface_altindex(struct usb_interface *iface)
250802ac6454SAndrew Thompson {
250902ac6454SAndrew Thompson 	return (iface->alt_index);
251002ac6454SAndrew Thompson }
251102ac6454SAndrew Thompson 
251202ac6454SAndrew Thompson uint8_t
2513a593f6b8SAndrew Thompson usbd_get_bus_index(struct usb_device *udev)
251402ac6454SAndrew Thompson {
251502ac6454SAndrew Thompson 	return ((uint8_t)device_get_unit(udev->bus->bdev));
251602ac6454SAndrew Thompson }
251702ac6454SAndrew Thompson 
251802ac6454SAndrew Thompson uint8_t
2519a593f6b8SAndrew Thompson usbd_get_device_index(struct usb_device *udev)
252002ac6454SAndrew Thompson {
252102ac6454SAndrew Thompson 	return (udev->device_index);
252202ac6454SAndrew Thompson }
252302ac6454SAndrew Thompson 
25248bb77249SHans Petter Selasky #if USB_HAVE_DEVCTL
25253acd904dSAndrew Thompson static void
25263acd904dSAndrew Thompson usb_notify_addq(const char *type, struct usb_device *udev)
25273acd904dSAndrew Thompson {
25283acd904dSAndrew Thompson 	struct usb_interface *iface;
25293acd904dSAndrew Thompson 	struct sbuf *sb;
25303acd904dSAndrew Thompson 	int i;
25313acd904dSAndrew Thompson 
25323acd904dSAndrew Thompson 	/* announce the device */
25333acd904dSAndrew Thompson 	sb = sbuf_new_auto();
25343acd904dSAndrew Thompson 	sbuf_printf(sb,
25358bb77249SHans Petter Selasky #if USB_HAVE_UGEN
25368bb77249SHans Petter Selasky 	    "ugen=%s "
253702c40241SHans Petter Selasky 	    "cdev=%s "
25388bb77249SHans Petter Selasky #endif
25393acd904dSAndrew Thompson 	    "vendor=0x%04x "
25403acd904dSAndrew Thompson 	    "product=0x%04x "
25413acd904dSAndrew Thompson 	    "devclass=0x%02x "
25423acd904dSAndrew Thompson 	    "devsubclass=0x%02x "
25433acd904dSAndrew Thompson 	    "sernum=\"%s\" "
25443acd904dSAndrew Thompson 	    "release=0x%04x "
254566390216SAndrew Thompson 	    "mode=%s "
25463acd904dSAndrew Thompson 	    "port=%u "
25478bb77249SHans Petter Selasky #if USB_HAVE_UGEN
25486df3eebbSNick Hibma 	    "parent=%s"
25498bb77249SHans Petter Selasky #endif
25508bb77249SHans Petter Selasky 	    "",
25518bb77249SHans Petter Selasky #if USB_HAVE_UGEN
25523acd904dSAndrew Thompson 	    udev->ugen_name,
255302c40241SHans Petter Selasky 	    udev->ugen_name,
25548bb77249SHans Petter Selasky #endif
25553acd904dSAndrew Thompson 	    UGETW(udev->ddesc.idVendor),
25563acd904dSAndrew Thompson 	    UGETW(udev->ddesc.idProduct),
25573acd904dSAndrew Thompson 	    udev->ddesc.bDeviceClass,
25583acd904dSAndrew Thompson 	    udev->ddesc.bDeviceSubClass,
2559ae538d85SAndrew Thompson 	    usb_get_serial(udev),
25603acd904dSAndrew Thompson 	    UGETW(udev->ddesc.bcdDevice),
256166390216SAndrew Thompson 	    (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
25628bb77249SHans Petter Selasky 	    udev->port_no
25638bb77249SHans Petter Selasky #if USB_HAVE_UGEN
25648bb77249SHans Petter Selasky 	    , udev->parent_hub != NULL ?
25653acd904dSAndrew Thompson 		udev->parent_hub->ugen_name :
25668bb77249SHans Petter Selasky 		device_get_nameunit(device_get_parent(udev->bus->bdev))
25678bb77249SHans Petter Selasky #endif
25688bb77249SHans Petter Selasky 	    );
25693acd904dSAndrew Thompson 	sbuf_finish(sb);
25703acd904dSAndrew Thompson 	devctl_notify("USB", "DEVICE", type, sbuf_data(sb));
25713acd904dSAndrew Thompson 	sbuf_delete(sb);
25723acd904dSAndrew Thompson 
25733acd904dSAndrew Thompson 	/* announce each interface */
25743acd904dSAndrew Thompson 	for (i = 0; i < USB_IFACE_MAX; i++) {
25753acd904dSAndrew Thompson 		iface = usbd_get_iface(udev, i);
25763acd904dSAndrew Thompson 		if (iface == NULL)
25773acd904dSAndrew Thompson 			break;		/* end of interfaces */
25783acd904dSAndrew Thompson 		if (iface->idesc == NULL)
25793acd904dSAndrew Thompson 			continue;	/* no interface descriptor */
25803acd904dSAndrew Thompson 
25813acd904dSAndrew Thompson 		sb = sbuf_new_auto();
25823acd904dSAndrew Thompson 		sbuf_printf(sb,
25838bb77249SHans Petter Selasky #if USB_HAVE_UGEN
25848bb77249SHans Petter Selasky 		    "ugen=%s "
258502c40241SHans Petter Selasky 		    "cdev=%s "
25868bb77249SHans Petter Selasky #endif
25873acd904dSAndrew Thompson 		    "vendor=0x%04x "
25883acd904dSAndrew Thompson 		    "product=0x%04x "
25893acd904dSAndrew Thompson 		    "devclass=0x%02x "
25903acd904dSAndrew Thompson 		    "devsubclass=0x%02x "
25913acd904dSAndrew Thompson 		    "sernum=\"%s\" "
25923acd904dSAndrew Thompson 		    "release=0x%04x "
259366390216SAndrew Thompson 		    "mode=%s "
25943acd904dSAndrew Thompson 		    "interface=%d "
25953acd904dSAndrew Thompson 		    "endpoints=%d "
25963acd904dSAndrew Thompson 		    "intclass=0x%02x "
25973acd904dSAndrew Thompson 		    "intsubclass=0x%02x "
25986df3eebbSNick Hibma 		    "intprotocol=0x%02x",
25998bb77249SHans Petter Selasky #if USB_HAVE_UGEN
26003acd904dSAndrew Thompson 		    udev->ugen_name,
260102c40241SHans Petter Selasky 		    udev->ugen_name,
26028bb77249SHans Petter Selasky #endif
26033acd904dSAndrew Thompson 		    UGETW(udev->ddesc.idVendor),
26043acd904dSAndrew Thompson 		    UGETW(udev->ddesc.idProduct),
26053acd904dSAndrew Thompson 		    udev->ddesc.bDeviceClass,
26063acd904dSAndrew Thompson 		    udev->ddesc.bDeviceSubClass,
2607ae538d85SAndrew Thompson 		    usb_get_serial(udev),
26083acd904dSAndrew Thompson 		    UGETW(udev->ddesc.bcdDevice),
260966390216SAndrew Thompson 		    (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
26103acd904dSAndrew Thompson 		    iface->idesc->bInterfaceNumber,
26113acd904dSAndrew Thompson 		    iface->idesc->bNumEndpoints,
26123acd904dSAndrew Thompson 		    iface->idesc->bInterfaceClass,
26133acd904dSAndrew Thompson 		    iface->idesc->bInterfaceSubClass,
26143acd904dSAndrew Thompson 		    iface->idesc->bInterfaceProtocol);
26153acd904dSAndrew Thompson 		sbuf_finish(sb);
26163acd904dSAndrew Thompson 		devctl_notify("USB", "INTERFACE", type, sbuf_data(sb));
26173acd904dSAndrew Thompson 		sbuf_delete(sb);
26183acd904dSAndrew Thompson 	}
26193acd904dSAndrew Thompson }
26208bb77249SHans Petter Selasky #endif
262102ac6454SAndrew Thompson 
26228bb77249SHans Petter Selasky #if USB_HAVE_UGEN
262302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
2624a593f6b8SAndrew Thompson  *	usb_fifo_free_wrap
262502ac6454SAndrew Thompson  *
262602ac6454SAndrew Thompson  * This function will free the FIFOs.
262702ac6454SAndrew Thompson  *
2628bdd41206SAndrew Thompson  * Description of "flag" argument: If the USB_UNCFG_FLAG_FREE_EP0 flag
2629bdd41206SAndrew Thompson  * is set and "iface_index" is set to "USB_IFACE_INDEX_ANY", we free
2630bdd41206SAndrew Thompson  * all FIFOs. If the USB_UNCFG_FLAG_FREE_EP0 flag is not set and
2631bdd41206SAndrew Thompson  * "iface_index" is set to "USB_IFACE_INDEX_ANY", we free all non
2632bdd41206SAndrew Thompson  * control endpoint FIFOs. If "iface_index" is not set to
2633bdd41206SAndrew Thompson  * "USB_IFACE_INDEX_ANY" the flag has no effect.
263402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
263502ac6454SAndrew Thompson static void
2636a593f6b8SAndrew Thompson usb_fifo_free_wrap(struct usb_device *udev,
263702ac6454SAndrew Thompson     uint8_t iface_index, uint8_t flag)
263802ac6454SAndrew Thompson {
2639760bc48eSAndrew Thompson 	struct usb_fifo *f;
264002ac6454SAndrew Thompson 	uint16_t i;
264102ac6454SAndrew Thompson 
264202ac6454SAndrew Thompson 	/*
264302ac6454SAndrew Thompson 	 * Free any USB FIFOs on the given interface:
264402ac6454SAndrew Thompson 	 */
264502ac6454SAndrew Thompson 	for (i = 0; i != USB_FIFO_MAX; i++) {
264602ac6454SAndrew Thompson 		f = udev->fifo[i];
264702ac6454SAndrew Thompson 		if (f == NULL) {
264802ac6454SAndrew Thompson 			continue;
264902ac6454SAndrew Thompson 		}
265002ac6454SAndrew Thompson 		/* Check if the interface index matches */
265102ac6454SAndrew Thompson 		if (iface_index == f->iface_index) {
2652a593f6b8SAndrew Thompson 			if (f->methods != &usb_ugen_methods) {
265302ac6454SAndrew Thompson 				/*
265402ac6454SAndrew Thompson 				 * Don't free any non-generic FIFOs in
265502ac6454SAndrew Thompson 				 * this case.
265602ac6454SAndrew Thompson 				 */
265702ac6454SAndrew Thompson 				continue;
265802ac6454SAndrew Thompson 			}
265902ac6454SAndrew Thompson 			if ((f->dev_ep_index == 0) &&
266002ac6454SAndrew Thompson 			    (f->fs_xfer == NULL)) {
266102ac6454SAndrew Thompson 				/* no need to free this FIFO */
266202ac6454SAndrew Thompson 				continue;
266302ac6454SAndrew Thompson 			}
266402ac6454SAndrew Thompson 		} else if (iface_index == USB_IFACE_INDEX_ANY) {
2665a593f6b8SAndrew Thompson 			if ((f->methods == &usb_ugen_methods) &&
2666bdd41206SAndrew Thompson 			    (f->dev_ep_index == 0) &&
2667bdd41206SAndrew Thompson 			    (!(flag & USB_UNCFG_FLAG_FREE_EP0)) &&
266802ac6454SAndrew Thompson 			    (f->fs_xfer == NULL)) {
266902ac6454SAndrew Thompson 				/* no need to free this FIFO */
267002ac6454SAndrew Thompson 				continue;
267102ac6454SAndrew Thompson 			}
267202ac6454SAndrew Thompson 		} else {
267302ac6454SAndrew Thompson 			/* no need to free this FIFO */
267402ac6454SAndrew Thompson 			continue;
267502ac6454SAndrew Thompson 		}
267602ac6454SAndrew Thompson 		/* free this FIFO */
2677a593f6b8SAndrew Thompson 		usb_fifo_free(f);
267802ac6454SAndrew Thompson 	}
267902ac6454SAndrew Thompson }
26800f6e8f93SAndrew Thompson #endif
268102ac6454SAndrew Thompson 
268202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
2683a593f6b8SAndrew Thompson  *	usb_peer_can_wakeup
268402ac6454SAndrew Thompson  *
268502ac6454SAndrew Thompson  * Return values:
268602ac6454SAndrew Thompson  * 0: Peer cannot do resume signalling.
268702ac6454SAndrew Thompson  * Else: Peer can do resume signalling.
268802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
268902ac6454SAndrew Thompson uint8_t
2690a593f6b8SAndrew Thompson usb_peer_can_wakeup(struct usb_device *udev)
269102ac6454SAndrew Thompson {
2692760bc48eSAndrew Thompson 	const struct usb_config_descriptor *cdp;
269302ac6454SAndrew Thompson 
269402ac6454SAndrew Thompson 	cdp = udev->cdesc;
2695f29a0724SAndrew Thompson 	if ((cdp != NULL) && (udev->flags.usb_mode == USB_MODE_HOST)) {
269602ac6454SAndrew Thompson 		return (cdp->bmAttributes & UC_REMOTE_WAKEUP);
269702ac6454SAndrew Thompson 	}
269802ac6454SAndrew Thompson 	return (0);			/* not supported */
269902ac6454SAndrew Thompson }
2700bd216778SAndrew Thompson 
2701bd216778SAndrew Thompson void
2702a593f6b8SAndrew Thompson usb_set_device_state(struct usb_device *udev, enum usb_dev_state state)
2703bd216778SAndrew Thompson {
2704bd216778SAndrew Thompson 
2705bd216778SAndrew Thompson 	KASSERT(state < USB_STATE_MAX, ("invalid udev state"));
2706bd216778SAndrew Thompson 
2707bd216778SAndrew Thompson 	DPRINTF("udev %p state %s -> %s\n", udev,
2708a593f6b8SAndrew Thompson 	    usb_statestr(udev->state), usb_statestr(state));
2709963169b4SHans Petter Selasky 
2710f97da128SHans Petter Selasky #if USB_HAVE_UGEN
2711f97da128SHans Petter Selasky 	mtx_lock(&usb_ref_lock);
2712f97da128SHans Petter Selasky #endif
2713f97da128SHans Petter Selasky 	udev->state = state;
2714f97da128SHans Petter Selasky #if USB_HAVE_UGEN
2715f97da128SHans Petter Selasky 	mtx_unlock(&usb_ref_lock);
2716f97da128SHans Petter Selasky #endif
2717963169b4SHans Petter Selasky 	if (udev->bus->methods->device_state_change != NULL)
2718963169b4SHans Petter Selasky 		(udev->bus->methods->device_state_change) (udev);
2719963169b4SHans Petter Selasky }
2720963169b4SHans Petter Selasky 
2721963169b4SHans Petter Selasky enum usb_dev_state
2722963169b4SHans Petter Selasky usb_get_device_state(struct usb_device *udev)
2723963169b4SHans Petter Selasky {
2724963169b4SHans Petter Selasky 	if (udev == NULL)
2725963169b4SHans Petter Selasky 		return (USB_STATE_DETACHED);
2726963169b4SHans Petter Selasky 	return (udev->state);
2727bd216778SAndrew Thompson }
2728bd216778SAndrew Thompson 
2729ec8f3127SAndrew Thompson uint8_t
2730a593f6b8SAndrew Thompson usbd_device_attached(struct usb_device *udev)
2731bd216778SAndrew Thompson {
2732bd216778SAndrew Thompson 	return (udev->state > USB_STATE_DETACHED);
2733bd216778SAndrew Thompson }
2734cb18f7d1SAlfred Perlstein 
27356950c75fSHans Petter Selasky /*
27366950c75fSHans Petter Selasky  * The following function locks enumerating the given USB device. If
27376950c75fSHans Petter Selasky  * the lock is already grabbed this function returns zero. Else a
27386950c75fSHans Petter Selasky  * non-zero value is returned.
27396950c75fSHans Petter Selasky  */
27406950c75fSHans Petter Selasky uint8_t
2741cb18f7d1SAlfred Perlstein usbd_enum_lock(struct usb_device *udev)
2742cb18f7d1SAlfred Perlstein {
27436950c75fSHans Petter Selasky 	if (sx_xlocked(&udev->enum_sx))
27446950c75fSHans Petter Selasky 		return (0);
27456950c75fSHans Petter Selasky 
274691cd9240SAndrew Thompson 	sx_xlock(&udev->enum_sx);
27472df1e9a6SAndrew Thompson 	sx_xlock(&udev->sr_sx);
2748cb18f7d1SAlfred Perlstein 	/*
2749cb18f7d1SAlfred Perlstein 	 * NEWBUS LOCK NOTE: We should check if any parent SX locks
2750cb18f7d1SAlfred Perlstein 	 * are locked before locking Giant. Else the lock can be
2751cb18f7d1SAlfred Perlstein 	 * locked multiple times.
2752cb18f7d1SAlfred Perlstein 	 */
2753cb18f7d1SAlfred Perlstein 	mtx_lock(&Giant);
27546950c75fSHans Petter Selasky 	return (1);
2755cb18f7d1SAlfred Perlstein }
2756cb18f7d1SAlfred Perlstein 
2757cb18f7d1SAlfred Perlstein /* The following function unlocks enumerating the given USB device. */
2758cb18f7d1SAlfred Perlstein 
2759cb18f7d1SAlfred Perlstein void
2760cb18f7d1SAlfred Perlstein usbd_enum_unlock(struct usb_device *udev)
2761cb18f7d1SAlfred Perlstein {
2762cb18f7d1SAlfred Perlstein 	mtx_unlock(&Giant);
276391cd9240SAndrew Thompson 	sx_xunlock(&udev->enum_sx);
27642df1e9a6SAndrew Thompson 	sx_xunlock(&udev->sr_sx);
27652df1e9a6SAndrew Thompson }
27662df1e9a6SAndrew Thompson 
27672df1e9a6SAndrew Thompson /* The following function locks suspend and resume. */
27682df1e9a6SAndrew Thompson 
27692df1e9a6SAndrew Thompson void
27702df1e9a6SAndrew Thompson usbd_sr_lock(struct usb_device *udev)
27712df1e9a6SAndrew Thompson {
27722df1e9a6SAndrew Thompson 	sx_xlock(&udev->sr_sx);
27732df1e9a6SAndrew Thompson 	/*
27742df1e9a6SAndrew Thompson 	 * NEWBUS LOCK NOTE: We should check if any parent SX locks
27752df1e9a6SAndrew Thompson 	 * are locked before locking Giant. Else the lock can be
27762df1e9a6SAndrew Thompson 	 * locked multiple times.
27772df1e9a6SAndrew Thompson 	 */
27782df1e9a6SAndrew Thompson 	mtx_lock(&Giant);
27792df1e9a6SAndrew Thompson }
27802df1e9a6SAndrew Thompson 
27812df1e9a6SAndrew Thompson /* The following function unlocks suspend and resume. */
27822df1e9a6SAndrew Thompson 
27832df1e9a6SAndrew Thompson void
27842df1e9a6SAndrew Thompson usbd_sr_unlock(struct usb_device *udev)
27852df1e9a6SAndrew Thompson {
27862df1e9a6SAndrew Thompson 	mtx_unlock(&Giant);
27872df1e9a6SAndrew Thompson 	sx_xunlock(&udev->sr_sx);
2788cb18f7d1SAlfred Perlstein }
2789cb18f7d1SAlfred Perlstein 
2790cb18f7d1SAlfred Perlstein /*
2791cb18f7d1SAlfred Perlstein  * The following function checks the enumerating lock for the given
2792cb18f7d1SAlfred Perlstein  * USB device.
2793cb18f7d1SAlfred Perlstein  */
2794cb18f7d1SAlfred Perlstein 
2795cb18f7d1SAlfred Perlstein uint8_t
2796cb18f7d1SAlfred Perlstein usbd_enum_is_locked(struct usb_device *udev)
2797cb18f7d1SAlfred Perlstein {
279891cd9240SAndrew Thompson 	return (sx_xlocked(&udev->enum_sx));
2799cb18f7d1SAlfred Perlstein }
28008427ed84SHans Petter Selasky 
28018427ed84SHans Petter Selasky /*
28028427ed84SHans Petter Selasky  * The following function is used to set the per-interface specific
28038427ed84SHans Petter Selasky  * plug and play information. The string referred to by the pnpinfo
28048427ed84SHans Petter Selasky  * argument can safely be freed after calling this function. The
28058427ed84SHans Petter Selasky  * pnpinfo of an interface will be reset at device detach or when
28068427ed84SHans Petter Selasky  * passing a NULL argument to this function. This function
28078427ed84SHans Petter Selasky  * returns zero on success, else a USB_ERR_XXX failure code.
28088427ed84SHans Petter Selasky  */
28098427ed84SHans Petter Selasky 
28108427ed84SHans Petter Selasky usb_error_t
28118427ed84SHans Petter Selasky usbd_set_pnpinfo(struct usb_device *udev, uint8_t iface_index, const char *pnpinfo)
28128427ed84SHans Petter Selasky {
28138427ed84SHans Petter Selasky 	struct usb_interface *iface;
28148427ed84SHans Petter Selasky 
28158427ed84SHans Petter Selasky 	iface = usbd_get_iface(udev, iface_index);
28168427ed84SHans Petter Selasky 	if (iface == NULL)
28178427ed84SHans Petter Selasky 		return (USB_ERR_INVAL);
28188427ed84SHans Petter Selasky 
28198427ed84SHans Petter Selasky 	if (iface->pnpinfo != NULL) {
28208427ed84SHans Petter Selasky 		free(iface->pnpinfo, M_USBDEV);
28218427ed84SHans Petter Selasky 		iface->pnpinfo = NULL;
28228427ed84SHans Petter Selasky 	}
28238427ed84SHans Petter Selasky 
28248427ed84SHans Petter Selasky 	if (pnpinfo == NULL || pnpinfo[0] == 0)
28258427ed84SHans Petter Selasky 		return (0);		/* success */
28268427ed84SHans Petter Selasky 
28278427ed84SHans Petter Selasky 	iface->pnpinfo = strdup(pnpinfo, M_USBDEV);
28288427ed84SHans Petter Selasky 	if (iface->pnpinfo == NULL)
28298427ed84SHans Petter Selasky 		return (USB_ERR_NOMEM);
28308427ed84SHans Petter Selasky 
28318427ed84SHans Petter Selasky 	return (0);			/* success */
28328427ed84SHans Petter Selasky }
28338427ed84SHans Petter Selasky 
2834d46dc4adSHans Petter Selasky usb_error_t
2835d46dc4adSHans Petter Selasky usbd_add_dynamic_quirk(struct usb_device *udev, uint16_t quirk)
2836d46dc4adSHans Petter Selasky {
2837d46dc4adSHans Petter Selasky 	uint8_t x;
2838d46dc4adSHans Petter Selasky 
2839d46dc4adSHans Petter Selasky 	for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) {
28403f37fb62SHans Petter Selasky 		if (udev->autoQuirk[x] == 0 ||
28413f37fb62SHans Petter Selasky 		    udev->autoQuirk[x] == quirk) {
2842d46dc4adSHans Petter Selasky 			udev->autoQuirk[x] = quirk;
2843d46dc4adSHans Petter Selasky 			return (0);	/* success */
2844d46dc4adSHans Petter Selasky 		}
2845d46dc4adSHans Petter Selasky 	}
2846d46dc4adSHans Petter Selasky 	return (USB_ERR_NOMEM);
2847d46dc4adSHans Petter Selasky }
2848a5cf1aaaSHans Petter Selasky 
2849a5cf1aaaSHans Petter Selasky /*
2850a5cf1aaaSHans Petter Selasky  * The following function is used to select the endpoint mode. It
2851a5cf1aaaSHans Petter Selasky  * should not be called outside enumeration context.
2852a5cf1aaaSHans Petter Selasky  */
2853a5cf1aaaSHans Petter Selasky 
2854a5cf1aaaSHans Petter Selasky usb_error_t
2855a5cf1aaaSHans Petter Selasky usbd_set_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep,
2856a5cf1aaaSHans Petter Selasky     uint8_t ep_mode)
2857a5cf1aaaSHans Petter Selasky {
2858a5cf1aaaSHans Petter Selasky 	usb_error_t error;
2859f61fc4bdSHans Petter Selasky 	uint8_t do_unlock;
2860a5cf1aaaSHans Petter Selasky 
28616950c75fSHans Petter Selasky 	/* Prevent re-enumeration */
28626950c75fSHans Petter Selasky 	do_unlock = usbd_enum_lock(udev);
2863a5cf1aaaSHans Petter Selasky 
2864a5cf1aaaSHans Petter Selasky 	if (udev->bus->methods->set_endpoint_mode != NULL) {
2865a5cf1aaaSHans Petter Selasky 		error = (udev->bus->methods->set_endpoint_mode) (
2866a5cf1aaaSHans Petter Selasky 		    udev, ep, ep_mode);
2867a5cf1aaaSHans Petter Selasky 	} else if (ep_mode != USB_EP_MODE_DEFAULT) {
2868a5cf1aaaSHans Petter Selasky 		error = USB_ERR_INVAL;
2869a5cf1aaaSHans Petter Selasky 	} else {
2870a5cf1aaaSHans Petter Selasky 		error = 0;
2871a5cf1aaaSHans Petter Selasky 	}
2872a5cf1aaaSHans Petter Selasky 
2873a5cf1aaaSHans Petter Selasky 	/* only set new mode regardless of error */
2874a5cf1aaaSHans Petter Selasky 	ep->ep_mode = ep_mode;
2875a5cf1aaaSHans Petter Selasky 
2876f61fc4bdSHans Petter Selasky 	if (do_unlock)
2877f61fc4bdSHans Petter Selasky 		usbd_enum_unlock(udev);
2878a5cf1aaaSHans Petter Selasky 	return (error);
2879a5cf1aaaSHans Petter Selasky }
2880a5cf1aaaSHans Petter Selasky 
2881a5cf1aaaSHans Petter Selasky uint8_t
2882a5cf1aaaSHans Petter Selasky usbd_get_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep)
2883a5cf1aaaSHans Petter Selasky {
2884a5cf1aaaSHans Petter Selasky 	return (ep->ep_mode);
2885a5cf1aaaSHans Petter Selasky }
2886