Johan Hovold 1a7e3948cb USB: add device-tree support for interfaces
Add OF device-tree support for USB interfaces.

USB "interface nodes" are children of USB "device nodes" and are
identified by an interface number and a configuration value:

	&usb1 { /* host controller */
		dev1: device@1 { /* device at port 1 */
			compatible = "usb1234,5678";
			reg = <1>;

			#address-cells = <2>;
			#size-cells = <0>;

			interface@0,2 { /* interface 0 of configuration 2 */
				compatible = "usbif1234,5678.config2.0";
				reg = <0 2>;
			};
		};
	};

The configuration component is not included in the textual
representation of an interface-node unit address for configuration 1:

	&dev1 {
		interface@0 {	/* interface 0 of configuration 1 */
			compatible = "usbif1234,5678.config1.0";
			reg = <0 1>;
		};
	};

When a USB device of class 0 or 9 (hub) has only a single configuration
with a single interface, a special case "combined node" is used instead
of a device node with an interface node:

	&usb1 {
		device@2 {
			compatible = "usb1234,abcd";
			reg = <2>;
		};
	};

Combined nodes are shared by the two device structures representing the
USB device and its interface in the kernel's device model.

Note that, as for device nodes, the compatible strings for interface
nodes are currently not used.

For more details see "Open Firmware Recommended Practice: Universal
Serial Bus Version 1" and the binding documentation.

Signed-off-by: Johan Hovold <johan@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-28 15:12:38 +01:00

133 lines
3.3 KiB
C

// SPDX-License-Identifier: GPL-2.0
/*
* of.c The helpers for hcd device tree support
*
* Copyright (C) 2016 Freescale Semiconductor, Inc.
* Author: Peter Chen <peter.chen@freescale.com>
* Copyright (C) 2017 Johan Hovold <johan@kernel.org>
*/
#include <linux/of.h>
#include <linux/of_platform.h>
#include <linux/usb/of.h>
/**
* usb_of_get_child_node - Find the device node match port number
* @parent: the parent device node
* @portnum: the port number which device is connecting
*
* Find the node from device tree according to its port number.
*
* Return: A pointer to the node with incremented refcount if found, or
* %NULL otherwise.
*/
struct device_node *usb_of_get_child_node(struct device_node *parent,
int portnum)
{
struct device_node *node;
u32 port;
for_each_child_of_node(parent, node) {
if (!of_property_read_u32(node, "reg", &port)) {
if (port == portnum)
return node;
}
}
return NULL;
}
EXPORT_SYMBOL_GPL(usb_of_get_child_node);
/**
* usb_of_has_combined_node() - determine whether a device has a combined node
* @udev: USB device
*
* Determine whether a USB device has a so called combined node which is
* shared with its sole interface. This is the case if and only if the device
* has a node and its decriptors report the following:
*
* 1) bDeviceClass is 0 or 9, and
* 2) bNumConfigurations is 1, and
* 3) bNumInterfaces is 1.
*
* Return: True iff the device has a device node and its descriptors match the
* criteria for a combined node.
*/
bool usb_of_has_combined_node(struct usb_device *udev)
{
struct usb_device_descriptor *ddesc = &udev->descriptor;
struct usb_config_descriptor *cdesc;
if (!udev->dev.of_node)
return false;
switch (ddesc->bDeviceClass) {
case USB_CLASS_PER_INTERFACE:
case USB_CLASS_HUB:
if (ddesc->bNumConfigurations == 1) {
cdesc = &udev->config->desc;
if (cdesc->bNumInterfaces == 1)
return true;
}
}
return false;
}
EXPORT_SYMBOL_GPL(usb_of_has_combined_node);
/**
* usb_of_get_interface_node() - get a USB interface node
* @udev: USB device of interface
* @config: configuration value
* @ifnum: interface number
*
* Look up the node of a USB interface given its USB device, configuration
* value and interface number.
*
* Return: A pointer to the node with incremented refcount if found, or
* %NULL otherwise.
*/
struct device_node *
usb_of_get_interface_node(struct usb_device *udev, u8 config, u8 ifnum)
{
struct device_node *node;
u32 reg[2];
for_each_child_of_node(udev->dev.of_node, node) {
if (of_property_read_u32_array(node, "reg", reg, 2))
continue;
if (reg[0] == ifnum && reg[1] == config)
return node;
}
return NULL;
}
EXPORT_SYMBOL_GPL(usb_of_get_interface_node);
/**
* usb_of_get_companion_dev - Find the companion device
* @dev: the device pointer to find a companion
*
* Find the companion device from platform bus.
*
* Takes a reference to the returned struct device which needs to be dropped
* after use.
*
* Return: On success, a pointer to the companion device, %NULL on failure.
*/
struct device *usb_of_get_companion_dev(struct device *dev)
{
struct device_node *node;
struct platform_device *pdev = NULL;
node = of_parse_phandle(dev->of_node, "companion", 0);
if (node)
pdev = of_find_device_by_node(node);
of_node_put(node);
return pdev ? &pdev->dev : NULL;
}
EXPORT_SYMBOL_GPL(usb_of_get_companion_dev);