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