Index: head/sys/dev/usb/if_aue.c =================================================================== --- head/sys/dev/usb/if_aue.c (revision 67529) +++ head/sys/dev/usb/if_aue.c (revision 67530) @@ -1,1555 +1,1570 @@ /* * Copyright (c) 1997, 1998, 1999, 2000 * 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. * * $FreeBSD$ */ /* * ADMtek AN986 Pegasus USB to ethernet driver. Datasheet is available * from http://www.admtek.com.tw. * * Written by Bill Paul * Electrical Engineering Department * Columbia University, New York City */ /* * The Pegasus chip uses four USB "endpoints" to provide 10/100 ethernet * support: the control endpoint for reading/writing registers, burst * read endpoint for packet reception, burst write for packet transmission * and one for "interrupts." The chip uses the same RX filter scheme * as the other ADMtek ethernet parts: one perfect filter entry for the * the station address and a 64-bit multicast hash table. The chip supports * both MII and HomePNA attachments. * * Since the maximum data transfer speed of USB is supposed to be 12Mbps, * you're never really going to get 100Mbps speeds from this device. I * think the idea is to allow the device to connect to 10 or 100Mbps * networks, not necessarily to provide 100Mbps performance. Also, since * the controller uses an external PHY chip, it's possible that board * designers might simply choose a 10Mbps PHY. * * Registers are accessed using usbd_do_request(). Packet transfers are * done using usbd_transfer() and friends. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include MODULE_DEPEND(aue, miibus, 1, 1, 1); /* "controller miibus0" required. See GENERIC if you get errors here. */ #include "miibus_if.h" #ifndef lint static const char rcsid[] = "$FreeBSD$"; #endif /* * Various supported device vendors/products. */ Static struct aue_type aue_devs[] = { { USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUS }, { USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB100 }, { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX }, { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650 }, { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX }, { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX_PNA }, { USB_VENDOR_SMC, USB_PRODUCT_SMC_2202USB }, { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100TX }, { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TA }, { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TX }, { USB_VENDOR_KINGSTON, USB_PRODUCT_KINGSTON_KNU101TX, }, { 0, 0 } }; Static struct usb_qdat aue_qdat; Static int aue_match __P((device_t)); Static int aue_attach __P((device_t)); Static int aue_detach __P((device_t)); Static int aue_tx_list_init __P((struct aue_softc *)); Static int aue_rx_list_init __P((struct aue_softc *)); Static int aue_newbuf __P((struct aue_softc *, struct aue_chain *, struct mbuf *)); Static int aue_encap __P((struct aue_softc *, struct mbuf *, int)); #ifdef AUE_INTR_PIPE Static void aue_intr __P((usbd_xfer_handle, usbd_private_handle, usbd_status)); #endif Static void aue_rxeof __P((usbd_xfer_handle, usbd_private_handle, usbd_status)); Static void aue_txeof __P((usbd_xfer_handle, usbd_private_handle, usbd_status)); Static void aue_tick __P((void *)); Static void aue_rxstart __P((struct ifnet *)); Static void aue_start __P((struct ifnet *)); Static int aue_ioctl __P((struct ifnet *, u_long, caddr_t)); Static void aue_init __P((void *)); Static void aue_stop __P((struct aue_softc *)); Static void aue_watchdog __P((struct ifnet *)); Static void aue_shutdown __P((device_t)); Static int aue_ifmedia_upd __P((struct ifnet *)); Static void aue_ifmedia_sts __P((struct ifnet *, struct ifmediareq *)); Static void aue_eeprom_getword __P((struct aue_softc *, int, u_int16_t *)); Static void aue_read_eeprom __P((struct aue_softc *, caddr_t, int, int, int)); Static int aue_miibus_readreg __P((device_t, int, int)); Static int aue_miibus_writereg __P((device_t, int, int, int)); Static void aue_miibus_statchg __P((device_t)); Static void aue_setmulti __P((struct aue_softc *)); Static u_int32_t aue_crc __P((caddr_t)); Static void aue_reset __P((struct aue_softc *)); Static int csr_read_1 __P((struct aue_softc *, int)); Static int csr_write_1 __P((struct aue_softc *, int, int)); Static int csr_read_2 __P((struct aue_softc *, int)); Static int csr_write_2 __P((struct aue_softc *, int, int)); Static device_method_t aue_methods[] = { /* Device interface */ DEVMETHOD(device_probe, aue_match), DEVMETHOD(device_attach, aue_attach), DEVMETHOD(device_detach, aue_detach), DEVMETHOD(device_shutdown, aue_shutdown), /* bus interface */ DEVMETHOD(bus_print_child, bus_generic_print_child), DEVMETHOD(bus_driver_added, bus_generic_driver_added), /* MII interface */ DEVMETHOD(miibus_readreg, aue_miibus_readreg), DEVMETHOD(miibus_writereg, aue_miibus_writereg), DEVMETHOD(miibus_statchg, aue_miibus_statchg), { 0, 0 } }; Static driver_t aue_driver = { "aue", aue_methods, sizeof(struct aue_softc) }; Static devclass_t aue_devclass; DRIVER_MODULE(if_aue, uhub, aue_driver, aue_devclass, usbd_driver_load, 0); DRIVER_MODULE(miibus, aue, miibus_driver, miibus_devclass, 0, 0); #define AUE_SETBIT(sc, reg, x) \ csr_write_1(sc, reg, csr_read_1(sc, reg) | (x)) #define AUE_CLRBIT(sc, reg, x) \ csr_write_1(sc, reg, csr_read_1(sc, reg) & ~(x)) Static int csr_read_1(sc, reg) struct aue_softc *sc; int reg; { usb_device_request_t req; usbd_status err; u_int8_t val = 0; - int s; if (sc->aue_gone) return(0); - s = splusb(); + AUE_LOCK(sc); req.bmRequestType = UT_READ_VENDOR_DEVICE; req.bRequest = AUE_UR_READREG; USETW(req.wValue, 0); USETW(req.wIndex, reg); USETW(req.wLength, 1); err = usbd_do_request_flags(sc->aue_udev, &req, &val, USBD_NO_TSLEEP, NULL); - splx(s); + AUE_UNLOCK(sc); if (err) return(0); return(val); } Static int csr_read_2(sc, reg) struct aue_softc *sc; int reg; { usb_device_request_t req; usbd_status err; u_int16_t val = 0; - int s; if (sc->aue_gone) return(0); - s = splusb(); + AUE_LOCK(sc); req.bmRequestType = UT_READ_VENDOR_DEVICE; req.bRequest = AUE_UR_READREG; USETW(req.wValue, 0); USETW(req.wIndex, reg); USETW(req.wLength, 2); err = usbd_do_request_flags(sc->aue_udev, &req, &val, USBD_NO_TSLEEP, NULL); - splx(s); + AUE_UNLOCK(sc); if (err) return(0); return(val); } Static int csr_write_1(sc, reg, val) struct aue_softc *sc; int reg, val; { usb_device_request_t req; usbd_status err; - int s; if (sc->aue_gone) return(0); - s = splusb(); + AUE_LOCK(sc); req.bmRequestType = UT_WRITE_VENDOR_DEVICE; req.bRequest = AUE_UR_WRITEREG; USETW(req.wValue, val); USETW(req.wIndex, reg); USETW(req.wLength, 1); err = usbd_do_request_flags(sc->aue_udev, &req, &val, USBD_NO_TSLEEP, NULL); - splx(s); + AUE_UNLOCK(sc); if (err) return(-1); return(0); } Static int csr_write_2(sc, reg, val) struct aue_softc *sc; int reg, val; { usb_device_request_t req; usbd_status err; - int s; if (sc->aue_gone) return(0); - s = splusb(); + AUE_LOCK(sc); req.bmRequestType = UT_WRITE_VENDOR_DEVICE; req.bRequest = AUE_UR_WRITEREG; USETW(req.wValue, val); USETW(req.wIndex, reg); USETW(req.wLength, 2); err = usbd_do_request_flags(sc->aue_udev, &req, &val, USBD_NO_TSLEEP, NULL); - splx(s); + AUE_UNLOCK(sc); if (err) return(-1); return(0); } /* * Read a word of data stored in the EEPROM at address 'addr.' */ Static void aue_eeprom_getword(sc, addr, dest) struct aue_softc *sc; int addr; u_int16_t *dest; { register int i; u_int16_t word = 0; csr_write_1(sc, AUE_EE_REG, addr); csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ); for (i = 0; i < AUE_TIMEOUT; i++) { if (csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE) break; } if (i == AUE_TIMEOUT) { printf("aue%d: EEPROM read timed out\n", sc->aue_unit); } word = csr_read_2(sc, AUE_EE_DATA); *dest = word; return; } /* * Read a sequence of words from the EEPROM. */ Static void aue_read_eeprom(sc, dest, off, cnt, swap) struct aue_softc *sc; caddr_t dest; int off; int cnt; int swap; { int i; u_int16_t word = 0, *ptr; for (i = 0; i < cnt; i++) { aue_eeprom_getword(sc, off + i, &word); ptr = (u_int16_t *)(dest + (i * 2)); if (swap) *ptr = ntohs(word); else *ptr = word; } return; } Static int aue_miibus_readreg(dev, phy, reg) device_t dev; int phy, reg; { struct aue_softc *sc; int i; u_int16_t val = 0; sc = device_get_softc(dev); /* * The Am79C901 HomePNA PHY actually contains * two transceivers: a 1Mbps HomePNA PHY and a * 10Mbps full/half duplex ethernet PHY with * NWAY autoneg. However in the ADMtek adapter, * only the 1Mbps PHY is actually connected to * anything, so we ignore the 10Mbps one. It * happens to be configured for MII address 3, * so we filter that out. */ if (sc->aue_info->aue_vid == USB_VENDOR_ADMTEK && sc->aue_info->aue_did == USB_PRODUCT_ADMTEK_PEGASUS) { if (phy == 3) return(0); #ifdef notdef if (phy != 1) return(0); #endif } csr_write_1(sc, AUE_PHY_ADDR, phy); csr_write_1(sc, AUE_PHY_CTL, reg|AUE_PHYCTL_READ); for (i = 0; i < AUE_TIMEOUT; i++) { if (csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE) break; } if (i == AUE_TIMEOUT) { printf("aue%d: MII read timed out\n", sc->aue_unit); } val = csr_read_2(sc, AUE_PHY_DATA); return(val); } Static int aue_miibus_writereg(dev, phy, reg, data) device_t dev; int phy, reg, data; { struct aue_softc *sc; int i; if (phy == 3) return(0); sc = device_get_softc(dev); csr_write_2(sc, AUE_PHY_DATA, data); csr_write_1(sc, AUE_PHY_ADDR, phy); csr_write_1(sc, AUE_PHY_CTL, reg|AUE_PHYCTL_WRITE); for (i = 0; i < AUE_TIMEOUT; i++) { if (csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE) break; } if (i == AUE_TIMEOUT) { printf("aue%d: MII read timed out\n", sc->aue_unit); } return(0); } Static void aue_miibus_statchg(dev) device_t dev; { struct aue_softc *sc; struct mii_data *mii; sc = device_get_softc(dev); mii = device_get_softc(sc->aue_miibus); AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB|AUE_CTL0_TX_ENB); if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) { AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL); } else { AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL); } if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) { AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX); } else { AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX); } AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB|AUE_CTL0_TX_ENB); /* * Set the LED modes on the LinkSys adapter. * This turns on the 'dual link LED' bin in the auxmode * register of the Broadcom PHY. */ if ((sc->aue_info->aue_vid == USB_VENDOR_LINKSYS && sc->aue_info->aue_did == USB_PRODUCT_LINKSYS_USB100TX) || (sc->aue_info->aue_vid == USB_VENDOR_LINKSYS && sc->aue_info->aue_did == USB_PRODUCT_LINKSYS_USB10TA) || (sc->aue_info->aue_vid == USB_VENDOR_DLINK && sc->aue_info->aue_did == USB_PRODUCT_DLINK_DSB650TX) || (sc->aue_info->aue_vid == USB_VENDOR_DLINK && sc->aue_info->aue_did == USB_PRODUCT_DLINK_DSB650)) { u_int16_t auxmode; auxmode = aue_miibus_readreg(dev, 0, 0x1b); aue_miibus_writereg(dev, 0, 0x1b, auxmode | 0x04); } return; } #define AUE_POLY 0xEDB88320 #define AUE_BITS 6 Static u_int32_t aue_crc(addr) caddr_t addr; { u_int32_t idx, bit, data, crc; /* Compute CRC for the address value. */ crc = 0xFFFFFFFF; /* initial value */ for (idx = 0; idx < 6; idx++) { for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1) crc = (crc >> 1) ^ (((crc ^ data) & 1) ? AUE_POLY : 0); } return (crc & ((1 << AUE_BITS) - 1)); } Static void aue_setmulti(sc) struct aue_softc *sc; { struct ifnet *ifp; struct ifmultiaddr *ifma; u_int32_t h = 0, i; ifp = &sc->arpcom.ac_if; if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI); return; } AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI); /* first, zot all the existing hash bits */ for (i = 0; i < 8; i++) csr_write_1(sc, AUE_MAR0 + i, 0); /* now program new ones */ for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL; ifma = ifma->ifma_link.le_next) { if (ifma->ifma_addr->sa_family != AF_LINK) continue; h = aue_crc(LLADDR((struct sockaddr_dl *)ifma->ifma_addr)); AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0xF)); } return; } Static void aue_reset(sc) struct aue_softc *sc; { register int i; AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC); for (i = 0; i < AUE_TIMEOUT; i++) { if (!(csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC)) break; } if (i == AUE_TIMEOUT) printf("aue%d: reset failed\n", sc->aue_unit); /* * The PHY(s) attached to the Pegasus chip may be held * in reset until we flip on the GPIO outputs. Make sure * to set the GPIO pins high so that the PHY(s) will * be enabled. * * Note: We force all of the GPIO pins low first, *then* * enable the ones we want. */ csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0); csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0|AUE_GPIO_SEL1); /* Grrr. LinkSys has to be different from everyone else. */ if ((sc->aue_info->aue_vid == USB_VENDOR_LINKSYS && sc->aue_info->aue_did == USB_PRODUCT_LINKSYS_USB100TX) || (sc->aue_info->aue_vid == USB_VENDOR_LINKSYS && sc->aue_info->aue_did == USB_PRODUCT_LINKSYS_USB10TA) || (sc->aue_info->aue_vid == USB_VENDOR_DLINK && sc->aue_info->aue_did == USB_PRODUCT_DLINK_DSB650TX) || (sc->aue_info->aue_vid == USB_VENDOR_DLINK && sc->aue_info->aue_did == USB_PRODUCT_DLINK_DSB650)) { csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0|AUE_GPIO_SEL1); csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0|AUE_GPIO_SEL1| AUE_GPIO_OUT0); } /* Wait a little while for the chip to get its brains in order. */ DELAY(10000); return; } /* * Probe for a Pegasus chip. */ USB_MATCH(aue) { USB_MATCH_START(aue, uaa); struct aue_type *t; if (!uaa->iface) return(UMATCH_NONE); t = aue_devs; while(t->aue_vid) { if (uaa->vendor == t->aue_vid && uaa->product == t->aue_did) { return(UMATCH_VENDOR_PRODUCT); } t++; } return(UMATCH_NONE); } /* * Attach the interface. Allocate softc structures, do ifmedia * setup and ethernet/BPF attach. */ USB_ATTACH(aue) { USB_ATTACH_START(aue, sc, uaa); char devinfo[1024]; - int s; u_char eaddr[ETHER_ADDR_LEN]; struct ifnet *ifp; usb_interface_descriptor_t *id; usb_endpoint_descriptor_t *ed; int i; struct aue_type *t; - s = splimp(); - bzero(sc, sizeof(struct aue_softc)); sc->aue_iface = uaa->iface; sc->aue_udev = uaa->device; sc->aue_unit = device_get_unit(self); if (usbd_set_config_no(sc->aue_udev, AUE_CONFIG_NO, 0)) { printf("aue%d: getting interface handle failed\n", sc->aue_unit); - splx(s); USB_ATTACH_ERROR_RETURN; } t = aue_devs; while(t->aue_vid) { if (uaa->vendor == t->aue_vid && uaa->product == t->aue_did) { sc->aue_info = t; break; } t++; } id = usbd_get_interface_descriptor(uaa->iface); usbd_devinfo(uaa->device, 0, devinfo); device_set_desc_copy(self, devinfo); printf("%s: %s\n", USBDEVNAME(self), devinfo); /* Find endpoints. */ for (i = 0; i < id->bNumEndpoints; i++) { ed = usbd_interface2endpoint_descriptor(uaa->iface, i); if (!ed) { printf("aue%d: couldn't get ep %d\n", sc->aue_unit, i); - splx(s); USB_ATTACH_ERROR_RETURN; } if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) { sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress; } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) { sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress; } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) { sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress; } } + mtx_init(&sc->aue_mtx, device_get_nameunit(self), MTX_DEF); + AUE_LOCK(sc); + /* Reset the adapter. */ aue_reset(sc); /* * Get station address from the EEPROM. */ aue_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 0); /* * A Pegasus chip was detected. Inform the world. */ printf("aue%d: Ethernet address: %6D\n", sc->aue_unit, eaddr, ":"); bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN); ifp = &sc->arpcom.ac_if; ifp->if_softc = sc; ifp->if_unit = sc->aue_unit; ifp->if_name = "aue"; ifp->if_mtu = ETHERMTU; ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; ifp->if_ioctl = aue_ioctl; ifp->if_output = ether_output; ifp->if_start = aue_start; ifp->if_watchdog = aue_watchdog; ifp->if_init = aue_init; ifp->if_baudrate = 10000000; ifp->if_snd.ifq_maxlen = IFQ_MAXLEN; /* * Do MII setup. * NOTE: Doing this causes child devices to be attached to us, * which we would normally disconnect at in the detach routine * using device_delete_child(). However the USB code is set up * such that when this driver is removed, all children devices * are removed as well. In effect, the USB code ends up detaching * all of our children for us, so we don't have to do is ourselves * in aue_detach(). It's important to point this out since if * we *do* try to detach the child devices ourselves, we will * end up getting the children deleted twice, which will crash * the system. */ if (mii_phy_probe(self, &sc->aue_miibus, aue_ifmedia_upd, aue_ifmedia_sts)) { printf("aue%d: MII without any PHY!\n", sc->aue_unit); - splx(s); + AUE_UNLOCK(sc); + mtx_destroy(&sc->aue_mtx); USB_ATTACH_ERROR_RETURN; } aue_qdat.ifp = ifp; aue_qdat.if_rxstart = aue_rxstart; /* * Call MI attach routine. */ ether_ifattach(ifp, ETHER_BPF_SUPPORTED); callout_handle_init(&sc->aue_stat_ch); usb_register_netisr(); sc->aue_gone = 0; - splx(s); + AUE_UNLOCK(sc); USB_ATTACH_SUCCESS_RETURN; } Static int aue_detach(dev) device_t dev; { struct aue_softc *sc; struct ifnet *ifp; - int s; - s = splusb(); - sc = device_get_softc(dev); + AUE_LOCK(sc); ifp = &sc->arpcom.ac_if; sc->aue_gone = 1; untimeout(aue_tick, sc, sc->aue_stat_ch); ether_ifdetach(ifp, ETHER_BPF_SUPPORTED); if (sc->aue_ep[AUE_ENDPT_TX] != NULL) usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]); if (sc->aue_ep[AUE_ENDPT_RX] != NULL) usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]); #ifdef AUE_INTR_PIPE if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]); #endif - splx(s); + AUE_UNLOCK(sc); + mtx_destroy(&sc->aue_mtx); + return(0); } /* * Initialize an RX descriptor and attach an MBUF cluster. */ Static int aue_newbuf(sc, c, m) struct aue_softc *sc; struct aue_chain *c; struct mbuf *m; { struct mbuf *m_new = NULL; if (m == NULL) { MGETHDR(m_new, M_DONTWAIT, MT_DATA); if (m_new == NULL) { printf("aue%d: no memory for rx list " "-- packet dropped!\n", sc->aue_unit); return(ENOBUFS); } MCLGET(m_new, M_DONTWAIT); if (!(m_new->m_flags & M_EXT)) { printf("aue%d: no memory for rx list " "-- packet dropped!\n", sc->aue_unit); m_freem(m_new); return(ENOBUFS); } m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; } else { m_new = m; m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; m_new->m_data = m_new->m_ext.ext_buf; } m_adj(m_new, ETHER_ALIGN); c->aue_mbuf = m_new; return(0); } Static int aue_rx_list_init(sc) struct aue_softc *sc; { struct aue_cdata *cd; struct aue_chain *c; int i; cd = &sc->aue_cdata; for (i = 0; i < AUE_RX_LIST_CNT; i++) { c = &cd->aue_rx_chain[i]; c->aue_sc = sc; c->aue_idx = i; if (aue_newbuf(sc, c, NULL) == ENOBUFS) return(ENOBUFS); if (c->aue_xfer == NULL) { c->aue_xfer = usbd_alloc_xfer(sc->aue_udev); if (c->aue_xfer == NULL) return(ENOBUFS); } } return(0); } Static int aue_tx_list_init(sc) struct aue_softc *sc; { struct aue_cdata *cd; struct aue_chain *c; int i; cd = &sc->aue_cdata; for (i = 0; i < AUE_TX_LIST_CNT; i++) { c = &cd->aue_tx_chain[i]; c->aue_sc = sc; c->aue_idx = i; c->aue_mbuf = NULL; if (c->aue_xfer == NULL) { c->aue_xfer = usbd_alloc_xfer(sc->aue_udev); if (c->aue_xfer == NULL) return(ENOBUFS); } c->aue_buf = malloc(AUE_BUFSZ, M_USBDEV, M_NOWAIT); if (c->aue_buf == NULL) return(ENOBUFS); } return(0); } #ifdef AUE_INTR_PIPE Static void aue_intr(xfer, priv, status) usbd_xfer_handle xfer; usbd_private_handle priv; usbd_status status; { struct aue_softc *sc; struct ifnet *ifp; struct aue_intrpkt *p; - int s; - s = splimp(); - sc = priv; + AUE_LOCK(sc); ifp = &sc->arpcom.ac_if; if (!(ifp->if_flags & IFF_RUNNING)) { - splx(s); + AUE_UNLOCK(sc); return; } if (status != USBD_NORMAL_COMPLETION) { if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { - splx(s); + AUE_UNLOCK(sc); return; } printf("aue%d: usb error on intr: %s\n", sc->aue_unit, usbd_errstr(status)); if (status == USBD_STALLED) usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]); - splx(s); + AUE_UNLOCK(sc); return; } usbd_get_xfer_status(xfer, NULL, (void **)&p, NULL, NULL); if (p->aue_txstat0) ifp->if_oerrors++; if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL & AUE_TXSTAT0_EXCESSCOLL)) ifp->if_collisions++; - splx(s); + AUE_UNLOCK(sc); return; } #endif Static void aue_rxstart(ifp) struct ifnet *ifp; { struct aue_softc *sc; struct aue_chain *c; sc = ifp->if_softc; + AUE_LOCK(sc); c = &sc->aue_cdata.aue_rx_chain[sc->aue_cdata.aue_rx_prod]; if (aue_newbuf(sc, c, NULL) == ENOBUFS) { ifp->if_ierrors++; + AUE_UNLOCK(sc); return; } /* Setup new transfer. */ usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX], c, mtod(c->aue_mbuf, char *), AUE_BUFSZ, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, aue_rxeof); usbd_transfer(c->aue_xfer); + AUE_UNLOCK(sc); return; } /* * A frame has been uploaded: pass the resulting mbuf chain up to * the higher level protocols. */ Static void aue_rxeof(xfer, priv, status) usbd_xfer_handle xfer; usbd_private_handle priv; usbd_status status; { struct aue_softc *sc; struct aue_chain *c; struct mbuf *m; struct ifnet *ifp; int total_len = 0; struct aue_rxpkt r; c = priv; sc = c->aue_sc; + if (sc->aue_gone) + return; + AUE_LOCK(sc); ifp = &sc->arpcom.ac_if; - if (!(ifp->if_flags & IFF_RUNNING)) + if (!(ifp->if_flags & IFF_RUNNING)) { + AUE_UNLOCK(sc); return; + } if (status != USBD_NORMAL_COMPLETION) { - if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) + if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { + AUE_UNLOCK(sc); return; + } printf("aue%d: usb error on rx: %s\n", sc->aue_unit, usbd_errstr(status)); if (status == USBD_STALLED) usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]); goto done; } usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); if (total_len <= 4 + ETHER_CRC_LEN) { ifp->if_ierrors++; goto done; } m = c->aue_mbuf; bcopy(mtod(m, char *) + total_len - 4, (char *)&r, sizeof(r)); /* Turn off all the non-error bits in the rx status word. */ r.aue_rxstat &= AUE_RXSTAT_MASK; if (r.aue_rxstat) { ifp->if_ierrors++; goto done; } /* No errors; receive the packet. */ total_len -= (4 + ETHER_CRC_LEN); ifp->if_ipackets++; m->m_pkthdr.rcvif = (struct ifnet *)&aue_qdat; m->m_pkthdr.len = m->m_len = total_len; /* Put the packet on the special USB input queue. */ usb_ether_input(m); - + AUE_UNLOCK(sc); return; done: /* Setup new transfer. */ usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX], c, mtod(c->aue_mbuf, char *), AUE_BUFSZ, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, aue_rxeof); usbd_transfer(xfer); + AUE_UNLOCK(sc); return; } /* * A frame was downloaded to the chip. It's safe for us to clean up * the list buffers. */ Static void aue_txeof(xfer, priv, status) usbd_xfer_handle xfer; usbd_private_handle priv; usbd_status status; { struct aue_softc *sc; struct aue_chain *c; struct ifnet *ifp; usbd_status err; - int s; - s = splimp(); - c = priv; sc = c->aue_sc; + AUE_LOCK(sc); ifp = &sc->arpcom.ac_if; if (status != USBD_NORMAL_COMPLETION) { if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { - splx(s); + AUE_UNLOCK(sc); return; } printf("aue%d: usb error on tx: %s\n", sc->aue_unit, usbd_errstr(status)); if (status == USBD_STALLED) usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_TX]); - splx(s); + AUE_UNLOCK(sc); return; } ifp->if_timer = 0; ifp->if_flags &= ~IFF_OACTIVE; usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &err); if (c->aue_mbuf != NULL) { c->aue_mbuf->m_pkthdr.rcvif = ifp; usb_tx_done(c->aue_mbuf); c->aue_mbuf = NULL; } if (err) ifp->if_oerrors++; else ifp->if_opackets++; - splx(s); + AUE_UNLOCK(sc); return; } Static void aue_tick(xsc) void *xsc; { struct aue_softc *sc; struct ifnet *ifp; struct mii_data *mii; - int s; - s = splimp(); - sc = xsc; - if (sc == NULL) { - splx(s); + if (sc == NULL) return; - } + AUE_LOCK(sc); + ifp = &sc->arpcom.ac_if; mii = device_get_softc(sc->aue_miibus); if (mii == NULL) { - splx(s); + AUE_UNLOCK(sc); return; } mii_tick(mii); if (!sc->aue_link) { mii_pollstat(mii); if (mii->mii_media_status & IFM_ACTIVE && IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) sc->aue_link++; if (ifp->if_snd.ifq_head != NULL) aue_start(ifp); } sc->aue_stat_ch = timeout(aue_tick, sc, hz); - splx(s); + AUE_UNLOCK(sc); return; } Static int aue_encap(sc, m, idx) struct aue_softc *sc; struct mbuf *m; int idx; { int total_len; struct aue_chain *c; usbd_status err; c = &sc->aue_cdata.aue_tx_chain[idx]; /* * Copy the mbuf data into a contiguous buffer, leaving two * bytes at the beginning to hold the frame length. */ m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2); c->aue_mbuf = m; total_len = m->m_pkthdr.len + 2; /* * The ADMtek documentation says that the packet length is * supposed to be specified in the first two bytes of the * transfer, however it actually seems to ignore this info * and base the frame size on the bulk transfer length. */ c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len; c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8); usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX], c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER, 10000, aue_txeof); /* Transmit */ err = usbd_transfer(c->aue_xfer); if (err != USBD_IN_PROGRESS) { aue_stop(sc); return(EIO); } sc->aue_cdata.aue_tx_cnt++; return(0); } Static void aue_start(ifp) struct ifnet *ifp; { struct aue_softc *sc; struct mbuf *m_head = NULL; sc = ifp->if_softc; + AUE_LOCK(sc); - if (!sc->aue_link) + if (!sc->aue_link) { + AUE_UNLOCK(sc); return; + } - if (ifp->if_flags & IFF_OACTIVE) + if (ifp->if_flags & IFF_OACTIVE) { + AUE_UNLOCK(sc); return; + } IF_DEQUEUE(&ifp->if_snd, m_head); - if (m_head == NULL) + if (m_head == NULL) { + AUE_UNLOCK(sc); return; + } if (aue_encap(sc, m_head, 0)) { IF_PREPEND(&ifp->if_snd, m_head); ifp->if_flags |= IFF_OACTIVE; + AUE_UNLOCK(sc); return; } /* * If there's a BPF listener, bounce a copy of this frame * to him. */ if (ifp->if_bpf) bpf_mtap(ifp, m_head); ifp->if_flags |= IFF_OACTIVE; /* * Set a timeout in case the chip goes out to lunch. */ ifp->if_timer = 5; + AUE_UNLOCK(sc); return; } Static void aue_init(xsc) void *xsc; { struct aue_softc *sc = xsc; struct ifnet *ifp = &sc->arpcom.ac_if; struct mii_data *mii; struct aue_chain *c; usbd_status err; - int i, s; + int i; - if (ifp->if_flags & IFF_RUNNING) + AUE_LOCK(sc); + + if (ifp->if_flags & IFF_RUNNING) { + AUE_UNLOCK(sc); return; + } - s = splimp(); - /* * Cancel pending I/O and free all RX/TX buffers. */ aue_reset(sc); mii = device_get_softc(sc->aue_miibus); /* Set MAC address */ for (i = 0; i < ETHER_ADDR_LEN; i++) csr_write_1(sc, AUE_PAR0 + i, sc->arpcom.ac_enaddr[i]); /* If we want promiscuous mode, set the allframes bit. */ if (ifp->if_flags & IFF_PROMISC) { AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC); } else { AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC); } /* Init TX ring. */ if (aue_tx_list_init(sc) == ENOBUFS) { printf("aue%d: tx list init failed\n", sc->aue_unit); - splx(s); + AUE_UNLOCK(sc); return; } /* Init RX ring. */ if (aue_rx_list_init(sc) == ENOBUFS) { printf("aue%d: rx list init failed\n", sc->aue_unit); - splx(s); + AUE_UNLOCK(sc); return; } #ifdef AUE_INTR_PIPE sc->aue_cdata.aue_ibuf = malloc(AUE_INTR_PKTLEN, M_USBDEV, M_NOWAIT); #endif /* Load the multicast filter. */ aue_setmulti(sc); /* Enable RX and TX */ csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND|AUE_CTL0_RX_ENB); AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB); AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR); mii_mediachg(mii); /* Open RX and TX pipes. */ err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX], USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]); if (err) { printf("aue%d: open rx pipe failed: %s\n", sc->aue_unit, usbd_errstr(err)); - splx(s); + AUE_UNLOCK(sc); return; } err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX], USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]); if (err) { printf("aue%d: open tx pipe failed: %s\n", sc->aue_unit, usbd_errstr(err)); - splx(s); + AUE_UNLOCK(sc); return; } #ifdef AUE_INTR_PIPE err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR], USBD_SHORT_XFER_OK, &sc->aue_ep[AUE_ENDPT_INTR], sc, sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr, AUE_INTR_INTERVAL); if (err) { printf("aue%d: open intr pipe failed: %s\n", sc->aue_unit, usbd_errstr(err)); - splx(s); + AUE_UNLOCK(sc); return; } #endif /* Start up the receive pipe. */ for (i = 0; i < AUE_RX_LIST_CNT; i++) { c = &sc->aue_cdata.aue_rx_chain[i]; usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX], c, mtod(c->aue_mbuf, char *), AUE_BUFSZ, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, aue_rxeof); usbd_transfer(c->aue_xfer); } ifp->if_flags |= IFF_RUNNING; ifp->if_flags &= ~IFF_OACTIVE; - (void)splx(s); - sc->aue_stat_ch = timeout(aue_tick, sc, hz); + AUE_UNLOCK(sc); + return; } /* * Set media options. */ Static int aue_ifmedia_upd(ifp) struct ifnet *ifp; { struct aue_softc *sc; struct mii_data *mii; sc = ifp->if_softc; mii = device_get_softc(sc->aue_miibus); sc->aue_link = 0; if (mii->mii_instance) { struct mii_softc *miisc; for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL; miisc = LIST_NEXT(miisc, mii_list)) mii_phy_reset(miisc); } mii_mediachg(mii); return(0); } /* * Report current media status. */ Static void aue_ifmedia_sts(ifp, ifmr) struct ifnet *ifp; struct ifmediareq *ifmr; { struct aue_softc *sc; struct mii_data *mii; sc = ifp->if_softc; mii = device_get_softc(sc->aue_miibus); mii_pollstat(mii); ifmr->ifm_active = mii->mii_media_active; ifmr->ifm_status = mii->mii_media_status; return; } Static int aue_ioctl(ifp, command, data) struct ifnet *ifp; u_long command; caddr_t data; { struct aue_softc *sc = ifp->if_softc; struct ifreq *ifr = (struct ifreq *) data; struct mii_data *mii; - int s, error = 0; + int error = 0; - s = splimp(); + AUE_LOCK(sc); switch(command) { case SIOCSIFADDR: case SIOCGIFADDR: case SIOCSIFMTU: error = ether_ioctl(ifp, command, data); break; case SIOCSIFFLAGS: if (ifp->if_flags & IFF_UP) { if (ifp->if_flags & IFF_RUNNING && ifp->if_flags & IFF_PROMISC && !(sc->aue_if_flags & IFF_PROMISC)) { AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC); } else if (ifp->if_flags & IFF_RUNNING && !(ifp->if_flags & IFF_PROMISC) && sc->aue_if_flags & IFF_PROMISC) { AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC); } else if (!(ifp->if_flags & IFF_RUNNING)) aue_init(sc); } else { if (ifp->if_flags & IFF_RUNNING) aue_stop(sc); } sc->aue_if_flags = ifp->if_flags; error = 0; break; case SIOCADDMULTI: case SIOCDELMULTI: aue_setmulti(sc); error = 0; break; case SIOCGIFMEDIA: case SIOCSIFMEDIA: mii = device_get_softc(sc->aue_miibus); error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); break; default: error = EINVAL; break; } - (void)splx(s); + AUE_UNLOCK(sc); return(error); } Static void aue_watchdog(ifp) struct ifnet *ifp; { struct aue_softc *sc; struct aue_chain *c; usbd_status stat; sc = ifp->if_softc; + AUE_LOCK(sc); ifp->if_oerrors++; printf("aue%d: watchdog timeout\n", sc->aue_unit); c = &sc->aue_cdata.aue_tx_chain[0]; usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &stat); aue_txeof(c->aue_xfer, c, stat); if (ifp->if_snd.ifq_head != NULL) aue_start(ifp); - + AUE_UNLOCK(sc); return; } /* * Stop the adapter and free any mbufs allocated to the * RX and TX lists. */ Static void aue_stop(sc) struct aue_softc *sc; { usbd_status err; struct ifnet *ifp; int i; + AUE_LOCK(sc); ifp = &sc->arpcom.ac_if; ifp->if_timer = 0; csr_write_1(sc, AUE_CTL0, 0); csr_write_1(sc, AUE_CTL1, 0); aue_reset(sc); untimeout(aue_tick, sc, sc->aue_stat_ch); /* Stop transfers. */ if (sc->aue_ep[AUE_ENDPT_RX] != NULL) { err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]); if (err) { printf("aue%d: abort rx pipe failed: %s\n", sc->aue_unit, usbd_errstr(err)); } err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]); if (err) { printf("aue%d: close rx pipe failed: %s\n", sc->aue_unit, usbd_errstr(err)); } sc->aue_ep[AUE_ENDPT_RX] = NULL; } if (sc->aue_ep[AUE_ENDPT_TX] != NULL) { err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]); if (err) { printf("aue%d: abort tx pipe failed: %s\n", sc->aue_unit, usbd_errstr(err)); } err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]); if (err) { printf("aue%d: close tx pipe failed: %s\n", sc->aue_unit, usbd_errstr(err)); } sc->aue_ep[AUE_ENDPT_TX] = NULL; } #ifdef AUE_INTR_PIPE if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) { err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]); if (err) { printf("aue%d: abort intr pipe failed: %s\n", sc->aue_unit, usbd_errstr(err)); } err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]); if (err) { printf("aue%d: close intr pipe failed: %s\n", sc->aue_unit, usbd_errstr(err)); } sc->aue_ep[AUE_ENDPT_INTR] = NULL; } #endif /* Free RX resources. */ for (i = 0; i < AUE_RX_LIST_CNT; i++) { if (sc->aue_cdata.aue_rx_chain[i].aue_buf != NULL) { free(sc->aue_cdata.aue_rx_chain[i].aue_buf, M_USBDEV); sc->aue_cdata.aue_rx_chain[i].aue_buf = NULL; } if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) { m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf); sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL; } if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) { usbd_free_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer); sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL; } } /* Free TX resources. */ for (i = 0; i < AUE_TX_LIST_CNT; i++) { if (sc->aue_cdata.aue_tx_chain[i].aue_buf != NULL) { free(sc->aue_cdata.aue_tx_chain[i].aue_buf, M_USBDEV); sc->aue_cdata.aue_tx_chain[i].aue_buf = NULL; } if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) { m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf); sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL; } if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) { usbd_free_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer); sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL; } } #ifdef AUE_INTR_PIPE free(sc->aue_cdata.aue_ibuf, M_USBDEV); sc->aue_cdata.aue_ibuf = NULL; #endif sc->aue_link = 0; ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); + AUE_UNLOCK(sc); return; } /* * Stop all chip I/O so that the kernel's probe routines don't * get confused by errant DMAs when rebooting. */ Static void aue_shutdown(dev) device_t dev; { struct aue_softc *sc; sc = device_get_softc(dev); - + sc->aue_gone++; + AUE_LOCK(sc); aue_reset(sc); aue_stop(sc); + AUE_UNLOCK(sc); return; } Index: head/sys/dev/usb/if_auereg.h =================================================================== --- head/sys/dev/usb/if_auereg.h (revision 67529) +++ head/sys/dev/usb/if_auereg.h (revision 67530) @@ -1,255 +1,259 @@ /* * Copyright (c) 1997, 1998, 1999 * 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. * * $FreeBSD$ */ /* * Register definitions for ADMtek Pegasus AN986 USB to Ethernet * chip. The Pegasus uses a total of four USB endpoints: the control * endpoint (0), a bulk read endpoint for receiving packets (1), * a bulk write endpoint for sending packets (2) and an interrupt * endpoint for passing RX and TX status (3). Endpoint 0 is used * to read and write the ethernet module's registers. All registers * are 8 bits wide. * * Packet transfer is done in 64 byte chunks. The last chunk in a * transfer is denoted by having a length less that 64 bytes. For * the RX case, the data includes an optional RX status word. */ #define AUE_VENDORID_ADMTEK 0x07A6 #define AUE_DEVICEID_PEGASUS 0x0986 #define AUE_VENDORID_BILLIONTON 0x08DD #define AUE_DEVICEID_USB100 0x0986 #define AUE_VENDORID_MELCO 0x0411 #define AUE_DEVICEID_LUATX 0x0001 #define AUE_UR_READREG 0xF0 #define AUE_UR_WRITEREG 0xF1 #define AUE_CONFIG_NO 1 /* * Note that while the ADMtek technically has four * endpoints, the control endpoint (endpoint 0) is * regarded as special by the USB code and drivers * don't have direct access to it. (We access it * using usbd_do_request() when reading/writing * registers.) Consequently, our endpoint indexes * don't match those in the ADMtek Pegasus manual: * we consider the RX data endpoint to be index 0 * and work up from there. */ #define AUE_ENDPT_RX 0x0 #define AUE_ENDPT_TX 0x1 #define AUE_ENDPT_INTR 0x2 #define AUE_ENDPT_MAX 0x3 #define AUE_INTR_PKTLEN 0x8 #define AUE_CTL0 0x00 #define AUE_CTL1 0x01 #define AUE_CTL2 0x02 #define AUE_MAR0 0x08 #define AUE_MAR1 0x09 #define AUE_MAR2 0x0A #define AUE_MAR3 0x0B #define AUE_MAR4 0x0C #define AUE_MAR5 0x0D #define AUE_MAR6 0x0E #define AUE_MAR7 0x0F #define AUE_MAR AUE_MAR0 #define AUE_PAR0 0x10 #define AUE_PAR1 0x11 #define AUE_PAR2 0x12 #define AUE_PAR3 0x13 #define AUE_PAR4 0x14 #define AUE_PAR5 0x15 #define AUE_PAR AUE_PAR0 #define AUE_PAUSE0 0x18 #define AUE_PAUSE1 0x19 #define AUE_PAUSE AUE_PAUSE0 #define AUE_RX_FLOWCTL_CNT 0x1A #define AUE_RX_FLOWCTL_FIFO 0x1B #define AUE_EE_REG 0x20 #define AUE_EE_DATA0 0x21 #define AUE_EE_DATA1 0x22 #define AUE_EE_DATA AUE_EE_DATA0 #define AUE_EE_CTL 0x23 #define AUE_PHY_ADDR 0x25 #define AUE_PHY_DATA0 0x26 #define AUE_PHY_DATA1 0x27 #define AUE_PHY_DATA AUE_PHY_DATA0 #define AUE_PHY_CTL 0x28 #define AUE_USB_STS 0x2A #define AUE_TXSTAT0 0x2B #define AUE_TXSTAT1 0x2C #define AUE_TXSTAT AUE_TXSTAT0 #define AUE_RXSTAT 0x2D #define AUE_PKTLOST0 0x2E #define AUE_PKTLOST1 0x2F #define AUE_PKTLOST AUE_PKTLOST0 #define AUE_GPIO0 0x7E #define AUE_GPIO1 0x7F #define AUE_CTL0_INCLUDE_RXCRC 0x01 #define AUE_CTL0_ALLMULTI 0x02 #define AUE_CTL0_STOP_BACKOFF 0x04 #define AUE_CTL0_RXSTAT_APPEND 0x08 #define AUE_CTL0_WAKEON_ENB 0x10 #define AUE_CTL0_RXPAUSE_ENB 0x20 #define AUE_CTL0_RX_ENB 0x40 #define AUE_CTL0_TX_ENB 0x80 #define AUE_CTL1_HOMELAN 0x04 #define AUE_CTL1_RESETMAC 0x08 #define AUE_CTL1_SPEEDSEL 0x10 /* 0 = 10mbps, 1 = 100mbps */ #define AUE_CTL1_DUPLEX 0x20 /* 0 = half, 1 = full */ #define AUE_CTL1_DELAYHOME 0x40 #define AUE_CTL2_EP3_CLR 0x01 /* reading EP3 clrs status regs */ #define AUE_CTL2_RX_BADFRAMES 0x02 #define AUE_CTL2_RX_PROMISC 0x04 #define AUE_CTL2_LOOPBACK 0x08 #define AUE_CTL2_EEPROMWR_ENB 0x10 #define AUE_CTL2_EEPROM_LOAD 0x20 #define AUE_EECTL_WRITE 0x01 #define AUE_EECTL_READ 0x02 #define AUE_EECTL_DONE 0x04 #define AUE_PHYCTL_PHYREG 0x1F #define AUE_PHYCTL_WRITE 0x20 #define AUE_PHYCTL_READ 0x40 #define AUE_PHYCTL_DONE 0x80 #define AUE_USBSTS_SUSPEND 0x01 #define AUE_USBSTS_RESUME 0x02 #define AUE_TXSTAT0_JABTIMO 0x04 #define AUE_TXSTAT0_CARLOSS 0x08 #define AUE_TXSTAT0_NOCARRIER 0x10 #define AUE_TXSTAT0_LATECOLL 0x20 #define AUE_TXSTAT0_EXCESSCOLL 0x40 #define AUE_TXSTAT0_UNDERRUN 0x80 #define AUE_TXSTAT1_PKTCNT 0x0F #define AUE_TXSTAT1_FIFO_EMPTY 0x40 #define AUE_TXSTAT1_FIFO_FULL 0x80 #define AUE_RXSTAT_OVERRUN 0x01 #define AUE_RXSTAT_PAUSE 0x02 #define AUE_GPIO_IN0 0x01 #define AUE_GPIO_OUT0 0x02 #define AUE_GPIO_SEL0 0x04 #define AUE_GPIO_IN1 0x08 #define AUE_GPIO_OUT1 0x10 #define AUE_GPIO_SEL1 0x20 struct aue_intrpkt { u_int8_t aue_txstat0; u_int8_t aue_txstat1; u_int8_t aue_rxstat; u_int8_t aue_rxlostpkt0; u_int8_t aue_rxlostpkt1; u_int8_t aue_wakeupstat; u_int8_t aue_rsvd; }; struct aue_rxpkt { u_int16_t aue_pktlen; u_int8_t aue_rxstat; }; #define AUE_RXSTAT_MCAST 0x01 #define AUE_RXSTAT_GIANT 0x02 #define AUE_RXSTAT_RUNT 0x04 #define AUE_RXSTAT_CRCERR 0x08 #define AUE_RXSTAT_DRIBBLE 0x10 #define AUE_RXSTAT_MASK 0x1E struct aue_type { u_int16_t aue_vid; u_int16_t aue_did; }; #define AUE_TX_LIST_CNT 1 #define AUE_RX_LIST_CNT 1 struct aue_softc; struct aue_chain { struct aue_softc *aue_sc; usbd_xfer_handle aue_xfer; char *aue_buf; struct mbuf *aue_mbuf; int aue_idx; }; struct aue_cdata { struct aue_chain aue_tx_chain[AUE_TX_LIST_CNT]; struct aue_chain aue_rx_chain[AUE_RX_LIST_CNT]; struct aue_intrpkt *aue_ibuf; int aue_tx_prod; int aue_tx_cons; int aue_tx_cnt; int aue_rx_prod; }; #define AUE_INC(x, y) (x) = (x + 1) % y struct aue_softc { struct arpcom arpcom; device_t aue_miibus; usbd_device_handle aue_udev; usbd_interface_handle aue_iface; struct aue_type *aue_info; int aue_ed[AUE_ENDPT_MAX]; usbd_pipe_handle aue_ep[AUE_ENDPT_MAX]; int aue_unit; u_int8_t aue_link; u_int8_t aue_gone; int aue_if_flags; struct aue_cdata aue_cdata; struct callout_handle aue_stat_ch; + struct mtx aue_mtx; }; + +#define AUE_LOCK(_sc) mtx_enter(&(_sc)->aue_mtx, MTX_DEF) +#define AUE_UNLOCK(_sc) mtx_exit(&(_sc)->aue_mtx, MTX_DEF) #define AUE_TIMEOUT 1000 #define ETHER_ALIGN 2 #define AUE_BUFSZ 1536 #define AUE_MIN_FRAMELEN 60 #define AUE_INTR_INTERVAL 100 /* ms */ Index: head/sys/dev/usb/if_cue.c =================================================================== --- head/sys/dev/usb/if_cue.c (revision 67529) +++ head/sys/dev/usb/if_cue.c (revision 67530) @@ -1,1226 +1,1231 @@ /* * Copyright (c) 1997, 1998, 1999, 2000 * 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. * * $FreeBSD$ */ /* * CATC USB-EL1210A USB to ethernet driver. Used in the CATC Netmate * adapters and others. * * Written by Bill Paul * Electrical Engineering Department * Columbia University, New York City */ /* * The CATC USB-EL1210A provides USB ethernet support at 10Mbps. The * RX filter uses a 512-bit multicast hash table, single perfect entry * for the station address, and promiscuous mode. Unlike the ADMtek * and KLSI chips, the CATC ASIC supports read and write combining * mode where multiple packets can be transfered using a single bulk * transaction, which helps performance a great deal. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef lint static const char rcsid[] = "$FreeBSD$"; #endif /* * Various supported device vendors/products. */ Static struct cue_type cue_devs[] = { { USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE }, { USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE2 }, { USB_VENDOR_SMARTBRIDGES, USB_PRODUCT_SMARTBRIDGES_SMARTLINK }, { 0, 0 } }; Static struct usb_qdat cue_qdat; Static int cue_match __P((device_t)); Static int cue_attach __P((device_t)); Static int cue_detach __P((device_t)); Static int cue_tx_list_init __P((struct cue_softc *)); Static int cue_rx_list_init __P((struct cue_softc *)); Static int cue_newbuf __P((struct cue_softc *, struct cue_chain *, struct mbuf *)); Static int cue_encap __P((struct cue_softc *, struct mbuf *, int)); Static void cue_rxeof __P((usbd_xfer_handle, usbd_private_handle, usbd_status)); Static void cue_txeof __P((usbd_xfer_handle, usbd_private_handle, usbd_status)); Static void cue_tick __P((void *)); Static void cue_rxstart __P((struct ifnet *)); Static void cue_start __P((struct ifnet *)); Static int cue_ioctl __P((struct ifnet *, u_long, caddr_t)); Static void cue_init __P((void *)); Static void cue_stop __P((struct cue_softc *)); Static void cue_watchdog __P((struct ifnet *)); Static void cue_shutdown __P((device_t)); Static void cue_setmulti __P((struct cue_softc *)); Static u_int32_t cue_crc __P((caddr_t)); Static void cue_reset __P((struct cue_softc *)); Static int csr_read_1 __P((struct cue_softc *, int)); Static int csr_write_1 __P((struct cue_softc *, int, int)); Static int csr_read_2 __P((struct cue_softc *, int)); #ifdef notdef Static int csr_write_2 __P((struct cue_softc *, int, int)); #endif Static int cue_mem __P((struct cue_softc *, int, int, void *, int)); Static int cue_getmac __P((struct cue_softc *, void *)); Static device_method_t cue_methods[] = { /* Device interface */ DEVMETHOD(device_probe, cue_match), DEVMETHOD(device_attach, cue_attach), DEVMETHOD(device_detach, cue_detach), DEVMETHOD(device_shutdown, cue_shutdown), { 0, 0 } }; Static driver_t cue_driver = { "cue", cue_methods, sizeof(struct cue_softc) }; Static devclass_t cue_devclass; DRIVER_MODULE(if_cue, uhub, cue_driver, cue_devclass, usbd_driver_load, 0); #define CUE_SETBIT(sc, reg, x) \ csr_write_1(sc, reg, csr_read_1(sc, reg) | (x)) #define CUE_CLRBIT(sc, reg, x) \ csr_write_1(sc, reg, csr_read_1(sc, reg) & ~(x)) Static int csr_read_1(sc, reg) struct cue_softc *sc; int reg; { usb_device_request_t req; usbd_status err; u_int8_t val = 0; - int s; if (sc->cue_gone) return(0); - s = splusb(); + CUE_LOCK(sc); req.bmRequestType = UT_READ_VENDOR_DEVICE; req.bRequest = CUE_CMD_READREG; USETW(req.wValue, 0); USETW(req.wIndex, reg); USETW(req.wLength, 1); err = usbd_do_request_flags(sc->cue_udev, &req, &val, USBD_NO_TSLEEP, NULL); - splx(s); + CUE_UNLOCK(sc); if (err) return(0); return(val); } Static int csr_read_2(sc, reg) struct cue_softc *sc; int reg; { usb_device_request_t req; usbd_status err; u_int16_t val = 0; - int s; if (sc->cue_gone) return(0); - s = splusb(); + CUE_LOCK(sc); req.bmRequestType = UT_READ_VENDOR_DEVICE; req.bRequest = CUE_CMD_READREG; USETW(req.wValue, 0); USETW(req.wIndex, reg); USETW(req.wLength, 2); err = usbd_do_request_flags(sc->cue_udev, &req, &val, USBD_NO_TSLEEP, NULL); - splx(s); + CUE_UNLOCK(sc); if (err) return(0); return(val); } Static int csr_write_1(sc, reg, val) struct cue_softc *sc; int reg, val; { usb_device_request_t req; usbd_status err; - int s; if (sc->cue_gone) return(0); - s = splusb(); + CUE_LOCK(sc); req.bmRequestType = UT_WRITE_VENDOR_DEVICE; req.bRequest = CUE_CMD_WRITEREG; USETW(req.wValue, val); USETW(req.wIndex, reg); USETW(req.wLength, 0); err = usbd_do_request_flags(sc->cue_udev, &req, &val, USBD_NO_TSLEEP, NULL); - splx(s); + CUE_UNLOCK(sc); if (err) return(-1); return(0); } #ifdef notdef Static int csr_write_2(sc, reg, val) struct cue_softc *sc; int reg, val; { usb_device_request_t req; usbd_status err; - int s; if (sc->cue_gone) return(0); - s = splusb(); + CUE_LOCK(sc); req.bmRequestType = UT_WRITE_VENDOR_DEVICE; req.bRequest = CUE_CMD_WRITEREG; USETW(req.wValue, val); USETW(req.wIndex, reg); USETW(req.wLength, 0); err = usbd_do_request_flags(sc->cue_udev, &req, &val, USBD_NO_TSLEEP, NULL); - splx(s); + CUE_UNLOCK(sc); if (err) return(-1); return(0); } #endif Static int cue_mem(sc, cmd, addr, buf, len) struct cue_softc *sc; int cmd; int addr; void *buf; int len; { usb_device_request_t req; usbd_status err; - int s; if (sc->cue_gone) return(0); - s = splusb(); + CUE_LOCK(sc); if (cmd == CUE_CMD_READSRAM) req.bmRequestType = UT_READ_VENDOR_DEVICE; else req.bmRequestType = UT_WRITE_VENDOR_DEVICE; req.bRequest = cmd; USETW(req.wValue, 0); USETW(req.wIndex, addr); USETW(req.wLength, len); err = usbd_do_request_flags(sc->cue_udev, &req, &buf, USBD_NO_TSLEEP, NULL); - splx(s); + CUE_UNLOCK(sc); if (err) return(-1); return(0); } Static int cue_getmac(sc, buf) struct cue_softc *sc; void *buf; { usb_device_request_t req; usbd_status err; - int s; if (sc->cue_gone) return(0); - s = splusb(); + CUE_LOCK(sc); req.bmRequestType = UT_READ_VENDOR_DEVICE; req.bRequest = CUE_CMD_GET_MACADDR; USETW(req.wValue, 0); USETW(req.wIndex, 0); USETW(req.wLength, ETHER_ADDR_LEN); err = usbd_do_request_flags(sc->cue_udev, &req, buf, USBD_NO_TSLEEP, NULL); - splx(s); + CUE_UNLOCK(sc); if (err) { printf("cue%d: read MAC address failed\n", sc->cue_unit); return(-1); } return(0); } #define CUE_POLY 0xEDB88320 #define CUE_BITS 9 Static u_int32_t cue_crc(addr) caddr_t addr; { u_int32_t idx, bit, data, crc; /* Compute CRC for the address value. */ crc = 0xFFFFFFFF; /* initial value */ for (idx = 0; idx < 6; idx++) { for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1) crc = (crc >> 1) ^ (((crc ^ data) & 1) ? CUE_POLY : 0); } return (crc & ((1 << CUE_BITS) - 1)); } Static void cue_setmulti(sc) struct cue_softc *sc; { struct ifnet *ifp; struct ifmultiaddr *ifma; u_int32_t h = 0, i; ifp = &sc->arpcom.ac_if; if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { for (i = 0; i < CUE_MCAST_TABLE_LEN; i++) sc->cue_mctab[i] = 0xFF; cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR, &sc->cue_mctab, CUE_MCAST_TABLE_LEN); return; } /* first, zot all the existing hash bits */ for (i = 0; i < CUE_MCAST_TABLE_LEN; i++) sc->cue_mctab[i] = 0; /* now program new ones */ for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL; ifma = ifma->ifma_link.le_next) { if (ifma->ifma_addr->sa_family != AF_LINK) continue; h = cue_crc(LLADDR((struct sockaddr_dl *)ifma->ifma_addr)); sc->cue_mctab[h >> 3] |= 1 << (h & 0x7); } /* * Also include the broadcast address in the filter * so we can receive broadcast frames. */ if (ifp->if_flags & IFF_BROADCAST) { h = cue_crc(etherbroadcastaddr); sc->cue_mctab[h >> 3] |= 1 << (h & 0x7); } cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR, &sc->cue_mctab, CUE_MCAST_TABLE_LEN); return; } Static void cue_reset(sc) struct cue_softc *sc; { usb_device_request_t req; usbd_status err; - int s; if (sc->cue_gone) return; - s = splusb(); - req.bmRequestType = UT_WRITE_VENDOR_DEVICE; req.bRequest = CUE_CMD_RESET; USETW(req.wValue, 0); USETW(req.wIndex, 0); USETW(req.wLength, 0); err = usbd_do_request_flags(sc->cue_udev, &req, NULL, USBD_NO_TSLEEP, NULL); - splx(s); - if (err) printf("cue%d: reset failed\n", sc->cue_unit); /* Wait a little while for the chip to get its brains in order. */ DELAY(1000); return; } /* * Probe for a Pegasus chip. */ USB_MATCH(cue) { USB_MATCH_START(cue, uaa); struct cue_type *t; if (!uaa->iface) return(UMATCH_NONE); t = cue_devs; while(t->cue_vid) { if (uaa->vendor == t->cue_vid && uaa->product == t->cue_did) { return(UMATCH_VENDOR_PRODUCT); } t++; } return(UMATCH_NONE); } /* * Attach the interface. Allocate softc structures, do ifmedia * setup and ethernet/BPF attach. */ USB_ATTACH(cue) { USB_ATTACH_START(cue, sc, uaa); char devinfo[1024]; - int s; u_char eaddr[ETHER_ADDR_LEN]; struct ifnet *ifp; usb_interface_descriptor_t *id; usb_endpoint_descriptor_t *ed; int i; - s = splimp(); - bzero(sc, sizeof(struct cue_softc)); sc->cue_iface = uaa->iface; sc->cue_udev = uaa->device; sc->cue_unit = device_get_unit(self); if (usbd_set_config_no(sc->cue_udev, CUE_CONFIG_NO, 0)) { printf("cue%d: getting interface handle failed\n", sc->cue_unit); - splx(s); USB_ATTACH_ERROR_RETURN; } id = usbd_get_interface_descriptor(uaa->iface); usbd_devinfo(uaa->device, 0, devinfo); device_set_desc_copy(self, devinfo); printf("%s: %s\n", USBDEVNAME(self), devinfo); /* Find endpoints. */ for (i = 0; i < id->bNumEndpoints; i++) { ed = usbd_interface2endpoint_descriptor(uaa->iface, i); if (!ed) { printf("cue%d: couldn't get ep %d\n", sc->cue_unit, i); - splx(s); USB_ATTACH_ERROR_RETURN; } if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) { sc->cue_ed[CUE_ENDPT_RX] = ed->bEndpointAddress; } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) { sc->cue_ed[CUE_ENDPT_TX] = ed->bEndpointAddress; } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) { sc->cue_ed[CUE_ENDPT_INTR] = ed->bEndpointAddress; } } + mtx_init(&sc->cue_mtx, device_get_nameunit(self), MTX_DEF); + CUE_LOCK(sc); + #ifdef notdef /* Reset the adapter. */ cue_reset(sc); #endif /* * Get station address. */ cue_getmac(sc, &eaddr); /* * A CATC chip was detected. Inform the world. */ printf("cue%d: Ethernet address: %6D\n", sc->cue_unit, eaddr, ":"); bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN); ifp = &sc->arpcom.ac_if; ifp->if_softc = sc; ifp->if_unit = sc->cue_unit; ifp->if_name = "cue"; ifp->if_mtu = ETHERMTU; ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; ifp->if_ioctl = cue_ioctl; ifp->if_output = ether_output; ifp->if_start = cue_start; ifp->if_watchdog = cue_watchdog; ifp->if_init = cue_init; ifp->if_baudrate = 10000000; ifp->if_snd.ifq_maxlen = IFQ_MAXLEN; cue_qdat.ifp = ifp; cue_qdat.if_rxstart = cue_rxstart; /* * Call MI attach routine. */ ether_ifattach(ifp, ETHER_BPF_SUPPORTED); callout_handle_init(&sc->cue_stat_ch); usb_register_netisr(); sc->cue_gone = 0; - splx(s); + CUE_UNLOCK(sc); USB_ATTACH_SUCCESS_RETURN; } Static int cue_detach(dev) device_t dev; { struct cue_softc *sc; struct ifnet *ifp; - int s; - s = splusb(); - sc = device_get_softc(dev); + CUE_LOCK(sc); ifp = &sc->arpcom.ac_if; sc->cue_gone = 1; untimeout(cue_tick, sc, sc->cue_stat_ch); ether_ifdetach(ifp, ETHER_BPF_SUPPORTED); if (sc->cue_ep[CUE_ENDPT_TX] != NULL) usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_TX]); if (sc->cue_ep[CUE_ENDPT_RX] != NULL) usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_RX]); if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_INTR]); - splx(s); + CUE_UNLOCK(sc); + mtx_destroy(&sc->cue_mtx); return(0); } /* * Initialize an RX descriptor and attach an MBUF cluster. */ Static int cue_newbuf(sc, c, m) struct cue_softc *sc; struct cue_chain *c; struct mbuf *m; { struct mbuf *m_new = NULL; if (m == NULL) { MGETHDR(m_new, M_DONTWAIT, MT_DATA); if (m_new == NULL) { printf("cue%d: no memory for rx list " "-- packet dropped!\n", sc->cue_unit); return(ENOBUFS); } MCLGET(m_new, M_DONTWAIT); if (!(m_new->m_flags & M_EXT)) { printf("cue%d: no memory for rx list " "-- packet dropped!\n", sc->cue_unit); m_freem(m_new); return(ENOBUFS); } m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; } else { m_new = m; m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; m_new->m_data = m_new->m_ext.ext_buf; } m_adj(m_new, ETHER_ALIGN); c->cue_mbuf = m_new; return(0); } Static int cue_rx_list_init(sc) struct cue_softc *sc; { struct cue_cdata *cd; struct cue_chain *c; int i; cd = &sc->cue_cdata; for (i = 0; i < CUE_RX_LIST_CNT; i++) { c = &cd->cue_rx_chain[i]; c->cue_sc = sc; c->cue_idx = i; if (cue_newbuf(sc, c, NULL) == ENOBUFS) return(ENOBUFS); if (c->cue_xfer == NULL) { c->cue_xfer = usbd_alloc_xfer(sc->cue_udev); if (c->cue_xfer == NULL) return(ENOBUFS); } } return(0); } Static int cue_tx_list_init(sc) struct cue_softc *sc; { struct cue_cdata *cd; struct cue_chain *c; int i; cd = &sc->cue_cdata; for (i = 0; i < CUE_TX_LIST_CNT; i++) { c = &cd->cue_tx_chain[i]; c->cue_sc = sc; c->cue_idx = i; c->cue_mbuf = NULL; if (c->cue_xfer == NULL) { c->cue_xfer = usbd_alloc_xfer(sc->cue_udev); if (c->cue_xfer == NULL) return(ENOBUFS); } c->cue_buf = malloc(CUE_BUFSZ, M_USBDEV, M_NOWAIT); if (c->cue_buf == NULL) return(ENOBUFS); } return(0); } Static void cue_rxstart(ifp) struct ifnet *ifp; { struct cue_softc *sc; struct cue_chain *c; sc = ifp->if_softc; + CUE_LOCK(sc); c = &sc->cue_cdata.cue_rx_chain[sc->cue_cdata.cue_rx_prod]; if (cue_newbuf(sc, c, NULL) == ENOBUFS) { ifp->if_ierrors++; + CUE_UNLOCK(sc); return; } /* Setup new transfer. */ usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX], c, mtod(c->cue_mbuf, char *), CUE_BUFSZ, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, cue_rxeof); usbd_transfer(c->cue_xfer); + CUE_UNLOCK(sc); return; } /* * A frame has been uploaded: pass the resulting mbuf chain up to * the higher level protocols. */ Static void cue_rxeof(xfer, priv, status) usbd_xfer_handle xfer; usbd_private_handle priv; usbd_status status; { struct cue_softc *sc; struct cue_chain *c; struct mbuf *m; struct ifnet *ifp; int total_len = 0; u_int16_t len; c = priv; sc = c->cue_sc; + CUE_LOCK(sc); ifp = &sc->arpcom.ac_if; - if (!(ifp->if_flags & IFF_RUNNING)) + if (!(ifp->if_flags & IFF_RUNNING)) { + CUE_UNLOCK(sc); return; + } if (status != USBD_NORMAL_COMPLETION) { - if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) + if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { + CUE_UNLOCK(sc); return; + } printf("cue%d: usb error on rx: %s\n", sc->cue_unit, usbd_errstr(status)); if (status == USBD_STALLED) usbd_clear_endpoint_stall(sc->cue_ep[CUE_ENDPT_RX]); goto done; } usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); m = c->cue_mbuf; len = *mtod(m, u_int16_t *); /* No errors; receive the packet. */ total_len = len; if (len < sizeof(struct ether_header)) { ifp->if_ierrors++; goto done; } ifp->if_ipackets++; m_adj(m, sizeof(u_int16_t)); m->m_pkthdr.rcvif = (struct ifnet *)&cue_qdat; m->m_pkthdr.len = m->m_len = total_len; /* Put the packet on the special USB input queue. */ usb_ether_input(m); + CUE_UNLOCK(sc); return; done: /* Setup new transfer. */ usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX], c, mtod(c->cue_mbuf, char *), CUE_BUFSZ, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, cue_rxeof); usbd_transfer(c->cue_xfer); + CUE_UNLOCK(sc); return; } /* * A frame was downloaded to the chip. It's safe for us to clean up * the list buffers. */ Static void cue_txeof(xfer, priv, status) usbd_xfer_handle xfer; usbd_private_handle priv; usbd_status status; { struct cue_softc *sc; struct cue_chain *c; struct ifnet *ifp; usbd_status err; - int s; - s = splimp(); - c = priv; sc = c->cue_sc; + CUE_LOCK(sc); ifp = &sc->arpcom.ac_if; if (status != USBD_NORMAL_COMPLETION) { if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { - splx(s); + CUE_UNLOCK(sc); return; } printf("cue%d: usb error on tx: %s\n", sc->cue_unit, usbd_errstr(status)); if (status == USBD_STALLED) usbd_clear_endpoint_stall(sc->cue_ep[CUE_ENDPT_TX]); - splx(s); + CUE_UNLOCK(sc); return; } ifp->if_timer = 0; ifp->if_flags &= ~IFF_OACTIVE; usbd_get_xfer_status(c->cue_xfer, NULL, NULL, NULL, &err); if (c->cue_mbuf != NULL) { c->cue_mbuf->m_pkthdr.rcvif = ifp; usb_tx_done(c->cue_mbuf); c->cue_mbuf = NULL; } if (err) ifp->if_oerrors++; else ifp->if_opackets++; - splx(s); + CUE_UNLOCK(sc); return; } Static void cue_tick(xsc) void *xsc; { struct cue_softc *sc; struct ifnet *ifp; - int s; - s = splimp(); - sc = xsc; - if (sc == NULL) { - splx(s); + if (sc == NULL) return; - } + CUE_LOCK(sc); + ifp = &sc->arpcom.ac_if; ifp->if_collisions += csr_read_2(sc, CUE_TX_SINGLECOLL); ifp->if_collisions += csr_read_2(sc, CUE_TX_MULTICOLL); ifp->if_collisions += csr_read_2(sc, CUE_TX_EXCESSCOLL); if (csr_read_2(sc, CUE_RX_FRAMEERR)) ifp->if_ierrors++; sc->cue_stat_ch = timeout(cue_tick, sc, hz); - splx(s); + CUE_UNLOCK(sc); return; } Static int cue_encap(sc, m, idx) struct cue_softc *sc; struct mbuf *m; int idx; { int total_len; struct cue_chain *c; usbd_status err; c = &sc->cue_cdata.cue_tx_chain[idx]; /* * Copy the mbuf data into a contiguous buffer, leaving two * bytes at the beginning to hold the frame length. */ m_copydata(m, 0, m->m_pkthdr.len, c->cue_buf + 2); c->cue_mbuf = m; total_len = m->m_pkthdr.len + 2; /* The first two bytes are the frame length */ c->cue_buf[0] = (u_int8_t)m->m_pkthdr.len; c->cue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8); usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_TX], c, c->cue_buf, total_len, 0, 10000, cue_txeof); /* Transmit */ err = usbd_transfer(c->cue_xfer); if (err != USBD_IN_PROGRESS) { cue_stop(sc); return(EIO); } sc->cue_cdata.cue_tx_cnt++; return(0); } Static void cue_start(ifp) struct ifnet *ifp; { struct cue_softc *sc; struct mbuf *m_head = NULL; sc = ifp->if_softc; + CUE_LOCK(sc); - if (ifp->if_flags & IFF_OACTIVE) + if (ifp->if_flags & IFF_OACTIVE) { + CUE_UNLOCK(sc); return; + } IF_DEQUEUE(&ifp->if_snd, m_head); - if (m_head == NULL) + if (m_head == NULL) { + CUE_UNLOCK(sc); return; + } if (cue_encap(sc, m_head, 0)) { IF_PREPEND(&ifp->if_snd, m_head); ifp->if_flags |= IFF_OACTIVE; + CUE_UNLOCK(sc); return; } /* * If there's a BPF listener, bounce a copy of this frame * to him. */ if (ifp->if_bpf) bpf_mtap(ifp, m_head); ifp->if_flags |= IFF_OACTIVE; /* * Set a timeout in case the chip goes out to lunch. */ ifp->if_timer = 5; + CUE_UNLOCK(sc); return; } Static void cue_init(xsc) void *xsc; { struct cue_softc *sc = xsc; struct ifnet *ifp = &sc->arpcom.ac_if; struct cue_chain *c; usbd_status err; - int i, s; + int i; if (ifp->if_flags & IFF_RUNNING) return; - s = splimp(); + CUE_LOCK(sc); /* * Cancel pending I/O and free all RX/TX buffers. */ #ifdef foo cue_reset(sc); #endif /* Set MAC address */ for (i = 0; i < ETHER_ADDR_LEN; i++) csr_write_1(sc, CUE_PAR0 - i, sc->arpcom.ac_enaddr[i]); /* Enable RX logic. */ csr_write_1(sc, CUE_ETHCTL, CUE_ETHCTL_RX_ON|CUE_ETHCTL_MCAST_ON); /* If we want promiscuous mode, set the allframes bit. */ if (ifp->if_flags & IFF_PROMISC) { CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC); } else { CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC); } /* Init TX ring. */ if (cue_tx_list_init(sc) == ENOBUFS) { printf("cue%d: tx list init failed\n", sc->cue_unit); - splx(s); + CUE_UNLOCK(sc); return; } /* Init RX ring. */ if (cue_rx_list_init(sc) == ENOBUFS) { printf("cue%d: rx list init failed\n", sc->cue_unit); - splx(s); + CUE_UNLOCK(sc); return; } /* Load the multicast filter. */ cue_setmulti(sc); /* * Set the number of RX and TX buffers that we want * to reserve inside the ASIC. */ csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES); csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES); /* Set advanced operation modes. */ csr_write_1(sc, CUE_ADVANCED_OPMODES, CUE_AOP_EMBED_RXLEN|0x01); /* 1 wait state */ /* Program the LED operation. */ csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK); /* Open RX and TX pipes. */ err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_RX], USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_RX]); if (err) { printf("cue%d: open rx pipe failed: %s\n", sc->cue_unit, usbd_errstr(err)); - splx(s); + CUE_UNLOCK(sc); return; } err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_TX], USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_TX]); if (err) { printf("cue%d: open tx pipe failed: %s\n", sc->cue_unit, usbd_errstr(err)); - splx(s); + CUE_UNLOCK(sc); return; } /* Start up the receive pipe. */ for (i = 0; i < CUE_RX_LIST_CNT; i++) { c = &sc->cue_cdata.cue_rx_chain[i]; usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX], c, mtod(c->cue_mbuf, char *), CUE_BUFSZ, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, cue_rxeof); usbd_transfer(c->cue_xfer); } ifp->if_flags |= IFF_RUNNING; ifp->if_flags &= ~IFF_OACTIVE; - (void)splx(s); + CUE_UNLOCK(sc); sc->cue_stat_ch = timeout(cue_tick, sc, hz); return; } Static int cue_ioctl(ifp, command, data) struct ifnet *ifp; u_long command; caddr_t data; { struct cue_softc *sc = ifp->if_softc; - int s, error = 0; + int error = 0; - s = splimp(); + CUE_LOCK(sc); switch(command) { case SIOCSIFADDR: case SIOCGIFADDR: case SIOCSIFMTU: error = ether_ioctl(ifp, command, data); break; case SIOCSIFFLAGS: if (ifp->if_flags & IFF_UP) { if (ifp->if_flags & IFF_RUNNING && ifp->if_flags & IFF_PROMISC && !(sc->cue_if_flags & IFF_PROMISC)) { CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC); cue_setmulti(sc); } else if (ifp->if_flags & IFF_RUNNING && !(ifp->if_flags & IFF_PROMISC) && sc->cue_if_flags & IFF_PROMISC) { CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC); cue_setmulti(sc); } else if (!(ifp->if_flags & IFF_RUNNING)) cue_init(sc); } else { if (ifp->if_flags & IFF_RUNNING) cue_stop(sc); } sc->cue_if_flags = ifp->if_flags; error = 0; break; case SIOCADDMULTI: case SIOCDELMULTI: cue_setmulti(sc); error = 0; break; default: error = EINVAL; break; } - (void)splx(s); + CUE_UNLOCK(sc); return(error); } Static void cue_watchdog(ifp) struct ifnet *ifp; { struct cue_softc *sc; struct cue_chain *c; - usbd_status stat; + sc = ifp->if_softc; + CUE_LOCK(sc); ifp->if_oerrors++; printf("cue%d: watchdog timeout\n", sc->cue_unit); c = &sc->cue_cdata.cue_tx_chain[0]; usbd_get_xfer_status(c->cue_xfer, NULL, NULL, NULL, &stat); cue_txeof(c->cue_xfer, c, stat); if (ifp->if_snd.ifq_head != NULL) cue_start(ifp); + CUE_UNLOCK(sc); return; } /* * Stop the adapter and free any mbufs allocated to the * RX and TX lists. */ Static void cue_stop(sc) struct cue_softc *sc; { usbd_status err; struct ifnet *ifp; int i; + CUE_LOCK(sc); + ifp = &sc->arpcom.ac_if; ifp->if_timer = 0; csr_write_1(sc, CUE_ETHCTL, 0); cue_reset(sc); untimeout(cue_tick, sc, sc->cue_stat_ch); /* Stop transfers. */ if (sc->cue_ep[CUE_ENDPT_RX] != NULL) { err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_RX]); if (err) { printf("cue%d: abort rx pipe failed: %s\n", sc->cue_unit, usbd_errstr(err)); } err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_RX]); if (err) { printf("cue%d: close rx pipe failed: %s\n", sc->cue_unit, usbd_errstr(err)); } sc->cue_ep[CUE_ENDPT_RX] = NULL; } if (sc->cue_ep[CUE_ENDPT_TX] != NULL) { err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_TX]); if (err) { printf("cue%d: abort tx pipe failed: %s\n", sc->cue_unit, usbd_errstr(err)); } err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_TX]); if (err) { printf("cue%d: close tx pipe failed: %s\n", sc->cue_unit, usbd_errstr(err)); } sc->cue_ep[CUE_ENDPT_TX] = NULL; } if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) { err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_INTR]); if (err) { printf("cue%d: abort intr pipe failed: %s\n", sc->cue_unit, usbd_errstr(err)); } err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_INTR]); if (err) { printf("cue%d: close intr pipe failed: %s\n", sc->cue_unit, usbd_errstr(err)); } sc->cue_ep[CUE_ENDPT_INTR] = NULL; } /* Free RX resources. */ for (i = 0; i < CUE_RX_LIST_CNT; i++) { if (sc->cue_cdata.cue_rx_chain[i].cue_buf != NULL) { free(sc->cue_cdata.cue_rx_chain[i].cue_buf, M_USBDEV); sc->cue_cdata.cue_rx_chain[i].cue_buf = NULL; } if (sc->cue_cdata.cue_rx_chain[i].cue_mbuf != NULL) { m_freem(sc->cue_cdata.cue_rx_chain[i].cue_mbuf); sc->cue_cdata.cue_rx_chain[i].cue_mbuf = NULL; } if (sc->cue_cdata.cue_rx_chain[i].cue_xfer != NULL) { usbd_free_xfer(sc->cue_cdata.cue_rx_chain[i].cue_xfer); sc->cue_cdata.cue_rx_chain[i].cue_xfer = NULL; } } /* Free TX resources. */ for (i = 0; i < CUE_TX_LIST_CNT; i++) { if (sc->cue_cdata.cue_tx_chain[i].cue_buf != NULL) { free(sc->cue_cdata.cue_tx_chain[i].cue_buf, M_USBDEV); sc->cue_cdata.cue_tx_chain[i].cue_buf = NULL; } if (sc->cue_cdata.cue_tx_chain[i].cue_mbuf != NULL) { m_freem(sc->cue_cdata.cue_tx_chain[i].cue_mbuf); sc->cue_cdata.cue_tx_chain[i].cue_mbuf = NULL; } if (sc->cue_cdata.cue_tx_chain[i].cue_xfer != NULL) { usbd_free_xfer(sc->cue_cdata.cue_tx_chain[i].cue_xfer); sc->cue_cdata.cue_tx_chain[i].cue_xfer = NULL; } } ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); + CUE_UNLOCK(sc); return; } /* * Stop all chip I/O so that the kernel's probe routines don't * get confused by errant DMAs when rebooting. */ Static void cue_shutdown(dev) device_t dev; { struct cue_softc *sc; sc = device_get_softc(dev); + CUE_LOCK(sc); cue_reset(sc); cue_stop(sc); + CUE_UNLOCK(sc); return; } Index: head/sys/dev/usb/if_cuereg.h =================================================================== --- head/sys/dev/usb/if_cuereg.h (revision 67529) +++ head/sys/dev/usb/if_cuereg.h (revision 67530) @@ -1,182 +1,186 @@ /* * Copyright (c) 1997, 1998, 1999, 2000 * 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. * * $FreeBSD$ */ /* * Definitions for the CATC Netmate II USB to ethernet controller. */ /* * Vendor specific control commands. */ #define CUE_CMD_RESET 0xF4 #define CUE_CMD_GET_MACADDR 0xF2 #define CUE_CMD_WRITEREG 0xFA #define CUE_CMD_READREG 0xFB #define CUE_CMD_READSRAM 0xF1 #define CUE_CMD_WRITESRAM 0xFC /* * Internal registers */ #define CUE_TX_BUFCNT 0x20 #define CUE_RX_BUFCNT 0x21 #define CUE_ADVANCED_OPMODES 0x22 #define CUE_TX_BUFPKTS 0x23 #define CUE_RX_BUFPKTS 0x24 #define CUE_RX_MAXCHAIN 0x25 #define CUE_ETHCTL 0x60 #define CUE_ETHSTS 0x61 #define CUE_PAR5 0x62 #define CUE_PAR4 0x63 #define CUE_PAR3 0x64 #define CUE_PAR2 0x65 #define CUE_PAR1 0x66 #define CUE_PAR0 0x67 /* Error counters, all 16 bits wide. */ #define CUE_TX_SINGLECOLL 0x69 #define CUE_TX_MULTICOLL 0x6B #define CUE_TX_EXCESSCOLL 0x6D #define CUE_RX_FRAMEERR 0x6F #define CUE_LEDCTL 0x81 /* Advenced operating mode register */ #define CUE_AOP_SRAMWAITS 0x03 #define CUE_AOP_EMBED_RXLEN 0x08 #define CUE_AOP_RXCOMBINE 0x10 #define CUE_AOP_TXCOMBINE 0x20 #define CUE_AOP_EVEN_PKT_READS 0x40 #define CUE_AOP_LOOPBK 0x80 /* Ethernet control register */ #define CUE_ETHCTL_RX_ON 0x01 #define CUE_ETHCTL_LINK_POLARITY 0x02 #define CUE_ETHCTL_LINK_FORCE_OK 0x04 #define CUE_ETHCTL_MCAST_ON 0x08 #define CUE_ETHCTL_PROMISC 0x10 /* Ethernet status register */ #define CUE_ETHSTS_NO_CARRIER 0x01 #define CUE_ETHSTS_LATECOLL 0x02 #define CUE_ETHSTS_EXCESSCOLL 0x04 #define CUE_ETHSTS_TXBUF_AVAIL 0x08 #define CUE_ETHSTS_BAD_POLARITY 0x10 #define CUE_ETHSTS_LINK_OK 0x20 /* LED control register */ #define CUE_LEDCTL_BLINK_1X 0x00 #define CUE_LEDCTL_BLINK_2X 0x01 #define CUE_LEDCTL_BLINK_QUARTER_ON 0x02 #define CUE_LEDCTL_BLINK_QUARTER_OFF 0x03 #define CUE_LEDCTL_OFF 0x04 #define CUE_LEDCTL_FOLLOW_LINK 0x08 /* * Address in ASIC's internal SRAM where the * multicast hash table lives. The table is 64 bytes long, * giving us a 512-bit table. We have to set the bit that * corresponds to the broadcast address in order to enable * reception of broadcast frames. */ #define CUE_MCAST_TABLE_ADDR 0xFA80 #define CUE_MCAST_TABLE_LEN 64 #define CUE_TIMEOUT 1000 #define ETHER_ALIGN 2 #define CUE_BUFSZ 1536 #define CUE_MIN_FRAMELEN 60 #define CUE_RX_FRAMES 1 #define CUE_TX_FRAMES 1 #define CUE_RX_LIST_CNT 1 #define CUE_TX_LIST_CNT 1 #define CUE_CTL_READ 0x01 #define CUE_CTL_WRITE 0x02 #define CUE_CONFIG_NO 1 /* * The interrupt endpoint is currently unused * by the KLSI part. */ #define CUE_ENDPT_RX 0x0 #define CUE_ENDPT_TX 0x1 #define CUE_ENDPT_INTR 0x2 #define CUE_ENDPT_MAX 0x3 struct cue_type { u_int16_t cue_vid; u_int16_t cue_did; }; struct cue_softc; struct cue_chain { struct cue_softc *cue_sc; usbd_xfer_handle cue_xfer; char *cue_buf; struct mbuf *cue_mbuf; int cue_accum; int cue_idx; }; struct cue_cdata { struct cue_chain cue_tx_chain[CUE_TX_LIST_CNT]; struct cue_chain cue_rx_chain[CUE_RX_LIST_CNT]; int cue_tx_prod; int cue_tx_cons; int cue_tx_cnt; int cue_rx_prod; }; #define CUE_INC(x, y) (x) = (x + 1) % y struct cue_softc { struct arpcom arpcom; usbd_device_handle cue_udev; usbd_interface_handle cue_iface; int cue_ed[CUE_ENDPT_MAX]; usbd_pipe_handle cue_ep[CUE_ENDPT_MAX]; int cue_unit; u_int8_t cue_mctab[CUE_MCAST_TABLE_LEN]; int cue_if_flags; u_int8_t cue_gone; u_int16_t cue_rxfilt; struct cue_cdata cue_cdata; struct callout_handle cue_stat_ch; + struct mtx cue_mtx; }; + +#define CUE_LOCK(_sc) mtx_enter(&(_sc)->cue_mtx, MTX_DEF) +#define CUE_UNLOCK(_sc) mtx_exit(&(_sc)->cue_mtx, MTX_DEF) Index: head/sys/dev/usb/if_kue.c =================================================================== --- head/sys/dev/usb/if_kue.c (revision 67529) +++ head/sys/dev/usb/if_kue.c (revision 67530) @@ -1,1128 +1,1145 @@ /* * Copyright (c) 1997, 1998, 1999, 2000 * 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. * * $FreeBSD$ */ /* * Kawasaki LSI KL5KUSB101B USB to ethernet adapter driver. * * Written by Bill Paul * Electrical Engineering Department * Columbia University, New York City */ /* * The KLSI USB to ethernet adapter chip contains an USB serial interface, * ethernet MAC and embedded microcontroller (called the QT Engine). * The chip must have firmware loaded into it before it will operate. * Packets are passed between the chip and host via bulk transfers. * There is an interrupt endpoint mentioned in the software spec, however * it's currently unused. This device is 10Mbps half-duplex only, hence * there is no media selection logic. The MAC supports a 128 entry * multicast filter, though the exact size of the filter can depend * on the firmware. Curiously, while the software spec describes various * ethernet statistics counters, my sample adapter and firmware combination * claims not to support any statistics counters at all. * * Note that once we load the firmware in the device, we have to be * careful not to load it again: if you restart your computer but * leave the adapter attached to the USB controller, it may remain * powered on and retain its firmware. In this case, we don't need * to load the firmware a second time. * * Special thanks to Rob Furr for providing an ADS Technologies * adapter for development and testing. No monkeys were harmed during * the development of this driver. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef lint static const char rcsid[] = "$FreeBSD$"; #endif /* * Various supported device vendors/products. */ Static struct kue_type kue_devs[] = { { USB_VENDOR_AOX, USB_PRODUCT_AOX_USB101 }, { USB_VENDOR_KLSI, USB_PRODUCT_AOX_USB101 }, { USB_VENDOR_ADS, USB_PRODUCT_ADS_UBS10BT }, { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC10T }, { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_EA101 }, { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET }, { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET2 }, { USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_E45 }, { USB_VENDOR_3COM, USB_PRODUCT_3COM_3C19250 }, { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_ETHER_USB_T }, { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650C }, { USB_VENDOR_SMC, USB_PRODUCT_SMC_2102USB }, { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10T }, { USB_VENDOR_KLSI, USB_PRODUCT_KLSI_DUH3E10BT }, { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET3 }, { 0, 0 } }; Static struct usb_qdat kue_qdat; Static int kue_match __P((device_t)); Static int kue_attach __P((device_t)); Static int kue_detach __P((device_t)); Static void kue_shutdown __P((device_t)); Static int kue_tx_list_init __P((struct kue_softc *)); Static int kue_rx_list_init __P((struct kue_softc *)); Static int kue_newbuf __P((struct kue_softc *, struct kue_chain *, struct mbuf *)); Static int kue_encap __P((struct kue_softc *, struct mbuf *, int)); Static void kue_rxeof __P((usbd_xfer_handle, usbd_private_handle, usbd_status)); Static void kue_txeof __P((usbd_xfer_handle, usbd_private_handle, usbd_status)); Static void kue_start __P((struct ifnet *)); Static void kue_rxstart __P((struct ifnet *)); Static int kue_ioctl __P((struct ifnet *, u_long, caddr_t)); Static void kue_init __P((void *)); Static void kue_stop __P((struct kue_softc *)); Static void kue_watchdog __P((struct ifnet *)); Static void kue_setmulti __P((struct kue_softc *)); Static void kue_reset __P((struct kue_softc *)); Static usbd_status kue_do_request __P((usbd_device_handle, usb_device_request_t *, void *)); Static usbd_status kue_ctl __P((struct kue_softc *, int, u_int8_t, u_int16_t, char *, int)); Static usbd_status kue_setword __P((struct kue_softc *, u_int8_t, u_int16_t)); Static int kue_load_fw __P((struct kue_softc *)); Static device_method_t kue_methods[] = { /* Device interface */ DEVMETHOD(device_probe, kue_match), DEVMETHOD(device_attach, kue_attach), DEVMETHOD(device_detach, kue_detach), DEVMETHOD(device_shutdown, kue_shutdown), { 0, 0 } }; Static driver_t kue_driver = { "kue", kue_methods, sizeof(struct kue_softc) }; Static devclass_t kue_devclass; DRIVER_MODULE(if_kue, uhub, kue_driver, kue_devclass, usbd_driver_load, 0); /* * We have a custom do_request function which is almost like the * regular do_request function, except it has a much longer timeout. * Why? Because we need to make requests over the control endpoint * to download the firmware to the device, which can take longer * than the default timeout. */ Static usbd_status kue_do_request(dev, req, data) usbd_device_handle dev; usb_device_request_t *req; void *data; { usbd_xfer_handle xfer; usbd_status err; xfer = usbd_alloc_xfer(dev); usbd_setup_default_xfer(xfer, dev, 0, 500000, req, data, UGETW(req->wLength), USBD_SHORT_XFER_OK|USBD_NO_TSLEEP, 0); err = usbd_sync_transfer(xfer); usbd_free_xfer(xfer); return(err); } Static usbd_status kue_setword(sc, breq, word) struct kue_softc *sc; u_int8_t breq; u_int16_t word; { usbd_device_handle dev; usb_device_request_t req; usbd_status err; - int s; if (sc->kue_gone) return(USBD_NORMAL_COMPLETION); dev = sc->kue_udev; - s = splusb(); + KUE_LOCK(sc); req.bmRequestType = UT_WRITE_VENDOR_DEVICE; req.bRequest = breq; USETW(req.wValue, word); USETW(req.wIndex, 0); USETW(req.wLength, 0); err = kue_do_request(dev, &req, NULL); - splx(s); + KUE_UNLOCK(sc); return(err); } Static usbd_status kue_ctl(sc, rw, breq, val, data, len) struct kue_softc *sc; int rw; u_int8_t breq; u_int16_t val; char *data; int len; { usbd_device_handle dev; usb_device_request_t req; usbd_status err; - int s; dev = sc->kue_udev; if (sc->kue_gone) return(USBD_NORMAL_COMPLETION); - s = splusb(); + KUE_LOCK(sc); if (rw == KUE_CTL_WRITE) req.bmRequestType = UT_WRITE_VENDOR_DEVICE; else req.bmRequestType = UT_READ_VENDOR_DEVICE; req.bRequest = breq; USETW(req.wValue, val); USETW(req.wIndex, 0); USETW(req.wLength, len); err = kue_do_request(dev, &req, data); - splx(s); + KUE_UNLOCK(sc); return(err); } Static int kue_load_fw(sc) struct kue_softc *sc; { usbd_status err; usb_device_descriptor_t *dd; int hwrev; dd = &sc->kue_udev->ddesc; hwrev = UGETW(dd->bcdDevice); /* * First, check if we even need to load the firmware. * If the device was still attached when the system was * rebooted, it may already have firmware loaded in it. * If this is the case, we don't need to do it again. * And in fact, if we try to load it again, we'll hang, * so we have to avoid this condition if we don't want * to look stupid. * * We can test this quickly by checking the bcdRevision * code. The NIC will return a different revision code if * it's probed while the firmware is still loaded and * running. */ if (hwrev == 0x0202) return(0); /* Load code segment */ err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN, 0, kue_code_seg, sizeof(kue_code_seg)); if (err) { printf("kue%d: failed to load code segment: %s\n", sc->kue_unit, usbd_errstr(err)); return(ENXIO); } /* Load fixup segment */ err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN, 0, kue_fix_seg, sizeof(kue_fix_seg)); if (err) { printf("kue%d: failed to load fixup segment: %s\n", sc->kue_unit, usbd_errstr(err)); return(ENXIO); } /* Send trigger command. */ err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN, 0, kue_trig_seg, sizeof(kue_trig_seg)); if (err) { printf("kue%d: failed to load trigger segment: %s\n", sc->kue_unit, usbd_errstr(err)); return(ENXIO); } return(0); } Static void kue_setmulti(sc) struct kue_softc *sc; { struct ifnet *ifp; struct ifmultiaddr *ifma; int i = 0; ifp = &sc->arpcom.ac_if; if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI; sc->kue_rxfilt &= ~KUE_RXFILT_MULTICAST; kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt); return; } sc->kue_rxfilt &= ~KUE_RXFILT_ALLMULTI; for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL; ifma = ifma->ifma_link.le_next) { if (ifma->ifma_addr->sa_family != AF_LINK) continue; /* * If there are too many addresses for the * internal filter, switch over to allmulti mode. */ if (i == KUE_MCFILTCNT(sc)) break; bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), KUE_MCFILT(sc, i), ETHER_ADDR_LEN); i++; } if (i == KUE_MCFILTCNT(sc)) sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI; else { sc->kue_rxfilt |= KUE_RXFILT_MULTICAST; kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MCAST_FILTERS, i, sc->kue_mcfilters, i * ETHER_ADDR_LEN); } kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt); return; } /* * Issue a SET_CONFIGURATION command to reset the MAC. This should be * done after the firmware is loaded into the adapter in order to * bring it into proper operation. */ Static void kue_reset(sc) struct kue_softc *sc; { if (usbd_set_config_no(sc->kue_udev, KUE_CONFIG_NO, 0)) { printf("kue%d: getting interface handle failed\n", sc->kue_unit); } /* Wait a little while for the chip to get its brains in order. */ DELAY(1000); return; } /* * Probe for a KLSI chip. */ USB_MATCH(kue) { USB_MATCH_START(kue, uaa); struct kue_type *t; if (!uaa->iface) return(UMATCH_NONE); t = kue_devs; while(t->kue_vid) { if (uaa->vendor == t->kue_vid && uaa->product == t->kue_did) { return(UMATCH_VENDOR_PRODUCT); } t++; } return(UMATCH_NONE); } /* * Attach the interface. Allocate softc structures, do * setup and ethernet/BPF attach. */ USB_ATTACH(kue) { USB_ATTACH_START(kue, sc, uaa); char devinfo[1024]; - int s; struct ifnet *ifp; usbd_status err; usb_interface_descriptor_t *id; usb_endpoint_descriptor_t *ed; int i; - s = splimp(); - bzero(sc, sizeof(struct kue_softc)); sc->kue_iface = uaa->iface; sc->kue_udev = uaa->device; sc->kue_unit = device_get_unit(self); id = usbd_get_interface_descriptor(uaa->iface); usbd_devinfo(uaa->device, 0, devinfo); device_set_desc_copy(self, devinfo); printf("%s: %s\n", USBDEVNAME(self), devinfo); /* Find endpoints. */ for (i = 0; i < id->bNumEndpoints; i++) { ed = usbd_interface2endpoint_descriptor(uaa->iface, i); if (!ed) { printf("kue%d: couldn't get ep %d\n", sc->kue_unit, i); - splx(s); USB_ATTACH_ERROR_RETURN; } if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) { sc->kue_ed[KUE_ENDPT_RX] = ed->bEndpointAddress; } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) { sc->kue_ed[KUE_ENDPT_TX] = ed->bEndpointAddress; } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) { sc->kue_ed[KUE_ENDPT_INTR] = ed->bEndpointAddress; } } + mtx_init(&sc->kue_mtx, device_get_nameunit(self), MTX_DEF); + KUE_LOCK(sc); + /* Load the firmware into the NIC. */ if (kue_load_fw(sc)) { - splx(s); + KUE_UNLOCK(sc); + mtx_destroy(&sc->kue_mtx); USB_ATTACH_ERROR_RETURN; } /* Reset the adapter. */ kue_reset(sc); /* Read ethernet descriptor */ err = kue_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR, 0, (char *)&sc->kue_desc, sizeof(sc->kue_desc)); sc->kue_mcfilters = malloc(KUE_MCFILTCNT(sc) * ETHER_ADDR_LEN, M_USBDEV, M_NOWAIT); /* * A KLSI chip was detected. Inform the world. */ printf("kue%d: Ethernet address: %6D\n", sc->kue_unit, sc->kue_desc.kue_macaddr, ":"); bcopy(sc->kue_desc.kue_macaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN); ifp = &sc->arpcom.ac_if; ifp->if_softc = sc; ifp->if_unit = sc->kue_unit; ifp->if_name = "kue"; ifp->if_mtu = ETHERMTU; ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; ifp->if_ioctl = kue_ioctl; ifp->if_output = ether_output; ifp->if_start = kue_start; ifp->if_watchdog = kue_watchdog; ifp->if_init = kue_init; ifp->if_baudrate = 10000000; ifp->if_snd.ifq_maxlen = IFQ_MAXLEN; kue_qdat.ifp = ifp; kue_qdat.if_rxstart = kue_rxstart; /* * Call MI attach routine. */ ether_ifattach(ifp, ETHER_BPF_SUPPORTED); usb_register_netisr(); sc->kue_gone = 0; - splx(s); + KUE_UNLOCK(sc); + USB_ATTACH_SUCCESS_RETURN; } Static int kue_detach(dev) device_t dev; { struct kue_softc *sc; struct ifnet *ifp; - int s; - s = splusb(); - sc = device_get_softc(dev); + KUE_LOCK(sc); ifp = &sc->arpcom.ac_if; sc->kue_gone = 1; if (ifp != NULL) ether_ifdetach(ifp, ETHER_BPF_SUPPORTED); if (sc->kue_ep[KUE_ENDPT_TX] != NULL) usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]); if (sc->kue_ep[KUE_ENDPT_RX] != NULL) usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]); if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]); if (sc->kue_mcfilters != NULL) free(sc->kue_mcfilters, M_USBDEV); - splx(s); + KUE_UNLOCK(sc); + mtx_destroy(&sc->kue_mtx); return(0); } /* * Initialize an RX descriptor and attach an MBUF cluster. */ Static int kue_newbuf(sc, c, m) struct kue_softc *sc; struct kue_chain *c; struct mbuf *m; { struct mbuf *m_new = NULL; if (m == NULL) { MGETHDR(m_new, M_DONTWAIT, MT_DATA); if (m_new == NULL) { printf("kue%d: no memory for rx list " "-- packet dropped!\n", sc->kue_unit); return(ENOBUFS); } MCLGET(m_new, M_DONTWAIT); if (!(m_new->m_flags & M_EXT)) { printf("kue%d: no memory for rx list " "-- packet dropped!\n", sc->kue_unit); m_freem(m_new); return(ENOBUFS); } m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; } else { m_new = m; m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; m_new->m_data = m_new->m_ext.ext_buf; } c->kue_mbuf = m_new; return(0); } Static int kue_rx_list_init(sc) struct kue_softc *sc; { struct kue_cdata *cd; struct kue_chain *c; int i; cd = &sc->kue_cdata; for (i = 0; i < KUE_RX_LIST_CNT; i++) { c = &cd->kue_rx_chain[i]; c->kue_sc = sc; c->kue_idx = i; if (kue_newbuf(sc, c, NULL) == ENOBUFS) return(ENOBUFS); if (c->kue_xfer == NULL) { c->kue_xfer = usbd_alloc_xfer(sc->kue_udev); if (c->kue_xfer == NULL) return(ENOBUFS); } } return(0); } Static int kue_tx_list_init(sc) struct kue_softc *sc; { struct kue_cdata *cd; struct kue_chain *c; int i; cd = &sc->kue_cdata; for (i = 0; i < KUE_TX_LIST_CNT; i++) { c = &cd->kue_tx_chain[i]; c->kue_sc = sc; c->kue_idx = i; c->kue_mbuf = NULL; if (c->kue_xfer == NULL) { c->kue_xfer = usbd_alloc_xfer(sc->kue_udev); if (c->kue_xfer == NULL) return(ENOBUFS); } c->kue_buf = malloc(KUE_BUFSZ, M_USBDEV, M_NOWAIT); if (c->kue_buf == NULL) return(ENOBUFS); } return(0); } Static void kue_rxstart(ifp) struct ifnet *ifp; { struct kue_softc *sc; struct kue_chain *c; sc = ifp->if_softc; + KUE_LOCK(sc); c = &sc->kue_cdata.kue_rx_chain[sc->kue_cdata.kue_rx_prod]; if (kue_newbuf(sc, c, NULL) == ENOBUFS) { ifp->if_ierrors++; return; } /* Setup new transfer. */ usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX], c, mtod(c->kue_mbuf, char *), KUE_BUFSZ, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, kue_rxeof); usbd_transfer(c->kue_xfer); + KUE_UNLOCK(sc); + return; } /* * A frame has been uploaded: pass the resulting mbuf chain up to * the higher level protocols. */ Static void kue_rxeof(xfer, priv, status) usbd_xfer_handle xfer; usbd_private_handle priv; usbd_status status; { struct kue_softc *sc; struct kue_chain *c; struct mbuf *m; struct ifnet *ifp; int total_len = 0; u_int16_t len; c = priv; sc = c->kue_sc; + KUE_LOCK(sc); ifp = &sc->arpcom.ac_if; - if (!(ifp->if_flags & IFF_RUNNING)) + if (!(ifp->if_flags & IFF_RUNNING)) { + KUE_UNLOCK(sc); return; + } if (status != USBD_NORMAL_COMPLETION) { - if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) + if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { + KUE_UNLOCK(sc); return; + } printf("kue%d: usb error on rx: %s\n", sc->kue_unit, usbd_errstr(status)); if (status == USBD_STALLED) usbd_clear_endpoint_stall(sc->kue_ep[KUE_ENDPT_RX]); goto done; } usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); m = c->kue_mbuf; if (total_len <= 1) goto done; len = *mtod(m, u_int16_t *); m_adj(m, sizeof(u_int16_t)); /* No errors; receive the packet. */ total_len = len; if (len < sizeof(struct ether_header)) { ifp->if_ierrors++; goto done; } ifp->if_ipackets++; m->m_pkthdr.rcvif = (struct ifnet *)&kue_qdat; m->m_pkthdr.len = m->m_len = total_len; /* Put the packet on the special USB input queue. */ usb_ether_input(m); + KUE_UNLOCK(sc); return; done: /* Setup new transfer. */ usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX], c, mtod(c->kue_mbuf, char *), KUE_BUFSZ, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, kue_rxeof); usbd_transfer(c->kue_xfer); + KUE_UNLOCK(sc); return; } /* * A frame was downloaded to the chip. It's safe for us to clean up * the list buffers. */ Static void kue_txeof(xfer, priv, status) usbd_xfer_handle xfer; usbd_private_handle priv; usbd_status status; { struct kue_softc *sc; struct kue_chain *c; struct ifnet *ifp; usbd_status err; - int s; - s = splimp(); - c = priv; sc = c->kue_sc; + KUE_LOCK(sc); + ifp = &sc->arpcom.ac_if; ifp->if_timer = 0; ifp->if_flags &= ~IFF_OACTIVE; if (status != USBD_NORMAL_COMPLETION) { if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { - splx(s); + KUE_UNLOCK(sc); return; } printf("kue%d: usb error on tx: %s\n", sc->kue_unit, usbd_errstr(status)); if (status == USBD_STALLED) usbd_clear_endpoint_stall(sc->kue_ep[KUE_ENDPT_TX]); - splx(s); + KUE_UNLOCK(sc); return; } usbd_get_xfer_status(c->kue_xfer, NULL, NULL, NULL, &err); if (c->kue_mbuf != NULL) { c->kue_mbuf->m_pkthdr.rcvif = ifp; usb_tx_done(c->kue_mbuf); c->kue_mbuf = NULL; } if (err) ifp->if_oerrors++; else ifp->if_opackets++; - splx(s); + KUE_UNLOCK(sc); return; } Static int kue_encap(sc, m, idx) struct kue_softc *sc; struct mbuf *m; int idx; { int total_len; struct kue_chain *c; usbd_status err; c = &sc->kue_cdata.kue_tx_chain[idx]; /* * Copy the mbuf data into a contiguous buffer, leaving two * bytes at the beginning to hold the frame length. */ m_copydata(m, 0, m->m_pkthdr.len, c->kue_buf + 2); c->kue_mbuf = m; total_len = m->m_pkthdr.len + 2; total_len += 64 - (total_len % 64); /* Frame length is specified in the first 2 bytes of the buffer. */ c->kue_buf[0] = (u_int8_t)m->m_pkthdr.len; c->kue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8); usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_TX], c, c->kue_buf, total_len, 0, 10000, kue_txeof); /* Transmit */ err = usbd_transfer(c->kue_xfer); if (err != USBD_IN_PROGRESS) { kue_stop(sc); return(EIO); } sc->kue_cdata.kue_tx_cnt++; return(0); } Static void kue_start(ifp) struct ifnet *ifp; { struct kue_softc *sc; struct mbuf *m_head = NULL; sc = ifp->if_softc; + KUE_LOCK(sc); - if (ifp->if_flags & IFF_OACTIVE) + if (ifp->if_flags & IFF_OACTIVE) { + KUE_UNLOCK(sc); return; + } IF_DEQUEUE(&ifp->if_snd, m_head); - if (m_head == NULL) + if (m_head == NULL) { + KUE_UNLOCK(sc); return; + } if (kue_encap(sc, m_head, 0)) { IF_PREPEND(&ifp->if_snd, m_head); ifp->if_flags |= IFF_OACTIVE; + KUE_UNLOCK(sc); return; } /* * If there's a BPF listener, bounce a copy of this frame * to him. */ if (ifp->if_bpf) bpf_mtap(ifp, m_head); ifp->if_flags |= IFF_OACTIVE; /* * Set a timeout in case the chip goes out to lunch. */ ifp->if_timer = 5; + KUE_UNLOCK(sc); return; } Static void kue_init(xsc) void *xsc; { struct kue_softc *sc = xsc; struct ifnet *ifp = &sc->arpcom.ac_if; struct kue_chain *c; usbd_status err; - int i, s; + int i; + KUE_LOCK(sc); + if (ifp->if_flags & IFF_RUNNING) return; - s = splimp(); - /* Set MAC address */ kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC, 0, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN); sc->kue_rxfilt = KUE_RXFILT_UNICAST|KUE_RXFILT_BROADCAST; /* If we want promiscuous mode, set the allframes bit. */ if (ifp->if_flags & IFF_PROMISC) sc->kue_rxfilt |= KUE_RXFILT_PROMISC; kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt); /* I'm not sure how to tune these. */ #ifdef notdef /* * Leave this one alone for now; setting it * wrong causes lockups on some machines/controllers. */ kue_setword(sc, KUE_CMD_SET_SOFS, 1); #endif kue_setword(sc, KUE_CMD_SET_URB_SIZE, 64); /* Init TX ring. */ if (kue_tx_list_init(sc) == ENOBUFS) { printf("kue%d: tx list init failed\n", sc->kue_unit); - splx(s); + KUE_UNLOCK(sc); return; } /* Init RX ring. */ if (kue_rx_list_init(sc) == ENOBUFS) { printf("kue%d: rx list init failed\n", sc->kue_unit); - splx(s); + KUE_UNLOCK(sc); return; } /* Load the multicast filter. */ kue_setmulti(sc); /* Open RX and TX pipes. */ err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_RX], USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_RX]); if (err) { printf("kue%d: open rx pipe failed: %s\n", sc->kue_unit, usbd_errstr(err)); - splx(s); + KUE_UNLOCK(sc); return; } err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_TX], USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_TX]); if (err) { printf("kue%d: open tx pipe failed: %s\n", sc->kue_unit, usbd_errstr(err)); - splx(s); + KUE_UNLOCK(sc); return; } /* Start up the receive pipe. */ for (i = 0; i < KUE_RX_LIST_CNT; i++) { c = &sc->kue_cdata.kue_rx_chain[i]; usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX], c, mtod(c->kue_mbuf, char *), KUE_BUFSZ, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, kue_rxeof); usbd_transfer(c->kue_xfer); } ifp->if_flags |= IFF_RUNNING; ifp->if_flags &= ~IFF_OACTIVE; - (void)splx(s); + KUE_UNLOCK(sc); return; } Static int kue_ioctl(ifp, command, data) struct ifnet *ifp; u_long command; caddr_t data; { struct kue_softc *sc = ifp->if_softc; - int s, error = 0; + int error = 0; - s = splimp(); + KUE_LOCK(sc); switch(command) { case SIOCSIFADDR: case SIOCGIFADDR: case SIOCSIFMTU: error = ether_ioctl(ifp, command, data); break; case SIOCSIFFLAGS: if (ifp->if_flags & IFF_UP) { if (ifp->if_flags & IFF_RUNNING && ifp->if_flags & IFF_PROMISC && !(sc->kue_if_flags & IFF_PROMISC)) { sc->kue_rxfilt |= KUE_RXFILT_PROMISC; kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt); } else if (ifp->if_flags & IFF_RUNNING && !(ifp->if_flags & IFF_PROMISC) && sc->kue_if_flags & IFF_PROMISC) { sc->kue_rxfilt &= ~KUE_RXFILT_PROMISC; kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt); } else if (!(ifp->if_flags & IFF_RUNNING)) kue_init(sc); } else { if (ifp->if_flags & IFF_RUNNING) kue_stop(sc); } sc->kue_if_flags = ifp->if_flags; error = 0; break; case SIOCADDMULTI: case SIOCDELMULTI: kue_setmulti(sc); error = 0; break; default: error = EINVAL; break; } - (void)splx(s); + KUE_UNLOCK(sc); return(error); } Static void kue_watchdog(ifp) struct ifnet *ifp; { struct kue_softc *sc; struct kue_chain *c; usbd_status stat; sc = ifp->if_softc; - + KUE_LOCK(sc); ifp->if_oerrors++; printf("kue%d: watchdog timeout\n", sc->kue_unit); c = &sc->kue_cdata.kue_tx_chain[0]; usbd_get_xfer_status(c->kue_xfer, NULL, NULL, NULL, &stat); kue_txeof(c->kue_xfer, c, stat); if (ifp->if_snd.ifq_head != NULL) kue_start(ifp); + KUE_UNLOCK(sc); return; } /* * Stop the adapter and free any mbufs allocated to the * RX and TX lists. */ Static void kue_stop(sc) struct kue_softc *sc; { usbd_status err; struct ifnet *ifp; int i; + KUE_LOCK(sc); ifp = &sc->arpcom.ac_if; ifp->if_timer = 0; /* Stop transfers. */ if (sc->kue_ep[KUE_ENDPT_RX] != NULL) { err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]); if (err) { printf("kue%d: abort rx pipe failed: %s\n", sc->kue_unit, usbd_errstr(err)); } err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_RX]); if (err) { printf("kue%d: close rx pipe failed: %s\n", sc->kue_unit, usbd_errstr(err)); } sc->kue_ep[KUE_ENDPT_RX] = NULL; } if (sc->kue_ep[KUE_ENDPT_TX] != NULL) { err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]); if (err) { printf("kue%d: abort tx pipe failed: %s\n", sc->kue_unit, usbd_errstr(err)); } err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_TX]); if (err) { printf("kue%d: close tx pipe failed: %s\n", sc->kue_unit, usbd_errstr(err)); } sc->kue_ep[KUE_ENDPT_TX] = NULL; } if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) { err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]); if (err) { printf("kue%d: abort intr pipe failed: %s\n", sc->kue_unit, usbd_errstr(err)); } err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_INTR]); if (err) { printf("kue%d: close intr pipe failed: %s\n", sc->kue_unit, usbd_errstr(err)); } sc->kue_ep[KUE_ENDPT_INTR] = NULL; } /* Free RX resources. */ for (i = 0; i < KUE_RX_LIST_CNT; i++) { if (sc->kue_cdata.kue_rx_chain[i].kue_buf != NULL) { free(sc->kue_cdata.kue_rx_chain[i].kue_buf, M_USBDEV); sc->kue_cdata.kue_rx_chain[i].kue_buf = NULL; } if (sc->kue_cdata.kue_rx_chain[i].kue_mbuf != NULL) { m_freem(sc->kue_cdata.kue_rx_chain[i].kue_mbuf); sc->kue_cdata.kue_rx_chain[i].kue_mbuf = NULL; } if (sc->kue_cdata.kue_rx_chain[i].kue_xfer != NULL) { usbd_free_xfer(sc->kue_cdata.kue_rx_chain[i].kue_xfer); sc->kue_cdata.kue_rx_chain[i].kue_xfer = NULL; } } /* Free TX resources. */ for (i = 0; i < KUE_TX_LIST_CNT; i++) { if (sc->kue_cdata.kue_tx_chain[i].kue_buf != NULL) { free(sc->kue_cdata.kue_tx_chain[i].kue_buf, M_USBDEV); sc->kue_cdata.kue_tx_chain[i].kue_buf = NULL; } if (sc->kue_cdata.kue_tx_chain[i].kue_mbuf != NULL) { m_freem(sc->kue_cdata.kue_tx_chain[i].kue_mbuf); sc->kue_cdata.kue_tx_chain[i].kue_mbuf = NULL; } if (sc->kue_cdata.kue_tx_chain[i].kue_xfer != NULL) { usbd_free_xfer(sc->kue_cdata.kue_tx_chain[i].kue_xfer); sc->kue_cdata.kue_tx_chain[i].kue_xfer = NULL; } } ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); + KUE_UNLOCK(sc); return; } /* * Stop all chip I/O so that the kernel's probe routines don't * get confused by errant DMAs when rebooting. */ Static void kue_shutdown(dev) device_t dev; { struct kue_softc *sc; sc = device_get_softc(dev); kue_stop(sc); return; } Index: head/sys/dev/usb/if_kuereg.h =================================================================== --- head/sys/dev/usb/if_kuereg.h (revision 67529) +++ head/sys/dev/usb/if_kuereg.h (revision 67530) @@ -1,173 +1,177 @@ /* * Copyright (c) 1997, 1998, 1999, 2000 * 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. * * $FreeBSD$ */ /* * Definitions for the KLSI KL5KUSB101B USB to ethernet controller. * The KLSI part is controlled via vendor control requests, the structure * of which depend a bit on the firmware running on the internal * microcontroller. The one exception is the 'send scan data' command, * which is used to load the firmware. */ #define KUE_CMD_GET_ETHER_DESCRIPTOR 0x00 #define KUE_CMD_SET_MCAST_FILTERS 0x01 #define KUE_CMD_SET_PKT_FILTER 0x02 #define KUE_CMD_GET_ETHERSTATS 0x03 #define KUE_CMD_GET_GPIO 0x04 #define KUE_CMD_SET_GPIO 0x05 #define KUE_CMD_SET_MAC 0x06 #define KUE_CMD_GET_MAC 0x07 #define KUE_CMD_SET_URB_SIZE 0x08 #define KUE_CMD_SET_SOFS 0x09 #define KUE_CMD_SET_EVEN_PKTS 0x0A #define KUE_CMD_SEND_SCAN 0xFF struct kue_ether_desc { u_int8_t kue_len; u_int8_t kue_rsvd0; u_int8_t kue_rsvd1; u_int8_t kue_macaddr[ETHER_ADDR_LEN]; u_int8_t kue_etherstats[4]; u_int8_t kue_maxseg[2]; u_int8_t kue_mcastfilt[2]; u_int8_t kue_rsvd2; }; #define KUE_ETHERSTATS(x) \ (*(u_int32_t *)&(x)->kue_desc.kue_etherstats) #define KUE_MAXSEG(x) \ (*(u_int16_t *)&(x)->kue_desc.kue_maxseg) #define KUE_MCFILTCNT(x) \ ((*(u_int16_t *)&(x)->kue_desc.kue_mcastfilt) & 0x7FFF) #define KUE_MCFILT(x, y) \ (char *)&(sc->kue_mcfilters[y * ETHER_ADDR_LEN]) #define KUE_STAT_TX_OK 0x00000001 #define KUE_STAT_RX_OK 0x00000002 #define KUE_STAT_TX_ERR 0x00000004 #define KUE_STAT_RX_ERR 0x00000008 #define KUE_STAT_RX_NOBUF 0x00000010 #define KUE_STAT_TX_UCAST_BYTES 0x00000020 #define KUE_STAT_TX_UCAST_FRAMES 0x00000040 #define KUE_STAT_TX_MCAST_BYTES 0x00000080 #define KUE_STAT_TX_MCAST_FRAMES 0x00000100 #define KUE_STAT_TX_BCAST_BYTES 0x00000200 #define KUE_STAT_TX_BCAST_FRAMES 0x00000400 #define KUE_STAT_RX_UCAST_BYTES 0x00000800 #define KUE_STAT_RX_UCAST_FRAMES 0x00001000 #define KUE_STAT_RX_MCAST_BYTES 0x00002000 #define KUE_STAT_RX_MCAST_FRAMES 0x00004000 #define KUE_STAT_RX_BCAST_BYTES 0x00008000 #define KUE_STAT_RX_BCAST_FRAMES 0x00010000 #define KUE_STAT_RX_CRCERR 0x00020000 #define KUE_STAT_TX_QUEUE_LENGTH 0x00040000 #define KUE_STAT_RX_ALIGNERR 0x00080000 #define KUE_STAT_TX_SINGLECOLL 0x00100000 #define KUE_STAT_TX_MULTICOLL 0x00200000 #define KUE_STAT_TX_DEFERRED 0x00400000 #define KUE_STAT_TX_MAXCOLLS 0x00800000 #define KUE_STAT_RX_OVERRUN 0x01000000 #define KUE_STAT_TX_UNDERRUN 0x02000000 #define KUE_STAT_TX_SQE_ERR 0x04000000 #define KUE_STAT_TX_CARRLOSS 0x08000000 #define KUE_STAT_RX_LATECOLL 0x10000000 #define KUE_RXFILT_PROMISC 0x0001 #define KUE_RXFILT_ALLMULTI 0x0002 #define KUE_RXFILT_UNICAST 0x0004 #define KUE_RXFILT_BROADCAST 0x0008 #define KUE_RXFILT_MULTICAST 0x0010 #define KUE_TIMEOUT 1000 #define ETHER_ALIGN 2 #define KUE_BUFSZ 1536 #define KUE_MIN_FRAMELEN 60 #define KUE_RX_LIST_CNT 1 #define KUE_TX_LIST_CNT 1 #define KUE_CTL_READ 0x01 #define KUE_CTL_WRITE 0x02 #define KUE_CONFIG_NO 1 /* * The interrupt endpoint is currently unused * by the KLSI part. */ #define KUE_ENDPT_RX 0x0 #define KUE_ENDPT_TX 0x1 #define KUE_ENDPT_INTR 0x2 #define KUE_ENDPT_MAX 0x3 struct kue_type { u_int16_t kue_vid; u_int16_t kue_did; }; struct kue_softc; struct kue_chain { struct kue_softc *kue_sc; usbd_xfer_handle kue_xfer; char *kue_buf; struct mbuf *kue_mbuf; int kue_idx; }; struct kue_cdata { struct kue_chain kue_tx_chain[KUE_TX_LIST_CNT]; struct kue_chain kue_rx_chain[KUE_RX_LIST_CNT]; int kue_tx_prod; int kue_tx_cons; int kue_tx_cnt; int kue_rx_prod; }; #define KUE_INC(x, y) (x) = (x + 1) % y struct kue_softc { struct arpcom arpcom; usbd_device_handle kue_udev; usbd_interface_handle kue_iface; struct kue_ether_desc kue_desc; int kue_ed[KUE_ENDPT_MAX]; usbd_pipe_handle kue_ep[KUE_ENDPT_MAX]; int kue_unit; int kue_if_flags; u_int8_t kue_gone; u_int16_t kue_rxfilt; u_int8_t *kue_mcfilters; struct kue_cdata kue_cdata; + struct mtx kue_mtx; }; + +#define KUE_LOCK(_sc) mtx_enter(&(_sc)->kue_mtx, MTX_DEF) +#define KUE_UNLOCK(_sc) mtx_exit(&(_sc)->kue_mtx, MTX_DEF) Index: head/sys/dev/usb/usb_ethersubr.c =================================================================== --- head/sys/dev/usb/usb_ethersubr.c (revision 67529) +++ head/sys/dev/usb/usb_ethersubr.c (revision 67530) @@ -1,154 +1,161 @@ /* * Copyright (c) 1997, 1998, 1999, 2000 * 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. * * $FreeBSD$ */ /* * Callbacks in the USB code operate at splusb() (actually splbio() * in FreeBSD). However adding packets to the input queues has to be * done at splimp(). It is conceivable that this arrangement could * trigger a condition where the splimp() is ignored and the input * queues could get trampled in spite of our best effors to prevent * it. To work around this, we implement a special input queue for * USB ethernet adapter drivers. Rather than passing the frames directly * to ether_input(), we pass them here, then schedule a soft interrupt * to hand them to ether_input() later, outside of the USB interrupt * context. * * It's questional as to whether this code should be expanded to * handle other kinds of devices, or handle USB transfer callbacks * in general. Right now, I need USB network interfaces to work * properly. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef lint Static const char rcsid[] = "$FreeBSD$"; #endif Static struct ifqueue usbq_rx; +Static struct mtx usbq_rx_mtx; Static struct ifqueue usbq_tx; +Static struct mtx usbq_tx_mtx; +Static int mtx_inited = 0; Static void usbintr __P((void)); Static void usbintr() { struct ether_header *eh; struct mbuf *m; struct usb_qdat *q; struct ifnet *ifp; - int s; - s = splimp(); - /* Check the RX queue */ while(1) { + mtx_enter(&usbq_rx_mtx, MTX_DEF); IF_DEQUEUE(&usbq_rx, m); + mtx_exit(&usbq_rx_mtx, MTX_DEF); if (m == NULL) break; eh = mtod(m, struct ether_header *); q = (struct usb_qdat *)m->m_pkthdr.rcvif; ifp = q->ifp; m->m_pkthdr.rcvif = ifp; m_adj(m, sizeof(struct ether_header)); ether_input(ifp, eh, m); /* Re-arm the receiver */ (*q->if_rxstart)(ifp); if (ifp->if_snd.ifq_head != NULL) (*ifp->if_start)(ifp); } /* Check the TX queue */ while(1) { + mtx_enter(&usbq_tx_mtx, MTX_DEF); IF_DEQUEUE(&usbq_tx, m); + mtx_exit(&usbq_tx_mtx, MTX_DEF); if (m == NULL) break; ifp = m->m_pkthdr.rcvif; m_freem(m); if (ifp->if_snd.ifq_head != NULL) (*ifp->if_start)(ifp); } - splx(s); - return; } void usb_register_netisr() { + if (mtx_inited) + return; register_netisr(NETISR_USB, usbintr); + mtx_init(&usbq_tx_mtx, "usbq_tx_mtx", MTX_DEF); + mtx_init(&usbq_rx_mtx, "usbq_rx_mtx", MTX_DEF); + mtx_inited++; return; } /* * Must be called at splusp() (actually splbio()). This should be * the case when called from a transfer callback routine. */ void usb_ether_input(m) struct mbuf *m; { - int s; - s = splimp(); + mtx_enter(&usbq_rx_mtx, MTX_DEF); IF_ENQUEUE(&usbq_rx, m); + mtx_exit(&usbq_rx_mtx, MTX_DEF); schednetisr(NETISR_USB); - splx(s); + return; } void usb_tx_done(m) struct mbuf *m; { - int s; - s = splimp(); + mtx_enter(&usbq_tx_mtx, MTX_DEF); IF_ENQUEUE(&usbq_tx, m); + mtx_exit(&usbq_tx_mtx, MTX_DEF); schednetisr(NETISR_USB); - splx(s); + return; }