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