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