Lines Matching +full:parent +full:- +full:locked

1 // SPDX-License-Identifier: GPL-2.0
6 * (C) Copyright Johannes Erdfelt 1999-2001
11 * (C) Copyright David Brownell 2000-2004
14 * (C) Copyright Greg Kroah-Hartman 2002-2003
45 #include <linux/dma-mapping.h>
118 * usb_find_common_endpoints() -- look up common endpoint descriptors
125 * Search the alternate setting's endpoint descriptors for the first bulk-in,
126 * bulk-out, interrupt-in and interrupt-out endpoints and return them in the
132 * Return: Zero if all requested descriptors were found, or -ENXIO otherwise.
152 for (i = 0; i < alt->desc.bNumEndpoints; ++i) { in usb_find_common_endpoints()
153 epd = &alt->endpoint[i].desc; in usb_find_common_endpoints()
159 return -ENXIO; in usb_find_common_endpoints()
164 * usb_find_common_endpoints_reverse() -- look up common endpoint descriptors
171 * Search the alternate setting's endpoint descriptors for the last bulk-in,
172 * bulk-out, interrupt-in and interrupt-out endpoints and return them in the
178 * Return: Zero if all requested descriptors were found, or -ENXIO otherwise.
198 for (i = alt->desc.bNumEndpoints - 1; i >= 0; --i) { in usb_find_common_endpoints_reverse()
199 epd = &alt->endpoint[i].desc; in usb_find_common_endpoints_reverse()
205 return -ENXIO; in usb_find_common_endpoints_reverse()
210 * usb_find_alt_setting() - Given a configuration, find the alternate setting
230 for (i = 0; i < config->desc.bNumInterfaces; i++) { in usb_find_alt_setting()
231 if (config->intf_cache[i]->altsetting[0].desc.bInterfaceNumber in usb_find_alt_setting()
233 intf_cache = config->intf_cache[i]; in usb_find_alt_setting()
239 for (i = 0; i < intf_cache->num_altsetting; i++) in usb_find_alt_setting()
240 if (intf_cache->altsetting[i].desc.bAlternateSetting == alt_num) in usb_find_alt_setting()
241 return &intf_cache->altsetting[i]; in usb_find_alt_setting()
245 config->desc.bConfigurationValue); in usb_find_alt_setting()
251 * usb_ifnum_to_if - get the interface object with a given interface number
266 * on this device or you have locked the device!
274 struct usb_host_config *config = dev->actconfig; in usb_ifnum_to_if()
279 for (i = 0; i < config->desc.bNumInterfaces; i++) in usb_ifnum_to_if()
280 if (config->interface[i]->altsetting[0] in usb_ifnum_to_if()
282 return config->interface[i]; in usb_ifnum_to_if()
289 * usb_altnum_to_altsetting - get the altsetting structure with a given alternate setting number.
302 * or you have locked the device!
313 for (i = 0; i < intf->num_altsetting; i++) { in usb_altnum_to_altsetting()
314 if (intf->altsetting[i].desc.bAlternateSetting == altnum) in usb_altnum_to_altsetting()
315 return &intf->altsetting[i]; in usb_altnum_to_altsetting()
334 if (dev->driver != arg->drv) in __find_interface()
337 return intf->minor == arg->minor; in __find_interface()
341 * usb_find_interface - find usb_interface pointer for driver and device
357 argb.drv = &drv->drvwrap.driver; in usb_find_interface()
381 return arg->fn(to_usb_device(dev), arg->data); in __each_dev()
385 * usb_for_each_dev - iterate over all USB devices in the system
419 for (i = 0; i < hdev->maxchild; i++) { in __each_hub()
420 ret = arg->fn(&hub->ports[i]->dev, arg->data); in __each_hub()
431 * usb_for_each_port - interate over all USB ports in the system
448 * usb_release_dev - free a usb device structure when all users of it are finished.
460 hcd = bus_to_hcd(udev->bus); in usb_release_dev()
464 of_node_put(dev->of_node); in usb_release_dev()
466 kfree(udev->product); in usb_release_dev()
467 kfree(udev->manufacturer); in usb_release_dev()
468 kfree(udev->serial); in usb_release_dev()
478 if (add_uevent_var(env, "BUSNUM=%03d", usb_dev->bus->busnum)) in usb_dev_uevent()
479 return -ENOMEM; in usb_dev_uevent()
481 if (add_uevent_var(env, "DEVNUM=%03d", usb_dev->devnum)) in usb_dev_uevent()
482 return -ENOMEM; in usb_dev_uevent()
489 /* USB device Power-Management thunks.
561 usb_dev->bus->busnum, usb_dev->devnum); in usb_devnode()
579 return hcd->wireless; in usb_bus_is_wusb()
586 if (!dev->parent) in usb_dev_authorized()
589 switch (hcd->dev_policy) { in usb_dev_authorized()
598 hub = usb_hub_to_struct_hub(dev->parent); in usb_dev_authorized()
599 return hub->ports[dev->portnum - 1]->connect_type == in usb_dev_authorized()
605 * usb_alloc_dev - usb device constructor (usbcore-internal)
606 * @parent: hub to which device is connected; null to allocate a root hub
608 * @port1: one-based index of port; ignored for root hubs
615 * This call may not be used in a non-sleeping context.
620 struct usb_device *usb_alloc_dev(struct usb_device *parent, in usb_alloc_dev() argument
637 if (usb_hcd->driver->alloc_dev && parent && in usb_alloc_dev()
638 !usb_hcd->driver->alloc_dev(usb_hcd, dev)) { in usb_alloc_dev()
644 device_initialize(&dev->dev); in usb_alloc_dev()
645 dev->dev.bus = &usb_bus_type; in usb_alloc_dev()
646 dev->dev.type = &usb_device_type; in usb_alloc_dev()
647 dev->dev.groups = usb_device_groups; in usb_alloc_dev()
648 set_dev_node(&dev->dev, dev_to_node(bus->sysdev)); in usb_alloc_dev()
649 dev->state = USB_STATE_ATTACHED; in usb_alloc_dev()
650 dev->lpm_disable_count = 1; in usb_alloc_dev()
651 atomic_set(&dev->urbnum, 0); in usb_alloc_dev()
653 INIT_LIST_HEAD(&dev->ep0.urb_list); in usb_alloc_dev()
654 dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE; in usb_alloc_dev()
655 dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT; in usb_alloc_dev()
657 usb_enable_endpoint(dev, &dev->ep0, false); in usb_alloc_dev()
658 dev->can_submit = 1; in usb_alloc_dev()
663 * dev->devpath will be stable until USB is re-cabled, and hubs in usb_alloc_dev()
665 * as stable: bus->busnum changes easily from modprobe order, in usb_alloc_dev()
668 if (unlikely(!parent)) { in usb_alloc_dev()
669 dev->devpath[0] = '0'; in usb_alloc_dev()
670 dev->route = 0; in usb_alloc_dev()
672 dev->dev.parent = bus->controller; in usb_alloc_dev()
673 device_set_of_node_from_dev(&dev->dev, bus->sysdev); in usb_alloc_dev()
674 dev_set_name(&dev->dev, "usb%d", bus->busnum); in usb_alloc_dev()
677 /* match any labeling on the hubs; it's one-based */ in usb_alloc_dev()
678 if (parent->devpath[0] == '0') { in usb_alloc_dev()
679 snprintf(dev->devpath, sizeof dev->devpath, in usb_alloc_dev()
682 dev->route = 0; in usb_alloc_dev()
684 snprintf(dev->devpath, sizeof dev->devpath, in usb_alloc_dev()
685 "%s.%d", parent->devpath, port1); in usb_alloc_dev()
688 dev->route = parent->route + in usb_alloc_dev()
689 (port1 << ((parent->level - 1)*4)); in usb_alloc_dev()
691 dev->route = parent->route + in usb_alloc_dev()
692 (15 << ((parent->level - 1)*4)); in usb_alloc_dev()
695 dev->dev.parent = &parent->dev; in usb_alloc_dev()
696 dev_set_name(&dev->dev, "%d-%s", bus->busnum, dev->devpath); in usb_alloc_dev()
698 if (!parent->parent) { in usb_alloc_dev()
703 dev->dev.of_node = usb_of_get_device_node(parent, raw_port); in usb_alloc_dev()
708 dev->portnum = port1; in usb_alloc_dev()
709 dev->bus = bus; in usb_alloc_dev()
710 dev->parent = parent; in usb_alloc_dev()
711 INIT_LIST_HEAD(&dev->filelist); in usb_alloc_dev()
714 pm_runtime_set_autosuspend_delay(&dev->dev, in usb_alloc_dev()
716 dev->connect_time = jiffies; in usb_alloc_dev()
717 dev->active_duration = -jiffies; in usb_alloc_dev()
720 dev->authorized = usb_dev_authorized(dev, usb_hcd); in usb_alloc_dev()
722 dev->wusb = usb_bus_is_wusb(bus) ? 1 : 0; in usb_alloc_dev()
729 * usb_get_dev - increments the reference count of the usb device structure
743 get_device(&dev->dev); in usb_get_dev()
749 * usb_put_dev - release a use of the usb device structure
758 put_device(&dev->dev); in usb_put_dev()
763 * usb_get_intf - increments the reference count of the usb interface structure
777 get_device(&intf->dev); in usb_get_intf()
783 * usb_put_intf - release a use of the usb interface structure
793 put_device(&intf->dev); in usb_put_intf()
798 * usb_intf_get_dma_device - acquire a reference on the usb interface's DMA endpoint
816 if (!udev->bus) in usb_intf_get_dma_device()
819 dmadev = get_device(udev->bus->sysdev); in usb_intf_get_dma_device()
820 if (!dmadev || !dmadev->dma_mask) { in usb_intf_get_dma_device()
831 * USB devices and interfaces are locked using the semaphore in their
834 * USB device locked as well as their particular interface.
836 * Complications arise when several devices are to be locked at the same
837 * time. Only hub-aware drivers that are part of usbcore ever have to
841 * When locking both a device and its parent, always lock the
842 * the parent first.
846 * usb_lock_device_for_reset - cautiously acquire the lock for a usb device structure
847 * @udev: device that's being locked
854 * disconnect; in some drivers (such as usb-storage) the disconnect()
864 if (udev->state == USB_STATE_NOTATTACHED) in usb_lock_device_for_reset()
865 return -ENODEV; in usb_lock_device_for_reset()
866 if (udev->state == USB_STATE_SUSPENDED) in usb_lock_device_for_reset()
867 return -EHOSTUNREACH; in usb_lock_device_for_reset()
868 if (iface && (iface->condition == USB_INTERFACE_UNBINDING || in usb_lock_device_for_reset()
869 iface->condition == USB_INTERFACE_UNBOUND)) in usb_lock_device_for_reset()
870 return -EINTR; in usb_lock_device_for_reset()
877 return -EBUSY; in usb_lock_device_for_reset()
880 if (udev->state == USB_STATE_NOTATTACHED) in usb_lock_device_for_reset()
881 return -ENODEV; in usb_lock_device_for_reset()
882 if (udev->state == USB_STATE_SUSPENDED) in usb_lock_device_for_reset()
883 return -EHOSTUNREACH; in usb_lock_device_for_reset()
884 if (iface && (iface->condition == USB_INTERFACE_UNBINDING || in usb_lock_device_for_reset()
885 iface->condition == USB_INTERFACE_UNBOUND)) in usb_lock_device_for_reset()
886 return -EINTR; in usb_lock_device_for_reset()
893 * usb_get_current_frame_number - return current bus frame number
912 /*-------------------------------------------------------------------*/
926 if (header->bLength < 2 || header->bLength > size) { in __usb_get_extra_descriptor()
930 header->bDescriptorType, in __usb_get_extra_descriptor()
931 header->bLength); in __usb_get_extra_descriptor()
932 return -1; in __usb_get_extra_descriptor()
935 if (header->bDescriptorType == type && header->bLength >= minsize) { in __usb_get_extra_descriptor()
940 buffer += header->bLength; in __usb_get_extra_descriptor()
941 size -= header->bLength; in __usb_get_extra_descriptor()
943 return -1; in __usb_get_extra_descriptor()
948 * usb_alloc_coherent - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP
955 * cpu-space pointer to a buffer that may be used to perform DMA to the
956 * specified device. Such cpu-space buffers are returned along with the DMA
960 * These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags
965 * architectures where CPU caches are not DMA-coherent. On systems without
966 * bus-snooping caches, these buffers are uncached.
973 if (!dev || !dev->bus) in usb_alloc_coherent()
975 return hcd_buffer_alloc(dev->bus, size, mem_flags, dma); in usb_alloc_coherent()
980 * usb_free_coherent - free memory allocated with usb_alloc_coherent()
993 if (!dev || !dev->bus) in usb_free_coherent()
997 hcd_buffer_free(dev->bus, size, addr, dma); in usb_free_coherent()
1011 if (dev->type == &usb_device_type) in usb_bus_notify()
1013 else if (dev->type == &usb_if_device_type) in usb_bus_notify()
1018 if (dev->type == &usb_device_type) in usb_bus_notify()
1020 else if (dev->type == &usb_if_device_type) in usb_bus_notify()