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