Home | History | Annotate | Line # | Download | only in usb
if_url.c revision 1.76
      1 /*	$NetBSD: if_url.c,v 1.76 2020/03/15 23:04:51 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001, 2002
      5  *     Shingo WATANABE <nabe (at) nabechan.org>.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the author nor the names of any co-contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  */
     32 
     33 /*
     34  * The RTL8150L(Realtek USB to fast ethernet controller) spec can be found at
     35  *   ftp://ftp.realtek.com.tw/lancard/data_sheet/8150/8150v14.pdf
     36  *   ftp://152.104.125.40/lancard/data_sheet/8150/8150v14.pdf
     37  */
     38 
     39 /*
     40  * TODO:
     41  *	Interrupt Endpoint support
     42  *	External PHYs
     43  *	powerhook() support?
     44  */
     45 
     46 #include <sys/cdefs.h>
     47 __KERNEL_RCSID(0, "$NetBSD: if_url.c,v 1.76 2020/03/15 23:04:51 thorpej Exp $");
     48 
     49 #ifdef _KERNEL_OPT
     50 #include "opt_inet.h"
     51 #include "opt_usb.h"
     52 #endif
     53 
     54 #include <sys/param.h>
     55 
     56 #include <net/if_ether.h>
     57 #ifdef INET
     58 #include <netinet/in.h>
     59 #include <netinet/if_inarp.h>
     60 #endif
     61 
     62 #include <dev/mii/urlphyreg.h>
     63 
     64 #include <dev/usb/usbnet.h>
     65 
     66 #include <dev/usb/if_urlreg.h>
     67 
     68 /* Function declarations */
     69 static int	url_match(device_t, cfdata_t, void *);
     70 static void	url_attach(device_t, device_t, void *);
     71 
     72 CFATTACH_DECL_NEW(url, sizeof(struct usbnet), url_match, url_attach,
     73     usbnet_detach, usbnet_activate);
     74 
     75 static unsigned	url_uno_tx_prepare(struct usbnet *, struct mbuf *,
     76 				   struct usbnet_chain *);
     77 static void url_uno_rx_loop(struct usbnet *, struct usbnet_chain *, uint32_t);
     78 static int url_uno_mii_read_reg(struct usbnet *, int, int, uint16_t *);
     79 static int url_uno_mii_write_reg(struct usbnet *, int, int, uint16_t);
     80 static int url_uno_ioctl(struct ifnet *, u_long, void *);
     81 static void url_uno_stop(struct ifnet *, int);
     82 static void url_uno_mii_statchg(struct ifnet *);
     83 static int url_uno_init(struct ifnet *);
     84 static void url_setiff_locked(struct usbnet *);
     85 static void url_reset(struct usbnet *);
     86 
     87 static int url_csr_read_1(struct usbnet *, int);
     88 static int url_csr_read_2(struct usbnet *, int);
     89 static int url_csr_write_1(struct usbnet *, int, int);
     90 static int url_csr_write_2(struct usbnet *, int, int);
     91 static int url_csr_write_4(struct usbnet *, int, int);
     92 static int url_mem(struct usbnet *, int, int, void *, int);
     93 
     94 static const struct usbnet_ops url_ops = {
     95 	.uno_stop = url_uno_stop,
     96 	.uno_ioctl = url_uno_ioctl,
     97 	.uno_read_reg = url_uno_mii_read_reg,
     98 	.uno_write_reg = url_uno_mii_write_reg,
     99 	.uno_statchg = url_uno_mii_statchg,
    100 	.uno_tx_prepare = url_uno_tx_prepare,
    101 	.uno_rx_loop = url_uno_rx_loop,
    102 	.uno_init = url_uno_init,
    103 };
    104 
    105 /* Macros */
    106 #ifdef URL_DEBUG
    107 #define DPRINTF(x)	if (urldebug) printf x
    108 #define DPRINTFN(n, x)	if (urldebug >= (n)) printf x
    109 int urldebug = 0;
    110 #else
    111 #define DPRINTF(x)
    112 #define DPRINTFN(n, x)
    113 #endif
    114 
    115 #define	URL_SETBIT(un, reg, x)	\
    116 	url_csr_write_1(un, reg, url_csr_read_1(un, reg) | (x))
    117 
    118 #define	URL_SETBIT2(un, reg, x)	\
    119 	url_csr_write_2(un, reg, url_csr_read_2(un, reg) | (x))
    120 
    121 #define	URL_CLRBIT(un, reg, x)	\
    122 	url_csr_write_1(un, reg, url_csr_read_1(un, reg) & ~(x))
    123 
    124 #define	URL_CLRBIT2(un, reg, x)	\
    125 	url_csr_write_2(un, reg, url_csr_read_2(un, reg) & ~(x))
    126 
    127 static const struct url_type {
    128 	struct usb_devno url_dev;
    129 	uint16_t url_flags;
    130 #define URL_EXT_PHY	0x0001
    131 } url_devs [] = {
    132 	/* MELCO LUA-KTX */
    133 	{{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAKTX }, 0},
    134 	/* Realtek RTL8150L Generic (GREEN HOUSE USBKR100) */
    135 	{{ USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_RTL8150L}, 0},
    136 	/* Longshine LCS-8138TX */
    137 	{{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_LCS8138TX}, 0},
    138 	/* Micronet SP128AR */
    139 	{{ USB_VENDOR_MICRONET, USB_PRODUCT_MICRONET_SP128AR}, 0},
    140 	/* OQO model 01 */
    141 	{{ USB_VENDOR_OQO, USB_PRODUCT_OQO_ETHER01}, 0},
    142 };
    143 #define url_lookup(v, p) ((const struct url_type *)usb_lookup(url_devs, v, p))
    144 
    145 
    146 /* Probe */
    147 static int
    148 url_match(device_t parent, cfdata_t match, void *aux)
    149 {
    150 	struct usb_attach_arg *uaa = aux;
    151 
    152 	return url_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
    153 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    154 }
    155 /* Attach */
    156 static void
    157 url_attach(device_t parent, device_t self, void *aux)
    158 {
    159 	USBNET_MII_DECL_DEFAULT(unm);
    160 	struct usbnet * const un = device_private(self);
    161 	struct usb_attach_arg *uaa = aux;
    162 	struct usbd_device *dev = uaa->uaa_device;
    163 	struct usbd_interface *iface;
    164 	usbd_status err;
    165 	usb_interface_descriptor_t *id;
    166 	usb_endpoint_descriptor_t *ed;
    167 	char *devinfop;
    168 	int i;
    169 
    170 	aprint_naive("\n");
    171 	aprint_normal("\n");
    172 	devinfop = usbd_devinfo_alloc(dev, 0);
    173 	aprint_normal_dev(self, "%s\n", devinfop);
    174 	usbd_devinfo_free(devinfop);
    175 
    176 	un->un_dev = self;
    177 	un->un_udev = dev;
    178 	un->un_sc = un;
    179 	un->un_ops = &url_ops;
    180 	un->un_rx_xfer_flags = USBD_SHORT_XFER_OK;
    181 	un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER;
    182 	un->un_rx_list_cnt = URL_RX_LIST_CNT;
    183 	un->un_tx_list_cnt = URL_TX_LIST_CNT;
    184 	un->un_rx_bufsz = URL_BUFSZ;
    185 	un->un_tx_bufsz = URL_BUFSZ;
    186 
    187 	/* Move the device into the configured state. */
    188 	err = usbd_set_config_no(dev, URL_CONFIG_NO, 1);
    189 	if (err) {
    190 		aprint_error_dev(self, "failed to set configuration"
    191 		    ", err=%s\n", usbd_errstr(err));
    192 		return;
    193 	}
    194 
    195 	/* get control interface */
    196 	err = usbd_device2interface_handle(dev, URL_IFACE_INDEX, &iface);
    197 	if (err) {
    198 		aprint_error_dev(self, "failed to get interface, err=%s\n",
    199 		       usbd_errstr(err));
    200 		return;
    201 	}
    202 
    203 	un->un_iface = iface;
    204 	un->un_flags = url_lookup(uaa->uaa_vendor, uaa->uaa_product)->url_flags;
    205 #if 0
    206 	if (un->un_flags & URL_EXT_PHY) {
    207 		un->un_read_reg_cb = url_ext_mii_read_reg;
    208 		un->un_write_reg_cb = url_ext_mii_write_reg;
    209 	}
    210 #endif
    211 
    212 	/* get interface descriptor */
    213 	id = usbd_get_interface_descriptor(un->un_iface);
    214 
    215 	/* find endpoints */
    216 	un->un_ed[USBNET_ENDPT_RX] = un->un_ed[USBNET_ENDPT_TX] =
    217 	    un->un_ed[USBNET_ENDPT_INTR] = 0;
    218 	for (i = 0; i < id->bNumEndpoints; i++) {
    219 		ed = usbd_interface2endpoint_descriptor(un->un_iface, i);
    220 		if (ed == NULL) {
    221 			aprint_error_dev(self,
    222 			    "couldn't get endpoint %d\n", i);
    223 			return;
    224 		}
    225 		if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
    226 		    UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
    227 			un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress;
    228 		else if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
    229 			 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT)
    230 			un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress;
    231 		else if ((ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT &&
    232 			 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
    233 			un->un_ed[USBNET_ENDPT_INTR] = ed->bEndpointAddress;
    234 	}
    235 
    236 	if (un->un_ed[USBNET_ENDPT_RX] == 0 ||
    237 	    un->un_ed[USBNET_ENDPT_TX] == 0 ||
    238 	    un->un_ed[USBNET_ENDPT_INTR] == 0) {
    239 		aprint_error_dev(self, "missing endpoint\n");
    240 		return;
    241 	}
    242 
    243 	/* Set these up now for url_mem().  */
    244 	usbnet_attach(un, "urldet");
    245 
    246 	usbnet_lock_core(un);
    247 	usbnet_busy(un);
    248 
    249 	/* reset the adapter */
    250 	url_reset(un);
    251 
    252 	/* Get Ethernet Address */
    253 	err = url_mem(un, URL_CMD_READMEM, URL_IDR0, (void *)un->un_eaddr,
    254 		      ETHER_ADDR_LEN);
    255 	usbnet_unbusy(un);
    256 	usbnet_unlock_core(un);
    257 	if (err) {
    258 		aprint_error_dev(self, "read MAC address failed\n");
    259 		goto bad;
    260 	}
    261 
    262 	/* initialize interface information */
    263 	usbnet_attach_ifp(un, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST,
    264 	    0, &unm);
    265 
    266 	return;
    267 
    268  bad:
    269 	usbnet_set_dying(un, true);
    270 	return;
    271 }
    272 
    273 /* read/write memory */
    274 static int
    275 url_mem(struct usbnet *un, int cmd, int offset, void *buf, int len)
    276 {
    277 	usb_device_request_t req;
    278 	usbd_status err;
    279 
    280 	usbnet_isowned_core(un);
    281 
    282 	DPRINTFN(0x200,
    283 		("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    284 
    285 	if (usbnet_isdying(un))
    286 		return 0;
    287 
    288 	if (cmd == URL_CMD_READMEM)
    289 		req.bmRequestType = UT_READ_VENDOR_DEVICE;
    290 	else
    291 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    292 	req.bRequest = URL_REQ_MEM;
    293 	USETW(req.wValue, offset);
    294 	USETW(req.wIndex, 0x0000);
    295 	USETW(req.wLength, len);
    296 
    297 	err = usbd_do_request(un->un_udev, &req, buf);
    298 	if (err) {
    299 		DPRINTF(("%s: url_mem(): %s failed. off=%04x, err=%d\n",
    300 			 device_xname(un->un_dev),
    301 			 cmd == URL_CMD_READMEM ? "read" : "write",
    302 			 offset, err));
    303 	}
    304 
    305 	return err;
    306 }
    307 
    308 /* read 1byte from register */
    309 static int
    310 url_csr_read_1(struct usbnet *un, int reg)
    311 {
    312 	uint8_t val = 0;
    313 
    314 	DPRINTFN(0x100,
    315 		 ("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    316 
    317 	return url_mem(un, URL_CMD_READMEM, reg, &val, 1) ? 0 : val;
    318 }
    319 
    320 /* read 2bytes from register */
    321 static int
    322 url_csr_read_2(struct usbnet *un, int reg)
    323 {
    324 	uWord val;
    325 
    326 	DPRINTFN(0x100,
    327 		 ("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    328 
    329 	USETW(val, 0);
    330 	return url_mem(un, URL_CMD_READMEM, reg, &val, 2) ? 0 : UGETW(val);
    331 }
    332 
    333 /* write 1byte to register */
    334 static int
    335 url_csr_write_1(struct usbnet *un, int reg, int aval)
    336 {
    337 	uint8_t val = aval;
    338 
    339 	DPRINTFN(0x100,
    340 		 ("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    341 
    342 	return url_mem(un, URL_CMD_WRITEMEM, reg, &val, 1) ? -1 : 0;
    343 }
    344 
    345 /* write 2bytes to register */
    346 static int
    347 url_csr_write_2(struct usbnet *un, int reg, int aval)
    348 {
    349 	uWord val;
    350 
    351 	DPRINTFN(0x100,
    352 		 ("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    353 
    354 	USETW(val, aval);
    355 
    356 	return url_mem(un, URL_CMD_WRITEMEM, reg, &val, 2) ? -1 : 0;
    357 }
    358 
    359 /* write 4bytes to register */
    360 static int
    361 url_csr_write_4(struct usbnet *un, int reg, int aval)
    362 {
    363 	uDWord val;
    364 
    365 	DPRINTFN(0x100,
    366 		 ("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    367 
    368 	USETDW(val, aval);
    369 
    370 	return url_mem(un, URL_CMD_WRITEMEM, reg, &val, 4) ? -1 : 0;
    371 }
    372 
    373 static int
    374 url_init_locked(struct ifnet *ifp)
    375 {
    376 	struct usbnet * const un = ifp->if_softc;
    377 	const u_char *eaddr;
    378 	int i;
    379 
    380 	DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    381 
    382 	usbnet_isowned_core(un);
    383 
    384 	if (usbnet_isdying(un))
    385 		return EIO;
    386 
    387 	/* Cancel pending I/O and free all TX/RX buffers */
    388 	usbnet_stop(un, ifp, 1);
    389 
    390 	eaddr = CLLADDR(ifp->if_sadl);
    391 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    392 		url_csr_write_1(un, URL_IDR0 + i, eaddr[i]);
    393 
    394 	/* Init transmission control register */
    395 	URL_CLRBIT(un, URL_TCR,
    396 		   URL_TCR_TXRR1 | URL_TCR_TXRR0 |
    397 		   URL_TCR_IFG1 | URL_TCR_IFG0 |
    398 		   URL_TCR_NOCRC);
    399 
    400 	/* Init receive control register */
    401 	URL_SETBIT2(un, URL_RCR, URL_RCR_TAIL | URL_RCR_AD);
    402 	if (ifp->if_flags & IFF_BROADCAST)
    403 		URL_SETBIT2(un, URL_RCR, URL_RCR_AB);
    404 	else
    405 		URL_CLRBIT2(un, URL_RCR, URL_RCR_AB);
    406 
    407 	/* If we want promiscuous mode, accept all physical frames. */
    408 	if (ifp->if_flags & IFF_PROMISC)
    409 		URL_SETBIT2(un, URL_RCR, URL_RCR_AAM | URL_RCR_AAP);
    410 	else
    411 		URL_CLRBIT2(un, URL_RCR, URL_RCR_AAM | URL_RCR_AAP);
    412 
    413 	/* Load the multicast filter */
    414 	url_setiff_locked(un);
    415 
    416 	/* Enable RX and TX */
    417 	URL_SETBIT(un, URL_CR, URL_CR_TE | URL_CR_RE);
    418 
    419 	return usbnet_init_rx_tx(un);
    420 }
    421 
    422 static int
    423 url_uno_init(struct ifnet *ifp)
    424 {
    425 	struct usbnet * const un = ifp->if_softc;
    426 
    427 	usbnet_lock_core(un);
    428 	usbnet_busy(un);
    429 	int ret = url_init_locked(ifp);
    430 	usbnet_unbusy(un);
    431 	usbnet_unlock_core(un);
    432 
    433 	return ret;
    434 }
    435 
    436 static void
    437 url_reset(struct usbnet *un)
    438 {
    439 	int i;
    440 
    441 	DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    442 
    443 	if (usbnet_isdying(un))
    444 		return;
    445 
    446 	URL_SETBIT(un, URL_CR, URL_CR_SOFT_RST);
    447 
    448 	for (i = 0; i < URL_TX_TIMEOUT; i++) {
    449 		if (!(url_csr_read_1(un, URL_CR) & URL_CR_SOFT_RST))
    450 			break;
    451 		delay(10);	/* XXX */
    452 	}
    453 
    454 	delay(10000);		/* XXX */
    455 }
    456 
    457 #define url_calchash(addr) (ether_crc32_be((addr), ETHER_ADDR_LEN) >> 26)
    458 
    459 static void
    460 url_setiff_locked(struct usbnet *un)
    461 {
    462 	struct ifnet * const ifp = usbnet_ifp(un);
    463 	struct ethercom *ec = usbnet_ec(un);
    464 	struct ether_multi *enm;
    465 	struct ether_multistep step;
    466 	uint32_t hashes[2] = { 0, 0 };
    467 	int h = 0;
    468 	int mcnt = 0;
    469 
    470 	DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    471 
    472 	usbnet_isowned_core(un);
    473 
    474 	if (usbnet_isdying(un))
    475 		return;
    476 
    477 	if (ifp->if_flags & IFF_PROMISC) {
    478 		URL_SETBIT2(un, URL_RCR, URL_RCR_AAM | URL_RCR_AAP);
    479 		return;
    480 	} else if (ifp->if_flags & IFF_ALLMULTI) {
    481 allmulti:
    482 		ifp->if_flags |= IFF_ALLMULTI;
    483 		URL_SETBIT2(un, URL_RCR, URL_RCR_AAM);
    484 		URL_CLRBIT2(un, URL_RCR, URL_RCR_AAP);
    485 		return;
    486 	}
    487 
    488 	/* first, zot all the existing hash bits */
    489 	url_csr_write_4(un, URL_MAR0, 0);
    490 	url_csr_write_4(un, URL_MAR4, 0);
    491 
    492 	/* now program new ones */
    493 	ETHER_LOCK(ec);
    494 	ETHER_FIRST_MULTI(step, ec, enm);
    495 	while (enm != NULL) {
    496 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    497 		    ETHER_ADDR_LEN) != 0) {
    498 			ETHER_UNLOCK(ec);
    499 			goto allmulti;
    500 		}
    501 
    502 		h = url_calchash(enm->enm_addrlo);
    503 		if (h < 32)
    504 			hashes[0] |= (1 << h);
    505 		else
    506 			hashes[1] |= (1 << (h -32));
    507 		mcnt++;
    508 		ETHER_NEXT_MULTI(step, enm);
    509 	}
    510 	ETHER_UNLOCK(ec);
    511 
    512 	ifp->if_flags &= ~IFF_ALLMULTI;
    513 
    514 	URL_CLRBIT2(un, URL_RCR, URL_RCR_AAM | URL_RCR_AAP);
    515 
    516 	if (mcnt) {
    517 		URL_SETBIT2(un, URL_RCR, URL_RCR_AM);
    518 	} else {
    519 		URL_CLRBIT2(un, URL_RCR, URL_RCR_AM);
    520 	}
    521 	url_csr_write_4(un, URL_MAR0, hashes[0]);
    522 	url_csr_write_4(un, URL_MAR4, hashes[1]);
    523 }
    524 
    525 static unsigned
    526 url_uno_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c)
    527 {
    528 	int total_len;
    529 
    530 	DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev),__func__));
    531 
    532 	KASSERT(un->un_tx_bufsz >= URL_MIN_FRAME_LEN);
    533 	if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz)
    534 		return 0;
    535 
    536 	/* Copy the mbuf data into a contiguous buffer */
    537 	m_copydata(m, 0, m->m_pkthdr.len, c->unc_buf);
    538 	total_len = m->m_pkthdr.len;
    539 
    540 	if (total_len < URL_MIN_FRAME_LEN) {
    541 		memset(c->unc_buf + total_len, 0,
    542 		    URL_MIN_FRAME_LEN - total_len);
    543 		total_len = URL_MIN_FRAME_LEN;
    544 	}
    545 
    546 	DPRINTF(("%s: %s: send %d bytes\n", device_xname(un->un_dev),
    547 		 __func__, total_len));
    548 
    549 	return total_len;
    550 }
    551 
    552 static void
    553 url_uno_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len)
    554 {
    555 	struct ifnet *ifp = usbnet_ifp(un);
    556 	url_rxhdr_t rxhdr;
    557 
    558 	DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev),__func__));
    559 
    560 	if (total_len <= ETHER_CRC_LEN || total_len <= sizeof(rxhdr)) {
    561 		if_statinc(ifp, if_ierrors);
    562 		return;
    563 	}
    564 
    565 	memcpy(&rxhdr, c->unc_buf + total_len - ETHER_CRC_LEN, sizeof(rxhdr));
    566 
    567 	DPRINTF(("%s: RX Status: %dbytes%s%s%s%s packets\n",
    568 		 device_xname(un->un_dev),
    569 		 UGETW(rxhdr) & URL_RXHDR_BYTEC_MASK,
    570 		 UGETW(rxhdr) & URL_RXHDR_VALID_MASK ? ", Valid" : "",
    571 		 UGETW(rxhdr) & URL_RXHDR_RUNTPKT_MASK ? ", Runt" : "",
    572 		 UGETW(rxhdr) & URL_RXHDR_PHYPKT_MASK ? ", Physical match" : "",
    573 		 UGETW(rxhdr) & URL_RXHDR_MCASTPKT_MASK ? ", Multicast" : ""));
    574 
    575 	if ((UGETW(rxhdr) & URL_RXHDR_VALID_MASK) == 0) {
    576 		if_statinc(ifp, if_ierrors);
    577 		return;
    578 	}
    579 
    580 	total_len -= ETHER_CRC_LEN;
    581 
    582 	DPRINTF(("%s: %s: deliver %d\n", device_xname(un->un_dev),
    583 		 __func__, total_len));
    584 	usbnet_enqueue(un, c->unc_buf, total_len, 0, 0, 0);
    585 }
    586 
    587 #if 0
    588 static void url_intr(void)
    589 {
    590 }
    591 #endif
    592 
    593 static int
    594 url_uno_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    595 {
    596 	struct usbnet * const un = ifp->if_softc;
    597 
    598 	usbnet_lock_core(un);
    599 	usbnet_busy(un);
    600 
    601 	switch (cmd) {
    602 	case SIOCADDMULTI:
    603 	case SIOCDELMULTI:
    604 		url_setiff_locked(un);
    605 		break;
    606 	default:
    607 		break;
    608 	}
    609 
    610 	usbnet_unbusy(un);
    611 	usbnet_unlock_core(un);
    612 
    613 	return 0;
    614 }
    615 
    616 /* Stop the adapter and free any mbufs allocated to the RX and TX lists. */
    617 static void
    618 url_uno_stop(struct ifnet *ifp, int disable)
    619 {
    620 	struct usbnet * const un = ifp->if_softc;
    621 
    622 	DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    623 
    624 	url_reset(un);
    625 }
    626 
    627 static int
    628 url_uno_mii_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val)
    629 {
    630 	uint16_t data;
    631 	usbd_status err = USBD_NORMAL_COMPLETION;
    632 
    633 	DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x\n",
    634 		 device_xname(un->un_dev), __func__, phy, reg));
    635 
    636 	/* XXX: one PHY only for the RTL8150 internal PHY */
    637 	if (phy != 0) {
    638 		DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
    639 			 device_xname(un->un_dev), __func__, phy));
    640 		return EINVAL;
    641 	}
    642 
    643 	switch (reg) {
    644 	case MII_BMCR:		/* Control Register */
    645 		reg = URL_BMCR;
    646 		break;
    647 	case MII_BMSR:		/* Status Register */
    648 		reg = URL_BMSR;
    649 		break;
    650 	case MII_PHYIDR1:
    651 	case MII_PHYIDR2:
    652 		*val = 0;
    653 		goto R_DONE;
    654 		break;
    655 	case MII_ANAR:		/* Autonegotiation advertisement */
    656 		reg = URL_ANAR;
    657 		break;
    658 	case MII_ANLPAR:	/* Autonegotiation link partner abilities */
    659 		reg = URL_ANLP;
    660 		break;
    661 	case URLPHY_MSR:	/* Media Status Register */
    662 		reg = URL_MSR;
    663 		break;
    664 	default:
    665 		printf("%s: %s: bad register %04x\n",
    666 		       device_xname(un->un_dev), __func__, reg);
    667 		return EINVAL;
    668 	}
    669 
    670 	if (reg == URL_MSR)
    671 		data = url_csr_read_1(un, reg);
    672 	else
    673 		data = url_csr_read_2(un, reg);
    674 	*val = data;
    675 
    676  R_DONE:
    677 	DPRINTFN(0xff, ("%s: %s: phy=%d reg=0x%04x => 0x%04hx\n",
    678 		 device_xname(un->un_dev), __func__, phy, reg, *val));
    679 
    680 	return err;
    681 }
    682 
    683 static int
    684 url_uno_mii_write_reg(struct usbnet *un, int phy, int reg, uint16_t val)
    685 {
    686 
    687 	DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x val=0x%04hx\n",
    688 		 device_xname(un->un_dev), __func__, phy, reg, val));
    689 
    690 	/* XXX: one PHY only for the RTL8150 internal PHY */
    691 	if (phy != 0) {
    692 		DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
    693 			 device_xname(un->un_dev), __func__, phy));
    694 		return EINVAL;
    695 	}
    696 
    697 	switch (reg) {
    698 	case MII_BMCR:		/* Control Register */
    699 		reg = URL_BMCR;
    700 		break;
    701 	case MII_BMSR:		/* Status Register */
    702 		reg = URL_BMSR;
    703 		break;
    704 	case MII_PHYIDR1:
    705 	case MII_PHYIDR2:
    706 		return 0;
    707 	case MII_ANAR:		/* Autonegotiation advertisement */
    708 		reg = URL_ANAR;
    709 		break;
    710 	case MII_ANLPAR:	/* Autonegotiation link partner abilities */
    711 		reg = URL_ANLP;
    712 		break;
    713 	case URLPHY_MSR:	/* Media Status Register */
    714 		reg = URL_MSR;
    715 		break;
    716 	default:
    717 		printf("%s: %s: bad register %04x\n",
    718 		       device_xname(un->un_dev), __func__, reg);
    719 		return EINVAL;
    720 	}
    721 
    722 	if (reg == URL_MSR)
    723 		url_csr_write_1(un, reg, val);
    724 	else
    725 		url_csr_write_2(un, reg, val);
    726 
    727 	return 0;
    728 }
    729 
    730 static void
    731 url_uno_mii_statchg(struct ifnet *ifp)
    732 {
    733 	struct usbnet * const un = ifp->if_softc;
    734 
    735 	DPRINTF(("%s: %s: enter\n", ifp->if_xname, __func__));
    736 
    737 	/* XXX */
    738 	usbnet_set_link(un, true);
    739 }
    740 
    741 #if 0
    742 /*
    743  * external PHYs support, but not test.
    744  */
    745 static usbd_status
    746 url_ext_mii_read_reg(struct usbnet *un, int phy, int reg)
    747 {
    748 	uint16_t val;
    749 
    750 	DPRINTF(("%s: %s: enter, phy=%d reg=0x%04x\n",
    751 		 device_xname(un->un_dev), __func__, phy, reg));
    752 
    753 	url_csr_write_1(un, URL_PHYADD, phy & URL_PHYADD_MASK);
    754 	/*
    755 	 * RTL8150L will initiate a MII management data transaction
    756 	 * if PHYCNT_OWN bit is set 1 by software. After transaction,
    757 	 * this bit is auto cleared by TRL8150L.
    758 	 */
    759 	url_csr_write_1(un, URL_PHYCNT,
    760 			(reg | URL_PHYCNT_PHYOWN) & ~URL_PHYCNT_RWCR);
    761 	for (i = 0; i < URL_TIMEOUT; i++) {
    762 		if ((url_csr_read_1(un, URL_PHYCNT) & URL_PHYCNT_PHYOWN) == 0)
    763 			break;
    764 	}
    765 	if (i == URL_TIMEOUT) {
    766 		printf("%s: MII read timed out\n", device_xname(un->un_dev));
    767 	}
    768 
    769 	val = url_csr_read_2(un, URL_PHYDAT);
    770 
    771 	DPRINTF(("%s: %s: phy=%d reg=0x%04x => 0x%04x\n",
    772 		 device_xname(un->un_dev), __func__, phy, reg, val));
    773 
    774 	return USBD_NORMAL_COMPLETION;
    775 }
    776 
    777 static usbd_status
    778 url_ext_mii_write_reg(struct usbnet *un, int phy, int reg, int data)
    779 {
    780 
    781 	DPRINTF(("%s: %s: enter, phy=%d reg=0x%04x data=0x%04x\n",
    782 		 device_xname(un->un_dev), __func__, phy, reg, data));
    783 
    784 	url_csr_write_2(un, URL_PHYDAT, data);
    785 	url_csr_write_1(un, URL_PHYADD, phy);
    786 	url_csr_write_1(un, URL_PHYCNT, reg | URL_PHYCNT_RWCR);	/* Write */
    787 
    788 	for (i=0; i < URL_TIMEOUT; i++) {
    789 		if (url_csr_read_1(un, URL_PHYCNT) & URL_PHYCNT_PHYOWN)
    790 			break;
    791 	}
    792 
    793 	if (i == URL_TIMEOUT) {
    794 		printf("%s: MII write timed out\n",
    795 		       device_xname(un->un_dev));
    796 		return USBD_TIMEOUT;
    797 	}
    798 
    799 	return USBD_NORMAL_COMPLETION;
    800 }
    801 #endif
    802 
    803 #ifdef _MODULE
    804 #include "ioconf.c"
    805 #endif
    806 
    807 USBNET_MODULE(url)
    808