Lines Matching +full:powered +full:- +full:while +full:- +full:suspended

1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2008-2023 Hans Petter Selasky
119 int usb_template = -1;
127 /*------------------------------------------------------------------------*
133 *------------------------------------------------------------------------*/
144 while (max >= 0) { in usb_trigger_reprobe_on_off()
147 max--; in usb_trigger_reprobe_on_off()
149 if (bus == NULL || bus->devices == NULL || in usb_trigger_reprobe_on_off()
150 bus->devices[USB_ROOT_HUB_ADDR] == NULL) { in usb_trigger_reprobe_on_off()
155 udev = bus->devices[USB_ROOT_HUB_ADDR]; in usb_trigger_reprobe_on_off()
157 if (udev->refcount == USB_DEV_REF_MAX) { in usb_trigger_reprobe_on_off()
162 udev->refcount++; in usb_trigger_reprobe_on_off()
201 if (--(udev->refcount) == 0) in usb_trigger_reprobe_on_off()
202 cv_broadcast(&udev->ref_cv); in usb_trigger_reprobe_on_off()
207 /*------------------------------------------------------------------------*
212 *------------------------------------------------------------------------*/
240 if (error != 0 || req->newptr == NULL || usb_template == val) in sysctl_hw_usb_template()
268 [USB_STATE_POWERED] = "POWERED",
282 return (udev->manufacturer ? udev->manufacturer : "Unknown"); in usb_get_manufacturer()
288 return (udev->product ? udev->product : ""); in usb_get_product()
294 return (udev->serial ? udev->serial : ""); in usb_get_serial()
297 /*------------------------------------------------------------------------*
306 *------------------------------------------------------------------------*/
310 struct usb_endpoint *ep = udev->endpoints; in usbd_get_ep_by_addr()
311 struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max; in usbd_get_ep_by_addr()
327 if (ep->edesc == NULL) { in usbd_get_ep_by_addr()
331 if ((ep->edesc->bEndpointAddress & EA_MASK) == ea_val) { in usbd_get_ep_by_addr()
339 if ((udev->ctrl_ep.edesc != NULL) && in usbd_get_ep_by_addr()
340 ((udev->ctrl_ep.edesc->bEndpointAddress & EA_MASK) == ea_val)) { in usbd_get_ep_by_addr()
341 ep = &udev->ctrl_ep; in usbd_get_ep_by_addr()
350 /*------------------------------------------------------------------------*
359 *------------------------------------------------------------------------*/
364 struct usb_endpoint *ep = udev->endpoints; in usbd_get_endpoint()
365 struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max; in usbd_get_endpoint()
366 uint8_t index = setup->ep_index; in usbd_get_endpoint()
374 udev, iface_index, setup->endpoint, in usbd_get_endpoint()
375 setup->type, setup->direction, setup->ep_index); in usbd_get_endpoint()
379 if (setup->usb_mode != USB_MODE_DUAL && in usbd_get_endpoint()
380 udev->flags.usb_mode != setup->usb_mode) { in usbd_get_endpoint()
381 /* wrong mode - no endpoint */ in usbd_get_endpoint()
387 if (setup->direction == UE_DIR_RX) { in usbd_get_endpoint()
389 ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ? in usbd_get_endpoint()
391 } else if (setup->direction == UE_DIR_TX) { in usbd_get_endpoint()
393 ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ? in usbd_get_endpoint()
395 } else if (setup->direction == UE_DIR_ANY) { in usbd_get_endpoint()
402 ea_val = (setup->direction & (UE_DIR_IN | UE_DIR_OUT)); in usbd_get_endpoint()
407 if (setup->endpoint == UE_ADDR_ANY) { in usbd_get_endpoint()
412 ea_val |= (setup->endpoint & UE_ADDR); in usbd_get_endpoint()
417 if (setup->type == UE_BULK_INTR) { in usbd_get_endpoint()
421 } else if (setup->type == UE_TYPE_ANY) { in usbd_get_endpoint()
428 type_val = (setup->type & UE_XFERTYPE); in usbd_get_endpoint()
434 * the endpoints from the beginning of the "udev->endpoints" array. in usbd_get_endpoint()
437 if ((ep->edesc == NULL) || in usbd_get_endpoint()
438 (ep->iface_index != iface_index)) { in usbd_get_endpoint()
443 if (((ep->edesc->bEndpointAddress & ea_mask) == ea_val) && in usbd_get_endpoint()
444 ((ep->edesc->bmAttributes & type_mask) == type_val)) { in usbd_get_endpoint()
445 if (!index--) { in usbd_get_endpoint()
456 if ((udev->ctrl_ep.edesc != NULL) && in usbd_get_endpoint()
457 ((udev->ctrl_ep.edesc->bEndpointAddress & ea_mask) == ea_val) && in usbd_get_endpoint()
458 ((udev->ctrl_ep.edesc->bmAttributes & type_mask) == type_val) && in usbd_get_endpoint()
460 ep = &udev->ctrl_ep; in usbd_get_endpoint()
469 /*------------------------------------------------------------------------*
474 * the unsigned 8-bit integer pointed to by "count".
479 *------------------------------------------------------------------------*/
483 if (udev->cdesc == NULL) { in usbd_interface_count()
487 *count = udev->ifaces_max; in usbd_interface_count()
491 /*------------------------------------------------------------------------*
497 *------------------------------------------------------------------------*/
507 methods = udev->bus->methods; in usb_init_endpoint()
509 (methods->endpoint_init) (udev, edesc, ep); in usb_init_endpoint()
512 ep->edesc = edesc; in usb_init_endpoint()
513 ep->ecomp = ecomp; in usb_init_endpoint()
514 ep->iface_index = iface_index; in usb_init_endpoint()
518 TAILQ_INIT(&ep->endpoint_q[x].head); in usb_init_endpoint()
519 ep->endpoint_q[x].command = &usbd_pipe_start; in usb_init_endpoint()
523 if (ep->methods == NULL) in usb_init_endpoint()
526 /* check for SUPER-speed streams mode endpoint */ in usb_init_endpoint()
527 if (udev->speed == USB_SPEED_SUPER && ecomp != NULL && in usb_init_endpoint()
528 (edesc->bmAttributes & UE_XFERTYPE) == UE_BULK && in usb_init_endpoint()
529 (UE_GET_BULK_STREAMS(ecomp->bmAttributes) != 0)) { in usb_init_endpoint()
536 if (methods->clear_stall != NULL) { in usb_init_endpoint()
537 USB_BUS_LOCK(udev->bus); in usb_init_endpoint()
538 (methods->clear_stall) (udev, ep); in usb_init_endpoint()
539 USB_BUS_UNLOCK(udev->bus); in usb_init_endpoint()
543 /*-----------------------------------------------------------------------*
552 *------------------------------------------------------------------------*/
562 ep_end = udev->endpoints + udev->endpoints_max; in usb_endpoint_foreach()
566 ep = udev->endpoints; in usb_endpoint_foreach()
571 while (ep != ep_end) { in usb_endpoint_foreach()
572 if (ep->edesc != NULL) in usb_endpoint_foreach()
579 /*------------------------------------------------------------------------*
584 *------------------------------------------------------------------------*/
589 DPRINTF("Refcount = %d\n", (int)udev->refcount); in usb_wait_pending_refs()
592 udev->refcount--; in usb_wait_pending_refs()
593 while (1) { in usb_wait_pending_refs()
595 if (udev->refcount == 0) { in usb_wait_pending_refs()
597 udev->refcount = USB_DEV_REF_MAX; in usb_wait_pending_refs()
600 cv_wait(&udev->ref_cv, &usb_ref_lock); in usb_wait_pending_refs()
606 /*------------------------------------------------------------------------*
613 *------------------------------------------------------------------------*/
619 /* Prevent re-enumeration */ in usb_unconfigure()
637 if (udev->linux_endpoint_start != NULL) { in usb_unconfigure()
639 udev->linux_endpoint_start = NULL; in usb_unconfigure()
646 if (udev->cdesc != NULL) { in usb_unconfigure()
647 if (udev->flags.usb_mode != USB_MODE_DEVICE) in usb_unconfigure()
648 usbd_free_config_desc(udev, udev->cdesc); in usb_unconfigure()
649 udev->cdesc = NULL; in usb_unconfigure()
652 udev->curr_config_no = USB_UNCONFIG_NO; in usb_unconfigure()
653 udev->curr_config_index = USB_UNCONFIG_INDEX; in usb_unconfigure()
659 /*------------------------------------------------------------------------*
669 *------------------------------------------------------------------------*/
683 /* Prevent re-enumeration */ in usbd_set_config_index()
695 if (udev->state == USB_STATE_CONFIGURED) in usbd_set_config_index()
700 if (udev->flags.usb_mode == USB_MODE_DEVICE) { in usbd_set_config_index()
714 udev->cdesc = cdp; in usbd_set_config_index()
716 /* Figure out if the device is self or bus powered. */ in usbd_set_config_index()
718 if ((!udev->flags.uq_bus_powered) && in usbd_set_config_index()
719 (cdp->bmAttributes & UC_SELF_POWERED) && in usbd_set_config_index()
720 (udev->flags.usb_mode == USB_MODE_HOST)) { in usbd_set_config_index()
721 /* May be self powered. */ in usbd_set_config_index()
722 if (cdp->bmAttributes & UC_BUS_POWERED) { in usbd_set_config_index()
740 udev->address, cdp->bConfigurationValue, cdp->bmAttributes, in usbd_set_config_index()
741 selfpowered, cdp->bMaxPower * 2); in usbd_set_config_index()
744 power = cdp->bMaxPower * 2; in usbd_set_config_index()
746 if (udev->parent_hub) { in usbd_set_config_index()
747 max_power = udev->parent_hub->hub->portpower; in usbd_set_config_index()
758 if (udev->flags.usb_mode == USB_MODE_HOST) { in usbd_set_config_index()
759 udev->flags.self_powered = selfpowered; in usbd_set_config_index()
761 udev->power = power; in usbd_set_config_index()
762 udev->curr_config_no = cdp->bConfigurationValue; in usbd_set_config_index()
763 udev->curr_config_index = index; in usbd_set_config_index()
767 err = usbd_req_set_config(udev, NULL, cdp->bConfigurationValue); in usbd_set_config_index()
797 /*------------------------------------------------------------------------*
813 *------------------------------------------------------------------------*/
844 sx_assert(&udev->enum_sx, SA_LOCKED); in usb_config_parse()
846 /* check for in-use endpoints */ in usb_config_parse()
849 ep = udev->endpoints; in usb_config_parse()
850 ep_max = udev->endpoints_max; in usb_config_parse()
851 while (ep_max--) { in usb_config_parse()
854 iface_index == ep->iface_index) { in usb_config_parse()
855 if (ep->refcount_alloc != 0) in usb_config_parse()
862 ep = udev->endpoints; in usb_config_parse()
863 ep_max = udev->endpoints_max; in usb_config_parse()
864 while (ep_max--) { in usb_config_parse()
867 iface_index == ep->iface_index) { in usb_config_parse()
877 if (ep->edesc != NULL && in usb_config_parse()
878 udev->bus->methods->endpoint_uninit != NULL) in usb_config_parse()
879 udev->bus->methods->endpoint_uninit(udev, ep); in usb_config_parse()
884 ep->iface_index = USB_IFACE_INDEX_ANY; in usb_config_parse()
898 while ((id = usb_idesc_foreach(udev->cdesc, &ips))) { in usb_config_parse()
899 iface = udev->ifaces + ips.iface_index; in usb_config_parse()
917 iface->num_altsetting = ips.iface_index_alt + 1; in usb_config_parse()
930 iface->idesc = id; in usb_config_parse()
932 iface->alt_index = alt_index; in usb_config_parse()
935 iface->parent_iface_index = in usb_config_parse()
940 DPRINTFN(5, "found idesc nendpt=%d\n", id->bNumEndpoints); in usb_config_parse()
947 while ((ed = usb_edesc_foreach(udev->cdesc, ed))) { in usb_config_parse()
954 ep = udev->endpoints + temp; in usb_config_parse()
959 ecomp = usb_ed_comp_foreach(udev->cdesc, (void *)ed); in usb_config_parse()
978 udev->ifaces_max = ips.iface_index; in usb_config_parse()
980 udev->ifaces = NULL; in usb_config_parse()
981 if (udev->ifaces_max != 0) { in usb_config_parse()
982 udev->ifaces = malloc(sizeof(*iface) * udev->ifaces_max, in usb_config_parse()
984 if (udev->ifaces == NULL) { in usb_config_parse()
992 udev->endpoints = malloc(sizeof(*ep) * ep_max, in usb_config_parse()
994 if (udev->endpoints == NULL) { in usb_config_parse()
999 udev->endpoints = NULL; in usb_config_parse()
1002 USB_BUS_LOCK(udev->bus); in usb_config_parse()
1003 udev->endpoints_max = ep_max; in usb_config_parse()
1004 /* reset any ongoing clear-stall */ in usb_config_parse()
1005 udev->ep_curr = NULL; in usb_config_parse()
1006 USB_BUS_UNLOCK(udev->bus); in usb_config_parse()
1014 USB_BUS_LOCK(udev->bus); in usb_config_parse()
1015 udev->endpoints_max = 0; in usb_config_parse()
1016 /* reset any ongoing clear-stall */ in usb_config_parse()
1017 udev->ep_curr = NULL; in usb_config_parse()
1018 USB_BUS_UNLOCK(udev->bus); in usb_config_parse()
1021 free(udev->ifaces, M_USB); in usb_config_parse()
1022 udev->ifaces = NULL; in usb_config_parse()
1025 free(udev->endpoints, M_USB); in usb_config_parse()
1026 udev->endpoints = NULL; in usb_config_parse()
1028 udev->ifaces_max = 0; in usb_config_parse()
1034 /*------------------------------------------------------------------------*
1047 *------------------------------------------------------------------------*/
1056 /* Prevent re-enumeration */ in usbd_set_alt_interface_index()
1063 if (iface->alt_index == alt_index) { in usbd_set_alt_interface_index()
1083 if (iface->alt_index != alt_index) { in usbd_set_alt_interface_index()
1090 iface->idesc->bAlternateSetting); in usbd_set_alt_interface_index()
1098 /*------------------------------------------------------------------------*
1107 *------------------------------------------------------------------------*/
1128 et = (ep->edesc->bmAttributes & UE_XFERTYPE); in usbd_set_endpoint_stall()
1139 USB_BUS_LOCK(udev->bus); in usbd_set_endpoint_stall()
1142 was_stalled = ep->is_stalled; in usbd_set_endpoint_stall()
1147 USB_BUS_UNLOCK(udev->bus); in usbd_set_endpoint_stall()
1152 ep->is_stalled = 1; in usbd_set_endpoint_stall()
1158 xfer = ep->endpoint_q[x].curr; in usbd_set_endpoint_stall()
1167 (udev->bus->methods->xfer_stall) (xfer); in usbd_set_endpoint_stall()
1171 (udev->bus->methods->set_stall) (udev, ep, &do_stall); in usbd_set_endpoint_stall()
1174 ep->toggle_next = 0; /* reset data toggle */ in usbd_set_endpoint_stall()
1175 ep->is_stalled = 0; /* clear stalled state */ in usbd_set_endpoint_stall()
1177 (udev->bus->methods->clear_stall) (udev, ep); in usbd_set_endpoint_stall()
1181 usb_command_wrapper(&ep->endpoint_q[x], in usbd_set_endpoint_stall()
1182 ep->endpoint_q[x].curr); in usbd_set_endpoint_stall()
1185 USB_BUS_UNLOCK(udev->bus); in usbd_set_endpoint_stall()
1189 /*------------------------------------------------------------------------*
1190 * usb_reset_iface_endpoints - used in USB device side mode
1191 *------------------------------------------------------------------------*/
1198 ep = udev->endpoints; in usb_reset_iface_endpoints()
1199 ep_end = udev->endpoints + udev->endpoints_max; in usb_reset_iface_endpoints()
1202 if ((ep->edesc == NULL) || in usb_reset_iface_endpoints()
1203 (ep->iface_index != iface_index)) { in usb_reset_iface_endpoints()
1212 /*------------------------------------------------------------------------*
1219 *------------------------------------------------------------------------*/
1240 device_get_nameunit(udev->parent_dev), in usb_detach_device_sub()
1241 udev->port_no, udev->address); in usb_detach_device_sub()
1245 if (udev->flags.peer_suspended) { in usb_detach_device_sub()
1253 if (device_delete_child(udev->parent_dev, dev)) { in usb_detach_device_sub()
1270 /*------------------------------------------------------------------------*
1277 *------------------------------------------------------------------------*/
1291 sx_assert(&udev->enum_sx, SA_LOCKED); in usb_detach_device()
1295 * chance to detach the sub-devices in the correct order. in usb_detach_device()
1297 * will detach all sub-devices from the bottom and upwards! in usb_detach_device()
1315 usb_detach_device_sub(udev, &iface->subdev, in usb_detach_device()
1316 &iface->pnpinfo, flag); in usb_detach_device()
1320 /*------------------------------------------------------------------------*
1326 *------------------------------------------------------------------------*/
1335 iface = uaa->iface; in usb_probe_and_attach_sub()
1336 if (iface->parent_iface_index != USB_IFACE_INDEX_ANY) { in usb_probe_and_attach_sub()
1340 dev = iface->subdev; in usb_probe_and_attach_sub()
1348 /* clear "iface->subdev" as early as possible */ in usb_probe_and_attach_sub()
1350 iface->subdev = NULL; in usb_probe_and_attach_sub()
1352 if (device_delete_child(udev->parent_dev, dev)) { in usb_probe_and_attach_sub()
1361 if (uaa->temp_dev == NULL) { in usb_probe_and_attach_sub()
1363 uaa->temp_dev = device_add_child(udev->parent_dev, NULL, DEVICE_UNIT_ANY); in usb_probe_and_attach_sub()
1364 if (uaa->temp_dev == NULL) { in usb_probe_and_attach_sub()
1365 device_printf(udev->parent_dev, in usb_probe_and_attach_sub()
1369 device_set_ivars(uaa->temp_dev, uaa); in usb_probe_and_attach_sub()
1370 device_quiet(uaa->temp_dev); in usb_probe_and_attach_sub()
1376 iface->subdev = uaa->temp_dev; in usb_probe_and_attach_sub()
1378 if (device_probe_and_attach(iface->subdev) == 0) { in usb_probe_and_attach_sub()
1383 uaa->temp_dev = NULL; in usb_probe_and_attach_sub()
1384 device_set_ivars(iface->subdev, NULL); in usb_probe_and_attach_sub()
1386 if (udev->flags.peer_suspended) { in usb_probe_and_attach_sub()
1387 err = DEVICE_SUSPEND(iface->subdev); in usb_probe_and_attach_sub()
1389 device_printf(iface->subdev, "Suspend failed\n"); in usb_probe_and_attach_sub()
1394 iface->subdev = NULL; in usb_probe_and_attach_sub()
1399 /*------------------------------------------------------------------------*
1408 *------------------------------------------------------------------------*/
1421 iface->parent_iface_index = parent_index; in usbd_set_parent_iface()
1430 uaa->device = udev; in usb_init_attach_arg()
1431 uaa->usb_mode = udev->flags.usb_mode; in usb_init_attach_arg()
1432 uaa->port = udev->port_no; in usb_init_attach_arg()
1433 uaa->dev_state = UAA_DEV_READY; in usb_init_attach_arg()
1435 uaa->info.idVendor = UGETW(udev->ddesc.idVendor); in usb_init_attach_arg()
1436 uaa->info.idProduct = UGETW(udev->ddesc.idProduct); in usb_init_attach_arg()
1437 uaa->info.bcdDevice = UGETW(udev->ddesc.bcdDevice); in usb_init_attach_arg()
1438 uaa->info.bDeviceClass = udev->ddesc.bDeviceClass; in usb_init_attach_arg()
1439 uaa->info.bDeviceSubClass = udev->ddesc.bDeviceSubClass; in usb_init_attach_arg()
1440 uaa->info.bDeviceProtocol = udev->ddesc.bDeviceProtocol; in usb_init_attach_arg()
1441 uaa->info.bConfigIndex = udev->curr_config_index; in usb_init_attach_arg()
1442 uaa->info.bConfigNum = udev->curr_config_no; in usb_init_attach_arg()
1445 /*------------------------------------------------------------------------*
1454 *------------------------------------------------------------------------*/
1468 /* Prevent re-enumeration */ in usb_probe_and_attach()
1471 if (udev->curr_config_index == USB_UNCONFIG_INDEX) { in usb_probe_and_attach()
1472 /* do nothing - no configuration has been set */ in usb_probe_and_attach()
1520 if (iface->idesc == NULL) { in usb_probe_and_attach()
1527 iface->idesc->bInterfaceClass; in usb_probe_and_attach()
1529 iface->idesc->bInterfaceSubClass; in usb_probe_and_attach()
1531 iface->idesc->bInterfaceProtocol; in usb_probe_and_attach()
1534 iface->idesc->bInterfaceNumber; in usb_probe_and_attach()
1553 if (device_delete_child(udev->parent_dev, uaa.temp_dev)) in usb_probe_and_attach()
1563 /*------------------------------------------------------------------------*
1568 *------------------------------------------------------------------------*/
1591 /*------------------------------------------------------------------------*
1599 *------------------------------------------------------------------------*/
1612 sx_assert(&udev->sr_sx, SA_LOCKED); in usb_suspend_resume()
1614 USB_BUS_LOCK(udev->bus); in usb_suspend_resume()
1616 if (udev->flags.peer_suspended == do_suspend) { in usb_suspend_resume()
1617 USB_BUS_UNLOCK(udev->bus); in usb_suspend_resume()
1621 udev->flags.peer_suspended = do_suspend; in usb_suspend_resume()
1622 USB_BUS_UNLOCK(udev->bus); in usb_suspend_resume()
1632 usb_suspend_resume_sub(udev, iface->subdev, do_suspend); in usb_suspend_resume()
1637 /*------------------------------------------------------------------------*
1641 *------------------------------------------------------------------------*/
1646 struct usb_device *udev = pm->udev; in usbd_clear_stall_proc()
1649 USB_BUS_UNLOCK(udev->bus); in usbd_clear_stall_proc()
1650 USB_MTX_LOCK(&udev->device_mtx); in usbd_clear_stall_proc()
1653 usbd_transfer_start(udev->ctrl_xfer[1]); in usbd_clear_stall_proc()
1656 USB_MTX_UNLOCK(&udev->device_mtx); in usbd_clear_stall_proc()
1657 USB_BUS_LOCK(udev->bus); in usbd_clear_stall_proc()
1660 /*------------------------------------------------------------------------*
1664 *------------------------------------------------------------------------*/
1675 * It appears that some string-less USB chips will crash and in usb_get_langid()
1687 scratch_ptr = udev->scratch.data; in usb_get_langid()
1689 if (udev->flags.no_strings) { in usb_get_langid()
1691 } else if (udev->ddesc.iManufacturer || in usb_get_langid()
1692 udev->ddesc.iProduct || in usb_get_langid()
1693 udev->ddesc.iSerialNumber) { in usb_get_langid()
1702 udev->flags.no_strings = 1; in usb_get_langid()
1732 udev->langid = langid; in usb_get_langid()
1739 /*------------------------------------------------------------------------*
1743 * when a new device has been put in the powered state, but not yet in
1750 *------------------------------------------------------------------------*/
1779 (device_index != bus->devices_max) && in usb_alloc_device()
1780 (bus->devices[device_index] != NULL); in usb_alloc_device()
1783 if (device_index == bus->devices_max) { in usb_alloc_device()
1784 device_printf(bus->bdev, in usb_alloc_device()
1790 device_printf(bus->bdev, in usb_alloc_device()
1800 /* initialise our SX-lock */ in usb_alloc_device()
1801 sx_init_flags(&udev->enum_sx, "USB config SX lock", SX_DUPOK); in usb_alloc_device()
1802 sx_init_flags(&udev->sr_sx, "USB suspend and resume SX lock", SX_NOWITNESS); in usb_alloc_device()
1803 sx_init_flags(&udev->ctrl_sx, "USB control transfer SX lock", SX_DUPOK); in usb_alloc_device()
1805 cv_init(&udev->ctrlreq_cv, "WCTRL"); in usb_alloc_device()
1806 cv_init(&udev->ref_cv, "UGONE"); in usb_alloc_device()
1809 mtx_init(&udev->device_mtx, "USB device mutex", NULL, MTX_DEF); in usb_alloc_device()
1812 udev->cs_msg[0].hdr.pm_callback = &usbd_clear_stall_proc; in usb_alloc_device()
1813 udev->cs_msg[0].udev = udev; in usb_alloc_device()
1814 udev->cs_msg[1].hdr.pm_callback = &usbd_clear_stall_proc; in usb_alloc_device()
1815 udev->cs_msg[1].udev = udev; in usb_alloc_device()
1818 udev->parent_hub = parent_hub; in usb_alloc_device()
1819 udev->parent_dev = parent_dev; in usb_alloc_device()
1820 udev->port_index = port_index; in usb_alloc_device()
1821 udev->port_no = port_no; in usb_alloc_device()
1822 udev->depth = depth; in usb_alloc_device()
1823 udev->bus = bus; in usb_alloc_device()
1824 udev->address = USB_START_ADDR; /* default value */ in usb_alloc_device()
1825 udev->plugtime = (usb_ticks_t)ticks; in usb_alloc_device()
1831 udev->power_mode = usbd_filter_power_mode(udev, USB_POWER_MODE_ON); in usb_alloc_device()
1832 udev->pwr_save.last_xfer_time = ticks; in usb_alloc_device()
1834 udev->refcount = 1; in usb_alloc_device()
1837 udev->ctrl_ep_desc.bLength = sizeof(udev->ctrl_ep_desc); in usb_alloc_device()
1838 udev->ctrl_ep_desc.bDescriptorType = UDESC_ENDPOINT; in usb_alloc_device()
1839 udev->ctrl_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT; in usb_alloc_device()
1840 udev->ctrl_ep_desc.bmAttributes = UE_CONTROL; in usb_alloc_device()
1841 udev->ctrl_ep_desc.wMaxPacketSize[0] = USB_MAX_IPACKET; in usb_alloc_device()
1842 udev->ctrl_ep_desc.wMaxPacketSize[1] = 0; in usb_alloc_device()
1843 udev->ctrl_ep_desc.bInterval = 0; in usb_alloc_device()
1846 udev->ctrl_ep_comp_desc.bLength = sizeof(udev->ctrl_ep_comp_desc); in usb_alloc_device()
1847 udev->ctrl_ep_comp_desc.bDescriptorType = UDESC_ENDPOINT_SS_COMP; in usb_alloc_device()
1849 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET; in usb_alloc_device()
1851 udev->speed = speed; in usb_alloc_device()
1852 udev->flags.usb_mode = mode; in usb_alloc_device()
1857 hub = udev->parent_hub; in usb_alloc_device()
1859 while (hub) { in usb_alloc_device()
1860 if (hub->speed == USB_SPEED_HIGH) { in usb_alloc_device()
1861 udev->hs_hub_addr = hub->address; in usb_alloc_device()
1862 udev->parent_hs_hub = hub; in usb_alloc_device()
1863 udev->hs_port_no = adev->port_no; in usb_alloc_device()
1867 hub = hub->parent_hub; in usb_alloc_device()
1872 &udev->ctrl_ep_desc, in usb_alloc_device()
1873 &udev->ctrl_ep_comp_desc, in usb_alloc_device()
1874 &udev->ctrl_ep); in usb_alloc_device()
1877 udev->device_index = device_index; in usb_alloc_device()
1881 snprintf(udev->ugen_name, sizeof(udev->ugen_name), in usb_alloc_device()
1882 USB_GENERIC_NAME "%u.%u", device_get_unit(bus->bdev), in usb_alloc_device()
1884 SLIST_INIT(&udev->pd_list); in usb_alloc_device()
1887 udev->ctrl_dev = usb_make_dev(udev, NULL, 0, 0, in usb_alloc_device()
1891 if (udev->ctrl_dev != NULL) in usb_alloc_device()
1892 make_dev_alias(udev->ctrl_dev->cdev, "%s", udev->ugen_name); in usb_alloc_device()
1895 if (bus->methods->device_init != NULL) { in usb_alloc_device()
1896 err = (bus->methods->device_init) (udev); in usb_alloc_device()
1904 /* set powered device state after device init is complete */ in usb_alloc_device()
1907 if (udev->flags.usb_mode == USB_MODE_HOST) { in usb_alloc_device()
1914 if (udev->address == USB_START_ADDR) in usb_alloc_device()
1915 udev->address = device_index; in usb_alloc_device()
1918 * We ignore any set-address errors, hence there are in usb_alloc_device()
1922 * to the subsequent get-descriptor at the new in usb_alloc_device()
1923 * address, then we know that the set-address command in usb_alloc_device()
1928 "(%s, ignored)\n", udev->address, in usb_alloc_device()
1932 /* We are not self powered */ in usb_alloc_device()
1933 udev->flags.self_powered = 0; in usb_alloc_device()
1936 udev->curr_config_no = USB_UNCONFIG_NO; in usb_alloc_device()
1937 udev->curr_config_index = USB_UNCONFIG_INDEX; in usb_alloc_device()
1942 DPRINTFN(0, "setting up USB template failed - " in usb_alloc_device()
1970 udev->flags.uq_bus_powered = 1; in usb_alloc_device()
1973 udev->flags.no_strings = 1; in usb_alloc_device()
1978 /* assume 100mA bus powered for now. Changed when configured. */ in usb_alloc_device()
1979 udev->power = USB_MIN_POWER; in usb_alloc_device()
1983 if (udev->flags.usb_mode == USB_MODE_DEVICE) { in usb_alloc_device()
2021 if (udev->ddesc.bNumConfigurations != 0) { in usb_alloc_device()
2024 /* XXX try to re-enumerate the device */ in usb_alloc_device()
2031 config_index, usbd_errstr(err), udev->port_no, in usb_alloc_device()
2032 udev->address); in usb_alloc_device()
2041 if (!config_quirk && config_index + 1 < udev->ddesc.bNumConfigurations) { in usb_alloc_device()
2042 if ((udev->cdesc->bNumInterface < 2) && in usb_alloc_device()
2043 usbd_get_no_descriptors(udev->cdesc, UDESC_ENDPOINT) == 0) { in usb_alloc_device()
2053 * auto-install disk there: in usb_alloc_device()
2056 DPRINTFN(0, "Found possible auto-install " in usb_alloc_device()
2087 udev->address, udev, udev->parent_hub); in usb_alloc_device()
2089 /* register our device - we are ready */ in usb_alloc_device()
2091 parent_hub->hub->ports + port_index : NULL, udev, device_index); in usb_alloc_device()
2095 udev->ugen_symlink = usb_alloc_symlink(udev->ugen_name); in usb_alloc_device()
2098 printf("%s: <%s %s> at %s\n", udev->ugen_name, in usb_alloc_device()
2100 device_get_nameunit(udev->bus->bdev)); in usb_alloc_device()
2129 pd->bus_index = device_get_unit(udev->bus->bdev); in usb_make_dev()
2130 pd->dev_index = udev->device_index; in usb_make_dev()
2131 pd->ep_addr = ep; in usb_make_dev()
2132 pd->fifo_index = fi; in usb_make_dev()
2133 pd->mode = rwmode; in usb_make_dev()
2139 pd->bus_index, pd->dev_index, pd->ep_addr); in usb_make_dev()
2150 if (make_dev_s(&args, &pd->cdev, "%s", devname) != 0) { in usb_make_dev()
2162 pd->bus_index, pd->dev_index); in usb_destroy_dev_sync()
2168 destroy_dev(pd->cdev); in usb_destroy_dev_sync()
2182 bus = devclass_get_softc(usb_devclass_ptr, pd->bus_index); in usb_destroy_dev()
2190 /* make sure we can re-use the device name */ in usb_destroy_dev()
2191 delist_dev(pd->cdev); in usb_destroy_dev()
2194 SLIST_INSERT_HEAD(&bus->pd_cleanup_list, pd, pd_next); in usb_destroy_dev()
2197 &bus->cleanup_msg[0], &bus->cleanup_msg[1]); in usb_destroy_dev()
2211 KASSERT(SLIST_FIRST(&udev->pd_list) == NULL, ("stale cdev entries")); in usb_cdev_create()
2232 while ((desc = usb_desc_foreach(cd, desc))) { in usb_cdev_create()
2234 if ((desc->bDescriptorType == UDESC_ENDPOINT) && in usb_cdev_create()
2235 (desc->bLength >= sizeof(*ed))) { in usb_cdev_create()
2239 ep = ed->bEndpointAddress; in usb_cdev_create()
2258 SLIST_INSERT_HEAD(&udev->pd_list, pd, pd_next); in usb_cdev_create()
2269 while ((pd = SLIST_FIRST(&udev->pd_list)) != NULL) { in usb_cdev_free()
2270 KASSERT(pd->cdev->si_drv1 == pd, ("privdata corrupt")); in usb_cdev_free()
2272 SLIST_REMOVE(&udev->pd_list, pd, usb_fs_privdata, pd_next); in usb_cdev_free()
2279 /*------------------------------------------------------------------------*
2286 *------------------------------------------------------------------------*/
2295 DPRINTFN(4, "udev=%p port=%d\n", udev, udev->port_no); in usb_free_device()
2297 bus = udev->bus; in usb_free_device()
2308 printf("%s: <%s %s> at %s (disconnected)\n", udev->ugen_name, in usb_free_device()
2310 device_get_nameunit(bus->bdev)); in usb_free_device()
2314 if (udev->ugen_symlink) { in usb_free_device()
2315 usb_free_symlink(udev->ugen_symlink); in usb_free_device()
2316 udev->ugen_symlink = NULL; in usb_free_device()
2319 usb_destroy_dev(udev->ctrl_dev); in usb_free_device()
2322 if (udev->flags.usb_mode == USB_MODE_DEVICE) { in usb_free_device()
2324 usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX); in usb_free_device()
2331 usb_bus_port_set_device(bus, udev->parent_hub ? in usb_free_device()
2332 udev->parent_hub->hub->ports + udev->port_index : NULL, in usb_free_device()
2336 usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX); in usb_free_device()
2342 * Make sure that our clear-stall messages are not queued in usb_free_device()
2345 USB_BUS_LOCK(udev->bus); in usb_free_device()
2346 usb_proc_mwait(USB_BUS_CS_PROC(udev->bus), in usb_free_device()
2347 &udev->cs_msg[0], &udev->cs_msg[1]); in usb_free_device()
2348 USB_BUS_UNLOCK(udev->bus); in usb_free_device()
2353 sx_destroy(&udev->enum_sx); in usb_free_device()
2354 sx_destroy(&udev->sr_sx); in usb_free_device()
2355 sx_destroy(&udev->ctrl_sx); in usb_free_device()
2357 cv_destroy(&udev->ctrlreq_cv); in usb_free_device()
2358 cv_destroy(&udev->ref_cv); in usb_free_device()
2360 mtx_destroy(&udev->device_mtx); in usb_free_device()
2362 KASSERT(SLIST_FIRST(&udev->pd_list) == NULL, ("leaked cdev entries")); in usb_free_device()
2366 if (bus->methods->device_uninit != NULL) in usb_free_device()
2367 (bus->methods->device_uninit) (udev); in usb_free_device()
2370 free(udev->serial, M_USB); in usb_free_device()
2371 free(udev->manufacturer, M_USB); in usb_free_device()
2372 free(udev->product, M_USB); in usb_free_device()
2376 /*------------------------------------------------------------------------*
2385 *------------------------------------------------------------------------*/
2389 struct usb_interface *iface = udev->ifaces + iface_index; in usbd_get_iface()
2391 if (iface_index >= udev->ifaces_max) in usbd_get_iface()
2396 /*------------------------------------------------------------------------*
2410 *------------------------------------------------------------------------*/
2436 while ((desc = usb_desc_foreach(cd, desc))) { in usbd_find_descriptor()
2437 if (desc->bDescriptorType == UDESC_INTERFACE) { in usbd_find_descriptor()
2440 if (((desc->bDescriptorType & type_mask) == type) && in usbd_find_descriptor()
2441 ((desc->bDescriptorSubtype & subtype_mask) == subtype)) { in usbd_find_descriptor()
2448 /*------------------------------------------------------------------------*
2455 *------------------------------------------------------------------------*/
2459 struct usb_device_descriptor *udd = &udev->ddesc; in usb_devinfo()
2463 bcdUSB = UGETW(udd->bcdUSB); in usb_devinfo()
2464 bcdDevice = UGETW(udd->bcdDevice); in usb_devinfo()
2466 if (udd->bDeviceClass != 0xFF) { in usb_devinfo()
2471 udd->bDeviceClass, udd->bDeviceSubClass, in usb_devinfo()
2474 udev->address); in usb_devinfo()
2482 udev->address); in usb_devinfo()
2507 struct usb_device_descriptor *udd = &udev->ddesc; in usb_set_device_strings()
2520 temp_ptr = (char *)udev->scratch.data; in usb_set_device_strings()
2521 temp_size = sizeof(udev->scratch.data); in usb_set_device_strings()
2523 vendor_id = UGETW(udd->idVendor); in usb_set_device_strings()
2524 product_id = UGETW(udd->idProduct); in usb_set_device_strings()
2527 free(udev->serial, M_USB); in usb_set_device_strings()
2528 free(udev->manufacturer, M_USB); in usb_set_device_strings()
2529 free(udev->product, M_USB); in usb_set_device_strings()
2532 udev->serial = NULL; in usb_set_device_strings()
2533 udev->manufacturer = NULL; in usb_set_device_strings()
2534 udev->product = NULL; in usb_set_device_strings()
2538 udev->ddesc.iSerialNumber); in usb_set_device_strings()
2539 udev->serial = strdup(temp_ptr, M_USB); in usb_set_device_strings()
2543 udev->ddesc.iManufacturer); in usb_set_device_strings()
2546 udev->manufacturer = strdup(temp_ptr, M_USB); in usb_set_device_strings()
2550 udev->ddesc.iProduct); in usb_set_device_strings()
2553 udev->product = strdup(temp_ptr, M_USB); in usb_set_device_strings()
2556 if (udev->manufacturer == NULL || udev->product == NULL) { in usb_set_device_strings()
2557 for (kdp = usb_knowndevs; kdp->vendorname != NULL; kdp++) { in usb_set_device_strings()
2558 if (kdp->vendor == vendor_id && in usb_set_device_strings()
2559 (kdp->product == product_id || in usb_set_device_strings()
2560 (kdp->flags & USB_KNOWNDEV_NOPROD) != 0)) in usb_set_device_strings()
2563 if (kdp->vendorname != NULL) { in usb_set_device_strings()
2565 if (udev->manufacturer == NULL) { in usb_set_device_strings()
2566 udev->manufacturer = strdup(kdp->vendorname, in usb_set_device_strings()
2569 if (udev->product == NULL && in usb_set_device_strings()
2570 (kdp->flags & USB_KNOWNDEV_NOPROD) == 0) { in usb_set_device_strings()
2571 udev->product = strdup(kdp->productname, in usb_set_device_strings()
2578 if (udev->manufacturer == NULL) { in usb_set_device_strings()
2580 udev->manufacturer = strdup(temp_ptr, M_USB); in usb_set_device_strings()
2582 if (udev->product == NULL) { in usb_set_device_strings()
2584 udev->product = strdup(temp_ptr, M_USB); in usb_set_device_strings()
2598 return (udev->flags.usb_mode); in usbd_get_mode()
2608 return (udev->speed); in usbd_get_speed()
2615 switch (udev->speed) { in usbd_get_isoc_fps()
2629 return (&udev->ddesc); in usbd_get_device_descriptor()
2637 return (udev->cdesc); in usbd_get_config_descriptor()
2640 /*------------------------------------------------------------------------*
2641 * usb_test_quirk - test a device for a given quirk
2646 *------------------------------------------------------------------------*/
2659 if (uaa->device->autoQuirk[x] == quirk) in usb_test_quirk()
2665 found = (usb_test_quirk_p) (&uaa->info, quirk); in usb_test_quirk()
2675 return (iface->idesc); in usbd_get_interface_descriptor()
2681 return (iface->alt_index); in usbd_get_interface_altindex()
2687 return ((uint8_t)device_get_unit(udev->bus->bdev)); in usbd_get_bus_index()
2693 return (udev->device_index); in usbd_get_device_index()
2724 udev->ugen_name, in usb_notify_addq()
2725 udev->ugen_name, in usb_notify_addq()
2727 UGETW(udev->ddesc.idVendor), in usb_notify_addq()
2728 UGETW(udev->ddesc.idProduct), in usb_notify_addq()
2729 udev->ddesc.bDeviceClass, in usb_notify_addq()
2730 udev->ddesc.bDeviceSubClass, in usb_notify_addq()
2732 UGETW(udev->ddesc.bcdDevice), in usb_notify_addq()
2733 (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device", in usb_notify_addq()
2734 udev->port_no in usb_notify_addq()
2736 , udev->parent_hub != NULL ? in usb_notify_addq()
2737 udev->parent_hub->ugen_name : in usb_notify_addq()
2738 device_get_nameunit(device_get_parent(udev->bus->bdev)) in usb_notify_addq()
2750 if (iface->idesc == NULL) in usb_notify_addq()
2772 udev->ugen_name, in usb_notify_addq()
2773 udev->ugen_name, in usb_notify_addq()
2775 UGETW(udev->ddesc.idVendor), in usb_notify_addq()
2776 UGETW(udev->ddesc.idProduct), in usb_notify_addq()
2777 udev->ddesc.bDeviceClass, in usb_notify_addq()
2778 udev->ddesc.bDeviceSubClass, in usb_notify_addq()
2780 UGETW(udev->ddesc.bcdDevice), in usb_notify_addq()
2781 (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device", in usb_notify_addq()
2782 iface->idesc->bInterfaceNumber, in usb_notify_addq()
2783 iface->idesc->bNumEndpoints, in usb_notify_addq()
2784 iface->idesc->bInterfaceClass, in usb_notify_addq()
2785 iface->idesc->bInterfaceSubClass, in usb_notify_addq()
2786 iface->idesc->bInterfaceProtocol); in usb_notify_addq()
2795 /*------------------------------------------------------------------------*
2806 *------------------------------------------------------------------------*/
2818 f = udev->fifo[i]; in usb_fifo_free_wrap()
2823 if (iface_index == f->iface_index) { in usb_fifo_free_wrap()
2824 if (f->methods != &usb_ugen_methods) { in usb_fifo_free_wrap()
2826 * Don't free any non-generic FIFOs in in usb_fifo_free_wrap()
2831 if ((f->dev_ep_index == 0) && in usb_fifo_free_wrap()
2832 (f->fs_ep_max == 0)) { in usb_fifo_free_wrap()
2837 if ((f->methods == &usb_ugen_methods) && in usb_fifo_free_wrap()
2838 (f->dev_ep_index == 0) && in usb_fifo_free_wrap()
2840 (f->fs_ep_max == 0)) { in usb_fifo_free_wrap()
2854 /*------------------------------------------------------------------------*
2860 *------------------------------------------------------------------------*/
2866 cdp = udev->cdesc; in usb_peer_can_wakeup()
2867 if ((cdp != NULL) && (udev->flags.usb_mode == USB_MODE_HOST)) { in usb_peer_can_wakeup()
2868 return (cdp->bmAttributes & UC_REMOTE_WAKEUP); in usb_peer_can_wakeup()
2879 DPRINTF("udev %p state %s -> %s\n", udev, in usb_set_device_state()
2880 usb_statestr(udev->state), usb_statestr(state)); in usb_set_device_state()
2885 udev->state = state; in usb_set_device_state()
2889 if (udev->bus->methods->device_state_change != NULL) in usb_set_device_state()
2890 (udev->bus->methods->device_state_change) (udev); in usb_set_device_state()
2898 return (udev->state); in usb_get_device_state()
2904 return (udev->state > USB_STATE_DETACHED); in usbd_device_attached()
2915 if (sx_xlocked(&udev->enum_sx)) in usbd_enum_lock()
2918 sx_xlock(&udev->enum_sx); in usbd_enum_lock()
2919 sx_xlock(&udev->sr_sx); in usbd_enum_lock()
2937 if (sx_xlocked(&udev->enum_sx)) in usbd_enum_lock_sig()
2939 if (sx_xlock_sig(&udev->enum_sx)) in usbd_enum_lock_sig()
2941 if (sx_xlock_sig(&udev->sr_sx)) { in usbd_enum_lock_sig()
2942 sx_xunlock(&udev->enum_sx); in usbd_enum_lock_sig()
2956 sx_xunlock(&udev->enum_sx); in usbd_enum_unlock()
2957 sx_xunlock(&udev->sr_sx); in usbd_enum_unlock()
2965 sx_xlock(&udev->sr_sx); in usbd_sr_lock()
2980 sx_xunlock(&udev->sr_sx); in usbd_sr_unlock()
2991 return (sx_xlocked(&udev->enum_sx)); in usbd_enum_is_locked()
3002 if (sx_xlocked(&udev->ctrl_sx)) in usbd_ctrl_lock()
3004 sx_xlock(&udev->ctrl_sx); in usbd_ctrl_lock()
3008 * control transfer will timeout if the device is suspended! in usbd_ctrl_lock()
3018 sx_xunlock(&udev->ctrl_sx); in usbd_ctrl_unlock()
3029 * The following function is used to set the per-interface specific
3046 if (iface->pnpinfo != NULL) { in usbd_set_pnpinfo()
3047 free(iface->pnpinfo, M_USBDEV); in usbd_set_pnpinfo()
3048 iface->pnpinfo = NULL; in usbd_set_pnpinfo()
3054 iface->pnpinfo = strdup(pnpinfo, M_USBDEV); in usbd_set_pnpinfo()
3055 if (iface->pnpinfo == NULL) in usbd_set_pnpinfo()
3067 if (udev->autoQuirk[x] == 0 || in usbd_add_dynamic_quirk()
3068 udev->autoQuirk[x] == quirk) { in usbd_add_dynamic_quirk()
3069 udev->autoQuirk[x] = quirk; in usbd_add_dynamic_quirk()
3088 /* Prevent re-enumeration */ in usbd_set_endpoint_mode()
3091 if (udev->bus->methods->set_endpoint_mode != NULL) { in usbd_set_endpoint_mode()
3092 error = (udev->bus->methods->set_endpoint_mode) ( in usbd_set_endpoint_mode()
3101 ep->ep_mode = ep_mode; in usbd_set_endpoint_mode()
3111 return (ep->ep_mode); in usbd_get_endpoint_mode()