Home | History | Annotate | Line # | Download | only in usb
if_udav.c revision 1.86
      1  1.86  riastrad /*	$NetBSD: if_udav.c,v 1.86 2022/03/03 05:53:04 riastradh 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.86  riastrad __KERNEL_RCSID(0, "$NetBSD: if_udav.c,v 1.86 2022/03/03 05:53:04 riastradh 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 
     56  1.61     skrll #include <dev/usb/usbnet.h>
     57   1.1    itojun #include <dev/usb/if_udavreg.h>
     58   1.1    itojun 
     59   1.1    itojun /* Function declarations */
     60  1.73      maxv static int	udav_match(device_t, cfdata_t, void *);
     61  1.73      maxv static void	udav_attach(device_t, device_t, void *);
     62  1.57       mrg 
     63  1.64       mrg CFATTACH_DECL_NEW(udav, sizeof(struct usbnet), udav_match, udav_attach,
     64  1.61     skrll     usbnet_detach, usbnet_activate);
     65  1.61     skrll 
     66  1.61     skrll static void udav_chip_init(struct usbnet *);
     67   1.1    itojun 
     68  1.77   thorpej static unsigned udav_uno_tx_prepare(struct usbnet *, struct mbuf *,
     69  1.77   thorpej 				    struct usbnet_chain *);
     70  1.77   thorpej static void udav_uno_rx_loop(struct usbnet *, struct usbnet_chain *, uint32_t);
     71  1.77   thorpej static void udav_uno_stop(struct ifnet *, int);
     72  1.81  riastrad static void udav_uno_mcast(struct ifnet *);
     73  1.77   thorpej static int udav_uno_mii_read_reg(struct usbnet *, int, int, uint16_t *);
     74  1.77   thorpej static int udav_uno_mii_write_reg(struct usbnet *, int, int, uint16_t);
     75  1.77   thorpej static void udav_uno_mii_statchg(struct ifnet *);
     76  1.77   thorpej static int udav_uno_init(struct ifnet *);
     77  1.61     skrll static void udav_reset(struct usbnet *);
     78  1.61     skrll 
     79  1.64       mrg static int udav_csr_read(struct usbnet *, int, void *, int);
     80  1.64       mrg static int udav_csr_write(struct usbnet *, int, void *, int);
     81  1.64       mrg static int udav_csr_read1(struct usbnet *, int);
     82  1.64       mrg static int udav_csr_write1(struct usbnet *, int, unsigned char);
     83   1.1    itojun 
     84   1.1    itojun #if 0
     85  1.64       mrg static int udav_mem_read(struct usbnet *, int, void *, int);
     86  1.64       mrg static int udav_mem_write(struct usbnet *, int, void *, int);
     87  1.64       mrg static int udav_mem_write1(struct usbnet *, int, unsigned char);
     88   1.1    itojun #endif
     89   1.1    itojun 
     90   1.1    itojun /* Macros */
     91   1.1    itojun #ifdef UDAV_DEBUG
     92  1.31    dyoung #define DPRINTF(x)	if (udavdebug) printf x
     93  1.58   msaitoh #define DPRINTFN(n, x)	if (udavdebug >= (n)) printf x
     94  1.69       mrg int udavdebug = 0;
     95   1.1    itojun #else
     96   1.1    itojun #define DPRINTF(x)
     97  1.58   msaitoh #define DPRINTFN(n, x)
     98   1.1    itojun #endif
     99   1.1    itojun 
    100  1.64       mrg #define	UDAV_SETBIT(un, reg, x)	\
    101  1.64       mrg 	udav_csr_write1(un, reg, udav_csr_read1(un, reg) | (x))
    102   1.1    itojun 
    103  1.64       mrg #define	UDAV_CLRBIT(un, reg, x)	\
    104  1.64       mrg 	udav_csr_write1(un, reg, udav_csr_read1(un, reg) & ~(x))
    105   1.1    itojun 
    106   1.1    itojun static const struct udav_type {
    107   1.1    itojun 	struct usb_devno udav_dev;
    108  1.46     skrll 	uint16_t udav_flags;
    109   1.1    itojun #define UDAV_EXT_PHY	0x0001
    110  1.42  jakllsch #define UDAV_NO_PHY	0x0002
    111   1.1    itojun } udav_devs [] = {
    112   1.1    itojun 	/* Corega USB-TXC */
    113   1.1    itojun 	{{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXC }, 0},
    114  1.18   xtraeme 	/* ShanTou ST268 USB NIC */
    115  1.18   xtraeme 	{{ USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ST268_USB_NIC }, 0},
    116  1.22  drochner 	/* ShanTou ADM8515 */
    117  1.22  drochner 	{{ USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ADM8515 }, 0},
    118  1.30  jmcneill 	/* SUNRISING SR9600 */
    119  1.30  jmcneill 	{{ USB_VENDOR_SUNRISING, USB_PRODUCT_SUNRISING_SR9600 }, 0 },
    120  1.42  jakllsch 	/* SUNRISING QF9700 */
    121  1.42  jakllsch 	{{ USB_VENDOR_SUNRISING, USB_PRODUCT_SUNRISING_QF9700 }, UDAV_NO_PHY },
    122  1.35   mbalmer 	/* QUAN DM9601 */
    123  1.35   mbalmer 	{{USB_VENDOR_QUAN, USB_PRODUCT_QUAN_DM9601 }, 0},
    124   1.1    itojun #if 0
    125   1.1    itojun 	/* DAVICOM DM9601 Generic? */
    126   1.1    itojun 	/*  XXX: The following ids was obtained from the data sheet. */
    127   1.1    itojun 	{{ 0x0a46, 0x9601 }, 0},
    128   1.1    itojun #endif
    129   1.1    itojun };
    130   1.7  christos #define udav_lookup(v, p) ((const struct udav_type *)usb_lookup(udav_devs, v, p))
    131   1.1    itojun 
    132  1.73      maxv static const struct usbnet_ops udav_ops = {
    133  1.77   thorpej 	.uno_stop = udav_uno_stop,
    134  1.81  riastrad 	.uno_mcast = udav_uno_mcast,
    135  1.77   thorpej 	.uno_read_reg = udav_uno_mii_read_reg,
    136  1.77   thorpej 	.uno_write_reg = udav_uno_mii_write_reg,
    137  1.77   thorpej 	.uno_statchg = udav_uno_mii_statchg,
    138  1.77   thorpej 	.uno_tx_prepare = udav_uno_tx_prepare,
    139  1.77   thorpej 	.uno_rx_loop = udav_uno_rx_loop,
    140  1.77   thorpej 	.uno_init = udav_uno_init,
    141  1.63       mrg };
    142   1.1    itojun 
    143   1.1    itojun /* Probe */
    144  1.73      maxv static int
    145  1.31    dyoung udav_match(device_t parent, cfdata_t match, void *aux)
    146   1.1    itojun {
    147  1.31    dyoung 	struct usb_attach_arg *uaa = aux;
    148   1.1    itojun 
    149  1.46     skrll 	return udav_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
    150  1.46     skrll 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    151   1.1    itojun }
    152   1.1    itojun 
    153   1.1    itojun /* Attach */
    154  1.73      maxv static void
    155  1.31    dyoung udav_attach(device_t parent, device_t self, void *aux)
    156   1.1    itojun {
    157  1.71       mrg 	USBNET_MII_DECL_DEFAULT(unm);
    158  1.70       mrg 	struct usbnet_mii *unmp;
    159  1.64       mrg 	struct usbnet * const un = device_private(self);
    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.26    plunky 	aprint_naive("\n");
    170  1.26    plunky 	aprint_normal("\n");
    171   1.6  augustss 	devinfop = usbd_devinfo_alloc(dev, 0);
    172  1.24      cube 	aprint_normal_dev(self, "%s\n", devinfop);
    173   1.6  augustss 	usbd_devinfo_free(devinfop);
    174   1.1    itojun 
    175  1.61     skrll 	un->un_dev = self;
    176  1.61     skrll 	un->un_udev = dev;
    177  1.64       mrg 	un->un_sc = un;
    178  1.63       mrg 	un->un_ops = &udav_ops;
    179  1.65       mrg 	un->un_rx_xfer_flags = USBD_SHORT_XFER_OK;
    180  1.65       mrg 	un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER;
    181  1.65       mrg 	un->un_rx_list_cnt = UDAV_RX_LIST_CNT;
    182  1.65       mrg 	un->un_tx_list_cnt = UDAV_TX_LIST_CNT;
    183  1.65       mrg 	un->un_rx_bufsz = UDAV_BUFSZ;
    184  1.65       mrg 	un->un_tx_bufsz = UDAV_BUFSZ;
    185  1.61     skrll 
    186   1.1    itojun 	/* Move the device into the configured state. */
    187  1.22  drochner 	err = usbd_set_config_no(dev, UDAV_CONFIG_NO, 1); /* idx 0 */
    188   1.1    itojun 	if (err) {
    189  1.39     skrll 		aprint_error_dev(self, "failed to set configuration"
    190  1.39     skrll 		    ", err=%s\n", usbd_errstr(err));
    191  1.61     skrll 		return;
    192   1.1    itojun 	}
    193   1.1    itojun 
    194   1.1    itojun 	/* get control interface */
    195   1.1    itojun 	err = usbd_device2interface_handle(dev, UDAV_IFACE_INDEX, &iface);
    196   1.1    itojun 	if (err) {
    197  1.24      cube 		aprint_error_dev(self, "failed to get interface, err=%s\n",
    198   1.1    itojun 		       usbd_errstr(err));
    199  1.61     skrll 		return;
    200   1.1    itojun 	}
    201   1.1    itojun 
    202  1.61     skrll 	un->un_iface = iface;
    203  1.64       mrg 	un->un_flags = udav_lookup(uaa->uaa_vendor,
    204  1.48   msaitoh 	    uaa->uaa_product)->udav_flags;
    205   1.1    itojun 
    206   1.1    itojun 	/* get interface descriptor */
    207  1.61     skrll 	id = usbd_get_interface_descriptor(un->un_iface);
    208   1.1    itojun 
    209   1.1    itojun 	/* find endpoints */
    210  1.61     skrll 	un->un_ed[USBNET_ENDPT_RX] = un->un_ed[USBNET_ENDPT_TX] =
    211  1.61     skrll 	    un->un_ed[USBNET_ENDPT_INTR] = -1;
    212   1.1    itojun 	for (i = 0; i < id->bNumEndpoints; i++) {
    213  1.61     skrll 		ed = usbd_interface2endpoint_descriptor(un->un_iface, i);
    214   1.1    itojun 		if (ed == NULL) {
    215  1.24      cube 			aprint_error_dev(self, "couldn't get endpoint %d\n", i);
    216  1.61     skrll 			return;
    217   1.1    itojun 		}
    218   1.1    itojun 		if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
    219   1.1    itojun 		    UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
    220  1.61     skrll 			un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress;
    221   1.1    itojun 		else if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
    222   1.1    itojun 			 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT)
    223  1.61     skrll 			un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress;
    224   1.1    itojun 		else if ((ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT &&
    225   1.1    itojun 			 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
    226  1.61     skrll 			un->un_ed[USBNET_ENDPT_INTR] = ed->bEndpointAddress;
    227   1.1    itojun 	}
    228   1.1    itojun 
    229  1.62     skrll 	if (un->un_ed[USBNET_ENDPT_RX] == 0 ||
    230  1.62     skrll 	    un->un_ed[USBNET_ENDPT_TX] == 0 ||
    231  1.62     skrll 	    un->un_ed[USBNET_ENDPT_INTR] == 0) {
    232  1.24      cube 		aprint_error_dev(self, "missing endpoint\n");
    233  1.61     skrll 		return;
    234   1.1    itojun 	}
    235   1.1    itojun 
    236  1.61     skrll 	/* Not supported yet. */
    237  1.61     skrll 	un->un_ed[USBNET_ENDPT_INTR] = 0;
    238  1.61     skrll 
    239  1.78     skrll 	usbnet_attach(un, "udavdet");
    240  1.78     skrll 
    241  1.77   thorpej 	usbnet_lock_core(un);
    242  1.77   thorpej 
    243  1.61     skrll // 	/* reset the adapter */
    244  1.61     skrll // 	udav_reset(un);
    245   1.1    itojun 
    246   1.1    itojun 	/* Get Ethernet Address */
    247  1.64       mrg 	err = udav_csr_read(un, UDAV_PAR, un->un_eaddr, ETHER_ADDR_LEN);
    248  1.77   thorpej 	usbnet_unlock_core(un);
    249   1.1    itojun 	if (err) {
    250  1.24      cube 		aprint_error_dev(self, "read MAC address failed\n");
    251  1.61     skrll 		return;
    252   1.1    itojun 	}
    253   1.1    itojun 
    254  1.70       mrg 	if (ISSET(un->un_flags, UDAV_NO_PHY))
    255  1.70       mrg 		unmp = NULL;
    256  1.70       mrg 	else
    257  1.70       mrg 		unmp = &unm;
    258   1.1    itojun 
    259   1.9       wiz 	/* initialize interface information */
    260  1.70       mrg 	usbnet_attach_ifp(un, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST,
    261  1.70       mrg 	    0, unmp);
    262  1.42  jakllsch 
    263  1.31    dyoung 	return;
    264   1.1    itojun }
    265   1.1    itojun 
    266   1.1    itojun #if 0
    267   1.1    itojun /* read memory */
    268  1.61     skrll static int
    269  1.64       mrg udav_mem_read(struct usbnet *un, int offset, void *buf, int len)
    270   1.1    itojun {
    271   1.1    itojun 	usb_device_request_t req;
    272   1.1    itojun 	usbd_status err;
    273   1.1    itojun 
    274   1.1    itojun 	DPRINTFN(0x200,
    275  1.61     skrll 		("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    276   1.1    itojun 
    277  1.63       mrg 	if (usbnet_isdying(un))
    278  1.46     skrll 		return 0;
    279   1.1    itojun 
    280   1.1    itojun 	offset &= 0xffff;
    281   1.1    itojun 	len &= 0xff;
    282   1.1    itojun 
    283   1.1    itojun 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    284   1.1    itojun 	req.bRequest = UDAV_REQ_MEM_READ;
    285   1.1    itojun 	USETW(req.wValue, 0x0000);
    286   1.1    itojun 	USETW(req.wIndex, offset);
    287   1.1    itojun 	USETW(req.wLength, len);
    288   1.1    itojun 
    289  1.61     skrll 	err = usbd_do_request(un->un_udev, &req, buf);
    290   1.1    itojun 	if (err) {
    291   1.1    itojun 		DPRINTF(("%s: %s: read failed. off=%04x, err=%d\n",
    292  1.61     skrll 			 device_xname(un->un_dev), __func__, offset, err));
    293   1.1    itojun 	}
    294   1.1    itojun 
    295  1.46     skrll 	return err;
    296   1.1    itojun }
    297   1.1    itojun 
    298   1.1    itojun /* write memory */
    299  1.61     skrll static int
    300  1.64       mrg udav_mem_write(struct usbnet *un, int offset, void *buf, int len)
    301   1.1    itojun {
    302   1.1    itojun 	usb_device_request_t req;
    303   1.1    itojun 	usbd_status err;
    304   1.1    itojun 
    305   1.1    itojun 	DPRINTFN(0x200,
    306  1.61     skrll 		("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    307   1.1    itojun 
    308  1.63       mrg 	if (usbnet_isdying(un))
    309  1.46     skrll 		return 0;
    310   1.1    itojun 
    311   1.1    itojun 	offset &= 0xffff;
    312   1.1    itojun 	len &= 0xff;
    313   1.1    itojun 
    314   1.1    itojun 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    315   1.1    itojun 	req.bRequest = UDAV_REQ_MEM_WRITE;
    316   1.1    itojun 	USETW(req.wValue, 0x0000);
    317   1.1    itojun 	USETW(req.wIndex, offset);
    318   1.1    itojun 	USETW(req.wLength, len);
    319   1.1    itojun 
    320  1.61     skrll 	err = usbd_do_request(un->un_udev, &req, buf);
    321   1.1    itojun 	if (err) {
    322   1.1    itojun 		DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
    323  1.61     skrll 			 device_xname(un->un_dev), __func__, offset, err));
    324   1.1    itojun 	}
    325   1.1    itojun 
    326  1.46     skrll 	return err;
    327   1.1    itojun }
    328   1.1    itojun 
    329   1.1    itojun /* write memory */
    330  1.61     skrll static int
    331  1.64       mrg udav_mem_write1(struct usbnet *un, int offset, unsigned char ch)
    332   1.1    itojun {
    333   1.1    itojun 	usb_device_request_t req;
    334   1.1    itojun 	usbd_status err;
    335   1.1    itojun 
    336   1.1    itojun 	DPRINTFN(0x200,
    337  1.61     skrll 		("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    338   1.1    itojun 
    339  1.63       mrg 	if (usbnet_isdying(un))
    340  1.46     skrll 		return 0;
    341   1.1    itojun 
    342   1.1    itojun 	offset &= 0xffff;
    343   1.1    itojun 
    344   1.1    itojun 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    345   1.1    itojun 	req.bRequest = UDAV_REQ_MEM_WRITE1;
    346   1.1    itojun 	USETW(req.wValue, ch);
    347   1.1    itojun 	USETW(req.wIndex, offset);
    348   1.1    itojun 	USETW(req.wLength, 0x0000);
    349   1.1    itojun 
    350  1.61     skrll 	err = usbd_do_request(un->un_udev, &req, NULL);
    351   1.1    itojun 	if (err) {
    352   1.1    itojun 		DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
    353  1.61     skrll 			 device_xname(un->un_dev), __func__, offset, err));
    354   1.1    itojun 	}
    355   1.1    itojun 
    356  1.46     skrll 	return err;
    357   1.1    itojun }
    358   1.1    itojun #endif
    359   1.1    itojun 
    360   1.1    itojun /* read register(s) */
    361  1.61     skrll static int
    362  1.64       mrg udav_csr_read(struct usbnet *un, int offset, void *buf, int len)
    363   1.1    itojun {
    364   1.1    itojun 	usb_device_request_t req;
    365   1.1    itojun 	usbd_status err;
    366   1.1    itojun 
    367  1.77   thorpej 	usbnet_isowned_core(un);
    368  1.63       mrg 	KASSERT(!usbnet_isdying(un));
    369   1.1    itojun 
    370   1.1    itojun 	DPRINTFN(0x200,
    371  1.61     skrll 		("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    372   1.1    itojun 
    373   1.1    itojun 	offset &= 0xff;
    374   1.1    itojun 	len &= 0xff;
    375   1.1    itojun 
    376   1.1    itojun 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    377   1.1    itojun 	req.bRequest = UDAV_REQ_REG_READ;
    378   1.1    itojun 	USETW(req.wValue, 0x0000);
    379   1.1    itojun 	USETW(req.wIndex, offset);
    380   1.1    itojun 	USETW(req.wLength, len);
    381   1.1    itojun 
    382  1.61     skrll 	err = usbd_do_request(un->un_udev, &req, buf);
    383   1.1    itojun 	if (err) {
    384   1.1    itojun 		DPRINTF(("%s: %s: read failed. off=%04x, err=%d\n",
    385  1.61     skrll 			 device_xname(un->un_dev), __func__, offset, err));
    386   1.1    itojun 	}
    387   1.1    itojun 
    388  1.46     skrll 	return err;
    389   1.1    itojun }
    390   1.1    itojun 
    391   1.1    itojun /* write register(s) */
    392  1.61     skrll static int
    393  1.64       mrg udav_csr_write(struct usbnet *un, int offset, void *buf, int len)
    394   1.1    itojun {
    395   1.1    itojun 	usb_device_request_t req;
    396   1.1    itojun 	usbd_status err;
    397   1.1    itojun 
    398  1.77   thorpej 	usbnet_isowned_core(un);
    399  1.63       mrg 	KASSERT(!usbnet_isdying(un));
    400   1.1    itojun 
    401   1.1    itojun 	DPRINTFN(0x200,
    402  1.61     skrll 		("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    403   1.1    itojun 
    404   1.1    itojun 	offset &= 0xff;
    405   1.1    itojun 	len &= 0xff;
    406   1.1    itojun 
    407   1.1    itojun 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    408   1.1    itojun 	req.bRequest = UDAV_REQ_REG_WRITE;
    409   1.1    itojun 	USETW(req.wValue, 0x0000);
    410   1.1    itojun 	USETW(req.wIndex, offset);
    411   1.1    itojun 	USETW(req.wLength, len);
    412   1.1    itojun 
    413  1.61     skrll 	err = usbd_do_request(un->un_udev, &req, buf);
    414   1.1    itojun 	if (err) {
    415   1.1    itojun 		DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
    416  1.61     skrll 			 device_xname(un->un_dev), __func__, offset, err));
    417   1.1    itojun 	}
    418   1.1    itojun 
    419  1.46     skrll 	return err;
    420   1.1    itojun }
    421   1.1    itojun 
    422  1.61     skrll static int
    423  1.64       mrg udav_csr_read1(struct usbnet *un, int offset)
    424   1.1    itojun {
    425  1.46     skrll 	uint8_t val = 0;
    426   1.5     perry 
    427  1.77   thorpej 	usbnet_isowned_core(un);
    428   1.1    itojun 
    429   1.1    itojun 	DPRINTFN(0x200,
    430  1.61     skrll 		("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    431   1.1    itojun 
    432  1.63       mrg 	if (usbnet_isdying(un))
    433  1.46     skrll 		return 0;
    434   1.1    itojun 
    435  1.64       mrg 	return udav_csr_read(un, offset, &val, 1) ? 0 : val;
    436   1.1    itojun }
    437   1.1    itojun 
    438   1.1    itojun /* write a register */
    439  1.61     skrll static int
    440  1.64       mrg udav_csr_write1(struct usbnet *un, int offset, unsigned char ch)
    441   1.1    itojun {
    442   1.1    itojun 	usb_device_request_t req;
    443   1.1    itojun 	usbd_status err;
    444   1.1    itojun 
    445  1.77   thorpej 	usbnet_isowned_core(un);
    446  1.63       mrg 	KASSERT(!usbnet_isdying(un));
    447   1.1    itojun 
    448   1.1    itojun 	DPRINTFN(0x200,
    449  1.61     skrll 		("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    450   1.1    itojun 
    451   1.1    itojun 	offset &= 0xff;
    452   1.1    itojun 
    453   1.1    itojun 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    454   1.1    itojun 	req.bRequest = UDAV_REQ_REG_WRITE1;
    455   1.1    itojun 	USETW(req.wValue, ch);
    456   1.1    itojun 	USETW(req.wIndex, offset);
    457   1.1    itojun 	USETW(req.wLength, 0x0000);
    458   1.1    itojun 
    459  1.61     skrll 	err = usbd_do_request(un->un_udev, &req, NULL);
    460   1.1    itojun 	if (err) {
    461   1.1    itojun 		DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
    462  1.61     skrll 			 device_xname(un->un_dev), __func__, offset, err));
    463   1.1    itojun 	}
    464   1.1    itojun 
    465  1.46     skrll 	return err;
    466   1.1    itojun }
    467   1.1    itojun 
    468  1.61     skrll static int
    469  1.77   thorpej udav_uno_init(struct ifnet *ifp)
    470   1.1    itojun {
    471  1.61     skrll 	struct usbnet * const un = ifp->if_softc;
    472  1.61     skrll 	struct mii_data * const mii = usbnet_mii(un);
    473  1.20    dyoung 	uint8_t eaddr[ETHER_ADDR_LEN];
    474  1.67       mrg 	int rc = 0;
    475  1.61     skrll 
    476  1.61     skrll 	DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    477   1.1    itojun 
    478  1.67       mrg 	if (usbnet_isdying(un)) {
    479  1.46     skrll 		return EIO;
    480  1.67       mrg 	}
    481   1.1    itojun 
    482  1.61     skrll 	/* Cancel pending I/O and free all TX/RX buffers */
    483  1.61     skrll 	if (ifp->if_flags & IFF_RUNNING)
    484  1.61     skrll 		usbnet_stop(un, ifp, 1);
    485   1.1    itojun 
    486  1.20    dyoung 	memcpy(eaddr, CLLADDR(ifp->if_sadl), sizeof(eaddr));
    487  1.64       mrg 	udav_csr_write(un, UDAV_PAR, eaddr, ETHER_ADDR_LEN);
    488   1.1    itojun 
    489   1.1    itojun 	/* Initialize network control register */
    490   1.1    itojun 	/*  Disable loopback  */
    491  1.64       mrg 	UDAV_CLRBIT(un, UDAV_NCR, UDAV_NCR_LBK0 | UDAV_NCR_LBK1);
    492   1.1    itojun 
    493   1.1    itojun 	/* Initialize RX control register */
    494  1.64       mrg 	UDAV_SETBIT(un, UDAV_RCR, UDAV_RCR_DIS_LONG | UDAV_RCR_DIS_CRC);
    495   1.1    itojun 
    496   1.1    itojun 	/* If we want promiscuous mode, accept all physical frames. */
    497   1.1    itojun 	if (ifp->if_flags & IFF_PROMISC)
    498  1.64       mrg 		UDAV_SETBIT(un, UDAV_RCR, UDAV_RCR_ALL | UDAV_RCR_PRMSC);
    499   1.1    itojun 	else
    500  1.64       mrg 		UDAV_CLRBIT(un, UDAV_RCR, UDAV_RCR_ALL | UDAV_RCR_PRMSC);
    501   1.1    itojun 
    502   1.1    itojun 	/* Load the multicast filter */
    503  1.86  riastrad 	udav_uno_mcast(ifp);
    504   1.1    itojun 
    505   1.1    itojun 	/* Enable RX */
    506  1.64       mrg 	UDAV_SETBIT(un, UDAV_RCR, UDAV_RCR_RXEN);
    507   1.1    itojun 
    508   1.1    itojun 	/* clear POWER_DOWN state of internal PHY */
    509  1.64       mrg 	UDAV_SETBIT(un, UDAV_GPCR, UDAV_GPCR_GEP_CNTL0);
    510  1.64       mrg 	UDAV_CLRBIT(un, UDAV_GPR, UDAV_GPR_GEPIO0);
    511   1.1    itojun 
    512  1.67       mrg 	if (mii && (rc = mii_mediachg(mii)) == ENXIO)
    513  1.67       mrg 		rc = 0;
    514  1.46     skrll 
    515  1.77   thorpej 	if (rc != 0) {
    516  1.61     skrll 		return rc;
    517  1.77   thorpej 	}
    518  1.46     skrll 
    519  1.67       mrg 	if (usbnet_isdying(un))
    520  1.67       mrg 		rc = EIO;
    521  1.67       mrg 	else
    522  1.67       mrg 		rc = usbnet_init_rx_tx(un);
    523  1.77   thorpej 
    524  1.67       mrg 	return rc;
    525   1.1    itojun }
    526   1.1    itojun 
    527  1.61     skrll static void
    528  1.61     skrll udav_reset(struct usbnet *un)
    529   1.1    itojun {
    530  1.77   thorpej     	usbnet_isowned_core(un);
    531  1.61     skrll 
    532  1.63       mrg 	if (usbnet_isdying(un))
    533  1.61     skrll 		return;
    534  1.61     skrll 
    535  1.61     skrll 	DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    536  1.61     skrll 
    537  1.61     skrll 	udav_chip_init(un);
    538  1.61     skrll }
    539   1.1    itojun 
    540  1.61     skrll static void
    541  1.61     skrll udav_chip_init(struct usbnet *un)
    542  1.61     skrll {
    543  1.77   thorpej 	usbnet_isowned_core(un);
    544   1.1    itojun 
    545   1.1    itojun 	/* Select PHY */
    546   1.1    itojun #if 1
    547   1.1    itojun 	/*
    548   1.1    itojun 	 * XXX: force select internal phy.
    549   1.1    itojun 	 *	external phy routines are not tested.
    550   1.1    itojun 	 */
    551  1.64       mrg 	UDAV_CLRBIT(un, UDAV_NCR, UDAV_NCR_EXT_PHY);
    552   1.1    itojun #else
    553  1.64       mrg 	if (un->un_flags & UDAV_EXT_PHY) {
    554  1.64       mrg 		UDAV_SETBIT(un, UDAV_NCR, UDAV_NCR_EXT_PHY);
    555   1.1    itojun 	} else {
    556  1.64       mrg 		UDAV_CLRBIT(un, UDAV_NCR, UDAV_NCR_EXT_PHY);
    557   1.1    itojun 	}
    558   1.1    itojun #endif
    559   1.1    itojun 
    560  1.64       mrg 	UDAV_SETBIT(un, UDAV_NCR, UDAV_NCR_RST);
    561   1.1    itojun 
    562  1.61     skrll 	for (int i = 0; i < UDAV_TX_TIMEOUT; i++) {
    563  1.80  riastrad 		if (usbnet_isdying(un))
    564  1.80  riastrad 			return;
    565  1.64       mrg 		if (!(udav_csr_read1(un, UDAV_NCR) & UDAV_NCR_RST))
    566   1.1    itojun 			break;
    567   1.1    itojun 		delay(10);	/* XXX */
    568   1.1    itojun 	}
    569   1.1    itojun 	delay(10000);		/* XXX */
    570   1.1    itojun }
    571   1.1    itojun 
    572   1.1    itojun #define UDAV_BITS	6
    573   1.1    itojun 
    574   1.1    itojun #define UDAV_CALCHASH(addr) \
    575   1.1    itojun 	(ether_crc32_le((addr), ETHER_ADDR_LEN) & ((1 << UDAV_BITS) - 1))
    576   1.1    itojun 
    577  1.61     skrll static void
    578  1.86  riastrad udav_uno_mcast(struct ifnet *ifp)
    579   1.1    itojun {
    580  1.86  riastrad 	struct usbnet * const un = ifp->if_softc;
    581  1.61     skrll 	struct ethercom *ec = usbnet_ec(un);
    582   1.1    itojun 	struct ether_multi *enm;
    583   1.1    itojun 	struct ether_multistep step;
    584  1.46     skrll 	uint8_t hashes[8];
    585   1.1    itojun 	int h = 0;
    586   1.1    itojun 
    587  1.61     skrll 	DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    588  1.61     skrll 
    589  1.77   thorpej 	usbnet_isowned_core(un);
    590   1.1    itojun 
    591  1.63       mrg 	if (usbnet_isdying(un))
    592   1.1    itojun 		return;
    593   1.1    itojun 
    594  1.64       mrg 	if (ISSET(un->un_flags, UDAV_NO_PHY)) {
    595  1.64       mrg 		UDAV_SETBIT(un, UDAV_RCR, UDAV_RCR_ALL);
    596  1.64       mrg 		UDAV_SETBIT(un, UDAV_RCR, UDAV_RCR_PRMSC);
    597  1.42  jakllsch 		return;
    598  1.42  jakllsch 	}
    599  1.42  jakllsch 
    600   1.1    itojun 	if (ifp->if_flags & IFF_PROMISC) {
    601  1.64       mrg 		UDAV_SETBIT(un, UDAV_RCR, UDAV_RCR_ALL | UDAV_RCR_PRMSC);
    602   1.1    itojun 		return;
    603   1.1    itojun 	} else if (ifp->if_flags & IFF_ALLMULTI) {
    604  1.59   msaitoh allmulti:
    605   1.1    itojun 		ifp->if_flags |= IFF_ALLMULTI;
    606  1.64       mrg 		UDAV_SETBIT(un, UDAV_RCR, UDAV_RCR_ALL);
    607  1.64       mrg 		UDAV_CLRBIT(un, UDAV_RCR, UDAV_RCR_PRMSC);
    608   1.1    itojun 		return;
    609   1.1    itojun 	}
    610   1.1    itojun 
    611   1.1    itojun 	/* first, zot all the existing hash bits */
    612   1.1    itojun 	memset(hashes, 0x00, sizeof(hashes));
    613   1.1    itojun 	hashes[7] |= 0x80;	/* broadcast address */
    614  1.64       mrg 	udav_csr_write(un, UDAV_MAR, hashes, sizeof(hashes));
    615   1.1    itojun 
    616   1.1    itojun 	/* now program new ones */
    617  1.59   msaitoh 	ETHER_LOCK(ec);
    618  1.59   msaitoh 	ETHER_FIRST_MULTI(step, ec, enm);
    619   1.1    itojun 	while (enm != NULL) {
    620   1.1    itojun 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    621  1.59   msaitoh 		    ETHER_ADDR_LEN) != 0) {
    622  1.59   msaitoh 			ETHER_UNLOCK(ec);
    623   1.1    itojun 			goto allmulti;
    624  1.59   msaitoh 		}
    625   1.1    itojun 
    626   1.1    itojun 		h = UDAV_CALCHASH(enm->enm_addrlo);
    627   1.1    itojun 		hashes[h>>3] |= 1 << (h & 0x7);
    628   1.1    itojun 		ETHER_NEXT_MULTI(step, enm);
    629   1.1    itojun 	}
    630  1.59   msaitoh 	ETHER_UNLOCK(ec);
    631   1.1    itojun 
    632   1.1    itojun 	/* disable all multicast */
    633   1.1    itojun 	ifp->if_flags &= ~IFF_ALLMULTI;
    634  1.64       mrg 	UDAV_CLRBIT(un, UDAV_RCR, UDAV_RCR_ALL);
    635   1.1    itojun 
    636   1.1    itojun 	/* write hash value to the register */
    637  1.64       mrg 	udav_csr_write(un, UDAV_MAR, hashes, sizeof(hashes));
    638   1.1    itojun }
    639   1.1    itojun 
    640  1.61     skrll static unsigned
    641  1.77   thorpej udav_uno_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c)
    642   1.1    itojun {
    643  1.61     skrll 	int total_len;
    644  1.61     skrll 	uint8_t *buf = c->unc_buf;
    645   1.1    itojun 
    646  1.61     skrll 	DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    647   1.1    itojun 
    648  1.66     skrll 	if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz - 2)
    649  1.61     skrll 		return 0;
    650   1.1    itojun 
    651   1.1    itojun 	/* Copy the mbuf data into a contiguous buffer */
    652  1.61     skrll 	m_copydata(m, 0, m->m_pkthdr.len, buf + 2);
    653   1.1    itojun 	total_len = m->m_pkthdr.len;
    654   1.1    itojun 	if (total_len < UDAV_MIN_FRAME_LEN) {
    655  1.61     skrll 		memset(buf + 2 + total_len, 0,
    656   1.1    itojun 		    UDAV_MIN_FRAME_LEN - total_len);
    657   1.1    itojun 		total_len = UDAV_MIN_FRAME_LEN;
    658   1.1    itojun 	}
    659   1.1    itojun 
    660   1.1    itojun 	/* Frame length is specified in the first 2bytes of the buffer */
    661  1.61     skrll 	buf[0] = (uint8_t)total_len;
    662  1.61     skrll 	buf[1] = (uint8_t)(total_len >> 8);
    663   1.1    itojun 	total_len += 2;
    664   1.1    itojun 
    665  1.61     skrll 	DPRINTF(("%s: %s: send %d bytes\n", device_xname(un->un_dev),
    666  1.61     skrll 	    __func__, total_len));
    667   1.1    itojun 
    668  1.61     skrll 	return total_len;
    669   1.1    itojun }
    670   1.1    itojun 
    671  1.61     skrll static void
    672  1.77   thorpej udav_uno_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len)
    673   1.1    itojun {
    674  1.61     skrll 	struct ifnet *ifp = usbnet_ifp(un);
    675  1.61     skrll 	uint8_t *buf = c->unc_buf;
    676  1.61     skrll 	uint16_t pkt_len;
    677  1.61     skrll 	uint8_t pktstat;
    678   1.1    itojun 
    679  1.61     skrll 	DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    680   1.1    itojun 
    681  1.61     skrll 	/* first byte in received data */
    682  1.61     skrll 	pktstat = *buf;
    683  1.61     skrll 	total_len -= sizeof(pktstat);
    684  1.61     skrll 	buf += sizeof(pktstat);
    685   1.1    itojun 
    686  1.76  christos 	DPRINTF(("%s: RX Status: 0x%02x\n", device_xname(un->un_dev), pktstat));
    687   1.1    itojun 
    688  1.61     skrll 	pkt_len = UGETW(buf);
    689  1.61     skrll  	total_len -= sizeof(pkt_len);
    690  1.61     skrll 	buf += sizeof(pkt_len);
    691   1.1    itojun 
    692  1.76  christos 	DPRINTF(("%s: RX Length: 0x%02x\n", device_xname(un->un_dev), pkt_len));
    693   1.1    itojun 
    694  1.61     skrll 	if (pktstat & UDAV_RSR_LCS) {
    695  1.74   thorpej 		if_statinc(ifp, if_collisions);
    696  1.61     skrll 		return;
    697   1.1    itojun 	}
    698   1.1    itojun 
    699  1.61     skrll 	if (pkt_len < sizeof(struct ether_header) ||
    700  1.61     skrll 	    pkt_len > total_len ||
    701  1.61     skrll 	    (pktstat & UDAV_RSR_ERR)) {
    702  1.74   thorpej 		if_statinc(ifp, if_ierrors);
    703  1.61     skrll 		return;
    704   1.1    itojun 	}
    705   1.1    itojun 
    706  1.61     skrll 	pkt_len -= ETHER_CRC_LEN;
    707   1.1    itojun 
    708  1.76  christos 	DPRINTF(("%s: Rx deliver: 0x%02x\n", device_xname(un->un_dev), pkt_len));
    709   1.1    itojun 
    710  1.61     skrll 	usbnet_enqueue(un, buf, pkt_len, 0, 0, 0);
    711   1.1    itojun }
    712   1.1    itojun 
    713   1.1    itojun /* Stop the adapter and free any mbufs allocated to the RX and TX lists. */
    714  1.61     skrll static void
    715  1.77   thorpej udav_uno_stop(struct ifnet *ifp, int disable)
    716   1.1    itojun {
    717  1.61     skrll 	struct usbnet * const un = ifp->if_softc;
    718   1.1    itojun 
    719  1.61     skrll 	DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    720   1.1    itojun 
    721  1.61     skrll 	udav_reset(un);
    722   1.1    itojun }
    723   1.1    itojun 
    724  1.70       mrg static int
    725  1.77   thorpej udav_uno_mii_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val)
    726   1.1    itojun {
    727  1.56   msaitoh 	uint8_t data[2];
    728   1.1    itojun 
    729  1.76  christos 	DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x\n",
    730  1.61     skrll 		 device_xname(un->un_dev), __func__, phy, reg));
    731   1.1    itojun 
    732  1.63       mrg 	if (usbnet_isdying(un)) {
    733   1.1    itojun #ifdef DIAGNOSTIC
    734  1.61     skrll 		printf("%s: %s: dying\n", device_xname(un->un_dev),
    735   1.1    itojun 		       __func__);
    736   1.1    itojun #endif
    737  1.70       mrg 		return EINVAL;
    738   1.1    itojun 	}
    739   1.1    itojun 
    740   1.1    itojun 	/* XXX: one PHY only for the internal PHY */
    741   1.1    itojun 	if (phy != 0) {
    742   1.1    itojun 		DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
    743  1.61     skrll 			 device_xname(un->un_dev), __func__, phy));
    744  1.70       mrg 		return EINVAL;
    745   1.1    itojun 	}
    746   1.1    itojun 
    747   1.1    itojun 	/* select internal PHY and set PHY register address */
    748  1.64       mrg 	udav_csr_write1(un, UDAV_EPAR,
    749   1.1    itojun 			UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
    750   1.1    itojun 
    751   1.1    itojun 	/* select PHY operation and start read command */
    752  1.64       mrg 	udav_csr_write1(un, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRR);
    753   1.1    itojun 
    754   1.1    itojun 	/* XXX: should be wait? */
    755   1.1    itojun 
    756   1.1    itojun 	/* end read command */
    757  1.64       mrg 	UDAV_CLRBIT(un, UDAV_EPCR, UDAV_EPCR_ERPRR);
    758   1.1    itojun 
    759   1.1    itojun 	/* retrieve the result from data registers */
    760  1.64       mrg 	udav_csr_read(un, UDAV_EPDRL, data, 2);
    761   1.1    itojun 
    762  1.56   msaitoh 	*val = data[0] | (data[1] << 8);
    763   1.1    itojun 
    764  1.76  christos 	DPRINTFN(0xff, ("%s: %s: phy=%d reg=0x%04x => 0x%04hx\n",
    765  1.61     skrll 		device_xname(un->un_dev), __func__, phy, reg, *val));
    766   1.1    itojun 
    767  1.70       mrg 	return 0;
    768   1.1    itojun }
    769   1.1    itojun 
    770  1.70       mrg static int
    771  1.77   thorpej udav_uno_mii_write_reg(struct usbnet *un, int phy, int reg, uint16_t val)
    772   1.1    itojun {
    773  1.56   msaitoh 	uint8_t data[2];
    774   1.1    itojun 
    775  1.76  christos 	DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x val=0x%04hx\n",
    776  1.61     skrll 		 device_xname(un->un_dev), __func__, phy, reg, val));
    777   1.1    itojun 
    778  1.63       mrg 	if (usbnet_isdying(un)) {
    779   1.1    itojun #ifdef DIAGNOSTIC
    780  1.61     skrll 		printf("%s: %s: dying\n", device_xname(un->un_dev),
    781   1.1    itojun 		       __func__);
    782   1.1    itojun #endif
    783  1.70       mrg 		return EIO;
    784   1.1    itojun 	}
    785   1.1    itojun 
    786   1.1    itojun 	/* XXX: one PHY only for the internal PHY */
    787   1.1    itojun 	if (phy != 0) {
    788   1.1    itojun 		DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
    789  1.61     skrll 			 device_xname(un->un_dev), __func__, phy));
    790  1.70       mrg 		return EIO;
    791   1.1    itojun 	}
    792   1.1    itojun 
    793   1.1    itojun 	/* select internal PHY and set PHY register address */
    794  1.64       mrg 	udav_csr_write1(un, UDAV_EPAR,
    795   1.1    itojun 			UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
    796   1.1    itojun 
    797   1.1    itojun 	/* put the value to the data registers */
    798  1.56   msaitoh 	data[0] = val & 0xff;
    799  1.56   msaitoh 	data[1] = (val >> 8) & 0xff;
    800  1.64       mrg 	udav_csr_write(un, UDAV_EPDRL, data, 2);
    801   1.1    itojun 
    802   1.1    itojun 	/* select PHY operation and start write command */
    803  1.64       mrg 	udav_csr_write1(un, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRW);
    804   1.1    itojun 
    805   1.1    itojun 	/* XXX: should be wait? */
    806   1.1    itojun 
    807   1.1    itojun 	/* end write command */
    808  1.64       mrg 	UDAV_CLRBIT(un, UDAV_EPCR, UDAV_EPCR_ERPRW);
    809   1.1    itojun 
    810  1.70       mrg 	return 0;
    811   1.1    itojun }
    812   1.1    itojun 
    813  1.61     skrll static void
    814  1.77   thorpej udav_uno_mii_statchg(struct ifnet *ifp)
    815   1.1    itojun {
    816  1.61     skrll 	struct usbnet * const un = ifp->if_softc;
    817  1.61     skrll 	struct mii_data * const mii = usbnet_mii(un);
    818  1.61     skrll 
    819  1.61     skrll 	DPRINTF(("%s: %s: enter\n", ifp->if_xname, __func__));
    820   1.1    itojun 
    821  1.63       mrg 	if (usbnet_isdying(un))
    822   1.1    itojun 		return;
    823   1.1    itojun 
    824  1.72     skrll 	if ((mii->mii_media_status & IFM_ACTIVE) &&
    825  1.61     skrll 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
    826  1.61     skrll 		DPRINTF(("%s: %s: got link\n",
    827  1.61     skrll 			 device_xname(un->un_dev), __func__));
    828  1.65       mrg 		usbnet_set_link(un, true);
    829  1.61     skrll 	}
    830  1.61     skrll }
    831  1.61     skrll 
    832  1.61     skrll #ifdef _MODULE
    833  1.61     skrll #include "ioconf.c"
    834  1.61     skrll #endif
    835  1.61     skrll 
    836  1.67       mrg USBNET_MODULE(udav)
    837