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