Linux Audio

Check our new training course

Linux BSP upgrade and security maintenance

Need help to get security updates for your Linux BSP?
Loading...
v6.8
  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/usb/of.h>
 12
 13/**
 14 * usb_of_get_device_node() - get a USB device node
 15 * @hub: hub to which device is connected
 16 * @port1: one-based index of port
 17 *
 18 * Look up the node of a USB device given its parent hub device and one-based
 19 * port number.
 20 *
 21 * Return: A pointer to the node with incremented refcount if found, or
 22 * %NULL otherwise.
 23 */
 24struct device_node *usb_of_get_device_node(struct usb_device *hub, int port1)
 25{
 26	struct device_node *node;
 27	u32 reg;
 28
 29	for_each_child_of_node(hub->dev.of_node, node) {
 30		if (of_property_read_u32(node, "reg", &reg))
 31			continue;
 32
 33		if (reg == port1)
 34			return node;
 35	}
 36
 37	return NULL;
 38}
 39EXPORT_SYMBOL_GPL(usb_of_get_device_node);
 40
 41/**
 42 * usb_of_has_combined_node() - determine whether a device has a combined node
 43 * @udev: USB device
 44 *
 45 * Determine whether a USB device has a so called combined node which is
 46 * shared with its sole interface. This is the case if and only if the device
 47 * has a node and its descriptors report the following:
 48 *
 49 *	1) bDeviceClass is 0 or 9, and
 50 *	2) bNumConfigurations is 1, and
 51 *	3) bNumInterfaces is 1.
 52 *
 53 * Return: True iff the device has a device node and its descriptors match the
 54 * criteria for a combined node.
 55 */
 56bool usb_of_has_combined_node(struct usb_device *udev)
 57{
 58	struct usb_device_descriptor *ddesc = &udev->descriptor;
 59	struct usb_config_descriptor *cdesc;
 60
 61	if (!udev->dev.of_node)
 62		return false;
 63
 64	switch (ddesc->bDeviceClass) {
 65	case USB_CLASS_PER_INTERFACE:
 66	case USB_CLASS_HUB:
 67		if (ddesc->bNumConfigurations == 1) {
 68			cdesc = &udev->config->desc;
 69			if (cdesc->bNumInterfaces == 1)
 70				return true;
 71		}
 72	}
 73
 74	return false;
 75}
 76EXPORT_SYMBOL_GPL(usb_of_has_combined_node);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 77
 78/**
 79 * usb_of_get_interface_node() - get a USB interface node
 80 * @udev: USB device of interface
 81 * @config: configuration value
 82 * @ifnum: interface number
 83 *
 84 * Look up the node of a USB interface given its USB device, configuration
 85 * value and interface number.
 86 *
 87 * Return: A pointer to the node with incremented refcount if found, or
 88 * %NULL otherwise.
 89 */
 90struct device_node *
 91usb_of_get_interface_node(struct usb_device *udev, u8 config, u8 ifnum)
 92{
 93	struct device_node *node;
 94	u32 reg[2];
 95
 96	for_each_child_of_node(udev->dev.of_node, node) {
 97		if (of_property_read_u32_array(node, "reg", reg, 2))
 98			continue;
 99
100		if (reg[0] == ifnum && reg[1] == config)
101			return node;
102	}
103
104	return NULL;
105}
106EXPORT_SYMBOL_GPL(usb_of_get_interface_node);
v6.13.7
  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			of_node_put(child);
 90			return true;
 91		}
 92	}
 93
 94	return false;
 95}
 96
 97/**
 98 * usb_of_get_connect_type() - get a USB hub's port connect_type
 99 * @hub: hub to which port is for @port1
100 * @port1: one-based index of port
101 *
102 * Get the connect_type of @port1 based on the device node for @hub. If the
103 * port is described in the OF graph, the connect_type is "hotplug". If the
104 * @hub has a child device has with a 'reg' property equal to @port1 the
105 * connect_type is "hard-wired". If there isn't an OF graph or child node at
106 * all then the connect_type is "unknown". Otherwise, the port is considered
107 * "unused" because it isn't described at all.
108 *
109 * Return: A connect_type for @port1 based on the device node for @hub.
110 */
111enum usb_port_connect_type usb_of_get_connect_type(struct usb_device *hub, int port1)
112{
113	struct device_node *np, *child, *ep, *remote_np;
114	enum usb_port_connect_type connect_type;
115
116	/* Only set connect_type if binding has ports/hardwired devices. */
117	if (!usb_of_has_devices_or_graph(hub))
118		return USB_PORT_CONNECT_TYPE_UNKNOWN;
119
120	/* Assume port is unused if there's a graph or a child node. */
121	connect_type = USB_PORT_NOT_USED;
122
123	np = hub->dev.of_node;
124	/*
125	 * Hotplug ports are connected to an available remote node, e.g.
126	 * usb-a-connector compatible node, in the OF graph.
127	 */
128	if (of_graph_is_present(np)) {
129		ep = of_graph_get_endpoint_by_regs(np, port1, -1);
130		if (ep) {
131			remote_np = of_graph_get_remote_port_parent(ep);
132			of_node_put(ep);
133			if (of_device_is_available(remote_np))
134				connect_type = USB_PORT_CONNECT_TYPE_HOT_PLUG;
135			of_node_put(remote_np);
136		}
137	}
138
139	/*
140	 * Hard-wired ports are child nodes with a reg property corresponding
141	 * to the port number, i.e. a usb device.
142	 */
143	child = usb_of_get_device_node(hub, port1);
144	if (of_device_is_available(child))
145		connect_type = USB_PORT_CONNECT_TYPE_HARD_WIRED;
146	of_node_put(child);
147
148	return connect_type;
149}
150EXPORT_SYMBOL_GPL(usb_of_get_connect_type);
151
152/**
153 * usb_of_get_interface_node() - get a USB interface node
154 * @udev: USB device of interface
155 * @config: configuration value
156 * @ifnum: interface number
157 *
158 * Look up the node of a USB interface given its USB device, configuration
159 * value and interface number.
160 *
161 * Return: A pointer to the node with incremented refcount if found, or
162 * %NULL otherwise.
163 */
164struct device_node *
165usb_of_get_interface_node(struct usb_device *udev, u8 config, u8 ifnum)
166{
167	struct device_node *node;
168	u32 reg[2];
169
170	for_each_child_of_node(udev->dev.of_node, node) {
171		if (of_property_read_u32_array(node, "reg", reg, 2))
172			continue;
173
174		if (reg[0] == ifnum && reg[1] == config)
175			return node;
176	}
177
178	return NULL;
179}
180EXPORT_SYMBOL_GPL(usb_of_get_interface_node);