Date: Thu, 30 May 2019 14:40:23 +0000 (UTC) From: Ian Lepore <ian@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org Subject: svn commit: r348420 - in stable/12/sys: conf dev/usb dev/usb/net modules/usb/usb Message-ID: <201905301440.x4UEeNv6004256@repo.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: ian Date: Thu May 30 14:40:23 2019 New Revision: 348420 URL: https://svnweb.freebsd.org/changeset/base/348420 Log: MFC r347974, r348001, r348006, r348013, r348016, r348018, r348020 FDT support for if_muge and if_smsc drivers... r347974: Add common support functions for USB devices configured via FDT data. FDT data is sometimes used to configure usb devices which are hardwired into an embedded system. Because the devices are instantiated by the usb enumeration process rather than by ofwbus iterating through the fdt data, it is somewhat difficult for a usb driver to locate fdt data that belongs to it. In the past, various ad-hoc methods have been used, which can lead to errors such applying configuration that should apply only to a hardwired device onto a similar device attached by the user at runtime. For example, if the user adds an ethernet device that uses the same driver as the builtin ethernet, both devices might end up with the same MAC address. These changes add a new usb_fdt_get_node() helper function that a driver can use to locate FDT data that belongs to a single unique instance of the device. This function locates the proper FDT data using the mechanism detailed in the standard "usb-device.txt" binding document [1]. There is also a new usb_fdt_get_mac_addr() function, used to retrieve the mac address for a given device instance from the fdt data. It uses usb_fdt_get_node() to locate the right node in the FDT data, and attempts to obtain the mac-address or local-mac-address property (in that order, the same as linux does it). The existing if_smsc driver is modified to use the new functions, both as an example and for testing the new functions. Rpi and rpi2 boards use this driver and provide the mac address via the fdt data. [1] https://github.com/torvalds/linux/blob/master/Documentation/devicetree/bindings/usb/usb-device.txt Differential Revision: https://reviews.freebsd.org/D20262 r348001 by emaste: muge: configure LEDs per dtb (for Raspberry Pi 3B+) Also apply some style(9) and remove the message about EEPROM configuration (if there's an EEPROM the hardware handles LED configuration itself). PR: 237325 Reviewed by: ian Submitted by: Ralf <iz-rpi03@hs-karlsruhe.de> r348006 by emaste: muge: update FDT LED configuration Also use LED mode settings from the FDT to set the PHY. From v3 of the patch submitted in the PR. I moved the sc_led_modes and sc_led_modes_mask default setting outside of the #ifdef FDT case. PR: 237325 Submitted by: Ralf <iz-rpi03@hs-karlsruhe.de> Reviewed by: ian MFC with: r348001 Event: Waterloo Hackathon 2019 Differential Revision: https://reviews.freebsd.org/D20325 r348013: Use the new usb fdt support functions to locate the proper fdt node for the device instance, and to get the MAC address for the device instance. The ad-hoc code this replaces could find the wrong instance if multiple devices were present. r348016: Don't detour through sc->sc_ue when we have a direct pointer to ue in hand already. Also, shorten a variable name for nicer line-wrapping. No functional changes. r348018: A MAC adddress from FDT data should override anything stored in eeprom or OTP registers (because the user is in control of the fdt data). Remove the early returns from the code that tries to find a good mac address, so that the execution always flows through the routine to get an address from FDT data last, when on FDT-enabled systems. r348020: Reverse the bit logic of sc_led_modes_mask. Instead of initializing it to all-ones then carving out blocks of zeroes where specified values go, init it to all-zeroes, put in ones where values need to be masked, then use it as value &= ~sc_led_modes_mask. In addition to being more idiomatic, this means everything related to FDT data is initialized to zero along with the rest of the softc, and that allows removing some #ifdef FDT sections and wrapping the whole muge_set_leds() function in a single ifdef block. This also deletes the early-out from muge_set_leds() when an eeprom exists. Even if there is an eeprom with led config in it, the fdt data (if present) should override that, because the user is in control of the fdt data. Added: stable/12/sys/dev/usb/usb_fdt_support.c - copied unchanged from r347974, head/sys/dev/usb/usb_fdt_support.c stable/12/sys/dev/usb/usb_fdt_support.h - copied unchanged from r347974, head/sys/dev/usb/usb_fdt_support.h Modified: stable/12/sys/conf/files stable/12/sys/dev/usb/net/if_muge.c stable/12/sys/dev/usb/net/if_mugereg.h stable/12/sys/dev/usb/net/if_smsc.c stable/12/sys/modules/usb/usb/Makefile Directory Properties: stable/12/ (props changed) Modified: stable/12/sys/conf/files ============================================================================== --- stable/12/sys/conf/files Thu May 30 14:24:26 2019 (r348419) +++ stable/12/sys/conf/files Thu May 30 14:40:23 2019 (r348420) @@ -3303,6 +3303,7 @@ dev/usb/usb_dev.c optional usb dev/usb/usb_device.c optional usb dev/usb/usb_dynamic.c optional usb dev/usb/usb_error.c optional usb +dev/usb/usb_fdt_support.c optional usb fdt dev/usb/usb_generic.c optional usb dev/usb/usb_handle_request.c optional usb dev/usb/usb_hid.c optional usb Modified: stable/12/sys/dev/usb/net/if_muge.c ============================================================================== --- stable/12/sys/dev/usb/net/if_muge.c Thu May 30 14:24:26 2019 (r348419) +++ stable/12/sys/dev/usb/net/if_muge.c Thu May 30 14:40:23 2019 (r348420) @@ -97,6 +97,7 @@ __FBSDID("$FreeBSD$"); #include <dev/fdt/fdt_common.h> #include <dev/ofw/ofw_bus.h> #include <dev/ofw/ofw_bus_subr.h> +#include <dev/usb/usb_fdt_support.h> #endif #include <dev/usb/usb.h> @@ -173,6 +174,9 @@ struct muge_softc { struct mtx sc_mtx; struct usb_xfer *sc_xfer[MUGE_N_TRANSFER]; int sc_phyno; + uint32_t sc_leds; + uint16_t sc_led_modes; + uint16_t sc_led_modes_mask; /* Settings for the mac control (MAC_CSR) register. */ uint32_t sc_rfe_ctl; @@ -889,8 +893,9 @@ static int lan78xx_phy_init(struct muge_softc *sc) { muge_dbg_printf(sc, "Initializing PHY.\n"); - uint16_t bmcr; + uint16_t bmcr, lmsr; usb_ticks_t start_ticks; + uint32_t hw_reg; const usb_ticks_t max_ticks = USB_MS_TO_TICKS(1000); MUGE_LOCK_ASSERT(sc, MA_OWNED); @@ -931,6 +936,25 @@ lan78xx_phy_init(struct muge_softc *sc) bmcr |= BMCR_AUTOEN; lan78xx_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR, bmcr); bmcr = lan78xx_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR); + + /* Configure LED Modes. */ + if (sc->sc_led_modes_mask != 0) { + lmsr = lan78xx_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, + MUGE_PHY_LED_MODE); + lmsr &= ~sc->sc_led_modes_mask; + lmsr |= sc->sc_led_modes; + lan78xx_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, + MUGE_PHY_LED_MODE, lmsr); + } + + /* Enable appropriate LEDs. */ + if (sc->sc_leds != 0 && + lan78xx_read_reg(sc, ETH_HW_CFG, &hw_reg) == 0) { + hw_reg &= ~(ETH_HW_CFG_LEDO_EN_ | ETH_HW_CFG_LED1_EN_ | + ETH_HW_CFG_LED2_EN_ | ETH_HW_CFG_LED3_EN_ ); + hw_reg |= sc->sc_leds; + lan78xx_write_reg(sc, ETH_HW_CFG, hw_reg); + } return (0); } @@ -1431,101 +1455,7 @@ tr_setup: } } -#ifdef FDT /** - * muge_fdt_find_eth_node - find descendant node with required compatibility - * @start: start node - * @compatible: compatible string used to identify the node - * - * Loop through all descendant nodes and return first match with required - * compatibility. - * - * RETURNS: - * Returns node's phandle on success -1 otherwise - */ -static phandle_t -muge_fdt_find_eth_node(phandle_t start, const char *compatible) -{ - phandle_t child, node; - - /* Traverse through entire tree to find usb ethernet nodes. */ - for (node = OF_child(start); node != 0; node = OF_peer(node)) { - if (ofw_bus_node_is_compatible(node, compatible)) - return (node); - child = muge_fdt_find_eth_node(node, compatible); - if (child != -1) - return (child); - } - - return (-1); -} - -/** - * muge_fdt_read_mac_property - read MAC address from node - * @node: USB device node - * @mac: memory to store MAC address to - * - * Check for common properties that might contain MAC address - * passed by boot loader. - * - * RETURNS: - * Returns 0 on success, error code otherwise - */ -static int -muge_fdt_read_mac_property(phandle_t node, unsigned char *mac) -{ - int len; - - /* Check if there is property */ - if ((len = OF_getproplen(node, "local-mac-address")) > 0) { - if (len != ETHER_ADDR_LEN) - return (EINVAL); - - OF_getprop(node, "local-mac-address", mac, - ETHER_ADDR_LEN); - return (0); - } - - if ((len = OF_getproplen(node, "mac-address")) > 0) { - if (len != ETHER_ADDR_LEN) - return (EINVAL); - - OF_getprop(node, "mac-address", mac, - ETHER_ADDR_LEN); - return (0); - } - - return (ENXIO); -} - -/** - * muge_fdt_find_mac - read MAC address from node - * @compatible: compatible string for DTB node in the form "usb[N]NNN,[M]MMM" - * where NNN is vendor id and MMM is product id - * @mac: memory to store MAC address to - * - * Tries to find matching node in DTS and obtain MAC address info from it - * - * RETURNS: - * Returns 0 on success, error code otherwise - */ -static int -muge_fdt_find_mac(const char *compatible, unsigned char *mac) -{ - phandle_t node, root; - - root = OF_finddevice("/"); - node = muge_fdt_find_eth_node(root, compatible); - if (node != -1) { - if (muge_fdt_read_mac_property(node, mac) == 0) - return (0); - } - - return (ENXIO); -} -#endif - -/** * muge_set_mac_addr - Initiailizes NIC MAC address * @ue: the USB ethernet device * @@ -1537,12 +1467,8 @@ muge_set_mac_addr(struct usb_ether *ue) { struct muge_softc *sc = uether_getsc(ue); uint32_t mac_h, mac_l; -#ifdef FDT - char compatible[16]; - struct usb_attach_arg *uaa = device_get_ivars(ue->ue_dev); -#endif - memset(sc->sc_ue.ue_eaddr, 0xff, ETHER_ADDR_LEN); + memset(ue->ue_eaddr, 0xff, ETHER_ADDR_LEN); uint32_t val; lan78xx_read_reg(sc, 0, &val); @@ -1550,44 +1476,78 @@ muge_set_mac_addr(struct usb_ether *ue) /* Read current MAC address from RX_ADDRx registers. */ if ((lan78xx_read_reg(sc, ETH_RX_ADDRL, &mac_l) == 0) && (lan78xx_read_reg(sc, ETH_RX_ADDRH, &mac_h) == 0)) { - sc->sc_ue.ue_eaddr[5] = (uint8_t)((mac_h >> 8) & 0xff); - sc->sc_ue.ue_eaddr[4] = (uint8_t)((mac_h) & 0xff); - sc->sc_ue.ue_eaddr[3] = (uint8_t)((mac_l >> 24) & 0xff); - sc->sc_ue.ue_eaddr[2] = (uint8_t)((mac_l >> 16) & 0xff); - sc->sc_ue.ue_eaddr[1] = (uint8_t)((mac_l >> 8) & 0xff); - sc->sc_ue.ue_eaddr[0] = (uint8_t)((mac_l) & 0xff); + ue->ue_eaddr[5] = (uint8_t)((mac_h >> 8) & 0xff); + ue->ue_eaddr[4] = (uint8_t)((mac_h) & 0xff); + ue->ue_eaddr[3] = (uint8_t)((mac_l >> 24) & 0xff); + ue->ue_eaddr[2] = (uint8_t)((mac_l >> 16) & 0xff); + ue->ue_eaddr[1] = (uint8_t)((mac_l >> 8) & 0xff); + ue->ue_eaddr[0] = (uint8_t)((mac_l) & 0xff); } - /* If RX_ADDRx did not provide a valid MAC address, try EEPROM. */ - if (ETHER_IS_VALID(sc->sc_ue.ue_eaddr)) { + /* + * If RX_ADDRx did not provide a valid MAC address, try EEPROM. If that + * doesn't work, try OTP. Whether any of these methods work or not, try + * FDT data, because it is allowed to override the EEPROM/OTP values. + */ + if (ETHER_IS_VALID(ue->ue_eaddr)) { muge_dbg_printf(sc, "MAC assigned from registers\n"); - return; + } else if (lan78xx_eeprom_present(sc) && lan78xx_eeprom_read_raw(sc, + ETH_E2P_MAC_OFFSET, ue->ue_eaddr, ETHER_ADDR_LEN) == 0 && + ETHER_IS_VALID(ue->ue_eaddr)) { + muge_dbg_printf(sc, "MAC assigned from EEPROM\n"); + } else if (lan78xx_otp_read(sc, OTP_MAC_OFFSET, ue->ue_eaddr, + ETHER_ADDR_LEN) == 0 && ETHER_IS_VALID(ue->ue_eaddr)) { + muge_dbg_printf(sc, "MAC assigned from OTP\n"); } - if ((lan78xx_eeprom_present(sc) && - lan78xx_eeprom_read_raw(sc, ETH_E2P_MAC_OFFSET, - sc->sc_ue.ue_eaddr, ETHER_ADDR_LEN) == 0) || - (lan78xx_otp_read(sc, OTP_MAC_OFFSET, - sc->sc_ue.ue_eaddr, ETHER_ADDR_LEN) == 0)) { - if (ETHER_IS_VALID(sc->sc_ue.ue_eaddr)) { - muge_dbg_printf(sc, "MAC read from EEPROM\n"); - return; - } +#ifdef FDT + /* ue->ue_eaddr modified only if config exists for this dev instance. */ + usb_fdt_get_mac_addr(ue->ue_dev, ue); + if (ETHER_IS_VALID(ue->ue_eaddr)) { + muge_dbg_printf(sc, "MAC assigned from FDT data\n"); } +#endif + if (!ETHER_IS_VALID(ue->ue_eaddr)) { + muge_dbg_printf(sc, "MAC assigned randomly\n"); + arc4rand(ue->ue_eaddr, ETHER_ADDR_LEN, 0); + ue->ue_eaddr[0] &= ~0x01; /* unicast */ + ue->ue_eaddr[0] |= 0x02; /* locally administered */ + } +} + +/** + * muge_set_leds - Initializes NIC LEDs pattern + * @ue: the USB ethernet device + * + * Tries to store the LED modes. + * Supports only DTB blob like the Linux driver does. + */ +static void +muge_set_leds(struct usb_ether *ue) +{ #ifdef FDT - snprintf(compatible, sizeof(compatible), "usb%x,%x", - uaa->info.idVendor, uaa->info.idProduct); - if (muge_fdt_find_mac(compatible, sc->sc_ue.ue_eaddr) == 0) { - muge_dbg_printf(sc, "MAC assigned from FDT blob\n"); - return; + struct muge_softc *sc = uether_getsc(ue); + phandle_t node; + pcell_t modes[4]; /* 4 LEDs are possible */ + ssize_t proplen; + uint32_t count; + + if ((node = usb_fdt_get_node(ue->ue_dev, ue->ue_udev)) != -1 && + (proplen = OF_getencprop(node, "microchip,led-modes", modes, + sizeof(modes))) > 0) { + count = proplen / sizeof( uint32_t ); + sc->sc_leds = (count > 0) * ETH_HW_CFG_LEDO_EN_ | + (count > 1) * ETH_HW_CFG_LED1_EN_ | + (count > 2) * ETH_HW_CFG_LED2_EN_ | + (count > 3) * ETH_HW_CFG_LED3_EN_; + while (count-- > 0) { + sc->sc_led_modes |= (modes[count] & 0xf) << (4 * count); + sc->sc_led_modes_mask |= 0xf << (4 * count); + } + muge_dbg_printf(sc, "LED modes set from FDT data\n"); } #endif - - muge_dbg_printf(sc, "MAC assigned randomly\n"); - arc4rand(sc->sc_ue.ue_eaddr, ETHER_ADDR_LEN, 0); - sc->sc_ue.ue_eaddr[0] &= ~0x01; /* unicast */ - sc->sc_ue.ue_eaddr[0] |= 0x02; /* locally administered */ } /** @@ -1610,6 +1570,7 @@ muge_attach_post(struct usb_ether *ue) sc->sc_phyno = 1; muge_set_mac_addr(ue); + muge_set_leds(ue); /* Initialise the chip for the first time */ lan78xx_chip_init(sc); Modified: stable/12/sys/dev/usb/net/if_mugereg.h ============================================================================== --- stable/12/sys/dev/usb/net/if_mugereg.h Thu May 30 14:24:26 2019 (r348419) +++ stable/12/sys/dev/usb/net/if_mugereg.h Thu May 30 14:40:23 2019 (r348420) @@ -190,6 +190,8 @@ #define MUGE_EXT_PAGE_SPACE_1 0x0001 #define MUGE_EXT_PAGE_SPACE_2 0x0002 +#define MUGE_PHY_LED_MODE 29 + /* Extended Register Page 1 Space */ #define MUGE_EXT_MODE_CTRL 0x0013 #define MUGE_EXT_MODE_CTRL_MDIX_MASK_ 0x000C Modified: stable/12/sys/dev/usb/net/if_smsc.c ============================================================================== --- stable/12/sys/dev/usb/net/if_smsc.c Thu May 30 14:24:26 2019 (r348419) +++ stable/12/sys/dev/usb/net/if_smsc.c Thu May 30 14:40:23 2019 (r348420) @@ -97,6 +97,7 @@ __FBSDID("$FreeBSD$"); #include <dev/fdt/fdt_common.h> #include <dev/ofw/ofw_bus.h> #include <dev/ofw/ofw_bus_subr.h> +#include <dev/usb/usb_fdt_support.h> #endif #include <dev/usb/usb.h> @@ -1559,148 +1560,7 @@ smsc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data return (rc); } -#ifdef FDT -/* - * This is FreeBSD-specific compatibility strings for RPi/RPi2 - */ -static phandle_t -smsc_fdt_find_eth_node(phandle_t start) -{ - phandle_t child, node; - - /* Traverse through entire tree to find usb ethernet nodes. */ - for (node = OF_child(start); node != 0; node = OF_peer(node)) { - if ((ofw_bus_node_is_compatible(node, "net,ethernet") && - ofw_bus_node_is_compatible(node, "usb,device")) || - ofw_bus_node_is_compatible(node, "usb424,ec00")) - return (node); - child = smsc_fdt_find_eth_node(node); - if (child != -1) - return (child); - } - - return (-1); -} - -/* - * Check if node's path is <*>/usb/hub/ethernet - */ -static int -smsc_fdt_is_usb_eth(phandle_t node) -{ - char name[16]; - int len; - - memset(name, 0, sizeof(name)); - len = OF_getprop(node, "name", name, sizeof(name)); - if (len <= 0) - return (0); - - if (strcmp(name, "ethernet")) - return (0); - - node = OF_parent(node); - if (node == -1) - return (0); - len = OF_getprop(node, "name", name, sizeof(name)); - if (len <= 0) - return (0); - - if (strcmp(name, "hub")) - return (0); - - node = OF_parent(node); - if (node == -1) - return (0); - len = OF_getprop(node, "name", name, sizeof(name)); - if (len <= 0) - return (0); - - if (strcmp(name, "usb")) - return (0); - - return (1); -} - -static phandle_t -smsc_fdt_find_eth_node_by_path(phandle_t start) -{ - phandle_t child, node; - - /* Traverse through entire tree to find usb ethernet nodes. */ - for (node = OF_child(start); node != 0; node = OF_peer(node)) { - if (smsc_fdt_is_usb_eth(node)) - return (node); - child = smsc_fdt_find_eth_node_by_path(node); - if (child != -1) - return (child); - } - - return (-1); -} - -/* - * Look through known names that can contain mac address - * return 0 if valid MAC address has been found - */ -static int -smsc_fdt_read_mac_property(phandle_t node, unsigned char *mac) -{ - int len; - - /* Check if there is property */ - if ((len = OF_getproplen(node, "local-mac-address")) > 0) { - if (len != ETHER_ADDR_LEN) - return (EINVAL); - - OF_getprop(node, "local-mac-address", mac, - ETHER_ADDR_LEN); - return (0); - } - - if ((len = OF_getproplen(node, "mac-address")) > 0) { - if (len != ETHER_ADDR_LEN) - return (EINVAL); - - OF_getprop(node, "mac-address", mac, - ETHER_ADDR_LEN); - return (0); - } - - return (ENXIO); -} - /** - * Get MAC address from FDT blob. Firmware or loader should fill - * mac-address or local-mac-address property. Returns 0 if MAC address - * obtained, error code otherwise. - */ -static int -smsc_fdt_find_mac(unsigned char *mac) -{ - phandle_t node, root; - - root = OF_finddevice("/"); - node = smsc_fdt_find_eth_node(root); - if (node != -1) { - if (smsc_fdt_read_mac_property(node, mac) == 0) - return (0); - } - - /* - * If it's not FreeBSD FDT blob for RPi, try more - * generic .../usb/hub/ethernet - */ - node = smsc_fdt_find_eth_node_by_path(root); - - if (node != -1) - return smsc_fdt_read_mac_property(node, mac); - - return (ENXIO); -} -#endif - -/** * smsc_attach_post - Called after the driver attached to the USB interface * @ue: the USB ethernet device * @@ -1748,7 +1608,7 @@ smsc_attach_post(struct usb_ether *ue) err = smsc_eeprom_read(sc, 0x01, sc->sc_ue.ue_eaddr, ETHER_ADDR_LEN); #ifdef FDT if ((err != 0) || (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr))) - err = smsc_fdt_find_mac(sc->sc_ue.ue_eaddr); + err = usb_fdt_get_mac_addr(sc->sc_ue.ue_dev, &sc->sc_ue); #endif if ((err != 0) || (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr))) { read_random(sc->sc_ue.ue_eaddr, ETHER_ADDR_LEN); Copied: stable/12/sys/dev/usb/usb_fdt_support.c (from r347974, head/sys/dev/usb/usb_fdt_support.c) ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ stable/12/sys/dev/usb/usb_fdt_support.c Thu May 30 14:40:23 2019 (r348420, copy of r347974, head/sys/dev/usb/usb_fdt_support.c) @@ -0,0 +1,168 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2019 Ian Lepore <ian@FreeBSD.org> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/types.h> +#include <sys/condvar.h> + +#include <dev/fdt/fdt_common.h> +#include <dev/ofw/ofw_bus_subr.h> +#include <dev/ofw/openfirm.h> + +#include <dev/usb/usb.h> +#include <dev/usb/usbdi.h> +#include <dev/usb/usb_process.h> +#include <dev/usb/usb_busdma.h> +#include <dev/usb/usb_controller.h> +#include <dev/usb/usb_bus.h> +#include <dev/usb/usb_device.h> +#include <dev/usb/usb_freebsd.h> +#include <dev/usb/usb_fdt_support.h> +#include <dev/usb/net/usb_ethernet.h> + +/* + * Define a constant for allocating an array pointers to serve as a stack of + * devices between the controller and any arbitrary device on the bus. The + * stack ends with the device itself, so add 1 to the max hub nesting depth. + */ +#define MAX_UDEV_NEST (MAX(USB_HUB_MAX_DEPTH, USB_SS_HUB_DEPTH_MAX) + 1) + +static phandle_t +find_udev_in_children(phandle_t parent, struct usb_device *udev) +{ + phandle_t child; + ssize_t proplen; + uint32_t port; + char compat[16]; /* big enough for "usb1234,abcd" */ + + /* + * USB device nodes in FDT have a compatible string of "usb" followed by + * the vendorId,productId rendered in hex. The port number is encoded + * in the standard 'reg' property; it is one-based in the FDT data, but + * usb_device.port_index is zero-based. To uniquely identify a device, + * both the compatible string and the port number must match. + */ + snprintf(compat, sizeof(compat), "usb%x,%x", + UGETW(udev->ddesc.idVendor), UGETW(udev->ddesc.idProduct)); + for (child = OF_child(parent); child != 0; child = OF_peer(child)) { + if (!ofw_bus_node_is_compatible(child, compat)) + continue; + proplen = OF_getencprop(child, "reg", &port, sizeof(port)); + if (proplen != sizeof(port)) + continue; + if (port == (udev->port_index + 1)) + return (child); + } + return (-1); +} + +static bool +is_valid_mac_addr(uint8_t *addr) +{ + + /* + * All-bits-zero and all-bits-one are a couple common cases of what + * might get read from unprogrammed eeprom or OTP data, weed them out. + */ + if ((addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]) == 0x00) + return (false); + if ((addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) == 0xff) + return (false); + return (true); +} + +int +usb_fdt_get_mac_addr(device_t dev, struct usb_ether* ue) +{ + phandle_t node; + ssize_t i, proplen; + uint8_t mac[sizeof(ue->ue_eaddr)]; + static const char *properties[] = { + "mac-address", + "local-mac-address" + }; + + if ((node = usb_fdt_get_node(ue->ue_dev, ue->ue_udev)) == -1) + return (ENXIO); + for (i = 0; i < nitems(properties); ++i) { + proplen = OF_getprop(node, properties[i], mac, sizeof(mac)); + if (proplen == sizeof(mac) && is_valid_mac_addr(mac)) { + memcpy(ue->ue_eaddr, mac, sizeof(ue->ue_eaddr)); + return (0); + } + } + return (ENXIO); +} + +phandle_t +usb_fdt_get_node(device_t dev, struct usb_device *udev) +{ + struct usb_device *ud; + struct usb_device *udev_stack[MAX_UDEV_NEST]; + phandle_t controller_node, node; + int idx; + + /* + * Start searching at the controller node. The usb_device links to the + * bus, and its parent is the controller. If we can't get the + * controller node, the requesting device cannot be in the fdt data. + */ + if ((controller_node = ofw_bus_get_node(udev->bus->parent)) == -1) + return (-1); + + /* + * Walk up the usb hub ancestor hierarchy, building a stack of devices + * that begins with the requesting device and includes all the hubs + * between it and the controller, NOT including the root hub (the FDT + * bindings treat the controller and root hub as the same thing). + */ + for (ud = udev, idx = 0; ud->parent_hub != NULL; ud = ud->parent_hub) { + KASSERT(idx < nitems(udev_stack), ("Too many hubs")); + udev_stack[idx++] = ud; + } + + /* + * Now walk down the stack of udevs from the controller to the + * requesting device, and also down the hierarchy of nested children of + * the controller node in the fdt data. At each nesting level of fdt + * data look for a child node whose properties match the vID,pID,portIdx + * tuple for the udev at the corresponding layer of the udev stack. As + * long as we keep matching up child nodes with udevs, loop and search + * within the children of the just-found child for the next-deepest hub. + * If at any level we fail to find a matching node, stop searching and + * return. When we hit the end of the stack (the requesting device) we + * return whatever the result was for the search at that nesting level. + */ + for (node = controller_node;;) { + node = find_udev_in_children(node, udev_stack[--idx]); + if (idx == 0 || node == -1) + break; + } + return (node); +} Copied: stable/12/sys/dev/usb/usb_fdt_support.h (from r347974, head/sys/dev/usb/usb_fdt_support.h) ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ stable/12/sys/dev/usb/usb_fdt_support.h Thu May 30 14:40:23 2019 (r348420, copy of r347974, head/sys/dev/usb/usb_fdt_support.h) @@ -0,0 +1,48 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2019 Ian Lepore <ian@FreeBSD.org> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#ifndef _USB_FDT_SUPPORT_H_ +#define _USB_FDT_SUPPORT_H_ + +struct usb_device; +struct usb_ether; + +/* + * Get the device's MAC address from the FDT data. Fills in ue->ue_eaddr and + * returns 0 on success, otherwise leaves ue_eaddr untouched and returns + * non-zero. This first attempts to get the address from the "mac-address" + * property, and if that's not valid it tries the "local-mac-address" property; + * this matches the linux interpretation of the precedence of those properties. + */ +int usb_fdt_get_mac_addr(device_t dev, struct usb_ether* ue); + +/* Get the FDT node for dev. Returns -1 if dev is not in the FDT data. */ +phandle_t usb_fdt_get_node(device_t dev, struct usb_device* udev); + +#endif Modified: stable/12/sys/modules/usb/usb/Makefile ============================================================================== --- stable/12/sys/modules/usb/usb/Makefile Thu May 30 14:24:26 2019 (r348419) +++ stable/12/sys/modules/usb/usb/Makefile Thu May 30 14:40:23 2019 (r348420) @@ -39,4 +39,8 @@ SRCS= bus_if.h device_if.h usb_if.h usb_if.c vnode_if. usb_msctest.c usb_parse.c usb_pf.c usb_process.c usb_request.c \ usb_transfer.c usb_util.c +.if !empty(OPT_FDT) +SRCS+= usb_fdt_support.c ofw_bus_if.h +.endif + .include <bsd.kmod.mk>
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201905301440.x4UEeNv6004256>