/* $OpenBSD: if_mos.c,v 1.7 2009/10/13 19:33:17 pirofti Exp $ */ #define USB_DEBUG 1 /* * Copyright (c) 2010 Rick van der Zwet * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ /* * Copyright (c) 2008 Johann Christian Rode * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ /* * Copyright (c) 2005, 2006, 2007 Jonathan Gray * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ /* * Copyright (c) 1997, 1998, 1999, 2000-2003 * Bill Paul . All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by Bill Paul. * 4. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ #include /* * Moschip MCS7730/MCS7830 USB to Ethernet controller * The datasheet is available at the following URL: * http://www.moschip.com/data/products/MCS7830/Data%20Sheet_7830.pdf */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "usbdevs.h" #define USB_DEBUG_VAR mos_debug #include #include #include //#include #include "if_mosreg.h" #ifdef USB_DEBUG static int mos_debug = 10; SYSCTL_NODE(_hw_usb, OID_AUTO, mos, CTLFLAG_RW, 0, "USB mos"); SYSCTL_INT(_hw_usb_mos, OID_AUTO, debug, CTLFLAG_RW, &mos_debug, 0, "Debug level"); #endif #define USB_PRODUCT_MOSCHIP_MCS7730 0x7730 /* MCS7730 Ethernet */ #define USB_PRODUCT_SITECOMEU_LN030 0x0021 /* LN-030 */ /* * Various supported device vendors/products. */ static const struct usb_device_id mos_devs[] = { { USB_VPI(USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7730, MCS7730) }, { USB_VPI(USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7830 , MCS7830) }, { USB_VPI( USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_LN030 , MCS7830) }, }; int mos_probe(device_t dev); int mos_attach(device_t dev); void mos_attach_post(struct usb_ether *ue); int mos_detach(device_t dev); int mos_encap(struct mos_softc *, struct mbuf *, int); void mos_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error); void mos_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error); void mos_intr_callback(struct usb_xfer *xfer, usb_error_t error); void mos_tick(struct usb_ether *); void mos_start(struct usb_ether *); void mos_init(struct usb_ether *); void mos_chip_init(struct mos_softc *); void mos_stop(struct usb_ether *); void mos_watchdog(struct usb_ether *); int mos_miibus_readreg(device_t , int, int); int mos_miibus_writereg(device_t , int, int, int); void mos_miibus_statchg(device_t); int mos_ifmedia_upd(struct ifnet *); void mos_ifmedia_sts(struct ifnet *, struct ifmediareq *); void mos_reset(struct mos_softc *sc); int mos_reg_read_1(struct mos_softc *, int); int mos_reg_read_2(struct mos_softc *, int); int mos_reg_write_1(struct mos_softc *, int, int); int mos_reg_write_2(struct mos_softc *, int, int); int mos_readmac(struct mos_softc *, u_char *); int mos_writemac(struct mos_softc *, u_char *); int mos_write_mcast(struct mos_softc *, u_char *); void mos_setmulti(struct usb_ether *); static const struct usb_config mos_config[MOS_ENDPT_MAX] = { [MOS_ENDPT_TX] = { .type = UE_BULK, .endpoint = UE_ADDR_ANY, .direction = UE_DIR_OUT, .bufsize = (MCLBYTES + 2), .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, .callback = mos_bulk_write_callback, .timeout = 10000, /* 10 seconds */ }, [MOS_ENDPT_RX] = { .type = UE_BULK, .endpoint = UE_ADDR_ANY, .direction = UE_DIR_IN, .bufsize = (MCLBYTES + 4 + ETHER_CRC_LEN), .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, .callback = mos_bulk_read_callback, }, [MOS_ENDPT_INTR] = { .type = UE_INTERRUPT, .endpoint = UE_ADDR_ANY, .direction = UE_DIR_IN, .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, .bufsize = 0, /* use wMaxPacketSize */ .callback = mos_intr_callback, }, }; static device_method_t mos_methods[] = { /* Device interface */ DEVMETHOD(device_probe, mos_probe), DEVMETHOD(device_attach, mos_attach), DEVMETHOD(device_detach, mos_detach), /* bus interface */ DEVMETHOD(bus_print_child, bus_generic_print_child), DEVMETHOD(bus_driver_added, bus_generic_driver_added), /* MII interface */ DEVMETHOD(miibus_readreg, mos_miibus_readreg), DEVMETHOD(miibus_writereg, mos_miibus_writereg), DEVMETHOD(miibus_statchg, mos_miibus_statchg), {0, 0} }; static driver_t mos_driver = { .name = "mos", .methods = mos_methods, .size = sizeof(struct mos_softc) }; static devclass_t mos_devclass; DRIVER_MODULE(mos, uhub, mos_driver, mos_devclass, NULL, 0); DRIVER_MODULE(miibus, mos, miibus_driver, miibus_devclass, 0, 0); MODULE_DEPEND(mos, uether, 1, 1, 1); MODULE_DEPEND(mos, usb, 1, 1, 1); MODULE_DEPEND(mos, ether, 1, 1, 1); MODULE_DEPEND(mos, miibus, 1, 1, 1); static const struct usb_ether_methods mos_ue_methods = { .ue_attach_post = mos_attach_post, .ue_start = mos_start, .ue_init = mos_init, .ue_stop = mos_stop, .ue_tick = mos_tick, .ue_setmulti = mos_setmulti, //.ue_setpromisc = mos_setpromisc, .ue_mii_upd = mos_ifmedia_upd, .ue_mii_sts = mos_ifmedia_sts, }; int mos_reg_read_1(struct mos_softc *sc, int reg) { struct usb_device_request req; usb_error_t err; uByte val = 0; req.bmRequestType = UT_READ_VENDOR_DEVICE; req.bRequest = MOS_UR_READREG; USETW(req.wValue, 0); USETW(req.wIndex, reg); USETW(req.wLength, 1); err = uether_do_request(&sc->sc_ue, &req, &val, 1000); if (err) { DPRINTF("mos_reg_read_1 error, reg: %d\n", reg); return (-1); } return (val); } int mos_reg_read_2(struct mos_softc *sc, int reg) { struct usb_device_request req; usb_error_t err; uWord val; USETW(val,0); req.bmRequestType = UT_READ_VENDOR_DEVICE; req.bRequest = MOS_UR_READREG; USETW(req.wValue, 0); USETW(req.wIndex, reg); USETW(req.wLength, 2); err = uether_do_request(&sc->sc_ue, &req, &val, 1000); if (err) { DPRINTF("mos_reg_read_2 error, reg: %d\n", reg); return (-1); } return(UGETW(val)); } int mos_reg_write_1(struct mos_softc *sc, int reg, int aval) { struct usb_device_request req; usb_error_t err; uByte val; val = aval; req.bmRequestType = UT_WRITE_VENDOR_DEVICE; req.bRequest = MOS_UR_WRITEREG; USETW(req.wValue, 0); USETW(req.wIndex, reg); USETW(req.wLength, 1); err = uether_do_request(&sc->sc_ue, &req, &val, 1000); if (err) { DPRINTF("mos_reg_write_1 error, reg: %d\n", reg); return (-1); } return(0); } int mos_reg_write_2(struct mos_softc *sc, int reg, int aval) { struct usb_device_request req; usb_error_t err; uWord val; USETW(val, aval); req.bmRequestType = UT_WRITE_VENDOR_DEVICE; req.bRequest = MOS_UR_WRITEREG; USETW(req.wValue, 0); USETW(req.wIndex, reg); USETW(req.wLength, 2); err = uether_do_request(&sc->sc_ue, &req, &val, 1000); if (err) { DPRINTF("mos_reg_write_2 error, reg: %d\n", reg); return (-1); } return (0); } int mos_readmac(struct mos_softc *sc, u_char *mac) { struct usb_device_request req; usb_error_t err; req.bmRequestType = UT_READ_VENDOR_DEVICE; req.bRequest = MOS_UR_READREG; USETW(req.wValue, 0); USETW(req.wIndex, MOS_MAC); USETW(req.wLength, ETHER_ADDR_LEN); err = uether_do_request(&sc->sc_ue, &req, mac, 1000); if (err) { DPRINTF("mos_readmac error"); return (-1); } return (0); } int mos_writemac(struct mos_softc *sc, u_char *mac) { struct usb_device_request req; usb_error_t err; req.bmRequestType = UT_WRITE_VENDOR_DEVICE; req.bRequest = MOS_UR_WRITEREG; USETW(req.wValue, 0); USETW(req.wIndex, MOS_MAC); USETW(req.wLength, ETHER_ADDR_LEN); err = uether_do_request(&sc->sc_ue, &req, mac, 1000); if (err) { DPRINTF("mos_writemac error"); return (-1); } return (0); } int mos_write_mcast(struct mos_softc *sc, u_char *hashtbl) { struct usb_device_request req; usb_error_t err; req.bmRequestType = UT_WRITE_VENDOR_DEVICE; req.bRequest = MOS_UR_WRITEREG; USETW(req.wValue, 0); USETW(req.wIndex, MOS_MCAST_TABLE); USETW(req.wLength, 8); err = uether_do_request(&sc->sc_ue, &req, hashtbl, 1000); if (err) { DPRINTF("mos_reg_mcast error\n"); return(-1); } return(0); } int mos_miibus_readreg(struct device *dev, int phy, int reg) { struct mos_softc *sc = (void *)dev; uWord val; int i,res, locked; USETW(val, 0); locked = mtx_owned(&sc->sc_mtx); if (!locked) MOS_LOCK(sc); mos_reg_write_2(sc, MOS_PHY_DATA, 0); mos_reg_write_1(sc, MOS_PHY_CTL, (phy & MOS_PHYCTL_PHYADDR) | MOS_PHYCTL_READ); mos_reg_write_1(sc, MOS_PHY_STS, (reg & MOS_PHYSTS_PHYREG) | MOS_PHYSTS_PENDING); for (i = 0; i < MOS_TIMEOUT; i++) { if (mos_reg_read_1(sc, MOS_PHY_STS) & MOS_PHYSTS_READY) break; } if (i == MOS_TIMEOUT) { printf("%s: MII read timeout\n", "mos"); } res = mos_reg_read_2(sc, MOS_PHY_DATA); if (!locked) MOS_UNLOCK(sc); return (res); } int mos_miibus_writereg(device_t dev, int phy, int reg, int val) { struct mos_softc *sc = (void *)dev; int i, locked; locked = mtx_owned(&sc->sc_mtx); if (!locked) MOS_LOCK(sc); mos_reg_write_2(sc, MOS_PHY_DATA, val); mos_reg_write_1(sc, MOS_PHY_CTL, (phy & MOS_PHYCTL_PHYADDR) | MOS_PHYCTL_WRITE); mos_reg_write_1(sc, MOS_PHY_STS, (reg & MOS_PHYSTS_PHYREG) | MOS_PHYSTS_PENDING); for (i = 0; i < MOS_TIMEOUT; i++) { if (mos_reg_read_1(sc, MOS_PHY_STS) & MOS_PHYSTS_READY) break; } if (i == MOS_TIMEOUT) { printf("%s: MII write timeout\n", "mos"); } if (!locked) MOS_UNLOCK(sc); return 0; } void mos_miibus_statchg(device_t dev) { struct mos_softc *sc = device_get_softc(dev); struct mii_data *mii = GET_MII(sc); int val, err, locked; locked = mtx_owned(&sc->sc_mtx); if (!locked) MOS_LOCK(sc); /* disable RX, TX prior to changing FDX, SPEEDSEL */ val = mos_reg_read_1(sc, MOS_CTL); val &= ~(MOS_CTL_TX_ENB | MOS_CTL_RX_ENB); mos_reg_write_1(sc, MOS_CTL, val); /* reset register which counts dropped frames */ mos_reg_write_1(sc, MOS_FRAME_DROP_CNT, 0); if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) val |= MOS_CTL_FDX_ENB; else val &= ~(MOS_CTL_FDX_ENB); switch (IFM_SUBTYPE(mii->mii_media_active)) { case IFM_100_TX: val |= MOS_CTL_SPEEDSEL; break; case IFM_10_T: val &= ~(MOS_CTL_SPEEDSEL); break; } /* re-enable TX, RX */ val |= (MOS_CTL_TX_ENB | MOS_CTL_RX_ENB); err = mos_reg_write_1(sc, MOS_CTL, val); MOS_UNLOCK(sc); if (err) printf("%s: media change failed\n", "mos"); if (!locked) MOS_UNLOCK(sc); } /* * Set media options. */ int mos_ifmedia_upd(struct ifnet *ifp) { struct mos_softc *sc = ifp->if_softc; struct mii_data *mii = GET_MII(sc); sc->mos_link = 0; if (mii->mii_instance) { struct mii_softc *miisc; LIST_FOREACH(miisc, &mii->mii_phys, mii_list) mii_phy_reset(miisc); } mii_mediachg(mii); return (0); } /* * Report current media status. */ void mos_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) { struct mos_softc *sc = ifp->if_softc; struct mii_data *mii = GET_MII(sc); mii_pollstat(mii); ifmr->ifm_active = mii->mii_media_active; ifmr->ifm_status = mii->mii_media_status; } void mos_setmulti(struct usb_ether *ue) { struct mos_softc *sc = uether_getsc(ue); struct ifnet *ifp = uether_getifp(ue); //struct ether_multi *enm; struct ifmultiaddr *ifma; u_int32_t h = 0; u_int8_t rxmode; u_int8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; rxmode = mos_reg_read_1(sc, MOS_CTL); if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { allmulti: rxmode |= MOS_CTL_ALLMULTI; mos_reg_write_1(sc, MOS_CTL, rxmode); return; } else rxmode &= ~MOS_CTL_ALLMULTI; /* now program new ones */ if_maddr_rlock(ifp); TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { if (ifma->ifma_addr->sa_family != AF_LINK) goto allmulti; h = ether_crc32_be(LLADDR((struct sockaddr_dl *) ifma->ifma_addr), ETHER_ADDR_LEN) >> 26; //h = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26; hashtbl[h / 8] |= 1 << (h % 8); } if_maddr_runlock(ifp); ifp->if_flags &= ~IFF_ALLMULTI; mos_write_mcast(sc, (void *)&hashtbl); mos_reg_write_1(sc, MOS_CTL, rxmode); return; } void mos_reset(struct mos_softc *sc) { u_int8_t ctl; ctl = mos_reg_read_1(sc, MOS_CTL); ctl &= ~(MOS_CTL_RX_PROMISC | MOS_CTL_ALLMULTI | MOS_CTL_TX_ENB | MOS_CTL_RX_ENB); /* Disable RX, TX, promiscuous and allmulticast mode */ mos_reg_write_1(sc, MOS_CTL, ctl); /* Reset frame drop counter register to zero */ mos_reg_write_1(sc, MOS_FRAME_DROP_CNT, 0); /* Wait a little while for the chip to get its brains in order. */ DELAY(1000); return; } void mos_chip_init(struct mos_softc *sc) { int i; /* * Rev.C devices have a pause threshold register which needs to be set * at startup. */ if (mos_reg_read_1(sc, MOS_PAUSE_TRHD) != -1) { for (i=0;imos_phyaddrs[0] = 1; sc->mos_phyaddrs[1] = 0xFF; } /* * Probe for a MCS7x30 chip. */ int mos_probe(device_t dev) { DPRINTF("mos: probe\n"); struct usb_attach_arg *uaa = device_get_ivars(dev); if (uaa->usb_mode != USB_MODE_HOST) { DPRINTF("mos: not USB_MODE_HOST\n"); return (ENXIO); } //uint8_t bIfaceIndex; //uint8_t bIfaceNum; //uint8_t bConfigIndex; //uint8_t bConfigNum; if (uaa->info.bConfigNum != MOS_CONFIG_NO) { DPRINTF("mos: not MOS_CONFIG_NO %i vs %i\n", uaa->info.bConfigNum , MOS_CONFIG_NO); return (ENXIO); } if (uaa->info.bIfaceIndex != MOS_IFACE_IDX) { DPRINTF("mos: not MOS_IFACE_IDX %i vs %i\n", uaa->info.bIfaceIndex, MOS_IFACE_IDX); return (ENXIO); } DPRINTF("mos: probe by generic means\n"); int retval = usbd_lookup_id_by_uaa(mos_devs, sizeof(mos_devs), uaa); DPRINTF("mos: probe retval: %i (ENXIO:%i)\n", retval, ENXIO); return retval; } /* * Attach the interface. Allocate softc structures, do ifmedia * setup and ethernet/BPF attach. */ int mos_attach(device_t dev) { struct usb_attach_arg *uaa = device_get_ivars(dev); struct mos_softc *sc = device_get_softc(dev); struct usb_ether *ue = &sc->sc_ue; uint8_t iface_index; int error; sc->mos_flags = USB_GET_DRIVER_INFO(uaa); device_set_usb_desc(dev); mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF); iface_index = MOS_IFACE_IDX; error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer, mos_config, MOS_CONFIG_NO, sc, &sc->sc_mtx); if (error) { device_printf(dev, "allocating USB transfers failed\n"); goto detach; } ue->ue_sc = sc; ue->ue_dev = dev; ue->ue_udev = uaa->device; ue->ue_mtx = &sc->sc_mtx; ue->ue_methods = &mos_ue_methods; DPRINTFN(11,"%s:", "mos"); if (sc->mos_flags & MCS7730) { DPRINTFN(11," MCS7730"); } else if (sc->mos_flags & MCS7830) { DPRINTFN(11," MCS7830"); } error = uether_ifattach(ue); if (error) { device_printf(dev, "could not attach interface\n"); goto detach; } return (0); /* success */ detach: mos_detach(dev); return (ENXIO); /* failure */ } void mos_attach_post(struct usb_ether *ue) { struct mos_softc *sc = uether_getsc(ue); /* * Read MAC address, inform the world. */ // int err = mos_readmac(sc, (void*)&eaddr); // if (err) { // printf("%s: couldn't get MAC address\n", // "mos"); // return; // } // bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN); // printf(", address %s\n", ether_sprintf(eaddr)); mos_chip_init(sc); sc->mos_attached = 1; } int mos_detach(device_t dev) { struct mos_softc *sc = device_get_softc(dev); struct usb_ether *ue = &sc->sc_ue; usbd_transfer_unsetup(sc->sc_xfer, MOS_CONFIG_NO); uether_ifdetach(ue); mtx_destroy(&sc->sc_mtx); return (0); } /* * A frame has been uploaded: pass the resulting mbuf chain up to * the higher level protocols. */ void mos_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) { struct mos_softc *sc = usbd_xfer_softc(xfer); struct usb_ether *ue = &sc->sc_ue; struct ifnet *ifp = uether_getifp(ue); u_int8_t rxstat = 0; u_int32_t actlen; u_int16_t pktlen = 0; struct mbuf *stat; uint8_t status; struct usb_page_cache *pc; DPRINTFN(10, "%s: %s: enter\n", "mos",__func__); usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); pc = usbd_xfer_get_frame(xfer, 0); status = USB_GET_STATE(xfer); if (status != USB_ST_SETUP) { if (status == USB_ST_TRANSFERRED) { if (actlen <= 1) goto done; usbd_copy_out(pc, actlen - sizeof(stat), &stat, sizeof(stat)); /* evaluate status byte at the end */ pktlen = actlen - 1; //rxstat = stat[pktlen] & MOS_RXSTS_MASK; m_freem(stat); if (rxstat != MOS_RXSTS_VALID) { DPRINTF("%s: erroneous frame received: ", "mos"); if (rxstat & MOS_RXSTS_SHORT_FRAME) DPRINTF("frame size less than 64 bytes\n"); if (rxstat & MOS_RXSTS_LARGE_FRAME) DPRINTF("frame size larger than 1532 bytes\n"); if (rxstat & MOS_RXSTS_CRC_ERROR) DPRINTF("CRC error\n"); if (rxstat & MOS_RXSTS_ALIGN_ERROR) DPRINTF("alignment error\n"); ifp->if_ierrors++; goto done; } if ( pktlen < sizeof(struct ether_header) ) { ifp->if_ierrors++; goto done; } uether_rxbuf(ue, pc, 0, actlen); } else { DPRINTF("bulk read error, %s\n", usbd_errstr(error)); if (error == USB_ERR_CANCELLED) { return; } else { /* try to clear stall first */ usbd_xfer_set_stall(xfer); goto done; } } } done: usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); usbd_transfer_submit(xfer); uether_rxflush(ue); DPRINTFN(10,"%s: %s: start rx\n", "mos", __func__); return; } /* * A frame was downloaded to the chip. It's safe for us to clean up * the list buffers. */ void mos_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) { struct mos_softc *sc = usbd_xfer_softc(xfer); struct ifnet *ifp = uether_getifp(&sc->sc_ue); struct usb_page_cache *pc; struct mbuf *m; uint8_t status; int actlen; usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); pc = usbd_xfer_get_frame(xfer, 0); status = USB_GET_STATE(xfer); if (status != USB_ST_SETUP) { if (status == USB_ST_TRANSFERRED) { DPRINTFN(11, "transfer of %d bytes complete\n", actlen); ifp->if_opackets++; } else { DPRINTFN(11, "%s: usb error on tx: %s\n", "mos", usbd_errstr(status)); ifp->if_oerrors++; if (error == USB_ERR_CANCELLED) { return; } } } IFQ_DRV_DEQUEUE(&ifp->if_snd, m); usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len); usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len); /* * if there's a BPF listener, bounce a copy * of this frame to him: */ BPF_MTAP(ifp, m); m_freem(m); usbd_transfer_submit(xfer); if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) mos_start(&sc->sc_ue); ifp->if_opackets++; return; } void mos_tick(struct usb_ether *ue) { struct mos_softc *sc = uether_getsc(ue); struct ifnet *ifp = uether_getifp(ue); struct mii_data *mii = GET_MII(sc); if (sc == NULL) return; DPRINTFN(0xff,"%s: %s: enter\n", "mos", __func__); mii_tick(mii); if (!sc->mos_link && mii->mii_media_status & IFM_ACTIVE && IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { DPRINTF("%s: %s: got link\n", "mos", __func__); sc->mos_link++; if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) mos_start(&sc->sc_ue); } } //int //mos_encap(struct mos_softc *sc, struct mbuf *m, int idx) //{ // struct mos_chain *c; // int err; // int length; // // c = &sc->mos_cdata.mos_tx_chain[idx]; // // m_copydata(m, 0, m->m_pkthdr.len, c->mos_buf); // length = m->m_pkthdr.len; // // c->mos_mbuf = m; // // usbd_setup_xfer(c->mos_xfer, sc->mos_ep[MOS_ENDPT_TX], // c, c->mos_buf, length, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, // 10000, mos_txeof); // // /* Transmit */ // err = usbd_transfer(c->mos_xfer); // if (err != USBD_IN_PROGRESS) { // mos_stop(sc); // return(EIO); // } // // sc->mos_cdata.mos_tx_cnt++; // // return(0); //} void mos_start(struct usb_ether *ue) { struct mos_softc *sc = uether_getsc(ue); struct ifnet *ifp = uether_getifp(ue); struct mbuf *m_head = NULL; sc = ifp->if_softc; if (!sc->mos_link) return; //if (ifp->if_flags & IFF_OACTIVE) // return; IFQ_POLL(&ifp->if_snd, m_head); if (m_head == NULL) return; //if (mos_encap(sc, m_head, 0)) { // ifp->if_flags |= IFF_OACTIVE; // return; //} IFQ_DEQUEUE(&ifp->if_snd, m_head); /* * If there's a BPF listener, bounce a copy of this frame * to him. */ BPF_MTAP(ifp, m_head); /* * Set a timeout in case the chip goes out to lunch. */ //ifp->if_timer = 5; return; } void mos_init(struct usb_ether *ue) { struct mos_softc *sc = uether_getsc(ue); struct ifnet *ifp = uether_getifp(ue); u_int8_t rxmode; /* * Cancel pending I/O and free all RX/TX buffers. */ mos_reset(sc); /* * Write MAC address */ //mos_writemac(sc, sc->arpcom.ac_enaddr); /* Read and set transmitter IPG values */ sc->mos_ipgs[0] = mos_reg_read_1(sc, MOS_IPG0); sc->mos_ipgs[1] = mos_reg_read_1(sc, MOS_IPG1); mos_reg_write_1(sc, MOS_IPG0, sc->mos_ipgs[0]); mos_reg_write_1(sc, MOS_IPG1, sc->mos_ipgs[1]); /* Enable receiver and transmitter, bridge controls speed/duplex mode */ rxmode = mos_reg_read_1(sc, MOS_CTL); rxmode |= MOS_CTL_RX_ENB | MOS_CTL_TX_ENB | MOS_CTL_BS_ENB; rxmode &= ~(MOS_CTL_SLEEP); /* If we want promiscuous mode, set the allframes bit. */ if (ifp->if_flags & IFF_PROMISC) rxmode |= MOS_CTL_RX_PROMISC; /* XXX: broadcast mode? */ mos_reg_write_1(sc, MOS_CTL, rxmode); /* Load the multicast filter. */ mos_setmulti(ue); mii_mediachg(GET_MII(sc)); ifp->if_flags |= IFF_DRV_RUNNING; mos_start(ue); } void mos_intr_callback(struct usb_xfer *xfer, usb_error_t error) { struct mos_softc *sc = usbd_xfer_softc(xfer); struct ifnet *ifp = uether_getifp(&sc->sc_ue); int actlen; ifp->if_oerrors++; printf("%s: watchdog timeout\n", "mos"); usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); //XXX: Porting, how? mos_txeof(c->mos_xfer, c, stat); if (!IFQ_IS_EMPTY(&ifp->if_snd)) mos_start(&sc->sc_ue); } /* * Stop the adapter and free any mbufs allocated to the * RX and TX lists. */ void mos_stop(struct usb_ether *ue) { struct mos_softc *sc = uether_getsc(ue); struct ifnet *ifp = uether_getifp(ue); mos_reset(sc); ifp->if_flags &= ~IFF_DRV_RUNNING; /* * stop all the transfers, if not already stopped: */ usbd_transfer_stop(sc->sc_xfer[MOS_ENDPT_TX]); usbd_transfer_stop(sc->sc_xfer[MOS_ENDPT_RX]); usbd_transfer_stop(sc->sc_xfer[MOS_ENDPT_INTR]); sc->mos_link = 0; }