2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Ben Gray <bgray@freebsd.org>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 * SMSC LAN9xxx devices (http://www.smsc.com/)
35 * The LAN9500 & LAN9500A devices are stand-alone USB to Ethernet chips that
36 * support USB 2.0 and 10/100 Mbps Ethernet.
38 * The LAN951x devices are an integrated USB hub and USB to Ethernet adapter.
39 * The driver only covers the Ethernet part, the standard USB hub driver
40 * supports the hub part.
42 * This driver is closely modelled on the Linux driver written and copyrighted
48 * H/W TCP & UDP Checksum Offloading
49 * ---------------------------------
50 * The chip supports both tx and rx offloading of UDP & TCP checksums, this
51 * feature can be dynamically enabled/disabled.
53 * RX checksuming is performed across bytes after the IPv4 header to the end of
54 * the Ethernet frame, this means if the frame is padded with non-zero values
55 * the H/W checksum will be incorrect, however the rx code compensates for this.
57 * TX checksuming is more complicated, the device requires a special header to
58 * be prefixed onto the start of the frame which indicates the start and end
59 * positions of the UDP or TCP frame. This requires the driver to manually
60 * go through the packet data and decode the headers prior to sending.
61 * On Linux they generally provide cues to the location of the csum and the
62 * area to calculate it over, on FreeBSD we seem to have to do it all ourselves,
63 * hence this is not as optimal and therefore h/w tX checksum is currently not
67 #include <sys/stdint.h>
68 #include <sys/stddef.h>
69 #include <sys/param.h>
70 #include <sys/queue.h>
71 #include <sys/types.h>
72 #include <sys/systm.h>
73 #include <sys/kernel.h>
75 #include <sys/module.h>
77 #include <sys/mutex.h>
78 #include <sys/condvar.h>
79 #include <sys/socket.h>
80 #include <sys/sysctl.h>
82 #include <sys/unistd.h>
83 #include <sys/callout.h>
84 #include <sys/malloc.h>
86 #include <sys/random.h>
89 #include <net/if_var.h>
91 #include <netinet/in.h>
92 #include <netinet/ip.h>
94 #include "opt_platform.h"
97 #include <dev/fdt/fdt_common.h>
98 #include <dev/ofw/ofw_bus.h>
99 #include <dev/ofw/ofw_bus_subr.h>
102 #include <dev/usb/usb.h>
103 #include <dev/usb/usbdi.h>
104 #include <dev/usb/usbdi_util.h>
107 #define USB_DEBUG_VAR smsc_debug
108 #include <dev/usb/usb_debug.h>
109 #include <dev/usb/usb_process.h>
111 #include <dev/usb/net/usb_ethernet.h>
113 #include <dev/usb/net/if_smscreg.h>
116 static int smsc_debug = 0;
118 SYSCTL_NODE(_hw_usb, OID_AUTO, smsc, CTLFLAG_RW, 0, "USB smsc");
119 SYSCTL_INT(_hw_usb_smsc, OID_AUTO, debug, CTLFLAG_RWTUN, &smsc_debug, 0,
124 * Various supported device vendors/products.
126 static const struct usb_device_id smsc_devs[] = {
127 #define SMSC_DEV(p,i) { USB_VPI(USB_VENDOR_SMC2, USB_PRODUCT_SMC2_##p, i) }
128 SMSC_DEV(LAN89530_ETH, 0),
129 SMSC_DEV(LAN9500_ETH, 0),
130 SMSC_DEV(LAN9500_ETH_2, 0),
131 SMSC_DEV(LAN9500A_ETH, 0),
132 SMSC_DEV(LAN9500A_ETH_2, 0),
133 SMSC_DEV(LAN9505_ETH, 0),
134 SMSC_DEV(LAN9505A_ETH, 0),
135 SMSC_DEV(LAN9514_ETH, 0),
136 SMSC_DEV(LAN9514_ETH_2, 0),
137 SMSC_DEV(LAN9530_ETH, 0),
138 SMSC_DEV(LAN9730_ETH, 0),
139 SMSC_DEV(LAN9500_SAL10, 0),
140 SMSC_DEV(LAN9505_SAL10, 0),
141 SMSC_DEV(LAN9500A_SAL10, 0),
142 SMSC_DEV(LAN9505A_SAL10, 0),
143 SMSC_DEV(LAN9514_SAL10, 0),
144 SMSC_DEV(LAN9500A_HAL, 0),
145 SMSC_DEV(LAN9505A_HAL, 0),
151 #define smsc_dbg_printf(sc, fmt, args...) \
153 if (smsc_debug > 0) \
154 device_printf((sc)->sc_ue.ue_dev, "debug: " fmt, ##args); \
157 #define smsc_dbg_printf(sc, fmt, args...) do { } while (0)
160 #define smsc_warn_printf(sc, fmt, args...) \
161 device_printf((sc)->sc_ue.ue_dev, "warning: " fmt, ##args)
163 #define smsc_err_printf(sc, fmt, args...) \
164 device_printf((sc)->sc_ue.ue_dev, "error: " fmt, ##args)
167 #define ETHER_IS_ZERO(addr) \
168 (!(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]))
170 #define ETHER_IS_VALID(addr) \
171 (!ETHER_IS_MULTICAST(addr) && !ETHER_IS_ZERO(addr))
173 static device_probe_t smsc_probe;
174 static device_attach_t smsc_attach;
175 static device_detach_t smsc_detach;
177 static usb_callback_t smsc_bulk_read_callback;
178 static usb_callback_t smsc_bulk_write_callback;
180 static miibus_readreg_t smsc_miibus_readreg;
181 static miibus_writereg_t smsc_miibus_writereg;
182 static miibus_statchg_t smsc_miibus_statchg;
184 #if __FreeBSD_version > 1000000
185 static int smsc_attach_post_sub(struct usb_ether *ue);
187 static uether_fn_t smsc_attach_post;
188 static uether_fn_t smsc_init;
189 static uether_fn_t smsc_stop;
190 static uether_fn_t smsc_start;
191 static uether_fn_t smsc_tick;
192 static uether_fn_t smsc_setmulti;
193 static uether_fn_t smsc_setpromisc;
195 static int smsc_ifmedia_upd(struct ifnet *);
196 static void smsc_ifmedia_sts(struct ifnet *, struct ifmediareq *);
198 static int smsc_chip_init(struct smsc_softc *sc);
199 static int smsc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
201 static const struct usb_config smsc_config[SMSC_N_TRANSFER] = {
203 [SMSC_BULK_DT_WR] = {
205 .endpoint = UE_ADDR_ANY,
206 .direction = UE_DIR_OUT,
208 .bufsize = 16 * (MCLBYTES + 16),
209 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
210 .callback = smsc_bulk_write_callback,
211 .timeout = 10000, /* 10 seconds */
214 [SMSC_BULK_DT_RD] = {
216 .endpoint = UE_ADDR_ANY,
217 .direction = UE_DIR_IN,
218 .bufsize = 20480, /* bytes */
219 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
220 .callback = smsc_bulk_read_callback,
221 .timeout = 0, /* no timeout */
224 /* The SMSC chip supports an interrupt endpoints, however they aren't
225 * needed as we poll on the MII status.
229 static const struct usb_ether_methods smsc_ue_methods = {
230 .ue_attach_post = smsc_attach_post,
231 #if __FreeBSD_version > 1000000
232 .ue_attach_post_sub = smsc_attach_post_sub,
234 .ue_start = smsc_start,
235 .ue_ioctl = smsc_ioctl,
236 .ue_init = smsc_init,
237 .ue_stop = smsc_stop,
238 .ue_tick = smsc_tick,
239 .ue_setmulti = smsc_setmulti,
240 .ue_setpromisc = smsc_setpromisc,
241 .ue_mii_upd = smsc_ifmedia_upd,
242 .ue_mii_sts = smsc_ifmedia_sts,
246 * smsc_read_reg - Reads a 32-bit register on the device
247 * @sc: driver soft context
248 * @off: offset of the register
249 * @data: pointer a value that will be populated with the register value
252 * The device lock must be held before calling this function.
255 * 0 on success, a USB_ERR_?? error code on failure.
258 smsc_read_reg(struct smsc_softc *sc, uint32_t off, uint32_t *data)
260 struct usb_device_request req;
264 SMSC_LOCK_ASSERT(sc, MA_OWNED);
266 req.bmRequestType = UT_READ_VENDOR_DEVICE;
267 req.bRequest = SMSC_UR_READ_REG;
268 USETW(req.wValue, 0);
269 USETW(req.wIndex, off);
270 USETW(req.wLength, 4);
272 err = uether_do_request(&sc->sc_ue, &req, &buf, 1000);
274 smsc_warn_printf(sc, "Failed to read register 0x%0x\n", off);
276 *data = le32toh(buf);
282 * smsc_write_reg - Writes a 32-bit register on the device
283 * @sc: driver soft context
284 * @off: offset of the register
285 * @data: the 32-bit value to write into the register
288 * The device lock must be held before calling this function.
291 * 0 on success, a USB_ERR_?? error code on failure.
294 smsc_write_reg(struct smsc_softc *sc, uint32_t off, uint32_t data)
296 struct usb_device_request req;
300 SMSC_LOCK_ASSERT(sc, MA_OWNED);
304 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
305 req.bRequest = SMSC_UR_WRITE_REG;
306 USETW(req.wValue, 0);
307 USETW(req.wIndex, off);
308 USETW(req.wLength, 4);
310 err = uether_do_request(&sc->sc_ue, &req, &buf, 1000);
312 smsc_warn_printf(sc, "Failed to write register 0x%0x\n", off);
318 * smsc_wait_for_bits - Polls on a register value until bits are cleared
320 * @reg: offset of the register
321 * @bits: if the bits are clear the function returns
324 * The device lock must be held before calling this function.
327 * 0 on success, or a USB_ERR_?? error code on failure.
330 smsc_wait_for_bits(struct smsc_softc *sc, uint32_t reg, uint32_t bits)
332 usb_ticks_t start_ticks;
333 const usb_ticks_t max_ticks = USB_MS_TO_TICKS(1000);
337 SMSC_LOCK_ASSERT(sc, MA_OWNED);
339 start_ticks = (usb_ticks_t)ticks;
341 if ((err = smsc_read_reg(sc, reg, &val)) != 0)
346 uether_pause(&sc->sc_ue, hz / 100);
347 } while (((usb_ticks_t)(ticks - start_ticks)) < max_ticks);
349 return (USB_ERR_TIMEOUT);
353 * smsc_eeprom_read - Reads the attached EEPROM
355 * @off: the eeprom address offset
356 * @buf: stores the bytes
357 * @buflen: the number of bytes to read
359 * Simply reads bytes from an attached eeprom.
362 * The function takes and releases the device lock if it is not already held.
365 * 0 on success, or a USB_ERR_?? error code on failure.
368 smsc_eeprom_read(struct smsc_softc *sc, uint16_t off, uint8_t *buf, uint16_t buflen)
370 usb_ticks_t start_ticks;
371 const usb_ticks_t max_ticks = USB_MS_TO_TICKS(1000);
377 locked = mtx_owned(&sc->sc_mtx);
381 err = smsc_wait_for_bits(sc, SMSC_EEPROM_CMD, SMSC_EEPROM_CMD_BUSY);
383 smsc_warn_printf(sc, "eeprom busy, failed to read data\n");
387 /* start reading the bytes, one at a time */
388 for (i = 0; i < buflen; i++) {
390 val = SMSC_EEPROM_CMD_BUSY | (SMSC_EEPROM_CMD_ADDR_MASK & (off + i));
391 if ((err = smsc_write_reg(sc, SMSC_EEPROM_CMD, val)) != 0)
394 start_ticks = (usb_ticks_t)ticks;
396 if ((err = smsc_read_reg(sc, SMSC_EEPROM_CMD, &val)) != 0)
398 if (!(val & SMSC_EEPROM_CMD_BUSY) || (val & SMSC_EEPROM_CMD_TIMEOUT))
401 uether_pause(&sc->sc_ue, hz / 100);
402 } while (((usb_ticks_t)(ticks - start_ticks)) < max_ticks);
404 if (val & (SMSC_EEPROM_CMD_BUSY | SMSC_EEPROM_CMD_TIMEOUT)) {
405 smsc_warn_printf(sc, "eeprom command failed\n");
406 err = USB_ERR_IOERROR;
410 if ((err = smsc_read_reg(sc, SMSC_EEPROM_DATA, &val)) != 0)
413 buf[i] = (val & 0xff);
424 * smsc_miibus_readreg - Reads a MII/MDIO register
425 * @dev: usb ether device
426 * @phy: the number of phy reading from
427 * @reg: the register address
429 * Attempts to read a phy register over the MII bus.
432 * Takes and releases the device mutex lock if not already held.
435 * Returns the 16-bits read from the MII register, if this function fails 0
439 smsc_miibus_readreg(device_t dev, int phy, int reg)
441 struct smsc_softc *sc = device_get_softc(dev);
446 locked = mtx_owned(&sc->sc_mtx);
450 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
451 smsc_warn_printf(sc, "MII is busy\n");
455 addr = (phy << 11) | (reg << 6) | SMSC_MII_READ | SMSC_MII_BUSY;
456 smsc_write_reg(sc, SMSC_MII_ADDR, addr);
458 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0)
459 smsc_warn_printf(sc, "MII read timeout\n");
461 smsc_read_reg(sc, SMSC_MII_DATA, &val);
468 return (val & 0xFFFF);
472 * smsc_miibus_writereg - Writes a MII/MDIO register
473 * @dev: usb ether device
474 * @phy: the number of phy writing to
475 * @reg: the register address
476 * @val: the value to write
478 * Attempts to write a phy register over the MII bus.
481 * Takes and releases the device mutex lock if not already held.
484 * Always returns 0 regardless of success or failure.
487 smsc_miibus_writereg(device_t dev, int phy, int reg, int val)
489 struct smsc_softc *sc = device_get_softc(dev);
493 if (sc->sc_phyno != phy)
496 locked = mtx_owned(&sc->sc_mtx);
500 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
501 smsc_warn_printf(sc, "MII is busy\n");
506 smsc_write_reg(sc, SMSC_MII_DATA, val);
508 addr = (phy << 11) | (reg << 6) | SMSC_MII_WRITE | SMSC_MII_BUSY;
509 smsc_write_reg(sc, SMSC_MII_ADDR, addr);
511 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0)
512 smsc_warn_printf(sc, "MII write timeout\n");
523 * smsc_miibus_statchg - Called to detect phy status change
524 * @dev: usb ether device
526 * This function is called periodically by the system to poll for status
527 * changes of the link.
530 * Takes and releases the device mutex lock if not already held.
533 smsc_miibus_statchg(device_t dev)
535 struct smsc_softc *sc = device_get_softc(dev);
536 struct mii_data *mii = uether_getmii(&sc->sc_ue);
543 locked = mtx_owned(&sc->sc_mtx);
547 ifp = uether_getifp(&sc->sc_ue);
548 if (mii == NULL || ifp == NULL ||
549 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
552 /* Use the MII status to determine link status */
553 sc->sc_flags &= ~SMSC_FLAG_LINK;
554 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
555 (IFM_ACTIVE | IFM_AVALID)) {
556 switch (IFM_SUBTYPE(mii->mii_media_active)) {
559 sc->sc_flags |= SMSC_FLAG_LINK;
562 /* Gigabit ethernet not supported by chipset */
569 /* Lost link, do nothing. */
570 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) {
571 smsc_dbg_printf(sc, "link flag not set\n");
575 err = smsc_read_reg(sc, SMSC_AFC_CFG, &afc_cfg);
577 smsc_warn_printf(sc, "failed to read initial AFC_CFG, error %d\n", err);
581 /* Enable/disable full duplex operation and TX/RX pause */
582 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
583 smsc_dbg_printf(sc, "full duplex operation\n");
584 sc->sc_mac_csr &= ~SMSC_MAC_CSR_RCVOWN;
585 sc->sc_mac_csr |= SMSC_MAC_CSR_FDPX;
587 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
592 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
598 smsc_dbg_printf(sc, "half duplex operation\n");
599 sc->sc_mac_csr &= ~SMSC_MAC_CSR_FDPX;
600 sc->sc_mac_csr |= SMSC_MAC_CSR_RCVOWN;
606 err = smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
607 err += smsc_write_reg(sc, SMSC_FLOW, flow);
608 err += smsc_write_reg(sc, SMSC_AFC_CFG, afc_cfg);
610 smsc_warn_printf(sc, "media change failed, error %d\n", err);
618 * smsc_ifmedia_upd - Set media options
619 * @ifp: interface pointer
621 * Basically boilerplate code that simply calls the mii functions to set the
625 * The device lock must be held before this function is called.
628 * Returns 0 on success or a negative error code.
631 smsc_ifmedia_upd(struct ifnet *ifp)
633 struct smsc_softc *sc = ifp->if_softc;
634 struct mii_data *mii = uether_getmii(&sc->sc_ue);
635 struct mii_softc *miisc;
638 SMSC_LOCK_ASSERT(sc, MA_OWNED);
640 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
642 err = mii_mediachg(mii);
647 * smsc_ifmedia_sts - Report current media status
648 * @ifp: inet interface pointer
649 * @ifmr: interface media request
651 * Basically boilerplate code that simply calls the mii functions to get the
655 * Internally takes and releases the device lock.
658 smsc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
660 struct smsc_softc *sc = ifp->if_softc;
661 struct mii_data *mii = uether_getmii(&sc->sc_ue);
665 ifmr->ifm_active = mii->mii_media_active;
666 ifmr->ifm_status = mii->mii_media_status;
671 * smsc_hash - Calculate the hash of a mac address
672 * @addr: The mac address to calculate the hash on
674 * This function is used when configuring a range of m'cast mac addresses to
675 * filter on. The hash of the mac address is put in the device's mac hash
679 * Returns a value from 0-63 value which is the hash of the mac address.
681 static inline uint32_t
682 smsc_hash(uint8_t addr[ETHER_ADDR_LEN])
684 return (ether_crc32_be(addr, ETHER_ADDR_LEN) >> 26) & 0x3f;
688 * smsc_setmulti - Setup multicast
689 * @ue: usb ethernet device context
691 * Tells the device to either accept frames with a multicast mac address, a
692 * select group of m'cast mac addresses or just the devices mac address.
695 * Should be called with the SMSC lock held.
698 smsc_setmulti(struct usb_ether *ue)
700 struct smsc_softc *sc = uether_getsc(ue);
701 struct ifnet *ifp = uether_getifp(ue);
702 struct ifmultiaddr *ifma;
703 uint32_t hashtbl[2] = { 0, 0 };
706 SMSC_LOCK_ASSERT(sc, MA_OWNED);
708 if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
709 smsc_dbg_printf(sc, "receive all multicast enabled\n");
710 sc->sc_mac_csr |= SMSC_MAC_CSR_MCPAS;
711 sc->sc_mac_csr &= ~SMSC_MAC_CSR_HPFILT;
714 /* Take the lock of the mac address list before hashing each of them */
717 if (!CK_STAILQ_EMPTY(&ifp->if_multiaddrs)) {
718 /* We are filtering on a set of address so calculate hashes of each
719 * of the address and set the corresponding bits in the register.
721 sc->sc_mac_csr |= SMSC_MAC_CSR_HPFILT;
722 sc->sc_mac_csr &= ~(SMSC_MAC_CSR_PRMS | SMSC_MAC_CSR_MCPAS);
724 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
725 if (ifma->ifma_addr->sa_family != AF_LINK)
728 hash = smsc_hash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
729 hashtbl[hash >> 5] |= 1 << (hash & 0x1F);
732 /* Only receive packets with destination set to our mac address */
733 sc->sc_mac_csr &= ~(SMSC_MAC_CSR_MCPAS | SMSC_MAC_CSR_HPFILT);
736 if_maddr_runlock(ifp);
739 if (sc->sc_mac_csr & SMSC_MAC_CSR_HPFILT)
740 smsc_dbg_printf(sc, "receive select group of macs\n");
742 smsc_dbg_printf(sc, "receive own packets only\n");
745 /* Write the hash table and mac control registers */
746 smsc_write_reg(sc, SMSC_HASHH, hashtbl[1]);
747 smsc_write_reg(sc, SMSC_HASHL, hashtbl[0]);
748 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
753 * smsc_setpromisc - Enables/disables promiscuous mode
754 * @ue: usb ethernet device context
757 * Should be called with the SMSC lock held.
760 smsc_setpromisc(struct usb_ether *ue)
762 struct smsc_softc *sc = uether_getsc(ue);
763 struct ifnet *ifp = uether_getifp(ue);
765 smsc_dbg_printf(sc, "promiscuous mode %sabled\n",
766 (ifp->if_flags & IFF_PROMISC) ? "en" : "dis");
768 SMSC_LOCK_ASSERT(sc, MA_OWNED);
770 if (ifp->if_flags & IFF_PROMISC)
771 sc->sc_mac_csr |= SMSC_MAC_CSR_PRMS;
773 sc->sc_mac_csr &= ~SMSC_MAC_CSR_PRMS;
775 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
780 * smsc_sethwcsum - Enable or disable H/W UDP and TCP checksumming
781 * @sc: driver soft context
784 * Should be called with the SMSC lock held.
787 * Returns 0 on success or a negative error code.
789 static int smsc_sethwcsum(struct smsc_softc *sc)
791 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
798 SMSC_LOCK_ASSERT(sc, MA_OWNED);
800 err = smsc_read_reg(sc, SMSC_COE_CTRL, &val);
802 smsc_warn_printf(sc, "failed to read SMSC_COE_CTRL (err=%d)\n", err);
806 /* Enable/disable the Rx checksum */
807 if ((ifp->if_capabilities & ifp->if_capenable) & IFCAP_RXCSUM)
808 val |= SMSC_COE_CTRL_RX_EN;
810 val &= ~SMSC_COE_CTRL_RX_EN;
812 /* Enable/disable the Tx checksum (currently not supported) */
813 if ((ifp->if_capabilities & ifp->if_capenable) & IFCAP_TXCSUM)
814 val |= SMSC_COE_CTRL_TX_EN;
816 val &= ~SMSC_COE_CTRL_TX_EN;
818 err = smsc_write_reg(sc, SMSC_COE_CTRL, val);
820 smsc_warn_printf(sc, "failed to write SMSC_COE_CTRL (err=%d)\n", err);
828 * smsc_setmacaddress - Sets the mac address in the device
829 * @sc: driver soft context
830 * @addr: pointer to array contain at least 6 bytes of the mac
832 * Writes the MAC address into the device, usually the MAC is programmed with
833 * values from the EEPROM.
836 * Should be called with the SMSC lock held.
839 * Returns 0 on success or a negative error code.
842 smsc_setmacaddress(struct smsc_softc *sc, const uint8_t *addr)
847 smsc_dbg_printf(sc, "setting mac address to %02x:%02x:%02x:%02x:%02x:%02x\n",
848 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
850 SMSC_LOCK_ASSERT(sc, MA_OWNED);
852 val = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
853 if ((err = smsc_write_reg(sc, SMSC_MAC_ADDRL, val)) != 0)
856 val = (addr[5] << 8) | addr[4];
857 err = smsc_write_reg(sc, SMSC_MAC_ADDRH, val);
864 * smsc_reset - Reset the SMSC chip
865 * @sc: device soft context
868 * Should be called with the SMSC lock held.
871 smsc_reset(struct smsc_softc *sc)
873 struct usb_config_descriptor *cd;
876 cd = usbd_get_config_descriptor(sc->sc_ue.ue_udev);
878 err = usbd_req_set_config(sc->sc_ue.ue_udev, &sc->sc_mtx,
879 cd->bConfigurationValue);
881 smsc_warn_printf(sc, "reset failed (ignored)\n");
883 /* Wait a little while for the chip to get its brains in order. */
884 uether_pause(&sc->sc_ue, hz / 100);
886 /* Reinitialize controller to achieve full reset. */
892 * smsc_init - Initialises the LAN95xx chip
893 * @ue: USB ether interface
895 * Called when the interface is brought up (i.e. ifconfig ue0 up), this
896 * initialise the interface and the rx/tx pipes.
899 * Should be called with the SMSC lock held.
902 smsc_init(struct usb_ether *ue)
904 struct smsc_softc *sc = uether_getsc(ue);
905 struct ifnet *ifp = uether_getifp(ue);
907 SMSC_LOCK_ASSERT(sc, MA_OWNED);
909 if (smsc_setmacaddress(sc, IF_LLADDR(ifp)))
910 smsc_dbg_printf(sc, "setting MAC address failed\n");
912 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
915 /* Cancel pending I/O */
918 #if __FreeBSD_version <= 1000000
919 /* On earlier versions this was the first place we could tell the system
920 * that we supported h/w csuming, however this is only called after the
921 * the interface has been brought up - not ideal.
923 if (!(ifp->if_capabilities & IFCAP_RXCSUM)) {
924 ifp->if_capabilities |= IFCAP_RXCSUM;
925 ifp->if_capenable |= IFCAP_RXCSUM;
926 ifp->if_hwassist = 0;
929 /* TX checksuming is disabled for now
930 ifp->if_capabilities |= IFCAP_TXCSUM;
931 ifp->if_capenable |= IFCAP_TXCSUM;
932 ifp->if_hwassist = CSUM_TCP | CSUM_UDP;
936 /* Reset the ethernet interface. */
939 /* Load the multicast filter. */
942 /* TCP/UDP checksum offload engines. */
945 usbd_xfer_set_stall(sc->sc_xfer[SMSC_BULK_DT_WR]);
947 /* Indicate we are up and running. */
948 ifp->if_drv_flags |= IFF_DRV_RUNNING;
950 /* Switch to selected media. */
951 smsc_ifmedia_upd(ifp);
956 * smsc_bulk_read_callback - Read callback used to process the USB URB
957 * @xfer: the USB transfer
960 * Reads the URB data which can contain one or more ethernet frames, the
961 * frames are copyed into a mbuf and given to the system.
964 * No locking required, doesn't access internal driver settings.
967 smsc_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
969 struct smsc_softc *sc = usbd_xfer_softc(xfer);
970 struct usb_ether *ue = &sc->sc_ue;
971 struct ifnet *ifp = uether_getifp(ue);
973 struct usb_page_cache *pc;
979 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
980 smsc_dbg_printf(sc, "rx : actlen %d\n", actlen);
982 switch (USB_GET_STATE(xfer)) {
983 case USB_ST_TRANSFERRED:
985 /* There is always a zero length frame after bringing the IF up */
986 if (actlen < (sizeof(rxhdr) + ETHER_CRC_LEN))
989 /* There maybe multiple packets in the USB frame, each will have a
990 * header and each needs to have it's own mbuf allocated and populated
993 pc = usbd_xfer_get_frame(xfer, 0);
996 while (off < actlen) {
998 /* The frame header is always aligned on a 4 byte boundary */
999 off = ((off + 0x3) & ~0x3);
1001 usbd_copy_out(pc, off, &rxhdr, sizeof(rxhdr));
1002 off += (sizeof(rxhdr) + ETHER_ALIGN);
1003 rxhdr = le32toh(rxhdr);
1005 pktlen = (uint16_t)SMSC_RX_STAT_FRM_LENGTH(rxhdr);
1007 smsc_dbg_printf(sc, "rx : rxhdr 0x%08x : pktlen %d : actlen %d : "
1008 "off %d\n", rxhdr, pktlen, actlen, off);
1011 if (rxhdr & SMSC_RX_STAT_ERROR) {
1012 smsc_dbg_printf(sc, "rx error (hdr 0x%08x)\n", rxhdr);
1013 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1014 if (rxhdr & SMSC_RX_STAT_COLLISION)
1015 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
1018 /* Check if the ethernet frame is too big or too small */
1019 if ((pktlen < ETHER_HDR_LEN) || (pktlen > (actlen - off)))
1022 /* Create a new mbuf to store the packet in */
1023 m = uether_newbuf();
1025 smsc_warn_printf(sc, "failed to create new mbuf\n");
1026 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1030 usbd_copy_out(pc, off, mtod(m, uint8_t *), pktlen);
1032 /* Check if RX TCP/UDP checksumming is being offloaded */
1033 if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) {
1035 struct ether_header *eh;
1037 eh = mtod(m, struct ether_header *);
1039 /* Remove the extra 2 bytes of the csum */
1042 /* The checksum appears to be simplistically calculated
1043 * over the udp/tcp header and data up to the end of the
1044 * eth frame. Which means if the eth frame is padded
1045 * the csum calculation is incorrectly performed over
1046 * the padding bytes as well. Therefore to be safe we
1047 * ignore the H/W csum on frames less than or equal to
1050 * Ignore H/W csum for non-IPv4 packets.
1052 if ((be16toh(eh->ether_type) == ETHERTYPE_IP) &&
1053 (pktlen > ETHER_MIN_LEN)) {
1056 ip = (struct ip *)(eh + 1);
1057 if ((ip->ip_v == IPVERSION) &&
1058 ((ip->ip_p == IPPROTO_TCP) ||
1059 (ip->ip_p == IPPROTO_UDP))) {
1060 /* Indicate the UDP/TCP csum has been calculated */
1061 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
1063 /* Copy the TCP/UDP checksum from the last 2 bytes
1064 * of the transfer and put in the csum_data field.
1066 usbd_copy_out(pc, (off + pktlen),
1067 &m->m_pkthdr.csum_data, 2);
1069 /* The data is copied in network order, but the
1070 * csum algorithm in the kernel expects it to be
1071 * in host network order.
1073 m->m_pkthdr.csum_data = ntohs(m->m_pkthdr.csum_data);
1075 smsc_dbg_printf(sc, "RX checksum offloaded (0x%04x)\n",
1076 m->m_pkthdr.csum_data);
1080 /* Need to adjust the offset as well or we'll be off
1081 * by 2 because the csum is removed from the packet
1087 /* Finally enqueue the mbuf on the receive queue */
1088 /* Remove 4 trailing bytes */
1089 if (pktlen < (4 + ETHER_HDR_LEN)) {
1093 uether_rxmbuf(ue, m, pktlen - 4);
1096 /* Update the offset to move to the next potential packet */
1104 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1105 usbd_transfer_submit(xfer);
1110 if (error != USB_ERR_CANCELLED) {
1111 smsc_warn_printf(sc, "bulk read error, %s\n", usbd_errstr(error));
1112 usbd_xfer_set_stall(xfer);
1120 * smsc_bulk_write_callback - Write callback used to send ethernet frame(s)
1121 * @xfer: the USB transfer
1122 * @error: error code if the transfers is in an errored state
1124 * The main write function that pulls ethernet frames off the queue and sends
1131 smsc_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
1133 struct smsc_softc *sc = usbd_xfer_softc(xfer);
1134 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1135 struct usb_page_cache *pc;
1138 uint32_t frm_len = 0;
1141 switch (USB_GET_STATE(xfer)) {
1142 case USB_ST_TRANSFERRED:
1143 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1148 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0 ||
1149 (ifp->if_drv_flags & IFF_DRV_OACTIVE) != 0) {
1150 /* Don't send anything if there is no link or controller is busy. */
1154 for (nframes = 0; nframes < 16 &&
1155 !IFQ_DRV_IS_EMPTY(&ifp->if_snd); nframes++) {
1156 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1159 usbd_xfer_set_frame_offset(xfer, nframes * MCLBYTES,
1162 pc = usbd_xfer_get_frame(xfer, nframes);
1164 /* Each frame is prefixed with two 32-bit values describing the
1165 * length of the packet and buffer.
1167 txhdr = SMSC_TX_CTRL_0_BUF_SIZE(m->m_pkthdr.len) |
1168 SMSC_TX_CTRL_0_FIRST_SEG | SMSC_TX_CTRL_0_LAST_SEG;
1169 txhdr = htole32(txhdr);
1170 usbd_copy_in(pc, 0, &txhdr, sizeof(txhdr));
1172 txhdr = SMSC_TX_CTRL_1_PKT_LENGTH(m->m_pkthdr.len);
1173 txhdr = htole32(txhdr);
1174 usbd_copy_in(pc, 4, &txhdr, sizeof(txhdr));
1178 /* Next copy in the actual packet */
1179 usbd_m_copy_in(pc, frm_len, m, 0, m->m_pkthdr.len);
1180 frm_len += m->m_pkthdr.len;
1182 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1184 /* If there's a BPF listener, bounce a copy of this frame to him */
1189 /* Set frame length. */
1190 usbd_xfer_set_frame_len(xfer, nframes, frm_len);
1193 usbd_xfer_set_frames(xfer, nframes);
1194 usbd_transfer_submit(xfer);
1195 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1200 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1201 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1203 if (error != USB_ERR_CANCELLED) {
1204 smsc_err_printf(sc, "usb error on tx: %s\n", usbd_errstr(error));
1205 usbd_xfer_set_stall(xfer);
1213 * smsc_tick - Called periodically to monitor the state of the LAN95xx chip
1214 * @ue: USB ether interface
1216 * Simply calls the mii status functions to check the state of the link.
1219 * Should be called with the SMSC lock held.
1222 smsc_tick(struct usb_ether *ue)
1224 struct smsc_softc *sc = uether_getsc(ue);
1225 struct mii_data *mii = uether_getmii(&sc->sc_ue);
1227 SMSC_LOCK_ASSERT(sc, MA_OWNED);
1230 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) {
1231 smsc_miibus_statchg(ue->ue_dev);
1232 if ((sc->sc_flags & SMSC_FLAG_LINK) != 0)
1238 * smsc_start - Starts communication with the LAN95xx chip
1239 * @ue: USB ether interface
1245 smsc_start(struct usb_ether *ue)
1247 struct smsc_softc *sc = uether_getsc(ue);
1250 * start the USB transfers, if not already started:
1252 usbd_transfer_start(sc->sc_xfer[SMSC_BULK_DT_RD]);
1253 usbd_transfer_start(sc->sc_xfer[SMSC_BULK_DT_WR]);
1257 * smsc_stop - Stops communication with the LAN95xx chip
1258 * @ue: USB ether interface
1264 smsc_stop(struct usb_ether *ue)
1266 struct smsc_softc *sc = uether_getsc(ue);
1267 struct ifnet *ifp = uether_getifp(ue);
1269 SMSC_LOCK_ASSERT(sc, MA_OWNED);
1271 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1272 sc->sc_flags &= ~SMSC_FLAG_LINK;
1275 * stop all the transfers, if not already stopped:
1277 usbd_transfer_stop(sc->sc_xfer[SMSC_BULK_DT_WR]);
1278 usbd_transfer_stop(sc->sc_xfer[SMSC_BULK_DT_RD]);
1282 * smsc_phy_init - Initialises the in-built SMSC phy
1283 * @sc: driver soft context
1285 * Resets the PHY part of the chip and then initialises it to default
1286 * values. The 'link down' and 'auto-negotiation complete' interrupts
1287 * from the PHY are also enabled, however we don't monitor the interrupt
1288 * endpoints for the moment.
1291 * Returns 0 on success or EIO if failed to reset the PHY.
1294 smsc_phy_init(struct smsc_softc *sc)
1297 usb_ticks_t start_ticks;
1298 const usb_ticks_t max_ticks = USB_MS_TO_TICKS(1000);
1300 SMSC_LOCK_ASSERT(sc, MA_OWNED);
1302 /* Reset phy and wait for reset to complete */
1303 smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR, BMCR_RESET);
1305 start_ticks = ticks;
1307 uether_pause(&sc->sc_ue, hz / 100);
1308 bmcr = smsc_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR);
1309 } while ((bmcr & BMCR_RESET) && ((ticks - start_ticks) < max_ticks));
1311 if (((usb_ticks_t)(ticks - start_ticks)) >= max_ticks) {
1312 smsc_err_printf(sc, "PHY reset timed-out");
1316 smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_ANAR,
1317 ANAR_10 | ANAR_10_FD | ANAR_TX | ANAR_TX_FD | /* all modes */
1322 /* Setup the phy to interrupt when the link goes down or autoneg completes */
1323 smsc_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, SMSC_PHY_INTR_STAT);
1324 smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, SMSC_PHY_INTR_MASK,
1325 (SMSC_PHY_INTR_ANEG_COMP | SMSC_PHY_INTR_LINK_DOWN));
1327 /* Restart auto-negotation */
1328 bmcr = smsc_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR);
1329 bmcr |= BMCR_STARTNEG;
1330 smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR, bmcr);
1337 * smsc_chip_init - Initialises the chip after power on
1338 * @sc: driver soft context
1340 * This initialisation sequence is modelled on the procedure in the Linux
1344 * Returns 0 on success or an error code on failure.
1347 smsc_chip_init(struct smsc_softc *sc)
1354 locked = mtx_owned(&sc->sc_mtx);
1358 /* Enter H/W config mode */
1359 smsc_write_reg(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST);
1361 if ((err = smsc_wait_for_bits(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST)) != 0) {
1362 smsc_warn_printf(sc, "timed-out waiting for reset to complete\n");
1367 smsc_write_reg(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST);
1369 if ((err = smsc_wait_for_bits(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST)) != 0) {
1370 smsc_warn_printf(sc, "timed-out waiting for phy reset to complete\n");
1374 /* Set the mac address */
1375 if ((err = smsc_setmacaddress(sc, sc->sc_ue.ue_eaddr)) != 0) {
1376 smsc_warn_printf(sc, "failed to set the MAC address\n");
1380 /* Don't know what the HW_CFG_BIR bit is, but following the reset sequence
1381 * as used in the Linux driver.
1383 if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) != 0) {
1384 smsc_warn_printf(sc, "failed to read HW_CFG: %d\n", err);
1387 reg_val |= SMSC_HW_CFG_BIR;
1388 smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
1390 /* There is a so called 'turbo mode' that the linux driver supports, it
1391 * seems to allow you to jam multiple frames per Rx transaction. By default
1392 * this driver supports that and therefore allows multiple frames per URB.
1394 * The xfer buffer size needs to reflect this as well, therefore based on
1395 * the calculations in the Linux driver the RX bufsize is set to 18944,
1396 * bufsz = (16 * 1024 + 5 * 512)
1398 * Burst capability is the number of URBs that can be in a burst of data/
1401 if (usbd_get_speed(sc->sc_ue.ue_udev) == USB_SPEED_HIGH)
1406 smsc_write_reg(sc, SMSC_BURST_CAP, burst_cap);
1408 /* Set the default bulk in delay (magic value from Linux driver) */
1409 smsc_write_reg(sc, SMSC_BULK_IN_DLY, 0x00002000);
1414 * Initialise the RX interface
1416 if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) < 0) {
1417 smsc_warn_printf(sc, "failed to read HW_CFG: (err = %d)\n", err);
1421 /* Adjust the packet offset in the buffer (designed to try and align IP
1422 * header on 4 byte boundary)
1424 reg_val &= ~SMSC_HW_CFG_RXDOFF;
1425 reg_val |= (ETHER_ALIGN << 9) & SMSC_HW_CFG_RXDOFF;
1427 /* The following setings are used for 'turbo mode', a.k.a multiple frames
1428 * per Rx transaction (again info taken form Linux driver).
1430 reg_val |= (SMSC_HW_CFG_MEF | SMSC_HW_CFG_BCE);
1432 smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
1434 /* Clear the status register ? */
1435 smsc_write_reg(sc, SMSC_INTR_STATUS, 0xffffffff);
1437 /* Read and display the revision register */
1438 if ((err = smsc_read_reg(sc, SMSC_ID_REV, &sc->sc_rev_id)) < 0) {
1439 smsc_warn_printf(sc, "failed to read ID_REV (err = %d)\n", err);
1443 device_printf(sc->sc_ue.ue_dev, "chip 0x%04lx, rev. %04lx\n",
1444 (sc->sc_rev_id & SMSC_ID_REV_CHIP_ID_MASK) >> 16,
1445 (sc->sc_rev_id & SMSC_ID_REV_CHIP_REV_MASK));
1447 /* GPIO/LED setup */
1448 reg_val = SMSC_LED_GPIO_CFG_SPD_LED | SMSC_LED_GPIO_CFG_LNK_LED |
1449 SMSC_LED_GPIO_CFG_FDX_LED;
1450 smsc_write_reg(sc, SMSC_LED_GPIO_CFG, reg_val);
1453 * Initialise the TX interface
1455 smsc_write_reg(sc, SMSC_FLOW, 0);
1457 smsc_write_reg(sc, SMSC_AFC_CFG, AFC_CFG_DEFAULT);
1459 /* Read the current MAC configuration */
1460 if ((err = smsc_read_reg(sc, SMSC_MAC_CSR, &sc->sc_mac_csr)) < 0) {
1461 smsc_warn_printf(sc, "failed to read MAC_CSR (err=%d)\n", err);
1466 smsc_write_reg(sc, SMSC_VLAN1, (uint32_t)ETHERTYPE_VLAN);
1469 * Initialise the PHY
1471 if ((err = smsc_phy_init(sc)) != 0)
1478 sc->sc_mac_csr |= SMSC_MAC_CSR_TXEN;
1479 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
1480 smsc_write_reg(sc, SMSC_TX_CFG, SMSC_TX_CFG_ON);
1485 sc->sc_mac_csr |= SMSC_MAC_CSR_RXEN;
1486 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
1497 smsc_err_printf(sc, "smsc_chip_init failed (err=%d)\n", err);
1503 * smsc_ioctl - ioctl function for the device
1504 * @ifp: interface pointer
1505 * @cmd: the ioctl command
1506 * @data: data passed in the ioctl call, typically a pointer to struct ifreq.
1508 * The ioctl routine is overridden to detect change requests for the H/W
1509 * checksum capabilities.
1512 * 0 on success and an error code on failure.
1515 smsc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1517 struct usb_ether *ue = ifp->if_softc;
1518 struct smsc_softc *sc;
1524 if (cmd == SIOCSIFCAP) {
1526 sc = uether_getsc(ue);
1527 ifr = (struct ifreq *)data;
1534 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1536 /* Modify the RX CSUM enable bits */
1537 if ((mask & IFCAP_RXCSUM) != 0 &&
1538 (ifp->if_capabilities & IFCAP_RXCSUM) != 0) {
1539 ifp->if_capenable ^= IFCAP_RXCSUM;
1541 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1542 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1549 #if __FreeBSD_version > 1000000
1556 rc = uether_ioctl(ifp, cmd, data);
1564 * This is FreeBSD-specific compatibility strings for RPi/RPi2
1567 smsc_fdt_find_eth_node(phandle_t start)
1569 phandle_t child, node;
1571 /* Traverse through entire tree to find usb ethernet nodes. */
1572 for (node = OF_child(start); node != 0; node = OF_peer(node)) {
1573 if ((ofw_bus_node_is_compatible(node, "net,ethernet") &&
1574 ofw_bus_node_is_compatible(node, "usb,device")) ||
1575 ofw_bus_node_is_compatible(node, "usb424,ec00"))
1577 child = smsc_fdt_find_eth_node(node);
1586 * Check if node's path is <*>/usb/hub/ethernet
1589 smsc_fdt_is_usb_eth(phandle_t node)
1594 memset(name, 0, sizeof(name));
1595 len = OF_getprop(node, "name", name, sizeof(name));
1599 if (strcmp(name, "ethernet"))
1602 node = OF_parent(node);
1605 len = OF_getprop(node, "name", name, sizeof(name));
1609 if (strcmp(name, "hub"))
1612 node = OF_parent(node);
1615 len = OF_getprop(node, "name", name, sizeof(name));
1619 if (strcmp(name, "usb"))
1626 smsc_fdt_find_eth_node_by_path(phandle_t start)
1628 phandle_t child, node;
1630 /* Traverse through entire tree to find usb ethernet nodes. */
1631 for (node = OF_child(start); node != 0; node = OF_peer(node)) {
1632 if (smsc_fdt_is_usb_eth(node))
1634 child = smsc_fdt_find_eth_node_by_path(node);
1643 * Look through known names that can contain mac address
1644 * return 0 if valid MAC address has been found
1647 smsc_fdt_read_mac_property(phandle_t node, unsigned char *mac)
1651 /* Check if there is property */
1652 if ((len = OF_getproplen(node, "local-mac-address")) > 0) {
1653 if (len != ETHER_ADDR_LEN)
1656 OF_getprop(node, "local-mac-address", mac,
1661 if ((len = OF_getproplen(node, "mac-address")) > 0) {
1662 if (len != ETHER_ADDR_LEN)
1665 OF_getprop(node, "mac-address", mac,
1674 * Get MAC address from FDT blob. Firmware or loader should fill
1675 * mac-address or local-mac-address property. Returns 0 if MAC address
1676 * obtained, error code otherwise.
1679 smsc_fdt_find_mac(unsigned char *mac)
1681 phandle_t node, root;
1683 root = OF_finddevice("/");
1684 node = smsc_fdt_find_eth_node(root);
1686 if (smsc_fdt_read_mac_property(node, mac) == 0)
1691 * If it's not FreeBSD FDT blob for RPi, try more
1692 * generic .../usb/hub/ethernet
1694 node = smsc_fdt_find_eth_node_by_path(root);
1697 return smsc_fdt_read_mac_property(node, mac);
1704 * smsc_attach_post - Called after the driver attached to the USB interface
1705 * @ue: the USB ethernet device
1707 * This is where the chip is intialised for the first time. This is different
1708 * from the smsc_init() function in that that one is designed to setup the
1709 * H/W to match the UE settings and can be called after a reset.
1714 smsc_attach_post(struct usb_ether *ue)
1716 struct smsc_softc *sc = uether_getsc(ue);
1717 uint32_t mac_h, mac_l;
1720 smsc_dbg_printf(sc, "smsc_attach_post\n");
1722 /* Setup some of the basics */
1726 /* Attempt to get the mac address, if an EEPROM is not attached this
1727 * will just return FF:FF:FF:FF:FF:FF, so in such cases we invent a MAC
1728 * address based on urandom.
1730 memset(sc->sc_ue.ue_eaddr, 0xff, ETHER_ADDR_LEN);
1732 /* Check if there is already a MAC address in the register */
1733 if ((smsc_read_reg(sc, SMSC_MAC_ADDRL, &mac_l) == 0) &&
1734 (smsc_read_reg(sc, SMSC_MAC_ADDRH, &mac_h) == 0)) {
1735 sc->sc_ue.ue_eaddr[5] = (uint8_t)((mac_h >> 8) & 0xff);
1736 sc->sc_ue.ue_eaddr[4] = (uint8_t)((mac_h) & 0xff);
1737 sc->sc_ue.ue_eaddr[3] = (uint8_t)((mac_l >> 24) & 0xff);
1738 sc->sc_ue.ue_eaddr[2] = (uint8_t)((mac_l >> 16) & 0xff);
1739 sc->sc_ue.ue_eaddr[1] = (uint8_t)((mac_l >> 8) & 0xff);
1740 sc->sc_ue.ue_eaddr[0] = (uint8_t)((mac_l) & 0xff);
1743 /* MAC address is not set so try to read from EEPROM, if that fails generate
1744 * a random MAC address.
1746 if (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr)) {
1748 err = smsc_eeprom_read(sc, 0x01, sc->sc_ue.ue_eaddr, ETHER_ADDR_LEN);
1750 if ((err != 0) || (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr)))
1751 err = smsc_fdt_find_mac(sc->sc_ue.ue_eaddr);
1753 if ((err != 0) || (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr))) {
1754 read_random(sc->sc_ue.ue_eaddr, ETHER_ADDR_LEN);
1755 sc->sc_ue.ue_eaddr[0] &= ~0x01; /* unicast */
1756 sc->sc_ue.ue_eaddr[0] |= 0x02; /* locally administered */
1760 /* Initialise the chip for the first time */
1766 * smsc_attach_post_sub - Called after the driver attached to the USB interface
1767 * @ue: the USB ethernet device
1769 * Most of this is boilerplate code and copied from the base USB ethernet
1770 * driver. It has been overriden so that we can indicate to the system that
1771 * the chip supports H/W checksumming.
1774 * Returns 0 on success or a negative error code.
1776 #if __FreeBSD_version > 1000000
1778 smsc_attach_post_sub(struct usb_ether *ue)
1780 struct smsc_softc *sc;
1784 sc = uether_getsc(ue);
1786 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1787 ifp->if_start = uether_start;
1788 ifp->if_ioctl = smsc_ioctl;
1789 ifp->if_init = uether_init;
1790 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
1791 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
1792 IFQ_SET_READY(&ifp->if_snd);
1794 /* The chip supports TCP/UDP checksum offloading on TX and RX paths, however
1795 * currently only RX checksum is supported in the driver (see top of file).
1797 ifp->if_capabilities |= IFCAP_RXCSUM | IFCAP_VLAN_MTU;
1798 ifp->if_hwassist = 0;
1800 /* TX checksuming is disabled (for now?)
1801 ifp->if_capabilities |= IFCAP_TXCSUM;
1802 ifp->if_capenable |= IFCAP_TXCSUM;
1803 ifp->if_hwassist = CSUM_TCP | CSUM_UDP;
1806 ifp->if_capenable = ifp->if_capabilities;
1809 error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp,
1810 uether_ifmedia_upd, ue->ue_methods->ue_mii_sts,
1811 BMSR_DEFCAPMASK, sc->sc_phyno, MII_OFFSET_ANY, 0);
1816 #endif /* __FreeBSD_version > 1000000 */
1820 * smsc_probe - Probe the interface.
1821 * @dev: smsc device handle
1823 * Checks if the device is a match for this driver.
1826 * Returns 0 on success or an error code on failure.
1829 smsc_probe(device_t dev)
1831 struct usb_attach_arg *uaa = device_get_ivars(dev);
1833 if (uaa->usb_mode != USB_MODE_HOST)
1835 if (uaa->info.bConfigIndex != SMSC_CONFIG_INDEX)
1837 if (uaa->info.bIfaceIndex != SMSC_IFACE_IDX)
1840 return (usbd_lookup_id_by_uaa(smsc_devs, sizeof(smsc_devs), uaa));
1845 * smsc_attach - Attach the interface.
1846 * @dev: smsc device handle
1848 * Allocate softc structures, do ifmedia setup and ethernet/BPF attach.
1851 * Returns 0 on success or a negative error code.
1854 smsc_attach(device_t dev)
1856 struct usb_attach_arg *uaa = device_get_ivars(dev);
1857 struct smsc_softc *sc = device_get_softc(dev);
1858 struct usb_ether *ue = &sc->sc_ue;
1859 uint8_t iface_index;
1862 sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
1864 device_set_usb_desc(dev);
1866 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
1868 /* Setup the endpoints for the SMSC LAN95xx device(s) */
1869 iface_index = SMSC_IFACE_IDX;
1870 err = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
1871 smsc_config, SMSC_N_TRANSFER, sc, &sc->sc_mtx);
1873 device_printf(dev, "error: allocating USB transfers failed\n");
1879 ue->ue_udev = uaa->device;
1880 ue->ue_mtx = &sc->sc_mtx;
1881 ue->ue_methods = &smsc_ue_methods;
1883 err = uether_ifattach(ue);
1885 device_printf(dev, "error: could not attach interface\n");
1888 return (0); /* success */
1892 return (ENXIO); /* failure */
1896 * smsc_detach - Detach the interface.
1897 * @dev: smsc device handle
1903 smsc_detach(device_t dev)
1905 struct smsc_softc *sc = device_get_softc(dev);
1906 struct usb_ether *ue = &sc->sc_ue;
1908 usbd_transfer_unsetup(sc->sc_xfer, SMSC_N_TRANSFER);
1909 uether_ifdetach(ue);
1910 mtx_destroy(&sc->sc_mtx);
1915 static device_method_t smsc_methods[] = {
1916 /* Device interface */
1917 DEVMETHOD(device_probe, smsc_probe),
1918 DEVMETHOD(device_attach, smsc_attach),
1919 DEVMETHOD(device_detach, smsc_detach),
1922 DEVMETHOD(bus_print_child, bus_generic_print_child),
1923 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
1926 DEVMETHOD(miibus_readreg, smsc_miibus_readreg),
1927 DEVMETHOD(miibus_writereg, smsc_miibus_writereg),
1928 DEVMETHOD(miibus_statchg, smsc_miibus_statchg),
1933 static driver_t smsc_driver = {
1935 .methods = smsc_methods,
1936 .size = sizeof(struct smsc_softc),
1939 static devclass_t smsc_devclass;
1941 DRIVER_MODULE(smsc, uhub, smsc_driver, smsc_devclass, NULL, 0);
1942 DRIVER_MODULE(miibus, smsc, miibus_driver, miibus_devclass, 0, 0);
1943 MODULE_DEPEND(smsc, uether, 1, 1, 1);
1944 MODULE_DEPEND(smsc, usb, 1, 1, 1);
1945 MODULE_DEPEND(smsc, ether, 1, 1, 1);
1946 MODULE_DEPEND(smsc, miibus, 1, 1, 1);
1947 MODULE_VERSION(smsc, 1);
1948 USB_PNP_HOST_INFO(smsc_devs);