Home | History | Annotate | Line # | Download | only in usb
if_udav.c revision 1.62
      1  1.62     skrll /*	$NetBSD: if_udav.c,v 1.62 2019/08/07 20:34:12 skrll Exp $	*/
      2   1.1    itojun /*	$nabe: if_udav.c,v 1.3 2003/08/21 16:57:19 nabe Exp $	*/
      3  1.37       mrg 
      4   1.1    itojun /*
      5   1.1    itojun  * Copyright (c) 2003
      6   1.1    itojun  *     Shingo WATANABE <nabe (at) nabechan.org>.  All rights reserved.
      7   1.1    itojun  *
      8   1.1    itojun  * Redistribution and use in source and binary forms, with or without
      9   1.1    itojun  * modification, are permitted provided that the following conditions
     10   1.1    itojun  * are met:
     11   1.1    itojun  * 1. Redistributions of source code must retain the above copyright
     12   1.1    itojun  *    notice, this list of conditions and the following disclaimer.
     13   1.1    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1    itojun  *    notice, this list of conditions and the following disclaimer in the
     15   1.1    itojun  *    documentation and/or other materials provided with the distribution.
     16   1.2   tsutsui  * 3. Neither the name of the author nor the names of any co-contributors
     17   1.1    itojun  *    may be used to endorse or promote products derived from this software
     18   1.1    itojun  *    without specific prior written permission.
     19   1.1    itojun  *
     20   1.1    itojun  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     21   1.1    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22   1.1    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23   1.1    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     24   1.1    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25   1.1    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26   1.1    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27   1.1    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28   1.1    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29   1.1    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30   1.1    itojun  * SUCH DAMAGE.
     31   1.1    itojun  *
     32   1.1    itojun  */
     33   1.1    itojun 
     34   1.1    itojun /*
     35   1.1    itojun  * DM9601(DAVICOM USB to Ethernet MAC Controller with Integrated 10/100 PHY)
     36   1.1    itojun  * The spec can be found at the following url.
     37   1.1    itojun  *   http://www.davicom.com.tw/big5/download/Data%20Sheet/DM9601-DS-F01-062202s.pdf
     38   1.1    itojun  */
     39   1.1    itojun 
     40   1.1    itojun /*
     41   1.1    itojun  * TODO:
     42   1.1    itojun  *	Interrupt Endpoint support
     43   1.1    itojun  *	External PHYs
     44   1.1    itojun  *	powerhook() support?
     45   1.1    itojun  */
     46   1.1    itojun 
     47   1.1    itojun #include <sys/cdefs.h>
     48  1.62     skrll __KERNEL_RCSID(0, "$NetBSD: if_udav.c,v 1.62 2019/08/07 20:34:12 skrll Exp $");
     49   1.1    itojun 
     50  1.40  christos #ifdef _KERNEL_OPT
     51  1.49     skrll #include "opt_usb.h"
     52  1.40  christos #endif
     53   1.1    itojun 
     54   1.1    itojun #include <sys/param.h>
     55   1.1    itojun #include <sys/systm.h>
     56  1.61     skrll #include <sys/module.h>
     57   1.1    itojun #include <sys/kernel.h>
     58   1.1    itojun 
     59  1.61     skrll #include <dev/usb/usbnet.h>
     60   1.1    itojun #include <dev/usb/if_udavreg.h>
     61   1.1    itojun 
     62  1.61     skrll struct udav_softc {
     63  1.61     skrll 	struct usbnet		sc_un;
     64  1.61     skrll 	uint16_t		sc_flags;
     65  1.61     skrll };
     66   1.1    itojun 
     67   1.1    itojun /* Function declarations */
     68  1.48   msaitoh int	udav_match(device_t, cfdata_t, void *);
     69  1.48   msaitoh void	udav_attach(device_t, device_t, void *);
     70  1.57       mrg 
     71  1.48   msaitoh CFATTACH_DECL_NEW(udav, sizeof(struct udav_softc), udav_match, udav_attach,
     72  1.61     skrll     usbnet_detach, usbnet_activate);
     73  1.61     skrll 
     74  1.61     skrll static void udav_chip_init(struct usbnet *);
     75   1.1    itojun 
     76  1.61     skrll static unsigned udav_tx_prepare(struct usbnet *, struct mbuf *,
     77  1.61     skrll 			        struct usbnet_chain *);
     78  1.61     skrll static void udav_rxeof_loop(struct usbnet *, struct usbd_xfer *,
     79  1.61     skrll 			    struct usbnet_chain *, uint32_t);
     80  1.61     skrll static void udav_stop_cb(struct ifnet *, int);
     81  1.61     skrll static int udav_ioctl_cb(struct ifnet *, u_long, void *);
     82  1.61     skrll static usbd_status udav_mii_read_reg(struct usbnet *, int, int, uint16_t *);
     83  1.61     skrll static usbd_status udav_mii_write_reg(struct usbnet *, int, int, uint16_t);
     84  1.61     skrll static void udav_mii_statchg(struct ifnet *);
     85  1.61     skrll static int udav_init(struct ifnet *);
     86  1.61     skrll static void udav_setiff(struct usbnet *);
     87  1.61     skrll static void udav_setiff_locked(struct usbnet *);
     88  1.61     skrll static void udav_reset(struct usbnet *);
     89  1.61     skrll 
     90  1.61     skrll static int udav_csr_read(struct udav_softc *, int, void *, int);
     91  1.61     skrll static int udav_csr_write(struct udav_softc *, int, void *, int);
     92  1.61     skrll static int udav_csr_read1(struct udav_softc *, int);
     93  1.61     skrll static int udav_csr_write1(struct udav_softc *, int, unsigned char);
     94   1.1    itojun 
     95   1.1    itojun #if 0
     96  1.61     skrll static int udav_mem_read(struct udav_softc *, int, void *, int);
     97  1.61     skrll static int udav_mem_write(struct udav_softc *, int, void *, int);
     98  1.61     skrll static int udav_mem_write1(struct udav_softc *, int, unsigned char);
     99   1.1    itojun #endif
    100   1.1    itojun 
    101   1.1    itojun /* Macros */
    102   1.1    itojun #ifdef UDAV_DEBUG
    103  1.31    dyoung #define DPRINTF(x)	if (udavdebug) printf x
    104  1.58   msaitoh #define DPRINTFN(n, x)	if (udavdebug >= (n)) printf x
    105  1.61     skrll int udavdebug = 10;
    106   1.1    itojun #else
    107   1.1    itojun #define DPRINTF(x)
    108  1.58   msaitoh #define DPRINTFN(n, x)
    109   1.1    itojun #endif
    110   1.1    itojun 
    111   1.1    itojun #define	UDAV_SETBIT(sc, reg, x)	\
    112   1.1    itojun 	udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) | (x))
    113   1.1    itojun 
    114   1.1    itojun #define	UDAV_CLRBIT(sc, reg, x)	\
    115   1.1    itojun 	udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) & ~(x))
    116   1.1    itojun 
    117   1.1    itojun static const struct udav_type {
    118   1.1    itojun 	struct usb_devno udav_dev;
    119  1.46     skrll 	uint16_t udav_flags;
    120   1.1    itojun #define UDAV_EXT_PHY	0x0001
    121  1.42  jakllsch #define UDAV_NO_PHY	0x0002
    122   1.1    itojun } udav_devs [] = {
    123   1.1    itojun 	/* Corega USB-TXC */
    124   1.1    itojun 	{{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXC }, 0},
    125  1.18   xtraeme 	/* ShanTou ST268 USB NIC */
    126  1.18   xtraeme 	{{ USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ST268_USB_NIC }, 0},
    127  1.22  drochner 	/* ShanTou ADM8515 */
    128  1.22  drochner 	{{ USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ADM8515 }, 0},
    129  1.30  jmcneill 	/* SUNRISING SR9600 */
    130  1.30  jmcneill 	{{ USB_VENDOR_SUNRISING, USB_PRODUCT_SUNRISING_SR9600 }, 0 },
    131  1.42  jakllsch 	/* SUNRISING QF9700 */
    132  1.42  jakllsch 	{{ USB_VENDOR_SUNRISING, USB_PRODUCT_SUNRISING_QF9700 }, UDAV_NO_PHY },
    133  1.35   mbalmer 	/* QUAN DM9601 */
    134  1.35   mbalmer 	{{USB_VENDOR_QUAN, USB_PRODUCT_QUAN_DM9601 }, 0},
    135   1.1    itojun #if 0
    136   1.1    itojun 	/* DAVICOM DM9601 Generic? */
    137   1.1    itojun 	/*  XXX: The following ids was obtained from the data sheet. */
    138   1.1    itojun 	{{ 0x0a46, 0x9601 }, 0},
    139   1.1    itojun #endif
    140   1.1    itojun };
    141   1.7  christos #define udav_lookup(v, p) ((const struct udav_type *)usb_lookup(udav_devs, v, p))
    142   1.1    itojun 
    143   1.1    itojun 
    144   1.1    itojun /* Probe */
    145  1.40  christos int
    146  1.31    dyoung udav_match(device_t parent, cfdata_t match, void *aux)
    147   1.1    itojun {
    148  1.31    dyoung 	struct usb_attach_arg *uaa = aux;
    149   1.1    itojun 
    150  1.46     skrll 	return udav_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
    151  1.46     skrll 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    152   1.1    itojun }
    153   1.1    itojun 
    154   1.1    itojun /* Attach */
    155  1.40  christos void
    156  1.31    dyoung udav_attach(device_t parent, device_t self, void *aux)
    157   1.1    itojun {
    158  1.31    dyoung 	struct udav_softc *sc = device_private(self);
    159  1.61     skrll 	struct usbnet * const un = &sc->sc_un;
    160  1.31    dyoung 	struct usb_attach_arg *uaa = aux;
    161  1.46     skrll 	struct usbd_device *dev = uaa->uaa_device;
    162  1.46     skrll 	struct usbd_interface *iface;
    163   1.1    itojun 	usbd_status err;
    164   1.1    itojun 	usb_interface_descriptor_t *id;
    165   1.1    itojun 	usb_endpoint_descriptor_t *ed;
    166   1.6  augustss 	char *devinfop;
    167  1.61     skrll 	int i;
    168   1.1    itojun 
    169  1.61     skrll 	/* Switch to usbnet for device_private() */
    170  1.61     skrll 	self->dv_private = un;
    171  1.24      cube 
    172  1.26    plunky 	aprint_naive("\n");
    173  1.26    plunky 	aprint_normal("\n");
    174   1.6  augustss 	devinfop = usbd_devinfo_alloc(dev, 0);
    175  1.24      cube 	aprint_normal_dev(self, "%s\n", devinfop);
    176   1.6  augustss 	usbd_devinfo_free(devinfop);
    177   1.1    itojun 
    178  1.61     skrll 	un->un_dev = self;
    179  1.61     skrll 	un->un_udev = dev;
    180  1.61     skrll 	un->un_sc = sc;
    181  1.61     skrll 	un->un_stop_cb = udav_stop_cb;
    182  1.61     skrll 	un->un_ioctl_cb = udav_ioctl_cb;
    183  1.61     skrll 	un->un_read_reg_cb = udav_mii_read_reg;
    184  1.61     skrll 	un->un_write_reg_cb = udav_mii_write_reg;
    185  1.61     skrll 	un->un_statchg_cb = udav_mii_statchg;
    186  1.61     skrll 	un->un_tx_prepare_cb = udav_tx_prepare;
    187  1.61     skrll 	un->un_rx_loop_cb = udav_rxeof_loop;
    188  1.61     skrll 	un->un_init_cb = udav_init;
    189  1.61     skrll 	un->un_rx_xfer_flags = USBD_SHORT_XFER_OK;
    190  1.61     skrll 	un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER;
    191  1.61     skrll 
    192   1.1    itojun 	/* Move the device into the configured state. */
    193  1.22  drochner 	err = usbd_set_config_no(dev, UDAV_CONFIG_NO, 1); /* idx 0 */
    194   1.1    itojun 	if (err) {
    195  1.39     skrll 		aprint_error_dev(self, "failed to set configuration"
    196  1.39     skrll 		    ", err=%s\n", usbd_errstr(err));
    197  1.61     skrll 		return;
    198   1.1    itojun 	}
    199   1.1    itojun 
    200   1.1    itojun 	/* get control interface */
    201   1.1    itojun 	err = usbd_device2interface_handle(dev, UDAV_IFACE_INDEX, &iface);
    202   1.1    itojun 	if (err) {
    203  1.24      cube 		aprint_error_dev(self, "failed to get interface, err=%s\n",
    204   1.1    itojun 		       usbd_errstr(err));
    205  1.61     skrll 		return;
    206   1.1    itojun 	}
    207   1.1    itojun 
    208  1.61     skrll 	un->un_iface = iface;
    209  1.48   msaitoh 	sc->sc_flags = udav_lookup(uaa->uaa_vendor,
    210  1.48   msaitoh 	    uaa->uaa_product)->udav_flags;
    211   1.1    itojun 
    212   1.1    itojun 	/* get interface descriptor */
    213  1.61     skrll 	id = usbd_get_interface_descriptor(un->un_iface);
    214   1.1    itojun 
    215   1.1    itojun 	/* find endpoints */
    216  1.61     skrll 	un->un_ed[USBNET_ENDPT_RX] = un->un_ed[USBNET_ENDPT_TX] =
    217  1.61     skrll 	    un->un_ed[USBNET_ENDPT_INTR] = -1;
    218   1.1    itojun 	for (i = 0; i < id->bNumEndpoints; i++) {
    219  1.61     skrll 		ed = usbd_interface2endpoint_descriptor(un->un_iface, i);
    220   1.1    itojun 		if (ed == NULL) {
    221  1.24      cube 			aprint_error_dev(self, "couldn't get endpoint %d\n", i);
    222  1.61     skrll 			return;
    223   1.1    itojun 		}
    224   1.1    itojun 		if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
    225   1.1    itojun 		    UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
    226  1.61     skrll 			un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress;
    227   1.1    itojun 		else if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
    228   1.1    itojun 			 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT)
    229  1.61     skrll 			un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress;
    230   1.1    itojun 		else if ((ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT &&
    231   1.1    itojun 			 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
    232  1.61     skrll 			un->un_ed[USBNET_ENDPT_INTR] = ed->bEndpointAddress;
    233   1.1    itojun 	}
    234   1.1    itojun 
    235  1.62     skrll 	if (un->un_ed[USBNET_ENDPT_RX] == 0 ||
    236  1.62     skrll 	    un->un_ed[USBNET_ENDPT_TX] == 0 ||
    237  1.62     skrll 	    un->un_ed[USBNET_ENDPT_INTR] == 0) {
    238  1.24      cube 		aprint_error_dev(self, "missing endpoint\n");
    239  1.61     skrll 		return;
    240   1.1    itojun 	}
    241   1.1    itojun 
    242  1.61     skrll 	/* Not supported yet. */
    243  1.61     skrll 	un->un_ed[USBNET_ENDPT_INTR] = 0;
    244  1.61     skrll 
    245  1.61     skrll 	un->un_cdata.uncd_rx_bufsz = un->un_cdata.uncd_tx_bufsz = UDAV_BUFSZ;
    246  1.61     skrll 
    247  1.61     skrll // 	/* reset the adapter */
    248  1.61     skrll // 	udav_reset(un);
    249   1.1    itojun 
    250  1.61     skrll 	usbnet_attach(un, "udavdet", UDAV_RX_LIST_CNT, UDAV_TX_LIST_CNT);
    251   1.1    itojun 
    252   1.1    itojun 	/* Get Ethernet Address */
    253  1.61     skrll 	usbnet_lock_mii(un);
    254  1.61     skrll 	err = udav_csr_read(sc, UDAV_PAR, un->un_eaddr, ETHER_ADDR_LEN);
    255  1.61     skrll 	usbnet_unlock_mii(un);
    256   1.1    itojun 	if (err) {
    257  1.24      cube 		aprint_error_dev(self, "read MAC address failed\n");
    258  1.61     skrll 		return;
    259   1.1    itojun 	}
    260   1.1    itojun 
    261  1.61     skrll 	bool have_mii = !ISSET(sc->sc_flags, UDAV_NO_PHY);
    262  1.61     skrll 	if (!have_mii)
    263  1.61     skrll 		un->un_link = 1;
    264   1.1    itojun 
    265   1.9       wiz 	/* initialize interface information */
    266  1.61     skrll 	usbnet_attach_ifp(un, have_mii,
    267  1.61     skrll 	    IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST, 0, 0);
    268  1.42  jakllsch 
    269  1.31    dyoung 	return;
    270   1.1    itojun }
    271   1.1    itojun 
    272   1.1    itojun #if 0
    273   1.1    itojun /* read memory */
    274  1.61     skrll static int
    275   1.1    itojun udav_mem_read(struct udav_softc *sc, int offset, void *buf, int len)
    276   1.1    itojun {
    277   1.1    itojun 	usb_device_request_t req;
    278   1.1    itojun 	usbd_status err;
    279   1.1    itojun 
    280   1.1    itojun 	if (sc == NULL)
    281  1.46     skrll 		return 0;
    282   1.1    itojun 
    283   1.1    itojun 	DPRINTFN(0x200,
    284  1.61     skrll 		("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    285   1.1    itojun 
    286  1.61     skrll 	if (un->un_dying)
    287  1.46     skrll 		return 0;
    288   1.1    itojun 
    289   1.1    itojun 	offset &= 0xffff;
    290   1.1    itojun 	len &= 0xff;
    291   1.1    itojun 
    292   1.1    itojun 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    293   1.1    itojun 	req.bRequest = UDAV_REQ_MEM_READ;
    294   1.1    itojun 	USETW(req.wValue, 0x0000);
    295   1.1    itojun 	USETW(req.wIndex, offset);
    296   1.1    itojun 	USETW(req.wLength, len);
    297   1.1    itojun 
    298  1.61     skrll 	err = usbd_do_request(un->un_udev, &req, buf);
    299   1.1    itojun 	if (err) {
    300   1.1    itojun 		DPRINTF(("%s: %s: read failed. off=%04x, err=%d\n",
    301  1.61     skrll 			 device_xname(un->un_dev), __func__, offset, err));
    302   1.1    itojun 	}
    303   1.1    itojun 
    304  1.46     skrll 	return err;
    305   1.1    itojun }
    306   1.1    itojun 
    307   1.1    itojun /* write memory */
    308  1.61     skrll static int
    309   1.1    itojun udav_mem_write(struct udav_softc *sc, int offset, void *buf, int len)
    310   1.1    itojun {
    311   1.1    itojun 	usb_device_request_t req;
    312   1.1    itojun 	usbd_status err;
    313   1.1    itojun 
    314   1.1    itojun 	if (sc == NULL)
    315  1.46     skrll 		return 0;
    316   1.1    itojun 
    317   1.1    itojun 	DPRINTFN(0x200,
    318  1.61     skrll 		("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    319   1.1    itojun 
    320  1.61     skrll 	if (un->un_dying)
    321  1.46     skrll 		return 0;
    322   1.1    itojun 
    323   1.1    itojun 	offset &= 0xffff;
    324   1.1    itojun 	len &= 0xff;
    325   1.1    itojun 
    326   1.1    itojun 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    327   1.1    itojun 	req.bRequest = UDAV_REQ_MEM_WRITE;
    328   1.1    itojun 	USETW(req.wValue, 0x0000);
    329   1.1    itojun 	USETW(req.wIndex, offset);
    330   1.1    itojun 	USETW(req.wLength, len);
    331   1.1    itojun 
    332  1.61     skrll 	err = usbd_do_request(un->un_udev, &req, buf);
    333   1.1    itojun 	if (err) {
    334   1.1    itojun 		DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
    335  1.61     skrll 			 device_xname(un->un_dev), __func__, offset, err));
    336   1.1    itojun 	}
    337   1.1    itojun 
    338  1.46     skrll 	return err;
    339   1.1    itojun }
    340   1.1    itojun 
    341   1.1    itojun /* write memory */
    342  1.61     skrll static int
    343   1.1    itojun udav_mem_write1(struct udav_softc *sc, int offset, unsigned char ch)
    344   1.1    itojun {
    345   1.1    itojun 	usb_device_request_t req;
    346   1.1    itojun 	usbd_status err;
    347   1.1    itojun 
    348   1.1    itojun 	if (sc == NULL)
    349  1.46     skrll 		return 0;
    350   1.1    itojun 
    351   1.1    itojun 	DPRINTFN(0x200,
    352  1.61     skrll 		("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    353   1.1    itojun 
    354  1.61     skrll 	if (un->un_dying)
    355  1.46     skrll 		return 0;
    356   1.1    itojun 
    357   1.1    itojun 	offset &= 0xffff;
    358   1.1    itojun 
    359   1.1    itojun 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    360   1.1    itojun 	req.bRequest = UDAV_REQ_MEM_WRITE1;
    361   1.1    itojun 	USETW(req.wValue, ch);
    362   1.1    itojun 	USETW(req.wIndex, offset);
    363   1.1    itojun 	USETW(req.wLength, 0x0000);
    364   1.1    itojun 
    365  1.61     skrll 	err = usbd_do_request(un->un_udev, &req, NULL);
    366   1.1    itojun 	if (err) {
    367   1.1    itojun 		DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
    368  1.61     skrll 			 device_xname(un->un_dev), __func__, offset, err));
    369   1.1    itojun 	}
    370   1.1    itojun 
    371  1.46     skrll 	return err;
    372   1.1    itojun }
    373   1.1    itojun #endif
    374   1.1    itojun 
    375   1.1    itojun /* read register(s) */
    376  1.61     skrll static int
    377   1.1    itojun udav_csr_read(struct udav_softc *sc, int offset, void *buf, int len)
    378   1.1    itojun {
    379  1.61     skrll 	struct usbnet * const un = &sc->sc_un;
    380   1.1    itojun 	usb_device_request_t req;
    381   1.1    itojun 	usbd_status err;
    382   1.1    itojun 
    383  1.61     skrll 	usbnet_isowned_mii(un);
    384  1.61     skrll 	KASSERT(!un->un_dying);
    385   1.1    itojun 
    386   1.1    itojun 	DPRINTFN(0x200,
    387  1.61     skrll 		("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    388   1.1    itojun 
    389   1.1    itojun 	offset &= 0xff;
    390   1.1    itojun 	len &= 0xff;
    391   1.1    itojun 
    392   1.1    itojun 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    393   1.1    itojun 	req.bRequest = UDAV_REQ_REG_READ;
    394   1.1    itojun 	USETW(req.wValue, 0x0000);
    395   1.1    itojun 	USETW(req.wIndex, offset);
    396   1.1    itojun 	USETW(req.wLength, len);
    397   1.1    itojun 
    398  1.61     skrll 	err = usbd_do_request(un->un_udev, &req, buf);
    399   1.1    itojun 	if (err) {
    400   1.1    itojun 		DPRINTF(("%s: %s: read failed. off=%04x, err=%d\n",
    401  1.61     skrll 			 device_xname(un->un_dev), __func__, offset, err));
    402   1.1    itojun 	}
    403   1.1    itojun 
    404  1.46     skrll 	return err;
    405   1.1    itojun }
    406   1.1    itojun 
    407   1.1    itojun /* write register(s) */
    408  1.61     skrll static int
    409   1.1    itojun udav_csr_write(struct udav_softc *sc, int offset, void *buf, int len)
    410   1.1    itojun {
    411  1.61     skrll 	struct usbnet * const un = &sc->sc_un;
    412   1.1    itojun 	usb_device_request_t req;
    413   1.1    itojun 	usbd_status err;
    414   1.1    itojun 
    415  1.61     skrll 	usbnet_isowned_mii(un);
    416  1.61     skrll 	KASSERT(!un->un_dying);
    417   1.1    itojun 
    418   1.1    itojun 	DPRINTFN(0x200,
    419  1.61     skrll 		("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    420   1.1    itojun 
    421   1.1    itojun 	offset &= 0xff;
    422   1.1    itojun 	len &= 0xff;
    423   1.1    itojun 
    424   1.1    itojun 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    425   1.1    itojun 	req.bRequest = UDAV_REQ_REG_WRITE;
    426   1.1    itojun 	USETW(req.wValue, 0x0000);
    427   1.1    itojun 	USETW(req.wIndex, offset);
    428   1.1    itojun 	USETW(req.wLength, len);
    429   1.1    itojun 
    430  1.61     skrll 	err = usbd_do_request(un->un_udev, &req, buf);
    431   1.1    itojun 	if (err) {
    432   1.1    itojun 		DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
    433  1.61     skrll 			 device_xname(un->un_dev), __func__, offset, err));
    434   1.1    itojun 	}
    435   1.1    itojun 
    436  1.46     skrll 	return err;
    437   1.1    itojun }
    438   1.1    itojun 
    439  1.61     skrll static int
    440   1.1    itojun udav_csr_read1(struct udav_softc *sc, int offset)
    441   1.1    itojun {
    442  1.61     skrll 	struct usbnet * const un = &sc->sc_un;
    443  1.46     skrll 	uint8_t val = 0;
    444   1.5     perry 
    445  1.61     skrll 	usbnet_isowned_mii(un);
    446   1.1    itojun 
    447   1.1    itojun 	DPRINTFN(0x200,
    448  1.61     skrll 		("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    449   1.1    itojun 
    450  1.61     skrll 	if (un->un_dying)
    451  1.46     skrll 		return 0;
    452   1.1    itojun 
    453  1.46     skrll 	return udav_csr_read(sc, offset, &val, 1) ? 0 : val;
    454   1.1    itojun }
    455   1.1    itojun 
    456   1.1    itojun /* write a register */
    457  1.61     skrll static int
    458   1.1    itojun udav_csr_write1(struct udav_softc *sc, int offset, unsigned char ch)
    459   1.1    itojun {
    460  1.61     skrll 	struct usbnet * const un = &sc->sc_un;
    461   1.1    itojun 	usb_device_request_t req;
    462   1.1    itojun 	usbd_status err;
    463   1.1    itojun 
    464  1.61     skrll 	usbnet_isowned_mii(un);
    465  1.61     skrll 	KASSERT(!un->un_dying);
    466   1.1    itojun 
    467   1.1    itojun 	DPRINTFN(0x200,
    468  1.61     skrll 		("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    469   1.1    itojun 
    470   1.1    itojun 	offset &= 0xff;
    471   1.1    itojun 
    472   1.1    itojun 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    473   1.1    itojun 	req.bRequest = UDAV_REQ_REG_WRITE1;
    474   1.1    itojun 	USETW(req.wValue, ch);
    475   1.1    itojun 	USETW(req.wIndex, offset);
    476   1.1    itojun 	USETW(req.wLength, 0x0000);
    477   1.1    itojun 
    478  1.61     skrll 	err = usbd_do_request(un->un_udev, &req, NULL);
    479   1.1    itojun 	if (err) {
    480   1.1    itojun 		DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
    481  1.61     skrll 			 device_xname(un->un_dev), __func__, offset, err));
    482   1.1    itojun 	}
    483   1.1    itojun 
    484  1.46     skrll 	return err;
    485   1.1    itojun }
    486   1.1    itojun 
    487  1.61     skrll static int
    488  1.61     skrll udav_init_locked(struct ifnet *ifp)
    489   1.1    itojun {
    490  1.61     skrll 	struct usbnet * const un = ifp->if_softc;
    491  1.61     skrll 	struct mii_data * const mii = usbnet_mii(un);
    492  1.61     skrll 	struct udav_softc *sc = usbnet_softc(un);
    493  1.20    dyoung 	uint8_t eaddr[ETHER_ADDR_LEN];
    494  1.61     skrll 	int rc;
    495  1.61     skrll 
    496  1.61     skrll 	DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    497   1.1    itojun 
    498  1.61     skrll 	usbnet_isowned(un);
    499   1.1    itojun 
    500  1.61     skrll 	if (un->un_dying)
    501  1.46     skrll 		return EIO;
    502   1.1    itojun 
    503  1.61     skrll 	/* Cancel pending I/O and free all TX/RX buffers */
    504  1.61     skrll 	if (ifp->if_flags & IFF_RUNNING)
    505  1.61     skrll 		usbnet_stop(un, ifp, 1);
    506   1.1    itojun 
    507  1.61     skrll 	usbnet_lock_mii_un_locked(un);
    508   1.1    itojun 
    509  1.20    dyoung 	memcpy(eaddr, CLLADDR(ifp->if_sadl), sizeof(eaddr));
    510   1.1    itojun 	udav_csr_write(sc, UDAV_PAR, eaddr, ETHER_ADDR_LEN);
    511   1.1    itojun 
    512   1.1    itojun 	/* Initialize network control register */
    513   1.1    itojun 	/*  Disable loopback  */
    514   1.1    itojun 	UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_LBK0 | UDAV_NCR_LBK1);
    515   1.1    itojun 
    516   1.1    itojun 	/* Initialize RX control register */
    517   1.1    itojun 	UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_DIS_LONG | UDAV_RCR_DIS_CRC);
    518   1.1    itojun 
    519   1.1    itojun 	/* If we want promiscuous mode, accept all physical frames. */
    520   1.1    itojun 	if (ifp->if_flags & IFF_PROMISC)
    521  1.58   msaitoh 		UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL | UDAV_RCR_PRMSC);
    522   1.1    itojun 	else
    523  1.58   msaitoh 		UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_ALL | UDAV_RCR_PRMSC);
    524   1.1    itojun 
    525   1.1    itojun 	/* Load the multicast filter */
    526  1.61     skrll 	udav_setiff_locked(un);
    527   1.1    itojun 
    528   1.1    itojun 	/* Enable RX */
    529   1.1    itojun 	UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_RXEN);
    530   1.1    itojun 
    531   1.1    itojun 	/* clear POWER_DOWN state of internal PHY */
    532   1.1    itojun 	UDAV_SETBIT(sc, UDAV_GPCR, UDAV_GPCR_GEP_CNTL0);
    533   1.1    itojun 	UDAV_CLRBIT(sc, UDAV_GPR, UDAV_GPR_GEPIO0);
    534   1.1    itojun 
    535  1.61     skrll 	usbnet_unlock_mii_un_locked(un);
    536   1.1    itojun 
    537  1.61     skrll 	rc = 0;
    538  1.61     skrll 	if (mii) {
    539  1.61     skrll 		if ((rc = mii_mediachg(mii)) == ENXIO)
    540  1.61     skrll 			rc = 0;
    541  1.46     skrll 	}
    542  1.46     skrll 
    543  1.61     skrll 	if (rc != 0)
    544  1.61     skrll 		return rc;
    545  1.46     skrll 
    546  1.61     skrll 	return usbnet_init_rx_tx(un, 0, USBD_FORCE_SHORT_XFER);
    547  1.61     skrll }
    548  1.46     skrll 
    549  1.61     skrll static int
    550  1.61     skrll udav_init(struct ifnet *ifp)
    551  1.61     skrll {
    552  1.61     skrll 	struct usbnet * const un = ifp->if_softc;
    553   1.1    itojun 
    554  1.61     skrll 	usbnet_lock(un);
    555  1.61     skrll 	int ret = udav_init_locked(ifp);
    556  1.61     skrll 	usbnet_unlock(un);
    557   1.1    itojun 
    558  1.61     skrll 	return ret;
    559   1.1    itojun }
    560   1.1    itojun 
    561  1.61     skrll static void
    562  1.61     skrll udav_reset(struct usbnet *un)
    563   1.1    itojun {
    564  1.61     skrll     	usbnet_isowned(un);
    565  1.61     skrll 
    566  1.61     skrll 	if (un->un_dying)
    567  1.61     skrll 		return;
    568  1.61     skrll 
    569  1.61     skrll 	DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    570  1.61     skrll 
    571  1.61     skrll 	udav_chip_init(un);
    572  1.61     skrll }
    573   1.1    itojun 
    574  1.61     skrll static void
    575  1.61     skrll udav_chip_init(struct usbnet *un)
    576  1.61     skrll {
    577  1.61     skrll     	struct udav_softc * const sc = usbnet_softc(un);
    578   1.1    itojun 
    579  1.61     skrll 	usbnet_lock_mii_un_locked(un);
    580   1.1    itojun 
    581   1.1    itojun 	/* Select PHY */
    582   1.1    itojun #if 1
    583   1.1    itojun 	/*
    584   1.1    itojun 	 * XXX: force select internal phy.
    585   1.1    itojun 	 *	external phy routines are not tested.
    586   1.1    itojun 	 */
    587   1.1    itojun 	UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
    588   1.1    itojun #else
    589   1.1    itojun 	if (sc->sc_flags & UDAV_EXT_PHY) {
    590   1.1    itojun 		UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
    591   1.1    itojun 	} else {
    592   1.1    itojun 		UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
    593   1.1    itojun 	}
    594   1.1    itojun #endif
    595   1.1    itojun 
    596   1.1    itojun 	UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_RST);
    597   1.1    itojun 
    598  1.61     skrll 	for (int i = 0; i < UDAV_TX_TIMEOUT; i++) {
    599   1.1    itojun 		if (!(udav_csr_read1(sc, UDAV_NCR) & UDAV_NCR_RST))
    600   1.1    itojun 			break;
    601   1.1    itojun 		delay(10);	/* XXX */
    602   1.1    itojun 	}
    603   1.1    itojun 	delay(10000);		/* XXX */
    604   1.1    itojun 
    605  1.61     skrll 	usbnet_unlock_mii_un_locked(un);
    606   1.1    itojun }
    607   1.1    itojun 
    608   1.1    itojun #define UDAV_BITS	6
    609   1.1    itojun 
    610   1.1    itojun #define UDAV_CALCHASH(addr) \
    611   1.1    itojun 	(ether_crc32_le((addr), ETHER_ADDR_LEN) & ((1 << UDAV_BITS) - 1))
    612   1.1    itojun 
    613  1.61     skrll static void
    614  1.61     skrll udav_setiff_locked(struct usbnet *un)
    615   1.1    itojun {
    616  1.61     skrll 	struct udav_softc *sc = usbnet_softc(un);
    617  1.61     skrll 	struct ethercom *ec = usbnet_ec(un);
    618  1.61     skrll 	struct ifnet * const ifp = usbnet_ifp(un);
    619   1.1    itojun 	struct ether_multi *enm;
    620   1.1    itojun 	struct ether_multistep step;
    621  1.46     skrll 	uint8_t hashes[8];
    622   1.1    itojun 	int h = 0;
    623   1.1    itojun 
    624  1.61     skrll 	DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    625  1.61     skrll 
    626  1.61     skrll 	usbnet_isowned_mii(un);
    627   1.1    itojun 
    628  1.61     skrll 	if (un->un_dying)
    629   1.1    itojun 		return;
    630   1.1    itojun 
    631  1.42  jakllsch 	if (ISSET(sc->sc_flags, UDAV_NO_PHY)) {
    632  1.42  jakllsch 		UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL);
    633  1.42  jakllsch 		UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_PRMSC);
    634  1.42  jakllsch 		return;
    635  1.42  jakllsch 	}
    636  1.42  jakllsch 
    637   1.1    itojun 	if (ifp->if_flags & IFF_PROMISC) {
    638  1.58   msaitoh 		UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL | UDAV_RCR_PRMSC);
    639   1.1    itojun 		return;
    640   1.1    itojun 	} else if (ifp->if_flags & IFF_ALLMULTI) {
    641  1.59   msaitoh allmulti:
    642   1.1    itojun 		ifp->if_flags |= IFF_ALLMULTI;
    643   1.1    itojun 		UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL);
    644   1.1    itojun 		UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_PRMSC);
    645   1.1    itojun 		return;
    646   1.1    itojun 	}
    647   1.1    itojun 
    648   1.1    itojun 	/* first, zot all the existing hash bits */
    649   1.1    itojun 	memset(hashes, 0x00, sizeof(hashes));
    650   1.1    itojun 	hashes[7] |= 0x80;	/* broadcast address */
    651   1.1    itojun 	udav_csr_write(sc, UDAV_MAR, hashes, sizeof(hashes));
    652   1.1    itojun 
    653   1.1    itojun 	/* now program new ones */
    654  1.59   msaitoh 	ETHER_LOCK(ec);
    655  1.59   msaitoh 	ETHER_FIRST_MULTI(step, ec, enm);
    656   1.1    itojun 	while (enm != NULL) {
    657   1.1    itojun 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    658  1.59   msaitoh 		    ETHER_ADDR_LEN) != 0) {
    659  1.59   msaitoh 			ETHER_UNLOCK(ec);
    660   1.1    itojun 			goto allmulti;
    661  1.59   msaitoh 		}
    662   1.1    itojun 
    663   1.1    itojun 		h = UDAV_CALCHASH(enm->enm_addrlo);
    664   1.1    itojun 		hashes[h>>3] |= 1 << (h & 0x7);
    665   1.1    itojun 		ETHER_NEXT_MULTI(step, enm);
    666   1.1    itojun 	}
    667  1.59   msaitoh 	ETHER_UNLOCK(ec);
    668   1.1    itojun 
    669   1.1    itojun 	/* disable all multicast */
    670   1.1    itojun 	ifp->if_flags &= ~IFF_ALLMULTI;
    671   1.1    itojun 	UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_ALL);
    672   1.1    itojun 
    673   1.1    itojun 	/* write hash value to the register */
    674   1.1    itojun 	udav_csr_write(sc, UDAV_MAR, hashes, sizeof(hashes));
    675   1.1    itojun }
    676   1.1    itojun 
    677  1.61     skrll static void
    678  1.61     skrll udav_setiff(struct usbnet *un)
    679   1.1    itojun {
    680  1.61     skrll 	usbnet_lock_mii(un);
    681  1.61     skrll 	udav_setiff_locked(un);
    682  1.61     skrll 	usbnet_unlock_mii(un);
    683   1.1    itojun }
    684   1.1    itojun 
    685   1.1    itojun 
    686  1.61     skrll static unsigned
    687  1.61     skrll udav_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c)
    688   1.1    itojun {
    689  1.61     skrll 	int total_len;
    690  1.61     skrll 	uint8_t *buf = c->unc_buf;
    691   1.1    itojun 
    692  1.61     skrll 	usbnet_isowned_tx(un);
    693   1.1    itojun 
    694  1.61     skrll 	DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    695   1.1    itojun 
    696  1.61     skrll 	if (m->m_pkthdr.len > UDAV_BUFSZ - 2)
    697  1.61     skrll 		return 0;
    698   1.1    itojun 
    699   1.1    itojun 	/* Copy the mbuf data into a contiguous buffer */
    700  1.61     skrll 	m_copydata(m, 0, m->m_pkthdr.len, buf + 2);
    701   1.1    itojun 	total_len = m->m_pkthdr.len;
    702   1.1    itojun 	if (total_len < UDAV_MIN_FRAME_LEN) {
    703  1.61     skrll 		memset(buf + 2 + total_len, 0,
    704   1.1    itojun 		    UDAV_MIN_FRAME_LEN - total_len);
    705   1.1    itojun 		total_len = UDAV_MIN_FRAME_LEN;
    706   1.1    itojun 	}
    707   1.1    itojun 
    708   1.1    itojun 	/* Frame length is specified in the first 2bytes of the buffer */
    709  1.61     skrll 	buf[0] = (uint8_t)total_len;
    710  1.61     skrll 	buf[1] = (uint8_t)(total_len >> 8);
    711   1.1    itojun 	total_len += 2;
    712   1.1    itojun 
    713  1.61     skrll 	DPRINTF(("%s: %s: send %d bytes\n", device_xname(un->un_dev),
    714  1.61     skrll 	    __func__, total_len));
    715   1.1    itojun 
    716  1.61     skrll 	return total_len;
    717   1.1    itojun }
    718   1.1    itojun 
    719  1.61     skrll static void
    720  1.61     skrll udav_rxeof_loop(struct usbnet *un, struct usbd_xfer *xfer,
    721  1.61     skrll 	        struct usbnet_chain *c, uint32_t total_len)
    722   1.1    itojun {
    723  1.61     skrll 	struct ifnet *ifp = usbnet_ifp(un);
    724  1.61     skrll 	uint8_t *buf = c->unc_buf;
    725  1.61     skrll 	uint16_t pkt_len;
    726  1.61     skrll 	uint8_t pktstat;
    727   1.1    itojun 
    728  1.61     skrll 	DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    729   1.1    itojun 
    730  1.61     skrll 	/* first byte in received data */
    731  1.61     skrll 	pktstat = *buf;
    732  1.61     skrll 	total_len -= sizeof(pktstat);
    733  1.61     skrll 	buf += sizeof(pktstat);
    734   1.1    itojun 
    735  1.61     skrll 	DPRINTF(("%s: RX Status: 0x%02x\n", device_xname(un->un_dev), pktstat));
    736   1.1    itojun 
    737  1.61     skrll 	pkt_len = UGETW(buf);
    738  1.61     skrll  	total_len -= sizeof(pkt_len);
    739  1.61     skrll 	buf += sizeof(pkt_len);
    740   1.1    itojun 
    741  1.61     skrll 	DPRINTF(("%s: RX Length: 0x%02x\n", device_xname(un->un_dev), pkt_len));
    742   1.1    itojun 
    743  1.61     skrll 	if (pktstat & UDAV_RSR_LCS) {
    744   1.1    itojun 		ifp->if_collisions++;
    745  1.61     skrll 		return;
    746   1.1    itojun 	}
    747   1.1    itojun 
    748  1.61     skrll 	if (pkt_len < sizeof(struct ether_header) ||
    749  1.61     skrll 	    pkt_len > total_len ||
    750  1.61     skrll 	    (pktstat & UDAV_RSR_ERR)) {
    751   1.1    itojun 		ifp->if_ierrors++;
    752  1.61     skrll 		return;
    753   1.1    itojun 	}
    754   1.1    itojun 
    755  1.61     skrll 	pkt_len -= ETHER_CRC_LEN;
    756   1.1    itojun 
    757  1.61     skrll 	DPRINTF(("%s: Rx deliver: 0x%02x\n", device_xname(un->un_dev), pkt_len));
    758   1.1    itojun 
    759  1.61     skrll 	usbnet_enqueue(un, buf, pkt_len, 0, 0, 0);
    760   1.1    itojun }
    761   1.1    itojun 
    762  1.61     skrll static int
    763  1.61     skrll udav_ioctl_cb(struct ifnet *ifp, u_long cmd, void *data)
    764   1.1    itojun {
    765  1.61     skrll 	struct usbnet * const un = ifp->if_softc;
    766   1.1    itojun 
    767  1.61     skrll 	switch (cmd) {
    768  1.61     skrll 	case SIOCADDMULTI:
    769  1.61     skrll 	case SIOCDELMULTI:
    770  1.61     skrll 		udav_setiff(un);
    771  1.61     skrll 		break;
    772  1.61     skrll 	default:
    773  1.61     skrll 		break;
    774   1.1    itojun 	}
    775   1.1    itojun 
    776   1.1    itojun 
    777  1.61     skrll 	return 0;
    778   1.1    itojun }
    779   1.1    itojun 
    780   1.1    itojun /* Stop the adapter and free any mbufs allocated to the RX and TX lists. */
    781  1.61     skrll static void
    782  1.61     skrll udav_stop_cb(struct ifnet *ifp, int disable)
    783   1.1    itojun {
    784  1.61     skrll 	struct usbnet * const un = ifp->if_softc;
    785   1.1    itojun 
    786  1.61     skrll 	DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    787   1.1    itojun 
    788  1.61     skrll 	udav_reset(un);
    789   1.1    itojun }
    790   1.1    itojun 
    791  1.61     skrll static usbd_status
    792  1.61     skrll udav_mii_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val)
    793   1.1    itojun {
    794  1.61     skrll 	struct udav_softc *sc = usbnet_softc(un);
    795  1.56   msaitoh 	uint8_t data[2];
    796   1.1    itojun 
    797  1.61     skrll 	usbnet_isowned_mii(un);
    798   1.1    itojun 
    799   1.1    itojun 	DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x\n",
    800  1.61     skrll 		 device_xname(un->un_dev), __func__, phy, reg));
    801   1.1    itojun 
    802  1.61     skrll 	if (un->un_dying) {
    803   1.1    itojun #ifdef DIAGNOSTIC
    804  1.61     skrll 		printf("%s: %s: dying\n", device_xname(un->un_dev),
    805   1.1    itojun 		       __func__);
    806   1.1    itojun #endif
    807  1.61     skrll 		return USBD_INVAL;
    808   1.1    itojun 	}
    809   1.1    itojun 
    810   1.1    itojun 	/* XXX: one PHY only for the internal PHY */
    811   1.1    itojun 	if (phy != 0) {
    812   1.1    itojun 		DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
    813  1.61     skrll 			 device_xname(un->un_dev), __func__, phy));
    814  1.61     skrll 		return USBD_INVAL;
    815   1.1    itojun 	}
    816   1.1    itojun 
    817   1.1    itojun 	/* select internal PHY and set PHY register address */
    818   1.1    itojun 	udav_csr_write1(sc, UDAV_EPAR,
    819   1.1    itojun 			UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
    820   1.1    itojun 
    821   1.1    itojun 	/* select PHY operation and start read command */
    822   1.1    itojun 	udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRR);
    823   1.1    itojun 
    824   1.1    itojun 	/* XXX: should be wait? */
    825   1.1    itojun 
    826   1.1    itojun 	/* end read command */
    827   1.1    itojun 	UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRR);
    828   1.1    itojun 
    829   1.1    itojun 	/* retrieve the result from data registers */
    830  1.56   msaitoh 	udav_csr_read(sc, UDAV_EPDRL, data, 2);
    831   1.1    itojun 
    832  1.56   msaitoh 	*val = data[0] | (data[1] << 8);
    833   1.1    itojun 
    834  1.56   msaitoh 	DPRINTFN(0xff, ("%s: %s: phy=%d reg=0x%04x => 0x%04hx\n",
    835  1.61     skrll 		device_xname(un->un_dev), __func__, phy, reg, *val));
    836   1.1    itojun 
    837  1.61     skrll 	return USBD_NORMAL_COMPLETION;
    838   1.1    itojun }
    839   1.1    itojun 
    840  1.61     skrll static usbd_status
    841  1.61     skrll udav_mii_write_reg(struct usbnet *un, int phy, int reg, uint16_t val)
    842   1.1    itojun {
    843  1.61     skrll 	struct udav_softc *sc = usbnet_softc(un);
    844  1.56   msaitoh 	uint8_t data[2];
    845   1.1    itojun 
    846  1.61     skrll 	usbnet_isowned_mii(un);
    847   1.1    itojun 
    848  1.56   msaitoh 	DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x val=0x%04hx\n",
    849  1.61     skrll 		 device_xname(un->un_dev), __func__, phy, reg, val));
    850   1.1    itojun 
    851  1.61     skrll 	if (un->un_dying) {
    852   1.1    itojun #ifdef DIAGNOSTIC
    853  1.61     skrll 		printf("%s: %s: dying\n", device_xname(un->un_dev),
    854   1.1    itojun 		       __func__);
    855   1.1    itojun #endif
    856  1.56   msaitoh 		return -1;
    857   1.1    itojun 	}
    858   1.1    itojun 
    859   1.1    itojun 	/* XXX: one PHY only for the internal PHY */
    860   1.1    itojun 	if (phy != 0) {
    861   1.1    itojun 		DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
    862  1.61     skrll 			 device_xname(un->un_dev), __func__, phy));
    863  1.56   msaitoh 		return -1;
    864   1.1    itojun 	}
    865   1.1    itojun 
    866   1.1    itojun 	/* select internal PHY and set PHY register address */
    867   1.1    itojun 	udav_csr_write1(sc, UDAV_EPAR,
    868   1.1    itojun 			UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
    869   1.1    itojun 
    870   1.1    itojun 	/* put the value to the data registers */
    871  1.56   msaitoh 	data[0] = val & 0xff;
    872  1.56   msaitoh 	data[1] = (val >> 8) & 0xff;
    873  1.56   msaitoh 	udav_csr_write(sc, UDAV_EPDRL, data, 2);
    874   1.1    itojun 
    875   1.1    itojun 	/* select PHY operation and start write command */
    876   1.1    itojun 	udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRW);
    877   1.1    itojun 
    878   1.1    itojun 	/* XXX: should be wait? */
    879   1.1    itojun 
    880   1.1    itojun 	/* end write command */
    881   1.1    itojun 	UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRW);
    882   1.1    itojun 
    883  1.61     skrll 	return USBD_NORMAL_COMPLETION;
    884   1.1    itojun }
    885   1.1    itojun 
    886  1.61     skrll static void
    887  1.61     skrll udav_mii_statchg(struct ifnet *ifp)
    888   1.1    itojun {
    889  1.61     skrll 	struct usbnet * const un = ifp->if_softc;
    890  1.61     skrll 	struct mii_data * const mii = usbnet_mii(un);
    891  1.61     skrll 
    892  1.61     skrll 	DPRINTF(("%s: %s: enter\n", ifp->if_xname, __func__));
    893   1.1    itojun 
    894  1.61     skrll 	if (un->un_dying)
    895   1.1    itojun 		return;
    896   1.1    itojun 
    897  1.61     skrll 	un->un_link = false;
    898  1.61     skrll 	if (mii->mii_media_status & IFM_ACTIVE &&
    899  1.61     skrll 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
    900  1.61     skrll 		DPRINTF(("%s: %s: got link\n",
    901  1.61     skrll 			 device_xname(un->un_dev), __func__));
    902  1.61     skrll 		un->un_link = true;
    903  1.61     skrll 	}
    904  1.61     skrll }
    905  1.61     skrll 
    906  1.61     skrll MODULE(MODULE_CLASS_DRIVER, if_udav, "usbnet");
    907  1.61     skrll 
    908  1.61     skrll #ifdef _MODULE
    909  1.61     skrll #include "ioconf.c"
    910  1.61     skrll #endif
    911  1.61     skrll 
    912  1.61     skrll static int
    913  1.61     skrll if_udav_modcmd(modcmd_t cmd, void *aux)
    914  1.61     skrll {
    915  1.61     skrll 	int error = 0;
    916  1.61     skrll 
    917  1.61     skrll 	switch (cmd) {
    918  1.61     skrll 	case MODULE_CMD_INIT:
    919  1.61     skrll #ifdef _MODULE
    920  1.61     skrll 		error = config_init_component(cfdriver_ioconf_udav,
    921  1.61     skrll 		    cfattach_ioconf_udav, cfdata_ioconf_udav);
    922  1.61     skrll #endif
    923  1.61     skrll 		return error;
    924  1.61     skrll 	case MODULE_CMD_FINI:
    925  1.61     skrll #ifdef _MODULE
    926  1.61     skrll 		error = config_fini_component(cfdriver_ioconf_udav,
    927  1.61     skrll 		    cfattach_ioconf_udav, cfdata_ioconf_udav);
    928   1.1    itojun #endif
    929  1.61     skrll 		return error;
    930  1.61     skrll 	default:
    931  1.61     skrll 		return ENOTTY;
    932  1.61     skrll 	}
    933   1.1    itojun }
    934