xref: /freebsd/sys/dev/usb/usb_fdt_support.c (revision 6be3386466ab79a84b48429ae66244f21526d3df)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2019 Ian Lepore <ian@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/types.h>
32 #include <sys/condvar.h>
33 
34 #include <dev/fdt/fdt_common.h>
35 #include <dev/ofw/ofw_bus_subr.h>
36 #include <dev/ofw/openfirm.h>
37 
38 #include <dev/usb/usb.h>
39 #include <dev/usb/usbdi.h>
40 #include <dev/usb/usb_process.h>
41 #include <dev/usb/usb_busdma.h>
42 #include <dev/usb/usb_controller.h>
43 #include <dev/usb/usb_bus.h>
44 #include <dev/usb/usb_device.h>
45 #include <dev/usb/usb_freebsd.h>
46 #include <dev/usb/usb_fdt_support.h>
47 #include <dev/usb/net/usb_ethernet.h>
48 
49 /*
50  * Define a constant for allocating an array pointers to serve as a stack of
51  * devices between the controller and any arbitrary device on the bus.  The
52  * stack ends with the device itself, so add 1 to the max hub nesting depth.
53  */
54 #define	MAX_UDEV_NEST	(MAX(USB_HUB_MAX_DEPTH, USB_SS_HUB_DEPTH_MAX) + 1)
55 
56 static phandle_t
57 find_udev_in_children(phandle_t parent, struct usb_device *udev)
58 {
59 	phandle_t child;
60 	ssize_t proplen;
61 	uint32_t port;
62 	char compat[16]; /* big enough for "usb1234,abcd" */
63 
64 	/*
65 	 * USB device nodes in FDT have a compatible string of "usb" followed by
66 	 * the vendorId,productId rendered in hex.  The port number is encoded
67 	 * in the standard 'reg' property; it is one-based in the FDT data, but
68 	 * usb_device.port_index is zero-based.  To uniquely identify a device,
69 	 * both the compatible string and the port number must match.
70 	 */
71 	snprintf(compat, sizeof(compat), "usb%x,%x",
72 	    UGETW(udev->ddesc.idVendor), UGETW(udev->ddesc.idProduct));
73 	for (child = OF_child(parent); child != 0; child = OF_peer(child)) {
74 		if (!ofw_bus_node_is_compatible(child, compat))
75 			continue;
76 		proplen = OF_getencprop(child, "reg", &port, sizeof(port));
77 		if (proplen != sizeof(port))
78 			continue;
79 		if (port == (udev->port_index + 1))
80 			return (child);
81 	}
82 	return (-1);
83 }
84 
85 static bool
86 is_valid_mac_addr(uint8_t *addr)
87 {
88 
89 	/*
90 	 * All-bits-zero and all-bits-one are a couple common cases of what
91 	 * might get read from unprogrammed eeprom or OTP data, weed them out.
92 	 */
93 	if ((addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]) == 0x00)
94 		return (false);
95 	if ((addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) == 0xff)
96 		return (false);
97 	return (true);
98 }
99 
100 int
101 usb_fdt_get_mac_addr(device_t dev, struct usb_ether* ue)
102 {
103 	phandle_t node;
104 	ssize_t i, proplen;
105 	uint8_t mac[sizeof(ue->ue_eaddr)];
106 	static const char *properties[] = {
107 	    "mac-address",
108 	    "local-mac-address"
109 	};
110 
111 	if ((node = usb_fdt_get_node(ue->ue_dev, ue->ue_udev)) == -1)
112 		return (ENXIO);
113 	for (i = 0; i < nitems(properties); ++i) {
114 		proplen = OF_getprop(node, properties[i], mac, sizeof(mac));
115 		if (proplen == sizeof(mac) && is_valid_mac_addr(mac)) {
116 			memcpy(ue->ue_eaddr, mac, sizeof(ue->ue_eaddr));
117 			return (0);
118 		}
119 	}
120 	return (ENXIO);
121 }
122 
123 phandle_t
124 usb_fdt_get_node(device_t dev, struct usb_device *udev)
125 {
126 	struct usb_device *ud;
127 	struct usb_device *udev_stack[MAX_UDEV_NEST];
128 	phandle_t controller_node, node;
129 	int idx;
130 
131 	/*
132 	 * Start searching at the controller node.  The usb_device links to the
133 	 * bus, and its parent is the controller.  If we can't get the
134 	 * controller node, the requesting device cannot be in the fdt data.
135 	 */
136 	if ((controller_node = ofw_bus_get_node(udev->bus->parent)) == -1)
137 		return (-1);
138 
139 	/*
140 	 * Walk up the usb hub ancestor hierarchy, building a stack of devices
141 	 * that begins with the requesting device and includes all the hubs
142 	 * between it and the controller, NOT including the root hub (the FDT
143 	 * bindings treat the controller and root hub as the same thing).
144 	 */
145 	for (ud = udev, idx = 0; ud->parent_hub != NULL; ud = ud->parent_hub) {
146 		KASSERT(idx < nitems(udev_stack), ("Too many hubs"));
147 		udev_stack[idx++] = ud;
148 	}
149 
150 	/*
151 	 * Now walk down the stack of udevs from the controller to the
152 	 * requesting device, and also down the hierarchy of nested children of
153 	 * the controller node in the fdt data.  At each nesting level of fdt
154 	 * data look for a child node whose properties match the vID,pID,portIdx
155 	 * tuple for the udev at the corresponding layer of the udev stack.  As
156 	 * long as we keep matching up child nodes with udevs, loop and search
157 	 * within the children of the just-found child for the next-deepest hub.
158 	 * If at any level we fail to find a matching node, stop searching and
159 	 * return.  When we hit the end of the stack (the requesting device) we
160 	 * return whatever the result was for the search at that nesting level.
161 	 */
162 	for (node = controller_node;;) {
163 		node = find_udev_in_children(node, udev_stack[--idx]);
164 		if (idx == 0 || node == -1)
165 			break;
166 	}
167 	return (node);
168 }
169