xref: /OK3568_Linux_fs/kernel/drivers/usb/core/of.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * of.c		The helpers for hcd device tree support
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2016 Freescale Semiconductor, Inc.
6*4882a593Smuzhiyun  *	Author: Peter Chen <peter.chen@freescale.com>
7*4882a593Smuzhiyun  * Copyright (C) 2017 Johan Hovold <johan@kernel.org>
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/of.h>
11*4882a593Smuzhiyun #include <linux/of_platform.h>
12*4882a593Smuzhiyun #include <linux/usb/of.h>
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun /**
15*4882a593Smuzhiyun  * usb_of_get_device_node() - get a USB device node
16*4882a593Smuzhiyun  * @hub: hub to which device is connected
17*4882a593Smuzhiyun  * @port1: one-based index of port
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  * Look up the node of a USB device given its parent hub device and one-based
20*4882a593Smuzhiyun  * port number.
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  * Return: A pointer to the node with incremented refcount if found, or
23*4882a593Smuzhiyun  * %NULL otherwise.
24*4882a593Smuzhiyun  */
usb_of_get_device_node(struct usb_device * hub,int port1)25*4882a593Smuzhiyun struct device_node *usb_of_get_device_node(struct usb_device *hub, int port1)
26*4882a593Smuzhiyun {
27*4882a593Smuzhiyun 	struct device_node *node;
28*4882a593Smuzhiyun 	u32 reg;
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun 	for_each_child_of_node(hub->dev.of_node, node) {
31*4882a593Smuzhiyun 		if (of_property_read_u32(node, "reg", &reg))
32*4882a593Smuzhiyun 			continue;
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun 		if (reg == port1)
35*4882a593Smuzhiyun 			return node;
36*4882a593Smuzhiyun 	}
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 	return NULL;
39*4882a593Smuzhiyun }
40*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_of_get_device_node);
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun /**
43*4882a593Smuzhiyun  * usb_of_has_combined_node() - determine whether a device has a combined node
44*4882a593Smuzhiyun  * @udev: USB device
45*4882a593Smuzhiyun  *
46*4882a593Smuzhiyun  * Determine whether a USB device has a so called combined node which is
47*4882a593Smuzhiyun  * shared with its sole interface. This is the case if and only if the device
48*4882a593Smuzhiyun  * has a node and its descriptors report the following:
49*4882a593Smuzhiyun  *
50*4882a593Smuzhiyun  *	1) bDeviceClass is 0 or 9, and
51*4882a593Smuzhiyun  *	2) bNumConfigurations is 1, and
52*4882a593Smuzhiyun  *	3) bNumInterfaces is 1.
53*4882a593Smuzhiyun  *
54*4882a593Smuzhiyun  * Return: True iff the device has a device node and its descriptors match the
55*4882a593Smuzhiyun  * criteria for a combined node.
56*4882a593Smuzhiyun  */
usb_of_has_combined_node(struct usb_device * udev)57*4882a593Smuzhiyun bool usb_of_has_combined_node(struct usb_device *udev)
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun 	struct usb_device_descriptor *ddesc = &udev->descriptor;
60*4882a593Smuzhiyun 	struct usb_config_descriptor *cdesc;
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	if (!udev->dev.of_node)
63*4882a593Smuzhiyun 		return false;
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	switch (ddesc->bDeviceClass) {
66*4882a593Smuzhiyun 	case USB_CLASS_PER_INTERFACE:
67*4882a593Smuzhiyun 	case USB_CLASS_HUB:
68*4882a593Smuzhiyun 		if (ddesc->bNumConfigurations == 1) {
69*4882a593Smuzhiyun 			cdesc = &udev->config->desc;
70*4882a593Smuzhiyun 			if (cdesc->bNumInterfaces == 1)
71*4882a593Smuzhiyun 				return true;
72*4882a593Smuzhiyun 		}
73*4882a593Smuzhiyun 	}
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	return false;
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_of_has_combined_node);
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun /**
80*4882a593Smuzhiyun  * usb_of_get_interface_node() - get a USB interface node
81*4882a593Smuzhiyun  * @udev: USB device of interface
82*4882a593Smuzhiyun  * @config: configuration value
83*4882a593Smuzhiyun  * @ifnum: interface number
84*4882a593Smuzhiyun  *
85*4882a593Smuzhiyun  * Look up the node of a USB interface given its USB device, configuration
86*4882a593Smuzhiyun  * value and interface number.
87*4882a593Smuzhiyun  *
88*4882a593Smuzhiyun  * Return: A pointer to the node with incremented refcount if found, or
89*4882a593Smuzhiyun  * %NULL otherwise.
90*4882a593Smuzhiyun  */
91*4882a593Smuzhiyun struct device_node *
usb_of_get_interface_node(struct usb_device * udev,u8 config,u8 ifnum)92*4882a593Smuzhiyun usb_of_get_interface_node(struct usb_device *udev, u8 config, u8 ifnum)
93*4882a593Smuzhiyun {
94*4882a593Smuzhiyun 	struct device_node *node;
95*4882a593Smuzhiyun 	u32 reg[2];
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	for_each_child_of_node(udev->dev.of_node, node) {
98*4882a593Smuzhiyun 		if (of_property_read_u32_array(node, "reg", reg, 2))
99*4882a593Smuzhiyun 			continue;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 		if (reg[0] == ifnum && reg[1] == config)
102*4882a593Smuzhiyun 			return node;
103*4882a593Smuzhiyun 	}
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	return NULL;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_of_get_interface_node);
108