1 /* $NetBSD: if_udav.c,v 1.2 2003/09/04 15:17:38 tsutsui Exp $ */
2 /* $nabe: if_udav.c,v 1.3 2003/08/21 16:57:19 nabe Exp $ */
5 * SPDX-License-Identifier: BSD-3-Clause
8 * Shingo WATANABE <nabe@nabechan.org>. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * DM9601(DAVICOM USB to Ethernet MAC Controller with Integrated 10/100 PHY)
38 * The spec can be found at the following url.
39 * http://ptm2.cc.utu.fi/ftp/network/cards/DM9601/From_NET/DM9601-DS-P01-930914.pdf
44 * Interrupt Endpoint support
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
51 #include <sys/stdint.h>
52 #include <sys/stddef.h>
53 #include <sys/param.h>
54 #include <sys/queue.h>
55 #include <sys/types.h>
56 #include <sys/systm.h>
57 #include <sys/socket.h>
58 #include <sys/kernel.h>
60 #include <sys/module.h>
62 #include <sys/mutex.h>
63 #include <sys/condvar.h>
64 #include <sys/sysctl.h>
66 #include <sys/unistd.h>
67 #include <sys/callout.h>
68 #include <sys/malloc.h>
72 #include <net/if_var.h>
73 #include <net/if_media.h>
75 #include <dev/mii/mii.h>
76 #include <dev/mii/miivar.h>
78 #include <dev/usb/usb.h>
79 #include <dev/usb/usbdi.h>
80 #include <dev/usb/usbdi_util.h>
83 #include "miibus_if.h"
85 #define USB_DEBUG_VAR udav_debug
86 #include <dev/usb/usb_debug.h>
87 #include <dev/usb/usb_process.h>
89 #include <dev/usb/net/usb_ethernet.h>
90 #include <dev/usb/net/if_udavreg.h>
94 static device_probe_t udav_probe;
95 static device_attach_t udav_attach;
96 static device_detach_t udav_detach;
98 static usb_callback_t udav_bulk_write_callback;
99 static usb_callback_t udav_bulk_read_callback;
100 static usb_callback_t udav_intr_callback;
102 static uether_fn_t udav_attach_post;
103 static uether_fn_t udav_init;
104 static uether_fn_t udav_stop;
105 static uether_fn_t udav_start;
106 static uether_fn_t udav_tick;
107 static uether_fn_t udav_setmulti;
108 static uether_fn_t udav_setpromisc;
110 static int udav_csr_read(struct udav_softc *, uint16_t, void *, int);
111 static int udav_csr_write(struct udav_softc *, uint16_t, void *, int);
112 static uint8_t udav_csr_read1(struct udav_softc *, uint16_t);
113 static int udav_csr_write1(struct udav_softc *, uint16_t, uint8_t);
114 static void udav_reset(struct udav_softc *);
115 static int udav_ifmedia_upd(struct ifnet *);
116 static void udav_ifmedia_status(struct ifnet *, struct ifmediareq *);
118 static miibus_readreg_t udav_miibus_readreg;
119 static miibus_writereg_t udav_miibus_writereg;
120 static miibus_statchg_t udav_miibus_statchg;
122 static const struct usb_config udav_config[UDAV_N_TRANSFER] = {
124 [UDAV_BULK_DT_WR] = {
126 .endpoint = UE_ADDR_ANY,
127 .direction = UE_DIR_OUT,
128 .bufsize = (MCLBYTES + 2),
129 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
130 .callback = udav_bulk_write_callback,
131 .timeout = 10000, /* 10 seconds */
134 [UDAV_BULK_DT_RD] = {
136 .endpoint = UE_ADDR_ANY,
137 .direction = UE_DIR_IN,
138 .bufsize = (MCLBYTES + 3),
139 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
140 .callback = udav_bulk_read_callback,
141 .timeout = 0, /* no timeout */
144 [UDAV_INTR_DT_RD] = {
145 .type = UE_INTERRUPT,
146 .endpoint = UE_ADDR_ANY,
147 .direction = UE_DIR_IN,
148 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
149 .bufsize = 0, /* use wMaxPacketSize */
150 .callback = udav_intr_callback,
154 static device_method_t udav_methods[] = {
155 /* Device interface */
156 DEVMETHOD(device_probe, udav_probe),
157 DEVMETHOD(device_attach, udav_attach),
158 DEVMETHOD(device_detach, udav_detach),
161 DEVMETHOD(miibus_readreg, udav_miibus_readreg),
162 DEVMETHOD(miibus_writereg, udav_miibus_writereg),
163 DEVMETHOD(miibus_statchg, udav_miibus_statchg),
168 static driver_t udav_driver = {
170 .methods = udav_methods,
171 .size = sizeof(struct udav_softc),
174 static devclass_t udav_devclass;
176 static const STRUCT_USB_HOST_ID udav_devs[] = {
177 /* ShanTou DM9601 USB NIC */
178 {USB_VPI(USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_DM9601, 0)},
179 /* ShanTou ST268 USB NIC */
180 {USB_VPI(USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ST268, 0)},
182 {USB_VPI(USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXC, 0)},
183 /* ShanTou AMD8515 USB NIC */
184 {USB_VPI(USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ADM8515, 0)},
185 /* Kontron AG USB Ethernet */
186 {USB_VPI(USB_VENDOR_KONTRON, USB_PRODUCT_KONTRON_DM9601, 0)},
187 {USB_VPI(USB_VENDOR_KONTRON, USB_PRODUCT_KONTRON_JP1082,
191 DRIVER_MODULE(udav, uhub, udav_driver, udav_devclass, NULL, 0);
192 DRIVER_MODULE(miibus, udav, miibus_driver, miibus_devclass, 0, 0);
193 MODULE_DEPEND(udav, uether, 1, 1, 1);
194 MODULE_DEPEND(udav, usb, 1, 1, 1);
195 MODULE_DEPEND(udav, ether, 1, 1, 1);
196 MODULE_DEPEND(udav, miibus, 1, 1, 1);
197 MODULE_VERSION(udav, 1);
198 USB_PNP_HOST_INFO(udav_devs);
200 static const struct usb_ether_methods udav_ue_methods = {
201 .ue_attach_post = udav_attach_post,
202 .ue_start = udav_start,
203 .ue_init = udav_init,
204 .ue_stop = udav_stop,
205 .ue_tick = udav_tick,
206 .ue_setmulti = udav_setmulti,
207 .ue_setpromisc = udav_setpromisc,
208 .ue_mii_upd = udav_ifmedia_upd,
209 .ue_mii_sts = udav_ifmedia_status,
212 static const struct usb_ether_methods udav_ue_methods_nophy = {
213 .ue_attach_post = udav_attach_post,
214 .ue_start = udav_start,
215 .ue_init = udav_init,
216 .ue_stop = udav_stop,
217 .ue_setmulti = udav_setmulti,
218 .ue_setpromisc = udav_setpromisc,
222 static int udav_debug = 0;
224 static SYSCTL_NODE(_hw_usb, OID_AUTO, udav, CTLFLAG_RW, 0, "USB udav");
225 SYSCTL_INT(_hw_usb_udav, OID_AUTO, debug, CTLFLAG_RWTUN, &udav_debug, 0,
229 #define UDAV_SETBIT(sc, reg, x) \
230 udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) | (x))
232 #define UDAV_CLRBIT(sc, reg, x) \
233 udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) & ~(x))
236 udav_attach_post(struct usb_ether *ue)
238 struct udav_softc *sc = uether_getsc(ue);
240 /* reset the adapter */
243 /* Get Ethernet Address */
244 udav_csr_read(sc, UDAV_PAR, ue->ue_eaddr, ETHER_ADDR_LEN);
248 udav_probe(device_t dev)
250 struct usb_attach_arg *uaa = device_get_ivars(dev);
252 if (uaa->usb_mode != USB_MODE_HOST)
254 if (uaa->info.bConfigIndex != UDAV_CONFIG_INDEX)
256 if (uaa->info.bIfaceIndex != UDAV_IFACE_INDEX)
259 return (usbd_lookup_id_by_uaa(udav_devs, sizeof(udav_devs), uaa));
263 udav_attach(device_t dev)
265 struct usb_attach_arg *uaa = device_get_ivars(dev);
266 struct udav_softc *sc = device_get_softc(dev);
267 struct usb_ether *ue = &sc->sc_ue;
271 sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
273 device_set_usb_desc(dev);
275 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
277 iface_index = UDAV_IFACE_INDEX;
278 error = usbd_transfer_setup(uaa->device, &iface_index,
279 sc->sc_xfer, udav_config, UDAV_N_TRANSFER, sc, &sc->sc_mtx);
281 device_printf(dev, "allocating USB transfers failed\n");
286 * The JP1082 has an unusable PHY and provides no link information.
288 if (sc->sc_flags & UDAV_FLAG_NO_PHY) {
289 ue->ue_methods = &udav_ue_methods_nophy;
290 sc->sc_flags |= UDAV_FLAG_LINK;
292 ue->ue_methods = &udav_ue_methods;
297 ue->ue_udev = uaa->device;
298 ue->ue_mtx = &sc->sc_mtx;
300 error = uether_ifattach(ue);
302 device_printf(dev, "could not attach interface\n");
306 return (0); /* success */
310 return (ENXIO); /* failure */
314 udav_detach(device_t dev)
316 struct udav_softc *sc = device_get_softc(dev);
317 struct usb_ether *ue = &sc->sc_ue;
319 usbd_transfer_unsetup(sc->sc_xfer, UDAV_N_TRANSFER);
321 mtx_destroy(&sc->sc_mtx);
328 udav_mem_read(struct udav_softc *sc, uint16_t offset, void *buf,
331 struct usb_device_request req;
335 req.bmRequestType = UT_READ_VENDOR_DEVICE;
336 req.bRequest = UDAV_REQ_MEM_READ;
337 USETW(req.wValue, 0x0000);
338 USETW(req.wIndex, offset);
339 USETW(req.wLength, len);
341 return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
345 udav_mem_write(struct udav_softc *sc, uint16_t offset, void *buf,
348 struct usb_device_request req;
352 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
353 req.bRequest = UDAV_REQ_MEM_WRITE;
354 USETW(req.wValue, 0x0000);
355 USETW(req.wIndex, offset);
356 USETW(req.wLength, len);
358 return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
362 udav_mem_write1(struct udav_softc *sc, uint16_t offset,
365 struct usb_device_request req;
367 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
368 req.bRequest = UDAV_REQ_MEM_WRITE1;
369 USETW(req.wValue, ch);
370 USETW(req.wIndex, offset);
371 USETW(req.wLength, 0x0000);
373 return (uether_do_request(&sc->sc_ue, &req, NULL, 1000));
378 udav_csr_read(struct udav_softc *sc, uint16_t offset, void *buf, int len)
380 struct usb_device_request req;
384 req.bmRequestType = UT_READ_VENDOR_DEVICE;
385 req.bRequest = UDAV_REQ_REG_READ;
386 USETW(req.wValue, 0x0000);
387 USETW(req.wIndex, offset);
388 USETW(req.wLength, len);
390 return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
394 udav_csr_write(struct udav_softc *sc, uint16_t offset, void *buf, int len)
396 struct usb_device_request req;
401 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
402 req.bRequest = UDAV_REQ_REG_WRITE;
403 USETW(req.wValue, 0x0000);
404 USETW(req.wIndex, offset);
405 USETW(req.wLength, len);
407 return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
411 udav_csr_read1(struct udav_softc *sc, uint16_t offset)
415 udav_csr_read(sc, offset, &val, 1);
420 udav_csr_write1(struct udav_softc *sc, uint16_t offset,
423 struct usb_device_request req;
427 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
428 req.bRequest = UDAV_REQ_REG_WRITE1;
429 USETW(req.wValue, ch);
430 USETW(req.wIndex, offset);
431 USETW(req.wLength, 0x0000);
433 return (uether_do_request(&sc->sc_ue, &req, NULL, 1000));
437 udav_init(struct usb_ether *ue)
439 struct udav_softc *sc = ue->ue_sc;
440 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
442 UDAV_LOCK_ASSERT(sc, MA_OWNED);
449 /* set MAC address */
450 udav_csr_write(sc, UDAV_PAR, IF_LLADDR(ifp), ETHER_ADDR_LEN);
452 /* initialize network control register */
454 /* disable loopback */
455 UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_LBK0 | UDAV_NCR_LBK1);
457 /* Initialize RX control register */
458 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_DIS_LONG | UDAV_RCR_DIS_CRC);
460 /* load multicast filter and update promiscious mode bit */
464 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_RXEN);
466 /* clear POWER_DOWN state of internal PHY */
467 UDAV_SETBIT(sc, UDAV_GPCR, UDAV_GPCR_GEP_CNTL0);
468 UDAV_CLRBIT(sc, UDAV_GPR, UDAV_GPR_GEPIO0);
470 usbd_xfer_set_stall(sc->sc_xfer[UDAV_BULK_DT_WR]);
472 ifp->if_drv_flags |= IFF_DRV_RUNNING;
477 udav_reset(struct udav_softc *sc)
484 * XXX: force select internal phy.
485 * external phy routines are not tested.
487 UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
489 if (sc->sc_flags & UDAV_EXT_PHY)
490 UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
492 UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
495 UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_RST);
497 for (i = 0; i < UDAV_TX_TIMEOUT; i++) {
498 if (!(udav_csr_read1(sc, UDAV_NCR) & UDAV_NCR_RST))
500 if (uether_pause(&sc->sc_ue, hz / 100))
504 uether_pause(&sc->sc_ue, hz / 100);
509 udav_setmulti(struct usb_ether *ue)
511 struct udav_softc *sc = ue->ue_sc;
512 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
513 struct ifmultiaddr *ifma;
514 uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
517 UDAV_LOCK_ASSERT(sc, MA_OWNED);
519 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
520 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC);
524 /* first, zot all the existing hash bits */
525 memset(hashtbl, 0x00, sizeof(hashtbl));
526 hashtbl[7] |= 0x80; /* broadcast address */
527 udav_csr_write(sc, UDAV_MAR, hashtbl, sizeof(hashtbl));
529 /* now program new ones */
531 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
533 if (ifma->ifma_addr->sa_family != AF_LINK)
535 h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
536 ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
537 hashtbl[h / 8] |= 1 << (h % 8);
539 if_maddr_runlock(ifp);
541 /* disable all multicast */
542 UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_ALL);
544 /* write hash value to the register */
545 udav_csr_write(sc, UDAV_MAR, hashtbl, sizeof(hashtbl));
549 udav_setpromisc(struct usb_ether *ue)
551 struct udav_softc *sc = ue->ue_sc;
552 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
555 rxmode = udav_csr_read1(sc, UDAV_RCR);
556 rxmode &= ~(UDAV_RCR_ALL | UDAV_RCR_PRMSC);
558 if (ifp->if_flags & IFF_PROMISC)
559 rxmode |= UDAV_RCR_ALL | UDAV_RCR_PRMSC;
560 else if (ifp->if_flags & IFF_ALLMULTI)
561 rxmode |= UDAV_RCR_ALL;
563 /* write new mode bits */
564 udav_csr_write1(sc, UDAV_RCR, rxmode);
568 udav_start(struct usb_ether *ue)
570 struct udav_softc *sc = ue->ue_sc;
573 * start the USB transfers, if not already started:
575 usbd_transfer_start(sc->sc_xfer[UDAV_INTR_DT_RD]);
576 usbd_transfer_start(sc->sc_xfer[UDAV_BULK_DT_RD]);
577 usbd_transfer_start(sc->sc_xfer[UDAV_BULK_DT_WR]);
581 udav_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
583 struct udav_softc *sc = usbd_xfer_softc(xfer);
584 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
585 struct usb_page_cache *pc;
591 switch (USB_GET_STATE(xfer)) {
592 case USB_ST_TRANSFERRED:
593 DPRINTFN(11, "transfer complete\n");
594 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
599 if ((sc->sc_flags & UDAV_FLAG_LINK) == 0) {
601 * don't send anything if there is no link !
605 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
609 if (m->m_pkthdr.len > MCLBYTES)
610 m->m_pkthdr.len = MCLBYTES;
611 if (m->m_pkthdr.len < UDAV_MIN_FRAME_LEN) {
612 extra_len = UDAV_MIN_FRAME_LEN - m->m_pkthdr.len;
617 temp_len = (m->m_pkthdr.len + extra_len);
620 * the frame length is specified in the first 2 bytes of the
623 buf[0] = (uint8_t)(temp_len);
624 buf[1] = (uint8_t)(temp_len >> 8);
628 pc = usbd_xfer_get_frame(xfer, 0);
629 usbd_copy_in(pc, 0, buf, 2);
630 usbd_m_copy_in(pc, 2, m, 0, m->m_pkthdr.len);
633 usbd_frame_zero(pc, temp_len - extra_len, extra_len);
635 * if there's a BPF listener, bounce a copy
636 * of this frame to him:
642 usbd_xfer_set_frame_len(xfer, 0, temp_len);
643 usbd_transfer_submit(xfer);
647 DPRINTFN(11, "transfer error, %s\n",
650 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
652 if (error != USB_ERR_CANCELLED) {
653 /* try to clear stall first */
654 usbd_xfer_set_stall(xfer);
662 udav_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
664 struct udav_softc *sc = usbd_xfer_softc(xfer);
665 struct usb_ether *ue = &sc->sc_ue;
666 struct ifnet *ifp = uether_getifp(ue);
667 struct usb_page_cache *pc;
668 struct udav_rxpkt stat;
672 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
674 switch (USB_GET_STATE(xfer)) {
675 case USB_ST_TRANSFERRED:
677 if (actlen < (int)(sizeof(stat) + ETHER_CRC_LEN)) {
678 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
681 pc = usbd_xfer_get_frame(xfer, 0);
682 usbd_copy_out(pc, 0, &stat, sizeof(stat));
683 actlen -= sizeof(stat);
684 len = min(actlen, le16toh(stat.pktlen));
685 len -= ETHER_CRC_LEN;
687 if (stat.rxstat & UDAV_RSR_LCS) {
688 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
691 if (stat.rxstat & UDAV_RSR_ERR) {
692 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
695 uether_rxbuf(ue, pc, sizeof(stat), len);
699 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
700 usbd_transfer_submit(xfer);
705 DPRINTF("bulk read error, %s\n",
708 if (error != USB_ERR_CANCELLED) {
709 /* try to clear stall first */
710 usbd_xfer_set_stall(xfer);
718 udav_intr_callback(struct usb_xfer *xfer, usb_error_t error)
720 switch (USB_GET_STATE(xfer)) {
721 case USB_ST_TRANSFERRED:
724 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
725 usbd_transfer_submit(xfer);
729 if (error != USB_ERR_CANCELLED) {
730 /* try to clear stall first */
731 usbd_xfer_set_stall(xfer);
739 udav_stop(struct usb_ether *ue)
741 struct udav_softc *sc = ue->ue_sc;
742 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
744 UDAV_LOCK_ASSERT(sc, MA_OWNED);
746 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
747 if (!(sc->sc_flags & UDAV_FLAG_NO_PHY))
748 sc->sc_flags &= ~UDAV_FLAG_LINK;
751 * stop all the transfers, if not already stopped:
753 usbd_transfer_stop(sc->sc_xfer[UDAV_BULK_DT_WR]);
754 usbd_transfer_stop(sc->sc_xfer[UDAV_BULK_DT_RD]);
755 usbd_transfer_stop(sc->sc_xfer[UDAV_INTR_DT_RD]);
761 udav_ifmedia_upd(struct ifnet *ifp)
763 struct udav_softc *sc = ifp->if_softc;
764 struct mii_data *mii = GET_MII(sc);
765 struct mii_softc *miisc;
768 UDAV_LOCK_ASSERT(sc, MA_OWNED);
770 sc->sc_flags &= ~UDAV_FLAG_LINK;
771 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
773 error = mii_mediachg(mii);
778 udav_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr)
780 struct udav_softc *sc = ifp->if_softc;
781 struct mii_data *mii = GET_MII(sc);
785 ifmr->ifm_active = mii->mii_media_active;
786 ifmr->ifm_status = mii->mii_media_status;
791 udav_tick(struct usb_ether *ue)
793 struct udav_softc *sc = ue->ue_sc;
794 struct mii_data *mii = GET_MII(sc);
796 UDAV_LOCK_ASSERT(sc, MA_OWNED);
799 if ((sc->sc_flags & UDAV_FLAG_LINK) == 0
800 && mii->mii_media_status & IFM_ACTIVE &&
801 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
802 sc->sc_flags |= UDAV_FLAG_LINK;
808 udav_miibus_readreg(device_t dev, int phy, int reg)
810 struct udav_softc *sc = device_get_softc(dev);
815 /* XXX: one PHY only for the internal PHY */
819 locked = mtx_owned(&sc->sc_mtx);
823 /* select internal PHY and set PHY register address */
824 udav_csr_write1(sc, UDAV_EPAR,
825 UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
827 /* select PHY operation and start read command */
828 udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRR);
830 /* XXX: should we wait? */
832 /* end read command */
833 UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRR);
835 /* retrieve the result from data registers */
836 udav_csr_read(sc, UDAV_EPDRL, val, 2);
838 data16 = (val[0] | (val[1] << 8));
840 DPRINTFN(11, "phy=%d reg=0x%04x => 0x%04x\n",
849 udav_miibus_writereg(device_t dev, int phy, int reg, int data)
851 struct udav_softc *sc = device_get_softc(dev);
855 /* XXX: one PHY only for the internal PHY */
859 locked = mtx_owned(&sc->sc_mtx);
863 /* select internal PHY and set PHY register address */
864 udav_csr_write1(sc, UDAV_EPAR,
865 UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
867 /* put the value to the data registers */
868 val[0] = (data & 0xff);
869 val[1] = (data >> 8) & 0xff;
870 udav_csr_write(sc, UDAV_EPDRL, val, 2);
872 /* select PHY operation and start write command */
873 udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRW);
875 /* XXX: should we wait? */
877 /* end write command */
878 UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRW);
886 udav_miibus_statchg(device_t dev)