Home | History | Annotate | Line # | Download | only in usb
if_mos.c revision 1.9
      1 /*	$NetBSD: if_mos.c,v 1.9 2022/03/03 05:50:57 riastradh Exp $	*/
      2 /*	$OpenBSD: if_mos.c,v 1.40 2019/07/07 06:40:10 kevlo Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 2008 Johann Christian Rode <jcrode (at) gmx.net>
      6  *
      7  * Permission to use, copy, modify, and distribute this software for any
      8  * purpose with or without fee is hereby granted, provided that the above
      9  * copyright notice and this permission notice appear in all copies.
     10  *
     11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  */
     19 
     20 /*
     21  * Copyright (c) 2005, 2006, 2007 Jonathan Gray <jsg (at) openbsd.org>
     22  *
     23  * Permission to use, copy, modify, and distribute this software for any
     24  * purpose with or without fee is hereby granted, provided that the above
     25  * copyright notice and this permission notice appear in all copies.
     26  *
     27  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     28  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     29  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     30  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     31  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     32  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     33  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     34  */
     35 
     36 /*
     37  * Copyright (c) 1997, 1998, 1999, 2000-2003
     38  *	Bill Paul <wpaul (at) windriver.com>.  All rights reserved.
     39  *
     40  * Redistribution and use in source and binary forms, with or without
     41  * modification, are permitted provided that the following conditions
     42  * are met:
     43  * 1. Redistributions of source code must retain the above copyright
     44  *    notice, this list of conditions and the following disclaimer.
     45  * 2. Redistributions in binary form must reproduce the above copyright
     46  *    notice, this list of conditions and the following disclaimer in the
     47  *    documentation and/or other materials provided with the distribution.
     48  * 3. All advertising materials mentioning features or use of this software
     49  *    must display the following acknowledgement:
     50  *	This product includes software developed by Bill Paul.
     51  * 4. Neither the name of the author nor the names of any co-contributors
     52  *    may be used to endorse or promote products derived from this software
     53  *    without specific prior written permission.
     54  *
     55  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
     59  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     60  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     61  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     62  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     63  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     64  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     65  * THE POSSIBILITY OF SUCH DAMAGE.
     66  */
     67 
     68 /*
     69  * Moschip MCS7730/MCS7830/MCS7832 USB to Ethernet controller
     70  * The datasheet is available at the following URL:
     71  * http://www.moschip.com/data/products/MCS7830/Data%20Sheet_7830.pdf
     72  */
     73 
     74 #include <sys/cdefs.h>
     75 __KERNEL_RCSID(0, "$NetBSD: if_mos.c,v 1.9 2022/03/03 05:50:57 riastradh Exp $");
     76 
     77 #include <sys/param.h>
     78 
     79 #include <dev/usb/usbnet.h>
     80 #include <dev/usb/if_mosreg.h>
     81 
     82 #define MOS_PAUSE_REWRITES	3
     83 
     84 #define MOS_TIMEOUT		1000
     85 
     86 #define MOS_RX_LIST_CNT		1
     87 #define MOS_TX_LIST_CNT		1
     88 
     89 /* Maximum size of a fast ethernet frame plus one byte for the status */
     90 #define MOS_BUFSZ	 	(ETHER_MAX_LEN+1)
     91 
     92 /*
     93  * USB endpoints.
     94  */
     95 #define MOS_ENDPT_RX		0
     96 #define MOS_ENDPT_TX		1
     97 #define MOS_ENDPT_INTR		2
     98 #define MOS_ENDPT_MAX		3
     99 
    100 /*
    101  * USB vendor requests.
    102  */
    103 #define MOS_UR_READREG		0x0e
    104 #define MOS_UR_WRITEREG		0x0d
    105 
    106 #define MOS_CONFIG_NO		1
    107 #define MOS_IFACE_IDX		0
    108 
    109 struct mos_type {
    110 	struct usb_devno	mos_dev;
    111 	u_int16_t		mos_flags;
    112 #define MCS7730	0x0001		/* MCS7730 */
    113 #define MCS7830	0x0002		/* MCS7830 */
    114 #define MCS7832	0x0004		/* MCS7832 */
    115 };
    116 
    117 #define MOS_INC(x, y)           (x) = (x + 1) % y
    118 
    119 #ifdef MOS_DEBUG
    120 #define DPRINTF(x)      do { if (mosdebug) printf x; } while (0)
    121 #define DPRINTFN(n,x)   do { if (mosdebug >= (n)) printf x; } while (0)
    122 int     mosdebug = 0;
    123 #else
    124 #define DPRINTF(x)	__nothing
    125 #define DPRINTFN(n,x)	__nothing
    126 #endif
    127 
    128 /*
    129  * Various supported device vendors/products.
    130  */
    131 static const struct mos_type mos_devs[] = {
    132 	{ { USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7730 }, MCS7730 },
    133 	{ { USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7830 }, MCS7830 },
    134 	{ { USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7832 }, MCS7832 },
    135 	{ { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_LN030 }, MCS7830 },
    136 };
    137 #define mos_lookup(v, p) ((const struct mos_type *)usb_lookup(mos_devs, v, p))
    138 
    139 static int mos_match(device_t, cfdata_t, void *);
    140 static void mos_attach(device_t, device_t, void *);
    141 
    142 CFATTACH_DECL_NEW(mos, sizeof(struct usbnet),
    143 	mos_match, mos_attach, usbnet_detach, usbnet_activate);
    144 
    145 static void mos_uno_rx_loop(struct usbnet *, struct usbnet_chain *, uint32_t);
    146 static unsigned mos_uno_tx_prepare(struct usbnet *, struct mbuf *,
    147 				   struct usbnet_chain *);
    148 static int mos_uno_ioctl(struct ifnet *, u_long, void *);
    149 static int mos_uno_init(struct ifnet *);
    150 static void mos_chip_init(struct usbnet *);
    151 static void mos_uno_stop(struct ifnet *ifp, int disable);
    152 static int mos_uno_mii_read_reg(struct usbnet *, int, int, uint16_t *);
    153 static int mos_uno_mii_write_reg(struct usbnet *, int, int, uint16_t);
    154 static void mos_uno_mii_statchg(struct ifnet *);
    155 static void mos_reset(struct usbnet *);
    156 
    157 static int mos_reg_read_1(struct usbnet *, int);
    158 static int mos_reg_read_2(struct usbnet *, int);
    159 static int mos_reg_write_1(struct usbnet *, int, int);
    160 static int mos_reg_write_2(struct usbnet *, int, int);
    161 static int mos_readmac(struct usbnet *);
    162 static int mos_writemac(struct usbnet *);
    163 static int mos_write_mcast(struct usbnet *, uint8_t *);
    164 
    165 static const struct usbnet_ops mos_ops = {
    166 	.uno_stop = mos_uno_stop,
    167 	.uno_ioctl = mos_uno_ioctl,
    168 	.uno_read_reg = mos_uno_mii_read_reg,
    169 	.uno_write_reg = mos_uno_mii_write_reg,
    170 	.uno_statchg = mos_uno_mii_statchg,
    171 	.uno_tx_prepare = mos_uno_tx_prepare,
    172 	.uno_rx_loop = mos_uno_rx_loop,
    173 	.uno_init = mos_uno_init,
    174 };
    175 
    176 static int
    177 mos_reg_read_1(struct usbnet *un, int reg)
    178 {
    179 	usb_device_request_t	req;
    180 	usbd_status		err;
    181 	uByte			val = 0;
    182 
    183 	if (usbnet_isdying(un))
    184 		return 0;
    185 
    186 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    187 	req.bRequest = MOS_UR_READREG;
    188 	USETW(req.wValue, 0);
    189 	USETW(req.wIndex, reg);
    190 	USETW(req.wLength, 1);
    191 
    192 	err = usbd_do_request(un->un_udev, &req, &val);
    193 
    194 	if (err) {
    195 		aprint_error_dev(un->un_dev, "read reg %x\n", reg);
    196 		return 0;
    197 	}
    198 
    199 	return val;
    200 }
    201 
    202 static int
    203 mos_reg_read_2(struct usbnet *un, int reg)
    204 {
    205 	usb_device_request_t	req;
    206 	usbd_status		err;
    207 	uWord			val;
    208 
    209 	if (usbnet_isdying(un))
    210 		return 0;
    211 
    212 	USETW(val,0);
    213 
    214 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    215 	req.bRequest = MOS_UR_READREG;
    216 	USETW(req.wValue, 0);
    217 	USETW(req.wIndex, reg);
    218 	USETW(req.wLength, 2);
    219 
    220 	err = usbd_do_request(un->un_udev, &req, &val);
    221 
    222 	if (err) {
    223 		aprint_error_dev(un->un_dev, "read reg2 %x\n", reg);
    224 		return 0;
    225 	}
    226 
    227 	return UGETW(val);
    228 }
    229 
    230 static int
    231 mos_reg_write_1(struct usbnet *un, int reg, int aval)
    232 {
    233 	usb_device_request_t	req;
    234 	usbd_status		err;
    235 	uByte			val;
    236 
    237 	if (usbnet_isdying(un))
    238 		return 0;
    239 
    240 	val = aval;
    241 
    242 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    243 	req.bRequest = MOS_UR_WRITEREG;
    244 	USETW(req.wValue, 0);
    245 	USETW(req.wIndex, reg);
    246 	USETW(req.wLength, 1);
    247 
    248 	err = usbd_do_request(un->un_udev, &req, &val);
    249 
    250 	if (err)
    251 		aprint_error_dev(un->un_dev, "write reg %x <- %x\n",
    252 		    reg, aval);
    253 
    254 	return 0;
    255 }
    256 
    257 static int
    258 mos_reg_write_2(struct usbnet *un, int reg, int aval)
    259 {
    260 	usb_device_request_t	req;
    261 	usbd_status		err;
    262 	uWord			val;
    263 
    264 	USETW(val, aval);
    265 
    266 	if (usbnet_isdying(un))
    267 		return EIO;
    268 
    269 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    270 	req.bRequest = MOS_UR_WRITEREG;
    271 	USETW(req.wValue, 0);
    272 	USETW(req.wIndex, reg);
    273 	USETW(req.wLength, 2);
    274 
    275 	err = usbd_do_request(un->un_udev, &req, &val);
    276 
    277 	if (err)
    278 		aprint_error_dev(un->un_dev, "write reg2 %x <- %x\n",
    279 		    reg, aval);
    280 
    281 	return 0;
    282 }
    283 
    284 static int
    285 mos_readmac(struct usbnet *un)
    286 {
    287 	usb_device_request_t	req;
    288 	usbd_status		err;
    289 
    290 	if (usbnet_isdying(un))
    291 		return 0;
    292 
    293 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    294 	req.bRequest = MOS_UR_READREG;
    295 	USETW(req.wValue, 0);
    296 	USETW(req.wIndex, MOS_MAC);
    297 	USETW(req.wLength, ETHER_ADDR_LEN);
    298 
    299 	err = usbd_do_request(un->un_udev, &req, un->un_eaddr);
    300 
    301 	if (err)
    302 		aprint_error_dev(un->un_dev, "%s: failed", __func__);
    303 
    304 	return err;
    305 }
    306 
    307 static int
    308 mos_writemac(struct usbnet *un)
    309 {
    310 	usb_device_request_t	req;
    311 	usbd_status		err;
    312 
    313 	if (usbnet_isdying(un))
    314 		return 0;
    315 
    316 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    317 	req.bRequest = MOS_UR_WRITEREG;
    318 	USETW(req.wValue, 0);
    319 	USETW(req.wIndex, MOS_MAC);
    320 	USETW(req.wLength, ETHER_ADDR_LEN);
    321 
    322 	err = usbd_do_request(un->un_udev, &req, un->un_eaddr);
    323 
    324 	if (err)
    325 		aprint_error_dev(un->un_dev, "%s: failed", __func__);
    326 
    327 	return 0;
    328 }
    329 
    330 static int
    331 mos_write_mcast(struct usbnet *un, uint8_t *hashtbl)
    332 {
    333 	usb_device_request_t	req;
    334 	usbd_status		err;
    335 
    336 	if (usbnet_isdying(un))
    337 		return EIO;
    338 
    339 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    340 	req.bRequest = MOS_UR_WRITEREG;
    341 	USETW(req.wValue, 0);
    342 	USETW(req.wIndex, MOS_MCAST_TABLE);
    343 	USETW(req.wLength, 8);
    344 
    345 	err = usbd_do_request(un->un_udev, &req, hashtbl);
    346 
    347 	if (err) {
    348 		aprint_error_dev(un->un_dev, "%s: failed", __func__);
    349 		return(-1);
    350 	}
    351 
    352 	return 0;
    353 }
    354 
    355 static int
    356 mos_uno_mii_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val)
    357 {
    358 	int			i, res;
    359 
    360 	mos_reg_write_2(un, MOS_PHY_DATA, 0);
    361 	mos_reg_write_1(un, MOS_PHY_CTL, (phy & MOS_PHYCTL_PHYADDR) |
    362 	    MOS_PHYCTL_READ);
    363 	mos_reg_write_1(un, MOS_PHY_STS, (reg & MOS_PHYSTS_PHYREG) |
    364 	    MOS_PHYSTS_PENDING);
    365 
    366 	for (i = 0; i < MOS_TIMEOUT; i++) {
    367 		if (usbnet_isdying(un))
    368 			return ENXIO;
    369 		if (mos_reg_read_1(un, MOS_PHY_STS) & MOS_PHYSTS_READY)
    370 			break;
    371 	}
    372 	if (i == MOS_TIMEOUT) {
    373 		aprint_error_dev(un->un_dev, "read PHY failed\n");
    374 		return EIO;
    375 	}
    376 
    377 	res = mos_reg_read_2(un, MOS_PHY_DATA);
    378 	*val = res;
    379 
    380 	DPRINTFN(10,("%s: %s: phy %d reg %d val %u\n",
    381 	    device_xname(un->un_dev), __func__, phy, reg, res));
    382 
    383 	return 0;
    384 }
    385 
    386 static int
    387 mos_uno_mii_write_reg(struct usbnet *un, int phy, int reg, uint16_t val)
    388 {
    389 	int			i;
    390 
    391 	DPRINTFN(10,("%s: %s: phy %d reg %d val %u\n",
    392 	    device_xname(un->un_dev), __func__, phy, reg, val));
    393 
    394 	mos_reg_write_2(un, MOS_PHY_DATA, val);
    395 	mos_reg_write_1(un, MOS_PHY_CTL, (phy & MOS_PHYCTL_PHYADDR) |
    396 	    MOS_PHYCTL_WRITE);
    397 	mos_reg_write_1(un, MOS_PHY_STS, (reg & MOS_PHYSTS_PHYREG) |
    398 	    MOS_PHYSTS_PENDING);
    399 
    400 	for (i = 0; i < MOS_TIMEOUT; i++) {
    401 		if (usbnet_isdying(un))
    402 			return ENXIO;
    403 		if (mos_reg_read_1(un, MOS_PHY_STS) & MOS_PHYSTS_READY)
    404 			break;
    405 	}
    406 	if (i == MOS_TIMEOUT) {
    407 		aprint_error_dev(un->un_dev, "write PHY failed\n");
    408 		return EIO;
    409 	}
    410 
    411 	return 0;
    412 }
    413 
    414 void
    415 mos_uno_mii_statchg(struct ifnet *ifp)
    416 {
    417 	struct usbnet * const		un = ifp->if_softc;
    418 	struct mii_data * const		mii = usbnet_mii(un);
    419 	int				val, err;
    420 
    421 	if (usbnet_isdying(un))
    422 		return;
    423 
    424 	DPRINTFN(10,("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    425 
    426 	/* disable RX, TX prior to changing FDX, SPEEDSEL */
    427 	val = mos_reg_read_1(un, MOS_CTL);
    428 	val &= ~(MOS_CTL_TX_ENB | MOS_CTL_RX_ENB);
    429 	mos_reg_write_1(un, MOS_CTL, val);
    430 
    431 	/* reset register which counts dropped frames */
    432 	mos_reg_write_1(un, MOS_FRAME_DROP_CNT, 0);
    433 
    434 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
    435 		val |= MOS_CTL_FDX_ENB;
    436 	else
    437 		val &= ~(MOS_CTL_FDX_ENB);
    438 
    439 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
    440 	    (IFM_ACTIVE | IFM_AVALID)) {
    441 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
    442 		case IFM_100_TX:
    443 			val |=  MOS_CTL_SPEEDSEL;
    444 			break;
    445 		case IFM_10_T:
    446 			val &= ~(MOS_CTL_SPEEDSEL);
    447 			break;
    448 		}
    449 		usbnet_set_link(un, true);
    450 	}
    451 
    452 	/* re-enable TX, RX */
    453 	val |= (MOS_CTL_TX_ENB | MOS_CTL_RX_ENB);
    454 	err = mos_reg_write_1(un, MOS_CTL, val);
    455 
    456 	if (err)
    457 		aprint_error_dev(un->un_dev, "media change failed\n");
    458 }
    459 
    460 static void
    461 mos_rcvfilt_locked(struct usbnet *un)
    462 {
    463 	struct ifnet		*ifp = usbnet_ifp(un);
    464 	struct ethercom		*ec = usbnet_ec(un);
    465 	struct ether_multi	*enm;
    466 	struct ether_multistep	step;
    467 	u_int32_t h = 0;
    468 	u_int8_t rxmode, mchash[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
    469 
    470 	if (usbnet_isdying(un))
    471 		return;
    472 
    473 	rxmode = mos_reg_read_1(un, MOS_CTL);
    474 	rxmode &= ~(MOS_CTL_ALLMULTI | MOS_CTL_RX_PROMISC);
    475 
    476 	ETHER_LOCK(ec);
    477 	if (ifp->if_flags & IFF_PROMISC) {
    478 		ec->ec_flags |= ETHER_F_ALLMULTI;
    479 		ETHER_UNLOCK(ec);
    480 		/* run promisc. mode */
    481 		rxmode |= MOS_CTL_ALLMULTI; /* ??? */
    482 		rxmode |= MOS_CTL_RX_PROMISC;
    483 		goto update;
    484 	}
    485 	ec->ec_flags &= ~ETHER_F_ALLMULTI;
    486 	ETHER_FIRST_MULTI(step, ec, enm);
    487 	while (enm != NULL) {
    488 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
    489 			ec->ec_flags |= ETHER_F_ALLMULTI;
    490 			ETHER_UNLOCK(ec);
    491 			memset(mchash, 0, sizeof(mchash)); /* correct ??? */
    492 			/* accept all multicast frame */
    493 			rxmode |= MOS_CTL_ALLMULTI;
    494 			goto update;
    495 		}
    496 		h = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
    497 		/* 3(31:29) and 3(28:26) sampling to have uint8_t[8] */
    498 		mchash[h >> 29] |= 1 << ((h >> 26) % 8);
    499 		ETHER_NEXT_MULTI(step, enm);
    500 	}
    501 	ETHER_UNLOCK(ec);
    502 	/* MOS receive filter is always on */
    503  update:
    504 	/*
    505 	 * The datasheet claims broadcast frames were always accepted
    506 	 * regardless of filter settings. But the hardware seems to
    507 	 * filter broadcast frames, so pass them explicitly.
    508 	 */
    509 	mchash[7] |= 0x80;
    510 	mos_write_mcast(un, mchash);
    511 	mos_reg_write_1(un, MOS_CTL, rxmode);
    512 }
    513 
    514 static void
    515 mos_reset(struct usbnet *un)
    516 {
    517 	u_int8_t ctl;
    518 
    519 	if (usbnet_isdying(un))
    520 		return;
    521 
    522 	ctl = mos_reg_read_1(un, MOS_CTL);
    523 	ctl &= ~(MOS_CTL_RX_PROMISC | MOS_CTL_ALLMULTI | MOS_CTL_TX_ENB |
    524 	    MOS_CTL_RX_ENB);
    525 	/* Disable RX, TX, promiscuous and allmulticast mode */
    526 	mos_reg_write_1(un, MOS_CTL, ctl);
    527 
    528 	/* Reset frame drop counter register to zero */
    529 	mos_reg_write_1(un, MOS_FRAME_DROP_CNT, 0);
    530 
    531 	/* Wait a little while for the chip to get its brains in order. */
    532 	DELAY(1000);
    533 }
    534 
    535 void
    536 mos_chip_init(struct usbnet *un)
    537 {
    538 	int	i;
    539 
    540 	/*
    541 	 * Rev.C devices have a pause threshold register which needs to be set
    542 	 * at startup.
    543 	 */
    544 	if (mos_reg_read_1(un, MOS_PAUSE_TRHD) != -1) {
    545 		for (i = 0; i < MOS_PAUSE_REWRITES; i++)
    546 			mos_reg_write_1(un, MOS_PAUSE_TRHD, 0);
    547 	}
    548 }
    549 
    550 /*
    551  * Probe for a MCS7x30 chip.
    552  */
    553 static int
    554 mos_match(device_t parent, cfdata_t match, void *aux)
    555 {
    556 	struct usb_attach_arg *uaa = aux;
    557 
    558 	return (mos_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
    559 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    560 }
    561 
    562 /*
    563  * Attach the interface.
    564  */
    565 static void
    566 mos_attach(device_t parent, device_t self, void *aux)
    567 {
    568 	USBNET_MII_DECL_DEFAULT(unm);
    569 	struct usbnet *		un = device_private(self);
    570 	struct usb_attach_arg	*uaa = aux;
    571 	struct usbd_device	*dev = uaa->uaa_device;
    572 	usbd_status		err;
    573 	usb_interface_descriptor_t 	*id;
    574 	usb_endpoint_descriptor_t 	*ed;
    575 	char			*devinfop;
    576 	int			i;
    577 
    578 	aprint_naive("\n");
    579 	aprint_normal("\n");
    580 	devinfop = usbd_devinfo_alloc(dev, 0);
    581 	aprint_normal_dev(self, "%s\n", devinfop);
    582 	usbd_devinfo_free(devinfop);
    583 
    584 	un->un_dev = self;
    585 	un->un_udev = dev;
    586 	un->un_sc = un;
    587 	un->un_ops = &mos_ops;
    588 	un->un_rx_xfer_flags = USBD_SHORT_XFER_OK;
    589 	un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER;
    590 	un->un_rx_list_cnt = MOS_RX_LIST_CNT;
    591 	un->un_tx_list_cnt = MOS_TX_LIST_CNT;
    592 	un->un_rx_bufsz = un->un_tx_bufsz = MOS_BUFSZ;
    593 
    594 	err = usbd_set_config_no(dev, MOS_CONFIG_NO, 1);
    595 	if (err) {
    596 		aprint_error_dev(self, "failed to set configuration"
    597 		    ", err=%s\n", usbd_errstr(err));
    598 		return;
    599 	}
    600 
    601 	err = usbd_device2interface_handle(dev, MOS_IFACE_IDX, &un->un_iface);
    602 	if (err) {
    603 		aprint_error_dev(self, "failed getting interface handle"
    604 		    ", err=%s\n", usbd_errstr(err));
    605 		return;
    606 	}
    607 
    608 	un->un_flags = mos_lookup(uaa->uaa_vendor, uaa->uaa_product)->mos_flags;
    609 
    610 	id = usbd_get_interface_descriptor(un->un_iface);
    611 
    612 	/* Find endpoints. */
    613 	for (i = 0; i < id->bNumEndpoints; i++) {
    614 		ed = usbd_interface2endpoint_descriptor(un->un_iface, i);
    615 		if (!ed) {
    616 			aprint_error_dev(self, "couldn't get ep %d\n", i);
    617 			return;
    618 		}
    619 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    620 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    621 			un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress;
    622 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    623 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    624 			un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress;
    625 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    626 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    627 			un->un_ed[USBNET_ENDPT_INTR] = ed->bEndpointAddress;
    628 		}
    629 	}
    630 
    631 	if (un->un_flags & MCS7730)
    632 		aprint_normal_dev(self, "MCS7730\n");
    633 	else if (un->un_flags & MCS7830)
    634 		aprint_normal_dev(self, "MCS7830\n");
    635 	else if (un->un_flags & MCS7832)
    636 		aprint_normal_dev(self, "MCS7832\n");
    637 
    638 	/* Set these up now for register access. */
    639 	usbnet_attach(un, "mosdet");
    640 
    641 	mos_chip_init(un);
    642 
    643 	/*
    644 	 * Read MAC address, inform the world.
    645 	 */
    646 	err = mos_readmac(un);
    647 	if (err) {
    648 		aprint_error_dev(self, "couldn't read MAC address\n");
    649 		return;
    650 	}
    651 
    652 	struct ifnet *ifp = usbnet_ifp(un);
    653 	ifp->if_capabilities = ETHERCAP_VLAN_MTU;
    654 
    655 	usbnet_attach_ifp(un, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST,
    656 	    0, &unm);
    657 }
    658 
    659 /*
    660  * A frame has been uploaded: pass the resulting mbuf chain up to
    661  * the higher level protocols.
    662  */
    663 void
    664 mos_uno_rx_loop(struct usbnet * un, struct usbnet_chain *c, uint32_t total_len)
    665 {
    666 	struct ifnet		*ifp = usbnet_ifp(un);
    667 	uint8_t			*buf = c->unc_buf;
    668 	u_int8_t		rxstat;
    669 	u_int16_t		pktlen = 0;
    670 
    671 	DPRINTFN(5,("%s: %s: enter len %u\n",
    672 	    device_xname(un->un_dev), __func__, total_len));
    673 
    674 	if (total_len <= 1)
    675 		return;
    676 
    677 	/* evaluate status byte at the end */
    678 	pktlen = total_len - 1;
    679 	if (pktlen > un->un_rx_bufsz) {
    680 		if_statinc(ifp, if_ierrors);
    681 		return;
    682 	}
    683 	rxstat = buf[pktlen] & MOS_RXSTS_MASK;
    684 
    685 	if (rxstat != MOS_RXSTS_VALID) {
    686 		DPRINTF(("%s: erroneous frame received: ",
    687 		    device_xname(un->un_dev)));
    688 		if (rxstat & MOS_RXSTS_SHORT_FRAME)
    689 			DPRINTF(("frame size less than 64 bytes\n"));
    690 		if (rxstat & MOS_RXSTS_LARGE_FRAME)
    691 			DPRINTF(("frame size larger than 1532 bytes\n"));
    692 		if (rxstat & MOS_RXSTS_CRC_ERROR)
    693 			DPRINTF(("CRC error\n"));
    694 		if (rxstat & MOS_RXSTS_ALIGN_ERROR)
    695 			DPRINTF(("alignment error\n"));
    696 		if_statinc(ifp, if_ierrors);
    697 		return;
    698 	}
    699 
    700 	if (pktlen < sizeof(struct ether_header) ) {
    701 		if_statinc(ifp, if_ierrors);
    702 		return;
    703 	}
    704 
    705 	usbnet_enqueue(un, c->unc_buf, pktlen, 0, 0, 0);
    706 }
    707 
    708 static unsigned
    709 mos_uno_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c)
    710 {
    711 	int			length;
    712 
    713 	if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz)
    714 		return 0;
    715 
    716 	m_copydata(m, 0, m->m_pkthdr.len, c->unc_buf);
    717 	length = m->m_pkthdr.len;
    718 
    719 	DPRINTFN(5,("%s: %s: len %u\n",
    720 	    device_xname(un->un_dev), __func__, length));
    721 
    722 	return length;
    723 }
    724 
    725 static int
    726 mos_init_locked(struct ifnet *ifp)
    727 {
    728 	struct usbnet * const un = ifp->if_softc;
    729 	u_int8_t		rxmode;
    730 	unsigned char		ipgs[2];
    731 
    732 	if (usbnet_isdying(un))
    733 		return EIO;
    734 
    735 	/* Cancel pending I/O */
    736 	usbnet_stop(un, ifp, 1);
    737 
    738 	/* Reset the ethernet interface. */
    739 	mos_reset(un);
    740 
    741 	/* Write MAC address. */
    742 	mos_writemac(un);
    743 
    744 	/* Read and set transmitter IPG values */
    745 	ipgs[0] = mos_reg_read_1(un, MOS_IPG0);
    746 	ipgs[1] = mos_reg_read_1(un, MOS_IPG1);
    747 	mos_reg_write_1(un, MOS_IPG0, ipgs[0]);
    748 	mos_reg_write_1(un, MOS_IPG1, ipgs[1]);
    749 
    750 	/* Accept multicast frame or run promisc. mode */
    751 	mos_rcvfilt_locked(un);
    752 
    753 	/* Enable receiver and transmitter, bridge controls speed/duplex mode */
    754 	rxmode = mos_reg_read_1(un, MOS_CTL);
    755 	rxmode |= MOS_CTL_RX_ENB | MOS_CTL_TX_ENB | MOS_CTL_BS_ENB;
    756 	rxmode &= ~(MOS_CTL_SLEEP);
    757 	mos_reg_write_1(un, MOS_CTL, rxmode);
    758 
    759 	return usbnet_init_rx_tx(un);
    760 }
    761 
    762 static int
    763 mos_uno_init(struct ifnet *ifp)
    764 {
    765 	struct usbnet * const un = ifp->if_softc;
    766 
    767 	usbnet_busy(un);
    768 	int ret = mos_init_locked(ifp);
    769 	usbnet_unbusy(un);
    770 
    771 	return ret;
    772 }
    773 
    774 static int
    775 mos_uno_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    776 {
    777 	struct usbnet * const un = ifp->if_softc;
    778 
    779 	usbnet_lock_core(un);
    780 	usbnet_busy(un);
    781 
    782 	switch (cmd) {
    783 	case SIOCADDMULTI:
    784 	case SIOCDELMULTI:
    785 		mos_rcvfilt_locked(un);
    786 		break;
    787 	default:
    788 		break;
    789 	}
    790 
    791 	usbnet_unbusy(un);
    792 	usbnet_unlock_core(un);
    793 
    794 	return 0;
    795 }
    796 
    797 void
    798 mos_uno_stop(struct ifnet *ifp, int disable)
    799 {
    800 	struct usbnet * const un = ifp->if_softc;
    801 
    802 	mos_reset(un);
    803 }
    804