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/socket.h>
78 #include <sys/sysctl.h>
80 #include <sys/unistd.h>
81 #include <sys/callout.h>
82 #include <sys/malloc.h>
84 #include <sys/random.h>
87 #include <net/if_var.h>
89 #include <netinet/in.h>
90 #include <netinet/ip.h>
92 #include "opt_platform.h"
95 #include <dev/fdt/fdt_common.h>
96 #include <dev/ofw/ofw_bus.h>
97 #include <dev/ofw/ofw_bus_subr.h>
100 #include <dev/usb/usb.h>
101 #include <dev/usb/usbdi.h>
102 #include <dev/usb/usbdi_util.h>
105 #define USB_DEBUG_VAR smsc_debug
106 #include <dev/usb/usb_debug.h>
107 #include <dev/usb/usb_process.h>
109 #include <dev/usb/net/usb_ethernet.h>
111 #include <dev/usb/net/if_smscreg.h>
114 static int smsc_debug = 0;
116 SYSCTL_NODE(_hw_usb, OID_AUTO, smsc, CTLFLAG_RW, 0, "USB smsc");
117 SYSCTL_INT(_hw_usb_smsc, OID_AUTO, debug, CTLFLAG_RW, &smsc_debug, 0,
122 * Various supported device vendors/products.
124 static const struct usb_device_id smsc_devs[] = {
125 #define SMSC_DEV(p,i) { USB_VPI(USB_VENDOR_SMC2, USB_PRODUCT_SMC2_##p, i) }
126 SMSC_DEV(LAN89530_ETH, 0),
127 SMSC_DEV(LAN9500_ETH, 0),
128 SMSC_DEV(LAN9500_ETH_2, 0),
129 SMSC_DEV(LAN9500A_ETH, 0),
130 SMSC_DEV(LAN9500A_ETH_2, 0),
131 SMSC_DEV(LAN9505_ETH, 0),
132 SMSC_DEV(LAN9505A_ETH, 0),
133 SMSC_DEV(LAN9514_ETH, 0),
134 SMSC_DEV(LAN9514_ETH_2, 0),
135 SMSC_DEV(LAN9530_ETH, 0),
136 SMSC_DEV(LAN9730_ETH, 0),
137 SMSC_DEV(LAN9500_SAL10, 0),
138 SMSC_DEV(LAN9505_SAL10, 0),
139 SMSC_DEV(LAN9500A_SAL10, 0),
140 SMSC_DEV(LAN9505A_SAL10, 0),
141 SMSC_DEV(LAN9514_SAL10, 0),
142 SMSC_DEV(LAN9500A_HAL, 0),
143 SMSC_DEV(LAN9505A_HAL, 0),
149 #define smsc_dbg_printf(sc, fmt, args...) \
151 if (smsc_debug > 0) \
152 device_printf((sc)->sc_ue.ue_dev, "debug: " fmt, ##args); \
155 #define smsc_dbg_printf(sc, fmt, args...)
158 #define smsc_warn_printf(sc, fmt, args...) \
159 device_printf((sc)->sc_ue.ue_dev, "warning: " fmt, ##args)
161 #define smsc_err_printf(sc, fmt, args...) \
162 device_printf((sc)->sc_ue.ue_dev, "error: " fmt, ##args)
165 #define ETHER_IS_ZERO(addr) \
166 (!(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]))
168 #define ETHER_IS_VALID(addr) \
169 (!ETHER_IS_MULTICAST(addr) && !ETHER_IS_ZERO(addr))
171 static device_probe_t smsc_probe;
172 static device_attach_t smsc_attach;
173 static device_detach_t smsc_detach;
175 static usb_callback_t smsc_bulk_read_callback;
176 static usb_callback_t smsc_bulk_write_callback;
178 static miibus_readreg_t smsc_miibus_readreg;
179 static miibus_writereg_t smsc_miibus_writereg;
180 static miibus_statchg_t smsc_miibus_statchg;
182 #if __FreeBSD_version > 1000000
183 static int smsc_attach_post_sub(struct usb_ether *ue);
185 static uether_fn_t smsc_attach_post;
186 static uether_fn_t smsc_init;
187 static uether_fn_t smsc_stop;
188 static uether_fn_t smsc_start;
189 static uether_fn_t smsc_tick;
190 static uether_fn_t smsc_setmulti;
191 static uether_fn_t smsc_setpromisc;
193 static int smsc_ifmedia_upd(struct ifnet *);
194 static void smsc_ifmedia_sts(struct ifnet *, struct ifmediareq *);
196 static int smsc_chip_init(struct smsc_softc *sc);
197 static int smsc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
199 static const struct usb_config smsc_config[SMSC_N_TRANSFER] = {
201 [SMSC_BULK_DT_WR] = {
203 .endpoint = UE_ADDR_ANY,
204 .direction = UE_DIR_OUT,
206 .bufsize = 16 * (MCLBYTES + 16),
207 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
208 .callback = smsc_bulk_write_callback,
209 .timeout = 10000, /* 10 seconds */
212 [SMSC_BULK_DT_RD] = {
214 .endpoint = UE_ADDR_ANY,
215 .direction = UE_DIR_IN,
216 .bufsize = 20480, /* bytes */
217 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
218 .callback = smsc_bulk_read_callback,
219 .timeout = 0, /* no timeout */
222 /* The SMSC chip supports an interrupt endpoints, however they aren't
223 * needed as we poll on the MII status.
227 static const struct usb_ether_methods smsc_ue_methods = {
228 .ue_attach_post = smsc_attach_post,
229 #if __FreeBSD_version > 1000000
230 .ue_attach_post_sub = smsc_attach_post_sub,
232 .ue_start = smsc_start,
233 .ue_ioctl = smsc_ioctl,
234 .ue_init = smsc_init,
235 .ue_stop = smsc_stop,
236 .ue_tick = smsc_tick,
237 .ue_setmulti = smsc_setmulti,
238 .ue_setpromisc = smsc_setpromisc,
239 .ue_mii_upd = smsc_ifmedia_upd,
240 .ue_mii_sts = smsc_ifmedia_sts,
244 * smsc_read_reg - Reads a 32-bit register on the device
245 * @sc: driver soft context
246 * @off: offset of the register
247 * @data: pointer a value that will be populated with the register value
250 * The device lock must be held before calling this function.
253 * 0 on success, a USB_ERR_?? error code on failure.
256 smsc_read_reg(struct smsc_softc *sc, uint32_t off, uint32_t *data)
258 struct usb_device_request req;
262 SMSC_LOCK_ASSERT(sc, MA_OWNED);
264 req.bmRequestType = UT_READ_VENDOR_DEVICE;
265 req.bRequest = SMSC_UR_READ_REG;
266 USETW(req.wValue, 0);
267 USETW(req.wIndex, off);
268 USETW(req.wLength, 4);
270 err = uether_do_request(&sc->sc_ue, &req, &buf, 1000);
272 smsc_warn_printf(sc, "Failed to read register 0x%0x\n", off);
274 *data = le32toh(buf);
280 * smsc_write_reg - Writes a 32-bit register on the device
281 * @sc: driver soft context
282 * @off: offset of the register
283 * @data: the 32-bit value to write into the register
286 * The device lock must be held before calling this function.
289 * 0 on success, a USB_ERR_?? error code on failure.
292 smsc_write_reg(struct smsc_softc *sc, uint32_t off, uint32_t data)
294 struct usb_device_request req;
298 SMSC_LOCK_ASSERT(sc, MA_OWNED);
302 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
303 req.bRequest = SMSC_UR_WRITE_REG;
304 USETW(req.wValue, 0);
305 USETW(req.wIndex, off);
306 USETW(req.wLength, 4);
308 err = uether_do_request(&sc->sc_ue, &req, &buf, 1000);
310 smsc_warn_printf(sc, "Failed to write register 0x%0x\n", off);
316 * smsc_wait_for_bits - Polls on a register value until bits are cleared
318 * @reg: offset of the register
319 * @bits: if the bits are clear the function returns
322 * The device lock must be held before calling this function.
325 * 0 on success, or a USB_ERR_?? error code on failure.
328 smsc_wait_for_bits(struct smsc_softc *sc, uint32_t reg, uint32_t bits)
330 usb_ticks_t start_ticks;
331 const usb_ticks_t max_ticks = USB_MS_TO_TICKS(1000);
335 SMSC_LOCK_ASSERT(sc, MA_OWNED);
337 start_ticks = (usb_ticks_t)ticks;
339 if ((err = smsc_read_reg(sc, reg, &val)) != 0)
344 uether_pause(&sc->sc_ue, hz / 100);
345 } while (((usb_ticks_t)(ticks - start_ticks)) < max_ticks);
347 return (USB_ERR_TIMEOUT);
351 * smsc_eeprom_read - Reads the attached EEPROM
353 * @off: the eeprom address offset
354 * @buf: stores the bytes
355 * @buflen: the number of bytes to read
357 * Simply reads bytes from an attached eeprom.
360 * The function takes and releases the device lock if it is not already held.
363 * 0 on success, or a USB_ERR_?? error code on failure.
366 smsc_eeprom_read(struct smsc_softc *sc, uint16_t off, uint8_t *buf, uint16_t buflen)
368 usb_ticks_t start_ticks;
369 const usb_ticks_t max_ticks = USB_MS_TO_TICKS(1000);
375 locked = mtx_owned(&sc->sc_mtx);
379 err = smsc_wait_for_bits(sc, SMSC_EEPROM_CMD, SMSC_EEPROM_CMD_BUSY);
381 smsc_warn_printf(sc, "eeprom busy, failed to read data\n");
385 /* start reading the bytes, one at a time */
386 for (i = 0; i < buflen; i++) {
388 val = SMSC_EEPROM_CMD_BUSY | (SMSC_EEPROM_CMD_ADDR_MASK & (off + i));
389 if ((err = smsc_write_reg(sc, SMSC_EEPROM_CMD, val)) != 0)
392 start_ticks = (usb_ticks_t)ticks;
394 if ((err = smsc_read_reg(sc, SMSC_EEPROM_CMD, &val)) != 0)
396 if (!(val & SMSC_EEPROM_CMD_BUSY) || (val & SMSC_EEPROM_CMD_TIMEOUT))
399 uether_pause(&sc->sc_ue, hz / 100);
400 } while (((usb_ticks_t)(ticks - start_ticks)) < max_ticks);
402 if (val & (SMSC_EEPROM_CMD_BUSY | SMSC_EEPROM_CMD_TIMEOUT)) {
403 smsc_warn_printf(sc, "eeprom command failed\n");
404 err = USB_ERR_IOERROR;
408 if ((err = smsc_read_reg(sc, SMSC_EEPROM_DATA, &val)) != 0)
411 buf[i] = (val & 0xff);
422 * smsc_miibus_readreg - Reads a MII/MDIO register
423 * @dev: usb ether device
424 * @phy: the number of phy reading from
425 * @reg: the register address
427 * Attempts to read a phy register over the MII bus.
430 * Takes and releases the device mutex lock if not already held.
433 * Returns the 16-bits read from the MII register, if this function fails 0
437 smsc_miibus_readreg(device_t dev, int phy, int reg)
439 struct smsc_softc *sc = device_get_softc(dev);
444 locked = mtx_owned(&sc->sc_mtx);
448 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
449 smsc_warn_printf(sc, "MII is busy\n");
453 addr = (phy << 11) | (reg << 6) | SMSC_MII_READ;
454 smsc_write_reg(sc, SMSC_MII_ADDR, addr);
456 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0)
457 smsc_warn_printf(sc, "MII read timeout\n");
459 smsc_read_reg(sc, SMSC_MII_DATA, &val);
466 return (val & 0xFFFF);
470 * smsc_miibus_writereg - Writes a MII/MDIO register
471 * @dev: usb ether device
472 * @phy: the number of phy writing to
473 * @reg: the register address
474 * @val: the value to write
476 * Attempts to write a phy register over the MII bus.
479 * Takes and releases the device mutex lock if not already held.
482 * Always returns 0 regardless of success or failure.
485 smsc_miibus_writereg(device_t dev, int phy, int reg, int val)
487 struct smsc_softc *sc = device_get_softc(dev);
491 if (sc->sc_phyno != phy)
494 locked = mtx_owned(&sc->sc_mtx);
498 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
499 smsc_warn_printf(sc, "MII is busy\n");
504 smsc_write_reg(sc, SMSC_MII_DATA, val);
506 addr = (phy << 11) | (reg << 6) | SMSC_MII_WRITE;
507 smsc_write_reg(sc, SMSC_MII_ADDR, addr);
509 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0)
510 smsc_warn_printf(sc, "MII write timeout\n");
521 * smsc_miibus_statchg - Called to detect phy status change
522 * @dev: usb ether device
524 * This function is called periodically by the system to poll for status
525 * changes of the link.
528 * Takes and releases the device mutex lock if not already held.
531 smsc_miibus_statchg(device_t dev)
533 struct smsc_softc *sc = device_get_softc(dev);
534 struct mii_data *mii = uether_getmii(&sc->sc_ue);
541 locked = mtx_owned(&sc->sc_mtx);
545 ifp = uether_getifp(&sc->sc_ue);
546 if (mii == NULL || ifp == NULL ||
547 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
550 /* Use the MII status to determine link status */
551 sc->sc_flags &= ~SMSC_FLAG_LINK;
552 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
553 (IFM_ACTIVE | IFM_AVALID)) {
554 switch (IFM_SUBTYPE(mii->mii_media_active)) {
557 sc->sc_flags |= SMSC_FLAG_LINK;
560 /* Gigabit ethernet not supported by chipset */
567 /* Lost link, do nothing. */
568 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) {
569 smsc_dbg_printf(sc, "link flag not set\n");
573 err = smsc_read_reg(sc, SMSC_AFC_CFG, &afc_cfg);
575 smsc_warn_printf(sc, "failed to read initial AFC_CFG, error %d\n", err);
579 /* Enable/disable full duplex operation and TX/RX pause */
580 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
581 smsc_dbg_printf(sc, "full duplex operation\n");
582 sc->sc_mac_csr &= ~SMSC_MAC_CSR_RCVOWN;
583 sc->sc_mac_csr |= SMSC_MAC_CSR_FDPX;
585 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
590 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
596 smsc_dbg_printf(sc, "half duplex operation\n");
597 sc->sc_mac_csr &= ~SMSC_MAC_CSR_FDPX;
598 sc->sc_mac_csr |= SMSC_MAC_CSR_RCVOWN;
604 err = smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
605 err += smsc_write_reg(sc, SMSC_FLOW, flow);
606 err += smsc_write_reg(sc, SMSC_AFC_CFG, afc_cfg);
608 smsc_warn_printf(sc, "media change failed, error %d\n", err);
616 * smsc_ifmedia_upd - Set media options
617 * @ifp: interface pointer
619 * Basically boilerplate code that simply calls the mii functions to set the
623 * The device lock must be held before this function is called.
626 * Returns 0 on success or a negative error code.
629 smsc_ifmedia_upd(struct ifnet *ifp)
631 struct smsc_softc *sc = ifp->if_softc;
632 struct mii_data *mii = uether_getmii(&sc->sc_ue);
633 struct mii_softc *miisc;
636 SMSC_LOCK_ASSERT(sc, MA_OWNED);
638 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
640 err = mii_mediachg(mii);
645 * smsc_ifmedia_sts - Report current media status
646 * @ifp: inet interface pointer
647 * @ifmr: interface media request
649 * Basically boilerplate code that simply calls the mii functions to get the
653 * Internally takes and releases the device lock.
656 smsc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
658 struct smsc_softc *sc = ifp->if_softc;
659 struct mii_data *mii = uether_getmii(&sc->sc_ue);
663 ifmr->ifm_active = mii->mii_media_active;
664 ifmr->ifm_status = mii->mii_media_status;
669 * smsc_hash - Calculate the hash of a mac address
670 * @addr: The mac address to calculate the hash on
672 * This function is used when configuring a range of m'cast mac addresses to
673 * filter on. The hash of the mac address is put in the device's mac hash
677 * Returns a value from 0-63 value which is the hash of the mac address.
679 static inline uint32_t
680 smsc_hash(uint8_t addr[ETHER_ADDR_LEN])
682 return (ether_crc32_be(addr, ETHER_ADDR_LEN) >> 26) & 0x3f;
686 * smsc_setmulti - Setup multicast
687 * @ue: usb ethernet device context
689 * Tells the device to either accept frames with a multicast mac address, a
690 * select group of m'cast mac addresses or just the devices mac address.
693 * Should be called with the SMSC lock held.
696 smsc_setmulti(struct usb_ether *ue)
698 struct smsc_softc *sc = uether_getsc(ue);
699 struct ifnet *ifp = uether_getifp(ue);
700 struct ifmultiaddr *ifma;
701 uint32_t hashtbl[2] = { 0, 0 };
704 SMSC_LOCK_ASSERT(sc, MA_OWNED);
706 if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
707 smsc_dbg_printf(sc, "receive all multicast enabled\n");
708 sc->sc_mac_csr |= SMSC_MAC_CSR_MCPAS;
709 sc->sc_mac_csr &= ~SMSC_MAC_CSR_HPFILT;
712 /* Take the lock of the mac address list before hashing each of them */
715 if (!TAILQ_EMPTY(&ifp->if_multiaddrs)) {
716 /* We are filtering on a set of address so calculate hashes of each
717 * of the address and set the corresponding bits in the register.
719 sc->sc_mac_csr |= SMSC_MAC_CSR_HPFILT;
720 sc->sc_mac_csr &= ~(SMSC_MAC_CSR_PRMS | SMSC_MAC_CSR_MCPAS);
722 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
723 if (ifma->ifma_addr->sa_family != AF_LINK)
726 hash = smsc_hash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
727 hashtbl[hash >> 5] |= 1 << (hash & 0x1F);
730 /* Only receive packets with destination set to our mac address */
731 sc->sc_mac_csr &= ~(SMSC_MAC_CSR_MCPAS | SMSC_MAC_CSR_HPFILT);
734 if_maddr_runlock(ifp);
737 if (sc->sc_mac_csr & SMSC_MAC_CSR_HPFILT)
738 smsc_dbg_printf(sc, "receive select group of macs\n");
740 smsc_dbg_printf(sc, "receive own packets only\n");
743 /* Write the hash table and mac control registers */
744 smsc_write_reg(sc, SMSC_HASHH, hashtbl[1]);
745 smsc_write_reg(sc, SMSC_HASHL, hashtbl[0]);
746 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
751 * smsc_setpromisc - Enables/disables promiscuous mode
752 * @ue: usb ethernet device context
755 * Should be called with the SMSC lock held.
758 smsc_setpromisc(struct usb_ether *ue)
760 struct smsc_softc *sc = uether_getsc(ue);
761 struct ifnet *ifp = uether_getifp(ue);
763 smsc_dbg_printf(sc, "promiscuous mode %sabled\n",
764 (ifp->if_flags & IFF_PROMISC) ? "en" : "dis");
766 SMSC_LOCK_ASSERT(sc, MA_OWNED);
768 if (ifp->if_flags & IFF_PROMISC)
769 sc->sc_mac_csr |= SMSC_MAC_CSR_PRMS;
771 sc->sc_mac_csr &= ~SMSC_MAC_CSR_PRMS;
773 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
778 * smsc_sethwcsum - Enable or disable H/W UDP and TCP checksumming
779 * @sc: driver soft context
782 * Should be called with the SMSC lock held.
785 * Returns 0 on success or a negative error code.
787 static int smsc_sethwcsum(struct smsc_softc *sc)
789 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
796 SMSC_LOCK_ASSERT(sc, MA_OWNED);
798 err = smsc_read_reg(sc, SMSC_COE_CTRL, &val);
800 smsc_warn_printf(sc, "failed to read SMSC_COE_CTRL (err=%d)\n", err);
804 /* Enable/disable the Rx checksum */
805 if ((ifp->if_capabilities & ifp->if_capenable) & IFCAP_RXCSUM)
806 val |= SMSC_COE_CTRL_RX_EN;
808 val &= ~SMSC_COE_CTRL_RX_EN;
810 /* Enable/disable the Tx checksum (currently not supported) */
811 if ((ifp->if_capabilities & ifp->if_capenable) & IFCAP_TXCSUM)
812 val |= SMSC_COE_CTRL_TX_EN;
814 val &= ~SMSC_COE_CTRL_TX_EN;
816 err = smsc_write_reg(sc, SMSC_COE_CTRL, val);
818 smsc_warn_printf(sc, "failed to write SMSC_COE_CTRL (err=%d)\n", err);
827 * smsc_setmacaddress - Sets the mac address in the device
828 * @sc: driver soft context
829 * @addr: pointer to array contain at least 6 bytes of the mac
831 * Writes the MAC address into the device, usually the MAC is programmed with
832 * values from the EEPROM.
835 * Should be called with the SMSC lock held.
838 * Returns 0 on success or a negative error code.
841 smsc_setmacaddress(struct smsc_softc *sc, const uint8_t *addr)
846 smsc_dbg_printf(sc, "setting mac address to %02x:%02x:%02x:%02x:%02x:%02x\n",
847 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
849 SMSC_LOCK_ASSERT(sc, MA_OWNED);
851 val = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
852 if ((err = smsc_write_reg(sc, SMSC_MAC_ADDRL, val)) != 0)
855 val = (addr[5] << 8) | addr[4];
856 err = smsc_write_reg(sc, SMSC_MAC_ADDRH, val);
863 * smsc_reset - Reset the SMSC chip
864 * @sc: device soft context
867 * Should be called with the SMSC lock held.
870 smsc_reset(struct smsc_softc *sc)
872 struct usb_config_descriptor *cd;
875 cd = usbd_get_config_descriptor(sc->sc_ue.ue_udev);
877 err = usbd_req_set_config(sc->sc_ue.ue_udev, &sc->sc_mtx,
878 cd->bConfigurationValue);
880 smsc_warn_printf(sc, "reset failed (ignored)\n");
882 /* Wait a little while for the chip to get its brains in order. */
883 uether_pause(&sc->sc_ue, hz / 100);
885 /* Reinitialize controller to achieve full reset. */
891 * smsc_init - Initialises the LAN95xx chip
892 * @ue: USB ether interface
894 * Called when the interface is brought up (i.e. ifconfig ue0 up), this
895 * initialise the interface and the rx/tx pipes.
898 * Should be called with the SMSC lock held.
901 smsc_init(struct usb_ether *ue)
903 struct smsc_softc *sc = uether_getsc(ue);
904 struct ifnet *ifp = uether_getifp(ue);
906 SMSC_LOCK_ASSERT(sc, MA_OWNED);
908 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
911 /* Cancel pending I/O */
914 #if __FreeBSD_version <= 1000000
915 /* On earlier versions this was the first place we could tell the system
916 * that we supported h/w csuming, however this is only called after the
917 * the interface has been brought up - not ideal.
919 if (!(ifp->if_capabilities & IFCAP_RXCSUM)) {
920 ifp->if_capabilities |= IFCAP_RXCSUM;
921 ifp->if_capenable |= IFCAP_RXCSUM;
922 ifp->if_hwassist = 0;
925 /* TX checksuming is disabled for now
926 ifp->if_capabilities |= IFCAP_TXCSUM;
927 ifp->if_capenable |= IFCAP_TXCSUM;
928 ifp->if_hwassist = CSUM_TCP | CSUM_UDP;
932 /* Reset the ethernet interface. */
935 /* Load the multicast filter. */
938 /* TCP/UDP checksum offload engines. */
941 usbd_xfer_set_stall(sc->sc_xfer[SMSC_BULK_DT_WR]);
943 /* Indicate we are up and running. */
944 ifp->if_drv_flags |= IFF_DRV_RUNNING;
946 /* Switch to selected media. */
947 smsc_ifmedia_upd(ifp);
952 * smsc_bulk_read_callback - Read callback used to process the USB URB
953 * @xfer: the USB transfer
956 * Reads the URB data which can contain one or more ethernet frames, the
957 * frames are copyed into a mbuf and given to the system.
960 * No locking required, doesn't access internal driver settings.
963 smsc_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
965 struct smsc_softc *sc = usbd_xfer_softc(xfer);
966 struct usb_ether *ue = &sc->sc_ue;
967 struct ifnet *ifp = uether_getifp(ue);
969 struct usb_page_cache *pc;
975 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
976 smsc_dbg_printf(sc, "rx : actlen %d\n", actlen);
978 switch (USB_GET_STATE(xfer)) {
979 case USB_ST_TRANSFERRED:
981 /* There is always a zero length frame after bringing the IF up */
982 if (actlen < (sizeof(rxhdr) + ETHER_CRC_LEN))
985 /* There maybe multiple packets in the USB frame, each will have a
986 * header and each needs to have it's own mbuf allocated and populated
989 pc = usbd_xfer_get_frame(xfer, 0);
992 while (off < actlen) {
994 /* The frame header is always aligned on a 4 byte boundary */
995 off = ((off + 0x3) & ~0x3);
997 usbd_copy_out(pc, off, &rxhdr, sizeof(rxhdr));
998 off += (sizeof(rxhdr) + ETHER_ALIGN);
999 rxhdr = le32toh(rxhdr);
1001 pktlen = (uint16_t)SMSC_RX_STAT_FRM_LENGTH(rxhdr);
1003 smsc_dbg_printf(sc, "rx : rxhdr 0x%08x : pktlen %d : actlen %d : "
1004 "off %d\n", rxhdr, pktlen, actlen, off);
1007 if (rxhdr & SMSC_RX_STAT_ERROR) {
1008 smsc_dbg_printf(sc, "rx error (hdr 0x%08x)\n", rxhdr);
1009 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1010 if (rxhdr & SMSC_RX_STAT_COLLISION)
1011 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
1014 /* Check if the ethernet frame is too big or too small */
1015 if ((pktlen < ETHER_HDR_LEN) || (pktlen > (actlen - off)))
1018 /* Create a new mbuf to store the packet in */
1019 m = uether_newbuf();
1021 smsc_warn_printf(sc, "failed to create new mbuf\n");
1022 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1026 usbd_copy_out(pc, off, mtod(m, uint8_t *), pktlen);
1028 /* Check if RX TCP/UDP checksumming is being offloaded */
1029 if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) {
1031 struct ether_header *eh;
1033 eh = mtod(m, struct ether_header *);
1035 /* Remove the extra 2 bytes of the csum */
1038 /* The checksum appears to be simplistically calculated
1039 * over the udp/tcp header and data up to the end of the
1040 * eth frame. Which means if the eth frame is padded
1041 * the csum calculation is incorrectly performed over
1042 * the padding bytes as well. Therefore to be safe we
1043 * ignore the H/W csum on frames less than or equal to
1046 * Ignore H/W csum for non-IPv4 packets.
1048 if ((be16toh(eh->ether_type) == ETHERTYPE_IP) &&
1049 (pktlen > ETHER_MIN_LEN)) {
1052 ip = (struct ip *)(eh + 1);
1053 if ((ip->ip_v == IPVERSION) &&
1054 ((ip->ip_p == IPPROTO_TCP) ||
1055 (ip->ip_p == IPPROTO_UDP))) {
1056 /* Indicate the UDP/TCP csum has been calculated */
1057 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
1059 /* Copy the TCP/UDP checksum from the last 2 bytes
1060 * of the transfer and put in the csum_data field.
1062 usbd_copy_out(pc, (off + pktlen),
1063 &m->m_pkthdr.csum_data, 2);
1065 /* The data is copied in network order, but the
1066 * csum algorithm in the kernel expects it to be
1067 * in host network order.
1069 m->m_pkthdr.csum_data = ntohs(m->m_pkthdr.csum_data);
1071 smsc_dbg_printf(sc, "RX checksum offloaded (0x%04x)\n",
1072 m->m_pkthdr.csum_data);
1076 /* Need to adjust the offset as well or we'll be off
1077 * by 2 because the csum is removed from the packet
1083 /* Finally enqueue the mbuf on the receive queue */
1084 /* Remove 4 trailing bytes */
1085 if (pktlen < (4 + ETHER_HDR_LEN)) {
1089 uether_rxmbuf(ue, m, pktlen - 4);
1092 /* Update the offset to move to the next potential packet */
1100 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1101 usbd_transfer_submit(xfer);
1106 if (error != USB_ERR_CANCELLED) {
1107 smsc_warn_printf(sc, "bulk read error, %s\n", usbd_errstr(error));
1108 usbd_xfer_set_stall(xfer);
1116 * smsc_bulk_write_callback - Write callback used to send ethernet frame(s)
1117 * @xfer: the USB transfer
1118 * @error: error code if the transfers is in an errored state
1120 * The main write function that pulls ethernet frames off the queue and sends
1127 smsc_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
1129 struct smsc_softc *sc = usbd_xfer_softc(xfer);
1130 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1131 struct usb_page_cache *pc;
1134 uint32_t frm_len = 0;
1137 switch (USB_GET_STATE(xfer)) {
1138 case USB_ST_TRANSFERRED:
1139 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1144 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0 ||
1145 (ifp->if_drv_flags & IFF_DRV_OACTIVE) != 0) {
1146 /* Don't send anything if there is no link or controller is busy. */
1150 for (nframes = 0; nframes < 16 &&
1151 !IFQ_DRV_IS_EMPTY(&ifp->if_snd); nframes++) {
1152 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1155 usbd_xfer_set_frame_offset(xfer, nframes * MCLBYTES,
1158 pc = usbd_xfer_get_frame(xfer, nframes);
1160 /* Each frame is prefixed with two 32-bit values describing the
1161 * length of the packet and buffer.
1163 txhdr = SMSC_TX_CTRL_0_BUF_SIZE(m->m_pkthdr.len) |
1164 SMSC_TX_CTRL_0_FIRST_SEG | SMSC_TX_CTRL_0_LAST_SEG;
1165 txhdr = htole32(txhdr);
1166 usbd_copy_in(pc, 0, &txhdr, sizeof(txhdr));
1168 txhdr = SMSC_TX_CTRL_1_PKT_LENGTH(m->m_pkthdr.len);
1169 txhdr = htole32(txhdr);
1170 usbd_copy_in(pc, 4, &txhdr, sizeof(txhdr));
1174 /* Next copy in the actual packet */
1175 usbd_m_copy_in(pc, frm_len, m, 0, m->m_pkthdr.len);
1176 frm_len += m->m_pkthdr.len;
1178 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1180 /* If there's a BPF listener, bounce a copy of this frame to him */
1185 /* Set frame length. */
1186 usbd_xfer_set_frame_len(xfer, nframes, frm_len);
1189 usbd_xfer_set_frames(xfer, nframes);
1190 usbd_transfer_submit(xfer);
1191 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1196 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1197 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1199 if (error != USB_ERR_CANCELLED) {
1200 smsc_err_printf(sc, "usb error on tx: %s\n", usbd_errstr(error));
1201 usbd_xfer_set_stall(xfer);
1209 * smsc_tick - Called periodically to monitor the state of the LAN95xx chip
1210 * @ue: USB ether interface
1212 * Simply calls the mii status functions to check the state of the link.
1215 * Should be called with the SMSC lock held.
1218 smsc_tick(struct usb_ether *ue)
1220 struct smsc_softc *sc = uether_getsc(ue);
1221 struct mii_data *mii = uether_getmii(&sc->sc_ue);
1223 SMSC_LOCK_ASSERT(sc, MA_OWNED);
1226 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) {
1227 smsc_miibus_statchg(ue->ue_dev);
1228 if ((sc->sc_flags & SMSC_FLAG_LINK) != 0)
1234 * smsc_start - Starts communication with the LAN95xx chip
1235 * @ue: USB ether interface
1241 smsc_start(struct usb_ether *ue)
1243 struct smsc_softc *sc = uether_getsc(ue);
1246 * start the USB transfers, if not already started:
1248 usbd_transfer_start(sc->sc_xfer[SMSC_BULK_DT_RD]);
1249 usbd_transfer_start(sc->sc_xfer[SMSC_BULK_DT_WR]);
1253 * smsc_stop - Stops communication with the LAN95xx chip
1254 * @ue: USB ether interface
1260 smsc_stop(struct usb_ether *ue)
1262 struct smsc_softc *sc = uether_getsc(ue);
1263 struct ifnet *ifp = uether_getifp(ue);
1265 SMSC_LOCK_ASSERT(sc, MA_OWNED);
1267 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1268 sc->sc_flags &= ~SMSC_FLAG_LINK;
1271 * stop all the transfers, if not already stopped:
1273 usbd_transfer_stop(sc->sc_xfer[SMSC_BULK_DT_WR]);
1274 usbd_transfer_stop(sc->sc_xfer[SMSC_BULK_DT_RD]);
1278 * smsc_phy_init - Initialises the in-built SMSC phy
1279 * @sc: driver soft context
1281 * Resets the PHY part of the chip and then initialises it to default
1282 * values. The 'link down' and 'auto-negotiation complete' interrupts
1283 * from the PHY are also enabled, however we don't monitor the interrupt
1284 * endpoints for the moment.
1287 * Returns 0 on success or EIO if failed to reset the PHY.
1290 smsc_phy_init(struct smsc_softc *sc)
1293 usb_ticks_t start_ticks;
1294 const usb_ticks_t max_ticks = USB_MS_TO_TICKS(1000);
1296 SMSC_LOCK_ASSERT(sc, MA_OWNED);
1298 /* Reset phy and wait for reset to complete */
1299 smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR, BMCR_RESET);
1301 start_ticks = ticks;
1303 uether_pause(&sc->sc_ue, hz / 100);
1304 bmcr = smsc_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR);
1305 } while ((bmcr & MII_BMCR) && ((ticks - start_ticks) < max_ticks));
1307 if (((usb_ticks_t)(ticks - start_ticks)) >= max_ticks) {
1308 smsc_err_printf(sc, "PHY reset timed-out");
1312 smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_ANAR,
1313 ANAR_10 | ANAR_10_FD | ANAR_TX | ANAR_TX_FD | /* all modes */
1318 /* Setup the phy to interrupt when the link goes down or autoneg completes */
1319 smsc_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, SMSC_PHY_INTR_STAT);
1320 smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, SMSC_PHY_INTR_MASK,
1321 (SMSC_PHY_INTR_ANEG_COMP | SMSC_PHY_INTR_LINK_DOWN));
1323 /* Restart auto-negotation */
1324 bmcr = smsc_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR);
1325 bmcr |= BMCR_STARTNEG;
1326 smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR, bmcr);
1333 * smsc_chip_init - Initialises the chip after power on
1334 * @sc: driver soft context
1336 * This initialisation sequence is modelled on the procedure in the Linux
1340 * Returns 0 on success or an error code on failure.
1343 smsc_chip_init(struct smsc_softc *sc)
1350 locked = mtx_owned(&sc->sc_mtx);
1354 /* Enter H/W config mode */
1355 smsc_write_reg(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST);
1357 if ((err = smsc_wait_for_bits(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST)) != 0) {
1358 smsc_warn_printf(sc, "timed-out waiting for reset to complete\n");
1363 smsc_write_reg(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST);
1365 if ((err = smsc_wait_for_bits(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST) != 0)) {
1366 smsc_warn_printf(sc, "timed-out waiting for phy reset to complete\n");
1370 /* Set the mac address */
1371 if ((err = smsc_setmacaddress(sc, sc->sc_ue.ue_eaddr)) != 0) {
1372 smsc_warn_printf(sc, "failed to set the MAC address\n");
1376 /* Don't know what the HW_CFG_BIR bit is, but following the reset sequence
1377 * as used in the Linux driver.
1379 if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) != 0) {
1380 smsc_warn_printf(sc, "failed to read HW_CFG: %d\n", err);
1383 reg_val |= SMSC_HW_CFG_BIR;
1384 smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
1386 /* There is a so called 'turbo mode' that the linux driver supports, it
1387 * seems to allow you to jam multiple frames per Rx transaction. By default
1388 * this driver supports that and therefore allows multiple frames per URB.
1390 * The xfer buffer size needs to reflect this as well, therefore based on
1391 * the calculations in the Linux driver the RX bufsize is set to 18944,
1392 * bufsz = (16 * 1024 + 5 * 512)
1394 * Burst capability is the number of URBs that can be in a burst of data/
1397 if (usbd_get_speed(sc->sc_ue.ue_udev) == USB_SPEED_HIGH)
1402 smsc_write_reg(sc, SMSC_BURST_CAP, burst_cap);
1404 /* Set the default bulk in delay (magic value from Linux driver) */
1405 smsc_write_reg(sc, SMSC_BULK_IN_DLY, 0x00002000);
1410 * Initialise the RX interface
1412 if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) < 0) {
1413 smsc_warn_printf(sc, "failed to read HW_CFG: (err = %d)\n", err);
1417 /* Adjust the packet offset in the buffer (designed to try and align IP
1418 * header on 4 byte boundary)
1420 reg_val &= ~SMSC_HW_CFG_RXDOFF;
1421 reg_val |= (ETHER_ALIGN << 9) & SMSC_HW_CFG_RXDOFF;
1423 /* The following setings are used for 'turbo mode', a.k.a multiple frames
1424 * per Rx transaction (again info taken form Linux driver).
1426 reg_val |= (SMSC_HW_CFG_MEF | SMSC_HW_CFG_BCE);
1428 smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
1430 /* Clear the status register ? */
1431 smsc_write_reg(sc, SMSC_INTR_STATUS, 0xffffffff);
1433 /* Read and display the revision register */
1434 if ((err = smsc_read_reg(sc, SMSC_ID_REV, &sc->sc_rev_id)) < 0) {
1435 smsc_warn_printf(sc, "failed to read ID_REV (err = %d)\n", err);
1439 device_printf(sc->sc_ue.ue_dev, "chip 0x%04lx, rev. %04lx\n",
1440 (sc->sc_rev_id & SMSC_ID_REV_CHIP_ID_MASK) >> 16,
1441 (sc->sc_rev_id & SMSC_ID_REV_CHIP_REV_MASK));
1443 /* GPIO/LED setup */
1444 reg_val = SMSC_LED_GPIO_CFG_SPD_LED | SMSC_LED_GPIO_CFG_LNK_LED |
1445 SMSC_LED_GPIO_CFG_FDX_LED;
1446 smsc_write_reg(sc, SMSC_LED_GPIO_CFG, reg_val);
1449 * Initialise the TX interface
1451 smsc_write_reg(sc, SMSC_FLOW, 0);
1453 smsc_write_reg(sc, SMSC_AFC_CFG, AFC_CFG_DEFAULT);
1455 /* Read the current MAC configuration */
1456 if ((err = smsc_read_reg(sc, SMSC_MAC_CSR, &sc->sc_mac_csr)) < 0) {
1457 smsc_warn_printf(sc, "failed to read MAC_CSR (err=%d)\n", err);
1462 smsc_write_reg(sc, SMSC_VLAN1, (uint32_t)ETHERTYPE_VLAN);
1465 * Initialise the PHY
1467 if ((err = smsc_phy_init(sc)) != 0)
1474 sc->sc_mac_csr |= SMSC_MAC_CSR_TXEN;
1475 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
1476 smsc_write_reg(sc, SMSC_TX_CFG, SMSC_TX_CFG_ON);
1481 sc->sc_mac_csr |= SMSC_MAC_CSR_RXEN;
1482 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
1493 smsc_err_printf(sc, "smsc_chip_init failed (err=%d)\n", err);
1499 * smsc_ioctl - ioctl function for the device
1500 * @ifp: interface pointer
1501 * @cmd: the ioctl command
1502 * @data: data passed in the ioctl call, typically a pointer to struct ifreq.
1504 * The ioctl routine is overridden to detect change requests for the H/W
1505 * checksum capabilities.
1508 * 0 on success and an error code on failure.
1511 smsc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1513 struct usb_ether *ue = ifp->if_softc;
1514 struct smsc_softc *sc;
1520 if (cmd == SIOCSIFCAP) {
1522 sc = uether_getsc(ue);
1523 ifr = (struct ifreq *)data;
1530 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1532 /* Modify the RX CSUM enable bits */
1533 if ((mask & IFCAP_RXCSUM) != 0 &&
1534 (ifp->if_capabilities & IFCAP_RXCSUM) != 0) {
1535 ifp->if_capenable ^= IFCAP_RXCSUM;
1537 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1538 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1545 #if __FreeBSD_version > 1000000
1552 rc = uether_ioctl(ifp, cmd, data);
1560 smsc_fdt_find_eth_node(phandle_t start)
1562 phandle_t child, node;
1564 /* Traverse through entire tree to find usb ethernet nodes. */
1565 for (node = OF_child(start); node != 0; node = OF_peer(node)) {
1566 if (fdt_is_compatible(node, "net,ethernet") &&
1567 fdt_is_compatible(node, "usb,device"))
1569 child = smsc_fdt_find_eth_node(node);
1578 * Get MAC address from FDT blob. Firmware or loader should fill
1579 * mac-address or local-mac-address property. Returns 0 if MAC address
1580 * obtained, error code otherwise.
1583 smsc_fdt_find_mac(unsigned char *mac)
1585 phandle_t node, root;
1588 root = OF_finddevice("/");
1589 node = smsc_fdt_find_eth_node(root);
1592 /* Check if there is property */
1593 if ((len = OF_getproplen(node, "local-mac-address")) > 0) {
1594 if (len != ETHER_ADDR_LEN)
1597 OF_getprop(node, "local-mac-address", mac,
1602 if ((len = OF_getproplen(node, "mac-address")) > 0) {
1603 if (len != ETHER_ADDR_LEN)
1606 OF_getprop(node, "mac-address", mac,
1617 * smsc_attach_post - Called after the driver attached to the USB interface
1618 * @ue: the USB ethernet device
1620 * This is where the chip is intialised for the first time. This is different
1621 * from the smsc_init() function in that that one is designed to setup the
1622 * H/W to match the UE settings and can be called after a reset.
1627 smsc_attach_post(struct usb_ether *ue)
1629 struct smsc_softc *sc = uether_getsc(ue);
1630 uint32_t mac_h, mac_l;
1633 smsc_dbg_printf(sc, "smsc_attach_post\n");
1635 /* Setup some of the basics */
1639 /* Attempt to get the mac address, if an EEPROM is not attached this
1640 * will just return FF:FF:FF:FF:FF:FF, so in such cases we invent a MAC
1641 * address based on urandom.
1643 memset(sc->sc_ue.ue_eaddr, 0xff, ETHER_ADDR_LEN);
1645 /* Check if there is already a MAC address in the register */
1646 if ((smsc_read_reg(sc, SMSC_MAC_ADDRL, &mac_l) == 0) &&
1647 (smsc_read_reg(sc, SMSC_MAC_ADDRH, &mac_h) == 0)) {
1648 sc->sc_ue.ue_eaddr[5] = (uint8_t)((mac_h >> 8) & 0xff);
1649 sc->sc_ue.ue_eaddr[4] = (uint8_t)((mac_h) & 0xff);
1650 sc->sc_ue.ue_eaddr[3] = (uint8_t)((mac_l >> 24) & 0xff);
1651 sc->sc_ue.ue_eaddr[2] = (uint8_t)((mac_l >> 16) & 0xff);
1652 sc->sc_ue.ue_eaddr[1] = (uint8_t)((mac_l >> 8) & 0xff);
1653 sc->sc_ue.ue_eaddr[0] = (uint8_t)((mac_l) & 0xff);
1656 /* MAC address is not set so try to read from EEPROM, if that fails generate
1657 * a random MAC address.
1659 if (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr)) {
1661 err = smsc_eeprom_read(sc, 0x01, sc->sc_ue.ue_eaddr, ETHER_ADDR_LEN);
1663 if ((err != 0) || (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr)))
1664 err = smsc_fdt_find_mac(sc->sc_ue.ue_eaddr);
1666 if ((err != 0) || (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr))) {
1667 read_random(sc->sc_ue.ue_eaddr, ETHER_ADDR_LEN);
1668 sc->sc_ue.ue_eaddr[0] &= ~0x01; /* unicast */
1669 sc->sc_ue.ue_eaddr[0] |= 0x02; /* locally administered */
1673 /* Initialise the chip for the first time */
1679 * smsc_attach_post_sub - Called after the driver attached to the USB interface
1680 * @ue: the USB ethernet device
1682 * Most of this is boilerplate code and copied from the base USB ethernet
1683 * driver. It has been overriden so that we can indicate to the system that
1684 * the chip supports H/W checksumming.
1687 * Returns 0 on success or a negative error code.
1689 #if __FreeBSD_version > 1000000
1691 smsc_attach_post_sub(struct usb_ether *ue)
1693 struct smsc_softc *sc;
1697 sc = uether_getsc(ue);
1699 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1700 ifp->if_start = uether_start;
1701 ifp->if_ioctl = smsc_ioctl;
1702 ifp->if_init = uether_init;
1703 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
1704 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
1705 IFQ_SET_READY(&ifp->if_snd);
1707 /* The chip supports TCP/UDP checksum offloading on TX and RX paths, however
1708 * currently only RX checksum is supported in the driver (see top of file).
1710 ifp->if_capabilities |= IFCAP_RXCSUM;
1711 ifp->if_hwassist = 0;
1713 /* TX checksuming is disabled (for now?)
1714 ifp->if_capabilities |= IFCAP_TXCSUM;
1715 ifp->if_capenable |= IFCAP_TXCSUM;
1716 ifp->if_hwassist = CSUM_TCP | CSUM_UDP;
1719 ifp->if_capenable = ifp->if_capabilities;
1722 error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp,
1723 uether_ifmedia_upd, ue->ue_methods->ue_mii_sts,
1724 BMSR_DEFCAPMASK, sc->sc_phyno, MII_OFFSET_ANY, 0);
1729 #endif /* __FreeBSD_version > 1000000 */
1733 * smsc_probe - Probe the interface.
1734 * @dev: smsc device handle
1736 * Checks if the device is a match for this driver.
1739 * Returns 0 on success or an error code on failure.
1742 smsc_probe(device_t dev)
1744 struct usb_attach_arg *uaa = device_get_ivars(dev);
1746 if (uaa->usb_mode != USB_MODE_HOST)
1748 if (uaa->info.bConfigIndex != SMSC_CONFIG_INDEX)
1750 if (uaa->info.bIfaceIndex != SMSC_IFACE_IDX)
1753 return (usbd_lookup_id_by_uaa(smsc_devs, sizeof(smsc_devs), uaa));
1758 * smsc_attach - Attach the interface.
1759 * @dev: smsc device handle
1761 * Allocate softc structures, do ifmedia setup and ethernet/BPF attach.
1764 * Returns 0 on success or a negative error code.
1767 smsc_attach(device_t dev)
1769 struct usb_attach_arg *uaa = device_get_ivars(dev);
1770 struct smsc_softc *sc = device_get_softc(dev);
1771 struct usb_ether *ue = &sc->sc_ue;
1772 uint8_t iface_index;
1775 sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
1777 device_set_usb_desc(dev);
1779 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
1781 /* Setup the endpoints for the SMSC LAN95xx device(s) */
1782 iface_index = SMSC_IFACE_IDX;
1783 err = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
1784 smsc_config, SMSC_N_TRANSFER, sc, &sc->sc_mtx);
1786 device_printf(dev, "error: allocating USB transfers failed\n");
1792 ue->ue_udev = uaa->device;
1793 ue->ue_mtx = &sc->sc_mtx;
1794 ue->ue_methods = &smsc_ue_methods;
1796 err = uether_ifattach(ue);
1798 device_printf(dev, "error: could not attach interface\n");
1801 return (0); /* success */
1805 return (ENXIO); /* failure */
1809 * smsc_detach - Detach the interface.
1810 * @dev: smsc device handle
1816 smsc_detach(device_t dev)
1818 struct smsc_softc *sc = device_get_softc(dev);
1819 struct usb_ether *ue = &sc->sc_ue;
1821 usbd_transfer_unsetup(sc->sc_xfer, SMSC_N_TRANSFER);
1822 uether_ifdetach(ue);
1823 mtx_destroy(&sc->sc_mtx);
1828 static device_method_t smsc_methods[] = {
1829 /* Device interface */
1830 DEVMETHOD(device_probe, smsc_probe),
1831 DEVMETHOD(device_attach, smsc_attach),
1832 DEVMETHOD(device_detach, smsc_detach),
1835 DEVMETHOD(bus_print_child, bus_generic_print_child),
1836 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
1839 DEVMETHOD(miibus_readreg, smsc_miibus_readreg),
1840 DEVMETHOD(miibus_writereg, smsc_miibus_writereg),
1841 DEVMETHOD(miibus_statchg, smsc_miibus_statchg),
1846 static driver_t smsc_driver = {
1848 .methods = smsc_methods,
1849 .size = sizeof(struct smsc_softc),
1852 static devclass_t smsc_devclass;
1854 DRIVER_MODULE(smsc, uhub, smsc_driver, smsc_devclass, NULL, 0);
1855 DRIVER_MODULE(miibus, smsc, miibus_driver, miibus_devclass, 0, 0);
1856 MODULE_DEPEND(smsc, uether, 1, 1, 1);
1857 MODULE_DEPEND(smsc, usb, 1, 1, 1);
1858 MODULE_DEPEND(smsc, ether, 1, 1, 1);
1859 MODULE_DEPEND(smsc, miibus, 1, 1, 1);
1860 MODULE_VERSION(smsc, 1);