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