Linux Audio

Check our new training course

Loading...
v4.6
 
 1/*
 2 * of.c		The helpers for hcd device tree support
 3 *
 4 * Copyright (C) 2016 Freescale Semiconductor, Inc.
 5 * Author: Peter Chen <peter.chen@freescale.com>
 6 *
 7 * This program is free software: you can redistribute it and/or modify
 8 * it under the terms of the GNU General Public License version 2  of
 9 * the License as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <linux/of.h>
 
 
21
22/**
23 * usb_of_get_child_node - Find the device node match port number
24 * @parent: the parent device node
25 * @portnum: the port number which device is connecting
26 *
27 * Find the node from device tree according to its port number.
 
28 *
29 * Return: On success, a pointer to the device node, %NULL on failure.
 
30 */
31struct device_node *usb_of_get_child_node(struct device_node *parent,
32					int portnum)
33{
34	struct device_node *node;
35	u32 port;
36
37	for_each_child_of_node(parent, node) {
38		if (!of_property_read_u32(node, "reg", &port)) {
39			if (port == portnum)
40				return node;
41		}
 
42	}
43
44	return NULL;
45}
46EXPORT_SYMBOL_GPL(usb_of_get_child_node);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
v6.9.4
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * of.c		The helpers for hcd device tree support
  4 *
  5 * Copyright (C) 2016 Freescale Semiconductor, Inc.
  6 *	Author: Peter Chen <peter.chen@freescale.com>
  7 * Copyright (C) 2017 Johan Hovold <johan@kernel.org>
 
 
 
 
 
 
 
 
 
 
 
  8 */
  9
 10#include <linux/of.h>
 11#include <linux/of_graph.h>
 12#include <linux/usb/of.h>
 13
 14/**
 15 * usb_of_get_device_node() - get a USB device node
 16 * @hub: hub to which device is connected
 17 * @port1: one-based index of port
 18 *
 19 * Look up the node of a USB device given its parent hub device and one-based
 20 * port number.
 21 *
 22 * Return: A pointer to the node with incremented refcount if found, or
 23 * %NULL otherwise.
 24 */
 25struct device_node *usb_of_get_device_node(struct usb_device *hub, int port1)
 
 26{
 27	struct device_node *node;
 28	u32 reg;
 29
 30	for_each_child_of_node(hub->dev.of_node, node) {
 31		if (of_property_read_u32(node, "reg", &reg))
 32			continue;
 33
 34		if (reg == port1)
 35			return node;
 36	}
 37
 38	return NULL;
 39}
 40EXPORT_SYMBOL_GPL(usb_of_get_device_node);
 41
 42/**
 43 * usb_of_has_combined_node() - determine whether a device has a combined node
 44 * @udev: USB device
 45 *
 46 * Determine whether a USB device has a so called combined node which is
 47 * shared with its sole interface. This is the case if and only if the device
 48 * has a node and its descriptors report the following:
 49 *
 50 *	1) bDeviceClass is 0 or 9, and
 51 *	2) bNumConfigurations is 1, and
 52 *	3) bNumInterfaces is 1.
 53 *
 54 * Return: True iff the device has a device node and its descriptors match the
 55 * criteria for a combined node.
 56 */
 57bool usb_of_has_combined_node(struct usb_device *udev)
 58{
 59	struct usb_device_descriptor *ddesc = &udev->descriptor;
 60	struct usb_config_descriptor *cdesc;
 61
 62	if (!udev->dev.of_node)
 63		return false;
 64
 65	switch (ddesc->bDeviceClass) {
 66	case USB_CLASS_PER_INTERFACE:
 67	case USB_CLASS_HUB:
 68		if (ddesc->bNumConfigurations == 1) {
 69			cdesc = &udev->config->desc;
 70			if (cdesc->bNumInterfaces == 1)
 71				return true;
 72		}
 73	}
 74
 75	return false;
 76}
 77EXPORT_SYMBOL_GPL(usb_of_has_combined_node);
 78
 79static bool usb_of_has_devices_or_graph(const struct usb_device *hub)
 80{
 81	const struct device_node *np = hub->dev.of_node;
 82	struct device_node *child;
 83
 84	if (of_graph_is_present(np))
 85		return true;
 86
 87	for_each_child_of_node(np, child)
 88		if (of_property_present(child, "reg"))
 89			return true;
 90
 91	return false;
 92}
 93
 94/**
 95 * usb_of_get_connect_type() - get a USB hub's port connect_type
 96 * @hub: hub to which port is for @port1
 97 * @port1: one-based index of port
 98 *
 99 * Get the connect_type of @port1 based on the device node for @hub. If the
100 * port is described in the OF graph, the connect_type is "hotplug". If the
101 * @hub has a child device has with a 'reg' property equal to @port1 the
102 * connect_type is "hard-wired". If there isn't an OF graph or child node at
103 * all then the connect_type is "unknown". Otherwise, the port is considered
104 * "unused" because it isn't described at all.
105 *
106 * Return: A connect_type for @port1 based on the device node for @hub.
107 */
108enum usb_port_connect_type usb_of_get_connect_type(struct usb_device *hub, int port1)
109{
110	struct device_node *np, *child, *ep, *remote_np;
111	enum usb_port_connect_type connect_type;
112
113	/* Only set connect_type if binding has ports/hardwired devices. */
114	if (!usb_of_has_devices_or_graph(hub))
115		return USB_PORT_CONNECT_TYPE_UNKNOWN;
116
117	/* Assume port is unused if there's a graph or a child node. */
118	connect_type = USB_PORT_NOT_USED;
119
120	np = hub->dev.of_node;
121	/*
122	 * Hotplug ports are connected to an available remote node, e.g.
123	 * usb-a-connector compatible node, in the OF graph.
124	 */
125	if (of_graph_is_present(np)) {
126		ep = of_graph_get_endpoint_by_regs(np, port1, -1);
127		if (ep) {
128			remote_np = of_graph_get_remote_port_parent(ep);
129			of_node_put(ep);
130			if (of_device_is_available(remote_np))
131				connect_type = USB_PORT_CONNECT_TYPE_HOT_PLUG;
132			of_node_put(remote_np);
133		}
134	}
135
136	/*
137	 * Hard-wired ports are child nodes with a reg property corresponding
138	 * to the port number, i.e. a usb device.
139	 */
140	child = usb_of_get_device_node(hub, port1);
141	if (of_device_is_available(child))
142		connect_type = USB_PORT_CONNECT_TYPE_HARD_WIRED;
143	of_node_put(child);
144
145	return connect_type;
146}
147EXPORT_SYMBOL_GPL(usb_of_get_connect_type);
148
149/**
150 * usb_of_get_interface_node() - get a USB interface node
151 * @udev: USB device of interface
152 * @config: configuration value
153 * @ifnum: interface number
154 *
155 * Look up the node of a USB interface given its USB device, configuration
156 * value and interface number.
157 *
158 * Return: A pointer to the node with incremented refcount if found, or
159 * %NULL otherwise.
160 */
161struct device_node *
162usb_of_get_interface_node(struct usb_device *udev, u8 config, u8 ifnum)
163{
164	struct device_node *node;
165	u32 reg[2];
166
167	for_each_child_of_node(udev->dev.of_node, node) {
168		if (of_property_read_u32_array(node, "reg", reg, 2))
169			continue;
170
171		if (reg[0] == ifnum && reg[1] == config)
172			return node;
173	}
174
175	return NULL;
176}
177EXPORT_SYMBOL_GPL(usb_of_get_interface_node);