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