3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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>
35 #include <sys/module.h>
37 #include <sys/mutex.h>
38 #include <sys/condvar.h>
39 #include <sys/sysctl.h>
41 #include <sys/unistd.h>
42 #include <sys/callout.h>
43 #include <sys/malloc.h>
46 #include <sys/fcntl.h>
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>
59 #define USB_DEBUG_VAR usb_debug
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>
73 #include <dev/usb/usb_dev.h>
74 #include <dev/usb/usb_generic.h>
77 #include <dev/usb/quirk/usb_quirk.h>
79 #include <dev/usb/usb_controller.h>
80 #include <dev/usb/usb_bus.h>
82 /* function prototypes */
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 *,
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,
97 static usb_proc_callback_t usbd_clear_stall_proc;
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 *);
101 static void usb_notify_addq(const char *type, struct usb_device *);
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 *);
109 /* This variable is global to allow easy access to it: */
111 int usb_template = 0;
113 TUNABLE_INT("hw.usb.usb_template", &usb_template);
114 SYSCTL_INT(_hw_usb, OID_AUTO, template, CTLFLAG_RW | CTLFLAG_TUN,
115 &usb_template, 0, "Selected USB device side template");
117 /* English is default language */
119 static int usb_lang_id = 0x0009;
120 static int usb_lang_mask = 0x00FF;
122 TUNABLE_INT("hw.usb.usb_lang_id", &usb_lang_id);
123 SYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_id, CTLFLAG_RW | CTLFLAG_TUN,
124 &usb_lang_id, 0, "Preferred USB language ID");
126 TUNABLE_INT("hw.usb.usb_lang_mask", &usb_lang_mask);
127 SYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_mask, CTLFLAG_RW | CTLFLAG_TUN,
128 &usb_lang_mask, 0, "Preferred USB language mask");
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",
139 usb_statestr(enum usb_dev_state state)
141 return ((state < USB_STATE_MAX) ? statestr[state] : "UNKNOWN");
145 usb_get_manufacturer(struct usb_device *udev)
147 return (udev->manufacturer ? udev->manufacturer : "Unknown");
151 usb_get_product(struct usb_device *udev)
153 return (udev->product ? udev->product : "");
157 usb_get_serial(struct usb_device *udev)
159 return (udev->serial ? udev->serial : "");
162 /*------------------------------------------------------------------------*
163 * usbd_get_ep_by_addr
165 * This function searches for an USB ep by endpoint address and
171 *------------------------------------------------------------------------*/
172 struct usb_endpoint *
173 usbd_get_ep_by_addr(struct usb_device *udev, uint8_t ea_val)
175 struct usb_endpoint *ep = udev->endpoints;
176 struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max;
178 EA_MASK = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR),
182 * According to the USB specification not all bits are used
183 * for the endpoint address. Keep defined bits only:
188 * Iterate accross all the USB endpoints searching for a match
189 * based on the endpoint address:
191 for (; ep != ep_end; ep++) {
193 if (ep->edesc == NULL) {
196 /* do the mask and check the value */
197 if ((ep->edesc->bEndpointAddress & EA_MASK) == ea_val) {
203 * The default endpoint is always present and is checked separately:
205 if ((udev->ctrl_ep.edesc) &&
206 ((udev->ctrl_ep.edesc->bEndpointAddress & EA_MASK) == ea_val)) {
216 /*------------------------------------------------------------------------*
219 * This function searches for an USB endpoint based on the information
220 * given by the passed "struct usb_config" pointer.
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)
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;
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);
245 if (setup->usb_mode != USB_MODE_DUAL &&
246 udev->flags.usb_mode != setup->usb_mode) {
247 /* wrong mode - no endpoint */
251 /* setup expected endpoint direction mask and value */
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 */
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));
271 /* setup expected endpoint address */
273 if (setup->endpoint == UE_ADDR_ANY) {
274 /* match any endpoint address */
276 /* match the given endpoint address */
278 ea_val |= (setup->endpoint & UE_ADDR);
281 /* setup expected endpoint type */
283 if (setup->type == UE_BULK_INTR) {
284 /* this will match BULK and INTERRUPT endpoints */
287 } else if (setup->type == UE_TYPE_ANY) {
288 /* match any endpoint type */
292 /* match the given endpoint type */
293 type_mask = UE_XFERTYPE;
294 type_val = (setup->type & UE_XFERTYPE);
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.
302 for (; ep != ep_end; ep++) {
304 if ((ep->edesc == NULL) ||
305 (ep->iface_index != iface_index)) {
308 /* do the masks and check the values */
310 if (((ep->edesc->bEndpointAddress & ea_mask) == ea_val) &&
311 ((ep->edesc->bmAttributes & type_mask) == type_val)) {
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:
323 if ((udev->ctrl_ep.edesc) &&
324 ((udev->ctrl_ep.edesc->bEndpointAddress & ea_mask) == ea_val) &&
325 ((udev->ctrl_ep.edesc->bmAttributes & type_mask) == type_val) &&
336 /*------------------------------------------------------------------------*
337 * usbd_interface_count
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".
346 *------------------------------------------------------------------------*/
348 usbd_interface_count(struct usb_device *udev, uint8_t *count)
350 if (udev->cdesc == NULL) {
352 return (USB_ERR_NOT_CONFIGURED);
354 *count = udev->ifaces_max;
355 return (USB_ERR_NORMAL_COMPLETION);
359 /*------------------------------------------------------------------------*
362 * This function will initialise the USB endpoint structure pointed to by
363 * the "endpoint" argument. The structure pointed to by "endpoint" must be
364 * zeroed before calling this function.
365 *------------------------------------------------------------------------*/
367 usb_init_endpoint(struct usb_device *udev, uint8_t iface_index,
368 struct usb_endpoint_descriptor *edesc,
369 struct usb_endpoint_ss_comp_descriptor *ecomp,
370 struct usb_endpoint *ep)
372 struct usb_bus_methods *methods;
374 methods = udev->bus->methods;
376 (methods->endpoint_init) (udev, edesc, ep);
378 /* initialise USB endpoint structure */
381 ep->iface_index = iface_index;
382 TAILQ_INIT(&ep->endpoint_q.head);
383 ep->endpoint_q.command = &usbd_pipe_start;
385 /* the pipe is not supported by the hardware */
386 if (ep->methods == NULL)
389 /* clear stall, if any */
390 if (methods->clear_stall != NULL) {
391 USB_BUS_LOCK(udev->bus);
392 (methods->clear_stall) (udev, ep);
393 USB_BUS_UNLOCK(udev->bus);
397 /*-----------------------------------------------------------------------*
398 * usb_endpoint_foreach
400 * This function will iterate all the USB endpoints except the control
401 * endpoint. This function is NULL safe.
404 * NULL: End of USB endpoints
405 * Else: Pointer to next USB endpoint
406 *------------------------------------------------------------------------*/
407 struct usb_endpoint *
408 usb_endpoint_foreach(struct usb_device *udev, struct usb_endpoint *ep)
410 struct usb_endpoint *ep_end;
416 ep_end = udev->endpoints + udev->endpoints_max;
418 /* get next endpoint */
420 ep = udev->endpoints;
424 /* find next allocated ep */
425 while (ep != ep_end) {
426 if (ep->edesc != NULL)
433 /*------------------------------------------------------------------------*
434 * usb_wait_pending_refs
436 * This function will wait for any USB references to go away before
437 * returning. This function is used before freeing a USB device.
438 *------------------------------------------------------------------------*/
440 usb_wait_pending_refs(struct usb_device *udev)
443 DPRINTF("Refcount = %d\n", (int)udev->refcount);
445 mtx_lock(&usb_ref_lock);
448 /* wait for any pending references to go away */
449 if (udev->refcount == 0) {
450 /* prevent further refs being taken, if any */
451 udev->refcount = USB_DEV_REF_MAX;
454 cv_wait(&udev->ref_cv, &usb_ref_lock);
456 mtx_unlock(&usb_ref_lock);
460 /*------------------------------------------------------------------------*
463 * This function will free all USB interfaces and USB endpoints belonging
466 * Flag values, see "USB_UNCFG_FLAG_XXX".
467 *------------------------------------------------------------------------*/
469 usb_unconfigure(struct usb_device *udev, uint8_t flag)
473 /* Prevent re-enumeration */
474 do_unlock = usbd_enum_lock(udev);
476 /* detach all interface drivers */
477 usb_detach_device(udev, USB_IFACE_INDEX_ANY, flag);
480 /* free all FIFOs except control endpoint FIFOs */
481 usb_fifo_free_wrap(udev, USB_IFACE_INDEX_ANY, flag);
484 * Free all cdev's, if any.
489 #if USB_HAVE_COMPAT_LINUX
490 /* free Linux compat device, if any */
491 if (udev->linux_endpoint_start) {
492 usb_linux_free_device(udev);
493 udev->linux_endpoint_start = NULL;
497 usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_FREE);
499 /* free "cdesc" after "ifaces" and "endpoints", if any */
500 if (udev->cdesc != NULL) {
501 if (udev->flags.usb_mode != USB_MODE_DEVICE)
502 free(udev->cdesc, M_USB);
505 /* set unconfigured state */
506 udev->curr_config_no = USB_UNCONFIG_NO;
507 udev->curr_config_index = USB_UNCONFIG_INDEX;
510 usbd_enum_unlock(udev);
513 /*------------------------------------------------------------------------*
514 * usbd_set_config_index
516 * This function selects configuration by index, independent of the
517 * actual configuration number. This function should not be used by
523 *------------------------------------------------------------------------*/
525 usbd_set_config_index(struct usb_device *udev, uint8_t index)
527 struct usb_status ds;
528 struct usb_config_descriptor *cdp;
535 DPRINTFN(6, "udev=%p index=%d\n", udev, index);
537 /* Prevent re-enumeration */
538 do_unlock = usbd_enum_lock(udev);
540 usb_unconfigure(udev, 0);
542 if (index == USB_UNCONFIG_INDEX) {
544 * Leave unallocated when unconfiguring the
545 * device. "usb_unconfigure()" will also reset
546 * the current config number and index.
548 err = usbd_req_set_config(udev, NULL, USB_UNCONFIG_NO);
549 if (udev->state == USB_STATE_CONFIGURED)
550 usb_set_device_state(udev, USB_STATE_ADDRESSED);
553 /* get the full config descriptor */
554 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
555 /* save some memory */
556 err = usbd_req_get_descriptor_ptr(udev, &cdp,
557 (UDESC_CONFIG << 8) | index);
560 err = usbd_req_get_config_desc_full(udev,
561 NULL, &cdp, M_USB, index);
566 /* set the new config descriptor */
570 /* Figure out if the device is self or bus powered. */
572 if ((!udev->flags.uq_bus_powered) &&
573 (cdp->bmAttributes & UC_SELF_POWERED) &&
574 (udev->flags.usb_mode == USB_MODE_HOST)) {
575 /* May be self powered. */
576 if (cdp->bmAttributes & UC_BUS_POWERED) {
577 /* Must ask device. */
578 err = usbd_req_get_device_status(udev, NULL, &ds);
580 DPRINTFN(0, "could not read "
581 "device status: %s\n",
583 } else if (UGETW(ds.wStatus) & UDS_SELF_POWERED) {
586 DPRINTF("status=0x%04x \n",
591 DPRINTF("udev=%p cdesc=%p (addr %d) cno=%d attr=0x%02x, "
592 "selfpowered=%d, power=%d\n",
594 udev->address, cdp->bConfigurationValue, cdp->bmAttributes,
595 selfpowered, cdp->bMaxPower * 2);
597 /* Check if we have enough power. */
598 power = cdp->bMaxPower * 2;
600 if (udev->parent_hub) {
601 max_power = udev->parent_hub->hub->portpower;
603 max_power = USB_MAX_POWER;
606 if (power > max_power) {
607 DPRINTFN(0, "power exceeded %d > %d\n", power, max_power);
608 err = USB_ERR_NO_POWER;
611 /* Only update "self_powered" in USB Host Mode */
612 if (udev->flags.usb_mode == USB_MODE_HOST) {
613 udev->flags.self_powered = selfpowered;
616 udev->curr_config_no = cdp->bConfigurationValue;
617 udev->curr_config_index = index;
618 usb_set_device_state(udev, USB_STATE_CONFIGURED);
620 /* Set the actual configuration value. */
621 err = usbd_req_set_config(udev, NULL, cdp->bConfigurationValue);
626 err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_ALLOC);
631 err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_INIT);
637 /* create device nodes for each endpoint */
638 usb_cdev_create(udev);
642 DPRINTF("error=%s\n", usbd_errstr(err));
644 usb_unconfigure(udev, 0);
647 usbd_enum_unlock(udev);
651 /*------------------------------------------------------------------------*
654 * This function will allocate and free USB interfaces and USB endpoints,
655 * parse the USB configuration structure and initialise the USB endpoints
656 * and interfaces. If "iface_index" is not equal to
657 * "USB_IFACE_INDEX_ANY" then the "cmd" parameter is the
658 * alternate_setting to be selected for the given interface. Else the
659 * "cmd" parameter is defined by "USB_CFG_XXX". "iface_index" can be
660 * "USB_IFACE_INDEX_ANY" or a valid USB interface index. This function
661 * is typically called when setting the configuration or when setting
662 * an alternate interface.
667 *------------------------------------------------------------------------*/
669 usb_config_parse(struct usb_device *udev, uint8_t iface_index, uint8_t cmd)
671 struct usb_idesc_parse_state ips;
672 struct usb_interface_descriptor *id;
673 struct usb_endpoint_descriptor *ed;
674 struct usb_interface *iface;
675 struct usb_endpoint *ep;
683 if (iface_index != USB_IFACE_INDEX_ANY) {
684 /* parameter overload */
694 DPRINTFN(5, "iface_index=%d cmd=%d\n",
697 if (cmd == USB_CFG_FREE)
700 if (cmd == USB_CFG_INIT) {
701 sx_assert(&udev->enum_sx, SA_LOCKED);
703 /* check for in-use endpoints */
705 ep = udev->endpoints;
706 ep_max = udev->endpoints_max;
708 /* look for matching endpoints */
709 if ((iface_index == USB_IFACE_INDEX_ANY) ||
710 (iface_index == ep->iface_index)) {
711 if (ep->refcount_alloc != 0) {
713 * This typically indicates a
714 * more serious error.
716 err = USB_ERR_IN_USE;
719 memset(ep, 0, sizeof(*ep));
720 /* make sure we don't zero the endpoint again */
721 ep->iface_index = USB_IFACE_INDEX_ANY;
731 memset(&ips, 0, sizeof(ips));
736 while ((id = usb_idesc_foreach(udev->cdesc, &ips))) {
738 /* check for interface overflow */
739 if (ips.iface_index == USB_IFACE_MAX)
742 iface = udev->ifaces + ips.iface_index;
744 /* check for specific interface match */
746 if (cmd == USB_CFG_INIT) {
747 if ((iface_index != USB_IFACE_INDEX_ANY) &&
748 (iface_index != ips.iface_index)) {
749 /* wrong interface */
751 } else if (alt_index != ips.iface_index_alt) {
752 /* wrong alternate setting */
755 /* initialise interface */
761 /* check for new interface */
762 if (ips.iface_index_alt == 0) {
763 /* update current number of endpoints */
768 /* setup the USB interface structure */
770 /* set alternate index */
771 iface->alt_index = alt_index;
772 /* set default interface parent */
773 if (iface_index == USB_IFACE_INDEX_ANY) {
774 iface->parent_iface_index =
779 DPRINTFN(5, "found idesc nendpt=%d\n", id->bNumEndpoints);
781 ed = (struct usb_endpoint_descriptor *)id;
785 /* iterate all the endpoint descriptors */
786 while ((ed = usb_edesc_foreach(udev->cdesc, ed))) {
788 if (temp == USB_EP_MAX)
791 ep = udev->endpoints + temp;
796 ecomp = usb_ed_comp_foreach(udev->cdesc, (void *)ed);
798 DPRINTFN(5, "Found endpoint companion descriptor\n");
800 usb_init_endpoint(udev,
801 ips.iface_index, ed, ecomp, ep);
806 /* find maximum number of endpoints */
811 id = (struct usb_interface_descriptor *)ed;
815 /* NOTE: It is valid to have no interfaces and no endpoints! */
817 if (cmd == USB_CFG_ALLOC) {
818 udev->ifaces_max = ips.iface_index;
820 if (udev->ifaces_max != 0) {
821 udev->ifaces = malloc(sizeof(*iface) * udev->ifaces_max,
822 M_USB, M_WAITOK | M_ZERO);
823 if (udev->ifaces == NULL) {
829 udev->endpoints = malloc(sizeof(*ep) * ep_max,
830 M_USB, M_WAITOK | M_ZERO);
831 if (udev->endpoints == NULL) {
836 udev->endpoints = NULL;
838 USB_BUS_LOCK(udev->bus);
839 udev->endpoints_max = ep_max;
840 /* reset any ongoing clear-stall */
841 udev->ep_curr = NULL;
842 USB_BUS_UNLOCK(udev->bus);
847 if (cmd == USB_CFG_ALLOC) {
849 USB_BUS_LOCK(udev->bus);
850 udev->endpoints_max = 0;
851 /* reset any ongoing clear-stall */
852 udev->ep_curr = NULL;
853 USB_BUS_UNLOCK(udev->bus);
856 if (udev->ifaces != NULL)
857 free(udev->ifaces, M_USB);
858 if (udev->endpoints != NULL)
859 free(udev->endpoints, M_USB);
862 udev->endpoints = NULL;
863 udev->ifaces_max = 0;
869 /*------------------------------------------------------------------------*
870 * usbd_set_alt_interface_index
872 * This function will select an alternate interface index for the
873 * given interface index. The interface should not be in use when this
874 * function is called. That means there should not be any open USB
875 * transfers. Else an error is returned. If the alternate setting is
876 * already set this function will simply return success. This function
877 * is called in Host mode and Device mode!
882 *------------------------------------------------------------------------*/
884 usbd_set_alt_interface_index(struct usb_device *udev,
885 uint8_t iface_index, uint8_t alt_index)
887 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
891 /* Prevent re-enumeration */
892 do_unlock = usbd_enum_lock(udev);
898 if (iface->alt_index == alt_index) {
900 * Optimise away duplicate setting of
901 * alternate setting in USB Host Mode!
908 * Free all generic FIFOs for this interface, except control
911 usb_fifo_free_wrap(udev, iface_index, 0);
914 err = usb_config_parse(udev, iface_index, alt_index);
918 if (iface->alt_index != alt_index) {
919 /* the alternate setting does not exist */
924 err = usbd_req_set_alt_interface_no(udev, NULL, iface_index,
925 iface->idesc->bAlternateSetting);
929 usbd_enum_unlock(udev);
933 /*------------------------------------------------------------------------*
934 * usbd_set_endpoint_stall
936 * This function is used to make a BULK or INTERRUPT endpoint send
937 * STALL tokens in USB device mode.
942 *------------------------------------------------------------------------*/
944 usbd_set_endpoint_stall(struct usb_device *udev, struct usb_endpoint *ep,
947 struct usb_xfer *xfer;
953 DPRINTF("Cannot find endpoint\n");
955 * Pretend that the clear or set stall request is
956 * successful else some USB host stacks can do
957 * strange things, especially when a control endpoint
962 et = (ep->edesc->bmAttributes & UE_XFERTYPE);
964 if ((et != UE_BULK) &&
965 (et != UE_INTERRUPT)) {
967 * Should not stall control
968 * nor isochronous endpoints.
970 DPRINTF("Invalid endpoint\n");
973 USB_BUS_LOCK(udev->bus);
975 /* store current stall state */
976 was_stalled = ep->is_stalled;
978 /* check for no change */
979 if (was_stalled && do_stall) {
980 /* if the endpoint is already stalled do nothing */
981 USB_BUS_UNLOCK(udev->bus);
982 DPRINTF("No change\n");
985 /* set stalled state */
988 if (do_stall || (!was_stalled)) {
990 /* lookup the current USB transfer, if any */
991 xfer = ep->endpoint_q.curr;
997 * If "xfer" is non-NULL the "set_stall" method will
998 * complete the USB transfer like in case of a timeout
999 * setting the error code "USB_ERR_STALLED".
1001 (udev->bus->methods->set_stall) (udev, xfer, ep, &do_stall);
1004 ep->toggle_next = 0; /* reset data toggle */
1005 ep->is_stalled = 0; /* clear stalled state */
1007 (udev->bus->methods->clear_stall) (udev, ep);
1009 /* start up the current or next transfer, if any */
1010 usb_command_wrapper(&ep->endpoint_q, ep->endpoint_q.curr);
1012 USB_BUS_UNLOCK(udev->bus);
1016 /*------------------------------------------------------------------------*
1017 * usb_reset_iface_endpoints - used in USB device side mode
1018 *------------------------------------------------------------------------*/
1020 usb_reset_iface_endpoints(struct usb_device *udev, uint8_t iface_index)
1022 struct usb_endpoint *ep;
1023 struct usb_endpoint *ep_end;
1025 ep = udev->endpoints;
1026 ep_end = udev->endpoints + udev->endpoints_max;
1028 for (; ep != ep_end; ep++) {
1030 if ((ep->edesc == NULL) ||
1031 (ep->iface_index != iface_index)) {
1034 /* simulate a clear stall from the peer */
1035 usbd_set_endpoint_stall(udev, ep, 0);
1040 /*------------------------------------------------------------------------*
1041 * usb_detach_device_sub
1043 * This function will try to detach an USB device. If it fails a panic
1046 * Flag values, see "USB_UNCFG_FLAG_XXX".
1047 *------------------------------------------------------------------------*/
1049 usb_detach_device_sub(struct usb_device *udev, device_t *ppdev,
1050 char **ppnpinfo, uint8_t flag)
1059 * NOTE: It is important to clear "*ppdev" before deleting
1060 * the child due to some device methods being called late
1061 * during the delete process !
1065 device_printf(dev, "at %s, port %d, addr %d "
1067 device_get_nameunit(udev->parent_dev),
1068 udev->port_no, udev->address);
1070 if (device_is_attached(dev)) {
1071 if (udev->flags.peer_suspended) {
1072 err = DEVICE_RESUME(dev);
1074 device_printf(dev, "Resume failed\n");
1078 /* detach and delete child */
1079 if (device_delete_child(udev->parent_dev, dev)) {
1084 pnpinfo = *ppnpinfo;
1085 if (pnpinfo != NULL) {
1087 free(pnpinfo, M_USBDEV);
1092 /* Detach is not allowed to fail in the USB world */
1093 panic("usb_detach_device_sub: A USB driver would not detach\n");
1096 /*------------------------------------------------------------------------*
1099 * The following function will detach the matching interfaces.
1100 * This function is NULL safe.
1102 * Flag values, see "USB_UNCFG_FLAG_XXX".
1103 *------------------------------------------------------------------------*/
1105 usb_detach_device(struct usb_device *udev, uint8_t iface_index,
1108 struct usb_interface *iface;
1115 DPRINTFN(4, "udev=%p\n", udev);
1117 sx_assert(&udev->enum_sx, SA_LOCKED);
1120 * First detach the child to give the child's detach routine a
1121 * chance to detach the sub-devices in the correct order.
1122 * Then delete the child using "device_delete_child()" which
1123 * will detach all sub-devices from the bottom and upwards!
1125 if (iface_index != USB_IFACE_INDEX_ANY) {
1127 iface_index = i + 1;
1130 iface_index = USB_IFACE_MAX;
1135 for (; i != iface_index; i++) {
1137 iface = usbd_get_iface(udev, i);
1138 if (iface == NULL) {
1139 /* looks like the end of the USB interfaces */
1142 usb_detach_device_sub(udev, &iface->subdev,
1143 &iface->pnpinfo, flag);
1147 /*------------------------------------------------------------------------*
1148 * usb_probe_and_attach_sub
1153 *------------------------------------------------------------------------*/
1155 usb_probe_and_attach_sub(struct usb_device *udev,
1156 struct usb_attach_arg *uaa)
1158 struct usb_interface *iface;
1163 if (iface->parent_iface_index != USB_IFACE_INDEX_ANY) {
1164 /* leave interface alone */
1167 dev = iface->subdev;
1170 /* clean up after module unload */
1172 if (device_is_attached(dev)) {
1173 /* already a device there */
1176 /* clear "iface->subdev" as early as possible */
1178 iface->subdev = NULL;
1180 if (device_delete_child(udev->parent_dev, dev)) {
1183 * Panic here, else one can get a double call
1184 * to device_detach(). USB devices should
1185 * never fail on detach!
1187 panic("device_delete_child() failed\n");
1190 if (uaa->temp_dev == NULL) {
1192 /* create a new child */
1193 uaa->temp_dev = device_add_child(udev->parent_dev, NULL, -1);
1194 if (uaa->temp_dev == NULL) {
1195 device_printf(udev->parent_dev,
1196 "Device creation failed\n");
1197 return (1); /* failure */
1199 device_set_ivars(uaa->temp_dev, uaa);
1200 device_quiet(uaa->temp_dev);
1203 * Set "subdev" before probe and attach so that "devd" gets
1204 * the information it needs.
1206 iface->subdev = uaa->temp_dev;
1208 if (device_probe_and_attach(iface->subdev) == 0) {
1210 * The USB attach arguments are only available during probe
1213 uaa->temp_dev = NULL;
1214 device_set_ivars(iface->subdev, NULL);
1216 if (udev->flags.peer_suspended) {
1217 err = DEVICE_SUSPEND(iface->subdev);
1219 device_printf(iface->subdev, "Suspend failed\n");
1221 return (0); /* success */
1223 /* No USB driver found */
1224 iface->subdev = NULL;
1226 return (1); /* failure */
1229 /*------------------------------------------------------------------------*
1230 * usbd_set_parent_iface
1232 * Using this function will lock the alternate interface setting on an
1233 * interface. It is typically used for multi interface drivers. In USB
1234 * device side mode it is assumed that the alternate interfaces all
1235 * have the same endpoint descriptors. The default parent index value
1236 * is "USB_IFACE_INDEX_ANY". Then the alternate setting value is not
1238 *------------------------------------------------------------------------*/
1240 usbd_set_parent_iface(struct usb_device *udev, uint8_t iface_index,
1241 uint8_t parent_index)
1243 struct usb_interface *iface;
1249 iface = usbd_get_iface(udev, iface_index);
1251 iface->parent_iface_index = parent_index;
1255 usb_init_attach_arg(struct usb_device *udev,
1256 struct usb_attach_arg *uaa)
1258 memset(uaa, 0, sizeof(*uaa));
1261 uaa->usb_mode = udev->flags.usb_mode;
1262 uaa->port = udev->port_no;
1263 uaa->dev_state = UAA_DEV_READY;
1265 uaa->info.idVendor = UGETW(udev->ddesc.idVendor);
1266 uaa->info.idProduct = UGETW(udev->ddesc.idProduct);
1267 uaa->info.bcdDevice = UGETW(udev->ddesc.bcdDevice);
1268 uaa->info.bDeviceClass = udev->ddesc.bDeviceClass;
1269 uaa->info.bDeviceSubClass = udev->ddesc.bDeviceSubClass;
1270 uaa->info.bDeviceProtocol = udev->ddesc.bDeviceProtocol;
1271 uaa->info.bConfigIndex = udev->curr_config_index;
1272 uaa->info.bConfigNum = udev->curr_config_no;
1275 /*------------------------------------------------------------------------*
1276 * usb_probe_and_attach
1278 * This function is called from "uhub_explore_sub()",
1279 * "usb_handle_set_config()" and "usb_handle_request()".
1283 * Else: A control transfer failed
1284 *------------------------------------------------------------------------*/
1286 usb_probe_and_attach(struct usb_device *udev, uint8_t iface_index)
1288 struct usb_attach_arg uaa;
1289 struct usb_interface *iface;
1295 DPRINTF("udev == NULL\n");
1296 return (USB_ERR_INVAL);
1298 /* Prevent re-enumeration */
1299 do_unlock = usbd_enum_lock(udev);
1301 if (udev->curr_config_index == USB_UNCONFIG_INDEX) {
1302 /* do nothing - no configuration has been set */
1305 /* setup USB attach arguments */
1307 usb_init_attach_arg(udev, &uaa);
1310 * If the whole USB device is targeted, invoke the USB event
1313 if (iface_index == USB_IFACE_INDEX_ANY) {
1315 EVENTHANDLER_INVOKE(usb_dev_configured, udev, &uaa);
1317 if (uaa.dev_state != UAA_DEV_READY) {
1318 /* leave device unconfigured */
1319 usb_unconfigure(udev, 0);
1324 /* Check if only one interface should be probed: */
1325 if (iface_index != USB_IFACE_INDEX_ANY) {
1333 /* Do the probe and attach */
1334 for (; i != j; i++) {
1336 iface = usbd_get_iface(udev, i);
1337 if (iface == NULL) {
1339 * Looks like the end of the USB
1342 DPRINTFN(2, "end of interfaces "
1346 if (iface->idesc == NULL) {
1347 /* no interface descriptor */
1352 uaa.info.bInterfaceClass =
1353 iface->idesc->bInterfaceClass;
1354 uaa.info.bInterfaceSubClass =
1355 iface->idesc->bInterfaceSubClass;
1356 uaa.info.bInterfaceProtocol =
1357 iface->idesc->bInterfaceProtocol;
1358 uaa.info.bIfaceIndex = i;
1359 uaa.info.bIfaceNum =
1360 iface->idesc->bInterfaceNumber;
1361 uaa.driver_info = 0; /* reset driver_info */
1363 DPRINTFN(2, "iclass=%u/%u/%u iindex=%u/%u\n",
1364 uaa.info.bInterfaceClass,
1365 uaa.info.bInterfaceSubClass,
1366 uaa.info.bInterfaceProtocol,
1367 uaa.info.bIfaceIndex,
1368 uaa.info.bIfaceNum);
1370 usb_probe_and_attach_sub(udev, &uaa);
1373 * Remove the leftover child, if any, to enforce that
1374 * a new nomatch devd event is generated for the next
1375 * interface if no driver is found:
1377 if (uaa.temp_dev == NULL)
1379 if (device_delete_child(udev->parent_dev, uaa.temp_dev))
1380 DPRINTFN(0, "device delete child failed\n");
1381 uaa.temp_dev = NULL;
1385 usbd_enum_unlock(udev);
1389 /*------------------------------------------------------------------------*
1390 * usb_suspend_resume_sub
1392 * This function is called when the suspend or resume methods should
1393 * be executed on an USB device.
1394 *------------------------------------------------------------------------*/
1396 usb_suspend_resume_sub(struct usb_device *udev, device_t dev, uint8_t do_suspend)
1403 if (!device_is_attached(dev)) {
1407 err = DEVICE_SUSPEND(dev);
1409 err = DEVICE_RESUME(dev);
1412 device_printf(dev, "%s failed\n",
1413 do_suspend ? "Suspend" : "Resume");
1417 /*------------------------------------------------------------------------*
1418 * usb_suspend_resume
1420 * The following function will suspend or resume the USB device.
1425 *------------------------------------------------------------------------*/
1427 usb_suspend_resume(struct usb_device *udev, uint8_t do_suspend)
1429 struct usb_interface *iface;
1436 DPRINTFN(4, "udev=%p do_suspend=%d\n", udev, do_suspend);
1438 sx_assert(&udev->sr_sx, SA_LOCKED);
1440 USB_BUS_LOCK(udev->bus);
1441 /* filter the suspend events */
1442 if (udev->flags.peer_suspended == do_suspend) {
1443 USB_BUS_UNLOCK(udev->bus);
1447 udev->flags.peer_suspended = do_suspend;
1448 USB_BUS_UNLOCK(udev->bus);
1450 /* do the suspend or resume */
1452 for (i = 0; i != USB_IFACE_MAX; i++) {
1454 iface = usbd_get_iface(udev, i);
1455 if (iface == NULL) {
1456 /* looks like the end of the USB interfaces */
1459 usb_suspend_resume_sub(udev, iface->subdev, do_suspend);
1464 /*------------------------------------------------------------------------*
1465 * usbd_clear_stall_proc
1467 * This function performs generic USB clear stall operations.
1468 *------------------------------------------------------------------------*/
1470 usbd_clear_stall_proc(struct usb_proc_msg *_pm)
1472 struct usb_udev_msg *pm = (void *)_pm;
1473 struct usb_device *udev = pm->udev;
1476 USB_BUS_UNLOCK(udev->bus);
1477 mtx_lock(&udev->device_mtx);
1479 /* Start clear stall callback */
1480 usbd_transfer_start(udev->ctrl_xfer[1]);
1483 mtx_unlock(&udev->device_mtx);
1484 USB_BUS_LOCK(udev->bus);
1487 /*------------------------------------------------------------------------*
1490 * This function allocates a new USB device. This function is called
1491 * when a new device has been put in the powered state, but not yet in
1492 * the addressed state. Get initial descriptor, set the address, get
1493 * full descriptor and get strings.
1498 *------------------------------------------------------------------------*/
1500 usb_alloc_device(device_t parent_dev, struct usb_bus *bus,
1501 struct usb_device *parent_hub, uint8_t depth, uint8_t port_index,
1502 uint8_t port_no, enum usb_dev_speed speed, enum usb_hc_mode mode)
1504 struct usb_attach_arg uaa;
1505 struct usb_device *udev;
1506 struct usb_device *adev;
1507 struct usb_device *hub;
1508 uint8_t *scratch_ptr;
1510 uint8_t device_index;
1511 uint8_t config_index;
1512 uint8_t config_quirk;
1513 uint8_t set_config_failed;
1516 DPRINTF("parent_dev=%p, bus=%p, parent_hub=%p, depth=%u, "
1517 "port_index=%u, port_no=%u, speed=%u, usb_mode=%u\n",
1518 parent_dev, bus, parent_hub, depth, port_index, port_no,
1522 * Find an unused device index. In USB Host mode this is the
1523 * same as the device address.
1525 * Device index zero is not used and device index 1 should
1526 * always be the root hub.
1528 for (device_index = USB_ROOT_HUB_ADDR;
1529 (device_index != bus->devices_max) &&
1530 (bus->devices[device_index] != NULL);
1531 device_index++) /* nop */;
1533 if (device_index == bus->devices_max) {
1534 device_printf(bus->bdev,
1535 "No free USB device index for new device\n");
1540 device_printf(bus->bdev,
1541 "Invalid device depth\n");
1544 udev = malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO);
1548 /* initialise our SX-lock */
1549 sx_init_flags(&udev->enum_sx, "USB config SX lock", SX_DUPOK);
1550 sx_init_flags(&udev->sr_sx, "USB suspend and resume SX lock", SX_NOWITNESS);
1551 sx_init_flags(&udev->ctrl_sx, "USB control transfer SX lock", SX_DUPOK);
1553 cv_init(&udev->ctrlreq_cv, "WCTRL");
1554 cv_init(&udev->ref_cv, "UGONE");
1556 /* initialise our mutex */
1557 mtx_init(&udev->device_mtx, "USB device mutex", NULL, MTX_DEF);
1559 /* initialise generic clear stall */
1560 udev->cs_msg[0].hdr.pm_callback = &usbd_clear_stall_proc;
1561 udev->cs_msg[0].udev = udev;
1562 udev->cs_msg[1].hdr.pm_callback = &usbd_clear_stall_proc;
1563 udev->cs_msg[1].udev = udev;
1565 /* initialise some USB device fields */
1566 udev->parent_hub = parent_hub;
1567 udev->parent_dev = parent_dev;
1568 udev->port_index = port_index;
1569 udev->port_no = port_no;
1570 udev->depth = depth;
1572 udev->address = USB_START_ADDR; /* default value */
1573 udev->plugtime = (usb_ticks_t)ticks;
1575 * We need to force the power mode to "on" because there are plenty
1576 * of USB devices out there that do not work very well with
1577 * automatic suspend and resume!
1579 udev->power_mode = usbd_filter_power_mode(udev, USB_POWER_MODE_ON);
1580 udev->pwr_save.last_xfer_time = ticks;
1581 /* we are not ready yet */
1584 /* set up default endpoint descriptor */
1585 udev->ctrl_ep_desc.bLength = sizeof(udev->ctrl_ep_desc);
1586 udev->ctrl_ep_desc.bDescriptorType = UDESC_ENDPOINT;
1587 udev->ctrl_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
1588 udev->ctrl_ep_desc.bmAttributes = UE_CONTROL;
1589 udev->ctrl_ep_desc.wMaxPacketSize[0] = USB_MAX_IPACKET;
1590 udev->ctrl_ep_desc.wMaxPacketSize[1] = 0;
1591 udev->ctrl_ep_desc.bInterval = 0;
1593 /* set up default endpoint companion descriptor */
1594 udev->ctrl_ep_comp_desc.bLength = sizeof(udev->ctrl_ep_comp_desc);
1595 udev->ctrl_ep_comp_desc.bDescriptorType = UDESC_ENDPOINT_SS_COMP;
1597 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
1599 udev->speed = speed;
1600 udev->flags.usb_mode = mode;
1602 /* search for our High Speed USB HUB, if any */
1605 hub = udev->parent_hub;
1608 if (hub->speed == USB_SPEED_HIGH) {
1609 udev->hs_hub_addr = hub->address;
1610 udev->parent_hs_hub = hub;
1611 udev->hs_port_no = adev->port_no;
1615 hub = hub->parent_hub;
1618 /* init the default endpoint */
1619 usb_init_endpoint(udev, 0,
1620 &udev->ctrl_ep_desc,
1621 &udev->ctrl_ep_comp_desc,
1624 /* set device index */
1625 udev->device_index = device_index;
1628 /* Create ugen name */
1629 snprintf(udev->ugen_name, sizeof(udev->ugen_name),
1630 USB_GENERIC_NAME "%u.%u", device_get_unit(bus->bdev),
1632 LIST_INIT(&udev->pd_list);
1634 /* Create the control endpoint device */
1635 udev->ctrl_dev = usb_make_dev(udev, NULL, 0, 0,
1636 FREAD|FWRITE, UID_ROOT, GID_OPERATOR, 0600);
1638 /* Create a link from /dev/ugenX.X to the default endpoint */
1639 if (udev->ctrl_dev != NULL)
1640 make_dev_alias(udev->ctrl_dev->cdev, "%s", udev->ugen_name);
1642 /* Initialise device */
1643 if (bus->methods->device_init != NULL) {
1644 err = (bus->methods->device_init) (udev);
1646 DPRINTFN(0, "device init %d failed "
1647 "(%s, ignored)\n", device_index,
1652 /* set powered device state after device init is complete */
1653 usb_set_device_state(udev, USB_STATE_POWERED);
1655 if (udev->flags.usb_mode == USB_MODE_HOST) {
1657 err = usbd_req_set_address(udev, NULL, device_index);
1660 * This is the new USB device address from now on, if
1661 * the set address request didn't set it already.
1663 if (udev->address == USB_START_ADDR)
1664 udev->address = device_index;
1667 * We ignore any set-address errors, hence there are
1668 * buggy USB devices out there that actually receive
1669 * the SETUP PID, but manage to set the address before
1670 * the STATUS stage is ACK'ed. If the device responds
1671 * to the subsequent get-descriptor at the new
1672 * address, then we know that the set-address command
1676 DPRINTFN(0, "set address %d failed "
1677 "(%s, ignored)\n", udev->address,
1681 /* We are not self powered */
1682 udev->flags.self_powered = 0;
1684 /* Set unconfigured state */
1685 udev->curr_config_no = USB_UNCONFIG_NO;
1686 udev->curr_config_index = USB_UNCONFIG_INDEX;
1688 /* Setup USB descriptors */
1689 err = (usb_temp_setup_by_index_p) (udev, usb_template);
1691 DPRINTFN(0, "setting up USB template failed maybe the USB "
1692 "template module has not been loaded\n");
1696 usb_set_device_state(udev, USB_STATE_ADDRESSED);
1698 /* setup the device descriptor and the initial "wMaxPacketSize" */
1699 err = usbd_setup_device_desc(udev, NULL);
1702 /* try to enumerate two more times */
1703 err = usbd_req_re_enumerate(udev, NULL);
1705 err = usbd_req_re_enumerate(udev, NULL);
1713 * Setup temporary USB attach args so that we can figure out some
1714 * basic quirks for this device.
1716 usb_init_attach_arg(udev, &uaa);
1718 if (usb_test_quirk(&uaa, UQ_BUS_POWERED)) {
1719 udev->flags.uq_bus_powered = 1;
1721 if (usb_test_quirk(&uaa, UQ_NO_STRINGS)) {
1722 udev->flags.no_strings = 1;
1725 * Workaround for buggy USB devices.
1727 * It appears that some string-less USB chips will crash and
1728 * disappear if any attempts are made to read any string
1731 * Try to detect such chips by checking the strings in the USB
1732 * device descriptor. If no strings are present there we
1733 * simply disable all USB strings.
1736 /* Protect scratch area */
1737 do_unlock = usbd_ctrl_lock(udev);
1739 scratch_ptr = udev->scratch.data;
1741 if (udev->flags.no_strings) {
1742 err = USB_ERR_INVAL;
1743 } else if (udev->ddesc.iManufacturer ||
1744 udev->ddesc.iProduct ||
1745 udev->ddesc.iSerialNumber) {
1746 /* read out the language ID string */
1747 err = usbd_req_get_string_desc(udev, NULL,
1748 (char *)scratch_ptr, 4, 0, USB_LANGUAGE_TABLE);
1750 err = USB_ERR_INVAL;
1753 if (err || (scratch_ptr[0] < 4)) {
1754 udev->flags.no_strings = 1;
1761 /* load preferred value and mask */
1763 mask = usb_lang_mask;
1765 /* align length correctly */
1766 scratch_ptr[0] &= ~1U;
1768 /* fix compiler warning */
1771 /* search for preferred language */
1772 for (x = 2; (x < scratch_ptr[0]); x += 2) {
1773 langid = UGETW(scratch_ptr + x);
1774 if ((langid & mask) == pref)
1777 if (x >= scratch_ptr[0]) {
1778 /* pick the first language as the default */
1779 DPRINTFN(1, "Using first language\n");
1780 langid = UGETW(scratch_ptr + 2);
1783 DPRINTFN(1, "Language selected: 0x%04x\n", langid);
1784 udev->langid = langid;
1788 usbd_ctrl_unlock(udev);
1790 /* assume 100mA bus powered for now. Changed when configured. */
1791 udev->power = USB_MIN_POWER;
1792 /* fetch the vendor and product strings from the device */
1793 usbd_set_device_strings(udev);
1795 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
1796 /* USB device mode setup is complete */
1802 * Most USB devices should attach to config index 0 by
1805 if (usb_test_quirk(&uaa, UQ_CFG_INDEX_0)) {
1808 } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_1)) {
1811 } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_2)) {
1814 } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_3)) {
1817 } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_4)) {
1825 set_config_failed = 0;
1828 DPRINTF("setting config %u\n", config_index);
1830 /* get the USB device configured */
1831 err = usbd_set_config_index(udev, config_index);
1833 if (udev->ddesc.bNumConfigurations != 0) {
1834 if (!set_config_failed) {
1835 set_config_failed = 1;
1836 /* XXX try to re-enumerate the device */
1837 err = usbd_req_re_enumerate(udev, NULL);
1839 goto repeat_set_config;
1841 DPRINTFN(0, "Failure selecting configuration index %u:"
1842 "%s, port %u, addr %u (ignored)\n",
1843 config_index, usbd_errstr(err), udev->port_no,
1847 * Some USB devices do not have any configurations. Ignore any
1848 * set config failures!
1853 if (!config_quirk && config_index + 1 < udev->ddesc.bNumConfigurations) {
1854 if ((udev->cdesc->bNumInterface < 2) &&
1855 usbd_get_no_descriptors(udev->cdesc, UDESC_ENDPOINT) == 0) {
1856 DPRINTFN(0, "Found no endpoints, trying next config\n");
1858 goto repeat_set_config;
1860 if (config_index == 0) {
1862 * Try to figure out if we have an
1863 * auto-install disk there:
1865 if (usb_iface_is_cdrom(udev, 0)) {
1866 DPRINTFN(0, "Found possible auto-install "
1867 "disk (trying next config)\n");
1869 goto repeat_set_config;
1873 if (set_config_failed == 0 && config_index == 0 &&
1874 usb_test_quirk(&uaa, UQ_MSC_NO_SYNC_CACHE) == 0 &&
1875 usb_test_quirk(&uaa, UQ_MSC_NO_GETMAXLUN) == 0) {
1878 * Try to figure out if there are any MSC quirks we
1879 * should apply automatically:
1881 err = usb_msc_auto_quirk(udev, 0);
1884 set_config_failed = 1;
1885 goto repeat_set_config;
1890 DPRINTF("new dev (addr %d), udev=%p, parent_hub=%p\n",
1891 udev->address, udev, udev->parent_hub);
1893 /* register our device - we are ready */
1894 usb_bus_port_set_device(bus, parent_hub ?
1895 parent_hub->hub->ports + port_index : NULL, udev, device_index);
1898 /* Symlink the ugen device name */
1899 udev->ugen_symlink = usb_alloc_symlink(udev->ugen_name);
1901 /* Announce device */
1902 printf("%s: <%s> at %s\n", udev->ugen_name,
1903 usb_get_manufacturer(udev),
1904 device_get_nameunit(udev->bus->bdev));
1908 usb_notify_addq("ATTACH", udev);
1913 * Free USB device and all subdevices, if any.
1915 usb_free_device(udev, 0);
1922 struct usb_fs_privdata *
1923 usb_make_dev(struct usb_device *udev, const char *devname, int ep,
1924 int fi, int rwmode, uid_t uid, gid_t gid, int mode)
1926 struct usb_fs_privdata* pd;
1929 /* Store information to locate ourselves again later */
1930 pd = malloc(sizeof(struct usb_fs_privdata), M_USBDEV,
1932 pd->bus_index = device_get_unit(udev->bus->bdev);
1933 pd->dev_index = udev->device_index;
1935 pd->fifo_index = fi;
1938 /* Now, create the device itself */
1939 if (devname == NULL) {
1941 snprintf(buffer, sizeof(buffer), USB_DEVICE_DIR "/%u.%u.%u",
1942 pd->bus_index, pd->dev_index, pd->ep_addr);
1945 pd->cdev = make_dev(&usb_devsw, 0, uid, gid, mode, "%s", devname);
1947 if (pd->cdev == NULL) {
1948 DPRINTFN(0, "Failed to create device %s\n", devname);
1953 /* XXX setting si_drv1 and creating the device is not atomic! */
1954 pd->cdev->si_drv1 = pd;
1960 usb_destroy_dev_sync(struct usb_fs_privdata *pd)
1962 DPRINTFN(1, "Destroying device at ugen%d.%d\n",
1963 pd->bus_index, pd->dev_index);
1966 * Destroy character device synchronously. After this
1967 * all system calls are returned. Can block.
1969 destroy_dev(pd->cdev);
1975 usb_destroy_dev(struct usb_fs_privdata *pd)
1977 struct usb_bus *bus;
1982 mtx_lock(&usb_ref_lock);
1983 bus = devclass_get_softc(usb_devclass_ptr, pd->bus_index);
1984 mtx_unlock(&usb_ref_lock);
1987 usb_destroy_dev_sync(pd);
1992 LIST_INSERT_HEAD(&bus->pd_cleanup_list, pd, pd_next);
1993 /* get cleanup going */
1994 usb_proc_msignal(&bus->explore_proc,
1995 &bus->cleanup_msg[0], &bus->cleanup_msg[1]);
1996 USB_BUS_UNLOCK(bus);
2000 usb_cdev_create(struct usb_device *udev)
2002 struct usb_config_descriptor *cd;
2003 struct usb_endpoint_descriptor *ed;
2004 struct usb_descriptor *desc;
2005 struct usb_fs_privdata* pd;
2006 int inmode, outmode, inmask, outmask, mode;
2009 KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("stale cdev entries"));
2011 DPRINTFN(2, "Creating device nodes\n");
2013 if (usbd_get_mode(udev) == USB_MODE_DEVICE) {
2016 } else { /* USB_MODE_HOST */
2026 * Collect all used endpoint numbers instead of just
2027 * generating 16 static endpoints.
2029 cd = usbd_get_config_descriptor(udev);
2030 while ((desc = usb_desc_foreach(cd, desc))) {
2031 /* filter out all endpoint descriptors */
2032 if ((desc->bDescriptorType == UDESC_ENDPOINT) &&
2033 (desc->bLength >= sizeof(*ed))) {
2034 ed = (struct usb_endpoint_descriptor *)desc;
2037 ep = ed->bEndpointAddress;
2038 if (UE_GET_DIR(ep) == UE_DIR_OUT)
2039 outmask |= 1 << UE_GET_ADDR(ep);
2041 inmask |= 1 << UE_GET_ADDR(ep);
2045 /* Create all available endpoints except EP0 */
2046 for (ep = 1; ep < 16; ep++) {
2047 mode = (inmask & (1 << ep)) ? inmode : 0;
2048 mode |= (outmask & (1 << ep)) ? outmode : 0;
2050 continue; /* no IN or OUT endpoint */
2052 pd = usb_make_dev(udev, NULL, ep, 0,
2053 mode, UID_ROOT, GID_OPERATOR, 0600);
2056 LIST_INSERT_HEAD(&udev->pd_list, pd, pd_next);
2061 usb_cdev_free(struct usb_device *udev)
2063 struct usb_fs_privdata* pd;
2065 DPRINTFN(2, "Freeing device nodes\n");
2067 while ((pd = LIST_FIRST(&udev->pd_list)) != NULL) {
2068 KASSERT(pd->cdev->si_drv1 == pd, ("privdata corrupt"));
2070 LIST_REMOVE(pd, pd_next);
2072 usb_destroy_dev(pd);
2077 /*------------------------------------------------------------------------*
2080 * This function is NULL safe and will free an USB device and its
2081 * children devices, if any.
2083 * Flag values: Reserved, set to zero.
2084 *------------------------------------------------------------------------*/
2086 usb_free_device(struct usb_device *udev, uint8_t flag)
2088 struct usb_bus *bus;
2091 return; /* already freed */
2093 DPRINTFN(4, "udev=%p port=%d\n", udev, udev->port_no);
2097 /* set DETACHED state to prevent any further references */
2098 usb_set_device_state(udev, USB_STATE_DETACHED);
2101 usb_notify_addq("DETACH", udev);
2105 printf("%s: <%s> at %s (disconnected)\n", udev->ugen_name,
2106 usb_get_manufacturer(udev), device_get_nameunit(bus->bdev));
2108 /* Destroy UGEN symlink, if any */
2109 if (udev->ugen_symlink) {
2110 usb_free_symlink(udev->ugen_symlink);
2111 udev->ugen_symlink = NULL;
2114 usb_destroy_dev(udev->ctrl_dev);
2117 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2118 /* stop receiving any control transfers (Device Side Mode) */
2119 usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
2122 /* the following will get the device unconfigured in software */
2123 usb_unconfigure(udev, USB_UNCFG_FLAG_FREE_EP0);
2125 /* final device unregister after all character devices are closed */
2126 usb_bus_port_set_device(bus, udev->parent_hub ?
2127 udev->parent_hub->hub->ports + udev->port_index : NULL,
2128 NULL, USB_ROOT_HUB_ADDR);
2130 /* unsetup any leftover default USB transfers */
2131 usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
2133 /* template unsetup, if any */
2134 (usb_temp_unsetup_p) (udev);
2137 * Make sure that our clear-stall messages are not queued
2140 USB_BUS_LOCK(udev->bus);
2141 usb_proc_mwait(&udev->bus->non_giant_callback_proc,
2142 &udev->cs_msg[0], &udev->cs_msg[1]);
2143 USB_BUS_UNLOCK(udev->bus);
2145 /* wait for all references to go away */
2146 usb_wait_pending_refs(udev);
2148 sx_destroy(&udev->enum_sx);
2149 sx_destroy(&udev->sr_sx);
2150 sx_destroy(&udev->ctrl_sx);
2152 cv_destroy(&udev->ctrlreq_cv);
2153 cv_destroy(&udev->ref_cv);
2155 mtx_destroy(&udev->device_mtx);
2157 KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("leaked cdev entries"));
2160 /* Uninitialise device */
2161 if (bus->methods->device_uninit != NULL)
2162 (bus->methods->device_uninit) (udev);
2165 free(udev->serial, M_USB);
2166 free(udev->manufacturer, M_USB);
2167 free(udev->product, M_USB);
2171 /*------------------------------------------------------------------------*
2174 * This function is the safe way to get the USB interface structure
2175 * pointer by interface index.
2178 * NULL: Interface not present.
2179 * Else: Pointer to USB interface structure.
2180 *------------------------------------------------------------------------*/
2181 struct usb_interface *
2182 usbd_get_iface(struct usb_device *udev, uint8_t iface_index)
2184 struct usb_interface *iface = udev->ifaces + iface_index;
2186 if (iface_index >= udev->ifaces_max)
2191 /*------------------------------------------------------------------------*
2192 * usbd_find_descriptor
2194 * This function will lookup the first descriptor that matches the
2195 * criteria given by the arguments "type" and "subtype". Descriptors
2196 * will only be searched within the interface having the index
2197 * "iface_index". If the "id" argument points to an USB descriptor,
2198 * it will be skipped before the search is started. This allows
2199 * searching for multiple descriptors using the same criteria. Else
2200 * the search is started after the interface descriptor.
2203 * NULL: End of descriptors
2204 * Else: A descriptor matching the criteria
2205 *------------------------------------------------------------------------*/
2207 usbd_find_descriptor(struct usb_device *udev, void *id, uint8_t iface_index,
2208 uint8_t type, uint8_t type_mask,
2209 uint8_t subtype, uint8_t subtype_mask)
2211 struct usb_descriptor *desc;
2212 struct usb_config_descriptor *cd;
2213 struct usb_interface *iface;
2215 cd = usbd_get_config_descriptor(udev);
2220 iface = usbd_get_iface(udev, iface_index);
2221 if (iface == NULL) {
2224 id = usbd_get_interface_descriptor(iface);
2231 while ((desc = usb_desc_foreach(cd, desc))) {
2233 if (desc->bDescriptorType == UDESC_INTERFACE) {
2236 if (((desc->bDescriptorType & type_mask) == type) &&
2237 ((desc->bDescriptorSubtype & subtype_mask) == subtype)) {
2244 /*------------------------------------------------------------------------*
2247 * This function will dump information from the device descriptor
2248 * belonging to the USB device pointed to by "udev", to the string
2249 * pointed to by "dst_ptr" having a maximum length of "dst_len" bytes
2250 * including the terminating zero.
2251 *------------------------------------------------------------------------*/
2253 usb_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len)
2255 struct usb_device_descriptor *udd = &udev->ddesc;
2259 bcdUSB = UGETW(udd->bcdUSB);
2260 bcdDevice = UGETW(udd->bcdDevice);
2262 if (udd->bDeviceClass != 0xFF) {
2263 snprintf(dst_ptr, dst_len, "%s %s, class %d/%d, rev %x.%02x/"
2265 usb_get_manufacturer(udev),
2266 usb_get_product(udev),
2267 udd->bDeviceClass, udd->bDeviceSubClass,
2268 (bcdUSB >> 8), bcdUSB & 0xFF,
2269 (bcdDevice >> 8), bcdDevice & 0xFF,
2272 snprintf(dst_ptr, dst_len, "%s %s, rev %x.%02x/"
2274 usb_get_manufacturer(udev),
2275 usb_get_product(udev),
2276 (bcdUSB >> 8), bcdUSB & 0xFF,
2277 (bcdDevice >> 8), bcdDevice & 0xFF,
2284 * Descriptions of of known vendors and devices ("products").
2286 struct usb_knowndev {
2290 const char *vendorname;
2291 const char *productname;
2294 #define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */
2296 #include "usbdevs.h"
2297 #include "usbdevs_data.h"
2298 #endif /* USB_VERBOSE */
2301 usbd_set_device_strings(struct usb_device *udev)
2303 struct usb_device_descriptor *udd = &udev->ddesc;
2305 const struct usb_knowndev *kdp;
2310 uint16_t product_id;
2313 /* Protect scratch area */
2314 do_unlock = usbd_ctrl_lock(udev);
2316 temp_ptr = (char *)udev->scratch.data;
2317 temp_size = sizeof(udev->scratch.data);
2319 vendor_id = UGETW(udd->idVendor);
2320 product_id = UGETW(udd->idProduct);
2322 /* get serial number string */
2323 usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2324 udev->ddesc.iSerialNumber);
2325 udev->serial = strdup(temp_ptr, M_USB);
2327 /* get manufacturer string */
2328 usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2329 udev->ddesc.iManufacturer);
2330 usb_trim_spaces(temp_ptr);
2331 if (temp_ptr[0] != '\0')
2332 udev->manufacturer = strdup(temp_ptr, M_USB);
2334 /* get product string */
2335 usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2336 udev->ddesc.iProduct);
2337 usb_trim_spaces(temp_ptr);
2338 if (temp_ptr[0] != '\0')
2339 udev->product = strdup(temp_ptr, M_USB);
2342 if (udev->manufacturer == NULL || udev->product == NULL) {
2343 for (kdp = usb_knowndevs; kdp->vendorname != NULL; kdp++) {
2344 if (kdp->vendor == vendor_id &&
2345 (kdp->product == product_id ||
2346 (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
2349 if (kdp->vendorname != NULL) {
2350 /* XXX should use pointer to knowndevs string */
2351 if (udev->manufacturer == NULL) {
2352 udev->manufacturer = strdup(kdp->vendorname,
2355 if (udev->product == NULL &&
2356 (kdp->flags & USB_KNOWNDEV_NOPROD) == 0) {
2357 udev->product = strdup(kdp->productname,
2363 /* Provide default strings if none were found */
2364 if (udev->manufacturer == NULL) {
2365 snprintf(temp_ptr, temp_size, "vendor 0x%04x", vendor_id);
2366 udev->manufacturer = strdup(temp_ptr, M_USB);
2368 if (udev->product == NULL) {
2369 snprintf(temp_ptr, temp_size, "product 0x%04x", product_id);
2370 udev->product = strdup(temp_ptr, M_USB);
2374 usbd_ctrl_unlock(udev);
2382 usbd_get_mode(struct usb_device *udev)
2384 return (udev->flags.usb_mode);
2389 * See: USB_SPEED_XXX
2392 usbd_get_speed(struct usb_device *udev)
2394 return (udev->speed);
2398 usbd_get_isoc_fps(struct usb_device *udev)
2401 switch (udev->speed) {
2403 case USB_SPEED_FULL:
2410 struct usb_device_descriptor *
2411 usbd_get_device_descriptor(struct usb_device *udev)
2414 return (NULL); /* be NULL safe */
2415 return (&udev->ddesc);
2418 struct usb_config_descriptor *
2419 usbd_get_config_descriptor(struct usb_device *udev)
2422 return (NULL); /* be NULL safe */
2423 return (udev->cdesc);
2426 /*------------------------------------------------------------------------*
2427 * usb_test_quirk - test a device for a given quirk
2430 * 0: The USB device does not have the given quirk.
2431 * Else: The USB device has the given quirk.
2432 *------------------------------------------------------------------------*/
2434 usb_test_quirk(const struct usb_attach_arg *uaa, uint16_t quirk)
2439 if (quirk == UQ_NONE)
2442 /* search the automatic per device quirks first */
2444 for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) {
2445 if (uaa->device->autoQuirk[x] == quirk)
2449 /* search global quirk table, if any */
2451 found = (usb_test_quirk_p) (&uaa->info, quirk);
2456 struct usb_interface_descriptor *
2457 usbd_get_interface_descriptor(struct usb_interface *iface)
2460 return (NULL); /* be NULL safe */
2461 return (iface->idesc);
2465 usbd_get_interface_altindex(struct usb_interface *iface)
2467 return (iface->alt_index);
2471 usbd_get_bus_index(struct usb_device *udev)
2473 return ((uint8_t)device_get_unit(udev->bus->bdev));
2477 usbd_get_device_index(struct usb_device *udev)
2479 return (udev->device_index);
2483 /*------------------------------------------------------------------------*
2486 * This function will generate events for dev.
2487 *------------------------------------------------------------------------*/
2488 #ifndef BURN_BRIDGES
2490 usb_notify_addq_compat(const char *type, struct usb_device *udev)
2494 struct malloc_type *mt;
2495 const size_t buf_size = 512;
2497 /* Convert notify type */
2498 if (strcmp(type, "ATTACH") == 0)
2500 else if (strcmp(type, "DETACH") == 0)
2505 mtx_lock(&malloc_mtx);
2506 mt = malloc_desc2type("bus"); /* XXX M_BUS */
2507 mtx_unlock(&malloc_mtx);
2511 data = malloc(buf_size, mt, M_NOWAIT);
2515 /* String it all together. */
2516 snprintf(data, buf_size,
2525 "devsubclass=0x%02x "
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)
2544 , udev->parent_hub != NULL ?
2545 udev->parent_hub->ugen_name :
2546 device_get_nameunit(device_get_parent(udev->bus->bdev))
2550 devctl_queue_data(data);
2555 usb_notify_addq(const char *type, struct usb_device *udev)
2557 struct usb_interface *iface;
2561 #ifndef BURN_BRIDGES
2562 usb_notify_addq_compat(type, udev);
2565 /* announce the device */
2566 sb = sbuf_new_auto();
2575 "devsubclass=0x%02x "
2588 UGETW(udev->ddesc.idVendor),
2589 UGETW(udev->ddesc.idProduct),
2590 udev->ddesc.bDeviceClass,
2591 udev->ddesc.bDeviceSubClass,
2592 usb_get_serial(udev),
2593 UGETW(udev->ddesc.bcdDevice),
2594 (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
2597 , udev->parent_hub != NULL ?
2598 udev->parent_hub->ugen_name :
2599 device_get_nameunit(device_get_parent(udev->bus->bdev))
2603 devctl_notify("USB", "DEVICE", type, sbuf_data(sb));
2606 /* announce each interface */
2607 for (i = 0; i < USB_IFACE_MAX; i++) {
2608 iface = usbd_get_iface(udev, i);
2610 break; /* end of interfaces */
2611 if (iface->idesc == NULL)
2612 continue; /* no interface descriptor */
2614 sb = sbuf_new_auto();
2623 "devsubclass=0x%02x "
2630 "intsubclass=0x%02x "
2631 "intprotocol=0x%02x",
2636 UGETW(udev->ddesc.idVendor),
2637 UGETW(udev->ddesc.idProduct),
2638 udev->ddesc.bDeviceClass,
2639 udev->ddesc.bDeviceSubClass,
2640 usb_get_serial(udev),
2641 UGETW(udev->ddesc.bcdDevice),
2642 (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
2643 iface->idesc->bInterfaceNumber,
2644 iface->idesc->bNumEndpoints,
2645 iface->idesc->bInterfaceClass,
2646 iface->idesc->bInterfaceSubClass,
2647 iface->idesc->bInterfaceProtocol);
2649 devctl_notify("USB", "INTERFACE", type, sbuf_data(sb));
2656 /*------------------------------------------------------------------------*
2657 * usb_fifo_free_wrap
2659 * This function will free the FIFOs.
2661 * Description of "flag" argument: If the USB_UNCFG_FLAG_FREE_EP0 flag
2662 * is set and "iface_index" is set to "USB_IFACE_INDEX_ANY", we free
2663 * all FIFOs. If the USB_UNCFG_FLAG_FREE_EP0 flag is not set and
2664 * "iface_index" is set to "USB_IFACE_INDEX_ANY", we free all non
2665 * control endpoint FIFOs. If "iface_index" is not set to
2666 * "USB_IFACE_INDEX_ANY" the flag has no effect.
2667 *------------------------------------------------------------------------*/
2669 usb_fifo_free_wrap(struct usb_device *udev,
2670 uint8_t iface_index, uint8_t flag)
2676 * Free any USB FIFOs on the given interface:
2678 for (i = 0; i != USB_FIFO_MAX; i++) {
2683 /* Check if the interface index matches */
2684 if (iface_index == f->iface_index) {
2685 if (f->methods != &usb_ugen_methods) {
2687 * Don't free any non-generic FIFOs in
2692 if ((f->dev_ep_index == 0) &&
2693 (f->fs_xfer == NULL)) {
2694 /* no need to free this FIFO */
2697 } else if (iface_index == USB_IFACE_INDEX_ANY) {
2698 if ((f->methods == &usb_ugen_methods) &&
2699 (f->dev_ep_index == 0) &&
2700 (!(flag & USB_UNCFG_FLAG_FREE_EP0)) &&
2701 (f->fs_xfer == NULL)) {
2702 /* no need to free this FIFO */
2706 /* no need to free this FIFO */
2709 /* free this FIFO */
2715 /*------------------------------------------------------------------------*
2716 * usb_peer_can_wakeup
2719 * 0: Peer cannot do resume signalling.
2720 * Else: Peer can do resume signalling.
2721 *------------------------------------------------------------------------*/
2723 usb_peer_can_wakeup(struct usb_device *udev)
2725 const struct usb_config_descriptor *cdp;
2728 if ((cdp != NULL) && (udev->flags.usb_mode == USB_MODE_HOST)) {
2729 return (cdp->bmAttributes & UC_REMOTE_WAKEUP);
2731 return (0); /* not supported */
2735 usb_set_device_state(struct usb_device *udev, enum usb_dev_state state)
2738 KASSERT(state < USB_STATE_MAX, ("invalid udev state"));
2740 DPRINTF("udev %p state %s -> %s\n", udev,
2741 usb_statestr(udev->state), usb_statestr(state));
2744 mtx_lock(&usb_ref_lock);
2746 udev->state = state;
2748 mtx_unlock(&usb_ref_lock);
2750 if (udev->bus->methods->device_state_change != NULL)
2751 (udev->bus->methods->device_state_change) (udev);
2755 usb_get_device_state(struct usb_device *udev)
2758 return (USB_STATE_DETACHED);
2759 return (udev->state);
2763 usbd_device_attached(struct usb_device *udev)
2765 return (udev->state > USB_STATE_DETACHED);
2769 * The following function locks enumerating the given USB device. If
2770 * the lock is already grabbed this function returns zero. Else a
2771 * a value of one is returned.
2774 usbd_enum_lock(struct usb_device *udev)
2776 if (sx_xlocked(&udev->enum_sx))
2779 sx_xlock(&udev->enum_sx);
2780 sx_xlock(&udev->sr_sx);
2782 * NEWBUS LOCK NOTE: We should check if any parent SX locks
2783 * are locked before locking Giant. Else the lock can be
2784 * locked multiple times.
2792 * This function is the same like usbd_enum_lock() except a value of
2793 * 255 is returned when a signal is pending:
2796 usbd_enum_lock_sig(struct usb_device *udev)
2798 if (sx_xlocked(&udev->enum_sx))
2800 if (sx_xlock_sig(&udev->enum_sx))
2802 if (sx_xlock_sig(&udev->sr_sx)) {
2803 sx_xunlock(&udev->enum_sx);
2811 /* The following function unlocks enumerating the given USB device. */
2814 usbd_enum_unlock(struct usb_device *udev)
2817 sx_xunlock(&udev->enum_sx);
2818 sx_xunlock(&udev->sr_sx);
2821 /* The following function locks suspend and resume. */
2824 usbd_sr_lock(struct usb_device *udev)
2826 sx_xlock(&udev->sr_sx);
2828 * NEWBUS LOCK NOTE: We should check if any parent SX locks
2829 * are locked before locking Giant. Else the lock can be
2830 * locked multiple times.
2835 /* The following function unlocks suspend and resume. */
2838 usbd_sr_unlock(struct usb_device *udev)
2841 sx_xunlock(&udev->sr_sx);
2845 * The following function checks the enumerating lock for the given
2850 usbd_enum_is_locked(struct usb_device *udev)
2852 return (sx_xlocked(&udev->enum_sx));
2856 * The following function is used to serialize access to USB control
2857 * transfers and the USB scratch area. If the lock is already grabbed
2858 * this function returns zero. Else a value of one is returned.
2861 usbd_ctrl_lock(struct usb_device *udev)
2863 if (sx_xlocked(&udev->ctrl_sx))
2865 sx_xlock(&udev->ctrl_sx);
2868 * We need to allow suspend and resume at this point, else the
2869 * control transfer will timeout if the device is suspended!
2871 if (usbd_enum_is_locked(udev))
2872 usbd_sr_unlock(udev);
2877 usbd_ctrl_unlock(struct usb_device *udev)
2879 sx_xunlock(&udev->ctrl_sx);
2882 * Restore the suspend and resume lock after we have unlocked
2883 * the USB control transfer lock to avoid LOR:
2885 if (usbd_enum_is_locked(udev))
2890 * The following function is used to set the per-interface specific
2891 * plug and play information. The string referred to by the pnpinfo
2892 * argument can safely be freed after calling this function. The
2893 * pnpinfo of an interface will be reset at device detach or when
2894 * passing a NULL argument to this function. This function
2895 * returns zero on success, else a USB_ERR_XXX failure code.
2899 usbd_set_pnpinfo(struct usb_device *udev, uint8_t iface_index, const char *pnpinfo)
2901 struct usb_interface *iface;
2903 iface = usbd_get_iface(udev, iface_index);
2905 return (USB_ERR_INVAL);
2907 if (iface->pnpinfo != NULL) {
2908 free(iface->pnpinfo, M_USBDEV);
2909 iface->pnpinfo = NULL;
2912 if (pnpinfo == NULL || pnpinfo[0] == 0)
2913 return (0); /* success */
2915 iface->pnpinfo = strdup(pnpinfo, M_USBDEV);
2916 if (iface->pnpinfo == NULL)
2917 return (USB_ERR_NOMEM);
2919 return (0); /* success */
2923 usbd_add_dynamic_quirk(struct usb_device *udev, uint16_t quirk)
2927 for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) {
2928 if (udev->autoQuirk[x] == 0 ||
2929 udev->autoQuirk[x] == quirk) {
2930 udev->autoQuirk[x] = quirk;
2931 return (0); /* success */
2934 return (USB_ERR_NOMEM);