usb_device.c revision 195121
185587Sobrien/* $FreeBSD: head/sys/dev/usb/usb_device.c 195121 2009-06-27 21:23:30Z thompsa $ */
285587Sobrien/*-
385587Sobrien * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
485587Sobrien *
585587Sobrien * Redistribution and use in source and binary forms, with or without
685587Sobrien * modification, are permitted provided that the following conditions
785587Sobrien * are met:
885587Sobrien * 1. Redistributions of source code must retain the above copyright
985587Sobrien *    notice, this list of conditions and the following disclaimer.
1085587Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1185587Sobrien *    notice, this list of conditions and the following disclaimer in the
1285587Sobrien *    documentation and/or other materials provided with the distribution.
1385587Sobrien *
1485587Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1585587Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1685587Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1785587Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1885587Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1985587Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2085587Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2185587Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2285587Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2385587Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2485587Sobrien * SUCH DAMAGE.
2585587Sobrien */
2685587Sobrien
2785587Sobrien#include <sys/stdint.h>
2885587Sobrien#include <sys/stddef.h>
2985587Sobrien#include <sys/param.h>
3085587Sobrien#include <sys/queue.h>
3185587Sobrien#include <sys/types.h>
3285587Sobrien#include <sys/systm.h>
3385587Sobrien#include <sys/kernel.h>
3485587Sobrien#include <sys/bus.h>
3585587Sobrien#include <sys/linker_set.h>
3685587Sobrien#include <sys/module.h>
3785587Sobrien#include <sys/lock.h>
3885587Sobrien#include <sys/mutex.h>
3985587Sobrien#include <sys/condvar.h>
4085587Sobrien#include <sys/sysctl.h>
4185587Sobrien#include <sys/sx.h>
4285587Sobrien#include <sys/unistd.h>
4385587Sobrien#include <sys/callout.h>
4485587Sobrien#include <sys/malloc.h>
4585587Sobrien#include <sys/priv.h>
4685587Sobrien#include <sys/conf.h>
4785587Sobrien#include <sys/fcntl.h>
4885587Sobrien
4985587Sobrien#include <dev/usb/usb.h>
5085587Sobrien#include <dev/usb/usbdi.h>
5185587Sobrien#include <dev/usb/usbdi_util.h>
5285587Sobrien#include <dev/usb/usb_ioctl.h>
5385587Sobrien#include "usbdevs.h"
5485587Sobrien
5585587Sobrien#define	USB_DEBUG_VAR usb_debug
5685587Sobrien
5785587Sobrien#include <dev/usb/usb_core.h>
5885587Sobrien#include <dev/usb/usb_debug.h>
5985587Sobrien#include <dev/usb/usb_process.h>
6085587Sobrien#include <dev/usb/usb_device.h>
6185587Sobrien#include <dev/usb/usb_busdma.h>
6285587Sobrien#include <dev/usb/usb_transfer.h>
6385587Sobrien#include <dev/usb/usb_request.h>
6485587Sobrien#include <dev/usb/usb_dynamic.h>
6585587Sobrien#include <dev/usb/usb_hub.h>
6685587Sobrien#include <dev/usb/usb_util.h>
6785587Sobrien#include <dev/usb/usb_msctest.h>
6885587Sobrien#if USB_HAVE_UGEN
6985587Sobrien#include <dev/usb/usb_dev.h>
7085587Sobrien#include <dev/usb/usb_generic.h>
7185587Sobrien#endif
7285587Sobrien
7385587Sobrien#include <dev/usb/quirk/usb_quirk.h>
7485587Sobrien
7585587Sobrien#include <dev/usb/usb_controller.h>
7685587Sobrien#include <dev/usb/usb_bus.h>
7785587Sobrien
7885587Sobrien/* function prototypes  */
7985587Sobrien
8085587Sobrienstatic void	usb_init_endpoint(struct usb_device *, uint8_t,
8185587Sobrien		    struct usb_endpoint_descriptor *, struct usb_endpoint *);
8285587Sobrienstatic void	usb_unconfigure(struct usb_device *, uint8_t);
8385587Sobrienstatic void	usb_detach_device(struct usb_device *, uint8_t, uint8_t);
8485587Sobrienstatic void	usb_detach_device_sub(struct usb_device *, device_t *,
8585587Sobrien		    uint8_t);
8685587Sobrienstatic uint8_t	usb_probe_and_attach_sub(struct usb_device *,
8785587Sobrien		    struct usb_attach_arg *);
8885587Sobrienstatic void	usb_init_attach_arg(struct usb_device *,
8985587Sobrien		    struct usb_attach_arg *);
9085587Sobrienstatic void	usb_suspend_resume_sub(struct usb_device *, device_t,
9185587Sobrien		    uint8_t);
9285587Sobrienstatic void	usbd_clear_stall_proc(struct usb_proc_msg *_pm);
9385587Sobrienusb_error_t	usb_config_parse(struct usb_device *, uint8_t, uint8_t);
9485587Sobrienstatic void	usbd_set_device_strings(struct usb_device *);
9585587Sobrien#if USB_HAVE_UGEN
9685587Sobrienstatic void	usb_notify_addq(const char *type, struct usb_device *);
9785587Sobrienstatic void	usb_fifo_free_wrap(struct usb_device *, uint8_t, uint8_t);
9885587Sobrienstatic struct cdev *usb_make_dev(struct usb_device *, int, int);
9985587Sobrienstatic void	usb_cdev_create(struct usb_device *);
10085587Sobrienstatic void	usb_cdev_free(struct usb_device *);
10185587Sobrienstatic void	usb_cdev_cleanup(void *);
10285587Sobrien#endif
10385587Sobrien
10485587Sobrien/* This variable is global to allow easy access to it: */
10585587Sobrien
10685587Sobrienint	usb_template = 0;
10785587Sobrien
10885587SobrienSYSCTL_INT(_hw_usb, OID_AUTO, template, CTLFLAG_RW,
10985587Sobrien    &usb_template, 0, "Selected USB device side template");
11085587Sobrien
11185587Sobrienstatic const char* statestr[USB_STATE_MAX] = {
11285587Sobrien	[USB_STATE_DETACHED]	= "DETACHED",
11385587Sobrien	[USB_STATE_ATTACHED]	= "ATTACHED",
11485587Sobrien	[USB_STATE_POWERED]	= "POWERED",
11585587Sobrien	[USB_STATE_ADDRESSED]	= "ADDRESSED",
11685587Sobrien	[USB_STATE_CONFIGURED]	= "CONFIGURED",
11785587Sobrien};
11885587Sobrien
11985587Sobrienconst char *
12085587Sobrienusb_statestr(enum usb_dev_state state)
12185587Sobrien{
12285587Sobrien	return ((state < USB_STATE_MAX) ? statestr[state] : "UNKNOWN");
12385587Sobrien}
12485587Sobrien
12585587Sobrien/*------------------------------------------------------------------------*
12685587Sobrien *	usbd_get_ep_by_addr
12785587Sobrien *
12885587Sobrien * This function searches for an USB ep by endpoint address and
12985587Sobrien * direction.
13085587Sobrien *
13185587Sobrien * Returns:
13285587Sobrien * NULL: Failure
13385587Sobrien * Else: Success
13485587Sobrien *------------------------------------------------------------------------*/
13585587Sobrienstruct usb_endpoint *
13685587Sobrienusbd_get_ep_by_addr(struct usb_device *udev, uint8_t ea_val)
13785587Sobrien{
13885587Sobrien	struct usb_endpoint *ep = udev->endpoints;
13985587Sobrien	struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max;
14085587Sobrien	enum {
14185587Sobrien		EA_MASK = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR),
14285587Sobrien	};
14385587Sobrien
14485587Sobrien	/*
14585587Sobrien	 * According to the USB specification not all bits are used
14685587Sobrien	 * for the endpoint address. Keep defined bits only:
14785587Sobrien	 */
14885587Sobrien	ea_val &= EA_MASK;
14985587Sobrien
15085587Sobrien	/*
15185587Sobrien	 * Iterate accross all the USB endpoints searching for a match
15285587Sobrien	 * based on the endpoint address:
15385587Sobrien	 */
15485587Sobrien	for (; ep != ep_end; ep++) {
15585587Sobrien
15685587Sobrien		if (ep->edesc == NULL) {
15785587Sobrien			continue;
15885587Sobrien		}
15985587Sobrien		/* do the mask and check the value */
16085587Sobrien		if ((ep->edesc->bEndpointAddress & EA_MASK) == ea_val) {
16185587Sobrien			goto found;
16285587Sobrien		}
16385587Sobrien	}
16485587Sobrien
16585587Sobrien	/*
16685587Sobrien	 * The default endpoint is always present and is checked separately:
16785587Sobrien	 */
16885587Sobrien	if ((udev->default_ep.edesc) &&
16985587Sobrien	    ((udev->default_ep.edesc->bEndpointAddress & EA_MASK) == ea_val)) {
17085587Sobrien		ep = &udev->default_ep;
17185587Sobrien		goto found;
17285587Sobrien	}
17385587Sobrien	return (NULL);
17485587Sobrien
17585587Sobrienfound:
17685587Sobrien	return (ep);
17785587Sobrien}
17885587Sobrien
17985587Sobrien/*------------------------------------------------------------------------*
18085587Sobrien *	usbd_get_endpoint
18185587Sobrien *
18285587Sobrien * This function searches for an USB endpoint based on the information
18385587Sobrien * given by the passed "struct usb_config" pointer.
18485587Sobrien *
18585587Sobrien * Return values:
18685587Sobrien * NULL: No match.
18785587Sobrien * Else: Pointer to "struct usb_endpoint".
18885587Sobrien *------------------------------------------------------------------------*/
18985587Sobrienstruct usb_endpoint *
19085587Sobrienusbd_get_endpoint(struct usb_device *udev, uint8_t iface_index,
19185587Sobrien    const struct usb_config *setup)
19285587Sobrien{
19385587Sobrien	struct usb_endpoint *ep = udev->endpoints;
19485587Sobrien	struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max;
19585587Sobrien	uint8_t index = setup->ep_index;
19685587Sobrien	uint8_t ea_mask;
19785587Sobrien	uint8_t ea_val;
19885587Sobrien	uint8_t type_mask;
19985587Sobrien	uint8_t type_val;
20085587Sobrien
20185587Sobrien	DPRINTFN(10, "udev=%p iface_index=%d address=0x%x "
20285587Sobrien	    "type=0x%x dir=0x%x index=%d\n",
20385587Sobrien	    udev, iface_index, setup->endpoint,
20485587Sobrien	    setup->type, setup->direction, setup->ep_index);
20585587Sobrien
20685587Sobrien	/* check USB mode */
20785587Sobrien
20885587Sobrien	if (setup->usb_mode != USB_MODE_DUAL &&
20985587Sobrien	    udev->flags.usb_mode != setup->usb_mode) {
21085587Sobrien		/* wrong mode - no endpoint */
211272918Spfg		return (NULL);
21285587Sobrien	}
21385587Sobrien
21485587Sobrien	/* setup expected endpoint direction mask and value */
21585587Sobrien
21685587Sobrien	if (setup->direction == UE_DIR_RX) {
21785587Sobrien		ea_mask = (UE_DIR_IN | UE_DIR_OUT);
21885587Sobrien		ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ?
21985587Sobrien		    UE_DIR_OUT : UE_DIR_IN;
22085587Sobrien	} else if (setup->direction == UE_DIR_TX) {
22185587Sobrien		ea_mask = (UE_DIR_IN | UE_DIR_OUT);
22285587Sobrien		ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ?
22385587Sobrien		    UE_DIR_IN : UE_DIR_OUT;
22485587Sobrien	} else if (setup->direction == UE_DIR_ANY) {
22585587Sobrien		/* match any endpoint direction */
22685587Sobrien		ea_mask = 0;
22785587Sobrien		ea_val = 0;
22885587Sobrien	} else {
22985587Sobrien		/* match the given endpoint direction */
23085587Sobrien		ea_mask = (UE_DIR_IN | UE_DIR_OUT);
23185587Sobrien		ea_val = (setup->direction & (UE_DIR_IN | UE_DIR_OUT));
23285587Sobrien	}
23385587Sobrien
23485587Sobrien	/* setup expected endpoint address */
23585587Sobrien
23685587Sobrien	if (setup->endpoint == UE_ADDR_ANY) {
23785587Sobrien		/* match any endpoint address */
23885587Sobrien	} else {
23985587Sobrien		/* match the given endpoint address */
24085587Sobrien		ea_mask |= UE_ADDR;
24185587Sobrien		ea_val |= (setup->endpoint & UE_ADDR);
24285587Sobrien	}
24385587Sobrien
24485587Sobrien	/* setup expected endpoint type */
24585587Sobrien
24685587Sobrien	if (setup->type == UE_BULK_INTR) {
24785587Sobrien		/* this will match BULK and INTERRUPT endpoints */
24885587Sobrien		type_mask = 2;
24985587Sobrien		type_val = 2;
25085587Sobrien	} else if (setup->type == UE_TYPE_ANY) {
25185587Sobrien		/* match any endpoint type */
25285587Sobrien		type_mask = 0;
25385587Sobrien		type_val = 0;
25485587Sobrien	} else {
25585587Sobrien		/* match the given endpoint type */
25685587Sobrien		type_mask = UE_XFERTYPE;
25785587Sobrien		type_val = (setup->type & UE_XFERTYPE);
25885587Sobrien	}
25985587Sobrien
26085587Sobrien	/*
26185587Sobrien	 * Iterate accross all the USB endpoints searching for a match
26285587Sobrien	 * based on the endpoint address. Note that we are searching
26385587Sobrien	 * the endpoints from the beginning of the "udev->endpoints" array.
26485587Sobrien	 */
26585587Sobrien	for (; ep != ep_end; ep++) {
26685587Sobrien
26785587Sobrien		if ((ep->edesc == NULL) ||
26885587Sobrien		    (ep->iface_index != iface_index)) {
26985587Sobrien			continue;
27085587Sobrien		}
27185587Sobrien		/* do the masks and check the values */
27285587Sobrien
27385587Sobrien		if (((ep->edesc->bEndpointAddress & ea_mask) == ea_val) &&
27485587Sobrien		    ((ep->edesc->bmAttributes & type_mask) == type_val)) {
27585587Sobrien			if (!index--) {
27685587Sobrien				goto found;
27785587Sobrien			}
27885587Sobrien		}
27985587Sobrien	}
28085587Sobrien
28185587Sobrien	/*
28285587Sobrien	 * Match against default endpoint last, so that "any endpoint", "any
28385587Sobrien	 * address" and "any direction" returns the first endpoint of the
28485587Sobrien	 * interface. "iface_index" and "direction" is ignored:
28585587Sobrien	 */
28685587Sobrien	if ((udev->default_ep.edesc) &&
28785587Sobrien	    ((udev->default_ep.edesc->bEndpointAddress & ea_mask) == ea_val) &&
28885587Sobrien	    ((udev->default_ep.edesc->bmAttributes & type_mask) == type_val) &&
28985587Sobrien	    (!index)) {
29085587Sobrien		ep = &udev->default_ep;
29185587Sobrien		goto found;
29285587Sobrien	}
29385587Sobrien	return (NULL);
29485587Sobrien
29585587Sobrienfound:
29685587Sobrien	return (ep);
29785587Sobrien}
29885587Sobrien
29985587Sobrien/*------------------------------------------------------------------------*
30085587Sobrien *	usbd_interface_count
30185587Sobrien *
30285587Sobrien * This function stores the number of USB interfaces excluding
30385587Sobrien * alternate settings, which the USB config descriptor reports into
30485587Sobrien * the unsigned 8-bit integer pointed to by "count".
30585587Sobrien *
30685587Sobrien * Returns:
30785587Sobrien *    0: Success
30885587Sobrien * Else: Failure
30985587Sobrien *------------------------------------------------------------------------*/
31085587Sobrienusb_error_t
31185587Sobrienusbd_interface_count(struct usb_device *udev, uint8_t *count)
31285587Sobrien{
31385587Sobrien	if (udev->cdesc == NULL) {
31485587Sobrien		*count = 0;
31585587Sobrien		return (USB_ERR_NOT_CONFIGURED);
31685587Sobrien	}
31785587Sobrien	*count = udev->ifaces_max;
31885587Sobrien	return (USB_ERR_NORMAL_COMPLETION);
31985587Sobrien}
32085587Sobrien
32185587Sobrien
32285587Sobrien/*------------------------------------------------------------------------*
32385587Sobrien *	usb_init_endpoint
32485587Sobrien *
32585587Sobrien * This function will initialise the USB endpoint structure pointed to by
32685587Sobrien * the "endpoint" argument. The structure pointed to by "endpoint" must be
32785587Sobrien * zeroed before calling this function.
32885587Sobrien *------------------------------------------------------------------------*/
32985587Sobrienstatic void
33085587Sobrienusb_init_endpoint(struct usb_device *udev, uint8_t iface_index,
33185587Sobrien    struct usb_endpoint_descriptor *edesc, struct usb_endpoint *ep)
33285587Sobrien{
33385587Sobrien	struct usb_bus_methods *methods;
33485587Sobrien
33585587Sobrien	methods = udev->bus->methods;
33685587Sobrien
33785587Sobrien	(methods->endpoint_init) (udev, edesc, ep);
33885587Sobrien
33985587Sobrien	/* initialise USB endpoint structure */
34085587Sobrien	ep->edesc = edesc;
34185587Sobrien	ep->iface_index = iface_index;
34285587Sobrien	TAILQ_INIT(&ep->endpoint_q.head);
34385587Sobrien	ep->endpoint_q.command = &usbd_pipe_start;
34485587Sobrien
34585587Sobrien	/* the pipe is not supported by the hardware */
34685587Sobrien 	if (ep->methods == NULL)
34785587Sobrien		return;
34885587Sobrien
34985587Sobrien	/* clear stall, if any */
35085587Sobrien	if (methods->clear_stall != NULL) {
35185587Sobrien		USB_BUS_LOCK(udev->bus);
35285587Sobrien		(methods->clear_stall) (udev, ep);
35385587Sobrien		USB_BUS_UNLOCK(udev->bus);
35485587Sobrien	}
35585587Sobrien}
35685587Sobrien
35785587Sobrien/*-----------------------------------------------------------------------*
35885587Sobrien *	usb_endpoint_foreach
35985587Sobrien *
36085587Sobrien * This function will iterate all the USB endpoints except the control
36185587Sobrien * endpoint. This function is NULL safe.
36285587Sobrien *
36385587Sobrien * Return values:
36485587Sobrien * NULL: End of USB endpoints
36585587Sobrien * Else: Pointer to next USB endpoint
36685587Sobrien *------------------------------------------------------------------------*/
36785587Sobrienstruct usb_endpoint *
36885587Sobrienusb_endpoint_foreach(struct usb_device *udev, struct usb_endpoint *ep)
36985587Sobrien{
37085587Sobrien	struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max;
37185587Sobrien
37285587Sobrien	/* be NULL safe */
37385587Sobrien	if (udev == NULL)
37485587Sobrien		return (NULL);
37585587Sobrien
37685587Sobrien	/* get next endpoint */
37785587Sobrien	if (ep == NULL)
37885587Sobrien		ep = udev->endpoints;
37985587Sobrien	else
38085587Sobrien		ep++;
38185587Sobrien
38285587Sobrien	/* find next allocated ep */
38385587Sobrien	while (ep != ep_end) {
38485587Sobrien		if (ep->edesc != NULL)
38585587Sobrien			return (ep);
38685587Sobrien		ep++;
38785587Sobrien	}
38885587Sobrien	return (NULL);
38985587Sobrien}
39085587Sobrien
39185587Sobrien/*------------------------------------------------------------------------*
39285587Sobrien *	usb_unconfigure
39385587Sobrien *
39485587Sobrien * This function will free all USB interfaces and USB endpoints belonging
39585587Sobrien * to an USB device.
39685587Sobrien *
39785587Sobrien * Flag values, see "USB_UNCFG_FLAG_XXX".
39885587Sobrien *------------------------------------------------------------------------*/
39985587Sobrienstatic void
40085587Sobrienusb_unconfigure(struct usb_device *udev, uint8_t flag)
40185587Sobrien{
40285587Sobrien	uint8_t do_unlock;
40385587Sobrien
40485587Sobrien	/* automatic locking */
40585587Sobrien	if (sx_xlocked(udev->default_sx + 1)) {
40685587Sobrien		do_unlock = 0;
40785587Sobrien	} else {
40885587Sobrien		do_unlock = 1;
40985587Sobrien		sx_xlock(udev->default_sx + 1);
41085587Sobrien	}
41185587Sobrien
41285587Sobrien	/* detach all interface drivers */
41385587Sobrien	usb_detach_device(udev, USB_IFACE_INDEX_ANY, flag);
41485587Sobrien
41585587Sobrien#if USB_HAVE_UGEN
41685587Sobrien	/* free all FIFOs except control endpoint FIFOs */
41785587Sobrien	usb_fifo_free_wrap(udev, USB_IFACE_INDEX_ANY, flag);
41885587Sobrien
41985587Sobrien	/*
42085587Sobrien	 * Free all cdev's, if any.
42185587Sobrien	 */
42285587Sobrien	usb_cdev_free(udev);
42385587Sobrien#endif
42485587Sobrien
42585587Sobrien#if USB_HAVE_COMPAT_LINUX
42685587Sobrien	/* free Linux compat device, if any */
42785587Sobrien	if (udev->linux_endpoint_start) {
42885587Sobrien		usb_linux_free_device(udev);
42985587Sobrien		udev->linux_endpoint_start = NULL;
43085587Sobrien	}
43185587Sobrien#endif
43285587Sobrien
43385587Sobrien	usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_FREE);
43485587Sobrien
43585587Sobrien	/* free "cdesc" after "ifaces" and "endpoints", if any */
43685587Sobrien	if (udev->cdesc != NULL) {
43785587Sobrien		if (udev->flags.usb_mode != USB_MODE_DEVICE)
43885587Sobrien			free(udev->cdesc, M_USB);
43985587Sobrien		udev->cdesc = NULL;
44085587Sobrien	}
44185587Sobrien	/* set unconfigured state */
44285587Sobrien	udev->curr_config_no = USB_UNCONFIG_NO;
44385587Sobrien	udev->curr_config_index = USB_UNCONFIG_INDEX;
44485587Sobrien
44585587Sobrien	if (do_unlock) {
44685587Sobrien		sx_unlock(udev->default_sx + 1);
44785587Sobrien	}
44885587Sobrien}
44985587Sobrien
45085587Sobrien/*------------------------------------------------------------------------*
45185587Sobrien *	usbd_set_config_index
45285587Sobrien *
45385587Sobrien * This function selects configuration by index, independent of the
45485587Sobrien * actual configuration number. This function should not be used by
45585587Sobrien * USB drivers.
45685587Sobrien *
45785587Sobrien * Returns:
45885587Sobrien *    0: Success
45985587Sobrien * Else: Failure
46085587Sobrien *------------------------------------------------------------------------*/
46185587Sobrienusb_error_t
46285587Sobrienusbd_set_config_index(struct usb_device *udev, uint8_t index)
46385587Sobrien{
46485587Sobrien	struct usb_status ds;
46585587Sobrien	struct usb_config_descriptor *cdp;
46685587Sobrien	uint16_t power;
46785587Sobrien	uint16_t max_power;
46885587Sobrien	uint8_t selfpowered;
46985587Sobrien	uint8_t do_unlock;
47085587Sobrien	usb_error_t err;
47185587Sobrien
47285587Sobrien	DPRINTFN(6, "udev=%p index=%d\n", udev, index);
47385587Sobrien
47485587Sobrien	/* automatic locking */
47585587Sobrien	if (sx_xlocked(udev->default_sx + 1)) {
47685587Sobrien		do_unlock = 0;
47785587Sobrien	} else {
47885587Sobrien		do_unlock = 1;
47985587Sobrien		sx_xlock(udev->default_sx + 1);
48085587Sobrien	}
48185587Sobrien
48285587Sobrien	usb_unconfigure(udev, USB_UNCFG_FLAG_FREE_SUBDEV);
48385587Sobrien
48485587Sobrien	if (index == USB_UNCONFIG_INDEX) {
48585587Sobrien		/*
48685587Sobrien		 * Leave unallocated when unconfiguring the
48785587Sobrien		 * device. "usb_unconfigure()" will also reset
48885587Sobrien		 * the current config number and index.
48985587Sobrien		 */
49085587Sobrien		err = usbd_req_set_config(udev, NULL, USB_UNCONFIG_NO);
49185587Sobrien		if (udev->state == USB_STATE_CONFIGURED)
49285587Sobrien			usb_set_device_state(udev, USB_STATE_ADDRESSED);
49385587Sobrien		goto done;
49485587Sobrien	}
49585587Sobrien	/* get the full config descriptor */
49685587Sobrien	if (udev->flags.usb_mode == USB_MODE_DEVICE) {
49785587Sobrien		/* save some memory */
49885587Sobrien		err = usbd_req_get_descriptor_ptr(udev, &cdp,
49985587Sobrien		    (UDESC_CONFIG << 8) | index);
50085587Sobrien	} else {
50185587Sobrien		/* normal request */
50285587Sobrien		err = usbd_req_get_config_desc_full(udev,
50385587Sobrien		    NULL, &cdp, M_USB, index);
50485587Sobrien	}
50585587Sobrien	if (err) {
50685587Sobrien		goto done;
50785587Sobrien	}
50885587Sobrien	/* set the new config descriptor */
50985587Sobrien
51085587Sobrien	udev->cdesc = cdp;
51185587Sobrien
51285587Sobrien	/* Figure out if the device is self or bus powered. */
51385587Sobrien	selfpowered = 0;
51485587Sobrien	if ((!udev->flags.uq_bus_powered) &&
51585587Sobrien	    (cdp->bmAttributes & UC_SELF_POWERED) &&
51685587Sobrien	    (udev->flags.usb_mode == USB_MODE_HOST)) {
51785587Sobrien		/* May be self powered. */
51885587Sobrien		if (cdp->bmAttributes & UC_BUS_POWERED) {
51985587Sobrien			/* Must ask device. */
52085587Sobrien			err = usbd_req_get_device_status(udev, NULL, &ds);
52185587Sobrien			if (err) {
52285587Sobrien				DPRINTFN(0, "could not read "
52385587Sobrien				    "device status: %s\n",
52485587Sobrien				    usbd_errstr(err));
52585587Sobrien			} else if (UGETW(ds.wStatus) & UDS_SELF_POWERED) {
52685587Sobrien				selfpowered = 1;
52785587Sobrien			}
52885587Sobrien			DPRINTF("status=0x%04x \n",
52985587Sobrien				UGETW(ds.wStatus));
530		} else
531			selfpowered = 1;
532	}
533	DPRINTF("udev=%p cdesc=%p (addr %d) cno=%d attr=0x%02x, "
534	    "selfpowered=%d, power=%d\n",
535	    udev, cdp,
536	    udev->address, cdp->bConfigurationValue, cdp->bmAttributes,
537	    selfpowered, cdp->bMaxPower * 2);
538
539	/* Check if we have enough power. */
540	power = cdp->bMaxPower * 2;
541
542	if (udev->parent_hub) {
543		max_power = udev->parent_hub->hub->portpower;
544	} else {
545		max_power = USB_MAX_POWER;
546	}
547
548	if (power > max_power) {
549		DPRINTFN(0, "power exceeded %d > %d\n", power, max_power);
550		err = USB_ERR_NO_POWER;
551		goto done;
552	}
553	/* Only update "self_powered" in USB Host Mode */
554	if (udev->flags.usb_mode == USB_MODE_HOST) {
555		udev->flags.self_powered = selfpowered;
556	}
557	udev->power = power;
558	udev->curr_config_no = cdp->bConfigurationValue;
559	udev->curr_config_index = index;
560	usb_set_device_state(udev, USB_STATE_CONFIGURED);
561
562	/* Set the actual configuration value. */
563	err = usbd_req_set_config(udev, NULL, cdp->bConfigurationValue);
564	if (err) {
565		goto done;
566	}
567
568	err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_ALLOC);
569	if (err) {
570		goto done;
571	}
572
573	err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_INIT);
574	if (err) {
575		goto done;
576	}
577
578#if USB_HAVE_UGEN
579	/* create device nodes for each endpoint */
580	usb_cdev_create(udev);
581#endif
582
583done:
584	DPRINTF("error=%s\n", usbd_errstr(err));
585	if (err) {
586		usb_unconfigure(udev, USB_UNCFG_FLAG_FREE_SUBDEV);
587	}
588	if (do_unlock) {
589		sx_unlock(udev->default_sx + 1);
590	}
591	return (err);
592}
593
594/*------------------------------------------------------------------------*
595 *	usb_config_parse
596 *
597 * This function will allocate and free USB interfaces and USB endpoints,
598 * parse the USB configuration structure and initialise the USB endpoints
599 * and interfaces. If "iface_index" is not equal to
600 * "USB_IFACE_INDEX_ANY" then the "cmd" parameter is the
601 * alternate_setting to be selected for the given interface. Else the
602 * "cmd" parameter is defined by "USB_CFG_XXX". "iface_index" can be
603 * "USB_IFACE_INDEX_ANY" or a valid USB interface index. This function
604 * is typically called when setting the configuration or when setting
605 * an alternate interface.
606 *
607 * Returns:
608 *    0: Success
609 * Else: Failure
610 *------------------------------------------------------------------------*/
611usb_error_t
612usb_config_parse(struct usb_device *udev, uint8_t iface_index, uint8_t cmd)
613{
614	struct usb_idesc_parse_state ips;
615	struct usb_interface_descriptor *id;
616	struct usb_endpoint_descriptor *ed;
617	struct usb_interface *iface;
618	struct usb_endpoint *ep;
619	usb_error_t err;
620	uint8_t ep_curr;
621	uint8_t ep_max;
622	uint8_t temp;
623	uint8_t do_init;
624	uint8_t alt_index;
625
626	if (iface_index != USB_IFACE_INDEX_ANY) {
627		/* parameter overload */
628		alt_index = cmd;
629		cmd = USB_CFG_INIT;
630	} else {
631		/* not used */
632		alt_index = 0;
633	}
634
635	err = 0;
636
637	DPRINTFN(5, "iface_index=%d cmd=%d\n",
638	    iface_index, cmd);
639
640	if (cmd == USB_CFG_FREE)
641		goto cleanup;
642
643	if (cmd == USB_CFG_INIT) {
644		sx_assert(udev->default_sx + 1, SA_LOCKED);
645
646		/* check for in-use endpoints */
647
648		ep = udev->endpoints;
649		ep_max = udev->endpoints_max;
650		while (ep_max--) {
651			/* look for matching endpoints */
652			if ((iface_index == USB_IFACE_INDEX_ANY) ||
653			    (iface_index == ep->iface_index)) {
654				if (ep->refcount != 0) {
655					/*
656					 * This typically indicates a
657					 * more serious error.
658					 */
659					err = USB_ERR_IN_USE;
660				} else {
661					/* reset endpoint */
662					memset(ep, 0, sizeof(*ep));
663					/* make sure we don't zero the endpoint again */
664					ep->iface_index = USB_IFACE_INDEX_ANY;
665				}
666			}
667			ep++;
668		}
669
670		if (err)
671			return (err);
672	}
673
674	memset(&ips, 0, sizeof(ips));
675
676	ep_curr = 0;
677	ep_max = 0;
678
679	while ((id = usb_idesc_foreach(udev->cdesc, &ips))) {
680
681		/* check for interface overflow */
682		if (ips.iface_index == USB_IFACE_MAX)
683			break;			/* crazy */
684
685		iface = udev->ifaces + ips.iface_index;
686
687		/* check for specific interface match */
688
689		if (cmd == USB_CFG_INIT) {
690			if ((iface_index != USB_IFACE_INDEX_ANY) &&
691			    (iface_index != ips.iface_index)) {
692				/* wrong interface */
693				do_init = 0;
694			} else if (alt_index != ips.iface_index_alt) {
695				/* wrong alternate setting */
696				do_init = 0;
697			} else {
698				/* initialise interface */
699				do_init = 1;
700			}
701		} else
702			do_init = 0;
703
704		/* check for new interface */
705		if (ips.iface_index_alt == 0) {
706			/* update current number of endpoints */
707			ep_curr = ep_max;
708		}
709		/* check for init */
710		if (do_init) {
711			/* setup the USB interface structure */
712			iface->idesc = id;
713			/* default setting */
714			iface->parent_iface_index = USB_IFACE_INDEX_ANY;
715			/* set alternate index */
716			iface->alt_index = alt_index;
717		}
718
719		DPRINTFN(5, "found idesc nendpt=%d\n", id->bNumEndpoints);
720
721		ed = (struct usb_endpoint_descriptor *)id;
722
723		temp = ep_curr;
724
725		/* iterate all the endpoint descriptors */
726		while ((ed = usb_edesc_foreach(udev->cdesc, ed))) {
727
728			if (temp == USB_EP_MAX)
729				break;			/* crazy */
730
731			ep = udev->endpoints + temp;
732
733			if (do_init) {
734				usb_init_endpoint(udev,
735				    ips.iface_index, ed, ep);
736			}
737
738			temp ++;
739
740			/* find maximum number of endpoints */
741			if (ep_max < temp)
742				ep_max = temp;
743
744			/* optimalisation */
745			id = (struct usb_interface_descriptor *)ed;
746		}
747	}
748
749	/* NOTE: It is valid to have no interfaces and no endpoints! */
750
751	if (cmd == USB_CFG_ALLOC) {
752		udev->ifaces_max = ips.iface_index;
753		udev->ifaces = NULL;
754		if (udev->ifaces_max != 0) {
755			udev->ifaces = malloc(sizeof(*iface) * udev->ifaces_max,
756			        M_USB, M_WAITOK | M_ZERO);
757			if (udev->ifaces == NULL) {
758				err = USB_ERR_NOMEM;
759				goto done;
760			}
761		}
762		if (ep_max != 0) {
763			udev->endpoints = malloc(sizeof(*ep) * ep_max,
764			        M_USB, M_WAITOK | M_ZERO);
765			if (udev->endpoints == NULL) {
766				err = USB_ERR_NOMEM;
767				goto done;
768			}
769		} else {
770			udev->endpoints = NULL;
771		}
772		USB_BUS_LOCK(udev->bus);
773		udev->endpoints_max = ep_max;
774		/* reset any ongoing clear-stall */
775		udev->ep_curr = NULL;
776		USB_BUS_UNLOCK(udev->bus);
777	}
778
779done:
780	if (err) {
781		if (cmd == USB_CFG_ALLOC) {
782cleanup:
783			USB_BUS_LOCK(udev->bus);
784			udev->endpoints_max = 0;
785			/* reset any ongoing clear-stall */
786			udev->ep_curr = NULL;
787			USB_BUS_UNLOCK(udev->bus);
788
789			/* cleanup */
790			if (udev->ifaces != NULL)
791				free(udev->ifaces, M_USB);
792			if (udev->endpoints != NULL)
793				free(udev->endpoints, M_USB);
794
795			udev->ifaces = NULL;
796			udev->endpoints = NULL;
797			udev->ifaces_max = 0;
798		}
799	}
800	return (err);
801}
802
803/*------------------------------------------------------------------------*
804 *	usbd_set_alt_interface_index
805 *
806 * This function will select an alternate interface index for the
807 * given interface index. The interface should not be in use when this
808 * function is called. That means there should not be any open USB
809 * transfers. Else an error is returned. If the alternate setting is
810 * already set this function will simply return success. This function
811 * is called in Host mode and Device mode!
812 *
813 * Returns:
814 *    0: Success
815 * Else: Failure
816 *------------------------------------------------------------------------*/
817usb_error_t
818usbd_set_alt_interface_index(struct usb_device *udev,
819    uint8_t iface_index, uint8_t alt_index)
820{
821	struct usb_interface *iface = usbd_get_iface(udev, iface_index);
822	usb_error_t err;
823	uint8_t do_unlock;
824
825	/* automatic locking */
826	if (sx_xlocked(udev->default_sx + 1)) {
827		do_unlock = 0;
828	} else {
829		do_unlock = 1;
830		sx_xlock(udev->default_sx + 1);
831	}
832	if (iface == NULL) {
833		err = USB_ERR_INVAL;
834		goto done;
835	}
836	if (udev->flags.usb_mode == USB_MODE_DEVICE) {
837		usb_detach_device(udev, iface_index,
838		    USB_UNCFG_FLAG_FREE_SUBDEV);
839	} else {
840		if (iface->alt_index == alt_index) {
841			/*
842			 * Optimise away duplicate setting of
843			 * alternate setting in USB Host Mode!
844			 */
845			err = 0;
846			goto done;
847		}
848	}
849#if USB_HAVE_UGEN
850	/*
851	 * Free all generic FIFOs for this interface, except control
852	 * endpoint FIFOs:
853	 */
854	usb_fifo_free_wrap(udev, iface_index, 0);
855#endif
856
857	err = usb_config_parse(udev, iface_index, alt_index);
858	if (err) {
859		goto done;
860	}
861	err = usbd_req_set_alt_interface_no(udev, NULL, iface_index,
862	    iface->idesc->bAlternateSetting);
863
864done:
865	if (do_unlock) {
866		sx_unlock(udev->default_sx + 1);
867	}
868	return (err);
869}
870
871/*------------------------------------------------------------------------*
872 *	usbd_set_endpoint_stall
873 *
874 * This function is used to make a BULK or INTERRUPT endpoint
875 * send STALL tokens.
876 *
877 * Returns:
878 *    0: Success
879 * Else: Failure
880 *------------------------------------------------------------------------*/
881usb_error_t
882usbd_set_endpoint_stall(struct usb_device *udev, struct usb_endpoint *ep,
883    uint8_t do_stall)
884{
885	struct usb_xfer *xfer;
886	uint8_t et;
887	uint8_t was_stalled;
888
889	if (ep == NULL) {
890		/* nothing to do */
891		DPRINTF("Cannot find endpoint\n");
892		/*
893		 * Pretend that the clear or set stall request is
894		 * successful else some USB host stacks can do
895		 * strange things, especially when a control endpoint
896		 * stalls.
897		 */
898		return (0);
899	}
900	et = (ep->edesc->bmAttributes & UE_XFERTYPE);
901
902	if ((et != UE_BULK) &&
903	    (et != UE_INTERRUPT)) {
904		/*
905	         * Should not stall control
906	         * nor isochronous endpoints.
907	         */
908		DPRINTF("Invalid endpoint\n");
909		return (0);
910	}
911	USB_BUS_LOCK(udev->bus);
912
913	/* store current stall state */
914	was_stalled = ep->is_stalled;
915
916	/* check for no change */
917	if (was_stalled && do_stall) {
918		/* if the endpoint is already stalled do nothing */
919		USB_BUS_UNLOCK(udev->bus);
920		DPRINTF("No change\n");
921		return (0);
922	}
923	/* set stalled state */
924	ep->is_stalled = 1;
925
926	if (do_stall || (!was_stalled)) {
927		if (!was_stalled) {
928			/* lookup the current USB transfer, if any */
929			xfer = ep->endpoint_q.curr;
930		} else {
931			xfer = NULL;
932		}
933
934		/*
935		 * If "xfer" is non-NULL the "set_stall" method will
936		 * complete the USB transfer like in case of a timeout
937		 * setting the error code "USB_ERR_STALLED".
938		 */
939		(udev->bus->methods->set_stall) (udev, xfer, ep, &do_stall);
940	}
941	if (!do_stall) {
942		ep->toggle_next = 0;	/* reset data toggle */
943		ep->is_stalled = 0;	/* clear stalled state */
944
945		(udev->bus->methods->clear_stall) (udev, ep);
946
947		/* start up the current or next transfer, if any */
948		usb_command_wrapper(&ep->endpoint_q, ep->endpoint_q.curr);
949	}
950	USB_BUS_UNLOCK(udev->bus);
951	return (0);
952}
953
954/*------------------------------------------------------------------------*
955 *	usb_reset_iface_endpoints - used in USB device side mode
956 *------------------------------------------------------------------------*/
957usb_error_t
958usb_reset_iface_endpoints(struct usb_device *udev, uint8_t iface_index)
959{
960	struct usb_endpoint *ep;
961	struct usb_endpoint *ep_end;
962	usb_error_t err;
963
964	ep = udev->endpoints;
965	ep_end = udev->endpoints + udev->endpoints_max;
966
967	for (; ep != ep_end; ep++) {
968
969		if ((ep->edesc == NULL) ||
970		    (ep->iface_index != iface_index)) {
971			continue;
972		}
973		/* simulate a clear stall from the peer */
974		err = usbd_set_endpoint_stall(udev, ep, 0);
975		if (err) {
976			/* just ignore */
977		}
978	}
979	return (0);
980}
981
982/*------------------------------------------------------------------------*
983 *	usb_detach_device_sub
984 *
985 * This function will try to detach an USB device. If it fails a panic
986 * will result.
987 *
988 * Flag values, see "USB_UNCFG_FLAG_XXX".
989 *------------------------------------------------------------------------*/
990static void
991usb_detach_device_sub(struct usb_device *udev, device_t *ppdev,
992    uint8_t flag)
993{
994	device_t dev;
995	int err;
996
997	if (!(flag & USB_UNCFG_FLAG_FREE_SUBDEV)) {
998
999		*ppdev = NULL;
1000
1001	} else if (*ppdev) {
1002
1003		/*
1004		 * NOTE: It is important to clear "*ppdev" before deleting
1005		 * the child due to some device methods being called late
1006		 * during the delete process !
1007		 */
1008		dev = *ppdev;
1009		*ppdev = NULL;
1010
1011		device_printf(dev, "at %s, port %d, addr %d "
1012		    "(disconnected)\n",
1013		    device_get_nameunit(udev->parent_dev),
1014		    udev->port_no, udev->address);
1015
1016		if (device_is_attached(dev)) {
1017			if (udev->flags.peer_suspended) {
1018				err = DEVICE_RESUME(dev);
1019				if (err) {
1020					device_printf(dev, "Resume failed!\n");
1021				}
1022			}
1023			if (device_detach(dev)) {
1024				goto error;
1025			}
1026		}
1027		if (device_delete_child(udev->parent_dev, dev)) {
1028			goto error;
1029		}
1030	}
1031	return;
1032
1033error:
1034	/* Detach is not allowed to fail in the USB world */
1035	panic("An USB driver would not detach!\n");
1036}
1037
1038/*------------------------------------------------------------------------*
1039 *	usb_detach_device
1040 *
1041 * The following function will detach the matching interfaces.
1042 * This function is NULL safe.
1043 *
1044 * Flag values, see "USB_UNCFG_FLAG_XXX".
1045 *------------------------------------------------------------------------*/
1046void
1047usb_detach_device(struct usb_device *udev, uint8_t iface_index,
1048    uint8_t flag)
1049{
1050	struct usb_interface *iface;
1051	uint8_t i;
1052
1053	if (udev == NULL) {
1054		/* nothing to do */
1055		return;
1056	}
1057	DPRINTFN(4, "udev=%p\n", udev);
1058
1059	sx_assert(udev->default_sx + 1, SA_LOCKED);
1060
1061	/*
1062	 * First detach the child to give the child's detach routine a
1063	 * chance to detach the sub-devices in the correct order.
1064	 * Then delete the child using "device_delete_child()" which
1065	 * will detach all sub-devices from the bottom and upwards!
1066	 */
1067	if (iface_index != USB_IFACE_INDEX_ANY) {
1068		i = iface_index;
1069		iface_index = i + 1;
1070	} else {
1071		i = 0;
1072		iface_index = USB_IFACE_MAX;
1073	}
1074
1075	/* do the detach */
1076
1077	for (; i != iface_index; i++) {
1078
1079		iface = usbd_get_iface(udev, i);
1080		if (iface == NULL) {
1081			/* looks like the end of the USB interfaces */
1082			break;
1083		}
1084		usb_detach_device_sub(udev, &iface->subdev, flag);
1085	}
1086}
1087
1088/*------------------------------------------------------------------------*
1089 *	usb_probe_and_attach_sub
1090 *
1091 * Returns:
1092 *    0: Success
1093 * Else: Failure
1094 *------------------------------------------------------------------------*/
1095static uint8_t
1096usb_probe_and_attach_sub(struct usb_device *udev,
1097    struct usb_attach_arg *uaa)
1098{
1099	struct usb_interface *iface;
1100	device_t dev;
1101	int err;
1102
1103	iface = uaa->iface;
1104	if (iface->parent_iface_index != USB_IFACE_INDEX_ANY) {
1105		/* leave interface alone */
1106		return (0);
1107	}
1108	dev = iface->subdev;
1109	if (dev) {
1110
1111		/* clean up after module unload */
1112
1113		if (device_is_attached(dev)) {
1114			/* already a device there */
1115			return (0);
1116		}
1117		/* clear "iface->subdev" as early as possible */
1118
1119		iface->subdev = NULL;
1120
1121		if (device_delete_child(udev->parent_dev, dev)) {
1122
1123			/*
1124			 * Panic here, else one can get a double call
1125			 * to device_detach().  USB devices should
1126			 * never fail on detach!
1127			 */
1128			panic("device_delete_child() failed!\n");
1129		}
1130	}
1131	if (uaa->temp_dev == NULL) {
1132
1133		/* create a new child */
1134		uaa->temp_dev = device_add_child(udev->parent_dev, NULL, -1);
1135		if (uaa->temp_dev == NULL) {
1136			device_printf(udev->parent_dev,
1137			    "Device creation failed!\n");
1138			return (1);	/* failure */
1139		}
1140		device_set_ivars(uaa->temp_dev, uaa);
1141		device_quiet(uaa->temp_dev);
1142	}
1143	/*
1144	 * Set "subdev" before probe and attach so that "devd" gets
1145	 * the information it needs.
1146	 */
1147	iface->subdev = uaa->temp_dev;
1148
1149	if (device_probe_and_attach(iface->subdev) == 0) {
1150		/*
1151		 * The USB attach arguments are only available during probe
1152		 * and attach !
1153		 */
1154		uaa->temp_dev = NULL;
1155		device_set_ivars(iface->subdev, NULL);
1156
1157		if (udev->flags.peer_suspended) {
1158			err = DEVICE_SUSPEND(iface->subdev);
1159			if (err)
1160				device_printf(iface->subdev, "Suspend failed\n");
1161		}
1162		return (0);		/* success */
1163	} else {
1164		/* No USB driver found */
1165		iface->subdev = NULL;
1166	}
1167	return (1);			/* failure */
1168}
1169
1170/*------------------------------------------------------------------------*
1171 *	usbd_set_parent_iface
1172 *
1173 * Using this function will lock the alternate interface setting on an
1174 * interface. It is typically used for multi interface drivers. In USB
1175 * device side mode it is assumed that the alternate interfaces all
1176 * have the same endpoint descriptors. The default parent index value
1177 * is "USB_IFACE_INDEX_ANY". Then the alternate setting value is not
1178 * locked.
1179 *------------------------------------------------------------------------*/
1180void
1181usbd_set_parent_iface(struct usb_device *udev, uint8_t iface_index,
1182    uint8_t parent_index)
1183{
1184	struct usb_interface *iface;
1185
1186	iface = usbd_get_iface(udev, iface_index);
1187	if (iface) {
1188		iface->parent_iface_index = parent_index;
1189	}
1190}
1191
1192static void
1193usb_init_attach_arg(struct usb_device *udev,
1194    struct usb_attach_arg *uaa)
1195{
1196	bzero(uaa, sizeof(*uaa));
1197
1198	uaa->device = udev;
1199	uaa->usb_mode = udev->flags.usb_mode;
1200	uaa->port = udev->port_no;
1201
1202	uaa->info.idVendor = UGETW(udev->ddesc.idVendor);
1203	uaa->info.idProduct = UGETW(udev->ddesc.idProduct);
1204	uaa->info.bcdDevice = UGETW(udev->ddesc.bcdDevice);
1205	uaa->info.bDeviceClass = udev->ddesc.bDeviceClass;
1206	uaa->info.bDeviceSubClass = udev->ddesc.bDeviceSubClass;
1207	uaa->info.bDeviceProtocol = udev->ddesc.bDeviceProtocol;
1208	uaa->info.bConfigIndex = udev->curr_config_index;
1209	uaa->info.bConfigNum = udev->curr_config_no;
1210}
1211
1212/*------------------------------------------------------------------------*
1213 *	usb_probe_and_attach
1214 *
1215 * This function is called from "uhub_explore_sub()",
1216 * "usb_handle_set_config()" and "usb_handle_request()".
1217 *
1218 * Returns:
1219 *    0: Success
1220 * Else: A control transfer failed
1221 *------------------------------------------------------------------------*/
1222usb_error_t
1223usb_probe_and_attach(struct usb_device *udev, uint8_t iface_index)
1224{
1225	struct usb_attach_arg uaa;
1226	struct usb_interface *iface;
1227	uint8_t i;
1228	uint8_t j;
1229	uint8_t do_unlock;
1230
1231	if (udev == NULL) {
1232		DPRINTF("udev == NULL\n");
1233		return (USB_ERR_INVAL);
1234	}
1235	/* automatic locking */
1236	if (sx_xlocked(udev->default_sx + 1)) {
1237		do_unlock = 0;
1238	} else {
1239		do_unlock = 1;
1240		sx_xlock(udev->default_sx + 1);
1241	}
1242
1243	if (udev->curr_config_index == USB_UNCONFIG_INDEX) {
1244		/* do nothing - no configuration has been set */
1245		goto done;
1246	}
1247	/* setup USB attach arguments */
1248
1249	usb_init_attach_arg(udev, &uaa);
1250
1251	/* Check if only one interface should be probed: */
1252	if (iface_index != USB_IFACE_INDEX_ANY) {
1253		i = iface_index;
1254		j = i + 1;
1255	} else {
1256		i = 0;
1257		j = USB_IFACE_MAX;
1258	}
1259
1260	/* Do the probe and attach */
1261	for (; i != j; i++) {
1262
1263		iface = usbd_get_iface(udev, i);
1264		if (iface == NULL) {
1265			/*
1266			 * Looks like the end of the USB
1267			 * interfaces !
1268			 */
1269			DPRINTFN(2, "end of interfaces "
1270			    "at %u\n", i);
1271			break;
1272		}
1273		if (iface->idesc == NULL) {
1274			/* no interface descriptor */
1275			continue;
1276		}
1277		uaa.iface = iface;
1278
1279		uaa.info.bInterfaceClass =
1280		    iface->idesc->bInterfaceClass;
1281		uaa.info.bInterfaceSubClass =
1282		    iface->idesc->bInterfaceSubClass;
1283		uaa.info.bInterfaceProtocol =
1284		    iface->idesc->bInterfaceProtocol;
1285		uaa.info.bIfaceIndex = i;
1286		uaa.info.bIfaceNum =
1287		    iface->idesc->bInterfaceNumber;
1288		uaa.use_generic = 0;
1289
1290		DPRINTFN(2, "iclass=%u/%u/%u iindex=%u/%u\n",
1291		    uaa.info.bInterfaceClass,
1292		    uaa.info.bInterfaceSubClass,
1293		    uaa.info.bInterfaceProtocol,
1294		    uaa.info.bIfaceIndex,
1295		    uaa.info.bIfaceNum);
1296
1297		/* try specific interface drivers first */
1298
1299		if (usb_probe_and_attach_sub(udev, &uaa)) {
1300			/* ignore */
1301		}
1302		/* try generic interface drivers last */
1303
1304		uaa.use_generic = 1;
1305
1306		if (usb_probe_and_attach_sub(udev, &uaa)) {
1307			/* ignore */
1308		}
1309	}
1310
1311	if (uaa.temp_dev) {
1312		/* remove the last created child; it is unused */
1313
1314		if (device_delete_child(udev->parent_dev, uaa.temp_dev)) {
1315			DPRINTFN(0, "device delete child failed!\n");
1316		}
1317	}
1318done:
1319	if (do_unlock) {
1320		sx_unlock(udev->default_sx + 1);
1321	}
1322	return (0);
1323}
1324
1325/*------------------------------------------------------------------------*
1326 *	usb_suspend_resume_sub
1327 *
1328 * This function is called when the suspend or resume methods should
1329 * be executed on an USB device.
1330 *------------------------------------------------------------------------*/
1331static void
1332usb_suspend_resume_sub(struct usb_device *udev, device_t dev, uint8_t do_suspend)
1333{
1334	int err;
1335
1336	if (dev == NULL) {
1337		return;
1338	}
1339	if (!device_is_attached(dev)) {
1340		return;
1341	}
1342	if (do_suspend) {
1343		err = DEVICE_SUSPEND(dev);
1344	} else {
1345		err = DEVICE_RESUME(dev);
1346	}
1347	if (err) {
1348		device_printf(dev, "%s failed!\n",
1349		    do_suspend ? "Suspend" : "Resume");
1350	}
1351}
1352
1353/*------------------------------------------------------------------------*
1354 *	usb_suspend_resume
1355 *
1356 * The following function will suspend or resume the USB device.
1357 *
1358 * Returns:
1359 *    0: Success
1360 * Else: Failure
1361 *------------------------------------------------------------------------*/
1362usb_error_t
1363usb_suspend_resume(struct usb_device *udev, uint8_t do_suspend)
1364{
1365	struct usb_interface *iface;
1366	uint8_t i;
1367
1368	if (udev == NULL) {
1369		/* nothing to do */
1370		return (0);
1371	}
1372	DPRINTFN(4, "udev=%p do_suspend=%d\n", udev, do_suspend);
1373
1374	sx_assert(udev->default_sx + 1, SA_LOCKED);
1375
1376	USB_BUS_LOCK(udev->bus);
1377	/* filter the suspend events */
1378	if (udev->flags.peer_suspended == do_suspend) {
1379		USB_BUS_UNLOCK(udev->bus);
1380		/* nothing to do */
1381		return (0);
1382	}
1383	udev->flags.peer_suspended = do_suspend;
1384	USB_BUS_UNLOCK(udev->bus);
1385
1386	/* do the suspend or resume */
1387
1388	for (i = 0; i != USB_IFACE_MAX; i++) {
1389
1390		iface = usbd_get_iface(udev, i);
1391		if (iface == NULL) {
1392			/* looks like the end of the USB interfaces */
1393			break;
1394		}
1395		usb_suspend_resume_sub(udev, iface->subdev, do_suspend);
1396	}
1397	return (0);
1398}
1399
1400/*------------------------------------------------------------------------*
1401 *      usbd_clear_stall_proc
1402 *
1403 * This function performs generic USB clear stall operations.
1404 *------------------------------------------------------------------------*/
1405static void
1406usbd_clear_stall_proc(struct usb_proc_msg *_pm)
1407{
1408	struct usb_clear_stall_msg *pm = (void *)_pm;
1409	struct usb_device *udev = pm->udev;
1410
1411	/* Change lock */
1412	USB_BUS_UNLOCK(udev->bus);
1413	mtx_lock(udev->default_mtx);
1414
1415	/* Start clear stall callback */
1416	usbd_transfer_start(udev->default_xfer[1]);
1417
1418	/* Change lock */
1419	mtx_unlock(udev->default_mtx);
1420	USB_BUS_LOCK(udev->bus);
1421}
1422
1423/*------------------------------------------------------------------------*
1424 *	usb_alloc_device
1425 *
1426 * This function allocates a new USB device. This function is called
1427 * when a new device has been put in the powered state, but not yet in
1428 * the addressed state. Get initial descriptor, set the address, get
1429 * full descriptor and get strings.
1430 *
1431 * Return values:
1432 *    0: Failure
1433 * Else: Success
1434 *------------------------------------------------------------------------*/
1435struct usb_device *
1436usb_alloc_device(device_t parent_dev, struct usb_bus *bus,
1437    struct usb_device *parent_hub, uint8_t depth, uint8_t port_index,
1438    uint8_t port_no, enum usb_dev_speed speed, enum usb_hc_mode mode)
1439{
1440	struct usb_attach_arg uaa;
1441	struct usb_device *udev;
1442	struct usb_device *adev;
1443	struct usb_device *hub;
1444	uint8_t *scratch_ptr;
1445	uint32_t scratch_size;
1446	usb_error_t err;
1447	uint8_t device_index;
1448
1449	DPRINTF("parent_dev=%p, bus=%p, parent_hub=%p, depth=%u, "
1450	    "port_index=%u, port_no=%u, speed=%u, usb_mode=%u\n",
1451	    parent_dev, bus, parent_hub, depth, port_index, port_no,
1452	    speed, mode);
1453
1454	/*
1455	 * Find an unused device index. In USB Host mode this is the
1456	 * same as the device address.
1457	 *
1458	 * Device index zero is not used and device index 1 should
1459	 * always be the root hub.
1460	 */
1461	for (device_index = USB_ROOT_HUB_ADDR;
1462	    (device_index != bus->devices_max) &&
1463	    (bus->devices[device_index] != NULL);
1464	    device_index++) /* nop */;
1465
1466	if (device_index == bus->devices_max) {
1467		device_printf(bus->bdev,
1468		    "No free USB device index for new device!\n");
1469		return (NULL);
1470	}
1471
1472	if (depth > 0x10) {
1473		device_printf(bus->bdev,
1474		    "Invalid device depth!\n");
1475		return (NULL);
1476	}
1477	udev = malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO);
1478	if (udev == NULL) {
1479		return (NULL);
1480	}
1481	/* initialise our SX-lock */
1482	sx_init(udev->default_sx, "0123456789ABCDEF - USB device SX lock" + depth);
1483
1484	/* initialise our SX-lock */
1485	sx_init(udev->default_sx + 1, "0123456789ABCDEF - USB config SX lock" + depth);
1486
1487	cv_init(udev->default_cv, "WCTRL");
1488	cv_init(udev->default_cv + 1, "UGONE");
1489
1490	/* initialise our mutex */
1491	mtx_init(udev->default_mtx, "USB device mutex", NULL, MTX_DEF);
1492
1493	/* initialise generic clear stall */
1494	udev->cs_msg[0].hdr.pm_callback = &usbd_clear_stall_proc;
1495	udev->cs_msg[0].udev = udev;
1496	udev->cs_msg[1].hdr.pm_callback = &usbd_clear_stall_proc;
1497	udev->cs_msg[1].udev = udev;
1498
1499	/* initialise some USB device fields */
1500	udev->parent_hub = parent_hub;
1501	udev->parent_dev = parent_dev;
1502	udev->port_index = port_index;
1503	udev->port_no = port_no;
1504	udev->depth = depth;
1505	udev->bus = bus;
1506	udev->address = USB_START_ADDR;	/* default value */
1507	udev->plugtime = (usb_ticks_t)ticks;
1508	usb_set_device_state(udev, USB_STATE_POWERED);
1509	/*
1510	 * We need to force the power mode to "on" because there are plenty
1511	 * of USB devices out there that do not work very well with
1512	 * automatic suspend and resume!
1513	 */
1514	udev->power_mode = USB_POWER_MODE_ON;
1515	udev->pwr_save.last_xfer_time = ticks;
1516	/* we are not ready yet */
1517	udev->refcount = 1;
1518
1519	/* set up default endpoint descriptor */
1520	udev->default_ep_desc.bLength = sizeof(udev->default_ep_desc);
1521	udev->default_ep_desc.bDescriptorType = UDESC_ENDPOINT;
1522	udev->default_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
1523	udev->default_ep_desc.bmAttributes = UE_CONTROL;
1524	udev->default_ep_desc.wMaxPacketSize[0] = USB_MAX_IPACKET;
1525	udev->default_ep_desc.wMaxPacketSize[1] = 0;
1526	udev->default_ep_desc.bInterval = 0;
1527	udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
1528
1529	udev->speed = speed;
1530	udev->flags.usb_mode = mode;
1531
1532	/* search for our High Speed USB HUB, if any */
1533
1534	adev = udev;
1535	hub = udev->parent_hub;
1536
1537	while (hub) {
1538		if (hub->speed == USB_SPEED_HIGH) {
1539			udev->hs_hub_addr = hub->address;
1540			udev->parent_hs_hub = hub;
1541			udev->hs_port_no = adev->port_no;
1542			break;
1543		}
1544		adev = hub;
1545		hub = hub->parent_hub;
1546	}
1547
1548	/* init the default endpoint */
1549	usb_init_endpoint(udev, 0,
1550	    &udev->default_ep_desc,
1551	    &udev->default_ep);
1552
1553	/* set device index */
1554	udev->device_index = device_index;
1555
1556#if USB_HAVE_UGEN
1557	/* Create ugen name */
1558	snprintf(udev->ugen_name, sizeof(udev->ugen_name),
1559	    USB_GENERIC_NAME "%u.%u", device_get_unit(bus->bdev),
1560	    device_index);
1561	LIST_INIT(&udev->pd_list);
1562
1563	/* Create the control endpoint device */
1564	udev->default_dev = usb_make_dev(udev, 0, FREAD|FWRITE);
1565
1566	/* Create a link from /dev/ugenX.X to the default endpoint */
1567	make_dev_alias(udev->default_dev, udev->ugen_name);
1568#endif
1569	if (udev->flags.usb_mode == USB_MODE_HOST) {
1570
1571		err = usbd_req_set_address(udev, NULL, device_index);
1572
1573		/* This is the new USB device address from now on */
1574
1575		udev->address = device_index;
1576
1577		/*
1578		 * We ignore any set-address errors, hence there are
1579		 * buggy USB devices out there that actually receive
1580		 * the SETUP PID, but manage to set the address before
1581		 * the STATUS stage is ACK'ed. If the device responds
1582		 * to the subsequent get-descriptor at the new
1583		 * address, then we know that the set-address command
1584		 * was successful.
1585		 */
1586		if (err) {
1587			DPRINTFN(0, "set address %d failed "
1588			    "(%s, ignored)\n", udev->address,
1589			    usbd_errstr(err));
1590		}
1591		/* allow device time to set new address */
1592		usb_pause_mtx(NULL,
1593		    USB_MS_TO_TICKS(USB_SET_ADDRESS_SETTLE));
1594	} else {
1595		/* We are not self powered */
1596		udev->flags.self_powered = 0;
1597
1598		/* Set unconfigured state */
1599		udev->curr_config_no = USB_UNCONFIG_NO;
1600		udev->curr_config_index = USB_UNCONFIG_INDEX;
1601
1602		/* Setup USB descriptors */
1603		err = (usb_temp_setup_by_index_p) (udev, usb_template);
1604		if (err) {
1605			DPRINTFN(0, "setting up USB template failed maybe the USB "
1606			    "template module has not been loaded\n");
1607			goto done;
1608		}
1609	}
1610	usb_set_device_state(udev, USB_STATE_ADDRESSED);
1611
1612	/*
1613	 * Get the first 8 bytes of the device descriptor !
1614	 *
1615	 * NOTE: "usbd_do_request" will check the device descriptor
1616	 * next time we do a request to see if the maximum packet size
1617	 * changed! The 8 first bytes of the device descriptor
1618	 * contains the maximum packet size to use on control endpoint
1619	 * 0. If this value is different from "USB_MAX_IPACKET" a new
1620	 * USB control request will be setup!
1621	 */
1622	err = usbd_req_get_desc(udev, NULL, NULL, &udev->ddesc,
1623	    USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0);
1624	if (err) {
1625		DPRINTFN(0, "getting device descriptor "
1626		    "at addr %d failed, %s!\n", udev->address,
1627		    usbd_errstr(err));
1628		/* XXX try to re-enumerate the device */
1629		err = usbd_req_re_enumerate(udev, NULL);
1630		if (err) {
1631			goto done;
1632		}
1633	}
1634	DPRINTF("adding unit addr=%d, rev=%02x, class=%d, "
1635	    "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1636	    udev->address, UGETW(udev->ddesc.bcdUSB),
1637	    udev->ddesc.bDeviceClass,
1638	    udev->ddesc.bDeviceSubClass,
1639	    udev->ddesc.bDeviceProtocol,
1640	    udev->ddesc.bMaxPacketSize,
1641	    udev->ddesc.bLength,
1642	    udev->speed);
1643
1644	/* get the full device descriptor */
1645	err = usbd_req_get_device_desc(udev, NULL, &udev->ddesc);
1646	if (err) {
1647		DPRINTF("addr=%d, getting full desc failed\n",
1648		    udev->address);
1649		goto done;
1650	}
1651	/*
1652	 * Setup temporary USB attach args so that we can figure out some
1653	 * basic quirks for this device.
1654	 */
1655	usb_init_attach_arg(udev, &uaa);
1656
1657	if (usb_test_quirk(&uaa, UQ_BUS_POWERED)) {
1658		udev->flags.uq_bus_powered = 1;
1659	}
1660	if (usb_test_quirk(&uaa, UQ_NO_STRINGS)) {
1661		udev->flags.no_strings = 1;
1662	}
1663	/*
1664	 * Workaround for buggy USB devices.
1665	 *
1666	 * It appears that some string-less USB chips will crash and
1667	 * disappear if any attempts are made to read any string
1668	 * descriptors.
1669	 *
1670	 * Try to detect such chips by checking the strings in the USB
1671	 * device descriptor. If no strings are present there we
1672	 * simply disable all USB strings.
1673	 */
1674	scratch_ptr = udev->bus->scratch[0].data;
1675	scratch_size = sizeof(udev->bus->scratch[0].data);
1676
1677	if (udev->ddesc.iManufacturer ||
1678	    udev->ddesc.iProduct ||
1679	    udev->ddesc.iSerialNumber) {
1680		/* read out the language ID string */
1681		err = usbd_req_get_string_desc(udev, NULL,
1682		    (char *)scratch_ptr, 4, scratch_size,
1683		    USB_LANGUAGE_TABLE);
1684	} else {
1685		err = USB_ERR_INVAL;
1686	}
1687
1688	if (err || (scratch_ptr[0] < 4)) {
1689		udev->flags.no_strings = 1;
1690	} else {
1691		/* pick the first language as the default */
1692		udev->langid = UGETW(scratch_ptr + 2);
1693	}
1694
1695	/* assume 100mA bus powered for now. Changed when configured. */
1696	udev->power = USB_MIN_POWER;
1697	/* fetch the vendor and product strings from the device */
1698	usbd_set_device_strings(udev);
1699
1700	if (udev->flags.usb_mode == USB_MODE_HOST) {
1701		uint8_t config_index;
1702		uint8_t config_quirk;
1703		uint8_t set_config_failed = 0;
1704
1705		/*
1706		 * Most USB devices should attach to config index 0 by
1707		 * default
1708		 */
1709		if (usb_test_quirk(&uaa, UQ_CFG_INDEX_0)) {
1710			config_index = 0;
1711			config_quirk = 1;
1712		} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_1)) {
1713			config_index = 1;
1714			config_quirk = 1;
1715		} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_2)) {
1716			config_index = 2;
1717			config_quirk = 1;
1718		} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_3)) {
1719			config_index = 3;
1720			config_quirk = 1;
1721		} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_4)) {
1722			config_index = 4;
1723			config_quirk = 1;
1724		} else {
1725			config_index = 0;
1726			config_quirk = 0;
1727		}
1728
1729repeat_set_config:
1730
1731		DPRINTF("setting config %u\n", config_index);
1732
1733		/* get the USB device configured */
1734		err = usbd_set_config_index(udev, config_index);
1735		if (err) {
1736			if (udev->ddesc.bNumConfigurations != 0) {
1737				if (!set_config_failed) {
1738					set_config_failed = 1;
1739					/* XXX try to re-enumerate the device */
1740					err = usbd_req_re_enumerate(
1741					    udev, NULL);
1742					if (err == 0)
1743					    goto repeat_set_config;
1744				}
1745				DPRINTFN(0, "Failure selecting "
1746				    "configuration index %u: %s, port %u, "
1747				    "addr %u (ignored)\n",
1748				    config_index, usbd_errstr(err), udev->port_no,
1749				    udev->address);
1750			}
1751			/*
1752			 * Some USB devices do not have any
1753			 * configurations. Ignore any set config
1754			 * failures!
1755			 */
1756			err = 0;
1757		} else if (config_quirk) {
1758			/* user quirk selects configuration index */
1759		} else if ((config_index + 1) < udev->ddesc.bNumConfigurations) {
1760
1761			if ((udev->cdesc->bNumInterface < 2) &&
1762			    (usbd_get_no_descriptors(udev->cdesc,
1763			    UDESC_ENDPOINT) == 0)) {
1764				DPRINTFN(0, "Found no endpoints "
1765				    "(trying next config)!\n");
1766				config_index++;
1767				goto repeat_set_config;
1768			}
1769			if (config_index == 0) {
1770				/*
1771				 * Try to figure out if we have an
1772				 * auto-install disk there:
1773				 */
1774				if (usb_test_autoinstall(udev, 0, 0) == 0) {
1775					DPRINTFN(0, "Found possible auto-install "
1776					    "disk (trying next config)\n");
1777					config_index++;
1778					goto repeat_set_config;
1779				}
1780			}
1781		} else if (usb_test_huawei_autoinst_p(udev, &uaa) == 0) {
1782			DPRINTFN(0, "Found Huawei auto-install disk!\n");
1783			err = USB_ERR_STALLED;	/* fake an error */
1784		}
1785	} else {
1786		err = 0;		/* set success */
1787	}
1788
1789	DPRINTF("new dev (addr %d), udev=%p, parent_hub=%p\n",
1790	    udev->address, udev, udev->parent_hub);
1791
1792	/* register our device - we are ready */
1793	usb_bus_port_set_device(bus, parent_hub ?
1794	    parent_hub->hub->ports + port_index : NULL, udev, device_index);
1795
1796#if USB_HAVE_UGEN
1797	/* Symlink the ugen device name */
1798	udev->ugen_symlink = usb_alloc_symlink(udev->ugen_name);
1799
1800	/* Announce device */
1801	printf("%s: <%s> at %s\n", udev->ugen_name, udev->manufacturer,
1802	    device_get_nameunit(udev->bus->bdev));
1803
1804	usb_notify_addq("+", udev);
1805#endif
1806done:
1807	if (err) {
1808		/* free device  */
1809		usb_free_device(udev,
1810		    USB_UNCFG_FLAG_FREE_SUBDEV |
1811		    USB_UNCFG_FLAG_FREE_EP0);
1812		udev = NULL;
1813	}
1814	return (udev);
1815}
1816
1817#if USB_HAVE_UGEN
1818static struct cdev *
1819usb_make_dev(struct usb_device *udev, int ep, int mode)
1820{
1821	struct usb_fs_privdata* pd;
1822	char devname[20];
1823
1824	/* Store information to locate ourselves again later */
1825	pd = malloc(sizeof(struct usb_fs_privdata), M_USBDEV,
1826	    M_WAITOK | M_ZERO);
1827	pd->bus_index = device_get_unit(udev->bus->bdev);
1828	pd->dev_index = udev->device_index;
1829	pd->ep_addr = ep;
1830	pd->mode = mode;
1831
1832	/* Now, create the device itself */
1833	snprintf(devname, sizeof(devname), "%u.%u.%u",
1834	    pd->bus_index, pd->dev_index, pd->ep_addr);
1835	pd->cdev = make_dev(&usb_devsw, 0, UID_ROOT,
1836	    GID_OPERATOR, 0600, USB_DEVICE_DIR "/%s", devname);
1837	pd->cdev->si_drv1 = pd;
1838
1839	return (pd->cdev);
1840}
1841
1842static void
1843usb_cdev_create(struct usb_device *udev)
1844{
1845	struct usb_config_descriptor *cd;
1846	struct usb_endpoint_descriptor *ed;
1847	struct usb_descriptor *desc;
1848	struct usb_fs_privdata* pd;
1849	struct cdev *dev;
1850	int inmode, outmode, inmask, outmask, mode;
1851	uint8_t ep;
1852
1853	KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("stale cdev entries"));
1854
1855	DPRINTFN(2, "Creating device nodes\n");
1856
1857	if (usbd_get_mode(udev) == USB_MODE_DEVICE) {
1858		inmode = FWRITE;
1859		outmode = FREAD;
1860	} else {		 /* USB_MODE_HOST */
1861		inmode = FREAD;
1862		outmode = FWRITE;
1863	}
1864
1865	inmask = 0;
1866	outmask = 0;
1867	desc = NULL;
1868
1869	/*
1870	 * Collect all used endpoint numbers instead of just
1871	 * generating 16 static endpoints.
1872	 */
1873	cd = usbd_get_config_descriptor(udev);
1874	while ((desc = usb_desc_foreach(cd, desc))) {
1875		/* filter out all endpoint descriptors */
1876		if ((desc->bDescriptorType == UDESC_ENDPOINT) &&
1877		    (desc->bLength >= sizeof(*ed))) {
1878			ed = (struct usb_endpoint_descriptor *)desc;
1879
1880			/* update masks */
1881			ep = ed->bEndpointAddress;
1882			if (UE_GET_DIR(ep)  == UE_DIR_OUT)
1883				outmask |= 1 << UE_GET_ADDR(ep);
1884			else
1885				inmask |= 1 << UE_GET_ADDR(ep);
1886		}
1887	}
1888
1889	/* Create all available endpoints except EP0 */
1890	for (ep = 1; ep < 16; ep++) {
1891		mode = inmask & (1 << ep) ? inmode : 0;
1892		mode |= outmask & (1 << ep) ? outmode : 0;
1893		if (mode == 0)
1894			continue;	/* no IN or OUT endpoint */
1895
1896		dev = usb_make_dev(udev, ep, mode);
1897		pd = dev->si_drv1;
1898		LIST_INSERT_HEAD(&udev->pd_list, pd, pd_next);
1899	}
1900}
1901
1902static void
1903usb_cdev_free(struct usb_device *udev)
1904{
1905	struct usb_fs_privdata* pd;
1906
1907	DPRINTFN(2, "Freeing device nodes\n");
1908
1909	while ((pd = LIST_FIRST(&udev->pd_list)) != NULL) {
1910		KASSERT(pd->cdev->si_drv1 == pd, ("privdata corrupt"));
1911
1912		destroy_dev_sched_cb(pd->cdev, usb_cdev_cleanup, pd);
1913		pd->cdev = NULL;
1914		LIST_REMOVE(pd, pd_next);
1915	}
1916}
1917
1918static void
1919usb_cdev_cleanup(void* arg)
1920{
1921	free(arg, M_USBDEV);
1922}
1923#endif
1924
1925/*------------------------------------------------------------------------*
1926 *	usb_free_device
1927 *
1928 * This function is NULL safe and will free an USB device.
1929 *
1930 * Flag values, see "USB_UNCFG_FLAG_XXX".
1931 *------------------------------------------------------------------------*/
1932void
1933usb_free_device(struct usb_device *udev, uint8_t flag)
1934{
1935	struct usb_bus *bus;
1936
1937	if (udev == NULL)
1938		return;		/* already freed */
1939
1940	DPRINTFN(4, "udev=%p port=%d\n", udev, udev->port_no);
1941
1942	bus = udev->bus;
1943	usb_set_device_state(udev, USB_STATE_DETACHED);
1944
1945#if USB_HAVE_UGEN
1946	usb_notify_addq("-", udev);
1947
1948	printf("%s: <%s> at %s (disconnected)\n", udev->ugen_name,
1949	    udev->manufacturer, device_get_nameunit(bus->bdev));
1950
1951	/* Destroy UGEN symlink, if any */
1952	if (udev->ugen_symlink) {
1953		usb_free_symlink(udev->ugen_symlink);
1954		udev->ugen_symlink = NULL;
1955	}
1956#endif
1957	/*
1958	 * Unregister our device first which will prevent any further
1959	 * references:
1960	 */
1961	usb_bus_port_set_device(bus, udev->parent_hub ?
1962	    udev->parent_hub->hub->ports + udev->port_index : NULL,
1963	    NULL, USB_ROOT_HUB_ADDR);
1964
1965#if USB_HAVE_UGEN
1966	/* wait for all pending references to go away: */
1967	mtx_lock(&usb_ref_lock);
1968	udev->refcount--;
1969	while (udev->refcount != 0) {
1970		cv_wait(udev->default_cv + 1, &usb_ref_lock);
1971	}
1972	mtx_unlock(&usb_ref_lock);
1973
1974	destroy_dev_sched_cb(udev->default_dev, usb_cdev_cleanup,
1975	    udev->default_dev->si_drv1);
1976#endif
1977
1978	if (udev->flags.usb_mode == USB_MODE_DEVICE) {
1979		/* stop receiving any control transfers (Device Side Mode) */
1980		usbd_transfer_unsetup(udev->default_xfer, USB_DEFAULT_XFER_MAX);
1981	}
1982
1983	/* the following will get the device unconfigured in software */
1984	usb_unconfigure(udev, flag);
1985
1986	/* unsetup any leftover default USB transfers */
1987	usbd_transfer_unsetup(udev->default_xfer, USB_DEFAULT_XFER_MAX);
1988
1989	/* template unsetup, if any */
1990	(usb_temp_unsetup_p) (udev);
1991
1992	/*
1993	 * Make sure that our clear-stall messages are not queued
1994	 * anywhere:
1995	 */
1996	USB_BUS_LOCK(udev->bus);
1997	usb_proc_mwait(&udev->bus->non_giant_callback_proc,
1998	    &udev->cs_msg[0], &udev->cs_msg[1]);
1999	USB_BUS_UNLOCK(udev->bus);
2000
2001	sx_destroy(udev->default_sx);
2002	sx_destroy(udev->default_sx + 1);
2003
2004	cv_destroy(udev->default_cv);
2005	cv_destroy(udev->default_cv + 1);
2006
2007	mtx_destroy(udev->default_mtx);
2008#if USB_HAVE_UGEN
2009	KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("leaked cdev entries"));
2010#endif
2011
2012	/* free device */
2013	free(udev->serial, M_USB);
2014	free(udev->manufacturer, M_USB);
2015	free(udev->product, M_USB);
2016	free(udev, M_USB);
2017}
2018
2019/*------------------------------------------------------------------------*
2020 *	usbd_get_iface
2021 *
2022 * This function is the safe way to get the USB interface structure
2023 * pointer by interface index.
2024 *
2025 * Return values:
2026 *   NULL: Interface not present.
2027 *   Else: Pointer to USB interface structure.
2028 *------------------------------------------------------------------------*/
2029struct usb_interface *
2030usbd_get_iface(struct usb_device *udev, uint8_t iface_index)
2031{
2032	struct usb_interface *iface = udev->ifaces + iface_index;
2033
2034	if (iface_index >= udev->ifaces_max)
2035		return (NULL);
2036	return (iface);
2037}
2038
2039/*------------------------------------------------------------------------*
2040 *	usbd_find_descriptor
2041 *
2042 * This function will lookup the first descriptor that matches the
2043 * criteria given by the arguments "type" and "subtype". Descriptors
2044 * will only be searched within the interface having the index
2045 * "iface_index".  If the "id" argument points to an USB descriptor,
2046 * it will be skipped before the search is started. This allows
2047 * searching for multiple descriptors using the same criteria. Else
2048 * the search is started after the interface descriptor.
2049 *
2050 * Return values:
2051 *   NULL: End of descriptors
2052 *   Else: A descriptor matching the criteria
2053 *------------------------------------------------------------------------*/
2054void   *
2055usbd_find_descriptor(struct usb_device *udev, void *id, uint8_t iface_index,
2056    uint8_t type, uint8_t type_mask,
2057    uint8_t subtype, uint8_t subtype_mask)
2058{
2059	struct usb_descriptor *desc;
2060	struct usb_config_descriptor *cd;
2061	struct usb_interface *iface;
2062
2063	cd = usbd_get_config_descriptor(udev);
2064	if (cd == NULL) {
2065		return (NULL);
2066	}
2067	if (id == NULL) {
2068		iface = usbd_get_iface(udev, iface_index);
2069		if (iface == NULL) {
2070			return (NULL);
2071		}
2072		id = usbd_get_interface_descriptor(iface);
2073		if (id == NULL) {
2074			return (NULL);
2075		}
2076	}
2077	desc = (void *)id;
2078
2079	while ((desc = usb_desc_foreach(cd, desc))) {
2080
2081		if (desc->bDescriptorType == UDESC_INTERFACE) {
2082			break;
2083		}
2084		if (((desc->bDescriptorType & type_mask) == type) &&
2085		    ((desc->bDescriptorSubtype & subtype_mask) == subtype)) {
2086			return (desc);
2087		}
2088	}
2089	return (NULL);
2090}
2091
2092/*------------------------------------------------------------------------*
2093 *	usb_devinfo
2094 *
2095 * This function will dump information from the device descriptor
2096 * belonging to the USB device pointed to by "udev", to the string
2097 * pointed to by "dst_ptr" having a maximum length of "dst_len" bytes
2098 * including the terminating zero.
2099 *------------------------------------------------------------------------*/
2100void
2101usb_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len)
2102{
2103	struct usb_device_descriptor *udd = &udev->ddesc;
2104	uint16_t bcdDevice;
2105	uint16_t bcdUSB;
2106
2107	bcdUSB = UGETW(udd->bcdUSB);
2108	bcdDevice = UGETW(udd->bcdDevice);
2109
2110	if (udd->bDeviceClass != 0xFF) {
2111		snprintf(dst_ptr, dst_len, "%s %s, class %d/%d, rev %x.%02x/"
2112		    "%x.%02x, addr %d",
2113		    udev->manufacturer, udev->product,
2114		    udd->bDeviceClass, udd->bDeviceSubClass,
2115		    (bcdUSB >> 8), bcdUSB & 0xFF,
2116		    (bcdDevice >> 8), bcdDevice & 0xFF,
2117		    udev->address);
2118	} else {
2119		snprintf(dst_ptr, dst_len, "%s %s, rev %x.%02x/"
2120		    "%x.%02x, addr %d",
2121		    udev->manufacturer, udev->product,
2122		    (bcdUSB >> 8), bcdUSB & 0xFF,
2123		    (bcdDevice >> 8), bcdDevice & 0xFF,
2124		    udev->address);
2125	}
2126}
2127
2128#ifdef USB_VERBOSE
2129/*
2130 * Descriptions of of known vendors and devices ("products").
2131 */
2132struct usb_knowndev {
2133	uint16_t vendor;
2134	uint16_t product;
2135	uint32_t flags;
2136	const char *vendorname;
2137	const char *productname;
2138};
2139
2140#define	USB_KNOWNDEV_NOPROD	0x01	/* match on vendor only */
2141
2142#include "usbdevs.h"
2143#include "usbdevs_data.h"
2144#endif					/* USB_VERBOSE */
2145
2146static void
2147usbd_set_device_strings(struct usb_device *udev)
2148{
2149	struct usb_device_descriptor *udd = &udev->ddesc;
2150#ifdef USB_VERBOSE
2151	const struct usb_knowndev *kdp;
2152#endif
2153	char temp[64];
2154	uint16_t vendor_id;
2155	uint16_t product_id;
2156
2157	vendor_id = UGETW(udd->idVendor);
2158	product_id = UGETW(udd->idProduct);
2159
2160	/* get serial number string */
2161	bzero(temp, sizeof(temp));
2162	usbd_req_get_string_any(udev, NULL, temp, sizeof(temp),
2163	    udev->ddesc.iSerialNumber);
2164	udev->serial = strdup(temp, M_USB);
2165
2166	/* get manufacturer string */
2167	bzero(temp, sizeof(temp));
2168	usbd_req_get_string_any(udev, NULL, temp, sizeof(temp),
2169	    udev->ddesc.iManufacturer);
2170	usb_trim_spaces(temp);
2171	if (temp[0] != '\0')
2172		udev->manufacturer = strdup(temp, M_USB);
2173
2174	/* get product string */
2175	bzero(temp, sizeof(temp));
2176	usbd_req_get_string_any(udev, NULL, temp, sizeof(temp),
2177	    udev->ddesc.iProduct);
2178	usb_trim_spaces(temp);
2179	if (temp[0] != '\0')
2180		udev->product = strdup(temp, M_USB);
2181
2182#ifdef USB_VERBOSE
2183	if (udev->manufacturer == NULL || udev->product == NULL) {
2184		for (kdp = usb_knowndevs; kdp->vendorname != NULL; kdp++) {
2185			if (kdp->vendor == vendor_id &&
2186			    (kdp->product == product_id ||
2187			    (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
2188				break;
2189		}
2190		if (kdp->vendorname != NULL) {
2191			/* XXX should use pointer to knowndevs string */
2192			if (udev->manufacturer == NULL) {
2193				udev->manufacturer = strdup(kdp->vendorname,
2194				    M_USB);
2195			}
2196			if (udev->product == NULL &&
2197			    (kdp->flags & USB_KNOWNDEV_NOPROD) == 0) {
2198				udev->product = strdup(kdp->productname,
2199				    M_USB);
2200			}
2201		}
2202	}
2203#endif
2204	/* Provide default strings if none were found */
2205	if (udev->manufacturer == NULL) {
2206		snprintf(temp, sizeof(temp), "vendor 0x%04x", vendor_id);
2207		udev->manufacturer = strdup(temp, M_USB);
2208	}
2209	if (udev->product == NULL) {
2210		snprintf(temp, sizeof(temp), "product 0x%04x", product_id);
2211		udev->product = strdup(temp, M_USB);
2212	}
2213}
2214
2215/*
2216 * Returns:
2217 * See: USB_MODE_XXX
2218 */
2219enum usb_hc_mode
2220usbd_get_mode(struct usb_device *udev)
2221{
2222	return (udev->flags.usb_mode);
2223}
2224
2225/*
2226 * Returns:
2227 * See: USB_SPEED_XXX
2228 */
2229enum usb_dev_speed
2230usbd_get_speed(struct usb_device *udev)
2231{
2232	return (udev->speed);
2233}
2234
2235uint32_t
2236usbd_get_isoc_fps(struct usb_device *udev)
2237{
2238	;				/* indent fix */
2239	switch (udev->speed) {
2240	case USB_SPEED_LOW:
2241	case USB_SPEED_FULL:
2242		return (1000);
2243	default:
2244		return (8000);
2245	}
2246}
2247
2248struct usb_device_descriptor *
2249usbd_get_device_descriptor(struct usb_device *udev)
2250{
2251	if (udev == NULL)
2252		return (NULL);		/* be NULL safe */
2253	return (&udev->ddesc);
2254}
2255
2256struct usb_config_descriptor *
2257usbd_get_config_descriptor(struct usb_device *udev)
2258{
2259	if (udev == NULL)
2260		return (NULL);		/* be NULL safe */
2261	return (udev->cdesc);
2262}
2263
2264/*------------------------------------------------------------------------*
2265 *	usb_test_quirk - test a device for a given quirk
2266 *
2267 * Return values:
2268 * 0: The USB device does not have the given quirk.
2269 * Else: The USB device has the given quirk.
2270 *------------------------------------------------------------------------*/
2271uint8_t
2272usb_test_quirk(const struct usb_attach_arg *uaa, uint16_t quirk)
2273{
2274	uint8_t found;
2275
2276	found = (usb_test_quirk_p) (&uaa->info, quirk);
2277	return (found);
2278}
2279
2280struct usb_interface_descriptor *
2281usbd_get_interface_descriptor(struct usb_interface *iface)
2282{
2283	if (iface == NULL)
2284		return (NULL);		/* be NULL safe */
2285	return (iface->idesc);
2286}
2287
2288uint8_t
2289usbd_get_interface_altindex(struct usb_interface *iface)
2290{
2291	return (iface->alt_index);
2292}
2293
2294uint8_t
2295usbd_get_bus_index(struct usb_device *udev)
2296{
2297	return ((uint8_t)device_get_unit(udev->bus->bdev));
2298}
2299
2300uint8_t
2301usbd_get_device_index(struct usb_device *udev)
2302{
2303	return (udev->device_index);
2304}
2305
2306#if USB_HAVE_UGEN
2307/*------------------------------------------------------------------------*
2308 *	usb_notify_addq
2309 *
2310 * This function will generate events for dev.
2311 *------------------------------------------------------------------------*/
2312static void
2313usb_notify_addq(const char *type, struct usb_device *udev)
2314{
2315	char *data = NULL;
2316	struct malloc_type *mt;
2317
2318	mtx_lock(&malloc_mtx);
2319	mt = malloc_desc2type("bus");	/* XXX M_BUS */
2320	mtx_unlock(&malloc_mtx);
2321	if (mt == NULL)
2322		return;
2323
2324	data = malloc(512, mt, M_NOWAIT);
2325	if (data == NULL)
2326		return;
2327
2328	/* String it all together. */
2329	snprintf(data, 1024,
2330	    "%s"
2331	    "%s "
2332	    "vendor=0x%04x "
2333	    "product=0x%04x "
2334	    "devclass=0x%02x "
2335	    "devsubclass=0x%02x "
2336	    "sernum=\"%s\" "
2337	    "at "
2338	    "port=%u "
2339	    "on "
2340	    "%s\n",
2341	    type,
2342	    udev->ugen_name,
2343	    UGETW(udev->ddesc.idVendor),
2344	    UGETW(udev->ddesc.idProduct),
2345	    udev->ddesc.bDeviceClass,
2346	    udev->ddesc.bDeviceSubClass,
2347	    udev->serial,
2348	    udev->port_no,
2349	    udev->parent_hub != NULL ?
2350		udev->parent_hub->ugen_name :
2351		device_get_nameunit(device_get_parent(udev->bus->bdev)));
2352
2353	devctl_queue_data(data);
2354}
2355
2356/*------------------------------------------------------------------------*
2357 *	usb_fifo_free_wrap
2358 *
2359 * This function will free the FIFOs.
2360 *
2361 * Description of "flag" argument: If the USB_UNCFG_FLAG_FREE_EP0 flag
2362 * is set and "iface_index" is set to "USB_IFACE_INDEX_ANY", we free
2363 * all FIFOs. If the USB_UNCFG_FLAG_FREE_EP0 flag is not set and
2364 * "iface_index" is set to "USB_IFACE_INDEX_ANY", we free all non
2365 * control endpoint FIFOs. If "iface_index" is not set to
2366 * "USB_IFACE_INDEX_ANY" the flag has no effect.
2367 *------------------------------------------------------------------------*/
2368static void
2369usb_fifo_free_wrap(struct usb_device *udev,
2370    uint8_t iface_index, uint8_t flag)
2371{
2372	struct usb_fifo *f;
2373	uint16_t i;
2374
2375	/*
2376	 * Free any USB FIFOs on the given interface:
2377	 */
2378	for (i = 0; i != USB_FIFO_MAX; i++) {
2379		f = udev->fifo[i];
2380		if (f == NULL) {
2381			continue;
2382		}
2383		/* Check if the interface index matches */
2384		if (iface_index == f->iface_index) {
2385			if (f->methods != &usb_ugen_methods) {
2386				/*
2387				 * Don't free any non-generic FIFOs in
2388				 * this case.
2389				 */
2390				continue;
2391			}
2392			if ((f->dev_ep_index == 0) &&
2393			    (f->fs_xfer == NULL)) {
2394				/* no need to free this FIFO */
2395				continue;
2396			}
2397		} else if (iface_index == USB_IFACE_INDEX_ANY) {
2398			if ((f->methods == &usb_ugen_methods) &&
2399			    (f->dev_ep_index == 0) &&
2400			    (!(flag & USB_UNCFG_FLAG_FREE_EP0)) &&
2401			    (f->fs_xfer == NULL)) {
2402				/* no need to free this FIFO */
2403				continue;
2404			}
2405		} else {
2406			/* no need to free this FIFO */
2407			continue;
2408		}
2409		/* free this FIFO */
2410		usb_fifo_free(f);
2411	}
2412}
2413#endif
2414
2415/*------------------------------------------------------------------------*
2416 *	usb_peer_can_wakeup
2417 *
2418 * Return values:
2419 * 0: Peer cannot do resume signalling.
2420 * Else: Peer can do resume signalling.
2421 *------------------------------------------------------------------------*/
2422uint8_t
2423usb_peer_can_wakeup(struct usb_device *udev)
2424{
2425	const struct usb_config_descriptor *cdp;
2426
2427	cdp = udev->cdesc;
2428	if ((cdp != NULL) && (udev->flags.usb_mode == USB_MODE_HOST)) {
2429		return (cdp->bmAttributes & UC_REMOTE_WAKEUP);
2430	}
2431	return (0);			/* not supported */
2432}
2433
2434void
2435usb_set_device_state(struct usb_device *udev, enum usb_dev_state state)
2436{
2437
2438	KASSERT(state < USB_STATE_MAX, ("invalid udev state"));
2439
2440	DPRINTF("udev %p state %s -> %s\n", udev,
2441	    usb_statestr(udev->state), usb_statestr(state));
2442	udev->state = state;
2443}
2444
2445uint8_t
2446usbd_device_attached(struct usb_device *udev)
2447{
2448	return (udev->state > USB_STATE_DETACHED);
2449}
2450