3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/stdint.h>
28 #include <sys/stddef.h>
29 #include <sys/param.h>
30 #include <sys/queue.h>
31 #include <sys/types.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
35 #include <sys/module.h>
37 #include <sys/mutex.h>
38 #include <sys/condvar.h>
39 #include <sys/sysctl.h>
41 #include <sys/unistd.h>
42 #include <sys/callout.h>
43 #include <sys/malloc.h>
46 #include <sys/fcntl.h>
48 #include <dev/usb/usb.h>
49 #include <dev/usb/usbdi.h>
50 #include <dev/usb/usbdi_util.h>
51 #include <dev/usb/usb_ioctl.h>
59 #define USB_DEBUG_VAR usb_debug
61 #include <dev/usb/usb_core.h>
62 #include <dev/usb/usb_debug.h>
63 #include <dev/usb/usb_process.h>
64 #include <dev/usb/usb_device.h>
65 #include <dev/usb/usb_busdma.h>
66 #include <dev/usb/usb_transfer.h>
67 #include <dev/usb/usb_request.h>
68 #include <dev/usb/usb_dynamic.h>
69 #include <dev/usb/usb_hub.h>
70 #include <dev/usb/usb_util.h>
71 #include <dev/usb/usb_msctest.h>
73 #include <dev/usb/usb_dev.h>
74 #include <dev/usb/usb_generic.h>
77 #include <dev/usb/quirk/usb_quirk.h>
79 #include <dev/usb/usb_controller.h>
80 #include <dev/usb/usb_bus.h>
82 /* function prototypes */
84 static void usb_init_endpoint(struct usb_device *, uint8_t,
85 struct usb_endpoint_descriptor *,
86 struct usb_endpoint_ss_comp_descriptor *,
87 struct usb_endpoint *);
88 static void usb_unconfigure(struct usb_device *, uint8_t);
89 static void usb_detach_device_sub(struct usb_device *, device_t *,
91 static uint8_t usb_probe_and_attach_sub(struct usb_device *,
92 struct usb_attach_arg *);
93 static void usb_init_attach_arg(struct usb_device *,
94 struct usb_attach_arg *);
95 static void usb_suspend_resume_sub(struct usb_device *, device_t,
97 static void usbd_clear_stall_proc(struct usb_proc_msg *_pm);
98 static usb_error_t usb_config_parse(struct usb_device *, uint8_t, uint8_t);
99 static void usbd_set_device_strings(struct usb_device *);
101 static void usb_notify_addq(const char *type, struct usb_device *);
104 static void usb_fifo_free_wrap(struct usb_device *, uint8_t, uint8_t);
105 static struct cdev *usb_make_dev(struct usb_device *, int, int);
106 static void usb_cdev_create(struct usb_device *);
107 static void usb_cdev_free(struct usb_device *);
108 static void usb_cdev_cleanup(void *);
111 /* This variable is global to allow easy access to it: */
113 int usb_template = 0;
115 TUNABLE_INT("hw.usb.usb_template", &usb_template);
116 SYSCTL_INT(_hw_usb, OID_AUTO, template, CTLFLAG_RW,
117 &usb_template, 0, "Selected USB device side template");
119 /* English is default language */
121 static int usb_lang_id = 0x0009;
122 static int usb_lang_mask = 0x00FF;
124 TUNABLE_INT("hw.usb.usb_lang_id", &usb_lang_id);
125 SYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_id, CTLFLAG_RW,
126 &usb_lang_id, 0, "Preferred USB language ID");
128 TUNABLE_INT("hw.usb.usb_lang_mask", &usb_lang_mask);
129 SYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_mask, CTLFLAG_RW,
130 &usb_lang_mask, 0, "Preferred USB language mask");
132 static const char* statestr[USB_STATE_MAX] = {
133 [USB_STATE_DETACHED] = "DETACHED",
134 [USB_STATE_ATTACHED] = "ATTACHED",
135 [USB_STATE_POWERED] = "POWERED",
136 [USB_STATE_ADDRESSED] = "ADDRESSED",
137 [USB_STATE_CONFIGURED] = "CONFIGURED",
141 usb_statestr(enum usb_dev_state state)
143 return ((state < USB_STATE_MAX) ? statestr[state] : "UNKNOWN");
147 usb_get_manufacturer(struct usb_device *udev)
149 return (udev->manufacturer ? udev->manufacturer : "Unknown");
153 usb_get_product(struct usb_device *udev)
155 return (udev->product ? udev->product : "");
159 usb_get_serial(struct usb_device *udev)
161 return (udev->serial ? udev->serial : "");
164 /*------------------------------------------------------------------------*
165 * usbd_get_ep_by_addr
167 * This function searches for an USB ep by endpoint address and
173 *------------------------------------------------------------------------*/
174 struct usb_endpoint *
175 usbd_get_ep_by_addr(struct usb_device *udev, uint8_t ea_val)
177 struct usb_endpoint *ep = udev->endpoints;
178 struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max;
180 EA_MASK = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR),
184 * According to the USB specification not all bits are used
185 * for the endpoint address. Keep defined bits only:
190 * Iterate accross all the USB endpoints searching for a match
191 * based on the endpoint address:
193 for (; ep != ep_end; ep++) {
195 if (ep->edesc == NULL) {
198 /* do the mask and check the value */
199 if ((ep->edesc->bEndpointAddress & EA_MASK) == ea_val) {
205 * The default endpoint is always present and is checked separately:
207 if ((udev->ctrl_ep.edesc) &&
208 ((udev->ctrl_ep.edesc->bEndpointAddress & EA_MASK) == ea_val)) {
218 /*------------------------------------------------------------------------*
221 * This function searches for an USB endpoint based on the information
222 * given by the passed "struct usb_config" pointer.
226 * Else: Pointer to "struct usb_endpoint".
227 *------------------------------------------------------------------------*/
228 struct usb_endpoint *
229 usbd_get_endpoint(struct usb_device *udev, uint8_t iface_index,
230 const struct usb_config *setup)
232 struct usb_endpoint *ep = udev->endpoints;
233 struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max;
234 uint8_t index = setup->ep_index;
240 DPRINTFN(10, "udev=%p iface_index=%d address=0x%x "
241 "type=0x%x dir=0x%x index=%d\n",
242 udev, iface_index, setup->endpoint,
243 setup->type, setup->direction, setup->ep_index);
247 if (setup->usb_mode != USB_MODE_DUAL &&
248 udev->flags.usb_mode != setup->usb_mode) {
249 /* wrong mode - no endpoint */
253 /* setup expected endpoint direction mask and value */
255 if (setup->direction == UE_DIR_RX) {
256 ea_mask = (UE_DIR_IN | UE_DIR_OUT);
257 ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ?
258 UE_DIR_OUT : UE_DIR_IN;
259 } else if (setup->direction == UE_DIR_TX) {
260 ea_mask = (UE_DIR_IN | UE_DIR_OUT);
261 ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ?
262 UE_DIR_IN : UE_DIR_OUT;
263 } else if (setup->direction == UE_DIR_ANY) {
264 /* match any endpoint direction */
268 /* match the given endpoint direction */
269 ea_mask = (UE_DIR_IN | UE_DIR_OUT);
270 ea_val = (setup->direction & (UE_DIR_IN | UE_DIR_OUT));
273 /* setup expected endpoint address */
275 if (setup->endpoint == UE_ADDR_ANY) {
276 /* match any endpoint address */
278 /* match the given endpoint address */
280 ea_val |= (setup->endpoint & UE_ADDR);
283 /* setup expected endpoint type */
285 if (setup->type == UE_BULK_INTR) {
286 /* this will match BULK and INTERRUPT endpoints */
289 } else if (setup->type == UE_TYPE_ANY) {
290 /* match any endpoint type */
294 /* match the given endpoint type */
295 type_mask = UE_XFERTYPE;
296 type_val = (setup->type & UE_XFERTYPE);
300 * Iterate accross all the USB endpoints searching for a match
301 * based on the endpoint address. Note that we are searching
302 * the endpoints from the beginning of the "udev->endpoints" array.
304 for (; ep != ep_end; ep++) {
306 if ((ep->edesc == NULL) ||
307 (ep->iface_index != iface_index)) {
310 /* do the masks and check the values */
312 if (((ep->edesc->bEndpointAddress & ea_mask) == ea_val) &&
313 ((ep->edesc->bmAttributes & type_mask) == type_val)) {
321 * Match against default endpoint last, so that "any endpoint", "any
322 * address" and "any direction" returns the first endpoint of the
323 * interface. "iface_index" and "direction" is ignored:
325 if ((udev->ctrl_ep.edesc) &&
326 ((udev->ctrl_ep.edesc->bEndpointAddress & ea_mask) == ea_val) &&
327 ((udev->ctrl_ep.edesc->bmAttributes & type_mask) == type_val) &&
338 /*------------------------------------------------------------------------*
339 * usbd_interface_count
341 * This function stores the number of USB interfaces excluding
342 * alternate settings, which the USB config descriptor reports into
343 * the unsigned 8-bit integer pointed to by "count".
348 *------------------------------------------------------------------------*/
350 usbd_interface_count(struct usb_device *udev, uint8_t *count)
352 if (udev->cdesc == NULL) {
354 return (USB_ERR_NOT_CONFIGURED);
356 *count = udev->ifaces_max;
357 return (USB_ERR_NORMAL_COMPLETION);
361 /*------------------------------------------------------------------------*
364 * This function will initialise the USB endpoint structure pointed to by
365 * the "endpoint" argument. The structure pointed to by "endpoint" must be
366 * zeroed before calling this function.
367 *------------------------------------------------------------------------*/
369 usb_init_endpoint(struct usb_device *udev, uint8_t iface_index,
370 struct usb_endpoint_descriptor *edesc,
371 struct usb_endpoint_ss_comp_descriptor *ecomp,
372 struct usb_endpoint *ep)
374 struct usb_bus_methods *methods;
376 methods = udev->bus->methods;
378 (methods->endpoint_init) (udev, edesc, ep);
380 /* initialise USB endpoint structure */
383 ep->iface_index = iface_index;
384 TAILQ_INIT(&ep->endpoint_q.head);
385 ep->endpoint_q.command = &usbd_pipe_start;
387 /* the pipe is not supported by the hardware */
388 if (ep->methods == NULL)
391 /* clear stall, if any */
392 if (methods->clear_stall != NULL) {
393 USB_BUS_LOCK(udev->bus);
394 (methods->clear_stall) (udev, ep);
395 USB_BUS_UNLOCK(udev->bus);
399 /*-----------------------------------------------------------------------*
400 * usb_endpoint_foreach
402 * This function will iterate all the USB endpoints except the control
403 * endpoint. This function is NULL safe.
406 * NULL: End of USB endpoints
407 * Else: Pointer to next USB endpoint
408 *------------------------------------------------------------------------*/
409 struct usb_endpoint *
410 usb_endpoint_foreach(struct usb_device *udev, struct usb_endpoint *ep)
412 struct usb_endpoint *ep_end;
418 ep_end = udev->endpoints + udev->endpoints_max;
420 /* get next endpoint */
422 ep = udev->endpoints;
426 /* find next allocated ep */
427 while (ep != ep_end) {
428 if (ep->edesc != NULL)
435 /*------------------------------------------------------------------------*
438 * This function will free all USB interfaces and USB endpoints belonging
441 * Flag values, see "USB_UNCFG_FLAG_XXX".
442 *------------------------------------------------------------------------*/
444 usb_unconfigure(struct usb_device *udev, uint8_t flag)
448 /* automatic locking */
449 if (usbd_enum_is_locked(udev)) {
453 usbd_enum_lock(udev);
456 /* detach all interface drivers */
457 usb_detach_device(udev, USB_IFACE_INDEX_ANY, flag);
460 /* free all FIFOs except control endpoint FIFOs */
461 usb_fifo_free_wrap(udev, USB_IFACE_INDEX_ANY, flag);
464 * Free all cdev's, if any.
469 #if USB_HAVE_COMPAT_LINUX
470 /* free Linux compat device, if any */
471 if (udev->linux_endpoint_start) {
472 usb_linux_free_device(udev);
473 udev->linux_endpoint_start = NULL;
477 usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_FREE);
479 /* free "cdesc" after "ifaces" and "endpoints", if any */
480 if (udev->cdesc != NULL) {
481 if (udev->flags.usb_mode != USB_MODE_DEVICE)
482 free(udev->cdesc, M_USB);
485 /* set unconfigured state */
486 udev->curr_config_no = USB_UNCONFIG_NO;
487 udev->curr_config_index = USB_UNCONFIG_INDEX;
490 usbd_enum_unlock(udev);
493 /*------------------------------------------------------------------------*
494 * usbd_set_config_index
496 * This function selects configuration by index, independent of the
497 * actual configuration number. This function should not be used by
503 *------------------------------------------------------------------------*/
505 usbd_set_config_index(struct usb_device *udev, uint8_t index)
507 struct usb_status ds;
508 struct usb_config_descriptor *cdp;
515 DPRINTFN(6, "udev=%p index=%d\n", udev, index);
517 /* automatic locking */
518 if (usbd_enum_is_locked(udev)) {
522 usbd_enum_lock(udev);
525 usb_unconfigure(udev, 0);
527 if (index == USB_UNCONFIG_INDEX) {
529 * Leave unallocated when unconfiguring the
530 * device. "usb_unconfigure()" will also reset
531 * the current config number and index.
533 err = usbd_req_set_config(udev, NULL, USB_UNCONFIG_NO);
534 if (udev->state == USB_STATE_CONFIGURED)
535 usb_set_device_state(udev, USB_STATE_ADDRESSED);
538 /* get the full config descriptor */
539 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
540 /* save some memory */
541 err = usbd_req_get_descriptor_ptr(udev, &cdp,
542 (UDESC_CONFIG << 8) | index);
545 err = usbd_req_get_config_desc_full(udev,
546 NULL, &cdp, M_USB, index);
551 /* set the new config descriptor */
555 /* Figure out if the device is self or bus powered. */
557 if ((!udev->flags.uq_bus_powered) &&
558 (cdp->bmAttributes & UC_SELF_POWERED) &&
559 (udev->flags.usb_mode == USB_MODE_HOST)) {
560 /* May be self powered. */
561 if (cdp->bmAttributes & UC_BUS_POWERED) {
562 /* Must ask device. */
563 err = usbd_req_get_device_status(udev, NULL, &ds);
565 DPRINTFN(0, "could not read "
566 "device status: %s\n",
568 } else if (UGETW(ds.wStatus) & UDS_SELF_POWERED) {
571 DPRINTF("status=0x%04x \n",
576 DPRINTF("udev=%p cdesc=%p (addr %d) cno=%d attr=0x%02x, "
577 "selfpowered=%d, power=%d\n",
579 udev->address, cdp->bConfigurationValue, cdp->bmAttributes,
580 selfpowered, cdp->bMaxPower * 2);
582 /* Check if we have enough power. */
583 power = cdp->bMaxPower * 2;
585 if (udev->parent_hub) {
586 max_power = udev->parent_hub->hub->portpower;
588 max_power = USB_MAX_POWER;
591 if (power > max_power) {
592 DPRINTFN(0, "power exceeded %d > %d\n", power, max_power);
593 err = USB_ERR_NO_POWER;
596 /* Only update "self_powered" in USB Host Mode */
597 if (udev->flags.usb_mode == USB_MODE_HOST) {
598 udev->flags.self_powered = selfpowered;
601 udev->curr_config_no = cdp->bConfigurationValue;
602 udev->curr_config_index = index;
603 usb_set_device_state(udev, USB_STATE_CONFIGURED);
605 /* Set the actual configuration value. */
606 err = usbd_req_set_config(udev, NULL, cdp->bConfigurationValue);
611 err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_ALLOC);
616 err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_INIT);
622 /* create device nodes for each endpoint */
623 usb_cdev_create(udev);
627 DPRINTF("error=%s\n", usbd_errstr(err));
629 usb_unconfigure(udev, 0);
632 usbd_enum_unlock(udev);
636 /*------------------------------------------------------------------------*
639 * This function will allocate and free USB interfaces and USB endpoints,
640 * parse the USB configuration structure and initialise the USB endpoints
641 * and interfaces. If "iface_index" is not equal to
642 * "USB_IFACE_INDEX_ANY" then the "cmd" parameter is the
643 * alternate_setting to be selected for the given interface. Else the
644 * "cmd" parameter is defined by "USB_CFG_XXX". "iface_index" can be
645 * "USB_IFACE_INDEX_ANY" or a valid USB interface index. This function
646 * is typically called when setting the configuration or when setting
647 * an alternate interface.
652 *------------------------------------------------------------------------*/
654 usb_config_parse(struct usb_device *udev, uint8_t iface_index, uint8_t cmd)
656 struct usb_idesc_parse_state ips;
657 struct usb_interface_descriptor *id;
658 struct usb_endpoint_descriptor *ed;
659 struct usb_interface *iface;
660 struct usb_endpoint *ep;
668 if (iface_index != USB_IFACE_INDEX_ANY) {
669 /* parameter overload */
679 DPRINTFN(5, "iface_index=%d cmd=%d\n",
682 if (cmd == USB_CFG_FREE)
685 if (cmd == USB_CFG_INIT) {
686 sx_assert(&udev->enum_sx, SA_LOCKED);
688 /* check for in-use endpoints */
690 ep = udev->endpoints;
691 ep_max = udev->endpoints_max;
693 /* look for matching endpoints */
694 if ((iface_index == USB_IFACE_INDEX_ANY) ||
695 (iface_index == ep->iface_index)) {
696 if (ep->refcount_alloc != 0) {
698 * This typically indicates a
699 * more serious error.
701 err = USB_ERR_IN_USE;
704 memset(ep, 0, sizeof(*ep));
705 /* make sure we don't zero the endpoint again */
706 ep->iface_index = USB_IFACE_INDEX_ANY;
716 memset(&ips, 0, sizeof(ips));
721 while ((id = usb_idesc_foreach(udev->cdesc, &ips))) {
723 /* check for interface overflow */
724 if (ips.iface_index == USB_IFACE_MAX)
727 iface = udev->ifaces + ips.iface_index;
729 /* check for specific interface match */
731 if (cmd == USB_CFG_INIT) {
732 if ((iface_index != USB_IFACE_INDEX_ANY) &&
733 (iface_index != ips.iface_index)) {
734 /* wrong interface */
736 } else if (alt_index != ips.iface_index_alt) {
737 /* wrong alternate setting */
740 /* initialise interface */
746 /* check for new interface */
747 if (ips.iface_index_alt == 0) {
748 /* update current number of endpoints */
753 /* setup the USB interface structure */
755 /* default setting */
756 iface->parent_iface_index = USB_IFACE_INDEX_ANY;
757 /* set alternate index */
758 iface->alt_index = alt_index;
761 DPRINTFN(5, "found idesc nendpt=%d\n", id->bNumEndpoints);
763 ed = (struct usb_endpoint_descriptor *)id;
767 /* iterate all the endpoint descriptors */
768 while ((ed = usb_edesc_foreach(udev->cdesc, ed))) {
770 if (temp == USB_EP_MAX)
773 ep = udev->endpoints + temp;
778 ecomp = usb_ed_comp_foreach(udev->cdesc, (void *)ed);
780 DPRINTFN(5, "Found endpoint companion descriptor\n");
782 usb_init_endpoint(udev,
783 ips.iface_index, ed, ecomp, ep);
788 /* find maximum number of endpoints */
793 id = (struct usb_interface_descriptor *)ed;
797 /* NOTE: It is valid to have no interfaces and no endpoints! */
799 if (cmd == USB_CFG_ALLOC) {
800 udev->ifaces_max = ips.iface_index;
802 if (udev->ifaces_max != 0) {
803 udev->ifaces = malloc(sizeof(*iface) * udev->ifaces_max,
804 M_USB, M_WAITOK | M_ZERO);
805 if (udev->ifaces == NULL) {
811 udev->endpoints = malloc(sizeof(*ep) * ep_max,
812 M_USB, M_WAITOK | M_ZERO);
813 if (udev->endpoints == NULL) {
818 udev->endpoints = NULL;
820 USB_BUS_LOCK(udev->bus);
821 udev->endpoints_max = ep_max;
822 /* reset any ongoing clear-stall */
823 udev->ep_curr = NULL;
824 USB_BUS_UNLOCK(udev->bus);
829 if (cmd == USB_CFG_ALLOC) {
831 USB_BUS_LOCK(udev->bus);
832 udev->endpoints_max = 0;
833 /* reset any ongoing clear-stall */
834 udev->ep_curr = NULL;
835 USB_BUS_UNLOCK(udev->bus);
838 if (udev->ifaces != NULL)
839 free(udev->ifaces, M_USB);
840 if (udev->endpoints != NULL)
841 free(udev->endpoints, M_USB);
844 udev->endpoints = NULL;
845 udev->ifaces_max = 0;
851 /*------------------------------------------------------------------------*
852 * usbd_set_alt_interface_index
854 * This function will select an alternate interface index for the
855 * given interface index. The interface should not be in use when this
856 * function is called. That means there should not be any open USB
857 * transfers. Else an error is returned. If the alternate setting is
858 * already set this function will simply return success. This function
859 * is called in Host mode and Device mode!
864 *------------------------------------------------------------------------*/
866 usbd_set_alt_interface_index(struct usb_device *udev,
867 uint8_t iface_index, uint8_t alt_index)
869 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
873 /* automatic locking */
874 if (usbd_enum_is_locked(udev)) {
878 usbd_enum_lock(udev);
884 if (iface->alt_index == alt_index) {
886 * Optimise away duplicate setting of
887 * alternate setting in USB Host Mode!
894 * Free all generic FIFOs for this interface, except control
897 usb_fifo_free_wrap(udev, iface_index, 0);
900 err = usb_config_parse(udev, iface_index, alt_index);
904 if (iface->alt_index != alt_index) {
905 /* the alternate setting does not exist */
910 err = usbd_req_set_alt_interface_no(udev, NULL, iface_index,
911 iface->idesc->bAlternateSetting);
915 usbd_enum_unlock(udev);
920 /*------------------------------------------------------------------------*
921 * usbd_set_endpoint_stall
923 * This function is used to make a BULK or INTERRUPT endpoint send
924 * STALL tokens in USB device mode.
929 *------------------------------------------------------------------------*/
931 usbd_set_endpoint_stall(struct usb_device *udev, struct usb_endpoint *ep,
934 struct usb_xfer *xfer;
940 DPRINTF("Cannot find endpoint\n");
942 * Pretend that the clear or set stall request is
943 * successful else some USB host stacks can do
944 * strange things, especially when a control endpoint
949 et = (ep->edesc->bmAttributes & UE_XFERTYPE);
951 if ((et != UE_BULK) &&
952 (et != UE_INTERRUPT)) {
954 * Should not stall control
955 * nor isochronous endpoints.
957 DPRINTF("Invalid endpoint\n");
960 USB_BUS_LOCK(udev->bus);
962 /* store current stall state */
963 was_stalled = ep->is_stalled;
965 /* check for no change */
966 if (was_stalled && do_stall) {
967 /* if the endpoint is already stalled do nothing */
968 USB_BUS_UNLOCK(udev->bus);
969 DPRINTF("No change\n");
972 /* set stalled state */
975 if (do_stall || (!was_stalled)) {
977 /* lookup the current USB transfer, if any */
978 xfer = ep->endpoint_q.curr;
984 * If "xfer" is non-NULL the "set_stall" method will
985 * complete the USB transfer like in case of a timeout
986 * setting the error code "USB_ERR_STALLED".
988 (udev->bus->methods->set_stall) (udev, xfer, ep, &do_stall);
991 ep->toggle_next = 0; /* reset data toggle */
992 ep->is_stalled = 0; /* clear stalled state */
994 (udev->bus->methods->clear_stall) (udev, ep);
996 /* start up the current or next transfer, if any */
997 usb_command_wrapper(&ep->endpoint_q, ep->endpoint_q.curr);
999 USB_BUS_UNLOCK(udev->bus);
1003 /*------------------------------------------------------------------------*
1004 * usb_reset_iface_endpoints - used in USB device side mode
1005 *------------------------------------------------------------------------*/
1007 usb_reset_iface_endpoints(struct usb_device *udev, uint8_t iface_index)
1009 struct usb_endpoint *ep;
1010 struct usb_endpoint *ep_end;
1012 ep = udev->endpoints;
1013 ep_end = udev->endpoints + udev->endpoints_max;
1015 for (; ep != ep_end; ep++) {
1017 if ((ep->edesc == NULL) ||
1018 (ep->iface_index != iface_index)) {
1021 /* simulate a clear stall from the peer */
1022 usbd_set_endpoint_stall(udev, ep, 0);
1027 /*------------------------------------------------------------------------*
1028 * usb_detach_device_sub
1030 * This function will try to detach an USB device. If it fails a panic
1033 * Flag values, see "USB_UNCFG_FLAG_XXX".
1034 *------------------------------------------------------------------------*/
1036 usb_detach_device_sub(struct usb_device *udev, device_t *ppdev,
1037 char **ppnpinfo, uint8_t flag)
1046 * NOTE: It is important to clear "*ppdev" before deleting
1047 * the child due to some device methods being called late
1048 * during the delete process !
1052 device_printf(dev, "at %s, port %d, addr %d "
1054 device_get_nameunit(udev->parent_dev),
1055 udev->port_no, udev->address);
1057 if (device_is_attached(dev)) {
1058 if (udev->flags.peer_suspended) {
1059 err = DEVICE_RESUME(dev);
1061 device_printf(dev, "Resume failed\n");
1064 if (device_detach(dev)) {
1068 if (device_delete_child(udev->parent_dev, dev)) {
1073 pnpinfo = *ppnpinfo;
1074 if (pnpinfo != NULL) {
1076 free(pnpinfo, M_USBDEV);
1081 /* Detach is not allowed to fail in the USB world */
1082 panic("usb_detach_device_sub: A USB driver would not detach\n");
1085 /*------------------------------------------------------------------------*
1088 * The following function will detach the matching interfaces.
1089 * This function is NULL safe.
1091 * Flag values, see "USB_UNCFG_FLAG_XXX".
1092 *------------------------------------------------------------------------*/
1094 usb_detach_device(struct usb_device *udev, uint8_t iface_index,
1097 struct usb_interface *iface;
1104 DPRINTFN(4, "udev=%p\n", udev);
1106 sx_assert(&udev->enum_sx, SA_LOCKED);
1109 * First detach the child to give the child's detach routine a
1110 * chance to detach the sub-devices in the correct order.
1111 * Then delete the child using "device_delete_child()" which
1112 * will detach all sub-devices from the bottom and upwards!
1114 if (iface_index != USB_IFACE_INDEX_ANY) {
1116 iface_index = i + 1;
1119 iface_index = USB_IFACE_MAX;
1124 for (; i != iface_index; i++) {
1126 iface = usbd_get_iface(udev, i);
1127 if (iface == NULL) {
1128 /* looks like the end of the USB interfaces */
1131 usb_detach_device_sub(udev, &iface->subdev,
1132 &iface->pnpinfo, flag);
1136 /*------------------------------------------------------------------------*
1137 * usb_probe_and_attach_sub
1142 *------------------------------------------------------------------------*/
1144 usb_probe_and_attach_sub(struct usb_device *udev,
1145 struct usb_attach_arg *uaa)
1147 struct usb_interface *iface;
1152 if (iface->parent_iface_index != USB_IFACE_INDEX_ANY) {
1153 /* leave interface alone */
1156 dev = iface->subdev;
1159 /* clean up after module unload */
1161 if (device_is_attached(dev)) {
1162 /* already a device there */
1165 /* clear "iface->subdev" as early as possible */
1167 iface->subdev = NULL;
1169 if (device_delete_child(udev->parent_dev, dev)) {
1172 * Panic here, else one can get a double call
1173 * to device_detach(). USB devices should
1174 * never fail on detach!
1176 panic("device_delete_child() failed\n");
1179 if (uaa->temp_dev == NULL) {
1181 /* create a new child */
1182 uaa->temp_dev = device_add_child(udev->parent_dev, NULL, -1);
1183 if (uaa->temp_dev == NULL) {
1184 device_printf(udev->parent_dev,
1185 "Device creation failed\n");
1186 return (1); /* failure */
1188 device_set_ivars(uaa->temp_dev, uaa);
1189 device_quiet(uaa->temp_dev);
1192 * Set "subdev" before probe and attach so that "devd" gets
1193 * the information it needs.
1195 iface->subdev = uaa->temp_dev;
1197 if (device_probe_and_attach(iface->subdev) == 0) {
1199 * The USB attach arguments are only available during probe
1202 uaa->temp_dev = NULL;
1203 device_set_ivars(iface->subdev, NULL);
1205 if (udev->flags.peer_suspended) {
1206 err = DEVICE_SUSPEND(iface->subdev);
1208 device_printf(iface->subdev, "Suspend failed\n");
1210 return (0); /* success */
1212 /* No USB driver found */
1213 iface->subdev = NULL;
1215 return (1); /* failure */
1218 /*------------------------------------------------------------------------*
1219 * usbd_set_parent_iface
1221 * Using this function will lock the alternate interface setting on an
1222 * interface. It is typically used for multi interface drivers. In USB
1223 * device side mode it is assumed that the alternate interfaces all
1224 * have the same endpoint descriptors. The default parent index value
1225 * is "USB_IFACE_INDEX_ANY". Then the alternate setting value is not
1227 *------------------------------------------------------------------------*/
1229 usbd_set_parent_iface(struct usb_device *udev, uint8_t iface_index,
1230 uint8_t parent_index)
1232 struct usb_interface *iface;
1234 iface = usbd_get_iface(udev, iface_index);
1236 iface->parent_iface_index = parent_index;
1241 usb_init_attach_arg(struct usb_device *udev,
1242 struct usb_attach_arg *uaa)
1244 bzero(uaa, sizeof(*uaa));
1247 uaa->usb_mode = udev->flags.usb_mode;
1248 uaa->port = udev->port_no;
1249 uaa->dev_state = UAA_DEV_READY;
1251 uaa->info.idVendor = UGETW(udev->ddesc.idVendor);
1252 uaa->info.idProduct = UGETW(udev->ddesc.idProduct);
1253 uaa->info.bcdDevice = UGETW(udev->ddesc.bcdDevice);
1254 uaa->info.bDeviceClass = udev->ddesc.bDeviceClass;
1255 uaa->info.bDeviceSubClass = udev->ddesc.bDeviceSubClass;
1256 uaa->info.bDeviceProtocol = udev->ddesc.bDeviceProtocol;
1257 uaa->info.bConfigIndex = udev->curr_config_index;
1258 uaa->info.bConfigNum = udev->curr_config_no;
1261 /*------------------------------------------------------------------------*
1262 * usb_probe_and_attach
1264 * This function is called from "uhub_explore_sub()",
1265 * "usb_handle_set_config()" and "usb_handle_request()".
1269 * Else: A control transfer failed
1270 *------------------------------------------------------------------------*/
1272 usb_probe_and_attach(struct usb_device *udev, uint8_t iface_index)
1274 struct usb_attach_arg uaa;
1275 struct usb_interface *iface;
1281 DPRINTF("udev == NULL\n");
1282 return (USB_ERR_INVAL);
1284 /* automatic locking */
1285 if (usbd_enum_is_locked(udev)) {
1289 usbd_enum_lock(udev);
1292 if (udev->curr_config_index == USB_UNCONFIG_INDEX) {
1293 /* do nothing - no configuration has been set */
1296 /* setup USB attach arguments */
1298 usb_init_attach_arg(udev, &uaa);
1300 /* Check if only one interface should be probed: */
1301 if (iface_index != USB_IFACE_INDEX_ANY) {
1309 /* Do the probe and attach */
1310 for (; i != j; i++) {
1312 iface = usbd_get_iface(udev, i);
1313 if (iface == NULL) {
1315 * Looks like the end of the USB
1318 DPRINTFN(2, "end of interfaces "
1322 if (iface->idesc == NULL) {
1323 /* no interface descriptor */
1328 uaa.info.bInterfaceClass =
1329 iface->idesc->bInterfaceClass;
1330 uaa.info.bInterfaceSubClass =
1331 iface->idesc->bInterfaceSubClass;
1332 uaa.info.bInterfaceProtocol =
1333 iface->idesc->bInterfaceProtocol;
1334 uaa.info.bIfaceIndex = i;
1335 uaa.info.bIfaceNum =
1336 iface->idesc->bInterfaceNumber;
1337 uaa.driver_info = 0; /* reset driver_info */
1339 DPRINTFN(2, "iclass=%u/%u/%u iindex=%u/%u\n",
1340 uaa.info.bInterfaceClass,
1341 uaa.info.bInterfaceSubClass,
1342 uaa.info.bInterfaceProtocol,
1343 uaa.info.bIfaceIndex,
1344 uaa.info.bIfaceNum);
1346 if (usb_probe_and_attach_sub(udev, &uaa)) {
1352 /* remove the last created child; it is unused */
1354 if (device_delete_child(udev->parent_dev, uaa.temp_dev)) {
1355 DPRINTFN(0, "device delete child failed\n");
1360 usbd_enum_unlock(udev);
1365 /*------------------------------------------------------------------------*
1366 * usb_suspend_resume_sub
1368 * This function is called when the suspend or resume methods should
1369 * be executed on an USB device.
1370 *------------------------------------------------------------------------*/
1372 usb_suspend_resume_sub(struct usb_device *udev, device_t dev, uint8_t do_suspend)
1379 if (!device_is_attached(dev)) {
1383 err = DEVICE_SUSPEND(dev);
1385 err = DEVICE_RESUME(dev);
1388 device_printf(dev, "%s failed\n",
1389 do_suspend ? "Suspend" : "Resume");
1393 /*------------------------------------------------------------------------*
1394 * usb_suspend_resume
1396 * The following function will suspend or resume the USB device.
1401 *------------------------------------------------------------------------*/
1403 usb_suspend_resume(struct usb_device *udev, uint8_t do_suspend)
1405 struct usb_interface *iface;
1412 DPRINTFN(4, "udev=%p do_suspend=%d\n", udev, do_suspend);
1414 sx_assert(&udev->sr_sx, SA_LOCKED);
1416 USB_BUS_LOCK(udev->bus);
1417 /* filter the suspend events */
1418 if (udev->flags.peer_suspended == do_suspend) {
1419 USB_BUS_UNLOCK(udev->bus);
1423 udev->flags.peer_suspended = do_suspend;
1424 USB_BUS_UNLOCK(udev->bus);
1426 /* do the suspend or resume */
1428 for (i = 0; i != USB_IFACE_MAX; i++) {
1430 iface = usbd_get_iface(udev, i);
1431 if (iface == NULL) {
1432 /* looks like the end of the USB interfaces */
1435 usb_suspend_resume_sub(udev, iface->subdev, do_suspend);
1440 /*------------------------------------------------------------------------*
1441 * usbd_clear_stall_proc
1443 * This function performs generic USB clear stall operations.
1444 *------------------------------------------------------------------------*/
1446 usbd_clear_stall_proc(struct usb_proc_msg *_pm)
1448 struct usb_clear_stall_msg *pm = (void *)_pm;
1449 struct usb_device *udev = pm->udev;
1452 USB_BUS_UNLOCK(udev->bus);
1453 mtx_lock(&udev->device_mtx);
1455 /* Start clear stall callback */
1456 usbd_transfer_start(udev->ctrl_xfer[1]);
1459 mtx_unlock(&udev->device_mtx);
1460 USB_BUS_LOCK(udev->bus);
1463 /*------------------------------------------------------------------------*
1466 * This function allocates a new USB device. This function is called
1467 * when a new device has been put in the powered state, but not yet in
1468 * the addressed state. Get initial descriptor, set the address, get
1469 * full descriptor and get strings.
1474 *------------------------------------------------------------------------*/
1476 usb_alloc_device(device_t parent_dev, struct usb_bus *bus,
1477 struct usb_device *parent_hub, uint8_t depth, uint8_t port_index,
1478 uint8_t port_no, enum usb_dev_speed speed, enum usb_hc_mode mode)
1480 struct usb_attach_arg uaa;
1481 struct usb_device *udev;
1482 struct usb_device *adev;
1483 struct usb_device *hub;
1484 uint8_t *scratch_ptr;
1485 size_t scratch_size;
1487 uint8_t device_index;
1488 uint8_t config_index;
1489 uint8_t config_quirk;
1490 uint8_t set_config_failed;
1492 DPRINTF("parent_dev=%p, bus=%p, parent_hub=%p, depth=%u, "
1493 "port_index=%u, port_no=%u, speed=%u, usb_mode=%u\n",
1494 parent_dev, bus, parent_hub, depth, port_index, port_no,
1498 * Find an unused device index. In USB Host mode this is the
1499 * same as the device address.
1501 * Device index zero is not used and device index 1 should
1502 * always be the root hub.
1504 for (device_index = USB_ROOT_HUB_ADDR;
1505 (device_index != bus->devices_max) &&
1506 (bus->devices[device_index] != NULL);
1507 device_index++) /* nop */;
1509 if (device_index == bus->devices_max) {
1510 device_printf(bus->bdev,
1511 "No free USB device index for new device\n");
1516 device_printf(bus->bdev,
1517 "Invalid device depth\n");
1520 udev = malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO);
1524 /* initialise our SX-lock */
1525 sx_init_flags(&udev->ctrl_sx, "USB device SX lock", SX_DUPOK);
1527 /* initialise our SX-lock */
1528 sx_init_flags(&udev->enum_sx, "USB config SX lock", SX_DUPOK);
1529 sx_init_flags(&udev->sr_sx, "USB suspend and resume SX lock", SX_DUPOK);
1531 cv_init(&udev->ctrlreq_cv, "WCTRL");
1532 cv_init(&udev->ref_cv, "UGONE");
1534 /* initialise our mutex */
1535 mtx_init(&udev->device_mtx, "USB device mutex", NULL, MTX_DEF);
1537 /* initialise generic clear stall */
1538 udev->cs_msg[0].hdr.pm_callback = &usbd_clear_stall_proc;
1539 udev->cs_msg[0].udev = udev;
1540 udev->cs_msg[1].hdr.pm_callback = &usbd_clear_stall_proc;
1541 udev->cs_msg[1].udev = udev;
1543 /* initialise some USB device fields */
1544 udev->parent_hub = parent_hub;
1545 udev->parent_dev = parent_dev;
1546 udev->port_index = port_index;
1547 udev->port_no = port_no;
1548 udev->depth = depth;
1550 udev->address = USB_START_ADDR; /* default value */
1551 udev->plugtime = (usb_ticks_t)ticks;
1553 * We need to force the power mode to "on" because there are plenty
1554 * of USB devices out there that do not work very well with
1555 * automatic suspend and resume!
1557 udev->power_mode = usbd_filter_power_mode(udev, USB_POWER_MODE_ON);
1558 udev->pwr_save.last_xfer_time = ticks;
1559 /* we are not ready yet */
1562 /* set up default endpoint descriptor */
1563 udev->ctrl_ep_desc.bLength = sizeof(udev->ctrl_ep_desc);
1564 udev->ctrl_ep_desc.bDescriptorType = UDESC_ENDPOINT;
1565 udev->ctrl_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
1566 udev->ctrl_ep_desc.bmAttributes = UE_CONTROL;
1567 udev->ctrl_ep_desc.wMaxPacketSize[0] = USB_MAX_IPACKET;
1568 udev->ctrl_ep_desc.wMaxPacketSize[1] = 0;
1569 udev->ctrl_ep_desc.bInterval = 0;
1571 /* set up default endpoint companion descriptor */
1572 udev->ctrl_ep_comp_desc.bLength = sizeof(udev->ctrl_ep_comp_desc);
1573 udev->ctrl_ep_comp_desc.bDescriptorType = UDESC_ENDPOINT_SS_COMP;
1575 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
1577 udev->speed = speed;
1578 udev->flags.usb_mode = mode;
1580 /* search for our High Speed USB HUB, if any */
1583 hub = udev->parent_hub;
1586 if (hub->speed == USB_SPEED_HIGH) {
1587 udev->hs_hub_addr = hub->address;
1588 udev->parent_hs_hub = hub;
1589 udev->hs_port_no = adev->port_no;
1593 hub = hub->parent_hub;
1596 /* init the default endpoint */
1597 usb_init_endpoint(udev, 0,
1598 &udev->ctrl_ep_desc,
1599 &udev->ctrl_ep_comp_desc,
1602 /* set device index */
1603 udev->device_index = device_index;
1606 /* Create ugen name */
1607 snprintf(udev->ugen_name, sizeof(udev->ugen_name),
1608 USB_GENERIC_NAME "%u.%u", device_get_unit(bus->bdev),
1610 LIST_INIT(&udev->pd_list);
1612 /* Create the control endpoint device */
1613 udev->ctrl_dev = usb_make_dev(udev, 0, FREAD|FWRITE);
1615 /* Create a link from /dev/ugenX.X to the default endpoint */
1616 make_dev_alias(udev->ctrl_dev, "%s", udev->ugen_name);
1618 /* Initialise device */
1619 if (bus->methods->device_init != NULL) {
1620 err = (bus->methods->device_init) (udev);
1622 DPRINTFN(0, "device init %d failed "
1623 "(%s, ignored)\n", device_index,
1628 /* set powered device state after device init is complete */
1629 usb_set_device_state(udev, USB_STATE_POWERED);
1631 if (udev->flags.usb_mode == USB_MODE_HOST) {
1633 err = usbd_req_set_address(udev, NULL, device_index);
1636 * This is the new USB device address from now on, if
1637 * the set address request didn't set it already.
1639 if (udev->address == USB_START_ADDR)
1640 udev->address = device_index;
1643 * We ignore any set-address errors, hence there are
1644 * buggy USB devices out there that actually receive
1645 * the SETUP PID, but manage to set the address before
1646 * the STATUS stage is ACK'ed. If the device responds
1647 * to the subsequent get-descriptor at the new
1648 * address, then we know that the set-address command
1652 DPRINTFN(0, "set address %d failed "
1653 "(%s, ignored)\n", udev->address,
1657 /* We are not self powered */
1658 udev->flags.self_powered = 0;
1660 /* Set unconfigured state */
1661 udev->curr_config_no = USB_UNCONFIG_NO;
1662 udev->curr_config_index = USB_UNCONFIG_INDEX;
1664 /* Setup USB descriptors */
1665 err = (usb_temp_setup_by_index_p) (udev, usb_template);
1667 DPRINTFN(0, "setting up USB template failed maybe the USB "
1668 "template module has not been loaded\n");
1672 usb_set_device_state(udev, USB_STATE_ADDRESSED);
1674 /* setup the device descriptor and the initial "wMaxPacketSize" */
1675 err = usbd_setup_device_desc(udev, NULL);
1678 /* XXX try to re-enumerate the device */
1679 err = usbd_req_re_enumerate(udev, NULL);
1685 * Setup temporary USB attach args so that we can figure out some
1686 * basic quirks for this device.
1688 usb_init_attach_arg(udev, &uaa);
1690 if (usb_test_quirk(&uaa, UQ_BUS_POWERED)) {
1691 udev->flags.uq_bus_powered = 1;
1693 if (usb_test_quirk(&uaa, UQ_NO_STRINGS)) {
1694 udev->flags.no_strings = 1;
1697 * Workaround for buggy USB devices.
1699 * It appears that some string-less USB chips will crash and
1700 * disappear if any attempts are made to read any string
1703 * Try to detect such chips by checking the strings in the USB
1704 * device descriptor. If no strings are present there we
1705 * simply disable all USB strings.
1707 scratch_ptr = udev->bus->scratch[0].data;
1708 scratch_size = sizeof(udev->bus->scratch[0].data);
1710 if (udev->ddesc.iManufacturer ||
1711 udev->ddesc.iProduct ||
1712 udev->ddesc.iSerialNumber) {
1713 /* read out the language ID string */
1714 err = usbd_req_get_string_desc(udev, NULL,
1715 (char *)scratch_ptr, 4, 0, USB_LANGUAGE_TABLE);
1717 err = USB_ERR_INVAL;
1720 if (err || (scratch_ptr[0] < 4)) {
1721 udev->flags.no_strings = 1;
1728 /* load preferred value and mask */
1730 mask = usb_lang_mask;
1732 /* align length correctly */
1733 scratch_ptr[0] &= ~1;
1735 /* fix compiler warning */
1738 /* search for preferred language */
1739 for (x = 2; (x < scratch_ptr[0]); x += 2) {
1740 langid = UGETW(scratch_ptr + x);
1741 if ((langid & mask) == pref)
1744 if (x >= scratch_ptr[0]) {
1745 /* pick the first language as the default */
1746 DPRINTFN(1, "Using first language\n");
1747 langid = UGETW(scratch_ptr + 2);
1750 DPRINTFN(1, "Language selected: 0x%04x\n", langid);
1751 udev->langid = langid;
1754 /* assume 100mA bus powered for now. Changed when configured. */
1755 udev->power = USB_MIN_POWER;
1756 /* fetch the vendor and product strings from the device */
1757 usbd_set_device_strings(udev);
1759 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
1760 /* USB device mode setup is complete */
1766 * Most USB devices should attach to config index 0 by
1769 if (usb_test_quirk(&uaa, UQ_CFG_INDEX_0)) {
1772 } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_1)) {
1775 } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_2)) {
1778 } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_3)) {
1781 } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_4)) {
1789 set_config_failed = 0;
1792 DPRINTF("setting config %u\n", config_index);
1794 /* get the USB device configured */
1795 err = usbd_set_config_index(udev, config_index);
1797 if (udev->ddesc.bNumConfigurations != 0) {
1798 if (!set_config_failed) {
1799 set_config_failed = 1;
1800 /* XXX try to re-enumerate the device */
1801 err = usbd_req_re_enumerate(udev, NULL);
1803 goto repeat_set_config;
1805 DPRINTFN(0, "Failure selecting configuration index %u:"
1806 "%s, port %u, addr %u (ignored)\n",
1807 config_index, usbd_errstr(err), udev->port_no,
1811 * Some USB devices do not have any configurations. Ignore any
1812 * set config failures!
1817 if (!config_quirk && config_index + 1 < udev->ddesc.bNumConfigurations) {
1818 if ((udev->cdesc->bNumInterface < 2) &&
1819 usbd_get_no_descriptors(udev->cdesc, UDESC_ENDPOINT) == 0) {
1820 DPRINTFN(0, "Found no endpoints, trying next config\n");
1822 goto repeat_set_config;
1824 if (config_index == 0) {
1826 * Try to figure out if we have an
1827 * auto-install disk there:
1829 if (usb_iface_is_cdrom(udev, 0)) {
1830 DPRINTFN(0, "Found possible auto-install "
1831 "disk (trying next config)\n");
1833 goto repeat_set_config;
1837 EVENTHANDLER_INVOKE(usb_dev_configured, udev, &uaa);
1838 if (uaa.dev_state != UAA_DEV_READY) {
1839 /* leave device unconfigured */
1840 usb_unconfigure(udev, 0);
1844 DPRINTF("new dev (addr %d), udev=%p, parent_hub=%p\n",
1845 udev->address, udev, udev->parent_hub);
1847 /* register our device - we are ready */
1848 usb_bus_port_set_device(bus, parent_hub ?
1849 parent_hub->hub->ports + port_index : NULL, udev, device_index);
1852 /* Symlink the ugen device name */
1853 udev->ugen_symlink = usb_alloc_symlink(udev->ugen_name);
1855 /* Announce device */
1856 printf("%s: <%s> at %s\n", udev->ugen_name,
1857 usb_get_manufacturer(udev),
1858 device_get_nameunit(udev->bus->bdev));
1862 usb_notify_addq("ATTACH", udev);
1867 * Free USB device and all subdevices, if any.
1869 usb_free_device(udev, 0);
1876 static struct cdev *
1877 usb_make_dev(struct usb_device *udev, int ep, int mode)
1879 struct usb_fs_privdata* pd;
1882 /* Store information to locate ourselves again later */
1883 pd = malloc(sizeof(struct usb_fs_privdata), M_USBDEV,
1885 pd->bus_index = device_get_unit(udev->bus->bdev);
1886 pd->dev_index = udev->device_index;
1890 /* Now, create the device itself */
1891 snprintf(devname, sizeof(devname), "%u.%u.%u",
1892 pd->bus_index, pd->dev_index, pd->ep_addr);
1893 pd->cdev = make_dev(&usb_devsw, 0, UID_ROOT,
1894 GID_OPERATOR, 0600, USB_DEVICE_DIR "/%s", devname);
1895 pd->cdev->si_drv1 = pd;
1901 usb_cdev_create(struct usb_device *udev)
1903 struct usb_config_descriptor *cd;
1904 struct usb_endpoint_descriptor *ed;
1905 struct usb_descriptor *desc;
1906 struct usb_fs_privdata* pd;
1908 int inmode, outmode, inmask, outmask, mode;
1911 KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("stale cdev entries"));
1913 DPRINTFN(2, "Creating device nodes\n");
1915 if (usbd_get_mode(udev) == USB_MODE_DEVICE) {
1918 } else { /* USB_MODE_HOST */
1928 * Collect all used endpoint numbers instead of just
1929 * generating 16 static endpoints.
1931 cd = usbd_get_config_descriptor(udev);
1932 while ((desc = usb_desc_foreach(cd, desc))) {
1933 /* filter out all endpoint descriptors */
1934 if ((desc->bDescriptorType == UDESC_ENDPOINT) &&
1935 (desc->bLength >= sizeof(*ed))) {
1936 ed = (struct usb_endpoint_descriptor *)desc;
1939 ep = ed->bEndpointAddress;
1940 if (UE_GET_DIR(ep) == UE_DIR_OUT)
1941 outmask |= 1 << UE_GET_ADDR(ep);
1943 inmask |= 1 << UE_GET_ADDR(ep);
1947 /* Create all available endpoints except EP0 */
1948 for (ep = 1; ep < 16; ep++) {
1949 mode = inmask & (1 << ep) ? inmode : 0;
1950 mode |= outmask & (1 << ep) ? outmode : 0;
1952 continue; /* no IN or OUT endpoint */
1954 dev = usb_make_dev(udev, ep, mode);
1956 LIST_INSERT_HEAD(&udev->pd_list, pd, pd_next);
1961 usb_cdev_free(struct usb_device *udev)
1963 struct usb_fs_privdata* pd;
1966 DPRINTFN(2, "Freeing device nodes\n");
1968 while ((pd = LIST_FIRST(&udev->pd_list)) != NULL) {
1969 KASSERT(pd->cdev->si_drv1 == pd, ("privdata corrupt"));
1973 LIST_REMOVE(pd, pd_next);
1975 destroy_dev_sched_cb(pcdev, usb_cdev_cleanup, pd);
1980 usb_cdev_cleanup(void* arg)
1982 free(arg, M_USBDEV);
1986 /*------------------------------------------------------------------------*
1989 * This function is NULL safe and will free an USB device and its
1990 * children devices, if any.
1992 * Flag values: Reserved, set to zero.
1993 *------------------------------------------------------------------------*/
1995 usb_free_device(struct usb_device *udev, uint8_t flag)
1997 struct usb_bus *bus;
2000 return; /* already freed */
2002 DPRINTFN(4, "udev=%p port=%d\n", udev, udev->port_no);
2005 usb_set_device_state(udev, USB_STATE_DETACHED);
2008 usb_notify_addq("DETACH", udev);
2012 printf("%s: <%s> at %s (disconnected)\n", udev->ugen_name,
2013 usb_get_manufacturer(udev), device_get_nameunit(bus->bdev));
2015 /* Destroy UGEN symlink, if any */
2016 if (udev->ugen_symlink) {
2017 usb_free_symlink(udev->ugen_symlink);
2018 udev->ugen_symlink = NULL;
2022 * Unregister our device first which will prevent any further
2025 usb_bus_port_set_device(bus, udev->parent_hub ?
2026 udev->parent_hub->hub->ports + udev->port_index : NULL,
2027 NULL, USB_ROOT_HUB_ADDR);
2030 /* wait for all pending references to go away: */
2031 mtx_lock(&usb_ref_lock);
2033 while (udev->refcount != 0) {
2034 cv_wait(&udev->ref_cv, &usb_ref_lock);
2036 mtx_unlock(&usb_ref_lock);
2038 destroy_dev_sched_cb(udev->ctrl_dev, usb_cdev_cleanup,
2039 udev->ctrl_dev->si_drv1);
2042 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2043 /* stop receiving any control transfers (Device Side Mode) */
2044 usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
2047 /* the following will get the device unconfigured in software */
2048 usb_unconfigure(udev, USB_UNCFG_FLAG_FREE_EP0);
2050 /* unsetup any leftover default USB transfers */
2051 usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
2053 /* template unsetup, if any */
2054 (usb_temp_unsetup_p) (udev);
2057 * Make sure that our clear-stall messages are not queued
2060 USB_BUS_LOCK(udev->bus);
2061 usb_proc_mwait(&udev->bus->non_giant_callback_proc,
2062 &udev->cs_msg[0], &udev->cs_msg[1]);
2063 USB_BUS_UNLOCK(udev->bus);
2065 sx_destroy(&udev->ctrl_sx);
2066 sx_destroy(&udev->enum_sx);
2067 sx_destroy(&udev->sr_sx);
2069 cv_destroy(&udev->ctrlreq_cv);
2070 cv_destroy(&udev->ref_cv);
2072 mtx_destroy(&udev->device_mtx);
2074 KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("leaked cdev entries"));
2077 /* Uninitialise device */
2078 if (bus->methods->device_uninit != NULL)
2079 (bus->methods->device_uninit) (udev);
2082 free(udev->serial, M_USB);
2083 free(udev->manufacturer, M_USB);
2084 free(udev->product, M_USB);
2088 /*------------------------------------------------------------------------*
2091 * This function is the safe way to get the USB interface structure
2092 * pointer by interface index.
2095 * NULL: Interface not present.
2096 * Else: Pointer to USB interface structure.
2097 *------------------------------------------------------------------------*/
2098 struct usb_interface *
2099 usbd_get_iface(struct usb_device *udev, uint8_t iface_index)
2101 struct usb_interface *iface = udev->ifaces + iface_index;
2103 if (iface_index >= udev->ifaces_max)
2108 /*------------------------------------------------------------------------*
2109 * usbd_find_descriptor
2111 * This function will lookup the first descriptor that matches the
2112 * criteria given by the arguments "type" and "subtype". Descriptors
2113 * will only be searched within the interface having the index
2114 * "iface_index". If the "id" argument points to an USB descriptor,
2115 * it will be skipped before the search is started. This allows
2116 * searching for multiple descriptors using the same criteria. Else
2117 * the search is started after the interface descriptor.
2120 * NULL: End of descriptors
2121 * Else: A descriptor matching the criteria
2122 *------------------------------------------------------------------------*/
2124 usbd_find_descriptor(struct usb_device *udev, void *id, uint8_t iface_index,
2125 uint8_t type, uint8_t type_mask,
2126 uint8_t subtype, uint8_t subtype_mask)
2128 struct usb_descriptor *desc;
2129 struct usb_config_descriptor *cd;
2130 struct usb_interface *iface;
2132 cd = usbd_get_config_descriptor(udev);
2137 iface = usbd_get_iface(udev, iface_index);
2138 if (iface == NULL) {
2141 id = usbd_get_interface_descriptor(iface);
2148 while ((desc = usb_desc_foreach(cd, desc))) {
2150 if (desc->bDescriptorType == UDESC_INTERFACE) {
2153 if (((desc->bDescriptorType & type_mask) == type) &&
2154 ((desc->bDescriptorSubtype & subtype_mask) == subtype)) {
2161 /*------------------------------------------------------------------------*
2164 * This function will dump information from the device descriptor
2165 * belonging to the USB device pointed to by "udev", to the string
2166 * pointed to by "dst_ptr" having a maximum length of "dst_len" bytes
2167 * including the terminating zero.
2168 *------------------------------------------------------------------------*/
2170 usb_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len)
2172 struct usb_device_descriptor *udd = &udev->ddesc;
2176 bcdUSB = UGETW(udd->bcdUSB);
2177 bcdDevice = UGETW(udd->bcdDevice);
2179 if (udd->bDeviceClass != 0xFF) {
2180 snprintf(dst_ptr, dst_len, "%s %s, class %d/%d, rev %x.%02x/"
2182 usb_get_manufacturer(udev),
2183 usb_get_product(udev),
2184 udd->bDeviceClass, udd->bDeviceSubClass,
2185 (bcdUSB >> 8), bcdUSB & 0xFF,
2186 (bcdDevice >> 8), bcdDevice & 0xFF,
2189 snprintf(dst_ptr, dst_len, "%s %s, rev %x.%02x/"
2191 usb_get_manufacturer(udev),
2192 usb_get_product(udev),
2193 (bcdUSB >> 8), bcdUSB & 0xFF,
2194 (bcdDevice >> 8), bcdDevice & 0xFF,
2201 * Descriptions of of known vendors and devices ("products").
2203 struct usb_knowndev {
2207 const char *vendorname;
2208 const char *productname;
2211 #define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */
2213 #include "usbdevs.h"
2214 #include "usbdevs_data.h"
2215 #endif /* USB_VERBOSE */
2218 usbd_set_device_strings(struct usb_device *udev)
2220 struct usb_device_descriptor *udd = &udev->ddesc;
2222 const struct usb_knowndev *kdp;
2227 uint16_t product_id;
2229 temp_ptr = (char *)udev->bus->scratch[0].data;
2230 temp_size = sizeof(udev->bus->scratch[0].data);
2232 vendor_id = UGETW(udd->idVendor);
2233 product_id = UGETW(udd->idProduct);
2235 /* get serial number string */
2236 usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2237 udev->ddesc.iSerialNumber);
2238 udev->serial = strdup(temp_ptr, M_USB);
2240 /* get manufacturer string */
2241 usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2242 udev->ddesc.iManufacturer);
2243 usb_trim_spaces(temp_ptr);
2244 if (temp_ptr[0] != '\0')
2245 udev->manufacturer = strdup(temp_ptr, M_USB);
2247 /* get product string */
2248 usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2249 udev->ddesc.iProduct);
2250 usb_trim_spaces(temp_ptr);
2251 if (temp_ptr[0] != '\0')
2252 udev->product = strdup(temp_ptr, M_USB);
2255 if (udev->manufacturer == NULL || udev->product == NULL) {
2256 for (kdp = usb_knowndevs; kdp->vendorname != NULL; kdp++) {
2257 if (kdp->vendor == vendor_id &&
2258 (kdp->product == product_id ||
2259 (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
2262 if (kdp->vendorname != NULL) {
2263 /* XXX should use pointer to knowndevs string */
2264 if (udev->manufacturer == NULL) {
2265 udev->manufacturer = strdup(kdp->vendorname,
2268 if (udev->product == NULL &&
2269 (kdp->flags & USB_KNOWNDEV_NOPROD) == 0) {
2270 udev->product = strdup(kdp->productname,
2276 /* Provide default strings if none were found */
2277 if (udev->manufacturer == NULL) {
2278 snprintf(temp_ptr, temp_size, "vendor 0x%04x", vendor_id);
2279 udev->manufacturer = strdup(temp_ptr, M_USB);
2281 if (udev->product == NULL) {
2282 snprintf(temp_ptr, temp_size, "product 0x%04x", product_id);
2283 udev->product = strdup(temp_ptr, M_USB);
2292 usbd_get_mode(struct usb_device *udev)
2294 return (udev->flags.usb_mode);
2299 * See: USB_SPEED_XXX
2302 usbd_get_speed(struct usb_device *udev)
2304 return (udev->speed);
2308 usbd_get_isoc_fps(struct usb_device *udev)
2311 switch (udev->speed) {
2313 case USB_SPEED_FULL:
2320 struct usb_device_descriptor *
2321 usbd_get_device_descriptor(struct usb_device *udev)
2324 return (NULL); /* be NULL safe */
2325 return (&udev->ddesc);
2328 struct usb_config_descriptor *
2329 usbd_get_config_descriptor(struct usb_device *udev)
2332 return (NULL); /* be NULL safe */
2333 return (udev->cdesc);
2336 /*------------------------------------------------------------------------*
2337 * usb_test_quirk - test a device for a given quirk
2340 * 0: The USB device does not have the given quirk.
2341 * Else: The USB device has the given quirk.
2342 *------------------------------------------------------------------------*/
2344 usb_test_quirk(const struct usb_attach_arg *uaa, uint16_t quirk)
2348 found = (usb_test_quirk_p) (&uaa->info, quirk);
2352 struct usb_interface_descriptor *
2353 usbd_get_interface_descriptor(struct usb_interface *iface)
2356 return (NULL); /* be NULL safe */
2357 return (iface->idesc);
2361 usbd_get_interface_altindex(struct usb_interface *iface)
2363 return (iface->alt_index);
2367 usbd_get_bus_index(struct usb_device *udev)
2369 return ((uint8_t)device_get_unit(udev->bus->bdev));
2373 usbd_get_device_index(struct usb_device *udev)
2375 return (udev->device_index);
2379 /*------------------------------------------------------------------------*
2382 * This function will generate events for dev.
2383 *------------------------------------------------------------------------*/
2384 #ifndef BURN_BRIDGES
2386 usb_notify_addq_compat(const char *type, struct usb_device *udev)
2390 struct malloc_type *mt;
2391 const size_t buf_size = 512;
2393 /* Convert notify type */
2394 if (strcmp(type, "ATTACH") == 0)
2396 else if (strcmp(type, "DETACH") == 0)
2401 mtx_lock(&malloc_mtx);
2402 mt = malloc_desc2type("bus"); /* XXX M_BUS */
2403 mtx_unlock(&malloc_mtx);
2407 data = malloc(buf_size, mt, M_NOWAIT);
2411 /* String it all together. */
2412 snprintf(data, buf_size,
2421 "devsubclass=0x%02x "
2433 UGETW(udev->ddesc.idVendor),
2434 UGETW(udev->ddesc.idProduct),
2435 udev->ddesc.bDeviceClass,
2436 udev->ddesc.bDeviceSubClass,
2437 usb_get_serial(udev),
2438 UGETW(udev->ddesc.bcdDevice)
2440 , udev->parent_hub != NULL ?
2441 udev->parent_hub->ugen_name :
2442 device_get_nameunit(device_get_parent(udev->bus->bdev))
2446 devctl_queue_data(data);
2451 usb_notify_addq(const char *type, struct usb_device *udev)
2453 struct usb_interface *iface;
2457 #ifndef BURN_BRIDGES
2458 usb_notify_addq_compat(type, udev);
2461 /* announce the device */
2462 sb = sbuf_new_auto();
2471 "devsubclass=0x%02x "
2484 UGETW(udev->ddesc.idVendor),
2485 UGETW(udev->ddesc.idProduct),
2486 udev->ddesc.bDeviceClass,
2487 udev->ddesc.bDeviceSubClass,
2488 usb_get_serial(udev),
2489 UGETW(udev->ddesc.bcdDevice),
2490 (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
2493 , udev->parent_hub != NULL ?
2494 udev->parent_hub->ugen_name :
2495 device_get_nameunit(device_get_parent(udev->bus->bdev))
2499 devctl_notify("USB", "DEVICE", type, sbuf_data(sb));
2502 /* announce each interface */
2503 for (i = 0; i < USB_IFACE_MAX; i++) {
2504 iface = usbd_get_iface(udev, i);
2506 break; /* end of interfaces */
2507 if (iface->idesc == NULL)
2508 continue; /* no interface descriptor */
2510 sb = sbuf_new_auto();
2519 "devsubclass=0x%02x "
2526 "intsubclass=0x%02x "
2527 "intprotocol=0x%02x",
2532 UGETW(udev->ddesc.idVendor),
2533 UGETW(udev->ddesc.idProduct),
2534 udev->ddesc.bDeviceClass,
2535 udev->ddesc.bDeviceSubClass,
2536 usb_get_serial(udev),
2537 UGETW(udev->ddesc.bcdDevice),
2538 (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
2539 iface->idesc->bInterfaceNumber,
2540 iface->idesc->bNumEndpoints,
2541 iface->idesc->bInterfaceClass,
2542 iface->idesc->bInterfaceSubClass,
2543 iface->idesc->bInterfaceProtocol);
2545 devctl_notify("USB", "INTERFACE", type, sbuf_data(sb));
2552 /*------------------------------------------------------------------------*
2553 * usb_fifo_free_wrap
2555 * This function will free the FIFOs.
2557 * Description of "flag" argument: If the USB_UNCFG_FLAG_FREE_EP0 flag
2558 * is set and "iface_index" is set to "USB_IFACE_INDEX_ANY", we free
2559 * all FIFOs. If the USB_UNCFG_FLAG_FREE_EP0 flag is not set and
2560 * "iface_index" is set to "USB_IFACE_INDEX_ANY", we free all non
2561 * control endpoint FIFOs. If "iface_index" is not set to
2562 * "USB_IFACE_INDEX_ANY" the flag has no effect.
2563 *------------------------------------------------------------------------*/
2565 usb_fifo_free_wrap(struct usb_device *udev,
2566 uint8_t iface_index, uint8_t flag)
2572 * Free any USB FIFOs on the given interface:
2574 for (i = 0; i != USB_FIFO_MAX; i++) {
2579 /* Check if the interface index matches */
2580 if (iface_index == f->iface_index) {
2581 if (f->methods != &usb_ugen_methods) {
2583 * Don't free any non-generic FIFOs in
2588 if ((f->dev_ep_index == 0) &&
2589 (f->fs_xfer == NULL)) {
2590 /* no need to free this FIFO */
2593 } else if (iface_index == USB_IFACE_INDEX_ANY) {
2594 if ((f->methods == &usb_ugen_methods) &&
2595 (f->dev_ep_index == 0) &&
2596 (!(flag & USB_UNCFG_FLAG_FREE_EP0)) &&
2597 (f->fs_xfer == NULL)) {
2598 /* no need to free this FIFO */
2602 /* no need to free this FIFO */
2605 /* free this FIFO */
2611 /*------------------------------------------------------------------------*
2612 * usb_peer_can_wakeup
2615 * 0: Peer cannot do resume signalling.
2616 * Else: Peer can do resume signalling.
2617 *------------------------------------------------------------------------*/
2619 usb_peer_can_wakeup(struct usb_device *udev)
2621 const struct usb_config_descriptor *cdp;
2624 if ((cdp != NULL) && (udev->flags.usb_mode == USB_MODE_HOST)) {
2625 return (cdp->bmAttributes & UC_REMOTE_WAKEUP);
2627 return (0); /* not supported */
2631 usb_set_device_state(struct usb_device *udev, enum usb_dev_state state)
2634 KASSERT(state < USB_STATE_MAX, ("invalid udev state"));
2636 DPRINTF("udev %p state %s -> %s\n", udev,
2637 usb_statestr(udev->state), usb_statestr(state));
2638 udev->state = state;
2640 if (udev->bus->methods->device_state_change != NULL)
2641 (udev->bus->methods->device_state_change) (udev);
2645 usb_get_device_state(struct usb_device *udev)
2648 return (USB_STATE_DETACHED);
2649 return (udev->state);
2653 usbd_device_attached(struct usb_device *udev)
2655 return (udev->state > USB_STATE_DETACHED);
2658 /* The following function locks enumerating the given USB device. */
2661 usbd_enum_lock(struct usb_device *udev)
2663 sx_xlock(&udev->enum_sx);
2664 sx_xlock(&udev->sr_sx);
2666 * NEWBUS LOCK NOTE: We should check if any parent SX locks
2667 * are locked before locking Giant. Else the lock can be
2668 * locked multiple times.
2673 /* The following function unlocks enumerating the given USB device. */
2676 usbd_enum_unlock(struct usb_device *udev)
2679 sx_xunlock(&udev->enum_sx);
2680 sx_xunlock(&udev->sr_sx);
2683 /* The following function locks suspend and resume. */
2686 usbd_sr_lock(struct usb_device *udev)
2688 sx_xlock(&udev->sr_sx);
2690 * NEWBUS LOCK NOTE: We should check if any parent SX locks
2691 * are locked before locking Giant. Else the lock can be
2692 * locked multiple times.
2697 /* The following function unlocks suspend and resume. */
2700 usbd_sr_unlock(struct usb_device *udev)
2703 sx_xunlock(&udev->sr_sx);
2707 * The following function checks the enumerating lock for the given
2712 usbd_enum_is_locked(struct usb_device *udev)
2714 return (sx_xlocked(&udev->enum_sx));
2718 * The following function is used to set the per-interface specific
2719 * plug and play information. The string referred to by the pnpinfo
2720 * argument can safely be freed after calling this function. The
2721 * pnpinfo of an interface will be reset at device detach or when
2722 * passing a NULL argument to this function. This function
2723 * returns zero on success, else a USB_ERR_XXX failure code.
2727 usbd_set_pnpinfo(struct usb_device *udev, uint8_t iface_index, const char *pnpinfo)
2729 struct usb_interface *iface;
2731 iface = usbd_get_iface(udev, iface_index);
2733 return (USB_ERR_INVAL);
2735 if (iface->pnpinfo != NULL) {
2736 free(iface->pnpinfo, M_USBDEV);
2737 iface->pnpinfo = NULL;
2740 if (pnpinfo == NULL || pnpinfo[0] == 0)
2741 return (0); /* success */
2743 iface->pnpinfo = strdup(pnpinfo, M_USBDEV);
2744 if (iface->pnpinfo == NULL)
2745 return (USB_ERR_NOMEM);
2747 return (0); /* success */