xref: /freebsd/sys/dev/usb/usb_device.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2008-2023 Hans Petter Selasky
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #ifdef USB_GLOBAL_INCLUDE_FILE
29 #include USB_GLOBAL_INCLUDE_FILE
30 #else
31 #include <sys/stdint.h>
32 #include <sys/stddef.h>
33 #include <sys/param.h>
34 #include <sys/eventhandler.h>
35 #include <sys/queue.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/bus.h>
39 #include <sys/module.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/condvar.h>
43 #include <sys/sysctl.h>
44 #include <sys/sx.h>
45 #include <sys/unistd.h>
46 #include <sys/callout.h>
47 #include <sys/malloc.h>
48 #include <sys/priv.h>
49 #include <sys/conf.h>
50 #include <sys/fcntl.h>
51 
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usbdi.h>
54 #include <dev/usb/usbdi_util.h>
55 #include <dev/usb/usb_ioctl.h>
56 
57 #if USB_HAVE_UGEN
58 #include <sys/sbuf.h>
59 #endif
60 
61 #include "usbdevs.h"
62 
63 #define	USB_DEBUG_VAR usb_debug
64 
65 #include <dev/usb/usb_core.h>
66 #include <dev/usb/usb_debug.h>
67 #include <dev/usb/usb_process.h>
68 #include <dev/usb/usb_device.h>
69 #include <dev/usb/usb_busdma.h>
70 #include <dev/usb/usb_transfer.h>
71 #include <dev/usb/usb_request.h>
72 #include <dev/usb/usb_dynamic.h>
73 #include <dev/usb/usb_hub.h>
74 #include <dev/usb/usb_util.h>
75 #include <dev/usb/usb_msctest.h>
76 #if USB_HAVE_UGEN
77 #include <dev/usb/usb_dev.h>
78 #include <dev/usb/usb_generic.h>
79 #endif
80 
81 #include <dev/usb/quirk/usb_quirk.h>
82 
83 #include <dev/usb/usb_controller.h>
84 #include <dev/usb/usb_bus.h>
85 #endif			/* USB_GLOBAL_INCLUDE_FILE */
86 
87 /* function prototypes  */
88 
89 static int	sysctl_hw_usb_template(SYSCTL_HANDLER_ARGS);
90 static void	usb_init_endpoint(struct usb_device *, uint8_t,
91 		    struct usb_endpoint_descriptor *,
92 		    struct usb_endpoint_ss_comp_descriptor *,
93 		    struct usb_endpoint *);
94 static void	usb_unconfigure(struct usb_device *, uint8_t);
95 static void	usb_detach_device_sub(struct usb_device *, device_t *,
96 		    char **, uint8_t);
97 static uint8_t	usb_probe_and_attach_sub(struct usb_device *,
98 		    struct usb_attach_arg *);
99 static void	usb_init_attach_arg(struct usb_device *,
100 		    struct usb_attach_arg *);
101 static void	usb_suspend_resume_sub(struct usb_device *, device_t,
102 		    uint8_t);
103 static usb_proc_callback_t usbd_clear_stall_proc;
104 static usb_error_t usb_config_parse(struct usb_device *, uint8_t, uint8_t);
105 #if USB_HAVE_DEVCTL
106 static void	usb_notify_addq(const char *type, struct usb_device *);
107 #endif
108 #if USB_HAVE_UGEN
109 static void	usb_fifo_free_wrap(struct usb_device *, uint8_t, uint8_t);
110 static void	usb_cdev_create(struct usb_device *);
111 static void	usb_cdev_free(struct usb_device *);
112 #endif
113 
114 /* This variable is global to allow easy access to it: */
115 
116 #ifdef	USB_TEMPLATE
117 int	usb_template = USB_TEMPLATE;
118 #else
119 int	usb_template = -1;
120 #endif
121 
122 SYSCTL_PROC(_hw_usb, OID_AUTO, template,
123     CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
124     NULL, 0, sysctl_hw_usb_template,
125     "I", "Selected USB device side template");
126 
127 /*------------------------------------------------------------------------*
128  *	usb_trigger_reprobe_on_off
129  *
130  * This function sets the pull up resistors for all ports currently
131  * operating in device mode either on (when on_not_off is 1), or off
132  * (when it's 0).
133  *------------------------------------------------------------------------*/
134 static void
135 usb_trigger_reprobe_on_off(int on_not_off)
136 {
137 	struct usb_port_status ps;
138 	struct usb_bus *bus;
139 	struct usb_device *udev;
140 	usb_error_t err;
141 	int do_unlock, max;
142 
143 	max = devclass_get_maxunit(usb_devclass_ptr);
144 	while (max >= 0) {
145 		mtx_lock(&usb_ref_lock);
146 		bus = devclass_get_softc(usb_devclass_ptr, max);
147 		max--;
148 
149 		if (bus == NULL || bus->devices == NULL ||
150 		    bus->devices[USB_ROOT_HUB_ADDR] == NULL) {
151 			mtx_unlock(&usb_ref_lock);
152 			continue;
153 		}
154 
155 		udev = bus->devices[USB_ROOT_HUB_ADDR];
156 
157 		if (udev->refcount == USB_DEV_REF_MAX) {
158 			mtx_unlock(&usb_ref_lock);
159 			continue;
160 		}
161 
162 		udev->refcount++;
163 		mtx_unlock(&usb_ref_lock);
164 
165 		do_unlock = usbd_enum_lock(udev);
166 		if (do_unlock > 1) {
167 			do_unlock = 0;
168 			goto next;
169 		}
170 
171 		err = usbd_req_get_port_status(udev, NULL, &ps, 1);
172 		if (err != 0) {
173 			DPRINTF("usbd_req_get_port_status() "
174 			    "failed: %s\n", usbd_errstr(err));
175 			goto next;
176 		}
177 
178 		if ((UGETW(ps.wPortStatus) & UPS_PORT_MODE_DEVICE) == 0)
179 			goto next;
180 
181 		if (on_not_off) {
182 			err = usbd_req_set_port_feature(udev, NULL, 1,
183 			    UHF_PORT_POWER);
184 			if (err != 0) {
185 				DPRINTF("usbd_req_set_port_feature() "
186 				    "failed: %s\n", usbd_errstr(err));
187 			}
188 		} else {
189 			err = usbd_req_clear_port_feature(udev, NULL, 1,
190 			    UHF_PORT_POWER);
191 			if (err != 0) {
192 				DPRINTF("usbd_req_clear_port_feature() "
193 				    "failed: %s\n", usbd_errstr(err));
194 			}
195 		}
196 
197 next:
198 		mtx_lock(&usb_ref_lock);
199 		if (do_unlock)
200 			usbd_enum_unlock(udev);
201 		if (--(udev->refcount) == 0)
202 			cv_broadcast(&udev->ref_cv);
203 		mtx_unlock(&usb_ref_lock);
204 	}
205 }
206 
207 /*------------------------------------------------------------------------*
208  *	usb_trigger_reprobe_all
209  *
210  * This function toggles the pull up resistors for all ports currently
211  * operating in device mode, causing the host machine to reenumerate them.
212  *------------------------------------------------------------------------*/
213 static void
214 usb_trigger_reprobe_all(void)
215 {
216 
217 	/*
218 	 * Set the pull up resistors off for all ports in device mode.
219 	 */
220 	usb_trigger_reprobe_on_off(0);
221 
222 	/*
223 	 * According to the DWC OTG spec this must be at least 3ms.
224 	 */
225 	usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
226 
227 	/*
228 	 * Set the pull up resistors back on.
229 	 */
230 	usb_trigger_reprobe_on_off(1);
231 }
232 
233 static int
234 sysctl_hw_usb_template(SYSCTL_HANDLER_ARGS)
235 {
236 	int error, val;
237 
238 	val = usb_template;
239 	error = sysctl_handle_int(oidp, &val, 0, req);
240 	if (error != 0 || req->newptr == NULL || usb_template == val)
241 		return (error);
242 
243 	usb_template = val;
244 
245 	if (usb_template < 0) {
246 		usb_trigger_reprobe_on_off(0);
247 	} else {
248 		usb_trigger_reprobe_all();
249 	}
250 
251 	return (0);
252 }
253 
254 /* English is default language */
255 
256 static int usb_lang_id = 0x0009;
257 static int usb_lang_mask = 0x00FF;
258 
259 SYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_id, CTLFLAG_RWTUN,
260     &usb_lang_id, 0, "Preferred USB language ID");
261 
262 SYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_mask, CTLFLAG_RWTUN,
263     &usb_lang_mask, 0, "Preferred USB language mask");
264 
265 static const char* statestr[USB_STATE_MAX] = {
266 	[USB_STATE_DETACHED]	= "DETACHED",
267 	[USB_STATE_ATTACHED]	= "ATTACHED",
268 	[USB_STATE_POWERED]	= "POWERED",
269 	[USB_STATE_ADDRESSED]	= "ADDRESSED",
270 	[USB_STATE_CONFIGURED]	= "CONFIGURED",
271 };
272 
273 const char *
274 usb_statestr(enum usb_dev_state state)
275 {
276 	return ((state < USB_STATE_MAX) ? statestr[state] : "UNKNOWN");
277 }
278 
279 const char *
280 usb_get_manufacturer(struct usb_device *udev)
281 {
282 	return (udev->manufacturer ? udev->manufacturer : "Unknown");
283 }
284 
285 const char *
286 usb_get_product(struct usb_device *udev)
287 {
288 	return (udev->product ? udev->product : "");
289 }
290 
291 const char *
292 usb_get_serial(struct usb_device *udev)
293 {
294 	return (udev->serial ? udev->serial : "");
295 }
296 
297 /*------------------------------------------------------------------------*
298  *	usbd_get_ep_by_addr
299  *
300  * This function searches for an USB ep by endpoint address and
301  * direction.
302  *
303  * Returns:
304  * NULL: Failure
305  * Else: Success
306  *------------------------------------------------------------------------*/
307 struct usb_endpoint *
308 usbd_get_ep_by_addr(struct usb_device *udev, uint8_t ea_val)
309 {
310 	struct usb_endpoint *ep = udev->endpoints;
311 	struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max;
312 	enum {
313 		EA_MASK = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR),
314 	};
315 
316 	/*
317 	 * According to the USB specification not all bits are used
318 	 * for the endpoint address. Keep defined bits only:
319 	 */
320 	ea_val &= EA_MASK;
321 
322 	/*
323 	 * Iterate across all the USB endpoints searching for a match
324 	 * based on the endpoint address:
325 	 */
326 	for (; ep != ep_end; ep++) {
327 		if (ep->edesc == NULL) {
328 			continue;
329 		}
330 		/* do the mask and check the value */
331 		if ((ep->edesc->bEndpointAddress & EA_MASK) == ea_val) {
332 			goto found;
333 		}
334 	}
335 
336 	/*
337 	 * The default endpoint is always present and is checked separately:
338 	 */
339 	if ((udev->ctrl_ep.edesc != NULL) &&
340 	    ((udev->ctrl_ep.edesc->bEndpointAddress & EA_MASK) == ea_val)) {
341 		ep = &udev->ctrl_ep;
342 		goto found;
343 	}
344 	return (NULL);
345 
346 found:
347 	return (ep);
348 }
349 
350 /*------------------------------------------------------------------------*
351  *	usbd_get_endpoint
352  *
353  * This function searches for an USB endpoint based on the information
354  * given by the passed "struct usb_config" pointer.
355  *
356  * Return values:
357  * NULL: No match.
358  * Else: Pointer to "struct usb_endpoint".
359  *------------------------------------------------------------------------*/
360 struct usb_endpoint *
361 usbd_get_endpoint(struct usb_device *udev, uint8_t iface_index,
362     const struct usb_config *setup)
363 {
364 	struct usb_endpoint *ep = udev->endpoints;
365 	struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max;
366 	uint8_t index = setup->ep_index;
367 	uint8_t ea_mask;
368 	uint8_t ea_val;
369 	uint8_t type_mask;
370 	uint8_t type_val;
371 
372 	DPRINTFN(10, "udev=%p iface_index=%d address=0x%x "
373 	    "type=0x%x dir=0x%x index=%d\n",
374 	    udev, iface_index, setup->endpoint,
375 	    setup->type, setup->direction, setup->ep_index);
376 
377 	/* check USB mode */
378 
379 	if (setup->usb_mode != USB_MODE_DUAL &&
380 	    udev->flags.usb_mode != setup->usb_mode) {
381 		/* wrong mode - no endpoint */
382 		return (NULL);
383 	}
384 
385 	/* setup expected endpoint direction mask and value */
386 
387 	if (setup->direction == UE_DIR_RX) {
388 		ea_mask = (UE_DIR_IN | UE_DIR_OUT);
389 		ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ?
390 		    UE_DIR_OUT : UE_DIR_IN;
391 	} else if (setup->direction == UE_DIR_TX) {
392 		ea_mask = (UE_DIR_IN | UE_DIR_OUT);
393 		ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ?
394 		    UE_DIR_IN : UE_DIR_OUT;
395 	} else if (setup->direction == UE_DIR_ANY) {
396 		/* match any endpoint direction */
397 		ea_mask = 0;
398 		ea_val = 0;
399 	} else {
400 		/* match the given endpoint direction */
401 		ea_mask = (UE_DIR_IN | UE_DIR_OUT);
402 		ea_val = (setup->direction & (UE_DIR_IN | UE_DIR_OUT));
403 	}
404 
405 	/* setup expected endpoint address */
406 
407 	if (setup->endpoint == UE_ADDR_ANY) {
408 		/* match any endpoint address */
409 	} else {
410 		/* match the given endpoint address */
411 		ea_mask |= UE_ADDR;
412 		ea_val |= (setup->endpoint & UE_ADDR);
413 	}
414 
415 	/* setup expected endpoint type */
416 
417 	if (setup->type == UE_BULK_INTR) {
418 		/* this will match BULK and INTERRUPT endpoints */
419 		type_mask = 2;
420 		type_val = 2;
421 	} else if (setup->type == UE_TYPE_ANY) {
422 		/* match any endpoint type */
423 		type_mask = 0;
424 		type_val = 0;
425 	} else {
426 		/* match the given endpoint type */
427 		type_mask = UE_XFERTYPE;
428 		type_val = (setup->type & UE_XFERTYPE);
429 	}
430 
431 	/*
432 	 * Iterate across all the USB endpoints searching for a match
433 	 * based on the endpoint address. Note that we are searching
434 	 * the endpoints from the beginning of the "udev->endpoints" array.
435 	 */
436 	for (; ep != ep_end; ep++) {
437 		if ((ep->edesc == NULL) ||
438 		    (ep->iface_index != iface_index)) {
439 			continue;
440 		}
441 		/* do the masks and check the values */
442 
443 		if (((ep->edesc->bEndpointAddress & ea_mask) == ea_val) &&
444 		    ((ep->edesc->bmAttributes & type_mask) == type_val)) {
445 			if (!index--) {
446 				goto found;
447 			}
448 		}
449 	}
450 
451 	/*
452 	 * Match against default endpoint last, so that "any endpoint", "any
453 	 * address" and "any direction" returns the first endpoint of the
454 	 * interface. "iface_index" and "direction" is ignored:
455 	 */
456 	if ((udev->ctrl_ep.edesc != NULL) &&
457 	    ((udev->ctrl_ep.edesc->bEndpointAddress & ea_mask) == ea_val) &&
458 	    ((udev->ctrl_ep.edesc->bmAttributes & type_mask) == type_val) &&
459 	    (!index)) {
460 		ep = &udev->ctrl_ep;
461 		goto found;
462 	}
463 	return (NULL);
464 
465 found:
466 	return (ep);
467 }
468 
469 /*------------------------------------------------------------------------*
470  *	usbd_interface_count
471  *
472  * This function stores the number of USB interfaces excluding
473  * alternate settings, which the USB config descriptor reports into
474  * the unsigned 8-bit integer pointed to by "count".
475  *
476  * Returns:
477  *    0: Success
478  * Else: Failure
479  *------------------------------------------------------------------------*/
480 usb_error_t
481 usbd_interface_count(struct usb_device *udev, uint8_t *count)
482 {
483 	if (udev->cdesc == NULL) {
484 		*count = 0;
485 		return (USB_ERR_NOT_CONFIGURED);
486 	}
487 	*count = udev->ifaces_max;
488 	return (USB_ERR_NORMAL_COMPLETION);
489 }
490 
491 /*------------------------------------------------------------------------*
492  *	usb_init_endpoint
493  *
494  * This function will initialise the USB endpoint structure pointed to by
495  * the "endpoint" argument. The structure pointed to by "endpoint" must be
496  * zeroed before calling this function.
497  *------------------------------------------------------------------------*/
498 static void
499 usb_init_endpoint(struct usb_device *udev, uint8_t iface_index,
500     struct usb_endpoint_descriptor *edesc,
501     struct usb_endpoint_ss_comp_descriptor *ecomp,
502     struct usb_endpoint *ep)
503 {
504 	const struct usb_bus_methods *methods;
505 	usb_stream_t x;
506 
507 	methods = udev->bus->methods;
508 
509 	(methods->endpoint_init) (udev, edesc, ep);
510 
511 	/* initialise USB endpoint structure */
512 	ep->edesc = edesc;
513 	ep->ecomp = ecomp;
514 	ep->iface_index = iface_index;
515 
516 	/* setup USB stream queues */
517 	for (x = 0; x != USB_MAX_EP_STREAMS; x++) {
518 		TAILQ_INIT(&ep->endpoint_q[x].head);
519 		ep->endpoint_q[x].command = &usbd_pipe_start;
520 	}
521 
522 	/* the pipe is not supported by the hardware */
523  	if (ep->methods == NULL)
524 		return;
525 
526 	/* check for SUPER-speed streams mode endpoint */
527 	if (udev->speed == USB_SPEED_SUPER && ecomp != NULL &&
528 	    (edesc->bmAttributes & UE_XFERTYPE) == UE_BULK &&
529 	    (UE_GET_BULK_STREAMS(ecomp->bmAttributes) != 0)) {
530 		usbd_set_endpoint_mode(udev, ep, USB_EP_MODE_STREAMS);
531 	} else {
532 		usbd_set_endpoint_mode(udev, ep, USB_EP_MODE_DEFAULT);
533 	}
534 
535 	/* clear stall, if any */
536 	if (methods->clear_stall != NULL) {
537 		USB_BUS_LOCK(udev->bus);
538 		(methods->clear_stall) (udev, ep);
539 		USB_BUS_UNLOCK(udev->bus);
540 	}
541 }
542 
543 /*-----------------------------------------------------------------------*
544  *	usb_endpoint_foreach
545  *
546  * This function will iterate all the USB endpoints except the control
547  * endpoint. This function is NULL safe.
548  *
549  * Return values:
550  * NULL: End of USB endpoints
551  * Else: Pointer to next USB endpoint
552  *------------------------------------------------------------------------*/
553 struct usb_endpoint *
554 usb_endpoint_foreach(struct usb_device *udev, struct usb_endpoint *ep)
555 {
556 	struct usb_endpoint *ep_end;
557 
558 	/* be NULL safe */
559 	if (udev == NULL)
560 		return (NULL);
561 
562 	ep_end = udev->endpoints + udev->endpoints_max;
563 
564 	/* get next endpoint */
565 	if (ep == NULL)
566 		ep = udev->endpoints;
567 	else
568 		ep++;
569 
570 	/* find next allocated ep */
571 	while (ep != ep_end) {
572 		if (ep->edesc != NULL)
573 			return (ep);
574 		ep++;
575 	}
576 	return (NULL);
577 }
578 
579 /*------------------------------------------------------------------------*
580  *	usb_wait_pending_refs
581  *
582  * This function will wait for any USB references to go away before
583  * returning. This function is used before freeing a USB device.
584  *------------------------------------------------------------------------*/
585 static void
586 usb_wait_pending_refs(struct usb_device *udev)
587 {
588 #if USB_HAVE_UGEN
589 	DPRINTF("Refcount = %d\n", (int)udev->refcount);
590 
591 	mtx_lock(&usb_ref_lock);
592 	udev->refcount--;
593 	while (1) {
594 		/* wait for any pending references to go away */
595 		if (udev->refcount == 0) {
596 			/* prevent further refs being taken, if any */
597 			udev->refcount = USB_DEV_REF_MAX;
598 			break;
599 		}
600 		cv_wait(&udev->ref_cv, &usb_ref_lock);
601 	}
602 	mtx_unlock(&usb_ref_lock);
603 #endif
604 }
605 
606 /*------------------------------------------------------------------------*
607  *	usb_unconfigure
608  *
609  * This function will free all USB interfaces and USB endpoints belonging
610  * to an USB device.
611  *
612  * Flag values, see "USB_UNCFG_FLAG_XXX".
613  *------------------------------------------------------------------------*/
614 static void
615 usb_unconfigure(struct usb_device *udev, uint8_t flag)
616 {
617 	uint8_t do_unlock;
618 
619 	/* Prevent re-enumeration */
620 	do_unlock = usbd_enum_lock(udev);
621 
622 	/* detach all interface drivers */
623 	usb_detach_device(udev, USB_IFACE_INDEX_ANY, flag);
624 
625 #if USB_HAVE_UGEN
626 	/* free all FIFOs except control endpoint FIFOs */
627 	usb_fifo_free_wrap(udev, USB_IFACE_INDEX_ANY, flag);
628 
629 	/*
630 	 * Free all cdev's, if any.
631 	 */
632 	usb_cdev_free(udev);
633 #endif
634 
635 #if USB_HAVE_COMPAT_LINUX
636 	/* free Linux compat device, if any */
637 	if (udev->linux_endpoint_start != NULL) {
638 		usb_linux_free_device_p(udev);
639 		udev->linux_endpoint_start = NULL;
640 	}
641 #endif
642 
643 	usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_FREE);
644 
645 	/* free "cdesc" after "ifaces" and "endpoints", if any */
646 	if (udev->cdesc != NULL) {
647 		if (udev->flags.usb_mode != USB_MODE_DEVICE)
648 			usbd_free_config_desc(udev, udev->cdesc);
649 		udev->cdesc = NULL;
650 	}
651 	/* set unconfigured state */
652 	udev->curr_config_no = USB_UNCONFIG_NO;
653 	udev->curr_config_index = USB_UNCONFIG_INDEX;
654 
655 	if (do_unlock)
656 		usbd_enum_unlock(udev);
657 }
658 
659 /*------------------------------------------------------------------------*
660  *	usbd_set_config_index
661  *
662  * This function selects configuration by index, independent of the
663  * actual configuration number. This function should not be used by
664  * USB drivers.
665  *
666  * Returns:
667  *    0: Success
668  * Else: Failure
669  *------------------------------------------------------------------------*/
670 usb_error_t
671 usbd_set_config_index(struct usb_device *udev, uint8_t index)
672 {
673 	struct usb_status ds;
674 	struct usb_config_descriptor *cdp;
675 	uint16_t power;
676 	uint16_t max_power;
677 	uint8_t selfpowered;
678 	uint8_t do_unlock;
679 	usb_error_t err;
680 
681 	DPRINTFN(6, "udev=%p index=%d\n", udev, index);
682 
683 	/* Prevent re-enumeration */
684 	do_unlock = usbd_enum_lock(udev);
685 
686 	usb_unconfigure(udev, 0);
687 
688 	if (index == USB_UNCONFIG_INDEX) {
689 		/*
690 		 * Leave unallocated when unconfiguring the
691 		 * device. "usb_unconfigure()" will also reset
692 		 * the current config number and index.
693 		 */
694 		err = usbd_req_set_config(udev, NULL, USB_UNCONFIG_NO);
695 		if (udev->state == USB_STATE_CONFIGURED)
696 			usb_set_device_state(udev, USB_STATE_ADDRESSED);
697 		goto done;
698 	}
699 	/* get the full config descriptor */
700 	if (udev->flags.usb_mode == USB_MODE_DEVICE) {
701 		/* save some memory */
702 		err = usbd_req_get_descriptor_ptr(udev, &cdp,
703 		    (UDESC_CONFIG << 8) | index);
704 	} else {
705 		/* normal request */
706 		err = usbd_req_get_config_desc_full(udev,
707 		    NULL, &cdp, index);
708 	}
709 	if (err) {
710 		goto done;
711 	}
712 	/* set the new config descriptor */
713 
714 	udev->cdesc = cdp;
715 
716 	/* Figure out if the device is self or bus powered. */
717 	selfpowered = 0;
718 	if ((!udev->flags.uq_bus_powered) &&
719 	    (cdp->bmAttributes & UC_SELF_POWERED) &&
720 	    (udev->flags.usb_mode == USB_MODE_HOST)) {
721 		/* May be self powered. */
722 		if (cdp->bmAttributes & UC_BUS_POWERED) {
723 			/* Must ask device. */
724 			err = usbd_req_get_device_status(udev, NULL, &ds);
725 			if (err) {
726 				DPRINTFN(0, "could not read "
727 				    "device status: %s\n",
728 				    usbd_errstr(err));
729 			} else if (UGETW(ds.wStatus) & UDS_SELF_POWERED) {
730 				selfpowered = 1;
731 			}
732 			DPRINTF("status=0x%04x \n",
733 				UGETW(ds.wStatus));
734 		} else
735 			selfpowered = 1;
736 	}
737 	DPRINTF("udev=%p cdesc=%p (addr %d) cno=%d attr=0x%02x, "
738 	    "selfpowered=%d, power=%d\n",
739 	    udev, cdp,
740 	    udev->address, cdp->bConfigurationValue, cdp->bmAttributes,
741 	    selfpowered, cdp->bMaxPower * 2);
742 
743 	/* Check if we have enough power. */
744 	power = cdp->bMaxPower * 2;
745 
746 	if (udev->parent_hub) {
747 		max_power = udev->parent_hub->hub->portpower;
748 	} else {
749 		max_power = USB_MAX_POWER;
750 	}
751 
752 	if (power > max_power) {
753 		DPRINTFN(0, "power exceeded %d > %d\n", power, max_power);
754 		err = USB_ERR_NO_POWER;
755 		goto done;
756 	}
757 	/* Only update "self_powered" in USB Host Mode */
758 	if (udev->flags.usb_mode == USB_MODE_HOST) {
759 		udev->flags.self_powered = selfpowered;
760 	}
761 	udev->power = power;
762 	udev->curr_config_no = cdp->bConfigurationValue;
763 	udev->curr_config_index = index;
764 	usb_set_device_state(udev, USB_STATE_CONFIGURED);
765 
766 	/* Set the actual configuration value. */
767 	err = usbd_req_set_config(udev, NULL, cdp->bConfigurationValue);
768 	if (err) {
769 		goto done;
770 	}
771 
772 	err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_ALLOC);
773 	if (err) {
774 		goto done;
775 	}
776 
777 	err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_INIT);
778 	if (err) {
779 		goto done;
780 	}
781 
782 #if USB_HAVE_UGEN
783 	/* create device nodes for each endpoint */
784 	usb_cdev_create(udev);
785 #endif
786 
787 done:
788 	DPRINTF("error=%s\n", usbd_errstr(err));
789 	if (err) {
790 		usb_unconfigure(udev, 0);
791 	}
792 	if (do_unlock)
793 		usbd_enum_unlock(udev);
794 	return (err);
795 }
796 
797 /*------------------------------------------------------------------------*
798  *	usb_config_parse
799  *
800  * This function will allocate and free USB interfaces and USB endpoints,
801  * parse the USB configuration structure and initialise the USB endpoints
802  * and interfaces. If "iface_index" is not equal to
803  * "USB_IFACE_INDEX_ANY" then the "cmd" parameter is the
804  * alternate_setting to be selected for the given interface. Else the
805  * "cmd" parameter is defined by "USB_CFG_XXX". "iface_index" can be
806  * "USB_IFACE_INDEX_ANY" or a valid USB interface index. This function
807  * is typically called when setting the configuration or when setting
808  * an alternate interface.
809  *
810  * Returns:
811  *    0: Success
812  * Else: Failure
813  *------------------------------------------------------------------------*/
814 static usb_error_t
815 usb_config_parse(struct usb_device *udev, uint8_t iface_index, uint8_t cmd)
816 {
817 	struct usb_idesc_parse_state ips;
818 	struct usb_interface_descriptor *id;
819 	struct usb_endpoint_descriptor *ed;
820 	struct usb_interface *iface;
821 	struct usb_endpoint *ep;
822 	usb_error_t err;
823 	uint8_t ep_curr;
824 	uint8_t ep_max;
825 	uint8_t temp;
826 	uint8_t do_init;
827 	uint8_t alt_index;
828 
829 	if (iface_index != USB_IFACE_INDEX_ANY) {
830 		/* parameter overload */
831 		alt_index = cmd;
832 		cmd = USB_CFG_INIT;
833 	} else {
834 		/* not used */
835 		alt_index = 0;
836 	}
837 
838 	err = 0;
839 
840 	DPRINTFN(5, "iface_index=%d cmd=%d\n",
841 	    iface_index, cmd);
842 
843 	if (cmd == USB_CFG_INIT || cmd == USB_CFG_FREE) {
844 		sx_assert(&udev->enum_sx, SA_LOCKED);
845 
846 		/* check for in-use endpoints */
847 
848 		if (cmd == USB_CFG_INIT) {
849 			ep = udev->endpoints;
850 			ep_max = udev->endpoints_max;
851 			while (ep_max--) {
852 				/* look for matching endpoints */
853 				if (iface_index == USB_IFACE_INDEX_ANY ||
854 				    iface_index == ep->iface_index) {
855 					if (ep->refcount_alloc != 0)
856 						return (USB_ERR_IN_USE);
857 				}
858 			}
859 		}
860 
861 		ep = udev->endpoints;
862 		ep_max = udev->endpoints_max;
863 		while (ep_max--) {
864 			/* look for matching endpoints */
865 			if (iface_index == USB_IFACE_INDEX_ANY ||
866 			    iface_index == ep->iface_index) {
867 				/*
868 				 * Check if hardware needs a callback
869 				 * to unconfigure the endpoint. This
870 				 * may happen multiple times,
871 				 * because the requested alternate
872 				 * setting may fail. The callback
873 				 * implementation should be aware of
874 				 * and handle that.
875 				 */
876 				if (ep->edesc != NULL &&
877 				    udev->bus->methods->endpoint_uninit != NULL)
878 					udev->bus->methods->endpoint_uninit(udev, ep);
879 
880 				/* reset endpoint */
881 				memset(ep, 0, sizeof(*ep));
882 				/* make sure we don't zero the endpoint again */
883 				ep->iface_index = USB_IFACE_INDEX_ANY;
884 			}
885 			ep++;
886 		}
887 
888 		if (cmd == USB_CFG_FREE)
889 			goto cleanup;
890 	}
891 
892 	memset(&ips, 0, sizeof(ips));
893 
894 	ep_curr = 0;
895 	ep_max = 0;
896 
897 	while ((id = usb_idesc_foreach(udev->cdesc, &ips))) {
898 		iface = udev->ifaces + ips.iface_index;
899 
900 		/* check for specific interface match */
901 
902 		if (cmd == USB_CFG_INIT) {
903 			if ((iface_index != USB_IFACE_INDEX_ANY) &&
904 			    (iface_index != ips.iface_index)) {
905 				/* wrong interface */
906 				do_init = 0;
907 			} else if (alt_index != ips.iface_index_alt) {
908 				/* wrong alternate setting */
909 				do_init = 0;
910 			} else {
911 				/* initialise interface */
912 				do_init = 1;
913 			}
914 			/* update number of alternate settings, if any */
915 			if (iface_index == USB_IFACE_INDEX_ANY)
916 				iface->num_altsetting = ips.iface_index_alt + 1;
917 		} else
918 			do_init = 0;
919 
920 		/* check for new interface */
921 		if (ips.iface_index_alt == 0) {
922 			/* update current number of endpoints */
923 			ep_curr = ep_max;
924 		}
925 
926 		/* check for init */
927 		if (do_init) {
928 			/* setup the USB interface structure */
929 			iface->idesc = id;
930 			/* set alternate index */
931 			iface->alt_index = alt_index;
932 			/* set default interface parent */
933 			if (iface_index == USB_IFACE_INDEX_ANY) {
934 				iface->parent_iface_index =
935 				    USB_IFACE_INDEX_ANY;
936 			}
937 		}
938 
939 		DPRINTFN(5, "found idesc nendpt=%d\n", id->bNumEndpoints);
940 
941 		ed = (struct usb_endpoint_descriptor *)id;
942 
943 		temp = ep_curr;
944 
945 		/* iterate all the endpoint descriptors */
946 		while ((ed = usb_edesc_foreach(udev->cdesc, ed))) {
947 			/* check if endpoint limit has been reached */
948 			if (temp >= USB_MAX_EP_UNITS) {
949 				DPRINTF("Endpoint limit reached\n");
950 				break;
951 			}
952 
953 			ep = udev->endpoints + temp;
954 
955 			if (do_init) {
956 				void *ecomp;
957 
958 				ecomp = usb_ed_comp_foreach(udev->cdesc, (void *)ed);
959 				if (ecomp != NULL)
960 					DPRINTFN(5, "Found endpoint companion descriptor\n");
961 
962 				usb_init_endpoint(udev,
963 				    ips.iface_index, ed, ecomp, ep);
964 			}
965 
966 			temp ++;
967 
968 			/* find maximum number of endpoints */
969 			if (ep_max < temp)
970 				ep_max = temp;
971 		}
972 	}
973 
974 	/* NOTE: It is valid to have no interfaces and no endpoints! */
975 
976 	if (cmd == USB_CFG_ALLOC) {
977 		udev->ifaces_max = ips.iface_index;
978 #if (USB_HAVE_FIXED_IFACE == 0)
979 		udev->ifaces = NULL;
980 		if (udev->ifaces_max != 0) {
981 			udev->ifaces = malloc(sizeof(*iface) * udev->ifaces_max,
982 			        M_USB, M_WAITOK | M_ZERO);
983 			if (udev->ifaces == NULL) {
984 				err = USB_ERR_NOMEM;
985 				goto done;
986 			}
987 		}
988 #endif
989 #if (USB_HAVE_FIXED_ENDPOINT == 0)
990 		if (ep_max != 0) {
991 			udev->endpoints = malloc(sizeof(*ep) * ep_max,
992 			        M_USB, M_WAITOK | M_ZERO);
993 			if (udev->endpoints == NULL) {
994 				err = USB_ERR_NOMEM;
995 				goto done;
996 			}
997 		} else {
998 			udev->endpoints = NULL;
999 		}
1000 #endif
1001 		USB_BUS_LOCK(udev->bus);
1002 		udev->endpoints_max = ep_max;
1003 		/* reset any ongoing clear-stall */
1004 		udev->ep_curr = NULL;
1005 		USB_BUS_UNLOCK(udev->bus);
1006 	}
1007 #if (USB_HAVE_FIXED_IFACE == 0) || (USB_HAVE_FIXED_ENDPOINT == 0)
1008 done:
1009 #endif
1010 	if (err) {
1011 		if (cmd == USB_CFG_ALLOC) {
1012 cleanup:
1013 			USB_BUS_LOCK(udev->bus);
1014 			udev->endpoints_max = 0;
1015 			/* reset any ongoing clear-stall */
1016 			udev->ep_curr = NULL;
1017 			USB_BUS_UNLOCK(udev->bus);
1018 
1019 #if (USB_HAVE_FIXED_IFACE == 0)
1020 			free(udev->ifaces, M_USB);
1021 			udev->ifaces = NULL;
1022 #endif
1023 #if (USB_HAVE_FIXED_ENDPOINT == 0)
1024 			free(udev->endpoints, M_USB);
1025 			udev->endpoints = NULL;
1026 #endif
1027 			udev->ifaces_max = 0;
1028 		}
1029 	}
1030 	return (err);
1031 }
1032 
1033 /*------------------------------------------------------------------------*
1034  *	usbd_set_alt_interface_index
1035  *
1036  * This function will select an alternate interface index for the
1037  * given interface index. The interface should not be in use when this
1038  * function is called. That means there should not be any open USB
1039  * transfers. Else an error is returned. If the alternate setting is
1040  * already set this function will simply return success. This function
1041  * is called in Host mode and Device mode!
1042  *
1043  * Returns:
1044  *    0: Success
1045  * Else: Failure
1046  *------------------------------------------------------------------------*/
1047 usb_error_t
1048 usbd_set_alt_interface_index(struct usb_device *udev,
1049     uint8_t iface_index, uint8_t alt_index)
1050 {
1051 	struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1052 	usb_error_t err;
1053 	uint8_t do_unlock;
1054 
1055 	/* Prevent re-enumeration */
1056 	do_unlock = usbd_enum_lock(udev);
1057 
1058 	if (iface == NULL) {
1059 		err = USB_ERR_INVAL;
1060 		goto done;
1061 	}
1062 	if (iface->alt_index == alt_index) {
1063 		/*
1064 		 * Optimise away duplicate setting of
1065 		 * alternate setting in USB Host Mode!
1066 		 */
1067 		err = 0;
1068 		goto done;
1069 	}
1070 #if USB_HAVE_UGEN
1071 	/*
1072 	 * Free all generic FIFOs for this interface, except control
1073 	 * endpoint FIFOs:
1074 	 */
1075 	usb_fifo_free_wrap(udev, iface_index, 0);
1076 #endif
1077 
1078 	err = usb_config_parse(udev, iface_index, alt_index);
1079 	if (err) {
1080 		goto done;
1081 	}
1082 	if (iface->alt_index != alt_index) {
1083 		/* the alternate setting does not exist */
1084 		err = USB_ERR_INVAL;
1085 		goto done;
1086 	}
1087 
1088 	err = usbd_req_set_alt_interface_no(udev, NULL, iface_index,
1089 	    iface->idesc->bAlternateSetting);
1090 
1091 done:
1092 	if (do_unlock)
1093 		usbd_enum_unlock(udev);
1094 	return (err);
1095 }
1096 
1097 /*------------------------------------------------------------------------*
1098  *	usbd_set_endpoint_stall
1099  *
1100  * This function is used to make a BULK or INTERRUPT endpoint send
1101  * STALL tokens in USB device mode.
1102  *
1103  * Returns:
1104  *    0: Success
1105  * Else: Failure
1106  *------------------------------------------------------------------------*/
1107 usb_error_t
1108 usbd_set_endpoint_stall(struct usb_device *udev, struct usb_endpoint *ep,
1109     uint8_t do_stall)
1110 {
1111 	struct usb_xfer *xfer;
1112 	usb_stream_t x;
1113 	uint8_t et;
1114 	uint8_t was_stalled;
1115 
1116 	if (ep == NULL) {
1117 		/* nothing to do */
1118 		DPRINTF("Cannot find endpoint\n");
1119 		/*
1120 		 * Pretend that the clear or set stall request is
1121 		 * successful else some USB host stacks can do
1122 		 * strange things, especially when a control endpoint
1123 		 * stalls.
1124 		 */
1125 		return (0);
1126 	}
1127 	et = (ep->edesc->bmAttributes & UE_XFERTYPE);
1128 
1129 	if ((et != UE_BULK) &&
1130 	    (et != UE_INTERRUPT)) {
1131 		/*
1132 	         * Should not stall control
1133 	         * nor isochronous endpoints.
1134 	         */
1135 		DPRINTF("Invalid endpoint\n");
1136 		return (0);
1137 	}
1138 	USB_BUS_LOCK(udev->bus);
1139 
1140 	/* store current stall state */
1141 	was_stalled = ep->is_stalled;
1142 
1143 	/* check for no change */
1144 	if (was_stalled && do_stall) {
1145 		/* if the endpoint is already stalled do nothing */
1146 		USB_BUS_UNLOCK(udev->bus);
1147 		DPRINTF("No change\n");
1148 		return (0);
1149 	}
1150 	/* set stalled state */
1151 	ep->is_stalled = 1;
1152 
1153 	if (do_stall || (!was_stalled)) {
1154 		if (!was_stalled) {
1155 			for (x = 0; x != USB_MAX_EP_STREAMS; x++) {
1156 				/* lookup the current USB transfer, if any */
1157 				xfer = ep->endpoint_q[x].curr;
1158 				if (xfer != NULL) {
1159 					/*
1160 					 * The "xfer_stall" method
1161 					 * will complete the USB
1162 					 * transfer like in case of a
1163 					 * timeout setting the error
1164 					 * code "USB_ERR_STALLED".
1165 					 */
1166 					(udev->bus->methods->xfer_stall) (xfer);
1167 				}
1168 			}
1169 		}
1170 		(udev->bus->methods->set_stall) (udev, ep, &do_stall);
1171 	}
1172 	if (!do_stall) {
1173 		ep->toggle_next = 0;	/* reset data toggle */
1174 		ep->is_stalled = 0;	/* clear stalled state */
1175 
1176 		(udev->bus->methods->clear_stall) (udev, ep);
1177 
1178 		/* start the current or next transfer, if any */
1179 		for (x = 0; x != USB_MAX_EP_STREAMS; x++) {
1180 			usb_command_wrapper(&ep->endpoint_q[x],
1181 			    ep->endpoint_q[x].curr);
1182 		}
1183 	}
1184 	USB_BUS_UNLOCK(udev->bus);
1185 	return (0);
1186 }
1187 
1188 /*------------------------------------------------------------------------*
1189  *	usb_reset_iface_endpoints - used in USB device side mode
1190  *------------------------------------------------------------------------*/
1191 usb_error_t
1192 usb_reset_iface_endpoints(struct usb_device *udev, uint8_t iface_index)
1193 {
1194 	struct usb_endpoint *ep;
1195 	struct usb_endpoint *ep_end;
1196 
1197 	ep = udev->endpoints;
1198 	ep_end = udev->endpoints + udev->endpoints_max;
1199 
1200 	for (; ep != ep_end; ep++) {
1201 		if ((ep->edesc == NULL) ||
1202 		    (ep->iface_index != iface_index)) {
1203 			continue;
1204 		}
1205 		/* simulate a clear stall from the peer */
1206 		usbd_set_endpoint_stall(udev, ep, 0);
1207 	}
1208 	return (0);
1209 }
1210 
1211 /*------------------------------------------------------------------------*
1212  *	usb_detach_device_sub
1213  *
1214  * This function will try to detach an USB device. If it fails a panic
1215  * will result.
1216  *
1217  * Flag values, see "USB_UNCFG_FLAG_XXX".
1218  *------------------------------------------------------------------------*/
1219 static void
1220 usb_detach_device_sub(struct usb_device *udev, device_t *ppdev,
1221     char **ppnpinfo, uint8_t flag)
1222 {
1223 	device_t dev;
1224 	char *pnpinfo;
1225 	int err;
1226 
1227 	dev = *ppdev;
1228 	if (dev) {
1229 		/*
1230 		 * NOTE: It is important to clear "*ppdev" before deleting
1231 		 * the child due to some device methods being called late
1232 		 * during the delete process !
1233 		 */
1234 		*ppdev = NULL;
1235 
1236 		if (!rebooting) {
1237 			device_printf(dev, "at %s, port %d, addr %d "
1238 			    "(disconnected)\n",
1239 			    device_get_nameunit(udev->parent_dev),
1240 			    udev->port_no, udev->address);
1241 		}
1242 
1243 		if (device_is_attached(dev)) {
1244 			if (udev->flags.peer_suspended) {
1245 				err = DEVICE_RESUME(dev);
1246 				if (err) {
1247 					device_printf(dev, "Resume failed\n");
1248 				}
1249 			}
1250 		}
1251 		/* detach and delete child */
1252 		if (device_delete_child(udev->parent_dev, dev)) {
1253 			goto error;
1254 		}
1255 	}
1256 
1257 	pnpinfo = *ppnpinfo;
1258 	if (pnpinfo != NULL) {
1259 		*ppnpinfo = NULL;
1260 		free(pnpinfo, M_USBDEV);
1261 	}
1262 	return;
1263 
1264 error:
1265 	/* Detach is not allowed to fail in the USB world */
1266 	panic("usb_detach_device_sub: A USB driver would not detach\n");
1267 }
1268 
1269 /*------------------------------------------------------------------------*
1270  *	usb_detach_device
1271  *
1272  * The following function will detach the matching interfaces.
1273  * This function is NULL safe.
1274  *
1275  * Flag values, see "USB_UNCFG_FLAG_XXX".
1276  *------------------------------------------------------------------------*/
1277 void
1278 usb_detach_device(struct usb_device *udev, uint8_t iface_index,
1279     uint8_t flag)
1280 {
1281 	struct usb_interface *iface;
1282 	uint8_t i;
1283 
1284 	if (udev == NULL) {
1285 		/* nothing to do */
1286 		return;
1287 	}
1288 	DPRINTFN(4, "udev=%p\n", udev);
1289 
1290 	sx_assert(&udev->enum_sx, SA_LOCKED);
1291 
1292 	/*
1293 	 * First detach the child to give the child's detach routine a
1294 	 * chance to detach the sub-devices in the correct order.
1295 	 * Then delete the child using "device_delete_child()" which
1296 	 * will detach all sub-devices from the bottom and upwards!
1297 	 */
1298 	if (iface_index != USB_IFACE_INDEX_ANY) {
1299 		i = iface_index;
1300 		iface_index = i + 1;
1301 	} else {
1302 		i = 0;
1303 		iface_index = USB_IFACE_MAX;
1304 	}
1305 
1306 	/* do the detach */
1307 
1308 	for (; i != iface_index; i++) {
1309 		iface = usbd_get_iface(udev, i);
1310 		if (iface == NULL) {
1311 			/* looks like the end of the USB interfaces */
1312 			break;
1313 		}
1314 		usb_detach_device_sub(udev, &iface->subdev,
1315 		    &iface->pnpinfo, flag);
1316 	}
1317 }
1318 
1319 /*------------------------------------------------------------------------*
1320  *	usb_probe_and_attach_sub
1321  *
1322  * Returns:
1323  *    0: Success
1324  * Else: Failure
1325  *------------------------------------------------------------------------*/
1326 static uint8_t
1327 usb_probe_and_attach_sub(struct usb_device *udev,
1328     struct usb_attach_arg *uaa)
1329 {
1330 	struct usb_interface *iface;
1331 	device_t dev;
1332 	int err;
1333 
1334 	iface = uaa->iface;
1335 	if (iface->parent_iface_index != USB_IFACE_INDEX_ANY) {
1336 		/* leave interface alone */
1337 		return (0);
1338 	}
1339 	dev = iface->subdev;
1340 	if (dev) {
1341 		/* clean up after module unload */
1342 
1343 		if (device_is_attached(dev)) {
1344 			/* already a device there */
1345 			return (0);
1346 		}
1347 		/* clear "iface->subdev" as early as possible */
1348 
1349 		iface->subdev = NULL;
1350 
1351 		if (device_delete_child(udev->parent_dev, dev)) {
1352 			/*
1353 			 * Panic here, else one can get a double call
1354 			 * to device_detach().  USB devices should
1355 			 * never fail on detach!
1356 			 */
1357 			panic("device_delete_child() failed\n");
1358 		}
1359 	}
1360 	if (uaa->temp_dev == NULL) {
1361 		/* create a new child */
1362 		uaa->temp_dev = device_add_child(udev->parent_dev, NULL, -1);
1363 		if (uaa->temp_dev == NULL) {
1364 			device_printf(udev->parent_dev,
1365 			    "Device creation failed\n");
1366 			return (1);	/* failure */
1367 		}
1368 		device_set_ivars(uaa->temp_dev, uaa);
1369 		device_quiet(uaa->temp_dev);
1370 	}
1371 	/*
1372 	 * Set "subdev" before probe and attach so that "devd" gets
1373 	 * the information it needs.
1374 	 */
1375 	iface->subdev = uaa->temp_dev;
1376 
1377 	if (device_probe_and_attach(iface->subdev) == 0) {
1378 		/*
1379 		 * The USB attach arguments are only available during probe
1380 		 * and attach !
1381 		 */
1382 		uaa->temp_dev = NULL;
1383 		device_set_ivars(iface->subdev, NULL);
1384 
1385 		if (udev->flags.peer_suspended) {
1386 			err = DEVICE_SUSPEND(iface->subdev);
1387 			if (err)
1388 				device_printf(iface->subdev, "Suspend failed\n");
1389 		}
1390 		return (0);		/* success */
1391 	} else {
1392 		/* No USB driver found */
1393 		iface->subdev = NULL;
1394 	}
1395 	return (1);			/* failure */
1396 }
1397 
1398 /*------------------------------------------------------------------------*
1399  *	usbd_set_parent_iface
1400  *
1401  * Using this function will lock the alternate interface setting on an
1402  * interface. It is typically used for multi interface drivers. In USB
1403  * device side mode it is assumed that the alternate interfaces all
1404  * have the same endpoint descriptors. The default parent index value
1405  * is "USB_IFACE_INDEX_ANY". Then the alternate setting value is not
1406  * locked.
1407  *------------------------------------------------------------------------*/
1408 void
1409 usbd_set_parent_iface(struct usb_device *udev, uint8_t iface_index,
1410     uint8_t parent_index)
1411 {
1412 	struct usb_interface *iface;
1413 
1414 	if (udev == NULL || iface_index == parent_index) {
1415 		/* nothing to do */
1416 		return;
1417 	}
1418 	iface = usbd_get_iface(udev, iface_index);
1419 	if (iface != NULL)
1420 		iface->parent_iface_index = parent_index;
1421 }
1422 
1423 static void
1424 usb_init_attach_arg(struct usb_device *udev,
1425     struct usb_attach_arg *uaa)
1426 {
1427 	memset(uaa, 0, sizeof(*uaa));
1428 
1429 	uaa->device = udev;
1430 	uaa->usb_mode = udev->flags.usb_mode;
1431 	uaa->port = udev->port_no;
1432 	uaa->dev_state = UAA_DEV_READY;
1433 
1434 	uaa->info.idVendor = UGETW(udev->ddesc.idVendor);
1435 	uaa->info.idProduct = UGETW(udev->ddesc.idProduct);
1436 	uaa->info.bcdDevice = UGETW(udev->ddesc.bcdDevice);
1437 	uaa->info.bDeviceClass = udev->ddesc.bDeviceClass;
1438 	uaa->info.bDeviceSubClass = udev->ddesc.bDeviceSubClass;
1439 	uaa->info.bDeviceProtocol = udev->ddesc.bDeviceProtocol;
1440 	uaa->info.bConfigIndex = udev->curr_config_index;
1441 	uaa->info.bConfigNum = udev->curr_config_no;
1442 }
1443 
1444 /*------------------------------------------------------------------------*
1445  *	usb_probe_and_attach
1446  *
1447  * This function is called from "uhub_explore_sub()",
1448  * "usb_handle_set_config()" and "usb_handle_request()".
1449  *
1450  * Returns:
1451  *    0: Success
1452  * Else: A control transfer failed
1453  *------------------------------------------------------------------------*/
1454 usb_error_t
1455 usb_probe_and_attach(struct usb_device *udev, uint8_t iface_index)
1456 {
1457 	struct usb_attach_arg uaa;
1458 	struct usb_interface *iface;
1459 	uint8_t i;
1460 	uint8_t j;
1461 	uint8_t do_unlock;
1462 
1463 	if (udev == NULL) {
1464 		DPRINTF("udev == NULL\n");
1465 		return (USB_ERR_INVAL);
1466 	}
1467 	/* Prevent re-enumeration */
1468 	do_unlock = usbd_enum_lock(udev);
1469 
1470 	if (udev->curr_config_index == USB_UNCONFIG_INDEX) {
1471 		/* do nothing - no configuration has been set */
1472 		goto done;
1473 	}
1474 	/* setup USB attach arguments */
1475 
1476 	usb_init_attach_arg(udev, &uaa);
1477 
1478 	/*
1479 	 * If the whole USB device is targeted, invoke the USB event
1480 	 * handler(s):
1481 	 */
1482 	if (iface_index == USB_IFACE_INDEX_ANY) {
1483 		if (usb_test_quirk(&uaa, UQ_MSC_DYMO_EJECT) != 0 &&
1484 		    usb_dymo_eject(udev, 0) == 0) {
1485 			/* success, mark the udev as disappearing */
1486 			uaa.dev_state = UAA_DEV_EJECTING;
1487 		}
1488 
1489 		EVENTHANDLER_INVOKE(usb_dev_configured, udev, &uaa);
1490 
1491 		if (uaa.dev_state != UAA_DEV_READY) {
1492 			/* leave device unconfigured */
1493 			usb_unconfigure(udev, 0);
1494 			goto done;
1495 		}
1496 	}
1497 
1498 	/* Check if only one interface should be probed: */
1499 	if (iface_index != USB_IFACE_INDEX_ANY) {
1500 		i = iface_index;
1501 		j = i + 1;
1502 	} else {
1503 		i = 0;
1504 		j = USB_IFACE_MAX;
1505 	}
1506 
1507 	/* Do the probe and attach */
1508 	for (; i != j; i++) {
1509 		iface = usbd_get_iface(udev, i);
1510 		if (iface == NULL) {
1511 			/*
1512 			 * Looks like the end of the USB
1513 			 * interfaces !
1514 			 */
1515 			DPRINTFN(2, "end of interfaces "
1516 			    "at %u\n", i);
1517 			break;
1518 		}
1519 		if (iface->idesc == NULL) {
1520 			/* no interface descriptor */
1521 			continue;
1522 		}
1523 		uaa.iface = iface;
1524 
1525 		uaa.info.bInterfaceClass =
1526 		    iface->idesc->bInterfaceClass;
1527 		uaa.info.bInterfaceSubClass =
1528 		    iface->idesc->bInterfaceSubClass;
1529 		uaa.info.bInterfaceProtocol =
1530 		    iface->idesc->bInterfaceProtocol;
1531 		uaa.info.bIfaceIndex = i;
1532 		uaa.info.bIfaceNum =
1533 		    iface->idesc->bInterfaceNumber;
1534 		uaa.driver_info = 0;	/* reset driver_info */
1535 
1536 		DPRINTFN(2, "iclass=%u/%u/%u iindex=%u/%u\n",
1537 		    uaa.info.bInterfaceClass,
1538 		    uaa.info.bInterfaceSubClass,
1539 		    uaa.info.bInterfaceProtocol,
1540 		    uaa.info.bIfaceIndex,
1541 		    uaa.info.bIfaceNum);
1542 
1543 		usb_probe_and_attach_sub(udev, &uaa);
1544 
1545 		/*
1546 		 * Remove the leftover child, if any, to enforce that
1547 		 * a new nomatch devd event is generated for the next
1548 		 * interface if no driver is found:
1549 		 */
1550 		if (uaa.temp_dev == NULL)
1551 			continue;
1552 		if (device_delete_child(udev->parent_dev, uaa.temp_dev))
1553 			DPRINTFN(0, "device delete child failed\n");
1554 		uaa.temp_dev = NULL;
1555 	}
1556 done:
1557 	if (do_unlock)
1558 		usbd_enum_unlock(udev);
1559 	return (0);
1560 }
1561 
1562 /*------------------------------------------------------------------------*
1563  *	usb_suspend_resume_sub
1564  *
1565  * This function is called when the suspend or resume methods should
1566  * be executed on an USB device.
1567  *------------------------------------------------------------------------*/
1568 static void
1569 usb_suspend_resume_sub(struct usb_device *udev, device_t dev, uint8_t do_suspend)
1570 {
1571 	int err;
1572 
1573 	if (dev == NULL) {
1574 		return;
1575 	}
1576 	if (!device_is_attached(dev)) {
1577 		return;
1578 	}
1579 	if (do_suspend) {
1580 		err = DEVICE_SUSPEND(dev);
1581 	} else {
1582 		err = DEVICE_RESUME(dev);
1583 	}
1584 	if (err) {
1585 		device_printf(dev, "%s failed\n",
1586 		    do_suspend ? "Suspend" : "Resume");
1587 	}
1588 }
1589 
1590 /*------------------------------------------------------------------------*
1591  *	usb_suspend_resume
1592  *
1593  * The following function will suspend or resume the USB device.
1594  *
1595  * Returns:
1596  *    0: Success
1597  * Else: Failure
1598  *------------------------------------------------------------------------*/
1599 usb_error_t
1600 usb_suspend_resume(struct usb_device *udev, uint8_t do_suspend)
1601 {
1602 	struct usb_interface *iface;
1603 	uint8_t i;
1604 
1605 	if (udev == NULL) {
1606 		/* nothing to do */
1607 		return (0);
1608 	}
1609 	DPRINTFN(4, "udev=%p do_suspend=%d\n", udev, do_suspend);
1610 
1611 	sx_assert(&udev->sr_sx, SA_LOCKED);
1612 
1613 	USB_BUS_LOCK(udev->bus);
1614 	/* filter the suspend events */
1615 	if (udev->flags.peer_suspended == do_suspend) {
1616 		USB_BUS_UNLOCK(udev->bus);
1617 		/* nothing to do */
1618 		return (0);
1619 	}
1620 	udev->flags.peer_suspended = do_suspend;
1621 	USB_BUS_UNLOCK(udev->bus);
1622 
1623 	/* do the suspend or resume */
1624 
1625 	for (i = 0; i != USB_IFACE_MAX; i++) {
1626 		iface = usbd_get_iface(udev, i);
1627 		if (iface == NULL) {
1628 			/* looks like the end of the USB interfaces */
1629 			break;
1630 		}
1631 		usb_suspend_resume_sub(udev, iface->subdev, do_suspend);
1632 	}
1633 	return (0);
1634 }
1635 
1636 /*------------------------------------------------------------------------*
1637  *      usbd_clear_stall_proc
1638  *
1639  * This function performs generic USB clear stall operations.
1640  *------------------------------------------------------------------------*/
1641 static void
1642 usbd_clear_stall_proc(struct usb_proc_msg *_pm)
1643 {
1644 	struct usb_udev_msg *pm = (void *)_pm;
1645 	struct usb_device *udev = pm->udev;
1646 
1647 	/* Change lock */
1648 	USB_BUS_UNLOCK(udev->bus);
1649 	USB_MTX_LOCK(&udev->device_mtx);
1650 
1651 	/* Start clear stall callback */
1652 	usbd_transfer_start(udev->ctrl_xfer[1]);
1653 
1654 	/* Change lock */
1655 	USB_MTX_UNLOCK(&udev->device_mtx);
1656 	USB_BUS_LOCK(udev->bus);
1657 }
1658 
1659 /*------------------------------------------------------------------------*
1660  *      usb_get_langid
1661  *
1662  * This function tries to figure out the USB string language to use.
1663  *------------------------------------------------------------------------*/
1664 void
1665 usb_get_langid(struct usb_device *udev)
1666 {
1667 	uint8_t *scratch_ptr;
1668 	uint8_t do_unlock;
1669 	int err;
1670 
1671 	/*
1672 	 * Workaround for buggy USB devices.
1673 	 *
1674 	 * It appears that some string-less USB chips will crash and
1675 	 * disappear if any attempts are made to read any string
1676 	 * descriptors.
1677 	 *
1678 	 * Try to detect such chips by checking the strings in the USB
1679 	 * device descriptor. If no strings are present there we
1680 	 * simply disable all USB strings.
1681 	 */
1682 
1683 	/* Protect scratch area */
1684 	do_unlock = usbd_ctrl_lock(udev);
1685 
1686 	scratch_ptr = udev->scratch.data;
1687 
1688 	if (udev->flags.no_strings) {
1689 		err = USB_ERR_INVAL;
1690 	} else if (udev->ddesc.iManufacturer ||
1691 	    udev->ddesc.iProduct ||
1692 	    udev->ddesc.iSerialNumber) {
1693 		/* read out the language ID string */
1694 		err = usbd_req_get_string_desc(udev, NULL,
1695 		    (char *)scratch_ptr, 4, 0, USB_LANGUAGE_TABLE);
1696 	} else {
1697 		err = USB_ERR_INVAL;
1698 	}
1699 
1700 	if (err || (scratch_ptr[0] < 4)) {
1701 		udev->flags.no_strings = 1;
1702 	} else {
1703 		uint16_t langid;
1704 		uint16_t pref;
1705 		uint16_t mask;
1706 		uint8_t x;
1707 
1708 		/* load preferred value and mask */
1709 		pref = usb_lang_id;
1710 		mask = usb_lang_mask;
1711 
1712 		/* align length correctly */
1713 		scratch_ptr[0] &= ~1U;
1714 
1715 		/* fix compiler warning */
1716 		langid = 0;
1717 
1718 		/* search for preferred language */
1719 		for (x = 2; x < scratch_ptr[0]; x += 2) {
1720 			langid = UGETW(scratch_ptr + x);
1721 			if ((langid & mask) == pref)
1722 				break;
1723 		}
1724 		if (x >= scratch_ptr[0]) {
1725 			/* pick the first language as the default */
1726 			DPRINTFN(1, "Using first language\n");
1727 			langid = UGETW(scratch_ptr + 2);
1728 		}
1729 
1730 		DPRINTFN(1, "Language selected: 0x%04x\n", langid);
1731 		udev->langid = langid;
1732 	}
1733 
1734 	if (do_unlock)
1735 		usbd_ctrl_unlock(udev);
1736 }
1737 
1738 /*------------------------------------------------------------------------*
1739  *	usb_alloc_device
1740  *
1741  * This function allocates a new USB device. This function is called
1742  * when a new device has been put in the powered state, but not yet in
1743  * the addressed state. Get initial descriptor, set the address, get
1744  * full descriptor and get strings.
1745  *
1746  * Return values:
1747  *    0: Failure
1748  * Else: Success
1749  *------------------------------------------------------------------------*/
1750 struct usb_device *
1751 usb_alloc_device(device_t parent_dev, struct usb_bus *bus,
1752     struct usb_device *parent_hub, uint8_t depth, uint8_t port_index,
1753     uint8_t port_no, enum usb_dev_speed speed, enum usb_hc_mode mode)
1754 {
1755 	struct usb_attach_arg uaa;
1756 	struct usb_device *udev;
1757 	struct usb_device *adev;
1758 	struct usb_device *hub;
1759 	usb_error_t err;
1760 	uint8_t device_index;
1761 	uint8_t config_index;
1762 	uint8_t config_quirk;
1763 	uint8_t set_config_failed;
1764 
1765 	DPRINTF("parent_dev=%p, bus=%p, parent_hub=%p, depth=%u, "
1766 	    "port_index=%u, port_no=%u, speed=%u, usb_mode=%u\n",
1767 	    parent_dev, bus, parent_hub, depth, port_index, port_no,
1768 	    speed, mode);
1769 
1770 	/*
1771 	 * Find an unused device index. In USB Host mode this is the
1772 	 * same as the device address.
1773 	 *
1774 	 * Device index zero is not used and device index 1 should
1775 	 * always be the root hub.
1776 	 */
1777 	for (device_index = USB_ROOT_HUB_ADDR;
1778 	    (device_index != bus->devices_max) &&
1779 	    (bus->devices[device_index] != NULL);
1780 	    device_index++) /* nop */;
1781 
1782 	if (device_index == bus->devices_max) {
1783 		device_printf(bus->bdev,
1784 		    "No free USB device index for new device\n");
1785 		return (NULL);
1786 	}
1787 
1788 	if (depth > 0x10) {
1789 		device_printf(bus->bdev,
1790 		    "Invalid device depth\n");
1791 		return (NULL);
1792 	}
1793 	udev = malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO);
1794 #if (USB_HAVE_MALLOC_WAITOK == 0)
1795 	if (udev == NULL) {
1796 		return (NULL);
1797 	}
1798 #endif
1799 	/* initialise our SX-lock */
1800 	sx_init_flags(&udev->enum_sx, "USB config SX lock", SX_DUPOK);
1801 	sx_init_flags(&udev->sr_sx, "USB suspend and resume SX lock", SX_NOWITNESS);
1802 	sx_init_flags(&udev->ctrl_sx, "USB control transfer SX lock", SX_DUPOK);
1803 
1804 	cv_init(&udev->ctrlreq_cv, "WCTRL");
1805 	cv_init(&udev->ref_cv, "UGONE");
1806 
1807 	/* initialise our mutex */
1808 	mtx_init(&udev->device_mtx, "USB device mutex", NULL, MTX_DEF);
1809 
1810 	/* initialise generic clear stall */
1811 	udev->cs_msg[0].hdr.pm_callback = &usbd_clear_stall_proc;
1812 	udev->cs_msg[0].udev = udev;
1813 	udev->cs_msg[1].hdr.pm_callback = &usbd_clear_stall_proc;
1814 	udev->cs_msg[1].udev = udev;
1815 
1816 	/* initialise some USB device fields */
1817 	udev->parent_hub = parent_hub;
1818 	udev->parent_dev = parent_dev;
1819 	udev->port_index = port_index;
1820 	udev->port_no = port_no;
1821 	udev->depth = depth;
1822 	udev->bus = bus;
1823 	udev->address = USB_START_ADDR;	/* default value */
1824 	udev->plugtime = (usb_ticks_t)ticks;
1825 	/*
1826 	 * We need to force the power mode to "on" because there are plenty
1827 	 * of USB devices out there that do not work very well with
1828 	 * automatic suspend and resume!
1829 	 */
1830 	udev->power_mode = usbd_filter_power_mode(udev, USB_POWER_MODE_ON);
1831 	udev->pwr_save.last_xfer_time = ticks;
1832 	/* we are not ready yet */
1833 	udev->refcount = 1;
1834 
1835 	/* set up default endpoint descriptor */
1836 	udev->ctrl_ep_desc.bLength = sizeof(udev->ctrl_ep_desc);
1837 	udev->ctrl_ep_desc.bDescriptorType = UDESC_ENDPOINT;
1838 	udev->ctrl_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
1839 	udev->ctrl_ep_desc.bmAttributes = UE_CONTROL;
1840 	udev->ctrl_ep_desc.wMaxPacketSize[0] = USB_MAX_IPACKET;
1841 	udev->ctrl_ep_desc.wMaxPacketSize[1] = 0;
1842 	udev->ctrl_ep_desc.bInterval = 0;
1843 
1844 	/* set up default endpoint companion descriptor */
1845 	udev->ctrl_ep_comp_desc.bLength = sizeof(udev->ctrl_ep_comp_desc);
1846 	udev->ctrl_ep_comp_desc.bDescriptorType = UDESC_ENDPOINT_SS_COMP;
1847 
1848 	udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
1849 
1850 	udev->speed = speed;
1851 	udev->flags.usb_mode = mode;
1852 
1853 	/* search for our High Speed USB HUB, if any */
1854 
1855 	adev = udev;
1856 	hub = udev->parent_hub;
1857 
1858 	while (hub) {
1859 		if (hub->speed == USB_SPEED_HIGH) {
1860 			udev->hs_hub_addr = hub->address;
1861 			udev->parent_hs_hub = hub;
1862 			udev->hs_port_no = adev->port_no;
1863 			break;
1864 		}
1865 		adev = hub;
1866 		hub = hub->parent_hub;
1867 	}
1868 
1869 	/* init the default endpoint */
1870 	usb_init_endpoint(udev, 0,
1871 	    &udev->ctrl_ep_desc,
1872 	    &udev->ctrl_ep_comp_desc,
1873 	    &udev->ctrl_ep);
1874 
1875 	/* set device index */
1876 	udev->device_index = device_index;
1877 
1878 #if USB_HAVE_UGEN
1879 	/* Create ugen name */
1880 	snprintf(udev->ugen_name, sizeof(udev->ugen_name),
1881 	    USB_GENERIC_NAME "%u.%u", device_get_unit(bus->bdev),
1882 	    device_index);
1883 	LIST_INIT(&udev->pd_list);
1884 
1885 	/* Create the control endpoint device */
1886 	udev->ctrl_dev = usb_make_dev(udev, NULL, 0, 0,
1887 	    FREAD|FWRITE, UID_ROOT, GID_OPERATOR, 0600);
1888 
1889 	/* Create a link from /dev/ugenX.X to the default endpoint */
1890 	if (udev->ctrl_dev != NULL)
1891 		make_dev_alias(udev->ctrl_dev->cdev, "%s", udev->ugen_name);
1892 #endif
1893 	/* Initialise device */
1894 	if (bus->methods->device_init != NULL) {
1895 		err = (bus->methods->device_init) (udev);
1896 		if (err != 0) {
1897 			DPRINTFN(0, "device init %d failed "
1898 			    "(%s, ignored)\n", device_index,
1899 			    usbd_errstr(err));
1900 			goto done;
1901 		}
1902 	}
1903 	/* set powered device state after device init is complete */
1904 	usb_set_device_state(udev, USB_STATE_POWERED);
1905 
1906 	if (udev->flags.usb_mode == USB_MODE_HOST) {
1907 		err = usbd_req_set_address(udev, NULL, device_index);
1908 
1909 		/*
1910 		 * This is the new USB device address from now on, if
1911 		 * the set address request didn't set it already.
1912 		 */
1913 		if (udev->address == USB_START_ADDR)
1914 			udev->address = device_index;
1915 
1916 		/*
1917 		 * We ignore any set-address errors, hence there are
1918 		 * buggy USB devices out there that actually receive
1919 		 * the SETUP PID, but manage to set the address before
1920 		 * the STATUS stage is ACK'ed. If the device responds
1921 		 * to the subsequent get-descriptor at the new
1922 		 * address, then we know that the set-address command
1923 		 * was successful.
1924 		 */
1925 		if (err) {
1926 			DPRINTFN(0, "set address %d failed "
1927 			    "(%s, ignored)\n", udev->address,
1928 			    usbd_errstr(err));
1929 		}
1930 	} else {
1931 		/* We are not self powered */
1932 		udev->flags.self_powered = 0;
1933 
1934 		/* Set unconfigured state */
1935 		udev->curr_config_no = USB_UNCONFIG_NO;
1936 		udev->curr_config_index = USB_UNCONFIG_INDEX;
1937 
1938 		/* Setup USB descriptors */
1939 		err = (usb_temp_setup_by_index_p) (udev, usb_template);
1940 		if (err) {
1941 			DPRINTFN(0, "setting up USB template failed - "
1942 			    "usb_template(4) not loaded?\n");
1943 			goto done;
1944 		}
1945 	}
1946 	usb_set_device_state(udev, USB_STATE_ADDRESSED);
1947 
1948 	/* setup the device descriptor and the initial "wMaxPacketSize" */
1949 	err = usbd_setup_device_desc(udev, NULL);
1950 
1951 	if (err != 0) {
1952 		/* try to enumerate two more times */
1953 		err = usbd_req_re_enumerate(udev, NULL);
1954 		if (err != 0) {
1955 			err = usbd_req_re_enumerate(udev, NULL);
1956 			if (err != 0) {
1957 				goto done;
1958 			}
1959 		}
1960 	}
1961 
1962 	/*
1963 	 * Setup temporary USB attach args so that we can figure out some
1964 	 * basic quirks for this device.
1965 	 */
1966 	usb_init_attach_arg(udev, &uaa);
1967 
1968 	if (usb_test_quirk(&uaa, UQ_BUS_POWERED)) {
1969 		udev->flags.uq_bus_powered = 1;
1970 	}
1971 	if (usb_test_quirk(&uaa, UQ_NO_STRINGS)) {
1972 		udev->flags.no_strings = 1;
1973 	}
1974 
1975 	usb_get_langid(udev);
1976 
1977 	/* assume 100mA bus powered for now. Changed when configured. */
1978 	udev->power = USB_MIN_POWER;
1979 	/* fetch the vendor and product strings from the device */
1980 	usb_set_device_strings(udev);
1981 
1982 	if (udev->flags.usb_mode == USB_MODE_DEVICE) {
1983 		/* USB device mode setup is complete */
1984 		err = 0;
1985 		goto config_done;
1986 	}
1987 
1988 	/*
1989 	 * Most USB devices should attach to config index 0 by
1990 	 * default
1991 	 */
1992 	if (usb_test_quirk(&uaa, UQ_CFG_INDEX_0)) {
1993 		config_index = 0;
1994 		config_quirk = 1;
1995 	} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_1)) {
1996 		config_index = 1;
1997 		config_quirk = 1;
1998 	} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_2)) {
1999 		config_index = 2;
2000 		config_quirk = 1;
2001 	} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_3)) {
2002 		config_index = 3;
2003 		config_quirk = 1;
2004 	} else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_4)) {
2005 		config_index = 4;
2006 		config_quirk = 1;
2007 	} else {
2008 		config_index = 0;
2009 		config_quirk = 0;
2010 	}
2011 
2012 	set_config_failed = 0;
2013 repeat_set_config:
2014 
2015 	DPRINTF("setting config %u\n", config_index);
2016 
2017 	/* get the USB device configured */
2018 	err = usbd_set_config_index(udev, config_index);
2019 	if (err) {
2020 		if (udev->ddesc.bNumConfigurations != 0) {
2021 			if (!set_config_failed) {
2022 				set_config_failed = 1;
2023 				/* XXX try to re-enumerate the device */
2024 				err = usbd_req_re_enumerate(udev, NULL);
2025 				if (err == 0)
2026 					goto repeat_set_config;
2027 			}
2028 			DPRINTFN(0, "Failure selecting configuration index %u:"
2029 			    "%s, port %u, addr %u (ignored)\n",
2030 			    config_index, usbd_errstr(err), udev->port_no,
2031 			    udev->address);
2032 		}
2033 		/*
2034 		 * Some USB devices do not have any configurations. Ignore any
2035 		 * set config failures!
2036 		 */
2037 		err = 0;
2038 		goto config_done;
2039 	}
2040 	if (!config_quirk && config_index + 1 < udev->ddesc.bNumConfigurations) {
2041 		if ((udev->cdesc->bNumInterface < 2) &&
2042 		    usbd_get_no_descriptors(udev->cdesc, UDESC_ENDPOINT) == 0) {
2043 			DPRINTFN(0, "Found no endpoints, trying next config\n");
2044 			config_index++;
2045 			goto repeat_set_config;
2046 		}
2047 #if USB_HAVE_MSCTEST
2048 		if (config_index == 0 &&
2049 		    usb_test_quirk(&uaa, UQ_MSC_NO_INQUIRY) == 0) {
2050 			/*
2051 			 * Try to figure out if we have an
2052 			 * auto-install disk there:
2053 			 */
2054 			if (usb_iface_is_cdrom(udev, 0)) {
2055 				DPRINTFN(0, "Found possible auto-install "
2056 				    "disk (trying next config)\n");
2057 				config_index++;
2058 				goto repeat_set_config;
2059 			}
2060 		}
2061 #endif
2062 	}
2063 #if USB_HAVE_MSCTEST
2064 	if (set_config_failed == 0 && config_index == 0 &&
2065 	    usb_test_quirk(&uaa, UQ_MSC_NO_START_STOP) == 0 &&
2066 	    usb_test_quirk(&uaa, UQ_MSC_NO_PREVENT_ALLOW) == 0 &&
2067 	    usb_test_quirk(&uaa, UQ_MSC_NO_SYNC_CACHE) == 0 &&
2068 	    usb_test_quirk(&uaa, UQ_MSC_NO_TEST_UNIT_READY) == 0 &&
2069 	    usb_test_quirk(&uaa, UQ_MSC_NO_GETMAXLUN) == 0 &&
2070 	    usb_test_quirk(&uaa, UQ_MSC_NO_INQUIRY) == 0) {
2071 		/*
2072 		 * Try to figure out if there are any MSC quirks we
2073 		 * should apply automatically:
2074 		 */
2075 		err = usb_msc_auto_quirk(udev, 0, &uaa);
2076 
2077 		if (err != 0) {
2078 			set_config_failed = 1;
2079 			goto repeat_set_config;
2080 		}
2081 	}
2082 #endif
2083 
2084 config_done:
2085 	DPRINTF("new dev (addr %d), udev=%p, parent_hub=%p\n",
2086 	    udev->address, udev, udev->parent_hub);
2087 
2088 	/* register our device - we are ready */
2089 	usb_bus_port_set_device(bus, parent_hub ?
2090 	    parent_hub->hub->ports + port_index : NULL, udev, device_index);
2091 
2092 #if USB_HAVE_UGEN
2093 	/* Symlink the ugen device name */
2094 	udev->ugen_symlink = usb_alloc_symlink(udev->ugen_name);
2095 
2096 	/* Announce device */
2097 	printf("%s: <%s %s> at %s\n", udev->ugen_name,
2098 	    usb_get_manufacturer(udev), usb_get_product(udev),
2099 	    device_get_nameunit(udev->bus->bdev));
2100 #endif
2101 
2102 #if USB_HAVE_DEVCTL
2103 	usb_notify_addq("ATTACH", udev);
2104 #endif
2105 done:
2106 	if (err) {
2107 		/*
2108 		 * Free USB device and all subdevices, if any.
2109 		 */
2110 		usb_free_device(udev, 0);
2111 		udev = NULL;
2112 	}
2113 	return (udev);
2114 }
2115 
2116 #if USB_HAVE_UGEN
2117 struct usb_fs_privdata *
2118 usb_make_dev(struct usb_device *udev, const char *devname, int ep,
2119     int fi, int rwmode, uid_t uid, gid_t gid, int mode)
2120 {
2121 	struct usb_fs_privdata* pd;
2122 	struct make_dev_args args;
2123 	char buffer[32];
2124 
2125 	/* Store information to locate ourselves again later */
2126 	pd = malloc(sizeof(struct usb_fs_privdata), M_USBDEV,
2127 	    M_WAITOK | M_ZERO);
2128 	pd->bus_index = device_get_unit(udev->bus->bdev);
2129 	pd->dev_index = udev->device_index;
2130 	pd->ep_addr = ep;
2131 	pd->fifo_index = fi;
2132 	pd->mode = rwmode;
2133 
2134 	/* Now, create the device itself */
2135 	if (devname == NULL) {
2136 		devname = buffer;
2137 		snprintf(buffer, sizeof(buffer), USB_DEVICE_DIR "/%u.%u.%u",
2138 		    pd->bus_index, pd->dev_index, pd->ep_addr);
2139 	}
2140 
2141 	/* Setup arguments for make_dev_s() */
2142 	make_dev_args_init(&args);
2143 	args.mda_devsw = &usb_devsw;
2144 	args.mda_uid = uid;
2145 	args.mda_gid = gid;
2146 	args.mda_mode = mode;
2147 	args.mda_si_drv1 = pd;
2148 
2149 	if (make_dev_s(&args, &pd->cdev, "%s", devname) != 0) {
2150 		DPRINTFN(0, "Failed to create device %s\n", devname);
2151 		free(pd, M_USBDEV);
2152 		return (NULL);
2153 	}
2154 	return (pd);
2155 }
2156 
2157 void
2158 usb_destroy_dev_sync(struct usb_fs_privdata *pd)
2159 {
2160 	DPRINTFN(1, "Destroying device at ugen%d.%d\n",
2161 	    pd->bus_index, pd->dev_index);
2162 
2163 	/*
2164 	 * Destroy character device synchronously. After this
2165 	 * all system calls are returned. Can block.
2166 	 */
2167 	destroy_dev(pd->cdev);
2168 
2169 	free(pd, M_USBDEV);
2170 }
2171 
2172 void
2173 usb_destroy_dev(struct usb_fs_privdata *pd)
2174 {
2175 	struct usb_bus *bus;
2176 
2177 	if (pd == NULL)
2178 		return;
2179 
2180 	mtx_lock(&usb_ref_lock);
2181 	bus = devclass_get_softc(usb_devclass_ptr, pd->bus_index);
2182 	mtx_unlock(&usb_ref_lock);
2183 
2184 	if (bus == NULL) {
2185 		usb_destroy_dev_sync(pd);
2186 		return;
2187 	}
2188 
2189 	/* make sure we can re-use the device name */
2190 	delist_dev(pd->cdev);
2191 
2192 	USB_BUS_LOCK(bus);
2193 	LIST_INSERT_HEAD(&bus->pd_cleanup_list, pd, pd_next);
2194 	/* get cleanup going */
2195 	usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
2196 	    &bus->cleanup_msg[0], &bus->cleanup_msg[1]);
2197 	USB_BUS_UNLOCK(bus);
2198 }
2199 
2200 static void
2201 usb_cdev_create(struct usb_device *udev)
2202 {
2203 	struct usb_config_descriptor *cd;
2204 	struct usb_endpoint_descriptor *ed;
2205 	struct usb_descriptor *desc;
2206 	struct usb_fs_privdata* pd;
2207 	int inmode, outmode, inmask, outmask, mode;
2208 	uint8_t ep;
2209 
2210 	KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("stale cdev entries"));
2211 
2212 	DPRINTFN(2, "Creating device nodes\n");
2213 
2214 	if (usbd_get_mode(udev) == USB_MODE_DEVICE) {
2215 		inmode = FWRITE;
2216 		outmode = FREAD;
2217 	} else {		 /* USB_MODE_HOST */
2218 		inmode = FREAD;
2219 		outmode = FWRITE;
2220 	}
2221 
2222 	inmask = 0;
2223 	outmask = 0;
2224 	desc = NULL;
2225 
2226 	/*
2227 	 * Collect all used endpoint numbers instead of just
2228 	 * generating 16 static endpoints.
2229 	 */
2230 	cd = usbd_get_config_descriptor(udev);
2231 	while ((desc = usb_desc_foreach(cd, desc))) {
2232 		/* filter out all endpoint descriptors */
2233 		if ((desc->bDescriptorType == UDESC_ENDPOINT) &&
2234 		    (desc->bLength >= sizeof(*ed))) {
2235 			ed = (struct usb_endpoint_descriptor *)desc;
2236 
2237 			/* update masks */
2238 			ep = ed->bEndpointAddress;
2239 			if (UE_GET_DIR(ep)  == UE_DIR_OUT)
2240 				outmask |= 1 << UE_GET_ADDR(ep);
2241 			else
2242 				inmask |= 1 << UE_GET_ADDR(ep);
2243 		}
2244 	}
2245 
2246 	/* Create all available endpoints except EP0 */
2247 	for (ep = 1; ep < 16; ep++) {
2248 		mode = (inmask & (1 << ep)) ? inmode : 0;
2249 		mode |= (outmask & (1 << ep)) ? outmode : 0;
2250 		if (mode == 0)
2251 			continue;	/* no IN or OUT endpoint */
2252 
2253 		pd = usb_make_dev(udev, NULL, ep, 0,
2254 		    mode, UID_ROOT, GID_OPERATOR, 0600);
2255 
2256 		if (pd != NULL)
2257 			LIST_INSERT_HEAD(&udev->pd_list, pd, pd_next);
2258 	}
2259 }
2260 
2261 static void
2262 usb_cdev_free(struct usb_device *udev)
2263 {
2264 	struct usb_fs_privdata* pd;
2265 
2266 	DPRINTFN(2, "Freeing device nodes\n");
2267 
2268 	while ((pd = LIST_FIRST(&udev->pd_list)) != NULL) {
2269 		KASSERT(pd->cdev->si_drv1 == pd, ("privdata corrupt"));
2270 
2271 		LIST_REMOVE(pd, pd_next);
2272 
2273 		usb_destroy_dev(pd);
2274 	}
2275 }
2276 #endif
2277 
2278 /*------------------------------------------------------------------------*
2279  *	usb_free_device
2280  *
2281  * This function is NULL safe and will free an USB device and its
2282  * children devices, if any.
2283  *
2284  * Flag values: Reserved, set to zero.
2285  *------------------------------------------------------------------------*/
2286 void
2287 usb_free_device(struct usb_device *udev, uint8_t flag)
2288 {
2289 	struct usb_bus *bus;
2290 
2291 	if (udev == NULL)
2292 		return;		/* already freed */
2293 
2294 	DPRINTFN(4, "udev=%p port=%d\n", udev, udev->port_no);
2295 
2296 	bus = udev->bus;
2297 
2298 	/* set DETACHED state to prevent any further references */
2299 	usb_set_device_state(udev, USB_STATE_DETACHED);
2300 
2301 #if USB_HAVE_DEVCTL
2302 	usb_notify_addq("DETACH", udev);
2303 #endif
2304 
2305 #if USB_HAVE_UGEN
2306 	if (!rebooting) {
2307 		printf("%s: <%s %s> at %s (disconnected)\n", udev->ugen_name,
2308 		    usb_get_manufacturer(udev), usb_get_product(udev),
2309 		    device_get_nameunit(bus->bdev));
2310 	}
2311 
2312 	/* Destroy UGEN symlink, if any */
2313 	if (udev->ugen_symlink) {
2314 		usb_free_symlink(udev->ugen_symlink);
2315 		udev->ugen_symlink = NULL;
2316 	}
2317 
2318 	usb_destroy_dev(udev->ctrl_dev);
2319 #endif
2320 
2321 	if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2322 		/* stop receiving any control transfers (Device Side Mode) */
2323 		usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
2324 	}
2325 
2326 	/* the following will get the device unconfigured in software */
2327 	usb_unconfigure(udev, USB_UNCFG_FLAG_FREE_EP0);
2328 
2329 	/* final device unregister after all character devices are closed */
2330 	usb_bus_port_set_device(bus, udev->parent_hub ?
2331 	    udev->parent_hub->hub->ports + udev->port_index : NULL,
2332 	    NULL, USB_ROOT_HUB_ADDR);
2333 
2334 	/* unsetup any leftover default USB transfers */
2335 	usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
2336 
2337 	/* template unsetup, if any */
2338 	(usb_temp_unsetup_p) (udev);
2339 
2340 	/*
2341 	 * Make sure that our clear-stall messages are not queued
2342 	 * anywhere:
2343 	 */
2344 	USB_BUS_LOCK(udev->bus);
2345 	usb_proc_mwait(USB_BUS_CS_PROC(udev->bus),
2346 	    &udev->cs_msg[0], &udev->cs_msg[1]);
2347 	USB_BUS_UNLOCK(udev->bus);
2348 
2349 	/* wait for all references to go away */
2350 	usb_wait_pending_refs(udev);
2351 
2352 	sx_destroy(&udev->enum_sx);
2353 	sx_destroy(&udev->sr_sx);
2354 	sx_destroy(&udev->ctrl_sx);
2355 
2356 	cv_destroy(&udev->ctrlreq_cv);
2357 	cv_destroy(&udev->ref_cv);
2358 
2359 	mtx_destroy(&udev->device_mtx);
2360 #if USB_HAVE_UGEN
2361 	KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("leaked cdev entries"));
2362 #endif
2363 
2364 	/* Uninitialise device */
2365 	if (bus->methods->device_uninit != NULL)
2366 		(bus->methods->device_uninit) (udev);
2367 
2368 	/* free device */
2369 	free(udev->serial, M_USB);
2370 	free(udev->manufacturer, M_USB);
2371 	free(udev->product, M_USB);
2372 	free(udev, M_USB);
2373 }
2374 
2375 /*------------------------------------------------------------------------*
2376  *	usbd_get_iface
2377  *
2378  * This function is the safe way to get the USB interface structure
2379  * pointer by interface index.
2380  *
2381  * Return values:
2382  *   NULL: Interface not present.
2383  *   Else: Pointer to USB interface structure.
2384  *------------------------------------------------------------------------*/
2385 struct usb_interface *
2386 usbd_get_iface(struct usb_device *udev, uint8_t iface_index)
2387 {
2388 	struct usb_interface *iface = udev->ifaces + iface_index;
2389 
2390 	if (iface_index >= udev->ifaces_max)
2391 		return (NULL);
2392 	return (iface);
2393 }
2394 
2395 /*------------------------------------------------------------------------*
2396  *	usbd_find_descriptor
2397  *
2398  * This function will lookup the first descriptor that matches the
2399  * criteria given by the arguments "type" and "subtype". Descriptors
2400  * will only be searched within the interface having the index
2401  * "iface_index".  If the "id" argument points to an USB descriptor,
2402  * it will be skipped before the search is started. This allows
2403  * searching for multiple descriptors using the same criteria. Else
2404  * the search is started after the interface descriptor.
2405  *
2406  * Return values:
2407  *   NULL: End of descriptors
2408  *   Else: A descriptor matching the criteria
2409  *------------------------------------------------------------------------*/
2410 void   *
2411 usbd_find_descriptor(struct usb_device *udev, void *id, uint8_t iface_index,
2412     uint8_t type, uint8_t type_mask,
2413     uint8_t subtype, uint8_t subtype_mask)
2414 {
2415 	struct usb_descriptor *desc;
2416 	struct usb_config_descriptor *cd;
2417 	struct usb_interface *iface;
2418 
2419 	cd = usbd_get_config_descriptor(udev);
2420 	if (cd == NULL) {
2421 		return (NULL);
2422 	}
2423 	if (id == NULL) {
2424 		iface = usbd_get_iface(udev, iface_index);
2425 		if (iface == NULL) {
2426 			return (NULL);
2427 		}
2428 		id = usbd_get_interface_descriptor(iface);
2429 		if (id == NULL) {
2430 			return (NULL);
2431 		}
2432 	}
2433 	desc = (void *)id;
2434 
2435 	while ((desc = usb_desc_foreach(cd, desc))) {
2436 		if (desc->bDescriptorType == UDESC_INTERFACE) {
2437 			break;
2438 		}
2439 		if (((desc->bDescriptorType & type_mask) == type) &&
2440 		    ((desc->bDescriptorSubtype & subtype_mask) == subtype)) {
2441 			return (desc);
2442 		}
2443 	}
2444 	return (NULL);
2445 }
2446 
2447 /*------------------------------------------------------------------------*
2448  *	usb_devinfo
2449  *
2450  * This function will dump information from the device descriptor
2451  * belonging to the USB device pointed to by "udev", to the string
2452  * pointed to by "dst_ptr" having a maximum length of "dst_len" bytes
2453  * including the terminating zero.
2454  *------------------------------------------------------------------------*/
2455 void
2456 usb_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len)
2457 {
2458 	struct usb_device_descriptor *udd = &udev->ddesc;
2459 	uint16_t bcdDevice;
2460 	uint16_t bcdUSB;
2461 
2462 	bcdUSB = UGETW(udd->bcdUSB);
2463 	bcdDevice = UGETW(udd->bcdDevice);
2464 
2465 	if (udd->bDeviceClass != 0xFF) {
2466 		snprintf(dst_ptr, dst_len, "%s %s, class %d/%d, rev %x.%02x/"
2467 		    "%x.%02x, addr %d",
2468 		    usb_get_manufacturer(udev),
2469 		    usb_get_product(udev),
2470 		    udd->bDeviceClass, udd->bDeviceSubClass,
2471 		    (bcdUSB >> 8), bcdUSB & 0xFF,
2472 		    (bcdDevice >> 8), bcdDevice & 0xFF,
2473 		    udev->address);
2474 	} else {
2475 		snprintf(dst_ptr, dst_len, "%s %s, rev %x.%02x/"
2476 		    "%x.%02x, addr %d",
2477 		    usb_get_manufacturer(udev),
2478 		    usb_get_product(udev),
2479 		    (bcdUSB >> 8), bcdUSB & 0xFF,
2480 		    (bcdDevice >> 8), bcdDevice & 0xFF,
2481 		    udev->address);
2482 	}
2483 }
2484 
2485 #ifdef USB_VERBOSE
2486 /*
2487  * Descriptions of of known vendors and devices ("products").
2488  */
2489 struct usb_knowndev {
2490 	uint16_t vendor;
2491 	uint16_t product;
2492 	uint32_t flags;
2493 	const char *vendorname;
2494 	const char *productname;
2495 };
2496 
2497 #define	USB_KNOWNDEV_NOPROD	0x01	/* match on vendor only */
2498 
2499 #include "usbdevs.h"
2500 #include "usbdevs_data.h"
2501 #endif					/* USB_VERBOSE */
2502 
2503 void
2504 usb_set_device_strings(struct usb_device *udev)
2505 {
2506 	struct usb_device_descriptor *udd = &udev->ddesc;
2507 #ifdef USB_VERBOSE
2508 	const struct usb_knowndev *kdp;
2509 #endif
2510 	char *temp_ptr;
2511 	size_t temp_size;
2512 	uint16_t vendor_id;
2513 	uint16_t product_id;
2514 	uint8_t do_unlock;
2515 
2516 	/* Protect scratch area */
2517 	do_unlock = usbd_ctrl_lock(udev);
2518 
2519 	temp_ptr = (char *)udev->scratch.data;
2520 	temp_size = sizeof(udev->scratch.data);
2521 
2522 	vendor_id = UGETW(udd->idVendor);
2523 	product_id = UGETW(udd->idProduct);
2524 
2525 	/* cleanup old strings, if any */
2526 	free(udev->serial, M_USB);
2527 	free(udev->manufacturer, M_USB);
2528 	free(udev->product, M_USB);
2529 
2530 	/* zero the string pointers */
2531 	udev->serial = NULL;
2532 	udev->manufacturer = NULL;
2533 	udev->product = NULL;
2534 
2535 	/* get serial number string */
2536 	usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2537 	    udev->ddesc.iSerialNumber);
2538 	udev->serial = strdup(temp_ptr, M_USB);
2539 
2540 	/* get manufacturer string */
2541 	usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2542 	    udev->ddesc.iManufacturer);
2543 	usb_trim_spaces(temp_ptr);
2544 	if (temp_ptr[0] != '\0')
2545 		udev->manufacturer = strdup(temp_ptr, M_USB);
2546 
2547 	/* get product string */
2548 	usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2549 	    udev->ddesc.iProduct);
2550 	usb_trim_spaces(temp_ptr);
2551 	if (temp_ptr[0] != '\0')
2552 		udev->product = strdup(temp_ptr, M_USB);
2553 
2554 #ifdef USB_VERBOSE
2555 	if (udev->manufacturer == NULL || udev->product == NULL) {
2556 		for (kdp = usb_knowndevs; kdp->vendorname != NULL; kdp++) {
2557 			if (kdp->vendor == vendor_id &&
2558 			    (kdp->product == product_id ||
2559 			    (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
2560 				break;
2561 		}
2562 		if (kdp->vendorname != NULL) {
2563 			/* XXX should use pointer to knowndevs string */
2564 			if (udev->manufacturer == NULL) {
2565 				udev->manufacturer = strdup(kdp->vendorname,
2566 				    M_USB);
2567 			}
2568 			if (udev->product == NULL &&
2569 			    (kdp->flags & USB_KNOWNDEV_NOPROD) == 0) {
2570 				udev->product = strdup(kdp->productname,
2571 				    M_USB);
2572 			}
2573 		}
2574 	}
2575 #endif
2576 	/* Provide default strings if none were found */
2577 	if (udev->manufacturer == NULL) {
2578 		snprintf(temp_ptr, temp_size, "vendor 0x%04x", vendor_id);
2579 		udev->manufacturer = strdup(temp_ptr, M_USB);
2580 	}
2581 	if (udev->product == NULL) {
2582 		snprintf(temp_ptr, temp_size, "product 0x%04x", product_id);
2583 		udev->product = strdup(temp_ptr, M_USB);
2584 	}
2585 
2586 	if (do_unlock)
2587 		usbd_ctrl_unlock(udev);
2588 }
2589 
2590 /*
2591  * Returns:
2592  * See: USB_MODE_XXX
2593  */
2594 enum usb_hc_mode
2595 usbd_get_mode(struct usb_device *udev)
2596 {
2597 	return (udev->flags.usb_mode);
2598 }
2599 
2600 /*
2601  * Returns:
2602  * See: USB_SPEED_XXX
2603  */
2604 enum usb_dev_speed
2605 usbd_get_speed(struct usb_device *udev)
2606 {
2607 	return (udev->speed);
2608 }
2609 
2610 uint32_t
2611 usbd_get_isoc_fps(struct usb_device *udev)
2612 {
2613 	;				/* indent fix */
2614 	switch (udev->speed) {
2615 	case USB_SPEED_LOW:
2616 	case USB_SPEED_FULL:
2617 		return (1000);
2618 	default:
2619 		return (8000);
2620 	}
2621 }
2622 
2623 struct usb_device_descriptor *
2624 usbd_get_device_descriptor(struct usb_device *udev)
2625 {
2626 	if (udev == NULL)
2627 		return (NULL);		/* be NULL safe */
2628 	return (&udev->ddesc);
2629 }
2630 
2631 struct usb_config_descriptor *
2632 usbd_get_config_descriptor(struct usb_device *udev)
2633 {
2634 	if (udev == NULL)
2635 		return (NULL);		/* be NULL safe */
2636 	return (udev->cdesc);
2637 }
2638 
2639 /*------------------------------------------------------------------------*
2640  *	usb_test_quirk - test a device for a given quirk
2641  *
2642  * Return values:
2643  * 0: The USB device does not have the given quirk.
2644  * Else: The USB device has the given quirk.
2645  *------------------------------------------------------------------------*/
2646 uint8_t
2647 usb_test_quirk(const struct usb_attach_arg *uaa, uint16_t quirk)
2648 {
2649 	uint8_t found;
2650 	uint8_t x;
2651 
2652 	if (quirk == UQ_NONE)
2653 		return (0);
2654 
2655 	/* search the automatic per device quirks first */
2656 
2657 	for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) {
2658 		if (uaa->device->autoQuirk[x] == quirk)
2659 			return (1);
2660 	}
2661 
2662 	/* search global quirk table, if any */
2663 
2664 	found = (usb_test_quirk_p) (&uaa->info, quirk);
2665 
2666 	return (found);
2667 }
2668 
2669 struct usb_interface_descriptor *
2670 usbd_get_interface_descriptor(struct usb_interface *iface)
2671 {
2672 	if (iface == NULL)
2673 		return (NULL);		/* be NULL safe */
2674 	return (iface->idesc);
2675 }
2676 
2677 uint8_t
2678 usbd_get_interface_altindex(struct usb_interface *iface)
2679 {
2680 	return (iface->alt_index);
2681 }
2682 
2683 uint8_t
2684 usbd_get_bus_index(struct usb_device *udev)
2685 {
2686 	return ((uint8_t)device_get_unit(udev->bus->bdev));
2687 }
2688 
2689 uint8_t
2690 usbd_get_device_index(struct usb_device *udev)
2691 {
2692 	return (udev->device_index);
2693 }
2694 
2695 #if USB_HAVE_DEVCTL
2696 static void
2697 usb_notify_addq(const char *type, struct usb_device *udev)
2698 {
2699 	struct usb_interface *iface;
2700 	struct sbuf *sb;
2701 	int i;
2702 
2703 	/* announce the device */
2704 	sb = sbuf_new_auto();
2705 	sbuf_printf(sb,
2706 #if USB_HAVE_UGEN
2707 	    "ugen=%s "
2708 	    "cdev=%s "
2709 #endif
2710 	    "vendor=0x%04x "
2711 	    "product=0x%04x "
2712 	    "devclass=0x%02x "
2713 	    "devsubclass=0x%02x "
2714 	    "sernum=\"%s\" "
2715 	    "release=0x%04x "
2716 	    "mode=%s "
2717 	    "port=%u "
2718 #if USB_HAVE_UGEN
2719 	    "parent=%s"
2720 #endif
2721 	    "",
2722 #if USB_HAVE_UGEN
2723 	    udev->ugen_name,
2724 	    udev->ugen_name,
2725 #endif
2726 	    UGETW(udev->ddesc.idVendor),
2727 	    UGETW(udev->ddesc.idProduct),
2728 	    udev->ddesc.bDeviceClass,
2729 	    udev->ddesc.bDeviceSubClass,
2730 	    usb_get_serial(udev),
2731 	    UGETW(udev->ddesc.bcdDevice),
2732 	    (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
2733 	    udev->port_no
2734 #if USB_HAVE_UGEN
2735 	    , udev->parent_hub != NULL ?
2736 		udev->parent_hub->ugen_name :
2737 		device_get_nameunit(device_get_parent(udev->bus->bdev))
2738 #endif
2739 	    );
2740 	sbuf_finish(sb);
2741 	devctl_notify("USB", "DEVICE", type, sbuf_data(sb));
2742 	sbuf_delete(sb);
2743 
2744 	/* announce each interface */
2745 	for (i = 0; i < USB_IFACE_MAX; i++) {
2746 		iface = usbd_get_iface(udev, i);
2747 		if (iface == NULL)
2748 			break;		/* end of interfaces */
2749 		if (iface->idesc == NULL)
2750 			continue;	/* no interface descriptor */
2751 
2752 		sb = sbuf_new_auto();
2753 		sbuf_printf(sb,
2754 #if USB_HAVE_UGEN
2755 		    "ugen=%s "
2756 		    "cdev=%s "
2757 #endif
2758 		    "vendor=0x%04x "
2759 		    "product=0x%04x "
2760 		    "devclass=0x%02x "
2761 		    "devsubclass=0x%02x "
2762 		    "sernum=\"%s\" "
2763 		    "release=0x%04x "
2764 		    "mode=%s "
2765 		    "interface=%d "
2766 		    "endpoints=%d "
2767 		    "intclass=0x%02x "
2768 		    "intsubclass=0x%02x "
2769 		    "intprotocol=0x%02x",
2770 #if USB_HAVE_UGEN
2771 		    udev->ugen_name,
2772 		    udev->ugen_name,
2773 #endif
2774 		    UGETW(udev->ddesc.idVendor),
2775 		    UGETW(udev->ddesc.idProduct),
2776 		    udev->ddesc.bDeviceClass,
2777 		    udev->ddesc.bDeviceSubClass,
2778 		    usb_get_serial(udev),
2779 		    UGETW(udev->ddesc.bcdDevice),
2780 		    (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
2781 		    iface->idesc->bInterfaceNumber,
2782 		    iface->idesc->bNumEndpoints,
2783 		    iface->idesc->bInterfaceClass,
2784 		    iface->idesc->bInterfaceSubClass,
2785 		    iface->idesc->bInterfaceProtocol);
2786 		sbuf_finish(sb);
2787 		devctl_notify("USB", "INTERFACE", type, sbuf_data(sb));
2788 		sbuf_delete(sb);
2789 	}
2790 }
2791 #endif
2792 
2793 #if USB_HAVE_UGEN
2794 /*------------------------------------------------------------------------*
2795  *	usb_fifo_free_wrap
2796  *
2797  * This function will free the FIFOs.
2798  *
2799  * Description of "flag" argument: If the USB_UNCFG_FLAG_FREE_EP0 flag
2800  * is set and "iface_index" is set to "USB_IFACE_INDEX_ANY", we free
2801  * all FIFOs. If the USB_UNCFG_FLAG_FREE_EP0 flag is not set and
2802  * "iface_index" is set to "USB_IFACE_INDEX_ANY", we free all non
2803  * control endpoint FIFOs. If "iface_index" is not set to
2804  * "USB_IFACE_INDEX_ANY" the flag has no effect.
2805  *------------------------------------------------------------------------*/
2806 static void
2807 usb_fifo_free_wrap(struct usb_device *udev,
2808     uint8_t iface_index, uint8_t flag)
2809 {
2810 	struct usb_fifo *f;
2811 	uint16_t i;
2812 
2813 	/*
2814 	 * Free any USB FIFOs on the given interface:
2815 	 */
2816 	for (i = 0; i != USB_FIFO_MAX; i++) {
2817 		f = udev->fifo[i];
2818 		if (f == NULL) {
2819 			continue;
2820 		}
2821 		/* Check if the interface index matches */
2822 		if (iface_index == f->iface_index) {
2823 			if (f->methods != &usb_ugen_methods) {
2824 				/*
2825 				 * Don't free any non-generic FIFOs in
2826 				 * this case.
2827 				 */
2828 				continue;
2829 			}
2830 			if ((f->dev_ep_index == 0) &&
2831 			    (f->fs_ep_max == 0)) {
2832 				/* no need to free this FIFO */
2833 				continue;
2834 			}
2835 		} else if (iface_index == USB_IFACE_INDEX_ANY) {
2836 			if ((f->methods == &usb_ugen_methods) &&
2837 			    (f->dev_ep_index == 0) &&
2838 			    (!(flag & USB_UNCFG_FLAG_FREE_EP0)) &&
2839 			    (f->fs_ep_max == 0)) {
2840 				/* no need to free this FIFO */
2841 				continue;
2842 			}
2843 		} else {
2844 			/* no need to free this FIFO */
2845 			continue;
2846 		}
2847 		/* free this FIFO */
2848 		usb_fifo_free(f);
2849 	}
2850 }
2851 #endif
2852 
2853 /*------------------------------------------------------------------------*
2854  *	usb_peer_can_wakeup
2855  *
2856  * Return values:
2857  * 0: Peer cannot do resume signalling.
2858  * Else: Peer can do resume signalling.
2859  *------------------------------------------------------------------------*/
2860 uint8_t
2861 usb_peer_can_wakeup(struct usb_device *udev)
2862 {
2863 	const struct usb_config_descriptor *cdp;
2864 
2865 	cdp = udev->cdesc;
2866 	if ((cdp != NULL) && (udev->flags.usb_mode == USB_MODE_HOST)) {
2867 		return (cdp->bmAttributes & UC_REMOTE_WAKEUP);
2868 	}
2869 	return (0);			/* not supported */
2870 }
2871 
2872 void
2873 usb_set_device_state(struct usb_device *udev, enum usb_dev_state state)
2874 {
2875 
2876 	KASSERT(state < USB_STATE_MAX, ("invalid udev state"));
2877 
2878 	DPRINTF("udev %p state %s -> %s\n", udev,
2879 	    usb_statestr(udev->state), usb_statestr(state));
2880 
2881 #if USB_HAVE_UGEN
2882 	mtx_lock(&usb_ref_lock);
2883 #endif
2884 	udev->state = state;
2885 #if USB_HAVE_UGEN
2886 	mtx_unlock(&usb_ref_lock);
2887 #endif
2888 	if (udev->bus->methods->device_state_change != NULL)
2889 		(udev->bus->methods->device_state_change) (udev);
2890 }
2891 
2892 enum usb_dev_state
2893 usb_get_device_state(struct usb_device *udev)
2894 {
2895 	if (udev == NULL)
2896 		return (USB_STATE_DETACHED);
2897 	return (udev->state);
2898 }
2899 
2900 uint8_t
2901 usbd_device_attached(struct usb_device *udev)
2902 {
2903 	return (udev->state > USB_STATE_DETACHED);
2904 }
2905 
2906 /*
2907  * The following function locks enumerating the given USB device. If
2908  * the lock is already grabbed this function returns zero. Else a
2909  * a value of one is returned.
2910  */
2911 uint8_t
2912 usbd_enum_lock(struct usb_device *udev)
2913 {
2914 	if (sx_xlocked(&udev->enum_sx))
2915 		return (0);
2916 
2917 	sx_xlock(&udev->enum_sx);
2918 	sx_xlock(&udev->sr_sx);
2919 	/*
2920 	 * NEWBUS LOCK NOTE: We should check if any parent SX locks
2921 	 * are locked before locking Giant. Else the lock can be
2922 	 * locked multiple times.
2923 	 */
2924 	bus_topo_lock();
2925 	return (1);
2926 }
2927 
2928 #if USB_HAVE_UGEN
2929 /*
2930  * This function is the same like usbd_enum_lock() except a value of
2931  * 255 is returned when a signal is pending:
2932  */
2933 uint8_t
2934 usbd_enum_lock_sig(struct usb_device *udev)
2935 {
2936 	if (sx_xlocked(&udev->enum_sx))
2937 		return (0);
2938 	if (sx_xlock_sig(&udev->enum_sx))
2939 		return (255);
2940 	if (sx_xlock_sig(&udev->sr_sx)) {
2941 		sx_xunlock(&udev->enum_sx);
2942 		return (255);
2943 	}
2944 	bus_topo_lock();
2945 	return (1);
2946 }
2947 #endif
2948 
2949 /* The following function unlocks enumerating the given USB device. */
2950 
2951 void
2952 usbd_enum_unlock(struct usb_device *udev)
2953 {
2954 	bus_topo_unlock();
2955 	sx_xunlock(&udev->enum_sx);
2956 	sx_xunlock(&udev->sr_sx);
2957 }
2958 
2959 /* The following function locks suspend and resume. */
2960 
2961 void
2962 usbd_sr_lock(struct usb_device *udev)
2963 {
2964 	sx_xlock(&udev->sr_sx);
2965 	/*
2966 	 * NEWBUS LOCK NOTE: We should check if any parent SX locks
2967 	 * are locked before locking Giant. Else the lock can be
2968 	 * locked multiple times.
2969 	 */
2970 	bus_topo_lock();
2971 }
2972 
2973 /* The following function unlocks suspend and resume. */
2974 
2975 void
2976 usbd_sr_unlock(struct usb_device *udev)
2977 {
2978 	bus_topo_unlock();
2979 	sx_xunlock(&udev->sr_sx);
2980 }
2981 
2982 /*
2983  * The following function checks the enumerating lock for the given
2984  * USB device.
2985  */
2986 
2987 uint8_t
2988 usbd_enum_is_locked(struct usb_device *udev)
2989 {
2990 	return (sx_xlocked(&udev->enum_sx));
2991 }
2992 
2993 /*
2994  * The following function is used to serialize access to USB control
2995  * transfers and the USB scratch area. If the lock is already grabbed
2996  * this function returns zero. Else a value of one is returned.
2997  */
2998 uint8_t
2999 usbd_ctrl_lock(struct usb_device *udev)
3000 {
3001 	if (sx_xlocked(&udev->ctrl_sx))
3002 		return (0);
3003 	sx_xlock(&udev->ctrl_sx);
3004 
3005 	/*
3006 	 * We need to allow suspend and resume at this point, else the
3007 	 * control transfer will timeout if the device is suspended!
3008 	 */
3009 	if (usbd_enum_is_locked(udev))
3010 		usbd_sr_unlock(udev);
3011 	return (1);
3012 }
3013 
3014 void
3015 usbd_ctrl_unlock(struct usb_device *udev)
3016 {
3017 	sx_xunlock(&udev->ctrl_sx);
3018 
3019 	/*
3020 	 * Restore the suspend and resume lock after we have unlocked
3021 	 * the USB control transfer lock to avoid LOR:
3022 	 */
3023 	if (usbd_enum_is_locked(udev))
3024 		usbd_sr_lock(udev);
3025 }
3026 
3027 /*
3028  * The following function is used to set the per-interface specific
3029  * plug and play information. The string referred to by the pnpinfo
3030  * argument can safely be freed after calling this function. The
3031  * pnpinfo of an interface will be reset at device detach or when
3032  * passing a NULL argument to this function. This function
3033  * returns zero on success, else a USB_ERR_XXX failure code.
3034  */
3035 
3036 usb_error_t
3037 usbd_set_pnpinfo(struct usb_device *udev, uint8_t iface_index, const char *pnpinfo)
3038 {
3039 	struct usb_interface *iface;
3040 
3041 	iface = usbd_get_iface(udev, iface_index);
3042 	if (iface == NULL)
3043 		return (USB_ERR_INVAL);
3044 
3045 	if (iface->pnpinfo != NULL) {
3046 		free(iface->pnpinfo, M_USBDEV);
3047 		iface->pnpinfo = NULL;
3048 	}
3049 
3050 	if (pnpinfo == NULL || pnpinfo[0] == 0)
3051 		return (0);		/* success */
3052 
3053 	iface->pnpinfo = strdup(pnpinfo, M_USBDEV);
3054 	if (iface->pnpinfo == NULL)
3055 		return (USB_ERR_NOMEM);
3056 
3057 	return (0);			/* success */
3058 }
3059 
3060 usb_error_t
3061 usbd_add_dynamic_quirk(struct usb_device *udev, uint16_t quirk)
3062 {
3063 	uint8_t x;
3064 
3065 	for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) {
3066 		if (udev->autoQuirk[x] == 0 ||
3067 		    udev->autoQuirk[x] == quirk) {
3068 			udev->autoQuirk[x] = quirk;
3069 			return (0);	/* success */
3070 		}
3071 	}
3072 	return (USB_ERR_NOMEM);
3073 }
3074 
3075 /*
3076  * The following function is used to select the endpoint mode. It
3077  * should not be called outside enumeration context.
3078  */
3079 
3080 usb_error_t
3081 usbd_set_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep,
3082     uint8_t ep_mode)
3083 {
3084 	usb_error_t error;
3085 	uint8_t do_unlock;
3086 
3087 	/* Prevent re-enumeration */
3088 	do_unlock = usbd_enum_lock(udev);
3089 
3090 	if (udev->bus->methods->set_endpoint_mode != NULL) {
3091 		error = (udev->bus->methods->set_endpoint_mode) (
3092 		    udev, ep, ep_mode);
3093 	} else if (ep_mode != USB_EP_MODE_DEFAULT) {
3094 		error = USB_ERR_INVAL;
3095 	} else {
3096 		error = 0;
3097 	}
3098 
3099 	/* only set new mode regardless of error */
3100 	ep->ep_mode = ep_mode;
3101 
3102 	if (do_unlock)
3103 		usbd_enum_unlock(udev);
3104 	return (error);
3105 }
3106 
3107 uint8_t
3108 usbd_get_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep)
3109 {
3110 	return (ep->ep_mode);
3111 }
3112