Home | History | Annotate | Line # | Download | only in usb
if_smsc.c revision 1.56
      1 /*	$NetBSD: if_smsc.c,v 1.56 2019/08/11 23:55:43 mrg Exp $	*/
      2 
      3 /*	$OpenBSD: if_smsc.c,v 1.4 2012/09/27 12:38:11 jsg Exp $	*/
      4 /*	$FreeBSD: src/sys/dev/usb/net/if_smsc.c,v 1.1 2012/08/15 04:03:55 gonzo Exp $ */
      5 /*-
      6  * Copyright (c) 2012
      7  *	Ben Gray <bgray (at) freebsd.org>.
      8  * All rights reserved.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 /*
     32  * SMSC LAN9xxx devices (http://www.smsc.com/)
     33  *
     34  * The LAN9500 & LAN9500A devices are stand-alone USB to Ethernet chips that
     35  * support USB 2.0 and 10/100 Mbps Ethernet.
     36  *
     37  * The LAN951x devices are an integrated USB hub and USB to Ethernet adapter.
     38  * The driver only covers the Ethernet part, the standard USB hub driver
     39  * supports the hub part.
     40  *
     41  * This driver is closely modelled on the Linux driver written and copyrighted
     42  * by SMSC.
     43  *
     44  * H/W TCP & UDP Checksum Offloading
     45  * ---------------------------------
     46  * The chip supports both tx and rx offloading of UDP & TCP checksums, this
     47  * feature can be dynamically enabled/disabled.
     48  *
     49  * RX checksuming is performed across bytes after the IPv4 header to the end of
     50  * the Ethernet frame, this means if the frame is padded with non-zero values
     51  * the H/W checksum will be incorrect, however the rx code compensates for this.
     52  *
     53  * TX checksuming is more complicated, the device requires a special header to
     54  * be prefixed onto the start of the frame which indicates the start and end
     55  * positions of the UDP or TCP frame.  This requires the driver to manually
     56  * go through the packet data and decode the headers prior to sending.
     57  * On Linux they generally provide cues to the location of the csum and the
     58  * area to calculate it over, on FreeBSD we seem to have to do it all ourselves,
     59  * hence this is not as optimal and therefore h/w TX checksum is currently not
     60  * implemented.
     61  */
     62 
     63 #include <sys/cdefs.h>
     64 __KERNEL_RCSID(0, "$NetBSD: if_smsc.c,v 1.56 2019/08/11 23:55:43 mrg Exp $");
     65 
     66 #ifdef _KERNEL_OPT
     67 #include "opt_usb.h"
     68 #endif
     69 
     70 #include <sys/param.h>
     71 #include <sys/module.h>
     72 
     73 #include <dev/usb/usbnet.h>
     74 #include <dev/usb/usbhist.h>
     75 
     76 #include <dev/usb/if_smscreg.h>
     77 
     78 #include "ioconf.h"
     79 
     80 struct smsc_softc {
     81 	struct usbnet		smsc_un;
     82 
     83 	/*
     84 	 * The following stores the settings in the mac control (MAC_CSR)
     85 	 * register
     86 	 */
     87 	uint32_t		sc_mac_csr;
     88 	uint32_t		sc_rev_id;
     89 
     90 	uint32_t		sc_coe_ctrl;
     91 };
     92 
     93 #define SMSC_MIN_BUFSZ		2048
     94 #define SMSC_MAX_BUFSZ		18944
     95 
     96 /*
     97  * Various supported device vendors/products.
     98  */
     99 static const struct usb_devno smsc_devs[] = {
    100 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_LAN89530 },
    101 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_LAN9530 },
    102 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_LAN9730 },
    103 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500 },
    104 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500A },
    105 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500A_ALT },
    106 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500A_HAL },
    107 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500A_SAL10 },
    108 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500_ALT },
    109 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500_SAL10 },
    110 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9505 },
    111 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9505A },
    112 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9505A_HAL },
    113 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9505A_SAL10 },
    114 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9505_SAL10 },
    115 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9512_14 },
    116 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9512_14_ALT },
    117 	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9512_14_SAL10 }
    118 };
    119 
    120 #ifdef USB_DEBUG
    121 #ifndef USMSC_DEBUG
    122 #define usmscdebug 0
    123 #else
    124 static int usmscdebug = 1;
    125 
    126 SYSCTL_SETUP(sysctl_hw_smsc_setup, "sysctl hw.usmsc setup")
    127 {
    128 	int err;
    129 	const struct sysctlnode *rnode;
    130 	const struct sysctlnode *cnode;
    131 
    132 	err = sysctl_createv(clog, 0, NULL, &rnode,
    133 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "usmsc",
    134 	    SYSCTL_DESCR("usmsc global controls"),
    135 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
    136 
    137 	if (err)
    138 		goto fail;
    139 
    140 	/* control debugging printfs */
    141 	err = sysctl_createv(clog, 0, &rnode, &cnode,
    142 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
    143 	    "debug", SYSCTL_DESCR("Enable debugging output"),
    144 	    NULL, 0, &usmscdebug, sizeof(usmscdebug), CTL_CREATE, CTL_EOL);
    145 	if (err)
    146 		goto fail;
    147 
    148 	return;
    149 fail:
    150 	aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
    151 }
    152 
    153 #endif /* SMSC_DEBUG */
    154 #endif /* USB_DEBUG */
    155 
    156 #define DPRINTF(FMT,A,B,C,D)	USBHIST_LOG(usmscdebug,FMT,A,B,C,D)
    157 #define DPRINTFN(N,FMT,A,B,C,D)	USBHIST_LOGN(usmscdebug,N,FMT,A,B,C,D)
    158 #define USMSCHIST_FUNC()	USBHIST_FUNC()
    159 #define USMSCHIST_CALLED()	USBHIST_CALLED(usmscdebug)
    160 
    161 #define smsc_warn_printf(un, fmt, args...) \
    162 	printf("%s: warning: " fmt, device_xname((un)->un_dev), ##args)
    163 
    164 #define smsc_err_printf(un, fmt, args...) \
    165 	printf("%s: error: " fmt, device_xname((un)->un_dev), ##args)
    166 
    167 /* Function declarations */
    168 int		 smsc_match(device_t, cfdata_t, void *);
    169 void		 smsc_attach(device_t, device_t, void *);
    170 
    171 CFATTACH_DECL_NEW(usmsc, sizeof(struct smsc_softc),
    172     smsc_match, smsc_attach, usbnet_detach, usbnet_activate);
    173 
    174 int		 smsc_chip_init(struct usbnet *);
    175 int		 smsc_setmacaddress(struct usbnet *, const uint8_t *);
    176 
    177 int		 smsc_init(struct ifnet *);
    178 int		 smsc_init_locked(struct ifnet *);
    179 int		 smsc_ioctl(struct ifnet *, u_long, void *);
    180 void		 smsc_stop_cb(struct ifnet *, int);
    181 
    182 void		 smsc_reset(struct smsc_softc *);
    183 
    184 static void	 smsc_miibus_statchg(struct ifnet *);
    185 int		 smsc_readreg(struct usbnet *, uint32_t, uint32_t *);
    186 int		 smsc_writereg(struct usbnet *, uint32_t, uint32_t);
    187 int		 smsc_wait_for_bits(struct usbnet *, uint32_t, uint32_t);
    188 usbd_status	 smsc_miibus_readreg(struct usbnet *, int, int, uint16_t *);
    189 usbd_status	 smsc_miibus_writereg(struct usbnet *, int, int, uint16_t);
    190 
    191 static int	 smsc_ioctl_cb(struct ifnet *, u_long, void *);
    192 static unsigned	 smsc_tx_prepare(struct usbnet *, struct mbuf *,
    193 		     struct usbnet_chain *);
    194 static void	 smsc_rxeof_loop(struct usbnet *, struct usbd_xfer *,
    195 		    struct usbnet_chain *, uint32_t);
    196 
    197 static struct usbnet_ops smsc_ops = {
    198 	.uno_stop = smsc_stop_cb,
    199 	.uno_ioctl = smsc_ioctl_cb,
    200 	.uno_read_reg = smsc_miibus_readreg,
    201 	.uno_write_reg = smsc_miibus_writereg,
    202 	.uno_statchg = smsc_miibus_statchg,
    203 	.uno_tx_prepare = smsc_tx_prepare,
    204 	.uno_rx_loop = smsc_rxeof_loop,
    205 	.uno_init = smsc_init,
    206 };
    207 
    208 int
    209 smsc_readreg(struct usbnet *un, uint32_t off, uint32_t *data)
    210 {
    211 	usb_device_request_t req;
    212 	uint32_t buf;
    213 	usbd_status err;
    214 
    215 	usbnet_isowned_mii(un);
    216 
    217 	if (usbnet_isdying(un))
    218 		return 0;
    219 
    220 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    221 	req.bRequest = SMSC_UR_READ_REG;
    222 	USETW(req.wValue, 0);
    223 	USETW(req.wIndex, off);
    224 	USETW(req.wLength, 4);
    225 
    226 	err = usbd_do_request(un->un_udev, &req, &buf);
    227 	if (err != 0)
    228 		smsc_warn_printf(un, "Failed to read register 0x%0x\n", off);
    229 
    230 	*data = le32toh(buf);
    231 
    232 	return err;
    233 }
    234 
    235 int
    236 smsc_writereg(struct usbnet *un, uint32_t off, uint32_t data)
    237 {
    238 	usb_device_request_t req;
    239 	uint32_t buf;
    240 	usbd_status err;
    241 
    242 	usbnet_isowned_mii(un);
    243 
    244 	if (usbnet_isdying(un))
    245 		return 0;
    246 
    247 	buf = htole32(data);
    248 
    249 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    250 	req.bRequest = SMSC_UR_WRITE_REG;
    251 	USETW(req.wValue, 0);
    252 	USETW(req.wIndex, off);
    253 	USETW(req.wLength, 4);
    254 
    255 	err = usbd_do_request(un->un_udev, &req, &buf);
    256 	if (err != 0)
    257 		smsc_warn_printf(un, "Failed to write register 0x%0x\n", off);
    258 
    259 	return err;
    260 }
    261 
    262 int
    263 smsc_wait_for_bits(struct usbnet *un, uint32_t reg, uint32_t bits)
    264 {
    265 	uint32_t val;
    266 	int err, i;
    267 
    268 	for (i = 0; i < 100; i++) {
    269 		if ((err = smsc_readreg(un, reg, &val)) != 0)
    270 			return err;
    271 		if (!(val & bits))
    272 			return 0;
    273 		DELAY(5);
    274 	}
    275 
    276 	return 1;
    277 }
    278 
    279 usbd_status
    280 smsc_miibus_readreg(struct usbnet *un, int phy, int reg, uint16_t *val)
    281 {
    282 	uint32_t addr;
    283 	uint32_t data = 0;
    284 	int rv = 0;
    285 
    286 	usbnet_isowned_mii(un);
    287 
    288 	if (smsc_wait_for_bits(un, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
    289 		smsc_warn_printf(un, "MII is busy\n");
    290 		rv = -1;
    291 		goto done;
    292 	}
    293 
    294 	addr = (phy << 11) | (reg << 6) | SMSC_MII_READ;
    295 	smsc_writereg(un, SMSC_MII_ADDR, addr);
    296 
    297 	if (smsc_wait_for_bits(un, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
    298 		smsc_warn_printf(un, "MII read timeout\n");
    299 		rv = ETIMEDOUT;
    300 	}
    301 
    302 	smsc_readreg(un, SMSC_MII_DATA, &data);
    303 
    304 done:
    305 	*val = data & 0xffff;
    306 	return rv;
    307 }
    308 
    309 usbd_status
    310 smsc_miibus_writereg(struct usbnet *un, int phy, int reg, uint16_t val)
    311 {
    312 	uint32_t addr;
    313 
    314 	usbnet_isowned_mii(un);
    315 
    316 	if (un->un_phyno != phy)
    317 		return -1;
    318 
    319 	if (smsc_wait_for_bits(un, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
    320 		smsc_warn_printf(un, "MII is busy\n");
    321 		return -1;
    322 	}
    323 
    324 	smsc_writereg(un, SMSC_MII_DATA, val);
    325 
    326 	addr = (phy << 11) | (reg << 6) | SMSC_MII_WRITE;
    327 	smsc_writereg(un, SMSC_MII_ADDR, addr);
    328 
    329 	if (smsc_wait_for_bits(un, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
    330 		smsc_warn_printf(un, "MII write timeout\n");
    331 		return ETIMEDOUT;
    332 	}
    333 
    334 	return 0;
    335 }
    336 
    337 void
    338 smsc_miibus_statchg(struct ifnet *ifp)
    339 {
    340 	USMSCHIST_FUNC(); USMSCHIST_CALLED();
    341 	struct usbnet * const un = ifp->if_softc;
    342 
    343 	if (usbnet_isdying(un))
    344 		return;
    345 
    346 	struct smsc_softc * const sc = usbnet_softc(un);
    347 	struct mii_data * const mii = usbnet_mii(un);
    348 	uint32_t flow;
    349 	uint32_t afc_cfg;
    350 
    351 	usbnet_set_link(un, false);
    352 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
    353 	    (IFM_ACTIVE | IFM_AVALID)) {
    354 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
    355 			case IFM_10_T:
    356 			case IFM_100_TX:
    357 				usbnet_set_link(un, true);
    358 				break;
    359 			case IFM_1000_T:
    360 				/* Gigabit ethernet not supported by chipset */
    361 				break;
    362 			default:
    363 				break;
    364 		}
    365 	}
    366 
    367 	/* Lost link, do nothing. */
    368 	if (!usbnet_havelink(un))
    369 		return;
    370 
    371 	usbnet_lock_mii(un);
    372 	int err = smsc_readreg(un, SMSC_AFC_CFG, &afc_cfg);
    373 	usbnet_unlock_mii(un);
    374 	if (err) {
    375 		smsc_warn_printf(un, "failed to read initial AFC_CFG, "
    376 		    "error %d\n", err);
    377 		return;
    378 	}
    379 
    380 	/* Enable/disable full duplex operation and TX/RX pause */
    381 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
    382 		DPRINTF("full duplex operation", 0, 0, 0, 0);
    383 		sc->sc_mac_csr &= ~SMSC_MAC_CSR_RCVOWN;
    384 		sc->sc_mac_csr |= SMSC_MAC_CSR_FDPX;
    385 
    386 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
    387 			flow = 0xffff0002;
    388 		else
    389 			flow = 0;
    390 
    391 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
    392 			afc_cfg |= 0xf;
    393 		else
    394 			afc_cfg &= ~0xf;
    395 	} else {
    396 		DPRINTF("half duplex operation", 0, 0, 0, 0);
    397 		sc->sc_mac_csr &= ~SMSC_MAC_CSR_FDPX;
    398 		sc->sc_mac_csr |= SMSC_MAC_CSR_RCVOWN;
    399 
    400 		flow = 0;
    401 		afc_cfg |= 0xf;
    402 	}
    403 
    404 	usbnet_lock_mii(un);
    405 	err = smsc_writereg(un, SMSC_MAC_CSR, sc->sc_mac_csr);
    406 	err += smsc_writereg(un, SMSC_FLOW, flow);
    407 	err += smsc_writereg(un, SMSC_AFC_CFG, afc_cfg);
    408 	usbnet_unlock_mii(un);
    409 
    410 	if (err)
    411 		smsc_warn_printf(un, "media change failed, error %d\n", err);
    412 }
    413 
    414 static inline uint32_t
    415 smsc_hash(uint8_t addr[ETHER_ADDR_LEN])
    416 {
    417 
    418 	return (ether_crc32_be(addr, ETHER_ADDR_LEN) >> 26) & 0x3f;
    419 }
    420 
    421 static void
    422 smsc_setiff_locked(struct usbnet *un)
    423 {
    424 	USMSCHIST_FUNC(); USMSCHIST_CALLED();
    425 	struct smsc_softc * const sc = usbnet_softc(un);
    426 	struct ifnet * const ifp = usbnet_ifp(un);
    427 	struct ethercom *ec = usbnet_ec(un);
    428 	struct ether_multi *enm;
    429 	struct ether_multistep step;
    430 	uint32_t hashtbl[2] = { 0, 0 };
    431 	uint32_t hash;
    432 
    433 	usbnet_isowned_mii(un);
    434 
    435 	if (usbnet_isdying(un))
    436 		return;
    437 
    438 	if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
    439 allmulti:
    440 		DPRINTF("receive all multicast enabled", 0, 0, 0, 0);
    441 		sc->sc_mac_csr |= SMSC_MAC_CSR_MCPAS;
    442 		sc->sc_mac_csr &= ~SMSC_MAC_CSR_HPFILT;
    443 		smsc_writereg(un, SMSC_MAC_CSR, sc->sc_mac_csr);
    444 		return;
    445 	} else {
    446 		sc->sc_mac_csr |= SMSC_MAC_CSR_HPFILT;
    447 		sc->sc_mac_csr &= ~(SMSC_MAC_CSR_PRMS | SMSC_MAC_CSR_MCPAS);
    448 	}
    449 
    450 	ETHER_LOCK(ec);
    451 	ETHER_FIRST_MULTI(step, ec, enm);
    452 	while (enm != NULL) {
    453 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
    454 			ETHER_UNLOCK(ec);
    455 			goto allmulti;
    456 		}
    457 
    458 		hash = smsc_hash(enm->enm_addrlo);
    459 		hashtbl[hash >> 5] |= 1 << (hash & 0x1F);
    460 		ETHER_NEXT_MULTI(step, enm);
    461 	}
    462 	ETHER_UNLOCK(ec);
    463 
    464 	/* Debug */
    465 	if (sc->sc_mac_csr & SMSC_MAC_CSR_HPFILT) {
    466 		DPRINTF("receive select group of macs", 0, 0, 0, 0);
    467 	} else {
    468 		DPRINTF("receive own packets only", 0, 0, 0, 0);
    469 	}
    470 
    471 	/* Write the hash table and mac control registers */
    472 
    473 	//XXX should we be doing this?
    474 	ifp->if_flags &= ~IFF_ALLMULTI;
    475 	smsc_writereg(un, SMSC_HASHH, hashtbl[1]);
    476 	smsc_writereg(un, SMSC_HASHL, hashtbl[0]);
    477 	smsc_writereg(un, SMSC_MAC_CSR, sc->sc_mac_csr);
    478 }
    479 
    480 static void
    481 smsc_setiff(struct usbnet *un)
    482 {
    483 	usbnet_lock_mii(un);
    484 	smsc_setiff_locked(un);
    485 	usbnet_unlock_mii(un);
    486 }
    487 
    488 static int
    489 smsc_setoe_locked(struct usbnet *un)
    490 {
    491 	struct smsc_softc * const sc = usbnet_softc(un);
    492 	struct ifnet * const ifp = usbnet_ifp(un);
    493 	uint32_t val;
    494 	int err;
    495 
    496 	usbnet_isowned_mii(un);
    497 
    498 	err = smsc_readreg(un, SMSC_COE_CTRL, &val);
    499 	if (err != 0) {
    500 		smsc_warn_printf(un, "failed to read SMSC_COE_CTRL (err=%d)\n",
    501 		    err);
    502 		return err;
    503 	}
    504 
    505 	/* Enable/disable the Rx checksum */
    506 	if (ifp->if_capenable & (IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx))
    507 		val |= (SMSC_COE_CTRL_RX_EN | SMSC_COE_CTRL_RX_MODE);
    508 	else
    509 		val &= ~(SMSC_COE_CTRL_RX_EN | SMSC_COE_CTRL_RX_MODE);
    510 
    511 	/* Enable/disable the Tx checksum (currently not supported) */
    512 	if (ifp->if_capenable & (IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_UDPv4_Tx))
    513 		val |= SMSC_COE_CTRL_TX_EN;
    514 	else
    515 		val &= ~SMSC_COE_CTRL_TX_EN;
    516 
    517 	sc->sc_coe_ctrl = val;
    518 
    519 	err = smsc_writereg(un, SMSC_COE_CTRL, val);
    520 	if (err != 0) {
    521 		smsc_warn_printf(un, "failed to write SMSC_COE_CTRL (err=%d)\n",
    522 		    err);
    523 		return err;
    524 	}
    525 
    526 	return 0;
    527 }
    528 
    529 static void
    530 smsc_setoe(struct usbnet *un)
    531 {
    532 
    533 	usbnet_lock_mii(un);
    534 	smsc_setoe_locked(un);
    535 	usbnet_unlock_mii(un);
    536 }
    537 
    538 
    539 int
    540 smsc_setmacaddress(struct usbnet *un, const uint8_t *addr)
    541 {
    542 	USMSCHIST_FUNC(); USMSCHIST_CALLED();
    543 	int err;
    544 	uint32_t val;
    545 
    546 	DPRINTF("setting mac address to %02jx:%02jx:%02jx:...", addr[0], addr[1],
    547 	    addr[2], 0);
    548 
    549 	DPRINTF("... %02jx:%0j2x:%02jx", addr[3], addr[4], addr[5], 0);
    550 
    551 	val = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
    552 	if ((err = smsc_writereg(un, SMSC_MAC_ADDRL, val)) != 0)
    553 		goto done;
    554 
    555 	val = (addr[5] << 8) | addr[4];
    556 	err = smsc_writereg(un, SMSC_MAC_ADDRH, val);
    557 
    558 done:
    559 	return err;
    560 }
    561 
    562 void
    563 smsc_reset(struct smsc_softc *sc)
    564 {
    565 	struct usbnet * const un = &sc->smsc_un;
    566 
    567 	usbnet_isowned(un);
    568 	if (usbnet_isdying(un))
    569 		return;
    570 
    571 	/* Wait a little while for the chip to get its brains in order. */
    572 	DELAY(1000);
    573 
    574 	/* Reinitialize controller to achieve full reset. */
    575 	smsc_chip_init(un);
    576 }
    577 
    578 int
    579 smsc_init(struct ifnet *ifp)
    580 {
    581 	struct usbnet * const un = ifp->if_softc;
    582 
    583 	usbnet_lock(un);
    584 	int ret = smsc_init_locked(ifp);
    585 	usbnet_unlock(un);
    586 
    587 	return ret;
    588 }
    589 
    590 int
    591 smsc_init_locked(struct ifnet *ifp)
    592 {
    593 	struct usbnet * const un = ifp->if_softc;
    594 	struct smsc_softc * const sc = usbnet_softc(un);
    595 
    596 	if (usbnet_isdying(un))
    597 		return EIO;
    598 
    599 	/* Cancel pending I/O */
    600 	usbnet_stop(un, ifp, 1);
    601 
    602 	/* Reset the ethernet interface. */
    603 	smsc_reset(sc);
    604 
    605 	usbnet_lock_mii_un_locked(un);
    606 
    607 	/* Load the multicast filter. */
    608 	smsc_setiff_locked(un);
    609 
    610 	/* TCP/UDP checksum offload engines. */
    611 	smsc_setoe_locked(un);
    612 
    613 	usbnet_unlock_mii_un_locked(un);
    614 
    615 	return usbnet_init_rx_tx(un);
    616 }
    617 
    618 void
    619 smsc_stop_cb(struct ifnet *ifp, int disable)
    620 {
    621 	struct usbnet * const un = ifp->if_softc;
    622 	struct smsc_softc * const sc = usbnet_softc(un);
    623 
    624 	// XXXNH didn't do this before
    625 	smsc_reset(sc);
    626 }
    627 
    628 int
    629 smsc_chip_init(struct usbnet *un)
    630 {
    631 	struct smsc_softc * const sc = usbnet_softc(un);
    632 	uint32_t reg_val;
    633 	int burst_cap;
    634 	int err;
    635 
    636 	usbnet_lock_mii_un_locked(un);
    637 
    638 	/* Enter H/W config mode */
    639 	smsc_writereg(un, SMSC_HW_CFG, SMSC_HW_CFG_LRST);
    640 
    641 	if ((err = smsc_wait_for_bits(un, SMSC_HW_CFG,
    642 	    SMSC_HW_CFG_LRST)) != 0) {
    643 		smsc_warn_printf(un, "timed-out waiting for reset to "
    644 		    "complete\n");
    645 		goto init_failed;
    646 	}
    647 
    648 	/* Reset the PHY */
    649 	smsc_writereg(un, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST);
    650 
    651 	if ((err = smsc_wait_for_bits(un, SMSC_PM_CTRL,
    652 	    SMSC_PM_CTRL_PHY_RST)) != 0) {
    653 		smsc_warn_printf(un, "timed-out waiting for phy reset to "
    654 		    "complete\n");
    655 		goto init_failed;
    656 	}
    657 	usbd_delay_ms(un->un_udev, 40);
    658 
    659 	/* Set the mac address */
    660 	struct ifnet * const ifp = usbnet_ifp(un);
    661 	const char *eaddr = CLLADDR(ifp->if_sadl);
    662 	if ((err = smsc_setmacaddress(un, eaddr)) != 0) {
    663 		smsc_warn_printf(un, "failed to set the MAC address\n");
    664 		goto init_failed;
    665 	}
    666 
    667 	/*
    668 	 * Don't know what the HW_CFG_BIR bit is, but following the reset
    669 	 * sequence as used in the Linux driver.
    670 	 */
    671 	if ((err = smsc_readreg(un, SMSC_HW_CFG, &reg_val)) != 0) {
    672 		smsc_warn_printf(un, "failed to read HW_CFG: %d\n", err);
    673 		goto init_failed;
    674 	}
    675 	reg_val |= SMSC_HW_CFG_BIR;
    676 	smsc_writereg(un, SMSC_HW_CFG, reg_val);
    677 
    678 	/*
    679 	 * There is a so called 'turbo mode' that the linux driver supports, it
    680 	 * seems to allow you to jam multiple frames per Rx transaction.
    681 	 * By default this driver supports that and therefore allows multiple
    682 	 * frames per USB transfer.
    683 	 *
    684 	 * The xfer buffer size needs to reflect this as well, therefore based
    685 	 * on the calculations in the Linux driver the RX bufsize is set to
    686 	 * 18944,
    687 	 *     bufsz = (16 * 1024 + 5 * 512)
    688 	 *
    689 	 * Burst capability is the number of URBs that can be in a burst of
    690 	 * data/ethernet frames.
    691 	 */
    692 
    693 	if (un->un_udev->ud_speed == USB_SPEED_HIGH)
    694 		burst_cap = 37;
    695 	else
    696 		burst_cap = 128;
    697 
    698 	smsc_writereg(un, SMSC_BURST_CAP, burst_cap);
    699 
    700 	/* Set the default bulk in delay (magic value from Linux driver) */
    701 	smsc_writereg(un, SMSC_BULK_IN_DLY, 0x00002000);
    702 
    703 	/*
    704 	 * Initialise the RX interface
    705 	 */
    706 	if ((err = smsc_readreg(un, SMSC_HW_CFG, &reg_val)) < 0) {
    707 		smsc_warn_printf(un, "failed to read HW_CFG: (err = %d)\n",
    708 		    err);
    709 		goto init_failed;
    710 	}
    711 
    712 	/*
    713 	 * The following settings are used for 'turbo mode', a.k.a multiple
    714 	 * frames per Rx transaction (again info taken form Linux driver).
    715 	 */
    716 	reg_val |= (SMSC_HW_CFG_MEF | SMSC_HW_CFG_BCE);
    717 
    718 	/*
    719 	 * set Rx data offset to ETHER_ALIGN which will make the IP header
    720 	 * align on a word boundary.
    721 	 */
    722 	reg_val |= ETHER_ALIGN << SMSC_HW_CFG_RXDOFF_SHIFT;
    723 
    724 	smsc_writereg(un, SMSC_HW_CFG, reg_val);
    725 
    726 	/* Clear the status register ? */
    727 	smsc_writereg(un, SMSC_INTR_STATUS, 0xffffffff);
    728 
    729 	/* Read and display the revision register */
    730 	if ((err = smsc_readreg(un, SMSC_ID_REV, &sc->sc_rev_id)) < 0) {
    731 		smsc_warn_printf(un, "failed to read ID_REV (err = %d)\n", err);
    732 		goto init_failed;
    733 	}
    734 
    735 	/* GPIO/LED setup */
    736 	reg_val = SMSC_LED_GPIO_CFG_SPD_LED | SMSC_LED_GPIO_CFG_LNK_LED |
    737 	    SMSC_LED_GPIO_CFG_FDX_LED;
    738 	smsc_writereg(un, SMSC_LED_GPIO_CFG, reg_val);
    739 
    740 	/*
    741 	 * Initialise the TX interface
    742 	 */
    743 	smsc_writereg(un, SMSC_FLOW, 0);
    744 
    745 	smsc_writereg(un, SMSC_AFC_CFG, AFC_CFG_DEFAULT);
    746 
    747 	/* Read the current MAC configuration */
    748 	if ((err = smsc_readreg(un, SMSC_MAC_CSR, &sc->sc_mac_csr)) < 0) {
    749 		smsc_warn_printf(un, "failed to read MAC_CSR (err=%d)\n", err);
    750 		goto init_failed;
    751 	}
    752 
    753 	/* disable pad stripping, collides with checksum offload */
    754 	sc->sc_mac_csr &= ~SMSC_MAC_CSR_PADSTR;
    755 
    756 	/* Vlan */
    757 	smsc_writereg(un, SMSC_VLAN1, (uint32_t)ETHERTYPE_VLAN);
    758 
    759 	/*
    760 	 * Start TX
    761 	 */
    762 	sc->sc_mac_csr |= SMSC_MAC_CSR_TXEN;
    763 	smsc_writereg(un, SMSC_MAC_CSR, sc->sc_mac_csr);
    764 	smsc_writereg(un, SMSC_TX_CFG, SMSC_TX_CFG_ON);
    765 
    766 	/*
    767 	 * Start RX
    768 	 */
    769 	sc->sc_mac_csr |= SMSC_MAC_CSR_RXEN;
    770 	smsc_writereg(un, SMSC_MAC_CSR, sc->sc_mac_csr);
    771 	usbnet_unlock_mii_un_locked(un);
    772 
    773 	return 0;
    774 
    775 init_failed:
    776 	usbnet_unlock_mii_un_locked(un);
    777 	smsc_err_printf(un, "smsc_chip_init failed (err=%d)\n", err);
    778 	return err;
    779 }
    780 
    781 static int
    782 smsc_ioctl_cb(struct ifnet *ifp, u_long cmd, void *data)
    783 {
    784 	struct usbnet * const un = ifp->if_softc;
    785 
    786 	switch (cmd) {
    787 	case SIOCSIFFLAGS:
    788 	case SIOCSETHERCAP:
    789 	case SIOCADDMULTI:
    790 	case SIOCDELMULTI:
    791 		smsc_setiff(un);
    792 		break;
    793 	case SIOCSIFCAP:
    794 		smsc_setoe(un);
    795 		break;
    796 	default:
    797 		break;
    798 	}
    799 
    800 	return 0;
    801 }
    802 
    803 int
    804 smsc_match(device_t parent, cfdata_t match, void *aux)
    805 {
    806 	struct usb_attach_arg *uaa = aux;
    807 
    808 	return (usb_lookup(smsc_devs, uaa->uaa_vendor, uaa->uaa_product) != NULL) ?
    809 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    810 }
    811 
    812 void
    813 smsc_attach(device_t parent, device_t self, void *aux)
    814 {
    815 	struct smsc_softc * const sc = device_private(self);
    816 	struct usbnet * const un = &sc->smsc_un;
    817 	struct usb_attach_arg *uaa = aux;
    818 	struct usbd_device *dev = uaa->uaa_device;
    819 	usb_interface_descriptor_t *id;
    820 	usb_endpoint_descriptor_t *ed;
    821 	char *devinfop;
    822 	unsigned bufsz;
    823 	int err, i;
    824 	uint32_t mac_h, mac_l;
    825 
    826 	KASSERT((void *)sc == un);
    827 
    828 	aprint_naive("\n");
    829 	aprint_normal("\n");
    830 
    831 	un->un_dev = self;
    832 	un->un_udev = dev;
    833 	un->un_sc = sc;
    834 	un->un_ops = &smsc_ops;
    835 	un->un_rx_xfer_flags = USBD_SHORT_XFER_OK;
    836 	un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER;
    837 	un->un_rx_list_cnt = SMSC_RX_LIST_CNT;
    838 	un->un_tx_list_cnt = SMSC_TX_LIST_CNT;
    839 
    840 	devinfop = usbd_devinfo_alloc(un->un_udev, 0);
    841 	aprint_normal_dev(self, "%s\n", devinfop);
    842 	usbd_devinfo_free(devinfop);
    843 
    844 	err = usbd_set_config_no(dev, SMSC_CONFIG_INDEX, 1);
    845 	if (err) {
    846 		aprint_error_dev(self, "failed to set configuration"
    847 		    ", err=%s\n", usbd_errstr(err));
    848 		return;
    849 	}
    850 
    851 	/* Setup the endpoints for the SMSC LAN95xx device(s) */
    852 	err = usbd_device2interface_handle(dev, SMSC_IFACE_IDX, &un->un_iface);
    853 	if (err) {
    854 		aprint_error_dev(self, "getting interface handle failed\n");
    855 		return;
    856 	}
    857 
    858 	id = usbd_get_interface_descriptor(un->un_iface);
    859 
    860 	if (dev->ud_speed >= USB_SPEED_HIGH) {
    861 		bufsz = SMSC_MAX_BUFSZ;
    862 	} else {
    863 		bufsz = SMSC_MIN_BUFSZ;
    864 	}
    865 	un->un_rx_bufsz = bufsz;
    866 	un->un_tx_bufsz = bufsz;
    867 
    868 	/* Find endpoints. */
    869 	for (i = 0; i < id->bNumEndpoints; i++) {
    870 		ed = usbd_interface2endpoint_descriptor(un->un_iface, i);
    871 		if (!ed) {
    872 			aprint_error_dev(self, "couldn't get ep %d\n", i);
    873 			return;
    874 		}
    875 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    876 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    877 			un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress;
    878 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    879 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    880 			un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress;
    881 #if 0 /* not used yet */
    882 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    883 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    884 			un->un_ed[USBNET_ENDPT_INTR] = ed->bEndpointAddress;
    885 #endif
    886 		}
    887 	}
    888 
    889 	usbnet_attach(un, "smscdet");
    890 
    891 #ifdef notyet
    892 	/*
    893 	 * We can do TCPv4, and UDPv4 checksums in hardware.
    894 	 */
    895 	struct ifnet *ifp = usbnet_ifp(un);
    896 
    897 	ifp->if_capabilities |=
    898 	    /*IFCAP_CSUM_TCPv4_Tx |*/ IFCAP_CSUM_TCPv4_Rx |
    899 	    /*IFCAP_CSUM_UDPv4_Tx |*/ IFCAP_CSUM_UDPv4_Rx;
    900 #endif
    901 	struct ethercom *ec = usbnet_ec(un);
    902 	ec->ec_capabilities = ETHERCAP_VLAN_MTU;
    903 
    904 	/* Setup some of the basics */
    905 	un->un_phyno = 1;
    906 
    907 	usbnet_lock_mii(un);
    908 	/*
    909 	 * Attempt to get the mac address, if an EEPROM is not attached this
    910 	 * will just return FF:FF:FF:FF:FF:FF, so in such cases we invent a MAC
    911 	 * address based on urandom.
    912 	 */
    913 	memset(un->un_eaddr, 0xff, ETHER_ADDR_LEN);
    914 
    915 	prop_dictionary_t dict = device_properties(self);
    916 	prop_data_t eaprop = prop_dictionary_get(dict, "mac-address");
    917 
    918 	if (eaprop != NULL) {
    919 		KASSERT(prop_object_type(eaprop) == PROP_TYPE_DATA);
    920 		KASSERT(prop_data_size(eaprop) == ETHER_ADDR_LEN);
    921 		memcpy(un->un_eaddr, prop_data_data_nocopy(eaprop),
    922 		    ETHER_ADDR_LEN);
    923 	} else {
    924 		/* Check if there is already a MAC address in the register */
    925 		if ((smsc_readreg(un, SMSC_MAC_ADDRL, &mac_l) == 0) &&
    926 		    (smsc_readreg(un, SMSC_MAC_ADDRH, &mac_h) == 0)) {
    927 			un->un_eaddr[5] = (uint8_t)((mac_h >> 8) & 0xff);
    928 			un->un_eaddr[4] = (uint8_t)((mac_h) & 0xff);
    929 			un->un_eaddr[3] = (uint8_t)((mac_l >> 24) & 0xff);
    930 			un->un_eaddr[2] = (uint8_t)((mac_l >> 16) & 0xff);
    931 			un->un_eaddr[1] = (uint8_t)((mac_l >> 8) & 0xff);
    932 			un->un_eaddr[0] = (uint8_t)((mac_l) & 0xff);
    933 		}
    934 	}
    935 	usbnet_unlock_mii(un);
    936 
    937 	usbnet_attach_ifp(un, true, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST,
    938 	    0, 0);
    939 }
    940 
    941 void
    942 smsc_rxeof_loop(struct usbnet * un, struct usbd_xfer *xfer,
    943     struct usbnet_chain *c, uint32_t total_len)
    944 {
    945 	USMSCHIST_FUNC(); USMSCHIST_CALLED();
    946 	struct smsc_softc * const sc = usbnet_softc(un);
    947 	struct ifnet *ifp = usbnet_ifp(un);
    948 	uint8_t *buf = c->unc_buf;
    949 
    950 	usbnet_isowned_rx(un);
    951 
    952 	DPRINTF("total_len %jd/0x%jx", total_len, total_len, 0, 0);
    953 	while (total_len != 0) {
    954 		uint32_t rxhdr;
    955 		if (total_len < sizeof(rxhdr)) {
    956 			DPRINTF("total_len %jd < sizeof(rxhdr) %jd",
    957 			    total_len, sizeof(rxhdr), 0, 0);
    958 			ifp->if_ierrors++;
    959 			return;
    960 		}
    961 
    962 		memcpy(&rxhdr, buf, sizeof(rxhdr));
    963 		rxhdr = le32toh(rxhdr);
    964 		buf += sizeof(rxhdr);
    965 		total_len -= sizeof(rxhdr);
    966 
    967 		if (rxhdr & SMSC_RX_STAT_COLLISION)
    968 			ifp->if_collisions++;
    969 
    970 		if (rxhdr & (SMSC_RX_STAT_ERROR
    971 			   | SMSC_RX_STAT_LENGTH_ERROR
    972 			   | SMSC_RX_STAT_MII_ERROR)) {
    973 			DPRINTF("rx error (hdr 0x%08jx)", rxhdr, 0, 0, 0);
    974 			ifp->if_ierrors++;
    975 			return;
    976 		}
    977 
    978 		uint16_t pktlen = (uint16_t)SMSC_RX_STAT_FRM_LENGTH(rxhdr);
    979 		DPRINTF("total_len %jd pktlen %jd rxhdr 0x%08jx", total_len,
    980 		    pktlen, rxhdr, 0);
    981 
    982 		if (pktlen < ETHER_HDR_LEN) {
    983 			DPRINTF("pktlen %jd < ETHER_HDR_LEN %jd", pktlen,
    984 			    ETHER_HDR_LEN, 0, 0);
    985 			ifp->if_ierrors++;
    986 			return;
    987 		}
    988 
    989 		pktlen += ETHER_ALIGN;
    990 
    991 		if (pktlen > MCLBYTES) {
    992 			DPRINTF("pktlen %jd > MCLBYTES %jd", pktlen, MCLBYTES, 0,
    993 			    0);
    994 			ifp->if_ierrors++;
    995 			return;
    996 		}
    997 
    998 		if (pktlen > total_len) {
    999 			DPRINTF("pktlen %jd > total_len %jd", pktlen, total_len,
   1000 			    0, 0);
   1001 			ifp->if_ierrors++;
   1002 			return;
   1003 		}
   1004 
   1005 		uint8_t *pktbuf = buf + ETHER_ALIGN;
   1006 		size_t buflen = pktlen - ETHER_ALIGN;
   1007 		int mbuf_flags = M_HASFCS;
   1008 		int csum_flags = 0;
   1009 		uint16_t csum_data = 0;
   1010 
   1011  		KASSERT(pktlen < MCLBYTES);
   1012 
   1013 		/* Check if RX TCP/UDP checksumming is being offloaded */
   1014 		if (sc->sc_coe_ctrl & SMSC_COE_CTRL_RX_EN) {
   1015 			DPRINTF("RX checksum offload checking", 0, 0, 0, 0);
   1016 			struct ether_header *eh = (struct ether_header *)pktbuf;
   1017 			const size_t cssz = sizeof(csum_data);
   1018 
   1019 			/* Remove the extra 2 bytes of the csum */
   1020 			buflen -= cssz;
   1021 
   1022 			/*
   1023 			 * The checksum appears to be simplistically calculated
   1024 			 * over the udp/tcp header and data up to the end of the
   1025 			 * eth frame.  Which means if the eth frame is padded
   1026 			 * the csum calculation is incorrectly performed over
   1027 			 * the padding bytes as well. Therefore to be safe we
   1028 			 * ignore the H/W csum on frames less than or equal to
   1029 			 * 64 bytes.
   1030 			 *
   1031 			 * Ignore H/W csum for non-IPv4 packets.
   1032 			 */
   1033 			DPRINTF("Ethertype %02jx pktlen %02jx",
   1034 			    be16toh(eh->ether_type), pktlen, 0, 0);
   1035 			if (be16toh(eh->ether_type) == ETHERTYPE_IP &&
   1036 			    pktlen > ETHER_MIN_LEN) {
   1037 
   1038 				csum_flags |=
   1039 				    (M_CSUM_TCPv4 | M_CSUM_UDPv4 | M_CSUM_DATA);
   1040 
   1041 				/*
   1042 				 * Copy the TCP/UDP checksum from the last 2
   1043 				 * bytes of the transfer and put in the
   1044 				 * csum_data field.
   1045 				 */
   1046 				memcpy(&csum_data, buf + pktlen - cssz, cssz);
   1047 
   1048 				/*
   1049 				 * The data is copied in network order, but the
   1050 				 * csum algorithm in the kernel expects it to be
   1051 				 * in host network order.
   1052 				 */
   1053 				csum_data = ntohs(csum_data);
   1054 				DPRINTF("RX checksum offloaded (0x%04jx)",
   1055 				    csum_data, 0, 0, 0);
   1056 			}
   1057 		}
   1058 
   1059 		/* round up to next longword */
   1060 		pktlen = (pktlen + 3) & ~0x3;
   1061 
   1062 		/* total_len does not include the padding */
   1063 		if (pktlen > total_len)
   1064 			pktlen = total_len;
   1065 
   1066 		buf += pktlen;
   1067 		total_len -= pktlen;
   1068 
   1069 		/* push the packet up */
   1070 		usbnet_enqueue(un, pktbuf, buflen, csum_flags, csum_data,
   1071 		    mbuf_flags);
   1072 	}
   1073 }
   1074 
   1075 static unsigned
   1076 smsc_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c)
   1077 {
   1078 	uint32_t txhdr;
   1079 	uint32_t frm_len = 0;
   1080 
   1081 	usbnet_isowned_tx(un);
   1082 
   1083 	const size_t hdrsz = sizeof(txhdr) * 2;
   1084 
   1085 	if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz - hdrsz)
   1086 		return 0;
   1087 
   1088 	/*
   1089 	 * Each frame is prefixed with two 32-bit values describing the
   1090 	 * length of the packet and buffer.
   1091 	 */
   1092 	txhdr = SMSC_TX_CTRL_0_BUF_SIZE(m->m_pkthdr.len) |
   1093 	    SMSC_TX_CTRL_0_FIRST_SEG | SMSC_TX_CTRL_0_LAST_SEG;
   1094 	txhdr = htole32(txhdr);
   1095 	memcpy(c->unc_buf, &txhdr, sizeof(txhdr));
   1096 
   1097 	txhdr = SMSC_TX_CTRL_1_PKT_LENGTH(m->m_pkthdr.len);
   1098 	txhdr = htole32(txhdr);
   1099 	memcpy(c->unc_buf + sizeof(txhdr), &txhdr, sizeof(txhdr));
   1100 
   1101 	frm_len += hdrsz;
   1102 
   1103 	/* Next copy in the actual packet */
   1104 	m_copydata(m, 0, m->m_pkthdr.len, c->unc_buf + frm_len);
   1105 	frm_len += m->m_pkthdr.len;
   1106 
   1107 	return frm_len;
   1108 }
   1109 
   1110 MODULE(MODULE_CLASS_DRIVER, if_smsc, "usbnet");
   1111 
   1112 #ifdef _MODULE
   1113 #include "ioconf.c"
   1114 #endif
   1115 
   1116 static int
   1117 if_smsc_modcmd(modcmd_t cmd, void *aux)
   1118 {
   1119 	int error = 0;
   1120 
   1121 	switch (cmd) {
   1122 	case MODULE_CMD_INIT:
   1123 #ifdef _MODULE
   1124 		error = config_init_component(cfdriver_ioconf_smsc,
   1125 		    cfattach_ioconf_smsc, cfdata_ioconf_smsc);
   1126 #endif
   1127 		return error;
   1128 	case MODULE_CMD_FINI:
   1129 #ifdef _MODULE
   1130 		error = config_fini_component(cfdriver_ioconf_smsc,
   1131 		    cfattach_ioconf_smsc, cfdata_ioconf_smsc);
   1132 #endif
   1133 		return error;
   1134 	default:
   1135 		return ENOTTY;
   1136 	}
   1137 }
   1138