usb_device.c revision 190181
1195098Sed/* $FreeBSD: head/sys/dev/usb/usb_device.c 190181 2009-03-20 21:50:54Z thompsa $ */
2195098Sed/*-
3195098Sed * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4195098Sed *
5195098Sed * Redistribution and use in source and binary forms, with or without
6195098Sed * modification, are permitted provided that the following conditions
7195098Sed * are met:
8195098Sed * 1. Redistributions of source code must retain the above copyright
9195098Sed *    notice, this list of conditions and the following disclaimer.
10195098Sed * 2. Redistributions in binary form must reproduce the above copyright
11198090Srdivacky *    notice, this list of conditions and the following disclaimer in the
12198090Srdivacky *    documentation and/or other materials provided with the distribution.
13195098Sed *
14198090Srdivacky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15218893Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16195340Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17198090Srdivacky * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18198090Srdivacky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19195098Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20205407Srdivacky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21202878Srdivacky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22202878Srdivacky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23198090Srdivacky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24198090Srdivacky * SUCH DAMAGE.
25198090Srdivacky */
26202878Srdivacky
27218893Sdim#include <dev/usb/usb_mfunc.h>
28218893Sdim#include <dev/usb/usb_error.h>
29218893Sdim#include <dev/usb/usb.h>
30218893Sdim#include <dev/usb/usb_ioctl.h>
31195098Sed#include "usbdevs.h"
32195098Sed
33195098Sed#define	USB_DEBUG_VAR usb2_debug
34195098Sed
35198090Srdivacky#include <dev/usb/usb_core.h>
36202878Srdivacky#include <dev/usb/usb_debug.h>
37198090Srdivacky#include <dev/usb/usb_process.h>
38205407Srdivacky#include <dev/usb/usb_device.h>
39212904Sdim#include <dev/usb/usb_busdma.h>
40218893Sdim#include <dev/usb/usb_transfer.h>
41218893Sdim#include <dev/usb/usb_parse.h>
42202878Srdivacky#include <dev/usb/usb_request.h>
43202878Srdivacky#include <dev/usb/usb_dynamic.h>
44203954Srdivacky#include <dev/usb/usb_hub.h>
45203954Srdivacky#include <dev/usb/usb_util.h>
46203954Srdivacky#include <dev/usb/usb_mbuf.h>
47218893Sdim#include <dev/usb/usb_msctest.h>
48203954Srdivacky#if USB_HAVE_UGEN
49218893Sdim#include <dev/usb/usb_dev.h>
50218893Sdim#include <dev/usb/usb_generic.h>
51198090Srdivacky#endif
52202878Srdivacky
53218893Sdim#include <dev/usb/quirk/usb_quirk.h>
54218893Sdim
55218893Sdim#include <dev/usb/usb_controller.h>
56218893Sdim#include <dev/usb/usb_bus.h>
57218893Sdim
58205218Srdivacky/* function prototypes */
59218893Sdim
60218893Sdimstatic void	usb2_fill_pipe_data(struct usb2_device *, uint8_t,
61218893Sdim		    struct usb2_endpoint_descriptor *, struct usb2_pipe *);
62203954Srdivackystatic void	usb2_free_pipe_data(struct usb2_device *, uint8_t, uint8_t);
63203954Srdivackystatic void	usb2_free_iface_data(struct usb2_device *);
64203954Srdivackystatic void	usb2_detach_device_sub(struct usb2_device *, device_t *,
65198090Srdivacky		    uint8_t);
66195098Sedstatic uint8_t	usb2_probe_and_attach_sub(struct usb2_device *,
67202878Srdivacky		    struct usb2_attach_arg *);
68202878Srdivackystatic void	usb2_init_attach_arg(struct usb2_device *,
69202878Srdivacky		    struct usb2_attach_arg *);
70202878Srdivackystatic void	usb2_suspend_resume_sub(struct usb2_device *, device_t,
71202878Srdivacky		    uint8_t);
72202878Srdivackystatic void	usb2_clear_stall_proc(struct usb2_proc_msg *_pm);
73202878Srdivacky#if USB_HAVE_STRINGS
74202878Srdivackystatic void	usb2_check_strings(struct usb2_device *);
75202878Srdivacky#endif
76203954Srdivackystatic usb2_error_t usb2_fill_iface_data(struct usb2_device *, uint8_t,
77203954Srdivacky		    uint8_t);
78203954Srdivackystatic void	usb2_notify_addq(const char *type, struct usb2_device *);
79203954Srdivacky
80218893Sdim#if USB_HAVE_UGEN
81206274Srdivackystatic void	usb2_fifo_free_wrap(struct usb2_device *, uint8_t, uint8_t);
82206274Srdivackystatic struct cdev *usb2_make_dev(struct usb2_device *, int, int);
83203954Srdivackystatic void	usb2_cdev_create(struct usb2_device *);
84202878Srdivackystatic void	usb2_cdev_free(struct usb2_device *);
85202878Srdivackystatic void	usb2_cdev_cleanup(void *);
86202878Srdivacky#endif
87202878Srdivacky
88202878Srdivacky/* This variable is global to allow easy access to it: */
89203954Srdivacky
90203954Srdivackyint	usb2_template = 0;
91203954Srdivacky
92203954SrdivackySYSCTL_INT(_hw_usb2, OID_AUTO, template, CTLFLAG_RW,
93202878Srdivacky    &usb2_template, 0, "Selected USB device side template");
94202878Srdivacky
95202878Srdivacky
96202878Srdivacky/*------------------------------------------------------------------------*
97202878Srdivacky *	usb2_get_pipe_by_addr
98202878Srdivacky *
99202878Srdivacky * This function searches for an USB pipe by endpoint address and
100202878Srdivacky * direction.
101203954Srdivacky *
102202878Srdivacky * Returns:
103202878Srdivacky * NULL: Failure
104202878Srdivacky * Else: Success
105202878Srdivacky *------------------------------------------------------------------------*/
106203954Srdivackystruct usb2_pipe *
107198090Srdivackyusb2_get_pipe_by_addr(struct usb2_device *udev, uint8_t ea_val)
108198090Srdivacky{
109195098Sed	struct usb2_pipe *pipe = udev->pipes;
110218893Sdim	struct usb2_pipe *pipe_end = udev->pipes + USB_EP_MAX;
111195098Sed	enum {
112218893Sdim		EA_MASK = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR),
113218893Sdim	};
114218893Sdim
115218893Sdim	/*
116218893Sdim	 * According to the USB specification not all bits are used
117218893Sdim	 * for the endpoint address. Keep defined bits only:
118218893Sdim	 */
119218893Sdim	ea_val &= EA_MASK;
120198090Srdivacky
121195098Sed	/*
122202878Srdivacky	 * Iterate accross all the USB pipes searching for a match
123218893Sdim	 * based on the endpoint address:
124195098Sed	 */
125198090Srdivacky	for (; pipe != pipe_end; pipe++) {
126218893Sdim
127218893Sdim		if (pipe->edesc == NULL) {
128218893Sdim			continue;
129218893Sdim		}
130195098Sed		/* do the mask and check the value */
131202878Srdivacky		if ((pipe->edesc->bEndpointAddress & EA_MASK) == ea_val) {
132195098Sed			goto found;
133198090Srdivacky		}
134208599Srdivacky	}
135208599Srdivacky
136208599Srdivacky	/*
137208599Srdivacky	 * The default pipe is always present and is checked separately:
138203954Srdivacky	 */
139202878Srdivacky	if ((udev->default_pipe.edesc) &&
140198090Srdivacky	    ((udev->default_pipe.edesc->bEndpointAddress & EA_MASK) == ea_val)) {
141195098Sed		pipe = &udev->default_pipe;
142202878Srdivacky		goto found;
143202878Srdivacky	}
144202878Srdivacky	return (NULL);
145202878Srdivacky
146202878Srdivackyfound:
147218893Sdim	return (pipe);
148198090Srdivacky}
149198090Srdivacky
150195098Sed/*------------------------------------------------------------------------*
151208599Srdivacky *	usb2_get_pipe
152208599Srdivacky *
153218893Sdim * This function searches for an USB pipe based on the information
154202878Srdivacky * given by the passed "struct usb2_config" pointer.
155195098Sed *
156218893Sdim * Return values:
157218893Sdim * NULL: No match.
158218893Sdim * Else: Pointer to "struct usb2_pipe".
159218893Sdim *------------------------------------------------------------------------*/
160218893Sdimstruct usb2_pipe *
161218893Sdimusb2_get_pipe(struct usb2_device *udev, uint8_t iface_index,
162218893Sdim    const struct usb2_config *setup)
163218893Sdim{
164218893Sdim	struct usb2_pipe *pipe = udev->pipes;
165203954Srdivacky	struct usb2_pipe *pipe_end = udev->pipes + USB_EP_MAX;
166195098Sed	uint8_t index = setup->ep_index;
167218893Sdim	uint8_t ea_mask;
168202878Srdivacky	uint8_t ea_val;
169202878Srdivacky	uint8_t type_mask;
170202878Srdivacky	uint8_t type_val;
171198090Srdivacky
172198090Srdivacky	DPRINTFN(10, "udev=%p iface_index=%d address=0x%x "
173198090Srdivacky	    "type=0x%x dir=0x%x index=%d\n",
174195098Sed	    udev, iface_index, setup->endpoint,
175204642Srdivacky	    setup->type, setup->direction, setup->ep_index);
176204642Srdivacky
177204642Srdivacky	/* setup expected endpoint direction mask and value */
178198090Srdivacky
179198090Srdivacky	if (setup->direction == UE_DIR_ANY) {
180203954Srdivacky		/* match any endpoint direction */
181203954Srdivacky		ea_mask = 0;
182218893Sdim		ea_val = 0;
183218893Sdim	} else {
184218893Sdim		/* match the given endpoint direction */
185218893Sdim		ea_mask = (UE_DIR_IN | UE_DIR_OUT);
186203954Srdivacky		ea_val = (setup->direction & (UE_DIR_IN | UE_DIR_OUT));
187218893Sdim	}
188218893Sdim
189218893Sdim	/* setup expected endpoint address */
190218893Sdim
191218893Sdim	if (setup->endpoint == UE_ADDR_ANY) {
192218893Sdim		/* match any endpoint address */
193218893Sdim	} else {
194218893Sdim		/* match the given endpoint address */
195203954Srdivacky		ea_mask |= UE_ADDR;
196218893Sdim		ea_val |= (setup->endpoint & UE_ADDR);
197218893Sdim	}
198206274Srdivacky
199206274Srdivacky	/* setup expected endpoint type */
200206274Srdivacky
201218893Sdim	if (setup->type == UE_BULK_INTR) {
202198090Srdivacky		/* this will match BULK and INTERRUPT endpoints */
203218893Sdim		type_mask = 2;
204198090Srdivacky		type_val = 2;
205198090Srdivacky	} else if (setup->type == UE_TYPE_ANY) {
206195098Sed		/* match any endpoint type */
207198090Srdivacky		type_mask = 0;
208195098Sed		type_val = 0;
209202878Srdivacky	} else {
210202878Srdivacky		/* match the given endpoint type */
211202878Srdivacky		type_mask = UE_XFERTYPE;
212202878Srdivacky		type_val = (setup->type & UE_XFERTYPE);
213202878Srdivacky	}
214202878Srdivacky
215218893Sdim	/*
216202878Srdivacky	 * Iterate accross all the USB pipes searching for a match
217202878Srdivacky	 * based on the endpoint address. Note that we are searching
218218893Sdim	 * the pipes from the beginning of the "udev->pipes" array.
219202878Srdivacky	 */
220202878Srdivacky	for (; pipe != pipe_end; pipe++) {
221202878Srdivacky
222218893Sdim		if ((pipe->edesc == NULL) ||
223202878Srdivacky		    (pipe->iface_index != iface_index)) {
224202878Srdivacky			continue;
225202878Srdivacky		}
226202878Srdivacky		/* do the masks and check the values */
227202878Srdivacky
228202878Srdivacky		if (((pipe->edesc->bEndpointAddress & ea_mask) == ea_val) &&
229202878Srdivacky		    ((pipe->edesc->bmAttributes & type_mask) == type_val)) {
230202878Srdivacky			if (!index--) {
231202878Srdivacky				goto found;
232218893Sdim			}
233202878Srdivacky		}
234202878Srdivacky	}
235218893Sdim
236202878Srdivacky	/*
237202878Srdivacky	 * Match against default pipe last, so that "any pipe", "any
238202878Srdivacky	 * address" and "any direction" returns the first pipe of the
239202878Srdivacky	 * interface. "iface_index" and "direction" is ignored:
240202878Srdivacky	 */
241202878Srdivacky	if ((udev->default_pipe.edesc) &&
242202878Srdivacky	    ((udev->default_pipe.edesc->bEndpointAddress & ea_mask) == ea_val) &&
243218893Sdim	    ((udev->default_pipe.edesc->bmAttributes & type_mask) == type_val) &&
244202878Srdivacky	    (!index)) {
245202878Srdivacky		pipe = &udev->default_pipe;
246218893Sdim		goto found;
247202878Srdivacky	}
248202878Srdivacky	return (NULL);
249202878Srdivacky
250202878Srdivackyfound:
251202878Srdivacky	return (pipe);
252195098Sed}
253195098Sed
254195098Sed/*------------------------------------------------------------------------*
255195098Sed *	usb2_interface_count
256195098Sed *
257218893Sdim * This function stores the number of USB interfaces excluding
258198090Srdivacky * alternate settings, which the USB config descriptor reports into
259218893Sdim * the unsigned 8-bit integer pointed to by "count".
260195098Sed *
261195098Sed * Returns:
262195098Sed *    0: Success
263198090Srdivacky * Else: Failure
264208599Srdivacky *------------------------------------------------------------------------*/
265218893Sdimusb2_error_t
266195098Sedusb2_interface_count(struct usb2_device *udev, uint8_t *count)
267218893Sdim{
268202878Srdivacky	if (udev->cdesc == NULL) {
269218893Sdim		*count = 0;
270195098Sed		return (USB_ERR_NOT_CONFIGURED);
271195098Sed	}
272202878Srdivacky	*count = udev->cdesc->bNumInterface;
273198090Srdivacky	return (USB_ERR_NORMAL_COMPLETION);
274198090Srdivacky}
275218893Sdim
276202878Srdivacky
277218893Sdim/*------------------------------------------------------------------------*
278218893Sdim *	usb2_fill_pipe_data
279195098Sed *
280202878Srdivacky * This function will initialise the USB pipe structure pointed to by
281198090Srdivacky * the "pipe" argument.
282195098Sed *------------------------------------------------------------------------*/
283218893Sdimstatic void
284218893Sdimusb2_fill_pipe_data(struct usb2_device *udev, uint8_t iface_index,
285218893Sdim    struct usb2_endpoint_descriptor *edesc, struct usb2_pipe *pipe)
286218893Sdim{
287218893Sdim
288218893Sdim	bzero(pipe, sizeof(*pipe));
289218893Sdim
290218893Sdim	(udev->bus->methods->pipe_init) (udev, edesc, pipe);
291218893Sdim
292198090Srdivacky	if (pipe->methods == NULL) {
293202878Srdivacky		/* the pipe is invalid: just return */
294202878Srdivacky		return;
295198396Srdivacky	}
296198396Srdivacky	/* initialise USB pipe structure */
297208599Srdivacky	pipe->edesc = edesc;
298195098Sed	pipe->iface_index = iface_index;
299195098Sed	TAILQ_INIT(&pipe->pipe_q.head);
300218893Sdim	pipe->pipe_q.command = &usb2_pipe_start;
301218893Sdim
302218893Sdim	/* clear stall, if any */
303218893Sdim	if (udev->bus->methods->clear_stall) {
304218893Sdim		USB_BUS_LOCK(udev->bus);
305218893Sdim		(udev->bus->methods->clear_stall) (udev, pipe);
306218893Sdim		USB_BUS_UNLOCK(udev->bus);
307218893Sdim	}
308218893Sdim}
309218893Sdim
310218893Sdim/*------------------------------------------------------------------------*
311218893Sdim *	usb2_free_pipe_data
312198396Srdivacky *
313202878Srdivacky * This function will free USB pipe data for the given interface
314195098Sed * index. Hence we do not have any dynamic allocations we simply clear
315202878Srdivacky * "pipe->edesc" to indicate that the USB pipe structure can be
316203954Srdivacky * reused. The pipes belonging to the given interface should not be in
317203954Srdivacky * use when this function is called and no check is performed to
318203954Srdivacky * prevent this.
319203954Srdivacky *------------------------------------------------------------------------*/
320203954Srdivackystatic void
321203954Srdivackyusb2_free_pipe_data(struct usb2_device *udev,
322218893Sdim    uint8_t iface_index, uint8_t iface_mask)
323203954Srdivacky{
324203954Srdivacky	struct usb2_pipe *pipe = udev->pipes;
325203954Srdivacky	struct usb2_pipe *pipe_end = udev->pipes + USB_EP_MAX;
326203954Srdivacky
327203954Srdivacky	while (pipe != pipe_end) {
328203954Srdivacky		if ((pipe->iface_index & iface_mask) == iface_index) {
329203954Srdivacky			/* free pipe */
330203954Srdivacky			pipe->edesc = NULL;
331203954Srdivacky		}
332203954Srdivacky		pipe++;
333203954Srdivacky	}
334218893Sdim}
335203954Srdivacky
336203954Srdivacky/*------------------------------------------------------------------------*
337203954Srdivacky *	usb2_pipe_foreach
338203954Srdivacky *
339203954Srdivacky * This function will iterate all the USB endpoints except the control
340203954Srdivacky * endpoint. This function is NULL safe.
341210299Sed *
342210299Sed * Return values:
343210299Sed * NULL: End of USB pipes
344210299Sed * Else: Pointer to next USB pipe
345210299Sed *------------------------------------------------------------------------*/
346210299Sedstruct usb2_pipe *
347218893Sdimusb2_pipe_foreach(struct usb2_device *udev, struct usb2_pipe *pipe)
348210299Sed{
349210299Sed	struct usb2_pipe *pipe_end = udev->pipes + USB_EP_MAX;
350210299Sed
351210299Sed	/* be NULL safe */
352210299Sed	if (udev == NULL)
353202878Srdivacky		return (NULL);
354202878Srdivacky
355210299Sed	/* get next pipe */
356195098Sed	if (pipe == NULL)
357195098Sed		pipe = udev->pipes;
358202878Srdivacky	else
359202878Srdivacky		pipe++;
360195098Sed
361195098Sed	/* find next allocated pipe */
362198090Srdivacky	while (pipe != pipe_end) {
363202878Srdivacky		if (pipe->edesc != NULL)
364202878Srdivacky			return (pipe);
365198090Srdivacky		pipe++;
366198090Srdivacky	}
367208599Srdivacky	return (NULL);
368208599Srdivacky}
369208599Srdivacky
370208599Srdivacky/*------------------------------------------------------------------------*
371208599Srdivacky *	usb2_fill_iface_data
372208599Srdivacky *
373208599Srdivacky * This function will fill in interface data and allocate USB pipes
374208599Srdivacky * for all the endpoints that belong to the given interface. This
375208599Srdivacky * function is typically called when setting the configuration or when
376208599Srdivacky * setting an alternate interface.
377208599Srdivacky *------------------------------------------------------------------------*/
378208599Srdivackystatic usb2_error_t
379208599Srdivackyusb2_fill_iface_data(struct usb2_device *udev,
380208599Srdivacky    uint8_t iface_index, uint8_t alt_index)
381208599Srdivacky{
382208599Srdivacky	struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
383208599Srdivacky	struct usb2_pipe *pipe;
384208599Srdivacky	struct usb2_pipe *pipe_end;
385208599Srdivacky	struct usb2_interface_descriptor *id;
386208599Srdivacky	struct usb2_endpoint_descriptor *ed = NULL;
387203954Srdivacky	struct usb2_descriptor *desc;
388203954Srdivacky	uint8_t nendpt;
389203954Srdivacky
390203954Srdivacky	if (iface == NULL) {
391203954Srdivacky		return (USB_ERR_INVAL);
392202878Srdivacky	}
393198090Srdivacky	DPRINTFN(5, "iface_index=%d alt_index=%d\n",
394202878Srdivacky	    iface_index, alt_index);
395203954Srdivacky
396203954Srdivacky	sx_assert(udev->default_sx + 1, SA_LOCKED);
397202878Srdivacky
398202878Srdivacky	pipe = udev->pipes;
399202878Srdivacky	pipe_end = udev->pipes + USB_EP_MAX;
400202878Srdivacky
401202878Srdivacky	/*
402198090Srdivacky	 * Check if any USB pipes on the given USB interface are in
403198090Srdivacky	 * use:
404202878Srdivacky	 */
405202878Srdivacky	while (pipe != pipe_end) {
406202878Srdivacky		if ((pipe->edesc != NULL) &&
407202878Srdivacky		    (pipe->iface_index == iface_index) &&
408202878Srdivacky		    (pipe->refcount != 0)) {
409202878Srdivacky			return (USB_ERR_IN_USE);
410202878Srdivacky		}
411202878Srdivacky		pipe++;
412202878Srdivacky	}
413202878Srdivacky
414198090Srdivacky	pipe = &udev->pipes[0];
415198090Srdivacky
416198090Srdivacky	id = usb2_find_idesc(udev->cdesc, iface_index, alt_index);
417198090Srdivacky	if (id == NULL) {
418218893Sdim		return (USB_ERR_INVAL);
419198090Srdivacky	}
420198090Srdivacky	/*
421198090Srdivacky	 * Free old pipes after we know that an interface descriptor exists,
422218893Sdim	 * if any.
423198090Srdivacky	 */
424202878Srdivacky	usb2_free_pipe_data(udev, iface_index, 0 - 1);
425198090Srdivacky
426198090Srdivacky	/* Setup USB interface structure */
427198090Srdivacky	iface->idesc = id;
428202878Srdivacky	iface->alt_index = alt_index;
429198090Srdivacky	iface->parent_iface_index = USB_IFACE_INDEX_ANY;
430198090Srdivacky
431208599Srdivacky	nendpt = id->bNumEndpoints;
432208599Srdivacky	DPRINTFN(5, "found idesc nendpt=%d\n", nendpt);
433208599Srdivacky
434208599Srdivacky	desc = (void *)id;
435208599Srdivacky
436208599Srdivacky	while (nendpt--) {
437208599Srdivacky		DPRINTFN(11, "endpt=%d\n", nendpt);
438208599Srdivacky
439208599Srdivacky		while ((desc = usb2_desc_foreach(udev->cdesc, desc))) {
440218893Sdim			if ((desc->bDescriptorType == UDESC_ENDPOINT) &&
441208599Srdivacky			    (desc->bLength >= sizeof(*ed))) {
442208599Srdivacky				goto found;
443208599Srdivacky			}
444218893Sdim			if (desc->bDescriptorType == UDESC_INTERFACE) {
445208599Srdivacky				break;
446208599Srdivacky			}
447208599Srdivacky		}
448202878Srdivacky		goto error;
449202878Srdivacky
450203954Srdivackyfound:
451203954Srdivacky		ed = (void *)desc;
452218893Sdim
453203954Srdivacky		/* find a free pipe */
454203954Srdivacky		while (pipe != pipe_end) {
455203954Srdivacky			if (pipe->edesc == NULL) {
456203954Srdivacky				/* pipe is free */
457203954Srdivacky				usb2_fill_pipe_data(udev, iface_index, ed, pipe);
458203954Srdivacky				break;
459218893Sdim			}
460203954Srdivacky			pipe++;
461203954Srdivacky		}
462203954Srdivacky	}
463203954Srdivacky	return (USB_ERR_NORMAL_COMPLETION);
464218893Sdim
465203954Srdivackyerror:
466203954Srdivacky	/* passed end, or bad desc */
467203954Srdivacky	DPRINTFN(0, "%s: bad descriptor(s), addr=%d!\n",
468203954Srdivacky	    __FUNCTION__, udev->address);
469203954Srdivacky
470203954Srdivacky	/* free old pipes if any */
471203954Srdivacky	usb2_free_pipe_data(udev, iface_index, 0 - 1);
472203954Srdivacky	return (USB_ERR_INVAL);
473203954Srdivacky}
474203954Srdivacky
475203954Srdivacky/*------------------------------------------------------------------------*
476203954Srdivacky *	usb2_free_iface_data
477203954Srdivacky *
478203954Srdivacky * This function will free all USB interfaces and USB pipes belonging
479218893Sdim * to an USB device.
480203954Srdivacky *------------------------------------------------------------------------*/
481203954Srdivackystatic void
482203954Srdivackyusb2_free_iface_data(struct usb2_device *udev)
483203954Srdivacky{
484202878Srdivacky	struct usb2_interface *iface = udev->ifaces;
485218893Sdim	struct usb2_interface *iface_end = udev->ifaces + USB_IFACE_MAX;
486202878Srdivacky
487218893Sdim	/* mtx_assert() */
488202878Srdivacky
489202878Srdivacky#if USB_HAVE_COMPAT_LINUX
490202878Srdivacky	/* free Linux compat device, if any */
491202878Srdivacky	if (udev->linux_dev) {
492202878Srdivacky		usb_linux_free_device(udev->linux_dev);
493202878Srdivacky		udev->linux_dev = NULL;
494202878Srdivacky	}
495202878Srdivacky#endif
496202878Srdivacky	/* free all pipes, if any */
497202878Srdivacky	usb2_free_pipe_data(udev, 0, 0);
498202878Srdivacky
499202878Srdivacky	/* free all interfaces, if any */
500202878Srdivacky	while (iface != iface_end) {
501202878Srdivacky		iface->idesc = NULL;
502202878Srdivacky		iface->alt_index = 0;
503202878Srdivacky		iface->parent_iface_index = USB_IFACE_INDEX_ANY;
504203954Srdivacky		iface++;
505203954Srdivacky	}
506202878Srdivacky
507195098Sed	/* free "cdesc" after "ifaces", if any */
508195098Sed	if (udev->cdesc) {
509202878Srdivacky		free(udev->cdesc, M_USB);
510202878Srdivacky		udev->cdesc = NULL;
511218893Sdim	}
512218893Sdim	/* set unconfigured state */
513218893Sdim	udev->curr_config_no = USB_UNCONFIG_NO;
514218893Sdim	udev->curr_config_index = USB_UNCONFIG_INDEX;
515218893Sdim}
516218893Sdim
517218893Sdim/*------------------------------------------------------------------------*
518202878Srdivacky *	usb2_set_config_index
519195098Sed *
520202878Srdivacky * This function selects configuration by index, independent of the
521202878Srdivacky * actual configuration number. This function should not be used by
522202878Srdivacky * USB drivers.
523202878Srdivacky *
524202878Srdivacky * Returns:
525202878Srdivacky *    0: Success
526202878Srdivacky * Else: Failure
527202878Srdivacky *------------------------------------------------------------------------*/
528218893Sdimusb2_error_t
529218893Sdimusb2_set_config_index(struct usb2_device *udev, uint8_t index)
530218893Sdim{
531218893Sdim	struct usb2_status ds;
532218893Sdim	struct usb2_hub_descriptor hd;
533218893Sdim	struct usb2_config_descriptor *cdp;
534202878Srdivacky	uint16_t power;
535218893Sdim	uint16_t max_power;
536218893Sdim	uint8_t nifc;
537202878Srdivacky	uint8_t selfpowered;
538202878Srdivacky	uint8_t do_unlock;
539195098Sed	usb2_error_t err;
540218893Sdim
541202878Srdivacky	DPRINTFN(6, "udev=%p index=%d\n", udev, index);
542218893Sdim
543202878Srdivacky	/* automatic locking */
544202878Srdivacky	if (sx_xlocked(udev->default_sx + 1)) {
545195098Sed		do_unlock = 0;
546218893Sdim	} else {
547218893Sdim		do_unlock = 1;
548218893Sdim		sx_xlock(udev->default_sx + 1);
549218893Sdim	}
550218893Sdim
551202878Srdivacky	/* detach all interface drivers */
552218893Sdim	usb2_detach_device(udev, USB_IFACE_INDEX_ANY, 1);
553218893Sdim
554202878Srdivacky#if USB_HAVE_UGEN
555195098Sed	/* free all FIFOs except control endpoint FIFOs */
556195098Sed	usb2_fifo_free_wrap(udev, USB_IFACE_INDEX_ANY, 0);
557218893Sdim
558218893Sdim	/* free all configuration data structures */
559218893Sdim	usb2_cdev_free(udev);
560218893Sdim#endif
561218893Sdim	usb2_free_iface_data(udev);
562218893Sdim
563218893Sdim	if (index == USB_UNCONFIG_INDEX) {
564218893Sdim		/*
565218893Sdim		 * Leave unallocated when unconfiguring the
566218893Sdim		 * device. "usb2_free_iface_data()" will also reset
567218893Sdim		 * the current config number and index.
568203954Srdivacky		 */
569203954Srdivacky		err = usb2_req_set_config(udev, NULL, USB_UNCONFIG_NO);
570203954Srdivacky		goto done;
571203954Srdivacky	}
572203954Srdivacky	/* get the full config descriptor */
573203954Srdivacky	err = usb2_req_get_config_desc_full(udev,
574203954Srdivacky	    NULL, &cdp, M_USB, index);
575202878Srdivacky	if (err) {
576202878Srdivacky		goto done;
577202878Srdivacky	}
578202878Srdivacky	/* set the new config descriptor */
579202878Srdivacky
580218893Sdim	udev->cdesc = cdp;
581202878Srdivacky
582202878Srdivacky	if (cdp->bNumInterface > USB_IFACE_MAX) {
583202878Srdivacky		DPRINTFN(0, "too many interfaces: %d\n", cdp->bNumInterface);
584202878Srdivacky		cdp->bNumInterface = USB_IFACE_MAX;
585202878Srdivacky	}
586202878Srdivacky	/* Figure out if the device is self or bus powered. */
587202878Srdivacky	selfpowered = 0;
588202878Srdivacky	if ((!udev->flags.uq_bus_powered) &&
589202878Srdivacky	    (cdp->bmAttributes & UC_SELF_POWERED) &&
590202878Srdivacky	    (udev->flags.usb2_mode == USB_MODE_HOST)) {
591202878Srdivacky		/* May be self powered. */
592202878Srdivacky		if (cdp->bmAttributes & UC_BUS_POWERED) {
593202878Srdivacky			/* Must ask device. */
594195098Sed			if (udev->flags.uq_power_claim) {
595195098Sed				/*
596195098Sed				 * HUB claims to be self powered, but isn't.
597198090Srdivacky				 * It seems that the power status can be
598198090Srdivacky				 * determined by the HUB characteristics.
599198090Srdivacky				 */
600198090Srdivacky				err = usb2_req_get_hub_descriptor
601198090Srdivacky				    (udev, NULL, &hd, 1);
602198090Srdivacky				if (err) {
603198090Srdivacky					DPRINTFN(0, "could not read "
604198090Srdivacky					    "HUB descriptor: %s\n",
605198090Srdivacky					    usb2_errstr(err));
606198090Srdivacky
607198090Srdivacky				} else if (UGETW(hd.wHubCharacteristics) &
608218893Sdim				    UHD_PWR_INDIVIDUAL) {
609198090Srdivacky					selfpowered = 1;
610198090Srdivacky				}
611198090Srdivacky				DPRINTF("characteristics=0x%04x\n",
612198090Srdivacky				    UGETW(hd.wHubCharacteristics));
613195098Sed			} else {
614198090Srdivacky				err = usb2_req_get_device_status
615198090Srdivacky				    (udev, NULL, &ds);
616198090Srdivacky				if (err) {
617198090Srdivacky					DPRINTFN(0, "could not read "
618218893Sdim					    "device status: %s\n",
619198090Srdivacky					    usb2_errstr(err));
620198090Srdivacky				} else if (UGETW(ds.wStatus) & UDS_SELF_POWERED) {
621202878Srdivacky					selfpowered = 1;
622198090Srdivacky				}
623198090Srdivacky				DPRINTF("status=0x%04x \n",
624218893Sdim				    UGETW(ds.wStatus));
625198090Srdivacky			}
626198090Srdivacky		} else
627195098Sed			selfpowered = 1;
628198090Srdivacky	}
629198090Srdivacky	DPRINTF("udev=%p cdesc=%p (addr %d) cno=%d attr=0x%02x, "
630198090Srdivacky	    "selfpowered=%d, power=%d\n",
631198090Srdivacky	    udev, cdp,
632198090Srdivacky	    cdp->bConfigurationValue, udev->address, cdp->bmAttributes,
633195098Sed	    selfpowered, cdp->bMaxPower * 2);
634195098Sed
635198090Srdivacky	/* Check if we have enough power. */
636195098Sed	power = cdp->bMaxPower * 2;
637218893Sdim
638195098Sed	if (udev->parent_hub) {
639202878Srdivacky		max_power = udev->parent_hub->hub->portpower;
640195098Sed	} else {
641195098Sed		max_power = USB_MAX_POWER;
642204642Srdivacky	}
643204642Srdivacky
644204642Srdivacky	if (power > max_power) {
645204642Srdivacky		DPRINTFN(0, "power exceeded %d > %d\n", power, max_power);
646204642Srdivacky		err = USB_ERR_NO_POWER;
647204642Srdivacky		goto done;
648204642Srdivacky	}
649198090Srdivacky	/* Only update "self_powered" in USB Host Mode */
650195098Sed	if (udev->flags.usb2_mode == USB_MODE_HOST) {
651195098Sed		udev->flags.self_powered = selfpowered;
652202878Srdivacky	}
653202878Srdivacky	udev->power = power;
654195098Sed	udev->curr_config_no = cdp->bConfigurationValue;
655195098Sed	udev->curr_config_index = index;
656198090Srdivacky
657203954Srdivacky	/* Set the actual configuration value. */
658203954Srdivacky	err = usb2_req_set_config(udev, NULL, cdp->bConfigurationValue);
659203954Srdivacky	if (err) {
660203954Srdivacky		goto done;
661203954Srdivacky	}
662203954Srdivacky	/* Allocate and fill interface data. */
663198090Srdivacky	nifc = cdp->bNumInterface;
664218893Sdim	while (nifc--) {
665218893Sdim		err = usb2_fill_iface_data(udev, nifc, 0);
666218893Sdim		if (err) {
667218893Sdim			goto done;
668218893Sdim		}
669218893Sdim	}
670218893Sdim#if USB_HAVE_UGEN
671218893Sdim	/* create device nodes for each endpoint */
672218893Sdim	usb2_cdev_create(udev);
673218893Sdim#endif
674218893Sdim
675218893Sdimdone:
676218893Sdim	DPRINTF("error=%s\n", usb2_errstr(err));
677218893Sdim	if (err) {
678218893Sdim#if USB_HAVE_UGEN
679218893Sdim		usb2_cdev_free(udev);
680218893Sdim#endif
681218893Sdim		usb2_free_iface_data(udev);
682218893Sdim	}
683218893Sdim	if (do_unlock) {
684218893Sdim		sx_unlock(udev->default_sx + 1);
685218893Sdim	}
686218893Sdim	return (err);
687218893Sdim}
688218893Sdim
689218893Sdim/*------------------------------------------------------------------------*
690218893Sdim *	usb2_set_alt_interface_index
691218893Sdim *
692218893Sdim * This function will select an alternate interface index for the
693218893Sdim * given interface index. The interface should not be in use when this
694218893Sdim * function is called. That means there should not be any open USB
695218893Sdim * transfers. Else an error is returned. If the alternate setting is
696218893Sdim * already set this function will simply return success. This function
697218893Sdim * is called in Host mode and Device mode!
698218893Sdim *
699218893Sdim * Returns:
700218893Sdim *    0: Success
701218893Sdim * Else: Failure
702218893Sdim *------------------------------------------------------------------------*/
703218893Sdimusb2_error_t
704203954Srdivackyusb2_set_alt_interface_index(struct usb2_device *udev,
705203954Srdivacky    uint8_t iface_index, uint8_t alt_index)
706203954Srdivacky{
707218893Sdim	struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
708218893Sdim	usb2_error_t err;
709218893Sdim	uint8_t do_unlock;
710218893Sdim
711218893Sdim	/* automatic locking */
712218893Sdim	if (sx_xlocked(udev->default_sx + 1)) {
713218893Sdim		do_unlock = 0;
714218893Sdim	} else {
715218893Sdim		do_unlock = 1;
716218893Sdim		sx_xlock(udev->default_sx + 1);
717218893Sdim	}
718218893Sdim	if (iface == NULL) {
719218893Sdim		err = USB_ERR_INVAL;
720218893Sdim		goto done;
721218893Sdim	}
722218893Sdim	if (udev->flags.usb2_mode == USB_MODE_DEVICE) {
723218893Sdim		usb2_detach_device(udev, iface_index, 1);
724218893Sdim	} else {
725218893Sdim		if (iface->alt_index == alt_index) {
726218893Sdim			/*
727218893Sdim			 * Optimise away duplicate setting of
728218893Sdim			 * alternate setting in USB Host Mode!
729218893Sdim			 */
730218893Sdim			err = 0;
731218893Sdim			goto done;
732218893Sdim		}
733218893Sdim	}
734218893Sdim#if USB_HAVE_UGEN
735218893Sdim	/*
736218893Sdim	 * Free all generic FIFOs for this interface, except control
737218893Sdim	 * endpoint FIFOs:
738218893Sdim	 */
739218893Sdim	usb2_fifo_free_wrap(udev, iface_index, 0);
740218893Sdim#endif
741218893Sdim
742218893Sdim	err = usb2_fill_iface_data(udev, iface_index, alt_index);
743218893Sdim	if (err) {
744218893Sdim		goto done;
745218893Sdim	}
746218893Sdim	err = usb2_req_set_alt_interface_no(udev, NULL, iface_index,
747218893Sdim	    iface->idesc->bAlternateSetting);
748218893Sdim
749218893Sdimdone:
750218893Sdim	if (do_unlock) {
751218893Sdim		sx_unlock(udev->default_sx + 1);
752218893Sdim	}
753218893Sdim	return (err);
754218893Sdim}
755218893Sdim
756218893Sdim/*------------------------------------------------------------------------*
757218893Sdim *	usb2_set_endpoint_stall
758218893Sdim *
759218893Sdim * This function is used to make a BULK or INTERRUPT endpoint
760218893Sdim * send STALL tokens.
761218893Sdim *
762218893Sdim * Returns:
763218893Sdim *    0: Success
764218893Sdim * Else: Failure
765218893Sdim *------------------------------------------------------------------------*/
766218893Sdimusb2_error_t
767218893Sdimusb2_set_endpoint_stall(struct usb2_device *udev, struct usb2_pipe *pipe,
768218893Sdim    uint8_t do_stall)
769218893Sdim{
770218893Sdim	struct usb2_xfer *xfer;
771218893Sdim	uint8_t et;
772218893Sdim	uint8_t was_stalled;
773218893Sdim
774218893Sdim	if (pipe == NULL) {
775218893Sdim		/* nothing to do */
776218893Sdim		DPRINTF("Cannot find endpoint\n");
777218893Sdim		/*
778203954Srdivacky		 * Pretend that the clear or set stall request is
779203954Srdivacky		 * successful else some USB host stacks can do
780203954Srdivacky		 * strange things, especially when a control endpoint
781203954Srdivacky		 * stalls.
782203954Srdivacky		 */
783203954Srdivacky		return (0);
784203954Srdivacky	}
785203954Srdivacky	et = (pipe->edesc->bmAttributes & UE_XFERTYPE);
786203954Srdivacky
787203954Srdivacky	if ((et != UE_BULK) &&
788203954Srdivacky	    (et != UE_INTERRUPT)) {
789203954Srdivacky		/*
790203954Srdivacky	         * Should not stall control
791203954Srdivacky	         * nor isochronous endpoints.
792203954Srdivacky	         */
793203954Srdivacky		DPRINTF("Invalid endpoint\n");
794203954Srdivacky		return (0);
795203954Srdivacky	}
796218893Sdim	USB_BUS_LOCK(udev->bus);
797203954Srdivacky
798203954Srdivacky	/* store current stall state */
799203954Srdivacky	was_stalled = pipe->is_stalled;
800203954Srdivacky
801203954Srdivacky	/* check for no change */
802203954Srdivacky	if (was_stalled && do_stall) {
803203954Srdivacky		/* if the pipe is already stalled do nothing */
804218893Sdim		USB_BUS_UNLOCK(udev->bus);
805218893Sdim		DPRINTF("No change\n");
806203954Srdivacky		return (0);
807203954Srdivacky	}
808203954Srdivacky	/* set stalled state */
809203954Srdivacky	pipe->is_stalled = 1;
810203954Srdivacky
811203954Srdivacky	if (do_stall || (!was_stalled)) {
812203954Srdivacky		if (!was_stalled) {
813203954Srdivacky			/* lookup the current USB transfer, if any */
814203954Srdivacky			xfer = pipe->pipe_q.curr;
815203954Srdivacky		} else {
816203954Srdivacky			xfer = NULL;
817203954Srdivacky		}
818203954Srdivacky
819203954Srdivacky		/*
820203954Srdivacky		 * If "xfer" is non-NULL the "set_stall" method will
821203954Srdivacky		 * complete the USB transfer like in case of a timeout
822203954Srdivacky		 * setting the error code "USB_ERR_STALLED".
823203954Srdivacky		 */
824203954Srdivacky		(udev->bus->methods->set_stall) (udev, xfer, pipe);
825218893Sdim	}
826218893Sdim	if (!do_stall) {
827218893Sdim		pipe->toggle_next = 0;	/* reset data toggle */
828218893Sdim		pipe->is_stalled = 0;	/* clear stalled state */
829218893Sdim
830218893Sdim		(udev->bus->methods->clear_stall) (udev, pipe);
831198090Srdivacky
832203954Srdivacky		/* start up the current or next transfer, if any */
833203954Srdivacky		usb2_command_wrapper(&pipe->pipe_q, pipe->pipe_q.curr);
834203954Srdivacky	}
835203954Srdivacky	USB_BUS_UNLOCK(udev->bus);
836203954Srdivacky	return (0);
837218893Sdim}
838218893Sdim
839218893Sdim/*------------------------------------------------------------------------*
840218893Sdim *	usb2_reset_iface_endpoints - used in USB device side mode
841218893Sdim *------------------------------------------------------------------------*/
842218893Sdimusb2_error_t
843218893Sdimusb2_reset_iface_endpoints(struct usb2_device *udev, uint8_t iface_index)
844218893Sdim{
845203954Srdivacky	struct usb2_pipe *pipe;
846203954Srdivacky	struct usb2_pipe *pipe_end;
847203954Srdivacky	usb2_error_t err;
848203954Srdivacky
849203954Srdivacky	pipe = udev->pipes;
850198090Srdivacky	pipe_end = udev->pipes + USB_EP_MAX;
851203954Srdivacky
852203954Srdivacky	for (; pipe != pipe_end; pipe++) {
853198090Srdivacky
854203954Srdivacky		if ((pipe->edesc == NULL) ||
855203954Srdivacky		    (pipe->iface_index != iface_index)) {
856218893Sdim			continue;
857203954Srdivacky		}
858203954Srdivacky		/* simulate a clear stall from the peer */
859195340Sed		err = usb2_set_endpoint_stall(udev, pipe, 0);
860203954Srdivacky		if (err) {
861198090Srdivacky			/* just ignore */
862203954Srdivacky		}
863218893Sdim	}
864203954Srdivacky	return (0);
865218893Sdim}
866218893Sdim
867218893Sdim/*------------------------------------------------------------------------*
868203954Srdivacky *	usb2_detach_device_sub
869203954Srdivacky *
870203954Srdivacky * This function will try to detach an USB device. If it fails a panic
871203954Srdivacky * will result.
872203954Srdivacky *------------------------------------------------------------------------*/
873208599Srdivackystatic void
874206083Srdivackyusb2_detach_device_sub(struct usb2_device *udev, device_t *ppdev,
875208599Srdivacky    uint8_t free_subdev)
876208599Srdivacky{
877208599Srdivacky	device_t dev;
878206083Srdivacky	int err;
879203954Srdivacky
880206274Srdivacky	if (!free_subdev) {
881203954Srdivacky
882203954Srdivacky		*ppdev = NULL;
883202878Srdivacky
884195098Sed	} else if (*ppdev) {
885195098Sed
886218893Sdim		/*
887206274Srdivacky		 * NOTE: It is important to clear "*ppdev" before deleting
888206274Srdivacky		 * the child due to some device methods being called late
889206274Srdivacky		 * during the delete process !
890206274Srdivacky		 */
891206274Srdivacky		dev = *ppdev;
892206274Srdivacky		*ppdev = NULL;
893206274Srdivacky
894206274Srdivacky		device_printf(dev, "at %s, port %d, addr %d "
895206274Srdivacky		    "(disconnected)\n",
896195098Sed		    device_get_nameunit(udev->parent_dev),
897218893Sdim		    udev->port_no, udev->address);
898218893Sdim
899218893Sdim		if (device_is_attached(dev)) {
900195098Sed			if (udev->flags.suspended) {
901203954Srdivacky				err = DEVICE_RESUME(dev);
902202878Srdivacky				if (err) {
903202878Srdivacky					device_printf(dev, "Resume failed!\n");
904218893Sdim				}
905218893Sdim			}
906218893Sdim			if (device_detach(dev)) {
907218893Sdim				goto error;
908218893Sdim			}
909195098Sed		}
910		if (device_delete_child(udev->parent_dev, dev)) {
911			goto error;
912		}
913	}
914	return;
915
916error:
917	/* Detach is not allowed to fail in the USB world */
918	panic("An USB driver would not detach!\n");
919}
920
921/*------------------------------------------------------------------------*
922 *	usb2_detach_device
923 *
924 * The following function will detach the matching interfaces.
925 * This function is NULL safe.
926 *------------------------------------------------------------------------*/
927void
928usb2_detach_device(struct usb2_device *udev, uint8_t iface_index,
929    uint8_t free_subdev)
930{
931	struct usb2_interface *iface;
932	uint8_t i;
933	uint8_t do_unlock;
934
935	if (udev == NULL) {
936		/* nothing to do */
937		return;
938	}
939	DPRINTFN(4, "udev=%p\n", udev);
940
941	/* automatic locking */
942	if (sx_xlocked(udev->default_sx + 1)) {
943		do_unlock = 0;
944	} else {
945		do_unlock = 1;
946		sx_xlock(udev->default_sx + 1);
947	}
948
949	/*
950	 * First detach the child to give the child's detach routine a
951	 * chance to detach the sub-devices in the correct order.
952	 * Then delete the child using "device_delete_child()" which
953	 * will detach all sub-devices from the bottom and upwards!
954	 */
955	if (iface_index != USB_IFACE_INDEX_ANY) {
956		i = iface_index;
957		iface_index = i + 1;
958	} else {
959		i = 0;
960		iface_index = USB_IFACE_MAX;
961	}
962
963	/* do the detach */
964
965	for (; i != iface_index; i++) {
966
967		iface = usb2_get_iface(udev, i);
968		if (iface == NULL) {
969			/* looks like the end of the USB interfaces */
970			break;
971		}
972		usb2_detach_device_sub(udev, &iface->subdev, free_subdev);
973	}
974
975	if (do_unlock) {
976		sx_unlock(udev->default_sx + 1);
977	}
978}
979
980/*------------------------------------------------------------------------*
981 *	usb2_probe_and_attach_sub
982 *
983 * Returns:
984 *    0: Success
985 * Else: Failure
986 *------------------------------------------------------------------------*/
987static uint8_t
988usb2_probe_and_attach_sub(struct usb2_device *udev,
989    struct usb2_attach_arg *uaa)
990{
991	struct usb2_interface *iface;
992	device_t dev;
993	int err;
994
995	iface = uaa->iface;
996	if (iface->parent_iface_index != USB_IFACE_INDEX_ANY) {
997		/* leave interface alone */
998		return (0);
999	}
1000	dev = iface->subdev;
1001	if (dev) {
1002
1003		/* clean up after module unload */
1004
1005		if (device_is_attached(dev)) {
1006			/* already a device there */
1007			return (0);
1008		}
1009		/* clear "iface->subdev" as early as possible */
1010
1011		iface->subdev = NULL;
1012
1013		if (device_delete_child(udev->parent_dev, dev)) {
1014
1015			/*
1016			 * Panic here, else one can get a double call
1017			 * to device_detach().  USB devices should
1018			 * never fail on detach!
1019			 */
1020			panic("device_delete_child() failed!\n");
1021		}
1022	}
1023	if (uaa->temp_dev == NULL) {
1024
1025		/* create a new child */
1026		uaa->temp_dev = device_add_child(udev->parent_dev, NULL, -1);
1027		if (uaa->temp_dev == NULL) {
1028			device_printf(udev->parent_dev,
1029			    "Device creation failed!\n");
1030			return (1);	/* failure */
1031		}
1032		device_set_ivars(uaa->temp_dev, uaa);
1033		device_quiet(uaa->temp_dev);
1034	}
1035	/*
1036	 * Set "subdev" before probe and attach so that "devd" gets
1037	 * the information it needs.
1038	 */
1039	iface->subdev = uaa->temp_dev;
1040
1041	if (device_probe_and_attach(iface->subdev) == 0) {
1042		/*
1043		 * The USB attach arguments are only available during probe
1044		 * and attach !
1045		 */
1046		uaa->temp_dev = NULL;
1047		device_set_ivars(iface->subdev, NULL);
1048
1049		if (udev->flags.suspended) {
1050			err = DEVICE_SUSPEND(iface->subdev);
1051			device_printf(iface->subdev, "Suspend failed\n");
1052		}
1053		return (0);		/* success */
1054	} else {
1055		/* No USB driver found */
1056		iface->subdev = NULL;
1057	}
1058	return (1);			/* failure */
1059}
1060
1061/*------------------------------------------------------------------------*
1062 *	usb2_set_parent_iface
1063 *
1064 * Using this function will lock the alternate interface setting on an
1065 * interface. It is typically used for multi interface drivers. In USB
1066 * device side mode it is assumed that the alternate interfaces all
1067 * have the same endpoint descriptors. The default parent index value
1068 * is "USB_IFACE_INDEX_ANY". Then the alternate setting value is not
1069 * locked.
1070 *------------------------------------------------------------------------*/
1071void
1072usb2_set_parent_iface(struct usb2_device *udev, uint8_t iface_index,
1073    uint8_t parent_index)
1074{
1075	struct usb2_interface *iface;
1076
1077	iface = usb2_get_iface(udev, iface_index);
1078	if (iface) {
1079		iface->parent_iface_index = parent_index;
1080	}
1081}
1082
1083static void
1084usb2_init_attach_arg(struct usb2_device *udev,
1085    struct usb2_attach_arg *uaa)
1086{
1087	bzero(uaa, sizeof(*uaa));
1088
1089	uaa->device = udev;
1090	uaa->usb2_mode = udev->flags.usb2_mode;
1091	uaa->port = udev->port_no;
1092
1093	uaa->info.idVendor = UGETW(udev->ddesc.idVendor);
1094	uaa->info.idProduct = UGETW(udev->ddesc.idProduct);
1095	uaa->info.bcdDevice = UGETW(udev->ddesc.bcdDevice);
1096	uaa->info.bDeviceClass = udev->ddesc.bDeviceClass;
1097	uaa->info.bDeviceSubClass = udev->ddesc.bDeviceSubClass;
1098	uaa->info.bDeviceProtocol = udev->ddesc.bDeviceProtocol;
1099	uaa->info.bConfigIndex = udev->curr_config_index;
1100	uaa->info.bConfigNum = udev->curr_config_no;
1101}
1102
1103/*------------------------------------------------------------------------*
1104 *	usb2_probe_and_attach
1105 *
1106 * This function is called from "uhub_explore_sub()",
1107 * "usb2_handle_set_config()" and "usb2_handle_request()".
1108 *
1109 * Returns:
1110 *    0: Success
1111 * Else: A control transfer failed
1112 *------------------------------------------------------------------------*/
1113usb2_error_t
1114usb2_probe_and_attach(struct usb2_device *udev, uint8_t iface_index)
1115{
1116	struct usb2_attach_arg uaa;
1117	struct usb2_interface *iface;
1118	uint8_t i;
1119	uint8_t j;
1120	uint8_t do_unlock;
1121
1122	if (udev == NULL) {
1123		DPRINTF("udev == NULL\n");
1124		return (USB_ERR_INVAL);
1125	}
1126	/* automatic locking */
1127	if (sx_xlocked(udev->default_sx + 1)) {
1128		do_unlock = 0;
1129	} else {
1130		do_unlock = 1;
1131		sx_xlock(udev->default_sx + 1);
1132	}
1133
1134	if (udev->curr_config_index == USB_UNCONFIG_INDEX) {
1135		/* do nothing - no configuration has been set */
1136		goto done;
1137	}
1138	/* setup USB attach arguments */
1139
1140	usb2_init_attach_arg(udev, &uaa);
1141
1142	/* Check if only one interface should be probed: */
1143	if (iface_index != USB_IFACE_INDEX_ANY) {
1144		i = iface_index;
1145		j = i + 1;
1146	} else {
1147		i = 0;
1148		j = USB_IFACE_MAX;
1149	}
1150
1151	/* Do the probe and attach */
1152	for (; i != j; i++) {
1153
1154		iface = usb2_get_iface(udev, i);
1155		if (iface == NULL) {
1156			/*
1157			 * Looks like the end of the USB
1158			 * interfaces !
1159			 */
1160			DPRINTFN(2, "end of interfaces "
1161			    "at %u\n", i);
1162			break;
1163		}
1164		if (iface->idesc == NULL) {
1165			/* no interface descriptor */
1166			continue;
1167		}
1168		uaa.iface = iface;
1169
1170		uaa.info.bInterfaceClass =
1171		    iface->idesc->bInterfaceClass;
1172		uaa.info.bInterfaceSubClass =
1173		    iface->idesc->bInterfaceSubClass;
1174		uaa.info.bInterfaceProtocol =
1175		    iface->idesc->bInterfaceProtocol;
1176		uaa.info.bIfaceIndex = i;
1177		uaa.info.bIfaceNum =
1178		    iface->idesc->bInterfaceNumber;
1179		uaa.use_generic = 0;
1180
1181		DPRINTFN(2, "iclass=%u/%u/%u iindex=%u/%u\n",
1182		    uaa.info.bInterfaceClass,
1183		    uaa.info.bInterfaceSubClass,
1184		    uaa.info.bInterfaceProtocol,
1185		    uaa.info.bIfaceIndex,
1186		    uaa.info.bIfaceNum);
1187
1188		/* try specific interface drivers first */
1189
1190		if (usb2_probe_and_attach_sub(udev, &uaa)) {
1191			/* ignore */
1192		}
1193		/* try generic interface drivers last */
1194
1195		uaa.use_generic = 1;
1196
1197		if (usb2_probe_and_attach_sub(udev, &uaa)) {
1198			/* ignore */
1199		}
1200	}
1201
1202	if (uaa.temp_dev) {
1203		/* remove the last created child; it is unused */
1204
1205		if (device_delete_child(udev->parent_dev, uaa.temp_dev)) {
1206			DPRINTFN(0, "device delete child failed!\n");
1207		}
1208	}
1209done:
1210	if (do_unlock) {
1211		sx_unlock(udev->default_sx + 1);
1212	}
1213	return (0);
1214}
1215
1216/*------------------------------------------------------------------------*
1217 *	usb2_suspend_resume_sub
1218 *
1219 * This function is called when the suspend or resume methods should
1220 * be executed on an USB device.
1221 *------------------------------------------------------------------------*/
1222static void
1223usb2_suspend_resume_sub(struct usb2_device *udev, device_t dev, uint8_t do_suspend)
1224{
1225	int err;
1226
1227	if (dev == NULL) {
1228		return;
1229	}
1230	if (!device_is_attached(dev)) {
1231		return;
1232	}
1233	if (do_suspend) {
1234		err = DEVICE_SUSPEND(dev);
1235	} else {
1236		err = DEVICE_RESUME(dev);
1237	}
1238	if (err) {
1239		device_printf(dev, "%s failed!\n",
1240		    do_suspend ? "Suspend" : "Resume");
1241	}
1242}
1243
1244/*------------------------------------------------------------------------*
1245 *	usb2_suspend_resume
1246 *
1247 * The following function will suspend or resume the USB device.
1248 *
1249 * Returns:
1250 *    0: Success
1251 * Else: Failure
1252 *------------------------------------------------------------------------*/
1253usb2_error_t
1254usb2_suspend_resume(struct usb2_device *udev, uint8_t do_suspend)
1255{
1256	struct usb2_interface *iface;
1257	uint8_t i;
1258
1259	if (udev == NULL) {
1260		/* nothing to do */
1261		return (0);
1262	}
1263	DPRINTFN(4, "udev=%p do_suspend=%d\n", udev, do_suspend);
1264
1265	sx_assert(udev->default_sx + 1, SA_LOCKED);
1266
1267	USB_BUS_LOCK(udev->bus);
1268	/* filter the suspend events */
1269	if (udev->flags.suspended == do_suspend) {
1270		USB_BUS_UNLOCK(udev->bus);
1271		/* nothing to do */
1272		return (0);
1273	}
1274	udev->flags.suspended = do_suspend;
1275	USB_BUS_UNLOCK(udev->bus);
1276
1277	/* do the suspend or resume */
1278
1279	for (i = 0; i != USB_IFACE_MAX; i++) {
1280
1281		iface = usb2_get_iface(udev, i);
1282		if (iface == NULL) {
1283			/* looks like the end of the USB interfaces */
1284			break;
1285		}
1286		usb2_suspend_resume_sub(udev, iface->subdev, do_suspend);
1287	}
1288	return (0);
1289}
1290
1291/*------------------------------------------------------------------------*
1292 *      usb2_clear_stall_proc
1293 *
1294 * This function performs generic USB clear stall operations.
1295 *------------------------------------------------------------------------*/
1296static void
1297usb2_clear_stall_proc(struct usb2_proc_msg *_pm)
1298{
1299	struct usb2_clear_stall_msg *pm = (void *)_pm;
1300	struct usb2_device *udev = pm->udev;
1301
1302	/* Change lock */
1303	USB_BUS_UNLOCK(udev->bus);
1304	mtx_lock(udev->default_mtx);
1305
1306	/* Start clear stall callback */
1307	usb2_transfer_start(udev->default_xfer[1]);
1308
1309	/* Change lock */
1310	mtx_unlock(udev->default_mtx);
1311	USB_BUS_LOCK(udev->bus);
1312}
1313
1314/*------------------------------------------------------------------------*
1315 *	usb2_alloc_device
1316 *
1317 * This function allocates a new USB device. This function is called
1318 * when a new device has been put in the powered state, but not yet in
1319 * the addressed state. Get initial descriptor, set the address, get
1320 * full descriptor and get strings.
1321 *
1322 * Return values:
1323 *    0: Failure
1324 * Else: Success
1325 *------------------------------------------------------------------------*/
1326struct usb2_device *
1327usb2_alloc_device(device_t parent_dev, struct usb2_bus *bus,
1328    struct usb2_device *parent_hub, uint8_t depth,
1329    uint8_t port_index, uint8_t port_no, uint8_t speed, uint8_t usb2_mode)
1330{
1331	struct usb2_attach_arg uaa;
1332	struct usb2_device *udev;
1333	struct usb2_device *adev;
1334	struct usb2_device *hub;
1335	uint8_t *scratch_ptr;
1336	uint32_t scratch_size;
1337	usb2_error_t err;
1338	uint8_t device_index;
1339
1340	DPRINTF("parent_dev=%p, bus=%p, parent_hub=%p, depth=%u, "
1341	    "port_index=%u, port_no=%u, speed=%u, usb2_mode=%u\n",
1342	    parent_dev, bus, parent_hub, depth, port_index, port_no,
1343	    speed, usb2_mode);
1344
1345	/*
1346	 * Find an unused device index. In USB Host mode this is the
1347	 * same as the device address.
1348	 *
1349	 * Device index zero is not used and device index 1 should
1350	 * always be the root hub.
1351	 */
1352	for (device_index = USB_ROOT_HUB_ADDR;
1353	    (device_index != bus->devices_max) &&
1354	    (bus->devices[device_index] != NULL);
1355	    device_index++) /* nop */;
1356
1357	if (device_index == bus->devices_max) {
1358		device_printf(bus->bdev,
1359		    "No free USB device index for new device!\n");
1360		return (NULL);
1361	}
1362
1363	if (depth > 0x10) {
1364		device_printf(bus->bdev,
1365		    "Invalid device depth!\n");
1366		return (NULL);
1367	}
1368	udev = malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO);
1369	if (udev == NULL) {
1370		return (NULL);
1371	}
1372	/* initialise our SX-lock */
1373	sx_init(udev->default_sx, "0123456789ABCDEF - USB device SX lock" + depth);
1374
1375	/* initialise our SX-lock */
1376	sx_init(udev->default_sx + 1, "0123456789ABCDEF - USB config SX lock" + depth);
1377
1378	usb2_cv_init(udev->default_cv, "WCTRL");
1379	usb2_cv_init(udev->default_cv + 1, "UGONE");
1380
1381	LIST_INIT(&udev->pd_list);
1382
1383	/* initialise our mutex */
1384	mtx_init(udev->default_mtx, "USB device mutex", NULL, MTX_DEF);
1385
1386	/* initialise generic clear stall */
1387	udev->cs_msg[0].hdr.pm_callback = &usb2_clear_stall_proc;
1388	udev->cs_msg[0].udev = udev;
1389	udev->cs_msg[1].hdr.pm_callback = &usb2_clear_stall_proc;
1390	udev->cs_msg[1].udev = udev;
1391
1392	/* initialise some USB device fields */
1393	udev->parent_hub = parent_hub;
1394	udev->parent_dev = parent_dev;
1395	udev->port_index = port_index;
1396	udev->port_no = port_no;
1397	udev->depth = depth;
1398	udev->bus = bus;
1399	udev->address = USB_START_ADDR;	/* default value */
1400	udev->plugtime = (usb2_ticks_t)ticks;
1401	/*
1402	 * We need to force the power mode to "on" because there are plenty
1403	 * of USB devices out there that do not work very well with
1404	 * automatic suspend and resume!
1405	 */
1406	udev->power_mode = USB_POWER_MODE_ON;
1407	udev->pwr_save.last_xfer_time = ticks;
1408
1409	/* we are not ready yet */
1410	udev->refcount = 1;
1411
1412	/* set up default endpoint descriptor */
1413	udev->default_ep_desc.bLength = sizeof(udev->default_ep_desc);
1414	udev->default_ep_desc.bDescriptorType = UDESC_ENDPOINT;
1415	udev->default_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
1416	udev->default_ep_desc.bmAttributes = UE_CONTROL;
1417	udev->default_ep_desc.wMaxPacketSize[0] = USB_MAX_IPACKET;
1418	udev->default_ep_desc.wMaxPacketSize[1] = 0;
1419	udev->default_ep_desc.bInterval = 0;
1420	udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
1421
1422	udev->speed = speed;
1423	udev->flags.usb2_mode = usb2_mode;
1424
1425	/* speed combination should be checked by the parent HUB */
1426
1427	hub = udev->parent_hub;
1428
1429	/* search for our High Speed USB HUB, if any */
1430
1431	adev = udev;
1432	hub = udev->parent_hub;
1433
1434	while (hub) {
1435		if (hub->speed == USB_SPEED_HIGH) {
1436			udev->hs_hub_addr = hub->address;
1437			udev->hs_port_no = adev->port_no;
1438			break;
1439		}
1440		adev = hub;
1441		hub = hub->parent_hub;
1442	}
1443
1444	/* init the default pipe */
1445	usb2_fill_pipe_data(udev, 0,
1446	    &udev->default_ep_desc,
1447	    &udev->default_pipe);
1448
1449	/* set device index */
1450	udev->device_index = device_index;
1451
1452	/* Create ugen name */
1453	snprintf(udev->ugen_name, sizeof(udev->ugen_name),
1454	    USB_GENERIC_NAME "%u.%u", device_get_unit(bus->bdev),
1455	    device_index);
1456#if USB_HAVE_UGEN
1457	/* Create the control endpoint device */
1458	udev->default_dev = usb2_make_dev(udev, 0, FREAD|FWRITE);
1459
1460	/* Create a link from /dev/ugenX.X to the default endpoint */
1461	make_dev_alias(udev->default_dev, udev->ugen_name);
1462#endif
1463
1464	if (udev->flags.usb2_mode == USB_MODE_HOST) {
1465
1466		err = usb2_req_set_address(udev, NULL, device_index);
1467
1468		/* This is the new USB device address from now on */
1469
1470		udev->address = device_index;
1471
1472		/*
1473		 * We ignore any set-address errors, hence there are
1474		 * buggy USB devices out there that actually receive
1475		 * the SETUP PID, but manage to set the address before
1476		 * the STATUS stage is ACK'ed. If the device responds
1477		 * to the subsequent get-descriptor at the new
1478		 * address, then we know that the set-address command
1479		 * was successful.
1480		 */
1481		if (err) {
1482			DPRINTFN(0, "set address %d failed "
1483			    "(ignored)\n", udev->address);
1484		}
1485		/* allow device time to set new address */
1486		usb2_pause_mtx(NULL,
1487		    USB_MS_TO_TICKS(USB_SET_ADDRESS_SETTLE));
1488	} else {
1489		/* We are not self powered */
1490		udev->flags.self_powered = 0;
1491
1492		/* Set unconfigured state */
1493		udev->curr_config_no = USB_UNCONFIG_NO;
1494		udev->curr_config_index = USB_UNCONFIG_INDEX;
1495
1496		/* Setup USB descriptors */
1497		err = (usb2_temp_setup_by_index_p) (udev, usb2_template);
1498		if (err) {
1499			DPRINTFN(0, "setting up USB template failed maybe the USB "
1500			    "template module has not been loaded\n");
1501			goto done;
1502		}
1503	}
1504
1505	/*
1506	 * Get the first 8 bytes of the device descriptor !
1507	 *
1508	 * NOTE: "usb2_do_request" will check the device descriptor
1509	 * next time we do a request to see if the maximum packet size
1510	 * changed! The 8 first bytes of the device descriptor
1511	 * contains the maximum packet size to use on control endpoint
1512	 * 0. If this value is different from "USB_MAX_IPACKET" a new
1513	 * USB control request will be setup!
1514	 */
1515	err = usb2_req_get_desc(udev, NULL, NULL, &udev->ddesc,
1516	    USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0);
1517	if (err) {
1518		DPRINTFN(0, "getting device descriptor "
1519		    "at addr %d failed!\n", udev->address);
1520		/* XXX try to re-enumerate the device */
1521		err = usb2_req_re_enumerate(udev, NULL);
1522		if (err) {
1523			goto done;
1524		}
1525	}
1526	DPRINTF("adding unit addr=%d, rev=%02x, class=%d, "
1527	    "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1528	    udev->address, UGETW(udev->ddesc.bcdUSB),
1529	    udev->ddesc.bDeviceClass,
1530	    udev->ddesc.bDeviceSubClass,
1531	    udev->ddesc.bDeviceProtocol,
1532	    udev->ddesc.bMaxPacketSize,
1533	    udev->ddesc.bLength,
1534	    udev->speed);
1535
1536	/* get the full device descriptor */
1537	err = usb2_req_get_device_desc(udev, NULL, &udev->ddesc);
1538	if (err) {
1539		DPRINTF("addr=%d, getting full desc failed\n",
1540		    udev->address);
1541		goto done;
1542	}
1543	/*
1544	 * Setup temporary USB attach args so that we can figure out some
1545	 * basic quirks for this device.
1546	 */
1547	usb2_init_attach_arg(udev, &uaa);
1548
1549	if (usb2_test_quirk(&uaa, UQ_BUS_POWERED)) {
1550		udev->flags.uq_bus_powered = 1;
1551	}
1552	if (usb2_test_quirk(&uaa, UQ_POWER_CLAIM)) {
1553		udev->flags.uq_power_claim = 1;
1554	}
1555	if (usb2_test_quirk(&uaa, UQ_NO_STRINGS)) {
1556		udev->flags.no_strings = 1;
1557	}
1558	/*
1559	 * Workaround for buggy USB devices.
1560	 *
1561	 * It appears that some string-less USB chips will crash and
1562	 * disappear if any attempts are made to read any string
1563	 * descriptors.
1564	 *
1565	 * Try to detect such chips by checking the strings in the USB
1566	 * device descriptor. If no strings are present there we
1567	 * simply disable all USB strings.
1568	 */
1569	scratch_ptr = udev->bus->scratch[0].data;
1570	scratch_size = sizeof(udev->bus->scratch[0].data);
1571
1572	if (udev->ddesc.iManufacturer ||
1573	    udev->ddesc.iProduct ||
1574	    udev->ddesc.iSerialNumber) {
1575		/* read out the language ID string */
1576		err = usb2_req_get_string_desc(udev, NULL,
1577		    (char *)scratch_ptr, 4, scratch_size,
1578		    USB_LANGUAGE_TABLE);
1579	} else {
1580		err = USB_ERR_INVAL;
1581	}
1582
1583	if (err || (scratch_ptr[0] < 4)) {
1584		udev->flags.no_strings = 1;
1585	} else {
1586		/* pick the first language as the default */
1587		udev->langid = UGETW(scratch_ptr + 2);
1588	}
1589
1590	/* assume 100mA bus powered for now. Changed when configured. */
1591	udev->power = USB_MIN_POWER;
1592
1593#if USB_HAVE_STRINGS
1594	/* get serial number string */
1595	err = usb2_req_get_string_any
1596	    (udev, NULL, (char *)scratch_ptr,
1597	    scratch_size, udev->ddesc.iSerialNumber);
1598
1599	strlcpy(udev->serial, (char *)scratch_ptr, sizeof(udev->serial));
1600
1601	/* get manufacturer string */
1602	err = usb2_req_get_string_any
1603	    (udev, NULL, (char *)scratch_ptr,
1604	    scratch_size, udev->ddesc.iManufacturer);
1605
1606	strlcpy(udev->manufacturer, (char *)scratch_ptr, sizeof(udev->manufacturer));
1607
1608	/* get product string */
1609	err = usb2_req_get_string_any
1610	    (udev, NULL, (char *)scratch_ptr,
1611	    scratch_size, udev->ddesc.iProduct);
1612
1613	strlcpy(udev->product, (char *)scratch_ptr, sizeof(udev->product));
1614
1615	/* finish up all the strings */
1616	usb2_check_strings(udev);
1617#endif
1618
1619	if (udev->flags.usb2_mode == USB_MODE_HOST) {
1620		uint8_t config_index;
1621		uint8_t config_quirk;
1622		uint8_t set_config_failed = 0;
1623
1624		/*
1625		 * Most USB devices should attach to config index 0 by
1626		 * default
1627		 */
1628		if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_0)) {
1629			config_index = 0;
1630			config_quirk = 1;
1631		} else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_1)) {
1632			config_index = 1;
1633			config_quirk = 1;
1634		} else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_2)) {
1635			config_index = 2;
1636			config_quirk = 1;
1637		} else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_3)) {
1638			config_index = 3;
1639			config_quirk = 1;
1640		} else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_4)) {
1641			config_index = 4;
1642			config_quirk = 1;
1643		} else {
1644			config_index = 0;
1645			config_quirk = 0;
1646		}
1647
1648repeat_set_config:
1649
1650		DPRINTF("setting config %u\n", config_index);
1651
1652		/* get the USB device configured */
1653		sx_xlock(udev->default_sx + 1);
1654		err = usb2_set_config_index(udev, config_index);
1655		sx_unlock(udev->default_sx + 1);
1656		if (err) {
1657			if (udev->ddesc.bNumConfigurations != 0) {
1658				if (!set_config_failed) {
1659					set_config_failed = 1;
1660					/* XXX try to re-enumerate the device */
1661					err = usb2_req_re_enumerate(
1662					    udev, NULL);
1663					if (err == 0)
1664					    goto repeat_set_config;
1665				}
1666				DPRINTFN(0, "Failure selecting "
1667				    "configuration index %u: %s, port %u, "
1668				    "addr %u (ignored)\n",
1669				    config_index, usb2_errstr(err), udev->port_no,
1670				    udev->address);
1671			}
1672			/*
1673			 * Some USB devices do not have any
1674			 * configurations. Ignore any set config
1675			 * failures!
1676			 */
1677			err = 0;
1678		} else if (config_quirk) {
1679			/* user quirk selects configuration index */
1680		} else if ((config_index + 1) < udev->ddesc.bNumConfigurations) {
1681
1682			if ((udev->cdesc->bNumInterface < 2) &&
1683			    (usb2_get_no_endpoints(udev->cdesc) == 0)) {
1684				DPRINTFN(0, "Found no endpoints "
1685				    "(trying next config)!\n");
1686				config_index++;
1687				goto repeat_set_config;
1688			}
1689			if (config_index == 0) {
1690				/*
1691				 * Try to figure out if we have an
1692				 * auto-install disk there:
1693				 */
1694				if (usb2_test_autoinstall(udev, 0, 0) == 0) {
1695					DPRINTFN(0, "Found possible auto-install "
1696					    "disk (trying next config)\n");
1697					config_index++;
1698					goto repeat_set_config;
1699				}
1700			}
1701		} else if (usb2_test_huawei_autoinst_p(udev, &uaa) == 0) {
1702			DPRINTFN(0, "Found Huawei auto-install disk!\n");
1703			err = USB_ERR_STALLED;	/* fake an error */
1704		}
1705	} else {
1706		err = 0;		/* set success */
1707	}
1708
1709	DPRINTF("new dev (addr %d), udev=%p, parent_hub=%p\n",
1710	    udev->address, udev, udev->parent_hub);
1711
1712	/* register our device - we are ready */
1713	usb2_bus_port_set_device(bus, parent_hub ?
1714	    parent_hub->hub->ports + port_index : NULL, udev, device_index);
1715
1716	/* Link and announce the ugen device name */
1717#if USB_HAVE_UGEN
1718	udev->ugen_symlink = usb2_alloc_symlink(udev->ugen_name);
1719#endif
1720	printf("%s: <%s> at %s\n", udev->ugen_name, udev->manufacturer,
1721	    device_get_nameunit(udev->bus->bdev));
1722
1723	usb2_notify_addq("+", udev);
1724done:
1725	if (err) {
1726		/* free device  */
1727		usb2_free_device(udev);
1728		udev = NULL;
1729	}
1730	return (udev);
1731}
1732
1733#if USB_HAVE_UGEN
1734static struct cdev *
1735usb2_make_dev(struct usb2_device *udev, int ep, int mode)
1736{
1737	struct usb2_fs_privdata* pd;
1738	char devname[20];
1739
1740	/* Store information to locate ourselves again later */
1741	pd = malloc(sizeof(struct usb2_fs_privdata), M_USBDEV,
1742	    M_WAITOK | M_ZERO);
1743	pd->bus_index = device_get_unit(udev->bus->bdev);
1744	pd->dev_index = udev->device_index;
1745	pd->ep_addr = ep;
1746	pd->mode = mode;
1747
1748	/* Now, create the device itself */
1749	snprintf(devname, sizeof(devname), "%u.%u.%u",
1750	    pd->bus_index, pd->dev_index, pd->ep_addr);
1751	pd->cdev = make_dev(&usb2_devsw, 0, UID_ROOT,
1752	    GID_OPERATOR, 0600, USB_DEVICE_DIR "/%s", devname);
1753	pd->cdev->si_drv1 = pd;
1754
1755	return (pd->cdev);
1756}
1757
1758static void
1759usb2_cdev_create(struct usb2_device *udev)
1760{
1761	struct usb2_config_descriptor *cd;
1762	struct usb2_endpoint_descriptor *ed;
1763	struct usb2_descriptor *desc;
1764	struct usb2_fs_privdata* pd;
1765	struct cdev *dev;
1766	int inmode, outmode, inmask, outmask, mode;
1767	uint8_t ep;
1768
1769	KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("stale cdev entries"));
1770
1771	DPRINTFN(2, "Creating device nodes\n");
1772
1773	if (usb2_get_mode(udev) == USB_MODE_DEVICE) {
1774		inmode = FWRITE;
1775		outmode = FREAD;
1776	} else {		 /* USB_MODE_HOST */
1777		inmode = FREAD;
1778		outmode = FWRITE;
1779	}
1780
1781	inmask = 0;
1782	outmask = 0;
1783	desc = NULL;
1784
1785	/*
1786	 * Collect all used endpoint numbers instead of just
1787	 * generating 16 static endpoints.
1788	 */
1789	cd = usb2_get_config_descriptor(udev);
1790	while ((desc = usb2_desc_foreach(cd, desc))) {
1791		/* filter out all endpoint descriptors */
1792		if ((desc->bDescriptorType == UDESC_ENDPOINT) &&
1793		    (desc->bLength >= sizeof(*ed))) {
1794			ed = (struct usb2_endpoint_descriptor *)desc;
1795
1796			/* update masks */
1797			ep = ed->bEndpointAddress;
1798			if (UE_GET_DIR(ep)  == UE_DIR_OUT)
1799				outmask |= 1 << UE_GET_ADDR(ep);
1800			else
1801				inmask |= 1 << UE_GET_ADDR(ep);
1802		}
1803	}
1804
1805	/* Create all available endpoints except EP0 */
1806	for (ep = 1; ep < 16; ep++) {
1807		mode = inmask & (1 << ep) ? inmode : 0;
1808		mode |= outmask & (1 << ep) ? outmode : 0;
1809		if (mode == 0)
1810			continue;	/* no IN or OUT endpoint */
1811
1812		dev = usb2_make_dev(udev, ep, mode);
1813		pd = dev->si_drv1;
1814		LIST_INSERT_HEAD(&udev->pd_list, pd, pd_next);
1815	}
1816}
1817
1818static void
1819usb2_cdev_free(struct usb2_device *udev)
1820{
1821	struct usb2_fs_privdata* pd;
1822
1823	DPRINTFN(2, "Freeing device nodes\n");
1824
1825	while ((pd = LIST_FIRST(&udev->pd_list)) != NULL) {
1826		KASSERT(pd->cdev->si_drv1 == pd, ("privdata corrupt"));
1827		KASSERT(pd->ep_addr > 0, ("freeing EP0"));
1828
1829		destroy_dev_sched_cb(pd->cdev, usb2_cdev_cleanup, pd);
1830		pd->cdev = NULL;
1831		LIST_REMOVE(pd, pd_next);
1832	}
1833}
1834
1835static void
1836usb2_cdev_cleanup(void* arg)
1837{
1838	free(arg, M_USBDEV);
1839}
1840#endif
1841
1842/*------------------------------------------------------------------------*
1843 *	usb2_free_device
1844 *
1845 * This function is NULL safe and will free an USB device.
1846 *------------------------------------------------------------------------*/
1847void
1848usb2_free_device(struct usb2_device *udev)
1849{
1850	struct usb2_bus *bus = udev->bus;;
1851
1852	DPRINTFN(4, "udev=%p port=%d\n", udev, udev->port_no);
1853
1854	usb2_notify_addq("-", udev);
1855
1856	printf("%s: <%s> at %s (disconnected)\n", udev->ugen_name,
1857	    udev->manufacturer, device_get_nameunit(bus->bdev));
1858
1859	/* Destroy UGEN symlink, if any */
1860	if (udev->ugen_symlink) {
1861#if USB_HAVE_UGEN
1862		usb2_free_symlink(udev->ugen_symlink);
1863#endif
1864		udev->ugen_symlink = NULL;
1865	}
1866	/*
1867	 * Unregister our device first which will prevent any further
1868	 * references:
1869	 */
1870	usb2_bus_port_set_device(bus, udev->parent_hub ?
1871	    udev->parent_hub->hub->ports + udev->port_index : NULL,
1872	    NULL, USB_ROOT_HUB_ADDR);
1873
1874#if USB_HAVE_UGEN
1875	/* wait for all pending references to go away: */
1876
1877	mtx_lock(&usb2_ref_lock);
1878	udev->refcount--;
1879	while (udev->refcount != 0) {
1880		usb2_cv_wait(udev->default_cv + 1, &usb2_ref_lock);
1881	}
1882	mtx_unlock(&usb2_ref_lock);
1883#endif
1884
1885	if (udev->flags.usb2_mode == USB_MODE_DEVICE) {
1886		/* stop receiving any control transfers (Device Side Mode) */
1887		usb2_transfer_unsetup(udev->default_xfer, USB_DEFAULT_XFER_MAX);
1888	}
1889#if USB_HAVE_UGEN
1890	/* free all FIFOs */
1891	usb2_fifo_free_wrap(udev, USB_IFACE_INDEX_ANY, 1);
1892
1893	/*
1894	 * Free all interface related data and FIFOs, if any.
1895	 */
1896	usb2_cdev_free(udev);
1897#endif
1898	usb2_free_iface_data(udev);
1899#if USB_HAVE_UGEN
1900	destroy_dev_sched_cb(udev->default_dev, usb2_cdev_cleanup,
1901	    udev->default_dev->si_drv1);
1902#endif
1903
1904	/* unsetup any leftover default USB transfers */
1905	usb2_transfer_unsetup(udev->default_xfer, USB_DEFAULT_XFER_MAX);
1906
1907	/* template unsetup, if any */
1908	(usb2_temp_unsetup_p) (udev);
1909
1910	/*
1911	 * Make sure that our clear-stall messages are not queued
1912	 * anywhere:
1913	 */
1914	USB_BUS_LOCK(udev->bus);
1915	usb2_proc_mwait(&udev->bus->non_giant_callback_proc,
1916	    &udev->cs_msg[0], &udev->cs_msg[1]);
1917	USB_BUS_UNLOCK(udev->bus);
1918
1919	sx_destroy(udev->default_sx);
1920	sx_destroy(udev->default_sx + 1);
1921
1922	usb2_cv_destroy(udev->default_cv);
1923	usb2_cv_destroy(udev->default_cv + 1);
1924
1925	mtx_destroy(udev->default_mtx);
1926	KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("leaked cdev entries"));
1927
1928	/* free device */
1929	free(udev, M_USB);
1930}
1931
1932/*------------------------------------------------------------------------*
1933 *	usb2_get_iface
1934 *
1935 * This function is the safe way to get the USB interface structure
1936 * pointer by interface index.
1937 *
1938 * Return values:
1939 *   NULL: Interface not present.
1940 *   Else: Pointer to USB interface structure.
1941 *------------------------------------------------------------------------*/
1942struct usb2_interface *
1943usb2_get_iface(struct usb2_device *udev, uint8_t iface_index)
1944{
1945	struct usb2_interface *iface = udev->ifaces + iface_index;
1946
1947	if ((iface < udev->ifaces) ||
1948	    (iface_index >= USB_IFACE_MAX) ||
1949	    (udev->cdesc == NULL) ||
1950	    (iface_index >= udev->cdesc->bNumInterface)) {
1951		return (NULL);
1952	}
1953	return (iface);
1954}
1955
1956/*------------------------------------------------------------------------*
1957 *	usb2_find_descriptor
1958 *
1959 * This function will lookup the first descriptor that matches the
1960 * criteria given by the arguments "type" and "subtype". Descriptors
1961 * will only be searched within the interface having the index
1962 * "iface_index".  If the "id" argument points to an USB descriptor,
1963 * it will be skipped before the search is started. This allows
1964 * searching for multiple descriptors using the same criteria. Else
1965 * the search is started after the interface descriptor.
1966 *
1967 * Return values:
1968 *   NULL: End of descriptors
1969 *   Else: A descriptor matching the criteria
1970 *------------------------------------------------------------------------*/
1971void   *
1972usb2_find_descriptor(struct usb2_device *udev, void *id, uint8_t iface_index,
1973    uint8_t type, uint8_t type_mask,
1974    uint8_t subtype, uint8_t subtype_mask)
1975{
1976	struct usb2_descriptor *desc;
1977	struct usb2_config_descriptor *cd;
1978	struct usb2_interface *iface;
1979
1980	cd = usb2_get_config_descriptor(udev);
1981	if (cd == NULL) {
1982		return (NULL);
1983	}
1984	if (id == NULL) {
1985		iface = usb2_get_iface(udev, iface_index);
1986		if (iface == NULL) {
1987			return (NULL);
1988		}
1989		id = usb2_get_interface_descriptor(iface);
1990		if (id == NULL) {
1991			return (NULL);
1992		}
1993	}
1994	desc = (void *)id;
1995
1996	while ((desc = usb2_desc_foreach(cd, desc))) {
1997
1998		if (desc->bDescriptorType == UDESC_INTERFACE) {
1999			break;
2000		}
2001		if (((desc->bDescriptorType & type_mask) == type) &&
2002		    ((desc->bDescriptorSubtype & subtype_mask) == subtype)) {
2003			return (desc);
2004		}
2005	}
2006	return (NULL);
2007}
2008
2009/*------------------------------------------------------------------------*
2010 *	usb2_devinfo
2011 *
2012 * This function will dump information from the device descriptor
2013 * belonging to the USB device pointed to by "udev", to the string
2014 * pointed to by "dst_ptr" having a maximum length of "dst_len" bytes
2015 * including the terminating zero.
2016 *------------------------------------------------------------------------*/
2017void
2018usb2_devinfo(struct usb2_device *udev, char *dst_ptr, uint16_t dst_len)
2019{
2020	struct usb2_device_descriptor *udd = &udev->ddesc;
2021	uint16_t bcdDevice;
2022	uint16_t bcdUSB;
2023
2024	bcdUSB = UGETW(udd->bcdUSB);
2025	bcdDevice = UGETW(udd->bcdDevice);
2026
2027	if (udd->bDeviceClass != 0xFF) {
2028		snprintf(dst_ptr, dst_len, "%s %s, class %d/%d, rev %x.%02x/"
2029		    "%x.%02x, addr %d", udev->manufacturer, udev->product,
2030		    udd->bDeviceClass, udd->bDeviceSubClass,
2031		    (bcdUSB >> 8), bcdUSB & 0xFF,
2032		    (bcdDevice >> 8), bcdDevice & 0xFF,
2033		    udev->address);
2034	} else {
2035		snprintf(dst_ptr, dst_len, "%s %s, rev %x.%02x/"
2036		    "%x.%02x, addr %d", udev->manufacturer, udev->product,
2037		    (bcdUSB >> 8), bcdUSB & 0xFF,
2038		    (bcdDevice >> 8), bcdDevice & 0xFF,
2039		    udev->address);
2040	}
2041}
2042
2043#if USB_HAVE_STRINGS
2044#if USB_VERBOSE
2045/*
2046 * Descriptions of of known vendors and devices ("products").
2047 */
2048struct usb_knowndev {
2049	uint16_t vendor;
2050	uint16_t product;
2051	uint32_t flags;
2052	const char *vendorname;
2053	const char *productname;
2054};
2055
2056#define	USB_KNOWNDEV_NOPROD	0x01	/* match on vendor only */
2057
2058#include "usbdevs.h"
2059#include "usbdevs_data.h"
2060#endif					/* USB_VERBOSE */
2061
2062/*------------------------------------------------------------------------*
2063 *	usb2_check_strings
2064 *
2065 * This function checks the manufacturer and product strings and will
2066 * fill in defaults for missing strings.
2067 *------------------------------------------------------------------------*/
2068static void
2069usb2_check_strings(struct usb2_device *udev)
2070{
2071	struct usb2_device_descriptor *udd = &udev->ddesc;
2072	const char *vendor;
2073	const char *product;
2074
2075#if USB_VERBOSE
2076	const struct usb_knowndev *kdp;
2077
2078#endif
2079	uint16_t vendor_id;
2080	uint16_t product_id;
2081
2082	usb2_trim_spaces(udev->manufacturer);
2083	usb2_trim_spaces(udev->product);
2084
2085	if (udev->manufacturer[0]) {
2086		vendor = udev->manufacturer;
2087	} else {
2088		vendor = NULL;
2089	}
2090
2091	if (udev->product[0]) {
2092		product = udev->product;
2093	} else {
2094		product = NULL;
2095	}
2096
2097	vendor_id = UGETW(udd->idVendor);
2098	product_id = UGETW(udd->idProduct);
2099
2100#if USB_VERBOSE
2101	if (vendor == NULL || product == NULL) {
2102
2103		for (kdp = usb_knowndevs;
2104		    kdp->vendorname != NULL;
2105		    kdp++) {
2106			if (kdp->vendor == vendor_id &&
2107			    (kdp->product == product_id ||
2108			    (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
2109				break;
2110		}
2111		if (kdp->vendorname != NULL) {
2112			if (vendor == NULL)
2113				vendor = kdp->vendorname;
2114			if (product == NULL)
2115				product = (kdp->flags & USB_KNOWNDEV_NOPROD) == 0 ?
2116				    kdp->productname : NULL;
2117		}
2118	}
2119#endif
2120	if (vendor && *vendor) {
2121		if (udev->manufacturer != vendor) {
2122			strlcpy(udev->manufacturer, vendor,
2123			    sizeof(udev->manufacturer));
2124		}
2125	} else {
2126		snprintf(udev->manufacturer,
2127		    sizeof(udev->manufacturer), "vendor 0x%04x", vendor_id);
2128	}
2129
2130	if (product && *product) {
2131		if (udev->product != product) {
2132			strlcpy(udev->product, product,
2133			    sizeof(udev->product));
2134		}
2135	} else {
2136		snprintf(udev->product,
2137		    sizeof(udev->product), "product 0x%04x", product_id);
2138	}
2139}
2140#endif
2141
2142/*
2143 * Returns:
2144 * See: USB_MODE_XXX
2145 */
2146uint8_t
2147usb2_get_mode(struct usb2_device *udev)
2148{
2149	return (udev->flags.usb2_mode);
2150}
2151
2152/*
2153 * Returns:
2154 * See: USB_SPEED_XXX
2155 */
2156uint8_t
2157usb2_get_speed(struct usb2_device *udev)
2158{
2159	return (udev->speed);
2160}
2161
2162uint32_t
2163usb2_get_isoc_fps(struct usb2_device *udev)
2164{
2165	;				/* indent fix */
2166	switch (udev->speed) {
2167	case USB_SPEED_LOW:
2168	case USB_SPEED_FULL:
2169		return (1000);
2170	default:
2171		return (8000);
2172	}
2173}
2174
2175struct usb2_device_descriptor *
2176usb2_get_device_descriptor(struct usb2_device *udev)
2177{
2178	if (udev == NULL)
2179		return (NULL);		/* be NULL safe */
2180	return (&udev->ddesc);
2181}
2182
2183struct usb2_config_descriptor *
2184usb2_get_config_descriptor(struct usb2_device *udev)
2185{
2186	if (udev == NULL)
2187		return (NULL);		/* be NULL safe */
2188	return (udev->cdesc);
2189}
2190
2191/*------------------------------------------------------------------------*
2192 *	usb2_test_quirk - test a device for a given quirk
2193 *
2194 * Return values:
2195 * 0: The USB device does not have the given quirk.
2196 * Else: The USB device has the given quirk.
2197 *------------------------------------------------------------------------*/
2198uint8_t
2199usb2_test_quirk(const struct usb2_attach_arg *uaa, uint16_t quirk)
2200{
2201	uint8_t found;
2202
2203	found = (usb2_test_quirk_p) (&uaa->info, quirk);
2204	return (found);
2205}
2206
2207struct usb2_interface_descriptor *
2208usb2_get_interface_descriptor(struct usb2_interface *iface)
2209{
2210	if (iface == NULL)
2211		return (NULL);		/* be NULL safe */
2212	return (iface->idesc);
2213}
2214
2215uint8_t
2216usb2_get_interface_altindex(struct usb2_interface *iface)
2217{
2218	return (iface->alt_index);
2219}
2220
2221uint8_t
2222usb2_get_bus_index(struct usb2_device *udev)
2223{
2224	return ((uint8_t)device_get_unit(udev->bus->bdev));
2225}
2226
2227uint8_t
2228usb2_get_device_index(struct usb2_device *udev)
2229{
2230	return (udev->device_index);
2231}
2232
2233/*------------------------------------------------------------------------*
2234 *	usb2_notify_addq
2235 *
2236 * This function will generate events for dev.
2237 *------------------------------------------------------------------------*/
2238static void
2239usb2_notify_addq(const char *type, struct usb2_device *udev)
2240{
2241	char *data = NULL;
2242	struct malloc_type *mt;
2243
2244	mtx_lock(&malloc_mtx);
2245	mt = malloc_desc2type("bus");	/* XXX M_BUS */
2246	mtx_unlock(&malloc_mtx);
2247	if (mt == NULL)
2248		return;
2249
2250	data = malloc(512, mt, M_NOWAIT);
2251	if (data == NULL)
2252		return;
2253
2254	/* String it all together. */
2255	if (udev->parent_hub) {
2256		snprintf(data, 1024,
2257		    "%s"
2258		    "%s "
2259		    "vendor=0x%04x "
2260		    "product=0x%04x "
2261		    "devclass=0x%02x "
2262		    "devsubclass=0x%02x "
2263		    "sernum=\"%s\" "
2264		    "at "
2265		    "port=%u "
2266		    "on "
2267		    "%s\n",
2268		    type,
2269		    udev->ugen_name,
2270		    UGETW(udev->ddesc.idVendor),
2271		    UGETW(udev->ddesc.idProduct),
2272		    udev->ddesc.bDeviceClass,
2273		    udev->ddesc.bDeviceSubClass,
2274		    udev->serial,
2275		    udev->port_no,
2276		    udev->parent_hub->ugen_name);
2277	} else {
2278		snprintf(data, 1024,
2279		    "%s"
2280		    "%s "
2281		    "vendor=0x%04x "
2282		    "product=0x%04x "
2283		    "devclass=0x%02x "
2284		    "devsubclass=0x%02x "
2285		    "sernum=\"%s\" "
2286		    "at port=%u "
2287		    "on "
2288		    "%s\n",
2289		    type,
2290		    udev->ugen_name,
2291		    UGETW(udev->ddesc.idVendor),
2292		    UGETW(udev->ddesc.idProduct),
2293		    udev->ddesc.bDeviceClass,
2294		    udev->ddesc.bDeviceSubClass,
2295		    udev->serial,
2296		    udev->port_no,
2297		    device_get_nameunit(device_get_parent(udev->bus->bdev)));
2298	}
2299	devctl_queue_data(data);
2300}
2301
2302#if USB_HAVE_UGEN
2303/*------------------------------------------------------------------------*
2304 *	usb2_fifo_free_wrap
2305 *
2306 * This function will free the FIFOs.
2307 *
2308 * Flag values, if "iface_index" is equal to "USB_IFACE_INDEX_ANY".
2309 * 0: Free all FIFOs except generic control endpoints.
2310 * 1: Free all FIFOs.
2311 *
2312 * Flag values, if "iface_index" is not equal to "USB_IFACE_INDEX_ANY".
2313 * Not used.
2314 *------------------------------------------------------------------------*/
2315static void
2316usb2_fifo_free_wrap(struct usb2_device *udev,
2317    uint8_t iface_index, uint8_t flag)
2318{
2319	struct usb2_fifo *f;
2320	uint16_t i;
2321
2322	/*
2323	 * Free any USB FIFOs on the given interface:
2324	 */
2325	for (i = 0; i != USB_FIFO_MAX; i++) {
2326		f = udev->fifo[i];
2327		if (f == NULL) {
2328			continue;
2329		}
2330		/* Check if the interface index matches */
2331		if (iface_index == f->iface_index) {
2332			if (f->methods != &usb2_ugen_methods) {
2333				/*
2334				 * Don't free any non-generic FIFOs in
2335				 * this case.
2336				 */
2337				continue;
2338			}
2339			if ((f->dev_ep_index == 0) &&
2340			    (f->fs_xfer == NULL)) {
2341				/* no need to free this FIFO */
2342				continue;
2343			}
2344		} else if (iface_index == USB_IFACE_INDEX_ANY) {
2345			if ((f->methods == &usb2_ugen_methods) &&
2346			    (f->dev_ep_index == 0) && (flag == 0) &&
2347			    (f->fs_xfer == NULL)) {
2348				/* no need to free this FIFO */
2349				continue;
2350			}
2351		} else {
2352			/* no need to free this FIFO */
2353			continue;
2354		}
2355		/* free this FIFO */
2356		usb2_fifo_free(f);
2357	}
2358}
2359#endif
2360
2361/*------------------------------------------------------------------------*
2362 *	usb2_peer_can_wakeup
2363 *
2364 * Return values:
2365 * 0: Peer cannot do resume signalling.
2366 * Else: Peer can do resume signalling.
2367 *------------------------------------------------------------------------*/
2368uint8_t
2369usb2_peer_can_wakeup(struct usb2_device *udev)
2370{
2371	const struct usb2_config_descriptor *cdp;
2372
2373	cdp = udev->cdesc;
2374	if ((cdp != NULL) && (udev->flags.usb2_mode == USB_MODE_HOST)) {
2375		return (cdp->bmAttributes & UC_REMOTE_WAKEUP);
2376	}
2377	return (0);			/* not supported */
2378}
2379