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/linker_set.h>
36 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/condvar.h>
40 #include <sys/sysctl.h>
42 #include <sys/unistd.h>
43 #include <sys/callout.h>
44 #include <sys/malloc.h>
47 #include <sys/fcntl.h>
49 #include <dev/usb/usb.h>
50 #include <dev/usb/usbdi.h>
51 #include <dev/usb/usbdi_util.h>
52 #include <dev/usb/usb_ioctl.h>
60 #define USB_DEBUG_VAR usb_debug
62 #include <dev/usb/usb_core.h>
63 #include <dev/usb/usb_debug.h>
64 #include <dev/usb/usb_process.h>
65 #include <dev/usb/usb_device.h>
66 #include <dev/usb/usb_busdma.h>
67 #include <dev/usb/usb_transfer.h>
68 #include <dev/usb/usb_request.h>
69 #include <dev/usb/usb_dynamic.h>
70 #include <dev/usb/usb_hub.h>
71 #include <dev/usb/usb_util.h>
72 #include <dev/usb/usb_msctest.h>
74 #include <dev/usb/usb_dev.h>
75 #include <dev/usb/usb_generic.h>
78 #include <dev/usb/quirk/usb_quirk.h>
80 #include <dev/usb/usb_controller.h>
81 #include <dev/usb/usb_bus.h>
83 /* function prototypes */
85 static void usb_init_endpoint(struct usb_device *, uint8_t,
86 struct usb_endpoint_descriptor *,
87 struct usb_endpoint_ss_comp_descriptor *,
88 struct usb_endpoint *);
89 static void usb_unconfigure(struct usb_device *, uint8_t);
90 static void usb_detach_device_sub(struct usb_device *, device_t *,
92 static uint8_t usb_probe_and_attach_sub(struct usb_device *,
93 struct usb_attach_arg *);
94 static void usb_init_attach_arg(struct usb_device *,
95 struct usb_attach_arg *);
96 static void usb_suspend_resume_sub(struct usb_device *, device_t,
98 static void usbd_clear_stall_proc(struct usb_proc_msg *_pm);
99 static usb_error_t usb_config_parse(struct usb_device *, uint8_t, uint8_t);
100 static void usbd_set_device_strings(struct usb_device *);
102 static void usb_notify_addq(const char *type, struct usb_device *);
103 static void usb_fifo_free_wrap(struct usb_device *, uint8_t, uint8_t);
104 static struct cdev *usb_make_dev(struct usb_device *, int, int);
105 static void usb_cdev_create(struct usb_device *);
106 static void usb_cdev_free(struct usb_device *);
107 static void usb_cdev_cleanup(void *);
110 /* This variable is global to allow easy access to it: */
112 int usb_template = 0;
114 TUNABLE_INT("hw.usb.usb_template", &usb_template);
115 SYSCTL_INT(_hw_usb, OID_AUTO, template, CTLFLAG_RW,
116 &usb_template, 0, "Selected USB device side template");
118 /* English is default language */
120 static int usb_lang_id = 0x0009;
121 static int usb_lang_mask = 0x00FF;
123 TUNABLE_INT("hw.usb.usb_lang_id", &usb_lang_id);
124 SYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_id, CTLFLAG_RW,
125 &usb_lang_id, 0, "Preferred USB language ID");
127 TUNABLE_INT("hw.usb.usb_lang_mask", &usb_lang_mask);
128 SYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_mask, CTLFLAG_RW,
129 &usb_lang_mask, 0, "Preferred USB language mask");
131 static const char* statestr[USB_STATE_MAX] = {
132 [USB_STATE_DETACHED] = "DETACHED",
133 [USB_STATE_ATTACHED] = "ATTACHED",
134 [USB_STATE_POWERED] = "POWERED",
135 [USB_STATE_ADDRESSED] = "ADDRESSED",
136 [USB_STATE_CONFIGURED] = "CONFIGURED",
140 usb_statestr(enum usb_dev_state state)
142 return ((state < USB_STATE_MAX) ? statestr[state] : "UNKNOWN");
146 usb_get_manufacturer(struct usb_device *udev)
148 return (udev->manufacturer ? udev->manufacturer : "Unknown");
152 usb_get_product(struct usb_device *udev)
154 return (udev->product ? udev->product : "");
158 usb_get_serial(struct usb_device *udev)
160 return (udev->serial ? udev->serial : "");
163 /*------------------------------------------------------------------------*
164 * usbd_get_ep_by_addr
166 * This function searches for an USB ep by endpoint address and
172 *------------------------------------------------------------------------*/
173 struct usb_endpoint *
174 usbd_get_ep_by_addr(struct usb_device *udev, uint8_t ea_val)
176 struct usb_endpoint *ep = udev->endpoints;
177 struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max;
179 EA_MASK = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR),
183 * According to the USB specification not all bits are used
184 * for the endpoint address. Keep defined bits only:
189 * Iterate accross all the USB endpoints searching for a match
190 * based on the endpoint address:
192 for (; ep != ep_end; ep++) {
194 if (ep->edesc == NULL) {
197 /* do the mask and check the value */
198 if ((ep->edesc->bEndpointAddress & EA_MASK) == ea_val) {
204 * The default endpoint is always present and is checked separately:
206 if ((udev->ctrl_ep.edesc) &&
207 ((udev->ctrl_ep.edesc->bEndpointAddress & EA_MASK) == ea_val)) {
217 /*------------------------------------------------------------------------*
220 * This function searches for an USB endpoint based on the information
221 * given by the passed "struct usb_config" pointer.
225 * Else: Pointer to "struct usb_endpoint".
226 *------------------------------------------------------------------------*/
227 struct usb_endpoint *
228 usbd_get_endpoint(struct usb_device *udev, uint8_t iface_index,
229 const struct usb_config *setup)
231 struct usb_endpoint *ep = udev->endpoints;
232 struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max;
233 uint8_t index = setup->ep_index;
239 DPRINTFN(10, "udev=%p iface_index=%d address=0x%x "
240 "type=0x%x dir=0x%x index=%d\n",
241 udev, iface_index, setup->endpoint,
242 setup->type, setup->direction, setup->ep_index);
246 if (setup->usb_mode != USB_MODE_DUAL &&
247 udev->flags.usb_mode != setup->usb_mode) {
248 /* wrong mode - no endpoint */
252 /* setup expected endpoint direction mask and value */
254 if (setup->direction == UE_DIR_RX) {
255 ea_mask = (UE_DIR_IN | UE_DIR_OUT);
256 ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ?
257 UE_DIR_OUT : UE_DIR_IN;
258 } else if (setup->direction == UE_DIR_TX) {
259 ea_mask = (UE_DIR_IN | UE_DIR_OUT);
260 ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ?
261 UE_DIR_IN : UE_DIR_OUT;
262 } else if (setup->direction == UE_DIR_ANY) {
263 /* match any endpoint direction */
267 /* match the given endpoint direction */
268 ea_mask = (UE_DIR_IN | UE_DIR_OUT);
269 ea_val = (setup->direction & (UE_DIR_IN | UE_DIR_OUT));
272 /* setup expected endpoint address */
274 if (setup->endpoint == UE_ADDR_ANY) {
275 /* match any endpoint address */
277 /* match the given endpoint address */
279 ea_val |= (setup->endpoint & UE_ADDR);
282 /* setup expected endpoint type */
284 if (setup->type == UE_BULK_INTR) {
285 /* this will match BULK and INTERRUPT endpoints */
288 } else if (setup->type == UE_TYPE_ANY) {
289 /* match any endpoint type */
293 /* match the given endpoint type */
294 type_mask = UE_XFERTYPE;
295 type_val = (setup->type & UE_XFERTYPE);
299 * Iterate accross all the USB endpoints searching for a match
300 * based on the endpoint address. Note that we are searching
301 * the endpoints from the beginning of the "udev->endpoints" array.
303 for (; ep != ep_end; ep++) {
305 if ((ep->edesc == NULL) ||
306 (ep->iface_index != iface_index)) {
309 /* do the masks and check the values */
311 if (((ep->edesc->bEndpointAddress & ea_mask) == ea_val) &&
312 ((ep->edesc->bmAttributes & type_mask) == type_val)) {
320 * Match against default endpoint last, so that "any endpoint", "any
321 * address" and "any direction" returns the first endpoint of the
322 * interface. "iface_index" and "direction" is ignored:
324 if ((udev->ctrl_ep.edesc) &&
325 ((udev->ctrl_ep.edesc->bEndpointAddress & ea_mask) == ea_val) &&
326 ((udev->ctrl_ep.edesc->bmAttributes & type_mask) == type_val) &&
337 /*------------------------------------------------------------------------*
338 * usbd_interface_count
340 * This function stores the number of USB interfaces excluding
341 * alternate settings, which the USB config descriptor reports into
342 * the unsigned 8-bit integer pointed to by "count".
347 *------------------------------------------------------------------------*/
349 usbd_interface_count(struct usb_device *udev, uint8_t *count)
351 if (udev->cdesc == NULL) {
353 return (USB_ERR_NOT_CONFIGURED);
355 *count = udev->ifaces_max;
356 return (USB_ERR_NORMAL_COMPLETION);
360 /*------------------------------------------------------------------------*
363 * This function will initialise the USB endpoint structure pointed to by
364 * the "endpoint" argument. The structure pointed to by "endpoint" must be
365 * zeroed before calling this function.
366 *------------------------------------------------------------------------*/
368 usb_init_endpoint(struct usb_device *udev, uint8_t iface_index,
369 struct usb_endpoint_descriptor *edesc,
370 struct usb_endpoint_ss_comp_descriptor *ecomp,
371 struct usb_endpoint *ep)
373 struct usb_bus_methods *methods;
375 methods = udev->bus->methods;
377 (methods->endpoint_init) (udev, edesc, ep);
379 /* initialise USB endpoint structure */
382 ep->iface_index = iface_index;
383 TAILQ_INIT(&ep->endpoint_q.head);
384 ep->endpoint_q.command = &usbd_pipe_start;
386 /* the pipe is not supported by the hardware */
387 if (ep->methods == NULL)
390 /* clear stall, if any */
391 if (methods->clear_stall != NULL) {
392 USB_BUS_LOCK(udev->bus);
393 (methods->clear_stall) (udev, ep);
394 USB_BUS_UNLOCK(udev->bus);
398 /*-----------------------------------------------------------------------*
399 * usb_endpoint_foreach
401 * This function will iterate all the USB endpoints except the control
402 * endpoint. This function is NULL safe.
405 * NULL: End of USB endpoints
406 * Else: Pointer to next USB endpoint
407 *------------------------------------------------------------------------*/
408 struct usb_endpoint *
409 usb_endpoint_foreach(struct usb_device *udev, struct usb_endpoint *ep)
411 struct usb_endpoint *ep_end;
417 ep_end = udev->endpoints + udev->endpoints_max;
419 /* get next endpoint */
421 ep = udev->endpoints;
425 /* find next allocated ep */
426 while (ep != ep_end) {
427 if (ep->edesc != NULL)
434 /*------------------------------------------------------------------------*
437 * This function will free all USB interfaces and USB endpoints belonging
440 * Flag values, see "USB_UNCFG_FLAG_XXX".
441 *------------------------------------------------------------------------*/
443 usb_unconfigure(struct usb_device *udev, uint8_t flag)
447 /* automatic locking */
448 if (usbd_enum_is_locked(udev)) {
452 usbd_enum_lock(udev);
455 /* detach all interface drivers */
456 usb_detach_device(udev, USB_IFACE_INDEX_ANY, flag);
459 /* free all FIFOs except control endpoint FIFOs */
460 usb_fifo_free_wrap(udev, USB_IFACE_INDEX_ANY, flag);
463 * Free all cdev's, if any.
468 #if USB_HAVE_COMPAT_LINUX
469 /* free Linux compat device, if any */
470 if (udev->linux_endpoint_start) {
471 usb_linux_free_device(udev);
472 udev->linux_endpoint_start = NULL;
476 usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_FREE);
478 /* free "cdesc" after "ifaces" and "endpoints", if any */
479 if (udev->cdesc != NULL) {
480 if (udev->flags.usb_mode != USB_MODE_DEVICE)
481 free(udev->cdesc, M_USB);
484 /* set unconfigured state */
485 udev->curr_config_no = USB_UNCONFIG_NO;
486 udev->curr_config_index = USB_UNCONFIG_INDEX;
489 usbd_enum_unlock(udev);
492 /*------------------------------------------------------------------------*
493 * usbd_set_config_index
495 * This function selects configuration by index, independent of the
496 * actual configuration number. This function should not be used by
502 *------------------------------------------------------------------------*/
504 usbd_set_config_index(struct usb_device *udev, uint8_t index)
506 struct usb_status ds;
507 struct usb_config_descriptor *cdp;
514 DPRINTFN(6, "udev=%p index=%d\n", udev, index);
516 /* automatic locking */
517 if (usbd_enum_is_locked(udev)) {
521 usbd_enum_lock(udev);
524 usb_unconfigure(udev, 0);
526 if (index == USB_UNCONFIG_INDEX) {
528 * Leave unallocated when unconfiguring the
529 * device. "usb_unconfigure()" will also reset
530 * the current config number and index.
532 err = usbd_req_set_config(udev, NULL, USB_UNCONFIG_NO);
533 if (udev->state == USB_STATE_CONFIGURED)
534 usb_set_device_state(udev, USB_STATE_ADDRESSED);
537 /* get the full config descriptor */
538 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
539 /* save some memory */
540 err = usbd_req_get_descriptor_ptr(udev, &cdp,
541 (UDESC_CONFIG << 8) | index);
544 err = usbd_req_get_config_desc_full(udev,
545 NULL, &cdp, M_USB, index);
550 /* set the new config descriptor */
554 /* Figure out if the device is self or bus powered. */
556 if ((!udev->flags.uq_bus_powered) &&
557 (cdp->bmAttributes & UC_SELF_POWERED) &&
558 (udev->flags.usb_mode == USB_MODE_HOST)) {
559 /* May be self powered. */
560 if (cdp->bmAttributes & UC_BUS_POWERED) {
561 /* Must ask device. */
562 err = usbd_req_get_device_status(udev, NULL, &ds);
564 DPRINTFN(0, "could not read "
565 "device status: %s\n",
567 } else if (UGETW(ds.wStatus) & UDS_SELF_POWERED) {
570 DPRINTF("status=0x%04x \n",
575 DPRINTF("udev=%p cdesc=%p (addr %d) cno=%d attr=0x%02x, "
576 "selfpowered=%d, power=%d\n",
578 udev->address, cdp->bConfigurationValue, cdp->bmAttributes,
579 selfpowered, cdp->bMaxPower * 2);
581 /* Check if we have enough power. */
582 power = cdp->bMaxPower * 2;
584 if (udev->parent_hub) {
585 max_power = udev->parent_hub->hub->portpower;
587 max_power = USB_MAX_POWER;
590 if (power > max_power) {
591 DPRINTFN(0, "power exceeded %d > %d\n", power, max_power);
592 err = USB_ERR_NO_POWER;
595 /* Only update "self_powered" in USB Host Mode */
596 if (udev->flags.usb_mode == USB_MODE_HOST) {
597 udev->flags.self_powered = selfpowered;
600 udev->curr_config_no = cdp->bConfigurationValue;
601 udev->curr_config_index = index;
602 usb_set_device_state(udev, USB_STATE_CONFIGURED);
604 /* Set the actual configuration value. */
605 err = usbd_req_set_config(udev, NULL, cdp->bConfigurationValue);
610 err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_ALLOC);
615 err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_INIT);
621 /* create device nodes for each endpoint */
622 usb_cdev_create(udev);
626 DPRINTF("error=%s\n", usbd_errstr(err));
628 usb_unconfigure(udev, 0);
631 usbd_enum_unlock(udev);
635 /*------------------------------------------------------------------------*
638 * This function will allocate and free USB interfaces and USB endpoints,
639 * parse the USB configuration structure and initialise the USB endpoints
640 * and interfaces. If "iface_index" is not equal to
641 * "USB_IFACE_INDEX_ANY" then the "cmd" parameter is the
642 * alternate_setting to be selected for the given interface. Else the
643 * "cmd" parameter is defined by "USB_CFG_XXX". "iface_index" can be
644 * "USB_IFACE_INDEX_ANY" or a valid USB interface index. This function
645 * is typically called when setting the configuration or when setting
646 * an alternate interface.
651 *------------------------------------------------------------------------*/
653 usb_config_parse(struct usb_device *udev, uint8_t iface_index, uint8_t cmd)
655 struct usb_idesc_parse_state ips;
656 struct usb_interface_descriptor *id;
657 struct usb_endpoint_descriptor *ed;
658 struct usb_interface *iface;
659 struct usb_endpoint *ep;
667 if (iface_index != USB_IFACE_INDEX_ANY) {
668 /* parameter overload */
678 DPRINTFN(5, "iface_index=%d cmd=%d\n",
681 if (cmd == USB_CFG_FREE)
684 if (cmd == USB_CFG_INIT) {
685 sx_assert(&udev->enum_sx, SA_LOCKED);
687 /* check for in-use endpoints */
689 ep = udev->endpoints;
690 ep_max = udev->endpoints_max;
692 /* look for matching endpoints */
693 if ((iface_index == USB_IFACE_INDEX_ANY) ||
694 (iface_index == ep->iface_index)) {
695 if (ep->refcount_alloc != 0) {
697 * This typically indicates a
698 * more serious error.
700 err = USB_ERR_IN_USE;
703 memset(ep, 0, sizeof(*ep));
704 /* make sure we don't zero the endpoint again */
705 ep->iface_index = USB_IFACE_INDEX_ANY;
715 memset(&ips, 0, sizeof(ips));
720 while ((id = usb_idesc_foreach(udev->cdesc, &ips))) {
722 /* check for interface overflow */
723 if (ips.iface_index == USB_IFACE_MAX)
726 iface = udev->ifaces + ips.iface_index;
728 /* check for specific interface match */
730 if (cmd == USB_CFG_INIT) {
731 if ((iface_index != USB_IFACE_INDEX_ANY) &&
732 (iface_index != ips.iface_index)) {
733 /* wrong interface */
735 } else if (alt_index != ips.iface_index_alt) {
736 /* wrong alternate setting */
739 /* initialise interface */
745 /* check for new interface */
746 if (ips.iface_index_alt == 0) {
747 /* update current number of endpoints */
752 /* setup the USB interface structure */
754 /* default setting */
755 iface->parent_iface_index = USB_IFACE_INDEX_ANY;
756 /* set alternate index */
757 iface->alt_index = alt_index;
760 DPRINTFN(5, "found idesc nendpt=%d\n", id->bNumEndpoints);
762 ed = (struct usb_endpoint_descriptor *)id;
766 /* iterate all the endpoint descriptors */
767 while ((ed = usb_edesc_foreach(udev->cdesc, ed))) {
769 if (temp == USB_EP_MAX)
772 ep = udev->endpoints + temp;
777 ecomp = usb_ed_comp_foreach(udev->cdesc, (void *)ed);
779 DPRINTFN(5, "Found endpoint companion descriptor\n");
781 usb_init_endpoint(udev,
782 ips.iface_index, ed, ecomp, ep);
787 /* find maximum number of endpoints */
792 id = (struct usb_interface_descriptor *)ed;
796 /* NOTE: It is valid to have no interfaces and no endpoints! */
798 if (cmd == USB_CFG_ALLOC) {
799 udev->ifaces_max = ips.iface_index;
801 if (udev->ifaces_max != 0) {
802 udev->ifaces = malloc(sizeof(*iface) * udev->ifaces_max,
803 M_USB, M_WAITOK | M_ZERO);
804 if (udev->ifaces == NULL) {
810 udev->endpoints = malloc(sizeof(*ep) * ep_max,
811 M_USB, M_WAITOK | M_ZERO);
812 if (udev->endpoints == NULL) {
817 udev->endpoints = NULL;
819 USB_BUS_LOCK(udev->bus);
820 udev->endpoints_max = ep_max;
821 /* reset any ongoing clear-stall */
822 udev->ep_curr = NULL;
823 USB_BUS_UNLOCK(udev->bus);
828 if (cmd == USB_CFG_ALLOC) {
830 USB_BUS_LOCK(udev->bus);
831 udev->endpoints_max = 0;
832 /* reset any ongoing clear-stall */
833 udev->ep_curr = NULL;
834 USB_BUS_UNLOCK(udev->bus);
837 if (udev->ifaces != NULL)
838 free(udev->ifaces, M_USB);
839 if (udev->endpoints != NULL)
840 free(udev->endpoints, M_USB);
843 udev->endpoints = NULL;
844 udev->ifaces_max = 0;
850 /*------------------------------------------------------------------------*
851 * usbd_set_alt_interface_index
853 * This function will select an alternate interface index for the
854 * given interface index. The interface should not be in use when this
855 * function is called. That means there should not be any open USB
856 * transfers. Else an error is returned. If the alternate setting is
857 * already set this function will simply return success. This function
858 * is called in Host mode and Device mode!
863 *------------------------------------------------------------------------*/
865 usbd_set_alt_interface_index(struct usb_device *udev,
866 uint8_t iface_index, uint8_t alt_index)
868 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
872 /* automatic locking */
873 if (usbd_enum_is_locked(udev)) {
877 usbd_enum_lock(udev);
883 if (iface->alt_index == alt_index) {
885 * Optimise away duplicate setting of
886 * alternate setting in USB Host Mode!
893 * Free all generic FIFOs for this interface, except control
896 usb_fifo_free_wrap(udev, iface_index, 0);
899 err = usb_config_parse(udev, iface_index, alt_index);
903 if (iface->alt_index != alt_index) {
904 /* the alternate setting does not exist */
909 err = usbd_req_set_alt_interface_no(udev, NULL, iface_index,
910 iface->idesc->bAlternateSetting);
914 usbd_enum_unlock(udev);
919 /*------------------------------------------------------------------------*
920 * usbd_set_endpoint_stall
922 * This function is used to make a BULK or INTERRUPT endpoint send
923 * STALL tokens in USB device mode.
928 *------------------------------------------------------------------------*/
930 usbd_set_endpoint_stall(struct usb_device *udev, struct usb_endpoint *ep,
933 struct usb_xfer *xfer;
939 DPRINTF("Cannot find endpoint\n");
941 * Pretend that the clear or set stall request is
942 * successful else some USB host stacks can do
943 * strange things, especially when a control endpoint
948 et = (ep->edesc->bmAttributes & UE_XFERTYPE);
950 if ((et != UE_BULK) &&
951 (et != UE_INTERRUPT)) {
953 * Should not stall control
954 * nor isochronous endpoints.
956 DPRINTF("Invalid endpoint\n");
959 USB_BUS_LOCK(udev->bus);
961 /* store current stall state */
962 was_stalled = ep->is_stalled;
964 /* check for no change */
965 if (was_stalled && do_stall) {
966 /* if the endpoint is already stalled do nothing */
967 USB_BUS_UNLOCK(udev->bus);
968 DPRINTF("No change\n");
971 /* set stalled state */
974 if (do_stall || (!was_stalled)) {
976 /* lookup the current USB transfer, if any */
977 xfer = ep->endpoint_q.curr;
983 * If "xfer" is non-NULL the "set_stall" method will
984 * complete the USB transfer like in case of a timeout
985 * setting the error code "USB_ERR_STALLED".
987 (udev->bus->methods->set_stall) (udev, xfer, ep, &do_stall);
990 ep->toggle_next = 0; /* reset data toggle */
991 ep->is_stalled = 0; /* clear stalled state */
993 (udev->bus->methods->clear_stall) (udev, ep);
995 /* start up the current or next transfer, if any */
996 usb_command_wrapper(&ep->endpoint_q, ep->endpoint_q.curr);
998 USB_BUS_UNLOCK(udev->bus);
1002 /*------------------------------------------------------------------------*
1003 * usb_reset_iface_endpoints - used in USB device side mode
1004 *------------------------------------------------------------------------*/
1006 usb_reset_iface_endpoints(struct usb_device *udev, uint8_t iface_index)
1008 struct usb_endpoint *ep;
1009 struct usb_endpoint *ep_end;
1011 ep = udev->endpoints;
1012 ep_end = udev->endpoints + udev->endpoints_max;
1014 for (; ep != ep_end; ep++) {
1016 if ((ep->edesc == NULL) ||
1017 (ep->iface_index != iface_index)) {
1020 /* simulate a clear stall from the peer */
1021 usbd_set_endpoint_stall(udev, ep, 0);
1026 /*------------------------------------------------------------------------*
1027 * usb_detach_device_sub
1029 * This function will try to detach an USB device. If it fails a panic
1032 * Flag values, see "USB_UNCFG_FLAG_XXX".
1033 *------------------------------------------------------------------------*/
1035 usb_detach_device_sub(struct usb_device *udev, device_t *ppdev,
1044 * NOTE: It is important to clear "*ppdev" before deleting
1045 * the child due to some device methods being called late
1046 * during the delete process !
1050 device_printf(dev, "at %s, port %d, addr %d "
1052 device_get_nameunit(udev->parent_dev),
1053 udev->port_no, udev->address);
1055 if (device_is_attached(dev)) {
1056 if (udev->flags.peer_suspended) {
1057 err = DEVICE_RESUME(dev);
1059 device_printf(dev, "Resume failed\n");
1062 if (device_detach(dev)) {
1066 if (device_delete_child(udev->parent_dev, dev)) {
1073 /* Detach is not allowed to fail in the USB world */
1074 panic("A USB driver would not detach\n");
1077 /*------------------------------------------------------------------------*
1080 * The following function will detach the matching interfaces.
1081 * This function is NULL safe.
1083 * Flag values, see "USB_UNCFG_FLAG_XXX".
1084 *------------------------------------------------------------------------*/
1086 usb_detach_device(struct usb_device *udev, uint8_t iface_index,
1089 struct usb_interface *iface;
1096 DPRINTFN(4, "udev=%p\n", udev);
1098 sx_assert(&udev->enum_sx, SA_LOCKED);
1101 * First detach the child to give the child's detach routine a
1102 * chance to detach the sub-devices in the correct order.
1103 * Then delete the child using "device_delete_child()" which
1104 * will detach all sub-devices from the bottom and upwards!
1106 if (iface_index != USB_IFACE_INDEX_ANY) {
1108 iface_index = i + 1;
1111 iface_index = USB_IFACE_MAX;
1116 for (; i != iface_index; i++) {
1118 iface = usbd_get_iface(udev, i);
1119 if (iface == NULL) {
1120 /* looks like the end of the USB interfaces */
1123 usb_detach_device_sub(udev, &iface->subdev, flag);
1127 /*------------------------------------------------------------------------*
1128 * usb_probe_and_attach_sub
1133 *------------------------------------------------------------------------*/
1135 usb_probe_and_attach_sub(struct usb_device *udev,
1136 struct usb_attach_arg *uaa)
1138 struct usb_interface *iface;
1143 if (iface->parent_iface_index != USB_IFACE_INDEX_ANY) {
1144 /* leave interface alone */
1147 dev = iface->subdev;
1150 /* clean up after module unload */
1152 if (device_is_attached(dev)) {
1153 /* already a device there */
1156 /* clear "iface->subdev" as early as possible */
1158 iface->subdev = NULL;
1160 if (device_delete_child(udev->parent_dev, dev)) {
1163 * Panic here, else one can get a double call
1164 * to device_detach(). USB devices should
1165 * never fail on detach!
1167 panic("device_delete_child() failed\n");
1170 if (uaa->temp_dev == NULL) {
1172 /* create a new child */
1173 uaa->temp_dev = device_add_child(udev->parent_dev, NULL, -1);
1174 if (uaa->temp_dev == NULL) {
1175 device_printf(udev->parent_dev,
1176 "Device creation failed\n");
1177 return (1); /* failure */
1179 device_set_ivars(uaa->temp_dev, uaa);
1180 device_quiet(uaa->temp_dev);
1183 * Set "subdev" before probe and attach so that "devd" gets
1184 * the information it needs.
1186 iface->subdev = uaa->temp_dev;
1188 if (device_probe_and_attach(iface->subdev) == 0) {
1190 * The USB attach arguments are only available during probe
1193 uaa->temp_dev = NULL;
1194 device_set_ivars(iface->subdev, NULL);
1196 if (udev->flags.peer_suspended) {
1197 err = DEVICE_SUSPEND(iface->subdev);
1199 device_printf(iface->subdev, "Suspend failed\n");
1201 return (0); /* success */
1203 /* No USB driver found */
1204 iface->subdev = NULL;
1206 return (1); /* failure */
1209 /*------------------------------------------------------------------------*
1210 * usbd_set_parent_iface
1212 * Using this function will lock the alternate interface setting on an
1213 * interface. It is typically used for multi interface drivers. In USB
1214 * device side mode it is assumed that the alternate interfaces all
1215 * have the same endpoint descriptors. The default parent index value
1216 * is "USB_IFACE_INDEX_ANY". Then the alternate setting value is not
1218 *------------------------------------------------------------------------*/
1220 usbd_set_parent_iface(struct usb_device *udev, uint8_t iface_index,
1221 uint8_t parent_index)
1223 struct usb_interface *iface;
1225 iface = usbd_get_iface(udev, iface_index);
1227 iface->parent_iface_index = parent_index;
1232 usb_init_attach_arg(struct usb_device *udev,
1233 struct usb_attach_arg *uaa)
1235 bzero(uaa, sizeof(*uaa));
1238 uaa->usb_mode = udev->flags.usb_mode;
1239 uaa->port = udev->port_no;
1240 uaa->dev_state = UAA_DEV_READY;
1242 uaa->info.idVendor = UGETW(udev->ddesc.idVendor);
1243 uaa->info.idProduct = UGETW(udev->ddesc.idProduct);
1244 uaa->info.bcdDevice = UGETW(udev->ddesc.bcdDevice);
1245 uaa->info.bDeviceClass = udev->ddesc.bDeviceClass;
1246 uaa->info.bDeviceSubClass = udev->ddesc.bDeviceSubClass;
1247 uaa->info.bDeviceProtocol = udev->ddesc.bDeviceProtocol;
1248 uaa->info.bConfigIndex = udev->curr_config_index;
1249 uaa->info.bConfigNum = udev->curr_config_no;
1252 /*------------------------------------------------------------------------*
1253 * usb_probe_and_attach
1255 * This function is called from "uhub_explore_sub()",
1256 * "usb_handle_set_config()" and "usb_handle_request()".
1260 * Else: A control transfer failed
1261 *------------------------------------------------------------------------*/
1263 usb_probe_and_attach(struct usb_device *udev, uint8_t iface_index)
1265 struct usb_attach_arg uaa;
1266 struct usb_interface *iface;
1272 DPRINTF("udev == NULL\n");
1273 return (USB_ERR_INVAL);
1275 /* automatic locking */
1276 if (usbd_enum_is_locked(udev)) {
1280 usbd_enum_lock(udev);
1283 if (udev->curr_config_index == USB_UNCONFIG_INDEX) {
1284 /* do nothing - no configuration has been set */
1287 /* setup USB attach arguments */
1289 usb_init_attach_arg(udev, &uaa);
1291 /* Check if only one interface should be probed: */
1292 if (iface_index != USB_IFACE_INDEX_ANY) {
1300 /* Do the probe and attach */
1301 for (; i != j; i++) {
1303 iface = usbd_get_iface(udev, i);
1304 if (iface == NULL) {
1306 * Looks like the end of the USB
1309 DPRINTFN(2, "end of interfaces "
1313 if (iface->idesc == NULL) {
1314 /* no interface descriptor */
1319 uaa.info.bInterfaceClass =
1320 iface->idesc->bInterfaceClass;
1321 uaa.info.bInterfaceSubClass =
1322 iface->idesc->bInterfaceSubClass;
1323 uaa.info.bInterfaceProtocol =
1324 iface->idesc->bInterfaceProtocol;
1325 uaa.info.bIfaceIndex = i;
1326 uaa.info.bIfaceNum =
1327 iface->idesc->bInterfaceNumber;
1328 uaa.use_generic = 0;
1329 uaa.driver_info = 0; /* reset driver_info */
1331 DPRINTFN(2, "iclass=%u/%u/%u iindex=%u/%u\n",
1332 uaa.info.bInterfaceClass,
1333 uaa.info.bInterfaceSubClass,
1334 uaa.info.bInterfaceProtocol,
1335 uaa.info.bIfaceIndex,
1336 uaa.info.bIfaceNum);
1338 /* try specific interface drivers first */
1340 if (usb_probe_and_attach_sub(udev, &uaa)) {
1343 /* try generic interface drivers last */
1345 uaa.use_generic = 1;
1346 uaa.driver_info = 0; /* reset driver_info */
1348 if (usb_probe_and_attach_sub(udev, &uaa)) {
1354 /* remove the last created child; it is unused */
1356 if (device_delete_child(udev->parent_dev, uaa.temp_dev)) {
1357 DPRINTFN(0, "device delete child failed\n");
1362 usbd_enum_unlock(udev);
1367 /*------------------------------------------------------------------------*
1368 * usb_suspend_resume_sub
1370 * This function is called when the suspend or resume methods should
1371 * be executed on an USB device.
1372 *------------------------------------------------------------------------*/
1374 usb_suspend_resume_sub(struct usb_device *udev, device_t dev, uint8_t do_suspend)
1381 if (!device_is_attached(dev)) {
1385 err = DEVICE_SUSPEND(dev);
1387 err = DEVICE_RESUME(dev);
1390 device_printf(dev, "%s failed\n",
1391 do_suspend ? "Suspend" : "Resume");
1395 /*------------------------------------------------------------------------*
1396 * usb_suspend_resume
1398 * The following function will suspend or resume the USB device.
1403 *------------------------------------------------------------------------*/
1405 usb_suspend_resume(struct usb_device *udev, uint8_t do_suspend)
1407 struct usb_interface *iface;
1414 DPRINTFN(4, "udev=%p do_suspend=%d\n", udev, do_suspend);
1416 sx_assert(&udev->sr_sx, SA_LOCKED);
1418 USB_BUS_LOCK(udev->bus);
1419 /* filter the suspend events */
1420 if (udev->flags.peer_suspended == do_suspend) {
1421 USB_BUS_UNLOCK(udev->bus);
1425 udev->flags.peer_suspended = do_suspend;
1426 USB_BUS_UNLOCK(udev->bus);
1428 /* do the suspend or resume */
1430 for (i = 0; i != USB_IFACE_MAX; i++) {
1432 iface = usbd_get_iface(udev, i);
1433 if (iface == NULL) {
1434 /* looks like the end of the USB interfaces */
1437 usb_suspend_resume_sub(udev, iface->subdev, do_suspend);
1442 /*------------------------------------------------------------------------*
1443 * usbd_clear_stall_proc
1445 * This function performs generic USB clear stall operations.
1446 *------------------------------------------------------------------------*/
1448 usbd_clear_stall_proc(struct usb_proc_msg *_pm)
1450 struct usb_clear_stall_msg *pm = (void *)_pm;
1451 struct usb_device *udev = pm->udev;
1454 USB_BUS_UNLOCK(udev->bus);
1455 mtx_lock(&udev->device_mtx);
1457 /* Start clear stall callback */
1458 usbd_transfer_start(udev->ctrl_xfer[1]);
1461 mtx_unlock(&udev->device_mtx);
1462 USB_BUS_LOCK(udev->bus);
1465 /*------------------------------------------------------------------------*
1468 * This function allocates a new USB device. This function is called
1469 * when a new device has been put in the powered state, but not yet in
1470 * the addressed state. Get initial descriptor, set the address, get
1471 * full descriptor and get strings.
1476 *------------------------------------------------------------------------*/
1478 usb_alloc_device(device_t parent_dev, struct usb_bus *bus,
1479 struct usb_device *parent_hub, uint8_t depth, uint8_t port_index,
1480 uint8_t port_no, enum usb_dev_speed speed, enum usb_hc_mode mode)
1482 struct usb_attach_arg uaa;
1483 struct usb_device *udev;
1484 struct usb_device *adev;
1485 struct usb_device *hub;
1486 uint8_t *scratch_ptr;
1487 size_t scratch_size;
1489 uint8_t device_index;
1490 uint8_t config_index;
1491 uint8_t config_quirk;
1492 uint8_t set_config_failed;
1494 DPRINTF("parent_dev=%p, bus=%p, parent_hub=%p, depth=%u, "
1495 "port_index=%u, port_no=%u, speed=%u, usb_mode=%u\n",
1496 parent_dev, bus, parent_hub, depth, port_index, port_no,
1500 * Find an unused device index. In USB Host mode this is the
1501 * same as the device address.
1503 * Device index zero is not used and device index 1 should
1504 * always be the root hub.
1506 for (device_index = USB_ROOT_HUB_ADDR;
1507 (device_index != bus->devices_max) &&
1508 (bus->devices[device_index] != NULL);
1509 device_index++) /* nop */;
1511 if (device_index == bus->devices_max) {
1512 device_printf(bus->bdev,
1513 "No free USB device index for new device\n");
1518 device_printf(bus->bdev,
1519 "Invalid device depth\n");
1522 udev = malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO);
1526 /* initialise our SX-lock */
1527 sx_init_flags(&udev->ctrl_sx, "USB device SX lock", SX_DUPOK);
1529 /* initialise our SX-lock */
1530 sx_init_flags(&udev->enum_sx, "USB config SX lock", SX_DUPOK);
1531 sx_init_flags(&udev->sr_sx, "USB suspend and resume SX lock", SX_DUPOK);
1533 cv_init(&udev->ctrlreq_cv, "WCTRL");
1534 cv_init(&udev->ref_cv, "UGONE");
1536 /* initialise our mutex */
1537 mtx_init(&udev->device_mtx, "USB device mutex", NULL, MTX_DEF);
1539 /* initialise generic clear stall */
1540 udev->cs_msg[0].hdr.pm_callback = &usbd_clear_stall_proc;
1541 udev->cs_msg[0].udev = udev;
1542 udev->cs_msg[1].hdr.pm_callback = &usbd_clear_stall_proc;
1543 udev->cs_msg[1].udev = udev;
1545 /* initialise some USB device fields */
1546 udev->parent_hub = parent_hub;
1547 udev->parent_dev = parent_dev;
1548 udev->port_index = port_index;
1549 udev->port_no = port_no;
1550 udev->depth = depth;
1552 udev->address = USB_START_ADDR; /* default value */
1553 udev->plugtime = (usb_ticks_t)ticks;
1555 * We need to force the power mode to "on" because there are plenty
1556 * of USB devices out there that do not work very well with
1557 * automatic suspend and resume!
1559 udev->power_mode = usbd_filter_power_mode(udev, USB_POWER_MODE_ON);
1560 udev->pwr_save.last_xfer_time = ticks;
1561 /* we are not ready yet */
1564 /* set up default endpoint descriptor */
1565 udev->ctrl_ep_desc.bLength = sizeof(udev->ctrl_ep_desc);
1566 udev->ctrl_ep_desc.bDescriptorType = UDESC_ENDPOINT;
1567 udev->ctrl_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT;
1568 udev->ctrl_ep_desc.bmAttributes = UE_CONTROL;
1569 udev->ctrl_ep_desc.wMaxPacketSize[0] = USB_MAX_IPACKET;
1570 udev->ctrl_ep_desc.wMaxPacketSize[1] = 0;
1571 udev->ctrl_ep_desc.bInterval = 0;
1573 /* set up default endpoint companion descriptor */
1574 udev->ctrl_ep_comp_desc.bLength = sizeof(udev->ctrl_ep_comp_desc);
1575 udev->ctrl_ep_comp_desc.bDescriptorType = UDESC_ENDPOINT_SS_COMP;
1577 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET;
1579 udev->speed = speed;
1580 udev->flags.usb_mode = mode;
1582 /* search for our High Speed USB HUB, if any */
1585 hub = udev->parent_hub;
1588 if (hub->speed == USB_SPEED_HIGH) {
1589 udev->hs_hub_addr = hub->address;
1590 udev->parent_hs_hub = hub;
1591 udev->hs_port_no = adev->port_no;
1595 hub = hub->parent_hub;
1598 /* init the default endpoint */
1599 usb_init_endpoint(udev, 0,
1600 &udev->ctrl_ep_desc,
1601 &udev->ctrl_ep_comp_desc,
1604 /* set device index */
1605 udev->device_index = device_index;
1608 /* Create ugen name */
1609 snprintf(udev->ugen_name, sizeof(udev->ugen_name),
1610 USB_GENERIC_NAME "%u.%u", device_get_unit(bus->bdev),
1612 LIST_INIT(&udev->pd_list);
1614 /* Create the control endpoint device */
1615 udev->ctrl_dev = usb_make_dev(udev, 0, FREAD|FWRITE);
1617 /* Create a link from /dev/ugenX.X to the default endpoint */
1618 make_dev_alias(udev->ctrl_dev, "%s", udev->ugen_name);
1620 /* Initialise device */
1621 if (bus->methods->device_init != NULL) {
1622 err = (bus->methods->device_init) (udev);
1624 DPRINTFN(0, "device init %d failed "
1625 "(%s, ignored)\n", device_index,
1630 /* set powered device state after device init is complete */
1631 usb_set_device_state(udev, USB_STATE_POWERED);
1633 if (udev->flags.usb_mode == USB_MODE_HOST) {
1635 err = usbd_req_set_address(udev, NULL, device_index);
1638 * This is the new USB device address from now on, if
1639 * the set address request didn't set it already.
1641 if (udev->address == USB_START_ADDR)
1642 udev->address = device_index;
1645 * We ignore any set-address errors, hence there are
1646 * buggy USB devices out there that actually receive
1647 * the SETUP PID, but manage to set the address before
1648 * the STATUS stage is ACK'ed. If the device responds
1649 * to the subsequent get-descriptor at the new
1650 * address, then we know that the set-address command
1654 DPRINTFN(0, "set address %d failed "
1655 "(%s, ignored)\n", udev->address,
1659 /* We are not self powered */
1660 udev->flags.self_powered = 0;
1662 /* Set unconfigured state */
1663 udev->curr_config_no = USB_UNCONFIG_NO;
1664 udev->curr_config_index = USB_UNCONFIG_INDEX;
1666 /* Setup USB descriptors */
1667 err = (usb_temp_setup_by_index_p) (udev, usb_template);
1669 DPRINTFN(0, "setting up USB template failed maybe the USB "
1670 "template module has not been loaded\n");
1674 usb_set_device_state(udev, USB_STATE_ADDRESSED);
1676 /* setup the device descriptor and the initial "wMaxPacketSize" */
1677 err = usbd_setup_device_desc(udev, NULL);
1680 /* XXX try to re-enumerate the device */
1681 err = usbd_req_re_enumerate(udev, NULL);
1687 * Setup temporary USB attach args so that we can figure out some
1688 * basic quirks for this device.
1690 usb_init_attach_arg(udev, &uaa);
1692 if (usb_test_quirk(&uaa, UQ_BUS_POWERED)) {
1693 udev->flags.uq_bus_powered = 1;
1695 if (usb_test_quirk(&uaa, UQ_NO_STRINGS)) {
1696 udev->flags.no_strings = 1;
1699 * Workaround for buggy USB devices.
1701 * It appears that some string-less USB chips will crash and
1702 * disappear if any attempts are made to read any string
1705 * Try to detect such chips by checking the strings in the USB
1706 * device descriptor. If no strings are present there we
1707 * simply disable all USB strings.
1709 scratch_ptr = udev->bus->scratch[0].data;
1710 scratch_size = sizeof(udev->bus->scratch[0].data);
1712 if (udev->ddesc.iManufacturer ||
1713 udev->ddesc.iProduct ||
1714 udev->ddesc.iSerialNumber) {
1715 /* read out the language ID string */
1716 err = usbd_req_get_string_desc(udev, NULL,
1717 (char *)scratch_ptr, 4, 0, USB_LANGUAGE_TABLE);
1719 err = USB_ERR_INVAL;
1722 if (err || (scratch_ptr[0] < 4)) {
1723 udev->flags.no_strings = 1;
1730 /* load preferred value and mask */
1732 mask = usb_lang_mask;
1734 /* align length correctly */
1735 scratch_ptr[0] &= ~1;
1737 /* fix compiler warning */
1740 /* search for preferred language */
1741 for (x = 2; (x < scratch_ptr[0]); x += 2) {
1742 langid = UGETW(scratch_ptr + x);
1743 if ((langid & mask) == pref)
1746 if (x >= scratch_ptr[0]) {
1747 /* pick the first language as the default */
1748 DPRINTFN(1, "Using first language\n");
1749 langid = UGETW(scratch_ptr + 2);
1752 DPRINTFN(1, "Language selected: 0x%04x\n", langid);
1753 udev->langid = langid;
1756 /* assume 100mA bus powered for now. Changed when configured. */
1757 udev->power = USB_MIN_POWER;
1758 /* fetch the vendor and product strings from the device */
1759 usbd_set_device_strings(udev);
1761 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
1762 /* USB device mode setup is complete */
1768 * Most USB devices should attach to config index 0 by
1771 if (usb_test_quirk(&uaa, UQ_CFG_INDEX_0)) {
1774 } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_1)) {
1777 } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_2)) {
1780 } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_3)) {
1783 } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_4)) {
1791 set_config_failed = 0;
1794 DPRINTF("setting config %u\n", config_index);
1796 /* get the USB device configured */
1797 err = usbd_set_config_index(udev, config_index);
1799 if (udev->ddesc.bNumConfigurations != 0) {
1800 if (!set_config_failed) {
1801 set_config_failed = 1;
1802 /* XXX try to re-enumerate the device */
1803 err = usbd_req_re_enumerate(udev, NULL);
1805 goto repeat_set_config;
1807 DPRINTFN(0, "Failure selecting configuration index %u:"
1808 "%s, port %u, addr %u (ignored)\n",
1809 config_index, usbd_errstr(err), udev->port_no,
1813 * Some USB devices do not have any configurations. Ignore any
1814 * set config failures!
1819 if (!config_quirk && config_index + 1 < udev->ddesc.bNumConfigurations) {
1820 if ((udev->cdesc->bNumInterface < 2) &&
1821 usbd_get_no_descriptors(udev->cdesc, UDESC_ENDPOINT) == 0) {
1822 DPRINTFN(0, "Found no endpoints, trying next config\n");
1824 goto repeat_set_config;
1826 if (config_index == 0) {
1828 * Try to figure out if we have an
1829 * auto-install disk there:
1831 if (usb_iface_is_cdrom(udev, 0)) {
1832 DPRINTFN(0, "Found possible auto-install "
1833 "disk (trying next config)\n");
1835 goto repeat_set_config;
1839 EVENTHANDLER_INVOKE(usb_dev_configured, udev, &uaa);
1840 if (uaa.dev_state != UAA_DEV_READY) {
1841 /* leave device unconfigured */
1842 usb_unconfigure(udev, 0);
1846 DPRINTF("new dev (addr %d), udev=%p, parent_hub=%p\n",
1847 udev->address, udev, udev->parent_hub);
1849 /* register our device - we are ready */
1850 usb_bus_port_set_device(bus, parent_hub ?
1851 parent_hub->hub->ports + port_index : NULL, udev, device_index);
1854 /* Symlink the ugen device name */
1855 udev->ugen_symlink = usb_alloc_symlink(udev->ugen_name);
1857 /* Announce device */
1858 printf("%s: <%s> at %s\n", udev->ugen_name,
1859 usb_get_manufacturer(udev),
1860 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);
2010 printf("%s: <%s> at %s (disconnected)\n", udev->ugen_name,
2011 usb_get_manufacturer(udev), device_get_nameunit(bus->bdev));
2013 /* Destroy UGEN symlink, if any */
2014 if (udev->ugen_symlink) {
2015 usb_free_symlink(udev->ugen_symlink);
2016 udev->ugen_symlink = NULL;
2020 * Unregister our device first which will prevent any further
2023 usb_bus_port_set_device(bus, udev->parent_hub ?
2024 udev->parent_hub->hub->ports + udev->port_index : NULL,
2025 NULL, USB_ROOT_HUB_ADDR);
2028 /* wait for all pending references to go away: */
2029 mtx_lock(&usb_ref_lock);
2031 while (udev->refcount != 0) {
2032 cv_wait(&udev->ref_cv, &usb_ref_lock);
2034 mtx_unlock(&usb_ref_lock);
2036 destroy_dev_sched_cb(udev->ctrl_dev, usb_cdev_cleanup,
2037 udev->ctrl_dev->si_drv1);
2040 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
2041 /* stop receiving any control transfers (Device Side Mode) */
2042 usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
2045 /* the following will get the device unconfigured in software */
2046 usb_unconfigure(udev, USB_UNCFG_FLAG_FREE_EP0);
2048 /* unsetup any leftover default USB transfers */
2049 usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX);
2051 /* template unsetup, if any */
2052 (usb_temp_unsetup_p) (udev);
2055 * Make sure that our clear-stall messages are not queued
2058 USB_BUS_LOCK(udev->bus);
2059 usb_proc_mwait(&udev->bus->non_giant_callback_proc,
2060 &udev->cs_msg[0], &udev->cs_msg[1]);
2061 USB_BUS_UNLOCK(udev->bus);
2063 sx_destroy(&udev->ctrl_sx);
2064 sx_destroy(&udev->enum_sx);
2065 sx_destroy(&udev->sr_sx);
2067 cv_destroy(&udev->ctrlreq_cv);
2068 cv_destroy(&udev->ref_cv);
2070 mtx_destroy(&udev->device_mtx);
2072 KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("leaked cdev entries"));
2075 /* Uninitialise device */
2076 if (bus->methods->device_uninit != NULL)
2077 (bus->methods->device_uninit) (udev);
2080 free(udev->serial, M_USB);
2081 free(udev->manufacturer, M_USB);
2082 free(udev->product, M_USB);
2086 /*------------------------------------------------------------------------*
2089 * This function is the safe way to get the USB interface structure
2090 * pointer by interface index.
2093 * NULL: Interface not present.
2094 * Else: Pointer to USB interface structure.
2095 *------------------------------------------------------------------------*/
2096 struct usb_interface *
2097 usbd_get_iface(struct usb_device *udev, uint8_t iface_index)
2099 struct usb_interface *iface = udev->ifaces + iface_index;
2101 if (iface_index >= udev->ifaces_max)
2106 /*------------------------------------------------------------------------*
2107 * usbd_find_descriptor
2109 * This function will lookup the first descriptor that matches the
2110 * criteria given by the arguments "type" and "subtype". Descriptors
2111 * will only be searched within the interface having the index
2112 * "iface_index". If the "id" argument points to an USB descriptor,
2113 * it will be skipped before the search is started. This allows
2114 * searching for multiple descriptors using the same criteria. Else
2115 * the search is started after the interface descriptor.
2118 * NULL: End of descriptors
2119 * Else: A descriptor matching the criteria
2120 *------------------------------------------------------------------------*/
2122 usbd_find_descriptor(struct usb_device *udev, void *id, uint8_t iface_index,
2123 uint8_t type, uint8_t type_mask,
2124 uint8_t subtype, uint8_t subtype_mask)
2126 struct usb_descriptor *desc;
2127 struct usb_config_descriptor *cd;
2128 struct usb_interface *iface;
2130 cd = usbd_get_config_descriptor(udev);
2135 iface = usbd_get_iface(udev, iface_index);
2136 if (iface == NULL) {
2139 id = usbd_get_interface_descriptor(iface);
2146 while ((desc = usb_desc_foreach(cd, desc))) {
2148 if (desc->bDescriptorType == UDESC_INTERFACE) {
2151 if (((desc->bDescriptorType & type_mask) == type) &&
2152 ((desc->bDescriptorSubtype & subtype_mask) == subtype)) {
2159 /*------------------------------------------------------------------------*
2162 * This function will dump information from the device descriptor
2163 * belonging to the USB device pointed to by "udev", to the string
2164 * pointed to by "dst_ptr" having a maximum length of "dst_len" bytes
2165 * including the terminating zero.
2166 *------------------------------------------------------------------------*/
2168 usb_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len)
2170 struct usb_device_descriptor *udd = &udev->ddesc;
2174 bcdUSB = UGETW(udd->bcdUSB);
2175 bcdDevice = UGETW(udd->bcdDevice);
2177 if (udd->bDeviceClass != 0xFF) {
2178 snprintf(dst_ptr, dst_len, "%s %s, class %d/%d, rev %x.%02x/"
2180 usb_get_manufacturer(udev),
2181 usb_get_product(udev),
2182 udd->bDeviceClass, udd->bDeviceSubClass,
2183 (bcdUSB >> 8), bcdUSB & 0xFF,
2184 (bcdDevice >> 8), bcdDevice & 0xFF,
2187 snprintf(dst_ptr, dst_len, "%s %s, rev %x.%02x/"
2189 usb_get_manufacturer(udev),
2190 usb_get_product(udev),
2191 (bcdUSB >> 8), bcdUSB & 0xFF,
2192 (bcdDevice >> 8), bcdDevice & 0xFF,
2199 * Descriptions of of known vendors and devices ("products").
2201 struct usb_knowndev {
2205 const char *vendorname;
2206 const char *productname;
2209 #define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */
2211 #include "usbdevs.h"
2212 #include "usbdevs_data.h"
2213 #endif /* USB_VERBOSE */
2216 usbd_set_device_strings(struct usb_device *udev)
2218 struct usb_device_descriptor *udd = &udev->ddesc;
2220 const struct usb_knowndev *kdp;
2225 uint16_t product_id;
2227 temp_ptr = (char *)udev->bus->scratch[0].data;
2228 temp_size = sizeof(udev->bus->scratch[0].data);
2230 vendor_id = UGETW(udd->idVendor);
2231 product_id = UGETW(udd->idProduct);
2233 /* get serial number string */
2234 usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2235 udev->ddesc.iSerialNumber);
2236 udev->serial = strdup(temp_ptr, M_USB);
2238 /* get manufacturer string */
2239 usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2240 udev->ddesc.iManufacturer);
2241 usb_trim_spaces(temp_ptr);
2242 if (temp_ptr[0] != '\0')
2243 udev->manufacturer = strdup(temp_ptr, M_USB);
2245 /* get product string */
2246 usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size,
2247 udev->ddesc.iProduct);
2248 usb_trim_spaces(temp_ptr);
2249 if (temp_ptr[0] != '\0')
2250 udev->product = strdup(temp_ptr, M_USB);
2253 if (udev->manufacturer == NULL || udev->product == NULL) {
2254 for (kdp = usb_knowndevs; kdp->vendorname != NULL; kdp++) {
2255 if (kdp->vendor == vendor_id &&
2256 (kdp->product == product_id ||
2257 (kdp->flags & USB_KNOWNDEV_NOPROD) != 0))
2260 if (kdp->vendorname != NULL) {
2261 /* XXX should use pointer to knowndevs string */
2262 if (udev->manufacturer == NULL) {
2263 udev->manufacturer = strdup(kdp->vendorname,
2266 if (udev->product == NULL &&
2267 (kdp->flags & USB_KNOWNDEV_NOPROD) == 0) {
2268 udev->product = strdup(kdp->productname,
2274 /* Provide default strings if none were found */
2275 if (udev->manufacturer == NULL) {
2276 snprintf(temp_ptr, temp_size, "vendor 0x%04x", vendor_id);
2277 udev->manufacturer = strdup(temp_ptr, M_USB);
2279 if (udev->product == NULL) {
2280 snprintf(temp_ptr, temp_size, "product 0x%04x", product_id);
2281 udev->product = strdup(temp_ptr, M_USB);
2290 usbd_get_mode(struct usb_device *udev)
2292 return (udev->flags.usb_mode);
2297 * See: USB_SPEED_XXX
2300 usbd_get_speed(struct usb_device *udev)
2302 return (udev->speed);
2306 usbd_get_isoc_fps(struct usb_device *udev)
2309 switch (udev->speed) {
2311 case USB_SPEED_FULL:
2318 struct usb_device_descriptor *
2319 usbd_get_device_descriptor(struct usb_device *udev)
2322 return (NULL); /* be NULL safe */
2323 return (&udev->ddesc);
2326 struct usb_config_descriptor *
2327 usbd_get_config_descriptor(struct usb_device *udev)
2330 return (NULL); /* be NULL safe */
2331 return (udev->cdesc);
2334 /*------------------------------------------------------------------------*
2335 * usb_test_quirk - test a device for a given quirk
2338 * 0: The USB device does not have the given quirk.
2339 * Else: The USB device has the given quirk.
2340 *------------------------------------------------------------------------*/
2342 usb_test_quirk(const struct usb_attach_arg *uaa, uint16_t quirk)
2346 found = (usb_test_quirk_p) (&uaa->info, quirk);
2350 struct usb_interface_descriptor *
2351 usbd_get_interface_descriptor(struct usb_interface *iface)
2354 return (NULL); /* be NULL safe */
2355 return (iface->idesc);
2359 usbd_get_interface_altindex(struct usb_interface *iface)
2361 return (iface->alt_index);
2365 usbd_get_bus_index(struct usb_device *udev)
2367 return ((uint8_t)device_get_unit(udev->bus->bdev));
2371 usbd_get_device_index(struct usb_device *udev)
2373 return (udev->device_index);
2377 /*------------------------------------------------------------------------*
2380 * This function will generate events for dev.
2381 *------------------------------------------------------------------------*/
2382 #ifndef BURN_BRIDGES
2384 usb_notify_addq_compat(const char *type, struct usb_device *udev)
2388 struct malloc_type *mt;
2389 const size_t buf_size = 512;
2391 /* Convert notify type */
2392 if (strcmp(type, "ATTACH") == 0)
2394 else if (strcmp(type, "DETACH") == 0)
2399 mtx_lock(&malloc_mtx);
2400 mt = malloc_desc2type("bus"); /* XXX M_BUS */
2401 mtx_unlock(&malloc_mtx);
2405 data = malloc(buf_size, mt, M_NOWAIT);
2409 /* String it all together. */
2410 snprintf(data, buf_size,
2416 "devsubclass=0x%02x "
2425 UGETW(udev->ddesc.idVendor),
2426 UGETW(udev->ddesc.idProduct),
2427 udev->ddesc.bDeviceClass,
2428 udev->ddesc.bDeviceSubClass,
2429 usb_get_serial(udev),
2430 UGETW(udev->ddesc.bcdDevice),
2432 udev->parent_hub != NULL ?
2433 udev->parent_hub->ugen_name :
2434 device_get_nameunit(device_get_parent(udev->bus->bdev)));
2436 devctl_queue_data(data);
2441 usb_notify_addq(const char *type, struct usb_device *udev)
2443 struct usb_interface *iface;
2447 #ifndef BURN_BRIDGES
2448 usb_notify_addq_compat(type, udev);
2451 /* announce the device */
2452 sb = sbuf_new_auto();
2458 "devsubclass=0x%02x "
2465 UGETW(udev->ddesc.idVendor),
2466 UGETW(udev->ddesc.idProduct),
2467 udev->ddesc.bDeviceClass,
2468 udev->ddesc.bDeviceSubClass,
2469 usb_get_serial(udev),
2470 UGETW(udev->ddesc.bcdDevice),
2471 (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
2473 udev->parent_hub != NULL ?
2474 udev->parent_hub->ugen_name :
2475 device_get_nameunit(device_get_parent(udev->bus->bdev)));
2477 devctl_notify("USB", "DEVICE", type, sbuf_data(sb));
2480 /* announce each interface */
2481 for (i = 0; i < USB_IFACE_MAX; i++) {
2482 iface = usbd_get_iface(udev, i);
2484 break; /* end of interfaces */
2485 if (iface->idesc == NULL)
2486 continue; /* no interface descriptor */
2488 sb = sbuf_new_auto();
2494 "devsubclass=0x%02x "
2501 "intsubclass=0x%02x "
2502 "intprotocol=0x%02x\n",
2504 UGETW(udev->ddesc.idVendor),
2505 UGETW(udev->ddesc.idProduct),
2506 udev->ddesc.bDeviceClass,
2507 udev->ddesc.bDeviceSubClass,
2508 usb_get_serial(udev),
2509 UGETW(udev->ddesc.bcdDevice),
2510 (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
2511 iface->idesc->bInterfaceNumber,
2512 iface->idesc->bNumEndpoints,
2513 iface->idesc->bInterfaceClass,
2514 iface->idesc->bInterfaceSubClass,
2515 iface->idesc->bInterfaceProtocol);
2517 devctl_notify("USB", "INTERFACE", type, sbuf_data(sb));
2522 /*------------------------------------------------------------------------*
2523 * usb_fifo_free_wrap
2525 * This function will free the FIFOs.
2527 * Description of "flag" argument: If the USB_UNCFG_FLAG_FREE_EP0 flag
2528 * is set and "iface_index" is set to "USB_IFACE_INDEX_ANY", we free
2529 * all FIFOs. If the USB_UNCFG_FLAG_FREE_EP0 flag is not set and
2530 * "iface_index" is set to "USB_IFACE_INDEX_ANY", we free all non
2531 * control endpoint FIFOs. If "iface_index" is not set to
2532 * "USB_IFACE_INDEX_ANY" the flag has no effect.
2533 *------------------------------------------------------------------------*/
2535 usb_fifo_free_wrap(struct usb_device *udev,
2536 uint8_t iface_index, uint8_t flag)
2542 * Free any USB FIFOs on the given interface:
2544 for (i = 0; i != USB_FIFO_MAX; i++) {
2549 /* Check if the interface index matches */
2550 if (iface_index == f->iface_index) {
2551 if (f->methods != &usb_ugen_methods) {
2553 * Don't free any non-generic FIFOs in
2558 if ((f->dev_ep_index == 0) &&
2559 (f->fs_xfer == NULL)) {
2560 /* no need to free this FIFO */
2563 } else if (iface_index == USB_IFACE_INDEX_ANY) {
2564 if ((f->methods == &usb_ugen_methods) &&
2565 (f->dev_ep_index == 0) &&
2566 (!(flag & USB_UNCFG_FLAG_FREE_EP0)) &&
2567 (f->fs_xfer == NULL)) {
2568 /* no need to free this FIFO */
2572 /* no need to free this FIFO */
2575 /* free this FIFO */
2581 /*------------------------------------------------------------------------*
2582 * usb_peer_can_wakeup
2585 * 0: Peer cannot do resume signalling.
2586 * Else: Peer can do resume signalling.
2587 *------------------------------------------------------------------------*/
2589 usb_peer_can_wakeup(struct usb_device *udev)
2591 const struct usb_config_descriptor *cdp;
2594 if ((cdp != NULL) && (udev->flags.usb_mode == USB_MODE_HOST)) {
2595 return (cdp->bmAttributes & UC_REMOTE_WAKEUP);
2597 return (0); /* not supported */
2601 usb_set_device_state(struct usb_device *udev, enum usb_dev_state state)
2604 KASSERT(state < USB_STATE_MAX, ("invalid udev state"));
2606 DPRINTF("udev %p state %s -> %s\n", udev,
2607 usb_statestr(udev->state), usb_statestr(state));
2608 udev->state = state;
2610 if (udev->bus->methods->device_state_change != NULL)
2611 (udev->bus->methods->device_state_change) (udev);
2615 usb_get_device_state(struct usb_device *udev)
2618 return (USB_STATE_DETACHED);
2619 return (udev->state);
2623 usbd_device_attached(struct usb_device *udev)
2625 return (udev->state > USB_STATE_DETACHED);
2628 /* The following function locks enumerating the given USB device. */
2631 usbd_enum_lock(struct usb_device *udev)
2633 sx_xlock(&udev->enum_sx);
2634 sx_xlock(&udev->sr_sx);
2636 * NEWBUS LOCK NOTE: We should check if any parent SX locks
2637 * are locked before locking Giant. Else the lock can be
2638 * locked multiple times.
2643 /* The following function unlocks enumerating the given USB device. */
2646 usbd_enum_unlock(struct usb_device *udev)
2649 sx_xunlock(&udev->enum_sx);
2650 sx_xunlock(&udev->sr_sx);
2653 /* The following function locks suspend and resume. */
2656 usbd_sr_lock(struct usb_device *udev)
2658 sx_xlock(&udev->sr_sx);
2660 * NEWBUS LOCK NOTE: We should check if any parent SX locks
2661 * are locked before locking Giant. Else the lock can be
2662 * locked multiple times.
2667 /* The following function unlocks suspend and resume. */
2670 usbd_sr_unlock(struct usb_device *udev)
2673 sx_xunlock(&udev->sr_sx);
2677 * The following function checks the enumerating lock for the given
2682 usbd_enum_is_locked(struct usb_device *udev)
2684 return (sx_xlocked(&udev->enum_sx));