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++) {
329 if (ep->edesc == NULL) {
332 /* do the mask and check the value */
333 if ((ep->edesc->bEndpointAddress & EA_MASK) == ea_val) {
339 * The default endpoint is always present and is checked separately:
341 if ((udev->ctrl_ep.edesc != NULL) &&
342 ((udev->ctrl_ep.edesc->bEndpointAddress & EA_MASK) == ea_val)) {
352 /*------------------------------------------------------------------------*
355 * This function searches for an USB endpoint based on the information
356 * given by the passed "struct usb_config" pointer.
360 * Else: Pointer to "struct usb_endpoint".
361 *------------------------------------------------------------------------*/
362 struct usb_endpoint *
363 usbd_get_endpoint(struct usb_device *udev, uint8_t iface_index,
364 const struct usb_config *setup)
366 struct usb_endpoint *ep = udev->endpoints;
367 struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max;
368 uint8_t index = setup->ep_index;
374 DPRINTFN(10, "udev=%p iface_index=%d address=0x%x "
375 "type=0x%x dir=0x%x index=%d\n",
376 udev, iface_index, setup->endpoint,
377 setup->type, setup->direction, setup->ep_index);
381 if (setup->usb_mode != USB_MODE_DUAL &&
382 udev->flags.usb_mode != setup->usb_mode) {
383 /* wrong mode - no endpoint */
387 /* setup expected endpoint direction mask and value */
389 if (setup->direction == UE_DIR_RX) {
390 ea_mask = (UE_DIR_IN | UE_DIR_OUT);
391 ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ?
392 UE_DIR_OUT : UE_DIR_IN;
393 } else if (setup->direction == UE_DIR_TX) {
394 ea_mask = (UE_DIR_IN | UE_DIR_OUT);
395 ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ?
396 UE_DIR_IN : UE_DIR_OUT;
397 } else if (setup->direction == UE_DIR_ANY) {
398 /* match any endpoint direction */
402 /* match the given endpoint direction */
403 ea_mask = (UE_DIR_IN | UE_DIR_OUT);
404 ea_val = (setup->direction & (UE_DIR_IN | UE_DIR_OUT));
407 /* setup expected endpoint address */
409 if (setup->endpoint == UE_ADDR_ANY) {
410 /* match any endpoint address */
412 /* match the given endpoint address */
414 ea_val |= (setup->endpoint & UE_ADDR);
417 /* setup expected endpoint type */
419 if (setup->type == UE_BULK_INTR) {
420 /* this will match BULK and INTERRUPT endpoints */
423 } else if (setup->type == UE_TYPE_ANY) {
424 /* match any endpoint type */
428 /* match the given endpoint type */
429 type_mask = UE_XFERTYPE;
430 type_val = (setup->type & UE_XFERTYPE);
434 * Iterate across all the USB endpoints searching for a match
435 * based on the endpoint address. Note that we are searching
436 * the endpoints from the beginning of the "udev->endpoints" array.
438 for (; ep != ep_end; ep++) {
440 if ((ep->edesc == NULL) ||
441 (ep->iface_index != iface_index)) {
444 /* do the masks and check the values */
446 if (((ep->edesc->bEndpointAddress & ea_mask) == ea_val) &&
447 ((ep->edesc->bmAttributes & type_mask) == type_val)) {
455 * Match against default endpoint last, so that "any endpoint", "any
456 * address" and "any direction" returns the first endpoint of the
457 * interface. "iface_index" and "direction" is ignored:
459 if ((udev->ctrl_ep.edesc != NULL) &&
460 ((udev->ctrl_ep.edesc->bEndpointAddress & ea_mask) == ea_val) &&
461 ((udev->ctrl_ep.edesc->bmAttributes & type_mask) == type_val) &&
472 /*------------------------------------------------------------------------*
473 * usbd_interface_count
475 * This function stores the number of USB interfaces excluding
476 * alternate settings, which the USB config descriptor reports into
477 * the unsigned 8-bit integer pointed to by "count".
482 *------------------------------------------------------------------------*/
484 usbd_interface_count(struct usb_device *udev, uint8_t *count)
486 if (udev->cdesc == NULL) {
488 return (USB_ERR_NOT_CONFIGURED);
490 *count = udev->ifaces_max;
491 return (USB_ERR_NORMAL_COMPLETION);
494 /*------------------------------------------------------------------------*
497 * This function will initialise the USB endpoint structure pointed to by
498 * the "endpoint" argument. The structure pointed to by "endpoint" must be
499 * zeroed before calling this function.
500 *------------------------------------------------------------------------*/
502 usb_init_endpoint(struct usb_device *udev, uint8_t iface_index,
503 struct usb_endpoint_descriptor *edesc,
504 struct usb_endpoint_ss_comp_descriptor *ecomp,
505 struct usb_endpoint *ep)
507 const struct usb_bus_methods *methods;
510 methods = udev->bus->methods;
512 (methods->endpoint_init) (udev, edesc, ep);
514 /* initialise USB endpoint structure */
517 ep->iface_index = iface_index;
519 /* setup USB stream queues */
520 for (x = 0; x != USB_MAX_EP_STREAMS; x++) {
521 TAILQ_INIT(&ep->endpoint_q[x].head);
522 ep->endpoint_q[x].command = &usbd_pipe_start;
525 /* the pipe is not supported by the hardware */
526 if (ep->methods == NULL)
529 /* check for SUPER-speed streams mode endpoint */
530 if (udev->speed == USB_SPEED_SUPER && ecomp != NULL &&
531 (edesc->bmAttributes & UE_XFERTYPE) == UE_BULK &&
532 (UE_GET_BULK_STREAMS(ecomp->bmAttributes) != 0)) {
533 usbd_set_endpoint_mode(udev, ep, USB_EP_MODE_STREAMS);
535 usbd_set_endpoint_mode(udev, ep, USB_EP_MODE_DEFAULT);
538 /* clear stall, if any */
539 if (methods->clear_stall != NULL) {
540 USB_BUS_LOCK(udev->bus);
541 (methods->clear_stall) (udev, ep);
542 USB_BUS_UNLOCK(udev->bus);
546 /*-----------------------------------------------------------------------*
547 * usb_endpoint_foreach
549 * This function will iterate all the USB endpoints except the control
550 * endpoint. This function is NULL safe.
553 * NULL: End of USB endpoints
554 * Else: Pointer to next USB endpoint
555 *------------------------------------------------------------------------*/
556 struct usb_endpoint *
557 usb_endpoint_foreach(struct usb_device *udev, struct usb_endpoint *ep)
559 struct usb_endpoint *ep_end;
565 ep_end = udev->endpoints + udev->endpoints_max;
567 /* get next endpoint */
569 ep = udev->endpoints;
573 /* find next allocated ep */
574 while (ep != ep_end) {
575 if (ep->edesc != NULL)
582 /*------------------------------------------------------------------------*
583 * usb_wait_pending_refs
585 * This function will wait for any USB references to go away before
586 * returning. This function is used before freeing a USB device.
587 *------------------------------------------------------------------------*/
589 usb_wait_pending_refs(struct usb_device *udev)
592 DPRINTF("Refcount = %d\n", (int)udev->refcount);
594 mtx_lock(&usb_ref_lock);
597 /* wait for any pending references to go away */
598 if (udev->refcount == 0) {
599 /* prevent further refs being taken, if any */
600 udev->refcount = USB_DEV_REF_MAX;
603 cv_wait(&udev->ref_cv, &usb_ref_lock);
605 mtx_unlock(&usb_ref_lock);
609 /*------------------------------------------------------------------------*
612 * This function will free all USB interfaces and USB endpoints belonging
615 * Flag values, see "USB_UNCFG_FLAG_XXX".
616 *------------------------------------------------------------------------*/
618 usb_unconfigure(struct usb_device *udev, uint8_t flag)
622 /* Prevent re-enumeration */
623 do_unlock = usbd_enum_lock(udev);
625 /* detach all interface drivers */
626 usb_detach_device(udev, USB_IFACE_INDEX_ANY, flag);
629 /* free all FIFOs except control endpoint FIFOs */
630 usb_fifo_free_wrap(udev, USB_IFACE_INDEX_ANY, flag);
633 * Free all cdev's, if any.
638 #if USB_HAVE_COMPAT_LINUX
639 /* free Linux compat device, if any */
640 if (udev->linux_endpoint_start != NULL) {
641 usb_linux_free_device_p(udev);
642 udev->linux_endpoint_start = NULL;
646 usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_FREE);
648 /* free "cdesc" after "ifaces" and "endpoints", if any */
649 if (udev->cdesc != NULL) {
650 if (udev->flags.usb_mode != USB_MODE_DEVICE)
651 usbd_free_config_desc(udev, udev->cdesc);
654 /* set unconfigured state */
655 udev->curr_config_no = USB_UNCONFIG_NO;
656 udev->curr_config_index = USB_UNCONFIG_INDEX;
659 usbd_enum_unlock(udev);
662 /*------------------------------------------------------------------------*
663 * usbd_set_config_index
665 * This function selects configuration by index, independent of the
666 * actual configuration number. This function should not be used by
672 *------------------------------------------------------------------------*/
674 usbd_set_config_index(struct usb_device *udev, uint8_t index)
676 struct usb_status ds;
677 struct usb_config_descriptor *cdp;
684 DPRINTFN(6, "udev=%p index=%d\n", udev, index);
686 /* Prevent re-enumeration */
687 do_unlock = usbd_enum_lock(udev);
689 usb_unconfigure(udev, 0);
691 if (index == USB_UNCONFIG_INDEX) {
693 * Leave unallocated when unconfiguring the
694 * device. "usb_unconfigure()" will also reset
695 * the current config number and index.
697 err = usbd_req_set_config(udev, NULL, USB_UNCONFIG_NO);
698 if (udev->state == USB_STATE_CONFIGURED)
699 usb_set_device_state(udev, USB_STATE_ADDRESSED);
702 /* get the full config descriptor */
703 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
704 /* save some memory */
705 err = usbd_req_get_descriptor_ptr(udev, &cdp,
706 (UDESC_CONFIG << 8) | index);
709 err = usbd_req_get_config_desc_full(udev,
715 /* set the new config descriptor */
719 /* Figure out if the device is self or bus powered. */
721 if ((!udev->flags.uq_bus_powered) &&
722 (cdp->bmAttributes & UC_SELF_POWERED) &&
723 (udev->flags.usb_mode == USB_MODE_HOST)) {
724 /* May be self powered. */
725 if (cdp->bmAttributes & UC_BUS_POWERED) {
726 /* Must ask device. */
727 err = usbd_req_get_device_status(udev, NULL, &ds);
729 DPRINTFN(0, "could not read "
730 "device status: %s\n",
732 } else if (UGETW(ds.wStatus) & UDS_SELF_POWERED) {
735 DPRINTF("status=0x%04x \n",
740 DPRINTF("udev=%p cdesc=%p (addr %d) cno=%d attr=0x%02x, "
741 "selfpowered=%d, power=%d\n",
743 udev->address, cdp->bConfigurationValue, cdp->bmAttributes,
744 selfpowered, cdp->bMaxPower * 2);
746 /* Check if we have enough power. */
747 power = cdp->bMaxPower * 2;
749 if (udev->parent_hub) {
750 max_power = udev->parent_hub->hub->portpower;
752 max_power = USB_MAX_POWER;
755 if (power > max_power) {
756 DPRINTFN(0, "power exceeded %d > %d\n", power, max_power);
757 err = USB_ERR_NO_POWER;
760 /* Only update "self_powered" in USB Host Mode */
761 if (udev->flags.usb_mode == USB_MODE_HOST) {
762 udev->flags.self_powered = selfpowered;
765 udev->curr_config_no = cdp->bConfigurationValue;
766 udev->curr_config_index = index;
767 usb_set_device_state(udev, USB_STATE_CONFIGURED);
769 /* Set the actual configuration value. */
770 err = usbd_req_set_config(udev, NULL, cdp->bConfigurationValue);
775 err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_ALLOC);
780 err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_INIT);
786 /* create device nodes for each endpoint */
787 usb_cdev_create(udev);
791 DPRINTF("error=%s\n", usbd_errstr(err));
793 usb_unconfigure(udev, 0);
796 usbd_enum_unlock(udev);
800 /*------------------------------------------------------------------------*
803 * This function will allocate and free USB interfaces and USB endpoints,
804 * parse the USB configuration structure and initialise the USB endpoints
805 * and interfaces. If "iface_index" is not equal to
806 * "USB_IFACE_INDEX_ANY" then the "cmd" parameter is the
807 * alternate_setting to be selected for the given interface. Else the
808 * "cmd" parameter is defined by "USB_CFG_XXX". "iface_index" can be
809 * "USB_IFACE_INDEX_ANY" or a valid USB interface index. This function
810 * is typically called when setting the configuration or when setting
811 * an alternate interface.
816 *------------------------------------------------------------------------*/
818 usb_config_parse(struct usb_device *udev, uint8_t iface_index, uint8_t cmd)
820 struct usb_idesc_parse_state ips;
821 struct usb_interface_descriptor *id;
822 struct usb_endpoint_descriptor *ed;
823 struct usb_interface *iface;
824 struct usb_endpoint *ep;
832 if (iface_index != USB_IFACE_INDEX_ANY) {
833 /* parameter overload */
843 DPRINTFN(5, "iface_index=%d cmd=%d\n",
846 if (cmd == USB_CFG_FREE)
849 if (cmd == USB_CFG_INIT) {
850 sx_assert(&udev->enum_sx, SA_LOCKED);
852 /* check for in-use endpoints */
854 ep = udev->endpoints;
855 ep_max = udev->endpoints_max;
857 /* look for matching endpoints */
858 if ((iface_index == USB_IFACE_INDEX_ANY) ||
859 (iface_index == ep->iface_index)) {
860 if (ep->refcount_alloc != 0) {
862 * This typically indicates a
863 * more serious error.
865 err = USB_ERR_IN_USE;
868 memset(ep, 0, sizeof(*ep));
869 /* make sure we don't zero the endpoint again */
870 ep->iface_index = USB_IFACE_INDEX_ANY;
880 memset(&ips, 0, sizeof(ips));
885 while ((id = usb_idesc_foreach(udev->cdesc, &ips))) {
887 iface = udev->ifaces + ips.iface_index;
889 /* check for specific interface match */
891 if (cmd == USB_CFG_INIT) {
892 if ((iface_index != USB_IFACE_INDEX_ANY) &&
893 (iface_index != ips.iface_index)) {
894 /* wrong interface */
896 } else if (alt_index != ips.iface_index_alt) {
897 /* wrong alternate setting */
900 /* initialise interface */
906 /* check for new interface */
907 if (ips.iface_index_alt == 0) {
908 /* update current number of endpoints */
913 /* setup the USB interface structure */
915 /* set alternate index */
916 iface->alt_index = alt_index;
917 /* set default interface parent */
918 if (iface_index == USB_IFACE_INDEX_ANY) {
919 iface->parent_iface_index =
924 DPRINTFN(5, "found idesc nendpt=%d\n", id->bNumEndpoints);
926 ed = (struct usb_endpoint_descriptor *)id;
930 /* iterate all the endpoint descriptors */
931 while ((ed = usb_edesc_foreach(udev->cdesc, ed))) {
933 /* check if endpoint limit has been reached */
934 if (temp >= USB_MAX_EP_UNITS) {
935 DPRINTF("Endpoint limit reached\n");
939 ep = udev->endpoints + temp;
944 ecomp = usb_ed_comp_foreach(udev->cdesc, (void *)ed);
946 DPRINTFN(5, "Found endpoint companion descriptor\n");
948 usb_init_endpoint(udev,
949 ips.iface_index, ed, ecomp, ep);
954 /* find maximum number of endpoints */
960 /* NOTE: It is valid to have no interfaces and no endpoints! */
962 if (cmd == USB_CFG_ALLOC) {
963 udev->ifaces_max = ips.iface_index;
964 #if (USB_HAVE_FIXED_IFACE == 0)
966 if (udev->ifaces_max != 0) {
967 udev->ifaces = malloc(sizeof(*iface) * udev->ifaces_max,
968 M_USB, M_WAITOK | M_ZERO);
969 if (udev->ifaces == NULL) {
975 #if (USB_HAVE_FIXED_ENDPOINT == 0)
977 udev->endpoints = malloc(sizeof(*ep) * ep_max,
978 M_USB, M_WAITOK | M_ZERO);
979 if (udev->endpoints == NULL) {
984 udev->endpoints = NULL;
987 USB_BUS_LOCK(udev->bus);
988 udev->endpoints_max = ep_max;
989 /* reset any ongoing clear-stall */
990 udev->ep_curr = NULL;
991 USB_BUS_UNLOCK(udev->bus);
993 #if (USB_HAVE_FIXED_IFACE == 0) || (USB_HAVE_FIXED_ENDPOINT == 0)
997 if (cmd == USB_CFG_ALLOC) {
999 USB_BUS_LOCK(udev->bus);
1000 udev->endpoints_max = 0;
1001 /* reset any ongoing clear-stall */
1002 udev->ep_curr = NULL;
1003 USB_BUS_UNLOCK(udev->bus);
1005 #if (USB_HAVE_FIXED_IFACE == 0)
1006 free(udev->ifaces, M_USB);
1007 udev->ifaces = NULL;
1009 #if (USB_HAVE_FIXED_ENDPOINT == 0)
1010 free(udev->endpoints, M_USB);
1011 udev->endpoints = NULL;
1013 udev->ifaces_max = 0;
1019 /*------------------------------------------------------------------------*
1020 * usbd_set_alt_interface_index
1022 * This function will select an alternate interface index for the
1023 * given interface index. The interface should not be in use when this
1024 * function is called. That means there should not be any open USB
1025 * transfers. Else an error is returned. If the alternate setting is
1026 * already set this function will simply return success. This function
1027 * is called in Host mode and Device mode!
1032 *------------------------------------------------------------------------*/
1034 usbd_set_alt_interface_index(struct usb_device *udev,
1035 uint8_t iface_index, uint8_t alt_index)
1037 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
1041 /* Prevent re-enumeration */
1042 do_unlock = usbd_enum_lock(udev);
1044 if (iface == NULL) {
1045 err = USB_ERR_INVAL;
1048 if (iface->alt_index == alt_index) {
1050 * Optimise away duplicate setting of
1051 * alternate setting in USB Host Mode!
1058 * Free all generic FIFOs for this interface, except control
1061 usb_fifo_free_wrap(udev, iface_index, 0);
1064 err = usb_config_parse(udev, iface_index, alt_index);
1068 if (iface->alt_index != alt_index) {
1069 /* the alternate setting does not exist */
1070 err = USB_ERR_INVAL;
1074 err = usbd_req_set_alt_interface_no(udev, NULL, iface_index,
1075 iface->idesc->bAlternateSetting);
1079 usbd_enum_unlock(udev);
1083 /*------------------------------------------------------------------------*
1084 * usbd_set_endpoint_stall
1086 * This function is used to make a BULK or INTERRUPT endpoint send
1087 * STALL tokens in USB device mode.
1092 *------------------------------------------------------------------------*/
1094 usbd_set_endpoint_stall(struct usb_device *udev, struct usb_endpoint *ep,
1097 struct usb_xfer *xfer;
1100 uint8_t was_stalled;
1104 DPRINTF("Cannot find endpoint\n");
1106 * Pretend that the clear or set stall request is
1107 * successful else some USB host stacks can do
1108 * strange things, especially when a control endpoint
1113 et = (ep->edesc->bmAttributes & UE_XFERTYPE);
1115 if ((et != UE_BULK) &&
1116 (et != UE_INTERRUPT)) {
1118 * Should not stall control
1119 * nor isochronous endpoints.
1121 DPRINTF("Invalid endpoint\n");
1124 USB_BUS_LOCK(udev->bus);
1126 /* store current stall state */
1127 was_stalled = ep->is_stalled;
1129 /* check for no change */
1130 if (was_stalled && do_stall) {
1131 /* if the endpoint is already stalled do nothing */
1132 USB_BUS_UNLOCK(udev->bus);
1133 DPRINTF("No change\n");
1136 /* set stalled state */
1139 if (do_stall || (!was_stalled)) {
1141 for (x = 0; x != USB_MAX_EP_STREAMS; x++) {
1142 /* lookup the current USB transfer, if any */
1143 xfer = ep->endpoint_q[x].curr;
1146 * The "xfer_stall" method
1147 * will complete the USB
1148 * transfer like in case of a
1149 * timeout setting the error
1150 * code "USB_ERR_STALLED".
1152 (udev->bus->methods->xfer_stall) (xfer);
1156 (udev->bus->methods->set_stall) (udev, ep, &do_stall);
1159 ep->toggle_next = 0; /* reset data toggle */
1160 ep->is_stalled = 0; /* clear stalled state */
1162 (udev->bus->methods->clear_stall) (udev, ep);
1164 /* start the current or next transfer, if any */
1165 for (x = 0; x != USB_MAX_EP_STREAMS; x++) {
1166 usb_command_wrapper(&ep->endpoint_q[x],
1167 ep->endpoint_q[x].curr);
1170 USB_BUS_UNLOCK(udev->bus);
1174 /*------------------------------------------------------------------------*
1175 * usb_reset_iface_endpoints - used in USB device side mode
1176 *------------------------------------------------------------------------*/
1178 usb_reset_iface_endpoints(struct usb_device *udev, uint8_t iface_index)
1180 struct usb_endpoint *ep;
1181 struct usb_endpoint *ep_end;
1183 ep = udev->endpoints;
1184 ep_end = udev->endpoints + udev->endpoints_max;
1186 for (; ep != ep_end; ep++) {
1188 if ((ep->edesc == NULL) ||
1189 (ep->iface_index != iface_index)) {
1192 /* simulate a clear stall from the peer */
1193 usbd_set_endpoint_stall(udev, ep, 0);
1198 /*------------------------------------------------------------------------*
1199 * usb_detach_device_sub
1201 * This function will try to detach an USB device. If it fails a panic
1204 * Flag values, see "USB_UNCFG_FLAG_XXX".
1205 *------------------------------------------------------------------------*/
1207 usb_detach_device_sub(struct usb_device *udev, device_t *ppdev,
1208 char **ppnpinfo, uint8_t flag)
1217 * NOTE: It is important to clear "*ppdev" before deleting
1218 * the child due to some device methods being called late
1219 * during the delete process !
1224 device_printf(dev, "at %s, port %d, addr %d "
1226 device_get_nameunit(udev->parent_dev),
1227 udev->port_no, udev->address);
1230 if (device_is_attached(dev)) {
1231 if (udev->flags.peer_suspended) {
1232 err = DEVICE_RESUME(dev);
1234 device_printf(dev, "Resume failed\n");
1238 /* detach and delete child */
1239 if (device_delete_child(udev->parent_dev, dev)) {
1244 pnpinfo = *ppnpinfo;
1245 if (pnpinfo != NULL) {
1247 free(pnpinfo, M_USBDEV);
1252 /* Detach is not allowed to fail in the USB world */
1253 panic("usb_detach_device_sub: A USB driver would not detach\n");
1256 /*------------------------------------------------------------------------*
1259 * The following function will detach the matching interfaces.
1260 * This function is NULL safe.
1262 * Flag values, see "USB_UNCFG_FLAG_XXX".
1263 *------------------------------------------------------------------------*/
1265 usb_detach_device(struct usb_device *udev, uint8_t iface_index,
1268 struct usb_interface *iface;
1275 DPRINTFN(4, "udev=%p\n", udev);
1277 sx_assert(&udev->enum_sx, SA_LOCKED);
1280 * First detach the child to give the child's detach routine a
1281 * chance to detach the sub-devices in the correct order.
1282 * Then delete the child using "device_delete_child()" which
1283 * will detach all sub-devices from the bottom and upwards!
1285 if (iface_index != USB_IFACE_INDEX_ANY) {
1287 iface_index = i + 1;
1290 iface_index = USB_IFACE_MAX;
1295 for (; i != iface_index; i++) {
1297 iface = usbd_get_iface(udev, i);
1298 if (iface == NULL) {
1299 /* looks like the end of the USB interfaces */
1302 usb_detach_device_sub(udev, &iface->subdev,
1303 &iface->pnpinfo, flag);
1307 /*------------------------------------------------------------------------*
1308 * usb_probe_and_attach_sub
1313 *------------------------------------------------------------------------*/
1315 usb_probe_and_attach_sub(struct usb_device *udev,
1316 struct usb_attach_arg *uaa)
1318 struct usb_interface *iface;
1323 if (iface->parent_iface_index != USB_IFACE_INDEX_ANY) {
1324 /* leave interface alone */
1327 dev = iface->subdev;
1330 /* clean up after module unload */
1332 if (device_is_attached(dev)) {
1333 /* already a device there */
1336 /* clear "iface->subdev" as early as possible */
1338 iface->subdev = NULL;
1340 if (device_delete_child(udev->parent_dev, dev)) {
1343 * Panic here, else one can get a double call
1344 * to device_detach(). USB devices should
1345 * never fail on detach!
1347 panic("device_delete_child() failed\n");
1350 if (uaa->temp_dev == NULL) {
1352 /* create a new child */
1353 uaa->temp_dev = device_add_child(udev->parent_dev, NULL, -1);
1354 if (uaa->temp_dev == NULL) {
1355 device_printf(udev->parent_dev,
1356 "Device creation failed\n");
1357 return (1); /* failure */
1359 device_set_ivars(uaa->temp_dev, uaa);
1360 device_quiet(uaa->temp_dev);
1363 * Set "subdev" before probe and attach so that "devd" gets
1364 * the information it needs.
1366 iface->subdev = uaa->temp_dev;
1368 if (device_probe_and_attach(iface->subdev) == 0) {
1370 * The USB attach arguments are only available during probe
1373 uaa->temp_dev = NULL;
1374 device_set_ivars(iface->subdev, NULL);
1376 if (udev->flags.peer_suspended) {
1377 err = DEVICE_SUSPEND(iface->subdev);
1379 device_printf(iface->subdev, "Suspend failed\n");
1381 return (0); /* success */
1383 /* No USB driver found */
1384 iface->subdev = NULL;
1386 return (1); /* failure */
1389 /*------------------------------------------------------------------------*
1390 * usbd_set_parent_iface
1392 * Using this function will lock the alternate interface setting on an
1393 * interface. It is typically used for multi interface drivers. In USB
1394 * device side mode it is assumed that the alternate interfaces all
1395 * have the same endpoint descriptors. The default parent index value
1396 * is "USB_IFACE_INDEX_ANY". Then the alternate setting value is not
1398 *------------------------------------------------------------------------*/
1400 usbd_set_parent_iface(struct usb_device *udev, uint8_t iface_index,
1401 uint8_t parent_index)
1403 struct usb_interface *iface;
1405 if (udev == NULL || iface_index == parent_index) {
1409 iface = usbd_get_iface(udev, iface_index);
1411 iface->parent_iface_index = parent_index;
1415 usb_init_attach_arg(struct usb_device *udev,
1416 struct usb_attach_arg *uaa)
1418 memset(uaa, 0, sizeof(*uaa));
1421 uaa->usb_mode = udev->flags.usb_mode;
1422 uaa->port = udev->port_no;
1423 uaa->dev_state = UAA_DEV_READY;
1425 uaa->info.idVendor = UGETW(udev->ddesc.idVendor);
1426 uaa->info.idProduct = UGETW(udev->ddesc.idProduct);
1427 uaa->info.bcdDevice = UGETW(udev->ddesc.bcdDevice);
1428 uaa->info.bDeviceClass = udev->ddesc.bDeviceClass;
1429 uaa->info.bDeviceSubClass = udev->ddesc.bDeviceSubClass;
1430 uaa->info.bDeviceProtocol = udev->ddesc.bDeviceProtocol;
1431 uaa->info.bConfigIndex = udev->curr_config_index;
1432 uaa->info.bConfigNum = udev->curr_config_no;
1435 /*------------------------------------------------------------------------*
1436 * usb_probe_and_attach
1438 * This function is called from "uhub_explore_sub()",
1439 * "usb_handle_set_config()" and "usb_handle_request()".
1443 * Else: A control transfer failed
1444 *------------------------------------------------------------------------*/
1446 usb_probe_and_attach(struct usb_device *udev, uint8_t iface_index)
1448 struct usb_attach_arg uaa;
1449 struct usb_interface *iface;
1455 DPRINTF("udev == NULL\n");
1456 return (USB_ERR_INVAL);
1458 /* Prevent re-enumeration */
1459 do_unlock = usbd_enum_lock(udev);
1461 if (udev->curr_config_index == USB_UNCONFIG_INDEX) {
1462 /* do nothing - no configuration has been set */
1465 /* setup USB attach arguments */
1467 usb_init_attach_arg(udev, &uaa);
1470 * If the whole USB device is targeted, invoke the USB event
1473 if (iface_index == USB_IFACE_INDEX_ANY) {
1475 if (usb_test_quirk(&uaa, UQ_MSC_DYMO_EJECT) != 0 &&
1476 usb_dymo_eject(udev, 0) == 0) {
1477 /* success, mark the udev as disappearing */
1478 uaa.dev_state = UAA_DEV_EJECTING;
1481 EVENTHANDLER_INVOKE(usb_dev_configured, udev, &uaa);
1483 if (uaa.dev_state != UAA_DEV_READY) {
1484 /* leave device unconfigured */
1485 usb_unconfigure(udev, 0);
1490 /* Check if only one interface should be probed: */
1491 if (iface_index != USB_IFACE_INDEX_ANY) {
1499 /* Do the probe and attach */
1500 for (; i != j; i++) {
1502 iface = usbd_get_iface(udev, i);
1503 if (iface == NULL) {
1505 * Looks like the end of the USB
1508 DPRINTFN(2, "end of interfaces "
1512 if (iface->idesc == NULL) {
1513 /* no interface descriptor */
1518 uaa.info.bInterfaceClass =
1519 iface->idesc->bInterfaceClass;
1520 uaa.info.bInterfaceSubClass =
1521 iface->idesc->bInterfaceSubClass;
1522 uaa.info.bInterfaceProtocol =
1523 iface->idesc->bInterfaceProtocol;
1524 uaa.info.bIfaceIndex = i;
1525 uaa.info.bIfaceNum =
1526 iface->idesc->bInterfaceNumber;
1527 uaa.driver_info = 0; /* reset driver_info */
1529 DPRINTFN(2, "iclass=%u/%u/%u iindex=%u/%u\n",
1530 uaa.info.bInterfaceClass,
1531 uaa.info.bInterfaceSubClass,
1532 uaa.info.bInterfaceProtocol,
1533 uaa.info.bIfaceIndex,
1534 uaa.info.bIfaceNum);
1536 usb_probe_and_attach_sub(udev, &uaa);
1539 * Remove the leftover child, if any, to enforce that
1540 * a new nomatch devd event is generated for the next
1541 * interface if no driver is found:
1543 if (uaa.temp_dev == NULL)
1545 if (device_delete_child(udev->parent_dev, uaa.temp_dev))
1546 DPRINTFN(0, "device delete child failed\n");
1547 uaa.temp_dev = NULL;
1551 usbd_enum_unlock(udev);
1555 /*------------------------------------------------------------------------*
1556 * usb_suspend_resume_sub
1558 * This function is called when the suspend or resume methods should
1559 * be executed on an USB device.
1560 *------------------------------------------------------------------------*/
1562 usb_suspend_resume_sub(struct usb_device *udev, device_t dev, uint8_t do_suspend)
1569 if (!device_is_attached(dev)) {
1573 err = DEVICE_SUSPEND(dev);
1575 err = DEVICE_RESUME(dev);
1578 device_printf(dev, "%s failed\n",
1579 do_suspend ? "Suspend" : "Resume");
1583 /*------------------------------------------------------------------------*
1584 * usb_suspend_resume
1586 * The following function will suspend or resume the USB device.
1591 *------------------------------------------------------------------------*/
1593 usb_suspend_resume(struct usb_device *udev, uint8_t do_suspend)
1595 struct usb_interface *iface;
1602 DPRINTFN(4, "udev=%p do_suspend=%d\n", udev, do_suspend);
1604 sx_assert(&udev->sr_sx, SA_LOCKED);
1606 USB_BUS_LOCK(udev->bus);
1607 /* filter the suspend events */
1608 if (udev->flags.peer_suspended == do_suspend) {
1609 USB_BUS_UNLOCK(udev->bus);
1613 udev->flags.peer_suspended = do_suspend;
1614 USB_BUS_UNLOCK(udev->bus);
1616 /* do the suspend or resume */
1618 for (i = 0; i != USB_IFACE_MAX; i++) {
1620 iface = usbd_get_iface(udev, i);
1621 if (iface == NULL) {
1622 /* looks like the end of the USB interfaces */
1625 usb_suspend_resume_sub(udev, iface->subdev, do_suspend);
1630 /*------------------------------------------------------------------------*
1631 * usbd_clear_stall_proc
1633 * This function performs generic USB clear stall operations.
1634 *------------------------------------------------------------------------*/
1636 usbd_clear_stall_proc(struct usb_proc_msg *_pm)
1638 struct usb_udev_msg *pm = (void *)_pm;
1639 struct usb_device *udev = pm->udev;
1642 USB_BUS_UNLOCK(udev->bus);
1643 USB_MTX_LOCK(&udev->device_mtx);
1645 /* Start clear stall callback */
1646 usbd_transfer_start(udev->ctrl_xfer[1]);
1649 USB_MTX_UNLOCK(&udev->device_mtx);
1650 USB_BUS_LOCK(udev->bus);
1653 /*------------------------------------------------------------------------*
1656 * This function tries to figure out the USB string language to use.
1657 *------------------------------------------------------------------------*/
1659 usb_get_langid(struct usb_device *udev)
1661 uint8_t *scratch_ptr;
1666 * Workaround for buggy USB devices.
1668 * It appears that some string-less USB chips will crash and
1669 * disappear if any attempts are made to read any string
1672 * Try to detect such chips by checking the strings in the USB
1673 * device descriptor. If no strings are present there we
1674 * simply disable all USB strings.
1677 /* Protect scratch area */
1678 do_unlock = usbd_ctrl_lock(udev);
1680 scratch_ptr = udev->scratch.data;
1682 if (udev->flags.no_strings) {
1683 err = USB_ERR_INVAL;
1684 } else if (udev->ddesc.iManufacturer ||
1685 udev->ddesc.iProduct ||
1686 udev->ddesc.iSerialNumber) {
1687 /* read out the language ID string */
1688 err = usbd_req_get_string_desc(udev, NULL,
1689 (char *)scratch_ptr, 4, 0, USB_LANGUAGE_TABLE);
1691 err = USB_ERR_INVAL;
1694 if (err || (scratch_ptr[0] < 4)) {
1695 udev->flags.no_strings = 1;
1702 /* load preferred value and mask */
1704 mask = usb_lang_mask;
1706 /* align length correctly */
1707 scratch_ptr[0] &= ~1U;
1709 /* fix compiler warning */
1712 /* search for preferred language */
1713 for (x = 2; x < scratch_ptr[0]; x += 2) {
1714 langid = UGETW(scratch_ptr + x);
1715 if ((langid & mask) == pref)
1718 if (x >= scratch_ptr[0]) {
1719 /* pick the first language as the default */
1720 DPRINTFN(1, "Using first language\n");
1721 langid = UGETW(scratch_ptr + 2);
1724 DPRINTFN(1, "Language selected: 0x%04x\n", langid);
1725 udev->langid = langid;
1729 usbd_ctrl_unlock(udev);
1732 /*------------------------------------------------------------------------*
1735 * This function allocates a new USB device. This function is called
1736 * when a new device has been put in the powered state, but not yet in
1737 * the addressed state. Get initial descriptor, set the address, get
1738 * full descriptor and get strings.
1743 *------------------------------------------------------------------------*/
1745 usb_alloc_device(device_t parent_dev, struct usb_bus *bus,
1746 struct usb_device *parent_hub, uint8_t depth, uint8_t port_index,
1747 uint8_t port_no, enum usb_dev_speed speed, enum usb_hc_mode mode)
1749 struct usb_attach_arg uaa;
1750 struct usb_device *udev;
1751 struct usb_device *adev;
1752 struct usb_device *hub;
1754 uint8_t device_index;
1755 uint8_t config_index;
1756 uint8_t config_quirk;
1757 uint8_t set_config_failed;
1759 DPRINTF("parent_dev=%p, bus=%p, parent_hub=%p, depth=%u, "
1760 "port_index=%u, port_no=%u, speed=%u, usb_mode=%u\n",
1761 parent_dev, bus, parent_hub, depth, port_index, port_no,
1765 * Find an unused device index. In USB Host mode this is the
1766 * same as the device address.
1768 * Device index zero is not used and device index 1 should
1769 * always be the root hub.
1771 for (device_index = USB_ROOT_HUB_ADDR;
1772 (device_index != bus->devices_max) &&
1773 (bus->devices[device_index] != NULL);
1774 device_index++) /* nop */;
1776 if (device_index == bus->devices_max) {
1777 device_printf(bus->bdev,
1778 "No free USB device index for new device\n");
1783 device_printf(bus->bdev,
1784 "Invalid device depth\n");
1787 udev = malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO);
1791 /* initialise our SX-lock */
1792 sx_init_flags(&udev->enum_sx, "USB config SX lock", SX_DUPOK);
1793 sx_init_flags(&udev->sr_sx, "USB suspend and resume SX lock", SX_NOWITNESS);
1794 sx_init_flags(&udev->ctrl_sx, "USB control transfer SX lock", SX_DUPOK);
1796 cv_init(&udev->ctrlreq_cv, "WCTRL");
1797 cv_init(&udev->ref_cv, "UGONE");
1799 /* initialise our mutex */
1800 mtx_init(&udev->device_mtx, "USB device mutex", NULL, MTX_DEF);
1802 /* initialise generic clear stall */
1803 udev->cs_msg[0].hdr.pm_callback = &usbd_clear_stall_proc;
1804 udev->cs_msg[0].udev = udev;
1805 udev->cs_msg[1].hdr.pm_callback = &usbd_clear_stall_proc;
1806 udev->cs_msg[1].udev = udev;
1808 /* initialise some USB device fields */
1809 udev->parent_hub = parent_hub;
1810 udev->parent_dev = parent_dev;
1811 udev->port_index = port_index;
1812 udev->port_no = port_no;
1813 udev->depth = depth;
1815 udev->address = USB_START_ADDR; /* default value */
1816 udev->plugtime = (usb_ticks_t)ticks;
1818 * We need to force the power mode to "on" because there are plenty
1819 * of USB devices out there that do not work very well with
1820 * automatic suspend and resume!
1822 udev->power_mode = usbd_filter_power_mode(udev, USB_POWER_MODE_ON);
1823 udev->pwr_save.last_xfer_time = ticks;
1824 /* we are not ready yet */
1827 /* set up default endpoint descriptor */
1828 udev->ctrl_ep_desc.bLength = sizeof(udev->ctrl_ep_desc);
1829 udev->ctrl_ep_desc.bDescriptorType = UDESC_ENDPOINT;
1830 udev->ctrl_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
1831 udev->ctrl_ep_desc.bmAttributes = UE_CONTROL;
1832 udev->ctrl_ep_desc.wMaxPacketSize[0] = USB_MAX_IPACKET;
1833 udev->ctrl_ep_desc.wMaxPacketSize[1] = 0;
1834 udev->ctrl_ep_desc.bInterval = 0;
1836 /* set up default endpoint companion descriptor */
1837 udev->ctrl_ep_comp_desc.bLength = sizeof(udev->ctrl_ep_comp_desc);
1838 udev->ctrl_ep_comp_desc.bDescriptorType = UDESC_ENDPOINT_SS_COMP;
1840 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
1842 udev->speed = speed;
1843 udev->flags.usb_mode = mode;
1845 /* search for our High Speed USB HUB, if any */
1848 hub = udev->parent_hub;
1851 if (hub->speed == USB_SPEED_HIGH) {
1852 udev->hs_hub_addr = hub->address;
1853 udev->parent_hs_hub = hub;
1854 udev->hs_port_no = adev->port_no;
1858 hub = hub->parent_hub;
1861 /* init the default endpoint */
1862 usb_init_endpoint(udev, 0,
1863 &udev->ctrl_ep_desc,
1864 &udev->ctrl_ep_comp_desc,
1867 /* set device index */
1868 udev->device_index = device_index;
1871 /* Create ugen name */
1872 snprintf(udev->ugen_name, sizeof(udev->ugen_name),
1873 USB_GENERIC_NAME "%u.%u", device_get_unit(bus->bdev),
1875 LIST_INIT(&udev->pd_list);
1877 /* Create the control endpoint device */
1878 udev->ctrl_dev = usb_make_dev(udev, NULL, 0, 0,
1879 FREAD|FWRITE, UID_ROOT, GID_OPERATOR, 0600);
1881 /* Create a link from /dev/ugenX.X to the default endpoint */
1882 if (udev->ctrl_dev != NULL)
1883 make_dev_alias(udev->ctrl_dev->cdev, "%s", udev->ugen_name);
1885 /* Initialise device */
1886 if (bus->methods->device_init != NULL) {
1887 err = (bus->methods->device_init) (udev);
1889 DPRINTFN(0, "device init %d failed "
1890 "(%s, ignored)\n", device_index,
1895 /* set powered device state after device init is complete */
1896 usb_set_device_state(udev, USB_STATE_POWERED);
1898 if (udev->flags.usb_mode == USB_MODE_HOST) {
1900 err = usbd_req_set_address(udev, NULL, device_index);
1903 * This is the new USB device address from now on, if
1904 * the set address request didn't set it already.
1906 if (udev->address == USB_START_ADDR)
1907 udev->address = device_index;
1910 * We ignore any set-address errors, hence there are
1911 * buggy USB devices out there that actually receive
1912 * the SETUP PID, but manage to set the address before
1913 * the STATUS stage is ACK'ed. If the device responds
1914 * to the subsequent get-descriptor at the new
1915 * address, then we know that the set-address command
1919 DPRINTFN(0, "set address %d failed "
1920 "(%s, ignored)\n", udev->address,
1924 /* We are not self powered */
1925 udev->flags.self_powered = 0;
1927 /* Set unconfigured state */
1928 udev->curr_config_no = USB_UNCONFIG_NO;
1929 udev->curr_config_index = USB_UNCONFIG_INDEX;
1931 /* Setup USB descriptors */
1932 err = (usb_temp_setup_by_index_p) (udev, usb_template);
1934 DPRINTFN(0, "setting up USB template failed - "
1935 "usb_template(4) not loaded?\n");
1939 usb_set_device_state(udev, USB_STATE_ADDRESSED);
1941 /* setup the device descriptor and the initial "wMaxPacketSize" */
1942 err = usbd_setup_device_desc(udev, NULL);
1945 /* try to enumerate two more times */
1946 err = usbd_req_re_enumerate(udev, NULL);
1948 err = usbd_req_re_enumerate(udev, NULL);
1956 * Setup temporary USB attach args so that we can figure out some
1957 * basic quirks for this device.
1959 usb_init_attach_arg(udev, &uaa);
1961 if (usb_test_quirk(&uaa, UQ_BUS_POWERED)) {
1962 udev->flags.uq_bus_powered = 1;
1964 if (usb_test_quirk(&uaa, UQ_NO_STRINGS)) {
1965 udev->flags.no_strings = 1;
1968 usb_get_langid(udev);
1970 /* assume 100mA bus powered for now. Changed when configured. */
1971 udev->power = USB_MIN_POWER;
1972 /* fetch the vendor and product strings from the device */
1973 usb_set_device_strings(udev);
1975 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
1976 /* USB device mode setup is complete */
1982 * Most USB devices should attach to config index 0 by
1985 if (usb_test_quirk(&uaa, UQ_CFG_INDEX_0)) {
1988 } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_1)) {
1991 } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_2)) {
1994 } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_3)) {
1997 } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_4)) {
2005 set_config_failed = 0;
2008 DPRINTF("setting config %u\n", config_index);
2010 /* get the USB device configured */
2011 err = usbd_set_config_index(udev, config_index);
2013 if (udev->ddesc.bNumConfigurations != 0) {
2014 if (!set_config_failed) {
2015 set_config_failed = 1;
2016 /* XXX try to re-enumerate the device */
2017 err = usbd_req_re_enumerate(udev, NULL);
2019 goto repeat_set_config;
2021 DPRINTFN(0, "Failure selecting configuration index %u:"
2022 "%s, port %u, addr %u (ignored)\n",
2023 config_index, usbd_errstr(err), udev->port_no,
2027 * Some USB devices do not have any configurations. Ignore any
2028 * set config failures!
2033 if (!config_quirk && config_index + 1 < udev->ddesc.bNumConfigurations) {
2034 if ((udev->cdesc->bNumInterface < 2) &&
2035 usbd_get_no_descriptors(udev->cdesc, UDESC_ENDPOINT) == 0) {
2036 DPRINTFN(0, "Found no endpoints, trying next config\n");
2038 goto repeat_set_config;
2040 #if USB_HAVE_MSCTEST
2041 if (config_index == 0) {
2043 * Try to figure out if we have an
2044 * auto-install disk there:
2046 if (usb_iface_is_cdrom(udev, 0)) {
2047 DPRINTFN(0, "Found possible auto-install "
2048 "disk (trying next config)\n");
2050 goto repeat_set_config;
2055 #if USB_HAVE_MSCTEST
2056 if (set_config_failed == 0 && config_index == 0 &&
2057 usb_test_quirk(&uaa, UQ_MSC_NO_SYNC_CACHE) == 0 &&
2058 usb_test_quirk(&uaa, UQ_MSC_NO_GETMAXLUN) == 0) {
2061 * Try to figure out if there are any MSC quirks we
2062 * should apply automatically:
2064 err = usb_msc_auto_quirk(udev, 0);
2067 set_config_failed = 1;
2068 goto repeat_set_config;
2074 DPRINTF("new dev (addr %d), udev=%p, parent_hub=%p\n",
2075 udev->address, udev, udev->parent_hub);
2077 /* register our device - we are ready */
2078 usb_bus_port_set_device(bus, parent_hub ?
2079 parent_hub->hub->ports + port_index : NULL, udev, device_index);
2082 /* Symlink the ugen device name */
2083 udev->ugen_symlink = usb_alloc_symlink(udev->ugen_name);
2085 /* Announce device */
2086 printf("%s: <%s %s> at %s\n", udev->ugen_name,
2087 usb_get_manufacturer(udev), usb_get_product(udev),
2088 device_get_nameunit(udev->bus->bdev));
2092 usb_notify_addq("ATTACH", udev);
2097 * Free USB device and all subdevices, if any.
2099 usb_free_device(udev, 0);
2106 struct usb_fs_privdata *
2107 usb_make_dev(struct usb_device *udev, const char *devname, int ep,
2108 int fi, int rwmode, uid_t uid, gid_t gid, int mode)
2110 struct usb_fs_privdata* pd;
2111 struct make_dev_args args;
2114 /* Store information to locate ourselves again later */
2115 pd = malloc(sizeof(struct usb_fs_privdata), M_USBDEV,
2117 pd->bus_index = device_get_unit(udev->bus->bdev);
2118 pd->dev_index = udev->device_index;
2120 pd->fifo_index = fi;
2123 /* Now, create the device itself */
2124 if (devname == NULL) {
2126 snprintf(buffer, sizeof(buffer), USB_DEVICE_DIR "/%u.%u.%u",
2127 pd->bus_index, pd->dev_index, pd->ep_addr);
2130 /* Setup arguments for make_dev_s() */
2131 make_dev_args_init(&args);
2132 args.mda_devsw = &usb_devsw;
2135 args.mda_mode = mode;
2136 args.mda_si_drv1 = pd;
2138 if (make_dev_s(&args, &pd->cdev, "%s", devname) != 0) {
2139 DPRINTFN(0, "Failed to create device %s\n", devname);
2147 usb_destroy_dev_sync(struct usb_fs_privdata *pd)
2149 DPRINTFN(1, "Destroying device at ugen%d.%d\n",
2150 pd->bus_index, pd->dev_index);
2153 * Destroy character device synchronously. After this
2154 * all system calls are returned. Can block.
2156 destroy_dev(pd->cdev);
2162 usb_destroy_dev(struct usb_fs_privdata *pd)
2164 struct usb_bus *bus;
2169 mtx_lock(&usb_ref_lock);
2170 bus = devclass_get_softc(usb_devclass_ptr, pd->bus_index);
2171 mtx_unlock(&usb_ref_lock);
2174 usb_destroy_dev_sync(pd);
2178 /* make sure we can re-use the device name */
2179 delist_dev(pd->cdev);
2182 LIST_INSERT_HEAD(&bus->pd_cleanup_list, pd, pd_next);
2183 /* get cleanup going */
2184 usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
2185 &bus->cleanup_msg[0], &bus->cleanup_msg[1]);
2186 USB_BUS_UNLOCK(bus);
2190 usb_cdev_create(struct usb_device *udev)
2192 struct usb_config_descriptor *cd;
2193 struct usb_endpoint_descriptor *ed;
2194 struct usb_descriptor *desc;
2195 struct usb_fs_privdata* pd;
2196 int inmode, outmode, inmask, outmask, mode;
2199 KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("stale cdev entries"));
2201 DPRINTFN(2, "Creating device nodes\n");
2203 if (usbd_get_mode(udev) == USB_MODE_DEVICE) {
2206 } else { /* USB_MODE_HOST */
2216 * Collect all used endpoint numbers instead of just
2217 * generating 16 static endpoints.
2219 cd = usbd_get_config_descriptor(udev);
2220 while ((desc = usb_desc_foreach(cd, desc))) {
2221 /* filter out all endpoint descriptors */
2222 if ((desc->bDescriptorType == UDESC_ENDPOINT) &&
2223 (desc->bLength >= sizeof(*ed))) {
2224 ed = (struct usb_endpoint_descriptor *)desc;
2227 ep = ed->bEndpointAddress;
2228 if (UE_GET_DIR(ep) == UE_DIR_OUT)
2229 outmask |= 1 << UE_GET_ADDR(ep);
2231 inmask |= 1 << UE_GET_ADDR(ep);
2235 /* Create all available endpoints except EP0 */
2236 for (ep = 1; ep < 16; ep++) {
2237 mode = (inmask & (1 << ep)) ? inmode : 0;
2238 mode |= (outmask & (1 << ep)) ? outmode : 0;
2240 continue; /* no IN or OUT endpoint */
2242 pd = usb_make_dev(udev, NULL, ep, 0,
2243 mode, UID_ROOT, GID_OPERATOR, 0600);
2246 LIST_INSERT_HEAD(&udev->pd_list, pd, pd_next);
2251 usb_cdev_free(struct usb_device *udev)
2253 struct usb_fs_privdata* pd;
2255 DPRINTFN(2, "Freeing device nodes\n");
2257 while ((pd = LIST_FIRST(&udev->pd_list)) != NULL) {
2258 KASSERT(pd->cdev->si_drv1 == pd, ("privdata corrupt"));
2260 LIST_REMOVE(pd, pd_next);
2262 usb_destroy_dev(pd);
2267 /*------------------------------------------------------------------------*
2270 * This function is NULL safe and will free an USB device and its
2271 * children devices, if any.
2273 * Flag values: Reserved, set to zero.
2274 *------------------------------------------------------------------------*/
2276 usb_free_device(struct usb_device *udev, uint8_t flag)
2278 struct usb_bus *bus;
2281 return; /* already freed */
2283 DPRINTFN(4, "udev=%p port=%d\n", udev, udev->port_no);
2287 /* set DETACHED state to prevent any further references */
2288 usb_set_device_state(udev, USB_STATE_DETACHED);
2291 usb_notify_addq("DETACH", udev);
2296 printf("%s: <%s %s> at %s (disconnected)\n", udev->ugen_name,
2297 usb_get_manufacturer(udev), usb_get_product(udev),
2298 device_get_nameunit(bus->bdev));
2301 /* Destroy UGEN symlink, if any */
2302 if (udev->ugen_symlink) {
2303 usb_free_symlink(udev->ugen_symlink);
2304 udev->ugen_symlink = NULL;
2307 usb_destroy_dev(udev->ctrl_dev);
2310 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2311 /* stop receiving any control transfers (Device Side Mode) */
2312 usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
2315 /* the following will get the device unconfigured in software */
2316 usb_unconfigure(udev, USB_UNCFG_FLAG_FREE_EP0);
2318 /* final device unregister after all character devices are closed */
2319 usb_bus_port_set_device(bus, udev->parent_hub ?
2320 udev->parent_hub->hub->ports + udev->port_index : NULL,
2321 NULL, USB_ROOT_HUB_ADDR);
2323 /* unsetup any leftover default USB transfers */
2324 usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
2326 /* template unsetup, if any */
2327 (usb_temp_unsetup_p) (udev);
2330 * Make sure that our clear-stall messages are not queued
2333 USB_BUS_LOCK(udev->bus);
2334 usb_proc_mwait(USB_BUS_CS_PROC(udev->bus),
2335 &udev->cs_msg[0], &udev->cs_msg[1]);
2336 USB_BUS_UNLOCK(udev->bus);
2338 /* wait for all references to go away */
2339 usb_wait_pending_refs(udev);
2341 sx_destroy(&udev->enum_sx);
2342 sx_destroy(&udev->sr_sx);
2343 sx_destroy(&udev->ctrl_sx);
2345 cv_destroy(&udev->ctrlreq_cv);
2346 cv_destroy(&udev->ref_cv);
2348 mtx_destroy(&udev->device_mtx);
2350 KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("leaked cdev entries"));
2353 /* Uninitialise device */
2354 if (bus->methods->device_uninit != NULL)
2355 (bus->methods->device_uninit) (udev);
2358 free(udev->serial, M_USB);
2359 free(udev->manufacturer, M_USB);
2360 free(udev->product, M_USB);
2364 /*------------------------------------------------------------------------*
2367 * This function is the safe way to get the USB interface structure
2368 * pointer by interface index.
2371 * NULL: Interface not present.
2372 * Else: Pointer to USB interface structure.
2373 *------------------------------------------------------------------------*/
2374 struct usb_interface *
2375 usbd_get_iface(struct usb_device *udev, uint8_t iface_index)
2377 struct usb_interface *iface = udev->ifaces + iface_index;
2379 if (iface_index >= udev->ifaces_max)
2384 /*------------------------------------------------------------------------*
2385 * usbd_find_descriptor
2387 * This function will lookup the first descriptor that matches the
2388 * criteria given by the arguments "type" and "subtype". Descriptors
2389 * will only be searched within the interface having the index
2390 * "iface_index". If the "id" argument points to an USB descriptor,
2391 * it will be skipped before the search is started. This allows
2392 * searching for multiple descriptors using the same criteria. Else
2393 * the search is started after the interface descriptor.
2396 * NULL: End of descriptors
2397 * Else: A descriptor matching the criteria
2398 *------------------------------------------------------------------------*/
2400 usbd_find_descriptor(struct usb_device *udev, void *id, uint8_t iface_index,
2401 uint8_t type, uint8_t type_mask,
2402 uint8_t subtype, uint8_t subtype_mask)
2404 struct usb_descriptor *desc;
2405 struct usb_config_descriptor *cd;
2406 struct usb_interface *iface;
2408 cd = usbd_get_config_descriptor(udev);
2413 iface = usbd_get_iface(udev, iface_index);
2414 if (iface == NULL) {
2417 id = usbd_get_interface_descriptor(iface);
2424 while ((desc = usb_desc_foreach(cd, desc))) {
2426 if (desc->bDescriptorType == UDESC_INTERFACE) {
2429 if (((desc->bDescriptorType & type_mask) == type) &&
2430 ((desc->bDescriptorSubtype & subtype_mask) == subtype)) {
2437 /*------------------------------------------------------------------------*
2440 * This function will dump information from the device descriptor
2441 * belonging to the USB device pointed to by "udev", to the string
2442 * pointed to by "dst_ptr" having a maximum length of "dst_len" bytes
2443 * including the terminating zero.
2444 *------------------------------------------------------------------------*/
2446 usb_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len)
2448 struct usb_device_descriptor *udd = &udev->ddesc;
2452 bcdUSB = UGETW(udd->bcdUSB);
2453 bcdDevice = UGETW(udd->bcdDevice);
2455 if (udd->bDeviceClass != 0xFF) {
2456 snprintf(dst_ptr, dst_len, "%s %s, class %d/%d, rev %x.%02x/"
2458 usb_get_manufacturer(udev),
2459 usb_get_product(udev),
2460 udd->bDeviceClass, udd->bDeviceSubClass,
2461 (bcdUSB >> 8), bcdUSB & 0xFF,
2462 (bcdDevice >> 8), bcdDevice & 0xFF,
2465 snprintf(dst_ptr, dst_len, "%s %s, rev %x.%02x/"
2467 usb_get_manufacturer(udev),
2468 usb_get_product(udev),
2469 (bcdUSB >> 8), bcdUSB & 0xFF,
2470 (bcdDevice >> 8), bcdDevice & 0xFF,
2477 * Descriptions of of known vendors and devices ("products").
2479 struct usb_knowndev {
2483 const char *vendorname;
2484 const char *productname;
2487 #define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */
2489 #include "usbdevs.h"
2490 #include "usbdevs_data.h"
2491 #endif /* USB_VERBOSE */
2494 usb_set_device_strings(struct usb_device *udev)
2496 struct usb_device_descriptor *udd = &udev->ddesc;
2498 const struct usb_knowndev *kdp;
2503 uint16_t product_id;
2506 /* Protect scratch area */
2507 do_unlock = usbd_ctrl_lock(udev);
2509 temp_ptr = (char *)udev->scratch.data;
2510 temp_size = sizeof(udev->scratch.data);
2512 vendor_id = UGETW(udd->idVendor);
2513 product_id = UGETW(udd->idProduct);
2515 /* cleanup old strings, if any */
2516 free(udev->serial, M_USB);
2517 free(udev->manufacturer, M_USB);
2518 free(udev->product, M_USB);
2520 /* zero the string pointers */
2521 udev->serial = NULL;
2522 udev->manufacturer = NULL;
2523 udev->product = NULL;
2525 /* get serial number string */
2526 usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2527 udev->ddesc.iSerialNumber);
2528 udev->serial = strdup(temp_ptr, M_USB);
2530 /* get manufacturer string */
2531 usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2532 udev->ddesc.iManufacturer);
2533 usb_trim_spaces(temp_ptr);
2534 if (temp_ptr[0] != '\0')
2535 udev->manufacturer = strdup(temp_ptr, M_USB);
2537 /* get product string */
2538 usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2539 udev->ddesc.iProduct);
2540 usb_trim_spaces(temp_ptr);
2541 if (temp_ptr[0] != '\0')
2542 udev->product = strdup(temp_ptr, M_USB);
2545 if (udev->manufacturer == NULL || udev->product == NULL) {
2546 for (kdp = usb_knowndevs; kdp->vendorname != NULL; kdp++) {
2547 if (kdp->vendor == vendor_id &&
2548 (kdp->product == product_id ||
2549 (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
2552 if (kdp->vendorname != NULL) {
2553 /* XXX should use pointer to knowndevs string */
2554 if (udev->manufacturer == NULL) {
2555 udev->manufacturer = strdup(kdp->vendorname,
2558 if (udev->product == NULL &&
2559 (kdp->flags & USB_KNOWNDEV_NOPROD) == 0) {
2560 udev->product = strdup(kdp->productname,
2566 /* Provide default strings if none were found */
2567 if (udev->manufacturer == NULL) {
2568 snprintf(temp_ptr, temp_size, "vendor 0x%04x", vendor_id);
2569 udev->manufacturer = strdup(temp_ptr, M_USB);
2571 if (udev->product == NULL) {
2572 snprintf(temp_ptr, temp_size, "product 0x%04x", product_id);
2573 udev->product = strdup(temp_ptr, M_USB);
2577 usbd_ctrl_unlock(udev);
2585 usbd_get_mode(struct usb_device *udev)
2587 return (udev->flags.usb_mode);
2592 * See: USB_SPEED_XXX
2595 usbd_get_speed(struct usb_device *udev)
2597 return (udev->speed);
2601 usbd_get_isoc_fps(struct usb_device *udev)
2604 switch (udev->speed) {
2606 case USB_SPEED_FULL:
2613 struct usb_device_descriptor *
2614 usbd_get_device_descriptor(struct usb_device *udev)
2617 return (NULL); /* be NULL safe */
2618 return (&udev->ddesc);
2621 struct usb_config_descriptor *
2622 usbd_get_config_descriptor(struct usb_device *udev)
2625 return (NULL); /* be NULL safe */
2626 return (udev->cdesc);
2629 /*------------------------------------------------------------------------*
2630 * usb_test_quirk - test a device for a given quirk
2633 * 0: The USB device does not have the given quirk.
2634 * Else: The USB device has the given quirk.
2635 *------------------------------------------------------------------------*/
2637 usb_test_quirk(const struct usb_attach_arg *uaa, uint16_t quirk)
2642 if (quirk == UQ_NONE)
2645 /* search the automatic per device quirks first */
2647 for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) {
2648 if (uaa->device->autoQuirk[x] == quirk)
2652 /* search global quirk table, if any */
2654 found = (usb_test_quirk_p) (&uaa->info, quirk);
2659 struct usb_interface_descriptor *
2660 usbd_get_interface_descriptor(struct usb_interface *iface)
2663 return (NULL); /* be NULL safe */
2664 return (iface->idesc);
2668 usbd_get_interface_altindex(struct usb_interface *iface)
2670 return (iface->alt_index);
2674 usbd_get_bus_index(struct usb_device *udev)
2676 return ((uint8_t)device_get_unit(udev->bus->bdev));
2680 usbd_get_device_index(struct usb_device *udev)
2682 return (udev->device_index);
2687 usb_notify_addq(const char *type, struct usb_device *udev)
2689 struct usb_interface *iface;
2693 /* announce the device */
2694 sb = sbuf_new_auto();
2703 "devsubclass=0x%02x "
2716 UGETW(udev->ddesc.idVendor),
2717 UGETW(udev->ddesc.idProduct),
2718 udev->ddesc.bDeviceClass,
2719 udev->ddesc.bDeviceSubClass,
2720 usb_get_serial(udev),
2721 UGETW(udev->ddesc.bcdDevice),
2722 (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
2725 , udev->parent_hub != NULL ?
2726 udev->parent_hub->ugen_name :
2727 device_get_nameunit(device_get_parent(udev->bus->bdev))
2731 devctl_notify("USB", "DEVICE", type, sbuf_data(sb));
2734 /* announce each interface */
2735 for (i = 0; i < USB_IFACE_MAX; i++) {
2736 iface = usbd_get_iface(udev, i);
2738 break; /* end of interfaces */
2739 if (iface->idesc == NULL)
2740 continue; /* no interface descriptor */
2742 sb = sbuf_new_auto();
2751 "devsubclass=0x%02x "
2758 "intsubclass=0x%02x "
2759 "intprotocol=0x%02x",
2764 UGETW(udev->ddesc.idVendor),
2765 UGETW(udev->ddesc.idProduct),
2766 udev->ddesc.bDeviceClass,
2767 udev->ddesc.bDeviceSubClass,
2768 usb_get_serial(udev),
2769 UGETW(udev->ddesc.bcdDevice),
2770 (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
2771 iface->idesc->bInterfaceNumber,
2772 iface->idesc->bNumEndpoints,
2773 iface->idesc->bInterfaceClass,
2774 iface->idesc->bInterfaceSubClass,
2775 iface->idesc->bInterfaceProtocol);
2777 devctl_notify("USB", "INTERFACE", type, sbuf_data(sb));
2784 /*------------------------------------------------------------------------*
2785 * usb_fifo_free_wrap
2787 * This function will free the FIFOs.
2789 * Description of "flag" argument: If the USB_UNCFG_FLAG_FREE_EP0 flag
2790 * is set and "iface_index" is set to "USB_IFACE_INDEX_ANY", we free
2791 * all FIFOs. If the USB_UNCFG_FLAG_FREE_EP0 flag is not set and
2792 * "iface_index" is set to "USB_IFACE_INDEX_ANY", we free all non
2793 * control endpoint FIFOs. If "iface_index" is not set to
2794 * "USB_IFACE_INDEX_ANY" the flag has no effect.
2795 *------------------------------------------------------------------------*/
2797 usb_fifo_free_wrap(struct usb_device *udev,
2798 uint8_t iface_index, uint8_t flag)
2804 * Free any USB FIFOs on the given interface:
2806 for (i = 0; i != USB_FIFO_MAX; i++) {
2811 /* Check if the interface index matches */
2812 if (iface_index == f->iface_index) {
2813 if (f->methods != &usb_ugen_methods) {
2815 * Don't free any non-generic FIFOs in
2820 if ((f->dev_ep_index == 0) &&
2821 (f->fs_xfer == NULL)) {
2822 /* no need to free this FIFO */
2825 } else if (iface_index == USB_IFACE_INDEX_ANY) {
2826 if ((f->methods == &usb_ugen_methods) &&
2827 (f->dev_ep_index == 0) &&
2828 (!(flag & USB_UNCFG_FLAG_FREE_EP0)) &&
2829 (f->fs_xfer == NULL)) {
2830 /* no need to free this FIFO */
2834 /* no need to free this FIFO */
2837 /* free this FIFO */
2843 /*------------------------------------------------------------------------*
2844 * usb_peer_can_wakeup
2847 * 0: Peer cannot do resume signalling.
2848 * Else: Peer can do resume signalling.
2849 *------------------------------------------------------------------------*/
2851 usb_peer_can_wakeup(struct usb_device *udev)
2853 const struct usb_config_descriptor *cdp;
2856 if ((cdp != NULL) && (udev->flags.usb_mode == USB_MODE_HOST)) {
2857 return (cdp->bmAttributes & UC_REMOTE_WAKEUP);
2859 return (0); /* not supported */
2863 usb_set_device_state(struct usb_device *udev, enum usb_dev_state state)
2866 KASSERT(state < USB_STATE_MAX, ("invalid udev state"));
2868 DPRINTF("udev %p state %s -> %s\n", udev,
2869 usb_statestr(udev->state), usb_statestr(state));
2872 mtx_lock(&usb_ref_lock);
2874 udev->state = state;
2876 mtx_unlock(&usb_ref_lock);
2878 if (udev->bus->methods->device_state_change != NULL)
2879 (udev->bus->methods->device_state_change) (udev);
2883 usb_get_device_state(struct usb_device *udev)
2886 return (USB_STATE_DETACHED);
2887 return (udev->state);
2891 usbd_device_attached(struct usb_device *udev)
2893 return (udev->state > USB_STATE_DETACHED);
2897 * The following function locks enumerating the given USB device. If
2898 * the lock is already grabbed this function returns zero. Else a
2899 * a value of one is returned.
2902 usbd_enum_lock(struct usb_device *udev)
2904 if (sx_xlocked(&udev->enum_sx))
2907 sx_xlock(&udev->enum_sx);
2908 sx_xlock(&udev->sr_sx);
2910 * NEWBUS LOCK NOTE: We should check if any parent SX locks
2911 * are locked before locking Giant. Else the lock can be
2912 * locked multiple times.
2920 * This function is the same like usbd_enum_lock() except a value of
2921 * 255 is returned when a signal is pending:
2924 usbd_enum_lock_sig(struct usb_device *udev)
2926 if (sx_xlocked(&udev->enum_sx))
2928 if (sx_xlock_sig(&udev->enum_sx))
2930 if (sx_xlock_sig(&udev->sr_sx)) {
2931 sx_xunlock(&udev->enum_sx);
2939 /* The following function unlocks enumerating the given USB device. */
2942 usbd_enum_unlock(struct usb_device *udev)
2945 sx_xunlock(&udev->enum_sx);
2946 sx_xunlock(&udev->sr_sx);
2949 /* The following function locks suspend and resume. */
2952 usbd_sr_lock(struct usb_device *udev)
2954 sx_xlock(&udev->sr_sx);
2956 * NEWBUS LOCK NOTE: We should check if any parent SX locks
2957 * are locked before locking Giant. Else the lock can be
2958 * locked multiple times.
2963 /* The following function unlocks suspend and resume. */
2966 usbd_sr_unlock(struct usb_device *udev)
2969 sx_xunlock(&udev->sr_sx);
2973 * The following function checks the enumerating lock for the given
2978 usbd_enum_is_locked(struct usb_device *udev)
2980 return (sx_xlocked(&udev->enum_sx));
2984 * The following function is used to serialize access to USB control
2985 * transfers and the USB scratch area. If the lock is already grabbed
2986 * this function returns zero. Else a value of one is returned.
2989 usbd_ctrl_lock(struct usb_device *udev)
2991 if (sx_xlocked(&udev->ctrl_sx))
2993 sx_xlock(&udev->ctrl_sx);
2996 * We need to allow suspend and resume at this point, else the
2997 * control transfer will timeout if the device is suspended!
2999 if (usbd_enum_is_locked(udev))
3000 usbd_sr_unlock(udev);
3005 usbd_ctrl_unlock(struct usb_device *udev)
3007 sx_xunlock(&udev->ctrl_sx);
3010 * Restore the suspend and resume lock after we have unlocked
3011 * the USB control transfer lock to avoid LOR:
3013 if (usbd_enum_is_locked(udev))
3018 * The following function is used to set the per-interface specific
3019 * plug and play information. The string referred to by the pnpinfo
3020 * argument can safely be freed after calling this function. The
3021 * pnpinfo of an interface will be reset at device detach or when
3022 * passing a NULL argument to this function. This function
3023 * returns zero on success, else a USB_ERR_XXX failure code.
3027 usbd_set_pnpinfo(struct usb_device *udev, uint8_t iface_index, const char *pnpinfo)
3029 struct usb_interface *iface;
3031 iface = usbd_get_iface(udev, iface_index);
3033 return (USB_ERR_INVAL);
3035 if (iface->pnpinfo != NULL) {
3036 free(iface->pnpinfo, M_USBDEV);
3037 iface->pnpinfo = NULL;
3040 if (pnpinfo == NULL || pnpinfo[0] == 0)
3041 return (0); /* success */
3043 iface->pnpinfo = strdup(pnpinfo, M_USBDEV);
3044 if (iface->pnpinfo == NULL)
3045 return (USB_ERR_NOMEM);
3047 return (0); /* success */
3051 usbd_add_dynamic_quirk(struct usb_device *udev, uint16_t quirk)
3055 for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) {
3056 if (udev->autoQuirk[x] == 0 ||
3057 udev->autoQuirk[x] == quirk) {
3058 udev->autoQuirk[x] = quirk;
3059 return (0); /* success */
3062 return (USB_ERR_NOMEM);
3066 * The following function is used to select the endpoint mode. It
3067 * should not be called outside enumeration context.
3071 usbd_set_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep,
3077 /* Prevent re-enumeration */
3078 do_unlock = usbd_enum_lock(udev);
3080 if (udev->bus->methods->set_endpoint_mode != NULL) {
3081 error = (udev->bus->methods->set_endpoint_mode) (
3083 } else if (ep_mode != USB_EP_MODE_DEFAULT) {
3084 error = USB_ERR_INVAL;
3089 /* only set new mode regardless of error */
3090 ep->ep_mode = ep_mode;
3093 usbd_enum_unlock(udev);
3098 usbd_get_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep)
3100 return (ep->ep_mode);