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...)
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);
806 * smsc_setmacaddress - Sets the mac address in the device
807 * @sc: driver soft context
808 * @addr: pointer to array contain at least 6 bytes of the mac
810 * Writes the MAC address into the device, usually the MAC is programmed with
811 * values from the EEPROM.
814 * Should be called with the SMSC lock held.
817 * Returns 0 on success or a negative error code.
820 smsc_setmacaddress(struct smsc_softc *sc, const uint8_t *addr)
825 smsc_dbg_printf(sc, "setting mac address to %02x:%02x:%02x:%02x:%02x:%02x\n",
826 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
828 SMSC_LOCK_ASSERT(sc, MA_OWNED);
830 val = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
831 if ((err = smsc_write_reg(sc, SMSC_MAC_ADDRL, val)) != 0)
834 val = (addr[5] << 8) | addr[4];
835 err = smsc_write_reg(sc, SMSC_MAC_ADDRH, val);
842 * smsc_reset - Reset the SMSC chip
843 * @sc: device soft context
846 * Should be called with the SMSC lock held.
849 smsc_reset(struct smsc_softc *sc)
851 struct usb_config_descriptor *cd;
854 cd = usbd_get_config_descriptor(sc->sc_ue.ue_udev);
856 err = usbd_req_set_config(sc->sc_ue.ue_udev, &sc->sc_mtx,
857 cd->bConfigurationValue);
859 smsc_warn_printf(sc, "reset failed (ignored)\n");
861 /* Wait a little while for the chip to get its brains in order. */
862 uether_pause(&sc->sc_ue, hz / 100);
864 /* Reinitialize controller to achieve full reset. */
870 * smsc_init - Initialises the LAN95xx chip
871 * @ue: USB ether interface
873 * Called when the interface is brought up (i.e. ifconfig ue0 up), this
874 * initialise the interface and the rx/tx pipes.
877 * Should be called with the SMSC lock held.
880 smsc_init(struct usb_ether *ue)
882 struct smsc_softc *sc = uether_getsc(ue);
883 struct ifnet *ifp = uether_getifp(ue);
885 SMSC_LOCK_ASSERT(sc, MA_OWNED);
887 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
890 /* Cancel pending I/O */
893 #if __FreeBSD_version <= 1000000
894 /* On earlier versions this was the first place we could tell the system
895 * that we supported h/w csuming, however this is only called after the
896 * the interface has been brought up - not ideal.
898 if (!(ifp->if_capabilities & IFCAP_RXCSUM)) {
899 ifp->if_capabilities |= IFCAP_RXCSUM;
900 ifp->if_capenable |= IFCAP_RXCSUM;
901 ifp->if_hwassist = 0;
904 /* TX checksuming is disabled for now
905 ifp->if_capabilities |= IFCAP_TXCSUM;
906 ifp->if_capenable |= IFCAP_TXCSUM;
907 ifp->if_hwassist = CSUM_TCP | CSUM_UDP;
911 /* Reset the ethernet interface. */
914 /* Load the multicast filter. */
917 /* TCP/UDP checksum offload engines. */
920 usbd_xfer_set_stall(sc->sc_xfer[SMSC_BULK_DT_WR]);
922 /* Indicate we are up and running. */
923 ifp->if_drv_flags |= IFF_DRV_RUNNING;
925 /* Switch to selected media. */
926 smsc_ifmedia_upd(ifp);
931 * smsc_bulk_read_callback - Read callback used to process the USB URB
932 * @xfer: the USB transfer
935 * Reads the URB data which can contain one or more ethernet frames, the
936 * frames are copyed into a mbuf and given to the system.
939 * No locking required, doesn't access internal driver settings.
942 smsc_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
944 struct smsc_softc *sc = usbd_xfer_softc(xfer);
945 struct usb_ether *ue = &sc->sc_ue;
946 struct ifnet *ifp = uether_getifp(ue);
948 struct usb_page_cache *pc;
954 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
955 smsc_dbg_printf(sc, "rx : actlen %d\n", actlen);
957 switch (USB_GET_STATE(xfer)) {
958 case USB_ST_TRANSFERRED:
960 /* There is always a zero length frame after bringing the IF up */
961 if (actlen < (sizeof(rxhdr) + ETHER_CRC_LEN))
964 /* There maybe multiple packets in the USB frame, each will have a
965 * header and each needs to have it's own mbuf allocated and populated
968 pc = usbd_xfer_get_frame(xfer, 0);
971 while (off < actlen) {
973 /* The frame header is always aligned on a 4 byte boundary */
974 off = ((off + 0x3) & ~0x3);
976 usbd_copy_out(pc, off, &rxhdr, sizeof(rxhdr));
977 off += (sizeof(rxhdr) + ETHER_ALIGN);
978 rxhdr = le32toh(rxhdr);
980 pktlen = (uint16_t)SMSC_RX_STAT_FRM_LENGTH(rxhdr);
982 smsc_dbg_printf(sc, "rx : rxhdr 0x%08x : pktlen %d : actlen %d : "
983 "off %d\n", rxhdr, pktlen, actlen, off);
986 if (rxhdr & SMSC_RX_STAT_ERROR) {
987 smsc_dbg_printf(sc, "rx error (hdr 0x%08x)\n", rxhdr);
989 if (rxhdr & SMSC_RX_STAT_COLLISION)
990 ifp->if_collisions++;
993 /* Check if the ethernet frame is too big or too small */
994 if ((pktlen < ETHER_HDR_LEN) || (pktlen > (actlen - off)))
997 /* Create a new mbuf to store the packet in */
1000 smsc_warn_printf(sc, "failed to create new mbuf\n");
1005 usbd_copy_out(pc, off, mtod(m, uint8_t *), pktlen);
1007 /* Check if RX TCP/UDP checksumming is being offloaded */
1008 if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) {
1010 struct ether_header *eh;
1012 eh = mtod(m, struct ether_header *);
1014 /* Remove the extra 2 bytes of the csum */
1017 /* The checksum appears to be simplistically calculated
1018 * over the udp/tcp header and data up to the end of the
1019 * eth frame. Which means if the eth frame is padded
1020 * the csum calculation is incorrectly performed over
1021 * the padding bytes as well. Therefore to be safe we
1022 * ignore the H/W csum on frames less than or equal to
1025 * Ignore H/W csum for non-IPv4 packets.
1027 if ((be16toh(eh->ether_type) == ETHERTYPE_IP) &&
1028 (pktlen > ETHER_MIN_LEN)) {
1031 ip = (struct ip *)(eh + 1);
1032 if ((ip->ip_v == IPVERSION) &&
1033 ((ip->ip_p == IPPROTO_TCP) ||
1034 (ip->ip_p == IPPROTO_UDP))) {
1035 /* Indicate the UDP/TCP csum has been calculated */
1036 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
1038 /* Copy the TCP/UDP checksum from the last 2 bytes
1039 * of the transfer and put in the csum_data field.
1041 usbd_copy_out(pc, (off + pktlen),
1042 &m->m_pkthdr.csum_data, 2);
1044 /* The data is copied in network order, but the
1045 * csum algorithm in the kernel expects it to be
1046 * in host network order.
1048 m->m_pkthdr.csum_data = ntohs(m->m_pkthdr.csum_data);
1050 smsc_dbg_printf(sc, "RX checksum offloaded (0x%04x)\n",
1051 m->m_pkthdr.csum_data);
1055 /* Need to adjust the offset as well or we'll be off
1056 * by 2 because the csum is removed from the packet
1062 /* Finally enqueue the mbuf on the receive queue */
1063 /* Remove 4 trailing bytes */
1064 if (pktlen < (4 + ETHER_HDR_LEN)) {
1068 uether_rxmbuf(ue, m, pktlen - 4);
1071 /* Update the offset to move to the next potential packet */
1079 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1080 usbd_transfer_submit(xfer);
1085 if (error != USB_ERR_CANCELLED) {
1086 smsc_warn_printf(sc, "bulk read error, %s\n", usbd_errstr(error));
1087 usbd_xfer_set_stall(xfer);
1095 * smsc_bulk_write_callback - Write callback used to send ethernet frame(s)
1096 * @xfer: the USB transfer
1097 * @error: error code if the transfers is in an errored state
1099 * The main write function that pulls ethernet frames off the queue and sends
1106 smsc_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
1108 struct smsc_softc *sc = usbd_xfer_softc(xfer);
1109 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1110 struct usb_page_cache *pc;
1113 uint32_t frm_len = 0;
1116 switch (USB_GET_STATE(xfer)) {
1117 case USB_ST_TRANSFERRED:
1118 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1123 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0 ||
1124 (ifp->if_drv_flags & IFF_DRV_OACTIVE) != 0) {
1125 /* Don't send anything if there is no link or controller is busy. */
1129 for (nframes = 0; nframes < 16 &&
1130 !IFQ_DRV_IS_EMPTY(&ifp->if_snd); nframes++) {
1131 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1134 usbd_xfer_set_frame_offset(xfer, nframes * MCLBYTES,
1137 pc = usbd_xfer_get_frame(xfer, nframes);
1139 /* Each frame is prefixed with two 32-bit values describing the
1140 * length of the packet and buffer.
1142 txhdr = SMSC_TX_CTRL_0_BUF_SIZE(m->m_pkthdr.len) |
1143 SMSC_TX_CTRL_0_FIRST_SEG | SMSC_TX_CTRL_0_LAST_SEG;
1144 txhdr = htole32(txhdr);
1145 usbd_copy_in(pc, 0, &txhdr, sizeof(txhdr));
1147 txhdr = SMSC_TX_CTRL_1_PKT_LENGTH(m->m_pkthdr.len);
1148 txhdr = htole32(txhdr);
1149 usbd_copy_in(pc, 4, &txhdr, sizeof(txhdr));
1153 /* Next copy in the actual packet */
1154 usbd_m_copy_in(pc, frm_len, m, 0, m->m_pkthdr.len);
1155 frm_len += m->m_pkthdr.len;
1159 /* If there's a BPF listener, bounce a copy of this frame to him */
1164 /* Set frame length. */
1165 usbd_xfer_set_frame_len(xfer, nframes, frm_len);
1168 usbd_xfer_set_frames(xfer, nframes);
1169 usbd_transfer_submit(xfer);
1170 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1176 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1178 if (error != USB_ERR_CANCELLED) {
1179 smsc_err_printf(sc, "usb error on tx: %s\n", usbd_errstr(error));
1180 usbd_xfer_set_stall(xfer);
1188 * smsc_tick - Called periodically to monitor the state of the LAN95xx chip
1189 * @ue: USB ether interface
1191 * Simply calls the mii status functions to check the state of the link.
1194 * Should be called with the SMSC lock held.
1197 smsc_tick(struct usb_ether *ue)
1199 struct smsc_softc *sc = uether_getsc(ue);
1200 struct mii_data *mii = uether_getmii(&sc->sc_ue);
1202 SMSC_LOCK_ASSERT(sc, MA_OWNED);
1205 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) {
1206 smsc_miibus_statchg(ue->ue_dev);
1207 if ((sc->sc_flags & SMSC_FLAG_LINK) != 0)
1213 * smsc_start - Starts communication with the LAN95xx chip
1214 * @ue: USB ether interface
1220 smsc_start(struct usb_ether *ue)
1222 struct smsc_softc *sc = uether_getsc(ue);
1225 * start the USB transfers, if not already started:
1227 usbd_transfer_start(sc->sc_xfer[SMSC_BULK_DT_RD]);
1228 usbd_transfer_start(sc->sc_xfer[SMSC_BULK_DT_WR]);
1232 * smsc_stop - Stops communication with the LAN95xx chip
1233 * @ue: USB ether interface
1239 smsc_stop(struct usb_ether *ue)
1241 struct smsc_softc *sc = uether_getsc(ue);
1242 struct ifnet *ifp = uether_getifp(ue);
1244 SMSC_LOCK_ASSERT(sc, MA_OWNED);
1246 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1247 sc->sc_flags &= ~SMSC_FLAG_LINK;
1250 * stop all the transfers, if not already stopped:
1252 usbd_transfer_stop(sc->sc_xfer[SMSC_BULK_DT_WR]);
1253 usbd_transfer_stop(sc->sc_xfer[SMSC_BULK_DT_RD]);
1257 * smsc_phy_init - Initialises the in-built SMSC phy
1258 * @sc: driver soft context
1260 * Resets the PHY part of the chip and then initialises it to default
1261 * values. The 'link down' and 'auto-negotiation complete' interrupts
1262 * from the PHY are also enabled, however we don't monitor the interrupt
1263 * endpoints for the moment.
1266 * Returns 0 on success or EIO if failed to reset the PHY.
1269 smsc_phy_init(struct smsc_softc *sc)
1272 usb_ticks_t start_ticks;
1273 const usb_ticks_t max_ticks = USB_MS_TO_TICKS(1000);
1275 SMSC_LOCK_ASSERT(sc, MA_OWNED);
1277 /* Reset phy and wait for reset to complete */
1278 smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR, BMCR_RESET);
1280 start_ticks = ticks;
1282 uether_pause(&sc->sc_ue, hz / 100);
1283 bmcr = smsc_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR);
1284 } while ((bmcr & MII_BMCR) && ((ticks - start_ticks) < max_ticks));
1286 if (((usb_ticks_t)(ticks - start_ticks)) >= max_ticks) {
1287 smsc_err_printf(sc, "PHY reset timed-out");
1291 smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_ANAR,
1292 ANAR_10 | ANAR_10_FD | ANAR_TX | ANAR_TX_FD | /* all modes */
1297 /* Setup the phy to interrupt when the link goes down or autoneg completes */
1298 smsc_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, SMSC_PHY_INTR_STAT);
1299 smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, SMSC_PHY_INTR_MASK,
1300 (SMSC_PHY_INTR_ANEG_COMP | SMSC_PHY_INTR_LINK_DOWN));
1302 /* Restart auto-negotation */
1303 bmcr = smsc_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR);
1304 bmcr |= BMCR_STARTNEG;
1305 smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR, bmcr);
1312 * smsc_chip_init - Initialises the chip after power on
1313 * @sc: driver soft context
1315 * This initialisation sequence is modelled on the procedure in the Linux
1319 * Returns 0 on success or an error code on failure.
1322 smsc_chip_init(struct smsc_softc *sc)
1329 locked = mtx_owned(&sc->sc_mtx);
1333 /* Enter H/W config mode */
1334 smsc_write_reg(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST);
1336 if ((err = smsc_wait_for_bits(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST)) != 0) {
1337 smsc_warn_printf(sc, "timed-out waiting for reset to complete\n");
1342 smsc_write_reg(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST);
1344 if ((err = smsc_wait_for_bits(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST) != 0)) {
1345 smsc_warn_printf(sc, "timed-out waiting for phy reset to complete\n");
1349 /* Set the mac address */
1350 if ((err = smsc_setmacaddress(sc, sc->sc_ue.ue_eaddr)) != 0) {
1351 smsc_warn_printf(sc, "failed to set the MAC address\n");
1355 /* Don't know what the HW_CFG_BIR bit is, but following the reset sequence
1356 * as used in the Linux driver.
1358 if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) != 0) {
1359 smsc_warn_printf(sc, "failed to read HW_CFG: %d\n", err);
1362 reg_val |= SMSC_HW_CFG_BIR;
1363 smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
1365 /* There is a so called 'turbo mode' that the linux driver supports, it
1366 * seems to allow you to jam multiple frames per Rx transaction. By default
1367 * this driver supports that and therefore allows multiple frames per URB.
1369 * The xfer buffer size needs to reflect this as well, therefore based on
1370 * the calculations in the Linux driver the RX bufsize is set to 18944,
1371 * bufsz = (16 * 1024 + 5 * 512)
1373 * Burst capability is the number of URBs that can be in a burst of data/
1376 if (usbd_get_speed(sc->sc_ue.ue_udev) == USB_SPEED_HIGH)
1381 smsc_write_reg(sc, SMSC_BURST_CAP, burst_cap);
1383 /* Set the default bulk in delay (magic value from Linux driver) */
1384 smsc_write_reg(sc, SMSC_BULK_IN_DLY, 0x00002000);
1389 * Initialise the RX interface
1391 if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) < 0) {
1392 smsc_warn_printf(sc, "failed to read HW_CFG: (err = %d)\n", err);
1396 /* Adjust the packet offset in the buffer (designed to try and align IP
1397 * header on 4 byte boundary)
1399 reg_val &= ~SMSC_HW_CFG_RXDOFF;
1400 reg_val |= (ETHER_ALIGN << 9) & SMSC_HW_CFG_RXDOFF;
1402 /* The following setings are used for 'turbo mode', a.k.a multiple frames
1403 * per Rx transaction (again info taken form Linux driver).
1405 reg_val |= (SMSC_HW_CFG_MEF | SMSC_HW_CFG_BCE);
1407 smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
1409 /* Clear the status register ? */
1410 smsc_write_reg(sc, SMSC_INTR_STATUS, 0xffffffff);
1412 /* Read and display the revision register */
1413 if ((err = smsc_read_reg(sc, SMSC_ID_REV, &sc->sc_rev_id)) < 0) {
1414 smsc_warn_printf(sc, "failed to read ID_REV (err = %d)\n", err);
1418 device_printf(sc->sc_ue.ue_dev, "chip 0x%04lx, rev. %04lx\n",
1419 (sc->sc_rev_id & SMSC_ID_REV_CHIP_ID_MASK) >> 16,
1420 (sc->sc_rev_id & SMSC_ID_REV_CHIP_REV_MASK));
1422 /* GPIO/LED setup */
1423 reg_val = SMSC_LED_GPIO_CFG_SPD_LED | SMSC_LED_GPIO_CFG_LNK_LED |
1424 SMSC_LED_GPIO_CFG_FDX_LED;
1425 smsc_write_reg(sc, SMSC_LED_GPIO_CFG, reg_val);
1428 * Initialise the TX interface
1430 smsc_write_reg(sc, SMSC_FLOW, 0);
1432 smsc_write_reg(sc, SMSC_AFC_CFG, AFC_CFG_DEFAULT);
1434 /* Read the current MAC configuration */
1435 if ((err = smsc_read_reg(sc, SMSC_MAC_CSR, &sc->sc_mac_csr)) < 0) {
1436 smsc_warn_printf(sc, "failed to read MAC_CSR (err=%d)\n", err);
1441 smsc_write_reg(sc, SMSC_VLAN1, (uint32_t)ETHERTYPE_VLAN);
1444 * Initialise the PHY
1446 if ((err = smsc_phy_init(sc)) != 0)
1453 sc->sc_mac_csr |= SMSC_MAC_CSR_TXEN;
1454 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
1455 smsc_write_reg(sc, SMSC_TX_CFG, SMSC_TX_CFG_ON);
1460 sc->sc_mac_csr |= SMSC_MAC_CSR_RXEN;
1461 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
1472 smsc_err_printf(sc, "smsc_chip_init failed (err=%d)\n", err);
1478 * smsc_ioctl - ioctl function for the device
1479 * @ifp: interface pointer
1480 * @cmd: the ioctl command
1481 * @data: data passed in the ioctl call, typically a pointer to struct ifreq.
1483 * The ioctl routine is overridden to detect change requests for the H/W
1484 * checksum capabilities.
1487 * 0 on success and an error code on failure.
1490 smsc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1492 struct usb_ether *ue = ifp->if_softc;
1493 struct smsc_softc *sc;
1499 if (cmd == SIOCSIFCAP) {
1501 sc = uether_getsc(ue);
1502 ifr = (struct ifreq *)data;
1509 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1511 /* Modify the RX CSUM enable bits */
1512 if ((mask & IFCAP_RXCSUM) != 0 &&
1513 (ifp->if_capabilities & IFCAP_RXCSUM) != 0) {
1514 ifp->if_capenable ^= IFCAP_RXCSUM;
1516 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1517 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1524 #if __FreeBSD_version > 1000000
1531 rc = uether_ioctl(ifp, cmd, data);
1539 smsc_fdt_find_eth_node(phandle_t start)
1541 phandle_t child, node;
1543 /* Traverse through entire tree to find usb ethernet nodes. */
1544 for (node = OF_child(start); node != 0; node = OF_peer(node)) {
1545 if (fdt_is_compatible(node, "net,ethernet") &&
1546 fdt_is_compatible(node, "usb,device"))
1548 child = smsc_fdt_find_eth_node(node);
1557 * Get MAC address from FDT blob. Firmware or loader should fill
1558 * mac-address or local-mac-address property. Returns 0 if MAC address
1559 * obtained, error code otherwise.
1562 smsc_fdt_find_mac(unsigned char *mac)
1564 phandle_t node, root;
1567 root = OF_finddevice("/");
1568 node = smsc_fdt_find_eth_node(root);
1571 /* Check if there is property */
1572 if ((len = OF_getproplen(node, "local-mac-address")) > 0) {
1573 if (len != ETHER_ADDR_LEN)
1576 OF_getprop(node, "local-mac-address", mac,
1581 if ((len = OF_getproplen(node, "mac-address")) > 0) {
1582 if (len != ETHER_ADDR_LEN)
1585 OF_getprop(node, "mac-address", mac,
1596 * smsc_attach_post - Called after the driver attached to the USB interface
1597 * @ue: the USB ethernet device
1599 * This is where the chip is intialised for the first time. This is different
1600 * from the smsc_init() function in that that one is designed to setup the
1601 * H/W to match the UE settings and can be called after a reset.
1606 smsc_attach_post(struct usb_ether *ue)
1608 struct smsc_softc *sc = uether_getsc(ue);
1609 uint32_t mac_h, mac_l;
1612 smsc_dbg_printf(sc, "smsc_attach_post\n");
1614 /* Setup some of the basics */
1618 /* Attempt to get the mac address, if an EEPROM is not attached this
1619 * will just return FF:FF:FF:FF:FF:FF, so in such cases we invent a MAC
1620 * address based on urandom.
1622 memset(sc->sc_ue.ue_eaddr, 0xff, ETHER_ADDR_LEN);
1624 /* Check if there is already a MAC address in the register */
1625 if ((smsc_read_reg(sc, SMSC_MAC_ADDRL, &mac_l) == 0) &&
1626 (smsc_read_reg(sc, SMSC_MAC_ADDRH, &mac_h) == 0)) {
1627 sc->sc_ue.ue_eaddr[5] = (uint8_t)((mac_h >> 8) & 0xff);
1628 sc->sc_ue.ue_eaddr[4] = (uint8_t)((mac_h) & 0xff);
1629 sc->sc_ue.ue_eaddr[3] = (uint8_t)((mac_l >> 24) & 0xff);
1630 sc->sc_ue.ue_eaddr[2] = (uint8_t)((mac_l >> 16) & 0xff);
1631 sc->sc_ue.ue_eaddr[1] = (uint8_t)((mac_l >> 8) & 0xff);
1632 sc->sc_ue.ue_eaddr[0] = (uint8_t)((mac_l) & 0xff);
1635 /* MAC address is not set so try to read from EEPROM, if that fails generate
1636 * a random MAC address.
1638 if (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr)) {
1640 err = smsc_eeprom_read(sc, 0x01, sc->sc_ue.ue_eaddr, ETHER_ADDR_LEN);
1642 if ((err != 0) || (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr)))
1643 err = smsc_fdt_find_mac(sc->sc_ue.ue_eaddr);
1645 if ((err != 0) || (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr))) {
1646 read_random(sc->sc_ue.ue_eaddr, ETHER_ADDR_LEN);
1647 sc->sc_ue.ue_eaddr[0] &= ~0x01; /* unicast */
1648 sc->sc_ue.ue_eaddr[0] |= 0x02; /* locally administered */
1652 /* Initialise the chip for the first time */
1658 * smsc_attach_post_sub - Called after the driver attached to the USB interface
1659 * @ue: the USB ethernet device
1661 * Most of this is boilerplate code and copied from the base USB ethernet
1662 * driver. It has been overriden so that we can indicate to the system that
1663 * the chip supports H/W checksumming.
1666 * Returns 0 on success or a negative error code.
1668 #if __FreeBSD_version > 1000000
1670 smsc_attach_post_sub(struct usb_ether *ue)
1672 struct smsc_softc *sc;
1676 sc = uether_getsc(ue);
1678 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1679 ifp->if_start = uether_start;
1680 ifp->if_ioctl = smsc_ioctl;
1681 ifp->if_init = uether_init;
1682 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
1683 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
1684 IFQ_SET_READY(&ifp->if_snd);
1686 /* The chip supports TCP/UDP checksum offloading on TX and RX paths, however
1687 * currently only RX checksum is supported in the driver (see top of file).
1689 ifp->if_capabilities |= IFCAP_RXCSUM | IFCAP_VLAN_MTU;
1690 ifp->if_hwassist = 0;
1692 /* TX checksuming is disabled (for now?)
1693 ifp->if_capabilities |= IFCAP_TXCSUM;
1694 ifp->if_capenable |= IFCAP_TXCSUM;
1695 ifp->if_hwassist = CSUM_TCP | CSUM_UDP;
1698 ifp->if_capenable = ifp->if_capabilities;
1701 error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp,
1702 uether_ifmedia_upd, ue->ue_methods->ue_mii_sts,
1703 BMSR_DEFCAPMASK, sc->sc_phyno, MII_OFFSET_ANY, 0);
1708 #endif /* __FreeBSD_version > 1000000 */
1712 * smsc_probe - Probe the interface.
1713 * @dev: smsc device handle
1715 * Checks if the device is a match for this driver.
1718 * Returns 0 on success or an error code on failure.
1721 smsc_probe(device_t dev)
1723 struct usb_attach_arg *uaa = device_get_ivars(dev);
1725 if (uaa->usb_mode != USB_MODE_HOST)
1727 if (uaa->info.bConfigIndex != SMSC_CONFIG_INDEX)
1729 if (uaa->info.bIfaceIndex != SMSC_IFACE_IDX)
1732 return (usbd_lookup_id_by_uaa(smsc_devs, sizeof(smsc_devs), uaa));
1737 * smsc_attach - Attach the interface.
1738 * @dev: smsc device handle
1740 * Allocate softc structures, do ifmedia setup and ethernet/BPF attach.
1743 * Returns 0 on success or a negative error code.
1746 smsc_attach(device_t dev)
1748 struct usb_attach_arg *uaa = device_get_ivars(dev);
1749 struct smsc_softc *sc = device_get_softc(dev);
1750 struct usb_ether *ue = &sc->sc_ue;
1751 uint8_t iface_index;
1754 sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
1756 device_set_usb_desc(dev);
1758 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
1760 /* Setup the endpoints for the SMSC LAN95xx device(s) */
1761 iface_index = SMSC_IFACE_IDX;
1762 err = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
1763 smsc_config, SMSC_N_TRANSFER, sc, &sc->sc_mtx);
1765 device_printf(dev, "error: allocating USB transfers failed\n");
1771 ue->ue_udev = uaa->device;
1772 ue->ue_mtx = &sc->sc_mtx;
1773 ue->ue_methods = &smsc_ue_methods;
1775 err = uether_ifattach(ue);
1777 device_printf(dev, "error: could not attach interface\n");
1780 return (0); /* success */
1784 return (ENXIO); /* failure */
1788 * smsc_detach - Detach the interface.
1789 * @dev: smsc device handle
1795 smsc_detach(device_t dev)
1797 struct smsc_softc *sc = device_get_softc(dev);
1798 struct usb_ether *ue = &sc->sc_ue;
1800 usbd_transfer_unsetup(sc->sc_xfer, SMSC_N_TRANSFER);
1801 uether_ifdetach(ue);
1802 mtx_destroy(&sc->sc_mtx);
1807 static device_method_t smsc_methods[] = {
1808 /* Device interface */
1809 DEVMETHOD(device_probe, smsc_probe),
1810 DEVMETHOD(device_attach, smsc_attach),
1811 DEVMETHOD(device_detach, smsc_detach),
1814 DEVMETHOD(bus_print_child, bus_generic_print_child),
1815 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
1818 DEVMETHOD(miibus_readreg, smsc_miibus_readreg),
1819 DEVMETHOD(miibus_writereg, smsc_miibus_writereg),
1820 DEVMETHOD(miibus_statchg, smsc_miibus_statchg),
1825 static driver_t smsc_driver = {
1827 .methods = smsc_methods,
1828 .size = sizeof(struct smsc_softc),
1831 static devclass_t smsc_devclass;
1833 DRIVER_MODULE(smsc, uhub, smsc_driver, smsc_devclass, NULL, 0);
1834 DRIVER_MODULE(miibus, smsc, miibus_driver, miibus_devclass, 0, 0);
1835 MODULE_DEPEND(smsc, uether, 1, 1, 1);
1836 MODULE_DEPEND(smsc, usb, 1, 1, 1);
1837 MODULE_DEPEND(smsc, ether, 1, 1, 1);
1838 MODULE_DEPEND(smsc, miibus, 1, 1, 1);
1839 MODULE_VERSION(smsc, 1);