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