Lines Matching +full:usb +full:- +full:hub
1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/usb/core/usb.c
6 * (C) Copyright Johannes Erdfelt 1999-2001
9 * (C) Copyright Deti Fliegl 1999 (new USB architecture)
11 * (C) Copyright David Brownell 2000-2004
14 * (C) Copyright Greg Kroah-Hartman 2002-2003
20 * generic USB things that the real drivers can use..
22 * Think of this as a "USB library" rather than anything else,
35 #include <linux/usb.h>
36 #include <linux/usb/hcd.h>
40 #include <linux/usb/of.h>
45 #include <linux/dma-mapping.h>
47 #include "hub.h"
51 static bool nousb; /* Disable USB when built into kernel image */
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
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.
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
347 * that share the USB major number.
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
387 * @fn: callback function to be called for each USB device
389 * Iterate over all USB devices and call @fn for each, passing it @data. If it
402 * usb_release_dev - free a usb device structure when all users of it are finished.
405 * Will be called only by the device core when all users of this usb device are
414 hcd = bus_to_hcd(udev->bus); in usb_release_dev()
418 of_node_put(dev->of_node); in usb_release_dev()
420 kfree(udev->product); in usb_release_dev()
421 kfree(udev->manufacturer); in usb_release_dev()
422 kfree(udev->serial); in usb_release_dev()
432 if (add_uevent_var(env, "BUSNUM=%03d", usb_dev->bus->busnum)) in usb_dev_uevent()
433 return -ENOMEM; in usb_dev_uevent()
435 if (add_uevent_var(env, "DEVNUM=%03d", usb_dev->devnum)) in usb_dev_uevent()
436 return -ENOMEM; in usb_dev_uevent()
443 /* USB device Power-Management thunks.
444 * There's no need to distinguish here between quiescing a USB device
447 * USB interfaces there's no difference at all.
514 return kasprintf(GFP_KERNEL, "bus/usb/%03d/%03d", in usb_devnode()
515 usb_dev->bus->busnum, usb_dev->devnum); in usb_devnode()
533 return hcd->wireless; in usb_bus_is_wusb()
538 struct usb_hub *hub; in usb_dev_authorized() local
540 if (!dev->parent) in usb_dev_authorized()
541 return true; /* Root hub always ok [and always wired] */ in usb_dev_authorized()
543 switch (hcd->dev_policy) { in usb_dev_authorized()
552 hub = usb_hub_to_struct_hub(dev->parent); in usb_dev_authorized()
553 return hub->ports[dev->portnum - 1]->connect_type == in usb_dev_authorized()
559 * usb_alloc_dev - usb device constructor (usbcore-internal)
560 * @parent: hub to which device is connected; null to allocate a root hub
562 * @port1: one-based index of port; ignored for root hubs
566 * Only hub drivers (including virtual root hub drivers for host
569 * This call may not be used in a non-sleeping context.
571 * Return: On success, a pointer to the allocated usb device. %NULL on
591 if (usb_hcd->driver->alloc_dev && parent && in usb_alloc_dev()
592 !usb_hcd->driver->alloc_dev(usb_hcd, dev)) { in usb_alloc_dev()
598 device_initialize(&dev->dev); in usb_alloc_dev()
599 dev->dev.bus = &usb_bus_type; in usb_alloc_dev()
600 dev->dev.type = &usb_device_type; in usb_alloc_dev()
601 dev->dev.groups = usb_device_groups; in usb_alloc_dev()
602 set_dev_node(&dev->dev, dev_to_node(bus->sysdev)); in usb_alloc_dev()
603 dev->state = USB_STATE_ATTACHED; in usb_alloc_dev()
604 dev->lpm_disable_count = 1; in usb_alloc_dev()
605 atomic_set(&dev->urbnum, 0); in usb_alloc_dev()
607 INIT_LIST_HEAD(&dev->ep0.urb_list); in usb_alloc_dev()
608 dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE; in usb_alloc_dev()
609 dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT; in usb_alloc_dev()
611 usb_enable_endpoint(dev, &dev->ep0, false); in usb_alloc_dev()
612 dev->can_submit = 1; in usb_alloc_dev()
616 * string is a path along hub ports, from the root. Each device's in usb_alloc_dev()
617 * dev->devpath will be stable until USB is re-cabled, and hubs in usb_alloc_dev()
619 * as stable: bus->busnum changes easily from modprobe order, in usb_alloc_dev()
623 dev->devpath[0] = '0'; in usb_alloc_dev()
624 dev->route = 0; in usb_alloc_dev()
626 dev->dev.parent = bus->controller; in usb_alloc_dev()
627 device_set_of_node_from_dev(&dev->dev, bus->sysdev); in usb_alloc_dev()
628 dev_set_name(&dev->dev, "usb%d", bus->busnum); in usb_alloc_dev()
631 /* match any labeling on the hubs; it's one-based */ in usb_alloc_dev()
632 if (parent->devpath[0] == '0') { in usb_alloc_dev()
633 snprintf(dev->devpath, sizeof dev->devpath, in usb_alloc_dev()
636 dev->route = 0; in usb_alloc_dev()
638 snprintf(dev->devpath, sizeof dev->devpath, in usb_alloc_dev()
639 "%s.%d", parent->devpath, port1); in usb_alloc_dev()
642 dev->route = parent->route + in usb_alloc_dev()
643 (port1 << ((parent->level - 1)*4)); in usb_alloc_dev()
645 dev->route = parent->route + in usb_alloc_dev()
646 (15 << ((parent->level - 1)*4)); in usb_alloc_dev()
649 dev->dev.parent = &parent->dev; in usb_alloc_dev()
650 dev_set_name(&dev->dev, "%d-%s", bus->busnum, dev->devpath); in usb_alloc_dev()
652 if (!parent->parent) { in usb_alloc_dev()
653 /* device under root hub's port */ in usb_alloc_dev()
657 dev->dev.of_node = usb_of_get_device_node(parent, raw_port); in usb_alloc_dev()
659 /* hub driver sets up TT records */ in usb_alloc_dev()
662 dev->portnum = port1; in usb_alloc_dev()
663 dev->bus = bus; in usb_alloc_dev()
664 dev->parent = parent; in usb_alloc_dev()
665 INIT_LIST_HEAD(&dev->filelist); in usb_alloc_dev()
668 pm_runtime_set_autosuspend_delay(&dev->dev, in usb_alloc_dev()
670 dev->connect_time = jiffies; in usb_alloc_dev()
671 dev->active_duration = -jiffies; in usb_alloc_dev()
674 dev->authorized = usb_dev_authorized(dev, usb_hcd); in usb_alloc_dev()
676 dev->wusb = usb_bus_is_wusb(bus) ? 1 : 0; in usb_alloc_dev()
683 * usb_get_dev - increments the reference count of the usb device structure
688 * Drivers for USB interfaces should normally record such references in
693 * because the USB core guarantees that a usb_device will not be
701 get_device(&dev->dev); in usb_get_dev()
707 * usb_put_dev - release a use of the usb device structure
716 put_device(&dev->dev); in usb_put_dev()
721 * usb_get_intf - increments the reference count of the usb interface structure
726 * Drivers for USB interfaces should normally record such references in
731 * because the USB core guarantees that a usb_interface will not be
739 get_device(&intf->dev); in usb_get_intf()
745 * usb_put_intf - release a use of the usb interface structure
755 put_device(&intf->dev); in usb_put_intf()
760 * usb_intf_get_dma_device - acquire a reference on the usb interface's DMA endpoint
761 * @intf: the usb interface
763 * While a USB device cannot perform DMA operations by itself, many USB
765 * for the given USB interface, if any. The returned device structure must be
770 * Returns: A reference to the usb interface's DMA endpoint; or NULL if none
778 if (!udev->bus) in usb_intf_get_dma_device()
781 dmadev = get_device(udev->bus->sysdev); in usb_intf_get_dma_device()
782 if (!dmadev || !dmadev->dma_mask) { in usb_intf_get_dma_device()
791 /* USB device locking
793 * USB devices and interfaces are locked using the semaphore in their
794 * embedded struct device. The hub driver guarantees that whenever a
796 * USB device locked as well as their particular interface.
799 * time. Only hub-aware drivers that are part of usbcore ever have to
808 * usb_lock_device_for_reset - cautiously acquire the lock for a usb device structure
816 * disconnect; in some drivers (such as usb-storage) the disconnect()
826 if (udev->state == USB_STATE_NOTATTACHED) in usb_lock_device_for_reset()
827 return -ENODEV; in usb_lock_device_for_reset()
828 if (udev->state == USB_STATE_SUSPENDED) in usb_lock_device_for_reset()
829 return -EHOSTUNREACH; in usb_lock_device_for_reset()
830 if (iface && (iface->condition == USB_INTERFACE_UNBINDING || in usb_lock_device_for_reset()
831 iface->condition == USB_INTERFACE_UNBOUND)) in usb_lock_device_for_reset()
832 return -EINTR; in usb_lock_device_for_reset()
839 return -EBUSY; in usb_lock_device_for_reset()
842 if (udev->state == USB_STATE_NOTATTACHED) in usb_lock_device_for_reset()
843 return -ENODEV; in usb_lock_device_for_reset()
844 if (udev->state == USB_STATE_SUSPENDED) in usb_lock_device_for_reset()
845 return -EHOSTUNREACH; in usb_lock_device_for_reset()
846 if (iface && (iface->condition == USB_INTERFACE_UNBINDING || in usb_lock_device_for_reset()
847 iface->condition == USB_INTERFACE_UNBOUND)) in usb_lock_device_for_reset()
848 return -EINTR; in usb_lock_device_for_reset()
855 * usb_get_current_frame_number - return current bus frame number
858 * Return: The current frame number for the USB host controller used
859 * with the given USB device. This can be used when scheduling
874 /*-------------------------------------------------------------------*/
888 if (header->bLength < 2 || header->bLength > size) { in __usb_get_extra_descriptor()
892 header->bDescriptorType, in __usb_get_extra_descriptor()
893 header->bLength); in __usb_get_extra_descriptor()
894 return -1; in __usb_get_extra_descriptor()
897 if (header->bDescriptorType == type && header->bLength >= minsize) { in __usb_get_extra_descriptor()
902 buffer += header->bLength; in __usb_get_extra_descriptor()
903 size -= header->bLength; in __usb_get_extra_descriptor()
905 return -1; in __usb_get_extra_descriptor()
910 * usb_alloc_coherent - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP
917 * cpu-space pointer to a buffer that may be used to perform DMA to the
918 * specified device. Such cpu-space buffers are returned along with the DMA
922 * These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags
927 * architectures where CPU caches are not DMA-coherent. On systems without
928 * bus-snooping caches, these buffers are uncached.
935 if (!dev || !dev->bus) in usb_alloc_coherent()
937 return hcd_buffer_alloc(dev->bus, size, mem_flags, dma); in usb_alloc_coherent()
942 * usb_free_coherent - free memory allocated with usb_alloc_coherent()
955 if (!dev || !dev->bus) in usb_free_coherent()
959 hcd_buffer_free(dev->bus, size, addr, dma); in usb_free_coherent()
973 if (dev->type == &usb_device_type) in usb_bus_notify()
975 else if (dev->type == &usb_if_device_type) in usb_bus_notify()
980 if (dev->type == &usb_device_type) in usb_bus_notify()
982 else if (dev->type == &usb_if_device_type) in usb_bus_notify()
1011 pr_info("%s: USB support disabled\n", usbcore_name); in usb_init()