Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * of.c     The helpers for hcd device tree support
0004  *
0005  * Copyright (C) 2016 Freescale Semiconductor, Inc.
0006  *  Author: Peter Chen <peter.chen@freescale.com>
0007  * Copyright (C) 2017 Johan Hovold <johan@kernel.org>
0008  */
0009 
0010 #include <linux/of.h>
0011 #include <linux/of_platform.h>
0012 #include <linux/usb/of.h>
0013 
0014 /**
0015  * usb_of_get_device_node() - get a USB device node
0016  * @hub: hub to which device is connected
0017  * @port1: one-based index of port
0018  *
0019  * Look up the node of a USB device given its parent hub device and one-based
0020  * port number.
0021  *
0022  * Return: A pointer to the node with incremented refcount if found, or
0023  * %NULL otherwise.
0024  */
0025 struct device_node *usb_of_get_device_node(struct usb_device *hub, int port1)
0026 {
0027     struct device_node *node;
0028     u32 reg;
0029 
0030     for_each_child_of_node(hub->dev.of_node, node) {
0031         if (of_property_read_u32(node, "reg", &reg))
0032             continue;
0033 
0034         if (reg == port1)
0035             return node;
0036     }
0037 
0038     return NULL;
0039 }
0040 EXPORT_SYMBOL_GPL(usb_of_get_device_node);
0041 
0042 /**
0043  * usb_of_has_combined_node() - determine whether a device has a combined node
0044  * @udev: USB device
0045  *
0046  * Determine whether a USB device has a so called combined node which is
0047  * shared with its sole interface. This is the case if and only if the device
0048  * has a node and its descriptors report the following:
0049  *
0050  *  1) bDeviceClass is 0 or 9, and
0051  *  2) bNumConfigurations is 1, and
0052  *  3) bNumInterfaces is 1.
0053  *
0054  * Return: True iff the device has a device node and its descriptors match the
0055  * criteria for a combined node.
0056  */
0057 bool usb_of_has_combined_node(struct usb_device *udev)
0058 {
0059     struct usb_device_descriptor *ddesc = &udev->descriptor;
0060     struct usb_config_descriptor *cdesc;
0061 
0062     if (!udev->dev.of_node)
0063         return false;
0064 
0065     switch (ddesc->bDeviceClass) {
0066     case USB_CLASS_PER_INTERFACE:
0067     case USB_CLASS_HUB:
0068         if (ddesc->bNumConfigurations == 1) {
0069             cdesc = &udev->config->desc;
0070             if (cdesc->bNumInterfaces == 1)
0071                 return true;
0072         }
0073     }
0074 
0075     return false;
0076 }
0077 EXPORT_SYMBOL_GPL(usb_of_has_combined_node);
0078 
0079 /**
0080  * usb_of_get_interface_node() - get a USB interface node
0081  * @udev: USB device of interface
0082  * @config: configuration value
0083  * @ifnum: interface number
0084  *
0085  * Look up the node of a USB interface given its USB device, configuration
0086  * value and interface number.
0087  *
0088  * Return: A pointer to the node with incremented refcount if found, or
0089  * %NULL otherwise.
0090  */
0091 struct device_node *
0092 usb_of_get_interface_node(struct usb_device *udev, u8 config, u8 ifnum)
0093 {
0094     struct device_node *node;
0095     u32 reg[2];
0096 
0097     for_each_child_of_node(udev->dev.of_node, node) {
0098         if (of_property_read_u32_array(node, "reg", reg, 2))
0099             continue;
0100 
0101         if (reg[0] == ifnum && reg[1] == config)
0102             return node;
0103     }
0104 
0105     return NULL;
0106 }
0107 EXPORT_SYMBOL_GPL(usb_of_get_interface_node);