usb_device.c revision 185290
1/* $FreeBSD: head/sys/dev/usb2/core/usb2_device.c 185290 2008-11-25 08:04:40Z alfred $ */
2/*-
3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <dev/usb2/include/usb2_defs.h>
28#include <dev/usb2/include/usb2_mfunc.h>
29#include <dev/usb2/include/usb2_error.h>
30#include <dev/usb2/include/usb2_standard.h>
31#include <dev/usb2/include/usb2_ioctl.h>
32#include <dev/usb2/include/usb2_devid.h>
33
34#define	USB_DEBUG_VAR usb2_debug
35
36#include <dev/usb2/core/usb2_core.h>
37#include <dev/usb2/core/usb2_debug.h>
38#include <dev/usb2/core/usb2_process.h>
39#include <dev/usb2/core/usb2_device.h>
40#include <dev/usb2/core/usb2_busdma.h>
41#include <dev/usb2/core/usb2_transfer.h>
42#include <dev/usb2/core/usb2_parse.h>
43#include <dev/usb2/core/usb2_request.h>
44#include <dev/usb2/core/usb2_dynamic.h>
45#include <dev/usb2/core/usb2_hub.h>
46#include <dev/usb2/core/usb2_util.h>
47#include <dev/usb2/core/usb2_mbuf.h>
48#include <dev/usb2/core/usb2_dev.h>
49#include <dev/usb2/core/usb2_msctest.h>
50#include <dev/usb2/core/usb2_generic.h>
51
52#include <dev/usb2/quirk/usb2_quirk.h>
53
54#include <dev/usb2/controller/usb2_controller.h>
55#include <dev/usb2/controller/usb2_bus.h>
56
57/* function prototypes */
58
59static void usb2_fill_pipe_data(struct usb2_device *udev, uint8_t iface_index, struct usb2_endpoint_descriptor *edesc, struct usb2_pipe *pipe);
60static void usb2_free_pipe_data(struct usb2_device *udev, uint8_t iface_index, uint8_t iface_mask);
61static void usb2_free_iface_data(struct usb2_device *udev);
62static void usb2_detach_device_sub(struct usb2_device *udev, device_t *ppdev, uint8_t free_subdev);
63static uint8_t usb2_probe_and_attach_sub(struct usb2_device *udev, struct usb2_attach_arg *uaa);
64static void usb2_init_attach_arg(struct usb2_device *udev, struct usb2_attach_arg *uaa);
65static void usb2_suspend_resume_sub(struct usb2_device *udev, device_t dev, uint8_t do_suspend);
66static void usb2_clear_stall_proc(struct usb2_proc_msg *_pm);
67static void usb2_check_strings(struct usb2_device *udev);
68static usb2_error_t usb2_fill_iface_data(struct usb2_device *udev, uint8_t iface_index, uint8_t alt_index);
69static void usb2_notify_addq(const char *type, struct usb2_device *udev);
70static void usb2_fifo_free_wrap(struct usb2_device *udev, uint8_t iface_index, uint8_t flag);
71
72/* static structures */
73
74static const uint8_t usb2_hub_speed_combs[USB_SPEED_MAX][USB_SPEED_MAX] = {
75	/* HUB *//* subdevice */
76	[USB_SPEED_HIGH][USB_SPEED_HIGH] = 1,
77	[USB_SPEED_HIGH][USB_SPEED_FULL] = 1,
78	[USB_SPEED_HIGH][USB_SPEED_LOW] = 1,
79	[USB_SPEED_FULL][USB_SPEED_FULL] = 1,
80	[USB_SPEED_FULL][USB_SPEED_LOW] = 1,
81	[USB_SPEED_LOW][USB_SPEED_LOW] = 1,
82};
83
84/* This variable is global to allow easy access to it: */
85
86int	usb2_template = 0;
87
88SYSCTL_INT(_hw_usb2, OID_AUTO, template, CTLFLAG_RW,
89    &usb2_template, 0, "Selected USB device side template");
90
91
92/*------------------------------------------------------------------------*
93 *	usb2_get_pipe_by_addr
94 *
95 * This function searches for an USB pipe by endpoint address and
96 * direction.
97 *
98 * Returns:
99 * NULL: Failure
100 * Else: Success
101 *------------------------------------------------------------------------*/
102struct usb2_pipe *
103usb2_get_pipe_by_addr(struct usb2_device *udev, uint8_t ea_val)
104{
105	struct usb2_pipe *pipe = udev->pipes;
106	struct usb2_pipe *pipe_end = udev->pipes + USB_EP_MAX;
107	enum {
108		EA_MASK = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR),
109	};
110
111	/*
112	 * According to the USB specification not all bits are used
113	 * for the endpoint address. Keep defined bits only:
114	 */
115	ea_val &= EA_MASK;
116
117	/*
118	 * Iterate accross all the USB pipes searching for a match
119	 * based on the endpoint address:
120	 */
121	for (; pipe != pipe_end; pipe++) {
122
123		if (pipe->edesc == NULL) {
124			continue;
125		}
126		/* do the mask and check the value */
127		if ((pipe->edesc->bEndpointAddress & EA_MASK) == ea_val) {
128			goto found;
129		}
130	}
131
132	/*
133	 * The default pipe is always present and is checked separately:
134	 */
135	if ((udev->default_pipe.edesc) &&
136	    ((udev->default_pipe.edesc->bEndpointAddress & EA_MASK) == ea_val)) {
137		pipe = &udev->default_pipe;
138		goto found;
139	}
140	return (NULL);
141
142found:
143	return (pipe);
144}
145
146/*------------------------------------------------------------------------*
147 *	usb2_get_pipe
148 *
149 * This function searches for an USB pipe based on the information
150 * given by the passed "struct usb2_config" pointer.
151 *
152 * Return values:
153 * NULL: No match.
154 * Else: Pointer to "struct usb2_pipe".
155 *------------------------------------------------------------------------*/
156struct usb2_pipe *
157usb2_get_pipe(struct usb2_device *udev, uint8_t iface_index,
158    const struct usb2_config *setup)
159{
160	struct usb2_pipe *pipe = udev->pipes;
161	struct usb2_pipe *pipe_end = udev->pipes + USB_EP_MAX;
162	uint8_t index = setup->ep_index;
163	uint8_t ea_mask;
164	uint8_t ea_val;
165	uint8_t type_mask;
166	uint8_t type_val;
167
168	DPRINTFN(10, "udev=%p iface_index=%d address=0x%x "
169	    "type=0x%x dir=0x%x index=%d\n",
170	    udev, iface_index, setup->endpoint,
171	    setup->type, setup->direction, setup->ep_index);
172
173	/* setup expected endpoint direction mask and value */
174
175	if (setup->direction == UE_DIR_ANY) {
176		/* match any endpoint direction */
177		ea_mask = 0;
178		ea_val = 0;
179	} else {
180		/* match the given endpoint direction */
181		ea_mask = (UE_DIR_IN | UE_DIR_OUT);
182		ea_val = (setup->direction & (UE_DIR_IN | UE_DIR_OUT));
183	}
184
185	/* setup expected endpoint address */
186
187	if (setup->endpoint == UE_ADDR_ANY) {
188		/* match any endpoint address */
189	} else {
190		/* match the given endpoint address */
191		ea_mask |= UE_ADDR;
192		ea_val |= (setup->endpoint & UE_ADDR);
193	}
194
195	/* setup expected endpoint type */
196
197	if (setup->type == UE_BULK_INTR) {
198		/* this will match BULK and INTERRUPT endpoints */
199		type_mask = 2;
200		type_val = 2;
201	} else if (setup->type == UE_TYPE_ANY) {
202		/* match any endpoint type */
203		type_mask = 0;
204		type_val = 0;
205	} else {
206		/* match the given endpoint type */
207		type_mask = UE_XFERTYPE;
208		type_val = (setup->type & UE_XFERTYPE);
209	}
210
211	/*
212	 * Iterate accross all the USB pipes searching for a match
213	 * based on the endpoint address. Note that we are searching
214	 * the pipes from the beginning of the "udev->pipes" array.
215	 */
216	for (; pipe != pipe_end; pipe++) {
217
218		if ((pipe->edesc == NULL) ||
219		    (pipe->iface_index != iface_index)) {
220			continue;
221		}
222		/* do the masks and check the values */
223
224		if (((pipe->edesc->bEndpointAddress & ea_mask) == ea_val) &&
225		    ((pipe->edesc->bmAttributes & type_mask) == type_val)) {
226			if (!index--) {
227				goto found;
228			}
229		}
230	}
231
232	/*
233	 * Match against default pipe last, so that "any pipe", "any
234	 * address" and "any direction" returns the first pipe of the
235	 * interface. "iface_index" and "direction" is ignored:
236	 */
237	if ((udev->default_pipe.edesc) &&
238	    ((udev->default_pipe.edesc->bEndpointAddress & ea_mask) == ea_val) &&
239	    ((udev->default_pipe.edesc->bmAttributes & type_mask) == type_val) &&
240	    (!index)) {
241		pipe = &udev->default_pipe;
242		goto found;
243	}
244	return (NULL);
245
246found:
247	return (pipe);
248}
249
250/*------------------------------------------------------------------------*
251 *	usb2_interface_count
252 *
253 * This function stores the number of USB interfaces excluding
254 * alternate settings, which the USB config descriptor reports into
255 * the unsigned 8-bit integer pointed to by "count".
256 *
257 * Returns:
258 *    0: Success
259 * Else: Failure
260 *------------------------------------------------------------------------*/
261usb2_error_t
262usb2_interface_count(struct usb2_device *udev, uint8_t *count)
263{
264	if (udev->cdesc == NULL) {
265		*count = 0;
266		return (USB_ERR_NOT_CONFIGURED);
267	}
268	*count = udev->cdesc->bNumInterface;
269	return (USB_ERR_NORMAL_COMPLETION);
270}
271
272
273/*------------------------------------------------------------------------*
274 *	usb2_fill_pipe_data
275 *
276 * This function will initialise the USB pipe structure pointed to by
277 * the "pipe" argument.
278 *------------------------------------------------------------------------*/
279static void
280usb2_fill_pipe_data(struct usb2_device *udev, uint8_t iface_index,
281    struct usb2_endpoint_descriptor *edesc, struct usb2_pipe *pipe)
282{
283	bzero(pipe, sizeof(*pipe));
284
285	(udev->bus->methods->pipe_init) (udev, edesc, pipe);
286
287	if (pipe->methods == NULL) {
288		/* the pipe is invalid: just return */
289		return;
290	}
291	/* initialise USB pipe structure */
292	pipe->edesc = edesc;
293	pipe->iface_index = iface_index;
294	TAILQ_INIT(&pipe->pipe_q.head);
295	pipe->pipe_q.command = &usb2_pipe_start;
296
297	/* clear stall, if any */
298	if (udev->bus->methods->clear_stall) {
299		USB_BUS_LOCK(udev->bus);
300		(udev->bus->methods->clear_stall) (udev, pipe);
301		USB_BUS_UNLOCK(udev->bus);
302	}
303	return;
304}
305
306/*------------------------------------------------------------------------*
307 *	usb2_free_pipe_data
308 *
309 * This function will free USB pipe data for the given interface
310 * index. Hence we do not have any dynamic allocations we simply clear
311 * "pipe->edesc" to indicate that the USB pipe structure can be
312 * reused. The pipes belonging to the given interface should not be in
313 * use when this function is called and no check is performed to
314 * prevent this.
315 *------------------------------------------------------------------------*/
316static void
317usb2_free_pipe_data(struct usb2_device *udev,
318    uint8_t iface_index, uint8_t iface_mask)
319{
320	struct usb2_pipe *pipe = udev->pipes;
321	struct usb2_pipe *pipe_end = udev->pipes + USB_EP_MAX;
322
323	while (pipe != pipe_end) {
324		if ((pipe->iface_index & iface_mask) == iface_index) {
325			/* free pipe */
326			pipe->edesc = NULL;
327		}
328		pipe++;
329	}
330	return;
331}
332
333/*------------------------------------------------------------------------*
334 *	usb2_fill_iface_data
335 *
336 * This function will fill in interface data and allocate USB pipes
337 * for all the endpoints that belong to the given interface. This
338 * function is typically called when setting the configuration or when
339 * setting an alternate interface.
340 *------------------------------------------------------------------------*/
341static usb2_error_t
342usb2_fill_iface_data(struct usb2_device *udev,
343    uint8_t iface_index, uint8_t alt_index)
344{
345	struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
346	struct usb2_pipe *pipe;
347	struct usb2_pipe *pipe_end;
348	struct usb2_interface_descriptor *id;
349	struct usb2_endpoint_descriptor *ed = NULL;
350	struct usb2_descriptor *desc;
351	uint8_t nendpt;
352
353	if (iface == NULL) {
354		return (USB_ERR_INVAL);
355	}
356	DPRINTFN(5, "iface_index=%d alt_index=%d\n",
357	    iface_index, alt_index);
358
359	sx_assert(udev->default_sx + 1, SA_LOCKED);
360
361	pipe = udev->pipes;
362	pipe_end = udev->pipes + USB_EP_MAX;
363
364	/*
365	 * Check if any USB pipes on the given USB interface are in
366	 * use:
367	 */
368	while (pipe != pipe_end) {
369		if ((pipe->edesc != NULL) &&
370		    (pipe->iface_index == iface_index) &&
371		    (pipe->refcount != 0)) {
372			return (USB_ERR_IN_USE);
373		}
374		pipe++;
375	}
376
377	pipe = &udev->pipes[0];
378
379	id = usb2_find_idesc(udev->cdesc, iface_index, alt_index);
380	if (id == NULL) {
381		return (USB_ERR_INVAL);
382	}
383	/*
384	 * Free old pipes after we know that an interface descriptor exists,
385	 * if any.
386	 */
387	usb2_free_pipe_data(udev, iface_index, 0 - 1);
388
389	/* Setup USB interface structure */
390	iface->idesc = id;
391	iface->alt_index = alt_index;
392	iface->parent_iface_index = USB_IFACE_INDEX_ANY;
393
394	nendpt = id->bNumEndpoints;
395	DPRINTFN(5, "found idesc nendpt=%d\n", nendpt);
396
397	desc = (void *)id;
398
399	while (nendpt--) {
400		DPRINTFN(11, "endpt=%d\n", nendpt);
401
402		while ((desc = usb2_desc_foreach(udev->cdesc, desc))) {
403			if ((desc->bDescriptorType == UDESC_ENDPOINT) &&
404			    (desc->bLength >= sizeof(*ed))) {
405				goto found;
406			}
407			if (desc->bDescriptorType == UDESC_INTERFACE) {
408				break;
409			}
410		}
411		goto error;
412
413found:
414		ed = (void *)desc;
415
416		/* find a free pipe */
417		while (pipe != pipe_end) {
418			if (pipe->edesc == NULL) {
419				/* pipe is free */
420				usb2_fill_pipe_data(udev, iface_index, ed, pipe);
421				break;
422			}
423			pipe++;
424		}
425	}
426	return (USB_ERR_NORMAL_COMPLETION);
427
428error:
429	/* passed end, or bad desc */
430	DPRINTFN(0, "%s: bad descriptor(s), addr=%d!\n",
431	    __FUNCTION__, udev->address);
432
433	/* free old pipes if any */
434	usb2_free_pipe_data(udev, iface_index, 0 - 1);
435	return (USB_ERR_INVAL);
436}
437
438/*------------------------------------------------------------------------*
439 *	usb2_free_iface_data
440 *
441 * This function will free all USB interfaces and USB pipes belonging
442 * to an USB device.
443 *------------------------------------------------------------------------*/
444static void
445usb2_free_iface_data(struct usb2_device *udev)
446{
447	struct usb2_interface *iface = udev->ifaces;
448	struct usb2_interface *iface_end = udev->ifaces + USB_IFACE_MAX;
449
450	/* mtx_assert() */
451
452	/* free Linux compat device, if any */
453	if (udev->linux_dev) {
454		usb_linux_free_device(udev->linux_dev);
455		udev->linux_dev = NULL;
456	}
457	/* free all pipes, if any */
458	usb2_free_pipe_data(udev, 0, 0);
459
460	/* free all interfaces, if any */
461	while (iface != iface_end) {
462		iface->idesc = NULL;
463		iface->alt_index = 0;
464		iface->parent_iface_index = USB_IFACE_INDEX_ANY;
465		iface->perm.mode = 0;	/* disable permissions */
466		iface++;
467	}
468
469	/* free "cdesc" after "ifaces", if any */
470	if (udev->cdesc) {
471		free(udev->cdesc, M_USB);
472		udev->cdesc = NULL;
473	}
474	/* set unconfigured state */
475	udev->curr_config_no = USB_UNCONFIG_NO;
476	udev->curr_config_index = USB_UNCONFIG_INDEX;
477	return;
478}
479
480/*------------------------------------------------------------------------*
481 *	usb2_set_config_index
482 *
483 * This function selects configuration by index, independent of the
484 * actual configuration number. This function should not be used by
485 * USB drivers.
486 *
487 * Returns:
488 *    0: Success
489 * Else: Failure
490 *------------------------------------------------------------------------*/
491usb2_error_t
492usb2_set_config_index(struct usb2_device *udev, uint8_t index)
493{
494	struct usb2_status ds;
495	struct usb2_hub_descriptor hd;
496	struct usb2_config_descriptor *cdp;
497	uint16_t power;
498	uint16_t max_power;
499	uint8_t nifc;
500	uint8_t selfpowered;
501	uint8_t do_unlock;
502	usb2_error_t err;
503
504	DPRINTFN(6, "udev=%p index=%d\n", udev, index);
505
506	/* automatic locking */
507	if (sx_xlocked(udev->default_sx + 1)) {
508		do_unlock = 0;
509	} else {
510		do_unlock = 1;
511		sx_xlock(udev->default_sx + 1);
512	}
513
514	/* detach all interface drivers */
515	usb2_detach_device(udev, USB_IFACE_INDEX_ANY, 1);
516
517	/* free all FIFOs except control endpoint FIFOs */
518	usb2_fifo_free_wrap(udev, USB_IFACE_INDEX_ANY, 0);
519
520	/* free all configuration data structures */
521	usb2_free_iface_data(udev);
522
523	if (index == USB_UNCONFIG_INDEX) {
524		/*
525		 * Leave unallocated when unconfiguring the
526		 * device. "usb2_free_iface_data()" will also reset
527		 * the current config number and index.
528		 */
529		err = usb2_req_set_config(udev, &Giant, USB_UNCONFIG_NO);
530		goto done;
531	}
532	/* get the full config descriptor */
533	err = usb2_req_get_config_desc_full(udev,
534	    &Giant, &cdp, M_USB, index);
535	if (err) {
536		goto done;
537	}
538	/* set the new config descriptor */
539
540	udev->cdesc = cdp;
541
542	if (cdp->bNumInterface > USB_IFACE_MAX) {
543		DPRINTFN(0, "too many interfaces: %d\n", cdp->bNumInterface);
544		cdp->bNumInterface = USB_IFACE_MAX;
545	}
546	/* Figure out if the device is self or bus powered. */
547	selfpowered = 0;
548	if ((!udev->flags.uq_bus_powered) &&
549	    (cdp->bmAttributes & UC_SELF_POWERED) &&
550	    (udev->flags.usb2_mode == USB_MODE_HOST)) {
551		/* May be self powered. */
552		if (cdp->bmAttributes & UC_BUS_POWERED) {
553			/* Must ask device. */
554			if (udev->flags.uq_power_claim) {
555				/*
556				 * HUB claims to be self powered, but isn't.
557				 * It seems that the power status can be
558				 * determined by the HUB characteristics.
559				 */
560				err = usb2_req_get_hub_descriptor
561				    (udev, &Giant, &hd, 1);
562				if (err) {
563					DPRINTFN(0, "could not read "
564					    "HUB descriptor: %s\n",
565					    usb2_errstr(err));
566
567				} else if (UGETW(hd.wHubCharacteristics) &
568				    UHD_PWR_INDIVIDUAL) {
569					selfpowered = 1;
570				}
571				DPRINTF("characteristics=0x%04x\n",
572				    UGETW(hd.wHubCharacteristics));
573			} else {
574				err = usb2_req_get_device_status
575				    (udev, &Giant, &ds);
576				if (err) {
577					DPRINTFN(0, "could not read "
578					    "device status: %s\n",
579					    usb2_errstr(err));
580				} else if (UGETW(ds.wStatus) & UDS_SELF_POWERED) {
581					selfpowered = 1;
582				}
583				DPRINTF("status=0x%04x \n",
584				    UGETW(ds.wStatus));
585			}
586		} else
587			selfpowered = 1;
588	}
589	DPRINTF("udev=%p cdesc=%p (addr %d) cno=%d attr=0x%02x, "
590	    "selfpowered=%d, power=%d\n",
591	    udev, cdp,
592	    cdp->bConfigurationValue, udev->address, cdp->bmAttributes,
593	    selfpowered, cdp->bMaxPower * 2);
594
595	/* Check if we have enough power. */
596	power = cdp->bMaxPower * 2;
597
598	if (udev->parent_hub) {
599		max_power = udev->parent_hub->hub->portpower;
600	} else {
601		max_power = USB_MAX_POWER;
602	}
603
604	if (power > max_power) {
605		DPRINTFN(0, "power exceeded %d > %d\n", power, max_power);
606		err = USB_ERR_NO_POWER;
607		goto done;
608	}
609	/* Only update "self_powered" in USB Host Mode */
610	if (udev->flags.usb2_mode == USB_MODE_HOST) {
611		udev->flags.self_powered = selfpowered;
612	}
613	udev->power = power;
614	udev->curr_config_no = cdp->bConfigurationValue;
615	udev->curr_config_index = index;
616
617	/* Set the actual configuration value. */
618	err = usb2_req_set_config(udev, &Giant, cdp->bConfigurationValue);
619	if (err) {
620		goto done;
621	}
622	/* Allocate and fill interface data. */
623	nifc = cdp->bNumInterface;
624	while (nifc--) {
625		err = usb2_fill_iface_data(udev, nifc, 0);
626		if (err) {
627			goto done;
628		}
629	}
630
631done:
632	DPRINTF("error=%s\n", usb2_errstr(err));
633	if (err) {
634		usb2_free_iface_data(udev);
635	}
636	if (do_unlock) {
637		sx_unlock(udev->default_sx + 1);
638	}
639	return (err);
640}
641
642/*------------------------------------------------------------------------*
643 *	usb2_set_alt_interface_index
644 *
645 * This function will select an alternate interface index for the
646 * given interface index. The interface should not be in use when this
647 * function is called. That means there should be no open USB
648 * transfers. Else an error is returned.
649 *
650 * Returns:
651 *    0: Success
652 * Else: Failure
653 *------------------------------------------------------------------------*/
654usb2_error_t
655usb2_set_alt_interface_index(struct usb2_device *udev,
656    uint8_t iface_index, uint8_t alt_index)
657{
658	struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
659	usb2_error_t err;
660	uint8_t do_unlock;
661
662	/* automatic locking */
663	if (sx_xlocked(udev->default_sx + 1)) {
664		do_unlock = 0;
665	} else {
666		do_unlock = 1;
667		sx_xlock(udev->default_sx + 1);
668	}
669	if (iface == NULL) {
670		err = USB_ERR_INVAL;
671		goto done;
672	}
673	if (udev->flags.usb2_mode == USB_MODE_DEVICE) {
674		usb2_detach_device(udev, iface_index, 1);
675	}
676	/*
677	 * Free all generic FIFOs for this interface, except control
678	 * endpoint FIFOs:
679	 */
680	usb2_fifo_free_wrap(udev, iface_index, 0);
681
682	err = usb2_fill_iface_data(udev, iface_index, alt_index);
683	if (err) {
684		goto done;
685	}
686	err = usb2_req_set_alt_interface_no
687	    (udev, &Giant, iface_index,
688	    iface->idesc->bAlternateSetting);
689
690done:
691	if (do_unlock) {
692		sx_unlock(udev->default_sx + 1);
693	}
694	return (err);
695}
696
697/*------------------------------------------------------------------------*
698 *	usb2_set_endpoint_stall
699 *
700 * This function is used to make a BULK or INTERRUPT endpoint
701 * send STALL tokens.
702 *
703 * Returns:
704 *    0: Success
705 * Else: Failure
706 *------------------------------------------------------------------------*/
707usb2_error_t
708usb2_set_endpoint_stall(struct usb2_device *udev, struct usb2_pipe *pipe,
709    uint8_t do_stall)
710{
711	struct usb2_xfer *xfer;
712	uint8_t et;
713	uint8_t was_stalled;
714
715	if (pipe == NULL) {
716		/* nothing to do */
717		DPRINTF("Cannot find endpoint\n");
718		/*
719		 * Pretend that the clear or set stall request is
720		 * successful else some USB host stacks can do
721		 * strange things, especially when a control endpoint
722		 * stalls.
723		 */
724		return (0);
725	}
726	et = (pipe->edesc->bmAttributes & UE_XFERTYPE);
727
728	if ((et != UE_BULK) &&
729	    (et != UE_INTERRUPT)) {
730		/*
731	         * Should not stall control
732	         * nor isochronous endpoints.
733	         */
734		DPRINTF("Invalid endpoint\n");
735		return (0);
736	}
737	USB_BUS_LOCK(udev->bus);
738
739	/* store current stall state */
740	was_stalled = pipe->is_stalled;
741
742	/* check for no change */
743	if (was_stalled && do_stall) {
744		/* if the pipe is already stalled do nothing */
745		USB_BUS_UNLOCK(udev->bus);
746		DPRINTF("No change\n");
747		return (0);
748	}
749	/* set stalled state */
750	pipe->is_stalled = 1;
751
752	if (do_stall || (!was_stalled)) {
753		if (!was_stalled) {
754			/* lookup the current USB transfer, if any */
755			xfer = pipe->pipe_q.curr;
756		} else {
757			xfer = NULL;
758		}
759
760		/*
761		 * If "xfer" is non-NULL the "set_stall" method will
762		 * complete the USB transfer like in case of a timeout
763		 * setting the error code "USB_ERR_STALLED".
764		 */
765		(udev->bus->methods->set_stall) (udev, xfer, pipe);
766	}
767	if (!do_stall) {
768		pipe->toggle_next = 0;	/* reset data toggle */
769		pipe->is_stalled = 0;	/* clear stalled state */
770
771		(udev->bus->methods->clear_stall) (udev, pipe);
772
773		/* start up the current or next transfer, if any */
774		usb2_command_wrapper(&pipe->pipe_q, pipe->pipe_q.curr);
775	}
776	USB_BUS_UNLOCK(udev->bus);
777	return (0);
778}
779
780/*------------------------------------------------------------------------*
781 *	usb2_reset_iface_endpoints - used in USB device side mode
782 *------------------------------------------------------------------------*/
783usb2_error_t
784usb2_reset_iface_endpoints(struct usb2_device *udev, uint8_t iface_index)
785{
786	struct usb2_pipe *pipe;
787	struct usb2_pipe *pipe_end;
788	usb2_error_t err;
789
790	pipe = udev->pipes;
791	pipe_end = udev->pipes + USB_EP_MAX;
792
793	for (; pipe != pipe_end; pipe++) {
794
795		if ((pipe->edesc == NULL) ||
796		    (pipe->iface_index != iface_index)) {
797			continue;
798		}
799		/* simulate a clear stall from the peer */
800		err = usb2_set_endpoint_stall(udev, pipe, 0);
801		if (err) {
802			/* just ignore */
803		}
804	}
805	return (0);
806}
807
808/*------------------------------------------------------------------------*
809 *	usb2_detach_device_sub
810 *
811 * This function will try to detach an USB device. If it fails a panic
812 * will result.
813 *------------------------------------------------------------------------*/
814static void
815usb2_detach_device_sub(struct usb2_device *udev, device_t *ppdev,
816    uint8_t free_subdev)
817{
818	device_t dev;
819	int err;
820
821	if (!free_subdev) {
822
823		*ppdev = NULL;
824
825	} else if (*ppdev) {
826
827		/*
828		 * NOTE: It is important to clear "*ppdev" before deleting
829		 * the child due to some device methods being called late
830		 * during the delete process !
831		 */
832		dev = *ppdev;
833		*ppdev = NULL;
834
835		device_printf(dev, "at %s, port %d, addr %d "
836		    "(disconnected)\n",
837		    device_get_nameunit(udev->parent_dev),
838		    udev->port_no, udev->address);
839
840		if (device_is_attached(dev)) {
841			if (udev->flags.suspended) {
842				err = DEVICE_RESUME(dev);
843				if (err) {
844					device_printf(dev, "Resume failed!\n");
845				}
846			}
847			if (device_detach(dev)) {
848				goto error;
849			}
850		}
851		if (device_delete_child(udev->parent_dev, dev)) {
852			goto error;
853		}
854	}
855	return;
856
857error:
858	/* Detach is not allowed to fail in the USB world */
859	panic("An USB driver would not detach!\n");
860	return;
861}
862
863/*------------------------------------------------------------------------*
864 *	usb2_detach_device
865 *
866 * The following function will detach the matching interfaces.
867 * This function is NULL safe.
868 *------------------------------------------------------------------------*/
869void
870usb2_detach_device(struct usb2_device *udev, uint8_t iface_index,
871    uint8_t free_subdev)
872{
873	struct usb2_interface *iface;
874	uint8_t i;
875	uint8_t do_unlock;
876
877	if (udev == NULL) {
878		/* nothing to do */
879		return;
880	}
881	DPRINTFN(4, "udev=%p\n", udev);
882
883	/* automatic locking */
884	if (sx_xlocked(udev->default_sx + 1)) {
885		do_unlock = 0;
886	} else {
887		do_unlock = 1;
888		sx_xlock(udev->default_sx + 1);
889	}
890
891	/*
892	 * First detach the child to give the child's detach routine a
893	 * chance to detach the sub-devices in the correct order.
894	 * Then delete the child using "device_delete_child()" which
895	 * will detach all sub-devices from the bottom and upwards!
896	 */
897	if (iface_index != USB_IFACE_INDEX_ANY) {
898		i = iface_index;
899		iface_index = i + 1;
900	} else {
901		i = 0;
902		iface_index = USB_IFACE_MAX;
903	}
904
905	/* do the detach */
906
907	for (; i != iface_index; i++) {
908
909		iface = usb2_get_iface(udev, i);
910		if (iface == NULL) {
911			/* looks like the end of the USB interfaces */
912			break;
913		}
914		usb2_detach_device_sub(udev, &iface->subdev, free_subdev);
915	}
916
917	if (do_unlock) {
918		sx_unlock(udev->default_sx + 1);
919	}
920	return;
921}
922
923/*------------------------------------------------------------------------*
924 *	usb2_probe_and_attach_sub
925 *
926 * Returns:
927 *    0: Success
928 * Else: Failure
929 *------------------------------------------------------------------------*/
930static uint8_t
931usb2_probe_and_attach_sub(struct usb2_device *udev,
932    struct usb2_attach_arg *uaa)
933{
934	struct usb2_interface *iface;
935	device_t dev;
936	int err;
937
938	iface = uaa->iface;
939	if (iface->parent_iface_index != USB_IFACE_INDEX_ANY) {
940		/* leave interface alone */
941		return (0);
942	}
943	dev = iface->subdev;
944	if (dev) {
945
946		/* clean up after module unload */
947
948		if (device_is_attached(dev)) {
949			/* already a device there */
950			return (0);
951		}
952		/* clear "iface->subdev" as early as possible */
953
954		iface->subdev = NULL;
955
956		if (device_delete_child(udev->parent_dev, dev)) {
957
958			/*
959			 * Panic here, else one can get a double call
960			 * to device_detach().  USB devices should
961			 * never fail on detach!
962			 */
963			panic("device_delete_child() failed!\n");
964		}
965	}
966	if (uaa->temp_dev == NULL) {
967
968		/* create a new child */
969		uaa->temp_dev = device_add_child(udev->parent_dev, NULL, -1);
970		if (uaa->temp_dev == NULL) {
971			device_printf(udev->parent_dev,
972			    "Device creation failed!\n");
973			return (1);	/* failure */
974		}
975		device_set_ivars(uaa->temp_dev, uaa);
976		device_quiet(uaa->temp_dev);
977	}
978	/*
979	 * Set "subdev" before probe and attach so that "devd" gets
980	 * the information it needs.
981	 */
982	iface->subdev = uaa->temp_dev;
983
984	if (device_probe_and_attach(iface->subdev) == 0) {
985		/*
986		 * The USB attach arguments are only available during probe
987		 * and attach !
988		 */
989		uaa->temp_dev = NULL;
990		device_set_ivars(iface->subdev, NULL);
991
992		if (udev->flags.suspended) {
993			err = DEVICE_SUSPEND(iface->subdev);
994			device_printf(iface->subdev, "Suspend failed\n");
995		}
996		return (0);		/* success */
997	} else {
998		/* No USB driver found */
999		iface->subdev = NULL;
1000	}
1001	return (1);			/* failure */
1002}
1003
1004/*------------------------------------------------------------------------*
1005 *	usb2_set_parent_iface
1006 *
1007 * Using this function will lock the alternate interface setting on an
1008 * interface. It is typically used for multi interface drivers. In USB
1009 * device side mode it is assumed that the alternate interfaces all
1010 * have the same endpoint descriptors. The default parent index value
1011 * is "USB_IFACE_INDEX_ANY". Then the alternate setting value is not
1012 * locked.
1013 *------------------------------------------------------------------------*/
1014void
1015usb2_set_parent_iface(struct usb2_device *udev, uint8_t iface_index,
1016    uint8_t parent_index)
1017{
1018	struct usb2_interface *iface;
1019
1020	iface = usb2_get_iface(udev, iface_index);
1021	if (iface) {
1022		iface->parent_iface_index = parent_index;
1023	}
1024	return;
1025}
1026
1027static void
1028usb2_init_attach_arg(struct usb2_device *udev,
1029    struct usb2_attach_arg *uaa)
1030{
1031	bzero(uaa, sizeof(*uaa));
1032
1033	uaa->device = udev;
1034	uaa->usb2_mode = udev->flags.usb2_mode;
1035	uaa->port = udev->port_no;
1036
1037	uaa->info.idVendor = UGETW(udev->ddesc.idVendor);
1038	uaa->info.idProduct = UGETW(udev->ddesc.idProduct);
1039	uaa->info.bcdDevice = UGETW(udev->ddesc.bcdDevice);
1040	uaa->info.bDeviceClass = udev->ddesc.bDeviceClass;
1041	uaa->info.bDeviceSubClass = udev->ddesc.bDeviceSubClass;
1042	uaa->info.bDeviceProtocol = udev->ddesc.bDeviceProtocol;
1043	uaa->info.bConfigIndex = udev->curr_config_index;
1044	uaa->info.bConfigNum = udev->curr_config_no;
1045
1046	return;
1047}
1048
1049/*------------------------------------------------------------------------*
1050 *	usb2_probe_and_attach
1051 *
1052 * This function is called from "uhub_explore_sub()",
1053 * "usb2_handle_set_config()" and "usb2_handle_request()".
1054 *
1055 * Returns:
1056 *    0: Success
1057 * Else: A control transfer failed
1058 *------------------------------------------------------------------------*/
1059usb2_error_t
1060usb2_probe_and_attach(struct usb2_device *udev, uint8_t iface_index)
1061{
1062	struct usb2_attach_arg uaa;
1063	struct usb2_interface *iface;
1064	uint8_t i;
1065	uint8_t j;
1066	uint8_t do_unlock;
1067
1068	if (udev == NULL) {
1069		DPRINTF("udev == NULL\n");
1070		return (USB_ERR_INVAL);
1071	}
1072	/* automatic locking */
1073	if (sx_xlocked(udev->default_sx + 1)) {
1074		do_unlock = 0;
1075	} else {
1076		do_unlock = 1;
1077		sx_xlock(udev->default_sx + 1);
1078	}
1079
1080	if (udev->curr_config_index == USB_UNCONFIG_INDEX) {
1081		/* do nothing - no configuration has been set */
1082		goto done;
1083	}
1084	/* setup USB attach arguments */
1085
1086	usb2_init_attach_arg(udev, &uaa);
1087
1088	/* Check if only one interface should be probed: */
1089	if (iface_index != USB_IFACE_INDEX_ANY) {
1090		i = iface_index;
1091		j = i + 1;
1092	} else {
1093		i = 0;
1094		j = USB_IFACE_MAX;
1095	}
1096
1097	/* Do the probe and attach */
1098	for (; i != j; i++) {
1099
1100		iface = usb2_get_iface(udev, i);
1101		if (iface == NULL) {
1102			/*
1103			 * Looks like the end of the USB
1104			 * interfaces !
1105			 */
1106			DPRINTFN(2, "end of interfaces "
1107			    "at %u\n", i);
1108			break;
1109		}
1110		if (iface->idesc == NULL) {
1111			/* no interface descriptor */
1112			continue;
1113		}
1114		uaa.iface = iface;
1115
1116		uaa.info.bInterfaceClass =
1117		    iface->idesc->bInterfaceClass;
1118		uaa.info.bInterfaceSubClass =
1119		    iface->idesc->bInterfaceSubClass;
1120		uaa.info.bInterfaceProtocol =
1121		    iface->idesc->bInterfaceProtocol;
1122		uaa.info.bIfaceIndex = i;
1123		uaa.info.bIfaceNum =
1124		    iface->idesc->bInterfaceNumber;
1125		uaa.use_generic = 0;
1126
1127		DPRINTFN(2, "iclass=%u/%u/%u iindex=%u/%u\n",
1128		    uaa.info.bInterfaceClass,
1129		    uaa.info.bInterfaceSubClass,
1130		    uaa.info.bInterfaceProtocol,
1131		    uaa.info.bIfaceIndex,
1132		    uaa.info.bIfaceNum);
1133
1134		/* try specific interface drivers first */
1135
1136		if (usb2_probe_and_attach_sub(udev, &uaa)) {
1137			/* ignore */
1138		}
1139		/* try generic interface drivers last */
1140
1141		uaa.use_generic = 1;
1142
1143		if (usb2_probe_and_attach_sub(udev, &uaa)) {
1144			/* ignore */
1145		}
1146	}
1147
1148	if (uaa.temp_dev) {
1149		/* remove the last created child; it is unused */
1150
1151		if (device_delete_child(udev->parent_dev, uaa.temp_dev)) {
1152			DPRINTFN(0, "device delete child failed!\n");
1153		}
1154	}
1155done:
1156	if (do_unlock) {
1157		sx_unlock(udev->default_sx + 1);
1158	}
1159	return (0);
1160}
1161
1162/*------------------------------------------------------------------------*
1163 *	usb2_suspend_resume_sub
1164 *
1165 * This function is called when the suspend or resume methods should
1166 * be executed on an USB device.
1167 *------------------------------------------------------------------------*/
1168static void
1169usb2_suspend_resume_sub(struct usb2_device *udev, device_t dev, uint8_t do_suspend)
1170{
1171	int err;
1172
1173	if (dev == NULL) {
1174		return;
1175	}
1176	if (!device_is_attached(dev)) {
1177		return;
1178	}
1179	if (do_suspend) {
1180		err = DEVICE_SUSPEND(dev);
1181	} else {
1182		err = DEVICE_RESUME(dev);
1183	}
1184	if (err) {
1185		device_printf(dev, "%s failed!\n",
1186		    do_suspend ? "Suspend" : "Resume");
1187	}
1188	return;
1189}
1190
1191/*------------------------------------------------------------------------*
1192 *	usb2_suspend_resume_device
1193 *
1194 * The following function will suspend or resume the USB device.
1195 *
1196 * Returns:
1197 *    0: Success
1198 * Else: Failure
1199 *------------------------------------------------------------------------*/
1200usb2_error_t
1201usb2_suspend_resume(struct usb2_device *udev, uint8_t do_suspend)
1202{
1203	struct usb2_interface *iface;
1204	uint8_t i;
1205
1206	if (udev == NULL) {
1207		/* nothing to do */
1208		return (0);
1209	}
1210	DPRINTFN(4, "udev=%p do_suspend=%d\n", udev, do_suspend);
1211
1212	sx_assert(udev->default_sx + 1, SA_LOCKED);
1213
1214	USB_BUS_LOCK(udev->bus);
1215	/* filter the suspend events */
1216	if (udev->flags.suspended == do_suspend) {
1217		USB_BUS_UNLOCK(udev->bus);
1218		/* nothing to do */
1219		return (0);
1220	}
1221	udev->flags.suspended = do_suspend;
1222	USB_BUS_UNLOCK(udev->bus);
1223
1224	/* do the suspend or resume */
1225
1226	for (i = 0; i != USB_IFACE_MAX; i++) {
1227
1228		iface = usb2_get_iface(udev, i);
1229		if (iface == NULL) {
1230			/* looks like the end of the USB interfaces */
1231			break;
1232		}
1233		usb2_suspend_resume_sub(udev, iface->subdev, do_suspend);
1234	}
1235	return (0);
1236}
1237
1238/*------------------------------------------------------------------------*
1239 *      usb2_clear_stall_proc
1240 *
1241 * This function performs generic USB clear stall operations.
1242 *------------------------------------------------------------------------*/
1243static void
1244usb2_clear_stall_proc(struct usb2_proc_msg *_pm)
1245{
1246	struct usb2_clear_stall_msg *pm = (void *)_pm;
1247	struct usb2_device *udev = pm->udev;
1248
1249	/* Change lock */
1250	USB_BUS_UNLOCK(udev->bus);
1251	mtx_lock(udev->default_mtx);
1252
1253	/* Start clear stall callback */
1254	usb2_transfer_start(udev->default_xfer[1]);
1255
1256	/* Change lock */
1257	mtx_unlock(udev->default_mtx);
1258	USB_BUS_LOCK(udev->bus);
1259	return;
1260}
1261
1262/*------------------------------------------------------------------------*
1263 *	usb2_alloc_device
1264 *
1265 * This function allocates a new USB device. This function is called
1266 * when a new device has been put in the powered state, but not yet in
1267 * the addressed state. Get initial descriptor, set the address, get
1268 * full descriptor and get strings.
1269 *
1270 * Return values:
1271 *    0: Failure
1272 * Else: Success
1273 *------------------------------------------------------------------------*/
1274struct usb2_device *
1275usb2_alloc_device(device_t parent_dev, struct usb2_bus *bus,
1276    struct usb2_device *parent_hub, uint8_t depth,
1277    uint8_t port_index, uint8_t port_no, uint8_t speed, uint8_t usb2_mode)
1278{
1279	struct usb2_attach_arg uaa;
1280	struct usb2_device *udev;
1281	struct usb2_device *adev;
1282	struct usb2_device *hub;
1283	uint8_t *scratch_ptr;
1284	uint32_t scratch_size;
1285	usb2_error_t err;
1286	uint8_t device_index;
1287
1288	DPRINTF("parent_dev=%p, bus=%p, parent_hub=%p, depth=%u, "
1289	    "port_index=%u, port_no=%u, speed=%u, usb2_mode=%u\n",
1290	    parent_dev, bus, parent_hub, depth, port_index, port_no,
1291	    speed, usb2_mode);
1292
1293	/*
1294	 * Find an unused device index. In USB Host mode this is the
1295	 * same as the device address.
1296	 *
1297	 * NOTE: Index 1 is reserved for the Root HUB.
1298	 */
1299	for (device_index = USB_ROOT_HUB_ADDR; device_index !=
1300	    USB_MAX_DEVICES; device_index++) {
1301		if (bus->devices[device_index] == NULL)
1302			break;
1303	}
1304
1305	if (device_index == USB_MAX_DEVICES) {
1306		device_printf(bus->bdev,
1307		    "No free USB device index for new device!\n");
1308		return (NULL);
1309	}
1310	if (depth > 0x10) {
1311		device_printf(bus->bdev,
1312		    "Invalid device depth!\n");
1313		return (NULL);
1314	}
1315	udev = malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO);
1316	if (udev == NULL) {
1317		return (NULL);
1318	}
1319	/* initialise our SX-lock */
1320	sx_init(udev->default_sx, "0123456789ABCDEF - USB device SX lock" + depth);
1321
1322	/* initialise our SX-lock */
1323	sx_init(udev->default_sx + 1, "0123456789ABCDEF - USB config SX lock" + depth);
1324
1325	usb2_cv_init(udev->default_cv, "WCTRL");
1326	usb2_cv_init(udev->default_cv + 1, "UGONE");
1327
1328	/* initialise our mutex */
1329	mtx_init(udev->default_mtx, "USB device mutex", NULL, MTX_DEF);
1330
1331	/* initialise generic clear stall */
1332	udev->cs_msg[0].hdr.pm_callback = &usb2_clear_stall_proc;
1333	udev->cs_msg[0].udev = udev;
1334	udev->cs_msg[1].hdr.pm_callback = &usb2_clear_stall_proc;
1335	udev->cs_msg[1].udev = udev;
1336
1337	/* initialise some USB device fields */
1338	udev->parent_hub = parent_hub;
1339	udev->parent_dev = parent_dev;
1340	udev->port_index = port_index;
1341	udev->port_no = port_no;
1342	udev->depth = depth;
1343	udev->bus = bus;
1344	udev->address = USB_START_ADDR;	/* default value */
1345	udev->plugtime = (uint32_t)ticks;
1346	udev->power_mode = USB_POWER_MODE_ON;
1347
1348	/* we are not ready yet */
1349	udev->refcount = 1;
1350
1351	/* set up default endpoint descriptor */
1352	udev->default_ep_desc.bLength = sizeof(udev->default_ep_desc);
1353	udev->default_ep_desc.bDescriptorType = UDESC_ENDPOINT;
1354	udev->default_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
1355	udev->default_ep_desc.bmAttributes = UE_CONTROL;
1356	udev->default_ep_desc.wMaxPacketSize[0] = USB_MAX_IPACKET;
1357	udev->default_ep_desc.wMaxPacketSize[1] = 0;
1358	udev->default_ep_desc.bInterval = 0;
1359	udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
1360
1361	udev->speed = speed;
1362	udev->flags.usb2_mode = usb2_mode;
1363
1364	/* check speed combination */
1365
1366	hub = udev->parent_hub;
1367	if (hub) {
1368		if (usb2_hub_speed_combs[hub->speed][speed] == 0) {
1369#if USB_DEBUG
1370			printf("%s: the selected subdevice and HUB speed "
1371			    "combination is not supported %d/%d.\n",
1372			    __FUNCTION__, speed, hub->speed);
1373#endif
1374			/* reject this combination */
1375			err = USB_ERR_INVAL;
1376			goto done;
1377		}
1378	}
1379	/* search for our High Speed USB HUB, if any */
1380
1381	adev = udev;
1382	hub = udev->parent_hub;
1383
1384	while (hub) {
1385		if (hub->speed == USB_SPEED_HIGH) {
1386			udev->hs_hub_addr = hub->address;
1387			udev->hs_port_no = adev->port_no;
1388			break;
1389		}
1390		adev = hub;
1391		hub = hub->parent_hub;
1392	}
1393
1394	/* init the default pipe */
1395	usb2_fill_pipe_data(udev, 0,
1396	    &udev->default_ep_desc,
1397	    &udev->default_pipe);
1398
1399	/* set device index */
1400	udev->device_index = device_index;
1401
1402	if (udev->flags.usb2_mode == USB_MODE_HOST) {
1403
1404		err = usb2_req_set_address(udev, &Giant, device_index);
1405
1406		/* This is the new USB device address from now on */
1407
1408		udev->address = device_index;
1409
1410		/*
1411		 * We ignore any set-address errors, hence there are
1412		 * buggy USB devices out there that actually receive
1413		 * the SETUP PID, but manage to set the address before
1414		 * the STATUS stage is ACK'ed. If the device responds
1415		 * to the subsequent get-descriptor at the new
1416		 * address, then we know that the set-address command
1417		 * was successful.
1418		 */
1419		if (err) {
1420			DPRINTFN(0, "set address %d failed "
1421			    "(ignored)\n", udev->address);
1422		}
1423		/* allow device time to set new address */
1424		usb2_pause_mtx(&Giant, USB_SET_ADDRESS_SETTLE);
1425	} else {
1426		/* We are not self powered */
1427		udev->flags.self_powered = 0;
1428
1429		/* Set unconfigured state */
1430		udev->curr_config_no = USB_UNCONFIG_NO;
1431		udev->curr_config_index = USB_UNCONFIG_INDEX;
1432
1433		/* Setup USB descriptors */
1434		err = (usb2_temp_setup_by_index_p) (udev, usb2_template);
1435		if (err) {
1436			DPRINTFN(0, "setting up USB template failed maybe the USB "
1437			    "template module has not been loaded\n");
1438			goto done;
1439		}
1440	}
1441
1442	/*
1443	 * Get the first 8 bytes of the device descriptor !
1444	 *
1445	 * NOTE: "usb2_do_request" will check the device descriptor
1446	 * next time we do a request to see if the maximum packet size
1447	 * changed! The 8 first bytes of the device descriptor
1448	 * contains the maximum packet size to use on control endpoint
1449	 * 0. If this value is different from "USB_MAX_IPACKET" a new
1450	 * USB control request will be setup!
1451	 */
1452	err = usb2_req_get_desc(udev, &Giant, &udev->ddesc,
1453	    USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0);
1454	if (err) {
1455		DPRINTFN(0, "getting device descriptor "
1456		    "at addr %d failed!\n", udev->address);
1457		goto done;
1458	}
1459	DPRINTF("adding unit addr=%d, rev=%02x, class=%d, "
1460	    "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n",
1461	    udev->address, UGETW(udev->ddesc.bcdUSB),
1462	    udev->ddesc.bDeviceClass,
1463	    udev->ddesc.bDeviceSubClass,
1464	    udev->ddesc.bDeviceProtocol,
1465	    udev->ddesc.bMaxPacketSize,
1466	    udev->ddesc.bLength,
1467	    udev->speed);
1468
1469	/* get the full device descriptor */
1470	err = usb2_req_get_device_desc(udev, &Giant, &udev->ddesc);
1471	if (err) {
1472		DPRINTF("addr=%d, getting full desc failed\n",
1473		    udev->address);
1474		goto done;
1475	}
1476	/*
1477	 * Setup temporary USB attach args so that we can figure out some
1478	 * basic quirks for this device.
1479	 */
1480	usb2_init_attach_arg(udev, &uaa);
1481
1482	if (usb2_test_quirk(&uaa, UQ_BUS_POWERED)) {
1483		udev->flags.uq_bus_powered = 1;
1484	}
1485	if (usb2_test_quirk(&uaa, UQ_POWER_CLAIM)) {
1486		udev->flags.uq_power_claim = 1;
1487	}
1488	if (usb2_test_quirk(&uaa, UQ_NO_STRINGS)) {
1489		udev->flags.no_strings = 1;
1490	}
1491	/*
1492	 * Workaround for buggy USB devices.
1493	 *
1494	 * It appears that some string-less USB chips will crash and
1495	 * disappear if any attempts are made to read any string
1496	 * descriptors.
1497	 *
1498	 * Try to detect such chips by checking the strings in the USB
1499	 * device descriptor. If no strings are present there we
1500	 * simply disable all USB strings.
1501	 */
1502	scratch_ptr = udev->bus->scratch[0].data;
1503	scratch_size = sizeof(udev->bus->scratch[0].data);
1504
1505	if (udev->ddesc.iManufacturer ||
1506	    udev->ddesc.iProduct ||
1507	    udev->ddesc.iSerialNumber) {
1508		/* read out the language ID string */
1509		err = usb2_req_get_string_desc(udev, &Giant,
1510		    (char *)scratch_ptr, 4, scratch_size,
1511		    USB_LANGUAGE_TABLE);
1512	} else {
1513		err = USB_ERR_INVAL;
1514	}
1515
1516	if (err || (scratch_ptr[0] < 4)) {
1517		udev->flags.no_strings = 1;
1518	} else {
1519		/* pick the first language as the default */
1520		udev->langid = UGETW(scratch_ptr + 2);
1521	}
1522
1523	/* assume 100mA bus powered for now. Changed when configured. */
1524	udev->power = USB_MIN_POWER;
1525
1526	/* get serial number string */
1527	err = usb2_req_get_string_any
1528	    (udev, &Giant, (char *)scratch_ptr,
1529	    scratch_size, udev->ddesc.iSerialNumber);
1530
1531	strlcpy(udev->serial, (char *)scratch_ptr, sizeof(udev->serial));
1532
1533	/* get manufacturer string */
1534	err = usb2_req_get_string_any
1535	    (udev, &Giant, (char *)scratch_ptr,
1536	    scratch_size, udev->ddesc.iManufacturer);
1537
1538	strlcpy(udev->manufacturer, (char *)scratch_ptr, sizeof(udev->manufacturer));
1539
1540	/* get product string */
1541	err = usb2_req_get_string_any
1542	    (udev, &Giant, (char *)scratch_ptr,
1543	    scratch_size, udev->ddesc.iProduct);
1544
1545	strlcpy(udev->product, (char *)scratch_ptr, sizeof(udev->product));
1546
1547	/* finish up all the strings */
1548	usb2_check_strings(udev);
1549
1550	if (udev->flags.usb2_mode == USB_MODE_HOST) {
1551		uint8_t config_index;
1552		uint8_t config_quirk;
1553
1554		/*
1555		 * Most USB devices should attach to config index 0 by
1556		 * default
1557		 */
1558		if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_0)) {
1559			config_index = 0;
1560			config_quirk = 1;
1561		} else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_1)) {
1562			config_index = 1;
1563			config_quirk = 1;
1564		} else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_2)) {
1565			config_index = 2;
1566			config_quirk = 1;
1567		} else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_3)) {
1568			config_index = 3;
1569			config_quirk = 1;
1570		} else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_4)) {
1571			config_index = 4;
1572			config_quirk = 1;
1573		} else {
1574			config_index = 0;
1575			config_quirk = 0;
1576		}
1577
1578repeat_set_config:
1579
1580		DPRINTF("setting config %u\n", config_index);
1581
1582		/* get the USB device configured */
1583		sx_xlock(udev->default_sx + 1);
1584		err = usb2_set_config_index(udev, config_index);
1585		sx_unlock(udev->default_sx + 1);
1586		if (err) {
1587			DPRINTFN(0, "Failure selecting "
1588			    "configuration index %u: %s, port %u, addr %u\n",
1589			    config_index, usb2_errstr(err), udev->port_no,
1590			    udev->address);
1591
1592		} else if (config_quirk) {
1593			/* user quirk selects configuration index */
1594		} else if ((config_index + 1) < udev->ddesc.bNumConfigurations) {
1595
1596			if ((udev->cdesc->bNumInterface < 2) &&
1597			    (usb2_get_no_endpoints(udev->cdesc) == 0)) {
1598				DPRINTFN(0, "Found no endpoints "
1599				    "(trying next config)!\n");
1600				config_index++;
1601				goto repeat_set_config;
1602			}
1603			if (config_index == 0) {
1604				/*
1605				 * Try to figure out if we have an
1606				 * auto-install disk there:
1607				 */
1608				if (usb2_test_autoinstall(udev, 0, 0) == 0) {
1609					DPRINTFN(0, "Found possible auto-install "
1610					    "disk (trying next config)\n");
1611					config_index++;
1612					goto repeat_set_config;
1613				}
1614			}
1615		} else if (usb2_test_huawei(udev, &uaa) == 0) {
1616			DPRINTFN(0, "Found Huawei auto-install disk!\n");
1617			err = USB_ERR_STALLED;	/* fake an error */
1618		}
1619	} else {
1620		err = 0;		/* set success */
1621	}
1622
1623	DPRINTF("new dev (addr %d), udev=%p, parent_hub=%p\n",
1624	    udev->address, udev, udev->parent_hub);
1625
1626	/* register our device - we are ready */
1627	usb2_bus_port_set_device(bus, parent_hub ?
1628	    parent_hub->hub->ports + port_index : NULL, udev, device_index);
1629
1630	/* make a symlink for UGEN */
1631	if (snprintf((char *)scratch_ptr, scratch_size,
1632	    USB_DEVICE_NAME "%u.%u.0.0",
1633	    device_get_unit(udev->bus->bdev),
1634	    udev->device_index)) {
1635		/* ignore */
1636	}
1637	udev->ugen_symlink =
1638	    usb2_alloc_symlink((char *)scratch_ptr, "ugen%u.%u",
1639	    device_get_unit(udev->bus->bdev),
1640	    udev->device_index);
1641
1642	printf("ugen%u.%u: <%s> at %s\n",
1643	    device_get_unit(udev->bus->bdev),
1644	    udev->device_index, udev->manufacturer,
1645	    device_get_nameunit(udev->bus->bdev));
1646
1647	usb2_notify_addq("+", udev);
1648done:
1649	if (err) {
1650		/* free device  */
1651		usb2_free_device(udev);
1652		udev = NULL;
1653	}
1654	return (udev);
1655}
1656
1657/*------------------------------------------------------------------------*
1658 *	usb2_free_device
1659 *
1660 * This function is NULL safe and will free an USB device.
1661 *------------------------------------------------------------------------*/
1662void
1663usb2_free_device(struct usb2_device *udev)
1664{
1665	struct usb2_bus *bus;
1666
1667	if (udev == NULL) {
1668		/* already freed */
1669		return;
1670	}
1671	DPRINTFN(4, "udev=%p port=%d\n", udev, udev->port_no);
1672
1673	usb2_notify_addq("-", udev);
1674
1675	bus = udev->bus;
1676
1677	/*
1678	 * Destroy UGEN symlink, if any
1679	 */
1680	if (udev->ugen_symlink) {
1681		usb2_free_symlink(udev->ugen_symlink);
1682		udev->ugen_symlink = NULL;
1683	}
1684	/*
1685	 * Unregister our device first which will prevent any further
1686	 * references:
1687	 */
1688	usb2_bus_port_set_device(bus, udev->parent_hub ?
1689	    udev->parent_hub->hub->ports + udev->port_index : NULL,
1690	    NULL, USB_ROOT_HUB_ADDR);
1691
1692	/* wait for all pending references to go away: */
1693
1694	mtx_lock(&usb2_ref_lock);
1695	udev->refcount--;
1696	while (udev->refcount != 0) {
1697		usb2_cv_wait(udev->default_cv + 1, &usb2_ref_lock);
1698	}
1699	mtx_unlock(&usb2_ref_lock);
1700
1701	if (udev->flags.usb2_mode == USB_MODE_DEVICE) {
1702		/* stop receiving any control transfers (Device Side Mode) */
1703		usb2_transfer_unsetup(udev->default_xfer, USB_DEFAULT_XFER_MAX);
1704	}
1705	/* free all FIFOs */
1706	usb2_fifo_free_wrap(udev, USB_IFACE_INDEX_ANY, 1);
1707
1708	/*
1709	 * Free all interface related data and FIFOs, if any.
1710	 */
1711	usb2_free_iface_data(udev);
1712
1713	/* unsetup any leftover default USB transfers */
1714	usb2_transfer_unsetup(udev->default_xfer, USB_DEFAULT_XFER_MAX);
1715
1716	(usb2_temp_unsetup_p) (udev);
1717
1718	sx_destroy(udev->default_sx);
1719	sx_destroy(udev->default_sx + 1);
1720
1721	usb2_cv_destroy(udev->default_cv);
1722	usb2_cv_destroy(udev->default_cv + 1);
1723
1724	mtx_destroy(udev->default_mtx);
1725
1726	/* free device */
1727	free(udev, M_USB);
1728
1729	return;
1730}
1731
1732/*------------------------------------------------------------------------*
1733 *	usb2_get_iface
1734 *
1735 * This function is the safe way to get the USB interface structure
1736 * pointer by interface index.
1737 *
1738 * Return values:
1739 *   NULL: Interface not present.
1740 *   Else: Pointer to USB interface structure.
1741 *------------------------------------------------------------------------*/
1742struct usb2_interface *
1743usb2_get_iface(struct usb2_device *udev, uint8_t iface_index)
1744{
1745	struct usb2_interface *iface = udev->ifaces + iface_index;
1746
1747	if ((iface < udev->ifaces) ||
1748	    (iface_index >= USB_IFACE_MAX) ||
1749	    (udev->cdesc == NULL) ||
1750	    (iface_index >= udev->cdesc->bNumInterface)) {
1751		return (NULL);
1752	}
1753	return (iface);
1754}
1755
1756/*------------------------------------------------------------------------*
1757 *	usb2_find_descriptor
1758 *
1759 * This function will lookup the first descriptor that matches the
1760 * criteria given by the arguments "type" and "subtype". Descriptors
1761 * will only be searched within the interface having the index
1762 * "iface_index".  If the "id" argument points to an USB descriptor,
1763 * it will be skipped before the search is started. This allows
1764 * searching for multiple descriptors using the same criteria. Else
1765 * the search is started after the interface descriptor.
1766 *
1767 * Return values:
1768 *   NULL: End of descriptors
1769 *   Else: A descriptor matching the criteria
1770 *------------------------------------------------------------------------*/
1771void   *
1772usb2_find_descriptor(struct usb2_device *udev, void *id, uint8_t iface_index,
1773    uint8_t type, uint8_t type_mask,
1774    uint8_t subtype, uint8_t subtype_mask)
1775{
1776	struct usb2_descriptor *desc;
1777	struct usb2_config_descriptor *cd;
1778	struct usb2_interface *iface;
1779
1780	cd = usb2_get_config_descriptor(udev);
1781	if (cd == NULL) {
1782		return (NULL);
1783	}
1784	if (id == NULL) {
1785		iface = usb2_get_iface(udev, iface_index);
1786		if (iface == NULL) {
1787			return (NULL);
1788		}
1789		id = usb2_get_interface_descriptor(iface);
1790		if (id == NULL) {
1791			return (NULL);
1792		}
1793	}
1794	desc = (void *)id;
1795
1796	while ((desc = usb2_desc_foreach(cd, desc))) {
1797
1798		if (desc->bDescriptorType == UDESC_INTERFACE) {
1799			break;
1800		}
1801		if (((desc->bDescriptorType & type_mask) == type) &&
1802		    ((desc->bDescriptorSubtype & subtype_mask) == subtype)) {
1803			return (desc);
1804		}
1805	}
1806	return (NULL);
1807}
1808
1809/*------------------------------------------------------------------------*
1810 *	usb2_devinfo
1811 *
1812 * This function will dump information from the device descriptor
1813 * belonging to the USB device pointed to by "udev", to the string
1814 * pointed to by "dst_ptr" having a maximum length of "dst_len" bytes
1815 * including the terminating zero.
1816 *------------------------------------------------------------------------*/
1817void
1818usb2_devinfo(struct usb2_device *udev, char *dst_ptr, uint16_t dst_len)
1819{
1820	struct usb2_device_descriptor *udd = &udev->ddesc;
1821	uint16_t bcdDevice;
1822	uint16_t bcdUSB;
1823
1824	bcdUSB = UGETW(udd->bcdUSB);
1825	bcdDevice = UGETW(udd->bcdDevice);
1826
1827	if (udd->bDeviceClass != 0xFF) {
1828		snprintf(dst_ptr, dst_len, "%s %s, class %d/%d, rev %x.%02x/"
1829		    "%x.%02x, addr %d", udev->manufacturer, udev->product,
1830		    udd->bDeviceClass, udd->bDeviceSubClass,
1831		    (bcdUSB >> 8), bcdUSB & 0xFF,
1832		    (bcdDevice >> 8), bcdDevice & 0xFF,
1833		    udev->address);
1834	} else {
1835		snprintf(dst_ptr, dst_len, "%s %s, rev %x.%02x/"
1836		    "%x.%02x, addr %d", udev->manufacturer, udev->product,
1837		    (bcdUSB >> 8), bcdUSB & 0xFF,
1838		    (bcdDevice >> 8), bcdDevice & 0xFF,
1839		    udev->address);
1840	}
1841	return;
1842}
1843
1844#if USB_VERBOSE
1845/*
1846 * Descriptions of of known vendors and devices ("products").
1847 */
1848struct usb_knowndev {
1849	uint16_t vendor;
1850	uint16_t product;
1851	uint32_t flags;
1852	const char *vendorname;
1853	const char *productname;
1854};
1855
1856#define	USB_KNOWNDEV_NOPROD	0x01	/* match on vendor only */
1857
1858#include <dev/usb2/include/usb2_devid.h>
1859#include <dev/usb2/include/usb2_devtable.h>
1860#endif					/* USB_VERBOSE */
1861
1862/*------------------------------------------------------------------------*
1863 *	usb2_check_strings
1864 *
1865 * This function checks the manufacturer and product strings and will
1866 * fill in defaults for missing strings.
1867 *------------------------------------------------------------------------*/
1868static void
1869usb2_check_strings(struct usb2_device *udev)
1870{
1871	struct usb2_device_descriptor *udd = &udev->ddesc;
1872	const char *vendor;
1873	const char *product;
1874
1875#if USB_VERBOSE
1876	const struct usb_knowndev *kdp;
1877
1878#endif
1879	uint16_t vendor_id;
1880	uint16_t product_id;
1881
1882	usb2_trim_spaces(udev->manufacturer);
1883	usb2_trim_spaces(udev->product);
1884
1885	if (udev->manufacturer[0]) {
1886		vendor = udev->manufacturer;
1887	} else {
1888		vendor = NULL;
1889	}
1890
1891	if (udev->product[0]) {
1892		product = udev->product;
1893	} else {
1894		product = NULL;
1895	}
1896
1897	vendor_id = UGETW(udd->idVendor);
1898	product_id = UGETW(udd->idProduct);
1899
1900#if USB_VERBOSE
1901	if (vendor == NULL || product == NULL) {
1902
1903		for (kdp = usb_knowndevs;
1904		    kdp->vendorname != NULL;
1905		    kdp++) {
1906			if (kdp->vendor == vendor_id &&
1907			    (kdp->product == product_id ||
1908			    (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
1909				break;
1910		}
1911		if (kdp->vendorname != NULL) {
1912			if (vendor == NULL)
1913				vendor = kdp->vendorname;
1914			if (product == NULL)
1915				product = (kdp->flags & USB_KNOWNDEV_NOPROD) == 0 ?
1916				    kdp->productname : NULL;
1917		}
1918	}
1919#endif
1920	if (vendor && *vendor) {
1921		if (udev->manufacturer != vendor) {
1922			strlcpy(udev->manufacturer, vendor,
1923			    sizeof(udev->manufacturer));
1924		}
1925	} else {
1926		snprintf(udev->manufacturer,
1927		    sizeof(udev->manufacturer), "vendor 0x%04x", vendor_id);
1928	}
1929
1930	if (product && *product) {
1931		if (udev->product != product) {
1932			strlcpy(udev->product, product,
1933			    sizeof(udev->product));
1934		}
1935	} else {
1936		snprintf(udev->product,
1937		    sizeof(udev->product), "product 0x%04x", product_id);
1938	}
1939	return;
1940}
1941
1942uint8_t
1943usb2_get_speed(struct usb2_device *udev)
1944{
1945	return (udev->speed);
1946}
1947
1948struct usb2_device_descriptor *
1949usb2_get_device_descriptor(struct usb2_device *udev)
1950{
1951	if (udev == NULL)
1952		return (NULL);		/* be NULL safe */
1953	return (&udev->ddesc);
1954}
1955
1956struct usb2_config_descriptor *
1957usb2_get_config_descriptor(struct usb2_device *udev)
1958{
1959	if (udev == NULL)
1960		return (NULL);		/* be NULL safe */
1961	return (udev->cdesc);
1962}
1963
1964/*------------------------------------------------------------------------*
1965 *	usb2_test_quirk - test a device for a given quirk
1966 *
1967 * Return values:
1968 * 0: The USB device does not have the given quirk.
1969 * Else: The USB device has the given quirk.
1970 *------------------------------------------------------------------------*/
1971uint8_t
1972usb2_test_quirk(const struct usb2_attach_arg *uaa, uint16_t quirk)
1973{
1974	uint8_t found;
1975
1976	found = (usb2_test_quirk_p) (&uaa->info, quirk);
1977	return (found);
1978}
1979
1980struct usb2_interface_descriptor *
1981usb2_get_interface_descriptor(struct usb2_interface *iface)
1982{
1983	if (iface == NULL)
1984		return (NULL);		/* be NULL safe */
1985	return (iface->idesc);
1986}
1987
1988uint8_t
1989usb2_get_interface_altindex(struct usb2_interface *iface)
1990{
1991	return (iface->alt_index);
1992}
1993
1994uint8_t
1995usb2_get_bus_index(struct usb2_device *udev)
1996{
1997	return ((uint8_t)device_get_unit(udev->bus->bdev));
1998}
1999
2000uint8_t
2001usb2_get_device_index(struct usb2_device *udev)
2002{
2003	return (udev->device_index);
2004}
2005
2006/*------------------------------------------------------------------------*
2007 *	usb2_notify_addq
2008 *
2009 * This function will generate events for dev.
2010 *------------------------------------------------------------------------*/
2011static void
2012usb2_notify_addq(const char *type, struct usb2_device *udev)
2013{
2014	char *data = NULL;
2015	struct malloc_type *mt;
2016
2017	mtx_lock(&malloc_mtx);
2018	mt = malloc_desc2type("bus");	/* XXX M_BUS */
2019	mtx_unlock(&malloc_mtx);
2020	if (mt == NULL)
2021		return;
2022
2023	data = malloc(512, mt, M_NOWAIT);
2024	if (data == NULL)
2025		return;
2026
2027	/* String it all together. */
2028	if (udev->parent_hub) {
2029		snprintf(data, 1024,
2030		    "%s"
2031		    "ugen%u.%u "
2032		    "vendor=0x%04x "
2033		    "product=0x%04x "
2034		    "devclass=0x%02x "
2035		    "devsubclass=0x%02x "
2036		    "sernum=\"%s\" "
2037		    "at "
2038		    "port=%u "
2039		    "on "
2040		    "ugen%u.%u\n",
2041		    type,
2042		    device_get_unit(udev->bus->bdev),
2043		    udev->device_index,
2044		    UGETW(udev->ddesc.idVendor),
2045		    UGETW(udev->ddesc.idProduct),
2046		    udev->ddesc.bDeviceClass,
2047		    udev->ddesc.bDeviceSubClass,
2048		    udev->serial,
2049		    udev->port_no,
2050		    device_get_unit(udev->bus->bdev),
2051		    udev->parent_hub->device_index);
2052	} else {
2053		snprintf(data, 1024,
2054		    "%s"
2055		    "ugen%u.%u "
2056		    "vendor=0x%04x "
2057		    "product=0x%04x "
2058		    "devclass=0x%02x "
2059		    "devsubclass=0x%02x "
2060		    "sernum=\"%s\" "
2061		    "at port=%u "
2062		    "on "
2063		    "%s\n",
2064		    type,
2065		    device_get_unit(udev->bus->bdev),
2066		    udev->device_index,
2067		    UGETW(udev->ddesc.idVendor),
2068		    UGETW(udev->ddesc.idProduct),
2069		    udev->ddesc.bDeviceClass,
2070		    udev->ddesc.bDeviceSubClass,
2071		    udev->serial,
2072		    udev->port_no,
2073		    device_get_nameunit(device_get_parent(udev->bus->bdev)));
2074	}
2075	devctl_queue_data(data);
2076	return;
2077}
2078
2079/*------------------------------------------------------------------------*
2080 *	usb2_fifo_free_wrap
2081 *
2082 * This function will free the FIFOs.
2083 *
2084 * Flag values, if "iface_index" is equal to "USB_IFACE_INDEX_ANY".
2085 * 0: Free all FIFOs except generic control endpoints.
2086 * 1: Free all FIFOs.
2087 *
2088 * Flag values, if "iface_index" is not equal to "USB_IFACE_INDEX_ANY".
2089 * Not used.
2090 *------------------------------------------------------------------------*/
2091static void
2092usb2_fifo_free_wrap(struct usb2_device *udev,
2093    uint8_t iface_index, uint8_t flag)
2094{
2095	struct usb2_fifo *f;
2096	uint16_t i;
2097
2098	/*
2099	 * Free any USB FIFOs on the given interface:
2100	 */
2101	for (i = 0; i != USB_FIFO_MAX; i++) {
2102		f = udev->fifo[i];
2103		if (f == NULL) {
2104			continue;
2105		}
2106		/* Check if the interface index matches */
2107		if (iface_index == f->iface_index) {
2108			if (f->methods != &usb2_ugen_methods) {
2109				/*
2110				 * Don't free any non-generic FIFOs in
2111				 * this case.
2112				 */
2113				continue;
2114			}
2115			if ((f->dev_ep_index == 0) &&
2116			    (f->fs_xfer == NULL)) {
2117				/* no need to free this FIFO */
2118				continue;
2119			}
2120		} else if (iface_index == USB_IFACE_INDEX_ANY) {
2121			if ((f->methods == &usb2_ugen_methods) &&
2122			    (f->dev_ep_index == 0) && (flag == 0) &&
2123			    (f->fs_xfer == NULL)) {
2124				/* no need to free this FIFO */
2125				continue;
2126			}
2127		} else {
2128			/* no need to free this FIFO */
2129			continue;
2130		}
2131		/* free this FIFO */
2132		usb2_fifo_free(f);
2133	}
2134	return;
2135}
2136