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(LAN9514_ETH, 0),
132 #define smsc_dbg_printf(sc, fmt, args...) \
134 if (smsc_debug > 0) \
135 device_printf((sc)->sc_ue.ue_dev, "debug: " fmt, ##args); \
138 #define smsc_dbg_printf(sc, fmt, args...)
141 #define smsc_warn_printf(sc, fmt, args...) \
142 device_printf((sc)->sc_ue.ue_dev, "warning: " fmt, ##args)
144 #define smsc_err_printf(sc, fmt, args...) \
145 device_printf((sc)->sc_ue.ue_dev, "error: " fmt, ##args)
148 #define ETHER_IS_ZERO(addr) \
149 (!(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]))
151 #define ETHER_IS_VALID(addr) \
152 (!ETHER_IS_MULTICAST(addr) && !ETHER_IS_ZERO(addr))
154 static device_probe_t smsc_probe;
155 static device_attach_t smsc_attach;
156 static device_detach_t smsc_detach;
158 static usb_callback_t smsc_bulk_read_callback;
159 static usb_callback_t smsc_bulk_write_callback;
161 static miibus_readreg_t smsc_miibus_readreg;
162 static miibus_writereg_t smsc_miibus_writereg;
163 static miibus_statchg_t smsc_miibus_statchg;
165 #if __FreeBSD_version > 1000000
166 static int smsc_attach_post_sub(struct usb_ether *ue);
168 static uether_fn_t smsc_attach_post;
169 static uether_fn_t smsc_init;
170 static uether_fn_t smsc_stop;
171 static uether_fn_t smsc_start;
172 static uether_fn_t smsc_tick;
173 static uether_fn_t smsc_setmulti;
174 static uether_fn_t smsc_setpromisc;
176 static int smsc_ifmedia_upd(struct ifnet *);
177 static void smsc_ifmedia_sts(struct ifnet *, struct ifmediareq *);
179 static int smsc_chip_init(struct smsc_softc *sc);
180 static int smsc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
182 static const struct usb_config smsc_config[SMSC_N_TRANSFER] = {
184 [SMSC_BULK_DT_WR] = {
186 .endpoint = UE_ADDR_ANY,
187 .direction = UE_DIR_OUT,
189 .bufsize = 16 * (MCLBYTES + 16),
190 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
191 .callback = smsc_bulk_write_callback,
192 .timeout = 10000, /* 10 seconds */
195 [SMSC_BULK_DT_RD] = {
197 .endpoint = UE_ADDR_ANY,
198 .direction = UE_DIR_IN,
199 .bufsize = 20480, /* bytes */
200 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
201 .callback = smsc_bulk_read_callback,
202 .timeout = 0, /* no timeout */
205 /* The SMSC chip supports an interrupt endpoints, however they aren't
206 * needed as we poll on the MII status.
210 static const struct usb_ether_methods smsc_ue_methods = {
211 .ue_attach_post = smsc_attach_post,
212 #if __FreeBSD_version > 1000000
213 .ue_attach_post_sub = smsc_attach_post_sub,
215 .ue_start = smsc_start,
216 .ue_ioctl = smsc_ioctl,
217 .ue_init = smsc_init,
218 .ue_stop = smsc_stop,
219 .ue_tick = smsc_tick,
220 .ue_setmulti = smsc_setmulti,
221 .ue_setpromisc = smsc_setpromisc,
222 .ue_mii_upd = smsc_ifmedia_upd,
223 .ue_mii_sts = smsc_ifmedia_sts,
227 * smsc_read_reg - Reads a 32-bit register on the device
228 * @sc: driver soft context
229 * @off: offset of the register
230 * @data: pointer a value that will be populated with the register value
233 * The device lock must be held before calling this function.
236 * 0 on success, a USB_ERR_?? error code on failure.
239 smsc_read_reg(struct smsc_softc *sc, uint32_t off, uint32_t *data)
241 struct usb_device_request req;
245 SMSC_LOCK_ASSERT(sc, MA_OWNED);
247 req.bmRequestType = UT_READ_VENDOR_DEVICE;
248 req.bRequest = SMSC_UR_READ_REG;
249 USETW(req.wValue, 0);
250 USETW(req.wIndex, off);
251 USETW(req.wLength, 4);
253 err = uether_do_request(&sc->sc_ue, &req, &buf, 1000);
255 smsc_warn_printf(sc, "Failed to read register 0x%0x\n", off);
257 *data = le32toh(buf);
263 * smsc_write_reg - Writes a 32-bit register on the device
264 * @sc: driver soft context
265 * @off: offset of the register
266 * @data: the 32-bit value to write into the register
269 * The device lock must be held before calling this function.
272 * 0 on success, a USB_ERR_?? error code on failure.
275 smsc_write_reg(struct smsc_softc *sc, uint32_t off, uint32_t data)
277 struct usb_device_request req;
281 SMSC_LOCK_ASSERT(sc, MA_OWNED);
285 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
286 req.bRequest = SMSC_UR_WRITE_REG;
287 USETW(req.wValue, 0);
288 USETW(req.wIndex, off);
289 USETW(req.wLength, 4);
291 err = uether_do_request(&sc->sc_ue, &req, &buf, 1000);
293 smsc_warn_printf(sc, "Failed to write register 0x%0x\n", off);
299 * smsc_wait_for_bits - Polls on a register value until bits are cleared
301 * @reg: offset of the register
302 * @bits: if the bits are clear the function returns
305 * The device lock must be held before calling this function.
308 * 0 on success, or a USB_ERR_?? error code on failure.
311 smsc_wait_for_bits(struct smsc_softc *sc, uint32_t reg, uint32_t bits)
313 usb_ticks_t start_ticks;
314 const usb_ticks_t max_ticks = USB_MS_TO_TICKS(1000);
318 SMSC_LOCK_ASSERT(sc, MA_OWNED);
320 start_ticks = (usb_ticks_t)ticks;
322 if ((err = smsc_read_reg(sc, reg, &val)) != 0)
327 uether_pause(&sc->sc_ue, hz / 100);
328 } while (((usb_ticks_t)(ticks - start_ticks)) < max_ticks);
330 return (USB_ERR_TIMEOUT);
334 * smsc_eeprom_read - Reads the attached EEPROM
336 * @off: the eeprom address offset
337 * @buf: stores the bytes
338 * @buflen: the number of bytes to read
340 * Simply reads bytes from an attached eeprom.
343 * The function takes and releases the device lock if it is not already held.
346 * 0 on success, or a USB_ERR_?? error code on failure.
349 smsc_eeprom_read(struct smsc_softc *sc, uint16_t off, uint8_t *buf, uint16_t buflen)
351 usb_ticks_t start_ticks;
352 const usb_ticks_t max_ticks = USB_MS_TO_TICKS(1000);
358 locked = mtx_owned(&sc->sc_mtx);
362 err = smsc_wait_for_bits(sc, SMSC_EEPROM_CMD, SMSC_EEPROM_CMD_BUSY);
364 smsc_warn_printf(sc, "eeprom busy, failed to read data\n");
368 /* start reading the bytes, one at a time */
369 for (i = 0; i < buflen; i++) {
371 val = SMSC_EEPROM_CMD_BUSY | (SMSC_EEPROM_CMD_ADDR_MASK & (off + i));
372 if ((err = smsc_write_reg(sc, SMSC_EEPROM_CMD, val)) != 0)
375 start_ticks = (usb_ticks_t)ticks;
377 if ((err = smsc_read_reg(sc, SMSC_EEPROM_CMD, &val)) != 0)
379 if (!(val & SMSC_EEPROM_CMD_BUSY) || (val & SMSC_EEPROM_CMD_TIMEOUT))
382 uether_pause(&sc->sc_ue, hz / 100);
383 } while (((usb_ticks_t)(ticks - start_ticks)) < max_ticks);
385 if (val & (SMSC_EEPROM_CMD_BUSY | SMSC_EEPROM_CMD_TIMEOUT)) {
386 smsc_warn_printf(sc, "eeprom command failed\n");
387 err = USB_ERR_IOERROR;
391 if ((err = smsc_read_reg(sc, SMSC_EEPROM_DATA, &val)) != 0)
394 buf[i] = (val & 0xff);
405 * smsc_miibus_readreg - Reads a MII/MDIO register
406 * @dev: usb ether device
407 * @phy: the number of phy reading from
408 * @reg: the register address
410 * Attempts to read a phy register over the MII bus.
413 * Takes and releases the device mutex lock if not already held.
416 * Returns the 16-bits read from the MII register, if this function fails 0
420 smsc_miibus_readreg(device_t dev, int phy, int reg)
422 struct smsc_softc *sc = device_get_softc(dev);
427 locked = mtx_owned(&sc->sc_mtx);
431 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
432 smsc_warn_printf(sc, "MII is busy\n");
436 addr = (phy << 11) | (reg << 6) | SMSC_MII_READ;
437 smsc_write_reg(sc, SMSC_MII_ADDR, addr);
439 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0)
440 smsc_warn_printf(sc, "MII read timeout\n");
442 smsc_read_reg(sc, SMSC_MII_DATA, &val);
449 return (val & 0xFFFF);
453 * smsc_miibus_writereg - Writes a MII/MDIO register
454 * @dev: usb ether device
455 * @phy: the number of phy writing to
456 * @reg: the register address
457 * @val: the value to write
459 * Attempts to write a phy register over the MII bus.
462 * Takes and releases the device mutex lock if not already held.
465 * Always returns 0 regardless of success or failure.
468 smsc_miibus_writereg(device_t dev, int phy, int reg, int val)
470 struct smsc_softc *sc = device_get_softc(dev);
474 if (sc->sc_phyno != phy)
477 locked = mtx_owned(&sc->sc_mtx);
481 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
482 smsc_warn_printf(sc, "MII is busy\n");
487 smsc_write_reg(sc, SMSC_MII_DATA, val);
489 addr = (phy << 11) | (reg << 6) | SMSC_MII_WRITE;
490 smsc_write_reg(sc, SMSC_MII_ADDR, addr);
492 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0)
493 smsc_warn_printf(sc, "MII write timeout\n");
504 * smsc_miibus_statchg - Called to detect phy status change
505 * @dev: usb ether device
507 * This function is called periodically by the system to poll for status
508 * changes of the link.
511 * Takes and releases the device mutex lock if not already held.
514 smsc_miibus_statchg(device_t dev)
516 struct smsc_softc *sc = device_get_softc(dev);
517 struct mii_data *mii = uether_getmii(&sc->sc_ue);
524 locked = mtx_owned(&sc->sc_mtx);
528 ifp = uether_getifp(&sc->sc_ue);
529 if (mii == NULL || ifp == NULL ||
530 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
533 /* Use the MII status to determine link status */
534 sc->sc_flags &= ~SMSC_FLAG_LINK;
535 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
536 (IFM_ACTIVE | IFM_AVALID)) {
537 switch (IFM_SUBTYPE(mii->mii_media_active)) {
540 sc->sc_flags |= SMSC_FLAG_LINK;
543 /* Gigabit ethernet not supported by chipset */
550 /* Lost link, do nothing. */
551 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) {
552 smsc_dbg_printf(sc, "link flag not set\n");
556 err = smsc_read_reg(sc, SMSC_AFC_CFG, &afc_cfg);
558 smsc_warn_printf(sc, "failed to read initial AFC_CFG, error %d\n", err);
562 /* Enable/disable full duplex operation and TX/RX pause */
563 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
564 smsc_dbg_printf(sc, "full duplex operation\n");
565 sc->sc_mac_csr &= ~SMSC_MAC_CSR_RCVOWN;
566 sc->sc_mac_csr |= SMSC_MAC_CSR_FDPX;
568 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
573 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
579 smsc_dbg_printf(sc, "half duplex operation\n");
580 sc->sc_mac_csr &= ~SMSC_MAC_CSR_FDPX;
581 sc->sc_mac_csr |= SMSC_MAC_CSR_RCVOWN;
587 err = smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
588 err += smsc_write_reg(sc, SMSC_FLOW, flow);
589 err += smsc_write_reg(sc, SMSC_AFC_CFG, afc_cfg);
591 smsc_warn_printf(sc, "media change failed, error %d\n", err);
599 * smsc_ifmedia_upd - Set media options
600 * @ifp: interface pointer
602 * Basically boilerplate code that simply calls the mii functions to set the
606 * The device lock must be held before this function is called.
609 * Returns 0 on success or a negative error code.
612 smsc_ifmedia_upd(struct ifnet *ifp)
614 struct smsc_softc *sc = ifp->if_softc;
615 struct mii_data *mii = uether_getmii(&sc->sc_ue);
616 struct mii_softc *miisc;
619 SMSC_LOCK_ASSERT(sc, MA_OWNED);
621 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
623 err = mii_mediachg(mii);
628 * smsc_ifmedia_sts - Report current media status
629 * @ifp: inet interface pointer
630 * @ifmr: interface media request
632 * Basically boilerplate code that simply calls the mii functions to get the
636 * Internally takes and releases the device lock.
639 smsc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
641 struct smsc_softc *sc = ifp->if_softc;
642 struct mii_data *mii = uether_getmii(&sc->sc_ue);
646 ifmr->ifm_active = mii->mii_media_active;
647 ifmr->ifm_status = mii->mii_media_status;
652 * smsc_hash - Calculate the hash of a mac address
653 * @addr: The mac address to calculate the hash on
655 * This function is used when configuring a range of m'cast mac addresses to
656 * filter on. The hash of the mac address is put in the device's mac hash
660 * Returns a value from 0-63 value which is the hash of the mac address.
662 static inline uint32_t
663 smsc_hash(uint8_t addr[ETHER_ADDR_LEN])
665 return (ether_crc32_be(addr, ETHER_ADDR_LEN) >> 26) & 0x3f;
669 * smsc_setmulti - Setup multicast
670 * @ue: usb ethernet device context
672 * Tells the device to either accept frames with a multicast mac address, a
673 * select group of m'cast mac addresses or just the devices mac address.
676 * Should be called with the SMSC lock held.
679 smsc_setmulti(struct usb_ether *ue)
681 struct smsc_softc *sc = uether_getsc(ue);
682 struct ifnet *ifp = uether_getifp(ue);
683 struct ifmultiaddr *ifma;
684 uint32_t hashtbl[2] = { 0, 0 };
687 SMSC_LOCK_ASSERT(sc, MA_OWNED);
689 if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
690 smsc_dbg_printf(sc, "receive all multicast enabled\n");
691 sc->sc_mac_csr |= SMSC_MAC_CSR_MCPAS;
692 sc->sc_mac_csr &= ~SMSC_MAC_CSR_HPFILT;
695 /* Take the lock of the mac address list before hashing each of them */
698 if (!TAILQ_EMPTY(&ifp->if_multiaddrs)) {
699 /* We are filtering on a set of address so calculate hashes of each
700 * of the address and set the corresponding bits in the register.
702 sc->sc_mac_csr |= SMSC_MAC_CSR_HPFILT;
703 sc->sc_mac_csr &= ~(SMSC_MAC_CSR_PRMS | SMSC_MAC_CSR_MCPAS);
705 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
706 if (ifma->ifma_addr->sa_family != AF_LINK)
709 hash = smsc_hash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
710 hashtbl[hash >> 5] |= 1 << (hash & 0x1F);
713 /* Only receive packets with destination set to our mac address */
714 sc->sc_mac_csr &= ~(SMSC_MAC_CSR_MCPAS | SMSC_MAC_CSR_HPFILT);
717 if_maddr_runlock(ifp);
720 if (sc->sc_mac_csr & SMSC_MAC_CSR_HPFILT)
721 smsc_dbg_printf(sc, "receive select group of macs\n");
723 smsc_dbg_printf(sc, "receive own packets only\n");
726 /* Write the hash table and mac control registers */
727 smsc_write_reg(sc, SMSC_HASHH, hashtbl[1]);
728 smsc_write_reg(sc, SMSC_HASHL, hashtbl[0]);
729 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
734 * smsc_setpromisc - Enables/disables promiscuous mode
735 * @ue: usb ethernet device context
738 * Should be called with the SMSC lock held.
741 smsc_setpromisc(struct usb_ether *ue)
743 struct smsc_softc *sc = uether_getsc(ue);
744 struct ifnet *ifp = uether_getifp(ue);
746 smsc_dbg_printf(sc, "promiscuous mode %sabled\n",
747 (ifp->if_flags & IFF_PROMISC) ? "en" : "dis");
749 SMSC_LOCK_ASSERT(sc, MA_OWNED);
751 if (ifp->if_flags & IFF_PROMISC)
752 sc->sc_mac_csr |= SMSC_MAC_CSR_PRMS;
754 sc->sc_mac_csr &= ~SMSC_MAC_CSR_PRMS;
756 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
761 * smsc_sethwcsum - Enable or disable H/W UDP and TCP checksumming
762 * @sc: driver soft context
765 * Should be called with the SMSC lock held.
768 * Returns 0 on success or a negative error code.
770 static int smsc_sethwcsum(struct smsc_softc *sc)
772 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
779 SMSC_LOCK_ASSERT(sc, MA_OWNED);
781 err = smsc_read_reg(sc, SMSC_COE_CTRL, &val);
783 smsc_warn_printf(sc, "failed to read SMSC_COE_CTRL (err=%d)\n", err);
787 /* Enable/disable the Rx checksum */
788 if ((ifp->if_capabilities & ifp->if_capenable) & IFCAP_RXCSUM)
789 val |= SMSC_COE_CTRL_RX_EN;
791 val &= ~SMSC_COE_CTRL_RX_EN;
793 /* Enable/disable the Tx checksum (currently not supported) */
794 if ((ifp->if_capabilities & ifp->if_capenable) & IFCAP_TXCSUM)
795 val |= SMSC_COE_CTRL_TX_EN;
797 val &= ~SMSC_COE_CTRL_TX_EN;
799 err = smsc_write_reg(sc, SMSC_COE_CTRL, val);
801 smsc_warn_printf(sc, "failed to write SMSC_COE_CTRL (err=%d)\n", err);
810 * smsc_setmacaddress - Sets the mac address in the device
811 * @sc: driver soft context
812 * @addr: pointer to array contain at least 6 bytes of the mac
814 * Writes the MAC address into the device, usually the MAC is programmed with
815 * values from the EEPROM.
818 * Should be called with the SMSC lock held.
821 * Returns 0 on success or a negative error code.
824 smsc_setmacaddress(struct smsc_softc *sc, const uint8_t *addr)
829 smsc_dbg_printf(sc, "setting mac address to %02x:%02x:%02x:%02x:%02x:%02x\n",
830 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
832 SMSC_LOCK_ASSERT(sc, MA_OWNED);
834 val = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
835 if ((err = smsc_write_reg(sc, SMSC_MAC_ADDRL, val)) != 0)
838 val = (addr[5] << 8) | addr[4];
839 err = smsc_write_reg(sc, SMSC_MAC_ADDRH, val);
846 * smsc_reset - Reset the SMSC chip
847 * @sc: device soft context
850 * Should be called with the SMSC lock held.
853 smsc_reset(struct smsc_softc *sc)
855 struct usb_config_descriptor *cd;
858 cd = usbd_get_config_descriptor(sc->sc_ue.ue_udev);
860 err = usbd_req_set_config(sc->sc_ue.ue_udev, &sc->sc_mtx,
861 cd->bConfigurationValue);
863 smsc_warn_printf(sc, "reset failed (ignored)\n");
865 /* Wait a little while for the chip to get its brains in order. */
866 uether_pause(&sc->sc_ue, hz / 100);
868 /* Reinitialize controller to achieve full reset. */
874 * smsc_init - Initialises the LAN95xx chip
875 * @ue: USB ether interface
877 * Called when the interface is brought up (i.e. ifconfig ue0 up), this
878 * initialise the interface and the rx/tx pipes.
881 * Should be called with the SMSC lock held.
884 smsc_init(struct usb_ether *ue)
886 struct smsc_softc *sc = uether_getsc(ue);
887 struct ifnet *ifp = uether_getifp(ue);
889 SMSC_LOCK_ASSERT(sc, MA_OWNED);
891 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
894 /* Cancel pending I/O */
897 #if __FreeBSD_version <= 1000000
898 /* On earlier versions this was the first place we could tell the system
899 * that we supported h/w csuming, however this is only called after the
900 * the interface has been brought up - not ideal.
902 if (!(ifp->if_capabilities & IFCAP_RXCSUM)) {
903 ifp->if_capabilities |= IFCAP_RXCSUM;
904 ifp->if_capenable |= IFCAP_RXCSUM;
905 ifp->if_hwassist = 0;
908 /* TX checksuming is disabled for now
909 ifp->if_capabilities |= IFCAP_TXCSUM;
910 ifp->if_capenable |= IFCAP_TXCSUM;
911 ifp->if_hwassist = CSUM_TCP | CSUM_UDP;
915 /* Reset the ethernet interface. */
918 /* Load the multicast filter. */
921 /* TCP/UDP checksum offload engines. */
924 usbd_xfer_set_stall(sc->sc_xfer[SMSC_BULK_DT_WR]);
926 /* Indicate we are up and running. */
927 ifp->if_drv_flags |= IFF_DRV_RUNNING;
929 /* Switch to selected media. */
930 smsc_ifmedia_upd(ifp);
935 * smsc_bulk_read_callback - Read callback used to process the USB URB
936 * @xfer: the USB transfer
939 * Reads the URB data which can contain one or more ethernet frames, the
940 * frames are copyed into a mbuf and given to the system.
943 * No locking required, doesn't access internal driver settings.
946 smsc_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
948 struct smsc_softc *sc = usbd_xfer_softc(xfer);
949 struct usb_ether *ue = &sc->sc_ue;
950 struct ifnet *ifp = uether_getifp(ue);
952 struct usb_page_cache *pc;
958 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
959 smsc_dbg_printf(sc, "rx : actlen %d\n", actlen);
961 switch (USB_GET_STATE(xfer)) {
962 case USB_ST_TRANSFERRED:
964 /* There is always a zero length frame after bringing the IF up */
965 if (actlen < (sizeof(rxhdr) + ETHER_CRC_LEN))
968 /* There maybe multiple packets in the USB frame, each will have a
969 * header and each needs to have it's own mbuf allocated and populated
972 pc = usbd_xfer_get_frame(xfer, 0);
975 while (off < actlen) {
977 /* The frame header is always aligned on a 4 byte boundary */
978 off = ((off + 0x3) & ~0x3);
980 usbd_copy_out(pc, off, &rxhdr, sizeof(rxhdr));
981 off += (sizeof(rxhdr) + ETHER_ALIGN);
982 rxhdr = le32toh(rxhdr);
984 pktlen = (uint16_t)SMSC_RX_STAT_FRM_LENGTH(rxhdr);
986 smsc_dbg_printf(sc, "rx : rxhdr 0x%08x : pktlen %d : actlen %d : "
987 "off %d\n", rxhdr, pktlen, actlen, off);
990 if (rxhdr & SMSC_RX_STAT_ERROR) {
991 smsc_dbg_printf(sc, "rx error (hdr 0x%08x)\n", rxhdr);
992 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
993 if (rxhdr & SMSC_RX_STAT_COLLISION)
994 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
997 /* Check if the ethernet frame is too big or too small */
998 if ((pktlen < ETHER_HDR_LEN) || (pktlen > (actlen - off)))
1001 /* Create a new mbuf to store the packet in */
1002 m = uether_newbuf();
1004 smsc_warn_printf(sc, "failed to create new mbuf\n");
1005 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1009 usbd_copy_out(pc, off, mtod(m, uint8_t *), pktlen);
1011 /* Check if RX TCP/UDP checksumming is being offloaded */
1012 if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) {
1014 struct ether_header *eh;
1016 eh = mtod(m, struct ether_header *);
1018 /* Remove the extra 2 bytes of the csum */
1021 /* The checksum appears to be simplistically calculated
1022 * over the udp/tcp header and data up to the end of the
1023 * eth frame. Which means if the eth frame is padded
1024 * the csum calculation is incorrectly performed over
1025 * the padding bytes as well. Therefore to be safe we
1026 * ignore the H/W csum on frames less than or equal to
1029 * Ignore H/W csum for non-IPv4 packets.
1031 if ((be16toh(eh->ether_type) == ETHERTYPE_IP) &&
1032 (pktlen > ETHER_MIN_LEN)) {
1035 ip = (struct ip *)(eh + 1);
1036 if ((ip->ip_v == IPVERSION) &&
1037 ((ip->ip_p == IPPROTO_TCP) ||
1038 (ip->ip_p == IPPROTO_UDP))) {
1039 /* Indicate the UDP/TCP csum has been calculated */
1040 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
1042 /* Copy the TCP/UDP checksum from the last 2 bytes
1043 * of the transfer and put in the csum_data field.
1045 usbd_copy_out(pc, (off + pktlen),
1046 &m->m_pkthdr.csum_data, 2);
1048 /* The data is copied in network order, but the
1049 * csum algorithm in the kernel expects it to be
1050 * in host network order.
1052 m->m_pkthdr.csum_data = ntohs(m->m_pkthdr.csum_data);
1054 smsc_dbg_printf(sc, "RX checksum offloaded (0x%04x)\n",
1055 m->m_pkthdr.csum_data);
1059 /* Need to adjust the offset as well or we'll be off
1060 * by 2 because the csum is removed from the packet
1066 /* Finally enqueue the mbuf on the receive queue */
1067 /* Remove 4 trailing bytes */
1068 if (pktlen < (4 + ETHER_HDR_LEN)) {
1072 uether_rxmbuf(ue, m, pktlen - 4);
1075 /* Update the offset to move to the next potential packet */
1083 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1084 usbd_transfer_submit(xfer);
1089 if (error != USB_ERR_CANCELLED) {
1090 smsc_warn_printf(sc, "bulk read error, %s\n", usbd_errstr(error));
1091 usbd_xfer_set_stall(xfer);
1099 * smsc_bulk_write_callback - Write callback used to send ethernet frame(s)
1100 * @xfer: the USB transfer
1101 * @error: error code if the transfers is in an errored state
1103 * The main write function that pulls ethernet frames off the queue and sends
1110 smsc_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
1112 struct smsc_softc *sc = usbd_xfer_softc(xfer);
1113 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1114 struct usb_page_cache *pc;
1117 uint32_t frm_len = 0;
1120 switch (USB_GET_STATE(xfer)) {
1121 case USB_ST_TRANSFERRED:
1122 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1127 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0 ||
1128 (ifp->if_drv_flags & IFF_DRV_OACTIVE) != 0) {
1129 /* Don't send anything if there is no link or controller is busy. */
1133 for (nframes = 0; nframes < 16 &&
1134 !IFQ_DRV_IS_EMPTY(&ifp->if_snd); nframes++) {
1135 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1138 usbd_xfer_set_frame_offset(xfer, nframes * MCLBYTES,
1141 pc = usbd_xfer_get_frame(xfer, nframes);
1143 /* Each frame is prefixed with two 32-bit values describing the
1144 * length of the packet and buffer.
1146 txhdr = SMSC_TX_CTRL_0_BUF_SIZE(m->m_pkthdr.len) |
1147 SMSC_TX_CTRL_0_FIRST_SEG | SMSC_TX_CTRL_0_LAST_SEG;
1148 txhdr = htole32(txhdr);
1149 usbd_copy_in(pc, 0, &txhdr, sizeof(txhdr));
1151 txhdr = SMSC_TX_CTRL_1_PKT_LENGTH(m->m_pkthdr.len);
1152 txhdr = htole32(txhdr);
1153 usbd_copy_in(pc, 4, &txhdr, sizeof(txhdr));
1157 /* Next copy in the actual packet */
1158 usbd_m_copy_in(pc, frm_len, m, 0, m->m_pkthdr.len);
1159 frm_len += m->m_pkthdr.len;
1161 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1163 /* If there's a BPF listener, bounce a copy of this frame to him */
1168 /* Set frame length. */
1169 usbd_xfer_set_frame_len(xfer, nframes, frm_len);
1172 usbd_xfer_set_frames(xfer, nframes);
1173 usbd_transfer_submit(xfer);
1174 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1179 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1180 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1182 if (error != USB_ERR_CANCELLED) {
1183 smsc_err_printf(sc, "usb error on tx: %s\n", usbd_errstr(error));
1184 usbd_xfer_set_stall(xfer);
1192 * smsc_tick - Called periodically to monitor the state of the LAN95xx chip
1193 * @ue: USB ether interface
1195 * Simply calls the mii status functions to check the state of the link.
1198 * Should be called with the SMSC lock held.
1201 smsc_tick(struct usb_ether *ue)
1203 struct smsc_softc *sc = uether_getsc(ue);
1204 struct mii_data *mii = uether_getmii(&sc->sc_ue);
1206 SMSC_LOCK_ASSERT(sc, MA_OWNED);
1209 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) {
1210 smsc_miibus_statchg(ue->ue_dev);
1211 if ((sc->sc_flags & SMSC_FLAG_LINK) != 0)
1217 * smsc_start - Starts communication with the LAN95xx chip
1218 * @ue: USB ether interface
1224 smsc_start(struct usb_ether *ue)
1226 struct smsc_softc *sc = uether_getsc(ue);
1229 * start the USB transfers, if not already started:
1231 usbd_transfer_start(sc->sc_xfer[SMSC_BULK_DT_RD]);
1232 usbd_transfer_start(sc->sc_xfer[SMSC_BULK_DT_WR]);
1236 * smsc_stop - Stops communication with the LAN95xx chip
1237 * @ue: USB ether interface
1243 smsc_stop(struct usb_ether *ue)
1245 struct smsc_softc *sc = uether_getsc(ue);
1246 struct ifnet *ifp = uether_getifp(ue);
1248 SMSC_LOCK_ASSERT(sc, MA_OWNED);
1250 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1251 sc->sc_flags &= ~SMSC_FLAG_LINK;
1254 * stop all the transfers, if not already stopped:
1256 usbd_transfer_stop(sc->sc_xfer[SMSC_BULK_DT_WR]);
1257 usbd_transfer_stop(sc->sc_xfer[SMSC_BULK_DT_RD]);
1261 * smsc_phy_init - Initialises the in-built SMSC phy
1262 * @sc: driver soft context
1264 * Resets the PHY part of the chip and then initialises it to default
1265 * values. The 'link down' and 'auto-negotiation complete' interrupts
1266 * from the PHY are also enabled, however we don't monitor the interrupt
1267 * endpoints for the moment.
1270 * Returns 0 on success or EIO if failed to reset the PHY.
1273 smsc_phy_init(struct smsc_softc *sc)
1276 usb_ticks_t start_ticks;
1277 const usb_ticks_t max_ticks = USB_MS_TO_TICKS(1000);
1279 SMSC_LOCK_ASSERT(sc, MA_OWNED);
1281 /* Reset phy and wait for reset to complete */
1282 smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR, BMCR_RESET);
1284 start_ticks = ticks;
1286 uether_pause(&sc->sc_ue, hz / 100);
1287 bmcr = smsc_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR);
1288 } while ((bmcr & MII_BMCR) && ((ticks - start_ticks) < max_ticks));
1290 if (((usb_ticks_t)(ticks - start_ticks)) >= max_ticks) {
1291 smsc_err_printf(sc, "PHY reset timed-out");
1295 smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_ANAR,
1296 ANAR_10 | ANAR_10_FD | ANAR_TX | ANAR_TX_FD | /* all modes */
1301 /* Setup the phy to interrupt when the link goes down or autoneg completes */
1302 smsc_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, SMSC_PHY_INTR_STAT);
1303 smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, SMSC_PHY_INTR_MASK,
1304 (SMSC_PHY_INTR_ANEG_COMP | SMSC_PHY_INTR_LINK_DOWN));
1306 /* Restart auto-negotation */
1307 bmcr = smsc_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR);
1308 bmcr |= BMCR_STARTNEG;
1309 smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR, bmcr);
1316 * smsc_chip_init - Initialises the chip after power on
1317 * @sc: driver soft context
1319 * This initialisation sequence is modelled on the procedure in the Linux
1323 * Returns 0 on success or an error code on failure.
1326 smsc_chip_init(struct smsc_softc *sc)
1333 locked = mtx_owned(&sc->sc_mtx);
1337 /* Enter H/W config mode */
1338 smsc_write_reg(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST);
1340 if ((err = smsc_wait_for_bits(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST)) != 0) {
1341 smsc_warn_printf(sc, "timed-out waiting for reset to complete\n");
1346 smsc_write_reg(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST);
1348 if ((err = smsc_wait_for_bits(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST) != 0)) {
1349 smsc_warn_printf(sc, "timed-out waiting for phy reset to complete\n");
1353 /* Set the mac address */
1354 if ((err = smsc_setmacaddress(sc, sc->sc_ue.ue_eaddr)) != 0) {
1355 smsc_warn_printf(sc, "failed to set the MAC address\n");
1359 /* Don't know what the HW_CFG_BIR bit is, but following the reset sequence
1360 * as used in the Linux driver.
1362 if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) != 0) {
1363 smsc_warn_printf(sc, "failed to read HW_CFG: %d\n", err);
1366 reg_val |= SMSC_HW_CFG_BIR;
1367 smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
1369 /* There is a so called 'turbo mode' that the linux driver supports, it
1370 * seems to allow you to jam multiple frames per Rx transaction. By default
1371 * this driver supports that and therefore allows multiple frames per URB.
1373 * The xfer buffer size needs to reflect this as well, therefore based on
1374 * the calculations in the Linux driver the RX bufsize is set to 18944,
1375 * bufsz = (16 * 1024 + 5 * 512)
1377 * Burst capability is the number of URBs that can be in a burst of data/
1380 if (usbd_get_speed(sc->sc_ue.ue_udev) == USB_SPEED_HIGH)
1385 smsc_write_reg(sc, SMSC_BURST_CAP, burst_cap);
1387 /* Set the default bulk in delay (magic value from Linux driver) */
1388 smsc_write_reg(sc, SMSC_BULK_IN_DLY, 0x00002000);
1393 * Initialise the RX interface
1395 if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) < 0) {
1396 smsc_warn_printf(sc, "failed to read HW_CFG: (err = %d)\n", err);
1400 /* Adjust the packet offset in the buffer (designed to try and align IP
1401 * header on 4 byte boundary)
1403 reg_val &= ~SMSC_HW_CFG_RXDOFF;
1404 reg_val |= (ETHER_ALIGN << 9) & SMSC_HW_CFG_RXDOFF;
1406 /* The following setings are used for 'turbo mode', a.k.a multiple frames
1407 * per Rx transaction (again info taken form Linux driver).
1409 reg_val |= (SMSC_HW_CFG_MEF | SMSC_HW_CFG_BCE);
1411 smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
1413 /* Clear the status register ? */
1414 smsc_write_reg(sc, SMSC_INTR_STATUS, 0xffffffff);
1416 /* Read and display the revision register */
1417 if ((err = smsc_read_reg(sc, SMSC_ID_REV, &sc->sc_rev_id)) < 0) {
1418 smsc_warn_printf(sc, "failed to read ID_REV (err = %d)\n", err);
1422 device_printf(sc->sc_ue.ue_dev, "chip 0x%04lx, rev. %04lx\n",
1423 (sc->sc_rev_id & SMSC_ID_REV_CHIP_ID_MASK) >> 16,
1424 (sc->sc_rev_id & SMSC_ID_REV_CHIP_REV_MASK));
1426 /* GPIO/LED setup */
1427 reg_val = SMSC_LED_GPIO_CFG_SPD_LED | SMSC_LED_GPIO_CFG_LNK_LED |
1428 SMSC_LED_GPIO_CFG_FDX_LED;
1429 smsc_write_reg(sc, SMSC_LED_GPIO_CFG, reg_val);
1432 * Initialise the TX interface
1434 smsc_write_reg(sc, SMSC_FLOW, 0);
1436 smsc_write_reg(sc, SMSC_AFC_CFG, AFC_CFG_DEFAULT);
1438 /* Read the current MAC configuration */
1439 if ((err = smsc_read_reg(sc, SMSC_MAC_CSR, &sc->sc_mac_csr)) < 0) {
1440 smsc_warn_printf(sc, "failed to read MAC_CSR (err=%d)\n", err);
1445 smsc_write_reg(sc, SMSC_VLAN1, (uint32_t)ETHERTYPE_VLAN);
1448 * Initialise the PHY
1450 if ((err = smsc_phy_init(sc)) != 0)
1457 sc->sc_mac_csr |= SMSC_MAC_CSR_TXEN;
1458 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
1459 smsc_write_reg(sc, SMSC_TX_CFG, SMSC_TX_CFG_ON);
1464 sc->sc_mac_csr |= SMSC_MAC_CSR_RXEN;
1465 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
1476 smsc_err_printf(sc, "smsc_chip_init failed (err=%d)\n", err);
1482 * smsc_ioctl - ioctl function for the device
1483 * @ifp: interface pointer
1484 * @cmd: the ioctl command
1485 * @data: data passed in the ioctl call, typically a pointer to struct ifreq.
1487 * The ioctl routine is overridden to detect change requests for the H/W
1488 * checksum capabilities.
1491 * 0 on success and an error code on failure.
1494 smsc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1496 struct usb_ether *ue = ifp->if_softc;
1497 struct smsc_softc *sc;
1503 if (cmd == SIOCSIFCAP) {
1505 sc = uether_getsc(ue);
1506 ifr = (struct ifreq *)data;
1513 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1515 /* Modify the RX CSUM enable bits */
1516 if ((mask & IFCAP_RXCSUM) != 0 &&
1517 (ifp->if_capabilities & IFCAP_RXCSUM) != 0) {
1518 ifp->if_capenable ^= IFCAP_RXCSUM;
1520 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1521 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1528 #if __FreeBSD_version > 1000000
1535 rc = uether_ioctl(ifp, cmd, data);
1543 * Get MAC address from FDT blob. Firmware or loader should fill
1544 * mac-address or local-mac-address property Returns 0 if MAC address
1545 * obtained, error code otherwise
1548 smsc_fdt_find_mac(unsigned char *mac)
1550 phandle_t child, parent, root;
1553 root = OF_finddevice("/");
1557 /* Traverse through entire tree to find nodes usb ethernet nodes */
1558 for (child = OF_child(parent); child != 0; child = OF_peer(child)) {
1560 /* Find a 'leaf'. Start the search from this node. */
1561 while (OF_child(child)) {
1563 child = OF_child(child);
1566 if (fdt_is_compatible(child, "net,ethernet") &&
1567 fdt_is_compatible(child, "usb,device")) {
1569 /* Check if there is property */
1570 if ((len = OF_getproplen(child, "local-mac-address")) > 0) {
1571 if (len != ETHER_ADDR_LEN)
1574 OF_getprop(child, "local-mac-address", mac,
1579 if ((len = OF_getproplen(child, "mac-address")) > 0) {
1580 if (len != ETHER_ADDR_LEN)
1583 OF_getprop(child, "mac-address", mac,
1589 if (OF_peer(child) == 0) {
1590 /* No more siblings. */
1592 parent = OF_parent(child);
1601 * smsc_attach_post - Called after the driver attached to the USB interface
1602 * @ue: the USB ethernet device
1604 * This is where the chip is intialised for the first time. This is different
1605 * from the smsc_init() function in that that one is designed to setup the
1606 * H/W to match the UE settings and can be called after a reset.
1611 smsc_attach_post(struct usb_ether *ue)
1613 struct smsc_softc *sc = uether_getsc(ue);
1614 uint32_t mac_h, mac_l;
1617 smsc_dbg_printf(sc, "smsc_attach_post\n");
1619 /* Setup some of the basics */
1623 /* Attempt to get the mac address, if an EEPROM is not attached this
1624 * will just return FF:FF:FF:FF:FF:FF, so in such cases we invent a MAC
1625 * address based on urandom.
1627 memset(sc->sc_ue.ue_eaddr, 0xff, ETHER_ADDR_LEN);
1629 /* Check if there is already a MAC address in the register */
1630 if ((smsc_read_reg(sc, SMSC_MAC_ADDRL, &mac_l) == 0) &&
1631 (smsc_read_reg(sc, SMSC_MAC_ADDRH, &mac_h) == 0)) {
1632 sc->sc_ue.ue_eaddr[5] = (uint8_t)((mac_h >> 8) & 0xff);
1633 sc->sc_ue.ue_eaddr[4] = (uint8_t)((mac_h) & 0xff);
1634 sc->sc_ue.ue_eaddr[3] = (uint8_t)((mac_l >> 24) & 0xff);
1635 sc->sc_ue.ue_eaddr[2] = (uint8_t)((mac_l >> 16) & 0xff);
1636 sc->sc_ue.ue_eaddr[1] = (uint8_t)((mac_l >> 8) & 0xff);
1637 sc->sc_ue.ue_eaddr[0] = (uint8_t)((mac_l) & 0xff);
1640 /* MAC address is not set so try to read from EEPROM, if that fails generate
1641 * a random MAC address.
1643 if (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr)) {
1645 err = smsc_eeprom_read(sc, 0x01, sc->sc_ue.ue_eaddr, ETHER_ADDR_LEN);
1647 if ((err != 0) || (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr)))
1648 err = smsc_fdt_find_mac(sc->sc_ue.ue_eaddr);
1650 if ((err != 0) || (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr))) {
1651 read_random(sc->sc_ue.ue_eaddr, ETHER_ADDR_LEN);
1652 sc->sc_ue.ue_eaddr[0] &= ~0x01; /* unicast */
1653 sc->sc_ue.ue_eaddr[0] |= 0x02; /* locally administered */
1657 /* Initialise the chip for the first time */
1663 * smsc_attach_post_sub - Called after the driver attached to the USB interface
1664 * @ue: the USB ethernet device
1666 * Most of this is boilerplate code and copied from the base USB ethernet
1667 * driver. It has been overriden so that we can indicate to the system that
1668 * the chip supports H/W checksumming.
1671 * Returns 0 on success or a negative error code.
1673 #if __FreeBSD_version > 1000000
1675 smsc_attach_post_sub(struct usb_ether *ue)
1677 struct smsc_softc *sc;
1681 sc = uether_getsc(ue);
1683 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1684 ifp->if_start = uether_start;
1685 ifp->if_ioctl = smsc_ioctl;
1686 ifp->if_init = uether_init;
1687 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
1688 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
1689 IFQ_SET_READY(&ifp->if_snd);
1691 /* The chip supports TCP/UDP checksum offloading on TX and RX paths, however
1692 * currently only RX checksum is supported in the driver (see top of file).
1694 ifp->if_capabilities |= IFCAP_RXCSUM;
1695 ifp->if_hwassist = 0;
1697 /* TX checksuming is disabled (for now?)
1698 ifp->if_capabilities |= IFCAP_TXCSUM;
1699 ifp->if_capenable |= IFCAP_TXCSUM;
1700 ifp->if_hwassist = CSUM_TCP | CSUM_UDP;
1703 ifp->if_capenable = ifp->if_capabilities;
1706 error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp,
1707 uether_ifmedia_upd, ue->ue_methods->ue_mii_sts,
1708 BMSR_DEFCAPMASK, sc->sc_phyno, MII_OFFSET_ANY, 0);
1713 #endif /* __FreeBSD_version > 1000000 */
1717 * smsc_probe - Probe the interface.
1718 * @dev: smsc device handle
1720 * Checks if the device is a match for this driver.
1723 * Returns 0 on success or an error code on failure.
1726 smsc_probe(device_t dev)
1728 struct usb_attach_arg *uaa = device_get_ivars(dev);
1730 if (uaa->usb_mode != USB_MODE_HOST)
1732 if (uaa->info.bConfigIndex != SMSC_CONFIG_INDEX)
1734 if (uaa->info.bIfaceIndex != SMSC_IFACE_IDX)
1737 return (usbd_lookup_id_by_uaa(smsc_devs, sizeof(smsc_devs), uaa));
1742 * smsc_attach - Attach the interface.
1743 * @dev: smsc device handle
1745 * Allocate softc structures, do ifmedia setup and ethernet/BPF attach.
1748 * Returns 0 on success or a negative error code.
1751 smsc_attach(device_t dev)
1753 struct usb_attach_arg *uaa = device_get_ivars(dev);
1754 struct smsc_softc *sc = device_get_softc(dev);
1755 struct usb_ether *ue = &sc->sc_ue;
1756 uint8_t iface_index;
1759 sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
1761 device_set_usb_desc(dev);
1763 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
1765 /* Setup the endpoints for the SMSC LAN95xx device(s) */
1766 iface_index = SMSC_IFACE_IDX;
1767 err = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
1768 smsc_config, SMSC_N_TRANSFER, sc, &sc->sc_mtx);
1770 device_printf(dev, "error: allocating USB transfers failed\n");
1776 ue->ue_udev = uaa->device;
1777 ue->ue_mtx = &sc->sc_mtx;
1778 ue->ue_methods = &smsc_ue_methods;
1780 err = uether_ifattach(ue);
1782 device_printf(dev, "error: could not attach interface\n");
1785 return (0); /* success */
1789 return (ENXIO); /* failure */
1793 * smsc_detach - Detach the interface.
1794 * @dev: smsc device handle
1800 smsc_detach(device_t dev)
1802 struct smsc_softc *sc = device_get_softc(dev);
1803 struct usb_ether *ue = &sc->sc_ue;
1805 usbd_transfer_unsetup(sc->sc_xfer, SMSC_N_TRANSFER);
1806 uether_ifdetach(ue);
1807 mtx_destroy(&sc->sc_mtx);
1812 static device_method_t smsc_methods[] = {
1813 /* Device interface */
1814 DEVMETHOD(device_probe, smsc_probe),
1815 DEVMETHOD(device_attach, smsc_attach),
1816 DEVMETHOD(device_detach, smsc_detach),
1819 DEVMETHOD(bus_print_child, bus_generic_print_child),
1820 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
1823 DEVMETHOD(miibus_readreg, smsc_miibus_readreg),
1824 DEVMETHOD(miibus_writereg, smsc_miibus_writereg),
1825 DEVMETHOD(miibus_statchg, smsc_miibus_statchg),
1830 static driver_t smsc_driver = {
1832 .methods = smsc_methods,
1833 .size = sizeof(struct smsc_softc),
1836 static devclass_t smsc_devclass;
1838 DRIVER_MODULE(smsc, uhub, smsc_driver, smsc_devclass, NULL, 0);
1839 DRIVER_MODULE(miibus, smsc, miibus_driver, miibus_devclass, 0, 0);
1840 MODULE_DEPEND(smsc, uether, 1, 1, 1);
1841 MODULE_DEPEND(smsc, usb, 1, 1, 1);
1842 MODULE_DEPEND(smsc, ether, 1, 1, 1);
1843 MODULE_DEPEND(smsc, miibus, 1, 1, 1);
1844 MODULE_VERSION(smsc, 1);