xref: /freebsd/sys/dev/usb/usb_device.c (revision 4d4fa6edd40732c24765ce16bd376f26aaa076f1)
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 
2702ac6454SAndrew Thompson #include <dev/usb/usb_mfunc.h>
2802ac6454SAndrew Thompson #include <dev/usb/usb_error.h>
2902ac6454SAndrew Thompson #include <dev/usb/usb.h>
3002ac6454SAndrew Thompson #include <dev/usb/usb_ioctl.h>
3102ac6454SAndrew Thompson #include "usbdevs.h"
3202ac6454SAndrew Thompson 
3302ac6454SAndrew Thompson #define	USB_DEBUG_VAR usb2_debug
3402ac6454SAndrew Thompson 
3502ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
3602ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
3702ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
3802ac6454SAndrew Thompson #include <dev/usb/usb_device.h>
3902ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
4002ac6454SAndrew Thompson #include <dev/usb/usb_transfer.h>
4102ac6454SAndrew Thompson #include <dev/usb/usb_parse.h>
4202ac6454SAndrew Thompson #include <dev/usb/usb_request.h>
4302ac6454SAndrew Thompson #include <dev/usb/usb_dynamic.h>
4402ac6454SAndrew Thompson #include <dev/usb/usb_hub.h>
4502ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
4602ac6454SAndrew Thompson #include <dev/usb/usb_mbuf.h>
4702ac6454SAndrew Thompson #include <dev/usb/usb_msctest.h>
48bdc081c6SAndrew Thompson #if USB_HAVE_UGEN
49bdc081c6SAndrew Thompson #include <dev/usb/usb_dev.h>
5002ac6454SAndrew Thompson #include <dev/usb/usb_generic.h>
51bdc081c6SAndrew Thompson #endif
5202ac6454SAndrew Thompson 
5302ac6454SAndrew Thompson #include <dev/usb/quirk/usb_quirk.h>
5402ac6454SAndrew Thompson 
5502ac6454SAndrew Thompson #include <dev/usb/usb_controller.h>
5602ac6454SAndrew Thompson #include <dev/usb/usb_bus.h>
5702ac6454SAndrew Thompson 
5802ac6454SAndrew Thompson /* function prototypes  */
5902ac6454SAndrew Thompson 
60bdd41206SAndrew Thompson static void	usb2_init_pipe(struct usb2_device *, uint8_t,
6102ac6454SAndrew Thompson 		    struct usb2_endpoint_descriptor *, struct usb2_pipe *);
62bdd41206SAndrew Thompson static void	usb2_unconfigure(struct usb2_device *, uint8_t);
63bdd41206SAndrew Thompson static void	usb2_detach_device(struct usb2_device *, uint8_t, uint8_t);
6402ac6454SAndrew Thompson static void	usb2_detach_device_sub(struct usb2_device *, device_t *,
6502ac6454SAndrew Thompson 		    uint8_t);
6602ac6454SAndrew Thompson static uint8_t	usb2_probe_and_attach_sub(struct usb2_device *,
6702ac6454SAndrew Thompson 		    struct usb2_attach_arg *);
6802ac6454SAndrew Thompson static void	usb2_init_attach_arg(struct usb2_device *,
6902ac6454SAndrew Thompson 		    struct usb2_attach_arg *);
7002ac6454SAndrew Thompson static void	usb2_suspend_resume_sub(struct usb2_device *, device_t,
7102ac6454SAndrew Thompson 		    uint8_t);
7202ac6454SAndrew Thompson static void	usb2_clear_stall_proc(struct usb2_proc_msg *_pm);
73bdd41206SAndrew Thompson usb2_error_t	usb2_config_parse(struct usb2_device *, uint8_t, uint8_t);
74bdc081c6SAndrew Thompson #if USB_HAVE_STRINGS
7502ac6454SAndrew Thompson static void	usb2_check_strings(struct usb2_device *);
76bdc081c6SAndrew Thompson #endif
770f6e8f93SAndrew Thompson #if USB_HAVE_UGEN
788755859aSAndrew Thompson static void	usb2_notify_addq(const char *type, struct usb2_device *);
7902ac6454SAndrew Thompson static void	usb2_fifo_free_wrap(struct usb2_device *, uint8_t, uint8_t);
80f5f145baSAndrew Thompson static struct cdev *usb2_make_dev(struct usb2_device *, int, int);
81ee3e3ff5SAndrew Thompson static void	usb2_cdev_create(struct usb2_device *);
82ee3e3ff5SAndrew Thompson static void	usb2_cdev_free(struct usb2_device *);
83ee3e3ff5SAndrew Thompson static void	usb2_cdev_cleanup(void *);
840f6e8f93SAndrew Thompson #endif
8502ac6454SAndrew Thompson 
8602ac6454SAndrew Thompson /* This variable is global to allow easy access to it: */
8702ac6454SAndrew Thompson 
8802ac6454SAndrew Thompson int	usb2_template = 0;
8902ac6454SAndrew Thompson 
9002ac6454SAndrew Thompson SYSCTL_INT(_hw_usb2, OID_AUTO, template, CTLFLAG_RW,
9102ac6454SAndrew Thompson     &usb2_template, 0, "Selected USB device side template");
9202ac6454SAndrew Thompson 
934d4fa6edSAndrew Thompson static const char* statestr[USB_STATE_MAX] = {
944d4fa6edSAndrew Thompson 	[USB_STATE_DETACHED]	= "DETACHED",
954d4fa6edSAndrew Thompson 	[USB_STATE_ATTACHED]	= "ATTACHED",
964d4fa6edSAndrew Thompson 	[USB_STATE_POWERED]	= "POWERED",
974d4fa6edSAndrew Thompson 	[USB_STATE_ADDRESSED]	= "ADDRESSED",
984d4fa6edSAndrew Thompson 	[USB_STATE_CONFIGURED]	= "CONFIGURED",
994d4fa6edSAndrew Thompson 	[USB_STATE_SUSPENDED]	= "SUSPENDED"
1004d4fa6edSAndrew Thompson };
1014d4fa6edSAndrew Thompson 
1024d4fa6edSAndrew Thompson const char *
1034d4fa6edSAndrew Thompson usb2_statestr(enum usb_dev_state state)
1044d4fa6edSAndrew Thompson {
1054d4fa6edSAndrew Thompson 	KASSERT(state < USB_STATE_MAX, ("invalid udev state"));
1064d4fa6edSAndrew Thompson 
1074d4fa6edSAndrew Thompson 	return (statestr[state]);
1084d4fa6edSAndrew Thompson }
10902ac6454SAndrew Thompson 
11002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
11102ac6454SAndrew Thompson  *	usb2_get_pipe_by_addr
11202ac6454SAndrew Thompson  *
11302ac6454SAndrew Thompson  * This function searches for an USB pipe by endpoint address and
11402ac6454SAndrew Thompson  * direction.
11502ac6454SAndrew Thompson  *
11602ac6454SAndrew Thompson  * Returns:
11702ac6454SAndrew Thompson  * NULL: Failure
11802ac6454SAndrew Thompson  * Else: Success
11902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
12002ac6454SAndrew Thompson struct usb2_pipe *
12102ac6454SAndrew Thompson usb2_get_pipe_by_addr(struct usb2_device *udev, uint8_t ea_val)
12202ac6454SAndrew Thompson {
12302ac6454SAndrew Thompson 	struct usb2_pipe *pipe = udev->pipes;
12463521bbcSAndrew Thompson 	struct usb2_pipe *pipe_end = udev->pipes + udev->pipes_max;
12502ac6454SAndrew Thompson 	enum {
12602ac6454SAndrew Thompson 		EA_MASK = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR),
12702ac6454SAndrew Thompson 	};
12802ac6454SAndrew Thompson 
12902ac6454SAndrew Thompson 	/*
13002ac6454SAndrew Thompson 	 * According to the USB specification not all bits are used
13102ac6454SAndrew Thompson 	 * for the endpoint address. Keep defined bits only:
13202ac6454SAndrew Thompson 	 */
13302ac6454SAndrew Thompson 	ea_val &= EA_MASK;
13402ac6454SAndrew Thompson 
13502ac6454SAndrew Thompson 	/*
13602ac6454SAndrew Thompson 	 * Iterate accross all the USB pipes searching for a match
13702ac6454SAndrew Thompson 	 * based on the endpoint address:
13802ac6454SAndrew Thompson 	 */
13902ac6454SAndrew Thompson 	for (; pipe != pipe_end; pipe++) {
14002ac6454SAndrew Thompson 
14102ac6454SAndrew Thompson 		if (pipe->edesc == NULL) {
14202ac6454SAndrew Thompson 			continue;
14302ac6454SAndrew Thompson 		}
14402ac6454SAndrew Thompson 		/* do the mask and check the value */
14502ac6454SAndrew Thompson 		if ((pipe->edesc->bEndpointAddress & EA_MASK) == ea_val) {
14602ac6454SAndrew Thompson 			goto found;
14702ac6454SAndrew Thompson 		}
14802ac6454SAndrew Thompson 	}
14902ac6454SAndrew Thompson 
15002ac6454SAndrew Thompson 	/*
15102ac6454SAndrew Thompson 	 * The default pipe is always present and is checked separately:
15202ac6454SAndrew Thompson 	 */
15302ac6454SAndrew Thompson 	if ((udev->default_pipe.edesc) &&
15402ac6454SAndrew Thompson 	    ((udev->default_pipe.edesc->bEndpointAddress & EA_MASK) == ea_val)) {
15502ac6454SAndrew Thompson 		pipe = &udev->default_pipe;
15602ac6454SAndrew Thompson 		goto found;
15702ac6454SAndrew Thompson 	}
15802ac6454SAndrew Thompson 	return (NULL);
15902ac6454SAndrew Thompson 
16002ac6454SAndrew Thompson found:
16102ac6454SAndrew Thompson 	return (pipe);
16202ac6454SAndrew Thompson }
16302ac6454SAndrew Thompson 
16402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
16502ac6454SAndrew Thompson  *	usb2_get_pipe
16602ac6454SAndrew Thompson  *
16702ac6454SAndrew Thompson  * This function searches for an USB pipe based on the information
16802ac6454SAndrew Thompson  * given by the passed "struct usb2_config" pointer.
16902ac6454SAndrew Thompson  *
17002ac6454SAndrew Thompson  * Return values:
17102ac6454SAndrew Thompson  * NULL: No match.
17202ac6454SAndrew Thompson  * Else: Pointer to "struct usb2_pipe".
17302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
17402ac6454SAndrew Thompson struct usb2_pipe *
17502ac6454SAndrew Thompson usb2_get_pipe(struct usb2_device *udev, uint8_t iface_index,
17602ac6454SAndrew Thompson     const struct usb2_config *setup)
17702ac6454SAndrew Thompson {
17802ac6454SAndrew Thompson 	struct usb2_pipe *pipe = udev->pipes;
17963521bbcSAndrew Thompson 	struct usb2_pipe *pipe_end = udev->pipes + udev->pipes_max;
18002ac6454SAndrew Thompson 	uint8_t index = setup->ep_index;
18102ac6454SAndrew Thompson 	uint8_t ea_mask;
18202ac6454SAndrew Thompson 	uint8_t ea_val;
18302ac6454SAndrew Thompson 	uint8_t type_mask;
18402ac6454SAndrew Thompson 	uint8_t type_val;
18502ac6454SAndrew Thompson 
18602ac6454SAndrew Thompson 	DPRINTFN(10, "udev=%p iface_index=%d address=0x%x "
18702ac6454SAndrew Thompson 	    "type=0x%x dir=0x%x index=%d\n",
18802ac6454SAndrew Thompson 	    udev, iface_index, setup->endpoint,
18902ac6454SAndrew Thompson 	    setup->type, setup->direction, setup->ep_index);
19002ac6454SAndrew Thompson 
1914eae601eSAndrew Thompson 	/* check USB mode */
1924eae601eSAndrew Thompson 
1934eae601eSAndrew Thompson 	if ((setup->usb_mode != USB_MODE_MAX) &&
1944eae601eSAndrew Thompson 	    (udev->flags.usb2_mode != setup->usb_mode)) {
1954eae601eSAndrew Thompson 		/* wrong mode - no pipe */
1964eae601eSAndrew Thompson 		return (NULL);
1974eae601eSAndrew Thompson 	}
1984eae601eSAndrew Thompson 
19902ac6454SAndrew Thompson 	/* setup expected endpoint direction mask and value */
20002ac6454SAndrew Thompson 
20151ec1603SAndrew Thompson 	if (setup->direction == UE_DIR_RX) {
20251ec1603SAndrew Thompson 		ea_mask = (UE_DIR_IN | UE_DIR_OUT);
20351ec1603SAndrew Thompson 		ea_val = (udev->flags.usb2_mode == USB_MODE_DEVICE) ?
20451ec1603SAndrew Thompson 		    UE_DIR_OUT : UE_DIR_IN;
20551ec1603SAndrew Thompson 	} else if (setup->direction == UE_DIR_TX) {
20651ec1603SAndrew Thompson 		ea_mask = (UE_DIR_IN | UE_DIR_OUT);
20751ec1603SAndrew Thompson 		ea_val = (udev->flags.usb2_mode == USB_MODE_DEVICE) ?
20851ec1603SAndrew Thompson 		    UE_DIR_IN : UE_DIR_OUT;
20951ec1603SAndrew Thompson 	} else if (setup->direction == UE_DIR_ANY) {
21002ac6454SAndrew Thompson 		/* match any endpoint direction */
21102ac6454SAndrew Thompson 		ea_mask = 0;
21202ac6454SAndrew Thompson 		ea_val = 0;
21302ac6454SAndrew Thompson 	} else {
21402ac6454SAndrew Thompson 		/* match the given endpoint direction */
21502ac6454SAndrew Thompson 		ea_mask = (UE_DIR_IN | UE_DIR_OUT);
21602ac6454SAndrew Thompson 		ea_val = (setup->direction & (UE_DIR_IN | UE_DIR_OUT));
21702ac6454SAndrew Thompson 	}
21802ac6454SAndrew Thompson 
21902ac6454SAndrew Thompson 	/* setup expected endpoint address */
22002ac6454SAndrew Thompson 
22102ac6454SAndrew Thompson 	if (setup->endpoint == UE_ADDR_ANY) {
22202ac6454SAndrew Thompson 		/* match any endpoint address */
22302ac6454SAndrew Thompson 	} else {
22402ac6454SAndrew Thompson 		/* match the given endpoint address */
22502ac6454SAndrew Thompson 		ea_mask |= UE_ADDR;
22602ac6454SAndrew Thompson 		ea_val |= (setup->endpoint & UE_ADDR);
22702ac6454SAndrew Thompson 	}
22802ac6454SAndrew Thompson 
22902ac6454SAndrew Thompson 	/* setup expected endpoint type */
23002ac6454SAndrew Thompson 
23102ac6454SAndrew Thompson 	if (setup->type == UE_BULK_INTR) {
23202ac6454SAndrew Thompson 		/* this will match BULK and INTERRUPT endpoints */
23302ac6454SAndrew Thompson 		type_mask = 2;
23402ac6454SAndrew Thompson 		type_val = 2;
23502ac6454SAndrew Thompson 	} else if (setup->type == UE_TYPE_ANY) {
23602ac6454SAndrew Thompson 		/* match any endpoint type */
23702ac6454SAndrew Thompson 		type_mask = 0;
23802ac6454SAndrew Thompson 		type_val = 0;
23902ac6454SAndrew Thompson 	} else {
24002ac6454SAndrew Thompson 		/* match the given endpoint type */
24102ac6454SAndrew Thompson 		type_mask = UE_XFERTYPE;
24202ac6454SAndrew Thompson 		type_val = (setup->type & UE_XFERTYPE);
24302ac6454SAndrew Thompson 	}
24402ac6454SAndrew Thompson 
24502ac6454SAndrew Thompson 	/*
24602ac6454SAndrew Thompson 	 * Iterate accross all the USB pipes searching for a match
24702ac6454SAndrew Thompson 	 * based on the endpoint address. Note that we are searching
24802ac6454SAndrew Thompson 	 * the pipes from the beginning of the "udev->pipes" array.
24902ac6454SAndrew Thompson 	 */
25002ac6454SAndrew Thompson 	for (; pipe != pipe_end; pipe++) {
25102ac6454SAndrew Thompson 
25202ac6454SAndrew Thompson 		if ((pipe->edesc == NULL) ||
25302ac6454SAndrew Thompson 		    (pipe->iface_index != iface_index)) {
25402ac6454SAndrew Thompson 			continue;
25502ac6454SAndrew Thompson 		}
25602ac6454SAndrew Thompson 		/* do the masks and check the values */
25702ac6454SAndrew Thompson 
25802ac6454SAndrew Thompson 		if (((pipe->edesc->bEndpointAddress & ea_mask) == ea_val) &&
25902ac6454SAndrew Thompson 		    ((pipe->edesc->bmAttributes & type_mask) == type_val)) {
26002ac6454SAndrew Thompson 			if (!index--) {
26102ac6454SAndrew Thompson 				goto found;
26202ac6454SAndrew Thompson 			}
26302ac6454SAndrew Thompson 		}
26402ac6454SAndrew Thompson 	}
26502ac6454SAndrew Thompson 
26602ac6454SAndrew Thompson 	/*
26702ac6454SAndrew Thompson 	 * Match against default pipe last, so that "any pipe", "any
26802ac6454SAndrew Thompson 	 * address" and "any direction" returns the first pipe of the
26902ac6454SAndrew Thompson 	 * interface. "iface_index" and "direction" is ignored:
27002ac6454SAndrew Thompson 	 */
27102ac6454SAndrew Thompson 	if ((udev->default_pipe.edesc) &&
27202ac6454SAndrew Thompson 	    ((udev->default_pipe.edesc->bEndpointAddress & ea_mask) == ea_val) &&
27302ac6454SAndrew Thompson 	    ((udev->default_pipe.edesc->bmAttributes & type_mask) == type_val) &&
27402ac6454SAndrew Thompson 	    (!index)) {
27502ac6454SAndrew Thompson 		pipe = &udev->default_pipe;
27602ac6454SAndrew Thompson 		goto found;
27702ac6454SAndrew Thompson 	}
27802ac6454SAndrew Thompson 	return (NULL);
27902ac6454SAndrew Thompson 
28002ac6454SAndrew Thompson found:
28102ac6454SAndrew Thompson 	return (pipe);
28202ac6454SAndrew Thompson }
28302ac6454SAndrew Thompson 
28402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
28502ac6454SAndrew Thompson  *	usb2_interface_count
28602ac6454SAndrew Thompson  *
28702ac6454SAndrew Thompson  * This function stores the number of USB interfaces excluding
28802ac6454SAndrew Thompson  * alternate settings, which the USB config descriptor reports into
28902ac6454SAndrew Thompson  * the unsigned 8-bit integer pointed to by "count".
29002ac6454SAndrew Thompson  *
29102ac6454SAndrew Thompson  * Returns:
29202ac6454SAndrew Thompson  *    0: Success
29302ac6454SAndrew Thompson  * Else: Failure
29402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
29502ac6454SAndrew Thompson usb2_error_t
29602ac6454SAndrew Thompson usb2_interface_count(struct usb2_device *udev, uint8_t *count)
29702ac6454SAndrew Thompson {
29802ac6454SAndrew Thompson 	if (udev->cdesc == NULL) {
29902ac6454SAndrew Thompson 		*count = 0;
30002ac6454SAndrew Thompson 		return (USB_ERR_NOT_CONFIGURED);
30102ac6454SAndrew Thompson 	}
302bdd41206SAndrew Thompson 	*count = udev->ifaces_max;
30302ac6454SAndrew Thompson 	return (USB_ERR_NORMAL_COMPLETION);
30402ac6454SAndrew Thompson }
30502ac6454SAndrew Thompson 
30602ac6454SAndrew Thompson 
30702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
308bdd41206SAndrew Thompson  *	usb2_init_pipe
30902ac6454SAndrew Thompson  *
31002ac6454SAndrew Thompson  * This function will initialise the USB pipe structure pointed to by
311bdd41206SAndrew Thompson  * the "pipe" argument. The structure pointed to by "pipe" must be
312bdd41206SAndrew Thompson  * zeroed before calling this function.
31302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
31402ac6454SAndrew Thompson static void
315bdd41206SAndrew Thompson usb2_init_pipe(struct usb2_device *udev, uint8_t iface_index,
31602ac6454SAndrew Thompson     struct usb2_endpoint_descriptor *edesc, struct usb2_pipe *pipe)
31702ac6454SAndrew Thompson {
318bdd41206SAndrew Thompson 	struct usb2_bus_methods *methods;
319ee3e3ff5SAndrew Thompson 
320bdd41206SAndrew Thompson 	methods = udev->bus->methods;
32102ac6454SAndrew Thompson 
322bdd41206SAndrew Thompson 	(methods->pipe_init) (udev, edesc, pipe);
32302ac6454SAndrew Thompson 
32402ac6454SAndrew Thompson 	/* initialise USB pipe structure */
32502ac6454SAndrew Thompson 	pipe->edesc = edesc;
32602ac6454SAndrew Thompson 	pipe->iface_index = iface_index;
32702ac6454SAndrew Thompson 	TAILQ_INIT(&pipe->pipe_q.head);
32802ac6454SAndrew Thompson 	pipe->pipe_q.command = &usb2_pipe_start;
32902ac6454SAndrew Thompson 
33039307315SAndrew Thompson 	/* the pipe is not supported by the hardware */
33139307315SAndrew Thompson  	if (pipe->methods == NULL)
33239307315SAndrew Thompson 		return;
33339307315SAndrew Thompson 
33402ac6454SAndrew Thompson 	/* clear stall, if any */
335bdd41206SAndrew Thompson 	if (methods->clear_stall != NULL) {
33602ac6454SAndrew Thompson 		USB_BUS_LOCK(udev->bus);
337bdd41206SAndrew Thompson 		(methods->clear_stall) (udev, pipe);
33802ac6454SAndrew Thompson 		USB_BUS_UNLOCK(udev->bus);
33902ac6454SAndrew Thompson 	}
34002ac6454SAndrew Thompson }
34102ac6454SAndrew Thompson 
342bdd41206SAndrew Thompson /*-----------------------------------------------------------------------*
34316589beaSAndrew Thompson  *	usb2_pipe_foreach
34416589beaSAndrew Thompson  *
34516589beaSAndrew Thompson  * This function will iterate all the USB endpoints except the control
34616589beaSAndrew Thompson  * endpoint. This function is NULL safe.
34716589beaSAndrew Thompson  *
34816589beaSAndrew Thompson  * Return values:
34916589beaSAndrew Thompson  * NULL: End of USB pipes
35016589beaSAndrew Thompson  * Else: Pointer to next USB pipe
35116589beaSAndrew Thompson  *------------------------------------------------------------------------*/
35216589beaSAndrew Thompson struct usb2_pipe *
35316589beaSAndrew Thompson usb2_pipe_foreach(struct usb2_device *udev, struct usb2_pipe *pipe)
35416589beaSAndrew Thompson {
35563521bbcSAndrew Thompson 	struct usb2_pipe *pipe_end = udev->pipes + udev->pipes_max;
35616589beaSAndrew Thompson 
35716589beaSAndrew Thompson 	/* be NULL safe */
35816589beaSAndrew Thompson 	if (udev == NULL)
35916589beaSAndrew Thompson 		return (NULL);
36016589beaSAndrew Thompson 
36116589beaSAndrew Thompson 	/* get next pipe */
36216589beaSAndrew Thompson 	if (pipe == NULL)
36316589beaSAndrew Thompson 		pipe = udev->pipes;
36416589beaSAndrew Thompson 	else
36516589beaSAndrew Thompson 		pipe++;
36616589beaSAndrew Thompson 
36716589beaSAndrew Thompson 	/* find next allocated pipe */
36816589beaSAndrew Thompson 	while (pipe != pipe_end) {
36916589beaSAndrew Thompson 		if (pipe->edesc != NULL)
37016589beaSAndrew Thompson 			return (pipe);
37116589beaSAndrew Thompson 		pipe++;
37216589beaSAndrew Thompson 	}
37316589beaSAndrew Thompson 	return (NULL);
37416589beaSAndrew Thompson }
37516589beaSAndrew Thompson 
37616589beaSAndrew Thompson /*------------------------------------------------------------------------*
377bdd41206SAndrew Thompson  *	usb2_unconfigure
37802ac6454SAndrew Thompson  *
37902ac6454SAndrew Thompson  * This function will free all USB interfaces and USB pipes belonging
38002ac6454SAndrew Thompson  * to an USB device.
381bdd41206SAndrew Thompson  *
382bdd41206SAndrew Thompson  * Flag values, see "USB_UNCFG_FLAG_XXX".
38302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
38402ac6454SAndrew Thompson static void
385bdd41206SAndrew Thompson usb2_unconfigure(struct usb2_device *udev, uint8_t flag)
38602ac6454SAndrew Thompson {
387bdd41206SAndrew Thompson 	uint8_t do_unlock;
38802ac6454SAndrew Thompson 
389bdd41206SAndrew Thompson 	/* automatic locking */
390bdd41206SAndrew Thompson 	if (sx_xlocked(udev->default_sx + 1)) {
391bdd41206SAndrew Thompson 		do_unlock = 0;
392bdd41206SAndrew Thompson 	} else {
393bdd41206SAndrew Thompson 		do_unlock = 1;
394bdd41206SAndrew Thompson 		sx_xlock(udev->default_sx + 1);
395bdd41206SAndrew Thompson 	}
396bdd41206SAndrew Thompson 
397bdd41206SAndrew Thompson 	/* detach all interface drivers */
398bdd41206SAndrew Thompson 	usb2_detach_device(udev, USB_IFACE_INDEX_ANY, flag);
399bdd41206SAndrew Thompson 
400bdd41206SAndrew Thompson #if USB_HAVE_UGEN
401bdd41206SAndrew Thompson 	/* free all FIFOs except control endpoint FIFOs */
402bdd41206SAndrew Thompson 	usb2_fifo_free_wrap(udev, USB_IFACE_INDEX_ANY, flag);
403bdd41206SAndrew Thompson 
404bdd41206SAndrew Thompson 	/*
405bdd41206SAndrew Thompson 	 * Free all cdev's, if any.
406bdd41206SAndrew Thompson 	 */
407bdd41206SAndrew Thompson 	usb2_cdev_free(udev);
408bdd41206SAndrew Thompson #endif
40902ac6454SAndrew Thompson 
410bdc081c6SAndrew Thompson #if USB_HAVE_COMPAT_LINUX
41102ac6454SAndrew Thompson 	/* free Linux compat device, if any */
41202ac6454SAndrew Thompson 	if (udev->linux_dev) {
41302ac6454SAndrew Thompson 		usb_linux_free_device(udev->linux_dev);
41402ac6454SAndrew Thompson 		udev->linux_dev = NULL;
41502ac6454SAndrew Thompson 	}
416bdc081c6SAndrew Thompson #endif
41702ac6454SAndrew Thompson 
418bdd41206SAndrew Thompson 	usb2_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_FREE);
41902ac6454SAndrew Thompson 
420bdd41206SAndrew Thompson 	/* free "cdesc" after "ifaces" and "pipes", if any */
4217efaaa9aSAndrew Thompson 	if (udev->cdesc != NULL) {
4227efaaa9aSAndrew Thompson 		if (udev->flags.usb2_mode != USB_MODE_DEVICE)
42302ac6454SAndrew Thompson 			free(udev->cdesc, M_USB);
42402ac6454SAndrew Thompson 		udev->cdesc = NULL;
42502ac6454SAndrew Thompson 	}
42602ac6454SAndrew Thompson 	/* set unconfigured state */
42702ac6454SAndrew Thompson 	udev->curr_config_no = USB_UNCONFIG_NO;
42802ac6454SAndrew Thompson 	udev->curr_config_index = USB_UNCONFIG_INDEX;
429bdd41206SAndrew Thompson 
430bdd41206SAndrew Thompson 	if (do_unlock) {
431bdd41206SAndrew Thompson 		sx_unlock(udev->default_sx + 1);
432bdd41206SAndrew Thompson 	}
43302ac6454SAndrew Thompson }
43402ac6454SAndrew Thompson 
43502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
43602ac6454SAndrew Thompson  *	usb2_set_config_index
43702ac6454SAndrew Thompson  *
43802ac6454SAndrew Thompson  * This function selects configuration by index, independent of the
43902ac6454SAndrew Thompson  * actual configuration number. This function should not be used by
44002ac6454SAndrew Thompson  * USB drivers.
44102ac6454SAndrew Thompson  *
44202ac6454SAndrew Thompson  * Returns:
44302ac6454SAndrew Thompson  *    0: Success
44402ac6454SAndrew Thompson  * Else: Failure
44502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
44602ac6454SAndrew Thompson usb2_error_t
44702ac6454SAndrew Thompson usb2_set_config_index(struct usb2_device *udev, uint8_t index)
44802ac6454SAndrew Thompson {
44902ac6454SAndrew Thompson 	struct usb2_status ds;
45002ac6454SAndrew Thompson 	struct usb2_config_descriptor *cdp;
45102ac6454SAndrew Thompson 	uint16_t power;
45202ac6454SAndrew Thompson 	uint16_t max_power;
45302ac6454SAndrew Thompson 	uint8_t selfpowered;
45402ac6454SAndrew Thompson 	uint8_t do_unlock;
45502ac6454SAndrew Thompson 	usb2_error_t err;
45602ac6454SAndrew Thompson 
45702ac6454SAndrew Thompson 	DPRINTFN(6, "udev=%p index=%d\n", udev, index);
45802ac6454SAndrew Thompson 
45902ac6454SAndrew Thompson 	/* automatic locking */
46002ac6454SAndrew Thompson 	if (sx_xlocked(udev->default_sx + 1)) {
46102ac6454SAndrew Thompson 		do_unlock = 0;
46202ac6454SAndrew Thompson 	} else {
46302ac6454SAndrew Thompson 		do_unlock = 1;
46402ac6454SAndrew Thompson 		sx_xlock(udev->default_sx + 1);
46502ac6454SAndrew Thompson 	}
46602ac6454SAndrew Thompson 
467bdd41206SAndrew Thompson 	usb2_unconfigure(udev, USB_UNCFG_FLAG_FREE_SUBDEV);
46802ac6454SAndrew Thompson 
46902ac6454SAndrew Thompson 	if (index == USB_UNCONFIG_INDEX) {
47002ac6454SAndrew Thompson 		/*
47102ac6454SAndrew Thompson 		 * Leave unallocated when unconfiguring the
472bdd41206SAndrew Thompson 		 * device. "usb2_unconfigure()" will also reset
47302ac6454SAndrew Thompson 		 * the current config number and index.
47402ac6454SAndrew Thompson 		 */
475e2805033SAndrew Thompson 		err = usb2_req_set_config(udev, NULL, USB_UNCONFIG_NO);
476bd216778SAndrew Thompson 		if (udev->state == USB_STATE_CONFIGURED)
477bd216778SAndrew Thompson 			usb2_set_device_state(udev, USB_STATE_ADDRESSED);
47802ac6454SAndrew Thompson 		goto done;
47902ac6454SAndrew Thompson 	}
48002ac6454SAndrew Thompson 	/* get the full config descriptor */
4817efaaa9aSAndrew Thompson 	if (udev->flags.usb2_mode == USB_MODE_DEVICE) {
4827efaaa9aSAndrew Thompson 		/* save some memory */
483459d369eSAndrew Thompson 		err = usb2_req_get_descriptor_ptr(udev, &cdp,
484459d369eSAndrew Thompson 		    (UDESC_CONFIG << 8) | index);
4857efaaa9aSAndrew Thompson 	} else {
4867efaaa9aSAndrew Thompson 		/* normal request */
48702ac6454SAndrew Thompson 		err = usb2_req_get_config_desc_full(udev,
488e2805033SAndrew Thompson 		    NULL, &cdp, M_USB, index);
4897efaaa9aSAndrew Thompson 	}
49002ac6454SAndrew Thompson 	if (err) {
49102ac6454SAndrew Thompson 		goto done;
49202ac6454SAndrew Thompson 	}
49302ac6454SAndrew Thompson 	/* set the new config descriptor */
49402ac6454SAndrew Thompson 
49502ac6454SAndrew Thompson 	udev->cdesc = cdp;
49602ac6454SAndrew Thompson 
49702ac6454SAndrew Thompson 	/* Figure out if the device is self or bus powered. */
49802ac6454SAndrew Thompson 	selfpowered = 0;
49902ac6454SAndrew Thompson 	if ((!udev->flags.uq_bus_powered) &&
50002ac6454SAndrew Thompson 	    (cdp->bmAttributes & UC_SELF_POWERED) &&
50102ac6454SAndrew Thompson 	    (udev->flags.usb2_mode == USB_MODE_HOST)) {
50202ac6454SAndrew Thompson 		/* May be self powered. */
50302ac6454SAndrew Thompson 		if (cdp->bmAttributes & UC_BUS_POWERED) {
50402ac6454SAndrew Thompson 			/* Must ask device. */
5059f2b42e5SAndrew Thompson 			err = usb2_req_get_device_status(udev, NULL, &ds);
50602ac6454SAndrew Thompson 			if (err) {
50702ac6454SAndrew Thompson 				DPRINTFN(0, "could not read "
50802ac6454SAndrew Thompson 				    "device status: %s\n",
50902ac6454SAndrew Thompson 				    usb2_errstr(err));
51002ac6454SAndrew Thompson 			} else if (UGETW(ds.wStatus) & UDS_SELF_POWERED) {
51102ac6454SAndrew Thompson 				selfpowered = 1;
51202ac6454SAndrew Thompson 			}
51302ac6454SAndrew Thompson 			DPRINTF("status=0x%04x \n",
51402ac6454SAndrew Thompson 				UGETW(ds.wStatus));
51502ac6454SAndrew Thompson 		} else
51602ac6454SAndrew Thompson 			selfpowered = 1;
51702ac6454SAndrew Thompson 	}
51802ac6454SAndrew Thompson 	DPRINTF("udev=%p cdesc=%p (addr %d) cno=%d attr=0x%02x, "
51902ac6454SAndrew Thompson 	    "selfpowered=%d, power=%d\n",
52002ac6454SAndrew Thompson 	    udev, cdp,
52191f72cedSAndrew Thompson 	    udev->address, cdp->bConfigurationValue, cdp->bmAttributes,
52202ac6454SAndrew Thompson 	    selfpowered, cdp->bMaxPower * 2);
52302ac6454SAndrew Thompson 
52402ac6454SAndrew Thompson 	/* Check if we have enough power. */
52502ac6454SAndrew Thompson 	power = cdp->bMaxPower * 2;
52602ac6454SAndrew Thompson 
52702ac6454SAndrew Thompson 	if (udev->parent_hub) {
52802ac6454SAndrew Thompson 		max_power = udev->parent_hub->hub->portpower;
52902ac6454SAndrew Thompson 	} else {
53002ac6454SAndrew Thompson 		max_power = USB_MAX_POWER;
53102ac6454SAndrew Thompson 	}
53202ac6454SAndrew Thompson 
53302ac6454SAndrew Thompson 	if (power > max_power) {
53402ac6454SAndrew Thompson 		DPRINTFN(0, "power exceeded %d > %d\n", power, max_power);
53502ac6454SAndrew Thompson 		err = USB_ERR_NO_POWER;
53602ac6454SAndrew Thompson 		goto done;
53702ac6454SAndrew Thompson 	}
53802ac6454SAndrew Thompson 	/* Only update "self_powered" in USB Host Mode */
53902ac6454SAndrew Thompson 	if (udev->flags.usb2_mode == USB_MODE_HOST) {
54002ac6454SAndrew Thompson 		udev->flags.self_powered = selfpowered;
54102ac6454SAndrew Thompson 	}
54202ac6454SAndrew Thompson 	udev->power = power;
54302ac6454SAndrew Thompson 	udev->curr_config_no = cdp->bConfigurationValue;
54402ac6454SAndrew Thompson 	udev->curr_config_index = index;
545bd216778SAndrew Thompson 	usb2_set_device_state(udev, USB_STATE_CONFIGURED);
54602ac6454SAndrew Thompson 
54702ac6454SAndrew Thompson 	/* Set the actual configuration value. */
548e2805033SAndrew Thompson 	err = usb2_req_set_config(udev, NULL, cdp->bConfigurationValue);
54902ac6454SAndrew Thompson 	if (err) {
55002ac6454SAndrew Thompson 		goto done;
55102ac6454SAndrew Thompson 	}
552bdd41206SAndrew Thompson 
553bdd41206SAndrew Thompson 	err = usb2_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_ALLOC);
55402ac6454SAndrew Thompson 	if (err) {
55502ac6454SAndrew Thompson 		goto done;
55602ac6454SAndrew Thompson 	}
557bdd41206SAndrew Thompson 
558bdd41206SAndrew Thompson 	err = usb2_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_INIT);
559bdd41206SAndrew Thompson 	if (err) {
560bdd41206SAndrew Thompson 		goto done;
56102ac6454SAndrew Thompson 	}
562bdd41206SAndrew Thompson 
5630f6e8f93SAndrew Thompson #if USB_HAVE_UGEN
564ee3e3ff5SAndrew Thompson 	/* create device nodes for each endpoint */
565ee3e3ff5SAndrew Thompson 	usb2_cdev_create(udev);
5660f6e8f93SAndrew Thompson #endif
56702ac6454SAndrew Thompson 
56802ac6454SAndrew Thompson done:
56902ac6454SAndrew Thompson 	DPRINTF("error=%s\n", usb2_errstr(err));
57002ac6454SAndrew Thompson 	if (err) {
571bdd41206SAndrew Thompson 		usb2_unconfigure(udev, USB_UNCFG_FLAG_FREE_SUBDEV);
57202ac6454SAndrew Thompson 	}
57302ac6454SAndrew Thompson 	if (do_unlock) {
57402ac6454SAndrew Thompson 		sx_unlock(udev->default_sx + 1);
57502ac6454SAndrew Thompson 	}
57602ac6454SAndrew Thompson 	return (err);
57702ac6454SAndrew Thompson }
57802ac6454SAndrew Thompson 
57902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
580bdd41206SAndrew Thompson  *	usb2_config_parse
581bdd41206SAndrew Thompson  *
582bdd41206SAndrew Thompson  * This function will allocate and free USB interfaces and USB pipes,
583bdd41206SAndrew Thompson  * parse the USB configuration structure and initialise the USB pipes
584bdd41206SAndrew Thompson  * and interfaces. If "iface_index" is not equal to
585bdd41206SAndrew Thompson  * "USB_IFACE_INDEX_ANY" then the "cmd" parameter is the
586bdd41206SAndrew Thompson  * alternate_setting to be selected for the given interface. Else the
587bdd41206SAndrew Thompson  * "cmd" parameter is defined by "USB_CFG_XXX". "iface_index" can be
588bdd41206SAndrew Thompson  * "USB_IFACE_INDEX_ANY" or a valid USB interface index. This function
589bdd41206SAndrew Thompson  * is typically called when setting the configuration or when setting
590bdd41206SAndrew Thompson  * an alternate interface.
591bdd41206SAndrew Thompson  *
592bdd41206SAndrew Thompson  * Returns:
593bdd41206SAndrew Thompson  *    0: Success
594bdd41206SAndrew Thompson  * Else: Failure
595bdd41206SAndrew Thompson  *------------------------------------------------------------------------*/
596bdd41206SAndrew Thompson usb2_error_t
597bdd41206SAndrew Thompson usb2_config_parse(struct usb2_device *udev, uint8_t iface_index, uint8_t cmd)
598bdd41206SAndrew Thompson {
599bdd41206SAndrew Thompson 	struct usb2_idesc_parse_state ips;
600bdd41206SAndrew Thompson 	struct usb2_interface_descriptor *id;
601bdd41206SAndrew Thompson 	struct usb2_endpoint_descriptor *ed;
602bdd41206SAndrew Thompson 	struct usb2_interface *iface;
603bdd41206SAndrew Thompson 	struct usb2_pipe *pipe;
604bdd41206SAndrew Thompson 	usb2_error_t err;
605bdd41206SAndrew Thompson 	uint8_t ep_curr;
606bdd41206SAndrew Thompson 	uint8_t ep_max;
607bdd41206SAndrew Thompson 	uint8_t temp;
608bdd41206SAndrew Thompson 	uint8_t do_init;
609bdd41206SAndrew Thompson 	uint8_t alt_index;
610bdd41206SAndrew Thompson 
611bdd41206SAndrew Thompson 	if (iface_index != USB_IFACE_INDEX_ANY) {
612bdd41206SAndrew Thompson 		/* parameter overload */
613bdd41206SAndrew Thompson 		alt_index = cmd;
614bdd41206SAndrew Thompson 		cmd = USB_CFG_INIT;
615bdd41206SAndrew Thompson 	} else {
616bdd41206SAndrew Thompson 		/* not used */
617bdd41206SAndrew Thompson 		alt_index = 0;
618bdd41206SAndrew Thompson 	}
619bdd41206SAndrew Thompson 
620bdd41206SAndrew Thompson 	err = 0;
621bdd41206SAndrew Thompson 
622bdd41206SAndrew Thompson 	DPRINTFN(5, "iface_index=%d cmd=%d\n",
623bdd41206SAndrew Thompson 	    iface_index, cmd);
624bdd41206SAndrew Thompson 
625bdd41206SAndrew Thompson 	if (cmd == USB_CFG_FREE)
626bdd41206SAndrew Thompson 		goto cleanup;
627bdd41206SAndrew Thompson 
628bdd41206SAndrew Thompson 	if (cmd == USB_CFG_INIT) {
629bdd41206SAndrew Thompson 		sx_assert(udev->default_sx + 1, SA_LOCKED);
630bdd41206SAndrew Thompson 
631bdd41206SAndrew Thompson 		/* check for in-use pipes */
632bdd41206SAndrew Thompson 
633bdd41206SAndrew Thompson 		pipe = udev->pipes;
634bdd41206SAndrew Thompson 		ep_max = udev->pipes_max;
635bdd41206SAndrew Thompson 		while (ep_max--) {
636bdd41206SAndrew Thompson 			/* look for matching pipes */
637bdd41206SAndrew Thompson 			if ((iface_index == USB_IFACE_INDEX_ANY) ||
638bdd41206SAndrew Thompson 			    (iface_index == pipe->iface_index)) {
639bdd41206SAndrew Thompson 				if (pipe->refcount != 0) {
640bdd41206SAndrew Thompson 					/*
641bdd41206SAndrew Thompson 					 * This typically indicates a
642bdd41206SAndrew Thompson 					 * more serious error.
643bdd41206SAndrew Thompson 					 */
644bdd41206SAndrew Thompson 					err = USB_ERR_IN_USE;
645bdd41206SAndrew Thompson 				} else {
646bdd41206SAndrew Thompson 					/* reset pipe */
647bdd41206SAndrew Thompson 					memset(pipe, 0, sizeof(*pipe));
648bdd41206SAndrew Thompson 					/* make sure we don't zero the pipe again */
649bdd41206SAndrew Thompson 					pipe->iface_index = USB_IFACE_INDEX_ANY;
650bdd41206SAndrew Thompson 				}
651bdd41206SAndrew Thompson 			}
652bdd41206SAndrew Thompson 			pipe++;
653bdd41206SAndrew Thompson 		}
654bdd41206SAndrew Thompson 
655bdd41206SAndrew Thompson 		if (err)
656bdd41206SAndrew Thompson 			return (err);
657bdd41206SAndrew Thompson 	}
658bdd41206SAndrew Thompson 
659bdd41206SAndrew Thompson 	memset(&ips, 0, sizeof(ips));
660bdd41206SAndrew Thompson 
661bdd41206SAndrew Thompson 	ep_curr = 0;
662bdd41206SAndrew Thompson 	ep_max = 0;
663bdd41206SAndrew Thompson 
664bdd41206SAndrew Thompson 	while ((id = usb2_idesc_foreach(udev->cdesc, &ips))) {
665bdd41206SAndrew Thompson 
666bdd41206SAndrew Thompson 		/* check for interface overflow */
667bdd41206SAndrew Thompson 		if (ips.iface_index == USB_IFACE_MAX)
668bdd41206SAndrew Thompson 			break;			/* crazy */
669bdd41206SAndrew Thompson 
670bdd41206SAndrew Thompson 		iface = udev->ifaces + ips.iface_index;
671bdd41206SAndrew Thompson 
672bdd41206SAndrew Thompson 		/* check for specific interface match */
673bdd41206SAndrew Thompson 
674bdd41206SAndrew Thompson 		if (cmd == USB_CFG_INIT) {
675bdd41206SAndrew Thompson 			if ((iface_index != USB_IFACE_INDEX_ANY) &&
676bdd41206SAndrew Thompson 			    (iface_index != ips.iface_index)) {
677bdd41206SAndrew Thompson 				/* wrong interface */
678bdd41206SAndrew Thompson 				do_init = 0;
679bdd41206SAndrew Thompson 			} else if (alt_index != ips.iface_index_alt) {
680bdd41206SAndrew Thompson 				/* wrong alternate setting */
681bdd41206SAndrew Thompson 				do_init = 0;
682bdd41206SAndrew Thompson 			} else {
683bdd41206SAndrew Thompson 				/* initialise interface */
684bdd41206SAndrew Thompson 				do_init = 1;
685bdd41206SAndrew Thompson 			}
686bdd41206SAndrew Thompson 		} else
687bdd41206SAndrew Thompson 			do_init = 0;
688bdd41206SAndrew Thompson 
689bdd41206SAndrew Thompson 		/* check for new interface */
690bdd41206SAndrew Thompson 		if (ips.iface_index_alt == 0) {
691bdd41206SAndrew Thompson 			/* update current number of endpoints */
692bdd41206SAndrew Thompson 			ep_curr = ep_max;
693bdd41206SAndrew Thompson 		}
694bdd41206SAndrew Thompson 		/* check for init */
695bdd41206SAndrew Thompson 		if (do_init) {
696bdd41206SAndrew Thompson 			/* setup the USB interface structure */
697bdd41206SAndrew Thompson 			iface->idesc = id;
698bdd41206SAndrew Thompson 			/* default setting */
699bdd41206SAndrew Thompson 			iface->parent_iface_index = USB_IFACE_INDEX_ANY;
700bdd41206SAndrew Thompson 			/* set alternate index */
701bdd41206SAndrew Thompson 			iface->alt_index = alt_index;
702bdd41206SAndrew Thompson 		}
703bdd41206SAndrew Thompson 
704bdd41206SAndrew Thompson 		DPRINTFN(5, "found idesc nendpt=%d\n", id->bNumEndpoints);
705bdd41206SAndrew Thompson 
706bdd41206SAndrew Thompson 		ed = (struct usb2_endpoint_descriptor *)id;
707bdd41206SAndrew Thompson 
708bdd41206SAndrew Thompson 		temp = ep_curr;
709bdd41206SAndrew Thompson 
710bdd41206SAndrew Thompson 		/* iterate all the endpoint descriptors */
711bdd41206SAndrew Thompson 		while ((ed = usb2_edesc_foreach(udev->cdesc, ed))) {
712bdd41206SAndrew Thompson 
713bdd41206SAndrew Thompson 			if (temp == USB_EP_MAX)
714bdd41206SAndrew Thompson 				break;			/* crazy */
715bdd41206SAndrew Thompson 
716bdd41206SAndrew Thompson 			pipe = udev->pipes + temp;
717bdd41206SAndrew Thompson 
718bdd41206SAndrew Thompson 			if (do_init) {
719bdd41206SAndrew Thompson 				usb2_init_pipe(udev,
720bdd41206SAndrew Thompson 				    ips.iface_index, ed, pipe);
721bdd41206SAndrew Thompson 			}
722bdd41206SAndrew Thompson 
723bdd41206SAndrew Thompson 			temp ++;
724bdd41206SAndrew Thompson 
725bdd41206SAndrew Thompson 			/* find maximum number of endpoints */
726bdd41206SAndrew Thompson 			if (ep_max < temp)
727bdd41206SAndrew Thompson 				ep_max = temp;
728bdd41206SAndrew Thompson 
729bdd41206SAndrew Thompson 			/* optimalisation */
730bdd41206SAndrew Thompson 			id = (struct usb2_interface_descriptor *)ed;
731bdd41206SAndrew Thompson 		}
732bdd41206SAndrew Thompson 	}
733bdd41206SAndrew Thompson 
734bdd41206SAndrew Thompson 	/* NOTE: It is valid to have no interfaces and no endpoints! */
735bdd41206SAndrew Thompson 
736bdd41206SAndrew Thompson 	if (cmd == USB_CFG_ALLOC) {
737bdd41206SAndrew Thompson 		udev->ifaces_max = ips.iface_index;
738bdd41206SAndrew Thompson 		udev->ifaces = NULL;
739bdd41206SAndrew Thompson 		if (udev->ifaces_max != 0) {
740bdd41206SAndrew Thompson 			udev->ifaces = malloc(sizeof(*iface) * udev->ifaces_max,
741bdd41206SAndrew Thompson 			        M_USB, M_WAITOK | M_ZERO);
742bdd41206SAndrew Thompson 			if (udev->ifaces == NULL) {
743bdd41206SAndrew Thompson 				err = USB_ERR_NOMEM;
744bdd41206SAndrew Thompson 				goto done;
745bdd41206SAndrew Thompson 			}
746bdd41206SAndrew Thompson 		}
7479469c929SAndrew Thompson 		if (ep_max != 0) {
7489469c929SAndrew Thompson 			udev->pipes = malloc(sizeof(*pipe) * ep_max,
749bdd41206SAndrew Thompson 			        M_USB, M_WAITOK | M_ZERO);
750bdd41206SAndrew Thompson 			if (udev->pipes == NULL) {
751bdd41206SAndrew Thompson 				err = USB_ERR_NOMEM;
752bdd41206SAndrew Thompson 				goto done;
753bdd41206SAndrew Thompson 			}
7549469c929SAndrew Thompson 		} else {
7559469c929SAndrew Thompson 			udev->pipes = NULL;
756bdd41206SAndrew Thompson 		}
7579469c929SAndrew Thompson 		USB_BUS_LOCK(udev->bus);
7589469c929SAndrew Thompson 		udev->pipes_max = ep_max;
7599469c929SAndrew Thompson 		/* reset any ongoing clear-stall */
7609469c929SAndrew Thompson 		udev->pipe_curr = NULL;
7619469c929SAndrew Thompson 		USB_BUS_UNLOCK(udev->bus);
762bdd41206SAndrew Thompson 	}
763bdd41206SAndrew Thompson 
764bdd41206SAndrew Thompson done:
765bdd41206SAndrew Thompson 	if (err) {
766bdd41206SAndrew Thompson 		if (cmd == USB_CFG_ALLOC) {
767bdd41206SAndrew Thompson cleanup:
7689469c929SAndrew Thompson 			USB_BUS_LOCK(udev->bus);
7699469c929SAndrew Thompson 			udev->pipes_max = 0;
7709469c929SAndrew Thompson 			/* reset any ongoing clear-stall */
7719469c929SAndrew Thompson 			udev->pipe_curr = NULL;
7729469c929SAndrew Thompson 			USB_BUS_UNLOCK(udev->bus);
7739469c929SAndrew Thompson 
774bdd41206SAndrew Thompson 			/* cleanup */
775bdd41206SAndrew Thompson 			if (udev->ifaces != NULL)
776bdd41206SAndrew Thompson 				free(udev->ifaces, M_USB);
777bdd41206SAndrew Thompson 			if (udev->pipes != NULL)
778bdd41206SAndrew Thompson 				free(udev->pipes, M_USB);
779bdd41206SAndrew Thompson 
780bdd41206SAndrew Thompson 			udev->ifaces = NULL;
781bdd41206SAndrew Thompson 			udev->pipes = NULL;
782bdd41206SAndrew Thompson 			udev->ifaces_max = 0;
783bdd41206SAndrew Thompson 		}
784bdd41206SAndrew Thompson 	}
785bdd41206SAndrew Thompson 	return (err);
786bdd41206SAndrew Thompson }
787bdd41206SAndrew Thompson 
788bdd41206SAndrew Thompson /*------------------------------------------------------------------------*
78902ac6454SAndrew Thompson  *	usb2_set_alt_interface_index
79002ac6454SAndrew Thompson  *
79102ac6454SAndrew Thompson  * This function will select an alternate interface index for the
79202ac6454SAndrew Thompson  * given interface index. The interface should not be in use when this
793e2805033SAndrew Thompson  * function is called. That means there should not be any open USB
794e2805033SAndrew Thompson  * transfers. Else an error is returned. If the alternate setting is
795e2805033SAndrew Thompson  * already set this function will simply return success. This function
796e2805033SAndrew Thompson  * is called in Host mode and Device mode!
79702ac6454SAndrew Thompson  *
79802ac6454SAndrew Thompson  * Returns:
79902ac6454SAndrew Thompson  *    0: Success
80002ac6454SAndrew Thompson  * Else: Failure
80102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
80202ac6454SAndrew Thompson usb2_error_t
80302ac6454SAndrew Thompson usb2_set_alt_interface_index(struct usb2_device *udev,
80402ac6454SAndrew Thompson     uint8_t iface_index, uint8_t alt_index)
80502ac6454SAndrew Thompson {
80602ac6454SAndrew Thompson 	struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
80702ac6454SAndrew Thompson 	usb2_error_t err;
80802ac6454SAndrew Thompson 	uint8_t do_unlock;
80902ac6454SAndrew Thompson 
81002ac6454SAndrew Thompson 	/* automatic locking */
81102ac6454SAndrew Thompson 	if (sx_xlocked(udev->default_sx + 1)) {
81202ac6454SAndrew Thompson 		do_unlock = 0;
81302ac6454SAndrew Thompson 	} else {
81402ac6454SAndrew Thompson 		do_unlock = 1;
81502ac6454SAndrew Thompson 		sx_xlock(udev->default_sx + 1);
81602ac6454SAndrew Thompson 	}
81702ac6454SAndrew Thompson 	if (iface == NULL) {
81802ac6454SAndrew Thompson 		err = USB_ERR_INVAL;
81902ac6454SAndrew Thompson 		goto done;
82002ac6454SAndrew Thompson 	}
82102ac6454SAndrew Thompson 	if (udev->flags.usb2_mode == USB_MODE_DEVICE) {
822bdd41206SAndrew Thompson 		usb2_detach_device(udev, iface_index,
823bdd41206SAndrew Thompson 		    USB_UNCFG_FLAG_FREE_SUBDEV);
824e2805033SAndrew Thompson 	} else {
825e2805033SAndrew Thompson 		if (iface->alt_index == alt_index) {
826e2805033SAndrew Thompson 			/*
827e2805033SAndrew Thompson 			 * Optimise away duplicate setting of
828e2805033SAndrew Thompson 			 * alternate setting in USB Host Mode!
829e2805033SAndrew Thompson 			 */
830e2805033SAndrew Thompson 			err = 0;
831e2805033SAndrew Thompson 			goto done;
832e2805033SAndrew Thompson 		}
83302ac6454SAndrew Thompson 	}
8340f6e8f93SAndrew Thompson #if USB_HAVE_UGEN
83502ac6454SAndrew Thompson 	/*
83602ac6454SAndrew Thompson 	 * Free all generic FIFOs for this interface, except control
83702ac6454SAndrew Thompson 	 * endpoint FIFOs:
83802ac6454SAndrew Thompson 	 */
83902ac6454SAndrew Thompson 	usb2_fifo_free_wrap(udev, iface_index, 0);
8400f6e8f93SAndrew Thompson #endif
841bdd41206SAndrew Thompson 
842bdd41206SAndrew Thompson 	err = usb2_config_parse(udev, iface_index, alt_index);
84302ac6454SAndrew Thompson 	if (err) {
84402ac6454SAndrew Thompson 		goto done;
84502ac6454SAndrew Thompson 	}
846e2805033SAndrew Thompson 	err = usb2_req_set_alt_interface_no(udev, NULL, iface_index,
84702ac6454SAndrew Thompson 	    iface->idesc->bAlternateSetting);
84802ac6454SAndrew Thompson 
84902ac6454SAndrew Thompson done:
85002ac6454SAndrew Thompson 	if (do_unlock) {
85102ac6454SAndrew Thompson 		sx_unlock(udev->default_sx + 1);
85202ac6454SAndrew Thompson 	}
85302ac6454SAndrew Thompson 	return (err);
85402ac6454SAndrew Thompson }
85502ac6454SAndrew Thompson 
85602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
85702ac6454SAndrew Thompson  *	usb2_set_endpoint_stall
85802ac6454SAndrew Thompson  *
85902ac6454SAndrew Thompson  * This function is used to make a BULK or INTERRUPT endpoint
86002ac6454SAndrew Thompson  * send STALL tokens.
86102ac6454SAndrew Thompson  *
86202ac6454SAndrew Thompson  * Returns:
86302ac6454SAndrew Thompson  *    0: Success
86402ac6454SAndrew Thompson  * Else: Failure
86502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
86602ac6454SAndrew Thompson usb2_error_t
86702ac6454SAndrew Thompson usb2_set_endpoint_stall(struct usb2_device *udev, struct usb2_pipe *pipe,
86802ac6454SAndrew Thompson     uint8_t do_stall)
86902ac6454SAndrew Thompson {
87002ac6454SAndrew Thompson 	struct usb2_xfer *xfer;
87102ac6454SAndrew Thompson 	uint8_t et;
87202ac6454SAndrew Thompson 	uint8_t was_stalled;
87302ac6454SAndrew Thompson 
87402ac6454SAndrew Thompson 	if (pipe == NULL) {
87502ac6454SAndrew Thompson 		/* nothing to do */
87602ac6454SAndrew Thompson 		DPRINTF("Cannot find endpoint\n");
87702ac6454SAndrew Thompson 		/*
87802ac6454SAndrew Thompson 		 * Pretend that the clear or set stall request is
87902ac6454SAndrew Thompson 		 * successful else some USB host stacks can do
88002ac6454SAndrew Thompson 		 * strange things, especially when a control endpoint
88102ac6454SAndrew Thompson 		 * stalls.
88202ac6454SAndrew Thompson 		 */
88302ac6454SAndrew Thompson 		return (0);
88402ac6454SAndrew Thompson 	}
88502ac6454SAndrew Thompson 	et = (pipe->edesc->bmAttributes & UE_XFERTYPE);
88602ac6454SAndrew Thompson 
88702ac6454SAndrew Thompson 	if ((et != UE_BULK) &&
88802ac6454SAndrew Thompson 	    (et != UE_INTERRUPT)) {
88902ac6454SAndrew Thompson 		/*
89002ac6454SAndrew Thompson 	         * Should not stall control
89102ac6454SAndrew Thompson 	         * nor isochronous endpoints.
89202ac6454SAndrew Thompson 	         */
89302ac6454SAndrew Thompson 		DPRINTF("Invalid endpoint\n");
89402ac6454SAndrew Thompson 		return (0);
89502ac6454SAndrew Thompson 	}
89602ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
89702ac6454SAndrew Thompson 
89802ac6454SAndrew Thompson 	/* store current stall state */
89902ac6454SAndrew Thompson 	was_stalled = pipe->is_stalled;
90002ac6454SAndrew Thompson 
90102ac6454SAndrew Thompson 	/* check for no change */
90202ac6454SAndrew Thompson 	if (was_stalled && do_stall) {
90302ac6454SAndrew Thompson 		/* if the pipe is already stalled do nothing */
90402ac6454SAndrew Thompson 		USB_BUS_UNLOCK(udev->bus);
90502ac6454SAndrew Thompson 		DPRINTF("No change\n");
90602ac6454SAndrew Thompson 		return (0);
90702ac6454SAndrew Thompson 	}
90802ac6454SAndrew Thompson 	/* set stalled state */
90902ac6454SAndrew Thompson 	pipe->is_stalled = 1;
91002ac6454SAndrew Thompson 
91102ac6454SAndrew Thompson 	if (do_stall || (!was_stalled)) {
91202ac6454SAndrew Thompson 		if (!was_stalled) {
91302ac6454SAndrew Thompson 			/* lookup the current USB transfer, if any */
91402ac6454SAndrew Thompson 			xfer = pipe->pipe_q.curr;
91502ac6454SAndrew Thompson 		} else {
91602ac6454SAndrew Thompson 			xfer = NULL;
91702ac6454SAndrew Thompson 		}
91802ac6454SAndrew Thompson 
91902ac6454SAndrew Thompson 		/*
92002ac6454SAndrew Thompson 		 * If "xfer" is non-NULL the "set_stall" method will
92102ac6454SAndrew Thompson 		 * complete the USB transfer like in case of a timeout
92202ac6454SAndrew Thompson 		 * setting the error code "USB_ERR_STALLED".
92302ac6454SAndrew Thompson 		 */
92402ac6454SAndrew Thompson 		(udev->bus->methods->set_stall) (udev, xfer, pipe);
92502ac6454SAndrew Thompson 	}
92602ac6454SAndrew Thompson 	if (!do_stall) {
92702ac6454SAndrew Thompson 		pipe->toggle_next = 0;	/* reset data toggle */
92802ac6454SAndrew Thompson 		pipe->is_stalled = 0;	/* clear stalled state */
92902ac6454SAndrew Thompson 
93002ac6454SAndrew Thompson 		(udev->bus->methods->clear_stall) (udev, pipe);
93102ac6454SAndrew Thompson 
93202ac6454SAndrew Thompson 		/* start up the current or next transfer, if any */
93302ac6454SAndrew Thompson 		usb2_command_wrapper(&pipe->pipe_q, pipe->pipe_q.curr);
93402ac6454SAndrew Thompson 	}
93502ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
93602ac6454SAndrew Thompson 	return (0);
93702ac6454SAndrew Thompson }
93802ac6454SAndrew Thompson 
93902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
94002ac6454SAndrew Thompson  *	usb2_reset_iface_endpoints - used in USB device side mode
94102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
94202ac6454SAndrew Thompson usb2_error_t
94302ac6454SAndrew Thompson usb2_reset_iface_endpoints(struct usb2_device *udev, uint8_t iface_index)
94402ac6454SAndrew Thompson {
94502ac6454SAndrew Thompson 	struct usb2_pipe *pipe;
94602ac6454SAndrew Thompson 	struct usb2_pipe *pipe_end;
94702ac6454SAndrew Thompson 	usb2_error_t err;
94802ac6454SAndrew Thompson 
94902ac6454SAndrew Thompson 	pipe = udev->pipes;
95063521bbcSAndrew Thompson 	pipe_end = udev->pipes + udev->pipes_max;
95102ac6454SAndrew Thompson 
95202ac6454SAndrew Thompson 	for (; pipe != pipe_end; pipe++) {
95302ac6454SAndrew Thompson 
95402ac6454SAndrew Thompson 		if ((pipe->edesc == NULL) ||
95502ac6454SAndrew Thompson 		    (pipe->iface_index != iface_index)) {
95602ac6454SAndrew Thompson 			continue;
95702ac6454SAndrew Thompson 		}
95802ac6454SAndrew Thompson 		/* simulate a clear stall from the peer */
95902ac6454SAndrew Thompson 		err = usb2_set_endpoint_stall(udev, pipe, 0);
96002ac6454SAndrew Thompson 		if (err) {
96102ac6454SAndrew Thompson 			/* just ignore */
96202ac6454SAndrew Thompson 		}
96302ac6454SAndrew Thompson 	}
96402ac6454SAndrew Thompson 	return (0);
96502ac6454SAndrew Thompson }
96602ac6454SAndrew Thompson 
96702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
96802ac6454SAndrew Thompson  *	usb2_detach_device_sub
96902ac6454SAndrew Thompson  *
97002ac6454SAndrew Thompson  * This function will try to detach an USB device. If it fails a panic
97102ac6454SAndrew Thompson  * will result.
972bdd41206SAndrew Thompson  *
973bdd41206SAndrew Thompson  * Flag values, see "USB_UNCFG_FLAG_XXX".
97402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
97502ac6454SAndrew Thompson static void
97602ac6454SAndrew Thompson usb2_detach_device_sub(struct usb2_device *udev, device_t *ppdev,
977bdd41206SAndrew Thompson     uint8_t flag)
97802ac6454SAndrew Thompson {
97902ac6454SAndrew Thompson 	device_t dev;
98002ac6454SAndrew Thompson 	int err;
98102ac6454SAndrew Thompson 
982bdd41206SAndrew Thompson 	if (!(flag & USB_UNCFG_FLAG_FREE_SUBDEV)) {
98302ac6454SAndrew Thompson 
98402ac6454SAndrew Thompson 		*ppdev = NULL;
98502ac6454SAndrew Thompson 
98602ac6454SAndrew Thompson 	} else if (*ppdev) {
98702ac6454SAndrew Thompson 
98802ac6454SAndrew Thompson 		/*
98902ac6454SAndrew Thompson 		 * NOTE: It is important to clear "*ppdev" before deleting
99002ac6454SAndrew Thompson 		 * the child due to some device methods being called late
99102ac6454SAndrew Thompson 		 * during the delete process !
99202ac6454SAndrew Thompson 		 */
99302ac6454SAndrew Thompson 		dev = *ppdev;
99402ac6454SAndrew Thompson 		*ppdev = NULL;
99502ac6454SAndrew Thompson 
99602ac6454SAndrew Thompson 		device_printf(dev, "at %s, port %d, addr %d "
99702ac6454SAndrew Thompson 		    "(disconnected)\n",
99802ac6454SAndrew Thompson 		    device_get_nameunit(udev->parent_dev),
99902ac6454SAndrew Thompson 		    udev->port_no, udev->address);
100002ac6454SAndrew Thompson 
100102ac6454SAndrew Thompson 		if (device_is_attached(dev)) {
1002bd216778SAndrew Thompson 			if (udev->state == USB_STATE_SUSPENDED) {
100302ac6454SAndrew Thompson 				err = DEVICE_RESUME(dev);
100402ac6454SAndrew Thompson 				if (err) {
100502ac6454SAndrew Thompson 					device_printf(dev, "Resume failed!\n");
100602ac6454SAndrew Thompson 				}
100702ac6454SAndrew Thompson 			}
100802ac6454SAndrew Thompson 			if (device_detach(dev)) {
100902ac6454SAndrew Thompson 				goto error;
101002ac6454SAndrew Thompson 			}
101102ac6454SAndrew Thompson 		}
101202ac6454SAndrew Thompson 		if (device_delete_child(udev->parent_dev, dev)) {
101302ac6454SAndrew Thompson 			goto error;
101402ac6454SAndrew Thompson 		}
101502ac6454SAndrew Thompson 	}
101602ac6454SAndrew Thompson 	return;
101702ac6454SAndrew Thompson 
101802ac6454SAndrew Thompson error:
101902ac6454SAndrew Thompson 	/* Detach is not allowed to fail in the USB world */
102002ac6454SAndrew Thompson 	panic("An USB driver would not detach!\n");
102102ac6454SAndrew Thompson }
102202ac6454SAndrew Thompson 
102302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
102402ac6454SAndrew Thompson  *	usb2_detach_device
102502ac6454SAndrew Thompson  *
102602ac6454SAndrew Thompson  * The following function will detach the matching interfaces.
102702ac6454SAndrew Thompson  * This function is NULL safe.
1028bdd41206SAndrew Thompson  *
1029bdd41206SAndrew Thompson  * Flag values, see "USB_UNCFG_FLAG_XXX".
103002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
103102ac6454SAndrew Thompson void
103202ac6454SAndrew Thompson usb2_detach_device(struct usb2_device *udev, uint8_t iface_index,
1033bdd41206SAndrew Thompson     uint8_t flag)
103402ac6454SAndrew Thompson {
103502ac6454SAndrew Thompson 	struct usb2_interface *iface;
103602ac6454SAndrew Thompson 	uint8_t i;
103702ac6454SAndrew Thompson 
103802ac6454SAndrew Thompson 	if (udev == NULL) {
103902ac6454SAndrew Thompson 		/* nothing to do */
104002ac6454SAndrew Thompson 		return;
104102ac6454SAndrew Thompson 	}
104202ac6454SAndrew Thompson 	DPRINTFN(4, "udev=%p\n", udev);
104302ac6454SAndrew Thompson 
1044bdd41206SAndrew Thompson 	sx_assert(udev->default_sx + 1, SA_LOCKED);
104502ac6454SAndrew Thompson 
104602ac6454SAndrew Thompson 	/*
104702ac6454SAndrew Thompson 	 * First detach the child to give the child's detach routine a
104802ac6454SAndrew Thompson 	 * chance to detach the sub-devices in the correct order.
104902ac6454SAndrew Thompson 	 * Then delete the child using "device_delete_child()" which
105002ac6454SAndrew Thompson 	 * will detach all sub-devices from the bottom and upwards!
105102ac6454SAndrew Thompson 	 */
105202ac6454SAndrew Thompson 	if (iface_index != USB_IFACE_INDEX_ANY) {
105302ac6454SAndrew Thompson 		i = iface_index;
105402ac6454SAndrew Thompson 		iface_index = i + 1;
105502ac6454SAndrew Thompson 	} else {
105602ac6454SAndrew Thompson 		i = 0;
105702ac6454SAndrew Thompson 		iface_index = USB_IFACE_MAX;
105802ac6454SAndrew Thompson 	}
105902ac6454SAndrew Thompson 
106002ac6454SAndrew Thompson 	/* do the detach */
106102ac6454SAndrew Thompson 
106202ac6454SAndrew Thompson 	for (; i != iface_index; i++) {
106302ac6454SAndrew Thompson 
106402ac6454SAndrew Thompson 		iface = usb2_get_iface(udev, i);
106502ac6454SAndrew Thompson 		if (iface == NULL) {
106602ac6454SAndrew Thompson 			/* looks like the end of the USB interfaces */
106702ac6454SAndrew Thompson 			break;
106802ac6454SAndrew Thompson 		}
1069bdd41206SAndrew Thompson 		usb2_detach_device_sub(udev, &iface->subdev, flag);
107002ac6454SAndrew Thompson 	}
107102ac6454SAndrew Thompson }
107202ac6454SAndrew Thompson 
107302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
107402ac6454SAndrew Thompson  *	usb2_probe_and_attach_sub
107502ac6454SAndrew Thompson  *
107602ac6454SAndrew Thompson  * Returns:
107702ac6454SAndrew Thompson  *    0: Success
107802ac6454SAndrew Thompson  * Else: Failure
107902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
108002ac6454SAndrew Thompson static uint8_t
108102ac6454SAndrew Thompson usb2_probe_and_attach_sub(struct usb2_device *udev,
108202ac6454SAndrew Thompson     struct usb2_attach_arg *uaa)
108302ac6454SAndrew Thompson {
108402ac6454SAndrew Thompson 	struct usb2_interface *iface;
108502ac6454SAndrew Thompson 	device_t dev;
108602ac6454SAndrew Thompson 	int err;
108702ac6454SAndrew Thompson 
108802ac6454SAndrew Thompson 	iface = uaa->iface;
108902ac6454SAndrew Thompson 	if (iface->parent_iface_index != USB_IFACE_INDEX_ANY) {
109002ac6454SAndrew Thompson 		/* leave interface alone */
109102ac6454SAndrew Thompson 		return (0);
109202ac6454SAndrew Thompson 	}
109302ac6454SAndrew Thompson 	dev = iface->subdev;
109402ac6454SAndrew Thompson 	if (dev) {
109502ac6454SAndrew Thompson 
109602ac6454SAndrew Thompson 		/* clean up after module unload */
109702ac6454SAndrew Thompson 
109802ac6454SAndrew Thompson 		if (device_is_attached(dev)) {
109902ac6454SAndrew Thompson 			/* already a device there */
110002ac6454SAndrew Thompson 			return (0);
110102ac6454SAndrew Thompson 		}
110202ac6454SAndrew Thompson 		/* clear "iface->subdev" as early as possible */
110302ac6454SAndrew Thompson 
110402ac6454SAndrew Thompson 		iface->subdev = NULL;
110502ac6454SAndrew Thompson 
110602ac6454SAndrew Thompson 		if (device_delete_child(udev->parent_dev, dev)) {
110702ac6454SAndrew Thompson 
110802ac6454SAndrew Thompson 			/*
110902ac6454SAndrew Thompson 			 * Panic here, else one can get a double call
111002ac6454SAndrew Thompson 			 * to device_detach().  USB devices should
111102ac6454SAndrew Thompson 			 * never fail on detach!
111202ac6454SAndrew Thompson 			 */
111302ac6454SAndrew Thompson 			panic("device_delete_child() failed!\n");
111402ac6454SAndrew Thompson 		}
111502ac6454SAndrew Thompson 	}
111602ac6454SAndrew Thompson 	if (uaa->temp_dev == NULL) {
111702ac6454SAndrew Thompson 
111802ac6454SAndrew Thompson 		/* create a new child */
111902ac6454SAndrew Thompson 		uaa->temp_dev = device_add_child(udev->parent_dev, NULL, -1);
112002ac6454SAndrew Thompson 		if (uaa->temp_dev == NULL) {
112102ac6454SAndrew Thompson 			device_printf(udev->parent_dev,
112202ac6454SAndrew Thompson 			    "Device creation failed!\n");
112302ac6454SAndrew Thompson 			return (1);	/* failure */
112402ac6454SAndrew Thompson 		}
112502ac6454SAndrew Thompson 		device_set_ivars(uaa->temp_dev, uaa);
112602ac6454SAndrew Thompson 		device_quiet(uaa->temp_dev);
112702ac6454SAndrew Thompson 	}
112802ac6454SAndrew Thompson 	/*
112902ac6454SAndrew Thompson 	 * Set "subdev" before probe and attach so that "devd" gets
113002ac6454SAndrew Thompson 	 * the information it needs.
113102ac6454SAndrew Thompson 	 */
113202ac6454SAndrew Thompson 	iface->subdev = uaa->temp_dev;
113302ac6454SAndrew Thompson 
113402ac6454SAndrew Thompson 	if (device_probe_and_attach(iface->subdev) == 0) {
113502ac6454SAndrew Thompson 		/*
113602ac6454SAndrew Thompson 		 * The USB attach arguments are only available during probe
113702ac6454SAndrew Thompson 		 * and attach !
113802ac6454SAndrew Thompson 		 */
113902ac6454SAndrew Thompson 		uaa->temp_dev = NULL;
114002ac6454SAndrew Thompson 		device_set_ivars(iface->subdev, NULL);
114102ac6454SAndrew Thompson 
1142bd216778SAndrew Thompson 		if (udev->state == USB_STATE_SUSPENDED) {
114302ac6454SAndrew Thompson 			err = DEVICE_SUSPEND(iface->subdev);
114488b4e0abSAndrew Thompson 			if (err)
114502ac6454SAndrew Thompson 				device_printf(iface->subdev, "Suspend failed\n");
114602ac6454SAndrew Thompson 		}
114702ac6454SAndrew Thompson 		return (0);		/* success */
114802ac6454SAndrew Thompson 	} else {
114902ac6454SAndrew Thompson 		/* No USB driver found */
115002ac6454SAndrew Thompson 		iface->subdev = NULL;
115102ac6454SAndrew Thompson 	}
115202ac6454SAndrew Thompson 	return (1);			/* failure */
115302ac6454SAndrew Thompson }
115402ac6454SAndrew Thompson 
115502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
115602ac6454SAndrew Thompson  *	usb2_set_parent_iface
115702ac6454SAndrew Thompson  *
115802ac6454SAndrew Thompson  * Using this function will lock the alternate interface setting on an
115902ac6454SAndrew Thompson  * interface. It is typically used for multi interface drivers. In USB
116002ac6454SAndrew Thompson  * device side mode it is assumed that the alternate interfaces all
116102ac6454SAndrew Thompson  * have the same endpoint descriptors. The default parent index value
116202ac6454SAndrew Thompson  * is "USB_IFACE_INDEX_ANY". Then the alternate setting value is not
116302ac6454SAndrew Thompson  * locked.
116402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
116502ac6454SAndrew Thompson void
116602ac6454SAndrew Thompson usb2_set_parent_iface(struct usb2_device *udev, uint8_t iface_index,
116702ac6454SAndrew Thompson     uint8_t parent_index)
116802ac6454SAndrew Thompson {
116902ac6454SAndrew Thompson 	struct usb2_interface *iface;
117002ac6454SAndrew Thompson 
117102ac6454SAndrew Thompson 	iface = usb2_get_iface(udev, iface_index);
117202ac6454SAndrew Thompson 	if (iface) {
117302ac6454SAndrew Thompson 		iface->parent_iface_index = parent_index;
117402ac6454SAndrew Thompson 	}
117502ac6454SAndrew Thompson }
117602ac6454SAndrew Thompson 
117702ac6454SAndrew Thompson static void
117802ac6454SAndrew Thompson usb2_init_attach_arg(struct usb2_device *udev,
117902ac6454SAndrew Thompson     struct usb2_attach_arg *uaa)
118002ac6454SAndrew Thompson {
118102ac6454SAndrew Thompson 	bzero(uaa, sizeof(*uaa));
118202ac6454SAndrew Thompson 
118302ac6454SAndrew Thompson 	uaa->device = udev;
118402ac6454SAndrew Thompson 	uaa->usb2_mode = udev->flags.usb2_mode;
118502ac6454SAndrew Thompson 	uaa->port = udev->port_no;
118602ac6454SAndrew Thompson 
118702ac6454SAndrew Thompson 	uaa->info.idVendor = UGETW(udev->ddesc.idVendor);
118802ac6454SAndrew Thompson 	uaa->info.idProduct = UGETW(udev->ddesc.idProduct);
118902ac6454SAndrew Thompson 	uaa->info.bcdDevice = UGETW(udev->ddesc.bcdDevice);
119002ac6454SAndrew Thompson 	uaa->info.bDeviceClass = udev->ddesc.bDeviceClass;
119102ac6454SAndrew Thompson 	uaa->info.bDeviceSubClass = udev->ddesc.bDeviceSubClass;
119202ac6454SAndrew Thompson 	uaa->info.bDeviceProtocol = udev->ddesc.bDeviceProtocol;
119302ac6454SAndrew Thompson 	uaa->info.bConfigIndex = udev->curr_config_index;
119402ac6454SAndrew Thompson 	uaa->info.bConfigNum = udev->curr_config_no;
119502ac6454SAndrew Thompson }
119602ac6454SAndrew Thompson 
119702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
119802ac6454SAndrew Thompson  *	usb2_probe_and_attach
119902ac6454SAndrew Thompson  *
120002ac6454SAndrew Thompson  * This function is called from "uhub_explore_sub()",
120102ac6454SAndrew Thompson  * "usb2_handle_set_config()" and "usb2_handle_request()".
120202ac6454SAndrew Thompson  *
120302ac6454SAndrew Thompson  * Returns:
120402ac6454SAndrew Thompson  *    0: Success
120502ac6454SAndrew Thompson  * Else: A control transfer failed
120602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
120702ac6454SAndrew Thompson usb2_error_t
120802ac6454SAndrew Thompson usb2_probe_and_attach(struct usb2_device *udev, uint8_t iface_index)
120902ac6454SAndrew Thompson {
121002ac6454SAndrew Thompson 	struct usb2_attach_arg uaa;
121102ac6454SAndrew Thompson 	struct usb2_interface *iface;
121202ac6454SAndrew Thompson 	uint8_t i;
121302ac6454SAndrew Thompson 	uint8_t j;
121402ac6454SAndrew Thompson 	uint8_t do_unlock;
121502ac6454SAndrew Thompson 
121602ac6454SAndrew Thompson 	if (udev == NULL) {
121702ac6454SAndrew Thompson 		DPRINTF("udev == NULL\n");
121802ac6454SAndrew Thompson 		return (USB_ERR_INVAL);
121902ac6454SAndrew Thompson 	}
122002ac6454SAndrew Thompson 	/* automatic locking */
122102ac6454SAndrew Thompson 	if (sx_xlocked(udev->default_sx + 1)) {
122202ac6454SAndrew Thompson 		do_unlock = 0;
122302ac6454SAndrew Thompson 	} else {
122402ac6454SAndrew Thompson 		do_unlock = 1;
122502ac6454SAndrew Thompson 		sx_xlock(udev->default_sx + 1);
122602ac6454SAndrew Thompson 	}
122702ac6454SAndrew Thompson 
122802ac6454SAndrew Thompson 	if (udev->curr_config_index == USB_UNCONFIG_INDEX) {
122902ac6454SAndrew Thompson 		/* do nothing - no configuration has been set */
123002ac6454SAndrew Thompson 		goto done;
123102ac6454SAndrew Thompson 	}
123202ac6454SAndrew Thompson 	/* setup USB attach arguments */
123302ac6454SAndrew Thompson 
123402ac6454SAndrew Thompson 	usb2_init_attach_arg(udev, &uaa);
123502ac6454SAndrew Thompson 
123602ac6454SAndrew Thompson 	/* Check if only one interface should be probed: */
123702ac6454SAndrew Thompson 	if (iface_index != USB_IFACE_INDEX_ANY) {
123802ac6454SAndrew Thompson 		i = iface_index;
123902ac6454SAndrew Thompson 		j = i + 1;
124002ac6454SAndrew Thompson 	} else {
124102ac6454SAndrew Thompson 		i = 0;
124202ac6454SAndrew Thompson 		j = USB_IFACE_MAX;
124302ac6454SAndrew Thompson 	}
124402ac6454SAndrew Thompson 
124502ac6454SAndrew Thompson 	/* Do the probe and attach */
124602ac6454SAndrew Thompson 	for (; i != j; i++) {
124702ac6454SAndrew Thompson 
124802ac6454SAndrew Thompson 		iface = usb2_get_iface(udev, i);
124902ac6454SAndrew Thompson 		if (iface == NULL) {
125002ac6454SAndrew Thompson 			/*
125102ac6454SAndrew Thompson 			 * Looks like the end of the USB
125202ac6454SAndrew Thompson 			 * interfaces !
125302ac6454SAndrew Thompson 			 */
125402ac6454SAndrew Thompson 			DPRINTFN(2, "end of interfaces "
125502ac6454SAndrew Thompson 			    "at %u\n", i);
125602ac6454SAndrew Thompson 			break;
125702ac6454SAndrew Thompson 		}
125802ac6454SAndrew Thompson 		if (iface->idesc == NULL) {
125902ac6454SAndrew Thompson 			/* no interface descriptor */
126002ac6454SAndrew Thompson 			continue;
126102ac6454SAndrew Thompson 		}
126202ac6454SAndrew Thompson 		uaa.iface = iface;
126302ac6454SAndrew Thompson 
126402ac6454SAndrew Thompson 		uaa.info.bInterfaceClass =
126502ac6454SAndrew Thompson 		    iface->idesc->bInterfaceClass;
126602ac6454SAndrew Thompson 		uaa.info.bInterfaceSubClass =
126702ac6454SAndrew Thompson 		    iface->idesc->bInterfaceSubClass;
126802ac6454SAndrew Thompson 		uaa.info.bInterfaceProtocol =
126902ac6454SAndrew Thompson 		    iface->idesc->bInterfaceProtocol;
127002ac6454SAndrew Thompson 		uaa.info.bIfaceIndex = i;
127102ac6454SAndrew Thompson 		uaa.info.bIfaceNum =
127202ac6454SAndrew Thompson 		    iface->idesc->bInterfaceNumber;
127302ac6454SAndrew Thompson 		uaa.use_generic = 0;
127402ac6454SAndrew Thompson 
127502ac6454SAndrew Thompson 		DPRINTFN(2, "iclass=%u/%u/%u iindex=%u/%u\n",
127602ac6454SAndrew Thompson 		    uaa.info.bInterfaceClass,
127702ac6454SAndrew Thompson 		    uaa.info.bInterfaceSubClass,
127802ac6454SAndrew Thompson 		    uaa.info.bInterfaceProtocol,
127902ac6454SAndrew Thompson 		    uaa.info.bIfaceIndex,
128002ac6454SAndrew Thompson 		    uaa.info.bIfaceNum);
128102ac6454SAndrew Thompson 
128202ac6454SAndrew Thompson 		/* try specific interface drivers first */
128302ac6454SAndrew Thompson 
128402ac6454SAndrew Thompson 		if (usb2_probe_and_attach_sub(udev, &uaa)) {
128502ac6454SAndrew Thompson 			/* ignore */
128602ac6454SAndrew Thompson 		}
128702ac6454SAndrew Thompson 		/* try generic interface drivers last */
128802ac6454SAndrew Thompson 
128902ac6454SAndrew Thompson 		uaa.use_generic = 1;
129002ac6454SAndrew Thompson 
129102ac6454SAndrew Thompson 		if (usb2_probe_and_attach_sub(udev, &uaa)) {
129202ac6454SAndrew Thompson 			/* ignore */
129302ac6454SAndrew Thompson 		}
129402ac6454SAndrew Thompson 	}
129502ac6454SAndrew Thompson 
129602ac6454SAndrew Thompson 	if (uaa.temp_dev) {
129702ac6454SAndrew Thompson 		/* remove the last created child; it is unused */
129802ac6454SAndrew Thompson 
129902ac6454SAndrew Thompson 		if (device_delete_child(udev->parent_dev, uaa.temp_dev)) {
130002ac6454SAndrew Thompson 			DPRINTFN(0, "device delete child failed!\n");
130102ac6454SAndrew Thompson 		}
130202ac6454SAndrew Thompson 	}
130302ac6454SAndrew Thompson done:
130402ac6454SAndrew Thompson 	if (do_unlock) {
130502ac6454SAndrew Thompson 		sx_unlock(udev->default_sx + 1);
130602ac6454SAndrew Thompson 	}
130702ac6454SAndrew Thompson 	return (0);
130802ac6454SAndrew Thompson }
130902ac6454SAndrew Thompson 
131002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
131102ac6454SAndrew Thompson  *	usb2_suspend_resume_sub
131202ac6454SAndrew Thompson  *
131302ac6454SAndrew Thompson  * This function is called when the suspend or resume methods should
131402ac6454SAndrew Thompson  * be executed on an USB device.
131502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
131602ac6454SAndrew Thompson static void
131702ac6454SAndrew Thompson usb2_suspend_resume_sub(struct usb2_device *udev, device_t dev, uint8_t do_suspend)
131802ac6454SAndrew Thompson {
131902ac6454SAndrew Thompson 	int err;
132002ac6454SAndrew Thompson 
132102ac6454SAndrew Thompson 	if (dev == NULL) {
132202ac6454SAndrew Thompson 		return;
132302ac6454SAndrew Thompson 	}
132402ac6454SAndrew Thompson 	if (!device_is_attached(dev)) {
132502ac6454SAndrew Thompson 		return;
132602ac6454SAndrew Thompson 	}
132702ac6454SAndrew Thompson 	if (do_suspend) {
132802ac6454SAndrew Thompson 		err = DEVICE_SUSPEND(dev);
132902ac6454SAndrew Thompson 	} else {
133002ac6454SAndrew Thompson 		err = DEVICE_RESUME(dev);
133102ac6454SAndrew Thompson 	}
133202ac6454SAndrew Thompson 	if (err) {
133302ac6454SAndrew Thompson 		device_printf(dev, "%s failed!\n",
133402ac6454SAndrew Thompson 		    do_suspend ? "Suspend" : "Resume");
133502ac6454SAndrew Thompson 	}
133602ac6454SAndrew Thompson }
133702ac6454SAndrew Thompson 
133802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
133902ac6454SAndrew Thompson  *	usb2_suspend_resume
134002ac6454SAndrew Thompson  *
134102ac6454SAndrew Thompson  * The following function will suspend or resume the USB device.
134202ac6454SAndrew Thompson  *
134302ac6454SAndrew Thompson  * Returns:
134402ac6454SAndrew Thompson  *    0: Success
134502ac6454SAndrew Thompson  * Else: Failure
134602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
134702ac6454SAndrew Thompson usb2_error_t
134802ac6454SAndrew Thompson usb2_suspend_resume(struct usb2_device *udev, uint8_t do_suspend)
134902ac6454SAndrew Thompson {
135002ac6454SAndrew Thompson 	struct usb2_interface *iface;
135102ac6454SAndrew Thompson 	uint8_t i;
135202ac6454SAndrew Thompson 
135302ac6454SAndrew Thompson 	if (udev == NULL) {
135402ac6454SAndrew Thompson 		/* nothing to do */
135502ac6454SAndrew Thompson 		return (0);
135602ac6454SAndrew Thompson 	}
135702ac6454SAndrew Thompson 	DPRINTFN(4, "udev=%p do_suspend=%d\n", udev, do_suspend);
135802ac6454SAndrew Thompson 
135902ac6454SAndrew Thompson 	sx_assert(udev->default_sx + 1, SA_LOCKED);
136002ac6454SAndrew Thompson 
136102ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
136202ac6454SAndrew Thompson 	/* filter the suspend events */
1363bd216778SAndrew Thompson 	if ((udev->state == USB_STATE_SUSPENDED && do_suspend) ||
1364bd216778SAndrew Thompson 	    (udev->state != USB_STATE_SUSPENDED && !do_suspend)) {
136502ac6454SAndrew Thompson 		USB_BUS_UNLOCK(udev->bus);
136602ac6454SAndrew Thompson 		/* nothing to do */
136702ac6454SAndrew Thompson 		return (0);
136802ac6454SAndrew Thompson 	}
136902ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
137002ac6454SAndrew Thompson 
137102ac6454SAndrew Thompson 	/* do the suspend or resume */
137202ac6454SAndrew Thompson 
137302ac6454SAndrew Thompson 	for (i = 0; i != USB_IFACE_MAX; i++) {
137402ac6454SAndrew Thompson 
137502ac6454SAndrew Thompson 		iface = usb2_get_iface(udev, i);
137602ac6454SAndrew Thompson 		if (iface == NULL) {
137702ac6454SAndrew Thompson 			/* looks like the end of the USB interfaces */
137802ac6454SAndrew Thompson 			break;
137902ac6454SAndrew Thompson 		}
138002ac6454SAndrew Thompson 		usb2_suspend_resume_sub(udev, iface->subdev, do_suspend);
138102ac6454SAndrew Thompson 	}
138202ac6454SAndrew Thompson 	return (0);
138302ac6454SAndrew Thompson }
138402ac6454SAndrew Thompson 
138502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
138602ac6454SAndrew Thompson  *      usb2_clear_stall_proc
138702ac6454SAndrew Thompson  *
138802ac6454SAndrew Thompson  * This function performs generic USB clear stall operations.
138902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
139002ac6454SAndrew Thompson static void
139102ac6454SAndrew Thompson usb2_clear_stall_proc(struct usb2_proc_msg *_pm)
139202ac6454SAndrew Thompson {
139302ac6454SAndrew Thompson 	struct usb2_clear_stall_msg *pm = (void *)_pm;
139402ac6454SAndrew Thompson 	struct usb2_device *udev = pm->udev;
139502ac6454SAndrew Thompson 
139602ac6454SAndrew Thompson 	/* Change lock */
139702ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
139802ac6454SAndrew Thompson 	mtx_lock(udev->default_mtx);
139902ac6454SAndrew Thompson 
140002ac6454SAndrew Thompson 	/* Start clear stall callback */
140102ac6454SAndrew Thompson 	usb2_transfer_start(udev->default_xfer[1]);
140202ac6454SAndrew Thompson 
140302ac6454SAndrew Thompson 	/* Change lock */
140402ac6454SAndrew Thompson 	mtx_unlock(udev->default_mtx);
140502ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
140602ac6454SAndrew Thompson }
140702ac6454SAndrew Thompson 
140802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
140902ac6454SAndrew Thompson  *	usb2_alloc_device
141002ac6454SAndrew Thompson  *
141102ac6454SAndrew Thompson  * This function allocates a new USB device. This function is called
141202ac6454SAndrew Thompson  * when a new device has been put in the powered state, but not yet in
141302ac6454SAndrew Thompson  * the addressed state. Get initial descriptor, set the address, get
141402ac6454SAndrew Thompson  * full descriptor and get strings.
141502ac6454SAndrew Thompson  *
141602ac6454SAndrew Thompson  * Return values:
141702ac6454SAndrew Thompson  *    0: Failure
141802ac6454SAndrew Thompson  * Else: Success
141902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
142002ac6454SAndrew Thompson struct usb2_device *
142102ac6454SAndrew Thompson usb2_alloc_device(device_t parent_dev, struct usb2_bus *bus,
142202ac6454SAndrew Thompson     struct usb2_device *parent_hub, uint8_t depth,
142302ac6454SAndrew Thompson     uint8_t port_index, uint8_t port_no, uint8_t speed, uint8_t usb2_mode)
142402ac6454SAndrew Thompson {
142502ac6454SAndrew Thompson 	struct usb2_attach_arg uaa;
142602ac6454SAndrew Thompson 	struct usb2_device *udev;
142702ac6454SAndrew Thompson 	struct usb2_device *adev;
142802ac6454SAndrew Thompson 	struct usb2_device *hub;
142902ac6454SAndrew Thompson 	uint8_t *scratch_ptr;
143002ac6454SAndrew Thompson 	uint32_t scratch_size;
143102ac6454SAndrew Thompson 	usb2_error_t err;
143202ac6454SAndrew Thompson 	uint8_t device_index;
143302ac6454SAndrew Thompson 
143402ac6454SAndrew Thompson 	DPRINTF("parent_dev=%p, bus=%p, parent_hub=%p, depth=%u, "
143502ac6454SAndrew Thompson 	    "port_index=%u, port_no=%u, speed=%u, usb2_mode=%u\n",
143602ac6454SAndrew Thompson 	    parent_dev, bus, parent_hub, depth, port_index, port_no,
143702ac6454SAndrew Thompson 	    speed, usb2_mode);
143802ac6454SAndrew Thompson 
143902ac6454SAndrew Thompson 	/*
144002ac6454SAndrew Thompson 	 * Find an unused device index. In USB Host mode this is the
144102ac6454SAndrew Thompson 	 * same as the device address.
144202ac6454SAndrew Thompson 	 *
144302ac6454SAndrew Thompson 	 * Device index zero is not used and device index 1 should
144402ac6454SAndrew Thompson 	 * always be the root hub.
144502ac6454SAndrew Thompson 	 */
144602ac6454SAndrew Thompson 	for (device_index = USB_ROOT_HUB_ADDR;
144702ac6454SAndrew Thompson 	    (device_index != bus->devices_max) &&
144802ac6454SAndrew Thompson 	    (bus->devices[device_index] != NULL);
144902ac6454SAndrew Thompson 	    device_index++) /* nop */;
145002ac6454SAndrew Thompson 
145102ac6454SAndrew Thompson 	if (device_index == bus->devices_max) {
145202ac6454SAndrew Thompson 		device_printf(bus->bdev,
145302ac6454SAndrew Thompson 		    "No free USB device index for new device!\n");
145402ac6454SAndrew Thompson 		return (NULL);
145502ac6454SAndrew Thompson 	}
145602ac6454SAndrew Thompson 
145702ac6454SAndrew Thompson 	if (depth > 0x10) {
145802ac6454SAndrew Thompson 		device_printf(bus->bdev,
145902ac6454SAndrew Thompson 		    "Invalid device depth!\n");
146002ac6454SAndrew Thompson 		return (NULL);
146102ac6454SAndrew Thompson 	}
146202ac6454SAndrew Thompson 	udev = malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO);
146302ac6454SAndrew Thompson 	if (udev == NULL) {
146402ac6454SAndrew Thompson 		return (NULL);
146502ac6454SAndrew Thompson 	}
146602ac6454SAndrew Thompson 	/* initialise our SX-lock */
146702ac6454SAndrew Thompson 	sx_init(udev->default_sx, "0123456789ABCDEF - USB device SX lock" + depth);
146802ac6454SAndrew Thompson 
146902ac6454SAndrew Thompson 	/* initialise our SX-lock */
147002ac6454SAndrew Thompson 	sx_init(udev->default_sx + 1, "0123456789ABCDEF - USB config SX lock" + depth);
147102ac6454SAndrew Thompson 
147202ac6454SAndrew Thompson 	usb2_cv_init(udev->default_cv, "WCTRL");
147302ac6454SAndrew Thompson 	usb2_cv_init(udev->default_cv + 1, "UGONE");
147402ac6454SAndrew Thompson 
147502ac6454SAndrew Thompson 	/* initialise our mutex */
147602ac6454SAndrew Thompson 	mtx_init(udev->default_mtx, "USB device mutex", NULL, MTX_DEF);
147702ac6454SAndrew Thompson 
147802ac6454SAndrew Thompson 	/* initialise generic clear stall */
147902ac6454SAndrew Thompson 	udev->cs_msg[0].hdr.pm_callback = &usb2_clear_stall_proc;
148002ac6454SAndrew Thompson 	udev->cs_msg[0].udev = udev;
148102ac6454SAndrew Thompson 	udev->cs_msg[1].hdr.pm_callback = &usb2_clear_stall_proc;
148202ac6454SAndrew Thompson 	udev->cs_msg[1].udev = udev;
148302ac6454SAndrew Thompson 
148402ac6454SAndrew Thompson 	/* initialise some USB device fields */
148502ac6454SAndrew Thompson 	udev->parent_hub = parent_hub;
148602ac6454SAndrew Thompson 	udev->parent_dev = parent_dev;
148702ac6454SAndrew Thompson 	udev->port_index = port_index;
148802ac6454SAndrew Thompson 	udev->port_no = port_no;
148902ac6454SAndrew Thompson 	udev->depth = depth;
149002ac6454SAndrew Thompson 	udev->bus = bus;
149102ac6454SAndrew Thompson 	udev->address = USB_START_ADDR;	/* default value */
1492578d0effSAndrew Thompson 	udev->plugtime = (usb2_ticks_t)ticks;
1493bd216778SAndrew Thompson 	usb2_set_device_state(udev, USB_STATE_POWERED);
149402ac6454SAndrew Thompson 	/*
149502ac6454SAndrew Thompson 	 * We need to force the power mode to "on" because there are plenty
149602ac6454SAndrew Thompson 	 * of USB devices out there that do not work very well with
149702ac6454SAndrew Thompson 	 * automatic suspend and resume!
149802ac6454SAndrew Thompson 	 */
149902ac6454SAndrew Thompson 	udev->power_mode = USB_POWER_MODE_ON;
150002ac6454SAndrew Thompson 	udev->pwr_save.last_xfer_time = ticks;
150102ac6454SAndrew Thompson 	/* we are not ready yet */
150202ac6454SAndrew Thompson 	udev->refcount = 1;
150302ac6454SAndrew Thompson 
150402ac6454SAndrew Thompson 	/* set up default endpoint descriptor */
150502ac6454SAndrew Thompson 	udev->default_ep_desc.bLength = sizeof(udev->default_ep_desc);
150602ac6454SAndrew Thompson 	udev->default_ep_desc.bDescriptorType = UDESC_ENDPOINT;
150702ac6454SAndrew Thompson 	udev->default_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
150802ac6454SAndrew Thompson 	udev->default_ep_desc.bmAttributes = UE_CONTROL;
150902ac6454SAndrew Thompson 	udev->default_ep_desc.wMaxPacketSize[0] = USB_MAX_IPACKET;
151002ac6454SAndrew Thompson 	udev->default_ep_desc.wMaxPacketSize[1] = 0;
151102ac6454SAndrew Thompson 	udev->default_ep_desc.bInterval = 0;
151202ac6454SAndrew Thompson 	udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
151302ac6454SAndrew Thompson 
151402ac6454SAndrew Thompson 	udev->speed = speed;
151502ac6454SAndrew Thompson 	udev->flags.usb2_mode = usb2_mode;
151602ac6454SAndrew Thompson 
151702ac6454SAndrew Thompson 	/* search for our High Speed USB HUB, if any */
151802ac6454SAndrew Thompson 
151902ac6454SAndrew Thompson 	adev = udev;
152002ac6454SAndrew Thompson 	hub = udev->parent_hub;
152102ac6454SAndrew Thompson 
152202ac6454SAndrew Thompson 	while (hub) {
152302ac6454SAndrew Thompson 		if (hub->speed == USB_SPEED_HIGH) {
152402ac6454SAndrew Thompson 			udev->hs_hub_addr = hub->address;
1525495f25ceSAndrew Thompson 			udev->parent_hs_hub = hub;
152602ac6454SAndrew Thompson 			udev->hs_port_no = adev->port_no;
152702ac6454SAndrew Thompson 			break;
152802ac6454SAndrew Thompson 		}
152902ac6454SAndrew Thompson 		adev = hub;
153002ac6454SAndrew Thompson 		hub = hub->parent_hub;
153102ac6454SAndrew Thompson 	}
153202ac6454SAndrew Thompson 
153302ac6454SAndrew Thompson 	/* init the default pipe */
1534bdd41206SAndrew Thompson 	usb2_init_pipe(udev, 0,
153502ac6454SAndrew Thompson 	    &udev->default_ep_desc,
153602ac6454SAndrew Thompson 	    &udev->default_pipe);
153702ac6454SAndrew Thompson 
153802ac6454SAndrew Thompson 	/* set device index */
153902ac6454SAndrew Thompson 	udev->device_index = device_index;
154002ac6454SAndrew Thompson 
15418755859aSAndrew Thompson #if USB_HAVE_UGEN
15420f6e8f93SAndrew Thompson 	/* Create ugen name */
1543ee3e3ff5SAndrew Thompson 	snprintf(udev->ugen_name, sizeof(udev->ugen_name),
1544ee3e3ff5SAndrew Thompson 	    USB_GENERIC_NAME "%u.%u", device_get_unit(bus->bdev),
1545ee3e3ff5SAndrew Thompson 	    device_index);
15468755859aSAndrew Thompson 	LIST_INIT(&udev->pd_list);
15478755859aSAndrew Thompson 
15480f6e8f93SAndrew Thompson 	/* Create the control endpoint device */
15490f6e8f93SAndrew Thompson 	udev->default_dev = usb2_make_dev(udev, 0, FREAD|FWRITE);
15500f6e8f93SAndrew Thompson 
15510f6e8f93SAndrew Thompson 	/* Create a link from /dev/ugenX.X to the default endpoint */
1552ee3e3ff5SAndrew Thompson 	make_dev_alias(udev->default_dev, udev->ugen_name);
15530f6e8f93SAndrew Thompson #endif
155402ac6454SAndrew Thompson 	if (udev->flags.usb2_mode == USB_MODE_HOST) {
155502ac6454SAndrew Thompson 
1556e2805033SAndrew Thompson 		err = usb2_req_set_address(udev, NULL, device_index);
155702ac6454SAndrew Thompson 
155802ac6454SAndrew Thompson 		/* This is the new USB device address from now on */
155902ac6454SAndrew Thompson 
156002ac6454SAndrew Thompson 		udev->address = device_index;
156102ac6454SAndrew Thompson 
156202ac6454SAndrew Thompson 		/*
156302ac6454SAndrew Thompson 		 * We ignore any set-address errors, hence there are
156402ac6454SAndrew Thompson 		 * buggy USB devices out there that actually receive
156502ac6454SAndrew Thompson 		 * the SETUP PID, but manage to set the address before
156602ac6454SAndrew Thompson 		 * the STATUS stage is ACK'ed. If the device responds
156702ac6454SAndrew Thompson 		 * to the subsequent get-descriptor at the new
156802ac6454SAndrew Thompson 		 * address, then we know that the set-address command
156902ac6454SAndrew Thompson 		 * was successful.
157002ac6454SAndrew Thompson 		 */
157102ac6454SAndrew Thompson 		if (err) {
157202ac6454SAndrew Thompson 			DPRINTFN(0, "set address %d failed "
157303797f33SAndrew Thompson 			    "(%s, ignored)\n", udev->address,
157403797f33SAndrew Thompson 			    usb2_errstr(err));
157502ac6454SAndrew Thompson 		}
157602ac6454SAndrew Thompson 		/* allow device time to set new address */
1577e2805033SAndrew Thompson 		usb2_pause_mtx(NULL,
157802ac6454SAndrew Thompson 		    USB_MS_TO_TICKS(USB_SET_ADDRESS_SETTLE));
157902ac6454SAndrew Thompson 	} else {
158002ac6454SAndrew Thompson 		/* We are not self powered */
158102ac6454SAndrew Thompson 		udev->flags.self_powered = 0;
158202ac6454SAndrew Thompson 
158302ac6454SAndrew Thompson 		/* Set unconfigured state */
158402ac6454SAndrew Thompson 		udev->curr_config_no = USB_UNCONFIG_NO;
158502ac6454SAndrew Thompson 		udev->curr_config_index = USB_UNCONFIG_INDEX;
158602ac6454SAndrew Thompson 
158702ac6454SAndrew Thompson 		/* Setup USB descriptors */
158802ac6454SAndrew Thompson 		err = (usb2_temp_setup_by_index_p) (udev, usb2_template);
158902ac6454SAndrew Thompson 		if (err) {
159002ac6454SAndrew Thompson 			DPRINTFN(0, "setting up USB template failed maybe the USB "
159102ac6454SAndrew Thompson 			    "template module has not been loaded\n");
159202ac6454SAndrew Thompson 			goto done;
159302ac6454SAndrew Thompson 		}
159402ac6454SAndrew Thompson 	}
1595bd216778SAndrew Thompson 	usb2_set_device_state(udev, USB_STATE_ADDRESSED);
159602ac6454SAndrew Thompson 
159702ac6454SAndrew Thompson 	/*
159802ac6454SAndrew Thompson 	 * Get the first 8 bytes of the device descriptor !
159902ac6454SAndrew Thompson 	 *
160002ac6454SAndrew Thompson 	 * NOTE: "usb2_do_request" will check the device descriptor
160102ac6454SAndrew Thompson 	 * next time we do a request to see if the maximum packet size
160202ac6454SAndrew Thompson 	 * changed! The 8 first bytes of the device descriptor
160302ac6454SAndrew Thompson 	 * contains the maximum packet size to use on control endpoint
160402ac6454SAndrew Thompson 	 * 0. If this value is different from "USB_MAX_IPACKET" a new
160502ac6454SAndrew Thompson 	 * USB control request will be setup!
160602ac6454SAndrew Thompson 	 */
1607e2805033SAndrew Thompson 	err = usb2_req_get_desc(udev, NULL, NULL, &udev->ddesc,
160802ac6454SAndrew Thompson 	    USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0);
160902ac6454SAndrew Thompson 	if (err) {
161002ac6454SAndrew Thompson 		DPRINTFN(0, "getting device descriptor "
161103797f33SAndrew Thompson 		    "at addr %d failed, %s!\n", udev->address,
161203797f33SAndrew Thompson 		    usb2_errstr(err));
161302ac6454SAndrew Thompson 		/* XXX try to re-enumerate the device */
1614e2805033SAndrew Thompson 		err = usb2_req_re_enumerate(udev, NULL);
161502ac6454SAndrew Thompson 		if (err) {
161602ac6454SAndrew Thompson 			goto done;
161702ac6454SAndrew Thompson 		}
161802ac6454SAndrew Thompson 	}
161902ac6454SAndrew Thompson 	DPRINTF("adding unit addr=%d, rev=%02x, class=%d, "
162002ac6454SAndrew Thompson 	    "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
162102ac6454SAndrew Thompson 	    udev->address, UGETW(udev->ddesc.bcdUSB),
162202ac6454SAndrew Thompson 	    udev->ddesc.bDeviceClass,
162302ac6454SAndrew Thompson 	    udev->ddesc.bDeviceSubClass,
162402ac6454SAndrew Thompson 	    udev->ddesc.bDeviceProtocol,
162502ac6454SAndrew Thompson 	    udev->ddesc.bMaxPacketSize,
162602ac6454SAndrew Thompson 	    udev->ddesc.bLength,
162702ac6454SAndrew Thompson 	    udev->speed);
162802ac6454SAndrew Thompson 
162902ac6454SAndrew Thompson 	/* get the full device descriptor */
1630e2805033SAndrew Thompson 	err = usb2_req_get_device_desc(udev, NULL, &udev->ddesc);
163102ac6454SAndrew Thompson 	if (err) {
163202ac6454SAndrew Thompson 		DPRINTF("addr=%d, getting full desc failed\n",
163302ac6454SAndrew Thompson 		    udev->address);
163402ac6454SAndrew Thompson 		goto done;
163502ac6454SAndrew Thompson 	}
163602ac6454SAndrew Thompson 	/*
163702ac6454SAndrew Thompson 	 * Setup temporary USB attach args so that we can figure out some
163802ac6454SAndrew Thompson 	 * basic quirks for this device.
163902ac6454SAndrew Thompson 	 */
164002ac6454SAndrew Thompson 	usb2_init_attach_arg(udev, &uaa);
164102ac6454SAndrew Thompson 
164202ac6454SAndrew Thompson 	if (usb2_test_quirk(&uaa, UQ_BUS_POWERED)) {
164302ac6454SAndrew Thompson 		udev->flags.uq_bus_powered = 1;
164402ac6454SAndrew Thompson 	}
164502ac6454SAndrew Thompson 	if (usb2_test_quirk(&uaa, UQ_NO_STRINGS)) {
164602ac6454SAndrew Thompson 		udev->flags.no_strings = 1;
164702ac6454SAndrew Thompson 	}
164802ac6454SAndrew Thompson 	/*
164902ac6454SAndrew Thompson 	 * Workaround for buggy USB devices.
165002ac6454SAndrew Thompson 	 *
165102ac6454SAndrew Thompson 	 * It appears that some string-less USB chips will crash and
165202ac6454SAndrew Thompson 	 * disappear if any attempts are made to read any string
165302ac6454SAndrew Thompson 	 * descriptors.
165402ac6454SAndrew Thompson 	 *
165502ac6454SAndrew Thompson 	 * Try to detect such chips by checking the strings in the USB
165602ac6454SAndrew Thompson 	 * device descriptor. If no strings are present there we
165702ac6454SAndrew Thompson 	 * simply disable all USB strings.
165802ac6454SAndrew Thompson 	 */
165902ac6454SAndrew Thompson 	scratch_ptr = udev->bus->scratch[0].data;
166002ac6454SAndrew Thompson 	scratch_size = sizeof(udev->bus->scratch[0].data);
166102ac6454SAndrew Thompson 
166202ac6454SAndrew Thompson 	if (udev->ddesc.iManufacturer ||
166302ac6454SAndrew Thompson 	    udev->ddesc.iProduct ||
166402ac6454SAndrew Thompson 	    udev->ddesc.iSerialNumber) {
166502ac6454SAndrew Thompson 		/* read out the language ID string */
1666e2805033SAndrew Thompson 		err = usb2_req_get_string_desc(udev, NULL,
166702ac6454SAndrew Thompson 		    (char *)scratch_ptr, 4, scratch_size,
166802ac6454SAndrew Thompson 		    USB_LANGUAGE_TABLE);
166902ac6454SAndrew Thompson 	} else {
167002ac6454SAndrew Thompson 		err = USB_ERR_INVAL;
167102ac6454SAndrew Thompson 	}
167202ac6454SAndrew Thompson 
167302ac6454SAndrew Thompson 	if (err || (scratch_ptr[0] < 4)) {
167402ac6454SAndrew Thompson 		udev->flags.no_strings = 1;
167502ac6454SAndrew Thompson 	} else {
167602ac6454SAndrew Thompson 		/* pick the first language as the default */
167702ac6454SAndrew Thompson 		udev->langid = UGETW(scratch_ptr + 2);
167802ac6454SAndrew Thompson 	}
167902ac6454SAndrew Thompson 
168002ac6454SAndrew Thompson 	/* assume 100mA bus powered for now. Changed when configured. */
168102ac6454SAndrew Thompson 	udev->power = USB_MIN_POWER;
168202ac6454SAndrew Thompson 
1683bdc081c6SAndrew Thompson #if USB_HAVE_STRINGS
168402ac6454SAndrew Thompson 	/* get serial number string */
168502ac6454SAndrew Thompson 	err = usb2_req_get_string_any
1686e2805033SAndrew Thompson 	    (udev, NULL, (char *)scratch_ptr,
168702ac6454SAndrew Thompson 	    scratch_size, udev->ddesc.iSerialNumber);
168802ac6454SAndrew Thompson 
168902ac6454SAndrew Thompson 	strlcpy(udev->serial, (char *)scratch_ptr, sizeof(udev->serial));
169002ac6454SAndrew Thompson 
169102ac6454SAndrew Thompson 	/* get manufacturer string */
169202ac6454SAndrew Thompson 	err = usb2_req_get_string_any
1693e2805033SAndrew Thompson 	    (udev, NULL, (char *)scratch_ptr,
169402ac6454SAndrew Thompson 	    scratch_size, udev->ddesc.iManufacturer);
169502ac6454SAndrew Thompson 
169602ac6454SAndrew Thompson 	strlcpy(udev->manufacturer, (char *)scratch_ptr, sizeof(udev->manufacturer));
169702ac6454SAndrew Thompson 
169802ac6454SAndrew Thompson 	/* get product string */
169902ac6454SAndrew Thompson 	err = usb2_req_get_string_any
1700e2805033SAndrew Thompson 	    (udev, NULL, (char *)scratch_ptr,
170102ac6454SAndrew Thompson 	    scratch_size, udev->ddesc.iProduct);
170202ac6454SAndrew Thompson 
170302ac6454SAndrew Thompson 	strlcpy(udev->product, (char *)scratch_ptr, sizeof(udev->product));
170402ac6454SAndrew Thompson 
170502ac6454SAndrew Thompson 	/* finish up all the strings */
170602ac6454SAndrew Thompson 	usb2_check_strings(udev);
1707bdc081c6SAndrew Thompson #endif
170802ac6454SAndrew Thompson 
170902ac6454SAndrew Thompson 	if (udev->flags.usb2_mode == USB_MODE_HOST) {
171002ac6454SAndrew Thompson 		uint8_t config_index;
171102ac6454SAndrew Thompson 		uint8_t config_quirk;
171202ac6454SAndrew Thompson 		uint8_t set_config_failed = 0;
171302ac6454SAndrew Thompson 
171402ac6454SAndrew Thompson 		/*
171502ac6454SAndrew Thompson 		 * Most USB devices should attach to config index 0 by
171602ac6454SAndrew Thompson 		 * default
171702ac6454SAndrew Thompson 		 */
171802ac6454SAndrew Thompson 		if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_0)) {
171902ac6454SAndrew Thompson 			config_index = 0;
172002ac6454SAndrew Thompson 			config_quirk = 1;
172102ac6454SAndrew Thompson 		} else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_1)) {
172202ac6454SAndrew Thompson 			config_index = 1;
172302ac6454SAndrew Thompson 			config_quirk = 1;
172402ac6454SAndrew Thompson 		} else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_2)) {
172502ac6454SAndrew Thompson 			config_index = 2;
172602ac6454SAndrew Thompson 			config_quirk = 1;
172702ac6454SAndrew Thompson 		} else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_3)) {
172802ac6454SAndrew Thompson 			config_index = 3;
172902ac6454SAndrew Thompson 			config_quirk = 1;
173002ac6454SAndrew Thompson 		} else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_4)) {
173102ac6454SAndrew Thompson 			config_index = 4;
173202ac6454SAndrew Thompson 			config_quirk = 1;
173302ac6454SAndrew Thompson 		} else {
173402ac6454SAndrew Thompson 			config_index = 0;
173502ac6454SAndrew Thompson 			config_quirk = 0;
173602ac6454SAndrew Thompson 		}
173702ac6454SAndrew Thompson 
173802ac6454SAndrew Thompson repeat_set_config:
173902ac6454SAndrew Thompson 
174002ac6454SAndrew Thompson 		DPRINTF("setting config %u\n", config_index);
174102ac6454SAndrew Thompson 
174202ac6454SAndrew Thompson 		/* get the USB device configured */
174302ac6454SAndrew Thompson 		err = usb2_set_config_index(udev, config_index);
174402ac6454SAndrew Thompson 		if (err) {
174502ac6454SAndrew Thompson 			if (udev->ddesc.bNumConfigurations != 0) {
174602ac6454SAndrew Thompson 				if (!set_config_failed) {
174702ac6454SAndrew Thompson 					set_config_failed = 1;
174802ac6454SAndrew Thompson 					/* XXX try to re-enumerate the device */
174902ac6454SAndrew Thompson 					err = usb2_req_re_enumerate(
1750e2805033SAndrew Thompson 					    udev, NULL);
175102ac6454SAndrew Thompson 					if (err == 0)
175202ac6454SAndrew Thompson 					    goto repeat_set_config;
175302ac6454SAndrew Thompson 				}
175402ac6454SAndrew Thompson 				DPRINTFN(0, "Failure selecting "
175502ac6454SAndrew Thompson 				    "configuration index %u: %s, port %u, "
175602ac6454SAndrew Thompson 				    "addr %u (ignored)\n",
175702ac6454SAndrew Thompson 				    config_index, usb2_errstr(err), udev->port_no,
175802ac6454SAndrew Thompson 				    udev->address);
175902ac6454SAndrew Thompson 			}
176002ac6454SAndrew Thompson 			/*
176102ac6454SAndrew Thompson 			 * Some USB devices do not have any
176202ac6454SAndrew Thompson 			 * configurations. Ignore any set config
176302ac6454SAndrew Thompson 			 * failures!
176402ac6454SAndrew Thompson 			 */
176502ac6454SAndrew Thompson 			err = 0;
176602ac6454SAndrew Thompson 		} else if (config_quirk) {
176702ac6454SAndrew Thompson 			/* user quirk selects configuration index */
176802ac6454SAndrew Thompson 		} else if ((config_index + 1) < udev->ddesc.bNumConfigurations) {
176902ac6454SAndrew Thompson 
177002ac6454SAndrew Thompson 			if ((udev->cdesc->bNumInterface < 2) &&
1771bdd41206SAndrew Thompson 			    (usb2_get_no_descriptors(udev->cdesc,
1772bdd41206SAndrew Thompson 			    UDESC_ENDPOINT) == 0)) {
177302ac6454SAndrew Thompson 				DPRINTFN(0, "Found no endpoints "
177402ac6454SAndrew Thompson 				    "(trying next config)!\n");
177502ac6454SAndrew Thompson 				config_index++;
177602ac6454SAndrew Thompson 				goto repeat_set_config;
177702ac6454SAndrew Thompson 			}
177802ac6454SAndrew Thompson 			if (config_index == 0) {
177902ac6454SAndrew Thompson 				/*
178002ac6454SAndrew Thompson 				 * Try to figure out if we have an
178102ac6454SAndrew Thompson 				 * auto-install disk there:
178202ac6454SAndrew Thompson 				 */
178302ac6454SAndrew Thompson 				if (usb2_test_autoinstall(udev, 0, 0) == 0) {
178402ac6454SAndrew Thompson 					DPRINTFN(0, "Found possible auto-install "
178502ac6454SAndrew Thompson 					    "disk (trying next config)\n");
178602ac6454SAndrew Thompson 					config_index++;
178702ac6454SAndrew Thompson 					goto repeat_set_config;
178802ac6454SAndrew Thompson 				}
178902ac6454SAndrew Thompson 			}
179002ac6454SAndrew Thompson 		} else if (usb2_test_huawei_autoinst_p(udev, &uaa) == 0) {
179102ac6454SAndrew Thompson 			DPRINTFN(0, "Found Huawei auto-install disk!\n");
179202ac6454SAndrew Thompson 			err = USB_ERR_STALLED;	/* fake an error */
179302ac6454SAndrew Thompson 		}
179402ac6454SAndrew Thompson 	} else {
179502ac6454SAndrew Thompson 		err = 0;		/* set success */
179602ac6454SAndrew Thompson 	}
179702ac6454SAndrew Thompson 
179802ac6454SAndrew Thompson 	DPRINTF("new dev (addr %d), udev=%p, parent_hub=%p\n",
179902ac6454SAndrew Thompson 	    udev->address, udev, udev->parent_hub);
180002ac6454SAndrew Thompson 
180102ac6454SAndrew Thompson 	/* register our device - we are ready */
180202ac6454SAndrew Thompson 	usb2_bus_port_set_device(bus, parent_hub ?
180302ac6454SAndrew Thompson 	    parent_hub->hub->ports + port_index : NULL, udev, device_index);
180402ac6454SAndrew Thompson 
18050f6e8f93SAndrew Thompson #if USB_HAVE_UGEN
18068755859aSAndrew Thompson 	/* Symlink the ugen device name */
1807ee3e3ff5SAndrew Thompson 	udev->ugen_symlink = usb2_alloc_symlink(udev->ugen_name);
18088755859aSAndrew Thompson 
18098755859aSAndrew Thompson 	/* Announce device */
1810ee3e3ff5SAndrew Thompson 	printf("%s: <%s> at %s\n", udev->ugen_name, udev->manufacturer,
181102ac6454SAndrew Thompson 	    device_get_nameunit(udev->bus->bdev));
18127efaaa9aSAndrew Thompson 
181302ac6454SAndrew Thompson 	usb2_notify_addq("+", udev);
18148755859aSAndrew Thompson #endif
181502ac6454SAndrew Thompson done:
181602ac6454SAndrew Thompson 	if (err) {
181702ac6454SAndrew Thompson 		/* free device  */
1818bdd41206SAndrew Thompson 		usb2_free_device(udev,
1819bdd41206SAndrew Thompson 		    USB_UNCFG_FLAG_FREE_SUBDEV |
1820bdd41206SAndrew Thompson 		    USB_UNCFG_FLAG_FREE_EP0);
182102ac6454SAndrew Thompson 		udev = NULL;
182202ac6454SAndrew Thompson 	}
182302ac6454SAndrew Thompson 	return (udev);
182402ac6454SAndrew Thompson }
182502ac6454SAndrew Thompson 
18260f6e8f93SAndrew Thompson #if USB_HAVE_UGEN
1827ee3e3ff5SAndrew Thompson static struct cdev *
1828f5f145baSAndrew Thompson usb2_make_dev(struct usb2_device *udev, int ep, int mode)
1829ee3e3ff5SAndrew Thompson {
1830ee3e3ff5SAndrew Thompson 	struct usb2_fs_privdata* pd;
1831ee3e3ff5SAndrew Thompson 	char devname[20];
1832ee3e3ff5SAndrew Thompson 
1833ee3e3ff5SAndrew Thompson 	/* Store information to locate ourselves again later */
1834ee3e3ff5SAndrew Thompson 	pd = malloc(sizeof(struct usb2_fs_privdata), M_USBDEV,
1835ee3e3ff5SAndrew Thompson 	    M_WAITOK | M_ZERO);
1836ee3e3ff5SAndrew Thompson 	pd->bus_index = device_get_unit(udev->bus->bdev);
1837ee3e3ff5SAndrew Thompson 	pd->dev_index = udev->device_index;
1838ee3e3ff5SAndrew Thompson 	pd->ep_addr = ep;
1839ee3e3ff5SAndrew Thompson 	pd->mode = mode;
1840ee3e3ff5SAndrew Thompson 
1841ee3e3ff5SAndrew Thompson 	/* Now, create the device itself */
1842f5f145baSAndrew Thompson 	snprintf(devname, sizeof(devname), "%u.%u.%u",
1843f35aaff0SAndrew Thompson 	    pd->bus_index, pd->dev_index, pd->ep_addr);
1844ee3e3ff5SAndrew Thompson 	pd->cdev = make_dev(&usb2_devsw, 0, UID_ROOT,
1845ee3e3ff5SAndrew Thompson 	    GID_OPERATOR, 0600, USB_DEVICE_DIR "/%s", devname);
1846ee3e3ff5SAndrew Thompson 	pd->cdev->si_drv1 = pd;
1847ee3e3ff5SAndrew Thompson 
1848ee3e3ff5SAndrew Thompson 	return (pd->cdev);
1849ee3e3ff5SAndrew Thompson }
1850ee3e3ff5SAndrew Thompson 
1851ee3e3ff5SAndrew Thompson static void
1852ee3e3ff5SAndrew Thompson usb2_cdev_create(struct usb2_device *udev)
1853ee3e3ff5SAndrew Thompson {
1854f35aaff0SAndrew Thompson 	struct usb2_config_descriptor *cd;
1855f5f145baSAndrew Thompson 	struct usb2_endpoint_descriptor *ed;
1856f5f145baSAndrew Thompson 	struct usb2_descriptor *desc;
1857ee3e3ff5SAndrew Thompson 	struct usb2_fs_privdata* pd;
1858ee3e3ff5SAndrew Thompson 	struct cdev *dev;
1859f5f145baSAndrew Thompson 	int inmode, outmode, inmask, outmask, mode;
1860f5f145baSAndrew Thompson 	uint8_t ep;
1861ee3e3ff5SAndrew Thompson 
1862ee3e3ff5SAndrew Thompson 	KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("stale cdev entries"));
1863ee3e3ff5SAndrew Thompson 
1864ee3e3ff5SAndrew Thompson 	DPRINTFN(2, "Creating device nodes\n");
1865ee3e3ff5SAndrew Thompson 
1866ee3e3ff5SAndrew Thompson 	if (usb2_get_mode(udev) == USB_MODE_DEVICE) {
1867ee3e3ff5SAndrew Thompson 		inmode = FWRITE;
1868ee3e3ff5SAndrew Thompson 		outmode = FREAD;
1869ee3e3ff5SAndrew Thompson 	} else {		 /* USB_MODE_HOST */
1870ee3e3ff5SAndrew Thompson 		inmode = FREAD;
1871ee3e3ff5SAndrew Thompson 		outmode = FWRITE;
1872ee3e3ff5SAndrew Thompson 	}
1873ee3e3ff5SAndrew Thompson 
1874f5f145baSAndrew Thompson 	inmask = 0;
1875f5f145baSAndrew Thompson 	outmask = 0;
1876f5f145baSAndrew Thompson 	desc = NULL;
1877f5f145baSAndrew Thompson 
1878f5f145baSAndrew Thompson 	/*
1879f5f145baSAndrew Thompson 	 * Collect all used endpoint numbers instead of just
1880f5f145baSAndrew Thompson 	 * generating 16 static endpoints.
1881f5f145baSAndrew Thompson 	 */
1882f35aaff0SAndrew Thompson 	cd = usb2_get_config_descriptor(udev);
1883f5f145baSAndrew Thompson 	while ((desc = usb2_desc_foreach(cd, desc))) {
1884f5f145baSAndrew Thompson 		/* filter out all endpoint descriptors */
1885f5f145baSAndrew Thompson 		if ((desc->bDescriptorType == UDESC_ENDPOINT) &&
1886f5f145baSAndrew Thompson 		    (desc->bLength >= sizeof(*ed))) {
1887f5f145baSAndrew Thompson 			ed = (struct usb2_endpoint_descriptor *)desc;
1888f5f145baSAndrew Thompson 
1889f5f145baSAndrew Thompson 			/* update masks */
1890f5f145baSAndrew Thompson 			ep = ed->bEndpointAddress;
1891f5f145baSAndrew Thompson 			if (UE_GET_DIR(ep)  == UE_DIR_OUT)
1892f5f145baSAndrew Thompson 				outmask |= 1 << UE_GET_ADDR(ep);
1893f5f145baSAndrew Thompson 			else
1894f5f145baSAndrew Thompson 				inmask |= 1 << UE_GET_ADDR(ep);
1895f5f145baSAndrew Thompson 		}
1896f5f145baSAndrew Thompson 	}
1897ee3e3ff5SAndrew Thompson 
1898ee3e3ff5SAndrew Thompson 	/* Create all available endpoints except EP0 */
1899ee3e3ff5SAndrew Thompson 	for (ep = 1; ep < 16; ep++) {
1900f5f145baSAndrew Thompson 		mode = inmask & (1 << ep) ? inmode : 0;
1901f5f145baSAndrew Thompson 		mode |= outmask & (1 << ep) ? outmode : 0;
1902ee3e3ff5SAndrew Thompson 		if (mode == 0)
1903ee3e3ff5SAndrew Thompson 			continue;	/* no IN or OUT endpoint */
1904ee3e3ff5SAndrew Thompson 
1905f5f145baSAndrew Thompson 		dev = usb2_make_dev(udev, ep, mode);
1906ee3e3ff5SAndrew Thompson 		pd = dev->si_drv1;
1907ee3e3ff5SAndrew Thompson 		LIST_INSERT_HEAD(&udev->pd_list, pd, pd_next);
1908ee3e3ff5SAndrew Thompson 	}
1909ee3e3ff5SAndrew Thompson }
1910ee3e3ff5SAndrew Thompson 
1911ee3e3ff5SAndrew Thompson static void
1912ee3e3ff5SAndrew Thompson usb2_cdev_free(struct usb2_device *udev)
1913ee3e3ff5SAndrew Thompson {
1914ee3e3ff5SAndrew Thompson 	struct usb2_fs_privdata* pd;
1915ee3e3ff5SAndrew Thompson 
1916ee3e3ff5SAndrew Thompson 	DPRINTFN(2, "Freeing device nodes\n");
1917ee3e3ff5SAndrew Thompson 
1918ee3e3ff5SAndrew Thompson 	while ((pd = LIST_FIRST(&udev->pd_list)) != NULL) {
1919ee3e3ff5SAndrew Thompson 		KASSERT(pd->cdev->si_drv1 == pd, ("privdata corrupt"));
1920ee3e3ff5SAndrew Thompson 
1921ee3e3ff5SAndrew Thompson 		destroy_dev_sched_cb(pd->cdev, usb2_cdev_cleanup, pd);
1922ee3e3ff5SAndrew Thompson 		pd->cdev = NULL;
1923ee3e3ff5SAndrew Thompson 		LIST_REMOVE(pd, pd_next);
1924ee3e3ff5SAndrew Thompson 	}
1925ee3e3ff5SAndrew Thompson }
1926ee3e3ff5SAndrew Thompson 
1927ee3e3ff5SAndrew Thompson static void
1928ee3e3ff5SAndrew Thompson usb2_cdev_cleanup(void* arg)
1929ee3e3ff5SAndrew Thompson {
1930ee3e3ff5SAndrew Thompson 	free(arg, M_USBDEV);
1931ee3e3ff5SAndrew Thompson }
19320f6e8f93SAndrew Thompson #endif
1933ee3e3ff5SAndrew Thompson 
193402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
193502ac6454SAndrew Thompson  *	usb2_free_device
193602ac6454SAndrew Thompson  *
193702ac6454SAndrew Thompson  * This function is NULL safe and will free an USB device.
1938bdd41206SAndrew Thompson  *
1939bdd41206SAndrew Thompson  * Flag values, see "USB_UNCFG_FLAG_XXX".
194002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
194102ac6454SAndrew Thompson void
1942bdd41206SAndrew Thompson usb2_free_device(struct usb2_device *udev, uint8_t flag)
194302ac6454SAndrew Thompson {
1944bdd41206SAndrew Thompson 	struct usb2_bus *bus;
1945bdd41206SAndrew Thompson 
1946bdd41206SAndrew Thompson 	if (udev == NULL)
1947bdd41206SAndrew Thompson 		return;		/* already freed */
194802ac6454SAndrew Thompson 
194902ac6454SAndrew Thompson 	DPRINTFN(4, "udev=%p port=%d\n", udev, udev->port_no);
195002ac6454SAndrew Thompson 
1951bd216778SAndrew Thompson 	bus = udev->bus;
1952bd216778SAndrew Thompson 	usb2_set_device_state(udev, USB_STATE_DETACHED);
1953bdd41206SAndrew Thompson 
19548755859aSAndrew Thompson #if USB_HAVE_UGEN
195502ac6454SAndrew Thompson 	usb2_notify_addq("-", udev);
195602ac6454SAndrew Thompson 
1957ee3e3ff5SAndrew Thompson 	printf("%s: <%s> at %s (disconnected)\n", udev->ugen_name,
1958ee3e3ff5SAndrew Thompson 	    udev->manufacturer, device_get_nameunit(bus->bdev));
195902ac6454SAndrew Thompson 
1960ee3e3ff5SAndrew Thompson 	/* Destroy UGEN symlink, if any */
196102ac6454SAndrew Thompson 	if (udev->ugen_symlink) {
196202ac6454SAndrew Thompson 		usb2_free_symlink(udev->ugen_symlink);
196302ac6454SAndrew Thompson 		udev->ugen_symlink = NULL;
196402ac6454SAndrew Thompson 	}
19658755859aSAndrew Thompson #endif
196602ac6454SAndrew Thompson 	/*
196702ac6454SAndrew Thompson 	 * Unregister our device first which will prevent any further
196802ac6454SAndrew Thompson 	 * references:
196902ac6454SAndrew Thompson 	 */
197002ac6454SAndrew Thompson 	usb2_bus_port_set_device(bus, udev->parent_hub ?
197102ac6454SAndrew Thompson 	    udev->parent_hub->hub->ports + udev->port_index : NULL,
197202ac6454SAndrew Thompson 	    NULL, USB_ROOT_HUB_ADDR);
197302ac6454SAndrew Thompson 
19740f6e8f93SAndrew Thompson #if USB_HAVE_UGEN
197502ac6454SAndrew Thompson 	/* wait for all pending references to go away: */
197602ac6454SAndrew Thompson 	mtx_lock(&usb2_ref_lock);
197702ac6454SAndrew Thompson 	udev->refcount--;
197802ac6454SAndrew Thompson 	while (udev->refcount != 0) {
197902ac6454SAndrew Thompson 		usb2_cv_wait(udev->default_cv + 1, &usb2_ref_lock);
198002ac6454SAndrew Thompson 	}
198102ac6454SAndrew Thompson 	mtx_unlock(&usb2_ref_lock);
1982bdd41206SAndrew Thompson 
1983bdd41206SAndrew Thompson 	destroy_dev_sched_cb(udev->default_dev, usb2_cdev_cleanup,
1984bdd41206SAndrew Thompson 	    udev->default_dev->si_drv1);
19850f6e8f93SAndrew Thompson #endif
198602ac6454SAndrew Thompson 
198702ac6454SAndrew Thompson 	if (udev->flags.usb2_mode == USB_MODE_DEVICE) {
198802ac6454SAndrew Thompson 		/* stop receiving any control transfers (Device Side Mode) */
198902ac6454SAndrew Thompson 		usb2_transfer_unsetup(udev->default_xfer, USB_DEFAULT_XFER_MAX);
199002ac6454SAndrew Thompson 	}
199102ac6454SAndrew Thompson 
1992bdd41206SAndrew Thompson 	/* the following will get the device unconfigured in software */
1993bdd41206SAndrew Thompson 	usb2_unconfigure(udev, flag);
199402ac6454SAndrew Thompson 
199502ac6454SAndrew Thompson 	/* unsetup any leftover default USB transfers */
199602ac6454SAndrew Thompson 	usb2_transfer_unsetup(udev->default_xfer, USB_DEFAULT_XFER_MAX);
199702ac6454SAndrew Thompson 
199802ac6454SAndrew Thompson 	/* template unsetup, if any */
199902ac6454SAndrew Thompson 	(usb2_temp_unsetup_p) (udev);
200002ac6454SAndrew Thompson 
200102ac6454SAndrew Thompson 	/*
200202ac6454SAndrew Thompson 	 * Make sure that our clear-stall messages are not queued
200302ac6454SAndrew Thompson 	 * anywhere:
200402ac6454SAndrew Thompson 	 */
200502ac6454SAndrew Thompson 	USB_BUS_LOCK(udev->bus);
200602ac6454SAndrew Thompson 	usb2_proc_mwait(&udev->bus->non_giant_callback_proc,
200702ac6454SAndrew Thompson 	    &udev->cs_msg[0], &udev->cs_msg[1]);
200802ac6454SAndrew Thompson 	USB_BUS_UNLOCK(udev->bus);
200902ac6454SAndrew Thompson 
201002ac6454SAndrew Thompson 	sx_destroy(udev->default_sx);
201102ac6454SAndrew Thompson 	sx_destroy(udev->default_sx + 1);
201202ac6454SAndrew Thompson 
201302ac6454SAndrew Thompson 	usb2_cv_destroy(udev->default_cv);
201402ac6454SAndrew Thompson 	usb2_cv_destroy(udev->default_cv + 1);
201502ac6454SAndrew Thompson 
201602ac6454SAndrew Thompson 	mtx_destroy(udev->default_mtx);
20178755859aSAndrew Thompson #if USB_HAVE_UGEN
2018ee3e3ff5SAndrew Thompson 	KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("leaked cdev entries"));
20198755859aSAndrew Thompson #endif
202002ac6454SAndrew Thompson 
202102ac6454SAndrew Thompson 	/* free device */
202202ac6454SAndrew Thompson 	free(udev, M_USB);
202302ac6454SAndrew Thompson }
202402ac6454SAndrew Thompson 
202502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
202602ac6454SAndrew Thompson  *	usb2_get_iface
202702ac6454SAndrew Thompson  *
202802ac6454SAndrew Thompson  * This function is the safe way to get the USB interface structure
202902ac6454SAndrew Thompson  * pointer by interface index.
203002ac6454SAndrew Thompson  *
203102ac6454SAndrew Thompson  * Return values:
203202ac6454SAndrew Thompson  *   NULL: Interface not present.
203302ac6454SAndrew Thompson  *   Else: Pointer to USB interface structure.
203402ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
203502ac6454SAndrew Thompson struct usb2_interface *
203602ac6454SAndrew Thompson usb2_get_iface(struct usb2_device *udev, uint8_t iface_index)
203702ac6454SAndrew Thompson {
203802ac6454SAndrew Thompson 	struct usb2_interface *iface = udev->ifaces + iface_index;
203902ac6454SAndrew Thompson 
2040bdd41206SAndrew Thompson 	if (iface_index >= udev->ifaces_max)
204102ac6454SAndrew Thompson 		return (NULL);
204202ac6454SAndrew Thompson 	return (iface);
204302ac6454SAndrew Thompson }
204402ac6454SAndrew Thompson 
204502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
204602ac6454SAndrew Thompson  *	usb2_find_descriptor
204702ac6454SAndrew Thompson  *
204802ac6454SAndrew Thompson  * This function will lookup the first descriptor that matches the
204902ac6454SAndrew Thompson  * criteria given by the arguments "type" and "subtype". Descriptors
205002ac6454SAndrew Thompson  * will only be searched within the interface having the index
205102ac6454SAndrew Thompson  * "iface_index".  If the "id" argument points to an USB descriptor,
205202ac6454SAndrew Thompson  * it will be skipped before the search is started. This allows
205302ac6454SAndrew Thompson  * searching for multiple descriptors using the same criteria. Else
205402ac6454SAndrew Thompson  * the search is started after the interface descriptor.
205502ac6454SAndrew Thompson  *
205602ac6454SAndrew Thompson  * Return values:
205702ac6454SAndrew Thompson  *   NULL: End of descriptors
205802ac6454SAndrew Thompson  *   Else: A descriptor matching the criteria
205902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
206002ac6454SAndrew Thompson void   *
206102ac6454SAndrew Thompson usb2_find_descriptor(struct usb2_device *udev, void *id, uint8_t iface_index,
206202ac6454SAndrew Thompson     uint8_t type, uint8_t type_mask,
206302ac6454SAndrew Thompson     uint8_t subtype, uint8_t subtype_mask)
206402ac6454SAndrew Thompson {
206502ac6454SAndrew Thompson 	struct usb2_descriptor *desc;
206602ac6454SAndrew Thompson 	struct usb2_config_descriptor *cd;
206702ac6454SAndrew Thompson 	struct usb2_interface *iface;
206802ac6454SAndrew Thompson 
206902ac6454SAndrew Thompson 	cd = usb2_get_config_descriptor(udev);
207002ac6454SAndrew Thompson 	if (cd == NULL) {
207102ac6454SAndrew Thompson 		return (NULL);
207202ac6454SAndrew Thompson 	}
207302ac6454SAndrew Thompson 	if (id == NULL) {
207402ac6454SAndrew Thompson 		iface = usb2_get_iface(udev, iface_index);
207502ac6454SAndrew Thompson 		if (iface == NULL) {
207602ac6454SAndrew Thompson 			return (NULL);
207702ac6454SAndrew Thompson 		}
207802ac6454SAndrew Thompson 		id = usb2_get_interface_descriptor(iface);
207902ac6454SAndrew Thompson 		if (id == NULL) {
208002ac6454SAndrew Thompson 			return (NULL);
208102ac6454SAndrew Thompson 		}
208202ac6454SAndrew Thompson 	}
208302ac6454SAndrew Thompson 	desc = (void *)id;
208402ac6454SAndrew Thompson 
208502ac6454SAndrew Thompson 	while ((desc = usb2_desc_foreach(cd, desc))) {
208602ac6454SAndrew Thompson 
208702ac6454SAndrew Thompson 		if (desc->bDescriptorType == UDESC_INTERFACE) {
208802ac6454SAndrew Thompson 			break;
208902ac6454SAndrew Thompson 		}
209002ac6454SAndrew Thompson 		if (((desc->bDescriptorType & type_mask) == type) &&
209102ac6454SAndrew Thompson 		    ((desc->bDescriptorSubtype & subtype_mask) == subtype)) {
209202ac6454SAndrew Thompson 			return (desc);
209302ac6454SAndrew Thompson 		}
209402ac6454SAndrew Thompson 	}
209502ac6454SAndrew Thompson 	return (NULL);
209602ac6454SAndrew Thompson }
209702ac6454SAndrew Thompson 
209802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
209902ac6454SAndrew Thompson  *	usb2_devinfo
210002ac6454SAndrew Thompson  *
210102ac6454SAndrew Thompson  * This function will dump information from the device descriptor
210202ac6454SAndrew Thompson  * belonging to the USB device pointed to by "udev", to the string
210302ac6454SAndrew Thompson  * pointed to by "dst_ptr" having a maximum length of "dst_len" bytes
210402ac6454SAndrew Thompson  * including the terminating zero.
210502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
210602ac6454SAndrew Thompson void
210702ac6454SAndrew Thompson usb2_devinfo(struct usb2_device *udev, char *dst_ptr, uint16_t dst_len)
210802ac6454SAndrew Thompson {
210902ac6454SAndrew Thompson 	struct usb2_device_descriptor *udd = &udev->ddesc;
211002ac6454SAndrew Thompson 	uint16_t bcdDevice;
211102ac6454SAndrew Thompson 	uint16_t bcdUSB;
211202ac6454SAndrew Thompson 
211302ac6454SAndrew Thompson 	bcdUSB = UGETW(udd->bcdUSB);
211402ac6454SAndrew Thompson 	bcdDevice = UGETW(udd->bcdDevice);
211502ac6454SAndrew Thompson 
211602ac6454SAndrew Thompson 	if (udd->bDeviceClass != 0xFF) {
211702ac6454SAndrew Thompson 		snprintf(dst_ptr, dst_len, "%s %s, class %d/%d, rev %x.%02x/"
21188755859aSAndrew Thompson 		    "%x.%02x, addr %d",
21198755859aSAndrew Thompson #if USB_HAVE_STRINGS
21208755859aSAndrew Thompson 		    udev->manufacturer, udev->product,
21218755859aSAndrew Thompson #else
21228755859aSAndrew Thompson 		    "-", "-",
21238755859aSAndrew Thompson #endif
212402ac6454SAndrew Thompson 		    udd->bDeviceClass, udd->bDeviceSubClass,
212502ac6454SAndrew Thompson 		    (bcdUSB >> 8), bcdUSB & 0xFF,
212602ac6454SAndrew Thompson 		    (bcdDevice >> 8), bcdDevice & 0xFF,
212702ac6454SAndrew Thompson 		    udev->address);
212802ac6454SAndrew Thompson 	} else {
212902ac6454SAndrew Thompson 		snprintf(dst_ptr, dst_len, "%s %s, rev %x.%02x/"
21308755859aSAndrew Thompson 		    "%x.%02x, addr %d",
21318755859aSAndrew Thompson #if USB_HAVE_STRINGS
21328755859aSAndrew Thompson 		    udev->manufacturer, udev->product,
21338755859aSAndrew Thompson #else
21348755859aSAndrew Thompson 		    "-", "-",
21358755859aSAndrew Thompson #endif
213602ac6454SAndrew Thompson 		    (bcdUSB >> 8), bcdUSB & 0xFF,
213702ac6454SAndrew Thompson 		    (bcdDevice >> 8), bcdDevice & 0xFF,
213802ac6454SAndrew Thompson 		    udev->address);
213902ac6454SAndrew Thompson 	}
214002ac6454SAndrew Thompson }
214102ac6454SAndrew Thompson 
2142bdc081c6SAndrew Thompson #if USB_HAVE_STRINGS
214302ac6454SAndrew Thompson #if USB_VERBOSE
214402ac6454SAndrew Thompson /*
214502ac6454SAndrew Thompson  * Descriptions of of known vendors and devices ("products").
214602ac6454SAndrew Thompson  */
214702ac6454SAndrew Thompson struct usb_knowndev {
214802ac6454SAndrew Thompson 	uint16_t vendor;
214902ac6454SAndrew Thompson 	uint16_t product;
215002ac6454SAndrew Thompson 	uint32_t flags;
215102ac6454SAndrew Thompson 	const char *vendorname;
215202ac6454SAndrew Thompson 	const char *productname;
215302ac6454SAndrew Thompson };
215402ac6454SAndrew Thompson 
215502ac6454SAndrew Thompson #define	USB_KNOWNDEV_NOPROD	0x01	/* match on vendor only */
215602ac6454SAndrew Thompson 
215702ac6454SAndrew Thompson #include "usbdevs.h"
215802ac6454SAndrew Thompson #include "usbdevs_data.h"
215902ac6454SAndrew Thompson #endif					/* USB_VERBOSE */
216002ac6454SAndrew Thompson 
216102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
216202ac6454SAndrew Thompson  *	usb2_check_strings
216302ac6454SAndrew Thompson  *
216402ac6454SAndrew Thompson  * This function checks the manufacturer and product strings and will
216502ac6454SAndrew Thompson  * fill in defaults for missing strings.
216602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
216702ac6454SAndrew Thompson static void
216802ac6454SAndrew Thompson usb2_check_strings(struct usb2_device *udev)
216902ac6454SAndrew Thompson {
217002ac6454SAndrew Thompson 	struct usb2_device_descriptor *udd = &udev->ddesc;
217102ac6454SAndrew Thompson 	const char *vendor;
217202ac6454SAndrew Thompson 	const char *product;
217302ac6454SAndrew Thompson 
217402ac6454SAndrew Thompson #if USB_VERBOSE
217502ac6454SAndrew Thompson 	const struct usb_knowndev *kdp;
217602ac6454SAndrew Thompson 
217702ac6454SAndrew Thompson #endif
217802ac6454SAndrew Thompson 	uint16_t vendor_id;
217902ac6454SAndrew Thompson 	uint16_t product_id;
218002ac6454SAndrew Thompson 
218102ac6454SAndrew Thompson 	usb2_trim_spaces(udev->manufacturer);
218202ac6454SAndrew Thompson 	usb2_trim_spaces(udev->product);
218302ac6454SAndrew Thompson 
218402ac6454SAndrew Thompson 	if (udev->manufacturer[0]) {
218502ac6454SAndrew Thompson 		vendor = udev->manufacturer;
218602ac6454SAndrew Thompson 	} else {
218702ac6454SAndrew Thompson 		vendor = NULL;
218802ac6454SAndrew Thompson 	}
218902ac6454SAndrew Thompson 
219002ac6454SAndrew Thompson 	if (udev->product[0]) {
219102ac6454SAndrew Thompson 		product = udev->product;
219202ac6454SAndrew Thompson 	} else {
219302ac6454SAndrew Thompson 		product = NULL;
219402ac6454SAndrew Thompson 	}
219502ac6454SAndrew Thompson 
219602ac6454SAndrew Thompson 	vendor_id = UGETW(udd->idVendor);
219702ac6454SAndrew Thompson 	product_id = UGETW(udd->idProduct);
219802ac6454SAndrew Thompson 
219902ac6454SAndrew Thompson #if USB_VERBOSE
220002ac6454SAndrew Thompson 	if (vendor == NULL || product == NULL) {
220102ac6454SAndrew Thompson 
220202ac6454SAndrew Thompson 		for (kdp = usb_knowndevs;
220302ac6454SAndrew Thompson 		    kdp->vendorname != NULL;
220402ac6454SAndrew Thompson 		    kdp++) {
220502ac6454SAndrew Thompson 			if (kdp->vendor == vendor_id &&
220602ac6454SAndrew Thompson 			    (kdp->product == product_id ||
220702ac6454SAndrew Thompson 			    (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
220802ac6454SAndrew Thompson 				break;
220902ac6454SAndrew Thompson 		}
221002ac6454SAndrew Thompson 		if (kdp->vendorname != NULL) {
221102ac6454SAndrew Thompson 			if (vendor == NULL)
221202ac6454SAndrew Thompson 				vendor = kdp->vendorname;
221302ac6454SAndrew Thompson 			if (product == NULL)
221402ac6454SAndrew Thompson 				product = (kdp->flags & USB_KNOWNDEV_NOPROD) == 0 ?
221502ac6454SAndrew Thompson 				    kdp->productname : NULL;
221602ac6454SAndrew Thompson 		}
221702ac6454SAndrew Thompson 	}
221802ac6454SAndrew Thompson #endif
221902ac6454SAndrew Thompson 	if (vendor && *vendor) {
222002ac6454SAndrew Thompson 		if (udev->manufacturer != vendor) {
222102ac6454SAndrew Thompson 			strlcpy(udev->manufacturer, vendor,
222202ac6454SAndrew Thompson 			    sizeof(udev->manufacturer));
222302ac6454SAndrew Thompson 		}
222402ac6454SAndrew Thompson 	} else {
222502ac6454SAndrew Thompson 		snprintf(udev->manufacturer,
222602ac6454SAndrew Thompson 		    sizeof(udev->manufacturer), "vendor 0x%04x", vendor_id);
222702ac6454SAndrew Thompson 	}
222802ac6454SAndrew Thompson 
222902ac6454SAndrew Thompson 	if (product && *product) {
223002ac6454SAndrew Thompson 		if (udev->product != product) {
223102ac6454SAndrew Thompson 			strlcpy(udev->product, product,
223202ac6454SAndrew Thompson 			    sizeof(udev->product));
223302ac6454SAndrew Thompson 		}
223402ac6454SAndrew Thompson 	} else {
223502ac6454SAndrew Thompson 		snprintf(udev->product,
223602ac6454SAndrew Thompson 		    sizeof(udev->product), "product 0x%04x", product_id);
223702ac6454SAndrew Thompson 	}
223802ac6454SAndrew Thompson }
2239bdc081c6SAndrew Thompson #endif
224002ac6454SAndrew Thompson 
224102ac6454SAndrew Thompson /*
224202ac6454SAndrew Thompson  * Returns:
224302ac6454SAndrew Thompson  * See: USB_MODE_XXX
224402ac6454SAndrew Thompson  */
224502ac6454SAndrew Thompson uint8_t
224602ac6454SAndrew Thompson usb2_get_mode(struct usb2_device *udev)
224702ac6454SAndrew Thompson {
224802ac6454SAndrew Thompson 	return (udev->flags.usb2_mode);
224902ac6454SAndrew Thompson }
225002ac6454SAndrew Thompson 
225102ac6454SAndrew Thompson /*
225202ac6454SAndrew Thompson  * Returns:
225302ac6454SAndrew Thompson  * See: USB_SPEED_XXX
225402ac6454SAndrew Thompson  */
225502ac6454SAndrew Thompson uint8_t
225602ac6454SAndrew Thompson usb2_get_speed(struct usb2_device *udev)
225702ac6454SAndrew Thompson {
225802ac6454SAndrew Thompson 	return (udev->speed);
225902ac6454SAndrew Thompson }
226002ac6454SAndrew Thompson 
226102ac6454SAndrew Thompson uint32_t
226202ac6454SAndrew Thompson usb2_get_isoc_fps(struct usb2_device *udev)
226302ac6454SAndrew Thompson {
226402ac6454SAndrew Thompson 	;				/* indent fix */
226502ac6454SAndrew Thompson 	switch (udev->speed) {
226602ac6454SAndrew Thompson 	case USB_SPEED_LOW:
226702ac6454SAndrew Thompson 	case USB_SPEED_FULL:
226802ac6454SAndrew Thompson 		return (1000);
226902ac6454SAndrew Thompson 	default:
227002ac6454SAndrew Thompson 		return (8000);
227102ac6454SAndrew Thompson 	}
227202ac6454SAndrew Thompson }
227302ac6454SAndrew Thompson 
227402ac6454SAndrew Thompson struct usb2_device_descriptor *
227502ac6454SAndrew Thompson usb2_get_device_descriptor(struct usb2_device *udev)
227602ac6454SAndrew Thompson {
227702ac6454SAndrew Thompson 	if (udev == NULL)
227802ac6454SAndrew Thompson 		return (NULL);		/* be NULL safe */
227902ac6454SAndrew Thompson 	return (&udev->ddesc);
228002ac6454SAndrew Thompson }
228102ac6454SAndrew Thompson 
228202ac6454SAndrew Thompson struct usb2_config_descriptor *
228302ac6454SAndrew Thompson usb2_get_config_descriptor(struct usb2_device *udev)
228402ac6454SAndrew Thompson {
228502ac6454SAndrew Thompson 	if (udev == NULL)
228602ac6454SAndrew Thompson 		return (NULL);		/* be NULL safe */
228702ac6454SAndrew Thompson 	return (udev->cdesc);
228802ac6454SAndrew Thompson }
228902ac6454SAndrew Thompson 
229002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
229102ac6454SAndrew Thompson  *	usb2_test_quirk - test a device for a given quirk
229202ac6454SAndrew Thompson  *
229302ac6454SAndrew Thompson  * Return values:
229402ac6454SAndrew Thompson  * 0: The USB device does not have the given quirk.
229502ac6454SAndrew Thompson  * Else: The USB device has the given quirk.
229602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
229702ac6454SAndrew Thompson uint8_t
229802ac6454SAndrew Thompson usb2_test_quirk(const struct usb2_attach_arg *uaa, uint16_t quirk)
229902ac6454SAndrew Thompson {
230002ac6454SAndrew Thompson 	uint8_t found;
230102ac6454SAndrew Thompson 
230202ac6454SAndrew Thompson 	found = (usb2_test_quirk_p) (&uaa->info, quirk);
230302ac6454SAndrew Thompson 	return (found);
230402ac6454SAndrew Thompson }
230502ac6454SAndrew Thompson 
230602ac6454SAndrew Thompson struct usb2_interface_descriptor *
230702ac6454SAndrew Thompson usb2_get_interface_descriptor(struct usb2_interface *iface)
230802ac6454SAndrew Thompson {
230902ac6454SAndrew Thompson 	if (iface == NULL)
231002ac6454SAndrew Thompson 		return (NULL);		/* be NULL safe */
231102ac6454SAndrew Thompson 	return (iface->idesc);
231202ac6454SAndrew Thompson }
231302ac6454SAndrew Thompson 
231402ac6454SAndrew Thompson uint8_t
231502ac6454SAndrew Thompson usb2_get_interface_altindex(struct usb2_interface *iface)
231602ac6454SAndrew Thompson {
231702ac6454SAndrew Thompson 	return (iface->alt_index);
231802ac6454SAndrew Thompson }
231902ac6454SAndrew Thompson 
232002ac6454SAndrew Thompson uint8_t
232102ac6454SAndrew Thompson usb2_get_bus_index(struct usb2_device *udev)
232202ac6454SAndrew Thompson {
232302ac6454SAndrew Thompson 	return ((uint8_t)device_get_unit(udev->bus->bdev));
232402ac6454SAndrew Thompson }
232502ac6454SAndrew Thompson 
232602ac6454SAndrew Thompson uint8_t
232702ac6454SAndrew Thompson usb2_get_device_index(struct usb2_device *udev)
232802ac6454SAndrew Thompson {
232902ac6454SAndrew Thompson 	return (udev->device_index);
233002ac6454SAndrew Thompson }
233102ac6454SAndrew Thompson 
23328755859aSAndrew Thompson #if USB_HAVE_UGEN
233302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
233402ac6454SAndrew Thompson  *	usb2_notify_addq
233502ac6454SAndrew Thompson  *
233602ac6454SAndrew Thompson  * This function will generate events for dev.
233702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
233802ac6454SAndrew Thompson static void
233902ac6454SAndrew Thompson usb2_notify_addq(const char *type, struct usb2_device *udev)
234002ac6454SAndrew Thompson {
234102ac6454SAndrew Thompson 	char *data = NULL;
234202ac6454SAndrew Thompson 	struct malloc_type *mt;
234302ac6454SAndrew Thompson 
234402ac6454SAndrew Thompson 	mtx_lock(&malloc_mtx);
234502ac6454SAndrew Thompson 	mt = malloc_desc2type("bus");	/* XXX M_BUS */
234602ac6454SAndrew Thompson 	mtx_unlock(&malloc_mtx);
234702ac6454SAndrew Thompson 	if (mt == NULL)
234802ac6454SAndrew Thompson 		return;
234902ac6454SAndrew Thompson 
235002ac6454SAndrew Thompson 	data = malloc(512, mt, M_NOWAIT);
235102ac6454SAndrew Thompson 	if (data == NULL)
235202ac6454SAndrew Thompson 		return;
235302ac6454SAndrew Thompson 
235402ac6454SAndrew Thompson 	/* String it all together. */
235502ac6454SAndrew Thompson 	snprintf(data, 1024,
235602ac6454SAndrew Thompson 	    "%s"
2357ee3e3ff5SAndrew Thompson 	    "%s "
235802ac6454SAndrew Thompson 	    "vendor=0x%04x "
235902ac6454SAndrew Thompson 	    "product=0x%04x "
236002ac6454SAndrew Thompson 	    "devclass=0x%02x "
236102ac6454SAndrew Thompson 	    "devsubclass=0x%02x "
236202ac6454SAndrew Thompson 	    "sernum=\"%s\" "
236302ac6454SAndrew Thompson 	    "at "
236402ac6454SAndrew Thompson 	    "port=%u "
236502ac6454SAndrew Thompson 	    "on "
2366ee3e3ff5SAndrew Thompson 	    "%s\n",
236702ac6454SAndrew Thompson 	    type,
2368ee3e3ff5SAndrew Thompson 	    udev->ugen_name,
236902ac6454SAndrew Thompson 	    UGETW(udev->ddesc.idVendor),
237002ac6454SAndrew Thompson 	    UGETW(udev->ddesc.idProduct),
237102ac6454SAndrew Thompson 	    udev->ddesc.bDeviceClass,
237202ac6454SAndrew Thompson 	    udev->ddesc.bDeviceSubClass,
23738755859aSAndrew Thompson #if USB_HAVE_STRINGS
237402ac6454SAndrew Thompson 	    udev->serial,
23758755859aSAndrew Thompson #else
23768755859aSAndrew Thompson 	    "",
23778755859aSAndrew Thompson #endif
237802ac6454SAndrew Thompson 	    udev->port_no,
23798755859aSAndrew Thompson 	    udev->parent_hub != NULL ?
23808755859aSAndrew Thompson 		udev->parent_hub->ugen_name :
238102ac6454SAndrew Thompson 		device_get_nameunit(device_get_parent(udev->bus->bdev)));
238262a5f394SAlexander Kabaev 
238302ac6454SAndrew Thompson 	devctl_queue_data(data);
238402ac6454SAndrew Thompson }
238502ac6454SAndrew Thompson 
238602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
238702ac6454SAndrew Thompson  *	usb2_fifo_free_wrap
238802ac6454SAndrew Thompson  *
238902ac6454SAndrew Thompson  * This function will free the FIFOs.
239002ac6454SAndrew Thompson  *
2391bdd41206SAndrew Thompson  * Description of "flag" argument: If the USB_UNCFG_FLAG_FREE_EP0 flag
2392bdd41206SAndrew Thompson  * is set and "iface_index" is set to "USB_IFACE_INDEX_ANY", we free
2393bdd41206SAndrew Thompson  * all FIFOs. If the USB_UNCFG_FLAG_FREE_EP0 flag is not set and
2394bdd41206SAndrew Thompson  * "iface_index" is set to "USB_IFACE_INDEX_ANY", we free all non
2395bdd41206SAndrew Thompson  * control endpoint FIFOs. If "iface_index" is not set to
2396bdd41206SAndrew Thompson  * "USB_IFACE_INDEX_ANY" the flag has no effect.
239702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
239802ac6454SAndrew Thompson static void
239902ac6454SAndrew Thompson usb2_fifo_free_wrap(struct usb2_device *udev,
240002ac6454SAndrew Thompson     uint8_t iface_index, uint8_t flag)
240102ac6454SAndrew Thompson {
240202ac6454SAndrew Thompson 	struct usb2_fifo *f;
240302ac6454SAndrew Thompson 	uint16_t i;
240402ac6454SAndrew Thompson 
240502ac6454SAndrew Thompson 	/*
240602ac6454SAndrew Thompson 	 * Free any USB FIFOs on the given interface:
240702ac6454SAndrew Thompson 	 */
240802ac6454SAndrew Thompson 	for (i = 0; i != USB_FIFO_MAX; i++) {
240902ac6454SAndrew Thompson 		f = udev->fifo[i];
241002ac6454SAndrew Thompson 		if (f == NULL) {
241102ac6454SAndrew Thompson 			continue;
241202ac6454SAndrew Thompson 		}
241302ac6454SAndrew Thompson 		/* Check if the interface index matches */
241402ac6454SAndrew Thompson 		if (iface_index == f->iface_index) {
241502ac6454SAndrew Thompson 			if (f->methods != &usb2_ugen_methods) {
241602ac6454SAndrew Thompson 				/*
241702ac6454SAndrew Thompson 				 * Don't free any non-generic FIFOs in
241802ac6454SAndrew Thompson 				 * this case.
241902ac6454SAndrew Thompson 				 */
242002ac6454SAndrew Thompson 				continue;
242102ac6454SAndrew Thompson 			}
242202ac6454SAndrew Thompson 			if ((f->dev_ep_index == 0) &&
242302ac6454SAndrew Thompson 			    (f->fs_xfer == NULL)) {
242402ac6454SAndrew Thompson 				/* no need to free this FIFO */
242502ac6454SAndrew Thompson 				continue;
242602ac6454SAndrew Thompson 			}
242702ac6454SAndrew Thompson 		} else if (iface_index == USB_IFACE_INDEX_ANY) {
242802ac6454SAndrew Thompson 			if ((f->methods == &usb2_ugen_methods) &&
2429bdd41206SAndrew Thompson 			    (f->dev_ep_index == 0) &&
2430bdd41206SAndrew Thompson 			    (!(flag & USB_UNCFG_FLAG_FREE_EP0)) &&
243102ac6454SAndrew Thompson 			    (f->fs_xfer == NULL)) {
243202ac6454SAndrew Thompson 				/* no need to free this FIFO */
243302ac6454SAndrew Thompson 				continue;
243402ac6454SAndrew Thompson 			}
243502ac6454SAndrew Thompson 		} else {
243602ac6454SAndrew Thompson 			/* no need to free this FIFO */
243702ac6454SAndrew Thompson 			continue;
243802ac6454SAndrew Thompson 		}
243902ac6454SAndrew Thompson 		/* free this FIFO */
244002ac6454SAndrew Thompson 		usb2_fifo_free(f);
244102ac6454SAndrew Thompson 	}
244202ac6454SAndrew Thompson }
24430f6e8f93SAndrew Thompson #endif
244402ac6454SAndrew Thompson 
244502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
244602ac6454SAndrew Thompson  *	usb2_peer_can_wakeup
244702ac6454SAndrew Thompson  *
244802ac6454SAndrew Thompson  * Return values:
244902ac6454SAndrew Thompson  * 0: Peer cannot do resume signalling.
245002ac6454SAndrew Thompson  * Else: Peer can do resume signalling.
245102ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
245202ac6454SAndrew Thompson uint8_t
245302ac6454SAndrew Thompson usb2_peer_can_wakeup(struct usb2_device *udev)
245402ac6454SAndrew Thompson {
245502ac6454SAndrew Thompson 	const struct usb2_config_descriptor *cdp;
245602ac6454SAndrew Thompson 
245702ac6454SAndrew Thompson 	cdp = udev->cdesc;
245802ac6454SAndrew Thompson 	if ((cdp != NULL) && (udev->flags.usb2_mode == USB_MODE_HOST)) {
245902ac6454SAndrew Thompson 		return (cdp->bmAttributes & UC_REMOTE_WAKEUP);
246002ac6454SAndrew Thompson 	}
246102ac6454SAndrew Thompson 	return (0);			/* not supported */
246202ac6454SAndrew Thompson }
2463bd216778SAndrew Thompson 
2464bd216778SAndrew Thompson void
2465bd216778SAndrew Thompson usb2_set_device_state(struct usb2_device *udev, enum usb_dev_state state)
2466bd216778SAndrew Thompson {
2467bd216778SAndrew Thompson 
2468bd216778SAndrew Thompson 	KASSERT(state < USB_STATE_MAX, ("invalid udev state"));
2469bd216778SAndrew Thompson 
2470bd216778SAndrew Thompson 	DPRINTF("udev %p state %s -> %s\n", udev,
24714d4fa6edSAndrew Thompson 	    usb2_statestr(udev->state), usb2_statestr(state));
2472bd216778SAndrew Thompson 	udev->state = state;
2473bd216778SAndrew Thompson }
2474bd216778SAndrew Thompson 
2475bd216778SAndrew Thompson int
2476bd216778SAndrew Thompson usb2_device_attached(struct usb2_device *udev)
2477bd216778SAndrew Thompson {
2478bd216778SAndrew Thompson 	return (udev->state > USB_STATE_DETACHED);
2479bd216778SAndrew Thompson }
2480