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