3 * Ben Gray <bgray@freebsd.org>.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * SMSC LAN9xxx devices (http://www.smsc.com/)
33 * The LAN9500 & LAN9500A devices are stand-alone USB to Ethernet chips that
34 * support USB 2.0 and 10/100 Mbps Ethernet.
36 * The LAN951x devices are an integrated USB hub and USB to Ethernet adapter.
37 * The driver only covers the Ethernet part, the standard USB hub driver
38 * supports the hub part.
40 * This driver is closely modelled on the Linux driver written and copyrighted
46 * H/W TCP & UDP Checksum Offloading
47 * ---------------------------------
48 * The chip supports both tx and rx offloading of UDP & TCP checksums, this
49 * feature can be dynamically enabled/disabled.
51 * RX checksuming is performed across bytes after the IPv4 header to the end of
52 * the Ethernet frame, this means if the frame is padded with non-zero values
53 * the H/W checksum will be incorrect, however the rx code compensates for this.
55 * TX checksuming is more complicated, the device requires a special header to
56 * be prefixed onto the start of the frame which indicates the start and end
57 * positions of the UDP or TCP frame. This requires the driver to manually
58 * go through the packet data and decode the headers prior to sending.
59 * On Linux they generally provide cues to the location of the csum and the
60 * area to calculate it over, on FreeBSD we seem to have to do it all ourselves,
61 * hence this is not as optimal and therefore h/w tX checksum is currently not
65 #include <sys/stdint.h>
66 #include <sys/stddef.h>
67 #include <sys/param.h>
68 #include <sys/queue.h>
69 #include <sys/types.h>
70 #include <sys/systm.h>
71 #include <sys/kernel.h>
73 #include <sys/module.h>
75 #include <sys/mutex.h>
76 #include <sys/condvar.h>
77 #include <sys/sysctl.h>
79 #include <sys/unistd.h>
80 #include <sys/callout.h>
81 #include <sys/malloc.h>
83 #include <sys/random.h>
85 #include <netinet/in.h>
86 #include <netinet/ip.h>
88 #include "opt_platform.h"
91 #include <dev/fdt/fdt_common.h>
92 #include <dev/ofw/ofw_bus.h>
93 #include <dev/ofw/ofw_bus_subr.h>
96 #include <dev/usb/usb.h>
97 #include <dev/usb/usbdi.h>
98 #include <dev/usb/usbdi_util.h>
101 #define USB_DEBUG_VAR smsc_debug
102 #include <dev/usb/usb_debug.h>
103 #include <dev/usb/usb_process.h>
105 #include <dev/usb/net/usb_ethernet.h>
107 #include <dev/usb/net/if_smscreg.h>
110 static int smsc_debug = 0;
112 SYSCTL_NODE(_hw_usb, OID_AUTO, smsc, CTLFLAG_RW, 0, "USB smsc");
113 SYSCTL_INT(_hw_usb_smsc, OID_AUTO, debug, CTLFLAG_RW, &smsc_debug, 0,
118 * Various supported device vendors/products.
120 static const struct usb_device_id smsc_devs[] = {
121 #define SMSC_DEV(p,i) { USB_VPI(USB_VENDOR_SMC2, USB_PRODUCT_SMC2_##p, i) }
122 SMSC_DEV(LAN9514_ETH, 0),
128 #define smsc_dbg_printf(sc, fmt, args...) \
130 if (smsc_debug > 0) \
131 device_printf((sc)->sc_ue.ue_dev, "debug: " fmt, ##args); \
134 #define smsc_dbg_printf(sc, fmt, args...) do { } while (0)
137 #define smsc_warn_printf(sc, fmt, args...) \
138 device_printf((sc)->sc_ue.ue_dev, "warning: " fmt, ##args)
140 #define smsc_err_printf(sc, fmt, args...) \
141 device_printf((sc)->sc_ue.ue_dev, "error: " fmt, ##args)
144 #define ETHER_IS_ZERO(addr) \
145 (!(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]))
147 #define ETHER_IS_VALID(addr) \
148 (!ETHER_IS_MULTICAST(addr) && !ETHER_IS_ZERO(addr))
150 static device_probe_t smsc_probe;
151 static device_attach_t smsc_attach;
152 static device_detach_t smsc_detach;
154 static usb_callback_t smsc_bulk_read_callback;
155 static usb_callback_t smsc_bulk_write_callback;
157 static miibus_readreg_t smsc_miibus_readreg;
158 static miibus_writereg_t smsc_miibus_writereg;
159 static miibus_statchg_t smsc_miibus_statchg;
161 #if __FreeBSD_version > 1000000
162 static int smsc_attach_post_sub(struct usb_ether *ue);
164 static uether_fn_t smsc_attach_post;
165 static uether_fn_t smsc_init;
166 static uether_fn_t smsc_stop;
167 static uether_fn_t smsc_start;
168 static uether_fn_t smsc_tick;
169 static uether_fn_t smsc_setmulti;
170 static uether_fn_t smsc_setpromisc;
172 static int smsc_ifmedia_upd(struct ifnet *);
173 static void smsc_ifmedia_sts(struct ifnet *, struct ifmediareq *);
175 static int smsc_chip_init(struct smsc_softc *sc);
176 static int smsc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
178 static const struct usb_config smsc_config[SMSC_N_TRANSFER] = {
180 [SMSC_BULK_DT_WR] = {
182 .endpoint = UE_ADDR_ANY,
183 .direction = UE_DIR_OUT,
185 .bufsize = 16 * (MCLBYTES + 16),
186 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
187 .callback = smsc_bulk_write_callback,
188 .timeout = 10000, /* 10 seconds */
191 [SMSC_BULK_DT_RD] = {
193 .endpoint = UE_ADDR_ANY,
194 .direction = UE_DIR_IN,
195 .bufsize = 20480, /* bytes */
196 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
197 .callback = smsc_bulk_read_callback,
198 .timeout = 0, /* no timeout */
201 /* The SMSC chip supports an interrupt endpoints, however they aren't
202 * needed as we poll on the MII status.
206 static const struct usb_ether_methods smsc_ue_methods = {
207 .ue_attach_post = smsc_attach_post,
208 #if __FreeBSD_version > 1000000
209 .ue_attach_post_sub = smsc_attach_post_sub,
211 .ue_start = smsc_start,
212 .ue_ioctl = smsc_ioctl,
213 .ue_init = smsc_init,
214 .ue_stop = smsc_stop,
215 .ue_tick = smsc_tick,
216 .ue_setmulti = smsc_setmulti,
217 .ue_setpromisc = smsc_setpromisc,
218 .ue_mii_upd = smsc_ifmedia_upd,
219 .ue_mii_sts = smsc_ifmedia_sts,
223 * smsc_read_reg - Reads a 32-bit register on the device
224 * @sc: driver soft context
225 * @off: offset of the register
226 * @data: pointer a value that will be populated with the register value
229 * The device lock must be held before calling this function.
232 * 0 on success, a USB_ERR_?? error code on failure.
235 smsc_read_reg(struct smsc_softc *sc, uint32_t off, uint32_t *data)
237 struct usb_device_request req;
241 SMSC_LOCK_ASSERT(sc, MA_OWNED);
243 req.bmRequestType = UT_READ_VENDOR_DEVICE;
244 req.bRequest = SMSC_UR_READ_REG;
245 USETW(req.wValue, 0);
246 USETW(req.wIndex, off);
247 USETW(req.wLength, 4);
249 err = uether_do_request(&sc->sc_ue, &req, &buf, 1000);
251 smsc_warn_printf(sc, "Failed to read register 0x%0x\n", off);
253 *data = le32toh(buf);
259 * smsc_write_reg - Writes a 32-bit register on the device
260 * @sc: driver soft context
261 * @off: offset of the register
262 * @data: the 32-bit value to write into the register
265 * The device lock must be held before calling this function.
268 * 0 on success, a USB_ERR_?? error code on failure.
271 smsc_write_reg(struct smsc_softc *sc, uint32_t off, uint32_t data)
273 struct usb_device_request req;
277 SMSC_LOCK_ASSERT(sc, MA_OWNED);
281 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
282 req.bRequest = SMSC_UR_WRITE_REG;
283 USETW(req.wValue, 0);
284 USETW(req.wIndex, off);
285 USETW(req.wLength, 4);
287 err = uether_do_request(&sc->sc_ue, &req, &buf, 1000);
289 smsc_warn_printf(sc, "Failed to write register 0x%0x\n", off);
295 * smsc_wait_for_bits - Polls on a register value until bits are cleared
297 * @reg: offset of the register
298 * @bits: if the bits are clear the function returns
301 * The device lock must be held before calling this function.
304 * 0 on success, or a USB_ERR_?? error code on failure.
307 smsc_wait_for_bits(struct smsc_softc *sc, uint32_t reg, uint32_t bits)
309 usb_ticks_t start_ticks;
310 const usb_ticks_t max_ticks = USB_MS_TO_TICKS(1000);
314 SMSC_LOCK_ASSERT(sc, MA_OWNED);
316 start_ticks = (usb_ticks_t)ticks;
318 if ((err = smsc_read_reg(sc, reg, &val)) != 0)
323 uether_pause(&sc->sc_ue, hz / 100);
324 } while (((usb_ticks_t)(ticks - start_ticks)) < max_ticks);
326 return (USB_ERR_TIMEOUT);
330 * smsc_eeprom_read - Reads the attached EEPROM
332 * @off: the eeprom address offset
333 * @buf: stores the bytes
334 * @buflen: the number of bytes to read
336 * Simply reads bytes from an attached eeprom.
339 * The function takes and releases the device lock if it is not already held.
342 * 0 on success, or a USB_ERR_?? error code on failure.
345 smsc_eeprom_read(struct smsc_softc *sc, uint16_t off, uint8_t *buf, uint16_t buflen)
347 usb_ticks_t start_ticks;
348 const usb_ticks_t max_ticks = USB_MS_TO_TICKS(1000);
354 locked = mtx_owned(&sc->sc_mtx);
358 err = smsc_wait_for_bits(sc, SMSC_EEPROM_CMD, SMSC_EEPROM_CMD_BUSY);
360 smsc_warn_printf(sc, "eeprom busy, failed to read data\n");
364 /* start reading the bytes, one at a time */
365 for (i = 0; i < buflen; i++) {
367 val = SMSC_EEPROM_CMD_BUSY | (SMSC_EEPROM_CMD_ADDR_MASK & (off + i));
368 if ((err = smsc_write_reg(sc, SMSC_EEPROM_CMD, val)) != 0)
371 start_ticks = (usb_ticks_t)ticks;
373 if ((err = smsc_read_reg(sc, SMSC_EEPROM_CMD, &val)) != 0)
375 if (!(val & SMSC_EEPROM_CMD_BUSY) || (val & SMSC_EEPROM_CMD_TIMEOUT))
378 uether_pause(&sc->sc_ue, hz / 100);
379 } while (((usb_ticks_t)(ticks - start_ticks)) < max_ticks);
381 if (val & (SMSC_EEPROM_CMD_BUSY | SMSC_EEPROM_CMD_TIMEOUT)) {
382 smsc_warn_printf(sc, "eeprom command failed\n");
383 err = USB_ERR_IOERROR;
387 if ((err = smsc_read_reg(sc, SMSC_EEPROM_DATA, &val)) != 0)
390 buf[i] = (val & 0xff);
401 * smsc_miibus_readreg - Reads a MII/MDIO register
402 * @dev: usb ether device
403 * @phy: the number of phy reading from
404 * @reg: the register address
406 * Attempts to read a phy register over the MII bus.
409 * Takes and releases the device mutex lock if not already held.
412 * Returns the 16-bits read from the MII register, if this function fails 0
416 smsc_miibus_readreg(device_t dev, int phy, int reg)
418 struct smsc_softc *sc = device_get_softc(dev);
423 locked = mtx_owned(&sc->sc_mtx);
427 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
428 smsc_warn_printf(sc, "MII is busy\n");
432 addr = (phy << 11) | (reg << 6) | SMSC_MII_READ;
433 smsc_write_reg(sc, SMSC_MII_ADDR, addr);
435 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0)
436 smsc_warn_printf(sc, "MII read timeout\n");
438 smsc_read_reg(sc, SMSC_MII_DATA, &val);
445 return (val & 0xFFFF);
449 * smsc_miibus_writereg - Writes a MII/MDIO register
450 * @dev: usb ether device
451 * @phy: the number of phy writing to
452 * @reg: the register address
453 * @val: the value to write
455 * Attempts to write a phy register over the MII bus.
458 * Takes and releases the device mutex lock if not already held.
461 * Always returns 0 regardless of success or failure.
464 smsc_miibus_writereg(device_t dev, int phy, int reg, int val)
466 struct smsc_softc *sc = device_get_softc(dev);
470 if (sc->sc_phyno != phy)
473 locked = mtx_owned(&sc->sc_mtx);
477 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
478 smsc_warn_printf(sc, "MII is busy\n");
483 smsc_write_reg(sc, SMSC_MII_DATA, val);
485 addr = (phy << 11) | (reg << 6) | SMSC_MII_WRITE;
486 smsc_write_reg(sc, SMSC_MII_ADDR, addr);
488 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0)
489 smsc_warn_printf(sc, "MII write timeout\n");
500 * smsc_miibus_statchg - Called to detect phy status change
501 * @dev: usb ether device
503 * This function is called periodically by the system to poll for status
504 * changes of the link.
507 * Takes and releases the device mutex lock if not already held.
510 smsc_miibus_statchg(device_t dev)
512 struct smsc_softc *sc = device_get_softc(dev);
513 struct mii_data *mii = uether_getmii(&sc->sc_ue);
520 locked = mtx_owned(&sc->sc_mtx);
524 ifp = uether_getifp(&sc->sc_ue);
525 if (mii == NULL || ifp == NULL ||
526 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
529 /* Use the MII status to determine link status */
530 sc->sc_flags &= ~SMSC_FLAG_LINK;
531 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
532 (IFM_ACTIVE | IFM_AVALID)) {
533 switch (IFM_SUBTYPE(mii->mii_media_active)) {
536 sc->sc_flags |= SMSC_FLAG_LINK;
539 /* Gigabit ethernet not supported by chipset */
546 /* Lost link, do nothing. */
547 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) {
548 smsc_dbg_printf(sc, "link flag not set\n");
552 err = smsc_read_reg(sc, SMSC_AFC_CFG, &afc_cfg);
554 smsc_warn_printf(sc, "failed to read initial AFC_CFG, error %d\n", err);
558 /* Enable/disable full duplex operation and TX/RX pause */
559 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
560 smsc_dbg_printf(sc, "full duplex operation\n");
561 sc->sc_mac_csr &= ~SMSC_MAC_CSR_RCVOWN;
562 sc->sc_mac_csr |= SMSC_MAC_CSR_FDPX;
564 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
569 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
575 smsc_dbg_printf(sc, "half duplex operation\n");
576 sc->sc_mac_csr &= ~SMSC_MAC_CSR_FDPX;
577 sc->sc_mac_csr |= SMSC_MAC_CSR_RCVOWN;
583 err = smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
584 err += smsc_write_reg(sc, SMSC_FLOW, flow);
585 err += smsc_write_reg(sc, SMSC_AFC_CFG, afc_cfg);
587 smsc_warn_printf(sc, "media change failed, error %d\n", err);
595 * smsc_ifmedia_upd - Set media options
596 * @ifp: interface pointer
598 * Basically boilerplate code that simply calls the mii functions to set the
602 * The device lock must be held before this function is called.
605 * Returns 0 on success or a negative error code.
608 smsc_ifmedia_upd(struct ifnet *ifp)
610 struct smsc_softc *sc = ifp->if_softc;
611 struct mii_data *mii = uether_getmii(&sc->sc_ue);
612 struct mii_softc *miisc;
615 SMSC_LOCK_ASSERT(sc, MA_OWNED);
617 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
619 err = mii_mediachg(mii);
624 * smsc_ifmedia_sts - Report current media status
625 * @ifp: inet interface pointer
626 * @ifmr: interface media request
628 * Basically boilerplate code that simply calls the mii functions to get the
632 * Internally takes and releases the device lock.
635 smsc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
637 struct smsc_softc *sc = ifp->if_softc;
638 struct mii_data *mii = uether_getmii(&sc->sc_ue);
642 ifmr->ifm_active = mii->mii_media_active;
643 ifmr->ifm_status = mii->mii_media_status;
648 * smsc_hash - Calculate the hash of a mac address
649 * @addr: The mac address to calculate the hash on
651 * This function is used when configuring a range of m'cast mac addresses to
652 * filter on. The hash of the mac address is put in the device's mac hash
656 * Returns a value from 0-63 value which is the hash of the mac address.
658 static inline uint32_t
659 smsc_hash(uint8_t addr[ETHER_ADDR_LEN])
661 return (ether_crc32_be(addr, ETHER_ADDR_LEN) >> 26) & 0x3f;
665 * smsc_setmulti - Setup multicast
666 * @ue: usb ethernet device context
668 * Tells the device to either accept frames with a multicast mac address, a
669 * select group of m'cast mac addresses or just the devices mac address.
672 * Should be called with the SMSC lock held.
675 smsc_setmulti(struct usb_ether *ue)
677 struct smsc_softc *sc = uether_getsc(ue);
678 struct ifnet *ifp = uether_getifp(ue);
679 struct ifmultiaddr *ifma;
680 uint32_t hashtbl[2] = { 0, 0 };
683 SMSC_LOCK_ASSERT(sc, MA_OWNED);
685 if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
686 smsc_dbg_printf(sc, "receive all multicast enabled\n");
687 sc->sc_mac_csr |= SMSC_MAC_CSR_MCPAS;
688 sc->sc_mac_csr &= ~SMSC_MAC_CSR_HPFILT;
691 /* Take the lock of the mac address list before hashing each of them */
694 if (!TAILQ_EMPTY(&ifp->if_multiaddrs)) {
695 /* We are filtering on a set of address so calculate hashes of each
696 * of the address and set the corresponding bits in the register.
698 sc->sc_mac_csr |= SMSC_MAC_CSR_HPFILT;
699 sc->sc_mac_csr &= ~(SMSC_MAC_CSR_PRMS | SMSC_MAC_CSR_MCPAS);
701 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
702 if (ifma->ifma_addr->sa_family != AF_LINK)
705 hash = smsc_hash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
706 hashtbl[hash >> 5] |= 1 << (hash & 0x1F);
709 /* Only receive packets with destination set to our mac address */
710 sc->sc_mac_csr &= ~(SMSC_MAC_CSR_MCPAS | SMSC_MAC_CSR_HPFILT);
713 if_maddr_runlock(ifp);
716 if (sc->sc_mac_csr & SMSC_MAC_CSR_HPFILT)
717 smsc_dbg_printf(sc, "receive select group of macs\n");
719 smsc_dbg_printf(sc, "receive own packets only\n");
722 /* Write the hash table and mac control registers */
723 smsc_write_reg(sc, SMSC_HASHH, hashtbl[1]);
724 smsc_write_reg(sc, SMSC_HASHL, hashtbl[0]);
725 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
730 * smsc_setpromisc - Enables/disables promiscuous mode
731 * @ue: usb ethernet device context
734 * Should be called with the SMSC lock held.
737 smsc_setpromisc(struct usb_ether *ue)
739 struct smsc_softc *sc = uether_getsc(ue);
740 struct ifnet *ifp = uether_getifp(ue);
742 smsc_dbg_printf(sc, "promiscuous mode %sabled\n",
743 (ifp->if_flags & IFF_PROMISC) ? "en" : "dis");
745 SMSC_LOCK_ASSERT(sc, MA_OWNED);
747 if (ifp->if_flags & IFF_PROMISC)
748 sc->sc_mac_csr |= SMSC_MAC_CSR_PRMS;
750 sc->sc_mac_csr &= ~SMSC_MAC_CSR_PRMS;
752 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
757 * smsc_sethwcsum - Enable or disable H/W UDP and TCP checksumming
758 * @sc: driver soft context
761 * Should be called with the SMSC lock held.
764 * Returns 0 on success or a negative error code.
766 static int smsc_sethwcsum(struct smsc_softc *sc)
768 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
775 SMSC_LOCK_ASSERT(sc, MA_OWNED);
777 err = smsc_read_reg(sc, SMSC_COE_CTRL, &val);
779 smsc_warn_printf(sc, "failed to read SMSC_COE_CTRL (err=%d)\n", err);
783 /* Enable/disable the Rx checksum */
784 if ((ifp->if_capabilities & ifp->if_capenable) & IFCAP_RXCSUM)
785 val |= SMSC_COE_CTRL_RX_EN;
787 val &= ~SMSC_COE_CTRL_RX_EN;
789 /* Enable/disable the Tx checksum (currently not supported) */
790 if ((ifp->if_capabilities & ifp->if_capenable) & IFCAP_TXCSUM)
791 val |= SMSC_COE_CTRL_TX_EN;
793 val &= ~SMSC_COE_CTRL_TX_EN;
795 err = smsc_write_reg(sc, SMSC_COE_CTRL, val);
797 smsc_warn_printf(sc, "failed to write SMSC_COE_CTRL (err=%d)\n", err);
805 * smsc_setmacaddress - Sets the mac address in the device
806 * @sc: driver soft context
807 * @addr: pointer to array contain at least 6 bytes of the mac
809 * Writes the MAC address into the device, usually the MAC is programmed with
810 * values from the EEPROM.
813 * Should be called with the SMSC lock held.
816 * Returns 0 on success or a negative error code.
819 smsc_setmacaddress(struct smsc_softc *sc, const uint8_t *addr)
824 smsc_dbg_printf(sc, "setting mac address to %02x:%02x:%02x:%02x:%02x:%02x\n",
825 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
827 SMSC_LOCK_ASSERT(sc, MA_OWNED);
829 val = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
830 if ((err = smsc_write_reg(sc, SMSC_MAC_ADDRL, val)) != 0)
833 val = (addr[5] << 8) | addr[4];
834 err = smsc_write_reg(sc, SMSC_MAC_ADDRH, val);
841 * smsc_reset - Reset the SMSC chip
842 * @sc: device soft context
845 * Should be called with the SMSC lock held.
848 smsc_reset(struct smsc_softc *sc)
850 struct usb_config_descriptor *cd;
853 cd = usbd_get_config_descriptor(sc->sc_ue.ue_udev);
855 err = usbd_req_set_config(sc->sc_ue.ue_udev, &sc->sc_mtx,
856 cd->bConfigurationValue);
858 smsc_warn_printf(sc, "reset failed (ignored)\n");
860 /* Wait a little while for the chip to get its brains in order. */
861 uether_pause(&sc->sc_ue, hz / 100);
863 /* Reinitialize controller to achieve full reset. */
869 * smsc_init - Initialises the LAN95xx chip
870 * @ue: USB ether interface
872 * Called when the interface is brought up (i.e. ifconfig ue0 up), this
873 * initialise the interface and the rx/tx pipes.
876 * Should be called with the SMSC lock held.
879 smsc_init(struct usb_ether *ue)
881 struct smsc_softc *sc = uether_getsc(ue);
882 struct ifnet *ifp = uether_getifp(ue);
884 SMSC_LOCK_ASSERT(sc, MA_OWNED);
886 if (smsc_setmacaddress(sc, IF_LLADDR(ifp)))
887 smsc_dbg_printf(sc, "setting MAC address failed\n");
889 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
892 /* Cancel pending I/O */
895 #if __FreeBSD_version <= 1000000
896 /* On earlier versions this was the first place we could tell the system
897 * that we supported h/w csuming, however this is only called after the
898 * the interface has been brought up - not ideal.
900 if (!(ifp->if_capabilities & IFCAP_RXCSUM)) {
901 ifp->if_capabilities |= IFCAP_RXCSUM;
902 ifp->if_capenable |= IFCAP_RXCSUM;
903 ifp->if_hwassist = 0;
906 /* TX checksuming is disabled for now
907 ifp->if_capabilities |= IFCAP_TXCSUM;
908 ifp->if_capenable |= IFCAP_TXCSUM;
909 ifp->if_hwassist = CSUM_TCP | CSUM_UDP;
913 /* Reset the ethernet interface. */
916 /* Load the multicast filter. */
919 /* TCP/UDP checksum offload engines. */
922 usbd_xfer_set_stall(sc->sc_xfer[SMSC_BULK_DT_WR]);
924 /* Indicate we are up and running. */
925 ifp->if_drv_flags |= IFF_DRV_RUNNING;
927 /* Switch to selected media. */
928 smsc_ifmedia_upd(ifp);
933 * smsc_bulk_read_callback - Read callback used to process the USB URB
934 * @xfer: the USB transfer
937 * Reads the URB data which can contain one or more ethernet frames, the
938 * frames are copyed into a mbuf and given to the system.
941 * No locking required, doesn't access internal driver settings.
944 smsc_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
946 struct smsc_softc *sc = usbd_xfer_softc(xfer);
947 struct usb_ether *ue = &sc->sc_ue;
948 struct ifnet *ifp = uether_getifp(ue);
950 struct usb_page_cache *pc;
956 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
957 smsc_dbg_printf(sc, "rx : actlen %d\n", actlen);
959 switch (USB_GET_STATE(xfer)) {
960 case USB_ST_TRANSFERRED:
962 /* There is always a zero length frame after bringing the IF up */
963 if (actlen < (sizeof(rxhdr) + ETHER_CRC_LEN))
966 /* There maybe multiple packets in the USB frame, each will have a
967 * header and each needs to have it's own mbuf allocated and populated
970 pc = usbd_xfer_get_frame(xfer, 0);
973 while (off < actlen) {
975 /* The frame header is always aligned on a 4 byte boundary */
976 off = ((off + 0x3) & ~0x3);
978 if ((off + sizeof(rxhdr)) > actlen)
981 usbd_copy_out(pc, off, &rxhdr, sizeof(rxhdr));
982 off += (sizeof(rxhdr) + ETHER_ALIGN);
983 rxhdr = le32toh(rxhdr);
985 pktlen = (uint16_t)SMSC_RX_STAT_FRM_LENGTH(rxhdr);
987 smsc_dbg_printf(sc, "rx : rxhdr 0x%08x : pktlen %d : actlen %d : "
988 "off %d\n", rxhdr, pktlen, actlen, off);
991 if (rxhdr & SMSC_RX_STAT_ERROR) {
992 smsc_dbg_printf(sc, "rx error (hdr 0x%08x)\n", rxhdr);
994 if (rxhdr & SMSC_RX_STAT_COLLISION)
995 ifp->if_collisions++;
998 /* Check if the ethernet frame is too big or too small */
999 if ((pktlen < ETHER_HDR_LEN) || (pktlen > (actlen - off)))
1002 /* Create a new mbuf to store the packet in */
1003 m = uether_newbuf();
1005 smsc_warn_printf(sc, "failed to create new mbuf\n");
1009 if (pktlen > m->m_len) {
1010 smsc_dbg_printf(sc, "buffer too small %d vs %d bytes",
1012 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1016 usbd_copy_out(pc, off, mtod(m, uint8_t *), pktlen);
1018 /* Check if RX TCP/UDP checksumming is being offloaded */
1019 if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) {
1021 struct ether_header *eh;
1023 eh = mtod(m, struct ether_header *);
1025 /* Remove the extra 2 bytes of the csum */
1028 /* The checksum appears to be simplistically calculated
1029 * over the udp/tcp header and data up to the end of the
1030 * eth frame. Which means if the eth frame is padded
1031 * the csum calculation is incorrectly performed over
1032 * the padding bytes as well. Therefore to be safe we
1033 * ignore the H/W csum on frames less than or equal to
1036 * Ignore H/W csum for non-IPv4 packets.
1038 if ((be16toh(eh->ether_type) == ETHERTYPE_IP) &&
1039 (pktlen > ETHER_MIN_LEN)) {
1042 ip = (struct ip *)(eh + 1);
1043 if ((ip->ip_v == IPVERSION) &&
1044 ((ip->ip_p == IPPROTO_TCP) ||
1045 (ip->ip_p == IPPROTO_UDP))) {
1046 /* Indicate the UDP/TCP csum has been calculated */
1047 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
1049 /* Copy the TCP/UDP checksum from the last 2 bytes
1050 * of the transfer and put in the csum_data field.
1052 usbd_copy_out(pc, (off + pktlen),
1053 &m->m_pkthdr.csum_data, 2);
1055 /* The data is copied in network order, but the
1056 * csum algorithm in the kernel expects it to be
1057 * in host network order.
1059 m->m_pkthdr.csum_data = ntohs(m->m_pkthdr.csum_data);
1061 smsc_dbg_printf(sc, "RX checksum offloaded (0x%04x)\n",
1062 m->m_pkthdr.csum_data);
1066 /* Need to adjust the offset as well or we'll be off
1067 * by 2 because the csum is removed from the packet
1073 /* Finally enqueue the mbuf on the receive queue */
1074 /* Remove 4 trailing bytes */
1075 if (pktlen < (4 + ETHER_HDR_LEN)) {
1079 uether_rxmbuf(ue, m, pktlen - 4);
1082 /* Update the offset to move to the next potential packet */
1090 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1091 usbd_transfer_submit(xfer);
1096 if (error != USB_ERR_CANCELLED) {
1097 smsc_warn_printf(sc, "bulk read error, %s\n", usbd_errstr(error));
1098 usbd_xfer_set_stall(xfer);
1106 * smsc_bulk_write_callback - Write callback used to send ethernet frame(s)
1107 * @xfer: the USB transfer
1108 * @error: error code if the transfers is in an errored state
1110 * The main write function that pulls ethernet frames off the queue and sends
1117 smsc_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
1119 struct smsc_softc *sc = usbd_xfer_softc(xfer);
1120 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1121 struct usb_page_cache *pc;
1124 uint32_t frm_len = 0;
1127 switch (USB_GET_STATE(xfer)) {
1128 case USB_ST_TRANSFERRED:
1129 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1134 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0 ||
1135 (ifp->if_drv_flags & IFF_DRV_OACTIVE) != 0) {
1136 /* Don't send anything if there is no link or controller is busy. */
1140 for (nframes = 0; nframes < 16 &&
1141 !IFQ_DRV_IS_EMPTY(&ifp->if_snd); nframes++) {
1142 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1145 usbd_xfer_set_frame_offset(xfer, nframes * MCLBYTES,
1148 pc = usbd_xfer_get_frame(xfer, nframes);
1150 /* Each frame is prefixed with two 32-bit values describing the
1151 * length of the packet and buffer.
1153 txhdr = SMSC_TX_CTRL_0_BUF_SIZE(m->m_pkthdr.len) |
1154 SMSC_TX_CTRL_0_FIRST_SEG | SMSC_TX_CTRL_0_LAST_SEG;
1155 txhdr = htole32(txhdr);
1156 usbd_copy_in(pc, 0, &txhdr, sizeof(txhdr));
1158 txhdr = SMSC_TX_CTRL_1_PKT_LENGTH(m->m_pkthdr.len);
1159 txhdr = htole32(txhdr);
1160 usbd_copy_in(pc, 4, &txhdr, sizeof(txhdr));
1164 /* Next copy in the actual packet */
1165 usbd_m_copy_in(pc, frm_len, m, 0, m->m_pkthdr.len);
1166 frm_len += m->m_pkthdr.len;
1170 /* If there's a BPF listener, bounce a copy of this frame to him */
1175 /* Set frame length. */
1176 usbd_xfer_set_frame_len(xfer, nframes, frm_len);
1179 usbd_xfer_set_frames(xfer, nframes);
1180 usbd_transfer_submit(xfer);
1181 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1187 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1189 if (error != USB_ERR_CANCELLED) {
1190 smsc_err_printf(sc, "usb error on tx: %s\n", usbd_errstr(error));
1191 usbd_xfer_set_stall(xfer);
1199 * smsc_tick - Called periodically to monitor the state of the LAN95xx chip
1200 * @ue: USB ether interface
1202 * Simply calls the mii status functions to check the state of the link.
1205 * Should be called with the SMSC lock held.
1208 smsc_tick(struct usb_ether *ue)
1210 struct smsc_softc *sc = uether_getsc(ue);
1211 struct mii_data *mii = uether_getmii(&sc->sc_ue);
1213 SMSC_LOCK_ASSERT(sc, MA_OWNED);
1216 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) {
1217 smsc_miibus_statchg(ue->ue_dev);
1218 if ((sc->sc_flags & SMSC_FLAG_LINK) != 0)
1224 * smsc_start - Starts communication with the LAN95xx chip
1225 * @ue: USB ether interface
1231 smsc_start(struct usb_ether *ue)
1233 struct smsc_softc *sc = uether_getsc(ue);
1236 * start the USB transfers, if not already started:
1238 usbd_transfer_start(sc->sc_xfer[SMSC_BULK_DT_RD]);
1239 usbd_transfer_start(sc->sc_xfer[SMSC_BULK_DT_WR]);
1243 * smsc_stop - Stops communication with the LAN95xx chip
1244 * @ue: USB ether interface
1250 smsc_stop(struct usb_ether *ue)
1252 struct smsc_softc *sc = uether_getsc(ue);
1253 struct ifnet *ifp = uether_getifp(ue);
1255 SMSC_LOCK_ASSERT(sc, MA_OWNED);
1257 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1258 sc->sc_flags &= ~SMSC_FLAG_LINK;
1261 * stop all the transfers, if not already stopped:
1263 usbd_transfer_stop(sc->sc_xfer[SMSC_BULK_DT_WR]);
1264 usbd_transfer_stop(sc->sc_xfer[SMSC_BULK_DT_RD]);
1268 * smsc_phy_init - Initialises the in-built SMSC phy
1269 * @sc: driver soft context
1271 * Resets the PHY part of the chip and then initialises it to default
1272 * values. The 'link down' and 'auto-negotiation complete' interrupts
1273 * from the PHY are also enabled, however we don't monitor the interrupt
1274 * endpoints for the moment.
1277 * Returns 0 on success or EIO if failed to reset the PHY.
1280 smsc_phy_init(struct smsc_softc *sc)
1283 usb_ticks_t start_ticks;
1284 const usb_ticks_t max_ticks = USB_MS_TO_TICKS(1000);
1286 SMSC_LOCK_ASSERT(sc, MA_OWNED);
1288 /* Reset phy and wait for reset to complete */
1289 smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR, BMCR_RESET);
1291 start_ticks = ticks;
1293 uether_pause(&sc->sc_ue, hz / 100);
1294 bmcr = smsc_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR);
1295 } while ((bmcr & MII_BMCR) && ((ticks - start_ticks) < max_ticks));
1297 if (((usb_ticks_t)(ticks - start_ticks)) >= max_ticks) {
1298 smsc_err_printf(sc, "PHY reset timed-out");
1302 smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_ANAR,
1303 ANAR_10 | ANAR_10_FD | ANAR_TX | ANAR_TX_FD | /* all modes */
1308 /* Setup the phy to interrupt when the link goes down or autoneg completes */
1309 smsc_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, SMSC_PHY_INTR_STAT);
1310 smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, SMSC_PHY_INTR_MASK,
1311 (SMSC_PHY_INTR_ANEG_COMP | SMSC_PHY_INTR_LINK_DOWN));
1313 /* Restart auto-negotation */
1314 bmcr = smsc_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR);
1315 bmcr |= BMCR_STARTNEG;
1316 smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR, bmcr);
1323 * smsc_chip_init - Initialises the chip after power on
1324 * @sc: driver soft context
1326 * This initialisation sequence is modelled on the procedure in the Linux
1330 * Returns 0 on success or an error code on failure.
1333 smsc_chip_init(struct smsc_softc *sc)
1340 locked = mtx_owned(&sc->sc_mtx);
1344 /* Enter H/W config mode */
1345 smsc_write_reg(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST);
1347 if ((err = smsc_wait_for_bits(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST)) != 0) {
1348 smsc_warn_printf(sc, "timed-out waiting for reset to complete\n");
1353 smsc_write_reg(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST);
1355 if ((err = smsc_wait_for_bits(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST)) != 0) {
1356 smsc_warn_printf(sc, "timed-out waiting for phy reset to complete\n");
1360 /* Set the mac address */
1361 if ((err = smsc_setmacaddress(sc, sc->sc_ue.ue_eaddr)) != 0) {
1362 smsc_warn_printf(sc, "failed to set the MAC address\n");
1366 /* Don't know what the HW_CFG_BIR bit is, but following the reset sequence
1367 * as used in the Linux driver.
1369 if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) != 0) {
1370 smsc_warn_printf(sc, "failed to read HW_CFG: %d\n", err);
1373 reg_val |= SMSC_HW_CFG_BIR;
1374 smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
1376 /* There is a so called 'turbo mode' that the linux driver supports, it
1377 * seems to allow you to jam multiple frames per Rx transaction. By default
1378 * this driver supports that and therefore allows multiple frames per URB.
1380 * The xfer buffer size needs to reflect this as well, therefore based on
1381 * the calculations in the Linux driver the RX bufsize is set to 18944,
1382 * bufsz = (16 * 1024 + 5 * 512)
1384 * Burst capability is the number of URBs that can be in a burst of data/
1387 if (usbd_get_speed(sc->sc_ue.ue_udev) == USB_SPEED_HIGH)
1392 smsc_write_reg(sc, SMSC_BURST_CAP, burst_cap);
1394 /* Set the default bulk in delay (magic value from Linux driver) */
1395 smsc_write_reg(sc, SMSC_BULK_IN_DLY, 0x00002000);
1400 * Initialise the RX interface
1402 if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) < 0) {
1403 smsc_warn_printf(sc, "failed to read HW_CFG: (err = %d)\n", err);
1407 /* Adjust the packet offset in the buffer (designed to try and align IP
1408 * header on 4 byte boundary)
1410 reg_val &= ~SMSC_HW_CFG_RXDOFF;
1411 reg_val |= (ETHER_ALIGN << 9) & SMSC_HW_CFG_RXDOFF;
1413 /* The following setings are used for 'turbo mode', a.k.a multiple frames
1414 * per Rx transaction (again info taken form Linux driver).
1416 reg_val |= (SMSC_HW_CFG_MEF | SMSC_HW_CFG_BCE);
1418 smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
1420 /* Clear the status register ? */
1421 smsc_write_reg(sc, SMSC_INTR_STATUS, 0xffffffff);
1423 /* Read and display the revision register */
1424 if ((err = smsc_read_reg(sc, SMSC_ID_REV, &sc->sc_rev_id)) < 0) {
1425 smsc_warn_printf(sc, "failed to read ID_REV (err = %d)\n", err);
1429 device_printf(sc->sc_ue.ue_dev, "chip 0x%04lx, rev. %04lx\n",
1430 (sc->sc_rev_id & SMSC_ID_REV_CHIP_ID_MASK) >> 16,
1431 (sc->sc_rev_id & SMSC_ID_REV_CHIP_REV_MASK));
1433 /* GPIO/LED setup */
1434 reg_val = SMSC_LED_GPIO_CFG_SPD_LED | SMSC_LED_GPIO_CFG_LNK_LED |
1435 SMSC_LED_GPIO_CFG_FDX_LED;
1436 smsc_write_reg(sc, SMSC_LED_GPIO_CFG, reg_val);
1439 * Initialise the TX interface
1441 smsc_write_reg(sc, SMSC_FLOW, 0);
1443 smsc_write_reg(sc, SMSC_AFC_CFG, AFC_CFG_DEFAULT);
1445 /* Read the current MAC configuration */
1446 if ((err = smsc_read_reg(sc, SMSC_MAC_CSR, &sc->sc_mac_csr)) < 0) {
1447 smsc_warn_printf(sc, "failed to read MAC_CSR (err=%d)\n", err);
1452 smsc_write_reg(sc, SMSC_VLAN1, (uint32_t)ETHERTYPE_VLAN);
1455 * Initialise the PHY
1457 if ((err = smsc_phy_init(sc)) != 0)
1464 sc->sc_mac_csr |= SMSC_MAC_CSR_TXEN;
1465 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
1466 smsc_write_reg(sc, SMSC_TX_CFG, SMSC_TX_CFG_ON);
1471 sc->sc_mac_csr |= SMSC_MAC_CSR_RXEN;
1472 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
1483 smsc_err_printf(sc, "smsc_chip_init failed (err=%d)\n", err);
1489 * smsc_ioctl - ioctl function for the device
1490 * @ifp: interface pointer
1491 * @cmd: the ioctl command
1492 * @data: data passed in the ioctl call, typically a pointer to struct ifreq.
1494 * The ioctl routine is overridden to detect change requests for the H/W
1495 * checksum capabilities.
1498 * 0 on success and an error code on failure.
1501 smsc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1503 struct usb_ether *ue = ifp->if_softc;
1504 struct smsc_softc *sc;
1510 if (cmd == SIOCSIFCAP) {
1512 sc = uether_getsc(ue);
1513 ifr = (struct ifreq *)data;
1520 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1522 /* Modify the RX CSUM enable bits */
1523 if ((mask & IFCAP_RXCSUM) != 0 &&
1524 (ifp->if_capabilities & IFCAP_RXCSUM) != 0) {
1525 ifp->if_capenable ^= IFCAP_RXCSUM;
1527 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1528 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1535 #if __FreeBSD_version > 1000000
1542 rc = uether_ioctl(ifp, cmd, data);
1550 * This is FreeBSD-specific compatibility strings for RPi/RPi2
1553 smsc_fdt_find_eth_node(phandle_t start)
1555 phandle_t child, node;
1557 /* Traverse through entire tree to find usb ethernet nodes. */
1558 for (node = OF_child(start); node != 0; node = OF_peer(node)) {
1559 if (fdt_is_compatible(node, "net,ethernet") &&
1560 fdt_is_compatible(node, "usb,device"))
1562 child = smsc_fdt_find_eth_node(node);
1571 * Check if node's path is <*>/usb/hub/ethernet
1574 smsc_fdt_is_usb_eth(phandle_t node)
1579 memset(name, 0, sizeof(name));
1580 len = OF_getprop(node, "name", name, sizeof(name));
1584 if (strcmp(name, "ethernet"))
1587 node = OF_parent(node);
1590 len = OF_getprop(node, "name", name, sizeof(name));
1594 if (strcmp(name, "hub"))
1597 node = OF_parent(node);
1600 len = OF_getprop(node, "name", name, sizeof(name));
1604 if (strcmp(name, "usb"))
1611 smsc_fdt_find_eth_node_by_path(phandle_t start)
1613 phandle_t child, node;
1615 /* Traverse through entire tree to find usb ethernet nodes. */
1616 for (node = OF_child(start); node != 0; node = OF_peer(node)) {
1617 if (smsc_fdt_is_usb_eth(node))
1619 child = smsc_fdt_find_eth_node_by_path(node);
1628 * Get MAC address from FDT blob. Firmware or loader should fill
1629 * mac-address or local-mac-address property. Returns 0 if MAC address
1630 * obtained, error code otherwise.
1633 smsc_fdt_find_mac(unsigned char *mac)
1635 phandle_t node, root;
1638 root = OF_finddevice("/");
1639 node = smsc_fdt_find_eth_node(root);
1641 * If it's not FreeBSD FDT blob for RPi, try more
1642 * generic .../usb/hub/ethernet
1645 node = smsc_fdt_find_eth_node_by_path(root);
1648 /* Check if there is property */
1649 if ((len = OF_getproplen(node, "local-mac-address")) > 0) {
1650 if (len != ETHER_ADDR_LEN)
1653 OF_getprop(node, "local-mac-address", mac,
1658 if ((len = OF_getproplen(node, "mac-address")) > 0) {
1659 if (len != ETHER_ADDR_LEN)
1662 OF_getprop(node, "mac-address", mac,
1673 * smsc_attach_post - Called after the driver attached to the USB interface
1674 * @ue: the USB ethernet device
1676 * This is where the chip is intialised for the first time. This is different
1677 * from the smsc_init() function in that that one is designed to setup the
1678 * H/W to match the UE settings and can be called after a reset.
1683 smsc_attach_post(struct usb_ether *ue)
1685 struct smsc_softc *sc = uether_getsc(ue);
1686 uint32_t mac_h, mac_l;
1689 smsc_dbg_printf(sc, "smsc_attach_post\n");
1691 /* Setup some of the basics */
1695 /* Attempt to get the mac address, if an EEPROM is not attached this
1696 * will just return FF:FF:FF:FF:FF:FF, so in such cases we invent a MAC
1697 * address based on urandom.
1699 memset(sc->sc_ue.ue_eaddr, 0xff, ETHER_ADDR_LEN);
1701 /* Check if there is already a MAC address in the register */
1702 if ((smsc_read_reg(sc, SMSC_MAC_ADDRL, &mac_l) == 0) &&
1703 (smsc_read_reg(sc, SMSC_MAC_ADDRH, &mac_h) == 0)) {
1704 sc->sc_ue.ue_eaddr[5] = (uint8_t)((mac_h >> 8) & 0xff);
1705 sc->sc_ue.ue_eaddr[4] = (uint8_t)((mac_h) & 0xff);
1706 sc->sc_ue.ue_eaddr[3] = (uint8_t)((mac_l >> 24) & 0xff);
1707 sc->sc_ue.ue_eaddr[2] = (uint8_t)((mac_l >> 16) & 0xff);
1708 sc->sc_ue.ue_eaddr[1] = (uint8_t)((mac_l >> 8) & 0xff);
1709 sc->sc_ue.ue_eaddr[0] = (uint8_t)((mac_l) & 0xff);
1712 /* MAC address is not set so try to read from EEPROM, if that fails generate
1713 * a random MAC address.
1715 if (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr)) {
1717 err = smsc_eeprom_read(sc, 0x01, sc->sc_ue.ue_eaddr, ETHER_ADDR_LEN);
1719 if ((err != 0) || (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr)))
1720 err = smsc_fdt_find_mac(sc->sc_ue.ue_eaddr);
1722 if ((err != 0) || (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr))) {
1723 read_random(sc->sc_ue.ue_eaddr, ETHER_ADDR_LEN);
1724 sc->sc_ue.ue_eaddr[0] &= ~0x01; /* unicast */
1725 sc->sc_ue.ue_eaddr[0] |= 0x02; /* locally administered */
1729 /* Initialise the chip for the first time */
1735 * smsc_attach_post_sub - Called after the driver attached to the USB interface
1736 * @ue: the USB ethernet device
1738 * Most of this is boilerplate code and copied from the base USB ethernet
1739 * driver. It has been overriden so that we can indicate to the system that
1740 * the chip supports H/W checksumming.
1743 * Returns 0 on success or a negative error code.
1745 #if __FreeBSD_version > 1000000
1747 smsc_attach_post_sub(struct usb_ether *ue)
1749 struct smsc_softc *sc;
1753 sc = uether_getsc(ue);
1755 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1756 ifp->if_start = uether_start;
1757 ifp->if_ioctl = smsc_ioctl;
1758 ifp->if_init = uether_init;
1759 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
1760 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
1761 IFQ_SET_READY(&ifp->if_snd);
1763 /* The chip supports TCP/UDP checksum offloading on TX and RX paths, however
1764 * currently only RX checksum is supported in the driver (see top of file).
1766 ifp->if_capabilities |= IFCAP_RXCSUM | IFCAP_VLAN_MTU;
1767 ifp->if_hwassist = 0;
1769 /* TX checksuming is disabled (for now?)
1770 ifp->if_capabilities |= IFCAP_TXCSUM;
1771 ifp->if_capenable |= IFCAP_TXCSUM;
1772 ifp->if_hwassist = CSUM_TCP | CSUM_UDP;
1775 ifp->if_capenable = ifp->if_capabilities;
1778 error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp,
1779 uether_ifmedia_upd, ue->ue_methods->ue_mii_sts,
1780 BMSR_DEFCAPMASK, sc->sc_phyno, MII_OFFSET_ANY, 0);
1785 #endif /* __FreeBSD_version > 1000000 */
1789 * smsc_probe - Probe the interface.
1790 * @dev: smsc device handle
1792 * Checks if the device is a match for this driver.
1795 * Returns 0 on success or an error code on failure.
1798 smsc_probe(device_t dev)
1800 struct usb_attach_arg *uaa = device_get_ivars(dev);
1802 if (uaa->usb_mode != USB_MODE_HOST)
1804 if (uaa->info.bConfigIndex != SMSC_CONFIG_INDEX)
1806 if (uaa->info.bIfaceIndex != SMSC_IFACE_IDX)
1809 return (usbd_lookup_id_by_uaa(smsc_devs, sizeof(smsc_devs), uaa));
1814 * smsc_attach - Attach the interface.
1815 * @dev: smsc device handle
1817 * Allocate softc structures, do ifmedia setup and ethernet/BPF attach.
1820 * Returns 0 on success or a negative error code.
1823 smsc_attach(device_t dev)
1825 struct usb_attach_arg *uaa = device_get_ivars(dev);
1826 struct smsc_softc *sc = device_get_softc(dev);
1827 struct usb_ether *ue = &sc->sc_ue;
1828 uint8_t iface_index;
1831 sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
1833 device_set_usb_desc(dev);
1835 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
1837 /* Setup the endpoints for the SMSC LAN95xx device(s) */
1838 iface_index = SMSC_IFACE_IDX;
1839 err = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
1840 smsc_config, SMSC_N_TRANSFER, sc, &sc->sc_mtx);
1842 device_printf(dev, "error: allocating USB transfers failed\n");
1848 ue->ue_udev = uaa->device;
1849 ue->ue_mtx = &sc->sc_mtx;
1850 ue->ue_methods = &smsc_ue_methods;
1852 err = uether_ifattach(ue);
1854 device_printf(dev, "error: could not attach interface\n");
1857 return (0); /* success */
1861 return (ENXIO); /* failure */
1865 * smsc_detach - Detach the interface.
1866 * @dev: smsc device handle
1872 smsc_detach(device_t dev)
1874 struct smsc_softc *sc = device_get_softc(dev);
1875 struct usb_ether *ue = &sc->sc_ue;
1877 usbd_transfer_unsetup(sc->sc_xfer, SMSC_N_TRANSFER);
1878 uether_ifdetach(ue);
1879 mtx_destroy(&sc->sc_mtx);
1884 static device_method_t smsc_methods[] = {
1885 /* Device interface */
1886 DEVMETHOD(device_probe, smsc_probe),
1887 DEVMETHOD(device_attach, smsc_attach),
1888 DEVMETHOD(device_detach, smsc_detach),
1891 DEVMETHOD(bus_print_child, bus_generic_print_child),
1892 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
1895 DEVMETHOD(miibus_readreg, smsc_miibus_readreg),
1896 DEVMETHOD(miibus_writereg, smsc_miibus_writereg),
1897 DEVMETHOD(miibus_statchg, smsc_miibus_statchg),
1902 static driver_t smsc_driver = {
1904 .methods = smsc_methods,
1905 .size = sizeof(struct smsc_softc),
1908 static devclass_t smsc_devclass;
1910 DRIVER_MODULE(smsc, uhub, smsc_driver, smsc_devclass, NULL, 0);
1911 DRIVER_MODULE(miibus, smsc, miibus_driver, miibus_devclass, 0, 0);
1912 MODULE_DEPEND(smsc, uether, 1, 1, 1);
1913 MODULE_DEPEND(smsc, usb, 1, 1, 1);
1914 MODULE_DEPEND(smsc, ether, 1, 1, 1);
1915 MODULE_DEPEND(smsc, miibus, 1, 1, 1);
1916 MODULE_VERSION(smsc, 1);