kernel: Remove <sys/mutex.h> from all files that don't need it (2/2).
[dragonfly.git] / sys / bus / u4b / net / if_udav.c
blobe1d43b72dbd4ca9d490a382ebdd69047f1f0dfc5
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 $ */
3 /* $FreeBSD$ */
4 /*-
5 * Copyright (c) 2003
6 * Shingo WATANABE <nabe@nabechan.org>. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
35 * DM9601(DAVICOM USB to Ethernet MAC Controller with Integrated 10/100 PHY)
36 * The spec can be found at the following url.
37 * http://www.davicom.com.tw/big5/download/Data%20Sheet/DM9601-DS-P01-930914.pdf
41 * TODO:
42 * Interrupt Endpoint support
43 * External PHYs
46 #include <sys/stdint.h>
47 #include <sys/param.h>
48 #include <sys/queue.h>
49 #include <sys/types.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/bus.h>
53 #include <sys/module.h>
54 #include <sys/lock.h>
55 #include <sys/condvar.h>
56 #include <sys/sysctl.h>
57 #include <sys/unistd.h>
58 #include <sys/callout.h>
59 #include <sys/malloc.h>
60 #include <sys/priv.h>
62 #include <net/if.h>
63 #include <net/if_var.h>
64 #include <net/ifq_var.h>
66 #include <bus/u4b/usb.h>
67 #include <bus/u4b/usbdi.h>
68 #include <bus/u4b/usbdi_util.h>
69 #include "usbdevs.h"
71 #define USB_DEBUG_VAR udav_debug
72 #include <bus/u4b/usb_debug.h>
73 #include <bus/u4b/usb_process.h>
75 #include <bus/u4b/net/usb_ethernet.h>
76 #include <bus/u4b/net/if_udavreg.h>
78 /* prototypes */
80 static device_probe_t udav_probe;
81 static device_attach_t udav_attach;
82 static device_detach_t udav_detach;
84 static usb_callback_t udav_bulk_write_callback;
85 static usb_callback_t udav_bulk_read_callback;
86 static usb_callback_t udav_intr_callback;
88 static uether_fn_t udav_attach_post;
89 static uether_fn_t udav_init;
90 static uether_fn_t udav_stop;
91 static uether_fn_t udav_start;
92 static uether_fn_t udav_tick;
93 static uether_fn_t udav_setmulti;
94 static uether_fn_t udav_setpromisc;
96 static int udav_csr_read(struct udav_softc *, uint16_t, void *, int);
97 static int udav_csr_write(struct udav_softc *, uint16_t, void *, int);
98 static uint8_t udav_csr_read1(struct udav_softc *, uint16_t);
99 static int udav_csr_write1(struct udav_softc *, uint16_t, uint8_t);
100 static void udav_reset(struct udav_softc *);
101 static int udav_ifmedia_upd(struct ifnet *);
102 static void udav_ifmedia_status(struct ifnet *, struct ifmediareq *);
104 static miibus_readreg_t udav_miibus_readreg;
105 static miibus_writereg_t udav_miibus_writereg;
106 static miibus_statchg_t udav_miibus_statchg;
108 static const struct usb_config udav_config[UDAV_N_TRANSFER] = {
110 [UDAV_BULK_DT_WR] = {
111 .type = UE_BULK,
112 .endpoint = UE_ADDR_ANY,
113 .direction = UE_DIR_OUT,
114 .bufsize = (MCLBYTES + 2),
115 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
116 .callback = udav_bulk_write_callback,
117 .timeout = 10000, /* 10 seconds */
120 [UDAV_BULK_DT_RD] = {
121 .type = UE_BULK,
122 .endpoint = UE_ADDR_ANY,
123 .direction = UE_DIR_IN,
124 .bufsize = (MCLBYTES + 3),
125 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
126 .callback = udav_bulk_read_callback,
127 .timeout = 0, /* no timeout */
130 [UDAV_INTR_DT_RD] = {
131 .type = UE_INTERRUPT,
132 .endpoint = UE_ADDR_ANY,
133 .direction = UE_DIR_IN,
134 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
135 .bufsize = 0, /* use wMaxPacketSize */
136 .callback = udav_intr_callback,
140 static device_method_t udav_methods[] = {
141 /* Device interface */
142 DEVMETHOD(device_probe, udav_probe),
143 DEVMETHOD(device_attach, udav_attach),
144 DEVMETHOD(device_detach, udav_detach),
146 /* MII interface */
147 DEVMETHOD(miibus_readreg, udav_miibus_readreg),
148 DEVMETHOD(miibus_writereg, udav_miibus_writereg),
149 DEVMETHOD(miibus_statchg, udav_miibus_statchg),
151 DEVMETHOD_END
154 static driver_t udav_driver = {
155 .name = "udav",
156 .methods = udav_methods,
157 .size = sizeof(struct udav_softc),
160 static devclass_t udav_devclass;
162 DRIVER_MODULE(udav, uhub, udav_driver, udav_devclass, NULL, NULL);
163 DRIVER_MODULE(miibus, udav, miibus_driver, miibus_devclass, NULL, NULL);
164 MODULE_DEPEND(udav, uether, 1, 1, 1);
165 MODULE_DEPEND(udav, usb, 1, 1, 1);
166 MODULE_DEPEND(udav, ether, 1, 1, 1);
167 MODULE_DEPEND(udav, miibus, 1, 1, 1);
168 MODULE_VERSION(udav, 1);
170 static const struct usb_ether_methods udav_ue_methods = {
171 .ue_attach_post = udav_attach_post,
172 .ue_start = udav_start,
173 .ue_init = udav_init,
174 .ue_stop = udav_stop,
175 .ue_tick = udav_tick,
176 .ue_setmulti = udav_setmulti,
177 .ue_setpromisc = udav_setpromisc,
178 .ue_mii_upd = udav_ifmedia_upd,
179 .ue_mii_sts = udav_ifmedia_status,
182 static const struct usb_ether_methods udav_ue_methods_nophy = {
183 .ue_attach_post = udav_attach_post,
184 .ue_start = udav_start,
185 .ue_init = udav_init,
186 .ue_stop = udav_stop,
187 .ue_setmulti = udav_setmulti,
188 .ue_setpromisc = udav_setpromisc,
191 #ifdef USB_DEBUG
192 static int udav_debug = 0;
194 static SYSCTL_NODE(_hw_usb, OID_AUTO, udav, CTLFLAG_RW, 0, "USB udav");
195 SYSCTL_INT(_hw_usb_udav, OID_AUTO, debug, CTLFLAG_RW, &udav_debug, 0,
196 "Debug level");
197 #endif
199 #define UDAV_SETBIT(sc, reg, x) \
200 udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) | (x))
202 #define UDAV_CLRBIT(sc, reg, x) \
203 udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) & ~(x))
205 static const STRUCT_USB_HOST_ID udav_devs[] = {
206 /* ShanTou DM9601 USB NIC */
207 {USB_VPI(USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_DM9601, 0)},
208 /* ShanTou ST268 USB NIC */
209 {USB_VPI(USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ST268, 0)},
210 /* Corega USB-TXC */
211 {USB_VPI(USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXC, 0)},
212 /* ShanTou AMD8515 USB NIC */
213 {USB_VPI(USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ADM8515, 0)},
214 /* Kontron AG USB Ethernet */
215 {USB_VPI(USB_VENDOR_KONTRON, USB_PRODUCT_KONTRON_DM9601, 0)},
216 {USB_VPI(USB_VENDOR_KONTRON, USB_PRODUCT_KONTRON_JP1082,
217 UDAV_FLAG_NO_PHY)},
220 static void
221 udav_attach_post(struct usb_ether *ue)
223 struct udav_softc *sc = uether_getsc(ue);
225 /* reset the adapter */
226 udav_reset(sc);
228 /* Get Ethernet Address */
229 udav_csr_read(sc, UDAV_PAR, ue->ue_eaddr, ETHER_ADDR_LEN);
232 static int
233 udav_probe(device_t dev)
235 struct usb_attach_arg *uaa = device_get_ivars(dev);
237 if (uaa->usb_mode != USB_MODE_HOST)
238 return (ENXIO);
239 if (uaa->info.bConfigIndex != UDAV_CONFIG_INDEX)
240 return (ENXIO);
241 if (uaa->info.bIfaceIndex != UDAV_IFACE_INDEX)
242 return (ENXIO);
244 return (usbd_lookup_id_by_uaa(udav_devs, sizeof(udav_devs), uaa));
247 static int
248 udav_attach(device_t dev)
250 struct usb_attach_arg *uaa = device_get_ivars(dev);
251 struct udav_softc *sc = device_get_softc(dev);
252 struct usb_ether *ue = &sc->sc_ue;
253 uint8_t iface_index;
254 int error;
256 sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
258 device_set_usb_desc(dev);
260 lockinit(&sc->sc_lock, device_get_nameunit(dev), 0, 0);
262 iface_index = UDAV_IFACE_INDEX;
263 error = usbd_transfer_setup(uaa->device, &iface_index,
264 sc->sc_xfer, udav_config, UDAV_N_TRANSFER, sc, &sc->sc_lock);
265 if (error) {
266 device_printf(dev, "allocating USB transfers failed\n");
267 goto detach;
271 * The JP1082 has an unusable PHY and provides no link information.
273 if (sc->sc_flags & UDAV_FLAG_NO_PHY) {
274 ue->ue_methods = &udav_ue_methods_nophy;
275 sc->sc_flags |= UDAV_FLAG_LINK;
276 } else {
277 ue->ue_methods = &udav_ue_methods;
280 ue->ue_sc = sc;
281 ue->ue_dev = dev;
282 ue->ue_udev = uaa->device;
283 ue->ue_lock = &sc->sc_lock;
285 error = uether_ifattach(ue);
286 if (error) {
287 device_printf(dev, "could not attach interface\n");
288 goto detach;
291 return (0); /* success */
293 detach:
294 udav_detach(dev);
295 return (ENXIO); /* failure */
298 static int
299 udav_detach(device_t dev)
301 struct udav_softc *sc = device_get_softc(dev);
302 struct usb_ether *ue = &sc->sc_ue;
304 usbd_transfer_unsetup(sc->sc_xfer, UDAV_N_TRANSFER);
305 uether_ifdetach(ue);
306 lockuninit(&sc->sc_lock);
308 return (0);
311 #if 0
312 static int
313 udav_mem_read(struct udav_softc *sc, uint16_t offset, void *buf,
314 int len)
316 struct usb_device_request req;
318 len &= 0xff;
320 req.bmRequestType = UT_READ_VENDOR_DEVICE;
321 req.bRequest = UDAV_REQ_MEM_READ;
322 USETW(req.wValue, 0x0000);
323 USETW(req.wIndex, offset);
324 USETW(req.wLength, len);
326 return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
329 static int
330 udav_mem_write(struct udav_softc *sc, uint16_t offset, void *buf,
331 int len)
333 struct usb_device_request req;
335 len &= 0xff;
337 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
338 req.bRequest = UDAV_REQ_MEM_WRITE;
339 USETW(req.wValue, 0x0000);
340 USETW(req.wIndex, offset);
341 USETW(req.wLength, len);
343 return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
346 static int
347 udav_mem_write1(struct udav_softc *sc, uint16_t offset,
348 uint8_t ch)
350 struct usb_device_request req;
352 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
353 req.bRequest = UDAV_REQ_MEM_WRITE1;
354 USETW(req.wValue, ch);
355 USETW(req.wIndex, offset);
356 USETW(req.wLength, 0x0000);
358 return (uether_do_request(&sc->sc_ue, &req, NULL, 1000));
360 #endif
362 static int
363 udav_csr_read(struct udav_softc *sc, uint16_t offset, void *buf, int len)
365 struct usb_device_request req;
367 len &= 0xff;
369 req.bmRequestType = UT_READ_VENDOR_DEVICE;
370 req.bRequest = UDAV_REQ_REG_READ;
371 USETW(req.wValue, 0x0000);
372 USETW(req.wIndex, offset);
373 USETW(req.wLength, len);
375 return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
378 static int
379 udav_csr_write(struct udav_softc *sc, uint16_t offset, void *buf, int len)
381 struct usb_device_request req;
383 offset &= 0xff;
384 len &= 0xff;
386 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
387 req.bRequest = UDAV_REQ_REG_WRITE;
388 USETW(req.wValue, 0x0000);
389 USETW(req.wIndex, offset);
390 USETW(req.wLength, len);
392 return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
395 static uint8_t
396 udav_csr_read1(struct udav_softc *sc, uint16_t offset)
398 uint8_t val;
400 udav_csr_read(sc, offset, &val, 1);
401 return (val);
404 static int
405 udav_csr_write1(struct udav_softc *sc, uint16_t offset,
406 uint8_t ch)
408 struct usb_device_request req;
410 offset &= 0xff;
412 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
413 req.bRequest = UDAV_REQ_REG_WRITE1;
414 USETW(req.wValue, ch);
415 USETW(req.wIndex, offset);
416 USETW(req.wLength, 0x0000);
418 return (uether_do_request(&sc->sc_ue, &req, NULL, 1000));
421 static void
422 udav_init(struct usb_ether *ue)
424 struct udav_softc *sc = ue->ue_sc;
425 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
427 UDAV_LOCK_ASSERT(sc);
430 * Cancel pending I/O
432 udav_stop(ue);
434 /* set MAC address */
435 udav_csr_write(sc, UDAV_PAR, IF_LLADDR(ifp), ETHER_ADDR_LEN);
437 /* initialize network control register */
439 /* disable loopback */
440 UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_LBK0 | UDAV_NCR_LBK1);
442 /* Initialize RX control register */
443 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_DIS_LONG | UDAV_RCR_DIS_CRC);
445 /* load multicast filter and update promiscious mode bit */
446 udav_setpromisc(ue);
448 /* enable RX */
449 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_RXEN);
451 /* clear POWER_DOWN state of internal PHY */
452 UDAV_SETBIT(sc, UDAV_GPCR, UDAV_GPCR_GEP_CNTL0);
453 UDAV_CLRBIT(sc, UDAV_GPR, UDAV_GPR_GEPIO0);
455 usbd_xfer_set_stall(sc->sc_xfer[UDAV_BULK_DT_WR]);
457 #if 0 /* XXX */
458 ifp->if_drv_flags |= IFF_DRV_RUNNING;
459 #endif
460 udav_start(ue);
463 static void
464 udav_reset(struct udav_softc *sc)
466 int i;
468 /* Select PHY */
469 #if 1
471 * XXX: force select internal phy.
472 * external phy routines are not tested.
474 UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
475 #else
476 if (sc->sc_flags & UDAV_EXT_PHY)
477 UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
478 else
479 UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
480 #endif
482 UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_RST);
484 for (i = 0; i < UDAV_TX_TIMEOUT; i++) {
485 if (!(udav_csr_read1(sc, UDAV_NCR) & UDAV_NCR_RST))
486 break;
487 if (uether_pause(&sc->sc_ue, hz / 100))
488 break;
491 uether_pause(&sc->sc_ue, hz / 100);
494 #define UDAV_BITS 6
495 static void
496 udav_setmulti(struct usb_ether *ue)
498 struct udav_softc *sc = ue->ue_sc;
499 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
500 struct ifmultiaddr *ifma;
501 uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
502 int h = 0;
504 UDAV_LOCK_ASSERT(sc);
506 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
507 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC);
508 return;
511 /* first, zot all the existing hash bits */
512 memset(hashtbl, 0x00, sizeof(hashtbl));
513 hashtbl[7] |= 0x80; /* broadcast address */
514 udav_csr_write(sc, UDAV_MAR, hashtbl, sizeof(hashtbl));
516 /* now program new ones */
517 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
519 if (ifma->ifma_addr->sa_family != AF_LINK)
520 continue;
521 h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
522 ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
523 setbit(hashtbl, h);
526 /* disable all multicast */
527 UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_ALL);
529 /* write hash value to the register */
530 udav_csr_write(sc, UDAV_MAR, hashtbl, sizeof(hashtbl));
533 static void
534 udav_setpromisc(struct usb_ether *ue)
536 struct udav_softc *sc = ue->ue_sc;
537 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
538 uint8_t rxmode;
540 rxmode = udav_csr_read1(sc, UDAV_RCR);
541 rxmode &= ~(UDAV_RCR_ALL | UDAV_RCR_PRMSC);
543 if (ifp->if_flags & IFF_PROMISC)
544 rxmode |= UDAV_RCR_ALL | UDAV_RCR_PRMSC;
545 else if (ifp->if_flags & IFF_ALLMULTI)
546 rxmode |= UDAV_RCR_ALL;
548 /* write new mode bits */
549 udav_csr_write1(sc, UDAV_RCR, rxmode);
552 static void
553 udav_start(struct usb_ether *ue)
555 struct udav_softc *sc = ue->ue_sc;
558 * start the USB transfers, if not already started:
560 usbd_transfer_start(sc->sc_xfer[UDAV_INTR_DT_RD]);
561 usbd_transfer_start(sc->sc_xfer[UDAV_BULK_DT_RD]);
562 usbd_transfer_start(sc->sc_xfer[UDAV_BULK_DT_WR]);
565 static void
566 udav_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
568 struct udav_softc *sc = usbd_xfer_softc(xfer);
569 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
570 struct usb_page_cache *pc;
571 struct mbuf *m;
572 int extra_len;
573 int temp_len;
574 uint8_t buf[2];
576 switch (USB_GET_STATE(xfer)) {
577 case USB_ST_TRANSFERRED:
578 DPRINTFN(11, "transfer complete\n");
579 IFNET_STAT_INC(ifp, opackets, 1);
581 /* FALLTHROUGH */
582 case USB_ST_SETUP:
583 tr_setup:
584 if ((sc->sc_flags & UDAV_FLAG_LINK) == 0) {
586 * don't send anything if there is no link !
588 return;
590 m = ifq_dequeue(&ifp->if_snd);
592 if (m == NULL)
593 return;
594 if (m->m_pkthdr.len > MCLBYTES)
595 m->m_pkthdr.len = MCLBYTES;
596 if (m->m_pkthdr.len < UDAV_MIN_FRAME_LEN) {
597 extra_len = UDAV_MIN_FRAME_LEN - m->m_pkthdr.len;
598 } else {
599 extra_len = 0;
602 temp_len = (m->m_pkthdr.len + extra_len);
605 * the frame length is specified in the first 2 bytes of the
606 * buffer
608 buf[0] = (uint8_t)(temp_len);
609 buf[1] = (uint8_t)(temp_len >> 8);
611 temp_len += 2;
613 pc = usbd_xfer_get_frame(xfer, 0);
614 usbd_copy_in(pc, 0, buf, 2);
615 usbd_m_copy_in(pc, 2, m, 0, m->m_pkthdr.len);
617 if (extra_len)
618 usbd_frame_zero(pc, temp_len - extra_len, extra_len);
620 * if there's a BPF listener, bounce a copy
621 * of this frame to him:
623 BPF_MTAP(ifp, m);
625 m_freem(m);
627 usbd_xfer_set_frame_len(xfer, 0, temp_len);
628 usbd_transfer_submit(xfer);
629 return;
631 default: /* Error */
632 DPRINTFN(11, "transfer error, %s\n",
633 usbd_errstr(error));
635 IFNET_STAT_INC(ifp, oerrors, 1);
637 if (error != USB_ERR_CANCELLED) {
638 /* try to clear stall first */
639 usbd_xfer_set_stall(xfer);
640 goto tr_setup;
642 return;
646 static void
647 udav_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
649 struct udav_softc *sc = usbd_xfer_softc(xfer);
650 struct usb_ether *ue = &sc->sc_ue;
651 struct ifnet *ifp = uether_getifp(ue);
652 struct usb_page_cache *pc;
653 struct udav_rxpkt stat;
654 int len;
655 int actlen;
657 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
659 switch (USB_GET_STATE(xfer)) {
660 case USB_ST_TRANSFERRED:
662 if (actlen < sizeof(stat) + ETHER_CRC_LEN) {
663 IFNET_STAT_INC(ifp, ierrors, 1);
664 goto tr_setup;
666 pc = usbd_xfer_get_frame(xfer, 0);
667 usbd_copy_out(pc, 0, &stat, sizeof(stat));
668 actlen -= sizeof(stat);
669 len = min(actlen, le16toh(stat.pktlen));
670 len -= ETHER_CRC_LEN;
672 if (stat.rxstat & UDAV_RSR_LCS) {
673 IFNET_STAT_INC(ifp, collisions, 1);
674 goto tr_setup;
676 if (stat.rxstat & UDAV_RSR_ERR) {
677 IFNET_STAT_INC(ifp, ierrors, 1);
678 goto tr_setup;
680 uether_rxbuf(ue, pc, sizeof(stat), len);
681 /* FALLTHROUGH */
682 case USB_ST_SETUP:
683 tr_setup:
684 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
685 usbd_transfer_submit(xfer);
686 uether_rxflush(ue);
687 return;
689 default: /* Error */
690 DPRINTF("bulk read error, %s\n",
691 usbd_errstr(error));
693 if (error != USB_ERR_CANCELLED) {
694 /* try to clear stall first */
695 usbd_xfer_set_stall(xfer);
696 goto tr_setup;
698 return;
702 static void
703 udav_intr_callback(struct usb_xfer *xfer, usb_error_t error)
705 switch (USB_GET_STATE(xfer)) {
706 case USB_ST_TRANSFERRED:
707 case USB_ST_SETUP:
708 tr_setup:
709 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
710 usbd_transfer_submit(xfer);
711 return;
713 default: /* Error */
714 if (error != USB_ERR_CANCELLED) {
715 /* try to clear stall first */
716 usbd_xfer_set_stall(xfer);
717 goto tr_setup;
719 return;
723 static void
724 udav_stop(struct usb_ether *ue)
726 struct udav_softc *sc = ue->ue_sc;
727 #if 0 /* XXX */
728 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
729 #endif
731 UDAV_LOCK_ASSERT(sc);
733 #if 0 /* XXX */
734 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
735 #endif
736 if (!(sc->sc_flags & UDAV_FLAG_NO_PHY))
737 sc->sc_flags &= ~UDAV_FLAG_LINK;
740 * stop all the transfers, if not already stopped:
742 usbd_transfer_stop(sc->sc_xfer[UDAV_BULK_DT_WR]);
743 usbd_transfer_stop(sc->sc_xfer[UDAV_BULK_DT_RD]);
744 usbd_transfer_stop(sc->sc_xfer[UDAV_INTR_DT_RD]);
746 udav_reset(sc);
749 static int
750 udav_ifmedia_upd(struct ifnet *ifp)
752 struct udav_softc *sc = ifp->if_softc;
753 struct mii_data *mii = GET_MII(sc);
754 struct mii_softc *miisc;
755 int error;
757 UDAV_LOCK_ASSERT(sc);
759 sc->sc_flags &= ~UDAV_FLAG_LINK;
760 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
761 mii_phy_reset(miisc);
762 error = mii_mediachg(mii);
763 return (error);
766 static void
767 udav_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr)
769 struct udav_softc *sc = ifp->if_softc;
770 struct mii_data *mii = GET_MII(sc);
772 UDAV_LOCK(sc);
773 mii_pollstat(mii);
774 ifmr->ifm_active = mii->mii_media_active;
775 ifmr->ifm_status = mii->mii_media_status;
776 UDAV_UNLOCK(sc);
779 static void
780 udav_tick(struct usb_ether *ue)
782 struct udav_softc *sc = ue->ue_sc;
783 struct mii_data *mii = GET_MII(sc);
785 UDAV_LOCK_ASSERT(sc);
787 mii_tick(mii);
788 if ((sc->sc_flags & UDAV_FLAG_LINK) == 0
789 && mii->mii_media_status & IFM_ACTIVE &&
790 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
791 sc->sc_flags |= UDAV_FLAG_LINK;
792 udav_start(ue);
796 static int
797 udav_miibus_readreg(device_t dev, int phy, int reg)
799 struct udav_softc *sc = device_get_softc(dev);
800 uint16_t data16;
801 uint8_t val[2];
802 int locked;
804 /* XXX: one PHY only for the internal PHY */
805 if (phy != 0)
806 return (0);
808 locked = lockowned(&sc->sc_lock);
809 if (!locked)
810 UDAV_LOCK(sc);
812 /* select internal PHY and set PHY register address */
813 udav_csr_write1(sc, UDAV_EPAR,
814 UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
816 /* select PHY operation and start read command */
817 udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRR);
819 /* XXX: should we wait? */
821 /* end read command */
822 UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRR);
824 /* retrieve the result from data registers */
825 udav_csr_read(sc, UDAV_EPDRL, val, 2);
827 data16 = (val[0] | (val[1] << 8));
829 DPRINTFN(11, "phy=%d reg=0x%04x => 0x%04x\n",
830 phy, reg, data16);
832 if (!locked)
833 UDAV_UNLOCK(sc);
834 return (data16);
837 static int
838 udav_miibus_writereg(device_t dev, int phy, int reg, int data)
840 struct udav_softc *sc = device_get_softc(dev);
841 uint8_t val[2];
842 int locked;
844 /* XXX: one PHY only for the internal PHY */
845 if (phy != 0)
846 return (0);
848 locked = lockowned(&sc->sc_lock);
849 if (!locked)
850 UDAV_LOCK(sc);
852 /* select internal PHY and set PHY register address */
853 udav_csr_write1(sc, UDAV_EPAR,
854 UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
856 /* put the value to the data registers */
857 val[0] = (data & 0xff);
858 val[1] = (data >> 8) & 0xff;
859 udav_csr_write(sc, UDAV_EPDRL, val, 2);
861 /* select PHY operation and start write command */
862 udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRW);
864 /* XXX: should we wait? */
866 /* end write command */
867 UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRW);
869 if (!locked)
870 UDAV_UNLOCK(sc);
871 return (0);
874 static void
875 udav_miibus_statchg(device_t dev)
877 /* nothing to do */