1184610Salfred/* $FreeBSD$ */
2184610Salfred/*-
3184610Salfred * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4184610Salfred *
5184610Salfred * Redistribution and use in source and binary forms, with or without
6184610Salfred * modification, are permitted provided that the following conditions
7184610Salfred * are met:
8184610Salfred * 1. Redistributions of source code must retain the above copyright
9184610Salfred *    notice, this list of conditions and the following disclaimer.
10184610Salfred * 2. Redistributions in binary form must reproduce the above copyright
11184610Salfred *    notice, this list of conditions and the following disclaimer in the
12184610Salfred *    documentation and/or other materials provided with the distribution.
13184610Salfred *
14184610Salfred * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17184610Salfred * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18184610Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19184610Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20184610Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21184610Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22184610Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23184610Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24184610Salfred * SUCH DAMAGE.
25184610Salfred */
26184610Salfred
27194677Sthompsa#include <sys/stdint.h>
28194677Sthompsa#include <sys/stddef.h>
29194677Sthompsa#include <sys/param.h>
30194677Sthompsa#include <sys/queue.h>
31194677Sthompsa#include <sys/types.h>
32194677Sthompsa#include <sys/systm.h>
33194677Sthompsa#include <sys/kernel.h>
34194677Sthompsa#include <sys/bus.h>
35194677Sthompsa#include <sys/module.h>
36194677Sthompsa#include <sys/lock.h>
37194677Sthompsa#include <sys/mutex.h>
38194677Sthompsa#include <sys/condvar.h>
39194677Sthompsa#include <sys/sysctl.h>
40194677Sthompsa#include <sys/sx.h>
41194677Sthompsa#include <sys/unistd.h>
42194677Sthompsa#include <sys/callout.h>
43194677Sthompsa#include <sys/malloc.h>
44194677Sthompsa#include <sys/priv.h>
45194677Sthompsa#include <sys/conf.h>
46194677Sthompsa#include <sys/fcntl.h>
47194677Sthompsa
48188942Sthompsa#include <dev/usb/usb.h>
49194677Sthompsa#include <dev/usb/usbdi.h>
50194677Sthompsa#include <dev/usb/usbdi_util.h>
51188942Sthompsa#include <dev/usb/usb_ioctl.h>
52213435Shselasky
53213435Shselasky#if USB_HAVE_UGEN
54213435Shselasky#include <sys/sbuf.h>
55213435Shselasky#endif
56213435Shselasky
57188746Sthompsa#include "usbdevs.h"
58184610Salfred
59194228Sthompsa#define	USB_DEBUG_VAR usb_debug
60184610Salfred
61188942Sthompsa#include <dev/usb/usb_core.h>
62188942Sthompsa#include <dev/usb/usb_debug.h>
63188942Sthompsa#include <dev/usb/usb_process.h>
64188942Sthompsa#include <dev/usb/usb_device.h>
65188942Sthompsa#include <dev/usb/usb_busdma.h>
66188942Sthompsa#include <dev/usb/usb_transfer.h>
67188942Sthompsa#include <dev/usb/usb_request.h>
68188942Sthompsa#include <dev/usb/usb_dynamic.h>
69188942Sthompsa#include <dev/usb/usb_hub.h>
70188942Sthompsa#include <dev/usb/usb_util.h>
71190180Sthompsa#include <dev/usb/usb_msctest.h>
72190180Sthompsa#if USB_HAVE_UGEN
73188942Sthompsa#include <dev/usb/usb_dev.h>
74188942Sthompsa#include <dev/usb/usb_generic.h>
75190180Sthompsa#endif
76184610Salfred
77188942Sthompsa#include <dev/usb/quirk/usb_quirk.h>
78184610Salfred
79188942Sthompsa#include <dev/usb/usb_controller.h>
80188942Sthompsa#include <dev/usb/usb_bus.h>
81184610Salfred
82190754Sthompsa/* function prototypes  */
83184610Salfred
84194228Sthompsastatic void	usb_init_endpoint(struct usb_device *, uint8_t,
85213435Shselasky		    struct usb_endpoint_descriptor *,
86213435Shselasky		    struct usb_endpoint_ss_comp_descriptor *,
87213435Shselasky		    struct usb_endpoint *);
88194228Sthompsastatic void	usb_unconfigure(struct usb_device *, uint8_t);
89194228Sthompsastatic void	usb_detach_device_sub(struct usb_device *, device_t *,
90214429Shselasky		    char **, uint8_t);
91194228Sthompsastatic uint8_t	usb_probe_and_attach_sub(struct usb_device *,
92192984Sthompsa		    struct usb_attach_arg *);
93194228Sthompsastatic void	usb_init_attach_arg(struct usb_device *,
94192984Sthompsa		    struct usb_attach_arg *);
95194228Sthompsastatic void	usb_suspend_resume_sub(struct usb_device *, device_t,
96185948Sthompsa		    uint8_t);
97261106Shselaskystatic usb_proc_callback_t usbd_clear_stall_proc;
98213435Shselaskystatic usb_error_t usb_config_parse(struct usb_device *, uint8_t, uint8_t);
99194228Sthompsastatic void	usbd_set_device_strings(struct usb_device *);
100214221Shselasky#if USB_HAVE_DEVCTL
101214221Shselaskystatic void	usb_notify_addq(const char *type, struct usb_device *);
102214221Shselasky#endif
103190191Sthompsa#if USB_HAVE_UGEN
104194228Sthompsastatic void	usb_fifo_free_wrap(struct usb_device *, uint8_t, uint8_t);
105194228Sthompsastatic void	usb_cdev_create(struct usb_device *);
106194228Sthompsastatic void	usb_cdev_free(struct usb_device *);
107189599Sthompsa#endif
108184610Salfred
109184610Salfred/* This variable is global to allow easy access to it: */
110184610Salfred
111194228Sthompsaint	usb_template = 0;
112184610Salfred
113197559SthompsaTUNABLE_INT("hw.usb.usb_template", &usb_template);
114242775ShselaskySYSCTL_INT(_hw_usb, OID_AUTO, template, CTLFLAG_RW | CTLFLAG_TUN,
115194228Sthompsa    &usb_template, 0, "Selected USB device side template");
116184610Salfred
117197559Sthompsa/* English is default language */
118197559Sthompsa
119197559Sthompsastatic int usb_lang_id = 0x0009;
120197559Sthompsastatic int usb_lang_mask = 0x00FF;
121197559Sthompsa
122197559SthompsaTUNABLE_INT("hw.usb.usb_lang_id", &usb_lang_id);
123242775ShselaskySYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_id, CTLFLAG_RW | CTLFLAG_TUN,
124197559Sthompsa    &usb_lang_id, 0, "Preferred USB language ID");
125197559Sthompsa
126197559SthompsaTUNABLE_INT("hw.usb.usb_lang_mask", &usb_lang_mask);
127242775ShselaskySYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_mask, CTLFLAG_RW | CTLFLAG_TUN,
128197559Sthompsa    &usb_lang_mask, 0, "Preferred USB language mask");
129197559Sthompsa
130191498Sthompsastatic const char* statestr[USB_STATE_MAX] = {
131191498Sthompsa	[USB_STATE_DETACHED]	= "DETACHED",
132191498Sthompsa	[USB_STATE_ATTACHED]	= "ATTACHED",
133191498Sthompsa	[USB_STATE_POWERED]	= "POWERED",
134191498Sthompsa	[USB_STATE_ADDRESSED]	= "ADDRESSED",
135191498Sthompsa	[USB_STATE_CONFIGURED]	= "CONFIGURED",
136191498Sthompsa};
137184610Salfred
138191498Sthompsaconst char *
139194228Sthompsausb_statestr(enum usb_dev_state state)
140191498Sthompsa{
141191824Sthompsa	return ((state < USB_STATE_MAX) ? statestr[state] : "UNKNOWN");
142191498Sthompsa}
143191498Sthompsa
144212136Sthompsaconst char *
145212136Sthompsausb_get_manufacturer(struct usb_device *udev)
146212136Sthompsa{
147212136Sthompsa	return (udev->manufacturer ? udev->manufacturer : "Unknown");
148212136Sthompsa}
149212136Sthompsa
150212136Sthompsaconst char *
151212136Sthompsausb_get_product(struct usb_device *udev)
152212136Sthompsa{
153212136Sthompsa	return (udev->product ? udev->product : "");
154212136Sthompsa}
155212136Sthompsa
156212136Sthompsaconst char *
157212136Sthompsausb_get_serial(struct usb_device *udev)
158212136Sthompsa{
159212136Sthompsa	return (udev->serial ? udev->serial : "");
160212136Sthompsa}
161212136Sthompsa
162184610Salfred/*------------------------------------------------------------------------*
163194228Sthompsa *	usbd_get_ep_by_addr
164184610Salfred *
165193644Sthompsa * This function searches for an USB ep by endpoint address and
166184610Salfred * direction.
167184610Salfred *
168184610Salfred * Returns:
169184610Salfred * NULL: Failure
170184610Salfred * Else: Success
171184610Salfred *------------------------------------------------------------------------*/
172193644Sthompsastruct usb_endpoint *
173194228Sthompsausbd_get_ep_by_addr(struct usb_device *udev, uint8_t ea_val)
174184610Salfred{
175193644Sthompsa	struct usb_endpoint *ep = udev->endpoints;
176193644Sthompsa	struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max;
177184610Salfred	enum {
178184610Salfred		EA_MASK = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR),
179184610Salfred	};
180184610Salfred
181184610Salfred	/*
182184610Salfred	 * According to the USB specification not all bits are used
183184610Salfred	 * for the endpoint address. Keep defined bits only:
184184610Salfred	 */
185184610Salfred	ea_val &= EA_MASK;
186184610Salfred
187184610Salfred	/*
188193644Sthompsa	 * Iterate accross all the USB endpoints searching for a match
189184610Salfred	 * based on the endpoint address:
190184610Salfred	 */
191193644Sthompsa	for (; ep != ep_end; ep++) {
192184610Salfred
193193644Sthompsa		if (ep->edesc == NULL) {
194184610Salfred			continue;
195184610Salfred		}
196184610Salfred		/* do the mask and check the value */
197193644Sthompsa		if ((ep->edesc->bEndpointAddress & EA_MASK) == ea_val) {
198184610Salfred			goto found;
199184610Salfred		}
200184610Salfred	}
201184610Salfred
202184610Salfred	/*
203193644Sthompsa	 * The default endpoint is always present and is checked separately:
204184610Salfred	 */
205207080Sthompsa	if ((udev->ctrl_ep.edesc) &&
206207080Sthompsa	    ((udev->ctrl_ep.edesc->bEndpointAddress & EA_MASK) == ea_val)) {
207207080Sthompsa		ep = &udev->ctrl_ep;
208184610Salfred		goto found;
209184610Salfred	}
210184610Salfred	return (NULL);
211184610Salfred
212184610Salfredfound:
213193644Sthompsa	return (ep);
214184610Salfred}
215184610Salfred
216184610Salfred/*------------------------------------------------------------------------*
217194228Sthompsa *	usbd_get_endpoint
218184610Salfred *
219193644Sthompsa * This function searches for an USB endpoint based on the information
220192984Sthompsa * given by the passed "struct usb_config" pointer.
221184610Salfred *
222184610Salfred * Return values:
223184610Salfred * NULL: No match.
224193644Sthompsa * Else: Pointer to "struct usb_endpoint".
225184610Salfred *------------------------------------------------------------------------*/
226193644Sthompsastruct usb_endpoint *
227194228Sthompsausbd_get_endpoint(struct usb_device *udev, uint8_t iface_index,
228192984Sthompsa    const struct usb_config *setup)
229184610Salfred{
230193644Sthompsa	struct usb_endpoint *ep = udev->endpoints;
231193644Sthompsa	struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max;
232184610Salfred	uint8_t index = setup->ep_index;
233184610Salfred	uint8_t ea_mask;
234184610Salfred	uint8_t ea_val;
235184610Salfred	uint8_t type_mask;
236184610Salfred	uint8_t type_val;
237184610Salfred
238184610Salfred	DPRINTFN(10, "udev=%p iface_index=%d address=0x%x "
239184610Salfred	    "type=0x%x dir=0x%x index=%d\n",
240184610Salfred	    udev, iface_index, setup->endpoint,
241184610Salfred	    setup->type, setup->direction, setup->ep_index);
242184610Salfred
243190734Sthompsa	/* check USB mode */
244190734Sthompsa
245192499Sthompsa	if (setup->usb_mode != USB_MODE_DUAL &&
246192499Sthompsa	    udev->flags.usb_mode != setup->usb_mode) {
247193644Sthompsa		/* wrong mode - no endpoint */
248190734Sthompsa		return (NULL);
249190734Sthompsa	}
250190734Sthompsa
251184610Salfred	/* setup expected endpoint direction mask and value */
252184610Salfred
253190732Sthompsa	if (setup->direction == UE_DIR_RX) {
254190732Sthompsa		ea_mask = (UE_DIR_IN | UE_DIR_OUT);
255192499Sthompsa		ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ?
256190732Sthompsa		    UE_DIR_OUT : UE_DIR_IN;
257190732Sthompsa	} else if (setup->direction == UE_DIR_TX) {
258190732Sthompsa		ea_mask = (UE_DIR_IN | UE_DIR_OUT);
259192499Sthompsa		ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ?
260190732Sthompsa		    UE_DIR_IN : UE_DIR_OUT;
261190732Sthompsa	} else if (setup->direction == UE_DIR_ANY) {
262184610Salfred		/* match any endpoint direction */
263184610Salfred		ea_mask = 0;
264184610Salfred		ea_val = 0;
265184610Salfred	} else {
266184610Salfred		/* match the given endpoint direction */
267184610Salfred		ea_mask = (UE_DIR_IN | UE_DIR_OUT);
268184610Salfred		ea_val = (setup->direction & (UE_DIR_IN | UE_DIR_OUT));
269184610Salfred	}
270184610Salfred
271184610Salfred	/* setup expected endpoint address */
272184610Salfred
273184610Salfred	if (setup->endpoint == UE_ADDR_ANY) {
274184610Salfred		/* match any endpoint address */
275184610Salfred	} else {
276184610Salfred		/* match the given endpoint address */
277184610Salfred		ea_mask |= UE_ADDR;
278184610Salfred		ea_val |= (setup->endpoint & UE_ADDR);
279184610Salfred	}
280184610Salfred
281184610Salfred	/* setup expected endpoint type */
282184610Salfred
283184610Salfred	if (setup->type == UE_BULK_INTR) {
284184610Salfred		/* this will match BULK and INTERRUPT endpoints */
285184610Salfred		type_mask = 2;
286184610Salfred		type_val = 2;
287184610Salfred	} else if (setup->type == UE_TYPE_ANY) {
288184610Salfred		/* match any endpoint type */
289184610Salfred		type_mask = 0;
290184610Salfred		type_val = 0;
291184610Salfred	} else {
292184610Salfred		/* match the given endpoint type */
293184610Salfred		type_mask = UE_XFERTYPE;
294184610Salfred		type_val = (setup->type & UE_XFERTYPE);
295184610Salfred	}
296184610Salfred
297184610Salfred	/*
298193644Sthompsa	 * Iterate accross all the USB endpoints searching for a match
299184610Salfred	 * based on the endpoint address. Note that we are searching
300193644Sthompsa	 * the endpoints from the beginning of the "udev->endpoints" array.
301184610Salfred	 */
302193644Sthompsa	for (; ep != ep_end; ep++) {
303184610Salfred
304193644Sthompsa		if ((ep->edesc == NULL) ||
305193644Sthompsa		    (ep->iface_index != iface_index)) {
306184610Salfred			continue;
307184610Salfred		}
308184610Salfred		/* do the masks and check the values */
309184610Salfred
310193644Sthompsa		if (((ep->edesc->bEndpointAddress & ea_mask) == ea_val) &&
311193644Sthompsa		    ((ep->edesc->bmAttributes & type_mask) == type_val)) {
312184610Salfred			if (!index--) {
313184610Salfred				goto found;
314184610Salfred			}
315184610Salfred		}
316184610Salfred	}
317184610Salfred
318184610Salfred	/*
319193644Sthompsa	 * Match against default endpoint last, so that "any endpoint", "any
320193644Sthompsa	 * address" and "any direction" returns the first endpoint of the
321184610Salfred	 * interface. "iface_index" and "direction" is ignored:
322184610Salfred	 */
323207080Sthompsa	if ((udev->ctrl_ep.edesc) &&
324207080Sthompsa	    ((udev->ctrl_ep.edesc->bEndpointAddress & ea_mask) == ea_val) &&
325207080Sthompsa	    ((udev->ctrl_ep.edesc->bmAttributes & type_mask) == type_val) &&
326184610Salfred	    (!index)) {
327207080Sthompsa		ep = &udev->ctrl_ep;
328184610Salfred		goto found;
329184610Salfred	}
330184610Salfred	return (NULL);
331184610Salfred
332184610Salfredfound:
333193644Sthompsa	return (ep);
334184610Salfred}
335184610Salfred
336184610Salfred/*------------------------------------------------------------------------*
337194677Sthompsa *	usbd_interface_count
338184610Salfred *
339184610Salfred * This function stores the number of USB interfaces excluding
340184610Salfred * alternate settings, which the USB config descriptor reports into
341184610Salfred * the unsigned 8-bit integer pointed to by "count".
342184610Salfred *
343184610Salfred * Returns:
344184610Salfred *    0: Success
345184610Salfred * Else: Failure
346184610Salfred *------------------------------------------------------------------------*/
347193045Sthompsausb_error_t
348194677Sthompsausbd_interface_count(struct usb_device *udev, uint8_t *count)
349184610Salfred{
350184610Salfred	if (udev->cdesc == NULL) {
351184610Salfred		*count = 0;
352184610Salfred		return (USB_ERR_NOT_CONFIGURED);
353184610Salfred	}
354190730Sthompsa	*count = udev->ifaces_max;
355184610Salfred	return (USB_ERR_NORMAL_COMPLETION);
356184610Salfred}
357184610Salfred
358184610Salfred
359184610Salfred/*------------------------------------------------------------------------*
360194228Sthompsa *	usb_init_endpoint
361184610Salfred *
362193644Sthompsa * This function will initialise the USB endpoint structure pointed to by
363193644Sthompsa * the "endpoint" argument. The structure pointed to by "endpoint" must be
364190730Sthompsa * zeroed before calling this function.
365184610Salfred *------------------------------------------------------------------------*/
366184610Salfredstatic void
367194228Sthompsausb_init_endpoint(struct usb_device *udev, uint8_t iface_index,
368213435Shselasky    struct usb_endpoint_descriptor *edesc,
369213435Shselasky    struct usb_endpoint_ss_comp_descriptor *ecomp,
370213435Shselasky    struct usb_endpoint *ep)
371184610Salfred{
372192984Sthompsa	struct usb_bus_methods *methods;
373189110Sthompsa
374190730Sthompsa	methods = udev->bus->methods;
375184610Salfred
376193644Sthompsa	(methods->endpoint_init) (udev, edesc, ep);
377184610Salfred
378193644Sthompsa	/* initialise USB endpoint structure */
379193644Sthompsa	ep->edesc = edesc;
380213435Shselasky	ep->ecomp = ecomp;
381193644Sthompsa	ep->iface_index = iface_index;
382193644Sthompsa	TAILQ_INIT(&ep->endpoint_q.head);
383194228Sthompsa	ep->endpoint_q.command = &usbd_pipe_start;
384184610Salfred
385190735Sthompsa	/* the pipe is not supported by the hardware */
386193644Sthompsa 	if (ep->methods == NULL)
387190735Sthompsa		return;
388190735Sthompsa
389184610Salfred	/* clear stall, if any */
390190730Sthompsa	if (methods->clear_stall != NULL) {
391184824Sthompsa		USB_BUS_LOCK(udev->bus);
392193644Sthompsa		(methods->clear_stall) (udev, ep);
393184824Sthompsa		USB_BUS_UNLOCK(udev->bus);
394184610Salfred	}
395184610Salfred}
396184610Salfred
397190730Sthompsa/*-----------------------------------------------------------------------*
398194228Sthompsa *	usb_endpoint_foreach
399188985Sthompsa *
400188985Sthompsa * This function will iterate all the USB endpoints except the control
401188985Sthompsa * endpoint. This function is NULL safe.
402188985Sthompsa *
403188985Sthompsa * Return values:
404193644Sthompsa * NULL: End of USB endpoints
405193644Sthompsa * Else: Pointer to next USB endpoint
406188985Sthompsa *------------------------------------------------------------------------*/
407193644Sthompsastruct usb_endpoint *
408194228Sthompsausb_endpoint_foreach(struct usb_device *udev, struct usb_endpoint *ep)
409188985Sthompsa{
410197558Sthompsa	struct usb_endpoint *ep_end;
411188985Sthompsa
412188985Sthompsa	/* be NULL safe */
413188985Sthompsa	if (udev == NULL)
414188985Sthompsa		return (NULL);
415188985Sthompsa
416197558Sthompsa	ep_end = udev->endpoints + udev->endpoints_max;
417197558Sthompsa
418193644Sthompsa	/* get next endpoint */
419193644Sthompsa	if (ep == NULL)
420193644Sthompsa		ep = udev->endpoints;
421188985Sthompsa	else
422193644Sthompsa		ep++;
423188985Sthompsa
424193644Sthompsa	/* find next allocated ep */
425193644Sthompsa	while (ep != ep_end) {
426193644Sthompsa		if (ep->edesc != NULL)
427193644Sthompsa			return (ep);
428193644Sthompsa		ep++;
429188985Sthompsa	}
430188985Sthompsa	return (NULL);
431188985Sthompsa}
432188985Sthompsa
433188985Sthompsa/*------------------------------------------------------------------------*
434263800Shselasky *	usb_wait_pending_ref_locked
435263800Shselasky *
436263800Shselasky * This function will wait for any USB references to go away before
437263800Shselasky * returning and disable further USB device refcounting on the
438263800Shselasky * specified USB device. This function is used when detaching a USB
439263800Shselasky * device.
440263800Shselasky *------------------------------------------------------------------------*/
441263800Shselaskystatic void
442263800Shselaskyusb_wait_pending_ref_locked(struct usb_device *udev)
443263800Shselasky{
444263800Shselasky#if USB_HAVE_UGEN
445263800Shselasky	const uint16_t refcount =
446263800Shselasky	    usb_proc_is_called_from(
447263800Shselasky	    &udev->bus->explore_proc) ? 1 : 2;
448263800Shselasky
449263800Shselasky	DPRINTF("Refcount = %d\n", (int)refcount);
450263800Shselasky
451263800Shselasky	while (1) {
452263800Shselasky		/* wait for any pending references to go away */
453263800Shselasky		mtx_lock(&usb_ref_lock);
454263800Shselasky		if (udev->refcount == refcount) {
455263800Shselasky			/* prevent further refs being taken */
456263800Shselasky			udev->refcount = USB_DEV_REF_MAX;
457263800Shselasky			mtx_unlock(&usb_ref_lock);
458263800Shselasky			break;
459263800Shselasky		}
460263800Shselasky		usbd_enum_unlock(udev);
461263800Shselasky		cv_wait(&udev->ref_cv, &usb_ref_lock);
462263800Shselasky		mtx_unlock(&usb_ref_lock);
463263800Shselasky		(void) usbd_enum_lock(udev);
464263800Shselasky	}
465263800Shselasky#endif
466263800Shselasky}
467263800Shselasky
468263800Shselasky/*------------------------------------------------------------------------*
469263800Shselasky *	usb_ref_restore_locked
470263800Shselasky *
471263800Shselasky * This function will restore the reference count value after a call
472263800Shselasky * to "usb_wait_pending_ref_locked()".
473263800Shselasky *------------------------------------------------------------------------*/
474263800Shselaskystatic void
475263800Shselaskyusb_ref_restore_locked(struct usb_device *udev)
476263800Shselasky{
477263800Shselasky#if USB_HAVE_UGEN
478263800Shselasky	const uint16_t refcount =
479263800Shselasky	    usb_proc_is_called_from(
480263800Shselasky	    &udev->bus->explore_proc) ? 1 : 2;
481263800Shselasky
482263800Shselasky	DPRINTF("Refcount = %d\n", (int)refcount);
483263800Shselasky
484263800Shselasky	/* restore reference count and wakeup waiters, if any */
485263800Shselasky	mtx_lock(&usb_ref_lock);
486263800Shselasky	udev->refcount = refcount;
487263800Shselasky	cv_broadcast(&udev->ref_cv);
488263800Shselasky	mtx_unlock(&usb_ref_lock);
489263800Shselasky#endif
490263800Shselasky}
491263800Shselasky
492263800Shselasky/*------------------------------------------------------------------------*
493194228Sthompsa *	usb_unconfigure
494184610Salfred *
495193644Sthompsa * This function will free all USB interfaces and USB endpoints belonging
496190730Sthompsa * to an USB device.
497190730Sthompsa *
498190730Sthompsa * Flag values, see "USB_UNCFG_FLAG_XXX".
499184610Salfred *------------------------------------------------------------------------*/
500190730Sthompsastatic void
501194228Sthompsausb_unconfigure(struct usb_device *udev, uint8_t flag)
502184610Salfred{
503190730Sthompsa	uint8_t do_unlock;
504184610Salfred
505247090Shselasky	/* Prevent re-enumeration */
506247090Shselasky	do_unlock = usbd_enum_lock(udev);
507184610Salfred
508190730Sthompsa	/* detach all interface drivers */
509194228Sthompsa	usb_detach_device(udev, USB_IFACE_INDEX_ANY, flag);
510184610Salfred
511190730Sthompsa#if USB_HAVE_UGEN
512190730Sthompsa	/* free all FIFOs except control endpoint FIFOs */
513194228Sthompsa	usb_fifo_free_wrap(udev, USB_IFACE_INDEX_ANY, flag);
514184610Salfred
515184610Salfred	/*
516190730Sthompsa	 * Free all cdev's, if any.
517184610Salfred	 */
518194228Sthompsa	usb_cdev_free(udev);
519190730Sthompsa#endif
520184610Salfred
521190180Sthompsa#if USB_HAVE_COMPAT_LINUX
522184610Salfred	/* free Linux compat device, if any */
523192984Sthompsa	if (udev->linux_endpoint_start) {
524192984Sthompsa		usb_linux_free_device(udev);
525192984Sthompsa		udev->linux_endpoint_start = NULL;
526184610Salfred	}
527190180Sthompsa#endif
528184610Salfred
529194228Sthompsa	usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_FREE);
530184610Salfred
531193644Sthompsa	/* free "cdesc" after "ifaces" and "endpoints", if any */
532190727Sthompsa	if (udev->cdesc != NULL) {
533192499Sthompsa		if (udev->flags.usb_mode != USB_MODE_DEVICE)
534190727Sthompsa			free(udev->cdesc, M_USB);
535184610Salfred		udev->cdesc = NULL;
536184610Salfred	}
537184610Salfred	/* set unconfigured state */
538184610Salfred	udev->curr_config_no = USB_UNCONFIG_NO;
539184610Salfred	udev->curr_config_index = USB_UNCONFIG_INDEX;
540190730Sthompsa
541196498Salfred	if (do_unlock)
542196498Salfred		usbd_enum_unlock(udev);
543184610Salfred}
544184610Salfred
545184610Salfred/*------------------------------------------------------------------------*
546194228Sthompsa *	usbd_set_config_index
547184610Salfred *
548184610Salfred * This function selects configuration by index, independent of the
549184610Salfred * actual configuration number. This function should not be used by
550184610Salfred * USB drivers.
551184610Salfred *
552184610Salfred * Returns:
553184610Salfred *    0: Success
554184610Salfred * Else: Failure
555184610Salfred *------------------------------------------------------------------------*/
556193045Sthompsausb_error_t
557194228Sthompsausbd_set_config_index(struct usb_device *udev, uint8_t index)
558184610Salfred{
559192984Sthompsa	struct usb_status ds;
560192984Sthompsa	struct usb_config_descriptor *cdp;
561184610Salfred	uint16_t power;
562184610Salfred	uint16_t max_power;
563184610Salfred	uint8_t selfpowered;
564184610Salfred	uint8_t do_unlock;
565193045Sthompsa	usb_error_t err;
566184610Salfred
567184610Salfred	DPRINTFN(6, "udev=%p index=%d\n", udev, index);
568184610Salfred
569247090Shselasky	/* Prevent re-enumeration */
570247090Shselasky	do_unlock = usbd_enum_lock(udev);
571184610Salfred
572197553Sthompsa	usb_unconfigure(udev, 0);
573184610Salfred
574184610Salfred	if (index == USB_UNCONFIG_INDEX) {
575184610Salfred		/*
576184610Salfred		 * Leave unallocated when unconfiguring the
577194228Sthompsa		 * device. "usb_unconfigure()" will also reset
578184610Salfred		 * the current config number and index.
579184610Salfred		 */
580194228Sthompsa		err = usbd_req_set_config(udev, NULL, USB_UNCONFIG_NO);
581191494Sthompsa		if (udev->state == USB_STATE_CONFIGURED)
582194228Sthompsa			usb_set_device_state(udev, USB_STATE_ADDRESSED);
583184610Salfred		goto done;
584184610Salfred	}
585184610Salfred	/* get the full config descriptor */
586192499Sthompsa	if (udev->flags.usb_mode == USB_MODE_DEVICE) {
587190727Sthompsa		/* save some memory */
588194228Sthompsa		err = usbd_req_get_descriptor_ptr(udev, &cdp,
589191402Sthompsa		    (UDESC_CONFIG << 8) | index);
590190727Sthompsa	} else {
591190727Sthompsa		/* normal request */
592194228Sthompsa		err = usbd_req_get_config_desc_full(udev,
593190727Sthompsa		    NULL, &cdp, M_USB, index);
594190727Sthompsa	}
595184610Salfred	if (err) {
596184610Salfred		goto done;
597184610Salfred	}
598184610Salfred	/* set the new config descriptor */
599184610Salfred
600184610Salfred	udev->cdesc = cdp;
601184610Salfred
602184610Salfred	/* Figure out if the device is self or bus powered. */
603184610Salfred	selfpowered = 0;
604184610Salfred	if ((!udev->flags.uq_bus_powered) &&
605184610Salfred	    (cdp->bmAttributes & UC_SELF_POWERED) &&
606192499Sthompsa	    (udev->flags.usb_mode == USB_MODE_HOST)) {
607184610Salfred		/* May be self powered. */
608184610Salfred		if (cdp->bmAttributes & UC_BUS_POWERED) {
609184610Salfred			/* Must ask device. */
610194228Sthompsa			err = usbd_req_get_device_status(udev, NULL, &ds);
611190743Sthompsa			if (err) {
612190743Sthompsa				DPRINTFN(0, "could not read "
613190743Sthompsa				    "device status: %s\n",
614194228Sthompsa				    usbd_errstr(err));
615190743Sthompsa			} else if (UGETW(ds.wStatus) & UDS_SELF_POWERED) {
616190743Sthompsa				selfpowered = 1;
617184610Salfred			}
618190743Sthompsa			DPRINTF("status=0x%04x \n",
619190743Sthompsa				UGETW(ds.wStatus));
620184610Salfred		} else
621184610Salfred			selfpowered = 1;
622184610Salfred	}
623184610Salfred	DPRINTF("udev=%p cdesc=%p (addr %d) cno=%d attr=0x%02x, "
624184610Salfred	    "selfpowered=%d, power=%d\n",
625184610Salfred	    udev, cdp,
626190328Sthompsa	    udev->address, cdp->bConfigurationValue, cdp->bmAttributes,
627184610Salfred	    selfpowered, cdp->bMaxPower * 2);
628184610Salfred
629184610Salfred	/* Check if we have enough power. */
630184610Salfred	power = cdp->bMaxPower * 2;
631184610Salfred
632184610Salfred	if (udev->parent_hub) {
633184610Salfred		max_power = udev->parent_hub->hub->portpower;
634184610Salfred	} else {
635184610Salfred		max_power = USB_MAX_POWER;
636184610Salfred	}
637184610Salfred
638184610Salfred	if (power > max_power) {
639184610Salfred		DPRINTFN(0, "power exceeded %d > %d\n", power, max_power);
640184610Salfred		err = USB_ERR_NO_POWER;
641184610Salfred		goto done;
642184610Salfred	}
643184610Salfred	/* Only update "self_powered" in USB Host Mode */
644192499Sthompsa	if (udev->flags.usb_mode == USB_MODE_HOST) {
645184610Salfred		udev->flags.self_powered = selfpowered;
646184610Salfred	}
647184610Salfred	udev->power = power;
648184610Salfred	udev->curr_config_no = cdp->bConfigurationValue;
649184610Salfred	udev->curr_config_index = index;
650194228Sthompsa	usb_set_device_state(udev, USB_STATE_CONFIGURED);
651184610Salfred
652184610Salfred	/* Set the actual configuration value. */
653194228Sthompsa	err = usbd_req_set_config(udev, NULL, cdp->bConfigurationValue);
654184610Salfred	if (err) {
655184610Salfred		goto done;
656184610Salfred	}
657190730Sthompsa
658194228Sthompsa	err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_ALLOC);
659190730Sthompsa	if (err) {
660190730Sthompsa		goto done;
661184610Salfred	}
662190730Sthompsa
663194228Sthompsa	err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_INIT);
664190730Sthompsa	if (err) {
665190730Sthompsa		goto done;
666190730Sthompsa	}
667190730Sthompsa
668189599Sthompsa#if USB_HAVE_UGEN
669189110Sthompsa	/* create device nodes for each endpoint */
670194228Sthompsa	usb_cdev_create(udev);
671189599Sthompsa#endif
672184610Salfred
673184610Salfreddone:
674194228Sthompsa	DPRINTF("error=%s\n", usbd_errstr(err));
675184610Salfred	if (err) {
676197553Sthompsa		usb_unconfigure(udev, 0);
677184610Salfred	}
678196498Salfred	if (do_unlock)
679196498Salfred		usbd_enum_unlock(udev);
680184610Salfred	return (err);
681184610Salfred}
682184610Salfred
683184610Salfred/*------------------------------------------------------------------------*
684194228Sthompsa *	usb_config_parse
685190730Sthompsa *
686193644Sthompsa * This function will allocate and free USB interfaces and USB endpoints,
687193644Sthompsa * parse the USB configuration structure and initialise the USB endpoints
688190730Sthompsa * and interfaces. If "iface_index" is not equal to
689190730Sthompsa * "USB_IFACE_INDEX_ANY" then the "cmd" parameter is the
690190730Sthompsa * alternate_setting to be selected for the given interface. Else the
691190730Sthompsa * "cmd" parameter is defined by "USB_CFG_XXX". "iface_index" can be
692190730Sthompsa * "USB_IFACE_INDEX_ANY" or a valid USB interface index. This function
693190730Sthompsa * is typically called when setting the configuration or when setting
694190730Sthompsa * an alternate interface.
695190730Sthompsa *
696190730Sthompsa * Returns:
697190730Sthompsa *    0: Success
698190730Sthompsa * Else: Failure
699190730Sthompsa *------------------------------------------------------------------------*/
700213435Shselaskystatic usb_error_t
701194228Sthompsausb_config_parse(struct usb_device *udev, uint8_t iface_index, uint8_t cmd)
702190730Sthompsa{
703192984Sthompsa	struct usb_idesc_parse_state ips;
704192984Sthompsa	struct usb_interface_descriptor *id;
705192984Sthompsa	struct usb_endpoint_descriptor *ed;
706192984Sthompsa	struct usb_interface *iface;
707193644Sthompsa	struct usb_endpoint *ep;
708193045Sthompsa	usb_error_t err;
709190730Sthompsa	uint8_t ep_curr;
710190730Sthompsa	uint8_t ep_max;
711190730Sthompsa	uint8_t temp;
712190730Sthompsa	uint8_t do_init;
713190730Sthompsa	uint8_t alt_index;
714190730Sthompsa
715190730Sthompsa	if (iface_index != USB_IFACE_INDEX_ANY) {
716190730Sthompsa		/* parameter overload */
717190730Sthompsa		alt_index = cmd;
718190730Sthompsa		cmd = USB_CFG_INIT;
719190730Sthompsa	} else {
720190730Sthompsa		/* not used */
721190730Sthompsa		alt_index = 0;
722190730Sthompsa	}
723190730Sthompsa
724190730Sthompsa	err = 0;
725190730Sthompsa
726190730Sthompsa	DPRINTFN(5, "iface_index=%d cmd=%d\n",
727190730Sthompsa	    iface_index, cmd);
728190730Sthompsa
729190730Sthompsa	if (cmd == USB_CFG_FREE)
730190730Sthompsa		goto cleanup;
731190730Sthompsa
732190730Sthompsa	if (cmd == USB_CFG_INIT) {
733207079Sthompsa		sx_assert(&udev->enum_sx, SA_LOCKED);
734190730Sthompsa
735193644Sthompsa		/* check for in-use endpoints */
736190730Sthompsa
737193644Sthompsa		ep = udev->endpoints;
738193644Sthompsa		ep_max = udev->endpoints_max;
739190730Sthompsa		while (ep_max--) {
740193644Sthompsa			/* look for matching endpoints */
741190730Sthompsa			if ((iface_index == USB_IFACE_INDEX_ANY) ||
742193644Sthompsa			    (iface_index == ep->iface_index)) {
743199672Sthompsa				if (ep->refcount_alloc != 0) {
744190730Sthompsa					/*
745190730Sthompsa					 * This typically indicates a
746190730Sthompsa					 * more serious error.
747190730Sthompsa					 */
748190730Sthompsa					err = USB_ERR_IN_USE;
749190730Sthompsa				} else {
750193644Sthompsa					/* reset endpoint */
751193644Sthompsa					memset(ep, 0, sizeof(*ep));
752193644Sthompsa					/* make sure we don't zero the endpoint again */
753193644Sthompsa					ep->iface_index = USB_IFACE_INDEX_ANY;
754190730Sthompsa				}
755190730Sthompsa			}
756193644Sthompsa			ep++;
757190730Sthompsa		}
758190730Sthompsa
759190730Sthompsa		if (err)
760190730Sthompsa			return (err);
761190730Sthompsa	}
762190730Sthompsa
763190730Sthompsa	memset(&ips, 0, sizeof(ips));
764190730Sthompsa
765190730Sthompsa	ep_curr = 0;
766190730Sthompsa	ep_max = 0;
767190730Sthompsa
768194228Sthompsa	while ((id = usb_idesc_foreach(udev->cdesc, &ips))) {
769190730Sthompsa
770190730Sthompsa		/* check for interface overflow */
771190730Sthompsa		if (ips.iface_index == USB_IFACE_MAX)
772190730Sthompsa			break;			/* crazy */
773190730Sthompsa
774190730Sthompsa		iface = udev->ifaces + ips.iface_index;
775190730Sthompsa
776190730Sthompsa		/* check for specific interface match */
777190730Sthompsa
778190730Sthompsa		if (cmd == USB_CFG_INIT) {
779190730Sthompsa			if ((iface_index != USB_IFACE_INDEX_ANY) &&
780190730Sthompsa			    (iface_index != ips.iface_index)) {
781190730Sthompsa				/* wrong interface */
782190730Sthompsa				do_init = 0;
783190730Sthompsa			} else if (alt_index != ips.iface_index_alt) {
784190730Sthompsa				/* wrong alternate setting */
785190730Sthompsa				do_init = 0;
786190730Sthompsa			} else {
787190730Sthompsa				/* initialise interface */
788190730Sthompsa				do_init = 1;
789190730Sthompsa			}
790190730Sthompsa		} else
791190730Sthompsa			do_init = 0;
792190730Sthompsa
793190730Sthompsa		/* check for new interface */
794190730Sthompsa		if (ips.iface_index_alt == 0) {
795190730Sthompsa			/* update current number of endpoints */
796190730Sthompsa			ep_curr = ep_max;
797190730Sthompsa		}
798190730Sthompsa		/* check for init */
799190730Sthompsa		if (do_init) {
800190730Sthompsa			/* setup the USB interface structure */
801190730Sthompsa			iface->idesc = id;
802190730Sthompsa			/* set alternate index */
803190730Sthompsa			iface->alt_index = alt_index;
804236895Shselasky			/* set default interface parent */
805236895Shselasky			if (iface_index == USB_IFACE_INDEX_ANY) {
806236895Shselasky				iface->parent_iface_index =
807236895Shselasky				    USB_IFACE_INDEX_ANY;
808236895Shselasky			}
809190730Sthompsa		}
810190730Sthompsa
811190730Sthompsa		DPRINTFN(5, "found idesc nendpt=%d\n", id->bNumEndpoints);
812190730Sthompsa
813192984Sthompsa		ed = (struct usb_endpoint_descriptor *)id;
814190730Sthompsa
815190730Sthompsa		temp = ep_curr;
816190730Sthompsa
817190730Sthompsa		/* iterate all the endpoint descriptors */
818194228Sthompsa		while ((ed = usb_edesc_foreach(udev->cdesc, ed))) {
819190730Sthompsa
820190730Sthompsa			if (temp == USB_EP_MAX)
821190730Sthompsa				break;			/* crazy */
822190730Sthompsa
823193644Sthompsa			ep = udev->endpoints + temp;
824190730Sthompsa
825190730Sthompsa			if (do_init) {
826213435Shselasky				void *ecomp;
827213435Shselasky
828213435Shselasky				ecomp = usb_ed_comp_foreach(udev->cdesc, (void *)ed);
829213435Shselasky				if (ecomp != NULL)
830213435Shselasky					DPRINTFN(5, "Found endpoint companion descriptor\n");
831213435Shselasky
832194228Sthompsa				usb_init_endpoint(udev,
833213435Shselasky				    ips.iface_index, ed, ecomp, ep);
834190730Sthompsa			}
835190730Sthompsa
836190730Sthompsa			temp ++;
837190730Sthompsa
838190730Sthompsa			/* find maximum number of endpoints */
839190730Sthompsa			if (ep_max < temp)
840190730Sthompsa				ep_max = temp;
841190730Sthompsa
842190730Sthompsa			/* optimalisation */
843192984Sthompsa			id = (struct usb_interface_descriptor *)ed;
844190730Sthompsa		}
845190730Sthompsa	}
846190730Sthompsa
847190730Sthompsa	/* NOTE: It is valid to have no interfaces and no endpoints! */
848190730Sthompsa
849190730Sthompsa	if (cmd == USB_CFG_ALLOC) {
850190730Sthompsa		udev->ifaces_max = ips.iface_index;
851190730Sthompsa		udev->ifaces = NULL;
852190730Sthompsa		if (udev->ifaces_max != 0) {
853190730Sthompsa			udev->ifaces = malloc(sizeof(*iface) * udev->ifaces_max,
854190730Sthompsa			        M_USB, M_WAITOK | M_ZERO);
855190730Sthompsa			if (udev->ifaces == NULL) {
856190730Sthompsa				err = USB_ERR_NOMEM;
857190730Sthompsa				goto done;
858190730Sthompsa			}
859190730Sthompsa		}
860191398Sthompsa		if (ep_max != 0) {
861193644Sthompsa			udev->endpoints = malloc(sizeof(*ep) * ep_max,
862190730Sthompsa			        M_USB, M_WAITOK | M_ZERO);
863193644Sthompsa			if (udev->endpoints == NULL) {
864190730Sthompsa				err = USB_ERR_NOMEM;
865190730Sthompsa				goto done;
866190730Sthompsa			}
867191398Sthompsa		} else {
868193644Sthompsa			udev->endpoints = NULL;
869190730Sthompsa		}
870191398Sthompsa		USB_BUS_LOCK(udev->bus);
871193644Sthompsa		udev->endpoints_max = ep_max;
872191398Sthompsa		/* reset any ongoing clear-stall */
873193644Sthompsa		udev->ep_curr = NULL;
874191398Sthompsa		USB_BUS_UNLOCK(udev->bus);
875190730Sthompsa	}
876190730Sthompsa
877190730Sthompsadone:
878190730Sthompsa	if (err) {
879190730Sthompsa		if (cmd == USB_CFG_ALLOC) {
880190730Sthompsacleanup:
881191398Sthompsa			USB_BUS_LOCK(udev->bus);
882193644Sthompsa			udev->endpoints_max = 0;
883191398Sthompsa			/* reset any ongoing clear-stall */
884193644Sthompsa			udev->ep_curr = NULL;
885191398Sthompsa			USB_BUS_UNLOCK(udev->bus);
886191398Sthompsa
887190730Sthompsa			/* cleanup */
888190730Sthompsa			if (udev->ifaces != NULL)
889190730Sthompsa				free(udev->ifaces, M_USB);
890193644Sthompsa			if (udev->endpoints != NULL)
891193644Sthompsa				free(udev->endpoints, M_USB);
892190730Sthompsa
893190730Sthompsa			udev->ifaces = NULL;
894193644Sthompsa			udev->endpoints = NULL;
895190730Sthompsa			udev->ifaces_max = 0;
896190730Sthompsa		}
897190730Sthompsa	}
898190730Sthompsa	return (err);
899190730Sthompsa}
900190730Sthompsa
901190730Sthompsa/*------------------------------------------------------------------------*
902194228Sthompsa *	usbd_set_alt_interface_index
903184610Salfred *
904184610Salfred * This function will select an alternate interface index for the
905184610Salfred * given interface index. The interface should not be in use when this
906188986Sthompsa * function is called. That means there should not be any open USB
907188986Sthompsa * transfers. Else an error is returned. If the alternate setting is
908188986Sthompsa * already set this function will simply return success. This function
909188986Sthompsa * is called in Host mode and Device mode!
910184610Salfred *
911184610Salfred * Returns:
912184610Salfred *    0: Success
913184610Salfred * Else: Failure
914184610Salfred *------------------------------------------------------------------------*/
915193045Sthompsausb_error_t
916194228Sthompsausbd_set_alt_interface_index(struct usb_device *udev,
917184610Salfred    uint8_t iface_index, uint8_t alt_index)
918184610Salfred{
919194228Sthompsa	struct usb_interface *iface = usbd_get_iface(udev, iface_index);
920193045Sthompsa	usb_error_t err;
921184610Salfred	uint8_t do_unlock;
922184610Salfred
923247090Shselasky	/* Prevent re-enumeration */
924247090Shselasky	do_unlock = usbd_enum_lock(udev);
925247090Shselasky
926184610Salfred	if (iface == NULL) {
927184610Salfred		err = USB_ERR_INVAL;
928184610Salfred		goto done;
929184610Salfred	}
930195963Salfred	if (iface->alt_index == alt_index) {
931195963Salfred		/*
932195963Salfred		 * Optimise away duplicate setting of
933195963Salfred		 * alternate setting in USB Host Mode!
934195963Salfred		 */
935195963Salfred		err = 0;
936195963Salfred		goto done;
937184610Salfred	}
938189599Sthompsa#if USB_HAVE_UGEN
939185087Salfred	/*
940185087Salfred	 * Free all generic FIFOs for this interface, except control
941185087Salfred	 * endpoint FIFOs:
942185087Salfred	 */
943194228Sthompsa	usb_fifo_free_wrap(udev, iface_index, 0);
944189599Sthompsa#endif
945190730Sthompsa
946194228Sthompsa	err = usb_config_parse(udev, iface_index, alt_index);
947184610Salfred	if (err) {
948184610Salfred		goto done;
949184610Salfred	}
950195963Salfred	if (iface->alt_index != alt_index) {
951195963Salfred		/* the alternate setting does not exist */
952195963Salfred		err = USB_ERR_INVAL;
953195963Salfred		goto done;
954195963Salfred	}
955195963Salfred
956194228Sthompsa	err = usbd_req_set_alt_interface_no(udev, NULL, iface_index,
957184610Salfred	    iface->idesc->bAlternateSetting);
958184610Salfred
959184610Salfreddone:
960196498Salfred	if (do_unlock)
961196498Salfred		usbd_enum_unlock(udev);
962184610Salfred	return (err);
963184610Salfred}
964184610Salfred
965184610Salfred/*------------------------------------------------------------------------*
966194228Sthompsa *	usbd_set_endpoint_stall
967184610Salfred *
968213435Shselasky * This function is used to make a BULK or INTERRUPT endpoint send
969213435Shselasky * STALL tokens in USB device mode.
970184610Salfred *
971184610Salfred * Returns:
972184610Salfred *    0: Success
973184610Salfred * Else: Failure
974184610Salfred *------------------------------------------------------------------------*/
975193045Sthompsausb_error_t
976194228Sthompsausbd_set_endpoint_stall(struct usb_device *udev, struct usb_endpoint *ep,
977184610Salfred    uint8_t do_stall)
978184610Salfred{
979192984Sthompsa	struct usb_xfer *xfer;
980184610Salfred	uint8_t et;
981184610Salfred	uint8_t was_stalled;
982184610Salfred
983193644Sthompsa	if (ep == NULL) {
984184610Salfred		/* nothing to do */
985184610Salfred		DPRINTF("Cannot find endpoint\n");
986184610Salfred		/*
987184610Salfred		 * Pretend that the clear or set stall request is
988184610Salfred		 * successful else some USB host stacks can do
989184610Salfred		 * strange things, especially when a control endpoint
990184610Salfred		 * stalls.
991184610Salfred		 */
992184610Salfred		return (0);
993184610Salfred	}
994193644Sthompsa	et = (ep->edesc->bmAttributes & UE_XFERTYPE);
995184610Salfred
996184610Salfred	if ((et != UE_BULK) &&
997184610Salfred	    (et != UE_INTERRUPT)) {
998184610Salfred		/*
999184610Salfred	         * Should not stall control
1000184610Salfred	         * nor isochronous endpoints.
1001184610Salfred	         */
1002184610Salfred		DPRINTF("Invalid endpoint\n");
1003184610Salfred		return (0);
1004184610Salfred	}
1005184824Sthompsa	USB_BUS_LOCK(udev->bus);
1006184610Salfred
1007184610Salfred	/* store current stall state */
1008193644Sthompsa	was_stalled = ep->is_stalled;
1009184610Salfred
1010184610Salfred	/* check for no change */
1011184610Salfred	if (was_stalled && do_stall) {
1012193644Sthompsa		/* if the endpoint is already stalled do nothing */
1013184824Sthompsa		USB_BUS_UNLOCK(udev->bus);
1014184610Salfred		DPRINTF("No change\n");
1015184610Salfred		return (0);
1016184610Salfred	}
1017184610Salfred	/* set stalled state */
1018193644Sthompsa	ep->is_stalled = 1;
1019184610Salfred
1020184610Salfred	if (do_stall || (!was_stalled)) {
1021184610Salfred		if (!was_stalled) {
1022184610Salfred			/* lookup the current USB transfer, if any */
1023193644Sthompsa			xfer = ep->endpoint_q.curr;
1024184610Salfred		} else {
1025184610Salfred			xfer = NULL;
1026184610Salfred		}
1027184610Salfred
1028184610Salfred		/*
1029184610Salfred		 * If "xfer" is non-NULL the "set_stall" method will
1030184610Salfred		 * complete the USB transfer like in case of a timeout
1031184610Salfred		 * setting the error code "USB_ERR_STALLED".
1032184610Salfred		 */
1033195121Sthompsa		(udev->bus->methods->set_stall) (udev, xfer, ep, &do_stall);
1034184610Salfred	}
1035184610Salfred	if (!do_stall) {
1036193644Sthompsa		ep->toggle_next = 0;	/* reset data toggle */
1037193644Sthompsa		ep->is_stalled = 0;	/* clear stalled state */
1038184610Salfred
1039193644Sthompsa		(udev->bus->methods->clear_stall) (udev, ep);
1040184610Salfred
1041184610Salfred		/* start up the current or next transfer, if any */
1042194228Sthompsa		usb_command_wrapper(&ep->endpoint_q, ep->endpoint_q.curr);
1043184610Salfred	}
1044184824Sthompsa	USB_BUS_UNLOCK(udev->bus);
1045184610Salfred	return (0);
1046184610Salfred}
1047184610Salfred
1048184610Salfred/*------------------------------------------------------------------------*
1049194228Sthompsa *	usb_reset_iface_endpoints - used in USB device side mode
1050184610Salfred *------------------------------------------------------------------------*/
1051193045Sthompsausb_error_t
1052194228Sthompsausb_reset_iface_endpoints(struct usb_device *udev, uint8_t iface_index)
1053184610Salfred{
1054193644Sthompsa	struct usb_endpoint *ep;
1055193644Sthompsa	struct usb_endpoint *ep_end;
1056184610Salfred
1057193644Sthompsa	ep = udev->endpoints;
1058193644Sthompsa	ep_end = udev->endpoints + udev->endpoints_max;
1059184610Salfred
1060193644Sthompsa	for (; ep != ep_end; ep++) {
1061184610Salfred
1062193644Sthompsa		if ((ep->edesc == NULL) ||
1063193644Sthompsa		    (ep->iface_index != iface_index)) {
1064184610Salfred			continue;
1065184610Salfred		}
1066184610Salfred		/* simulate a clear stall from the peer */
1067195963Salfred		usbd_set_endpoint_stall(udev, ep, 0);
1068184610Salfred	}
1069184610Salfred	return (0);
1070184610Salfred}
1071184610Salfred
1072184610Salfred/*------------------------------------------------------------------------*
1073194228Sthompsa *	usb_detach_device_sub
1074184610Salfred *
1075184610Salfred * This function will try to detach an USB device. If it fails a panic
1076184610Salfred * will result.
1077190730Sthompsa *
1078190730Sthompsa * Flag values, see "USB_UNCFG_FLAG_XXX".
1079184610Salfred *------------------------------------------------------------------------*/
1080184610Salfredstatic void
1081194228Sthompsausb_detach_device_sub(struct usb_device *udev, device_t *ppdev,
1082214429Shselasky    char **ppnpinfo, uint8_t flag)
1083184610Salfred{
1084184610Salfred	device_t dev;
1085214429Shselasky	char *pnpinfo;
1086184610Salfred	int err;
1087184610Salfred
1088197553Sthompsa	dev = *ppdev;
1089197553Sthompsa	if (dev) {
1090184610Salfred		/*
1091184610Salfred		 * NOTE: It is important to clear "*ppdev" before deleting
1092184610Salfred		 * the child due to some device methods being called late
1093184610Salfred		 * during the delete process !
1094184610Salfred		 */
1095184610Salfred		*ppdev = NULL;
1096184610Salfred
1097184610Salfred		device_printf(dev, "at %s, port %d, addr %d "
1098184610Salfred		    "(disconnected)\n",
1099184610Salfred		    device_get_nameunit(udev->parent_dev),
1100184610Salfred		    udev->port_no, udev->address);
1101184610Salfred
1102184610Salfred		if (device_is_attached(dev)) {
1103191824Sthompsa			if (udev->flags.peer_suspended) {
1104184610Salfred				err = DEVICE_RESUME(dev);
1105184610Salfred				if (err) {
1106199816Sthompsa					device_printf(dev, "Resume failed\n");
1107184610Salfred				}
1108184610Salfred			}
1109184610Salfred			if (device_detach(dev)) {
1110184610Salfred				goto error;
1111184610Salfred			}
1112184610Salfred		}
1113184610Salfred		if (device_delete_child(udev->parent_dev, dev)) {
1114184610Salfred			goto error;
1115184610Salfred		}
1116184610Salfred	}
1117214429Shselasky
1118214429Shselasky	pnpinfo = *ppnpinfo;
1119214429Shselasky	if (pnpinfo != NULL) {
1120214429Shselasky		*ppnpinfo = NULL;
1121214429Shselasky		free(pnpinfo, M_USBDEV);
1122214429Shselasky	}
1123184610Salfred	return;
1124184610Salfred
1125184610Salfrederror:
1126184610Salfred	/* Detach is not allowed to fail in the USB world */
1127214429Shselasky	panic("usb_detach_device_sub: A USB driver would not detach\n");
1128184610Salfred}
1129184610Salfred
1130184610Salfred/*------------------------------------------------------------------------*
1131194228Sthompsa *	usb_detach_device
1132184610Salfred *
1133184610Salfred * The following function will detach the matching interfaces.
1134184610Salfred * This function is NULL safe.
1135190730Sthompsa *
1136190730Sthompsa * Flag values, see "USB_UNCFG_FLAG_XXX".
1137184610Salfred *------------------------------------------------------------------------*/
1138184610Salfredvoid
1139194228Sthompsausb_detach_device(struct usb_device *udev, uint8_t iface_index,
1140190730Sthompsa    uint8_t flag)
1141184610Salfred{
1142192984Sthompsa	struct usb_interface *iface;
1143184610Salfred	uint8_t i;
1144184610Salfred
1145184610Salfred	if (udev == NULL) {
1146184610Salfred		/* nothing to do */
1147184610Salfred		return;
1148184610Salfred	}
1149184610Salfred	DPRINTFN(4, "udev=%p\n", udev);
1150184610Salfred
1151207079Sthompsa	sx_assert(&udev->enum_sx, SA_LOCKED);
1152184610Salfred
1153263800Shselasky	/* wait for pending refs to go away */
1154263800Shselasky	usb_wait_pending_ref_locked(udev);
1155263800Shselasky
1156184610Salfred	/*
1157184610Salfred	 * First detach the child to give the child's detach routine a
1158184610Salfred	 * chance to detach the sub-devices in the correct order.
1159184610Salfred	 * Then delete the child using "device_delete_child()" which
1160184610Salfred	 * will detach all sub-devices from the bottom and upwards!
1161184610Salfred	 */
1162184610Salfred	if (iface_index != USB_IFACE_INDEX_ANY) {
1163184610Salfred		i = iface_index;
1164184610Salfred		iface_index = i + 1;
1165184610Salfred	} else {
1166184610Salfred		i = 0;
1167184610Salfred		iface_index = USB_IFACE_MAX;
1168184610Salfred	}
1169184610Salfred
1170184610Salfred	/* do the detach */
1171184610Salfred
1172184610Salfred	for (; i != iface_index; i++) {
1173184610Salfred
1174194228Sthompsa		iface = usbd_get_iface(udev, i);
1175184610Salfred		if (iface == NULL) {
1176184610Salfred			/* looks like the end of the USB interfaces */
1177184610Salfred			break;
1178184610Salfred		}
1179214429Shselasky		usb_detach_device_sub(udev, &iface->subdev,
1180214429Shselasky		    &iface->pnpinfo, flag);
1181184610Salfred	}
1182263800Shselasky
1183263800Shselasky	usb_ref_restore_locked(udev);
1184184610Salfred}
1185184610Salfred
1186184610Salfred/*------------------------------------------------------------------------*
1187194228Sthompsa *	usb_probe_and_attach_sub
1188184610Salfred *
1189184610Salfred * Returns:
1190184610Salfred *    0: Success
1191184610Salfred * Else: Failure
1192184610Salfred *------------------------------------------------------------------------*/
1193184610Salfredstatic uint8_t
1194194228Sthompsausb_probe_and_attach_sub(struct usb_device *udev,
1195192984Sthompsa    struct usb_attach_arg *uaa)
1196184610Salfred{
1197192984Sthompsa	struct usb_interface *iface;
1198184610Salfred	device_t dev;
1199184610Salfred	int err;
1200184610Salfred
1201184610Salfred	iface = uaa->iface;
1202184610Salfred	if (iface->parent_iface_index != USB_IFACE_INDEX_ANY) {
1203184610Salfred		/* leave interface alone */
1204184610Salfred		return (0);
1205184610Salfred	}
1206184610Salfred	dev = iface->subdev;
1207184610Salfred	if (dev) {
1208184610Salfred
1209184610Salfred		/* clean up after module unload */
1210184610Salfred
1211184610Salfred		if (device_is_attached(dev)) {
1212184610Salfred			/* already a device there */
1213184610Salfred			return (0);
1214184610Salfred		}
1215184610Salfred		/* clear "iface->subdev" as early as possible */
1216184610Salfred
1217184610Salfred		iface->subdev = NULL;
1218184610Salfred
1219184610Salfred		if (device_delete_child(udev->parent_dev, dev)) {
1220184610Salfred
1221184610Salfred			/*
1222184610Salfred			 * Panic here, else one can get a double call
1223184610Salfred			 * to device_detach().  USB devices should
1224184610Salfred			 * never fail on detach!
1225184610Salfred			 */
1226199816Sthompsa			panic("device_delete_child() failed\n");
1227184610Salfred		}
1228184610Salfred	}
1229184610Salfred	if (uaa->temp_dev == NULL) {
1230184610Salfred
1231184610Salfred		/* create a new child */
1232184610Salfred		uaa->temp_dev = device_add_child(udev->parent_dev, NULL, -1);
1233184610Salfred		if (uaa->temp_dev == NULL) {
1234184610Salfred			device_printf(udev->parent_dev,
1235199816Sthompsa			    "Device creation failed\n");
1236184610Salfred			return (1);	/* failure */
1237184610Salfred		}
1238184610Salfred		device_set_ivars(uaa->temp_dev, uaa);
1239184610Salfred		device_quiet(uaa->temp_dev);
1240184610Salfred	}
1241184610Salfred	/*
1242184610Salfred	 * Set "subdev" before probe and attach so that "devd" gets
1243184610Salfred	 * the information it needs.
1244184610Salfred	 */
1245184610Salfred	iface->subdev = uaa->temp_dev;
1246184610Salfred
1247184610Salfred	if (device_probe_and_attach(iface->subdev) == 0) {
1248184610Salfred		/*
1249184610Salfred		 * The USB attach arguments are only available during probe
1250184610Salfred		 * and attach !
1251184610Salfred		 */
1252184610Salfred		uaa->temp_dev = NULL;
1253184610Salfred		device_set_ivars(iface->subdev, NULL);
1254184610Salfred
1255191824Sthompsa		if (udev->flags.peer_suspended) {
1256184610Salfred			err = DEVICE_SUSPEND(iface->subdev);
1257191397Sthompsa			if (err)
1258191397Sthompsa				device_printf(iface->subdev, "Suspend failed\n");
1259184610Salfred		}
1260184610Salfred		return (0);		/* success */
1261184610Salfred	} else {
1262184610Salfred		/* No USB driver found */
1263184610Salfred		iface->subdev = NULL;
1264184610Salfred	}
1265184610Salfred	return (1);			/* failure */
1266184610Salfred}
1267184610Salfred
1268184610Salfred/*------------------------------------------------------------------------*
1269194228Sthompsa *	usbd_set_parent_iface
1270184610Salfred *
1271184610Salfred * Using this function will lock the alternate interface setting on an
1272184610Salfred * interface. It is typically used for multi interface drivers. In USB
1273184610Salfred * device side mode it is assumed that the alternate interfaces all
1274184610Salfred * have the same endpoint descriptors. The default parent index value
1275184610Salfred * is "USB_IFACE_INDEX_ANY". Then the alternate setting value is not
1276184610Salfred * locked.
1277184610Salfred *------------------------------------------------------------------------*/
1278184610Salfredvoid
1279194228Sthompsausbd_set_parent_iface(struct usb_device *udev, uint8_t iface_index,
1280184610Salfred    uint8_t parent_index)
1281184610Salfred{
1282192984Sthompsa	struct usb_interface *iface;
1283184610Salfred
1284236895Shselasky	if (udev == NULL) {
1285236895Shselasky		/* nothing to do */
1286236895Shselasky		return;
1287236895Shselasky	}
1288194228Sthompsa	iface = usbd_get_iface(udev, iface_index);
1289236895Shselasky	if (iface != NULL)
1290184610Salfred		iface->parent_iface_index = parent_index;
1291184610Salfred}
1292184610Salfred
1293184610Salfredstatic void
1294194228Sthompsausb_init_attach_arg(struct usb_device *udev,
1295192984Sthompsa    struct usb_attach_arg *uaa)
1296184610Salfred{
1297225350Shselasky	memset(uaa, 0, sizeof(*uaa));
1298225350Shselasky
1299184610Salfred	uaa->device = udev;
1300192499Sthompsa	uaa->usb_mode = udev->flags.usb_mode;
1301184610Salfred	uaa->port = udev->port_no;
1302200653Sthompsa	uaa->dev_state = UAA_DEV_READY;
1303184610Salfred
1304184610Salfred	uaa->info.idVendor = UGETW(udev->ddesc.idVendor);
1305184610Salfred	uaa->info.idProduct = UGETW(udev->ddesc.idProduct);
1306184610Salfred	uaa->info.bcdDevice = UGETW(udev->ddesc.bcdDevice);
1307184610Salfred	uaa->info.bDeviceClass = udev->ddesc.bDeviceClass;
1308184610Salfred	uaa->info.bDeviceSubClass = udev->ddesc.bDeviceSubClass;
1309184610Salfred	uaa->info.bDeviceProtocol = udev->ddesc.bDeviceProtocol;
1310184610Salfred	uaa->info.bConfigIndex = udev->curr_config_index;
1311184610Salfred	uaa->info.bConfigNum = udev->curr_config_no;
1312184610Salfred}
1313184610Salfred
1314184610Salfred/*------------------------------------------------------------------------*
1315194228Sthompsa *	usb_probe_and_attach
1316184610Salfred *
1317184610Salfred * This function is called from "uhub_explore_sub()",
1318194228Sthompsa * "usb_handle_set_config()" and "usb_handle_request()".
1319184610Salfred *
1320184610Salfred * Returns:
1321184610Salfred *    0: Success
1322184610Salfred * Else: A control transfer failed
1323184610Salfred *------------------------------------------------------------------------*/
1324193045Sthompsausb_error_t
1325194228Sthompsausb_probe_and_attach(struct usb_device *udev, uint8_t iface_index)
1326184610Salfred{
1327192984Sthompsa	struct usb_attach_arg uaa;
1328192984Sthompsa	struct usb_interface *iface;
1329184610Salfred	uint8_t i;
1330184610Salfred	uint8_t j;
1331184610Salfred	uint8_t do_unlock;
1332184610Salfred
1333184610Salfred	if (udev == NULL) {
1334184610Salfred		DPRINTF("udev == NULL\n");
1335184610Salfred		return (USB_ERR_INVAL);
1336184610Salfred	}
1337247090Shselasky	/* Prevent re-enumeration */
1338247090Shselasky	do_unlock = usbd_enum_lock(udev);
1339184610Salfred
1340184610Salfred	if (udev->curr_config_index == USB_UNCONFIG_INDEX) {
1341184610Salfred		/* do nothing - no configuration has been set */
1342184610Salfred		goto done;
1343184610Salfred	}
1344184610Salfred	/* setup USB attach arguments */
1345184610Salfred
1346194228Sthompsa	usb_init_attach_arg(udev, &uaa);
1347184610Salfred
1348223512Shselasky	/*
1349223512Shselasky	 * If the whole USB device is targeted, invoke the USB event
1350223512Shselasky	 * handler(s):
1351223512Shselasky	 */
1352223512Shselasky	if (iface_index == USB_IFACE_INDEX_ANY) {
1353223512Shselasky
1354223512Shselasky		EVENTHANDLER_INVOKE(usb_dev_configured, udev, &uaa);
1355223512Shselasky
1356223512Shselasky		if (uaa.dev_state != UAA_DEV_READY) {
1357223512Shselasky			/* leave device unconfigured */
1358223512Shselasky			usb_unconfigure(udev, 0);
1359223512Shselasky			goto done;
1360223512Shselasky		}
1361223512Shselasky	}
1362223512Shselasky
1363184610Salfred	/* Check if only one interface should be probed: */
1364184610Salfred	if (iface_index != USB_IFACE_INDEX_ANY) {
1365184610Salfred		i = iface_index;
1366184610Salfred		j = i + 1;
1367184610Salfred	} else {
1368184610Salfred		i = 0;
1369184610Salfred		j = USB_IFACE_MAX;
1370184610Salfred	}
1371184610Salfred
1372184610Salfred	/* Do the probe and attach */
1373184610Salfred	for (; i != j; i++) {
1374184610Salfred
1375194228Sthompsa		iface = usbd_get_iface(udev, i);
1376184610Salfred		if (iface == NULL) {
1377184610Salfred			/*
1378184610Salfred			 * Looks like the end of the USB
1379184610Salfred			 * interfaces !
1380184610Salfred			 */
1381184610Salfred			DPRINTFN(2, "end of interfaces "
1382184610Salfred			    "at %u\n", i);
1383184610Salfred			break;
1384184610Salfred		}
1385184610Salfred		if (iface->idesc == NULL) {
1386184610Salfred			/* no interface descriptor */
1387184610Salfred			continue;
1388184610Salfred		}
1389184610Salfred		uaa.iface = iface;
1390184610Salfred
1391184610Salfred		uaa.info.bInterfaceClass =
1392184610Salfred		    iface->idesc->bInterfaceClass;
1393184610Salfred		uaa.info.bInterfaceSubClass =
1394184610Salfred		    iface->idesc->bInterfaceSubClass;
1395184610Salfred		uaa.info.bInterfaceProtocol =
1396184610Salfred		    iface->idesc->bInterfaceProtocol;
1397184610Salfred		uaa.info.bIfaceIndex = i;
1398184610Salfred		uaa.info.bIfaceNum =
1399184610Salfred		    iface->idesc->bInterfaceNumber;
1400195963Salfred		uaa.driver_info = 0;	/* reset driver_info */
1401184610Salfred
1402184610Salfred		DPRINTFN(2, "iclass=%u/%u/%u iindex=%u/%u\n",
1403184610Salfred		    uaa.info.bInterfaceClass,
1404184610Salfred		    uaa.info.bInterfaceSubClass,
1405184610Salfred		    uaa.info.bInterfaceProtocol,
1406184610Salfred		    uaa.info.bIfaceIndex,
1407184610Salfred		    uaa.info.bIfaceNum);
1408184610Salfred
1409223513Shselasky		usb_probe_and_attach_sub(udev, &uaa);
1410184610Salfred
1411223513Shselasky		/*
1412223513Shselasky		 * Remove the leftover child, if any, to enforce that
1413223513Shselasky		 * a new nomatch devd event is generated for the next
1414223513Shselasky		 * interface if no driver is found:
1415223513Shselasky		 */
1416223513Shselasky		if (uaa.temp_dev == NULL)
1417223513Shselasky			continue;
1418223513Shselasky		if (device_delete_child(udev->parent_dev, uaa.temp_dev))
1419199816Sthompsa			DPRINTFN(0, "device delete child failed\n");
1420223513Shselasky		uaa.temp_dev = NULL;
1421184610Salfred	}
1422184610Salfreddone:
1423196498Salfred	if (do_unlock)
1424196498Salfred		usbd_enum_unlock(udev);
1425184610Salfred	return (0);
1426184610Salfred}
1427184610Salfred
1428184610Salfred/*------------------------------------------------------------------------*
1429194228Sthompsa *	usb_suspend_resume_sub
1430184610Salfred *
1431184610Salfred * This function is called when the suspend or resume methods should
1432184610Salfred * be executed on an USB device.
1433184610Salfred *------------------------------------------------------------------------*/
1434184610Salfredstatic void
1435194228Sthompsausb_suspend_resume_sub(struct usb_device *udev, device_t dev, uint8_t do_suspend)
1436184610Salfred{
1437184610Salfred	int err;
1438184610Salfred
1439184610Salfred	if (dev == NULL) {
1440184610Salfred		return;
1441184610Salfred	}
1442184610Salfred	if (!device_is_attached(dev)) {
1443184610Salfred		return;
1444184610Salfred	}
1445184610Salfred	if (do_suspend) {
1446184610Salfred		err = DEVICE_SUSPEND(dev);
1447184610Salfred	} else {
1448184610Salfred		err = DEVICE_RESUME(dev);
1449184610Salfred	}
1450184610Salfred	if (err) {
1451199816Sthompsa		device_printf(dev, "%s failed\n",
1452184610Salfred		    do_suspend ? "Suspend" : "Resume");
1453184610Salfred	}
1454184610Salfred}
1455184610Salfred
1456184610Salfred/*------------------------------------------------------------------------*
1457194228Sthompsa *	usb_suspend_resume
1458184610Salfred *
1459184610Salfred * The following function will suspend or resume the USB device.
1460184610Salfred *
1461184610Salfred * Returns:
1462184610Salfred *    0: Success
1463184610Salfred * Else: Failure
1464184610Salfred *------------------------------------------------------------------------*/
1465193045Sthompsausb_error_t
1466194228Sthompsausb_suspend_resume(struct usb_device *udev, uint8_t do_suspend)
1467184610Salfred{
1468192984Sthompsa	struct usb_interface *iface;
1469184610Salfred	uint8_t i;
1470184610Salfred
1471184610Salfred	if (udev == NULL) {
1472184610Salfred		/* nothing to do */
1473184610Salfred		return (0);
1474184610Salfred	}
1475184610Salfred	DPRINTFN(4, "udev=%p do_suspend=%d\n", udev, do_suspend);
1476184610Salfred
1477208008Sthompsa	sx_assert(&udev->sr_sx, SA_LOCKED);
1478184610Salfred
1479184824Sthompsa	USB_BUS_LOCK(udev->bus);
1480184610Salfred	/* filter the suspend events */
1481191824Sthompsa	if (udev->flags.peer_suspended == do_suspend) {
1482184824Sthompsa		USB_BUS_UNLOCK(udev->bus);
1483184610Salfred		/* nothing to do */
1484184610Salfred		return (0);
1485184610Salfred	}
1486191824Sthompsa	udev->flags.peer_suspended = do_suspend;
1487184824Sthompsa	USB_BUS_UNLOCK(udev->bus);
1488184610Salfred
1489184610Salfred	/* do the suspend or resume */
1490184610Salfred
1491184610Salfred	for (i = 0; i != USB_IFACE_MAX; i++) {
1492184610Salfred
1493194228Sthompsa		iface = usbd_get_iface(udev, i);
1494184610Salfred		if (iface == NULL) {
1495184610Salfred			/* looks like the end of the USB interfaces */
1496184610Salfred			break;
1497184610Salfred		}
1498194228Sthompsa		usb_suspend_resume_sub(udev, iface->subdev, do_suspend);
1499184610Salfred	}
1500184610Salfred	return (0);
1501184610Salfred}
1502184610Salfred
1503184610Salfred/*------------------------------------------------------------------------*
1504194228Sthompsa *      usbd_clear_stall_proc
1505184610Salfred *
1506184610Salfred * This function performs generic USB clear stall operations.
1507184610Salfred *------------------------------------------------------------------------*/
1508184610Salfredstatic void
1509194228Sthompsausbd_clear_stall_proc(struct usb_proc_msg *_pm)
1510184610Salfred{
1511261106Shselasky	struct usb_udev_msg *pm = (void *)_pm;
1512192984Sthompsa	struct usb_device *udev = pm->udev;
1513184610Salfred
1514184610Salfred	/* Change lock */
1515184824Sthompsa	USB_BUS_UNLOCK(udev->bus);
1516207079Sthompsa	mtx_lock(&udev->device_mtx);
1517184610Salfred
1518184610Salfred	/* Start clear stall callback */
1519207080Sthompsa	usbd_transfer_start(udev->ctrl_xfer[1]);
1520184610Salfred
1521184610Salfred	/* Change lock */
1522207079Sthompsa	mtx_unlock(&udev->device_mtx);
1523184824Sthompsa	USB_BUS_LOCK(udev->bus);
1524184610Salfred}
1525184610Salfred
1526184610Salfred/*------------------------------------------------------------------------*
1527194228Sthompsa *	usb_alloc_device
1528184610Salfred *
1529184610Salfred * This function allocates a new USB device. This function is called
1530184610Salfred * when a new device has been put in the powered state, but not yet in
1531184610Salfred * the addressed state. Get initial descriptor, set the address, get
1532184610Salfred * full descriptor and get strings.
1533184610Salfred *
1534184610Salfred * Return values:
1535184610Salfred *    0: Failure
1536184610Salfred * Else: Success
1537184610Salfred *------------------------------------------------------------------------*/
1538192984Sthompsastruct usb_device *
1539194228Sthompsausb_alloc_device(device_t parent_dev, struct usb_bus *bus,
1540192984Sthompsa    struct usb_device *parent_hub, uint8_t depth, uint8_t port_index,
1541192500Sthompsa    uint8_t port_no, enum usb_dev_speed speed, enum usb_hc_mode mode)
1542184610Salfred{
1543192984Sthompsa	struct usb_attach_arg uaa;
1544192984Sthompsa	struct usb_device *udev;
1545192984Sthompsa	struct usb_device *adev;
1546192984Sthompsa	struct usb_device *hub;
1547184610Salfred	uint8_t *scratch_ptr;
1548193045Sthompsa	usb_error_t err;
1549184610Salfred	uint8_t device_index;
1550200653Sthompsa	uint8_t config_index;
1551200653Sthompsa	uint8_t config_quirk;
1552200653Sthompsa	uint8_t set_config_failed;
1553247090Shselasky	uint8_t do_unlock;
1554184610Salfred
1555184610Salfred	DPRINTF("parent_dev=%p, bus=%p, parent_hub=%p, depth=%u, "
1556192499Sthompsa	    "port_index=%u, port_no=%u, speed=%u, usb_mode=%u\n",
1557184610Salfred	    parent_dev, bus, parent_hub, depth, port_index, port_no,
1558192499Sthompsa	    speed, mode);
1559184610Salfred
1560184610Salfred	/*
1561184610Salfred	 * Find an unused device index. In USB Host mode this is the
1562184610Salfred	 * same as the device address.
1563184610Salfred	 *
1564187170Sthompsa	 * Device index zero is not used and device index 1 should
1565187170Sthompsa	 * always be the root hub.
1566184610Salfred	 */
1567187171Sthompsa	for (device_index = USB_ROOT_HUB_ADDR;
1568187171Sthompsa	    (device_index != bus->devices_max) &&
1569187171Sthompsa	    (bus->devices[device_index] != NULL);
1570187171Sthompsa	    device_index++) /* nop */;
1571187171Sthompsa
1572187171Sthompsa	if (device_index == bus->devices_max) {
1573187171Sthompsa		device_printf(bus->bdev,
1574199816Sthompsa		    "No free USB device index for new device\n");
1575187171Sthompsa		return (NULL);
1576184610Salfred	}
1577184610Salfred
1578184610Salfred	if (depth > 0x10) {
1579184610Salfred		device_printf(bus->bdev,
1580199816Sthompsa		    "Invalid device depth\n");
1581184610Salfred		return (NULL);
1582184610Salfred	}
1583184610Salfred	udev = malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO);
1584184610Salfred	if (udev == NULL) {
1585184610Salfred		return (NULL);
1586184610Salfred	}
1587184610Salfred	/* initialise our SX-lock */
1588207079Sthompsa	sx_init_flags(&udev->enum_sx, "USB config SX lock", SX_DUPOK);
1589223512Shselasky	sx_init_flags(&udev->sr_sx, "USB suspend and resume SX lock", SX_NOWITNESS);
1590184610Salfred
1591207079Sthompsa	cv_init(&udev->ctrlreq_cv, "WCTRL");
1592207079Sthompsa	cv_init(&udev->ref_cv, "UGONE");
1593184610Salfred
1594184610Salfred	/* initialise our mutex */
1595207079Sthompsa	mtx_init(&udev->device_mtx, "USB device mutex", NULL, MTX_DEF);
1596184610Salfred
1597184610Salfred	/* initialise generic clear stall */
1598194228Sthompsa	udev->cs_msg[0].hdr.pm_callback = &usbd_clear_stall_proc;
1599184610Salfred	udev->cs_msg[0].udev = udev;
1600194228Sthompsa	udev->cs_msg[1].hdr.pm_callback = &usbd_clear_stall_proc;
1601184610Salfred	udev->cs_msg[1].udev = udev;
1602184610Salfred
1603184610Salfred	/* initialise some USB device fields */
1604184610Salfred	udev->parent_hub = parent_hub;
1605184610Salfred	udev->parent_dev = parent_dev;
1606184610Salfred	udev->port_index = port_index;
1607184610Salfred	udev->port_no = port_no;
1608184610Salfred	udev->depth = depth;
1609184610Salfred	udev->bus = bus;
1610184610Salfred	udev->address = USB_START_ADDR;	/* default value */
1611193045Sthompsa	udev->plugtime = (usb_ticks_t)ticks;
1612186730Salfred	/*
1613186730Salfred	 * We need to force the power mode to "on" because there are plenty
1614186730Salfred	 * of USB devices out there that do not work very well with
1615186730Salfred	 * automatic suspend and resume!
1616186730Salfred	 */
1617212135Sthompsa	udev->power_mode = usbd_filter_power_mode(udev, USB_POWER_MODE_ON);
1618186730Salfred	udev->pwr_save.last_xfer_time = ticks;
1619184610Salfred	/* we are not ready yet */
1620184610Salfred	udev->refcount = 1;
1621184610Salfred
1622184610Salfred	/* set up default endpoint descriptor */
1623207080Sthompsa	udev->ctrl_ep_desc.bLength = sizeof(udev->ctrl_ep_desc);
1624207080Sthompsa	udev->ctrl_ep_desc.bDescriptorType = UDESC_ENDPOINT;
1625207080Sthompsa	udev->ctrl_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
1626207080Sthompsa	udev->ctrl_ep_desc.bmAttributes = UE_CONTROL;
1627207080Sthompsa	udev->ctrl_ep_desc.wMaxPacketSize[0] = USB_MAX_IPACKET;
1628207080Sthompsa	udev->ctrl_ep_desc.wMaxPacketSize[1] = 0;
1629207080Sthompsa	udev->ctrl_ep_desc.bInterval = 0;
1630213435Shselasky
1631213435Shselasky	/* set up default endpoint companion descriptor */
1632213435Shselasky	udev->ctrl_ep_comp_desc.bLength = sizeof(udev->ctrl_ep_comp_desc);
1633213435Shselasky	udev->ctrl_ep_comp_desc.bDescriptorType = UDESC_ENDPOINT_SS_COMP;
1634213435Shselasky
1635184610Salfred	udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
1636184610Salfred
1637184610Salfred	udev->speed = speed;
1638192499Sthompsa	udev->flags.usb_mode = mode;
1639184610Salfred
1640184610Salfred	/* search for our High Speed USB HUB, if any */
1641184610Salfred
1642184610Salfred	adev = udev;
1643184610Salfred	hub = udev->parent_hub;
1644184610Salfred
1645184610Salfred	while (hub) {
1646184610Salfred		if (hub->speed == USB_SPEED_HIGH) {
1647184610Salfred			udev->hs_hub_addr = hub->address;
1648191395Sthompsa			udev->parent_hs_hub = hub;
1649184610Salfred			udev->hs_port_no = adev->port_no;
1650184610Salfred			break;
1651184610Salfred		}
1652184610Salfred		adev = hub;
1653184610Salfred		hub = hub->parent_hub;
1654184610Salfred	}
1655184610Salfred
1656193644Sthompsa	/* init the default endpoint */
1657194228Sthompsa	usb_init_endpoint(udev, 0,
1658207080Sthompsa	    &udev->ctrl_ep_desc,
1659213435Shselasky	    &udev->ctrl_ep_comp_desc,
1660207080Sthompsa	    &udev->ctrl_ep);
1661184610Salfred
1662184610Salfred	/* set device index */
1663184610Salfred	udev->device_index = device_index;
1664184610Salfred
1665190191Sthompsa#if USB_HAVE_UGEN
1666189599Sthompsa	/* Create ugen name */
1667189599Sthompsa	snprintf(udev->ugen_name, sizeof(udev->ugen_name),
1668189599Sthompsa	    USB_GENERIC_NAME "%u.%u", device_get_unit(bus->bdev),
1669189599Sthompsa	    device_index);
1670190191Sthompsa	LIST_INIT(&udev->pd_list);
1671190191Sthompsa
1672189110Sthompsa	/* Create the control endpoint device */
1673224777Shselasky	udev->ctrl_dev = usb_make_dev(udev, NULL, 0, 0,
1674224777Shselasky	    FREAD|FWRITE, UID_ROOT, GID_OPERATOR, 0600);
1675189599Sthompsa
1676189110Sthompsa	/* Create a link from /dev/ugenX.X to the default endpoint */
1677224777Shselasky	if (udev->ctrl_dev != NULL)
1678224777Shselasky		make_dev_alias(udev->ctrl_dev->cdev, "%s", udev->ugen_name);
1679189599Sthompsa#endif
1680213435Shselasky	/* Initialise device */
1681213435Shselasky	if (bus->methods->device_init != NULL) {
1682213435Shselasky		err = (bus->methods->device_init) (udev);
1683213435Shselasky		if (err != 0) {
1684213435Shselasky			DPRINTFN(0, "device init %d failed "
1685213435Shselasky			    "(%s, ignored)\n", device_index,
1686213435Shselasky			    usbd_errstr(err));
1687213435Shselasky			goto done;
1688213435Shselasky		}
1689213435Shselasky	}
1690213435Shselasky	/* set powered device state after device init is complete */
1691213435Shselasky	usb_set_device_state(udev, USB_STATE_POWERED);
1692213435Shselasky
1693192499Sthompsa	if (udev->flags.usb_mode == USB_MODE_HOST) {
1694184610Salfred
1695194228Sthompsa		err = usbd_req_set_address(udev, NULL, device_index);
1696184610Salfred
1697213435Shselasky		/*
1698213435Shselasky		 * This is the new USB device address from now on, if
1699213435Shselasky		 * the set address request didn't set it already.
1700213435Shselasky		 */
1701213435Shselasky		if (udev->address == USB_START_ADDR)
1702213435Shselasky			udev->address = device_index;
1703184610Salfred
1704184610Salfred		/*
1705184610Salfred		 * We ignore any set-address errors, hence there are
1706184610Salfred		 * buggy USB devices out there that actually receive
1707184610Salfred		 * the SETUP PID, but manage to set the address before
1708184610Salfred		 * the STATUS stage is ACK'ed. If the device responds
1709184610Salfred		 * to the subsequent get-descriptor at the new
1710184610Salfred		 * address, then we know that the set-address command
1711184610Salfred		 * was successful.
1712184610Salfred		 */
1713184610Salfred		if (err) {
1714184610Salfred			DPRINTFN(0, "set address %d failed "
1715190739Sthompsa			    "(%s, ignored)\n", udev->address,
1716194228Sthompsa			    usbd_errstr(err));
1717184610Salfred		}
1718184610Salfred	} else {
1719184610Salfred		/* We are not self powered */
1720184610Salfred		udev->flags.self_powered = 0;
1721184610Salfred
1722184610Salfred		/* Set unconfigured state */
1723184610Salfred		udev->curr_config_no = USB_UNCONFIG_NO;
1724184610Salfred		udev->curr_config_index = USB_UNCONFIG_INDEX;
1725184610Salfred
1726184610Salfred		/* Setup USB descriptors */
1727194228Sthompsa		err = (usb_temp_setup_by_index_p) (udev, usb_template);
1728184610Salfred		if (err) {
1729184610Salfred			DPRINTFN(0, "setting up USB template failed maybe the USB "
1730184610Salfred			    "template module has not been loaded\n");
1731184610Salfred			goto done;
1732184610Salfred		}
1733184610Salfred	}
1734194228Sthompsa	usb_set_device_state(udev, USB_STATE_ADDRESSED);
1735184610Salfred
1736213435Shselasky	/* setup the device descriptor and the initial "wMaxPacketSize" */
1737213435Shselasky	err = usbd_setup_device_desc(udev, NULL);
1738213435Shselasky
1739213435Shselasky	if (err != 0) {
1740255630Shselasky		/* try to enumerate two more times */
1741194228Sthompsa		err = usbd_req_re_enumerate(udev, NULL);
1742255630Shselasky		if (err != 0) {
1743255630Shselasky			err = usbd_req_re_enumerate(udev, NULL);
1744255630Shselasky			if (err != 0) {
1745255630Shselasky				goto done;
1746255630Shselasky			}
1747255630Shselasky		}
1748184610Salfred	}
1749184610Salfred
1750184610Salfred	/*
1751184610Salfred	 * Setup temporary USB attach args so that we can figure out some
1752184610Salfred	 * basic quirks for this device.
1753184610Salfred	 */
1754194228Sthompsa	usb_init_attach_arg(udev, &uaa);
1755184610Salfred
1756194228Sthompsa	if (usb_test_quirk(&uaa, UQ_BUS_POWERED)) {
1757184610Salfred		udev->flags.uq_bus_powered = 1;
1758184610Salfred	}
1759194228Sthompsa	if (usb_test_quirk(&uaa, UQ_NO_STRINGS)) {
1760184610Salfred		udev->flags.no_strings = 1;
1761184610Salfred	}
1762184610Salfred	/*
1763184610Salfred	 * Workaround for buggy USB devices.
1764184610Salfred	 *
1765184610Salfred	 * It appears that some string-less USB chips will crash and
1766184610Salfred	 * disappear if any attempts are made to read any string
1767184610Salfred	 * descriptors.
1768184610Salfred	 *
1769184610Salfred	 * Try to detect such chips by checking the strings in the USB
1770184610Salfred	 * device descriptor. If no strings are present there we
1771184610Salfred	 * simply disable all USB strings.
1772184610Salfred	 */
1773184610Salfred
1774247090Shselasky	/* Protect scratch area */
1775247090Shselasky	do_unlock = usbd_enum_lock(udev);
1776247090Shselasky
1777247090Shselasky	scratch_ptr = udev->scratch.data;
1778247090Shselasky
1779184610Salfred	if (udev->ddesc.iManufacturer ||
1780184610Salfred	    udev->ddesc.iProduct ||
1781184610Salfred	    udev->ddesc.iSerialNumber) {
1782184610Salfred		/* read out the language ID string */
1783194228Sthompsa		err = usbd_req_get_string_desc(udev, NULL,
1784201680Sthompsa		    (char *)scratch_ptr, 4, 0, USB_LANGUAGE_TABLE);
1785184610Salfred	} else {
1786184610Salfred		err = USB_ERR_INVAL;
1787184610Salfred	}
1788184610Salfred
1789184610Salfred	if (err || (scratch_ptr[0] < 4)) {
1790184610Salfred		udev->flags.no_strings = 1;
1791184610Salfred	} else {
1792197559Sthompsa		uint16_t langid;
1793197559Sthompsa		uint16_t pref;
1794197559Sthompsa		uint16_t mask;
1795197559Sthompsa		uint8_t x;
1796197559Sthompsa
1797197559Sthompsa		/* load preferred value and mask */
1798197559Sthompsa		pref = usb_lang_id;
1799197559Sthompsa		mask = usb_lang_mask;
1800197559Sthompsa
1801197559Sthompsa		/* align length correctly */
1802247090Shselasky		scratch_ptr[0] &= ~1U;
1803197559Sthompsa
1804197559Sthompsa		/* fix compiler warning */
1805197559Sthompsa		langid = 0;
1806197559Sthompsa
1807197559Sthompsa		/* search for preferred language */
1808197559Sthompsa		for (x = 2; (x < scratch_ptr[0]); x += 2) {
1809197559Sthompsa			langid = UGETW(scratch_ptr + x);
1810197559Sthompsa			if ((langid & mask) == pref)
1811197559Sthompsa				break;
1812197559Sthompsa		}
1813197559Sthompsa		if (x >= scratch_ptr[0]) {
1814197559Sthompsa			/* pick the first language as the default */
1815197559Sthompsa			DPRINTFN(1, "Using first language\n");
1816197559Sthompsa			langid = UGETW(scratch_ptr + 2);
1817197559Sthompsa		}
1818197559Sthompsa
1819197559Sthompsa		DPRINTFN(1, "Language selected: 0x%04x\n", langid);
1820197559Sthompsa		udev->langid = langid;
1821184610Salfred	}
1822184610Salfred
1823247090Shselasky	if (do_unlock)
1824247090Shselasky		usbd_enum_unlock(udev);
1825247090Shselasky
1826184610Salfred	/* assume 100mA bus powered for now. Changed when configured. */
1827184610Salfred	udev->power = USB_MIN_POWER;
1828192938Sthompsa	/* fetch the vendor and product strings from the device */
1829194228Sthompsa	usbd_set_device_strings(udev);
1830184610Salfred
1831200653Sthompsa	if (udev->flags.usb_mode == USB_MODE_DEVICE) {
1832200653Sthompsa		/* USB device mode setup is complete */
1833200653Sthompsa		err = 0;
1834200653Sthompsa		goto config_done;
1835200653Sthompsa	}
1836184610Salfred
1837200653Sthompsa	/*
1838200653Sthompsa	 * Most USB devices should attach to config index 0 by
1839200653Sthompsa	 * default
1840200653Sthompsa	 */
1841200653Sthompsa	if (usb_test_quirk(&uaa, UQ_CFG_INDEX_0)) {
1842200653Sthompsa		config_index = 0;
1843200653Sthompsa		config_quirk = 1;
1844200653Sthompsa	} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_1)) {
1845200653Sthompsa		config_index = 1;
1846200653Sthompsa		config_quirk = 1;
1847200653Sthompsa	} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_2)) {
1848200653Sthompsa		config_index = 2;
1849200653Sthompsa		config_quirk = 1;
1850200653Sthompsa	} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_3)) {
1851200653Sthompsa		config_index = 3;
1852200653Sthompsa		config_quirk = 1;
1853200653Sthompsa	} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_4)) {
1854200653Sthompsa		config_index = 4;
1855200653Sthompsa		config_quirk = 1;
1856200653Sthompsa	} else {
1857200653Sthompsa		config_index = 0;
1858200653Sthompsa		config_quirk = 0;
1859200653Sthompsa	}
1860184610Salfred
1861200653Sthompsa	set_config_failed = 0;
1862184610Salfredrepeat_set_config:
1863184610Salfred
1864200653Sthompsa	DPRINTF("setting config %u\n", config_index);
1865184610Salfred
1866200653Sthompsa	/* get the USB device configured */
1867200653Sthompsa	err = usbd_set_config_index(udev, config_index);
1868200653Sthompsa	if (err) {
1869200653Sthompsa		if (udev->ddesc.bNumConfigurations != 0) {
1870200653Sthompsa			if (!set_config_failed) {
1871200653Sthompsa				set_config_failed = 1;
1872200653Sthompsa				/* XXX try to re-enumerate the device */
1873200653Sthompsa				err = usbd_req_re_enumerate(udev, NULL);
1874200653Sthompsa				if (err == 0)
1875200653Sthompsa					goto repeat_set_config;
1876186730Salfred			}
1877200653Sthompsa			DPRINTFN(0, "Failure selecting configuration index %u:"
1878200653Sthompsa			    "%s, port %u, addr %u (ignored)\n",
1879200653Sthompsa			    config_index, usbd_errstr(err), udev->port_no,
1880200653Sthompsa			    udev->address);
1881200653Sthompsa		}
1882200653Sthompsa		/*
1883200653Sthompsa		 * Some USB devices do not have any configurations. Ignore any
1884200653Sthompsa		 * set config failures!
1885200653Sthompsa		 */
1886200653Sthompsa		err = 0;
1887200653Sthompsa		goto config_done;
1888200653Sthompsa	}
1889200653Sthompsa	if (!config_quirk && config_index + 1 < udev->ddesc.bNumConfigurations) {
1890200653Sthompsa		if ((udev->cdesc->bNumInterface < 2) &&
1891200653Sthompsa		    usbd_get_no_descriptors(udev->cdesc, UDESC_ENDPOINT) == 0) {
1892200653Sthompsa			DPRINTFN(0, "Found no endpoints, trying next config\n");
1893200653Sthompsa			config_index++;
1894200653Sthompsa			goto repeat_set_config;
1895200653Sthompsa		}
1896200653Sthompsa		if (config_index == 0) {
1897186730Salfred			/*
1898200653Sthompsa			 * Try to figure out if we have an
1899200653Sthompsa			 * auto-install disk there:
1900186730Salfred			 */
1901201681Sthompsa			if (usb_iface_is_cdrom(udev, 0)) {
1902200653Sthompsa				DPRINTFN(0, "Found possible auto-install "
1903200653Sthompsa				    "disk (trying next config)\n");
1904184610Salfred				config_index++;
1905184610Salfred				goto repeat_set_config;
1906184610Salfred			}
1907184610Salfred		}
1908184610Salfred	}
1909225350Shselasky	if (set_config_failed == 0 && config_index == 0 &&
1910227007Shselasky	    usb_test_quirk(&uaa, UQ_MSC_NO_SYNC_CACHE) == 0 &&
1911227007Shselasky	    usb_test_quirk(&uaa, UQ_MSC_NO_GETMAXLUN) == 0) {
1912184610Salfred
1913225350Shselasky		/*
1914225350Shselasky		 * Try to figure out if there are any MSC quirks we
1915225350Shselasky		 * should apply automatically:
1916225350Shselasky		 */
1917225350Shselasky		err = usb_msc_auto_quirk(udev, 0);
1918225350Shselasky
1919225350Shselasky		if (err != 0) {
1920225350Shselasky			set_config_failed = 1;
1921225350Shselasky			goto repeat_set_config;
1922225350Shselasky		}
1923225350Shselasky	}
1924225350Shselasky
1925200653Sthompsaconfig_done:
1926184610Salfred	DPRINTF("new dev (addr %d), udev=%p, parent_hub=%p\n",
1927184610Salfred	    udev->address, udev, udev->parent_hub);
1928184610Salfred
1929184610Salfred	/* register our device - we are ready */
1930194228Sthompsa	usb_bus_port_set_device(bus, parent_hub ?
1931184610Salfred	    parent_hub->hub->ports + port_index : NULL, udev, device_index);
1932184610Salfred
1933189599Sthompsa#if USB_HAVE_UGEN
1934190191Sthompsa	/* Symlink the ugen device name */
1935194228Sthompsa	udev->ugen_symlink = usb_alloc_symlink(udev->ugen_name);
1936190191Sthompsa
1937190191Sthompsa	/* Announce device */
1938212136Sthompsa	printf("%s: <%s> at %s\n", udev->ugen_name,
1939212136Sthompsa	    usb_get_manufacturer(udev),
1940184610Salfred	    device_get_nameunit(udev->bus->bdev));
1941214221Shselasky#endif
1942190727Sthompsa
1943214221Shselasky#if USB_HAVE_DEVCTL
1944207020Sthompsa	usb_notify_addq("ATTACH", udev);
1945190191Sthompsa#endif
1946184610Salfreddone:
1947184610Salfred	if (err) {
1948197553Sthompsa		/*
1949197553Sthompsa		 * Free USB device and all subdevices, if any.
1950197553Sthompsa		 */
1951197553Sthompsa		usb_free_device(udev, 0);
1952184610Salfred		udev = NULL;
1953184610Salfred	}
1954184610Salfred	return (udev);
1955184610Salfred}
1956184610Salfred
1957189599Sthompsa#if USB_HAVE_UGEN
1958224777Shselaskystruct usb_fs_privdata *
1959224777Shselaskyusb_make_dev(struct usb_device *udev, const char *devname, int ep,
1960224777Shselasky    int fi, int rwmode, uid_t uid, gid_t gid, int mode)
1961189110Sthompsa{
1962192984Sthompsa	struct usb_fs_privdata* pd;
1963224777Shselasky	char buffer[32];
1964189110Sthompsa
1965189110Sthompsa	/* Store information to locate ourselves again later */
1966192984Sthompsa	pd = malloc(sizeof(struct usb_fs_privdata), M_USBDEV,
1967189110Sthompsa	    M_WAITOK | M_ZERO);
1968189110Sthompsa	pd->bus_index = device_get_unit(udev->bus->bdev);
1969189110Sthompsa	pd->dev_index = udev->device_index;
1970189110Sthompsa	pd->ep_addr = ep;
1971224777Shselasky	pd->fifo_index = fi;
1972224777Shselasky	pd->mode = rwmode;
1973189110Sthompsa
1974189110Sthompsa	/* Now, create the device itself */
1975224777Shselasky	if (devname == NULL) {
1976224777Shselasky		devname = buffer;
1977224777Shselasky		snprintf(buffer, sizeof(buffer), USB_DEVICE_DIR "/%u.%u.%u",
1978224777Shselasky		    pd->bus_index, pd->dev_index, pd->ep_addr);
1979224777Shselasky	}
1980224777Shselasky
1981224777Shselasky	pd->cdev = make_dev(&usb_devsw, 0, uid, gid, mode, "%s", devname);
1982224777Shselasky
1983224777Shselasky	if (pd->cdev == NULL) {
1984224777Shselasky		DPRINTFN(0, "Failed to create device %s\n", devname);
1985224777Shselasky		free(pd, M_USBDEV);
1986224777Shselasky		return (NULL);
1987224777Shselasky	}
1988224777Shselasky
1989224777Shselasky	/* XXX setting si_drv1 and creating the device is not atomic! */
1990189110Sthompsa	pd->cdev->si_drv1 = pd;
1991189110Sthompsa
1992224777Shselasky	return (pd);
1993189110Sthompsa}
1994189110Sthompsa
1995224777Shselaskyvoid
1996224777Shselaskyusb_destroy_dev(struct usb_fs_privdata *pd)
1997224777Shselasky{
1998224777Shselasky	if (pd == NULL)
1999224777Shselasky		return;
2000224777Shselasky
2001224777Shselasky	destroy_dev(pd->cdev);
2002224777Shselasky
2003224777Shselasky	free(pd, M_USBDEV);
2004224777Shselasky}
2005224777Shselasky
2006189110Sthompsastatic void
2007194228Sthompsausb_cdev_create(struct usb_device *udev)
2008189110Sthompsa{
2009192984Sthompsa	struct usb_config_descriptor *cd;
2010192984Sthompsa	struct usb_endpoint_descriptor *ed;
2011192984Sthompsa	struct usb_descriptor *desc;
2012192984Sthompsa	struct usb_fs_privdata* pd;
2013189172Sthompsa	int inmode, outmode, inmask, outmask, mode;
2014189172Sthompsa	uint8_t ep;
2015189110Sthompsa
2016189110Sthompsa	KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("stale cdev entries"));
2017189110Sthompsa
2018189110Sthompsa	DPRINTFN(2, "Creating device nodes\n");
2019189110Sthompsa
2020194228Sthompsa	if (usbd_get_mode(udev) == USB_MODE_DEVICE) {
2021189110Sthompsa		inmode = FWRITE;
2022189110Sthompsa		outmode = FREAD;
2023189110Sthompsa	} else {		 /* USB_MODE_HOST */
2024189110Sthompsa		inmode = FREAD;
2025189110Sthompsa		outmode = FWRITE;
2026189110Sthompsa	}
2027189110Sthompsa
2028189172Sthompsa	inmask = 0;
2029189172Sthompsa	outmask = 0;
2030189172Sthompsa	desc = NULL;
2031189110Sthompsa
2032189172Sthompsa	/*
2033189172Sthompsa	 * Collect all used endpoint numbers instead of just
2034189172Sthompsa	 * generating 16 static endpoints.
2035189172Sthompsa	 */
2036194228Sthompsa	cd = usbd_get_config_descriptor(udev);
2037194228Sthompsa	while ((desc = usb_desc_foreach(cd, desc))) {
2038189172Sthompsa		/* filter out all endpoint descriptors */
2039189172Sthompsa		if ((desc->bDescriptorType == UDESC_ENDPOINT) &&
2040189172Sthompsa		    (desc->bLength >= sizeof(*ed))) {
2041192984Sthompsa			ed = (struct usb_endpoint_descriptor *)desc;
2042189110Sthompsa
2043189172Sthompsa			/* update masks */
2044189172Sthompsa			ep = ed->bEndpointAddress;
2045189172Sthompsa			if (UE_GET_DIR(ep)  == UE_DIR_OUT)
2046189172Sthompsa				outmask |= 1 << UE_GET_ADDR(ep);
2047189172Sthompsa			else
2048189172Sthompsa				inmask |= 1 << UE_GET_ADDR(ep);
2049189110Sthompsa		}
2050189110Sthompsa	}
2051189172Sthompsa
2052189172Sthompsa	/* Create all available endpoints except EP0 */
2053189172Sthompsa	for (ep = 1; ep < 16; ep++) {
2054224777Shselasky		mode = (inmask & (1 << ep)) ? inmode : 0;
2055224777Shselasky		mode |= (outmask & (1 << ep)) ? outmode : 0;
2056189172Sthompsa		if (mode == 0)
2057189172Sthompsa			continue;	/* no IN or OUT endpoint */
2058189172Sthompsa
2059224777Shselasky		pd = usb_make_dev(udev, NULL, ep, 0,
2060224777Shselasky		    mode, UID_ROOT, GID_OPERATOR, 0600);
2061224777Shselasky
2062224777Shselasky		if (pd != NULL)
2063224777Shselasky			LIST_INSERT_HEAD(&udev->pd_list, pd, pd_next);
2064189172Sthompsa	}
2065189110Sthompsa}
2066189110Sthompsa
2067189110Sthompsastatic void
2068194228Sthompsausb_cdev_free(struct usb_device *udev)
2069189110Sthompsa{
2070192984Sthompsa	struct usb_fs_privdata* pd;
2071189110Sthompsa
2072189110Sthompsa	DPRINTFN(2, "Freeing device nodes\n");
2073189110Sthompsa
2074189110Sthompsa	while ((pd = LIST_FIRST(&udev->pd_list)) != NULL) {
2075189110Sthompsa		KASSERT(pd->cdev->si_drv1 == pd, ("privdata corrupt"));
2076189110Sthompsa
2077189110Sthompsa		LIST_REMOVE(pd, pd_next);
2078224777Shselasky
2079224777Shselasky		usb_destroy_dev(pd);
2080189110Sthompsa	}
2081189110Sthompsa}
2082189599Sthompsa#endif
2083189110Sthompsa
2084184610Salfred/*------------------------------------------------------------------------*
2085194228Sthompsa *	usb_free_device
2086184610Salfred *
2087197553Sthompsa * This function is NULL safe and will free an USB device and its
2088197553Sthompsa * children devices, if any.
2089190730Sthompsa *
2090197553Sthompsa * Flag values: Reserved, set to zero.
2091184610Salfred *------------------------------------------------------------------------*/
2092184610Salfredvoid
2093194228Sthompsausb_free_device(struct usb_device *udev, uint8_t flag)
2094184610Salfred{
2095192984Sthompsa	struct usb_bus *bus;
2096184610Salfred
2097190730Sthompsa	if (udev == NULL)
2098190730Sthompsa		return;		/* already freed */
2099190730Sthompsa
2100184610Salfred	DPRINTFN(4, "udev=%p port=%d\n", udev, udev->port_no);
2101184610Salfred
2102191494Sthompsa	bus = udev->bus;
2103261109Shselasky
2104261109Shselasky	/* set DETACHED state to prevent any further references */
2105194228Sthompsa	usb_set_device_state(udev, USB_STATE_DETACHED);
2106190730Sthompsa
2107214221Shselasky#if USB_HAVE_DEVCTL
2108207020Sthompsa	usb_notify_addq("DETACH", udev);
2109214221Shselasky#endif
2110184610Salfred
2111214221Shselasky#if USB_HAVE_UGEN
2112189110Sthompsa	printf("%s: <%s> at %s (disconnected)\n", udev->ugen_name,
2113212136Sthompsa	    usb_get_manufacturer(udev), device_get_nameunit(bus->bdev));
2114184610Salfred
2115189110Sthompsa	/* Destroy UGEN symlink, if any */
2116184610Salfred	if (udev->ugen_symlink) {
2117194228Sthompsa		usb_free_symlink(udev->ugen_symlink);
2118184610Salfred		udev->ugen_symlink = NULL;
2119184610Salfred	}
2120184610Salfred
2121224777Shselasky	usb_destroy_dev(udev->ctrl_dev);
2122189599Sthompsa#endif
2123184610Salfred
2124192499Sthompsa	if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2125184610Salfred		/* stop receiving any control transfers (Device Side Mode) */
2126207080Sthompsa		usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
2127184610Salfred	}
2128184610Salfred
2129190730Sthompsa	/* the following will get the device unconfigured in software */
2130197553Sthompsa	usb_unconfigure(udev, USB_UNCFG_FLAG_FREE_EP0);
2131184610Salfred
2132261109Shselasky	/* final device unregister after all character devices are closed */
2133261109Shselasky	usb_bus_port_set_device(bus, udev->parent_hub ?
2134261109Shselasky	    udev->parent_hub->hub->ports + udev->port_index : NULL,
2135261109Shselasky	    NULL, USB_ROOT_HUB_ADDR);
2136261109Shselasky
2137184610Salfred	/* unsetup any leftover default USB transfers */
2138207080Sthompsa	usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
2139184610Salfred
2140187174Sthompsa	/* template unsetup, if any */
2141194228Sthompsa	(usb_temp_unsetup_p) (udev);
2142184610Salfred
2143187174Sthompsa	/*
2144187174Sthompsa	 * Make sure that our clear-stall messages are not queued
2145187174Sthompsa	 * anywhere:
2146187174Sthompsa	 */
2147187174Sthompsa	USB_BUS_LOCK(udev->bus);
2148194228Sthompsa	usb_proc_mwait(&udev->bus->non_giant_callback_proc,
2149187174Sthompsa	    &udev->cs_msg[0], &udev->cs_msg[1]);
2150187174Sthompsa	USB_BUS_UNLOCK(udev->bus);
2151187174Sthompsa
2152207079Sthompsa	sx_destroy(&udev->enum_sx);
2153208008Sthompsa	sx_destroy(&udev->sr_sx);
2154184610Salfred
2155207079Sthompsa	cv_destroy(&udev->ctrlreq_cv);
2156207079Sthompsa	cv_destroy(&udev->ref_cv);
2157184610Salfred
2158207079Sthompsa	mtx_destroy(&udev->device_mtx);
2159190191Sthompsa#if USB_HAVE_UGEN
2160189110Sthompsa	KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("leaked cdev entries"));
2161190191Sthompsa#endif
2162184610Salfred
2163213435Shselasky	/* Uninitialise device */
2164213435Shselasky	if (bus->methods->device_uninit != NULL)
2165213435Shselasky		(bus->methods->device_uninit) (udev);
2166213435Shselasky
2167184610Salfred	/* free device */
2168193042Sthompsa	free(udev->serial, M_USB);
2169193042Sthompsa	free(udev->manufacturer, M_USB);
2170193042Sthompsa	free(udev->product, M_USB);
2171184610Salfred	free(udev, M_USB);
2172184610Salfred}
2173184610Salfred
2174184610Salfred/*------------------------------------------------------------------------*
2175194228Sthompsa *	usbd_get_iface
2176184610Salfred *
2177184610Salfred * This function is the safe way to get the USB interface structure
2178184610Salfred * pointer by interface index.
2179184610Salfred *
2180184610Salfred * Return values:
2181184610Salfred *   NULL: Interface not present.
2182184610Salfred *   Else: Pointer to USB interface structure.
2183184610Salfred *------------------------------------------------------------------------*/
2184192984Sthompsastruct usb_interface *
2185194228Sthompsausbd_get_iface(struct usb_device *udev, uint8_t iface_index)
2186184610Salfred{
2187192984Sthompsa	struct usb_interface *iface = udev->ifaces + iface_index;
2188184610Salfred
2189190730Sthompsa	if (iface_index >= udev->ifaces_max)
2190184610Salfred		return (NULL);
2191184610Salfred	return (iface);
2192184610Salfred}
2193184610Salfred
2194184610Salfred/*------------------------------------------------------------------------*
2195194271Sthompsa *	usbd_find_descriptor
2196184610Salfred *
2197184610Salfred * This function will lookup the first descriptor that matches the
2198184610Salfred * criteria given by the arguments "type" and "subtype". Descriptors
2199184610Salfred * will only be searched within the interface having the index
2200184610Salfred * "iface_index".  If the "id" argument points to an USB descriptor,
2201184610Salfred * it will be skipped before the search is started. This allows
2202184610Salfred * searching for multiple descriptors using the same criteria. Else
2203184610Salfred * the search is started after the interface descriptor.
2204184610Salfred *
2205184610Salfred * Return values:
2206184610Salfred *   NULL: End of descriptors
2207184610Salfred *   Else: A descriptor matching the criteria
2208184610Salfred *------------------------------------------------------------------------*/
2209184610Salfredvoid   *
2210194271Sthompsausbd_find_descriptor(struct usb_device *udev, void *id, uint8_t iface_index,
2211184610Salfred    uint8_t type, uint8_t type_mask,
2212184610Salfred    uint8_t subtype, uint8_t subtype_mask)
2213184610Salfred{
2214192984Sthompsa	struct usb_descriptor *desc;
2215192984Sthompsa	struct usb_config_descriptor *cd;
2216192984Sthompsa	struct usb_interface *iface;
2217184610Salfred
2218194228Sthompsa	cd = usbd_get_config_descriptor(udev);
2219184610Salfred	if (cd == NULL) {
2220184610Salfred		return (NULL);
2221184610Salfred	}
2222184610Salfred	if (id == NULL) {
2223194228Sthompsa		iface = usbd_get_iface(udev, iface_index);
2224184610Salfred		if (iface == NULL) {
2225184610Salfred			return (NULL);
2226184610Salfred		}
2227194228Sthompsa		id = usbd_get_interface_descriptor(iface);
2228184610Salfred		if (id == NULL) {
2229184610Salfred			return (NULL);
2230184610Salfred		}
2231184610Salfred	}
2232184610Salfred	desc = (void *)id;
2233184610Salfred
2234194228Sthompsa	while ((desc = usb_desc_foreach(cd, desc))) {
2235184610Salfred
2236184610Salfred		if (desc->bDescriptorType == UDESC_INTERFACE) {
2237184610Salfred			break;
2238184610Salfred		}
2239184610Salfred		if (((desc->bDescriptorType & type_mask) == type) &&
2240184610Salfred		    ((desc->bDescriptorSubtype & subtype_mask) == subtype)) {
2241184610Salfred			return (desc);
2242184610Salfred		}
2243184610Salfred	}
2244184610Salfred	return (NULL);
2245184610Salfred}
2246184610Salfred
2247184610Salfred/*------------------------------------------------------------------------*
2248194228Sthompsa *	usb_devinfo
2249184610Salfred *
2250184610Salfred * This function will dump information from the device descriptor
2251184610Salfred * belonging to the USB device pointed to by "udev", to the string
2252184610Salfred * pointed to by "dst_ptr" having a maximum length of "dst_len" bytes
2253184610Salfred * including the terminating zero.
2254184610Salfred *------------------------------------------------------------------------*/
2255184610Salfredvoid
2256194228Sthompsausb_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len)
2257184610Salfred{
2258192984Sthompsa	struct usb_device_descriptor *udd = &udev->ddesc;
2259184610Salfred	uint16_t bcdDevice;
2260184610Salfred	uint16_t bcdUSB;
2261184610Salfred
2262184610Salfred	bcdUSB = UGETW(udd->bcdUSB);
2263184610Salfred	bcdDevice = UGETW(udd->bcdDevice);
2264184610Salfred
2265184610Salfred	if (udd->bDeviceClass != 0xFF) {
2266184610Salfred		snprintf(dst_ptr, dst_len, "%s %s, class %d/%d, rev %x.%02x/"
2267190191Sthompsa		    "%x.%02x, addr %d",
2268212136Sthompsa		    usb_get_manufacturer(udev),
2269212136Sthompsa		    usb_get_product(udev),
2270184610Salfred		    udd->bDeviceClass, udd->bDeviceSubClass,
2271184610Salfred		    (bcdUSB >> 8), bcdUSB & 0xFF,
2272184610Salfred		    (bcdDevice >> 8), bcdDevice & 0xFF,
2273184610Salfred		    udev->address);
2274184610Salfred	} else {
2275184610Salfred		snprintf(dst_ptr, dst_len, "%s %s, rev %x.%02x/"
2276190191Sthompsa		    "%x.%02x, addr %d",
2277212136Sthompsa		    usb_get_manufacturer(udev),
2278212136Sthompsa		    usb_get_product(udev),
2279184610Salfred		    (bcdUSB >> 8), bcdUSB & 0xFF,
2280184610Salfred		    (bcdDevice >> 8), bcdDevice & 0xFF,
2281184610Salfred		    udev->address);
2282184610Salfred	}
2283184610Salfred}
2284184610Salfred
2285194677Sthompsa#ifdef USB_VERBOSE
2286184610Salfred/*
2287184610Salfred * Descriptions of of known vendors and devices ("products").
2288184610Salfred */
2289184610Salfredstruct usb_knowndev {
2290184610Salfred	uint16_t vendor;
2291184610Salfred	uint16_t product;
2292184610Salfred	uint32_t flags;
2293184610Salfred	const char *vendorname;
2294184610Salfred	const char *productname;
2295184610Salfred};
2296184610Salfred
2297184610Salfred#define	USB_KNOWNDEV_NOPROD	0x01	/* match on vendor only */
2298184610Salfred
2299188746Sthompsa#include "usbdevs.h"
2300188746Sthompsa#include "usbdevs_data.h"
2301184610Salfred#endif					/* USB_VERBOSE */
2302184610Salfred
2303184610Salfredstatic void
2304194228Sthompsausbd_set_device_strings(struct usb_device *udev)
2305184610Salfred{
2306192984Sthompsa	struct usb_device_descriptor *udd = &udev->ddesc;
2307194677Sthompsa#ifdef USB_VERBOSE
2308184610Salfred	const struct usb_knowndev *kdp;
2309184610Salfred#endif
2310208008Sthompsa	char *temp_ptr;
2311197559Sthompsa	size_t temp_size;
2312184610Salfred	uint16_t vendor_id;
2313184610Salfred	uint16_t product_id;
2314247090Shselasky	uint8_t do_unlock;
2315184610Salfred
2316247090Shselasky	/* Protect scratch area */
2317247090Shselasky	do_unlock = usbd_enum_lock(udev);
2318197559Sthompsa
2319247090Shselasky	temp_ptr = (char *)udev->scratch.data;
2320247090Shselasky	temp_size = sizeof(udev->scratch.data);
2321247090Shselasky
2322192938Sthompsa	vendor_id = UGETW(udd->idVendor);
2323192938Sthompsa	product_id = UGETW(udd->idProduct);
2324184610Salfred
2325192938Sthompsa	/* get serial number string */
2326197559Sthompsa	usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2327192938Sthompsa	    udev->ddesc.iSerialNumber);
2328197559Sthompsa	udev->serial = strdup(temp_ptr, M_USB);
2329184610Salfred
2330192938Sthompsa	/* get manufacturer string */
2331197559Sthompsa	usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2332192938Sthompsa	    udev->ddesc.iManufacturer);
2333197559Sthompsa	usb_trim_spaces(temp_ptr);
2334197559Sthompsa	if (temp_ptr[0] != '\0')
2335197559Sthompsa		udev->manufacturer = strdup(temp_ptr, M_USB);
2336184610Salfred
2337192938Sthompsa	/* get product string */
2338197559Sthompsa	usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2339192938Sthompsa	    udev->ddesc.iProduct);
2340197559Sthompsa	usb_trim_spaces(temp_ptr);
2341197559Sthompsa	if (temp_ptr[0] != '\0')
2342197559Sthompsa		udev->product = strdup(temp_ptr, M_USB);
2343184610Salfred
2344194677Sthompsa#ifdef USB_VERBOSE
2345192938Sthompsa	if (udev->manufacturer == NULL || udev->product == NULL) {
2346192938Sthompsa		for (kdp = usb_knowndevs; kdp->vendorname != NULL; kdp++) {
2347184610Salfred			if (kdp->vendor == vendor_id &&
2348184610Salfred			    (kdp->product == product_id ||
2349184610Salfred			    (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
2350184610Salfred				break;
2351184610Salfred		}
2352184610Salfred		if (kdp->vendorname != NULL) {
2353192938Sthompsa			/* XXX should use pointer to knowndevs string */
2354192938Sthompsa			if (udev->manufacturer == NULL) {
2355192938Sthompsa				udev->manufacturer = strdup(kdp->vendorname,
2356192938Sthompsa				    M_USB);
2357192938Sthompsa			}
2358192938Sthompsa			if (udev->product == NULL &&
2359192938Sthompsa			    (kdp->flags & USB_KNOWNDEV_NOPROD) == 0) {
2360192938Sthompsa				udev->product = strdup(kdp->productname,
2361192938Sthompsa				    M_USB);
2362192938Sthompsa			}
2363184610Salfred		}
2364184610Salfred	}
2365184610Salfred#endif
2366192938Sthompsa	/* Provide default strings if none were found */
2367192938Sthompsa	if (udev->manufacturer == NULL) {
2368197559Sthompsa		snprintf(temp_ptr, temp_size, "vendor 0x%04x", vendor_id);
2369197559Sthompsa		udev->manufacturer = strdup(temp_ptr, M_USB);
2370184610Salfred	}
2371192938Sthompsa	if (udev->product == NULL) {
2372197559Sthompsa		snprintf(temp_ptr, temp_size, "product 0x%04x", product_id);
2373197559Sthompsa		udev->product = strdup(temp_ptr, M_USB);
2374184610Salfred	}
2375247090Shselasky
2376247090Shselasky	if (do_unlock)
2377247090Shselasky		usbd_enum_unlock(udev);
2378184610Salfred}
2379184610Salfred
2380188411Sthompsa/*
2381188411Sthompsa * Returns:
2382188411Sthompsa * See: USB_MODE_XXX
2383188411Sthompsa */
2384192499Sthompsaenum usb_hc_mode
2385194228Sthompsausbd_get_mode(struct usb_device *udev)
2386188411Sthompsa{
2387192499Sthompsa	return (udev->flags.usb_mode);
2388188411Sthompsa}
2389188411Sthompsa
2390188411Sthompsa/*
2391188411Sthompsa * Returns:
2392188411Sthompsa * See: USB_SPEED_XXX
2393188411Sthompsa */
2394192500Sthompsaenum usb_dev_speed
2395194228Sthompsausbd_get_speed(struct usb_device *udev)
2396184610Salfred{
2397184610Salfred	return (udev->speed);
2398184610Salfred}
2399184610Salfred
2400186730Salfreduint32_t
2401194228Sthompsausbd_get_isoc_fps(struct usb_device *udev)
2402186730Salfred{
2403186730Salfred	;				/* indent fix */
2404186730Salfred	switch (udev->speed) {
2405186730Salfred	case USB_SPEED_LOW:
2406186730Salfred	case USB_SPEED_FULL:
2407186730Salfred		return (1000);
2408186730Salfred	default:
2409186730Salfred		return (8000);
2410186730Salfred	}
2411186730Salfred}
2412186730Salfred
2413192984Sthompsastruct usb_device_descriptor *
2414194228Sthompsausbd_get_device_descriptor(struct usb_device *udev)
2415184610Salfred{
2416184610Salfred	if (udev == NULL)
2417184610Salfred		return (NULL);		/* be NULL safe */
2418184610Salfred	return (&udev->ddesc);
2419184610Salfred}
2420184610Salfred
2421192984Sthompsastruct usb_config_descriptor *
2422194228Sthompsausbd_get_config_descriptor(struct usb_device *udev)
2423184610Salfred{
2424184610Salfred	if (udev == NULL)
2425184610Salfred		return (NULL);		/* be NULL safe */
2426184610Salfred	return (udev->cdesc);
2427184610Salfred}
2428184610Salfred
2429184610Salfred/*------------------------------------------------------------------------*
2430194228Sthompsa *	usb_test_quirk - test a device for a given quirk
2431184610Salfred *
2432184610Salfred * Return values:
2433184610Salfred * 0: The USB device does not have the given quirk.
2434184610Salfred * Else: The USB device has the given quirk.
2435184610Salfred *------------------------------------------------------------------------*/
2436184610Salfreduint8_t
2437194228Sthompsausb_test_quirk(const struct usb_attach_arg *uaa, uint16_t quirk)
2438184610Salfred{
2439184610Salfred	uint8_t found;
2440225469Shselasky	uint8_t x;
2441184610Salfred
2442225469Shselasky	if (quirk == UQ_NONE)
2443225469Shselasky		return (0);
2444225469Shselasky
2445225469Shselasky	/* search the automatic per device quirks first */
2446225469Shselasky
2447225469Shselasky	for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) {
2448225469Shselasky		if (uaa->device->autoQuirk[x] == quirk)
2449225469Shselasky			return (1);
2450225469Shselasky	}
2451225469Shselasky
2452225469Shselasky	/* search global quirk table, if any */
2453225469Shselasky
2454194228Sthompsa	found = (usb_test_quirk_p) (&uaa->info, quirk);
2455225469Shselasky
2456184610Salfred	return (found);
2457184610Salfred}
2458184610Salfred
2459192984Sthompsastruct usb_interface_descriptor *
2460194228Sthompsausbd_get_interface_descriptor(struct usb_interface *iface)
2461184610Salfred{
2462184610Salfred	if (iface == NULL)
2463184610Salfred		return (NULL);		/* be NULL safe */
2464184610Salfred	return (iface->idesc);
2465184610Salfred}
2466184610Salfred
2467184610Salfreduint8_t
2468194228Sthompsausbd_get_interface_altindex(struct usb_interface *iface)
2469184610Salfred{
2470184610Salfred	return (iface->alt_index);
2471184610Salfred}
2472184610Salfred
2473184610Salfreduint8_t
2474194228Sthompsausbd_get_bus_index(struct usb_device *udev)
2475184610Salfred{
2476184610Salfred	return ((uint8_t)device_get_unit(udev->bus->bdev));
2477184610Salfred}
2478184610Salfred
2479184610Salfreduint8_t
2480194228Sthompsausbd_get_device_index(struct usb_device *udev)
2481184610Salfred{
2482184610Salfred	return (udev->device_index);
2483184610Salfred}
2484184610Salfred
2485214221Shselasky#if USB_HAVE_DEVCTL
2486184610Salfredstatic void
2487207020Sthompsausb_notify_addq(const char *type, struct usb_device *udev)
2488207020Sthompsa{
2489207020Sthompsa	struct usb_interface *iface;
2490207020Sthompsa	struct sbuf *sb;
2491207020Sthompsa	int i;
2492207020Sthompsa
2493207020Sthompsa	/* announce the device */
2494207020Sthompsa	sb = sbuf_new_auto();
2495207020Sthompsa	sbuf_printf(sb,
2496214221Shselasky#if USB_HAVE_UGEN
2497214221Shselasky	    "ugen=%s "
2498216072Shselasky	    "cdev=%s "
2499214221Shselasky#endif
2500207020Sthompsa	    "vendor=0x%04x "
2501207020Sthompsa	    "product=0x%04x "
2502207020Sthompsa	    "devclass=0x%02x "
2503207020Sthompsa	    "devsubclass=0x%02x "
2504207020Sthompsa	    "sernum=\"%s\" "
2505207020Sthompsa	    "release=0x%04x "
2506207027Sthompsa	    "mode=%s "
2507207020Sthompsa	    "port=%u "
2508214221Shselasky#if USB_HAVE_UGEN
2509214809Sn_hibma	    "parent=%s"
2510214221Shselasky#endif
2511214221Shselasky	    "",
2512214221Shselasky#if USB_HAVE_UGEN
2513207020Sthompsa	    udev->ugen_name,
2514216072Shselasky	    udev->ugen_name,
2515214221Shselasky#endif
2516207020Sthompsa	    UGETW(udev->ddesc.idVendor),
2517207020Sthompsa	    UGETW(udev->ddesc.idProduct),
2518207020Sthompsa	    udev->ddesc.bDeviceClass,
2519207020Sthompsa	    udev->ddesc.bDeviceSubClass,
2520212136Sthompsa	    usb_get_serial(udev),
2521207020Sthompsa	    UGETW(udev->ddesc.bcdDevice),
2522207027Sthompsa	    (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
2523214221Shselasky	    udev->port_no
2524214221Shselasky#if USB_HAVE_UGEN
2525214221Shselasky	    , udev->parent_hub != NULL ?
2526214221Shselasky		udev->parent_hub->ugen_name :
2527214221Shselasky		device_get_nameunit(device_get_parent(udev->bus->bdev))
2528214221Shselasky#endif
2529214221Shselasky	    );
2530207020Sthompsa	sbuf_finish(sb);
2531207020Sthompsa	devctl_notify("USB", "DEVICE", type, sbuf_data(sb));
2532207020Sthompsa	sbuf_delete(sb);
2533207020Sthompsa
2534207020Sthompsa	/* announce each interface */
2535207020Sthompsa	for (i = 0; i < USB_IFACE_MAX; i++) {
2536207020Sthompsa		iface = usbd_get_iface(udev, i);
2537207020Sthompsa		if (iface == NULL)
2538207020Sthompsa			break;		/* end of interfaces */
2539207020Sthompsa		if (iface->idesc == NULL)
2540207020Sthompsa			continue;	/* no interface descriptor */
2541207020Sthompsa
2542207020Sthompsa		sb = sbuf_new_auto();
2543207020Sthompsa		sbuf_printf(sb,
2544214221Shselasky#if USB_HAVE_UGEN
2545214221Shselasky		    "ugen=%s "
2546216072Shselasky		    "cdev=%s "
2547214221Shselasky#endif
2548207020Sthompsa		    "vendor=0x%04x "
2549207020Sthompsa		    "product=0x%04x "
2550207020Sthompsa		    "devclass=0x%02x "
2551207020Sthompsa		    "devsubclass=0x%02x "
2552207020Sthompsa		    "sernum=\"%s\" "
2553207020Sthompsa		    "release=0x%04x "
2554207027Sthompsa		    "mode=%s "
2555207020Sthompsa		    "interface=%d "
2556207020Sthompsa		    "endpoints=%d "
2557207020Sthompsa		    "intclass=0x%02x "
2558207020Sthompsa		    "intsubclass=0x%02x "
2559214809Sn_hibma		    "intprotocol=0x%02x",
2560214221Shselasky#if USB_HAVE_UGEN
2561207020Sthompsa		    udev->ugen_name,
2562216072Shselasky		    udev->ugen_name,
2563214221Shselasky#endif
2564207020Sthompsa		    UGETW(udev->ddesc.idVendor),
2565207020Sthompsa		    UGETW(udev->ddesc.idProduct),
2566207020Sthompsa		    udev->ddesc.bDeviceClass,
2567207020Sthompsa		    udev->ddesc.bDeviceSubClass,
2568212136Sthompsa		    usb_get_serial(udev),
2569207020Sthompsa		    UGETW(udev->ddesc.bcdDevice),
2570207027Sthompsa		    (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
2571207020Sthompsa		    iface->idesc->bInterfaceNumber,
2572207020Sthompsa		    iface->idesc->bNumEndpoints,
2573207020Sthompsa		    iface->idesc->bInterfaceClass,
2574207020Sthompsa		    iface->idesc->bInterfaceSubClass,
2575207020Sthompsa		    iface->idesc->bInterfaceProtocol);
2576207020Sthompsa		sbuf_finish(sb);
2577207020Sthompsa		devctl_notify("USB", "INTERFACE", type, sbuf_data(sb));
2578207020Sthompsa		sbuf_delete(sb);
2579207020Sthompsa	}
2580207020Sthompsa}
2581214221Shselasky#endif
2582207020Sthompsa
2583214221Shselasky#if USB_HAVE_UGEN
2584184610Salfred/*------------------------------------------------------------------------*
2585194228Sthompsa *	usb_fifo_free_wrap
2586184610Salfred *
2587185087Salfred * This function will free the FIFOs.
2588185087Salfred *
2589190730Sthompsa * Description of "flag" argument: If the USB_UNCFG_FLAG_FREE_EP0 flag
2590190730Sthompsa * is set and "iface_index" is set to "USB_IFACE_INDEX_ANY", we free
2591190730Sthompsa * all FIFOs. If the USB_UNCFG_FLAG_FREE_EP0 flag is not set and
2592190730Sthompsa * "iface_index" is set to "USB_IFACE_INDEX_ANY", we free all non
2593190730Sthompsa * control endpoint FIFOs. If "iface_index" is not set to
2594190730Sthompsa * "USB_IFACE_INDEX_ANY" the flag has no effect.
2595184610Salfred *------------------------------------------------------------------------*/
2596184610Salfredstatic void
2597194228Sthompsausb_fifo_free_wrap(struct usb_device *udev,
2598185087Salfred    uint8_t iface_index, uint8_t flag)
2599184610Salfred{
2600192984Sthompsa	struct usb_fifo *f;
2601184610Salfred	uint16_t i;
2602184610Salfred
2603184610Salfred	/*
2604184610Salfred	 * Free any USB FIFOs on the given interface:
2605184610Salfred	 */
2606184610Salfred	for (i = 0; i != USB_FIFO_MAX; i++) {
2607184610Salfred		f = udev->fifo[i];
2608184610Salfred		if (f == NULL) {
2609184610Salfred			continue;
2610184610Salfred		}
2611185087Salfred		/* Check if the interface index matches */
2612185087Salfred		if (iface_index == f->iface_index) {
2613194228Sthompsa			if (f->methods != &usb_ugen_methods) {
2614185087Salfred				/*
2615185087Salfred				 * Don't free any non-generic FIFOs in
2616185087Salfred				 * this case.
2617185087Salfred				 */
2618185087Salfred				continue;
2619185087Salfred			}
2620185290Salfred			if ((f->dev_ep_index == 0) &&
2621185290Salfred			    (f->fs_xfer == NULL)) {
2622185290Salfred				/* no need to free this FIFO */
2623185087Salfred				continue;
2624185087Salfred			}
2625185087Salfred		} else if (iface_index == USB_IFACE_INDEX_ANY) {
2626194228Sthompsa			if ((f->methods == &usb_ugen_methods) &&
2627190730Sthompsa			    (f->dev_ep_index == 0) &&
2628190730Sthompsa			    (!(flag & USB_UNCFG_FLAG_FREE_EP0)) &&
2629185290Salfred			    (f->fs_xfer == NULL)) {
2630185290Salfred				/* no need to free this FIFO */
2631185087Salfred				continue;
2632185087Salfred			}
2633185087Salfred		} else {
2634185290Salfred			/* no need to free this FIFO */
2635184610Salfred			continue;
2636184610Salfred		}
2637263800Shselasky		/* wait for pending refs to go away */
2638263800Shselasky		usb_wait_pending_ref_locked(udev);
2639263800Shselasky
2640185290Salfred		/* free this FIFO */
2641194228Sthompsa		usb_fifo_free(f);
2642263800Shselasky
2643263800Shselasky		/* restore refcount */
2644263800Shselasky		usb_ref_restore_locked(udev);
2645184610Salfred	}
2646184610Salfred}
2647189599Sthompsa#endif
2648186730Salfred
2649186730Salfred/*------------------------------------------------------------------------*
2650194228Sthompsa *	usb_peer_can_wakeup
2651186730Salfred *
2652186730Salfred * Return values:
2653186730Salfred * 0: Peer cannot do resume signalling.
2654186730Salfred * Else: Peer can do resume signalling.
2655186730Salfred *------------------------------------------------------------------------*/
2656186730Salfreduint8_t
2657194228Sthompsausb_peer_can_wakeup(struct usb_device *udev)
2658186730Salfred{
2659192984Sthompsa	const struct usb_config_descriptor *cdp;
2660186730Salfred
2661186730Salfred	cdp = udev->cdesc;
2662192499Sthompsa	if ((cdp != NULL) && (udev->flags.usb_mode == USB_MODE_HOST)) {
2663186730Salfred		return (cdp->bmAttributes & UC_REMOTE_WAKEUP);
2664186730Salfred	}
2665186730Salfred	return (0);			/* not supported */
2666186730Salfred}
2667191494Sthompsa
2668191494Sthompsavoid
2669194228Sthompsausb_set_device_state(struct usb_device *udev, enum usb_dev_state state)
2670191494Sthompsa{
2671191494Sthompsa
2672191494Sthompsa	KASSERT(state < USB_STATE_MAX, ("invalid udev state"));
2673191494Sthompsa
2674191494Sthompsa	DPRINTF("udev %p state %s -> %s\n", udev,
2675194228Sthompsa	    usb_statestr(udev->state), usb_statestr(state));
2676261109Shselasky
2677261109Shselasky#if USB_HAVE_UGEN
2678261109Shselasky	mtx_lock(&usb_ref_lock);
2679261109Shselasky#endif
2680191494Sthompsa	udev->state = state;
2681261109Shselasky#if USB_HAVE_UGEN
2682261109Shselasky	mtx_unlock(&usb_ref_lock);
2683261109Shselasky#endif
2684213435Shselasky	if (udev->bus->methods->device_state_change != NULL)
2685213435Shselasky		(udev->bus->methods->device_state_change) (udev);
2686191494Sthompsa}
2687191494Sthompsa
2688213435Shselaskyenum usb_dev_state
2689213435Shselaskyusb_get_device_state(struct usb_device *udev)
2690213435Shselasky{
2691213435Shselasky	if (udev == NULL)
2692213435Shselasky		return (USB_STATE_DETACHED);
2693213435Shselasky	return (udev->state);
2694213435Shselasky}
2695213435Shselasky
2696191824Sthompsauint8_t
2697194228Sthompsausbd_device_attached(struct usb_device *udev)
2698191494Sthompsa{
2699191494Sthompsa	return (udev->state > USB_STATE_DETACHED);
2700191494Sthompsa}
2701196498Salfred
2702247090Shselasky/*
2703247090Shselasky * The following function locks enumerating the given USB device. If
2704247090Shselasky * the lock is already grabbed this function returns zero. Else a
2705247090Shselasky * non-zero value is returned.
2706247090Shselasky */
2707247090Shselaskyuint8_t
2708196498Salfredusbd_enum_lock(struct usb_device *udev)
2709196498Salfred{
2710247090Shselasky	if (sx_xlocked(&udev->enum_sx))
2711247090Shselasky		return (0);
2712247090Shselasky
2713207079Sthompsa	sx_xlock(&udev->enum_sx);
2714208008Sthompsa	sx_xlock(&udev->sr_sx);
2715196498Salfred	/*
2716196498Salfred	 * NEWBUS LOCK NOTE: We should check if any parent SX locks
2717196498Salfred	 * are locked before locking Giant. Else the lock can be
2718196498Salfred	 * locked multiple times.
2719196498Salfred	 */
2720196498Salfred	mtx_lock(&Giant);
2721247090Shselasky	return (1);
2722196498Salfred}
2723196498Salfred
2724196498Salfred/* The following function unlocks enumerating the given USB device. */
2725196498Salfred
2726196498Salfredvoid
2727196498Salfredusbd_enum_unlock(struct usb_device *udev)
2728196498Salfred{
2729196498Salfred	mtx_unlock(&Giant);
2730207079Sthompsa	sx_xunlock(&udev->enum_sx);
2731208008Sthompsa	sx_xunlock(&udev->sr_sx);
2732196498Salfred}
2733196498Salfred
2734208008Sthompsa/* The following function locks suspend and resume. */
2735208008Sthompsa
2736208008Sthompsavoid
2737208008Sthompsausbd_sr_lock(struct usb_device *udev)
2738208008Sthompsa{
2739208008Sthompsa	sx_xlock(&udev->sr_sx);
2740208008Sthompsa	/*
2741208008Sthompsa	 * NEWBUS LOCK NOTE: We should check if any parent SX locks
2742208008Sthompsa	 * are locked before locking Giant. Else the lock can be
2743208008Sthompsa	 * locked multiple times.
2744208008Sthompsa	 */
2745208008Sthompsa	mtx_lock(&Giant);
2746208008Sthompsa}
2747208008Sthompsa
2748208008Sthompsa/* The following function unlocks suspend and resume. */
2749208008Sthompsa
2750208008Sthompsavoid
2751208008Sthompsausbd_sr_unlock(struct usb_device *udev)
2752208008Sthompsa{
2753208008Sthompsa	mtx_unlock(&Giant);
2754208008Sthompsa	sx_xunlock(&udev->sr_sx);
2755208008Sthompsa}
2756208008Sthompsa
2757196498Salfred/*
2758196498Salfred * The following function checks the enumerating lock for the given
2759196498Salfred * USB device.
2760196498Salfred */
2761196498Salfred
2762196498Salfreduint8_t
2763196498Salfredusbd_enum_is_locked(struct usb_device *udev)
2764196498Salfred{
2765207079Sthompsa	return (sx_xlocked(&udev->enum_sx));
2766196498Salfred}
2767214429Shselasky
2768214429Shselasky/*
2769214429Shselasky * The following function is used to set the per-interface specific
2770214429Shselasky * plug and play information. The string referred to by the pnpinfo
2771214429Shselasky * argument can safely be freed after calling this function. The
2772214429Shselasky * pnpinfo of an interface will be reset at device detach or when
2773214429Shselasky * passing a NULL argument to this function. This function
2774214429Shselasky * returns zero on success, else a USB_ERR_XXX failure code.
2775214429Shselasky */
2776214429Shselasky
2777214429Shselaskyusb_error_t
2778214429Shselaskyusbd_set_pnpinfo(struct usb_device *udev, uint8_t iface_index, const char *pnpinfo)
2779214429Shselasky{
2780214429Shselasky	struct usb_interface *iface;
2781214429Shselasky
2782214429Shselasky	iface = usbd_get_iface(udev, iface_index);
2783214429Shselasky	if (iface == NULL)
2784214429Shselasky		return (USB_ERR_INVAL);
2785214429Shselasky
2786214429Shselasky	if (iface->pnpinfo != NULL) {
2787214429Shselasky		free(iface->pnpinfo, M_USBDEV);
2788214429Shselasky		iface->pnpinfo = NULL;
2789214429Shselasky	}
2790214429Shselasky
2791214429Shselasky	if (pnpinfo == NULL || pnpinfo[0] == 0)
2792214429Shselasky		return (0);		/* success */
2793214429Shselasky
2794214429Shselasky	iface->pnpinfo = strdup(pnpinfo, M_USBDEV);
2795214429Shselasky	if (iface->pnpinfo == NULL)
2796214429Shselasky		return (USB_ERR_NOMEM);
2797214429Shselasky
2798214429Shselasky	return (0);			/* success */
2799214429Shselasky}
2800214429Shselasky
2801225350Shselaskyusb_error_t
2802225350Shselaskyusbd_add_dynamic_quirk(struct usb_device *udev, uint16_t quirk)
2803225350Shselasky{
2804225350Shselasky	uint8_t x;
2805225350Shselasky
2806225350Shselasky	for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) {
2807225469Shselasky		if (udev->autoQuirk[x] == 0 ||
2808225469Shselasky		    udev->autoQuirk[x] == quirk) {
2809225350Shselasky			udev->autoQuirk[x] = quirk;
2810225350Shselasky			return (0);	/* success */
2811225350Shselasky		}
2812225350Shselasky	}
2813225350Shselasky	return (USB_ERR_NOMEM);
2814225350Shselasky}
2815