xref: /freebsd/sys/dev/usb/usb_fdt_support.c (revision 06c3fb27)
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/types.h>
29 #include <sys/condvar.h>
30 
31 #include <dev/fdt/fdt_common.h>
32 #include <dev/ofw/ofw_bus_subr.h>
33 #include <dev/ofw/openfirm.h>
34 
35 #include <dev/usb/usb.h>
36 #include <dev/usb/usbdi.h>
37 #include <dev/usb/usb_process.h>
38 #include <dev/usb/usb_busdma.h>
39 #include <dev/usb/usb_controller.h>
40 #include <dev/usb/usb_bus.h>
41 #include <dev/usb/usb_device.h>
42 #include <dev/usb/usb_freebsd.h>
43 #include <dev/usb/usb_fdt_support.h>
44 #include <dev/usb/net/usb_ethernet.h>
45 
46 /*
47  * Define a constant for allocating an array pointers to serve as a stack of
48  * devices between the controller and any arbitrary device on the bus.  The
49  * stack ends with the device itself, so add 1 to the max hub nesting depth.
50  */
51 #define	MAX_UDEV_NEST	(MAX(USB_HUB_MAX_DEPTH, USB_SS_HUB_DEPTH_MAX) + 1)
52 
53 static phandle_t
54 find_udev_in_children(phandle_t parent, struct usb_device *udev)
55 {
56 	phandle_t child;
57 	ssize_t proplen;
58 	uint32_t port;
59 	char compat[16]; /* big enough for "usb1234,abcd" */
60 
61 	/*
62 	 * USB device nodes in FDT have a compatible string of "usb" followed by
63 	 * the vendorId,productId rendered in hex.  The port number is encoded
64 	 * in the standard 'reg' property; it is one-based in the FDT data, but
65 	 * usb_device.port_index is zero-based.  To uniquely identify a device,
66 	 * both the compatible string and the port number must match.
67 	 */
68 	snprintf(compat, sizeof(compat), "usb%x,%x",
69 	    UGETW(udev->ddesc.idVendor), UGETW(udev->ddesc.idProduct));
70 	for (child = OF_child(parent); child != 0; child = OF_peer(child)) {
71 		if (!ofw_bus_node_is_compatible(child, compat))
72 			continue;
73 		proplen = OF_getencprop(child, "reg", &port, sizeof(port));
74 		if (proplen != sizeof(port))
75 			continue;
76 		if (port == (udev->port_index + 1))
77 			return (child);
78 	}
79 	return (-1);
80 }
81 
82 static bool
83 is_valid_mac_addr(uint8_t *addr)
84 {
85 
86 	/*
87 	 * All-bits-zero and all-bits-one are a couple common cases of what
88 	 * might get read from unprogrammed eeprom or OTP data, weed them out.
89 	 */
90 	if ((addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]) == 0x00)
91 		return (false);
92 	if ((addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) == 0xff)
93 		return (false);
94 	return (true);
95 }
96 
97 int
98 usb_fdt_get_mac_addr(device_t dev, struct usb_ether* ue)
99 {
100 	phandle_t node;
101 	ssize_t i, proplen;
102 	uint8_t mac[sizeof(ue->ue_eaddr)];
103 	static const char *properties[] = {
104 	    "mac-address",
105 	    "local-mac-address"
106 	};
107 
108 	if ((node = usb_fdt_get_node(ue->ue_dev, ue->ue_udev)) == -1)
109 		return (ENXIO);
110 	for (i = 0; i < nitems(properties); ++i) {
111 		proplen = OF_getprop(node, properties[i], mac, sizeof(mac));
112 		if (proplen == sizeof(mac) && is_valid_mac_addr(mac)) {
113 			memcpy(ue->ue_eaddr, mac, sizeof(ue->ue_eaddr));
114 			return (0);
115 		}
116 	}
117 	return (ENXIO);
118 }
119 
120 phandle_t
121 usb_fdt_get_node(device_t dev, struct usb_device *udev)
122 {
123 	struct usb_device *ud;
124 	struct usb_device *udev_stack[MAX_UDEV_NEST];
125 	phandle_t controller_node, node;
126 	int idx;
127 
128 	/*
129 	 * Start searching at the controller node.  The usb_device links to the
130 	 * bus, and its parent is the controller.  If we can't get the
131 	 * controller node, the requesting device cannot be in the fdt data.
132 	 */
133 	if ((controller_node = ofw_bus_get_node(udev->bus->parent)) == -1)
134 		return (-1);
135 
136 	/*
137 	 * Walk up the usb hub ancestor hierarchy, building a stack of devices
138 	 * that begins with the requesting device and includes all the hubs
139 	 * between it and the controller, NOT including the root hub (the FDT
140 	 * bindings treat the controller and root hub as the same thing).
141 	 */
142 	for (ud = udev, idx = 0; ud->parent_hub != NULL; ud = ud->parent_hub) {
143 		KASSERT(idx < nitems(udev_stack), ("Too many hubs"));
144 		udev_stack[idx++] = ud;
145 	}
146 
147 	/*
148 	 * Now walk down the stack of udevs from the controller to the
149 	 * requesting device, and also down the hierarchy of nested children of
150 	 * the controller node in the fdt data.  At each nesting level of fdt
151 	 * data look for a child node whose properties match the vID,pID,portIdx
152 	 * tuple for the udev at the corresponding layer of the udev stack.  As
153 	 * long as we keep matching up child nodes with udevs, loop and search
154 	 * within the children of the just-found child for the next-deepest hub.
155 	 * If at any level we fail to find a matching node, stop searching and
156 	 * return.  When we hit the end of the stack (the requesting device) we
157 	 * return whatever the result was for the search at that nesting level.
158 	 */
159 	for (node = controller_node;;) {
160 		node = find_udev_in_children(node, udev_stack[--idx]);
161 		if (idx == 0 || node == -1)
162 			break;
163 	}
164 	return (node);
165 }
166