xref: /freebsd/sys/dev/usb/usb_device.c (revision d3bf5efc)
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 	/*
19320733245SPedro F. Giffuni 	 * Iterate across 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 	/*
30320733245SPedro F. Giffuni 	 * Iterate across 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 */
5098d59ecb2SHans Petter Selasky 	if (udev->linux_endpoint_start != NULL) {
5108d59ecb2SHans Petter Selasky 		usb_linux_free_device_p(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 		}
1107d3bf5efcSHans Petter Selasky 		/* detach and delete child */
110802ac6454SAndrew Thompson 		if (device_delete_child(udev->parent_dev, dev)) {
110902ac6454SAndrew Thompson 			goto error;
111002ac6454SAndrew Thompson 		}
111102ac6454SAndrew Thompson 	}
11128427ed84SHans Petter Selasky 
11138427ed84SHans Petter Selasky 	pnpinfo = *ppnpinfo;
11148427ed84SHans Petter Selasky 	if (pnpinfo != NULL) {
11158427ed84SHans Petter Selasky 		*ppnpinfo = NULL;
11168427ed84SHans Petter Selasky 		free(pnpinfo, M_USBDEV);
11178427ed84SHans Petter Selasky 	}
111802ac6454SAndrew Thompson 	return;
111902ac6454SAndrew Thompson 
112002ac6454SAndrew Thompson error:
112102ac6454SAndrew Thompson 	/* Detach is not allowed to fail in the USB world */
11228427ed84SHans Petter Selasky 	panic("usb_detach_device_sub: A USB driver would not detach\n");
112302ac6454SAndrew Thompson }
112402ac6454SAndrew Thompson 
112502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1126a593f6b8SAndrew Thompson  *	usb_detach_device
112702ac6454SAndrew Thompson  *
112802ac6454SAndrew Thompson  * The following function will detach the matching interfaces.
112902ac6454SAndrew Thompson  * This function is NULL safe.
1130bdd41206SAndrew Thompson  *
1131bdd41206SAndrew Thompson  * Flag values, see "USB_UNCFG_FLAG_XXX".
113202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
113302ac6454SAndrew Thompson void
1134a593f6b8SAndrew Thompson usb_detach_device(struct usb_device *udev, uint8_t iface_index,
1135bdd41206SAndrew Thompson     uint8_t flag)
113602ac6454SAndrew Thompson {
1137760bc48eSAndrew Thompson 	struct usb_interface *iface;
113802ac6454SAndrew Thompson 	uint8_t i;
113902ac6454SAndrew Thompson 
114002ac6454SAndrew Thompson 	if (udev == NULL) {
114102ac6454SAndrew Thompson 		/* nothing to do */
114202ac6454SAndrew Thompson 		return;
114302ac6454SAndrew Thompson 	}
114402ac6454SAndrew Thompson 	DPRINTFN(4, "udev=%p\n", udev);
114502ac6454SAndrew Thompson 
114691cd9240SAndrew Thompson 	sx_assert(&udev->enum_sx, SA_LOCKED);
114702ac6454SAndrew Thompson 
114802ac6454SAndrew Thompson 	/*
114902ac6454SAndrew Thompson 	 * First detach the child to give the child's detach routine a
115002ac6454SAndrew Thompson 	 * chance to detach the sub-devices in the correct order.
115102ac6454SAndrew Thompson 	 * Then delete the child using "device_delete_child()" which
115202ac6454SAndrew Thompson 	 * will detach all sub-devices from the bottom and upwards!
115302ac6454SAndrew Thompson 	 */
115402ac6454SAndrew Thompson 	if (iface_index != USB_IFACE_INDEX_ANY) {
115502ac6454SAndrew Thompson 		i = iface_index;
115602ac6454SAndrew Thompson 		iface_index = i + 1;
115702ac6454SAndrew Thompson 	} else {
115802ac6454SAndrew Thompson 		i = 0;
115902ac6454SAndrew Thompson 		iface_index = USB_IFACE_MAX;
116002ac6454SAndrew Thompson 	}
116102ac6454SAndrew Thompson 
116202ac6454SAndrew Thompson 	/* do the detach */
116302ac6454SAndrew Thompson 
116402ac6454SAndrew Thompson 	for (; i != iface_index; i++) {
116502ac6454SAndrew Thompson 
1166a593f6b8SAndrew Thompson 		iface = usbd_get_iface(udev, i);
116702ac6454SAndrew Thompson 		if (iface == NULL) {
116802ac6454SAndrew Thompson 			/* looks like the end of the USB interfaces */
116902ac6454SAndrew Thompson 			break;
117002ac6454SAndrew Thompson 		}
11718427ed84SHans Petter Selasky 		usb_detach_device_sub(udev, &iface->subdev,
11728427ed84SHans Petter Selasky 		    &iface->pnpinfo, flag);
117302ac6454SAndrew Thompson 	}
117402ac6454SAndrew Thompson }
117502ac6454SAndrew Thompson 
117602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1177a593f6b8SAndrew Thompson  *	usb_probe_and_attach_sub
117802ac6454SAndrew Thompson  *
117902ac6454SAndrew Thompson  * Returns:
118002ac6454SAndrew Thompson  *    0: Success
118102ac6454SAndrew Thompson  * Else: Failure
118202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
118302ac6454SAndrew Thompson static uint8_t
1184a593f6b8SAndrew Thompson usb_probe_and_attach_sub(struct usb_device *udev,
1185760bc48eSAndrew Thompson     struct usb_attach_arg *uaa)
118602ac6454SAndrew Thompson {
1187760bc48eSAndrew Thompson 	struct usb_interface *iface;
118802ac6454SAndrew Thompson 	device_t dev;
118902ac6454SAndrew Thompson 	int err;
119002ac6454SAndrew Thompson 
119102ac6454SAndrew Thompson 	iface = uaa->iface;
119202ac6454SAndrew Thompson 	if (iface->parent_iface_index != USB_IFACE_INDEX_ANY) {
119302ac6454SAndrew Thompson 		/* leave interface alone */
119402ac6454SAndrew Thompson 		return (0);
119502ac6454SAndrew Thompson 	}
119602ac6454SAndrew Thompson 	dev = iface->subdev;
119702ac6454SAndrew Thompson 	if (dev) {
119802ac6454SAndrew Thompson 
119902ac6454SAndrew Thompson 		/* clean up after module unload */
120002ac6454SAndrew Thompson 
120102ac6454SAndrew Thompson 		if (device_is_attached(dev)) {
120202ac6454SAndrew Thompson 			/* already a device there */
120302ac6454SAndrew Thompson 			return (0);
120402ac6454SAndrew Thompson 		}
120502ac6454SAndrew Thompson 		/* clear "iface->subdev" as early as possible */
120602ac6454SAndrew Thompson 
120702ac6454SAndrew Thompson 		iface->subdev = NULL;
120802ac6454SAndrew Thompson 
120902ac6454SAndrew Thompson 		if (device_delete_child(udev->parent_dev, dev)) {
121002ac6454SAndrew Thompson 
121102ac6454SAndrew Thompson 			/*
121202ac6454SAndrew Thompson 			 * Panic here, else one can get a double call
121302ac6454SAndrew Thompson 			 * to device_detach().  USB devices should
121402ac6454SAndrew Thompson 			 * never fail on detach!
121502ac6454SAndrew Thompson 			 */
1216767cb2e2SAndrew Thompson 			panic("device_delete_child() failed\n");
121702ac6454SAndrew Thompson 		}
121802ac6454SAndrew Thompson 	}
121902ac6454SAndrew Thompson 	if (uaa->temp_dev == NULL) {
122002ac6454SAndrew Thompson 
122102ac6454SAndrew Thompson 		/* create a new child */
122202ac6454SAndrew Thompson 		uaa->temp_dev = device_add_child(udev->parent_dev, NULL, -1);
122302ac6454SAndrew Thompson 		if (uaa->temp_dev == NULL) {
122402ac6454SAndrew Thompson 			device_printf(udev->parent_dev,
1225767cb2e2SAndrew Thompson 			    "Device creation failed\n");
122602ac6454SAndrew Thompson 			return (1);	/* failure */
122702ac6454SAndrew Thompson 		}
122802ac6454SAndrew Thompson 		device_set_ivars(uaa->temp_dev, uaa);
122902ac6454SAndrew Thompson 		device_quiet(uaa->temp_dev);
123002ac6454SAndrew Thompson 	}
123102ac6454SAndrew Thompson 	/*
123202ac6454SAndrew Thompson 	 * Set "subdev" before probe and attach so that "devd" gets
123302ac6454SAndrew Thompson 	 * the information it needs.
123402ac6454SAndrew Thompson 	 */
123502ac6454SAndrew Thompson 	iface->subdev = uaa->temp_dev;
123602ac6454SAndrew Thompson 
123702ac6454SAndrew Thompson 	if (device_probe_and_attach(iface->subdev) == 0) {
123802ac6454SAndrew Thompson 		/*
123902ac6454SAndrew Thompson 		 * The USB attach arguments are only available during probe
124002ac6454SAndrew Thompson 		 * and attach !
124102ac6454SAndrew Thompson 		 */
124202ac6454SAndrew Thompson 		uaa->temp_dev = NULL;
124302ac6454SAndrew Thompson 		device_set_ivars(iface->subdev, NULL);
124402ac6454SAndrew Thompson 
1245ec8f3127SAndrew Thompson 		if (udev->flags.peer_suspended) {
124602ac6454SAndrew Thompson 			err = DEVICE_SUSPEND(iface->subdev);
124788b4e0abSAndrew Thompson 			if (err)
124802ac6454SAndrew Thompson 				device_printf(iface->subdev, "Suspend failed\n");
124902ac6454SAndrew Thompson 		}
125002ac6454SAndrew Thompson 		return (0);		/* success */
125102ac6454SAndrew Thompson 	} else {
125202ac6454SAndrew Thompson 		/* No USB driver found */
125302ac6454SAndrew Thompson 		iface->subdev = NULL;
125402ac6454SAndrew Thompson 	}
125502ac6454SAndrew Thompson 	return (1);			/* failure */
125602ac6454SAndrew Thompson }
125702ac6454SAndrew Thompson 
125802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1259a593f6b8SAndrew Thompson  *	usbd_set_parent_iface
126002ac6454SAndrew Thompson  *
126102ac6454SAndrew Thompson  * Using this function will lock the alternate interface setting on an
126202ac6454SAndrew Thompson  * interface. It is typically used for multi interface drivers. In USB
126302ac6454SAndrew Thompson  * device side mode it is assumed that the alternate interfaces all
126402ac6454SAndrew Thompson  * have the same endpoint descriptors. The default parent index value
126502ac6454SAndrew Thompson  * is "USB_IFACE_INDEX_ANY". Then the alternate setting value is not
126602ac6454SAndrew Thompson  * locked.
126702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
126802ac6454SAndrew Thompson void
1269a593f6b8SAndrew Thompson usbd_set_parent_iface(struct usb_device *udev, uint8_t iface_index,
127002ac6454SAndrew Thompson     uint8_t parent_index)
127102ac6454SAndrew Thompson {
1272760bc48eSAndrew Thompson 	struct usb_interface *iface;
127302ac6454SAndrew Thompson 
1274d9073c1eSHans Petter Selasky 	if (udev == NULL) {
1275d9073c1eSHans Petter Selasky 		/* nothing to do */
1276d9073c1eSHans Petter Selasky 		return;
127702ac6454SAndrew Thompson 	}
1278d9073c1eSHans Petter Selasky 	iface = usbd_get_iface(udev, iface_index);
1279d9073c1eSHans Petter Selasky 	if (iface != NULL)
1280d9073c1eSHans Petter Selasky 		iface->parent_iface_index = parent_index;
128102ac6454SAndrew Thompson }
128202ac6454SAndrew Thompson 
128302ac6454SAndrew Thompson static void
1284a593f6b8SAndrew Thompson usb_init_attach_arg(struct usb_device *udev,
1285760bc48eSAndrew Thompson     struct usb_attach_arg *uaa)
128602ac6454SAndrew Thompson {
1287d46dc4adSHans Petter Selasky 	memset(uaa, 0, sizeof(*uaa));
128802ac6454SAndrew Thompson 
128902ac6454SAndrew Thompson 	uaa->device = udev;
1290f29a0724SAndrew Thompson 	uaa->usb_mode = udev->flags.usb_mode;
129102ac6454SAndrew Thompson 	uaa->port = udev->port_no;
12922a4c6157SAndrew Thompson 	uaa->dev_state = UAA_DEV_READY;
129302ac6454SAndrew Thompson 
129402ac6454SAndrew Thompson 	uaa->info.idVendor = UGETW(udev->ddesc.idVendor);
129502ac6454SAndrew Thompson 	uaa->info.idProduct = UGETW(udev->ddesc.idProduct);
129602ac6454SAndrew Thompson 	uaa->info.bcdDevice = UGETW(udev->ddesc.bcdDevice);
129702ac6454SAndrew Thompson 	uaa->info.bDeviceClass = udev->ddesc.bDeviceClass;
129802ac6454SAndrew Thompson 	uaa->info.bDeviceSubClass = udev->ddesc.bDeviceSubClass;
129902ac6454SAndrew Thompson 	uaa->info.bDeviceProtocol = udev->ddesc.bDeviceProtocol;
130002ac6454SAndrew Thompson 	uaa->info.bConfigIndex = udev->curr_config_index;
130102ac6454SAndrew Thompson 	uaa->info.bConfigNum = udev->curr_config_no;
130202ac6454SAndrew Thompson }
130302ac6454SAndrew Thompson 
130402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1305a593f6b8SAndrew Thompson  *	usb_probe_and_attach
130602ac6454SAndrew Thompson  *
130702ac6454SAndrew Thompson  * This function is called from "uhub_explore_sub()",
1308a593f6b8SAndrew Thompson  * "usb_handle_set_config()" and "usb_handle_request()".
130902ac6454SAndrew Thompson  *
131002ac6454SAndrew Thompson  * Returns:
131102ac6454SAndrew Thompson  *    0: Success
131202ac6454SAndrew Thompson  * Else: A control transfer failed
131302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1314e0a69b51SAndrew Thompson usb_error_t
1315a593f6b8SAndrew Thompson usb_probe_and_attach(struct usb_device *udev, uint8_t iface_index)
131602ac6454SAndrew Thompson {
1317760bc48eSAndrew Thompson 	struct usb_attach_arg uaa;
1318760bc48eSAndrew Thompson 	struct usb_interface *iface;
131902ac6454SAndrew Thompson 	uint8_t i;
132002ac6454SAndrew Thompson 	uint8_t j;
132102ac6454SAndrew Thompson 	uint8_t do_unlock;
132202ac6454SAndrew Thompson 
132302ac6454SAndrew Thompson 	if (udev == NULL) {
132402ac6454SAndrew Thompson 		DPRINTF("udev == NULL\n");
132502ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
132602ac6454SAndrew Thompson 	}
13276950c75fSHans Petter Selasky 	/* Prevent re-enumeration */
13286950c75fSHans Petter Selasky 	do_unlock = usbd_enum_lock(udev);
132902ac6454SAndrew Thompson 
133002ac6454SAndrew Thompson 	if (udev->curr_config_index == USB_UNCONFIG_INDEX) {
133102ac6454SAndrew Thompson 		/* do nothing - no configuration has been set */
133202ac6454SAndrew Thompson 		goto done;
133302ac6454SAndrew Thompson 	}
133402ac6454SAndrew Thompson 	/* setup USB attach arguments */
133502ac6454SAndrew Thompson 
1336a593f6b8SAndrew Thompson 	usb_init_attach_arg(udev, &uaa);
133702ac6454SAndrew Thompson 
1338a6b60150SHans Petter Selasky 	/*
1339a6b60150SHans Petter Selasky 	 * If the whole USB device is targeted, invoke the USB event
1340a6b60150SHans Petter Selasky 	 * handler(s):
1341a6b60150SHans Petter Selasky 	 */
1342a6b60150SHans Petter Selasky 	if (iface_index == USB_IFACE_INDEX_ANY) {
1343a6b60150SHans Petter Selasky 
1344415bcd89SHans Petter Selasky 		if (usb_test_quirk(&uaa, UQ_MSC_DYMO_EJECT) != 0 &&
1345415bcd89SHans Petter Selasky 		    usb_dymo_eject(udev, 0) == 0) {
1346415bcd89SHans Petter Selasky 			/* success, mark the udev as disappearing */
1347415bcd89SHans Petter Selasky 			uaa.dev_state = UAA_DEV_EJECTING;
1348415bcd89SHans Petter Selasky 		}
1349415bcd89SHans Petter Selasky 
1350a6b60150SHans Petter Selasky 		EVENTHANDLER_INVOKE(usb_dev_configured, udev, &uaa);
1351a6b60150SHans Petter Selasky 
1352a6b60150SHans Petter Selasky 		if (uaa.dev_state != UAA_DEV_READY) {
1353a6b60150SHans Petter Selasky 			/* leave device unconfigured */
1354a6b60150SHans Petter Selasky 			usb_unconfigure(udev, 0);
1355a6b60150SHans Petter Selasky 			goto done;
1356a6b60150SHans Petter Selasky 		}
1357a6b60150SHans Petter Selasky 	}
1358a6b60150SHans Petter Selasky 
135902ac6454SAndrew Thompson 	/* Check if only one interface should be probed: */
136002ac6454SAndrew Thompson 	if (iface_index != USB_IFACE_INDEX_ANY) {
136102ac6454SAndrew Thompson 		i = iface_index;
136202ac6454SAndrew Thompson 		j = i + 1;
136302ac6454SAndrew Thompson 	} else {
136402ac6454SAndrew Thompson 		i = 0;
136502ac6454SAndrew Thompson 		j = USB_IFACE_MAX;
136602ac6454SAndrew Thompson 	}
136702ac6454SAndrew Thompson 
136802ac6454SAndrew Thompson 	/* Do the probe and attach */
136902ac6454SAndrew Thompson 	for (; i != j; i++) {
137002ac6454SAndrew Thompson 
1371a593f6b8SAndrew Thompson 		iface = usbd_get_iface(udev, i);
137202ac6454SAndrew Thompson 		if (iface == NULL) {
137302ac6454SAndrew Thompson 			/*
137402ac6454SAndrew Thompson 			 * Looks like the end of the USB
137502ac6454SAndrew Thompson 			 * interfaces !
137602ac6454SAndrew Thompson 			 */
137702ac6454SAndrew Thompson 			DPRINTFN(2, "end of interfaces "
137802ac6454SAndrew Thompson 			    "at %u\n", i);
137902ac6454SAndrew Thompson 			break;
138002ac6454SAndrew Thompson 		}
138102ac6454SAndrew Thompson 		if (iface->idesc == NULL) {
138202ac6454SAndrew Thompson 			/* no interface descriptor */
138302ac6454SAndrew Thompson 			continue;
138402ac6454SAndrew Thompson 		}
138502ac6454SAndrew Thompson 		uaa.iface = iface;
138602ac6454SAndrew Thompson 
138702ac6454SAndrew Thompson 		uaa.info.bInterfaceClass =
138802ac6454SAndrew Thompson 		    iface->idesc->bInterfaceClass;
138902ac6454SAndrew Thompson 		uaa.info.bInterfaceSubClass =
139002ac6454SAndrew Thompson 		    iface->idesc->bInterfaceSubClass;
139102ac6454SAndrew Thompson 		uaa.info.bInterfaceProtocol =
139202ac6454SAndrew Thompson 		    iface->idesc->bInterfaceProtocol;
139302ac6454SAndrew Thompson 		uaa.info.bIfaceIndex = i;
139402ac6454SAndrew Thompson 		uaa.info.bIfaceNum =
139502ac6454SAndrew Thompson 		    iface->idesc->bInterfaceNumber;
1396bd73b187SAlfred Perlstein 		uaa.driver_info = 0;	/* reset driver_info */
139702ac6454SAndrew Thompson 
139802ac6454SAndrew Thompson 		DPRINTFN(2, "iclass=%u/%u/%u iindex=%u/%u\n",
139902ac6454SAndrew Thompson 		    uaa.info.bInterfaceClass,
140002ac6454SAndrew Thompson 		    uaa.info.bInterfaceSubClass,
140102ac6454SAndrew Thompson 		    uaa.info.bInterfaceProtocol,
140202ac6454SAndrew Thompson 		    uaa.info.bIfaceIndex,
140302ac6454SAndrew Thompson 		    uaa.info.bIfaceNum);
140402ac6454SAndrew Thompson 
1405d43ffe94SHans Petter Selasky 		usb_probe_and_attach_sub(udev, &uaa);
140602ac6454SAndrew Thompson 
1407d43ffe94SHans Petter Selasky 		/*
1408d43ffe94SHans Petter Selasky 		 * Remove the leftover child, if any, to enforce that
1409d43ffe94SHans Petter Selasky 		 * a new nomatch devd event is generated for the next
1410d43ffe94SHans Petter Selasky 		 * interface if no driver is found:
1411d43ffe94SHans Petter Selasky 		 */
1412d43ffe94SHans Petter Selasky 		if (uaa.temp_dev == NULL)
1413d43ffe94SHans Petter Selasky 			continue;
1414d43ffe94SHans Petter Selasky 		if (device_delete_child(udev->parent_dev, uaa.temp_dev))
1415767cb2e2SAndrew Thompson 			DPRINTFN(0, "device delete child failed\n");
1416d43ffe94SHans Petter Selasky 		uaa.temp_dev = NULL;
141702ac6454SAndrew Thompson 	}
141802ac6454SAndrew Thompson done:
1419cb18f7d1SAlfred Perlstein 	if (do_unlock)
1420cb18f7d1SAlfred Perlstein 		usbd_enum_unlock(udev);
142102ac6454SAndrew Thompson 	return (0);
142202ac6454SAndrew Thompson }
142302ac6454SAndrew Thompson 
142402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1425a593f6b8SAndrew Thompson  *	usb_suspend_resume_sub
142602ac6454SAndrew Thompson  *
142702ac6454SAndrew Thompson  * This function is called when the suspend or resume methods should
142802ac6454SAndrew Thompson  * be executed on an USB device.
142902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
143002ac6454SAndrew Thompson static void
1431a593f6b8SAndrew Thompson usb_suspend_resume_sub(struct usb_device *udev, device_t dev, uint8_t do_suspend)
143202ac6454SAndrew Thompson {
143302ac6454SAndrew Thompson 	int err;
143402ac6454SAndrew Thompson 
143502ac6454SAndrew Thompson 	if (dev == NULL) {
143602ac6454SAndrew Thompson 		return;
143702ac6454SAndrew Thompson 	}
143802ac6454SAndrew Thompson 	if (!device_is_attached(dev)) {
143902ac6454SAndrew Thompson 		return;
144002ac6454SAndrew Thompson 	}
144102ac6454SAndrew Thompson 	if (do_suspend) {
144202ac6454SAndrew Thompson 		err = DEVICE_SUSPEND(dev);
144302ac6454SAndrew Thompson 	} else {
144402ac6454SAndrew Thompson 		err = DEVICE_RESUME(dev);
144502ac6454SAndrew Thompson 	}
144602ac6454SAndrew Thompson 	if (err) {
1447767cb2e2SAndrew Thompson 		device_printf(dev, "%s failed\n",
144802ac6454SAndrew Thompson 		    do_suspend ? "Suspend" : "Resume");
144902ac6454SAndrew Thompson 	}
145002ac6454SAndrew Thompson }
145102ac6454SAndrew Thompson 
145202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1453a593f6b8SAndrew Thompson  *	usb_suspend_resume
145402ac6454SAndrew Thompson  *
145502ac6454SAndrew Thompson  * The following function will suspend or resume the USB device.
145602ac6454SAndrew Thompson  *
145702ac6454SAndrew Thompson  * Returns:
145802ac6454SAndrew Thompson  *    0: Success
145902ac6454SAndrew Thompson  * Else: Failure
146002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1461e0a69b51SAndrew Thompson usb_error_t
1462a593f6b8SAndrew Thompson usb_suspend_resume(struct usb_device *udev, uint8_t do_suspend)
146302ac6454SAndrew Thompson {
1464760bc48eSAndrew Thompson 	struct usb_interface *iface;
146502ac6454SAndrew Thompson 	uint8_t i;
146602ac6454SAndrew Thompson 
146702ac6454SAndrew Thompson 	if (udev == NULL) {
146802ac6454SAndrew Thompson 		/* nothing to do */
146902ac6454SAndrew Thompson 		return (0);
147002ac6454SAndrew Thompson 	}
147102ac6454SAndrew Thompson 	DPRINTFN(4, "udev=%p do_suspend=%d\n", udev, do_suspend);
147202ac6454SAndrew Thompson 
14732df1e9a6SAndrew Thompson 	sx_assert(&udev->sr_sx, SA_LOCKED);
147402ac6454SAndrew Thompson 
147502ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
147602ac6454SAndrew Thompson 	/* filter the suspend events */
1477ec8f3127SAndrew Thompson 	if (udev->flags.peer_suspended == do_suspend) {
147802ac6454SAndrew Thompson 		USB_BUS_UNLOCK(udev->bus);
147902ac6454SAndrew Thompson 		/* nothing to do */
148002ac6454SAndrew Thompson 		return (0);
148102ac6454SAndrew Thompson 	}
1482ec8f3127SAndrew Thompson 	udev->flags.peer_suspended = do_suspend;
148302ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
148402ac6454SAndrew Thompson 
148502ac6454SAndrew Thompson 	/* do the suspend or resume */
148602ac6454SAndrew Thompson 
148702ac6454SAndrew Thompson 	for (i = 0; i != USB_IFACE_MAX; i++) {
148802ac6454SAndrew Thompson 
1489a593f6b8SAndrew Thompson 		iface = usbd_get_iface(udev, i);
149002ac6454SAndrew Thompson 		if (iface == NULL) {
149102ac6454SAndrew Thompson 			/* looks like the end of the USB interfaces */
149202ac6454SAndrew Thompson 			break;
149302ac6454SAndrew Thompson 		}
1494a593f6b8SAndrew Thompson 		usb_suspend_resume_sub(udev, iface->subdev, do_suspend);
149502ac6454SAndrew Thompson 	}
149602ac6454SAndrew Thompson 	return (0);
149702ac6454SAndrew Thompson }
149802ac6454SAndrew Thompson 
149902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1500a593f6b8SAndrew Thompson  *      usbd_clear_stall_proc
150102ac6454SAndrew Thompson  *
150202ac6454SAndrew Thompson  * This function performs generic USB clear stall operations.
150302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
150402ac6454SAndrew Thompson static void
1505a593f6b8SAndrew Thompson usbd_clear_stall_proc(struct usb_proc_msg *_pm)
150602ac6454SAndrew Thompson {
1507a0d53e0bSHans Petter Selasky 	struct usb_udev_msg *pm = (void *)_pm;
1508760bc48eSAndrew Thompson 	struct usb_device *udev = pm->udev;
150902ac6454SAndrew Thompson 
151002ac6454SAndrew Thompson 	/* Change lock */
151102ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
15120eb8d462SHans Petter Selasky 	USB_MTX_LOCK(&udev->device_mtx);
151302ac6454SAndrew Thompson 
151402ac6454SAndrew Thompson 	/* Start clear stall callback */
15155b3bb704SAndrew Thompson 	usbd_transfer_start(udev->ctrl_xfer[1]);
151602ac6454SAndrew Thompson 
151702ac6454SAndrew Thompson 	/* Change lock */
15180eb8d462SHans Petter Selasky 	USB_MTX_UNLOCK(&udev->device_mtx);
151902ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
152002ac6454SAndrew Thompson }
152102ac6454SAndrew Thompson 
152202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1523a593f6b8SAndrew Thompson  *	usb_alloc_device
152402ac6454SAndrew Thompson  *
152502ac6454SAndrew Thompson  * This function allocates a new USB device. This function is called
152602ac6454SAndrew Thompson  * when a new device has been put in the powered state, but not yet in
152702ac6454SAndrew Thompson  * the addressed state. Get initial descriptor, set the address, get
152802ac6454SAndrew Thompson  * full descriptor and get strings.
152902ac6454SAndrew Thompson  *
153002ac6454SAndrew Thompson  * Return values:
153102ac6454SAndrew Thompson  *    0: Failure
153202ac6454SAndrew Thompson  * Else: Success
153302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
1534760bc48eSAndrew Thompson struct usb_device *
1535a593f6b8SAndrew Thompson usb_alloc_device(device_t parent_dev, struct usb_bus *bus,
1536760bc48eSAndrew Thompson     struct usb_device *parent_hub, uint8_t depth, uint8_t port_index,
15378d2dd5ddSAndrew Thompson     uint8_t port_no, enum usb_dev_speed speed, enum usb_hc_mode mode)
153802ac6454SAndrew Thompson {
1539760bc48eSAndrew Thompson 	struct usb_attach_arg uaa;
1540760bc48eSAndrew Thompson 	struct usb_device *udev;
1541760bc48eSAndrew Thompson 	struct usb_device *adev;
1542760bc48eSAndrew Thompson 	struct usb_device *hub;
154302ac6454SAndrew Thompson 	uint8_t *scratch_ptr;
1544e0a69b51SAndrew Thompson 	usb_error_t err;
154502ac6454SAndrew Thompson 	uint8_t device_index;
15462a4c6157SAndrew Thompson 	uint8_t config_index;
15472a4c6157SAndrew Thompson 	uint8_t config_quirk;
15482a4c6157SAndrew Thompson 	uint8_t set_config_failed;
15496950c75fSHans Petter Selasky 	uint8_t do_unlock;
155002ac6454SAndrew Thompson 
155102ac6454SAndrew Thompson 	DPRINTF("parent_dev=%p, bus=%p, parent_hub=%p, depth=%u, "
1552f29a0724SAndrew Thompson 	    "port_index=%u, port_no=%u, speed=%u, usb_mode=%u\n",
155302ac6454SAndrew Thompson 	    parent_dev, bus, parent_hub, depth, port_index, port_no,
1554f29a0724SAndrew Thompson 	    speed, mode);
155502ac6454SAndrew Thompson 
155602ac6454SAndrew Thompson 	/*
155702ac6454SAndrew Thompson 	 * Find an unused device index. In USB Host mode this is the
155802ac6454SAndrew Thompson 	 * same as the device address.
155902ac6454SAndrew Thompson 	 *
156002ac6454SAndrew Thompson 	 * Device index zero is not used and device index 1 should
156102ac6454SAndrew Thompson 	 * always be the root hub.
156202ac6454SAndrew Thompson 	 */
156302ac6454SAndrew Thompson 	for (device_index = USB_ROOT_HUB_ADDR;
156402ac6454SAndrew Thompson 	    (device_index != bus->devices_max) &&
156502ac6454SAndrew Thompson 	    (bus->devices[device_index] != NULL);
156602ac6454SAndrew Thompson 	    device_index++) /* nop */;
156702ac6454SAndrew Thompson 
156802ac6454SAndrew Thompson 	if (device_index == bus->devices_max) {
156902ac6454SAndrew Thompson 		device_printf(bus->bdev,
1570767cb2e2SAndrew Thompson 		    "No free USB device index for new device\n");
157102ac6454SAndrew Thompson 		return (NULL);
157202ac6454SAndrew Thompson 	}
157302ac6454SAndrew Thompson 
157402ac6454SAndrew Thompson 	if (depth > 0x10) {
157502ac6454SAndrew Thompson 		device_printf(bus->bdev,
1576767cb2e2SAndrew Thompson 		    "Invalid device depth\n");
157702ac6454SAndrew Thompson 		return (NULL);
157802ac6454SAndrew Thompson 	}
157902ac6454SAndrew Thompson 	udev = malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO);
158002ac6454SAndrew Thompson 	if (udev == NULL) {
158102ac6454SAndrew Thompson 		return (NULL);
158202ac6454SAndrew Thompson 	}
158302ac6454SAndrew Thompson 	/* initialise our SX-lock */
158491cd9240SAndrew Thompson 	sx_init_flags(&udev->enum_sx, "USB config SX lock", SX_DUPOK);
1585a6b60150SHans Petter Selasky 	sx_init_flags(&udev->sr_sx, "USB suspend and resume SX lock", SX_NOWITNESS);
158664cb5e2aSHans Petter Selasky 	sx_init_flags(&udev->ctrl_sx, "USB control transfer SX lock", SX_DUPOK);
158702ac6454SAndrew Thompson 
158891cd9240SAndrew Thompson 	cv_init(&udev->ctrlreq_cv, "WCTRL");
158991cd9240SAndrew Thompson 	cv_init(&udev->ref_cv, "UGONE");
159002ac6454SAndrew Thompson 
159102ac6454SAndrew Thompson 	/* initialise our mutex */
159291cd9240SAndrew Thompson 	mtx_init(&udev->device_mtx, "USB device mutex", NULL, MTX_DEF);
159302ac6454SAndrew Thompson 
159402ac6454SAndrew Thompson 	/* initialise generic clear stall */
1595a593f6b8SAndrew Thompson 	udev->cs_msg[0].hdr.pm_callback = &usbd_clear_stall_proc;
159602ac6454SAndrew Thompson 	udev->cs_msg[0].udev = udev;
1597a593f6b8SAndrew Thompson 	udev->cs_msg[1].hdr.pm_callback = &usbd_clear_stall_proc;
159802ac6454SAndrew Thompson 	udev->cs_msg[1].udev = udev;
159902ac6454SAndrew Thompson 
160002ac6454SAndrew Thompson 	/* initialise some USB device fields */
160102ac6454SAndrew Thompson 	udev->parent_hub = parent_hub;
160202ac6454SAndrew Thompson 	udev->parent_dev = parent_dev;
160302ac6454SAndrew Thompson 	udev->port_index = port_index;
160402ac6454SAndrew Thompson 	udev->port_no = port_no;
160502ac6454SAndrew Thompson 	udev->depth = depth;
160602ac6454SAndrew Thompson 	udev->bus = bus;
160702ac6454SAndrew Thompson 	udev->address = USB_START_ADDR;	/* default value */
1608e0a69b51SAndrew Thompson 	udev->plugtime = (usb_ticks_t)ticks;
160902ac6454SAndrew Thompson 	/*
161002ac6454SAndrew Thompson 	 * We need to force the power mode to "on" because there are plenty
161102ac6454SAndrew Thompson 	 * of USB devices out there that do not work very well with
161202ac6454SAndrew Thompson 	 * automatic suspend and resume!
161302ac6454SAndrew Thompson 	 */
1614d2d71ce7SAndrew Thompson 	udev->power_mode = usbd_filter_power_mode(udev, USB_POWER_MODE_ON);
161502ac6454SAndrew Thompson 	udev->pwr_save.last_xfer_time = ticks;
161602ac6454SAndrew Thompson 	/* we are not ready yet */
161702ac6454SAndrew Thompson 	udev->refcount = 1;
161802ac6454SAndrew Thompson 
161902ac6454SAndrew Thompson 	/* set up default endpoint descriptor */
16205b3bb704SAndrew Thompson 	udev->ctrl_ep_desc.bLength = sizeof(udev->ctrl_ep_desc);
16215b3bb704SAndrew Thompson 	udev->ctrl_ep_desc.bDescriptorType = UDESC_ENDPOINT;
16225b3bb704SAndrew Thompson 	udev->ctrl_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
16235b3bb704SAndrew Thompson 	udev->ctrl_ep_desc.bmAttributes = UE_CONTROL;
16245b3bb704SAndrew Thompson 	udev->ctrl_ep_desc.wMaxPacketSize[0] = USB_MAX_IPACKET;
16255b3bb704SAndrew Thompson 	udev->ctrl_ep_desc.wMaxPacketSize[1] = 0;
16265b3bb704SAndrew Thompson 	udev->ctrl_ep_desc.bInterval = 0;
1627963169b4SHans Petter Selasky 
1628963169b4SHans Petter Selasky 	/* set up default endpoint companion descriptor */
1629963169b4SHans Petter Selasky 	udev->ctrl_ep_comp_desc.bLength = sizeof(udev->ctrl_ep_comp_desc);
1630963169b4SHans Petter Selasky 	udev->ctrl_ep_comp_desc.bDescriptorType = UDESC_ENDPOINT_SS_COMP;
1631963169b4SHans Petter Selasky 
163202ac6454SAndrew Thompson 	udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
163302ac6454SAndrew Thompson 
163402ac6454SAndrew Thompson 	udev->speed = speed;
1635f29a0724SAndrew Thompson 	udev->flags.usb_mode = mode;
163602ac6454SAndrew Thompson 
163702ac6454SAndrew Thompson 	/* search for our High Speed USB HUB, if any */
163802ac6454SAndrew Thompson 
163902ac6454SAndrew Thompson 	adev = udev;
164002ac6454SAndrew Thompson 	hub = udev->parent_hub;
164102ac6454SAndrew Thompson 
164202ac6454SAndrew Thompson 	while (hub) {
164302ac6454SAndrew Thompson 		if (hub->speed == USB_SPEED_HIGH) {
164402ac6454SAndrew Thompson 			udev->hs_hub_addr = hub->address;
1645495f25ceSAndrew Thompson 			udev->parent_hs_hub = hub;
164602ac6454SAndrew Thompson 			udev->hs_port_no = adev->port_no;
164702ac6454SAndrew Thompson 			break;
164802ac6454SAndrew Thompson 		}
164902ac6454SAndrew Thompson 		adev = hub;
165002ac6454SAndrew Thompson 		hub = hub->parent_hub;
165102ac6454SAndrew Thompson 	}
165202ac6454SAndrew Thompson 
1653ae60fdfbSAndrew Thompson 	/* init the default endpoint */
1654a593f6b8SAndrew Thompson 	usb_init_endpoint(udev, 0,
16555b3bb704SAndrew Thompson 	    &udev->ctrl_ep_desc,
1656963169b4SHans Petter Selasky 	    &udev->ctrl_ep_comp_desc,
16575b3bb704SAndrew Thompson 	    &udev->ctrl_ep);
165802ac6454SAndrew Thompson 
165902ac6454SAndrew Thompson 	/* set device index */
166002ac6454SAndrew Thompson 	udev->device_index = device_index;
166102ac6454SAndrew Thompson 
16628755859aSAndrew Thompson #if USB_HAVE_UGEN
16630f6e8f93SAndrew Thompson 	/* Create ugen name */
1664ee3e3ff5SAndrew Thompson 	snprintf(udev->ugen_name, sizeof(udev->ugen_name),
1665ee3e3ff5SAndrew Thompson 	    USB_GENERIC_NAME "%u.%u", device_get_unit(bus->bdev),
1666ee3e3ff5SAndrew Thompson 	    device_index);
16678755859aSAndrew Thompson 	LIST_INIT(&udev->pd_list);
16688755859aSAndrew Thompson 
16690f6e8f93SAndrew Thompson 	/* Create the control endpoint device */
16702ffd5fdcSHans Petter Selasky 	udev->ctrl_dev = usb_make_dev(udev, NULL, 0, 0,
16712ffd5fdcSHans Petter Selasky 	    FREAD|FWRITE, UID_ROOT, GID_OPERATOR, 0600);
16720f6e8f93SAndrew Thompson 
16730f6e8f93SAndrew Thompson 	/* Create a link from /dev/ugenX.X to the default endpoint */
16742ffd5fdcSHans Petter Selasky 	if (udev->ctrl_dev != NULL)
16752ffd5fdcSHans Petter Selasky 		make_dev_alias(udev->ctrl_dev->cdev, "%s", udev->ugen_name);
16760f6e8f93SAndrew Thompson #endif
1677963169b4SHans Petter Selasky 	/* Initialise device */
1678963169b4SHans Petter Selasky 	if (bus->methods->device_init != NULL) {
1679963169b4SHans Petter Selasky 		err = (bus->methods->device_init) (udev);
1680963169b4SHans Petter Selasky 		if (err != 0) {
1681963169b4SHans Petter Selasky 			DPRINTFN(0, "device init %d failed "
1682963169b4SHans Petter Selasky 			    "(%s, ignored)\n", device_index,
1683963169b4SHans Petter Selasky 			    usbd_errstr(err));
1684963169b4SHans Petter Selasky 			goto done;
1685963169b4SHans Petter Selasky 		}
1686963169b4SHans Petter Selasky 	}
1687963169b4SHans Petter Selasky 	/* set powered device state after device init is complete */
1688963169b4SHans Petter Selasky 	usb_set_device_state(udev, USB_STATE_POWERED);
1689963169b4SHans Petter Selasky 
1690f29a0724SAndrew Thompson 	if (udev->flags.usb_mode == USB_MODE_HOST) {
169102ac6454SAndrew Thompson 
1692a593f6b8SAndrew Thompson 		err = usbd_req_set_address(udev, NULL, device_index);
169302ac6454SAndrew Thompson 
1694963169b4SHans Petter Selasky 		/*
1695963169b4SHans Petter Selasky 		 * This is the new USB device address from now on, if
1696963169b4SHans Petter Selasky 		 * the set address request didn't set it already.
1697963169b4SHans Petter Selasky 		 */
1698963169b4SHans Petter Selasky 		if (udev->address == USB_START_ADDR)
169902ac6454SAndrew Thompson 			udev->address = device_index;
170002ac6454SAndrew Thompson 
170102ac6454SAndrew Thompson 		/*
170202ac6454SAndrew Thompson 		 * We ignore any set-address errors, hence there are
170302ac6454SAndrew Thompson 		 * buggy USB devices out there that actually receive
170402ac6454SAndrew Thompson 		 * the SETUP PID, but manage to set the address before
170502ac6454SAndrew Thompson 		 * the STATUS stage is ACK'ed. If the device responds
170602ac6454SAndrew Thompson 		 * to the subsequent get-descriptor at the new
170702ac6454SAndrew Thompson 		 * address, then we know that the set-address command
170802ac6454SAndrew Thompson 		 * was successful.
170902ac6454SAndrew Thompson 		 */
171002ac6454SAndrew Thompson 		if (err) {
171102ac6454SAndrew Thompson 			DPRINTFN(0, "set address %d failed "
171203797f33SAndrew Thompson 			    "(%s, ignored)\n", udev->address,
1713a593f6b8SAndrew Thompson 			    usbd_errstr(err));
171402ac6454SAndrew Thompson 		}
171502ac6454SAndrew Thompson 	} else {
171602ac6454SAndrew Thompson 		/* We are not self powered */
171702ac6454SAndrew Thompson 		udev->flags.self_powered = 0;
171802ac6454SAndrew Thompson 
171902ac6454SAndrew Thompson 		/* Set unconfigured state */
172002ac6454SAndrew Thompson 		udev->curr_config_no = USB_UNCONFIG_NO;
172102ac6454SAndrew Thompson 		udev->curr_config_index = USB_UNCONFIG_INDEX;
172202ac6454SAndrew Thompson 
172302ac6454SAndrew Thompson 		/* Setup USB descriptors */
1724a593f6b8SAndrew Thompson 		err = (usb_temp_setup_by_index_p) (udev, usb_template);
172502ac6454SAndrew Thompson 		if (err) {
17266be75992SEdward Tomasz Napierala 			DPRINTFN(0, "setting up USB template failed - "
17276be75992SEdward Tomasz Napierala 			    "usb_template(4) not loaded?\n");
172802ac6454SAndrew Thompson 			goto done;
172902ac6454SAndrew Thompson 		}
173002ac6454SAndrew Thompson 	}
1731a593f6b8SAndrew Thompson 	usb_set_device_state(udev, USB_STATE_ADDRESSED);
173202ac6454SAndrew Thompson 
1733963169b4SHans Petter Selasky 	/* setup the device descriptor and the initial "wMaxPacketSize" */
1734963169b4SHans Petter Selasky 	err = usbd_setup_device_desc(udev, NULL);
1735963169b4SHans Petter Selasky 
1736963169b4SHans Petter Selasky 	if (err != 0) {
173723de050bSHans Petter Selasky 		/* try to enumerate two more times */
1738a593f6b8SAndrew Thompson 		err = usbd_req_re_enumerate(udev, NULL);
173923de050bSHans Petter Selasky 		if (err != 0) {
174023de050bSHans Petter Selasky 			err = usbd_req_re_enumerate(udev, NULL);
174123de050bSHans Petter Selasky 			if (err != 0) {
174202ac6454SAndrew Thompson 				goto done;
174302ac6454SAndrew Thompson 			}
174423de050bSHans Petter Selasky 		}
174523de050bSHans Petter Selasky 	}
174602ac6454SAndrew Thompson 
174702ac6454SAndrew Thompson 	/*
174802ac6454SAndrew Thompson 	 * Setup temporary USB attach args so that we can figure out some
174902ac6454SAndrew Thompson 	 * basic quirks for this device.
175002ac6454SAndrew Thompson 	 */
1751a593f6b8SAndrew Thompson 	usb_init_attach_arg(udev, &uaa);
175202ac6454SAndrew Thompson 
1753a593f6b8SAndrew Thompson 	if (usb_test_quirk(&uaa, UQ_BUS_POWERED)) {
175402ac6454SAndrew Thompson 		udev->flags.uq_bus_powered = 1;
175502ac6454SAndrew Thompson 	}
1756a593f6b8SAndrew Thompson 	if (usb_test_quirk(&uaa, UQ_NO_STRINGS)) {
175702ac6454SAndrew Thompson 		udev->flags.no_strings = 1;
175802ac6454SAndrew Thompson 	}
175902ac6454SAndrew Thompson 	/*
176002ac6454SAndrew Thompson 	 * Workaround for buggy USB devices.
176102ac6454SAndrew Thompson 	 *
176202ac6454SAndrew Thompson 	 * It appears that some string-less USB chips will crash and
176302ac6454SAndrew Thompson 	 * disappear if any attempts are made to read any string
176402ac6454SAndrew Thompson 	 * descriptors.
176502ac6454SAndrew Thompson 	 *
176602ac6454SAndrew Thompson 	 * Try to detect such chips by checking the strings in the USB
176702ac6454SAndrew Thompson 	 * device descriptor. If no strings are present there we
176802ac6454SAndrew Thompson 	 * simply disable all USB strings.
176902ac6454SAndrew Thompson 	 */
17706950c75fSHans Petter Selasky 
17716950c75fSHans Petter Selasky 	/* Protect scratch area */
177264cb5e2aSHans Petter Selasky 	do_unlock = usbd_ctrl_lock(udev);
17736950c75fSHans Petter Selasky 
17746950c75fSHans Petter Selasky 	scratch_ptr = udev->scratch.data;
177502ac6454SAndrew Thompson 
1776869df733SHans Petter Selasky 	if (udev->flags.no_strings) {
1777869df733SHans Petter Selasky 		err = USB_ERR_INVAL;
1778869df733SHans Petter Selasky 	} else if (udev->ddesc.iManufacturer ||
177902ac6454SAndrew Thompson 	    udev->ddesc.iProduct ||
178002ac6454SAndrew Thompson 	    udev->ddesc.iSerialNumber) {
178102ac6454SAndrew Thompson 		/* read out the language ID string */
1782a593f6b8SAndrew Thompson 		err = usbd_req_get_string_desc(udev, NULL,
1783c10e1453SAndrew Thompson 		    (char *)scratch_ptr, 4, 0, USB_LANGUAGE_TABLE);
178402ac6454SAndrew Thompson 	} else {
178502ac6454SAndrew Thompson 		err = USB_ERR_INVAL;
178602ac6454SAndrew Thompson 	}
178702ac6454SAndrew Thompson 
178802ac6454SAndrew Thompson 	if (err || (scratch_ptr[0] < 4)) {
178902ac6454SAndrew Thompson 		udev->flags.no_strings = 1;
179002ac6454SAndrew Thompson 	} else {
17917a918edfSAndrew Thompson 		uint16_t langid;
17927a918edfSAndrew Thompson 		uint16_t pref;
17937a918edfSAndrew Thompson 		uint16_t mask;
17947a918edfSAndrew Thompson 		uint8_t x;
17957a918edfSAndrew Thompson 
17967a918edfSAndrew Thompson 		/* load preferred value and mask */
17977a918edfSAndrew Thompson 		pref = usb_lang_id;
17987a918edfSAndrew Thompson 		mask = usb_lang_mask;
17997a918edfSAndrew Thompson 
18007a918edfSAndrew Thompson 		/* align length correctly */
18016950c75fSHans Petter Selasky 		scratch_ptr[0] &= ~1U;
18027a918edfSAndrew Thompson 
18037a918edfSAndrew Thompson 		/* fix compiler warning */
18047a918edfSAndrew Thompson 		langid = 0;
18057a918edfSAndrew Thompson 
18067a918edfSAndrew Thompson 		/* search for preferred language */
18077a918edfSAndrew Thompson 		for (x = 2; (x < scratch_ptr[0]); x += 2) {
18087a918edfSAndrew Thompson 			langid = UGETW(scratch_ptr + x);
18097a918edfSAndrew Thompson 			if ((langid & mask) == pref)
18107a918edfSAndrew Thompson 				break;
18117a918edfSAndrew Thompson 		}
18127a918edfSAndrew Thompson 		if (x >= scratch_ptr[0]) {
181302ac6454SAndrew Thompson 			/* pick the first language as the default */
18147a918edfSAndrew Thompson 			DPRINTFN(1, "Using first language\n");
18157a918edfSAndrew Thompson 			langid = UGETW(scratch_ptr + 2);
18167a918edfSAndrew Thompson 		}
18177a918edfSAndrew Thompson 
18187a918edfSAndrew Thompson 		DPRINTFN(1, "Language selected: 0x%04x\n", langid);
18197a918edfSAndrew Thompson 		udev->langid = langid;
182002ac6454SAndrew Thompson 	}
182102ac6454SAndrew Thompson 
18226950c75fSHans Petter Selasky 	if (do_unlock)
182364cb5e2aSHans Petter Selasky 		usbd_ctrl_unlock(udev);
18246950c75fSHans Petter Selasky 
182502ac6454SAndrew Thompson 	/* assume 100mA bus powered for now. Changed when configured. */
182602ac6454SAndrew Thompson 	udev->power = USB_MIN_POWER;
1827c86aead6SAndrew Thompson 	/* fetch the vendor and product strings from the device */
1828a593f6b8SAndrew Thompson 	usbd_set_device_strings(udev);
182902ac6454SAndrew Thompson 
18302a4c6157SAndrew Thompson 	if (udev->flags.usb_mode == USB_MODE_DEVICE) {
18312a4c6157SAndrew Thompson 		/* USB device mode setup is complete */
18322a4c6157SAndrew Thompson 		err = 0;
18332a4c6157SAndrew Thompson 		goto config_done;
18342a4c6157SAndrew Thompson 	}
183502ac6454SAndrew Thompson 
183602ac6454SAndrew Thompson 	/*
183702ac6454SAndrew Thompson 	 * Most USB devices should attach to config index 0 by
183802ac6454SAndrew Thompson 	 * default
183902ac6454SAndrew Thompson 	 */
1840a593f6b8SAndrew Thompson 	if (usb_test_quirk(&uaa, UQ_CFG_INDEX_0)) {
184102ac6454SAndrew Thompson 		config_index = 0;
184202ac6454SAndrew Thompson 		config_quirk = 1;
1843a593f6b8SAndrew Thompson 	} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_1)) {
184402ac6454SAndrew Thompson 		config_index = 1;
184502ac6454SAndrew Thompson 		config_quirk = 1;
1846a593f6b8SAndrew Thompson 	} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_2)) {
184702ac6454SAndrew Thompson 		config_index = 2;
184802ac6454SAndrew Thompson 		config_quirk = 1;
1849a593f6b8SAndrew Thompson 	} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_3)) {
185002ac6454SAndrew Thompson 		config_index = 3;
185102ac6454SAndrew Thompson 		config_quirk = 1;
1852a593f6b8SAndrew Thompson 	} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_4)) {
185302ac6454SAndrew Thompson 		config_index = 4;
185402ac6454SAndrew Thompson 		config_quirk = 1;
185502ac6454SAndrew Thompson 	} else {
185602ac6454SAndrew Thompson 		config_index = 0;
185702ac6454SAndrew Thompson 		config_quirk = 0;
185802ac6454SAndrew Thompson 	}
185902ac6454SAndrew Thompson 
18602a4c6157SAndrew Thompson 	set_config_failed = 0;
186102ac6454SAndrew Thompson repeat_set_config:
186202ac6454SAndrew Thompson 
186302ac6454SAndrew Thompson 	DPRINTF("setting config %u\n", config_index);
186402ac6454SAndrew Thompson 
186502ac6454SAndrew Thompson 	/* get the USB device configured */
1866a593f6b8SAndrew Thompson 	err = usbd_set_config_index(udev, config_index);
186702ac6454SAndrew Thompson 	if (err) {
186802ac6454SAndrew Thompson 		if (udev->ddesc.bNumConfigurations != 0) {
186902ac6454SAndrew Thompson 			if (!set_config_failed) {
187002ac6454SAndrew Thompson 				set_config_failed = 1;
187102ac6454SAndrew Thompson 				/* XXX try to re-enumerate the device */
18722a4c6157SAndrew Thompson 				err = usbd_req_re_enumerate(udev, NULL);
187302ac6454SAndrew Thompson 				if (err == 0)
187402ac6454SAndrew Thompson 					goto repeat_set_config;
187502ac6454SAndrew Thompson 			}
18762a4c6157SAndrew Thompson 			DPRINTFN(0, "Failure selecting configuration index %u:"
18772a4c6157SAndrew Thompson 			    "%s, port %u, addr %u (ignored)\n",
1878a593f6b8SAndrew Thompson 			    config_index, usbd_errstr(err), udev->port_no,
187902ac6454SAndrew Thompson 			    udev->address);
188002ac6454SAndrew Thompson 		}
188102ac6454SAndrew Thompson 		/*
18822a4c6157SAndrew Thompson 		 * Some USB devices do not have any configurations. Ignore any
18832a4c6157SAndrew Thompson 		 * set config failures!
188402ac6454SAndrew Thompson 		 */
188502ac6454SAndrew Thompson 		err = 0;
18862a4c6157SAndrew Thompson 		goto config_done;
18872a4c6157SAndrew Thompson 	}
18882a4c6157SAndrew Thompson 	if (!config_quirk && config_index + 1 < udev->ddesc.bNumConfigurations) {
188902ac6454SAndrew Thompson 		if ((udev->cdesc->bNumInterface < 2) &&
18902a4c6157SAndrew Thompson 		    usbd_get_no_descriptors(udev->cdesc, UDESC_ENDPOINT) == 0) {
18912a4c6157SAndrew Thompson 			DPRINTFN(0, "Found no endpoints, trying next config\n");
189202ac6454SAndrew Thompson 			config_index++;
189302ac6454SAndrew Thompson 			goto repeat_set_config;
189402ac6454SAndrew Thompson 		}
189515d4edd3SHans Petter Selasky #if USB_HAVE_MSCTEST
189602ac6454SAndrew Thompson 		if (config_index == 0) {
189702ac6454SAndrew Thompson 			/*
189802ac6454SAndrew Thompson 			 * Try to figure out if we have an
189902ac6454SAndrew Thompson 			 * auto-install disk there:
190002ac6454SAndrew Thompson 			 */
1901d84a79e7SAndrew Thompson 			if (usb_iface_is_cdrom(udev, 0)) {
190202ac6454SAndrew Thompson 				DPRINTFN(0, "Found possible auto-install "
190302ac6454SAndrew Thompson 				    "disk (trying next config)\n");
190402ac6454SAndrew Thompson 				config_index++;
190502ac6454SAndrew Thompson 				goto repeat_set_config;
190602ac6454SAndrew Thompson 			}
190702ac6454SAndrew Thompson 		}
190815d4edd3SHans Petter Selasky #endif
19092a4c6157SAndrew Thompson 	}
191015d4edd3SHans Petter Selasky #if USB_HAVE_MSCTEST
1911d46dc4adSHans Petter Selasky 	if (set_config_failed == 0 && config_index == 0 &&
19129157ad4bSHans Petter Selasky 	    usb_test_quirk(&uaa, UQ_MSC_NO_SYNC_CACHE) == 0 &&
19139157ad4bSHans Petter Selasky 	    usb_test_quirk(&uaa, UQ_MSC_NO_GETMAXLUN) == 0) {
1914d46dc4adSHans Petter Selasky 
1915d46dc4adSHans Petter Selasky 		/*
1916d46dc4adSHans Petter Selasky 		 * Try to figure out if there are any MSC quirks we
1917d46dc4adSHans Petter Selasky 		 * should apply automatically:
1918d46dc4adSHans Petter Selasky 		 */
1919d46dc4adSHans Petter Selasky 		err = usb_msc_auto_quirk(udev, 0);
1920d46dc4adSHans Petter Selasky 
1921d46dc4adSHans Petter Selasky 		if (err != 0) {
1922d46dc4adSHans Petter Selasky 			set_config_failed = 1;
1923d46dc4adSHans Petter Selasky 			goto repeat_set_config;
1924d46dc4adSHans Petter Selasky 		}
1925d46dc4adSHans Petter Selasky 	}
192615d4edd3SHans Petter Selasky #endif
192702ac6454SAndrew Thompson 
19282a4c6157SAndrew Thompson config_done:
192902ac6454SAndrew Thompson 	DPRINTF("new dev (addr %d), udev=%p, parent_hub=%p\n",
193002ac6454SAndrew Thompson 	    udev->address, udev, udev->parent_hub);
193102ac6454SAndrew Thompson 
193202ac6454SAndrew Thompson 	/* register our device - we are ready */
1933a593f6b8SAndrew Thompson 	usb_bus_port_set_device(bus, parent_hub ?
193402ac6454SAndrew Thompson 	    parent_hub->hub->ports + port_index : NULL, udev, device_index);
193502ac6454SAndrew Thompson 
19360f6e8f93SAndrew Thompson #if USB_HAVE_UGEN
19378755859aSAndrew Thompson 	/* Symlink the ugen device name */
1938a593f6b8SAndrew Thompson 	udev->ugen_symlink = usb_alloc_symlink(udev->ugen_name);
19398755859aSAndrew Thompson 
19408755859aSAndrew Thompson 	/* Announce device */
1941ae538d85SAndrew Thompson 	printf("%s: <%s> at %s\n", udev->ugen_name,
1942ae538d85SAndrew Thompson 	    usb_get_manufacturer(udev),
194302ac6454SAndrew Thompson 	    device_get_nameunit(udev->bus->bdev));
19448bb77249SHans Petter Selasky #endif
19457efaaa9aSAndrew Thompson 
19468bb77249SHans Petter Selasky #if USB_HAVE_DEVCTL
19473acd904dSAndrew Thompson 	usb_notify_addq("ATTACH", udev);
19488755859aSAndrew Thompson #endif
194902ac6454SAndrew Thompson done:
195002ac6454SAndrew Thompson 	if (err) {
1951d88688c7SAndrew Thompson 		/*
1952d88688c7SAndrew Thompson 		 * Free USB device and all subdevices, if any.
1953d88688c7SAndrew Thompson 		 */
1954d88688c7SAndrew Thompson 		usb_free_device(udev, 0);
195502ac6454SAndrew Thompson 		udev = NULL;
195602ac6454SAndrew Thompson 	}
195702ac6454SAndrew Thompson 	return (udev);
195802ac6454SAndrew Thompson }
195902ac6454SAndrew Thompson 
19600f6e8f93SAndrew Thompson #if USB_HAVE_UGEN
19612ffd5fdcSHans Petter Selasky struct usb_fs_privdata *
19622ffd5fdcSHans Petter Selasky usb_make_dev(struct usb_device *udev, const char *devname, int ep,
19632ffd5fdcSHans Petter Selasky     int fi, int rwmode, uid_t uid, gid_t gid, int mode)
1964ee3e3ff5SAndrew Thompson {
1965760bc48eSAndrew Thompson 	struct usb_fs_privdata* pd;
1966ac3490fdSHans Petter Selasky 	struct make_dev_args args;
19672ffd5fdcSHans Petter Selasky 	char buffer[32];
1968ee3e3ff5SAndrew Thompson 
1969ee3e3ff5SAndrew Thompson 	/* Store information to locate ourselves again later */
1970760bc48eSAndrew Thompson 	pd = malloc(sizeof(struct usb_fs_privdata), M_USBDEV,
1971ee3e3ff5SAndrew Thompson 	    M_WAITOK | M_ZERO);
1972ee3e3ff5SAndrew Thompson 	pd->bus_index = device_get_unit(udev->bus->bdev);
1973ee3e3ff5SAndrew Thompson 	pd->dev_index = udev->device_index;
1974ee3e3ff5SAndrew Thompson 	pd->ep_addr = ep;
19752ffd5fdcSHans Petter Selasky 	pd->fifo_index = fi;
19762ffd5fdcSHans Petter Selasky 	pd->mode = rwmode;
1977ee3e3ff5SAndrew Thompson 
1978ee3e3ff5SAndrew Thompson 	/* Now, create the device itself */
19792ffd5fdcSHans Petter Selasky 	if (devname == NULL) {
19802ffd5fdcSHans Petter Selasky 		devname = buffer;
19812ffd5fdcSHans Petter Selasky 		snprintf(buffer, sizeof(buffer), USB_DEVICE_DIR "/%u.%u.%u",
1982f35aaff0SAndrew Thompson 		    pd->bus_index, pd->dev_index, pd->ep_addr);
19832ffd5fdcSHans Petter Selasky 	}
19842ffd5fdcSHans Petter Selasky 
1985ac3490fdSHans Petter Selasky 	/* Setup arguments for make_dev_s() */
1986ac3490fdSHans Petter Selasky 	make_dev_args_init(&args);
1987ac3490fdSHans Petter Selasky 	args.mda_devsw = &usb_devsw;
1988ac3490fdSHans Petter Selasky 	args.mda_uid = uid;
1989ac3490fdSHans Petter Selasky 	args.mda_gid = gid;
1990ac3490fdSHans Petter Selasky 	args.mda_mode = mode;
1991ac3490fdSHans Petter Selasky 	args.mda_si_drv1 = pd;
19922ffd5fdcSHans Petter Selasky 
1993ac3490fdSHans Petter Selasky 	if (make_dev_s(&args, &pd->cdev, "%s", devname) != 0) {
19942ffd5fdcSHans Petter Selasky 		DPRINTFN(0, "Failed to create device %s\n", devname);
19952ffd5fdcSHans Petter Selasky 		free(pd, M_USBDEV);
19962ffd5fdcSHans Petter Selasky 		return (NULL);
19972ffd5fdcSHans Petter Selasky 	}
19982ffd5fdcSHans Petter Selasky 	return (pd);
19992ffd5fdcSHans Petter Selasky }
20002ffd5fdcSHans Petter Selasky 
20012ffd5fdcSHans Petter Selasky void
2002b78e84d1SHans Petter Selasky usb_destroy_dev_sync(struct usb_fs_privdata *pd)
20032ffd5fdcSHans Petter Selasky {
2004b78e84d1SHans Petter Selasky 	DPRINTFN(1, "Destroying device at ugen%d.%d\n",
2005b78e84d1SHans Petter Selasky 	    pd->bus_index, pd->dev_index);
20062ffd5fdcSHans Petter Selasky 
2007b78e84d1SHans Petter Selasky 	/*
2008b78e84d1SHans Petter Selasky 	 * Destroy character device synchronously. After this
2009b78e84d1SHans Petter Selasky 	 * all system calls are returned. Can block.
2010b78e84d1SHans Petter Selasky 	 */
20112ffd5fdcSHans Petter Selasky 	destroy_dev(pd->cdev);
20122ffd5fdcSHans Petter Selasky 
20132ffd5fdcSHans Petter Selasky 	free(pd, M_USBDEV);
2014ee3e3ff5SAndrew Thompson }
2015ee3e3ff5SAndrew Thompson 
2016b78e84d1SHans Petter Selasky void
2017b78e84d1SHans Petter Selasky usb_destroy_dev(struct usb_fs_privdata *pd)
2018b78e84d1SHans Petter Selasky {
2019b78e84d1SHans Petter Selasky 	struct usb_bus *bus;
2020b78e84d1SHans Petter Selasky 
2021b78e84d1SHans Petter Selasky 	if (pd == NULL)
2022b78e84d1SHans Petter Selasky 		return;
2023b78e84d1SHans Petter Selasky 
2024b78e84d1SHans Petter Selasky 	mtx_lock(&usb_ref_lock);
2025b78e84d1SHans Petter Selasky 	bus = devclass_get_softc(usb_devclass_ptr, pd->bus_index);
2026b78e84d1SHans Petter Selasky 	mtx_unlock(&usb_ref_lock);
2027b78e84d1SHans Petter Selasky 
2028b78e84d1SHans Petter Selasky 	if (bus == NULL) {
2029b78e84d1SHans Petter Selasky 		usb_destroy_dev_sync(pd);
2030b78e84d1SHans Petter Selasky 		return;
2031b78e84d1SHans Petter Selasky 	}
2032b78e84d1SHans Petter Selasky 
203307dbde67SHans Petter Selasky 	/* make sure we can re-use the device name */
203407dbde67SHans Petter Selasky 	delist_dev(pd->cdev);
203507dbde67SHans Petter Selasky 
2036b78e84d1SHans Petter Selasky 	USB_BUS_LOCK(bus);
2037b78e84d1SHans Petter Selasky 	LIST_INSERT_HEAD(&bus->pd_cleanup_list, pd, pd_next);
2038b78e84d1SHans Petter Selasky 	/* get cleanup going */
2039b78e84d1SHans Petter Selasky 	usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
2040b78e84d1SHans Petter Selasky 	    &bus->cleanup_msg[0], &bus->cleanup_msg[1]);
2041b78e84d1SHans Petter Selasky 	USB_BUS_UNLOCK(bus);
2042b78e84d1SHans Petter Selasky }
2043b78e84d1SHans Petter Selasky 
2044ee3e3ff5SAndrew Thompson static void
2045a593f6b8SAndrew Thompson usb_cdev_create(struct usb_device *udev)
2046ee3e3ff5SAndrew Thompson {
2047760bc48eSAndrew Thompson 	struct usb_config_descriptor *cd;
2048760bc48eSAndrew Thompson 	struct usb_endpoint_descriptor *ed;
2049760bc48eSAndrew Thompson 	struct usb_descriptor *desc;
2050760bc48eSAndrew Thompson 	struct usb_fs_privdata* pd;
2051f5f145baSAndrew Thompson 	int inmode, outmode, inmask, outmask, mode;
2052f5f145baSAndrew Thompson 	uint8_t ep;
2053ee3e3ff5SAndrew Thompson 
2054ee3e3ff5SAndrew Thompson 	KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("stale cdev entries"));
2055ee3e3ff5SAndrew Thompson 
2056ee3e3ff5SAndrew Thompson 	DPRINTFN(2, "Creating device nodes\n");
2057ee3e3ff5SAndrew Thompson 
2058a593f6b8SAndrew Thompson 	if (usbd_get_mode(udev) == USB_MODE_DEVICE) {
2059ee3e3ff5SAndrew Thompson 		inmode = FWRITE;
2060ee3e3ff5SAndrew Thompson 		outmode = FREAD;
2061ee3e3ff5SAndrew Thompson 	} else {		 /* USB_MODE_HOST */
2062ee3e3ff5SAndrew Thompson 		inmode = FREAD;
2063ee3e3ff5SAndrew Thompson 		outmode = FWRITE;
2064ee3e3ff5SAndrew Thompson 	}
2065ee3e3ff5SAndrew Thompson 
2066f5f145baSAndrew Thompson 	inmask = 0;
2067f5f145baSAndrew Thompson 	outmask = 0;
2068f5f145baSAndrew Thompson 	desc = NULL;
2069f5f145baSAndrew Thompson 
2070f5f145baSAndrew Thompson 	/*
2071f5f145baSAndrew Thompson 	 * Collect all used endpoint numbers instead of just
2072f5f145baSAndrew Thompson 	 * generating 16 static endpoints.
2073f5f145baSAndrew Thompson 	 */
2074a593f6b8SAndrew Thompson 	cd = usbd_get_config_descriptor(udev);
2075a593f6b8SAndrew Thompson 	while ((desc = usb_desc_foreach(cd, desc))) {
2076f5f145baSAndrew Thompson 		/* filter out all endpoint descriptors */
2077f5f145baSAndrew Thompson 		if ((desc->bDescriptorType == UDESC_ENDPOINT) &&
2078f5f145baSAndrew Thompson 		    (desc->bLength >= sizeof(*ed))) {
2079760bc48eSAndrew Thompson 			ed = (struct usb_endpoint_descriptor *)desc;
2080f5f145baSAndrew Thompson 
2081f5f145baSAndrew Thompson 			/* update masks */
2082f5f145baSAndrew Thompson 			ep = ed->bEndpointAddress;
2083f5f145baSAndrew Thompson 			if (UE_GET_DIR(ep)  == UE_DIR_OUT)
2084f5f145baSAndrew Thompson 				outmask |= 1 << UE_GET_ADDR(ep);
2085f5f145baSAndrew Thompson 			else
2086f5f145baSAndrew Thompson 				inmask |= 1 << UE_GET_ADDR(ep);
2087f5f145baSAndrew Thompson 		}
2088f5f145baSAndrew Thompson 	}
2089ee3e3ff5SAndrew Thompson 
2090ee3e3ff5SAndrew Thompson 	/* Create all available endpoints except EP0 */
2091ee3e3ff5SAndrew Thompson 	for (ep = 1; ep < 16; ep++) {
20922ffd5fdcSHans Petter Selasky 		mode = (inmask & (1 << ep)) ? inmode : 0;
20932ffd5fdcSHans Petter Selasky 		mode |= (outmask & (1 << ep)) ? outmode : 0;
2094ee3e3ff5SAndrew Thompson 		if (mode == 0)
2095ee3e3ff5SAndrew Thompson 			continue;	/* no IN or OUT endpoint */
2096ee3e3ff5SAndrew Thompson 
20972ffd5fdcSHans Petter Selasky 		pd = usb_make_dev(udev, NULL, ep, 0,
20982ffd5fdcSHans Petter Selasky 		    mode, UID_ROOT, GID_OPERATOR, 0600);
20992ffd5fdcSHans Petter Selasky 
21002ffd5fdcSHans Petter Selasky 		if (pd != NULL)
2101ee3e3ff5SAndrew Thompson 			LIST_INSERT_HEAD(&udev->pd_list, pd, pd_next);
2102ee3e3ff5SAndrew Thompson 	}
2103ee3e3ff5SAndrew Thompson }
2104ee3e3ff5SAndrew Thompson 
2105ee3e3ff5SAndrew Thompson static void
2106a593f6b8SAndrew Thompson usb_cdev_free(struct usb_device *udev)
2107ee3e3ff5SAndrew Thompson {
2108760bc48eSAndrew Thompson 	struct usb_fs_privdata* pd;
2109ee3e3ff5SAndrew Thompson 
2110ee3e3ff5SAndrew Thompson 	DPRINTFN(2, "Freeing device nodes\n");
2111ee3e3ff5SAndrew Thompson 
2112ee3e3ff5SAndrew Thompson 	while ((pd = LIST_FIRST(&udev->pd_list)) != NULL) {
2113ee3e3ff5SAndrew Thompson 		KASSERT(pd->cdev->si_drv1 == pd, ("privdata corrupt"));
2114ee3e3ff5SAndrew Thompson 
2115ee3e3ff5SAndrew Thompson 		LIST_REMOVE(pd, pd_next);
2116ee3e3ff5SAndrew Thompson 
21172ffd5fdcSHans Petter Selasky 		usb_destroy_dev(pd);
21182ffd5fdcSHans Petter Selasky 	}
2119ee3e3ff5SAndrew Thompson }
21200f6e8f93SAndrew Thompson #endif
2121ee3e3ff5SAndrew Thompson 
212202ac6454SAndrew Thompson /*------------------------------------------------------------------------*
2123a593f6b8SAndrew Thompson  *	usb_free_device
212402ac6454SAndrew Thompson  *
2125d88688c7SAndrew Thompson  * This function is NULL safe and will free an USB device and its
2126d88688c7SAndrew Thompson  * children devices, if any.
2127bdd41206SAndrew Thompson  *
2128d88688c7SAndrew Thompson  * Flag values: Reserved, set to zero.
212902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
213002ac6454SAndrew Thompson void
2131a593f6b8SAndrew Thompson usb_free_device(struct usb_device *udev, uint8_t flag)
213202ac6454SAndrew Thompson {
2133760bc48eSAndrew Thompson 	struct usb_bus *bus;
2134bdd41206SAndrew Thompson 
2135bdd41206SAndrew Thompson 	if (udev == NULL)
2136bdd41206SAndrew Thompson 		return;		/* already freed */
213702ac6454SAndrew Thompson 
213802ac6454SAndrew Thompson 	DPRINTFN(4, "udev=%p port=%d\n", udev, udev->port_no);
213902ac6454SAndrew Thompson 
2140bd216778SAndrew Thompson 	bus = udev->bus;
2141f97da128SHans Petter Selasky 
2142f97da128SHans Petter Selasky 	/* set DETACHED state to prevent any further references */
2143a593f6b8SAndrew Thompson 	usb_set_device_state(udev, USB_STATE_DETACHED);
2144bdd41206SAndrew Thompson 
21458bb77249SHans Petter Selasky #if USB_HAVE_DEVCTL
21463acd904dSAndrew Thompson 	usb_notify_addq("DETACH", udev);
21478bb77249SHans Petter Selasky #endif
214802ac6454SAndrew Thompson 
21498bb77249SHans Petter Selasky #if USB_HAVE_UGEN
2150cabe79d9SMarius Strobl 	if (!rebooting) {
2151ee3e3ff5SAndrew Thompson 		printf("%s: <%s> at %s (disconnected)\n", udev->ugen_name,
2152ae538d85SAndrew Thompson 		    usb_get_manufacturer(udev), device_get_nameunit(bus->bdev));
2153cabe79d9SMarius Strobl 	}
215402ac6454SAndrew Thompson 
2155ee3e3ff5SAndrew Thompson 	/* Destroy UGEN symlink, if any */
215602ac6454SAndrew Thompson 	if (udev->ugen_symlink) {
2157a593f6b8SAndrew Thompson 		usb_free_symlink(udev->ugen_symlink);
215802ac6454SAndrew Thompson 		udev->ugen_symlink = NULL;
215902ac6454SAndrew Thompson 	}
216002ac6454SAndrew Thompson 
21612ffd5fdcSHans Petter Selasky 	usb_destroy_dev(udev->ctrl_dev);
21620f6e8f93SAndrew Thompson #endif
216302ac6454SAndrew Thompson 
2164f29a0724SAndrew Thompson 	if (udev->flags.usb_mode == USB_MODE_DEVICE) {
216502ac6454SAndrew Thompson 		/* stop receiving any control transfers (Device Side Mode) */
21665b3bb704SAndrew Thompson 		usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
216702ac6454SAndrew Thompson 	}
216802ac6454SAndrew Thompson 
2169bdd41206SAndrew Thompson 	/* the following will get the device unconfigured in software */
2170d88688c7SAndrew Thompson 	usb_unconfigure(udev, USB_UNCFG_FLAG_FREE_EP0);
217102ac6454SAndrew Thompson 
2172f97da128SHans Petter Selasky 	/* final device unregister after all character devices are closed */
2173f97da128SHans Petter Selasky 	usb_bus_port_set_device(bus, udev->parent_hub ?
2174f97da128SHans Petter Selasky 	    udev->parent_hub->hub->ports + udev->port_index : NULL,
2175f97da128SHans Petter Selasky 	    NULL, USB_ROOT_HUB_ADDR);
2176f97da128SHans Petter Selasky 
217702ac6454SAndrew Thompson 	/* unsetup any leftover default USB transfers */
21785b3bb704SAndrew Thompson 	usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
217902ac6454SAndrew Thompson 
218002ac6454SAndrew Thompson 	/* template unsetup, if any */
2181a593f6b8SAndrew Thompson 	(usb_temp_unsetup_p) (udev);
218202ac6454SAndrew Thompson 
218302ac6454SAndrew Thompson 	/*
218402ac6454SAndrew Thompson 	 * Make sure that our clear-stall messages are not queued
218502ac6454SAndrew Thompson 	 * anywhere:
218602ac6454SAndrew Thompson 	 */
218702ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
218843ea03d7SHans Petter Selasky 	usb_proc_mwait(USB_BUS_CS_PROC(udev->bus),
218902ac6454SAndrew Thompson 	    &udev->cs_msg[0], &udev->cs_msg[1]);
219002ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
219102ac6454SAndrew Thompson 
2192b78e84d1SHans Petter Selasky 	/* wait for all references to go away */
2193b78e84d1SHans Petter Selasky 	usb_wait_pending_refs(udev);
2194b78e84d1SHans Petter Selasky 
219591cd9240SAndrew Thompson 	sx_destroy(&udev->enum_sx);
21962df1e9a6SAndrew Thompson 	sx_destroy(&udev->sr_sx);
219764cb5e2aSHans Petter Selasky 	sx_destroy(&udev->ctrl_sx);
219802ac6454SAndrew Thompson 
219991cd9240SAndrew Thompson 	cv_destroy(&udev->ctrlreq_cv);
220091cd9240SAndrew Thompson 	cv_destroy(&udev->ref_cv);
220102ac6454SAndrew Thompson 
220291cd9240SAndrew Thompson 	mtx_destroy(&udev->device_mtx);
22038755859aSAndrew Thompson #if USB_HAVE_UGEN
2204ee3e3ff5SAndrew Thompson 	KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("leaked cdev entries"));
22058755859aSAndrew Thompson #endif
220602ac6454SAndrew Thompson 
2207963169b4SHans Petter Selasky 	/* Uninitialise device */
2208963169b4SHans Petter Selasky 	if (bus->methods->device_uninit != NULL)
2209963169b4SHans Petter Selasky 		(bus->methods->device_uninit) (udev);
2210963169b4SHans Petter Selasky 
221102ac6454SAndrew Thompson 	/* free device */
221228212de2SAndrew Thompson 	free(udev->serial, M_USB);
221328212de2SAndrew Thompson 	free(udev->manufacturer, M_USB);
221428212de2SAndrew Thompson 	free(udev->product, M_USB);
221502ac6454SAndrew Thompson 	free(udev, M_USB);
221602ac6454SAndrew Thompson }
221702ac6454SAndrew Thompson 
221802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
2219a593f6b8SAndrew Thompson  *	usbd_get_iface
222002ac6454SAndrew Thompson  *
222102ac6454SAndrew Thompson  * This function is the safe way to get the USB interface structure
222202ac6454SAndrew Thompson  * pointer by interface index.
222302ac6454SAndrew Thompson  *
222402ac6454SAndrew Thompson  * Return values:
222502ac6454SAndrew Thompson  *   NULL: Interface not present.
222602ac6454SAndrew Thompson  *   Else: Pointer to USB interface structure.
222702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
2228760bc48eSAndrew Thompson struct usb_interface *
2229a593f6b8SAndrew Thompson usbd_get_iface(struct usb_device *udev, uint8_t iface_index)
223002ac6454SAndrew Thompson {
2231760bc48eSAndrew Thompson 	struct usb_interface *iface = udev->ifaces + iface_index;
223202ac6454SAndrew Thompson 
2233bdd41206SAndrew Thompson 	if (iface_index >= udev->ifaces_max)
223402ac6454SAndrew Thompson 		return (NULL);
223502ac6454SAndrew Thompson 	return (iface);
223602ac6454SAndrew Thompson }
223702ac6454SAndrew Thompson 
223802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
22393c8d24f4SAndrew Thompson  *	usbd_find_descriptor
224002ac6454SAndrew Thompson  *
224102ac6454SAndrew Thompson  * This function will lookup the first descriptor that matches the
224202ac6454SAndrew Thompson  * criteria given by the arguments "type" and "subtype". Descriptors
224302ac6454SAndrew Thompson  * will only be searched within the interface having the index
224402ac6454SAndrew Thompson  * "iface_index".  If the "id" argument points to an USB descriptor,
224502ac6454SAndrew Thompson  * it will be skipped before the search is started. This allows
224602ac6454SAndrew Thompson  * searching for multiple descriptors using the same criteria. Else
224702ac6454SAndrew Thompson  * the search is started after the interface descriptor.
224802ac6454SAndrew Thompson  *
224902ac6454SAndrew Thompson  * Return values:
225002ac6454SAndrew Thompson  *   NULL: End of descriptors
225102ac6454SAndrew Thompson  *   Else: A descriptor matching the criteria
225202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
225302ac6454SAndrew Thompson void   *
22543c8d24f4SAndrew Thompson usbd_find_descriptor(struct usb_device *udev, void *id, uint8_t iface_index,
225502ac6454SAndrew Thompson     uint8_t type, uint8_t type_mask,
225602ac6454SAndrew Thompson     uint8_t subtype, uint8_t subtype_mask)
225702ac6454SAndrew Thompson {
2258760bc48eSAndrew Thompson 	struct usb_descriptor *desc;
2259760bc48eSAndrew Thompson 	struct usb_config_descriptor *cd;
2260760bc48eSAndrew Thompson 	struct usb_interface *iface;
226102ac6454SAndrew Thompson 
2262a593f6b8SAndrew Thompson 	cd = usbd_get_config_descriptor(udev);
226302ac6454SAndrew Thompson 	if (cd == NULL) {
226402ac6454SAndrew Thompson 		return (NULL);
226502ac6454SAndrew Thompson 	}
226602ac6454SAndrew Thompson 	if (id == NULL) {
2267a593f6b8SAndrew Thompson 		iface = usbd_get_iface(udev, iface_index);
226802ac6454SAndrew Thompson 		if (iface == NULL) {
226902ac6454SAndrew Thompson 			return (NULL);
227002ac6454SAndrew Thompson 		}
2271a593f6b8SAndrew Thompson 		id = usbd_get_interface_descriptor(iface);
227202ac6454SAndrew Thompson 		if (id == NULL) {
227302ac6454SAndrew Thompson 			return (NULL);
227402ac6454SAndrew Thompson 		}
227502ac6454SAndrew Thompson 	}
227602ac6454SAndrew Thompson 	desc = (void *)id;
227702ac6454SAndrew Thompson 
2278a593f6b8SAndrew Thompson 	while ((desc = usb_desc_foreach(cd, desc))) {
227902ac6454SAndrew Thompson 
228002ac6454SAndrew Thompson 		if (desc->bDescriptorType == UDESC_INTERFACE) {
228102ac6454SAndrew Thompson 			break;
228202ac6454SAndrew Thompson 		}
228302ac6454SAndrew Thompson 		if (((desc->bDescriptorType & type_mask) == type) &&
228402ac6454SAndrew Thompson 		    ((desc->bDescriptorSubtype & subtype_mask) == subtype)) {
228502ac6454SAndrew Thompson 			return (desc);
228602ac6454SAndrew Thompson 		}
228702ac6454SAndrew Thompson 	}
228802ac6454SAndrew Thompson 	return (NULL);
228902ac6454SAndrew Thompson }
229002ac6454SAndrew Thompson 
229102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
2292a593f6b8SAndrew Thompson  *	usb_devinfo
229302ac6454SAndrew Thompson  *
229402ac6454SAndrew Thompson  * This function will dump information from the device descriptor
229502ac6454SAndrew Thompson  * belonging to the USB device pointed to by "udev", to the string
229602ac6454SAndrew Thompson  * pointed to by "dst_ptr" having a maximum length of "dst_len" bytes
229702ac6454SAndrew Thompson  * including the terminating zero.
229802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
229902ac6454SAndrew Thompson void
2300a593f6b8SAndrew Thompson usb_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len)
230102ac6454SAndrew Thompson {
2302760bc48eSAndrew Thompson 	struct usb_device_descriptor *udd = &udev->ddesc;
230302ac6454SAndrew Thompson 	uint16_t bcdDevice;
230402ac6454SAndrew Thompson 	uint16_t bcdUSB;
230502ac6454SAndrew Thompson 
230602ac6454SAndrew Thompson 	bcdUSB = UGETW(udd->bcdUSB);
230702ac6454SAndrew Thompson 	bcdDevice = UGETW(udd->bcdDevice);
230802ac6454SAndrew Thompson 
230902ac6454SAndrew Thompson 	if (udd->bDeviceClass != 0xFF) {
231002ac6454SAndrew Thompson 		snprintf(dst_ptr, dst_len, "%s %s, class %d/%d, rev %x.%02x/"
23118755859aSAndrew Thompson 		    "%x.%02x, addr %d",
2312ae538d85SAndrew Thompson 		    usb_get_manufacturer(udev),
2313ae538d85SAndrew Thompson 		    usb_get_product(udev),
231402ac6454SAndrew Thompson 		    udd->bDeviceClass, udd->bDeviceSubClass,
231502ac6454SAndrew Thompson 		    (bcdUSB >> 8), bcdUSB & 0xFF,
231602ac6454SAndrew Thompson 		    (bcdDevice >> 8), bcdDevice & 0xFF,
231702ac6454SAndrew Thompson 		    udev->address);
231802ac6454SAndrew Thompson 	} else {
231902ac6454SAndrew Thompson 		snprintf(dst_ptr, dst_len, "%s %s, rev %x.%02x/"
23208755859aSAndrew Thompson 		    "%x.%02x, addr %d",
2321ae538d85SAndrew Thompson 		    usb_get_manufacturer(udev),
2322ae538d85SAndrew Thompson 		    usb_get_product(udev),
232302ac6454SAndrew Thompson 		    (bcdUSB >> 8), bcdUSB & 0xFF,
232402ac6454SAndrew Thompson 		    (bcdDevice >> 8), bcdDevice & 0xFF,
232502ac6454SAndrew Thompson 		    udev->address);
232602ac6454SAndrew Thompson 	}
232702ac6454SAndrew Thompson }
232802ac6454SAndrew Thompson 
2329ed6d949aSAndrew Thompson #ifdef USB_VERBOSE
233002ac6454SAndrew Thompson /*
233102ac6454SAndrew Thompson  * Descriptions of of known vendors and devices ("products").
233202ac6454SAndrew Thompson  */
233302ac6454SAndrew Thompson struct usb_knowndev {
233402ac6454SAndrew Thompson 	uint16_t vendor;
233502ac6454SAndrew Thompson 	uint16_t product;
233602ac6454SAndrew Thompson 	uint32_t flags;
233702ac6454SAndrew Thompson 	const char *vendorname;
233802ac6454SAndrew Thompson 	const char *productname;
233902ac6454SAndrew Thompson };
234002ac6454SAndrew Thompson 
234102ac6454SAndrew Thompson #define	USB_KNOWNDEV_NOPROD	0x01	/* match on vendor only */
234202ac6454SAndrew Thompson 
234302ac6454SAndrew Thompson #include "usbdevs.h"
234402ac6454SAndrew Thompson #include "usbdevs_data.h"
234502ac6454SAndrew Thompson #endif					/* USB_VERBOSE */
234602ac6454SAndrew Thompson 
234702ac6454SAndrew Thompson static void
2348a593f6b8SAndrew Thompson usbd_set_device_strings(struct usb_device *udev)
234902ac6454SAndrew Thompson {
2350760bc48eSAndrew Thompson 	struct usb_device_descriptor *udd = &udev->ddesc;
2351ed6d949aSAndrew Thompson #ifdef USB_VERBOSE
235202ac6454SAndrew Thompson 	const struct usb_knowndev *kdp;
235302ac6454SAndrew Thompson #endif
23542df1e9a6SAndrew Thompson 	char *temp_ptr;
23557a918edfSAndrew Thompson 	size_t temp_size;
235602ac6454SAndrew Thompson 	uint16_t vendor_id;
235702ac6454SAndrew Thompson 	uint16_t product_id;
23586950c75fSHans Petter Selasky 	uint8_t do_unlock;
235902ac6454SAndrew Thompson 
23606950c75fSHans Petter Selasky 	/* Protect scratch area */
236164cb5e2aSHans Petter Selasky 	do_unlock = usbd_ctrl_lock(udev);
23626950c75fSHans Petter Selasky 
23636950c75fSHans Petter Selasky 	temp_ptr = (char *)udev->scratch.data;
23646950c75fSHans Petter Selasky 	temp_size = sizeof(udev->scratch.data);
23657a918edfSAndrew Thompson 
236602ac6454SAndrew Thompson 	vendor_id = UGETW(udd->idVendor);
236702ac6454SAndrew Thompson 	product_id = UGETW(udd->idProduct);
236802ac6454SAndrew Thompson 
2369c86aead6SAndrew Thompson 	/* get serial number string */
23707a918edfSAndrew Thompson 	usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2371c86aead6SAndrew Thompson 	    udev->ddesc.iSerialNumber);
23727a918edfSAndrew Thompson 	udev->serial = strdup(temp_ptr, M_USB);
237302ac6454SAndrew Thompson 
2374c86aead6SAndrew Thompson 	/* get manufacturer string */
23757a918edfSAndrew Thompson 	usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2376c86aead6SAndrew Thompson 	    udev->ddesc.iManufacturer);
23777a918edfSAndrew Thompson 	usb_trim_spaces(temp_ptr);
23787a918edfSAndrew Thompson 	if (temp_ptr[0] != '\0')
23797a918edfSAndrew Thompson 		udev->manufacturer = strdup(temp_ptr, M_USB);
2380c86aead6SAndrew Thompson 
2381c86aead6SAndrew Thompson 	/* get product string */
23827a918edfSAndrew Thompson 	usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2383c86aead6SAndrew Thompson 	    udev->ddesc.iProduct);
23847a918edfSAndrew Thompson 	usb_trim_spaces(temp_ptr);
23857a918edfSAndrew Thompson 	if (temp_ptr[0] != '\0')
23867a918edfSAndrew Thompson 		udev->product = strdup(temp_ptr, M_USB);
2387c86aead6SAndrew Thompson 
2388ed6d949aSAndrew Thompson #ifdef USB_VERBOSE
2389c86aead6SAndrew Thompson 	if (udev->manufacturer == NULL || udev->product == NULL) {
2390c86aead6SAndrew Thompson 		for (kdp = usb_knowndevs; kdp->vendorname != NULL; kdp++) {
239102ac6454SAndrew Thompson 			if (kdp->vendor == vendor_id &&
239202ac6454SAndrew Thompson 			    (kdp->product == product_id ||
239302ac6454SAndrew Thompson 			    (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
239402ac6454SAndrew Thompson 				break;
239502ac6454SAndrew Thompson 		}
239602ac6454SAndrew Thompson 		if (kdp->vendorname != NULL) {
2397c86aead6SAndrew Thompson 			/* XXX should use pointer to knowndevs string */
2398c86aead6SAndrew Thompson 			if (udev->manufacturer == NULL) {
2399c86aead6SAndrew Thompson 				udev->manufacturer = strdup(kdp->vendorname,
2400c86aead6SAndrew Thompson 				    M_USB);
2401c86aead6SAndrew Thompson 			}
2402c86aead6SAndrew Thompson 			if (udev->product == NULL &&
2403c86aead6SAndrew Thompson 			    (kdp->flags & USB_KNOWNDEV_NOPROD) == 0) {
2404c86aead6SAndrew Thompson 				udev->product = strdup(kdp->productname,
2405c86aead6SAndrew Thompson 				    M_USB);
2406c86aead6SAndrew Thompson 			}
240702ac6454SAndrew Thompson 		}
240802ac6454SAndrew Thompson 	}
240902ac6454SAndrew Thompson #endif
2410c86aead6SAndrew Thompson 	/* Provide default strings if none were found */
2411c86aead6SAndrew Thompson 	if (udev->manufacturer == NULL) {
24127a918edfSAndrew Thompson 		snprintf(temp_ptr, temp_size, "vendor 0x%04x", vendor_id);
24137a918edfSAndrew Thompson 		udev->manufacturer = strdup(temp_ptr, M_USB);
241402ac6454SAndrew Thompson 	}
2415c86aead6SAndrew Thompson 	if (udev->product == NULL) {
24167a918edfSAndrew Thompson 		snprintf(temp_ptr, temp_size, "product 0x%04x", product_id);
24177a918edfSAndrew Thompson 		udev->product = strdup(temp_ptr, M_USB);
241802ac6454SAndrew Thompson 	}
24196950c75fSHans Petter Selasky 
24206950c75fSHans Petter Selasky 	if (do_unlock)
242164cb5e2aSHans Petter Selasky 		usbd_ctrl_unlock(udev);
242202ac6454SAndrew Thompson }
242302ac6454SAndrew Thompson 
242402ac6454SAndrew Thompson /*
242502ac6454SAndrew Thompson  * Returns:
242602ac6454SAndrew Thompson  * See: USB_MODE_XXX
242702ac6454SAndrew Thompson  */
2428f29a0724SAndrew Thompson enum usb_hc_mode
2429a593f6b8SAndrew Thompson usbd_get_mode(struct usb_device *udev)
243002ac6454SAndrew Thompson {
2431f29a0724SAndrew Thompson 	return (udev->flags.usb_mode);
243202ac6454SAndrew Thompson }
243302ac6454SAndrew Thompson 
243402ac6454SAndrew Thompson /*
243502ac6454SAndrew Thompson  * Returns:
243602ac6454SAndrew Thompson  * See: USB_SPEED_XXX
243702ac6454SAndrew Thompson  */
24388d2dd5ddSAndrew Thompson enum usb_dev_speed
2439a593f6b8SAndrew Thompson usbd_get_speed(struct usb_device *udev)
244002ac6454SAndrew Thompson {
244102ac6454SAndrew Thompson 	return (udev->speed);
244202ac6454SAndrew Thompson }
244302ac6454SAndrew Thompson 
244402ac6454SAndrew Thompson uint32_t
2445a593f6b8SAndrew Thompson usbd_get_isoc_fps(struct usb_device *udev)
244602ac6454SAndrew Thompson {
244702ac6454SAndrew Thompson 	;				/* indent fix */
244802ac6454SAndrew Thompson 	switch (udev->speed) {
244902ac6454SAndrew Thompson 	case USB_SPEED_LOW:
245002ac6454SAndrew Thompson 	case USB_SPEED_FULL:
245102ac6454SAndrew Thompson 		return (1000);
245202ac6454SAndrew Thompson 	default:
245302ac6454SAndrew Thompson 		return (8000);
245402ac6454SAndrew Thompson 	}
245502ac6454SAndrew Thompson }
245602ac6454SAndrew Thompson 
2457760bc48eSAndrew Thompson struct usb_device_descriptor *
2458a593f6b8SAndrew Thompson usbd_get_device_descriptor(struct usb_device *udev)
245902ac6454SAndrew Thompson {
246002ac6454SAndrew Thompson 	if (udev == NULL)
246102ac6454SAndrew Thompson 		return (NULL);		/* be NULL safe */
246202ac6454SAndrew Thompson 	return (&udev->ddesc);
246302ac6454SAndrew Thompson }
246402ac6454SAndrew Thompson 
2465760bc48eSAndrew Thompson struct usb_config_descriptor *
2466a593f6b8SAndrew Thompson usbd_get_config_descriptor(struct usb_device *udev)
246702ac6454SAndrew Thompson {
246802ac6454SAndrew Thompson 	if (udev == NULL)
246902ac6454SAndrew Thompson 		return (NULL);		/* be NULL safe */
247002ac6454SAndrew Thompson 	return (udev->cdesc);
247102ac6454SAndrew Thompson }
247202ac6454SAndrew Thompson 
247302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
2474a593f6b8SAndrew Thompson  *	usb_test_quirk - test a device for a given quirk
247502ac6454SAndrew Thompson  *
247602ac6454SAndrew Thompson  * Return values:
247702ac6454SAndrew Thompson  * 0: The USB device does not have the given quirk.
247802ac6454SAndrew Thompson  * Else: The USB device has the given quirk.
247902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
248002ac6454SAndrew Thompson uint8_t
2481a593f6b8SAndrew Thompson usb_test_quirk(const struct usb_attach_arg *uaa, uint16_t quirk)
248202ac6454SAndrew Thompson {
248302ac6454SAndrew Thompson 	uint8_t found;
24843f37fb62SHans Petter Selasky 	uint8_t x;
24853f37fb62SHans Petter Selasky 
24863f37fb62SHans Petter Selasky 	if (quirk == UQ_NONE)
24873f37fb62SHans Petter Selasky 		return (0);
24883f37fb62SHans Petter Selasky 
24893f37fb62SHans Petter Selasky 	/* search the automatic per device quirks first */
24903f37fb62SHans Petter Selasky 
24913f37fb62SHans Petter Selasky 	for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) {
24923f37fb62SHans Petter Selasky 		if (uaa->device->autoQuirk[x] == quirk)
24933f37fb62SHans Petter Selasky 			return (1);
24943f37fb62SHans Petter Selasky 	}
24953f37fb62SHans Petter Selasky 
24963f37fb62SHans Petter Selasky 	/* search global quirk table, if any */
249702ac6454SAndrew Thompson 
2498a593f6b8SAndrew Thompson 	found = (usb_test_quirk_p) (&uaa->info, quirk);
24993f37fb62SHans Petter Selasky 
250002ac6454SAndrew Thompson 	return (found);
250102ac6454SAndrew Thompson }
250202ac6454SAndrew Thompson 
2503760bc48eSAndrew Thompson struct usb_interface_descriptor *
2504a593f6b8SAndrew Thompson usbd_get_interface_descriptor(struct usb_interface *iface)
250502ac6454SAndrew Thompson {
250602ac6454SAndrew Thompson 	if (iface == NULL)
250702ac6454SAndrew Thompson 		return (NULL);		/* be NULL safe */
250802ac6454SAndrew Thompson 	return (iface->idesc);
250902ac6454SAndrew Thompson }
251002ac6454SAndrew Thompson 
251102ac6454SAndrew Thompson uint8_t
2512a593f6b8SAndrew Thompson usbd_get_interface_altindex(struct usb_interface *iface)
251302ac6454SAndrew Thompson {
251402ac6454SAndrew Thompson 	return (iface->alt_index);
251502ac6454SAndrew Thompson }
251602ac6454SAndrew Thompson 
251702ac6454SAndrew Thompson uint8_t
2518a593f6b8SAndrew Thompson usbd_get_bus_index(struct usb_device *udev)
251902ac6454SAndrew Thompson {
252002ac6454SAndrew Thompson 	return ((uint8_t)device_get_unit(udev->bus->bdev));
252102ac6454SAndrew Thompson }
252202ac6454SAndrew Thompson 
252302ac6454SAndrew Thompson uint8_t
2524a593f6b8SAndrew Thompson usbd_get_device_index(struct usb_device *udev)
252502ac6454SAndrew Thompson {
252602ac6454SAndrew Thompson 	return (udev->device_index);
252702ac6454SAndrew Thompson }
252802ac6454SAndrew Thompson 
25298bb77249SHans Petter Selasky #if USB_HAVE_DEVCTL
25303acd904dSAndrew Thompson static void
25313acd904dSAndrew Thompson usb_notify_addq(const char *type, struct usb_device *udev)
25323acd904dSAndrew Thompson {
25333acd904dSAndrew Thompson 	struct usb_interface *iface;
25343acd904dSAndrew Thompson 	struct sbuf *sb;
25353acd904dSAndrew Thompson 	int i;
25363acd904dSAndrew Thompson 
25373acd904dSAndrew Thompson 	/* announce the device */
25383acd904dSAndrew Thompson 	sb = sbuf_new_auto();
25393acd904dSAndrew Thompson 	sbuf_printf(sb,
25408bb77249SHans Petter Selasky #if USB_HAVE_UGEN
25418bb77249SHans Petter Selasky 	    "ugen=%s "
254202c40241SHans Petter Selasky 	    "cdev=%s "
25438bb77249SHans Petter Selasky #endif
25443acd904dSAndrew Thompson 	    "vendor=0x%04x "
25453acd904dSAndrew Thompson 	    "product=0x%04x "
25463acd904dSAndrew Thompson 	    "devclass=0x%02x "
25473acd904dSAndrew Thompson 	    "devsubclass=0x%02x "
25483acd904dSAndrew Thompson 	    "sernum=\"%s\" "
25493acd904dSAndrew Thompson 	    "release=0x%04x "
255066390216SAndrew Thompson 	    "mode=%s "
25513acd904dSAndrew Thompson 	    "port=%u "
25528bb77249SHans Petter Selasky #if USB_HAVE_UGEN
25536df3eebbSNick Hibma 	    "parent=%s"
25548bb77249SHans Petter Selasky #endif
25558bb77249SHans Petter Selasky 	    "",
25568bb77249SHans Petter Selasky #if USB_HAVE_UGEN
25573acd904dSAndrew Thompson 	    udev->ugen_name,
255802c40241SHans Petter Selasky 	    udev->ugen_name,
25598bb77249SHans Petter Selasky #endif
25603acd904dSAndrew Thompson 	    UGETW(udev->ddesc.idVendor),
25613acd904dSAndrew Thompson 	    UGETW(udev->ddesc.idProduct),
25623acd904dSAndrew Thompson 	    udev->ddesc.bDeviceClass,
25633acd904dSAndrew Thompson 	    udev->ddesc.bDeviceSubClass,
2564ae538d85SAndrew Thompson 	    usb_get_serial(udev),
25653acd904dSAndrew Thompson 	    UGETW(udev->ddesc.bcdDevice),
256666390216SAndrew Thompson 	    (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
25678bb77249SHans Petter Selasky 	    udev->port_no
25688bb77249SHans Petter Selasky #if USB_HAVE_UGEN
25698bb77249SHans Petter Selasky 	    , udev->parent_hub != NULL ?
25703acd904dSAndrew Thompson 		udev->parent_hub->ugen_name :
25718bb77249SHans Petter Selasky 		device_get_nameunit(device_get_parent(udev->bus->bdev))
25728bb77249SHans Petter Selasky #endif
25738bb77249SHans Petter Selasky 	    );
25743acd904dSAndrew Thompson 	sbuf_finish(sb);
25753acd904dSAndrew Thompson 	devctl_notify("USB", "DEVICE", type, sbuf_data(sb));
25763acd904dSAndrew Thompson 	sbuf_delete(sb);
25773acd904dSAndrew Thompson 
25783acd904dSAndrew Thompson 	/* announce each interface */
25793acd904dSAndrew Thompson 	for (i = 0; i < USB_IFACE_MAX; i++) {
25803acd904dSAndrew Thompson 		iface = usbd_get_iface(udev, i);
25813acd904dSAndrew Thompson 		if (iface == NULL)
25823acd904dSAndrew Thompson 			break;		/* end of interfaces */
25833acd904dSAndrew Thompson 		if (iface->idesc == NULL)
25843acd904dSAndrew Thompson 			continue;	/* no interface descriptor */
25853acd904dSAndrew Thompson 
25863acd904dSAndrew Thompson 		sb = sbuf_new_auto();
25873acd904dSAndrew Thompson 		sbuf_printf(sb,
25888bb77249SHans Petter Selasky #if USB_HAVE_UGEN
25898bb77249SHans Petter Selasky 		    "ugen=%s "
259002c40241SHans Petter Selasky 		    "cdev=%s "
25918bb77249SHans Petter Selasky #endif
25923acd904dSAndrew Thompson 		    "vendor=0x%04x "
25933acd904dSAndrew Thompson 		    "product=0x%04x "
25943acd904dSAndrew Thompson 		    "devclass=0x%02x "
25953acd904dSAndrew Thompson 		    "devsubclass=0x%02x "
25963acd904dSAndrew Thompson 		    "sernum=\"%s\" "
25973acd904dSAndrew Thompson 		    "release=0x%04x "
259866390216SAndrew Thompson 		    "mode=%s "
25993acd904dSAndrew Thompson 		    "interface=%d "
26003acd904dSAndrew Thompson 		    "endpoints=%d "
26013acd904dSAndrew Thompson 		    "intclass=0x%02x "
26023acd904dSAndrew Thompson 		    "intsubclass=0x%02x "
26036df3eebbSNick Hibma 		    "intprotocol=0x%02x",
26048bb77249SHans Petter Selasky #if USB_HAVE_UGEN
26053acd904dSAndrew Thompson 		    udev->ugen_name,
260602c40241SHans Petter Selasky 		    udev->ugen_name,
26078bb77249SHans Petter Selasky #endif
26083acd904dSAndrew Thompson 		    UGETW(udev->ddesc.idVendor),
26093acd904dSAndrew Thompson 		    UGETW(udev->ddesc.idProduct),
26103acd904dSAndrew Thompson 		    udev->ddesc.bDeviceClass,
26113acd904dSAndrew Thompson 		    udev->ddesc.bDeviceSubClass,
2612ae538d85SAndrew Thompson 		    usb_get_serial(udev),
26133acd904dSAndrew Thompson 		    UGETW(udev->ddesc.bcdDevice),
261466390216SAndrew Thompson 		    (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
26153acd904dSAndrew Thompson 		    iface->idesc->bInterfaceNumber,
26163acd904dSAndrew Thompson 		    iface->idesc->bNumEndpoints,
26173acd904dSAndrew Thompson 		    iface->idesc->bInterfaceClass,
26183acd904dSAndrew Thompson 		    iface->idesc->bInterfaceSubClass,
26193acd904dSAndrew Thompson 		    iface->idesc->bInterfaceProtocol);
26203acd904dSAndrew Thompson 		sbuf_finish(sb);
26213acd904dSAndrew Thompson 		devctl_notify("USB", "INTERFACE", type, sbuf_data(sb));
26223acd904dSAndrew Thompson 		sbuf_delete(sb);
26233acd904dSAndrew Thompson 	}
26243acd904dSAndrew Thompson }
26258bb77249SHans Petter Selasky #endif
262602ac6454SAndrew Thompson 
26278bb77249SHans Petter Selasky #if USB_HAVE_UGEN
262802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
2629a593f6b8SAndrew Thompson  *	usb_fifo_free_wrap
263002ac6454SAndrew Thompson  *
263102ac6454SAndrew Thompson  * This function will free the FIFOs.
263202ac6454SAndrew Thompson  *
2633bdd41206SAndrew Thompson  * Description of "flag" argument: If the USB_UNCFG_FLAG_FREE_EP0 flag
2634bdd41206SAndrew Thompson  * is set and "iface_index" is set to "USB_IFACE_INDEX_ANY", we free
2635bdd41206SAndrew Thompson  * all FIFOs. If the USB_UNCFG_FLAG_FREE_EP0 flag is not set and
2636bdd41206SAndrew Thompson  * "iface_index" is set to "USB_IFACE_INDEX_ANY", we free all non
2637bdd41206SAndrew Thompson  * control endpoint FIFOs. If "iface_index" is not set to
2638bdd41206SAndrew Thompson  * "USB_IFACE_INDEX_ANY" the flag has no effect.
263902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
264002ac6454SAndrew Thompson static void
2641a593f6b8SAndrew Thompson usb_fifo_free_wrap(struct usb_device *udev,
264202ac6454SAndrew Thompson     uint8_t iface_index, uint8_t flag)
264302ac6454SAndrew Thompson {
2644760bc48eSAndrew Thompson 	struct usb_fifo *f;
264502ac6454SAndrew Thompson 	uint16_t i;
264602ac6454SAndrew Thompson 
264702ac6454SAndrew Thompson 	/*
264802ac6454SAndrew Thompson 	 * Free any USB FIFOs on the given interface:
264902ac6454SAndrew Thompson 	 */
265002ac6454SAndrew Thompson 	for (i = 0; i != USB_FIFO_MAX; i++) {
265102ac6454SAndrew Thompson 		f = udev->fifo[i];
265202ac6454SAndrew Thompson 		if (f == NULL) {
265302ac6454SAndrew Thompson 			continue;
265402ac6454SAndrew Thompson 		}
265502ac6454SAndrew Thompson 		/* Check if the interface index matches */
265602ac6454SAndrew Thompson 		if (iface_index == f->iface_index) {
2657a593f6b8SAndrew Thompson 			if (f->methods != &usb_ugen_methods) {
265802ac6454SAndrew Thompson 				/*
265902ac6454SAndrew Thompson 				 * Don't free any non-generic FIFOs in
266002ac6454SAndrew Thompson 				 * this case.
266102ac6454SAndrew Thompson 				 */
266202ac6454SAndrew Thompson 				continue;
266302ac6454SAndrew Thompson 			}
266402ac6454SAndrew Thompson 			if ((f->dev_ep_index == 0) &&
266502ac6454SAndrew Thompson 			    (f->fs_xfer == NULL)) {
266602ac6454SAndrew Thompson 				/* no need to free this FIFO */
266702ac6454SAndrew Thompson 				continue;
266802ac6454SAndrew Thompson 			}
266902ac6454SAndrew Thompson 		} else if (iface_index == USB_IFACE_INDEX_ANY) {
2670a593f6b8SAndrew Thompson 			if ((f->methods == &usb_ugen_methods) &&
2671bdd41206SAndrew Thompson 			    (f->dev_ep_index == 0) &&
2672bdd41206SAndrew Thompson 			    (!(flag & USB_UNCFG_FLAG_FREE_EP0)) &&
267302ac6454SAndrew Thompson 			    (f->fs_xfer == NULL)) {
267402ac6454SAndrew Thompson 				/* no need to free this FIFO */
267502ac6454SAndrew Thompson 				continue;
267602ac6454SAndrew Thompson 			}
267702ac6454SAndrew Thompson 		} else {
267802ac6454SAndrew Thompson 			/* no need to free this FIFO */
267902ac6454SAndrew Thompson 			continue;
268002ac6454SAndrew Thompson 		}
268102ac6454SAndrew Thompson 		/* free this FIFO */
2682a593f6b8SAndrew Thompson 		usb_fifo_free(f);
268302ac6454SAndrew Thompson 	}
268402ac6454SAndrew Thompson }
26850f6e8f93SAndrew Thompson #endif
268602ac6454SAndrew Thompson 
268702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
2688a593f6b8SAndrew Thompson  *	usb_peer_can_wakeup
268902ac6454SAndrew Thompson  *
269002ac6454SAndrew Thompson  * Return values:
269102ac6454SAndrew Thompson  * 0: Peer cannot do resume signalling.
269202ac6454SAndrew Thompson  * Else: Peer can do resume signalling.
269302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
269402ac6454SAndrew Thompson uint8_t
2695a593f6b8SAndrew Thompson usb_peer_can_wakeup(struct usb_device *udev)
269602ac6454SAndrew Thompson {
2697760bc48eSAndrew Thompson 	const struct usb_config_descriptor *cdp;
269802ac6454SAndrew Thompson 
269902ac6454SAndrew Thompson 	cdp = udev->cdesc;
2700f29a0724SAndrew Thompson 	if ((cdp != NULL) && (udev->flags.usb_mode == USB_MODE_HOST)) {
270102ac6454SAndrew Thompson 		return (cdp->bmAttributes & UC_REMOTE_WAKEUP);
270202ac6454SAndrew Thompson 	}
270302ac6454SAndrew Thompson 	return (0);			/* not supported */
270402ac6454SAndrew Thompson }
2705bd216778SAndrew Thompson 
2706bd216778SAndrew Thompson void
2707a593f6b8SAndrew Thompson usb_set_device_state(struct usb_device *udev, enum usb_dev_state state)
2708bd216778SAndrew Thompson {
2709bd216778SAndrew Thompson 
2710bd216778SAndrew Thompson 	KASSERT(state < USB_STATE_MAX, ("invalid udev state"));
2711bd216778SAndrew Thompson 
2712bd216778SAndrew Thompson 	DPRINTF("udev %p state %s -> %s\n", udev,
2713a593f6b8SAndrew Thompson 	    usb_statestr(udev->state), usb_statestr(state));
2714963169b4SHans Petter Selasky 
2715f97da128SHans Petter Selasky #if USB_HAVE_UGEN
2716f97da128SHans Petter Selasky 	mtx_lock(&usb_ref_lock);
2717f97da128SHans Petter Selasky #endif
2718f97da128SHans Petter Selasky 	udev->state = state;
2719f97da128SHans Petter Selasky #if USB_HAVE_UGEN
2720f97da128SHans Petter Selasky 	mtx_unlock(&usb_ref_lock);
2721f97da128SHans Petter Selasky #endif
2722963169b4SHans Petter Selasky 	if (udev->bus->methods->device_state_change != NULL)
2723963169b4SHans Petter Selasky 		(udev->bus->methods->device_state_change) (udev);
2724963169b4SHans Petter Selasky }
2725963169b4SHans Petter Selasky 
2726963169b4SHans Petter Selasky enum usb_dev_state
2727963169b4SHans Petter Selasky usb_get_device_state(struct usb_device *udev)
2728963169b4SHans Petter Selasky {
2729963169b4SHans Petter Selasky 	if (udev == NULL)
2730963169b4SHans Petter Selasky 		return (USB_STATE_DETACHED);
2731963169b4SHans Petter Selasky 	return (udev->state);
2732bd216778SAndrew Thompson }
2733bd216778SAndrew Thompson 
2734ec8f3127SAndrew Thompson uint8_t
2735a593f6b8SAndrew Thompson usbd_device_attached(struct usb_device *udev)
2736bd216778SAndrew Thompson {
2737bd216778SAndrew Thompson 	return (udev->state > USB_STATE_DETACHED);
2738bd216778SAndrew Thompson }
2739cb18f7d1SAlfred Perlstein 
27406950c75fSHans Petter Selasky /*
27416950c75fSHans Petter Selasky  * The following function locks enumerating the given USB device. If
27426950c75fSHans Petter Selasky  * the lock is already grabbed this function returns zero. Else a
274310aab8b6SHans Petter Selasky  * a value of one is returned.
27446950c75fSHans Petter Selasky  */
27456950c75fSHans Petter Selasky uint8_t
2746cb18f7d1SAlfred Perlstein usbd_enum_lock(struct usb_device *udev)
2747cb18f7d1SAlfred Perlstein {
27486950c75fSHans Petter Selasky 	if (sx_xlocked(&udev->enum_sx))
27496950c75fSHans Petter Selasky 		return (0);
27506950c75fSHans Petter Selasky 
275191cd9240SAndrew Thompson 	sx_xlock(&udev->enum_sx);
27522df1e9a6SAndrew Thompson 	sx_xlock(&udev->sr_sx);
2753cb18f7d1SAlfred Perlstein 	/*
2754cb18f7d1SAlfred Perlstein 	 * NEWBUS LOCK NOTE: We should check if any parent SX locks
2755cb18f7d1SAlfred Perlstein 	 * are locked before locking Giant. Else the lock can be
2756cb18f7d1SAlfred Perlstein 	 * locked multiple times.
2757cb18f7d1SAlfred Perlstein 	 */
2758cb18f7d1SAlfred Perlstein 	mtx_lock(&Giant);
27596950c75fSHans Petter Selasky 	return (1);
2760cb18f7d1SAlfred Perlstein }
2761cb18f7d1SAlfred Perlstein 
276210aab8b6SHans Petter Selasky #if USB_HAVE_UGEN
276310aab8b6SHans Petter Selasky /*
276410aab8b6SHans Petter Selasky  * This function is the same like usbd_enum_lock() except a value of
276510aab8b6SHans Petter Selasky  * 255 is returned when a signal is pending:
276610aab8b6SHans Petter Selasky  */
276710aab8b6SHans Petter Selasky uint8_t
276810aab8b6SHans Petter Selasky usbd_enum_lock_sig(struct usb_device *udev)
276910aab8b6SHans Petter Selasky {
277010aab8b6SHans Petter Selasky 	if (sx_xlocked(&udev->enum_sx))
277110aab8b6SHans Petter Selasky 		return (0);
277210aab8b6SHans Petter Selasky 	if (sx_xlock_sig(&udev->enum_sx))
277310aab8b6SHans Petter Selasky 		return (255);
277410aab8b6SHans Petter Selasky 	if (sx_xlock_sig(&udev->sr_sx)) {
277510aab8b6SHans Petter Selasky 		sx_xunlock(&udev->enum_sx);
277610aab8b6SHans Petter Selasky 		return (255);
277710aab8b6SHans Petter Selasky 	}
277810aab8b6SHans Petter Selasky 	mtx_lock(&Giant);
277910aab8b6SHans Petter Selasky 	return (1);
278010aab8b6SHans Petter Selasky }
278110aab8b6SHans Petter Selasky #endif
278210aab8b6SHans Petter Selasky 
2783cb18f7d1SAlfred Perlstein /* The following function unlocks enumerating the given USB device. */
2784cb18f7d1SAlfred Perlstein 
2785cb18f7d1SAlfred Perlstein void
2786cb18f7d1SAlfred Perlstein usbd_enum_unlock(struct usb_device *udev)
2787cb18f7d1SAlfred Perlstein {
2788cb18f7d1SAlfred Perlstein 	mtx_unlock(&Giant);
278991cd9240SAndrew Thompson 	sx_xunlock(&udev->enum_sx);
27902df1e9a6SAndrew Thompson 	sx_xunlock(&udev->sr_sx);
27912df1e9a6SAndrew Thompson }
27922df1e9a6SAndrew Thompson 
27932df1e9a6SAndrew Thompson /* The following function locks suspend and resume. */
27942df1e9a6SAndrew Thompson 
27952df1e9a6SAndrew Thompson void
27962df1e9a6SAndrew Thompson usbd_sr_lock(struct usb_device *udev)
27972df1e9a6SAndrew Thompson {
27982df1e9a6SAndrew Thompson 	sx_xlock(&udev->sr_sx);
27992df1e9a6SAndrew Thompson 	/*
28002df1e9a6SAndrew Thompson 	 * NEWBUS LOCK NOTE: We should check if any parent SX locks
28012df1e9a6SAndrew Thompson 	 * are locked before locking Giant. Else the lock can be
28022df1e9a6SAndrew Thompson 	 * locked multiple times.
28032df1e9a6SAndrew Thompson 	 */
28042df1e9a6SAndrew Thompson 	mtx_lock(&Giant);
28052df1e9a6SAndrew Thompson }
28062df1e9a6SAndrew Thompson 
28072df1e9a6SAndrew Thompson /* The following function unlocks suspend and resume. */
28082df1e9a6SAndrew Thompson 
28092df1e9a6SAndrew Thompson void
28102df1e9a6SAndrew Thompson usbd_sr_unlock(struct usb_device *udev)
28112df1e9a6SAndrew Thompson {
28122df1e9a6SAndrew Thompson 	mtx_unlock(&Giant);
28132df1e9a6SAndrew Thompson 	sx_xunlock(&udev->sr_sx);
2814cb18f7d1SAlfred Perlstein }
2815cb18f7d1SAlfred Perlstein 
2816cb18f7d1SAlfred Perlstein /*
2817cb18f7d1SAlfred Perlstein  * The following function checks the enumerating lock for the given
2818cb18f7d1SAlfred Perlstein  * USB device.
2819cb18f7d1SAlfred Perlstein  */
2820cb18f7d1SAlfred Perlstein 
2821cb18f7d1SAlfred Perlstein uint8_t
2822cb18f7d1SAlfred Perlstein usbd_enum_is_locked(struct usb_device *udev)
2823cb18f7d1SAlfred Perlstein {
282491cd9240SAndrew Thompson 	return (sx_xlocked(&udev->enum_sx));
2825cb18f7d1SAlfred Perlstein }
28268427ed84SHans Petter Selasky 
28278427ed84SHans Petter Selasky /*
282864cb5e2aSHans Petter Selasky  * The following function is used to serialize access to USB control
282964cb5e2aSHans Petter Selasky  * transfers and the USB scratch area. If the lock is already grabbed
283064cb5e2aSHans Petter Selasky  * this function returns zero. Else a value of one is returned.
283164cb5e2aSHans Petter Selasky  */
283264cb5e2aSHans Petter Selasky uint8_t
283364cb5e2aSHans Petter Selasky usbd_ctrl_lock(struct usb_device *udev)
283464cb5e2aSHans Petter Selasky {
283564cb5e2aSHans Petter Selasky 	if (sx_xlocked(&udev->ctrl_sx))
283664cb5e2aSHans Petter Selasky 		return (0);
283764cb5e2aSHans Petter Selasky 	sx_xlock(&udev->ctrl_sx);
283864cb5e2aSHans Petter Selasky 
283964cb5e2aSHans Petter Selasky 	/*
284064cb5e2aSHans Petter Selasky 	 * We need to allow suspend and resume at this point, else the
284164cb5e2aSHans Petter Selasky 	 * control transfer will timeout if the device is suspended!
284264cb5e2aSHans Petter Selasky 	 */
284364cb5e2aSHans Petter Selasky 	if (usbd_enum_is_locked(udev))
284464cb5e2aSHans Petter Selasky 		usbd_sr_unlock(udev);
284564cb5e2aSHans Petter Selasky 	return (1);
284664cb5e2aSHans Petter Selasky }
284764cb5e2aSHans Petter Selasky 
284864cb5e2aSHans Petter Selasky void
284964cb5e2aSHans Petter Selasky usbd_ctrl_unlock(struct usb_device *udev)
285064cb5e2aSHans Petter Selasky {
285164cb5e2aSHans Petter Selasky 	sx_xunlock(&udev->ctrl_sx);
285264cb5e2aSHans Petter Selasky 
285364cb5e2aSHans Petter Selasky 	/*
285464cb5e2aSHans Petter Selasky 	 * Restore the suspend and resume lock after we have unlocked
285564cb5e2aSHans Petter Selasky 	 * the USB control transfer lock to avoid LOR:
285664cb5e2aSHans Petter Selasky 	 */
285764cb5e2aSHans Petter Selasky 	if (usbd_enum_is_locked(udev))
285864cb5e2aSHans Petter Selasky 		usbd_sr_lock(udev);
285964cb5e2aSHans Petter Selasky }
286064cb5e2aSHans Petter Selasky 
286164cb5e2aSHans Petter Selasky /*
28628427ed84SHans Petter Selasky  * The following function is used to set the per-interface specific
28638427ed84SHans Petter Selasky  * plug and play information. The string referred to by the pnpinfo
28648427ed84SHans Petter Selasky  * argument can safely be freed after calling this function. The
28658427ed84SHans Petter Selasky  * pnpinfo of an interface will be reset at device detach or when
28668427ed84SHans Petter Selasky  * passing a NULL argument to this function. This function
28678427ed84SHans Petter Selasky  * returns zero on success, else a USB_ERR_XXX failure code.
28688427ed84SHans Petter Selasky  */
28698427ed84SHans Petter Selasky 
28708427ed84SHans Petter Selasky usb_error_t
28718427ed84SHans Petter Selasky usbd_set_pnpinfo(struct usb_device *udev, uint8_t iface_index, const char *pnpinfo)
28728427ed84SHans Petter Selasky {
28738427ed84SHans Petter Selasky 	struct usb_interface *iface;
28748427ed84SHans Petter Selasky 
28758427ed84SHans Petter Selasky 	iface = usbd_get_iface(udev, iface_index);
28768427ed84SHans Petter Selasky 	if (iface == NULL)
28778427ed84SHans Petter Selasky 		return (USB_ERR_INVAL);
28788427ed84SHans Petter Selasky 
28798427ed84SHans Petter Selasky 	if (iface->pnpinfo != NULL) {
28808427ed84SHans Petter Selasky 		free(iface->pnpinfo, M_USBDEV);
28818427ed84SHans Petter Selasky 		iface->pnpinfo = NULL;
28828427ed84SHans Petter Selasky 	}
28838427ed84SHans Petter Selasky 
28848427ed84SHans Petter Selasky 	if (pnpinfo == NULL || pnpinfo[0] == 0)
28858427ed84SHans Petter Selasky 		return (0);		/* success */
28868427ed84SHans Petter Selasky 
28878427ed84SHans Petter Selasky 	iface->pnpinfo = strdup(pnpinfo, M_USBDEV);
28888427ed84SHans Petter Selasky 	if (iface->pnpinfo == NULL)
28898427ed84SHans Petter Selasky 		return (USB_ERR_NOMEM);
28908427ed84SHans Petter Selasky 
28918427ed84SHans Petter Selasky 	return (0);			/* success */
28928427ed84SHans Petter Selasky }
28938427ed84SHans Petter Selasky 
2894d46dc4adSHans Petter Selasky usb_error_t
2895d46dc4adSHans Petter Selasky usbd_add_dynamic_quirk(struct usb_device *udev, uint16_t quirk)
2896d46dc4adSHans Petter Selasky {
2897d46dc4adSHans Petter Selasky 	uint8_t x;
2898d46dc4adSHans Petter Selasky 
2899d46dc4adSHans Petter Selasky 	for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) {
29003f37fb62SHans Petter Selasky 		if (udev->autoQuirk[x] == 0 ||
29013f37fb62SHans Petter Selasky 		    udev->autoQuirk[x] == quirk) {
2902d46dc4adSHans Petter Selasky 			udev->autoQuirk[x] = quirk;
2903d46dc4adSHans Petter Selasky 			return (0);	/* success */
2904d46dc4adSHans Petter Selasky 		}
2905d46dc4adSHans Petter Selasky 	}
2906d46dc4adSHans Petter Selasky 	return (USB_ERR_NOMEM);
2907d46dc4adSHans Petter Selasky }
2908a5cf1aaaSHans Petter Selasky 
2909a5cf1aaaSHans Petter Selasky /*
2910a5cf1aaaSHans Petter Selasky  * The following function is used to select the endpoint mode. It
2911a5cf1aaaSHans Petter Selasky  * should not be called outside enumeration context.
2912a5cf1aaaSHans Petter Selasky  */
2913a5cf1aaaSHans Petter Selasky 
2914a5cf1aaaSHans Petter Selasky usb_error_t
2915a5cf1aaaSHans Petter Selasky usbd_set_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep,
2916a5cf1aaaSHans Petter Selasky     uint8_t ep_mode)
2917a5cf1aaaSHans Petter Selasky {
2918a5cf1aaaSHans Petter Selasky 	usb_error_t error;
2919f61fc4bdSHans Petter Selasky 	uint8_t do_unlock;
2920a5cf1aaaSHans Petter Selasky 
29216950c75fSHans Petter Selasky 	/* Prevent re-enumeration */
29226950c75fSHans Petter Selasky 	do_unlock = usbd_enum_lock(udev);
2923a5cf1aaaSHans Petter Selasky 
2924a5cf1aaaSHans Petter Selasky 	if (udev->bus->methods->set_endpoint_mode != NULL) {
2925a5cf1aaaSHans Petter Selasky 		error = (udev->bus->methods->set_endpoint_mode) (
2926a5cf1aaaSHans Petter Selasky 		    udev, ep, ep_mode);
2927a5cf1aaaSHans Petter Selasky 	} else if (ep_mode != USB_EP_MODE_DEFAULT) {
2928a5cf1aaaSHans Petter Selasky 		error = USB_ERR_INVAL;
2929a5cf1aaaSHans Petter Selasky 	} else {
2930a5cf1aaaSHans Petter Selasky 		error = 0;
2931a5cf1aaaSHans Petter Selasky 	}
2932a5cf1aaaSHans Petter Selasky 
2933a5cf1aaaSHans Petter Selasky 	/* only set new mode regardless of error */
2934a5cf1aaaSHans Petter Selasky 	ep->ep_mode = ep_mode;
2935a5cf1aaaSHans Petter Selasky 
2936f61fc4bdSHans Petter Selasky 	if (do_unlock)
2937f61fc4bdSHans Petter Selasky 		usbd_enum_unlock(udev);
2938a5cf1aaaSHans Petter Selasky 	return (error);
2939a5cf1aaaSHans Petter Selasky }
2940a5cf1aaaSHans Petter Selasky 
2941a5cf1aaaSHans Petter Selasky uint8_t
2942a5cf1aaaSHans Petter Selasky usbd_get_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep)
2943a5cf1aaaSHans Petter Selasky {
2944a5cf1aaaSHans Petter Selasky 	return (ep->ep_mode);
2945a5cf1aaaSHans Petter Selasky }
2946