Index: files.usb =================================================================== RCS file: /cvsroot/src/sys/dev/usb/files.usb,v retrieving revision 1.164 diff -p -u -r1.164 files.usb --- files.usb 15 Aug 2019 08:02:32 -0000 1.164 +++ files.usb 15 Aug 2019 08:03:39 -0000 @@ -334,7 +334,7 @@ file dev/usb/uthum.c uthum # Ethernet adapters # ADMtek AN986 Pegasus -device aue: arp, ether, ifnet, mii, mii_phy +device aue: arp, ether, ifnet, mii, mii_phy, usbnet attach aue at usbdevif file dev/usb/if_aue.c aue @@ -349,12 +349,12 @@ attach cue at usbdevif file dev/usb/if_cue.c cue # Kawasaki LSI KL5KUSB101B -device kue: arp, ether, ifnet +device kue: arp, ether, ifnet, usbnet attach kue at usbdevif file dev/usb/if_kue.c kue # Prolific PL2302 host-host -device upl: ifnet +device upl: ifnet, usbnet attach upl at usbdevif file dev/usb/if_upl.c upl Index: if_aue.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_aue.c,v retrieving revision 1.156 diff -p -u -r1.156 if_aue.c --- if_aue.c 7 Aug 2019 06:31:03 -0000 1.156 +++ if_aue.c 15 Aug 2019 08:03:39 -0000 @@ -85,52 +85,39 @@ __KERNEL_RCSID(0, "$NetBSD: if_aue.c,v 1 #endif #include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include +#include +#include -#include #ifdef INET #include #include #endif - - -#include -#include - -#include -#include -#include -#include - -#include -#include - -#include - #ifdef AUE_DEBUG #define DPRINTF(x) if (auedebug) printf x #define DPRINTFN(n, x) if (auedebug >= (n)) printf x -int auedebug = 0; +int auedebug = 5; #else #define DPRINTF(x) #define DPRINTFN(n, x) #endif +#define AUE_TX_LIST_CNT 1 +#define AUE_RX_LIST_CNT 1 + +struct aue_softc { + struct usbnet aue_un; + struct usbnet_intr aue_intr; + struct aue_intrpkt aue_ibuf; +}; + +#define AUE_TIMEOUT 1000 +#define AUE_BUFSZ 1536 +#define AUE_MIN_FRAMELEN 60 +#define AUE_TX_TIMEOUT 10000 /* ms */ +#define AUE_INTR_INTERVAL 100 /* ms */ + /* * Various supported device vendors/products. */ @@ -142,7 +129,7 @@ struct aue_type { #define PII 0x0004 /* Pegasus II chip */ }; -Static const struct aue_type aue_devs[] = { +static const struct aue_type aue_devs[] = { {{ USB_VENDOR_3COM, USB_PRODUCT_3COM_3C460B}, PII }, {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX1}, PNA | PII }, {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX2}, PII }, @@ -210,49 +197,42 @@ Static const struct aue_type aue_devs[] int aue_match(device_t, cfdata_t, void *); void aue_attach(device_t, device_t, void *); -int aue_detach(device_t, int); -int aue_activate(device_t, enum devact); CFATTACH_DECL_NEW(aue, sizeof(struct aue_softc), aue_match, aue_attach, - aue_detach, aue_activate); + usbnet_detach, usbnet_activate); + +static void aue_reset_pegasus_II(struct aue_softc *); + +static void aue_stop_cb(struct ifnet *, int); +static int aue_ioctl_cb(struct ifnet *, u_long, void *); +static usbd_status aue_mii_read_reg(struct usbnet *, int, int, uint16_t *); +static usbd_status aue_mii_write_reg(struct usbnet *, int, int, uint16_t); +static void aue_mii_statchg(struct ifnet *); +static unsigned aue_tx_prepare(struct usbnet *, struct mbuf *, + struct usbnet_chain *); +static void aue_rx_loop(struct usbnet *, struct usbnet_chain *, uint32_t); +static int aue_init(struct ifnet *); +static void aue_intr(struct usbnet *, usbd_status); + +static struct usbnet_ops aue_ops = { + .uno_stop = aue_stop_cb, + .uno_ioctl = aue_ioctl_cb, + .uno_read_reg = aue_mii_read_reg, + .uno_write_reg = aue_mii_write_reg, + .uno_statchg = aue_mii_statchg, + .uno_tx_prepare = aue_tx_prepare, + .uno_rx_loop = aue_rx_loop, + .uno_init = aue_init, + .uno_intr = aue_intr, +}; -Static void aue_multithread(void *); +static uint32_t aue_crc(void *); +static void aue_reset(struct aue_softc *); -Static void aue_reset_pegasus_II(struct aue_softc *); -Static int aue_tx_list_init(struct aue_softc *); -Static int aue_rx_list_init(struct aue_softc *); -Static int aue_newbuf(struct aue_softc *, struct aue_chain *, struct mbuf *); -Static int aue_send(struct aue_softc *, struct mbuf *, int); -Static void aue_intr(struct usbd_xfer *, void *, usbd_status); -Static void aue_rxeof(struct usbd_xfer *, void *, usbd_status); -Static void aue_txeof(struct usbd_xfer *, void *, usbd_status); -Static void aue_tick(void *); -Static void aue_tick_task(void *); -Static void aue_start(struct ifnet *); -Static int aue_ioctl(struct ifnet *, u_long, void *); -Static void aue_init(void *); -Static void aue_stop(struct aue_softc *); -Static void aue_watchdog(struct ifnet *); -Static int aue_openpipes(struct aue_softc *); -Static int aue_ifmedia_upd(struct ifnet *); - -Static int aue_eeprom_getword(struct aue_softc *, int); -Static void aue_read_mac(struct aue_softc *, u_char *); -Static int aue_miibus_readreg(device_t, int, int, uint16_t *); -Static int aue_miibus_writereg(device_t, int, int, uint16_t); -Static void aue_miibus_statchg(struct ifnet *); - -Static void aue_lock_mii(struct aue_softc *); -Static void aue_unlock_mii(struct aue_softc *); - -Static void aue_setmulti(struct aue_softc *); -Static uint32_t aue_crc(void *); -Static void aue_reset(struct aue_softc *); - -Static int aue_csr_read_1(struct aue_softc *, int); -Static int aue_csr_write_1(struct aue_softc *, int, int); -Static int aue_csr_read_2(struct aue_softc *, int); -Static int aue_csr_write_2(struct aue_softc *, int, int); +static int aue_csr_read_1(struct aue_softc *, int); +static int aue_csr_write_1(struct aue_softc *, int, int); +static int aue_csr_read_2(struct aue_softc *, int); +static int aue_csr_write_2(struct aue_softc *, int, int); #define AUE_SETBIT(sc, reg, x) \ aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) | (x)) @@ -260,14 +240,17 @@ Static int aue_csr_write_2(struct aue_so #define AUE_CLRBIT(sc, reg, x) \ aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) & ~(x)) -Static int +static int aue_csr_read_1(struct aue_softc *sc, int reg) { + struct usbnet * const un = &sc->aue_un; usb_device_request_t req; usbd_status err; uByte val = 0; - if (sc->aue_dying) + usbnet_isowned_mii(un); + + if (usbnet_isdying(un)) return 0; req.bmRequestType = UT_READ_VENDOR_DEVICE; @@ -276,25 +259,28 @@ aue_csr_read_1(struct aue_softc *sc, int USETW(req.wIndex, reg); USETW(req.wLength, 1); - err = usbd_do_request(sc->aue_udev, &req, &val); + err = usbd_do_request(un->un_udev, &req, &val); if (err) { DPRINTF(("%s: aue_csr_read_1: reg=0x%x err=%s\n", - device_xname(sc->aue_dev), reg, usbd_errstr(err))); + device_xname(un->un_dev), reg, usbd_errstr(err))); return 0; } return val; } -Static int +static int aue_csr_read_2(struct aue_softc *sc, int reg) { + struct usbnet * const un = &sc->aue_un; usb_device_request_t req; usbd_status err; uWord val; - if (sc->aue_dying) + usbnet_isowned_mii(un); + + if (usbnet_isdying(un)) return 0; req.bmRequestType = UT_READ_VENDOR_DEVICE; @@ -303,25 +289,28 @@ aue_csr_read_2(struct aue_softc *sc, int USETW(req.wIndex, reg); USETW(req.wLength, 2); - err = usbd_do_request(sc->aue_udev, &req, &val); + err = usbd_do_request(un->un_udev, &req, &val); if (err) { DPRINTF(("%s: aue_csr_read_2: reg=0x%x err=%s\n", - device_xname(sc->aue_dev), reg, usbd_errstr(err))); + device_xname(un->un_dev), reg, usbd_errstr(err))); return 0; } return UGETW(val); } -Static int +static int aue_csr_write_1(struct aue_softc *sc, int reg, int aval) { + struct usbnet * const un = &sc->aue_un; usb_device_request_t req; usbd_status err; uByte val; - if (sc->aue_dying) + usbnet_isowned_mii(un); + + if (usbnet_isdying(un)) return 0; val = aval; @@ -331,25 +320,28 @@ aue_csr_write_1(struct aue_softc *sc, in USETW(req.wIndex, reg); USETW(req.wLength, 1); - err = usbd_do_request(sc->aue_udev, &req, &val); + err = usbd_do_request(un->un_udev, &req, &val); if (err) { DPRINTF(("%s: aue_csr_write_1: reg=0x%x err=%s\n", - device_xname(sc->aue_dev), reg, usbd_errstr(err))); + device_xname(un->un_dev), reg, usbd_errstr(err))); return -1; } return 0; } -Static int +static int aue_csr_write_2(struct aue_softc *sc, int reg, int aval) { + struct usbnet * const un = &sc->aue_un; usb_device_request_t req; usbd_status err; uWord val; - if (sc->aue_dying) + usbnet_isowned_mii(un); + + if (usbnet_isdying(un)) return 0; USETW(val, aval); @@ -359,11 +351,11 @@ aue_csr_write_2(struct aue_softc *sc, in USETW(req.wIndex, reg); USETW(req.wLength, 2); - err = usbd_do_request(sc->aue_udev, &req, &val); + err = usbd_do_request(un->un_udev, &req, &val); if (err) { DPRINTF(("%s: aue_csr_write_2: reg=0x%x err=%s\n", - device_xname(sc->aue_dev), reg, usbd_errstr(err))); + device_xname(un->un_dev), reg, usbd_errstr(err))); return -1; } @@ -373,10 +365,11 @@ aue_csr_write_2(struct aue_softc *sc, in /* * Read a word of data stored in the EEPROM at address 'addr.' */ -Static int +static int aue_eeprom_getword(struct aue_softc *sc, int addr) { - int i; + struct usbnet * const un = &sc->aue_un; + int i; aue_csr_write_1(sc, AUE_EE_REG, addr); aue_csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ); @@ -388,7 +381,7 @@ aue_eeprom_getword(struct aue_softc *sc, if (i == AUE_TIMEOUT) { printf("%s: EEPROM read timed out\n", - device_xname(sc->aue_dev)); + device_xname(un->un_dev)); } return aue_csr_read_2(sc, AUE_EE_DATA); @@ -397,14 +390,17 @@ aue_eeprom_getword(struct aue_softc *sc, /* * Read the MAC from the EEPROM. It's at offset 0. */ -Static void -aue_read_mac(struct aue_softc *sc, u_char *dest) +static void +aue_read_mac(struct usbnet *un, u_char *dest) { + struct aue_softc *sc = usbnet_softc(un); int i; int off = 0; int word; - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__)); + usbnet_isowned_mii(un); + + DPRINTFN(5,("%s: %s: enter\n", device_xname(un->un_dev), __func__)); for (i = 0; i < 3; i++) { word = aue_eeprom_getword(sc, off + i); @@ -413,34 +409,13 @@ aue_read_mac(struct aue_softc *sc, u_cha } } -/* Get exclusive access to the MII registers */ -Static void -aue_lock_mii(struct aue_softc *sc) +static usbd_status +aue_mii_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val) { - sc->aue_refcnt++; - mutex_enter(&sc->aue_mii_lock); -} - -Static void -aue_unlock_mii(struct aue_softc *sc) -{ - mutex_exit(&sc->aue_mii_lock); - if (--sc->aue_refcnt < 0) - usb_detach_wakeupold(sc->aue_dev); -} - -Static int -aue_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val) -{ - struct aue_softc *sc = device_private(dev); - int i, rv = 0; + struct aue_softc *sc = usbnet_softc(un); + int i; - if (sc->aue_dying) { -#ifdef DIAGNOSTIC - printf("%s: dying\n", device_xname(sc->aue_dev)); -#endif - return -1; - } + usbnet_isowned_mii(un); #if 0 /* @@ -456,11 +431,10 @@ aue_miibus_readreg(device_t dev, int phy if (sc->aue_vendor == USB_VENDOR_ADMTEK && sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) { if (phy == 3) - return -1; + return USBD_INVAL; } #endif - aue_lock_mii(sc); aue_csr_write_1(sc, AUE_PHY_ADDR, phy); aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ); @@ -470,39 +444,37 @@ aue_miibus_readreg(device_t dev, int phy } if (i == AUE_TIMEOUT) { - printf("%s: MII read timed out\n", device_xname(sc->aue_dev)); - rv = ETIMEDOUT; - goto out; + printf("%s: MII read timed out\n", device_xname(un->un_dev)); + return USBD_TIMEOUT; } *val = aue_csr_read_2(sc, AUE_PHY_DATA); DPRINTFN(11,("%s: %s: phy=%d reg=%d => 0x%04hx\n", - device_xname(sc->aue_dev), __func__, phy, reg, *val)); + device_xname(un->un_dev), __func__, phy, reg, *val)); -out: - aue_unlock_mii(sc); - return rv; + return USBD_NORMAL_COMPLETION; } -Static int -aue_miibus_writereg(device_t dev, int phy, int reg, uint16_t val) +static usbd_status +aue_mii_write_reg(struct usbnet *un, int phy, int reg, uint16_t val) { - struct aue_softc *sc = device_private(dev); - int i, rv = 0; + struct aue_softc *sc = usbnet_softc(un); + int i; + + usbnet_isowned_mii(un); #if 0 if (sc->aue_vendor == USB_VENDOR_ADMTEK && sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) { if (phy == 3) - return -1; + return USBD_INVAL; } #endif DPRINTFN(11,("%s: %s: phy=%d reg=%d data=0x%04hx\n", - device_xname(sc->aue_dev), __func__, phy, reg, val)); + device_xname(un->un_dev), __func__, phy, reg, val)); - aue_lock_mii(sc); aue_csr_write_2(sc, AUE_PHY_DATA, val); aue_csr_write_1(sc, AUE_PHY_ADDR, phy); aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE); @@ -513,23 +485,25 @@ aue_miibus_writereg(device_t dev, int ph } if (i == AUE_TIMEOUT) { - printf("%s: MII read timed out\n", device_xname(sc->aue_dev)); - rv = ETIMEDOUT; + printf("%s: MII read timed out\n", device_xname(un->un_dev)); + return USBD_TIMEOUT; } - aue_unlock_mii(sc); - return rv; + return USBD_NORMAL_COMPLETION; } -Static void -aue_miibus_statchg(struct ifnet *ifp) +static void +aue_mii_statchg(struct ifnet *ifp) { - struct aue_softc *sc = ifp->if_softc; - struct mii_data *mii = GET_MII(sc); + struct usbnet *un = ifp->if_softc; + struct aue_softc *sc = usbnet_softc(un); + struct mii_data *mii = usbnet_mii(un); + const bool hadlink __diagused = usbnet_havelink(un); - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__)); + DPRINTFN(5,("%s: %s: enter link %d status %x active %x\n", device_xname(un->un_dev), + __func__, hadlink, mii->mii_media_status, mii->mii_media_active)); - aue_lock_mii(sc); + usbnet_lock_mii(un); AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB); if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) { @@ -544,25 +518,38 @@ aue_miibus_statchg(struct ifnet *ifp) AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX); AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB); - aue_unlock_mii(sc); + + if (!usbnet_havelink(un)) { + //usbnet_unlock_mii(un); + //mii_pollstat(mii); /* XXX FreeBSD has removed this call */ + if (mii->mii_media_status & IFM_ACTIVE && + IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { + usbnet_set_link(un, true); + } + //usbnet_lock_mii(un); + } /* * 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_dying && (sc->aue_flags & LSYS)) { + if (!usbnet_isdying(un) && (un->un_flags & LSYS)) { uint16_t auxmode; - aue_miibus_readreg(sc->aue_dev, 0, 0x1b, &auxmode); - aue_miibus_writereg(sc->aue_dev, 0, 0x1b, auxmode | 0x04); + aue_mii_read_reg(un, 0, 0x1b, &auxmode); + aue_mii_write_reg(un, 0, 0x1b, auxmode | 0x04); + } + usbnet_unlock_mii(un); + if (usbnet_havelink(un) != hadlink) { + DPRINTFN(5,("%s: %s: exit link %d\n", device_xname(un->un_dev), + __func__, usbnet_havelink(un))); } - DPRINTFN(5,("%s: %s: exit\n", device_xname(sc->aue_dev), __func__)); } #define AUE_POLY 0xEDB88320 #define AUE_BITS 6 -Static uint32_t +static uint32_t aue_crc(void *addrv) { uint32_t idx, bit, data, crc; @@ -579,19 +566,20 @@ aue_crc(void *addrv) return crc & ((1 << AUE_BITS) - 1); } -Static void -aue_setmulti(struct aue_softc *sc) +static void +aue_setiff_locked(struct usbnet *un) { - struct ethercom *ec = &sc->aue_ec; - struct ifnet *ifp; + struct aue_softc * const sc = usbnet_softc(un); + struct ifnet * const ifp = usbnet_ifp(un); + struct ethercom * ec = usbnet_ec(un); struct ether_multi *enm; struct ether_multistep step; uint32_t h = 0, i; uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__)); + DPRINTFN(5,("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - ifp = GET_IFP(sc); + usbnet_isowned_mii(un); if (ifp->if_flags & IFF_PROMISC) { allmulti: @@ -625,26 +613,35 @@ allmulti: ifp->if_flags &= ~IFF_ALLMULTI; } -Static void +static void +aue_setiff(struct usbnet *un) +{ + usbnet_lock_mii(un); + aue_setiff_locked(un); + usbnet_unlock_mii(un); +} + +static void aue_reset_pegasus_II(struct aue_softc *sc) { /* Magic constants taken from Linux driver. */ aue_csr_write_1(sc, AUE_REG_1D, 0); aue_csr_write_1(sc, AUE_REG_7B, 2); #if 0 - if ((sc->aue_flags & HAS_HOME_PNA) && mii_mode) + if ((un->un_flags & PNA) && mii_mode) aue_csr_write_1(sc, AUE_REG_81, 6); else #endif aue_csr_write_1(sc, AUE_REG_81, 2); } -Static void +static void aue_reset(struct aue_softc *sc) { + struct usbnet * const un = &sc->aue_un; int i; - DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__)); + DPRINTFN(2,("%s: %s: enter\n", device_xname(un->un_dev), __func__)); AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC); @@ -654,11 +651,11 @@ aue_reset(struct aue_softc *sc) } if (i == AUE_TIMEOUT) - printf("%s: reset failed\n", device_xname(sc->aue_dev)); + printf("%s: reset failed\n", device_xname(un->un_dev)); #if 0 /* XXX what is mii_mode supposed to be */ - if (sc->aue_mii_mode && (sc->aue_flags & PNA)) + if (sc->sc_mii_mode && (un->un_flags & PNA)) aue_csr_write_1(sc, AUE_GPIO1, 0x34); else aue_csr_write_1(sc, AUE_GPIO1, 0x26); @@ -673,7 +670,7 @@ aue_reset(struct aue_softc *sc) * Note: We force all of the GPIO pins low first, *then* * enable the ones we want. */ - if (sc->aue_flags & LSYS) { + if (un->un_flags & LSYS) { /* Grrr. LinkSys has to be different from everyone else. */ aue_csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0 | AUE_GPIO_SEL1); @@ -684,11 +681,14 @@ aue_reset(struct aue_softc *sc) aue_csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0 | AUE_GPIO_SEL0 | AUE_GPIO_SEL1); - if (sc->aue_flags & PII) + if (un->un_flags & PII) aue_reset_pegasus_II(sc); /* Wait a little while for the chip to get its brains in order. */ - delay(10000); /* XXX */ + delay(10000); /* XXX */ + //usbd_delay_ms(un->un_udev, 10); /* XXX */ + + DPRINTFN(2,("%s: %s: exit\n", device_xname(un->un_dev), __func__)); } /* @@ -730,31 +730,42 @@ aue_match(device_t parent, cfdata_t matc void aue_attach(device_t parent, device_t self, void *aux) { - struct aue_softc *sc = device_private(self); + struct aue_softc * const sc = device_private(self); + struct usbnet * const un = &sc->aue_un; struct usb_attach_arg *uaa = aux; char *devinfop; - int s; - u_char eaddr[ETHER_ADDR_LEN]; - struct ifnet *ifp; - struct mii_data *mii; struct usbd_device *dev = uaa->uaa_device; - struct usbd_interface *iface; usbd_status err; usb_interface_descriptor_t *id; usb_endpoint_descriptor_t *ed; int i; - DPRINTFN(5,(" : aue_attach: sc=%p", sc)); + KASSERT((void *)sc == un); - sc->aue_dev = self; + DPRINTFN(5,(" : aue_attach: sc=%p", sc)); aprint_naive("\n"); aprint_normal("\n"); - devinfop = usbd_devinfo_alloc(uaa->uaa_device, 0); aprint_normal_dev(self, "%s\n", devinfop); usbd_devinfo_free(devinfop); + un->un_dev = self; + un->un_udev = dev; + un->un_sc = sc; + un->un_ops = &aue_ops; + un->un_intr = &sc->aue_intr; + un->un_rx_xfer_flags = USBD_SHORT_XFER_OK; + un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER; + un->un_rx_list_cnt = AUE_RX_LIST_CNT; + un->un_tx_list_cnt = AUE_RX_LIST_CNT; + un->un_rx_bufsz = AUE_BUFSZ; + un->un_tx_bufsz = AUE_BUFSZ; + + sc->aue_intr.uni_buf = &sc->aue_ibuf; + sc->aue_intr.uni_bufsz = sizeof(sc->aue_ibuf); + sc->aue_intr.uni_interval = AUE_INTR_INTERVAL; + err = usbd_set_config_no(dev, AUE_CONFIG_NO, 1); if (err) { aprint_error_dev(self, "failed to set configuration" @@ -762,43 +773,19 @@ aue_attach(device_t parent, device_t sel return; } - usb_init_task(&sc->aue_tick_task, aue_tick_task, sc, 0); - usb_init_task(&sc->aue_stop_task, (void (*)(void *))aue_stop, sc, 0); - mutex_init(&sc->aue_mii_lock, MUTEX_DEFAULT, IPL_NONE); - - err = usbd_device2interface_handle(dev, AUE_IFACE_IDX, &iface); + err = usbd_device2interface_handle(dev, AUE_IFACE_IDX, &un->un_iface); if (err) { aprint_error_dev(self, "getting interface handle failed\n"); return; } - sc->aue_closing = 0; - mutex_init(&sc->aue_mcmtx, MUTEX_DRIVER, IPL_NET); - cv_init(&sc->aue_domc, "auemc"); - cv_init(&sc->aue_closemc, "auemccl"); - - err = kthread_create(PRI_NONE, 0, NULL, - aue_multithread, sc, &sc->aue_thread, - "%s-mc", device_xname(sc->aue_dev)); + un->un_flags = aue_lookup(uaa->uaa_vendor, uaa->uaa_product)->aue_flags; - if (err) { - aprint_error_dev(self, - "creating multicast configuration thread\n"); - return; - } - sc->aue_flags = aue_lookup(uaa->uaa_vendor, - uaa->uaa_product)->aue_flags; - - sc->aue_udev = dev; - sc->aue_iface = iface; - sc->aue_product = uaa->uaa_product; - sc->aue_vendor = uaa->uaa_vendor; - - id = usbd_get_interface_descriptor(iface); + id = usbd_get_interface_descriptor(un->un_iface); /* Find endpoints. */ for (i = 0; i < id->bNumEndpoints; i++) { - ed = usbd_interface2endpoint_descriptor(iface, i); + ed = usbd_interface2endpoint_descriptor(un->un_iface, i); if (ed == NULL) { aprint_error_dev(self, "couldn't get endpoint descriptor %d\n", i); @@ -806,24 +793,27 @@ aue_attach(device_t parent, device_t sel } if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { - sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress; } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { - sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress; } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { - sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_INTR] = ed->bEndpointAddress; } } - if (sc->aue_ed[AUE_ENDPT_RX] == 0 || sc->aue_ed[AUE_ENDPT_TX] == 0 || - sc->aue_ed[AUE_ENDPT_INTR] == 0) { + if (un->un_ed[USBNET_ENDPT_RX] == 0 || + un->un_ed[USBNET_ENDPT_TX] == 0 || + un->un_ed[USBNET_ENDPT_INTR] == 0) { aprint_error_dev(self, "missing endpoint\n"); return; } + /* First level attach. */ + usbnet_attach(un, "auedet"); - s = splnet(); + usbnet_lock_mii(un); /* Reset the adapter. */ aue_reset(sc); @@ -831,277 +821,23 @@ aue_attach(device_t parent, device_t sel /* * Get station address from the EEPROM. */ - aue_read_mac(sc, eaddr); - - /* - * A Pegasus chip was detected. Inform the world. - */ - ifp = GET_IFP(sc); - aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr)); - - /* Initialize interface info.*/ - ifp->if_softc = sc; - ifp->if_mtu = ETHERMTU; - ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; - ifp->if_ioctl = aue_ioctl; - ifp->if_start = aue_start; - ifp->if_watchdog = aue_watchdog; - strlcpy(ifp->if_xname, device_xname(sc->aue_dev), IFNAMSIZ); - - IFQ_SET_READY(&ifp->if_snd); - - /* Initialize MII/media info. */ - mii = &sc->aue_mii; - mii->mii_ifp = ifp; - mii->mii_readreg = aue_miibus_readreg; - mii->mii_writereg = aue_miibus_writereg; - mii->mii_statchg = aue_miibus_statchg; - mii->mii_flags = MIIF_AUTOTSLEEP; - sc->aue_ec.ec_mii = mii; - ifmedia_init(&mii->mii_media, 0, aue_ifmedia_upd, ether_mediastatus); - mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0); - if (LIST_FIRST(&mii->mii_phys) == NULL) { - ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL); - ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE); - } else - ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO); - - /* Attach the interface. */ - if_attach(ifp); - ether_ifattach(ifp, eaddr); - rnd_attach_source(&sc->rnd_source, device_xname(sc->aue_dev), - RND_TYPE_NET, RND_FLAG_DEFAULT); - - callout_init(&(sc->aue_stat_ch), 0); - - sc->aue_attached = 1; - splx(s); - - usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->aue_udev, sc->aue_dev); - - if (!pmf_device_register(self, NULL, NULL)) - aprint_error_dev(self, "couldn't establish power handler\n"); - - return; -} - -int -aue_detach(device_t self, int flags) -{ - struct aue_softc *sc = device_private(self); - struct ifnet *ifp = GET_IFP(sc); - int s; - - DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__)); - - if (!sc->aue_attached) { - /* Detached before attached finished, so just bail out. */ - return 0; - } - - pmf_device_deregister(self); - - /* - * XXX Halting callout guarantees no more tick tasks. What - * guarantees no more stop tasks? What guarantees no more - * calls to aue_send? Don't we need to wait for if_detach or - * something? Should we set sc->aue_dying here? Is device - * deactivation guaranteed to have already happened? - */ - callout_halt(&sc->aue_stat_ch, NULL); - usb_rem_task_wait(sc->aue_udev, &sc->aue_tick_task, USB_TASKQ_DRIVER, - NULL); - usb_rem_task_wait(sc->aue_udev, &sc->aue_stop_task, USB_TASKQ_DRIVER, - NULL); - - sc->aue_closing = 1; - cv_signal(&sc->aue_domc); - - mutex_enter(&sc->aue_mcmtx); - cv_wait(&sc->aue_closemc,&sc->aue_mcmtx); - mutex_exit(&sc->aue_mcmtx); - - mutex_destroy(&sc->aue_mcmtx); - cv_destroy(&sc->aue_domc); - cv_destroy(&sc->aue_closemc); - - s = splusb(); - - if (ifp->if_flags & IFF_RUNNING) - aue_stop(sc); - - rnd_detach_source(&sc->rnd_source); - mii_detach(&sc->aue_mii, MII_PHY_ANY, MII_OFFSET_ANY); - ifmedia_delete_instance(&sc->aue_mii.mii_media, IFM_INST_ANY); - ether_ifdetach(ifp); - - if_detach(ifp); - -#ifdef DIAGNOSTIC - if (sc->aue_ep[AUE_ENDPT_TX] != NULL || - sc->aue_ep[AUE_ENDPT_RX] != NULL || - sc->aue_ep[AUE_ENDPT_INTR] != NULL) - aprint_error_dev(self, "detach has active endpoints\n"); -#endif - - sc->aue_attached = 0; - - if (--sc->aue_refcnt >= 0) { - /* Wait for processes to go away. */ - usb_detach_waitold(sc->aue_dev); - } - splx(s); - - usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->aue_udev, sc->aue_dev); - - mutex_destroy(&sc->aue_mii_lock); -#if 0 - mutex_destroy(&sc->wkmtx); -#endif - return 0; -} + aue_read_mac(un, un->un_eaddr); -int -aue_activate(device_t self, enum devact act) -{ - struct aue_softc *sc = device_private(self); - - DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__)); - - switch (act) { - case DVACT_DEACTIVATE: - if_deactivate(&sc->aue_ec.ec_if); - sc->aue_dying = 1; - return 0; - default: - return EOPNOTSUPP; - } -} - -/* - * Initialize an RX descriptor and attach an MBUF cluster. - */ -Static int -aue_newbuf(struct aue_softc *sc, struct aue_chain *c, struct mbuf *m) -{ - struct mbuf *m_new = NULL; - - DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__)); - - if (m == NULL) { - MGETHDR(m_new, M_DONTWAIT, MT_DATA); - if (m_new == NULL) { - aprint_error_dev(sc->aue_dev, "no memory for rx list " - "-- packet dropped!\n"); - return ENOBUFS; - } + usbnet_unlock_mii(un); - MCLGET(m_new, M_DONTWAIT); - if (!(m_new->m_flags & M_EXT)) { - aprint_error_dev(sc->aue_dev, "no memory for rx " - "list -- packet dropped!\n"); - 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; + usbnet_attach_ifp(un, true, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST, + 0, 0); } -Static int -aue_rx_list_init(struct aue_softc *sc) +static void +aue_intr(struct usbnet *un, usbd_status status) { - struct aue_cdata *cd; - struct aue_chain *c; - int i; + struct ifnet *ifp = usbnet_ifp(un); + struct aue_softc *sc = usbnet_softc(un); + struct aue_intrpkt *p = &sc->aue_ibuf; - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__)); - - cd = &sc->aue_cdata; - for (i = 0; i < AUE_RX_LIST_CNT; i++) { - c = &cd->aue_rx_chain[i]; - c->aue_sc = sc; - if (aue_newbuf(sc, c, NULL) == ENOBUFS) - return ENOBUFS; - if (c->aue_xfer == NULL) { - int err = usbd_create_xfer(sc->aue_ep[AUE_ENDPT_RX], - AUE_BUFSZ, 0, 0, &c->aue_xfer); - if (err) { - return err; - } - c->aue_buf = usbd_get_buffer(c->aue_xfer); - } - } - - return 0; -} - -Static int -aue_tx_list_init(struct aue_softc *sc) -{ - struct aue_cdata *cd; - struct aue_chain *c; - int i; - - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__)); - - 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_mbuf = NULL; - if (c->aue_xfer == NULL) { - int err = usbd_create_xfer(sc->aue_ep[AUE_ENDPT_TX], - AUE_BUFSZ, USBD_FORCE_SHORT_XFER, 0, &c->aue_xfer); - if (err) { - return err; - } - c->aue_buf = usbd_get_buffer(c->aue_xfer); - } - } - - return 0; -} - -Static void -aue_intr(struct usbd_xfer *xfer, void *priv, - usbd_status status) -{ - struct aue_softc *sc = priv; - struct ifnet *ifp = GET_IFP(sc); - struct aue_intrpkt *p = &sc->aue_cdata.aue_ibuf; - - DPRINTFN(15,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__)); - - if (sc->aue_dying) - return; - - if (!(ifp->if_flags & IFF_RUNNING)) - return; - - if (status != USBD_NORMAL_COMPLETION) { - if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { - return; - } - sc->aue_intr_errs++; - if (usbd_ratecheck(&sc->aue_rx_notice)) { - aprint_debug_dev(sc->aue_dev, - "%u usb errors on intr: %s\n", sc->aue_intr_errs, - usbd_errstr(status)); - sc->aue_intr_errs = 0; - } - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_RX]); - return; - } + DPRINTFN(20,("%s: %s: enter txstat0 %x\n", + device_xname(un->un_dev), __func__, p->aue_txstat0)); if (p->aue_txstat0) ifp->if_oerrors++; @@ -1110,216 +846,58 @@ aue_intr(struct usbd_xfer *xfer, void *p ifp->if_collisions++; } -/* - * A frame has been uploaded: pass the resulting mbuf chain up to - * the higher level protocols. - */ -Static void -aue_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status) +static void +aue_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len) { - struct aue_chain *c = priv; - struct aue_softc *sc = c->aue_sc; - struct ifnet *ifp = GET_IFP(sc); - struct mbuf *m; - uint32_t total_len; + struct ifnet *ifp = usbnet_ifp(un); + uint8_t *buf = c->unc_buf; struct aue_rxpkt r; - int s; + uint32_t pktlen; - DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__)); + DPRINTFN(5,("%s: %s: enter len %u\n", device_xname(un->un_dev), + __func__, total_len)); - if (sc->aue_dying) - return; - - if (!(ifp->if_flags & IFF_RUNNING)) - return; - - if (status != USBD_NORMAL_COMPLETION) { - if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) - return; - sc->aue_rx_errs++; - if (usbd_ratecheck(&sc->aue_rx_notice)) { - aprint_error_dev(sc->aue_dev, - "%u usb errors on rx: %s\n", sc->aue_rx_errs, - usbd_errstr(status)); - sc->aue_rx_errs = 0; - } - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_RX]); - goto done; - } - - usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); - - memcpy(mtod(c->aue_mbuf, char *), c->aue_buf, total_len); + usbnet_isowned_rx(un); if (total_len <= 4 + ETHER_CRC_LEN) { ifp->if_ierrors++; - goto done; + return; } - memcpy(&r, c->aue_buf + total_len - 4, sizeof(r)); + memcpy(&r, buf + total_len - 4, 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. */ - m = c->aue_mbuf; - total_len -= ETHER_CRC_LEN + 4; - m->m_pkthdr.len = m->m_len = total_len; - - m_set_rcvif(m, ifp); - - s = splnet(); - - /* XXX ugly */ - if (aue_newbuf(sc, c, NULL) == ENOBUFS) { - ifp->if_ierrors++; - goto done1; - } - - DPRINTFN(10,("%s: %s: deliver %d\n", device_xname(sc->aue_dev), - __func__, m->m_len)); - if_percpuq_enqueue(ifp->if_percpuq, m); - done1: - splx(s); - - done: - - /* Setup new transfer. */ - usbd_setup_xfer(xfer, c, c->aue_buf, AUE_BUFSZ, - USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, aue_rxeof); - usbd_transfer(xfer); - - DPRINTFN(10,("%s: %s: start rx\n", device_xname(sc->aue_dev), - __func__)); -} - -/* - * A frame was downloaded to the chip. It's safe for us to clean up - * the list buffers. - */ - -Static void -aue_txeof(struct usbd_xfer *xfer, void *priv, - usbd_status status) -{ - struct aue_chain *c = priv; - struct aue_softc *sc = c->aue_sc; - struct ifnet *ifp = GET_IFP(sc); - int s; - - if (sc->aue_dying) - return; - - s = splnet(); - - DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->aue_dev), - __func__, status)); - - ifp->if_timer = 0; - ifp->if_flags &= ~IFF_OACTIVE; - - if (status != USBD_NORMAL_COMPLETION) { - if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { - splx(s); - return; - } - ifp->if_oerrors++; - aprint_error_dev(sc->aue_dev, "usb error on tx: %s\n", - usbd_errstr(status)); - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_TX]); - splx(s); return; } - ifp->if_opackets++; - - m_freem(c->aue_mbuf); - c->aue_mbuf = NULL; - - if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) - aue_start(ifp); - - splx(s); -} - -Static void -aue_tick(void *xsc) -{ - struct aue_softc *sc = xsc; - - DPRINTFN(15,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__)); - - if (sc == NULL) - return; - - if (sc->aue_dying) - return; - - /* Perform periodic stuff in process context. */ - usb_add_task(sc->aue_udev, &sc->aue_tick_task, USB_TASKQ_DRIVER); -} - -Static void -aue_tick_task(void *xsc) -{ - struct aue_softc *sc = xsc; - struct ifnet *ifp; - struct mii_data *mii; - int s; - - DPRINTFN(15,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__)); - - if (sc->aue_dying) - return; - - ifp = GET_IFP(sc); - mii = GET_MII(sc); - if (mii == NULL) - return; - - s = splnet(); - - mii_tick(mii); - if (!sc->aue_link) { - mii_pollstat(mii); /* XXX FreeBSD has removed this call */ - if (mii->mii_media_status & IFM_ACTIVE && - IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { - DPRINTFN(2,("%s: %s: got link\n", - device_xname(sc->aue_dev), __func__)); - sc->aue_link++; - if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) - aue_start(ifp); - } - } - - callout_reset(&(sc->aue_stat_ch), (hz), (aue_tick), (sc)); + /* No errors; receive the packet. */ + pktlen = total_len - ETHER_CRC_LEN - 4; - splx(s); + usbnet_enqueue(un, buf, pktlen, 0, 0, 0); } -Static int -aue_send(struct aue_softc *sc, struct mbuf *m, int idx) +static unsigned +aue_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) { + uint8_t *buf = c->unc_buf; int total_len; - struct aue_chain *c; - usbd_status err; - DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__)); + DPRINTFN(5,("%s: %s: enter pktlen %d\n", device_xname(un->un_dev), + __func__, m->m_pkthdr.len)); - c = &sc->aue_cdata.aue_tx_chain[idx]; + usbnet_isowned_tx(un); + + if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz - 2) + return 0; /* * 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; + m_copydata(m, 0, m->m_pkthdr.len, buf + 2); /* * The ADMtek documentation says that the packet length is @@ -1327,96 +905,32 @@ aue_send(struct aue_softc *sc, struct mb * transfer, however it actually seems to ignore this info * and base the frame size on the bulk transfer length. */ - c->aue_buf[0] = (uint8_t)m->m_pkthdr.len; - c->aue_buf[1] = (uint8_t)(m->m_pkthdr.len >> 8); + buf[0] = (uint8_t)m->m_pkthdr.len; + buf[1] = (uint8_t)(m->m_pkthdr.len >> 8); total_len = m->m_pkthdr.len + 2; - usbd_setup_xfer(c->aue_xfer, c, c->aue_buf, total_len, - USBD_FORCE_SHORT_XFER, AUE_TX_TIMEOUT, aue_txeof); - - /* Transmit */ - err = usbd_transfer(c->aue_xfer); - if (err != USBD_IN_PROGRESS) { - aprint_error_dev(sc->aue_dev, "aue_send error=%s\n", - usbd_errstr(err)); - /* Stop the interface from process context. */ - usb_add_task(sc->aue_udev, &sc->aue_stop_task, - USB_TASKQ_DRIVER); - return EIO; - } - DPRINTFN(5,("%s: %s: send %d bytes\n", device_xname(sc->aue_dev), - __func__, total_len)); - - sc->aue_cdata.aue_tx_cnt++; - - return 0; + return total_len; } -Static void -aue_start(struct ifnet *ifp) +static int +aue_init_locked(struct ifnet *ifp) { - struct aue_softc *sc = ifp->if_softc; - struct mbuf *m_head = NULL; - - DPRINTFN(5,("%s: %s: enter, link=%d\n", device_xname(sc->aue_dev), - __func__, sc->aue_link)); - - if (sc->aue_dying) - return; - - if (!sc->aue_link) - return; - - if (ifp->if_flags & IFF_OACTIVE) - return; - - IFQ_POLL(&ifp->if_snd, m_head); - if (m_head == NULL) - return; - - if (aue_send(sc, m_head, 0)) { - ifp->if_flags |= IFF_OACTIVE; - return; - } - - IFQ_DEQUEUE(&ifp->if_snd, m_head); - - /* - * If there's a BPF listener, bounce a copy of this frame - * to him. - */ - bpf_mtap(ifp, m_head, BPF_D_OUT); - - ifp->if_flags |= IFF_OACTIVE; - - /* - * Set a timeout in case the chip goes out to lunch. - */ - ifp->if_timer = 5; -} - -Static void -aue_init(void *xsc) -{ - struct aue_softc *sc = xsc; - struct ifnet *ifp = GET_IFP(sc); - struct mii_data *mii = GET_MII(sc); - int i, s; + struct usbnet * const un = ifp->if_softc; + struct aue_softc *sc = usbnet_softc(un); + int i; const u_char *eaddr; - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__)); + DPRINTFN(5,("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - if (sc->aue_dying) - return; + if (usbnet_isdying(un)) + return EIO; - if (ifp->if_flags & IFF_RUNNING) - return; + /* Cancel pending I/O */ + usbnet_stop(un, ifp, 1); - s = splnet(); + usbnet_lock_mii_un_locked(un); - /* - * Cancel pending I/O and free all RX/TX buffers. - */ + /* Reset the interface. */ aue_reset(sc); eaddr = CLLADDR(ifp->if_sadl); @@ -1429,322 +943,65 @@ aue_init(void *xsc) else AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC); - if (sc->aue_ep[AUE_ENDPT_RX] == NULL) { - if (aue_openpipes(sc)) { - splx(s); - return; - } - } - /* Init TX ring. */ - if (aue_tx_list_init(sc)) { - aprint_error_dev(sc->aue_dev, "tx list init failed\n"); - splx(s); - return; - } - - /* Init RX ring. */ - if (aue_rx_list_init(sc)) { - aprint_error_dev(sc->aue_dev, "rx list init failed\n"); - splx(s); - return; - } - - /* Start up the receive pipe. */ - for (i = 0; i < AUE_RX_LIST_CNT; i++) { - struct aue_chain *c = &sc->aue_cdata.aue_rx_chain[i]; - - usbd_setup_xfer(c->aue_xfer, c, c->aue_buf, AUE_BUFSZ, - USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, aue_rxeof); - (void)usbd_transfer(c->aue_xfer); /* XXX */ - DPRINTFN(5,("%s: %s: start read\n", device_xname(sc->aue_dev), - __func__)); - - } - /* Load the multicast filter. */ - aue_setmulti(sc); - - /* Enable RX and TX */ - aue_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); - - ifp->if_flags |= IFF_RUNNING; - ifp->if_flags &= ~IFF_OACTIVE; + aue_setiff_locked(un); - splx(s); + usbnet_unlock_mii_un_locked(un); - callout_reset(&(sc->aue_stat_ch), (hz), (aue_tick), (sc)); + return usbnet_init_rx_tx(un); } -Static int -aue_openpipes(struct aue_softc *sc) +static int +aue_init(struct ifnet *ifp) { - usbd_status err; + struct usbnet * const un = ifp->if_softc; + int rv; - /* 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) { - aprint_error_dev(sc->aue_dev, "open rx pipe failed: %s\n", - usbd_errstr(err)); - return EIO; - } - err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX], - USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]); - if (err) { - aprint_error_dev(sc->aue_dev, "open tx pipe failed: %s\n", - usbd_errstr(err)); - return EIO; - } - err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR], - USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc, - &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr, - AUE_INTR_INTERVAL); - if (err) { - aprint_error_dev(sc->aue_dev, "open intr pipe failed: %s\n", - usbd_errstr(err)); - return EIO; - } + usbnet_lock(un); + rv = aue_init_locked(ifp); + usbnet_unlock(un); - return 0; -} - -/* - * Set media options. - */ -Static int -aue_ifmedia_upd(struct ifnet *ifp) -{ - struct aue_softc *sc = ifp->if_softc; - - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__)); - - if (sc->aue_dying) - return 0; - - return ether_mediachange(ifp); + return rv; } -Static int -aue_ioctl(struct ifnet *ifp, u_long command, void *data) +static int +aue_ioctl_cb(struct ifnet *ifp, u_long cmd, void *data) { - struct aue_softc *sc = ifp->if_softc; - struct ifaddr *ifa = (struct ifaddr *)data; - struct ifreq *ifr = (struct ifreq *)data; - int s, error = 0; + struct usbnet * const un = ifp->if_softc; - if (sc->aue_dying) - return EIO; - - s = splnet(); - - switch (command) { - case SIOCINITIFADDR: - ifp->if_flags |= IFF_UP; - aue_init(sc); + DPRINTFN(5,("%s: %s: enter cmd %lx data %p\n", + device_xname(un->un_dev), __func__, cmd, data)); - switch (ifa->ifa_addr->sa_family) { -#ifdef INET - case AF_INET: - arp_ifinit(ifp, ifa); - break; -#endif /* INET */ - } - break; - - case SIOCSIFMTU: - if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU) - error = EINVAL; - else if ((error = ifioctl_common(ifp, command, data)) == ENETRESET) - error = 0; - break; - - case SIOCSIFFLAGS: - if ((error = ifioctl_common(ifp, command, data)) != 0) - break; - 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; + switch (cmd) { + case SIOCADDMULTI: + case SIOCDELMULTI: + aue_init(ifp); + aue_setiff(un); break; default: - if ((error = ether_ioctl(ifp, command, data)) == ENETRESET) { - if (ifp->if_flags & IFF_RUNNING) { - cv_signal(&sc->aue_domc); - } - error = 0; - } break; } - splx(s); - - return error; -} - -Static void -aue_watchdog(struct ifnet *ifp) -{ - struct aue_softc *sc = ifp->if_softc; - struct aue_chain *c; - usbd_status stat; - int s; - - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__)); - - ifp->if_oerrors++; - aprint_error_dev(sc->aue_dev, "watchdog timeout\n"); - - s = splusb(); - 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 (IFQ_IS_EMPTY(&ifp->if_snd) == 0) - aue_start(ifp); - splx(s); + return 0; } -/* - * Stop the adapter and free any mbufs allocated to the - * RX and TX lists. - */ -Static void -aue_stop(struct aue_softc *sc) +static void +aue_stop_cb(struct ifnet *ifp, int disable) { - usbd_status err; - struct ifnet *ifp; - int i; - - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__)); + struct usbnet * const un = ifp->if_softc; + struct aue_softc * const sc = usbnet_softc(un); - ifp = GET_IFP(sc); - ifp->if_timer = 0; + DPRINTFN(5,("%s: %s: enter\n", device_xname(un->un_dev), __func__)); + usbnet_lock_mii_un_locked(un); aue_csr_write_1(sc, AUE_CTL0, 0); aue_csr_write_1(sc, AUE_CTL1, 0); aue_reset(sc); - callout_stop(&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("%s: abort rx pipe failed: %s\n", - device_xname(sc->aue_dev), usbd_errstr(err)); - } - err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]); - if (err) { - printf("%s: close rx pipe failed: %s\n", - device_xname(sc->aue_dev), 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("%s: abort tx pipe failed: %s\n", - device_xname(sc->aue_dev), usbd_errstr(err)); - } - } - - if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) { - err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]); - if (err) { - printf("%s: abort intr pipe failed: %s\n", - device_xname(sc->aue_dev), usbd_errstr(err)); - } - } - - /* Free RX resources. */ - for (i = 0; i < AUE_RX_LIST_CNT; i++) { - 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_destroy_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_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_destroy_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer); - sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL; - } - } - - /* Close pipes */ - if (sc->aue_ep[AUE_ENDPT_TX] != NULL) { - err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]); - if (err) { - printf("%s: close tx pipe failed: %s\n", - device_xname(sc->aue_dev), usbd_errstr(err)); - } - sc->aue_ep[AUE_ENDPT_TX] = NULL; - } - - if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) { - err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]); - if (err) { - printf("%s: close intr pipe failed: %s\n", - device_xname(sc->aue_dev), usbd_errstr(err)); - } - sc->aue_ep[AUE_ENDPT_INTR] = NULL; - } - - sc->aue_link = 0; - - ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); + usbnet_unlock_mii_un_locked(un); } -Static void -aue_multithread(void *arg) -{ - struct aue_softc *sc; - int s; - - sc = (struct aue_softc *)arg; - - while (1) { - mutex_enter(&sc->aue_mcmtx); - cv_wait(&sc->aue_domc,&sc->aue_mcmtx); - mutex_exit(&sc->aue_mcmtx); - - if (sc->aue_closing) - break; - - s = splnet(); - aue_init(sc); - /* XXX called by aue_init, but rc ifconfig hangs without it: */ - aue_setmulti(sc); - splx(s); - } - - cv_signal(&sc->aue_closemc); +#ifdef _MODULE +#include "ioconf.c" +#endif - kthread_exit(0); -} +USBNET_MODULE(aue) Index: if_auereg.h =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_auereg.h,v retrieving revision 1.30 diff -p -u -r1.30 if_auereg.h --- if_auereg.h 1 Aug 2019 00:10:22 -0000 1.30 +++ if_auereg.h 15 Aug 2019 08:03:39 -0000 @@ -66,10 +66,6 @@ * 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_CTL0 0x00 #define AUE_CTL1 0x01 @@ -198,73 +194,3 @@ struct aue_rxpkt { #define AUE_RXSTAT_CRCERR 0x08 #define AUE_RXSTAT_DRIBBLE 0x10 #define AUE_RXSTAT_MASK 0x1E - - -/*************** The rest belongs in if_auevar.h *************/ - -#define AUE_TX_LIST_CNT 1 -#define AUE_RX_LIST_CNT 1 - -struct aue_softc; - -struct aue_chain { - struct aue_softc *aue_sc; - struct usbd_xfer *aue_xfer; - char *aue_buf; - struct mbuf *aue_mbuf; -}; - -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_cnt; -}; - -struct aue_softc { - device_t aue_dev; - - struct ethercom aue_ec; - struct mii_data aue_mii; - krndsource_t rnd_source; - struct lwp *aue_thread; - int aue_closing; - kcondvar_t aue_domc; - kcondvar_t aue_closemc; - kmutex_t aue_mcmtx; -#define GET_IFP(sc) (&(sc)->aue_ec.ec_if) -#define GET_MII(sc) (&(sc)->aue_mii) - - struct callout aue_stat_ch; - - struct usbd_device *aue_udev; - struct usbd_interface *aue_iface; - uint16_t aue_vendor; - uint16_t aue_product; - int aue_ed[AUE_ENDPT_MAX]; - struct usbd_pipe *aue_ep[AUE_ENDPT_MAX]; - uint8_t aue_link; - int aue_if_flags; - struct aue_cdata aue_cdata; - - uint16_t aue_flags; - - int aue_refcnt; - char aue_dying; - char aue_attached; - u_int aue_rx_errs; - u_int aue_intr_errs; - struct timeval aue_rx_notice; - - struct usb_task aue_tick_task; - struct usb_task aue_stop_task; - - kmutex_t aue_mii_lock; -}; - -#define AUE_TIMEOUT 1000 -#define AUE_BUFSZ 1536 -#define AUE_MIN_FRAMELEN 60 -#define AUE_TX_TIMEOUT 10000 /* ms */ -#define AUE_INTR_INTERVAL 100 /* ms */ Index: if_kue.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_kue.c,v retrieving revision 1.97 diff -p -u -r1.97 if_kue.c --- if_kue.c 1 Aug 2019 00:10:22 -0000 1.97 +++ if_kue.c 15 Aug 2019 08:03:39 -0000 @@ -79,33 +79,15 @@ __KERNEL_RCSID(0, "$NetBSD: if_kue.c,v 1 #endif #include -#include -#include -#include #include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include + +#include #ifdef INET #include #include #endif -#include -#include -#include -#include -#include - #include #include @@ -160,37 +142,37 @@ static const struct usb_devno kue_devs[] int kue_match(device_t, cfdata_t, void *); void kue_attach(device_t, device_t, void *); int kue_detach(device_t, int); -int kue_activate(device_t, enum devact); CFATTACH_DECL_NEW(kue, sizeof(struct kue_softc), kue_match, kue_attach, - kue_detach, kue_activate); + kue_detach, usbnet_activate); -static int kue_tx_list_init(struct kue_softc *); -static int kue_rx_list_init(struct kue_softc *); -static int kue_send(struct kue_softc *, struct mbuf *, int); -static int kue_open_pipes(struct kue_softc *); -static void kue_rxeof(struct usbd_xfer *, void *, usbd_status); -static void kue_txeof(struct usbd_xfer *, void *, usbd_status); -static void kue_start(struct ifnet *); -static int kue_ioctl(struct ifnet *, u_long, void *); -static void kue_init(void *); -static void kue_stop(struct kue_softc *); -static void kue_watchdog(struct ifnet *); +static void kue_rx_loop(struct usbnet *, struct usbnet_chain *, uint32_t); +static unsigned kue_tx_prepare(struct usbnet *, struct mbuf *, + struct usbnet_chain *); +static int kue_ioctl_cb(struct ifnet *, u_long, void *); +static int kue_init(struct ifnet *); + +static struct usbnet_ops kue_ops = { + .uno_ioctl = kue_ioctl_cb, + .uno_tx_prepare = kue_tx_prepare, + .uno_rx_loop = kue_rx_loop, + .uno_init = kue_init, +}; -static void kue_setmulti(struct kue_softc *); -static void kue_reset(struct kue_softc *); +static void kue_setiff(struct usbnet *); +static void kue_reset(struct usbnet *); +static int kue_init(struct ifnet *); -static usbd_status kue_ctl(struct kue_softc *, int, uint8_t, +static usbd_status kue_ctl(struct usbnet *, int, uint8_t, uint16_t, void *, uint32_t); -static usbd_status kue_setword(struct kue_softc *, uint8_t, uint16_t); -static int kue_load_fw(struct kue_softc *); +static int kue_load_fw(struct usbnet *); static usbd_status -kue_setword(struct kue_softc *sc, uint8_t breq, uint16_t word) +kue_setword(struct usbnet *un, uint8_t breq, uint16_t word) { usb_device_request_t req; - DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__)); + DPRINTFN(10,("%s: %s: enter\n", device_xname(un->un_dev),__func__)); req.bmRequestType = UT_WRITE_VENDOR_DEVICE; req.bRequest = breq; @@ -198,16 +180,16 @@ kue_setword(struct kue_softc *sc, uint8_ USETW(req.wIndex, 0); USETW(req.wLength, 0); - return usbd_do_request(sc->kue_udev, &req, NULL); + return usbd_do_request(un->un_udev, &req, NULL); } static usbd_status -kue_ctl(struct kue_softc *sc, int rw, uint8_t breq, uint16_t val, +kue_ctl(struct usbnet *un, int rw, uint8_t breq, uint16_t val, void *data, uint32_t len) { usb_device_request_t req; - DPRINTFN(10,("%s: %s: enter, len=%d\n", device_xname(sc->kue_dev), + DPRINTFN(10,("%s: %s: enter, len=%d\n", device_xname(un->un_dev), __func__, len)); if (rw == KUE_CTL_WRITE) @@ -220,16 +202,16 @@ kue_ctl(struct kue_softc *sc, int rw, ui USETW(req.wIndex, 0); USETW(req.wLength, len); - return usbd_do_request(sc->kue_udev, &req, data); + return usbd_do_request(un->un_udev, &req, data); } static int -kue_load_fw(struct kue_softc *sc) +kue_load_fw(struct usbnet *un) { usb_device_descriptor_t dd; usbd_status err; - DPRINTFN(1,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__)); + DPRINTFN(1,("%s: %s: enter\n", device_xname(un->un_dev), __func__)); /* * First, check if we even need to load the firmware. @@ -245,54 +227,54 @@ kue_load_fw(struct kue_softc *sc) * it's probed while the firmware is still loaded and * running. */ - if (usbd_get_device_desc(sc->kue_udev, &dd)) + if (usbd_get_device_desc(un->un_udev, &dd)) return EIO; if (UGETW(dd.bcdDevice) == KUE_WARM_REV) { printf("%s: warm boot, no firmware download\n", - device_xname(sc->kue_dev)); + device_xname(un->un_dev)); return 0; } printf("%s: cold boot, downloading firmware\n", - device_xname(sc->kue_dev)); + device_xname(un->un_dev)); /* Load code segment */ DPRINTFN(1,("%s: kue_load_fw: download code_seg\n", - device_xname(sc->kue_dev))); + device_xname(un->un_dev))); /*XXXUNCONST*/ - err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN, + err = kue_ctl(un, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN, 0, __UNCONST(kue_code_seg), sizeof(kue_code_seg)); if (err) { printf("%s: failed to load code segment: %s\n", - device_xname(sc->kue_dev), usbd_errstr(err)); + device_xname(un->un_dev), usbd_errstr(err)); return EIO; } /* Load fixup segment */ DPRINTFN(1,("%s: kue_load_fw: download fix_seg\n", - device_xname(sc->kue_dev))); + device_xname(un->un_dev))); /*XXXUNCONST*/ - err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN, + err = kue_ctl(un, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN, 0, __UNCONST(kue_fix_seg), sizeof(kue_fix_seg)); if (err) { printf("%s: failed to load fixup segment: %s\n", - device_xname(sc->kue_dev), usbd_errstr(err)); + device_xname(un->un_dev), usbd_errstr(err)); return EIO; } /* Send trigger command. */ DPRINTFN(1,("%s: kue_load_fw: download trig_seg\n", - device_xname(sc->kue_dev))); + device_xname(un->un_dev))); /*XXXUNCONST*/ - err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN, + err = kue_ctl(un, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN, 0, __UNCONST(kue_trig_seg), sizeof(kue_trig_seg)); if (err) { printf("%s: failed to load trigger segment: %s\n", - device_xname(sc->kue_dev), usbd_errstr(err)); + device_xname(un->un_dev), usbd_errstr(err)); return EIO; } - usbd_delay_ms(sc->kue_udev, 10); + usbd_delay_ms(un->un_udev, 10); /* * Reload device descriptor. @@ -302,33 +284,34 @@ kue_load_fw(struct kue_softc *sc) * code. This confuses the quirk mechanism, which is * dependent on the revision data. */ - (void)usbd_reload_device_desc(sc->kue_udev); + (void)usbd_reload_device_desc(un->un_udev); - DPRINTFN(1,("%s: %s: done\n", device_xname(sc->kue_dev), __func__)); + DPRINTFN(1,("%s: %s: done\n", device_xname(un->un_dev), __func__)); /* Reset the adapter. */ - kue_reset(sc); + kue_reset(un); return 0; } static void -kue_setmulti(struct kue_softc *sc) +kue_setiff(struct usbnet *un) { - struct ethercom *ec = &sc->kue_ec; - struct ifnet *ifp = GET_IFP(sc); + struct ethercom * ec = usbnet_ec(un); + struct kue_softc * sc = usbnet_softc(un); + struct ifnet * const ifp = usbnet_ifp(un); struct ether_multi *enm; struct ether_multistep step; int i; - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__)); + DPRINTFN(5,("%s: %s: enter\n", device_xname(un->un_dev), __func__)); if (ifp->if_flags & IFF_PROMISC) { allmulti: ifp->if_flags |= IFF_ALLMULTI; sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI; sc->kue_rxfilt &= ~KUE_RXFILT_MULTICAST; - kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt); + kue_setword(un, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt); return; } @@ -354,10 +337,10 @@ allmulti: ifp->if_flags &= ~IFF_ALLMULTI; sc->kue_rxfilt |= KUE_RXFILT_MULTICAST; - kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MCAST_FILTERS, + kue_ctl(un, 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); + kue_setword(un, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt); } /* @@ -366,17 +349,17 @@ allmulti: * bring it into proper operation. */ static void -kue_reset(struct kue_softc *sc) +kue_reset(struct usbnet *un) { - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__)); + DPRINTFN(5,("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - if (usbd_set_config_no(sc->kue_udev, KUE_CONFIG_NO, 1) || - usbd_device2interface_handle(sc->kue_udev, KUE_IFACE_IDX, - &sc->kue_iface)) - printf("%s: reset failed\n", device_xname(sc->kue_dev)); + if (usbd_set_config_no(un->un_udev, KUE_CONFIG_NO, 1) || + usbd_device2interface_handle(un->un_udev, KUE_IFACE_IDX, + &un->un_iface)) + printf("%s: reset failed\n", device_xname(un->un_dev)); /* Wait a little while for the chip to get its brains in order. */ - usbd_delay_ms(sc->kue_udev, 10); + usbd_delay_ms(un->un_udev, 10); } /* @@ -401,28 +384,36 @@ void kue_attach(device_t parent, device_t self, void *aux) { struct kue_softc *sc = device_private(self); + struct usbnet * const un = &sc->kue_un; struct usb_attach_arg *uaa = aux; char *devinfop; - int s; - struct ifnet *ifp; struct usbd_device * dev = uaa->uaa_device; - struct usbd_interface * iface; usbd_status err; usb_interface_descriptor_t *id; usb_endpoint_descriptor_t *ed; int i; - DPRINTFN(5,(" : kue_attach: sc=%p, dev=%p", sc, dev)); + KASSERT((void *)sc == un); - sc->kue_dev = self; + DPRINTFN(5,(" : kue_attach: sc=%p, dev=%p", sc, dev)); aprint_naive("\n"); aprint_normal("\n"); - devinfop = usbd_devinfo_alloc(dev, 0); aprint_normal_dev(self, "%s\n", devinfop); usbd_devinfo_free(devinfop); + un->un_dev = self; + un->un_udev = dev; + un->un_sc = sc; + un->un_ops = &kue_ops; + un->un_rx_xfer_flags = USBD_SHORT_XFER_OK; + un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER; + un->un_rx_list_cnt = KUE_RX_LIST_CNT; + un->un_tx_list_cnt = KUE_TX_LIST_CNT; + un->un_rx_bufsz = KUE_BUFSZ; + un->un_tx_bufsz = KUE_BUFSZ; + err = usbd_set_config_no(dev, KUE_CONFIG_NO, 1); if (err) { aprint_error_dev(self, "failed to set configuration" @@ -430,102 +421,67 @@ kue_attach(device_t parent, device_t sel return; } - sc->kue_udev = dev; - sc->kue_product = uaa->uaa_product; - sc->kue_vendor = uaa->uaa_vendor; - /* Load the firmware into the NIC. */ - if (kue_load_fw(sc)) { + if (kue_load_fw(un)) { aprint_error_dev(self, "loading firmware failed\n"); return; } - err = usbd_device2interface_handle(dev, KUE_IFACE_IDX, &iface); + err = usbd_device2interface_handle(dev, KUE_IFACE_IDX, &un->un_iface); if (err) { aprint_error_dev(self, "getting interface handle failed\n"); return; } - sc->kue_iface = iface; - id = usbd_get_interface_descriptor(iface); + id = usbd_get_interface_descriptor(un->un_iface); /* Find endpoints. */ for (i = 0; i < id->bNumEndpoints; i++) { - ed = usbd_interface2endpoint_descriptor(iface, i); + ed = usbd_interface2endpoint_descriptor(un->un_iface, i); if (ed == NULL) { aprint_error_dev(self, "couldn't get ep %d\n", i); return; } if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { - sc->kue_ed[KUE_ENDPT_RX] = ed->bEndpointAddress; + un->un_ed[KUE_ENDPT_RX] = ed->bEndpointAddress; } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { - sc->kue_ed[KUE_ENDPT_TX] = ed->bEndpointAddress; + un->un_ed[KUE_ENDPT_TX] = ed->bEndpointAddress; } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { - sc->kue_ed[KUE_ENDPT_INTR] = ed->bEndpointAddress; + un->un_ed[KUE_ENDPT_INTR] = ed->bEndpointAddress; } } - if (sc->kue_ed[KUE_ENDPT_RX] == 0 || sc->kue_ed[KUE_ENDPT_TX] == 0) { + if (un->un_ed[KUE_ENDPT_RX] == 0 || un->un_ed[KUE_ENDPT_TX] == 0) { aprint_error_dev(self, "missing endpoint\n"); return; } + /* First level attach. */ + usbnet_attach(un, "kuedet"); /* Read ethernet descriptor */ - err = kue_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR, + err = kue_ctl(un, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR, 0, &sc->kue_desc, sizeof(sc->kue_desc)); if (err) { aprint_error_dev(self, "could not read Ethernet descriptor\n"); return; } + memcpy(un->un_eaddr, sc->kue_desc.kue_macaddr, sizeof un->un_eaddr); + /* XXXMRG what uses this? */ sc->kue_mcfilters = kmem_alloc(KUE_MCFILTCNT(sc) * ETHER_ADDR_LEN, KM_SLEEP); - s = splnet(); - - /* - * A KLSI chip was detected. Inform the world. - */ - aprint_normal_dev(self, "Ethernet address %s\n", - ether_sprintf(sc->kue_desc.kue_macaddr)); - - /* Initialize interface info.*/ - ifp = GET_IFP(sc); - ifp->if_softc = sc; - ifp->if_mtu = ETHERMTU; - ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; - ifp->if_ioctl = kue_ioctl; - ifp->if_start = kue_start; - ifp->if_watchdog = kue_watchdog; - strlcpy(ifp->if_xname, device_xname(sc->kue_dev), IFNAMSIZ); - - IFQ_SET_READY(&ifp->if_snd); - - /* Attach the interface. */ - if_attach(ifp); - ether_ifattach(ifp, sc->kue_desc.kue_macaddr); - rnd_attach_source(&sc->rnd_source, device_xname(sc->kue_dev), - RND_TYPE_NET, RND_FLAG_DEFAULT); - - sc->kue_attached = true; - splx(s); - - usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->kue_udev, sc->kue_dev); - - return; + usbnet_attach_ifp(un, false, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST, + 0, 0); } int kue_detach(device_t self, int flags) { struct kue_softc *sc = device_private(self); - struct ifnet *ifp = GET_IFP(sc); - int s; - - s = splusb(); /* XXX why? */ if (sc->kue_mcfilters != NULL) { kmem_free(sc->kue_mcfilters, @@ -533,99 +489,7 @@ kue_detach(device_t self, int flags) sc->kue_mcfilters = NULL; } - if (!sc->kue_attached) { - /* Detached before attached finished, so just bail out. */ - splx(s); - return 0; - } - - if (ifp->if_flags & IFF_RUNNING) - kue_stop(sc); - - rnd_detach_source(&sc->rnd_source); - ether_ifdetach(ifp); - - if_detach(ifp); - -#ifdef DIAGNOSTIC - if (sc->kue_ep[KUE_ENDPT_TX] != NULL || - sc->kue_ep[KUE_ENDPT_RX] != NULL || - sc->kue_ep[KUE_ENDPT_INTR] != NULL) - aprint_debug_dev(self, "detach has active endpoints\n"); -#endif - - sc->kue_attached = false; - splx(s); - - return 0; -} - -int -kue_activate(device_t self, enum devact act) -{ - struct kue_softc *sc = device_private(self); - - DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__)); - - switch (act) { - case DVACT_DEACTIVATE: - /* Deactivate the interface. */ - if_deactivate(&sc->kue_ec.ec_if); - sc->kue_dying = true; - return 0; - default: - return EOPNOTSUPP; - } -} - -static int -kue_rx_list_init(struct kue_softc *sc) -{ - struct kue_cdata *cd; - struct kue_chain *c; - int i; - - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__)); - - cd = &sc->kue_cdata; - for (i = 0; i < KUE_RX_LIST_CNT; i++) { - c = &cd->kue_rx_chain[i]; - c->kue_sc = sc; - if (c->kue_xfer == NULL) { - int error = usbd_create_xfer(sc->kue_ep[KUE_ENDPT_RX], - KUE_BUFSZ, 0, 0, &c->kue_xfer); - if (error) - return error; - c->kue_buf = usbd_get_buffer(c->kue_xfer); - } - } - - return 0; -} - -static int -kue_tx_list_init(struct kue_softc *sc) -{ - struct kue_cdata *cd; - struct kue_chain *c; - int i; - - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__)); - - cd = &sc->kue_cdata; - for (i = 0; i < KUE_TX_LIST_CNT; i++) { - c = &cd->kue_tx_chain[i]; - c->kue_sc = sc; - if (c->kue_xfer == NULL) { - int error = usbd_create_xfer(sc->kue_ep[KUE_ENDPT_TX], - KUE_BUFSZ, 0, 0, &c->kue_xfer); - if (error) - return error; - c->kue_buf = usbd_get_buffer(c->kue_xfer); - } - } - - return 0; + return usbnet_detach(self, flags); } /* @@ -633,252 +497,89 @@ kue_tx_list_init(struct kue_softc *sc) * the higher level protocols. */ static void -kue_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status) +kue_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len) { - struct kue_chain *c = priv; - struct kue_softc *sc = c->kue_sc; - struct ifnet *ifp = GET_IFP(sc); - struct mbuf *m; - int total_len, pktlen; - int s; - - DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->kue_dev), - __func__, status)); - - if (sc->kue_dying) - return; - - if (!(ifp->if_flags & IFF_RUNNING)) - return; - - if (status != USBD_NORMAL_COMPLETION) { - if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) - return; - sc->kue_rx_errs++; - if (usbd_ratecheck(&sc->kue_rx_notice)) { - printf("%s: %u usb errors on rx: %s\n", - device_xname(sc->kue_dev), sc->kue_rx_errs, - usbd_errstr(status)); - sc->kue_rx_errs = 0; - } - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async(sc->kue_ep[KUE_ENDPT_RX]); - goto done; - } - - usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); - - DPRINTFN(10,("%s: %s: total_len=%d len=%d\n", device_xname(sc->kue_dev), - __func__, total_len, - le16dec(c->kue_buf))); + struct ifnet *ifp = usbnet_ifp(un); + uint8_t *buf = c->unc_buf; + unsigned pktlen; + + DPRINTFN(10,("%s: %s: total_len=%d len=%d\n", + device_xname(un->un_dev), __func__, + total_len, le16dec(buf))); if (total_len <= 1) - goto done; + return; - pktlen = le16dec(c->kue_buf); + pktlen = le16dec(buf); if (pktlen > total_len - 2) - pktlen = total_len - 2; + pktlen = total_len - ETHER_ALIGN; if (pktlen < ETHER_MIN_LEN - ETHER_CRC_LEN || pktlen > MCLBYTES - ETHER_ALIGN) { ifp->if_ierrors++; - goto done; - } - - /* No errors; receive the packet. */ - MGETHDR(m, M_DONTWAIT, MT_DATA); - if (m == NULL) { - ifp->if_ierrors++; - goto done; - } - if (pktlen > MHLEN - ETHER_ALIGN) { - MCLGET(m, M_DONTWAIT); - if ((m->m_flags & M_EXT) == 0) { - m_freem(m); - ifp->if_ierrors++; - goto done; - } - } - m->m_data += ETHER_ALIGN; - - /* copy data to mbuf */ - memcpy(mtod(m, uint8_t *), c->kue_buf + 2, pktlen); - - m->m_pkthdr.len = m->m_len = pktlen; - m_set_rcvif(m, ifp); - - s = splnet(); - - DPRINTFN(10,("%s: %s: deliver %d\n", device_xname(sc->kue_dev), - __func__, m->m_len)); - if_percpuq_enqueue(ifp->if_percpuq, m); - - splx(s); - - done: - - /* Setup new transfer. */ - usbd_setup_xfer(c->kue_xfer, c, c->kue_buf, KUE_BUFSZ, - USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, kue_rxeof); - usbd_transfer(c->kue_xfer); - - DPRINTFN(10,("%s: %s: start rx\n", device_xname(sc->kue_dev), - __func__)); -} - -/* - * A frame was downloaded to the chip. It's safe for us to clean up - * the list buffers. - */ - -static void -kue_txeof(struct usbd_xfer *xfer, void *priv, - usbd_status status) -{ - struct kue_chain *c = priv; - struct kue_softc *sc = c->kue_sc; - struct ifnet *ifp = GET_IFP(sc); - int s; - - if (sc->kue_dying) - return; - - s = splnet(); - - DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->kue_dev), - __func__, status)); - - ifp->if_timer = 0; - ifp->if_flags &= ~IFF_OACTIVE; - - if (status != USBD_NORMAL_COMPLETION) { - if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { - splx(s); - return; - } - ifp->if_oerrors++; - printf("%s: usb error on tx: %s\n", device_xname(sc->kue_dev), - usbd_errstr(status)); - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async(sc->kue_ep[KUE_ENDPT_TX]); - splx(s); return; } - ifp->if_opackets++; - - if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) - kue_start(ifp); - - splx(s); -} - -static int -kue_send(struct kue_softc *sc, struct mbuf *m, int idx) -{ - int total_len; - struct kue_chain *c; - usbd_status err; - - DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__)); - - c = &sc->kue_cdata.kue_tx_chain[idx]; - - /* Frame length is specified in the first 2 bytes of the buffer. */ - le16enc(c->kue_buf, (uint16_t)m->m_pkthdr.len); - - /* - * 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); - - total_len = 2 + m->m_pkthdr.len; - total_len = roundup2(total_len, 64); - - usbd_setup_xfer(c->kue_xfer, c, c->kue_buf, total_len, 0, - USBD_DEFAULT_TIMEOUT, kue_txeof); - - /* Transmit */ - err = usbd_transfer(c->kue_xfer); - if (err != USBD_IN_PROGRESS) { - printf("%s: kue_send error=%s\n", device_xname(sc->kue_dev), - usbd_errstr(err)); - kue_stop(sc); - return EIO; - } - - sc->kue_cdata.kue_tx_cnt++; - - return 0; + DPRINTFN(10,("%s: %s: deliver %d\n", device_xname(un->un_dev), + __func__, pktlen)); + usbnet_enqueue(un, buf + ETHER_ALIGN, pktlen, 0, 0, 0); } -static void -kue_start(struct ifnet *ifp) +static unsigned +kue_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) { - struct kue_softc *sc = ifp->if_softc; - struct mbuf *m; - - DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__)); + int total_len, pkt_len; - if (sc->kue_dying) - return; + DPRINTFN(10,("%s: %s: enter\n", device_xname(un->un_dev),__func__)); - if (ifp->if_flags & IFF_OACTIVE) - return; + pkt_len = ETHER_ALIGN + m->m_pkthdr.len; + total_len = roundup2(pkt_len, 64); - IFQ_POLL(&ifp->if_snd, m); - if (m == NULL) - return; - - if (kue_send(sc, m, 0)) { - ifp->if_flags |= IFF_OACTIVE; - return; - } + if ((unsigned)total_len > un->un_tx_bufsz) + return 0; - IFQ_DEQUEUE(&ifp->if_snd, m); + /* Frame length is specified in the first 2 bytes of the buffer. */ + le16enc(c->unc_buf, (uint16_t)m->m_pkthdr.len); /* - * If there's a BPF listener, bounce a copy of this frame - * to him. + * Copy the mbuf data into a contiguous buffer after the frame length, + * possibly zeroing the rest of the buffer. */ - bpf_mtap(ifp, m, BPF_D_OUT); - m_freem(m); - - ifp->if_flags |= IFF_OACTIVE; + m_copydata(m, 0, m->m_pkthdr.len, c->unc_buf + ETHER_ALIGN); + if (total_len - pkt_len > 0) + memset(c->unc_buf + pkt_len, 0, total_len - pkt_len); - /* - * Set a timeout in case the chip goes out to lunch. - */ - ifp->if_timer = 6; + return total_len; } -static void -kue_init(void *xsc) +static int +kue_init(struct ifnet *ifp) { - struct kue_softc *sc = xsc; - struct ifnet *ifp = GET_IFP(sc); - int s; + struct usbnet * const un = ifp->if_softc; + struct kue_softc *sc = usbnet_softc(un); uint8_t eaddr[ETHER_ADDR_LEN]; - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__)); + DPRINTFN(5,("%s: %s: enter\n", device_xname(un->un_dev),__func__)); - if (ifp->if_flags & IFF_RUNNING) - return; + if (usbnet_isdying(un)) + return -1; - s = splnet(); + /* Cancel pending I/O */ + usbnet_stop(un, ifp, 1); memcpy(eaddr, CLLADDR(ifp->if_sadl), sizeof(eaddr)); /* Set MAC address */ - kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC, 0, eaddr, ETHER_ADDR_LEN); + kue_ctl(un, KUE_CTL_WRITE, KUE_CMD_SET_MAC, 0, eaddr, 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; + else + sc->kue_rxfilt &= ~KUE_RXFILT_PROMISC; - kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt); + kue_setword(un, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt); /* I'm not sure how to tune these. */ #if 0 @@ -886,266 +587,33 @@ kue_init(void *xsc) * Leave this one alone for now; setting it * wrong causes lockups on some machines/controllers. */ - kue_setword(sc, KUE_CMD_SET_SOFS, 1); + kue_setword(un, KUE_CMD_SET_SOFS, 1); #endif - kue_setword(sc, KUE_CMD_SET_URB_SIZE, 64); + kue_setword(un, KUE_CMD_SET_URB_SIZE, 64); /* Load the multicast filter. */ - kue_setmulti(sc); + kue_setiff(un); - if (sc->kue_ep[KUE_ENDPT_RX] == NULL) { - if (kue_open_pipes(sc)) { - splx(s); - return; - } - } - /* Init TX ring. */ - if (kue_tx_list_init(sc)) { - printf("%s: tx list init failed\n", device_xname(sc->kue_dev)); - splx(s); - return; - } - - /* Init RX ring. */ - if (kue_rx_list_init(sc)) { - printf("%s: rx list init failed\n", device_xname(sc->kue_dev)); - splx(s); - return; - } - - /* Start up the receive pipe. */ - for (size_t i = 0; i < KUE_RX_LIST_CNT; i++) { - struct kue_chain *c = &sc->kue_cdata.kue_rx_chain[i]; - usbd_setup_xfer(c->kue_xfer, c, c->kue_buf, KUE_BUFSZ, - USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, kue_rxeof); - DPRINTFN(5,("%s: %s: start read\n", device_xname(sc->kue_dev), - __func__)); - usbd_transfer(c->kue_xfer); - } - - ifp->if_flags |= IFF_RUNNING; - ifp->if_flags &= ~IFF_OACTIVE; - - splx(s); -} - -static int -kue_open_pipes(struct kue_softc *sc) -{ - usbd_status err; - - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__)); - - /* 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("%s: open rx pipe failed: %s\n", - device_xname(sc->kue_dev), usbd_errstr(err)); - return EIO; - } - - 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("%s: open tx pipe failed: %s\n", - device_xname(sc->kue_dev), usbd_errstr(err)); - return EIO; - } - - return 0; + return usbnet_init_rx_tx(un); } static int -kue_ioctl(struct ifnet *ifp, u_long command, void *data) +kue_ioctl_cb(struct ifnet *ifp, u_long cmd, void *data) { - struct kue_softc *sc = ifp->if_softc; - struct ifaddr *ifa = (struct ifaddr *)data; - struct ifreq *ifr = (struct ifreq *)data; - int s, error = 0; - - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__)); - - if (sc->kue_dying) - return EIO; - - s = splnet(); - - switch (command) { - case SIOCINITIFADDR: - ifp->if_flags |= IFF_UP; - kue_init(sc); - - switch (ifa->ifa_addr->sa_family) { -#ifdef INET - case AF_INET: - arp_ifinit(ifp, ifa); - break; -#endif /* INET */ - } - break; - - case SIOCSIFMTU: - if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU) - error = EINVAL; - else if ((error = ifioctl_common(ifp, command, data)) == ENETRESET) - error = 0; - break; - - case SIOCSIFFLAGS: - if ((error = ifioctl_common(ifp, command, data)) != 0) - break; - 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; + switch (cmd) { case SIOCADDMULTI: case SIOCDELMULTI: - error = ether_ioctl(ifp, command, data); - if (error == ENETRESET) { - if (ifp->if_flags & IFF_RUNNING) - kue_setmulti(sc); - error = 0; - } + kue_init(ifp); break; default: - error = ether_ioctl(ifp, command, data); break; } - splx(s); - - return error; -} - -static void -kue_watchdog(struct ifnet *ifp) -{ - struct kue_softc *sc = ifp->if_softc; - struct kue_chain *c; - usbd_status stat; - int s; - - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__)); - - if (sc->kue_dying) - return; - - ifp->if_oerrors++; - printf("%s: watchdog timeout\n", device_xname(sc->kue_dev)); - - s = splusb(); - 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 (IFQ_IS_EMPTY(&ifp->if_snd) == 0) - kue_start(ifp); - splx(s); + return 0; } -/* - * Stop the adapter and free any mbufs allocated to the - * RX and TX lists. - */ -static void -kue_stop(struct kue_softc *sc) -{ - usbd_status err; - struct ifnet *ifp; - int i; - - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__)); - - ifp = GET_IFP(sc); - 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("%s: abort rx pipe failed: %s\n", - device_xname(sc->kue_dev), usbd_errstr(err)); - } - } - - if (sc->kue_ep[KUE_ENDPT_TX] != NULL) { - err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]); - if (err) { - printf("%s: abort tx pipe failed: %s\n", - device_xname(sc->kue_dev), usbd_errstr(err)); - } - } - - if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) { - err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]); - if (err) { - printf("%s: abort intr pipe failed: %s\n", - device_xname(sc->kue_dev), usbd_errstr(err)); - } - } - - /* Free RX resources. */ - for (i = 0; i < KUE_RX_LIST_CNT; i++) { - if (sc->kue_cdata.kue_rx_chain[i].kue_xfer != NULL) { - usbd_destroy_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_xfer != NULL) { - usbd_destroy_xfer(sc->kue_cdata.kue_tx_chain[i].kue_xfer); - sc->kue_cdata.kue_tx_chain[i].kue_xfer = NULL; - } - } - - /* Close pipes. */ - if (sc->kue_ep[KUE_ENDPT_RX] != NULL) { - err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_RX]); - if (err) { - printf("%s: close rx pipe failed: %s\n", - device_xname(sc->kue_dev), usbd_errstr(err)); - } - sc->kue_ep[KUE_ENDPT_RX] = NULL; - } - - if (sc->kue_ep[KUE_ENDPT_TX] != NULL) { - err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_TX]); - if (err) { - printf("%s: close tx pipe failed: %s\n", - device_xname(sc->kue_dev), usbd_errstr(err)); - } - sc->kue_ep[KUE_ENDPT_TX] = NULL; - } - - if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) { - err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_INTR]); - if (err) { - printf("%s: close intr pipe failed: %s\n", - device_xname(sc->kue_dev), usbd_errstr(err)); - } - sc->kue_ep[KUE_ENDPT_INTR] = NULL; - } +#ifdef _MODULE +#include "ioconf.c" +#endif - ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); -} +USBNET_MODULE(kue) Index: if_kuereg.h =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_kuereg.h,v retrieving revision 1.23 diff -p -u -r1.23 if_kuereg.h --- if_kuereg.h 1 Aug 2019 00:10:22 -0000 1.23 +++ if_kuereg.h 15 Aug 2019 08:03:39 -0000 @@ -1,4 +1,5 @@ /* $NetBSD: if_kuereg.h,v 1.23 2019/08/01 00:10:22 mrg Exp $ */ + /* * Copyright (c) 1997, 1998, 1999, 2000 * Bill Paul . All rights reserved. @@ -141,6 +142,7 @@ struct kue_type { uint16_t kue_did; }; +#if 0 struct kue_softc; struct kue_chain { @@ -155,28 +157,12 @@ struct kue_cdata { int kue_tx_prod; int kue_tx_cnt; }; +#endif struct kue_softc { - device_t kue_dev; + struct usbnet kue_un; - struct ethercom kue_ec; - krndsource_t rnd_source; -#define GET_IFP(sc) (&(sc)->kue_ec.ec_if) - - struct usbd_device * kue_udev; - struct usbd_interface * kue_iface; - uint16_t kue_vendor; - uint16_t kue_product; struct kue_ether_desc kue_desc; - int kue_ed[KUE_ENDPT_MAX]; - struct usbd_pipe * kue_ep[KUE_ENDPT_MAX]; - int kue_if_flags; uint16_t kue_rxfilt; uint8_t *kue_mcfilters; - struct kue_cdata kue_cdata; - - bool kue_dying; - bool kue_attached; - u_int kue_rx_errs; - struct timeval kue_rx_notice; }; Index: if_upl.c =================================================================== RCS file: /cvsroot/src/sys/dev/usb/if_upl.c,v retrieving revision 1.64 diff -p -u -r1.64 if_upl.c --- if_upl.c 21 Jul 2019 10:27:56 -0000 1.64 +++ if_upl.c 15 Aug 2019 08:03:39 -0000 @@ -1,4 +1,5 @@ /* $NetBSD: if_upl.c,v 1.64 2019/07/21 10:27:56 mrg Exp $ */ + /* * Copyright (c) 2000 The NetBSD Foundation, Inc. * All rights reserved. @@ -42,22 +43,10 @@ __KERNEL_RCSID(0, "$NetBSD: if_upl.c,v 1 #endif #include -#include -#include -#include -#include -#include -#include -#include -#include +#include -#include #include -#include -#include - -#include #ifdef INET #include @@ -65,12 +54,6 @@ __KERNEL_RCSID(0, "$NetBSD: if_upl.c,v 1 #include #endif - -#include -#include -#include -#include - /* * 7 6 5 4 3 2 1 0 * tx rx 1 0 @@ -83,8 +66,6 @@ __KERNEL_RCSID(0, "$NetBSD: if_upl.c,v 1 #define UPL_RXDATA 0x40 #define UPL_TXOK 0x80 -#define UPL_INTR_PKTLEN 1 - #define UPL_CONFIG_NO 1 #define UPL_IFACE_IDX 0 @@ -94,61 +75,13 @@ __KERNEL_RCSID(0, "$NetBSD: if_upl.c,v 1 #define UPL_BUFSZ 1024 -#define UPL_RX_FRAMES 1 -#define UPL_TX_FRAMES 1 - #define UPL_RX_LIST_CNT 1 #define UPL_TX_LIST_CNT 1 -#define UPL_ENDPT_RX 0x0 -#define UPL_ENDPT_TX 0x1 -#define UPL_ENDPT_INTR 0x2 -#define UPL_ENDPT_MAX 0x3 - -struct upl_type { - uint16_t upl_vid; - uint16_t upl_did; -}; - -struct upl_softc; - -struct upl_chain { - struct upl_softc *upl_sc; - struct usbd_xfer *upl_xfer; - char *upl_buf; - struct mbuf *upl_mbuf; -}; - -struct upl_cdata { - struct upl_chain upl_tx_chain[UPL_TX_LIST_CNT]; - struct upl_chain upl_rx_chain[UPL_RX_LIST_CNT]; - int upl_tx_prod; - int upl_tx_cnt; -}; - struct upl_softc { - device_t sc_dev; - - struct ifnet sc_if; - krndsource_t sc_rnd_source; - - struct callout sc_stat_ch; - - struct usbd_device * sc_udev; - struct usbd_interface * sc_iface; - uint16_t sc_vendor; - uint16_t sc_product; - int sc_ed[UPL_ENDPT_MAX]; - struct usbd_pipe * sc_ep[UPL_ENDPT_MAX]; - struct upl_cdata sc_cdata; - + struct usbnet sc_un; + struct usbnet_intr sc_intr; uByte sc_ibuf; - - char sc_dying; - char sc_attached; - u_int sc_rx_errs; - struct timeval sc_rx_notice; - u_int sc_intr_errs; }; #ifdef UPL_DEBUG @@ -163,7 +96,7 @@ int upldebug = 0; /* * Various supported device vendors/products. */ -Static struct upl_type sc_devs[] = { +static struct usb_devno sc_devs[] = { { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2301 }, { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2302 }, { 0, 0 } @@ -171,29 +104,28 @@ Static struct upl_type sc_devs[] = { int upl_match(device_t, cfdata_t, void *); void upl_attach(device_t, device_t, void *); -int upl_detach(device_t, int); -int upl_activate(device_t, enum devact); CFATTACH_DECL_NEW(upl, sizeof(struct upl_softc), upl_match, upl_attach, - upl_detach, upl_activate); + usbnet_detach, usbnet_activate); -Static int upl_openpipes(struct upl_softc *); -Static int upl_tx_list_init(struct upl_softc *); -Static int upl_rx_list_init(struct upl_softc *); -Static int upl_newbuf(struct upl_softc *, struct upl_chain *, struct mbuf *); -Static int upl_send(struct upl_softc *, struct mbuf *, int); -Static void upl_intr(struct usbd_xfer *, void *, usbd_status); -Static void upl_rxeof(struct usbd_xfer *, void *, usbd_status); -Static void upl_txeof(struct usbd_xfer *, void *, usbd_status); -Static void upl_start(struct ifnet *); -Static int upl_ioctl(struct ifnet *, u_long, void *); -Static void upl_init(void *); -Static void upl_stop(struct upl_softc *); -Static void upl_watchdog(struct ifnet *); +static void upl_intr_cb(struct usbnet *, usbd_status); +static void upl_rx_loop(struct usbnet *, struct usbnet_chain *, uint32_t); +static unsigned upl_tx_prepare(struct usbnet *, struct mbuf *, + struct usbnet_chain *); +static int upl_ioctl_cb(struct ifnet *, u_long, void *); +static int upl_init(struct ifnet *); + +static struct usbnet_ops upl_ops = { + .uno_init = upl_init, + .uno_tx_prepare = upl_tx_prepare, + .uno_rx_loop = upl_rx_loop, + .uno_ioctl = upl_ioctl_cb, + .uno_intr = upl_intr_cb, +}; -Static int upl_output(struct ifnet *, struct mbuf *, const struct sockaddr *, +static int upl_output(struct ifnet *, struct mbuf *, const struct sockaddr *, const struct rtentry *); -Static void upl_input(struct ifnet *, struct mbuf *); +static void upl_input(struct ifnet *, struct mbuf *); /* * Probe for a Prolific chip. @@ -202,42 +134,51 @@ int upl_match(device_t parent, cfdata_t match, void *aux) { struct usb_attach_arg *uaa = aux; - struct upl_type *t; - - for (t = sc_devs; t->upl_vid != 0; t++) - if (uaa->uaa_vendor == t->upl_vid && uaa->uaa_product == t->upl_did) - return UMATCH_VENDOR_PRODUCT; - return UMATCH_NONE; + return usb_lookup(sc_devs, uaa->uaa_vendor, uaa->uaa_product) != NULL ? + UMATCH_VENDOR_PRODUCT : UMATCH_NONE; } void upl_attach(device_t parent, device_t self, void *aux) { struct upl_softc *sc = device_private(self); + struct usbnet * const un = &sc->sc_un; struct usb_attach_arg *uaa = aux; char *devinfop; - int s; struct usbd_device * dev = uaa->uaa_device; struct usbd_interface * iface; usbd_status err; - struct ifnet *ifp; usb_interface_descriptor_t *id; usb_endpoint_descriptor_t *ed; int i; - int rv; - DPRINTFN(5,(" : upl_attach: sc=%p, dev=%p", sc, dev)); + KASSERT((void *)sc == un); - sc->sc_dev = self; + DPRINTFN(5,(" : upl_attach: sc=%p, dev=%p", sc, dev)); aprint_naive("\n"); aprint_normal("\n"); - devinfop = usbd_devinfo_alloc(dev, 0); aprint_normal_dev(self, "%s\n", devinfop); usbd_devinfo_free(devinfop); + sc->sc_intr.uni_buf = &sc->sc_ibuf; + sc->sc_intr.uni_bufsz = sizeof(sc->sc_ibuf); + sc->sc_intr.uni_interval = UPL_INTR_INTERVAL; + + un->un_dev = self; + un->un_udev = dev; + un->un_sc = sc; + un->un_ops = &upl_ops; + un->un_intr = &sc->sc_intr; + un->un_rx_xfer_flags = USBD_SHORT_XFER_OK; + un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER; + un->un_rx_list_cnt = UPL_RX_LIST_CNT; + un->un_tx_list_cnt = UPL_TX_LIST_CNT; + un->un_rx_bufsz = UPL_BUFSZ; + un->un_tx_bufsz = UPL_BUFSZ; + err = usbd_set_config_no(dev, UPL_CONFIG_NO, 1); if (err) { aprint_error_dev(self, "failed to set configuration" @@ -245,17 +186,13 @@ upl_attach(device_t parent, device_t sel return; } - sc->sc_udev = dev; - sc->sc_product = uaa->uaa_product; - sc->sc_vendor = uaa->uaa_vendor; - err = usbd_device2interface_handle(dev, UPL_IFACE_IDX, &iface); if (err) { aprint_error_dev(self, "getting interface handle failed\n"); return; } - sc->sc_iface = iface; + un->un_iface = iface; id = usbd_get_interface_descriptor(iface); /* Find endpoints. */ @@ -267,34 +204,25 @@ upl_attach(device_t parent, device_t sel } if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { - sc->sc_ed[UPL_ENDPT_RX] = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress; } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { - sc->sc_ed[UPL_ENDPT_TX] = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress; } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { - sc->sc_ed[UPL_ENDPT_INTR] = ed->bEndpointAddress; + un->un_ed[USBNET_ENDPT_INTR] = ed->bEndpointAddress; } } - if (sc->sc_ed[UPL_ENDPT_RX] == 0 || sc->sc_ed[UPL_ENDPT_TX] == 0 || - sc->sc_ed[UPL_ENDPT_INTR] == 0) { + if (un->un_ed[USBNET_ENDPT_RX] == 0 || un->un_ed[USBNET_ENDPT_TX] == 0 || + un->un_ed[USBNET_ENDPT_INTR] == 0) { aprint_error_dev(self, "missing endpoint\n"); return; } - s = splnet(); - /* Initialize interface info.*/ - ifp = &sc->sc_if; - ifp->if_softc = sc; + struct ifnet *ifp = usbnet_ifp(un); ifp->if_mtu = UPL_BUFSZ; - ifp->if_flags = IFF_POINTOPOINT | IFF_NOARP | IFF_SIMPLEX; - ifp->if_ioctl = upl_ioctl; - ifp->if_start = upl_start; - ifp->if_watchdog = upl_watchdog; - strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ); - ifp->if_type = IFT_OTHER; ifp->if_addrlen = 0; ifp->if_hdrlen = 0; @@ -302,705 +230,100 @@ upl_attach(device_t parent, device_t sel ifp->_if_input = upl_input; ifp->if_baudrate = 12000000; ifp->if_dlt = DLT_RAW; - IFQ_SET_READY(&ifp->if_snd); - - /* Attach the interface. */ - rv = if_initialize(ifp); - if (rv != 0) { - aprint_error_dev(self, "if_initialize failed(%d)\n", rv); - splx(s); - return; - } - if_register(ifp); - if_alloc_sadl(ifp); - - bpf_attach(ifp, DLT_RAW, 0); - rnd_attach_source(&sc->sc_rnd_source, device_xname(sc->sc_dev), - RND_TYPE_NET, RND_FLAG_DEFAULT); - - sc->sc_attached = 1; - splx(s); - - usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev); - - return; -} - -int -upl_detach(device_t self, int flags) -{ - struct upl_softc *sc = device_private(self); - struct ifnet *ifp = &sc->sc_if; - int s; - - DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - s = splusb(); - - if (!sc->sc_attached) { - /* Detached before attached finished, so just bail out. */ - splx(s); - return 0; - } - - if (ifp->if_flags & IFF_RUNNING) - upl_stop(sc); - - rnd_detach_source(&sc->sc_rnd_source); - bpf_detach(ifp); - - if_detach(ifp); - -#ifdef DIAGNOSTIC - if (sc->sc_ep[UPL_ENDPT_TX] != NULL || - sc->sc_ep[UPL_ENDPT_RX] != NULL || - sc->sc_ep[UPL_ENDPT_INTR] != NULL) - aprint_debug_dev(self, "detach has active endpoints\n"); -#endif - - sc->sc_attached = 0; - splx(s); - - usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev); - - return 0; -} - -int -upl_activate(device_t self, enum devact act) -{ - struct upl_softc *sc = device_private(self); - - DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - switch (act) { - case DVACT_DEACTIVATE: - /* Deactivate the interface. */ - if_deactivate(&sc->sc_if); - sc->sc_dying = 1; - return 0; - default: - return EOPNOTSUPP; - } -} - -/* - * Initialize an RX descriptor and attach an MBUF cluster. - */ -Static int -upl_newbuf(struct upl_softc *sc, struct upl_chain *c, struct mbuf *m) -{ - struct mbuf *m_new = NULL; - - DPRINTFN(8,("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - if (m == NULL) { - MGETHDR(m_new, M_DONTWAIT, MT_DATA); - if (m_new == NULL) { - printf("%s: no memory for rx list " - "-- packet dropped!\n", device_xname(sc->sc_dev)); - return ENOBUFS; - } - - MCLGET(m_new, M_DONTWAIT); - if (!(m_new->m_flags & M_EXT)) { - printf("%s: no memory for rx list " - "-- packet dropped!\n", device_xname(sc->sc_dev)); - 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->upl_mbuf = m_new; - - return 0; -} - -Static int -upl_rx_list_init(struct upl_softc *sc) -{ - struct upl_cdata *cd; - struct upl_chain *c; - int i; - - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - cd = &sc->sc_cdata; - for (i = 0; i < UPL_RX_LIST_CNT; i++) { - c = &cd->upl_rx_chain[i]; - c->upl_sc = sc; - if (upl_newbuf(sc, c, NULL) == ENOBUFS) - return ENOBUFS; - if (c->upl_xfer == NULL) { - int error = usbd_create_xfer(sc->sc_ep[UPL_ENDPT_RX], - UPL_BUFSZ, 0, 0, &c->upl_xfer); - if (error) - return error; - c->upl_buf = usbd_get_buffer(c->upl_xfer); - } - } - - return 0; + usbnet_attach(un, "upldet"); + usbnet_attach_ifp(un, false, IFF_POINTOPOINT | IFF_NOARP | IFF_SIMPLEX, + 0, 0); } -Static int -upl_tx_list_init(struct upl_softc *sc) +static void +upl_rx_loop(struct usbnet * un, struct usbnet_chain *c, uint32_t total_len) { - struct upl_cdata *cd; - struct upl_chain *c; - int i; - - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); - - cd = &sc->sc_cdata; - for (i = 0; i < UPL_TX_LIST_CNT; i++) { - c = &cd->upl_tx_chain[i]; - c->upl_sc = sc; - c->upl_mbuf = NULL; - if (c->upl_xfer == NULL) { - int error = usbd_create_xfer(sc->sc_ep[UPL_ENDPT_TX], - UPL_BUFSZ, 0, 0, &c->upl_xfer); - if (error) - return error; - c->upl_buf = usbd_get_buffer(c->upl_xfer); - } - } - - return 0; -} - -/* - * A frame has been uploaded: pass the resulting mbuf chain up to - * the higher level protocols. - */ -Static void -upl_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status) -{ - struct upl_chain *c = priv; - struct upl_softc *sc = c->upl_sc; - struct ifnet *ifp = &sc->sc_if; - struct mbuf *m; - int total_len = 0; - int s; - - if (sc->sc_dying) - return; - - if (!(ifp->if_flags & IFF_RUNNING)) - return; - - if (status != USBD_NORMAL_COMPLETION) { - if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) - return; - sc->sc_rx_errs++; - if (usbd_ratecheck(&sc->sc_rx_notice)) { - printf("%s: %u usb errors on rx: %s\n", - device_xname(sc->sc_dev), sc->sc_rx_errs, - usbd_errstr(status)); - sc->sc_rx_errs = 0; - } - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async(sc->sc_ep[UPL_ENDPT_RX]); - goto done; - } - - usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); + usbnet_isowned_rx(un); DPRINTFN(9,("%s: %s: enter status=%d length=%d\n", - device_xname(sc->sc_dev), __func__, status, total_len)); - - m = c->upl_mbuf; - memcpy(mtod(c->upl_mbuf, char *), c->upl_buf, total_len); - - m->m_pkthdr.len = m->m_len = total_len; - - m_set_rcvif(m, ifp); - - s = splnet(); - - /* XXX ugly */ - if (upl_newbuf(sc, c, NULL) == ENOBUFS) { - ifp->if_ierrors++; - goto done1; - } - - DPRINTFN(10,("%s: %s: deliver %d\n", device_xname(sc->sc_dev), - __func__, m->m_len)); - - if_input((ifp), (m)); - - done1: - splx(s); - - done: -#if 1 - /* Setup new transfer. */ - usbd_setup_xfer(c->upl_xfer, c, c->upl_buf, UPL_BUFSZ, - USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, upl_rxeof); - usbd_transfer(c->upl_xfer); - - DPRINTFN(10,("%s: %s: start rx\n", device_xname(sc->sc_dev), - __func__)); -#endif -} - -/* - * A frame was downloaded to the chip. It's safe for us to clean up - * the list buffers. - */ -Static void -upl_txeof(struct usbd_xfer *xfer, void *priv, - usbd_status status) -{ - struct upl_chain *c = priv; - struct upl_softc *sc = c->upl_sc; - struct ifnet *ifp = &sc->sc_if; - int s; - - if (sc->sc_dying) - return; - - s = splnet(); - - DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->sc_dev), - __func__, status)); - - ifp->if_timer = 0; - ifp->if_flags &= ~IFF_OACTIVE; - - if (status != USBD_NORMAL_COMPLETION) { - if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { - splx(s); - return; - } - ifp->if_oerrors++; - printf("%s: usb error on tx: %s\n", device_xname(sc->sc_dev), - usbd_errstr(status)); - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async(sc->sc_ep[UPL_ENDPT_TX]); - splx(s); - return; - } - - ifp->if_opackets++; - - m_freem(c->upl_mbuf); - c->upl_mbuf = NULL; + device_xname(un->un_dev), __func__, status, total_len)); - if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) - upl_start(ifp); - - splx(s); + usbnet_input(un, c->unc_buf, total_len); } -Static int -upl_send(struct upl_softc *sc, struct mbuf *m, int idx) +static unsigned +upl_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) { - int total_len; - struct upl_chain *c; - usbd_status err; + int total_len; - c = &sc->sc_cdata.upl_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->upl_buf); - c->upl_mbuf = m; + if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz) + return 0; + m_copydata(m, 0, m->m_pkthdr.len, c->unc_buf); total_len = m->m_pkthdr.len; DPRINTFN(10,("%s: %s: total_len=%d\n", - device_xname(sc->sc_dev), __func__, total_len)); - - usbd_setup_xfer(c->upl_xfer, c, c->upl_buf, total_len, 0, - USBD_DEFAULT_TIMEOUT, upl_txeof); - - /* Transmit */ - err = usbd_transfer(c->upl_xfer); - if (err != USBD_IN_PROGRESS) { - printf("%s: upl_send error=%s\n", device_xname(sc->sc_dev), - usbd_errstr(err)); - upl_stop(sc); - return EIO; - } - - sc->sc_cdata.upl_tx_cnt++; + device_xname(un->un_dev), __func__, total_len)); - return 0; + return total_len; } -Static void -upl_start(struct ifnet *ifp) +static int +upl_init(struct ifnet *ifp) { - struct upl_softc *sc = ifp->if_softc; - struct mbuf *m_head = NULL; - - if (sc->sc_dying) - return; - - DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->sc_dev),__func__)); - - if (ifp->if_flags & IFF_OACTIVE) - return; - - IFQ_POLL(&ifp->if_snd, m_head); - if (m_head == NULL) - return; + struct usbnet * const un = ifp->if_softc; + int rv; - if (upl_send(sc, m_head, 0)) { - ifp->if_flags |= IFF_OACTIVE; - return; - } + usbnet_lock(un); + if (usbnet_isdying(un)) + rv = EIO; + else + rv = usbnet_init_rx_tx(un); + usbnet_unlock(un); - IFQ_DEQUEUE(&ifp->if_snd, m_head); - - /* - * If there's a BPF listener, bounce a copy of this frame - * to him. - */ - bpf_mtap(ifp, m_head, BPF_D_OUT); - - ifp->if_flags |= IFF_OACTIVE; - - /* - * Set a timeout in case the chip goes out to lunch. - */ - ifp->if_timer = 5; + return rv; } -Static void -upl_init(void *xsc) +static void +upl_intr_cb(struct usbnet *un, usbd_status status) { - struct upl_softc *sc = xsc; - struct ifnet *ifp = &sc->sc_if; - int s; - - if (sc->sc_dying) - return; - - DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->sc_dev),__func__)); - - if (ifp->if_flags & IFF_RUNNING) - return; - - s = splnet(); - - if (sc->sc_ep[UPL_ENDPT_RX] == NULL) { - if (upl_openpipes(sc)) { - splx(s); - return; - } - } - /* Init TX ring. */ - if (upl_tx_list_init(sc)) { - printf("%s: tx list init failed\n", device_xname(sc->sc_dev)); - splx(s); - return; - } - - /* Init RX ring. */ - if (upl_rx_list_init(sc)) { - printf("%s: rx list init failed\n", device_xname(sc->sc_dev)); - splx(s); - return; - } - - /* Start up the receive pipe. */ - for (int i = 0; i < UPL_RX_LIST_CNT; i++) { - struct upl_chain *c = &sc->sc_cdata.upl_rx_chain[i]; - usbd_setup_xfer(c->upl_xfer, c, c->upl_buf, UPL_BUFSZ, - USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, - upl_rxeof); - usbd_transfer(c->upl_xfer); - } - - ifp->if_flags |= IFF_RUNNING; - ifp->if_flags &= ~IFF_OACTIVE; - - splx(s); -} - -Static int -upl_openpipes(struct upl_softc *sc) -{ - usbd_status err; - - /* Open RX and TX pipes. */ - err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[UPL_ENDPT_RX], - USBD_EXCLUSIVE_USE, &sc->sc_ep[UPL_ENDPT_RX]); - if (err) { - printf("%s: open rx pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - return EIO; - } - err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[UPL_ENDPT_TX], - USBD_EXCLUSIVE_USE, &sc->sc_ep[UPL_ENDPT_TX]); - if (err) { - printf("%s: open tx pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - return EIO; - } - err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ed[UPL_ENDPT_INTR], - USBD_EXCLUSIVE_USE, &sc->sc_ep[UPL_ENDPT_INTR], sc, - &sc->sc_ibuf, UPL_INTR_PKTLEN, upl_intr, - UPL_INTR_INTERVAL); - if (err) { - printf("%s: open intr pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - return EIO; - } - - return 0; -} - -Static void -upl_intr(struct usbd_xfer *xfer, void *priv, - usbd_status status) -{ - struct upl_softc *sc = priv; - struct ifnet *ifp = &sc->sc_if; + struct upl_softc *sc = usbnet_softc(un); uByte stat; - DPRINTFN(15,("%s: %s: enter\n", device_xname(sc->sc_dev),__func__)); - - if (sc->sc_dying) - return; - - if (!(ifp->if_flags & IFF_RUNNING)) - return; - - if (status != USBD_NORMAL_COMPLETION) { - if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { - return; - } - sc->sc_intr_errs++; - if (usbd_ratecheck(&sc->sc_rx_notice)) { - printf("%s: %u usb errors on intr: %s\n", - device_xname(sc->sc_dev), sc->sc_rx_errs, - usbd_errstr(status)); - sc->sc_intr_errs = 0; - } - if (status == USBD_STALLED) - usbd_clear_endpoint_stall_async(sc->sc_ep[UPL_ENDPT_RX]); - return; - } - stat = sc->sc_ibuf; - if (stat == 0) return; - - DPRINTFN(10,("%s: %s: stat=0x%02x\n", device_xname(sc->sc_dev), + DPRINTFN(10,("%s: %s: stat=0x%02x\n", device_xname(un->un_dev), __func__, stat)); - } -Static int -upl_ioctl(struct ifnet *ifp, u_long command, void *data) +static int +upl_ioctl_cb(struct ifnet *ifp, u_long cmd, void *data) { - struct upl_softc *sc = ifp->if_softc; - struct ifaddr *ifa = (struct ifaddr *)data; - struct ifreq *ifr = (struct ifreq *)data; - int s, error = 0; - - if (sc->sc_dying) - return EIO; + if (cmd == SIOCSIFMTU) { + struct ifreq *ifr = data; - DPRINTFN(5,("%s: %s: cmd=0x%08lx\n", - device_xname(sc->sc_dev), __func__, command)); - - s = splnet(); - - switch(command) { - case SIOCINITIFADDR: - ifp->if_flags |= IFF_UP; - upl_init(sc); - - switch (ifa->ifa_addr->sa_family) { -#ifdef INET - case AF_INET: - break; -#endif /* INET */ - } - break; - - case SIOCSIFMTU: if (ifr->ifr_mtu > UPL_BUFSZ) - error = EINVAL; - else if ((error = ifioctl_common(ifp, command, data)) == ENETRESET) - error = 0; - break; - - case SIOCSIFFLAGS: - if ((error = ifioctl_common(ifp, command, data)) != 0) - break; - /* XXX re-use ether_ioctl() */ - switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) { - case IFF_UP: - upl_init(sc); - break; - case IFF_RUNNING: - upl_stop(sc); - break; - default: - break; - } - break; - default: - error = ifioctl_common(ifp, command, data); - break; + return EINVAL; } - - splx(s); - - return error; -} - -Static void -upl_watchdog(struct ifnet *ifp) -{ - struct upl_softc *sc = ifp->if_softc; - - DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->sc_dev),__func__)); - - if (sc->sc_dying) - return; - - ifp->if_oerrors++; - printf("%s: watchdog timeout\n", device_xname(sc->sc_dev)); - - upl_stop(sc); - upl_init(sc); - - if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) - upl_start(ifp); -} - -/* - * Stop the adapter and free any mbufs allocated to the - * RX and TX lists. - */ -Static void -upl_stop(struct upl_softc *sc) -{ - usbd_status err; - struct ifnet *ifp; - int i; - - DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->sc_dev),__func__)); - - ifp = &sc->sc_if; - ifp->if_timer = 0; - - /* Stop transfers. */ - if (sc->sc_ep[UPL_ENDPT_RX] != NULL) { - err = usbd_abort_pipe(sc->sc_ep[UPL_ENDPT_RX]); - if (err) { - printf("%s: abort rx pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - } - } - - if (sc->sc_ep[UPL_ENDPT_TX] != NULL) { - err = usbd_abort_pipe(sc->sc_ep[UPL_ENDPT_TX]); - if (err) { - printf("%s: abort tx pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - } - } - - if (sc->sc_ep[UPL_ENDPT_INTR] != NULL) { - err = usbd_abort_pipe(sc->sc_ep[UPL_ENDPT_INTR]); - if (err) { - printf("%s: abort intr pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - } - } - - /* Free RX resources. */ - for (i = 0; i < UPL_RX_LIST_CNT; i++) { - if (sc->sc_cdata.upl_rx_chain[i].upl_mbuf != NULL) { - m_freem(sc->sc_cdata.upl_rx_chain[i].upl_mbuf); - sc->sc_cdata.upl_rx_chain[i].upl_mbuf = NULL; - } - } - - /* Free TX resources. */ - for (i = 0; i < UPL_TX_LIST_CNT; i++) { - if (sc->sc_cdata.upl_tx_chain[i].upl_mbuf != NULL) { - m_freem(sc->sc_cdata.upl_tx_chain[i].upl_mbuf); - sc->sc_cdata.upl_tx_chain[i].upl_mbuf = NULL; - } - if (sc->sc_cdata.upl_tx_chain[i].upl_xfer != NULL) { - usbd_destroy_xfer(sc->sc_cdata.upl_tx_chain[i].upl_xfer); - sc->sc_cdata.upl_tx_chain[i].upl_xfer = NULL; - } - } - - /* Close pipes */ - if (sc->sc_ep[UPL_ENDPT_RX] != NULL) { - err = usbd_close_pipe(sc->sc_ep[UPL_ENDPT_RX]); - if (err) { - printf("%s: close rx pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - } - sc->sc_ep[UPL_ENDPT_RX] = NULL; - } - - if (sc->sc_ep[UPL_ENDPT_TX] != NULL) { - err = usbd_close_pipe(sc->sc_ep[UPL_ENDPT_TX]); - if (err) { - printf("%s: close tx pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - } - sc->sc_ep[UPL_ENDPT_TX] = NULL; - } - - if (sc->sc_ep[UPL_ENDPT_INTR] != NULL) { - err = usbd_close_pipe(sc->sc_ep[UPL_ENDPT_INTR]); - if (err) { - printf("%s: close intr pipe failed: %s\n", - device_xname(sc->sc_dev), usbd_errstr(err)); - } - sc->sc_ep[UPL_ENDPT_INTR] = NULL; - } - - ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); + return 0; } -Static int +static int upl_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, const struct rtentry *rt0) { - int error; + struct usbnet * const un __unused = ifp->if_softc; - DPRINTFN(10,("%s: %s: enter\n", - device_xname(((struct upl_softc *)ifp->if_softc)->sc_dev), - __func__)); + DPRINTFN(10,("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - /* - * if the queueing discipline needs packet classification, - * do it now. - */ + /* If the queueing discipline needs packet classification, do it now. */ IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family); /* * Queue message on interface, and start output if interface * not yet active. */ - error = if_transmit_lock(ifp, m); - - return error; + return if_transmit_lock(ifp, m); } -Static void +static void upl_input(struct ifnet *ifp, struct mbuf *m) { #ifdef INET @@ -1018,3 +341,9 @@ upl_input(struct ifnet *ifp, struct mbuf splx(s); #endif } + +#ifdef _MODULE +#include "ioconf.c" +#endif + +USBNET_MODULE(upl)