3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #ifdef USB_GLOBAL_INCLUDE_FILE
30 #include USB_GLOBAL_INCLUDE_FILE
32 #include <sys/stdint.h>
33 #include <sys/stddef.h>
34 #include <sys/param.h>
35 #include <sys/eventhandler.h>
36 #include <sys/queue.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/condvar.h>
44 #include <sys/sysctl.h>
46 #include <sys/unistd.h>
47 #include <sys/callout.h>
48 #include <sys/malloc.h>
51 #include <sys/fcntl.h>
53 #include <dev/usb/usb.h>
54 #include <dev/usb/usbdi.h>
55 #include <dev/usb/usbdi_util.h>
56 #include <dev/usb/usb_ioctl.h>
64 #define USB_DEBUG_VAR usb_debug
66 #include <dev/usb/usb_core.h>
67 #include <dev/usb/usb_debug.h>
68 #include <dev/usb/usb_process.h>
69 #include <dev/usb/usb_device.h>
70 #include <dev/usb/usb_busdma.h>
71 #include <dev/usb/usb_transfer.h>
72 #include <dev/usb/usb_request.h>
73 #include <dev/usb/usb_dynamic.h>
74 #include <dev/usb/usb_hub.h>
75 #include <dev/usb/usb_util.h>
76 #include <dev/usb/usb_msctest.h>
78 #include <dev/usb/usb_dev.h>
79 #include <dev/usb/usb_generic.h>
82 #include <dev/usb/quirk/usb_quirk.h>
84 #include <dev/usb/usb_controller.h>
85 #include <dev/usb/usb_bus.h>
86 #endif /* USB_GLOBAL_INCLUDE_FILE */
88 /* function prototypes */
90 static int sysctl_hw_usb_template(SYSCTL_HANDLER_ARGS);
91 static void usb_init_endpoint(struct usb_device *, uint8_t,
92 struct usb_endpoint_descriptor *,
93 struct usb_endpoint_ss_comp_descriptor *,
94 struct usb_endpoint *);
95 static void usb_unconfigure(struct usb_device *, uint8_t);
96 static void usb_detach_device_sub(struct usb_device *, device_t *,
98 static uint8_t usb_probe_and_attach_sub(struct usb_device *,
99 struct usb_attach_arg *);
100 static void usb_init_attach_arg(struct usb_device *,
101 struct usb_attach_arg *);
102 static void usb_suspend_resume_sub(struct usb_device *, device_t,
104 static usb_proc_callback_t usbd_clear_stall_proc;
105 static usb_error_t usb_config_parse(struct usb_device *, uint8_t, uint8_t);
107 static void usb_notify_addq(const char *type, struct usb_device *);
110 static void usb_fifo_free_wrap(struct usb_device *, uint8_t, uint8_t);
111 static void usb_cdev_create(struct usb_device *);
112 static void usb_cdev_free(struct usb_device *);
115 /* This variable is global to allow easy access to it: */
118 int usb_template = USB_TEMPLATE;
120 int usb_template = -1;
123 SYSCTL_PROC(_hw_usb, OID_AUTO, template,
124 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
125 NULL, 0, sysctl_hw_usb_template,
126 "I", "Selected USB device side template");
128 /*------------------------------------------------------------------------*
129 * usb_trigger_reprobe_on_off
131 * This function sets the pull up resistors for all ports currently
132 * operating in device mode either on (when on_not_off is 1), or off
134 *------------------------------------------------------------------------*/
136 usb_trigger_reprobe_on_off(int on_not_off)
138 struct usb_port_status ps;
140 struct usb_device *udev;
144 max = devclass_get_maxunit(usb_devclass_ptr);
146 mtx_lock(&usb_ref_lock);
147 bus = devclass_get_softc(usb_devclass_ptr, max);
150 if (bus == NULL || bus->devices == NULL ||
151 bus->devices[USB_ROOT_HUB_ADDR] == NULL) {
152 mtx_unlock(&usb_ref_lock);
156 udev = bus->devices[USB_ROOT_HUB_ADDR];
158 if (udev->refcount == USB_DEV_REF_MAX) {
159 mtx_unlock(&usb_ref_lock);
164 mtx_unlock(&usb_ref_lock);
166 do_unlock = usbd_enum_lock(udev);
172 err = usbd_req_get_port_status(udev, NULL, &ps, 1);
174 DPRINTF("usbd_req_get_port_status() "
175 "failed: %s\n", usbd_errstr(err));
179 if ((UGETW(ps.wPortStatus) & UPS_PORT_MODE_DEVICE) == 0)
183 err = usbd_req_set_port_feature(udev, NULL, 1,
186 DPRINTF("usbd_req_set_port_feature() "
187 "failed: %s\n", usbd_errstr(err));
190 err = usbd_req_clear_port_feature(udev, NULL, 1,
193 DPRINTF("usbd_req_clear_port_feature() "
194 "failed: %s\n", usbd_errstr(err));
199 mtx_lock(&usb_ref_lock);
201 usbd_enum_unlock(udev);
202 if (--(udev->refcount) == 0)
203 cv_broadcast(&udev->ref_cv);
204 mtx_unlock(&usb_ref_lock);
208 /*------------------------------------------------------------------------*
209 * usb_trigger_reprobe_all
211 * This function toggles the pull up resistors for all ports currently
212 * operating in device mode, causing the host machine to reenumerate them.
213 *------------------------------------------------------------------------*/
215 usb_trigger_reprobe_all(void)
219 * Set the pull up resistors off for all ports in device mode.
221 usb_trigger_reprobe_on_off(0);
224 * According to the DWC OTG spec this must be at least 3ms.
226 usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
229 * Set the pull up resistors back on.
231 usb_trigger_reprobe_on_off(1);
235 sysctl_hw_usb_template(SYSCTL_HANDLER_ARGS)
240 error = sysctl_handle_int(oidp, &val, 0, req);
241 if (error != 0 || req->newptr == NULL || usb_template == val)
246 if (usb_template < 0) {
247 usb_trigger_reprobe_on_off(0);
249 usb_trigger_reprobe_all();
255 /* English is default language */
257 static int usb_lang_id = 0x0009;
258 static int usb_lang_mask = 0x00FF;
260 SYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_id, CTLFLAG_RWTUN,
261 &usb_lang_id, 0, "Preferred USB language ID");
263 SYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_mask, CTLFLAG_RWTUN,
264 &usb_lang_mask, 0, "Preferred USB language mask");
266 static const char* statestr[USB_STATE_MAX] = {
267 [USB_STATE_DETACHED] = "DETACHED",
268 [USB_STATE_ATTACHED] = "ATTACHED",
269 [USB_STATE_POWERED] = "POWERED",
270 [USB_STATE_ADDRESSED] = "ADDRESSED",
271 [USB_STATE_CONFIGURED] = "CONFIGURED",
275 usb_statestr(enum usb_dev_state state)
277 return ((state < USB_STATE_MAX) ? statestr[state] : "UNKNOWN");
281 usb_get_manufacturer(struct usb_device *udev)
283 return (udev->manufacturer ? udev->manufacturer : "Unknown");
287 usb_get_product(struct usb_device *udev)
289 return (udev->product ? udev->product : "");
293 usb_get_serial(struct usb_device *udev)
295 return (udev->serial ? udev->serial : "");
298 /*------------------------------------------------------------------------*
299 * usbd_get_ep_by_addr
301 * This function searches for an USB ep by endpoint address and
307 *------------------------------------------------------------------------*/
308 struct usb_endpoint *
309 usbd_get_ep_by_addr(struct usb_device *udev, uint8_t ea_val)
311 struct usb_endpoint *ep = udev->endpoints;
312 struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max;
314 EA_MASK = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR),
318 * According to the USB specification not all bits are used
319 * for the endpoint address. Keep defined bits only:
324 * Iterate across all the USB endpoints searching for a match
325 * based on the endpoint address:
327 for (; ep != ep_end; ep++) {
328 if (ep->edesc == NULL) {
331 /* do the mask and check the value */
332 if ((ep->edesc->bEndpointAddress & EA_MASK) == ea_val) {
338 * The default endpoint is always present and is checked separately:
340 if ((udev->ctrl_ep.edesc != NULL) &&
341 ((udev->ctrl_ep.edesc->bEndpointAddress & EA_MASK) == ea_val)) {
351 /*------------------------------------------------------------------------*
354 * This function searches for an USB endpoint based on the information
355 * given by the passed "struct usb_config" pointer.
359 * Else: Pointer to "struct usb_endpoint".
360 *------------------------------------------------------------------------*/
361 struct usb_endpoint *
362 usbd_get_endpoint(struct usb_device *udev, uint8_t iface_index,
363 const struct usb_config *setup)
365 struct usb_endpoint *ep = udev->endpoints;
366 struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max;
367 uint8_t index = setup->ep_index;
373 DPRINTFN(10, "udev=%p iface_index=%d address=0x%x "
374 "type=0x%x dir=0x%x index=%d\n",
375 udev, iface_index, setup->endpoint,
376 setup->type, setup->direction, setup->ep_index);
380 if (setup->usb_mode != USB_MODE_DUAL &&
381 udev->flags.usb_mode != setup->usb_mode) {
382 /* wrong mode - no endpoint */
386 /* setup expected endpoint direction mask and value */
388 if (setup->direction == UE_DIR_RX) {
389 ea_mask = (UE_DIR_IN | UE_DIR_OUT);
390 ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ?
391 UE_DIR_OUT : UE_DIR_IN;
392 } else if (setup->direction == UE_DIR_TX) {
393 ea_mask = (UE_DIR_IN | UE_DIR_OUT);
394 ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ?
395 UE_DIR_IN : UE_DIR_OUT;
396 } else if (setup->direction == UE_DIR_ANY) {
397 /* match any endpoint direction */
401 /* match the given endpoint direction */
402 ea_mask = (UE_DIR_IN | UE_DIR_OUT);
403 ea_val = (setup->direction & (UE_DIR_IN | UE_DIR_OUT));
406 /* setup expected endpoint address */
408 if (setup->endpoint == UE_ADDR_ANY) {
409 /* match any endpoint address */
411 /* match the given endpoint address */
413 ea_val |= (setup->endpoint & UE_ADDR);
416 /* setup expected endpoint type */
418 if (setup->type == UE_BULK_INTR) {
419 /* this will match BULK and INTERRUPT endpoints */
422 } else if (setup->type == UE_TYPE_ANY) {
423 /* match any endpoint type */
427 /* match the given endpoint type */
428 type_mask = UE_XFERTYPE;
429 type_val = (setup->type & UE_XFERTYPE);
433 * Iterate across all the USB endpoints searching for a match
434 * based on the endpoint address. Note that we are searching
435 * the endpoints from the beginning of the "udev->endpoints" array.
437 for (; ep != ep_end; ep++) {
438 if ((ep->edesc == NULL) ||
439 (ep->iface_index != iface_index)) {
442 /* do the masks and check the values */
444 if (((ep->edesc->bEndpointAddress & ea_mask) == ea_val) &&
445 ((ep->edesc->bmAttributes & type_mask) == type_val)) {
453 * Match against default endpoint last, so that "any endpoint", "any
454 * address" and "any direction" returns the first endpoint of the
455 * interface. "iface_index" and "direction" is ignored:
457 if ((udev->ctrl_ep.edesc != NULL) &&
458 ((udev->ctrl_ep.edesc->bEndpointAddress & ea_mask) == ea_val) &&
459 ((udev->ctrl_ep.edesc->bmAttributes & type_mask) == type_val) &&
470 /*------------------------------------------------------------------------*
471 * usbd_interface_count
473 * This function stores the number of USB interfaces excluding
474 * alternate settings, which the USB config descriptor reports into
475 * the unsigned 8-bit integer pointed to by "count".
480 *------------------------------------------------------------------------*/
482 usbd_interface_count(struct usb_device *udev, uint8_t *count)
484 if (udev->cdesc == NULL) {
486 return (USB_ERR_NOT_CONFIGURED);
488 *count = udev->ifaces_max;
489 return (USB_ERR_NORMAL_COMPLETION);
492 /*------------------------------------------------------------------------*
495 * This function will initialise the USB endpoint structure pointed to by
496 * the "endpoint" argument. The structure pointed to by "endpoint" must be
497 * zeroed before calling this function.
498 *------------------------------------------------------------------------*/
500 usb_init_endpoint(struct usb_device *udev, uint8_t iface_index,
501 struct usb_endpoint_descriptor *edesc,
502 struct usb_endpoint_ss_comp_descriptor *ecomp,
503 struct usb_endpoint *ep)
505 const struct usb_bus_methods *methods;
508 methods = udev->bus->methods;
510 (methods->endpoint_init) (udev, edesc, ep);
512 /* initialise USB endpoint structure */
515 ep->iface_index = iface_index;
517 /* setup USB stream queues */
518 for (x = 0; x != USB_MAX_EP_STREAMS; x++) {
519 TAILQ_INIT(&ep->endpoint_q[x].head);
520 ep->endpoint_q[x].command = &usbd_pipe_start;
523 /* the pipe is not supported by the hardware */
524 if (ep->methods == NULL)
527 /* check for SUPER-speed streams mode endpoint */
528 if (udev->speed == USB_SPEED_SUPER && ecomp != NULL &&
529 (edesc->bmAttributes & UE_XFERTYPE) == UE_BULK &&
530 (UE_GET_BULK_STREAMS(ecomp->bmAttributes) != 0)) {
531 usbd_set_endpoint_mode(udev, ep, USB_EP_MODE_STREAMS);
533 usbd_set_endpoint_mode(udev, ep, USB_EP_MODE_DEFAULT);
536 /* clear stall, if any */
537 if (methods->clear_stall != NULL) {
538 USB_BUS_LOCK(udev->bus);
539 (methods->clear_stall) (udev, ep);
540 USB_BUS_UNLOCK(udev->bus);
544 /*-----------------------------------------------------------------------*
545 * usb_endpoint_foreach
547 * This function will iterate all the USB endpoints except the control
548 * endpoint. This function is NULL safe.
551 * NULL: End of USB endpoints
552 * Else: Pointer to next USB endpoint
553 *------------------------------------------------------------------------*/
554 struct usb_endpoint *
555 usb_endpoint_foreach(struct usb_device *udev, struct usb_endpoint *ep)
557 struct usb_endpoint *ep_end;
563 ep_end = udev->endpoints + udev->endpoints_max;
565 /* get next endpoint */
567 ep = udev->endpoints;
571 /* find next allocated ep */
572 while (ep != ep_end) {
573 if (ep->edesc != NULL)
580 /*------------------------------------------------------------------------*
581 * usb_wait_pending_refs
583 * This function will wait for any USB references to go away before
584 * returning. This function is used before freeing a USB device.
585 *------------------------------------------------------------------------*/
587 usb_wait_pending_refs(struct usb_device *udev)
590 DPRINTF("Refcount = %d\n", (int)udev->refcount);
592 mtx_lock(&usb_ref_lock);
595 /* wait for any pending references to go away */
596 if (udev->refcount == 0) {
597 /* prevent further refs being taken, if any */
598 udev->refcount = USB_DEV_REF_MAX;
601 cv_wait(&udev->ref_cv, &usb_ref_lock);
603 mtx_unlock(&usb_ref_lock);
607 /*------------------------------------------------------------------------*
610 * This function will free all USB interfaces and USB endpoints belonging
613 * Flag values, see "USB_UNCFG_FLAG_XXX".
614 *------------------------------------------------------------------------*/
616 usb_unconfigure(struct usb_device *udev, uint8_t flag)
620 /* Prevent re-enumeration */
621 do_unlock = usbd_enum_lock(udev);
623 /* detach all interface drivers */
624 usb_detach_device(udev, USB_IFACE_INDEX_ANY, flag);
627 /* free all FIFOs except control endpoint FIFOs */
628 usb_fifo_free_wrap(udev, USB_IFACE_INDEX_ANY, flag);
631 * Free all cdev's, if any.
636 #if USB_HAVE_COMPAT_LINUX
637 /* free Linux compat device, if any */
638 if (udev->linux_endpoint_start != NULL) {
639 usb_linux_free_device_p(udev);
640 udev->linux_endpoint_start = NULL;
644 usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_FREE);
646 /* free "cdesc" after "ifaces" and "endpoints", if any */
647 if (udev->cdesc != NULL) {
648 if (udev->flags.usb_mode != USB_MODE_DEVICE)
649 usbd_free_config_desc(udev, udev->cdesc);
652 /* set unconfigured state */
653 udev->curr_config_no = USB_UNCONFIG_NO;
654 udev->curr_config_index = USB_UNCONFIG_INDEX;
657 usbd_enum_unlock(udev);
660 /*------------------------------------------------------------------------*
661 * usbd_set_config_index
663 * This function selects configuration by index, independent of the
664 * actual configuration number. This function should not be used by
670 *------------------------------------------------------------------------*/
672 usbd_set_config_index(struct usb_device *udev, uint8_t index)
674 struct usb_status ds;
675 struct usb_config_descriptor *cdp;
682 DPRINTFN(6, "udev=%p index=%d\n", udev, index);
684 /* Prevent re-enumeration */
685 do_unlock = usbd_enum_lock(udev);
687 usb_unconfigure(udev, 0);
689 if (index == USB_UNCONFIG_INDEX) {
691 * Leave unallocated when unconfiguring the
692 * device. "usb_unconfigure()" will also reset
693 * the current config number and index.
695 err = usbd_req_set_config(udev, NULL, USB_UNCONFIG_NO);
696 if (udev->state == USB_STATE_CONFIGURED)
697 usb_set_device_state(udev, USB_STATE_ADDRESSED);
700 /* get the full config descriptor */
701 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
702 /* save some memory */
703 err = usbd_req_get_descriptor_ptr(udev, &cdp,
704 (UDESC_CONFIG << 8) | index);
707 err = usbd_req_get_config_desc_full(udev,
713 /* set the new config descriptor */
717 /* Figure out if the device is self or bus powered. */
719 if ((!udev->flags.uq_bus_powered) &&
720 (cdp->bmAttributes & UC_SELF_POWERED) &&
721 (udev->flags.usb_mode == USB_MODE_HOST)) {
722 /* May be self powered. */
723 if (cdp->bmAttributes & UC_BUS_POWERED) {
724 /* Must ask device. */
725 err = usbd_req_get_device_status(udev, NULL, &ds);
727 DPRINTFN(0, "could not read "
728 "device status: %s\n",
730 } else if (UGETW(ds.wStatus) & UDS_SELF_POWERED) {
733 DPRINTF("status=0x%04x \n",
738 DPRINTF("udev=%p cdesc=%p (addr %d) cno=%d attr=0x%02x, "
739 "selfpowered=%d, power=%d\n",
741 udev->address, cdp->bConfigurationValue, cdp->bmAttributes,
742 selfpowered, cdp->bMaxPower * 2);
744 /* Check if we have enough power. */
745 power = cdp->bMaxPower * 2;
747 if (udev->parent_hub) {
748 max_power = udev->parent_hub->hub->portpower;
750 max_power = USB_MAX_POWER;
753 if (power > max_power) {
754 DPRINTFN(0, "power exceeded %d > %d\n", power, max_power);
755 err = USB_ERR_NO_POWER;
758 /* Only update "self_powered" in USB Host Mode */
759 if (udev->flags.usb_mode == USB_MODE_HOST) {
760 udev->flags.self_powered = selfpowered;
763 udev->curr_config_no = cdp->bConfigurationValue;
764 udev->curr_config_index = index;
765 usb_set_device_state(udev, USB_STATE_CONFIGURED);
767 /* Set the actual configuration value. */
768 err = usbd_req_set_config(udev, NULL, cdp->bConfigurationValue);
773 err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_ALLOC);
778 err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_INIT);
784 /* create device nodes for each endpoint */
785 usb_cdev_create(udev);
789 DPRINTF("error=%s\n", usbd_errstr(err));
791 usb_unconfigure(udev, 0);
794 usbd_enum_unlock(udev);
798 /*------------------------------------------------------------------------*
801 * This function will allocate and free USB interfaces and USB endpoints,
802 * parse the USB configuration structure and initialise the USB endpoints
803 * and interfaces. If "iface_index" is not equal to
804 * "USB_IFACE_INDEX_ANY" then the "cmd" parameter is the
805 * alternate_setting to be selected for the given interface. Else the
806 * "cmd" parameter is defined by "USB_CFG_XXX". "iface_index" can be
807 * "USB_IFACE_INDEX_ANY" or a valid USB interface index. This function
808 * is typically called when setting the configuration or when setting
809 * an alternate interface.
814 *------------------------------------------------------------------------*/
816 usb_config_parse(struct usb_device *udev, uint8_t iface_index, uint8_t cmd)
818 struct usb_idesc_parse_state ips;
819 struct usb_interface_descriptor *id;
820 struct usb_endpoint_descriptor *ed;
821 struct usb_interface *iface;
822 struct usb_endpoint *ep;
830 if (iface_index != USB_IFACE_INDEX_ANY) {
831 /* parameter overload */
841 DPRINTFN(5, "iface_index=%d cmd=%d\n",
844 if (cmd == USB_CFG_FREE)
847 if (cmd == USB_CFG_INIT) {
848 sx_assert(&udev->enum_sx, SA_LOCKED);
850 /* check for in-use endpoints */
852 ep = udev->endpoints;
853 ep_max = udev->endpoints_max;
855 /* look for matching endpoints */
856 if ((iface_index == USB_IFACE_INDEX_ANY) ||
857 (iface_index == ep->iface_index)) {
858 if (ep->refcount_alloc != 0) {
860 * This typically indicates a
861 * more serious error.
863 err = USB_ERR_IN_USE;
866 memset(ep, 0, sizeof(*ep));
867 /* make sure we don't zero the endpoint again */
868 ep->iface_index = USB_IFACE_INDEX_ANY;
878 memset(&ips, 0, sizeof(ips));
883 while ((id = usb_idesc_foreach(udev->cdesc, &ips))) {
884 iface = udev->ifaces + ips.iface_index;
886 /* check for specific interface match */
888 if (cmd == USB_CFG_INIT) {
889 if ((iface_index != USB_IFACE_INDEX_ANY) &&
890 (iface_index != ips.iface_index)) {
891 /* wrong interface */
893 } else if (alt_index != ips.iface_index_alt) {
894 /* wrong alternate setting */
897 /* initialise interface */
903 /* check for new interface */
904 if (ips.iface_index_alt == 0) {
905 /* update current number of endpoints */
910 /* setup the USB interface structure */
912 /* set alternate index */
913 iface->alt_index = alt_index;
914 /* set default interface parent */
915 if (iface_index == USB_IFACE_INDEX_ANY) {
916 iface->parent_iface_index =
921 DPRINTFN(5, "found idesc nendpt=%d\n", id->bNumEndpoints);
923 ed = (struct usb_endpoint_descriptor *)id;
927 /* iterate all the endpoint descriptors */
928 while ((ed = usb_edesc_foreach(udev->cdesc, ed))) {
929 /* check if endpoint limit has been reached */
930 if (temp >= USB_MAX_EP_UNITS) {
931 DPRINTF("Endpoint limit reached\n");
935 ep = udev->endpoints + temp;
940 ecomp = usb_ed_comp_foreach(udev->cdesc, (void *)ed);
942 DPRINTFN(5, "Found endpoint companion descriptor\n");
944 usb_init_endpoint(udev,
945 ips.iface_index, ed, ecomp, ep);
950 /* find maximum number of endpoints */
956 /* NOTE: It is valid to have no interfaces and no endpoints! */
958 if (cmd == USB_CFG_ALLOC) {
959 udev->ifaces_max = ips.iface_index;
960 #if (USB_HAVE_FIXED_IFACE == 0)
962 if (udev->ifaces_max != 0) {
963 udev->ifaces = malloc(sizeof(*iface) * udev->ifaces_max,
964 M_USB, M_WAITOK | M_ZERO);
965 if (udev->ifaces == NULL) {
971 #if (USB_HAVE_FIXED_ENDPOINT == 0)
973 udev->endpoints = malloc(sizeof(*ep) * ep_max,
974 M_USB, M_WAITOK | M_ZERO);
975 if (udev->endpoints == NULL) {
980 udev->endpoints = NULL;
983 USB_BUS_LOCK(udev->bus);
984 udev->endpoints_max = ep_max;
985 /* reset any ongoing clear-stall */
986 udev->ep_curr = NULL;
987 USB_BUS_UNLOCK(udev->bus);
989 #if (USB_HAVE_FIXED_IFACE == 0) || (USB_HAVE_FIXED_ENDPOINT == 0)
993 if (cmd == USB_CFG_ALLOC) {
995 USB_BUS_LOCK(udev->bus);
996 udev->endpoints_max = 0;
997 /* reset any ongoing clear-stall */
998 udev->ep_curr = NULL;
999 USB_BUS_UNLOCK(udev->bus);
1001 #if (USB_HAVE_FIXED_IFACE == 0)
1002 free(udev->ifaces, M_USB);
1003 udev->ifaces = NULL;
1005 #if (USB_HAVE_FIXED_ENDPOINT == 0)
1006 free(udev->endpoints, M_USB);
1007 udev->endpoints = NULL;
1009 udev->ifaces_max = 0;
1015 /*------------------------------------------------------------------------*
1016 * usbd_set_alt_interface_index
1018 * This function will select an alternate interface index for the
1019 * given interface index. The interface should not be in use when this
1020 * function is called. That means there should not be any open USB
1021 * transfers. Else an error is returned. If the alternate setting is
1022 * already set this function will simply return success. This function
1023 * is called in Host mode and Device mode!
1028 *------------------------------------------------------------------------*/
1030 usbd_set_alt_interface_index(struct usb_device *udev,
1031 uint8_t iface_index, uint8_t alt_index)
1033 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1037 /* Prevent re-enumeration */
1038 do_unlock = usbd_enum_lock(udev);
1040 if (iface == NULL) {
1041 err = USB_ERR_INVAL;
1044 if (iface->alt_index == alt_index) {
1046 * Optimise away duplicate setting of
1047 * alternate setting in USB Host Mode!
1054 * Free all generic FIFOs for this interface, except control
1057 usb_fifo_free_wrap(udev, iface_index, 0);
1060 err = usb_config_parse(udev, iface_index, alt_index);
1064 if (iface->alt_index != alt_index) {
1065 /* the alternate setting does not exist */
1066 err = USB_ERR_INVAL;
1070 err = usbd_req_set_alt_interface_no(udev, NULL, iface_index,
1071 iface->idesc->bAlternateSetting);
1075 usbd_enum_unlock(udev);
1079 /*------------------------------------------------------------------------*
1080 * usbd_set_endpoint_stall
1082 * This function is used to make a BULK or INTERRUPT endpoint send
1083 * STALL tokens in USB device mode.
1088 *------------------------------------------------------------------------*/
1090 usbd_set_endpoint_stall(struct usb_device *udev, struct usb_endpoint *ep,
1093 struct usb_xfer *xfer;
1096 uint8_t was_stalled;
1100 DPRINTF("Cannot find endpoint\n");
1102 * Pretend that the clear or set stall request is
1103 * successful else some USB host stacks can do
1104 * strange things, especially when a control endpoint
1109 et = (ep->edesc->bmAttributes & UE_XFERTYPE);
1111 if ((et != UE_BULK) &&
1112 (et != UE_INTERRUPT)) {
1114 * Should not stall control
1115 * nor isochronous endpoints.
1117 DPRINTF("Invalid endpoint\n");
1120 USB_BUS_LOCK(udev->bus);
1122 /* store current stall state */
1123 was_stalled = ep->is_stalled;
1125 /* check for no change */
1126 if (was_stalled && do_stall) {
1127 /* if the endpoint is already stalled do nothing */
1128 USB_BUS_UNLOCK(udev->bus);
1129 DPRINTF("No change\n");
1132 /* set stalled state */
1135 if (do_stall || (!was_stalled)) {
1137 for (x = 0; x != USB_MAX_EP_STREAMS; x++) {
1138 /* lookup the current USB transfer, if any */
1139 xfer = ep->endpoint_q[x].curr;
1142 * The "xfer_stall" method
1143 * will complete the USB
1144 * transfer like in case of a
1145 * timeout setting the error
1146 * code "USB_ERR_STALLED".
1148 (udev->bus->methods->xfer_stall) (xfer);
1152 (udev->bus->methods->set_stall) (udev, ep, &do_stall);
1155 ep->toggle_next = 0; /* reset data toggle */
1156 ep->is_stalled = 0; /* clear stalled state */
1158 (udev->bus->methods->clear_stall) (udev, ep);
1160 /* start the current or next transfer, if any */
1161 for (x = 0; x != USB_MAX_EP_STREAMS; x++) {
1162 usb_command_wrapper(&ep->endpoint_q[x],
1163 ep->endpoint_q[x].curr);
1166 USB_BUS_UNLOCK(udev->bus);
1170 /*------------------------------------------------------------------------*
1171 * usb_reset_iface_endpoints - used in USB device side mode
1172 *------------------------------------------------------------------------*/
1174 usb_reset_iface_endpoints(struct usb_device *udev, uint8_t iface_index)
1176 struct usb_endpoint *ep;
1177 struct usb_endpoint *ep_end;
1179 ep = udev->endpoints;
1180 ep_end = udev->endpoints + udev->endpoints_max;
1182 for (; ep != ep_end; ep++) {
1183 if ((ep->edesc == NULL) ||
1184 (ep->iface_index != iface_index)) {
1187 /* simulate a clear stall from the peer */
1188 usbd_set_endpoint_stall(udev, ep, 0);
1193 /*------------------------------------------------------------------------*
1194 * usb_detach_device_sub
1196 * This function will try to detach an USB device. If it fails a panic
1199 * Flag values, see "USB_UNCFG_FLAG_XXX".
1200 *------------------------------------------------------------------------*/
1202 usb_detach_device_sub(struct usb_device *udev, device_t *ppdev,
1203 char **ppnpinfo, uint8_t flag)
1212 * NOTE: It is important to clear "*ppdev" before deleting
1213 * the child due to some device methods being called late
1214 * during the delete process !
1219 device_printf(dev, "at %s, port %d, addr %d "
1221 device_get_nameunit(udev->parent_dev),
1222 udev->port_no, udev->address);
1225 if (device_is_attached(dev)) {
1226 if (udev->flags.peer_suspended) {
1227 err = DEVICE_RESUME(dev);
1229 device_printf(dev, "Resume failed\n");
1233 /* detach and delete child */
1234 if (device_delete_child(udev->parent_dev, dev)) {
1239 pnpinfo = *ppnpinfo;
1240 if (pnpinfo != NULL) {
1242 free(pnpinfo, M_USBDEV);
1247 /* Detach is not allowed to fail in the USB world */
1248 panic("usb_detach_device_sub: A USB driver would not detach\n");
1251 /*------------------------------------------------------------------------*
1254 * The following function will detach the matching interfaces.
1255 * This function is NULL safe.
1257 * Flag values, see "USB_UNCFG_FLAG_XXX".
1258 *------------------------------------------------------------------------*/
1260 usb_detach_device(struct usb_device *udev, uint8_t iface_index,
1263 struct usb_interface *iface;
1270 DPRINTFN(4, "udev=%p\n", udev);
1272 sx_assert(&udev->enum_sx, SA_LOCKED);
1275 * First detach the child to give the child's detach routine a
1276 * chance to detach the sub-devices in the correct order.
1277 * Then delete the child using "device_delete_child()" which
1278 * will detach all sub-devices from the bottom and upwards!
1280 if (iface_index != USB_IFACE_INDEX_ANY) {
1282 iface_index = i + 1;
1285 iface_index = USB_IFACE_MAX;
1290 for (; i != iface_index; i++) {
1291 iface = usbd_get_iface(udev, i);
1292 if (iface == NULL) {
1293 /* looks like the end of the USB interfaces */
1296 usb_detach_device_sub(udev, &iface->subdev,
1297 &iface->pnpinfo, flag);
1301 /*------------------------------------------------------------------------*
1302 * usb_probe_and_attach_sub
1307 *------------------------------------------------------------------------*/
1309 usb_probe_and_attach_sub(struct usb_device *udev,
1310 struct usb_attach_arg *uaa)
1312 struct usb_interface *iface;
1317 if (iface->parent_iface_index != USB_IFACE_INDEX_ANY) {
1318 /* leave interface alone */
1321 dev = iface->subdev;
1323 /* clean up after module unload */
1325 if (device_is_attached(dev)) {
1326 /* already a device there */
1329 /* clear "iface->subdev" as early as possible */
1331 iface->subdev = NULL;
1333 if (device_delete_child(udev->parent_dev, dev)) {
1335 * Panic here, else one can get a double call
1336 * to device_detach(). USB devices should
1337 * never fail on detach!
1339 panic("device_delete_child() failed\n");
1342 if (uaa->temp_dev == NULL) {
1343 /* create a new child */
1344 uaa->temp_dev = device_add_child(udev->parent_dev, NULL, -1);
1345 if (uaa->temp_dev == NULL) {
1346 device_printf(udev->parent_dev,
1347 "Device creation failed\n");
1348 return (1); /* failure */
1350 device_set_ivars(uaa->temp_dev, uaa);
1351 device_quiet(uaa->temp_dev);
1354 * Set "subdev" before probe and attach so that "devd" gets
1355 * the information it needs.
1357 iface->subdev = uaa->temp_dev;
1359 if (device_probe_and_attach(iface->subdev) == 0) {
1361 * The USB attach arguments are only available during probe
1364 uaa->temp_dev = NULL;
1365 device_set_ivars(iface->subdev, NULL);
1367 if (udev->flags.peer_suspended) {
1368 err = DEVICE_SUSPEND(iface->subdev);
1370 device_printf(iface->subdev, "Suspend failed\n");
1372 return (0); /* success */
1374 /* No USB driver found */
1375 iface->subdev = NULL;
1377 return (1); /* failure */
1380 /*------------------------------------------------------------------------*
1381 * usbd_set_parent_iface
1383 * Using this function will lock the alternate interface setting on an
1384 * interface. It is typically used for multi interface drivers. In USB
1385 * device side mode it is assumed that the alternate interfaces all
1386 * have the same endpoint descriptors. The default parent index value
1387 * is "USB_IFACE_INDEX_ANY". Then the alternate setting value is not
1389 *------------------------------------------------------------------------*/
1391 usbd_set_parent_iface(struct usb_device *udev, uint8_t iface_index,
1392 uint8_t parent_index)
1394 struct usb_interface *iface;
1396 if (udev == NULL || iface_index == parent_index) {
1400 iface = usbd_get_iface(udev, iface_index);
1402 iface->parent_iface_index = parent_index;
1406 usb_init_attach_arg(struct usb_device *udev,
1407 struct usb_attach_arg *uaa)
1409 memset(uaa, 0, sizeof(*uaa));
1412 uaa->usb_mode = udev->flags.usb_mode;
1413 uaa->port = udev->port_no;
1414 uaa->dev_state = UAA_DEV_READY;
1416 uaa->info.idVendor = UGETW(udev->ddesc.idVendor);
1417 uaa->info.idProduct = UGETW(udev->ddesc.idProduct);
1418 uaa->info.bcdDevice = UGETW(udev->ddesc.bcdDevice);
1419 uaa->info.bDeviceClass = udev->ddesc.bDeviceClass;
1420 uaa->info.bDeviceSubClass = udev->ddesc.bDeviceSubClass;
1421 uaa->info.bDeviceProtocol = udev->ddesc.bDeviceProtocol;
1422 uaa->info.bConfigIndex = udev->curr_config_index;
1423 uaa->info.bConfigNum = udev->curr_config_no;
1426 /*------------------------------------------------------------------------*
1427 * usb_probe_and_attach
1429 * This function is called from "uhub_explore_sub()",
1430 * "usb_handle_set_config()" and "usb_handle_request()".
1434 * Else: A control transfer failed
1435 *------------------------------------------------------------------------*/
1437 usb_probe_and_attach(struct usb_device *udev, uint8_t iface_index)
1439 struct usb_attach_arg uaa;
1440 struct usb_interface *iface;
1446 DPRINTF("udev == NULL\n");
1447 return (USB_ERR_INVAL);
1449 /* Prevent re-enumeration */
1450 do_unlock = usbd_enum_lock(udev);
1452 if (udev->curr_config_index == USB_UNCONFIG_INDEX) {
1453 /* do nothing - no configuration has been set */
1456 /* setup USB attach arguments */
1458 usb_init_attach_arg(udev, &uaa);
1461 * If the whole USB device is targeted, invoke the USB event
1464 if (iface_index == USB_IFACE_INDEX_ANY) {
1465 if (usb_test_quirk(&uaa, UQ_MSC_DYMO_EJECT) != 0 &&
1466 usb_dymo_eject(udev, 0) == 0) {
1467 /* success, mark the udev as disappearing */
1468 uaa.dev_state = UAA_DEV_EJECTING;
1471 EVENTHANDLER_INVOKE(usb_dev_configured, udev, &uaa);
1473 if (uaa.dev_state != UAA_DEV_READY) {
1474 /* leave device unconfigured */
1475 usb_unconfigure(udev, 0);
1480 /* Check if only one interface should be probed: */
1481 if (iface_index != USB_IFACE_INDEX_ANY) {
1489 /* Do the probe and attach */
1490 for (; i != j; i++) {
1491 iface = usbd_get_iface(udev, i);
1492 if (iface == NULL) {
1494 * Looks like the end of the USB
1497 DPRINTFN(2, "end of interfaces "
1501 if (iface->idesc == NULL) {
1502 /* no interface descriptor */
1507 uaa.info.bInterfaceClass =
1508 iface->idesc->bInterfaceClass;
1509 uaa.info.bInterfaceSubClass =
1510 iface->idesc->bInterfaceSubClass;
1511 uaa.info.bInterfaceProtocol =
1512 iface->idesc->bInterfaceProtocol;
1513 uaa.info.bIfaceIndex = i;
1514 uaa.info.bIfaceNum =
1515 iface->idesc->bInterfaceNumber;
1516 uaa.driver_info = 0; /* reset driver_info */
1518 DPRINTFN(2, "iclass=%u/%u/%u iindex=%u/%u\n",
1519 uaa.info.bInterfaceClass,
1520 uaa.info.bInterfaceSubClass,
1521 uaa.info.bInterfaceProtocol,
1522 uaa.info.bIfaceIndex,
1523 uaa.info.bIfaceNum);
1525 usb_probe_and_attach_sub(udev, &uaa);
1528 * Remove the leftover child, if any, to enforce that
1529 * a new nomatch devd event is generated for the next
1530 * interface if no driver is found:
1532 if (uaa.temp_dev == NULL)
1534 if (device_delete_child(udev->parent_dev, uaa.temp_dev))
1535 DPRINTFN(0, "device delete child failed\n");
1536 uaa.temp_dev = NULL;
1540 usbd_enum_unlock(udev);
1544 /*------------------------------------------------------------------------*
1545 * usb_suspend_resume_sub
1547 * This function is called when the suspend or resume methods should
1548 * be executed on an USB device.
1549 *------------------------------------------------------------------------*/
1551 usb_suspend_resume_sub(struct usb_device *udev, device_t dev, uint8_t do_suspend)
1558 if (!device_is_attached(dev)) {
1562 err = DEVICE_SUSPEND(dev);
1564 err = DEVICE_RESUME(dev);
1567 device_printf(dev, "%s failed\n",
1568 do_suspend ? "Suspend" : "Resume");
1572 /*------------------------------------------------------------------------*
1573 * usb_suspend_resume
1575 * The following function will suspend or resume the USB device.
1580 *------------------------------------------------------------------------*/
1582 usb_suspend_resume(struct usb_device *udev, uint8_t do_suspend)
1584 struct usb_interface *iface;
1591 DPRINTFN(4, "udev=%p do_suspend=%d\n", udev, do_suspend);
1593 sx_assert(&udev->sr_sx, SA_LOCKED);
1595 USB_BUS_LOCK(udev->bus);
1596 /* filter the suspend events */
1597 if (udev->flags.peer_suspended == do_suspend) {
1598 USB_BUS_UNLOCK(udev->bus);
1602 udev->flags.peer_suspended = do_suspend;
1603 USB_BUS_UNLOCK(udev->bus);
1605 /* do the suspend or resume */
1607 for (i = 0; i != USB_IFACE_MAX; i++) {
1608 iface = usbd_get_iface(udev, i);
1609 if (iface == NULL) {
1610 /* looks like the end of the USB interfaces */
1613 usb_suspend_resume_sub(udev, iface->subdev, do_suspend);
1618 /*------------------------------------------------------------------------*
1619 * usbd_clear_stall_proc
1621 * This function performs generic USB clear stall operations.
1622 *------------------------------------------------------------------------*/
1624 usbd_clear_stall_proc(struct usb_proc_msg *_pm)
1626 struct usb_udev_msg *pm = (void *)_pm;
1627 struct usb_device *udev = pm->udev;
1630 USB_BUS_UNLOCK(udev->bus);
1631 USB_MTX_LOCK(&udev->device_mtx);
1633 /* Start clear stall callback */
1634 usbd_transfer_start(udev->ctrl_xfer[1]);
1637 USB_MTX_UNLOCK(&udev->device_mtx);
1638 USB_BUS_LOCK(udev->bus);
1641 /*------------------------------------------------------------------------*
1644 * This function tries to figure out the USB string language to use.
1645 *------------------------------------------------------------------------*/
1647 usb_get_langid(struct usb_device *udev)
1649 uint8_t *scratch_ptr;
1654 * Workaround for buggy USB devices.
1656 * It appears that some string-less USB chips will crash and
1657 * disappear if any attempts are made to read any string
1660 * Try to detect such chips by checking the strings in the USB
1661 * device descriptor. If no strings are present there we
1662 * simply disable all USB strings.
1665 /* Protect scratch area */
1666 do_unlock = usbd_ctrl_lock(udev);
1668 scratch_ptr = udev->scratch.data;
1670 if (udev->flags.no_strings) {
1671 err = USB_ERR_INVAL;
1672 } else if (udev->ddesc.iManufacturer ||
1673 udev->ddesc.iProduct ||
1674 udev->ddesc.iSerialNumber) {
1675 /* read out the language ID string */
1676 err = usbd_req_get_string_desc(udev, NULL,
1677 (char *)scratch_ptr, 4, 0, USB_LANGUAGE_TABLE);
1679 err = USB_ERR_INVAL;
1682 if (err || (scratch_ptr[0] < 4)) {
1683 udev->flags.no_strings = 1;
1690 /* load preferred value and mask */
1692 mask = usb_lang_mask;
1694 /* align length correctly */
1695 scratch_ptr[0] &= ~1U;
1697 /* fix compiler warning */
1700 /* search for preferred language */
1701 for (x = 2; x < scratch_ptr[0]; x += 2) {
1702 langid = UGETW(scratch_ptr + x);
1703 if ((langid & mask) == pref)
1706 if (x >= scratch_ptr[0]) {
1707 /* pick the first language as the default */
1708 DPRINTFN(1, "Using first language\n");
1709 langid = UGETW(scratch_ptr + 2);
1712 DPRINTFN(1, "Language selected: 0x%04x\n", langid);
1713 udev->langid = langid;
1717 usbd_ctrl_unlock(udev);
1720 /*------------------------------------------------------------------------*
1723 * This function allocates a new USB device. This function is called
1724 * when a new device has been put in the powered state, but not yet in
1725 * the addressed state. Get initial descriptor, set the address, get
1726 * full descriptor and get strings.
1731 *------------------------------------------------------------------------*/
1733 usb_alloc_device(device_t parent_dev, struct usb_bus *bus,
1734 struct usb_device *parent_hub, uint8_t depth, uint8_t port_index,
1735 uint8_t port_no, enum usb_dev_speed speed, enum usb_hc_mode mode)
1737 struct usb_attach_arg uaa;
1738 struct usb_device *udev;
1739 struct usb_device *adev;
1740 struct usb_device *hub;
1742 uint8_t device_index;
1743 uint8_t config_index;
1744 uint8_t config_quirk;
1745 uint8_t set_config_failed;
1747 DPRINTF("parent_dev=%p, bus=%p, parent_hub=%p, depth=%u, "
1748 "port_index=%u, port_no=%u, speed=%u, usb_mode=%u\n",
1749 parent_dev, bus, parent_hub, depth, port_index, port_no,
1753 * Find an unused device index. In USB Host mode this is the
1754 * same as the device address.
1756 * Device index zero is not used and device index 1 should
1757 * always be the root hub.
1759 for (device_index = USB_ROOT_HUB_ADDR;
1760 (device_index != bus->devices_max) &&
1761 (bus->devices[device_index] != NULL);
1762 device_index++) /* nop */;
1764 if (device_index == bus->devices_max) {
1765 device_printf(bus->bdev,
1766 "No free USB device index for new device\n");
1771 device_printf(bus->bdev,
1772 "Invalid device depth\n");
1775 udev = malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO);
1776 #if (USB_HAVE_MALLOC_WAITOK == 0)
1781 /* initialise our SX-lock */
1782 sx_init_flags(&udev->enum_sx, "USB config SX lock", SX_DUPOK);
1783 sx_init_flags(&udev->sr_sx, "USB suspend and resume SX lock", SX_NOWITNESS);
1784 sx_init_flags(&udev->ctrl_sx, "USB control transfer SX lock", SX_DUPOK);
1786 cv_init(&udev->ctrlreq_cv, "WCTRL");
1787 cv_init(&udev->ref_cv, "UGONE");
1789 /* initialise our mutex */
1790 mtx_init(&udev->device_mtx, "USB device mutex", NULL, MTX_DEF);
1792 /* initialise generic clear stall */
1793 udev->cs_msg[0].hdr.pm_callback = &usbd_clear_stall_proc;
1794 udev->cs_msg[0].udev = udev;
1795 udev->cs_msg[1].hdr.pm_callback = &usbd_clear_stall_proc;
1796 udev->cs_msg[1].udev = udev;
1798 /* initialise some USB device fields */
1799 udev->parent_hub = parent_hub;
1800 udev->parent_dev = parent_dev;
1801 udev->port_index = port_index;
1802 udev->port_no = port_no;
1803 udev->depth = depth;
1805 udev->address = USB_START_ADDR; /* default value */
1806 udev->plugtime = (usb_ticks_t)ticks;
1808 * We need to force the power mode to "on" because there are plenty
1809 * of USB devices out there that do not work very well with
1810 * automatic suspend and resume!
1812 udev->power_mode = usbd_filter_power_mode(udev, USB_POWER_MODE_ON);
1813 udev->pwr_save.last_xfer_time = ticks;
1814 /* we are not ready yet */
1817 /* set up default endpoint descriptor */
1818 udev->ctrl_ep_desc.bLength = sizeof(udev->ctrl_ep_desc);
1819 udev->ctrl_ep_desc.bDescriptorType = UDESC_ENDPOINT;
1820 udev->ctrl_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
1821 udev->ctrl_ep_desc.bmAttributes = UE_CONTROL;
1822 udev->ctrl_ep_desc.wMaxPacketSize[0] = USB_MAX_IPACKET;
1823 udev->ctrl_ep_desc.wMaxPacketSize[1] = 0;
1824 udev->ctrl_ep_desc.bInterval = 0;
1826 /* set up default endpoint companion descriptor */
1827 udev->ctrl_ep_comp_desc.bLength = sizeof(udev->ctrl_ep_comp_desc);
1828 udev->ctrl_ep_comp_desc.bDescriptorType = UDESC_ENDPOINT_SS_COMP;
1830 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
1832 udev->speed = speed;
1833 udev->flags.usb_mode = mode;
1835 /* search for our High Speed USB HUB, if any */
1838 hub = udev->parent_hub;
1841 if (hub->speed == USB_SPEED_HIGH) {
1842 udev->hs_hub_addr = hub->address;
1843 udev->parent_hs_hub = hub;
1844 udev->hs_port_no = adev->port_no;
1848 hub = hub->parent_hub;
1851 /* init the default endpoint */
1852 usb_init_endpoint(udev, 0,
1853 &udev->ctrl_ep_desc,
1854 &udev->ctrl_ep_comp_desc,
1857 /* set device index */
1858 udev->device_index = device_index;
1861 /* Create ugen name */
1862 snprintf(udev->ugen_name, sizeof(udev->ugen_name),
1863 USB_GENERIC_NAME "%u.%u", device_get_unit(bus->bdev),
1865 LIST_INIT(&udev->pd_list);
1867 /* Create the control endpoint device */
1868 udev->ctrl_dev = usb_make_dev(udev, NULL, 0, 0,
1869 FREAD|FWRITE, UID_ROOT, GID_OPERATOR, 0600);
1871 /* Create a link from /dev/ugenX.X to the default endpoint */
1872 if (udev->ctrl_dev != NULL)
1873 make_dev_alias(udev->ctrl_dev->cdev, "%s", udev->ugen_name);
1875 /* Initialise device */
1876 if (bus->methods->device_init != NULL) {
1877 err = (bus->methods->device_init) (udev);
1879 DPRINTFN(0, "device init %d failed "
1880 "(%s, ignored)\n", device_index,
1885 /* set powered device state after device init is complete */
1886 usb_set_device_state(udev, USB_STATE_POWERED);
1888 if (udev->flags.usb_mode == USB_MODE_HOST) {
1889 err = usbd_req_set_address(udev, NULL, device_index);
1892 * This is the new USB device address from now on, if
1893 * the set address request didn't set it already.
1895 if (udev->address == USB_START_ADDR)
1896 udev->address = device_index;
1899 * We ignore any set-address errors, hence there are
1900 * buggy USB devices out there that actually receive
1901 * the SETUP PID, but manage to set the address before
1902 * the STATUS stage is ACK'ed. If the device responds
1903 * to the subsequent get-descriptor at the new
1904 * address, then we know that the set-address command
1908 DPRINTFN(0, "set address %d failed "
1909 "(%s, ignored)\n", udev->address,
1913 /* We are not self powered */
1914 udev->flags.self_powered = 0;
1916 /* Set unconfigured state */
1917 udev->curr_config_no = USB_UNCONFIG_NO;
1918 udev->curr_config_index = USB_UNCONFIG_INDEX;
1920 /* Setup USB descriptors */
1921 err = (usb_temp_setup_by_index_p) (udev, usb_template);
1923 DPRINTFN(0, "setting up USB template failed - "
1924 "usb_template(4) not loaded?\n");
1928 usb_set_device_state(udev, USB_STATE_ADDRESSED);
1930 /* setup the device descriptor and the initial "wMaxPacketSize" */
1931 err = usbd_setup_device_desc(udev, NULL);
1934 /* try to enumerate two more times */
1935 err = usbd_req_re_enumerate(udev, NULL);
1937 err = usbd_req_re_enumerate(udev, NULL);
1945 * Setup temporary USB attach args so that we can figure out some
1946 * basic quirks for this device.
1948 usb_init_attach_arg(udev, &uaa);
1950 if (usb_test_quirk(&uaa, UQ_BUS_POWERED)) {
1951 udev->flags.uq_bus_powered = 1;
1953 if (usb_test_quirk(&uaa, UQ_NO_STRINGS)) {
1954 udev->flags.no_strings = 1;
1957 usb_get_langid(udev);
1959 /* assume 100mA bus powered for now. Changed when configured. */
1960 udev->power = USB_MIN_POWER;
1961 /* fetch the vendor and product strings from the device */
1962 usb_set_device_strings(udev);
1964 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
1965 /* USB device mode setup is complete */
1971 * Most USB devices should attach to config index 0 by
1974 if (usb_test_quirk(&uaa, UQ_CFG_INDEX_0)) {
1977 } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_1)) {
1980 } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_2)) {
1983 } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_3)) {
1986 } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_4)) {
1994 set_config_failed = 0;
1997 DPRINTF("setting config %u\n", config_index);
1999 /* get the USB device configured */
2000 err = usbd_set_config_index(udev, config_index);
2002 if (udev->ddesc.bNumConfigurations != 0) {
2003 if (!set_config_failed) {
2004 set_config_failed = 1;
2005 /* XXX try to re-enumerate the device */
2006 err = usbd_req_re_enumerate(udev, NULL);
2008 goto repeat_set_config;
2010 DPRINTFN(0, "Failure selecting configuration index %u:"
2011 "%s, port %u, addr %u (ignored)\n",
2012 config_index, usbd_errstr(err), udev->port_no,
2016 * Some USB devices do not have any configurations. Ignore any
2017 * set config failures!
2022 if (!config_quirk && config_index + 1 < udev->ddesc.bNumConfigurations) {
2023 if ((udev->cdesc->bNumInterface < 2) &&
2024 usbd_get_no_descriptors(udev->cdesc, UDESC_ENDPOINT) == 0) {
2025 DPRINTFN(0, "Found no endpoints, trying next config\n");
2027 goto repeat_set_config;
2029 #if USB_HAVE_MSCTEST
2030 if (config_index == 0) {
2032 * Try to figure out if we have an
2033 * auto-install disk there:
2035 if (usb_iface_is_cdrom(udev, 0)) {
2036 DPRINTFN(0, "Found possible auto-install "
2037 "disk (trying next config)\n");
2039 goto repeat_set_config;
2044 #if USB_HAVE_MSCTEST
2045 if (set_config_failed == 0 && config_index == 0 &&
2046 usb_test_quirk(&uaa, UQ_MSC_NO_SYNC_CACHE) == 0 &&
2047 usb_test_quirk(&uaa, UQ_MSC_NO_GETMAXLUN) == 0) {
2049 * Try to figure out if there are any MSC quirks we
2050 * should apply automatically:
2052 err = usb_msc_auto_quirk(udev, 0);
2055 set_config_failed = 1;
2056 goto repeat_set_config;
2062 DPRINTF("new dev (addr %d), udev=%p, parent_hub=%p\n",
2063 udev->address, udev, udev->parent_hub);
2065 /* register our device - we are ready */
2066 usb_bus_port_set_device(bus, parent_hub ?
2067 parent_hub->hub->ports + port_index : NULL, udev, device_index);
2070 /* Symlink the ugen device name */
2071 udev->ugen_symlink = usb_alloc_symlink(udev->ugen_name);
2073 /* Announce device */
2074 printf("%s: <%s %s> at %s\n", udev->ugen_name,
2075 usb_get_manufacturer(udev), usb_get_product(udev),
2076 device_get_nameunit(udev->bus->bdev));
2080 usb_notify_addq("ATTACH", udev);
2085 * Free USB device and all subdevices, if any.
2087 usb_free_device(udev, 0);
2094 struct usb_fs_privdata *
2095 usb_make_dev(struct usb_device *udev, const char *devname, int ep,
2096 int fi, int rwmode, uid_t uid, gid_t gid, int mode)
2098 struct usb_fs_privdata* pd;
2099 struct make_dev_args args;
2102 /* Store information to locate ourselves again later */
2103 pd = malloc(sizeof(struct usb_fs_privdata), M_USBDEV,
2105 pd->bus_index = device_get_unit(udev->bus->bdev);
2106 pd->dev_index = udev->device_index;
2108 pd->fifo_index = fi;
2111 /* Now, create the device itself */
2112 if (devname == NULL) {
2114 snprintf(buffer, sizeof(buffer), USB_DEVICE_DIR "/%u.%u.%u",
2115 pd->bus_index, pd->dev_index, pd->ep_addr);
2118 /* Setup arguments for make_dev_s() */
2119 make_dev_args_init(&args);
2120 args.mda_devsw = &usb_devsw;
2123 args.mda_mode = mode;
2124 args.mda_si_drv1 = pd;
2126 if (make_dev_s(&args, &pd->cdev, "%s", devname) != 0) {
2127 DPRINTFN(0, "Failed to create device %s\n", devname);
2135 usb_destroy_dev_sync(struct usb_fs_privdata *pd)
2137 DPRINTFN(1, "Destroying device at ugen%d.%d\n",
2138 pd->bus_index, pd->dev_index);
2141 * Destroy character device synchronously. After this
2142 * all system calls are returned. Can block.
2144 destroy_dev(pd->cdev);
2150 usb_destroy_dev(struct usb_fs_privdata *pd)
2152 struct usb_bus *bus;
2157 mtx_lock(&usb_ref_lock);
2158 bus = devclass_get_softc(usb_devclass_ptr, pd->bus_index);
2159 mtx_unlock(&usb_ref_lock);
2162 usb_destroy_dev_sync(pd);
2166 /* make sure we can re-use the device name */
2167 delist_dev(pd->cdev);
2170 LIST_INSERT_HEAD(&bus->pd_cleanup_list, pd, pd_next);
2171 /* get cleanup going */
2172 usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
2173 &bus->cleanup_msg[0], &bus->cleanup_msg[1]);
2174 USB_BUS_UNLOCK(bus);
2178 usb_cdev_create(struct usb_device *udev)
2180 struct usb_config_descriptor *cd;
2181 struct usb_endpoint_descriptor *ed;
2182 struct usb_descriptor *desc;
2183 struct usb_fs_privdata* pd;
2184 int inmode, outmode, inmask, outmask, mode;
2187 KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("stale cdev entries"));
2189 DPRINTFN(2, "Creating device nodes\n");
2191 if (usbd_get_mode(udev) == USB_MODE_DEVICE) {
2194 } else { /* USB_MODE_HOST */
2204 * Collect all used endpoint numbers instead of just
2205 * generating 16 static endpoints.
2207 cd = usbd_get_config_descriptor(udev);
2208 while ((desc = usb_desc_foreach(cd, desc))) {
2209 /* filter out all endpoint descriptors */
2210 if ((desc->bDescriptorType == UDESC_ENDPOINT) &&
2211 (desc->bLength >= sizeof(*ed))) {
2212 ed = (struct usb_endpoint_descriptor *)desc;
2215 ep = ed->bEndpointAddress;
2216 if (UE_GET_DIR(ep) == UE_DIR_OUT)
2217 outmask |= 1 << UE_GET_ADDR(ep);
2219 inmask |= 1 << UE_GET_ADDR(ep);
2223 /* Create all available endpoints except EP0 */
2224 for (ep = 1; ep < 16; ep++) {
2225 mode = (inmask & (1 << ep)) ? inmode : 0;
2226 mode |= (outmask & (1 << ep)) ? outmode : 0;
2228 continue; /* no IN or OUT endpoint */
2230 pd = usb_make_dev(udev, NULL, ep, 0,
2231 mode, UID_ROOT, GID_OPERATOR, 0600);
2234 LIST_INSERT_HEAD(&udev->pd_list, pd, pd_next);
2239 usb_cdev_free(struct usb_device *udev)
2241 struct usb_fs_privdata* pd;
2243 DPRINTFN(2, "Freeing device nodes\n");
2245 while ((pd = LIST_FIRST(&udev->pd_list)) != NULL) {
2246 KASSERT(pd->cdev->si_drv1 == pd, ("privdata corrupt"));
2248 LIST_REMOVE(pd, pd_next);
2250 usb_destroy_dev(pd);
2255 /*------------------------------------------------------------------------*
2258 * This function is NULL safe and will free an USB device and its
2259 * children devices, if any.
2261 * Flag values: Reserved, set to zero.
2262 *------------------------------------------------------------------------*/
2264 usb_free_device(struct usb_device *udev, uint8_t flag)
2266 struct usb_bus *bus;
2269 return; /* already freed */
2271 DPRINTFN(4, "udev=%p port=%d\n", udev, udev->port_no);
2275 /* set DETACHED state to prevent any further references */
2276 usb_set_device_state(udev, USB_STATE_DETACHED);
2279 usb_notify_addq("DETACH", udev);
2284 printf("%s: <%s %s> at %s (disconnected)\n", udev->ugen_name,
2285 usb_get_manufacturer(udev), usb_get_product(udev),
2286 device_get_nameunit(bus->bdev));
2289 /* Destroy UGEN symlink, if any */
2290 if (udev->ugen_symlink) {
2291 usb_free_symlink(udev->ugen_symlink);
2292 udev->ugen_symlink = NULL;
2295 usb_destroy_dev(udev->ctrl_dev);
2298 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2299 /* stop receiving any control transfers (Device Side Mode) */
2300 usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
2303 /* the following will get the device unconfigured in software */
2304 usb_unconfigure(udev, USB_UNCFG_FLAG_FREE_EP0);
2306 /* final device unregister after all character devices are closed */
2307 usb_bus_port_set_device(bus, udev->parent_hub ?
2308 udev->parent_hub->hub->ports + udev->port_index : NULL,
2309 NULL, USB_ROOT_HUB_ADDR);
2311 /* unsetup any leftover default USB transfers */
2312 usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
2314 /* template unsetup, if any */
2315 (usb_temp_unsetup_p) (udev);
2318 * Make sure that our clear-stall messages are not queued
2321 USB_BUS_LOCK(udev->bus);
2322 usb_proc_mwait(USB_BUS_CS_PROC(udev->bus),
2323 &udev->cs_msg[0], &udev->cs_msg[1]);
2324 USB_BUS_UNLOCK(udev->bus);
2326 /* wait for all references to go away */
2327 usb_wait_pending_refs(udev);
2329 sx_destroy(&udev->enum_sx);
2330 sx_destroy(&udev->sr_sx);
2331 sx_destroy(&udev->ctrl_sx);
2333 cv_destroy(&udev->ctrlreq_cv);
2334 cv_destroy(&udev->ref_cv);
2336 mtx_destroy(&udev->device_mtx);
2338 KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("leaked cdev entries"));
2341 /* Uninitialise device */
2342 if (bus->methods->device_uninit != NULL)
2343 (bus->methods->device_uninit) (udev);
2346 free(udev->serial, M_USB);
2347 free(udev->manufacturer, M_USB);
2348 free(udev->product, M_USB);
2352 /*------------------------------------------------------------------------*
2355 * This function is the safe way to get the USB interface structure
2356 * pointer by interface index.
2359 * NULL: Interface not present.
2360 * Else: Pointer to USB interface structure.
2361 *------------------------------------------------------------------------*/
2362 struct usb_interface *
2363 usbd_get_iface(struct usb_device *udev, uint8_t iface_index)
2365 struct usb_interface *iface = udev->ifaces + iface_index;
2367 if (iface_index >= udev->ifaces_max)
2372 /*------------------------------------------------------------------------*
2373 * usbd_find_descriptor
2375 * This function will lookup the first descriptor that matches the
2376 * criteria given by the arguments "type" and "subtype". Descriptors
2377 * will only be searched within the interface having the index
2378 * "iface_index". If the "id" argument points to an USB descriptor,
2379 * it will be skipped before the search is started. This allows
2380 * searching for multiple descriptors using the same criteria. Else
2381 * the search is started after the interface descriptor.
2384 * NULL: End of descriptors
2385 * Else: A descriptor matching the criteria
2386 *------------------------------------------------------------------------*/
2388 usbd_find_descriptor(struct usb_device *udev, void *id, uint8_t iface_index,
2389 uint8_t type, uint8_t type_mask,
2390 uint8_t subtype, uint8_t subtype_mask)
2392 struct usb_descriptor *desc;
2393 struct usb_config_descriptor *cd;
2394 struct usb_interface *iface;
2396 cd = usbd_get_config_descriptor(udev);
2401 iface = usbd_get_iface(udev, iface_index);
2402 if (iface == NULL) {
2405 id = usbd_get_interface_descriptor(iface);
2412 while ((desc = usb_desc_foreach(cd, desc))) {
2413 if (desc->bDescriptorType == UDESC_INTERFACE) {
2416 if (((desc->bDescriptorType & type_mask) == type) &&
2417 ((desc->bDescriptorSubtype & subtype_mask) == subtype)) {
2424 /*------------------------------------------------------------------------*
2427 * This function will dump information from the device descriptor
2428 * belonging to the USB device pointed to by "udev", to the string
2429 * pointed to by "dst_ptr" having a maximum length of "dst_len" bytes
2430 * including the terminating zero.
2431 *------------------------------------------------------------------------*/
2433 usb_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len)
2435 struct usb_device_descriptor *udd = &udev->ddesc;
2439 bcdUSB = UGETW(udd->bcdUSB);
2440 bcdDevice = UGETW(udd->bcdDevice);
2442 if (udd->bDeviceClass != 0xFF) {
2443 snprintf(dst_ptr, dst_len, "%s %s, class %d/%d, rev %x.%02x/"
2445 usb_get_manufacturer(udev),
2446 usb_get_product(udev),
2447 udd->bDeviceClass, udd->bDeviceSubClass,
2448 (bcdUSB >> 8), bcdUSB & 0xFF,
2449 (bcdDevice >> 8), bcdDevice & 0xFF,
2452 snprintf(dst_ptr, dst_len, "%s %s, rev %x.%02x/"
2454 usb_get_manufacturer(udev),
2455 usb_get_product(udev),
2456 (bcdUSB >> 8), bcdUSB & 0xFF,
2457 (bcdDevice >> 8), bcdDevice & 0xFF,
2464 * Descriptions of of known vendors and devices ("products").
2466 struct usb_knowndev {
2470 const char *vendorname;
2471 const char *productname;
2474 #define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */
2476 #include "usbdevs.h"
2477 #include "usbdevs_data.h"
2478 #endif /* USB_VERBOSE */
2481 usb_set_device_strings(struct usb_device *udev)
2483 struct usb_device_descriptor *udd = &udev->ddesc;
2485 const struct usb_knowndev *kdp;
2490 uint16_t product_id;
2493 /* Protect scratch area */
2494 do_unlock = usbd_ctrl_lock(udev);
2496 temp_ptr = (char *)udev->scratch.data;
2497 temp_size = sizeof(udev->scratch.data);
2499 vendor_id = UGETW(udd->idVendor);
2500 product_id = UGETW(udd->idProduct);
2502 /* cleanup old strings, if any */
2503 free(udev->serial, M_USB);
2504 free(udev->manufacturer, M_USB);
2505 free(udev->product, M_USB);
2507 /* zero the string pointers */
2508 udev->serial = NULL;
2509 udev->manufacturer = NULL;
2510 udev->product = NULL;
2512 /* get serial number string */
2513 usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2514 udev->ddesc.iSerialNumber);
2515 udev->serial = strdup(temp_ptr, M_USB);
2517 /* get manufacturer string */
2518 usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2519 udev->ddesc.iManufacturer);
2520 usb_trim_spaces(temp_ptr);
2521 if (temp_ptr[0] != '\0')
2522 udev->manufacturer = strdup(temp_ptr, M_USB);
2524 /* get product string */
2525 usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2526 udev->ddesc.iProduct);
2527 usb_trim_spaces(temp_ptr);
2528 if (temp_ptr[0] != '\0')
2529 udev->product = strdup(temp_ptr, M_USB);
2532 if (udev->manufacturer == NULL || udev->product == NULL) {
2533 for (kdp = usb_knowndevs; kdp->vendorname != NULL; kdp++) {
2534 if (kdp->vendor == vendor_id &&
2535 (kdp->product == product_id ||
2536 (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
2539 if (kdp->vendorname != NULL) {
2540 /* XXX should use pointer to knowndevs string */
2541 if (udev->manufacturer == NULL) {
2542 udev->manufacturer = strdup(kdp->vendorname,
2545 if (udev->product == NULL &&
2546 (kdp->flags & USB_KNOWNDEV_NOPROD) == 0) {
2547 udev->product = strdup(kdp->productname,
2553 /* Provide default strings if none were found */
2554 if (udev->manufacturer == NULL) {
2555 snprintf(temp_ptr, temp_size, "vendor 0x%04x", vendor_id);
2556 udev->manufacturer = strdup(temp_ptr, M_USB);
2558 if (udev->product == NULL) {
2559 snprintf(temp_ptr, temp_size, "product 0x%04x", product_id);
2560 udev->product = strdup(temp_ptr, M_USB);
2564 usbd_ctrl_unlock(udev);
2572 usbd_get_mode(struct usb_device *udev)
2574 return (udev->flags.usb_mode);
2579 * See: USB_SPEED_XXX
2582 usbd_get_speed(struct usb_device *udev)
2584 return (udev->speed);
2588 usbd_get_isoc_fps(struct usb_device *udev)
2591 switch (udev->speed) {
2593 case USB_SPEED_FULL:
2600 struct usb_device_descriptor *
2601 usbd_get_device_descriptor(struct usb_device *udev)
2604 return (NULL); /* be NULL safe */
2605 return (&udev->ddesc);
2608 struct usb_config_descriptor *
2609 usbd_get_config_descriptor(struct usb_device *udev)
2612 return (NULL); /* be NULL safe */
2613 return (udev->cdesc);
2616 /*------------------------------------------------------------------------*
2617 * usb_test_quirk - test a device for a given quirk
2620 * 0: The USB device does not have the given quirk.
2621 * Else: The USB device has the given quirk.
2622 *------------------------------------------------------------------------*/
2624 usb_test_quirk(const struct usb_attach_arg *uaa, uint16_t quirk)
2629 if (quirk == UQ_NONE)
2632 /* search the automatic per device quirks first */
2634 for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) {
2635 if (uaa->device->autoQuirk[x] == quirk)
2639 /* search global quirk table, if any */
2641 found = (usb_test_quirk_p) (&uaa->info, quirk);
2646 struct usb_interface_descriptor *
2647 usbd_get_interface_descriptor(struct usb_interface *iface)
2650 return (NULL); /* be NULL safe */
2651 return (iface->idesc);
2655 usbd_get_interface_altindex(struct usb_interface *iface)
2657 return (iface->alt_index);
2661 usbd_get_bus_index(struct usb_device *udev)
2663 return ((uint8_t)device_get_unit(udev->bus->bdev));
2667 usbd_get_device_index(struct usb_device *udev)
2669 return (udev->device_index);
2674 usb_notify_addq(const char *type, struct usb_device *udev)
2676 struct usb_interface *iface;
2680 /* announce the device */
2681 sb = sbuf_new_auto();
2690 "devsubclass=0x%02x "
2703 UGETW(udev->ddesc.idVendor),
2704 UGETW(udev->ddesc.idProduct),
2705 udev->ddesc.bDeviceClass,
2706 udev->ddesc.bDeviceSubClass,
2707 usb_get_serial(udev),
2708 UGETW(udev->ddesc.bcdDevice),
2709 (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
2712 , udev->parent_hub != NULL ?
2713 udev->parent_hub->ugen_name :
2714 device_get_nameunit(device_get_parent(udev->bus->bdev))
2718 devctl_notify("USB", "DEVICE", type, sbuf_data(sb));
2721 /* announce each interface */
2722 for (i = 0; i < USB_IFACE_MAX; i++) {
2723 iface = usbd_get_iface(udev, i);
2725 break; /* end of interfaces */
2726 if (iface->idesc == NULL)
2727 continue; /* no interface descriptor */
2729 sb = sbuf_new_auto();
2738 "devsubclass=0x%02x "
2745 "intsubclass=0x%02x "
2746 "intprotocol=0x%02x",
2751 UGETW(udev->ddesc.idVendor),
2752 UGETW(udev->ddesc.idProduct),
2753 udev->ddesc.bDeviceClass,
2754 udev->ddesc.bDeviceSubClass,
2755 usb_get_serial(udev),
2756 UGETW(udev->ddesc.bcdDevice),
2757 (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
2758 iface->idesc->bInterfaceNumber,
2759 iface->idesc->bNumEndpoints,
2760 iface->idesc->bInterfaceClass,
2761 iface->idesc->bInterfaceSubClass,
2762 iface->idesc->bInterfaceProtocol);
2764 devctl_notify("USB", "INTERFACE", type, sbuf_data(sb));
2771 /*------------------------------------------------------------------------*
2772 * usb_fifo_free_wrap
2774 * This function will free the FIFOs.
2776 * Description of "flag" argument: If the USB_UNCFG_FLAG_FREE_EP0 flag
2777 * is set and "iface_index" is set to "USB_IFACE_INDEX_ANY", we free
2778 * all FIFOs. If the USB_UNCFG_FLAG_FREE_EP0 flag is not set and
2779 * "iface_index" is set to "USB_IFACE_INDEX_ANY", we free all non
2780 * control endpoint FIFOs. If "iface_index" is not set to
2781 * "USB_IFACE_INDEX_ANY" the flag has no effect.
2782 *------------------------------------------------------------------------*/
2784 usb_fifo_free_wrap(struct usb_device *udev,
2785 uint8_t iface_index, uint8_t flag)
2791 * Free any USB FIFOs on the given interface:
2793 for (i = 0; i != USB_FIFO_MAX; i++) {
2798 /* Check if the interface index matches */
2799 if (iface_index == f->iface_index) {
2800 if (f->methods != &usb_ugen_methods) {
2802 * Don't free any non-generic FIFOs in
2807 if ((f->dev_ep_index == 0) &&
2808 (f->fs_xfer == NULL)) {
2809 /* no need to free this FIFO */
2812 } else if (iface_index == USB_IFACE_INDEX_ANY) {
2813 if ((f->methods == &usb_ugen_methods) &&
2814 (f->dev_ep_index == 0) &&
2815 (!(flag & USB_UNCFG_FLAG_FREE_EP0)) &&
2816 (f->fs_xfer == NULL)) {
2817 /* no need to free this FIFO */
2821 /* no need to free this FIFO */
2824 /* free this FIFO */
2830 /*------------------------------------------------------------------------*
2831 * usb_peer_can_wakeup
2834 * 0: Peer cannot do resume signalling.
2835 * Else: Peer can do resume signalling.
2836 *------------------------------------------------------------------------*/
2838 usb_peer_can_wakeup(struct usb_device *udev)
2840 const struct usb_config_descriptor *cdp;
2843 if ((cdp != NULL) && (udev->flags.usb_mode == USB_MODE_HOST)) {
2844 return (cdp->bmAttributes & UC_REMOTE_WAKEUP);
2846 return (0); /* not supported */
2850 usb_set_device_state(struct usb_device *udev, enum usb_dev_state state)
2853 KASSERT(state < USB_STATE_MAX, ("invalid udev state"));
2855 DPRINTF("udev %p state %s -> %s\n", udev,
2856 usb_statestr(udev->state), usb_statestr(state));
2859 mtx_lock(&usb_ref_lock);
2861 udev->state = state;
2863 mtx_unlock(&usb_ref_lock);
2865 if (udev->bus->methods->device_state_change != NULL)
2866 (udev->bus->methods->device_state_change) (udev);
2870 usb_get_device_state(struct usb_device *udev)
2873 return (USB_STATE_DETACHED);
2874 return (udev->state);
2878 usbd_device_attached(struct usb_device *udev)
2880 return (udev->state > USB_STATE_DETACHED);
2884 * The following function locks enumerating the given USB device. If
2885 * the lock is already grabbed this function returns zero. Else a
2886 * a value of one is returned.
2889 usbd_enum_lock(struct usb_device *udev)
2891 if (sx_xlocked(&udev->enum_sx))
2894 sx_xlock(&udev->enum_sx);
2895 sx_xlock(&udev->sr_sx);
2897 * NEWBUS LOCK NOTE: We should check if any parent SX locks
2898 * are locked before locking Giant. Else the lock can be
2899 * locked multiple times.
2907 * This function is the same like usbd_enum_lock() except a value of
2908 * 255 is returned when a signal is pending:
2911 usbd_enum_lock_sig(struct usb_device *udev)
2913 if (sx_xlocked(&udev->enum_sx))
2915 if (sx_xlock_sig(&udev->enum_sx))
2917 if (sx_xlock_sig(&udev->sr_sx)) {
2918 sx_xunlock(&udev->enum_sx);
2926 /* The following function unlocks enumerating the given USB device. */
2929 usbd_enum_unlock(struct usb_device *udev)
2932 sx_xunlock(&udev->enum_sx);
2933 sx_xunlock(&udev->sr_sx);
2936 /* The following function locks suspend and resume. */
2939 usbd_sr_lock(struct usb_device *udev)
2941 sx_xlock(&udev->sr_sx);
2943 * NEWBUS LOCK NOTE: We should check if any parent SX locks
2944 * are locked before locking Giant. Else the lock can be
2945 * locked multiple times.
2950 /* The following function unlocks suspend and resume. */
2953 usbd_sr_unlock(struct usb_device *udev)
2956 sx_xunlock(&udev->sr_sx);
2960 * The following function checks the enumerating lock for the given
2965 usbd_enum_is_locked(struct usb_device *udev)
2967 return (sx_xlocked(&udev->enum_sx));
2971 * The following function is used to serialize access to USB control
2972 * transfers and the USB scratch area. If the lock is already grabbed
2973 * this function returns zero. Else a value of one is returned.
2976 usbd_ctrl_lock(struct usb_device *udev)
2978 if (sx_xlocked(&udev->ctrl_sx))
2980 sx_xlock(&udev->ctrl_sx);
2983 * We need to allow suspend and resume at this point, else the
2984 * control transfer will timeout if the device is suspended!
2986 if (usbd_enum_is_locked(udev))
2987 usbd_sr_unlock(udev);
2992 usbd_ctrl_unlock(struct usb_device *udev)
2994 sx_xunlock(&udev->ctrl_sx);
2997 * Restore the suspend and resume lock after we have unlocked
2998 * the USB control transfer lock to avoid LOR:
3000 if (usbd_enum_is_locked(udev))
3005 * The following function is used to set the per-interface specific
3006 * plug and play information. The string referred to by the pnpinfo
3007 * argument can safely be freed after calling this function. The
3008 * pnpinfo of an interface will be reset at device detach or when
3009 * passing a NULL argument to this function. This function
3010 * returns zero on success, else a USB_ERR_XXX failure code.
3014 usbd_set_pnpinfo(struct usb_device *udev, uint8_t iface_index, const char *pnpinfo)
3016 struct usb_interface *iface;
3018 iface = usbd_get_iface(udev, iface_index);
3020 return (USB_ERR_INVAL);
3022 if (iface->pnpinfo != NULL) {
3023 free(iface->pnpinfo, M_USBDEV);
3024 iface->pnpinfo = NULL;
3027 if (pnpinfo == NULL || pnpinfo[0] == 0)
3028 return (0); /* success */
3030 iface->pnpinfo = strdup(pnpinfo, M_USBDEV);
3031 if (iface->pnpinfo == NULL)
3032 return (USB_ERR_NOMEM);
3034 return (0); /* success */
3038 usbd_add_dynamic_quirk(struct usb_device *udev, uint16_t quirk)
3042 for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) {
3043 if (udev->autoQuirk[x] == 0 ||
3044 udev->autoQuirk[x] == quirk) {
3045 udev->autoQuirk[x] = quirk;
3046 return (0); /* success */
3049 return (USB_ERR_NOMEM);
3053 * The following function is used to select the endpoint mode. It
3054 * should not be called outside enumeration context.
3058 usbd_set_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep,
3064 /* Prevent re-enumeration */
3065 do_unlock = usbd_enum_lock(udev);
3067 if (udev->bus->methods->set_endpoint_mode != NULL) {
3068 error = (udev->bus->methods->set_endpoint_mode) (
3070 } else if (ep_mode != USB_EP_MODE_DEFAULT) {
3071 error = USB_ERR_INVAL;
3076 /* only set new mode regardless of error */
3077 ep->ep_mode = ep_mode;
3080 usbd_enum_unlock(udev);
3085 usbd_get_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep)
3087 return (ep->ep_mode);