Module Name: src Committed By: mrg Date: Fri Aug 9 01:17:33 UTC 2019
Modified Files: src/sys/dev/usb: if_axe.c if_axen.c if_cdce.c if_smsc.c if_udav.c if_ure.c usbnet.c usbnet.h src/sys/sys: param.h Log Message: update usbnet some: - move rx/tx xfer flags into usbnet_cdata - move the callbacks into usbnet_ops structure - move rx/tx xfer flags arguments from usbnet_init_rx_tx() and move them all into usbnet_attach() arguments - s/miibus/mii/ in some places for consistency other clean up: - create wrapper functions for callbacks, move knowledge about special handling (OK to be missing, error eating) there. - use cdata pointer if already available - provide some more macros (will be real functions later) for accessing usbnet members, use existing ones more bump kernel version. To generate a diff of this commit: cvs rdiff -u -r1.106 -r1.107 src/sys/dev/usb/if_axe.c cvs rdiff -u -r1.56 -r1.57 src/sys/dev/usb/if_axen.c cvs rdiff -u -r1.58 -r1.59 src/sys/dev/usb/if_cdce.c cvs rdiff -u -r1.48 -r1.49 src/sys/dev/usb/if_smsc.c cvs rdiff -u -r1.62 -r1.63 src/sys/dev/usb/if_udav.c cvs rdiff -u -r1.18 -r1.19 src/sys/dev/usb/if_ure.c cvs rdiff -u -r1.9 -r1.10 src/sys/dev/usb/usbnet.c cvs rdiff -u -r1.6 -r1.7 src/sys/dev/usb/usbnet.h cvs rdiff -u -r1.603 -r1.604 src/sys/sys/param.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/sys/dev/usb/if_axe.c diff -u src/sys/dev/usb/if_axe.c:1.106 src/sys/dev/usb/if_axe.c:1.107 --- src/sys/dev/usb/if_axe.c:1.106 Tue Aug 6 01:42:22 2019 +++ src/sys/dev/usb/if_axe.c Fri Aug 9 01:17:33 2019 @@ -1,4 +1,4 @@ -/* $NetBSD: if_axe.c,v 1.106 2019/08/06 01:42:22 mrg Exp $ */ +/* $NetBSD: if_axe.c,v 1.107 2019/08/09 01:17:33 mrg Exp $ */ /* $OpenBSD: if_axe.c,v 1.137 2016/04/13 11:03:37 mpi Exp $ */ /* @@ -87,7 +87,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: if_axe.c,v 1.106 2019/08/06 01:42:22 mrg Exp $"); +__KERNEL_RCSID(0, "$NetBSD: if_axe.c,v 1.107 2019/08/09 01:17:33 mrg Exp $"); #ifdef _KERNEL_OPT #include "opt_usb.h" @@ -261,19 +261,33 @@ void axe_attach(device_t, device_t, void CFATTACH_DECL_NEW(axe, sizeof(struct axe_softc), axe_match, axe_attach, usbnet_detach, usbnet_activate); +static void axe_stop_cb(struct ifnet *, int); +static int axe_ioctl_cb(struct ifnet *, u_long, void *); +static int axe_init(struct ifnet *); +static usbd_status axe_mii_read_reg(struct usbnet *, int, int, uint16_t *); +static usbd_status axe_mii_write_reg(struct usbnet *, int, int, uint16_t); +static void axe_mii_statchg_cb(struct ifnet *); static void axe_rx_loop_cb(struct usbnet *, struct usbd_xfer *, struct usbnet_chain *, uint32_t); static unsigned axe_tx_prepare_cb(struct usbnet *, struct mbuf *, struct usbnet_chain *); -static int axe_init(struct ifnet *); -static void axe_stop_cb(struct ifnet *, int); -static int axe_ioctl_cb(struct ifnet *, u_long, void *); static void axe_ax88178_init(struct axe_softc *); static void axe_ax88772_init(struct axe_softc *); static void axe_ax88772a_init(struct axe_softc *); static void axe_ax88772b_init(struct axe_softc *); +static struct usbnet_ops axe_ops = { + .uno_stop = axe_stop_cb, + .uno_ioctl = axe_ioctl_cb, + .uno_read_reg = axe_mii_read_reg, + .uno_write_reg = axe_mii_write_reg, + .uno_statchg = axe_mii_statchg_cb, + .uno_tx_prepare = axe_tx_prepare_cb, + .uno_rx_loop = axe_rx_loop_cb, + .uno_init = axe_init, +}; + static usbd_status axe_cmd(struct axe_softc *sc, int cmd, int index, int val, void *buf) { @@ -284,7 +298,7 @@ axe_cmd(struct axe_softc *sc, int cmd, i usbnet_isowned_mii(un); - if (un->un_dying) + if (usbnet_isdying(un)) return -1; DPRINTFN(20, "cmd %#jx index %#jx val %#jx", cmd, index, val, 0); @@ -364,10 +378,10 @@ axe_mii_statchg_cb(struct ifnet *ifp) struct usbnet * const un = ifp->if_softc; struct axe_softc * const sc = usbnet_softc(un); - struct mii_data *mii = &un->un_mii; + struct mii_data *mii = usbnet_mii(un); int val, err; - if (un->un_dying) + if (usbnet_isdying(un)) return; val = 0; @@ -425,7 +439,7 @@ axe_setiff_locked(struct usbnet *un) usbnet_isowned_mii(un); - if (un->un_dying) + if (usbnet_isdying(un)) return; if (axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, &rxmode)) { @@ -516,7 +530,7 @@ axe_reset(struct usbnet *un) usbnet_isowned_mii(un); - if (un->un_dying) + if (usbnet_isdying(un)) return; /* @@ -871,16 +885,7 @@ axe_attach(device_t parent, device_t sel un->un_dev = self; un->un_udev = dev; un->un_sc = sc; - un->un_stop_cb = axe_stop_cb; - un->un_ioctl_cb = axe_ioctl_cb; - un->un_read_reg_cb = axe_mii_read_reg; - un->un_write_reg_cb = axe_mii_write_reg; - un->un_statchg_cb = axe_mii_statchg_cb; - un->un_tx_prepare_cb = axe_tx_prepare_cb; - un->un_rx_loop_cb = axe_rx_loop_cb; - un->un_init_cb = axe_init; - un->un_rx_xfer_flags = USBD_SHORT_XFER_OK; - un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER; + un->un_ops = &axe_ops; err = usbd_set_config_no(dev, AXE_CONFIG_NO, 1); if (err) { @@ -905,8 +910,6 @@ axe_attach(device_t parent, device_t sel AXE_178_MAX_BUFSZ : AXE_178_MIN_BUFSZ; else bufsz = AXE_172_BUFSZ; - un->un_cdata.uncd_rx_bufsz = bufsz; - un->un_cdata.uncd_tx_bufsz = bufsz; un->un_ed[USBNET_ENDPT_RX] = 0; un->un_ed[USBNET_ENDPT_TX] = 0; @@ -934,7 +937,8 @@ axe_attach(device_t parent, device_t sel } /* Set these up now for axe_cmd(). */ - usbnet_attach(un, "axedet", AXE_RX_LIST_CNT, AXE_TX_LIST_CNT); + usbnet_attach(un, "axedet", AXE_RX_LIST_CNT, AXE_TX_LIST_CNT, + USBD_SHORT_XFER_OK, USBD_FORCE_SHORT_XFER, bufsz, bufsz); /* We need the PHYID for init dance in some cases */ usbnet_lock_mii(un); @@ -1245,7 +1249,7 @@ axe_init_locked(struct ifnet *ifp) usbnet_isowned(un); - if (un->un_dying) + if (usbnet_isdying(un)) return EIO; /* Cancel pending I/O */ @@ -1342,7 +1346,7 @@ axe_init_locked(struct ifnet *ifp) usbnet_unlock_mii_un_locked(un); - return usbnet_init_rx_tx(un, 0, USBD_FORCE_SHORT_XFER); + return usbnet_init_rx_tx(un); } static int Index: src/sys/dev/usb/if_axen.c diff -u src/sys/dev/usb/if_axen.c:1.56 src/sys/dev/usb/if_axen.c:1.57 --- src/sys/dev/usb/if_axen.c:1.56 Tue Aug 6 01:42:22 2019 +++ src/sys/dev/usb/if_axen.c Fri Aug 9 01:17:33 2019 @@ -1,4 +1,4 @@ -/* $NetBSD: if_axen.c,v 1.56 2019/08/06 01:42:22 mrg Exp $ */ +/* $NetBSD: if_axen.c,v 1.57 2019/08/09 01:17:33 mrg Exp $ */ /* $OpenBSD: if_axen.c,v 1.3 2013/10/21 10:10:22 yuo Exp $ */ /* @@ -23,7 +23,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: if_axen.c,v 1.56 2019/08/06 01:42:22 mrg Exp $"); +__KERNEL_RCSID(0, "$NetBSD: if_axen.c,v 1.57 2019/08/09 01:17:33 mrg Exp $"); #ifdef _KERNEL_OPT #include "opt_usb.h" @@ -79,19 +79,32 @@ static void axen_attach(device_t, device CFATTACH_DECL_NEW(axen, sizeof(struct axen_softc), axen_match, axen_attach, usbnet_detach, usbnet_activate); -static unsigned axen_tx_prepare(struct usbnet *, struct mbuf *, - struct usbnet_chain *); -static int axen_init(struct ifnet *); static int axen_cmd(struct axen_softc *, int, int, int, void *); static void axen_reset(struct axen_softc *); static int axen_get_eaddr(struct axen_softc *, void *); -static void axen_stop_cb(struct ifnet *, int); static void axen_ax88179_init(struct axen_softc *); +static void axen_stop_cb(struct ifnet *, int); +static int axen_ioctl_cb(struct ifnet *, u_long, void *); static usbd_status axen_mii_read_reg(struct usbnet *, int, int, uint16_t *); static usbd_status axen_mii_write_reg(struct usbnet *, int, int, uint16_t); +static void axen_mii_statchg(struct ifnet *); static void axen_rxeof_loop(struct usbnet *, struct usbd_xfer *, struct usbnet_chain *, uint32_t); +static unsigned axen_tx_prepare(struct usbnet *, struct mbuf *, + struct usbnet_chain *); +static int axen_init(struct ifnet *); + +static struct usbnet_ops axen_ops = { + .uno_stop = axen_stop_cb, + .uno_ioctl = axen_ioctl_cb, + .uno_read_reg = axen_mii_read_reg, + .uno_write_reg = axen_mii_write_reg, + .uno_statchg = axen_mii_statchg, + .uno_tx_prepare = axen_tx_prepare, + .uno_rx_loop = axen_rxeof_loop, + .uno_init = axen_init, +}; static int axen_cmd(struct axen_softc *sc, int cmd, int index, int val, void *buf) @@ -102,7 +115,7 @@ axen_cmd(struct axen_softc *sc, int cmd, usbnet_isowned_mii(un); - if (un->un_dying) + if (usbnet_isdying(un)) return 0; if (AXEN_CMD_DIR(cmd)) @@ -162,7 +175,7 @@ axen_mii_statchg(struct ifnet *ifp) uint16_t val; uint16_t wval; - if (un->un_dying) + if (usbnet_isdying(un)) return; un->un_link = false; @@ -225,7 +238,7 @@ axen_setiff_locked(struct usbnet *un) uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; uint16_t wval; - if (un->un_dying) + if (usbnet_isdying(un)) return; usbnet_isowned_mii(un); @@ -295,7 +308,7 @@ axen_reset(struct axen_softc *sc) struct usbnet * const un = &sc->axen_un; usbnet_isowned(un); - if (un->un_dying) + if (usbnet_isdying(un)) return; /* XXX What to reset? */ @@ -501,18 +514,18 @@ axen_ax88179_init(struct axen_softc *sc) #define GMII_PHY_PAGE_SEL 0x1e #define GMII_PHY_PAGE_SEL 0x1f #define GMII_PAGE_EXT 0x0007 - usbnet_miibus_writereg(un->un_dev, un->un_phyno, GMII_PHY_PAGE_SEL, + usbnet_mii_writereg(un->un_dev, un->un_phyno, GMII_PHY_PAGE_SEL, GMII_PAGE_EXT); - usbnet_miibus_writereg(un->un_dev, un->un_phyno, GMII_PHY_PAGE, + usbnet_mii_writereg(un->un_dev, un->un_phyno, GMII_PHY_PAGE, 0x002c); #endif #if 1 /* XXX: phy hack ? */ - usbnet_miibus_writereg(un->un_dev, un->un_phyno, 0x1F, 0x0005); - usbnet_miibus_writereg(un->un_dev, un->un_phyno, 0x0C, 0x0000); - usbnet_miibus_readreg(un->un_dev, un->un_phyno, 0x0001, &wval); - usbnet_miibus_writereg(un->un_dev, un->un_phyno, 0x01, wval | 0x0080); - usbnet_miibus_writereg(un->un_dev, un->un_phyno, 0x1F, 0x0000); + usbnet_mii_writereg(un->un_dev, un->un_phyno, 0x1F, 0x0005); + usbnet_mii_writereg(un->un_dev, un->un_phyno, 0x0C, 0x0000); + usbnet_mii_readreg(un->un_dev, un->un_phyno, 0x0001, &wval); + usbnet_mii_writereg(un->un_dev, un->un_phyno, 0x01, wval | 0x0080); + usbnet_mii_writereg(un->un_dev, un->un_phyno, 0x1F, 0x0000); #endif } @@ -604,6 +617,7 @@ axen_attach(device_t parent, device_t se usb_interface_descriptor_t *id; usb_endpoint_descriptor_t *ed; char *devinfop; + unsigned rx_bufsz, tx_bufsz; uint16_t axen_flags; int i; @@ -619,16 +633,7 @@ axen_attach(device_t parent, device_t se un->un_dev = self; un->un_udev = dev; un->un_sc = sc; - un->un_stop_cb = axen_stop_cb; - un->un_ioctl_cb = axen_ioctl_cb; - un->un_read_reg_cb = axen_mii_read_reg; - un->un_write_reg_cb = axen_mii_write_reg; - un->un_statchg_cb = axen_mii_statchg; - un->un_tx_prepare_cb = axen_tx_prepare; - un->un_rx_loop_cb = axen_rxeof_loop; - un->un_init_cb = axen_init; - un->un_rx_xfer_flags = USBD_SHORT_XFER_OK; - un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER; + un->un_ops = &axen_ops; err = usbd_set_config_no(dev, AXEN_CONFIG_NO, 1); if (err) { @@ -648,19 +653,17 @@ axen_attach(device_t parent, device_t se /* decide on what our bufsize will be */ switch (dev->ud_speed) { case USB_SPEED_SUPER: - un->un_cdata.uncd_rx_bufsz = AXEN_BUFSZ_SS * 1024; + rx_bufsz = AXEN_BUFSZ_SS * 1024; break; case USB_SPEED_HIGH: - un->un_cdata.uncd_rx_bufsz = AXEN_BUFSZ_HS * 1024; + rx_bufsz = AXEN_BUFSZ_HS * 1024; break; default: - un->un_cdata.uncd_rx_bufsz = AXEN_BUFSZ_LS * 1024; + rx_bufsz = AXEN_BUFSZ_LS * 1024; break; } - - un->un_cdata.uncd_tx_bufsz = IP_MAXPACKET + - ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN + - sizeof(struct axen_sframe_hdr); + tx_bufsz = IP_MAXPACKET + ETHER_HDR_LEN + ETHER_CRC_LEN + + ETHER_VLAN_ENCAP_LEN + sizeof(struct axen_sframe_hdr); /* Find endpoints. */ id = usbd_get_interface_descriptor(un->un_iface); @@ -685,7 +688,9 @@ axen_attach(device_t parent, device_t se } /* Set these up now for axen_cmd(). */ - usbnet_attach(un, "axendet", AXEN_RX_LIST_CNT, AXEN_TX_LIST_CNT); + usbnet_attach(un, "axendet", AXEN_RX_LIST_CNT, AXEN_TX_LIST_CNT, + USBD_SHORT_XFER_OK, USBD_FORCE_SHORT_XFER, + rx_bufsz, tx_bufsz); un->un_phyno = AXEN_PHY_ID; DPRINTF(("%s: phyno %d\n", device_xname(self), un->un_phyno)); @@ -828,7 +833,7 @@ axen_rxeof_loop(struct usbnet *un, struc #endif if (pkt_count) - rnd_add_uint32(&un->un_rndsrc, pkt_count); + rnd_add_uint32(usbnet_rndsrc(un), pkt_count); do { if ((buf[0] != 0xee) || (buf[1] != 0xee)) { @@ -921,7 +926,7 @@ axen_init_locked(struct ifnet *ifp) usbnet_isowned(un); - if (un->un_dying) + if (usbnet_isdying(un)) return EIO; /* Cancel pending I/O */ @@ -951,7 +956,7 @@ axen_init_locked(struct ifnet *ifp) usbnet_unlock_mii_un_locked(un); - return usbnet_init_rx_tx(un, 0, USBD_FORCE_SHORT_XFER); + return usbnet_init_rx_tx(un); } static int Index: src/sys/dev/usb/if_cdce.c diff -u src/sys/dev/usb/if_cdce.c:1.58 src/sys/dev/usb/if_cdce.c:1.59 --- src/sys/dev/usb/if_cdce.c:1.58 Tue Aug 6 01:42:22 2019 +++ src/sys/dev/usb/if_cdce.c Fri Aug 9 01:17:33 2019 @@ -1,4 +1,4 @@ -/* $NetBSD: if_cdce.c,v 1.58 2019/08/06 01:42:22 mrg Exp $ */ +/* $NetBSD: if_cdce.c,v 1.59 2019/08/09 01:17:33 mrg Exp $ */ /* * Copyright (c) 1997, 1998, 1999, 2000-2003 Bill Paul <wp...@windriver.com> @@ -40,7 +40,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: if_cdce.c,v 1.58 2019/08/06 01:42:22 mrg Exp $"); +__KERNEL_RCSID(0, "$NetBSD: if_cdce.c,v 1.59 2019/08/09 01:17:33 mrg Exp $"); #include <sys/param.h> #include <sys/kernel.h> @@ -80,14 +80,21 @@ static const struct cdce_type cdce_devs[ static int cdce_match(device_t, cfdata_t, void *); static void cdce_attach(device_t, device_t, void *); -static int cdce_init(struct ifnet *); + +CFATTACH_DECL_NEW(cdce, sizeof(struct cdce_softc), cdce_match, cdce_attach, + usbnet_detach, usbnet_activate); + static void cdce_rx_loop(struct usbnet *, struct usbd_xfer *, struct usbnet_chain *, uint32_t); static unsigned cdce_tx_prepare(struct usbnet *, struct mbuf *, struct usbnet_chain *); +static int cdce_init(struct ifnet *); -CFATTACH_DECL_NEW(cdce, sizeof(struct cdce_softc), cdce_match, cdce_attach, - usbnet_detach, usbnet_activate); +static struct usbnet_ops cdce_ops = { + .uno_tx_prepare = cdce_tx_prepare, + .uno_rx_loop = cdce_rx_loop, + .uno_init = cdce_init, +}; static int cdce_match(device_t parent, cfdata_t match, void *aux) @@ -134,13 +141,7 @@ cdce_attach(device_t parent, device_t se un->un_dev = self; un->un_udev = dev; un->un_sc = sc; - un->un_init_cb = cdce_init; - un->un_tx_prepare_cb = cdce_tx_prepare; - un->un_rx_loop_cb = cdce_rx_loop; - un->un_rx_xfer_flags = USBD_SHORT_XFER_OK; - un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER; - un->un_cdata.uncd_rx_bufsz = CDCE_BUFSZ; - un->un_cdata.uncd_tx_bufsz = CDCE_BUFSZ; + un->un_ops = &cdce_ops; t = cdce_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product); if (t) @@ -250,7 +251,9 @@ cdce_attach(device_t parent, device_t se un->un_eaddr[5] = (uint8_t)(device_unit(un->un_dev)); } - usbnet_attach(un, "cdcedet", CDCE_RX_LIST_CNT, CDCE_TX_LIST_CNT); + usbnet_attach(un, "cdcedet", CDCE_RX_LIST_CNT, CDCE_TX_LIST_CNT, + USBD_SHORT_XFER_OK, USBD_FORCE_SHORT_XFER, + CDCE_BUFSZ, CDCE_BUFSZ); usbnet_attach_ifp(un, false, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST, 0, 0); } @@ -262,11 +265,11 @@ cdce_init(struct ifnet *ifp) int rv; usbnet_lock(un); - if (un->un_dying) + if (usbnet_isdying(un)) rv = EIO; else { usbnet_stop(un, ifp, 1); - rv = usbnet_init_rx_tx(un, 0, 0); + rv = usbnet_init_rx_tx(un); if (rv == 0) un->un_link = true; } Index: src/sys/dev/usb/if_smsc.c diff -u src/sys/dev/usb/if_smsc.c:1.48 src/sys/dev/usb/if_smsc.c:1.49 --- src/sys/dev/usb/if_smsc.c:1.48 Wed Aug 7 08:16:24 2019 +++ src/sys/dev/usb/if_smsc.c Fri Aug 9 01:17:33 2019 @@ -1,4 +1,4 @@ -/* $NetBSD: if_smsc.c,v 1.48 2019/08/07 08:16:24 mrg Exp $ */ +/* $NetBSD: if_smsc.c,v 1.49 2019/08/09 01:17:33 mrg Exp $ */ /* $OpenBSD: if_smsc.c,v 1.4 2012/09/27 12:38:11 jsg Exp $ */ /* $FreeBSD: src/sys/dev/usb/net/if_smsc.c,v 1.1 2012/08/15 04:03:55 gonzo Exp $ */ @@ -61,7 +61,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: if_smsc.c,v 1.48 2019/08/07 08:16:24 mrg Exp $"); +__KERNEL_RCSID(0, "$NetBSD: if_smsc.c,v 1.49 2019/08/09 01:17:33 mrg Exp $"); #ifdef _KERNEL_OPT #include "opt_usb.h" @@ -160,11 +160,23 @@ int smsc_wait_for_bits(struct usbnet * usbd_status smsc_miibus_readreg(struct usbnet *, int, int, uint16_t *); usbd_status smsc_miibus_writereg(struct usbnet *, int, int, uint16_t); +static int smsc_ioctl_cb(struct ifnet *, u_long, void *); static unsigned smsc_tx_prepare(struct usbnet *, struct mbuf *, struct usbnet_chain *); static void smsc_rxeof_loop(struct usbnet *, struct usbd_xfer *, struct usbnet_chain *, uint32_t); +static struct usbnet_ops smsc_ops = { + .uno_stop = smsc_stop_cb, + .uno_ioctl = smsc_ioctl_cb, + .uno_read_reg = smsc_miibus_readreg, + .uno_write_reg = smsc_miibus_writereg, + .uno_statchg = smsc_miibus_statchg, + .uno_tx_prepare = smsc_tx_prepare, + .uno_rx_loop = smsc_rxeof_loop, + .uno_init = smsc_init, +}; + int smsc_readreg(struct usbnet *un, uint32_t off, uint32_t *data) { @@ -174,7 +186,7 @@ smsc_readreg(struct usbnet *un, uint32_t usbnet_isowned_mii(un); - if (un->un_dying) + if (usbnet_isdying(un)) return 0; req.bmRequestType = UT_READ_VENDOR_DEVICE; @@ -201,7 +213,7 @@ smsc_writereg(struct usbnet *un, uint32_ usbnet_isowned_mii(un); - if (un->un_dying) + if (usbnet_isdying(un)) return 0; buf = htole32(data); @@ -299,7 +311,7 @@ smsc_miibus_statchg(struct ifnet *ifp) { struct usbnet * const un = ifp->if_softc; - if (un->un_dying) + if (usbnet_isdying(un)) return; struct smsc_softc * const sc = usbnet_softc(un); @@ -390,7 +402,7 @@ smsc_setiff_locked(struct usbnet *un) usbnet_isowned_mii(un); - if (un->un_dying) + if (usbnet_isdying(un)) return; if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) { @@ -521,7 +533,7 @@ smsc_reset(struct smsc_softc *sc) struct usbnet * const un = &sc->smsc_un; usbnet_isowned(un); - if (un->un_dying) + if (usbnet_isdying(un)) return; /* Wait a little while for the chip to get its brains in order. */ @@ -549,7 +561,7 @@ smsc_init_locked(struct ifnet *ifp) struct usbnet * const un = ifp->if_softc; struct smsc_softc * const sc = usbnet_softc(un); - if (un->un_dying) + if (usbnet_isdying(un)) return EIO; /* Cancel pending I/O */ @@ -568,7 +580,7 @@ smsc_init_locked(struct ifnet *ifp) usbnet_unlock_mii_un_locked(un); - return usbnet_init_rx_tx(un, 0, USBD_FORCE_SHORT_XFER);; + return usbnet_init_rx_tx(un); } void @@ -734,7 +746,6 @@ init_failed: return err; } - static int smsc_ioctl_cb(struct ifnet *ifp, u_long cmd, void *data) { @@ -776,6 +787,7 @@ smsc_attach(device_t parent, device_t se usb_interface_descriptor_t *id; usb_endpoint_descriptor_t *ed; char *devinfop; + unsigned bufsz; int err, i; uint32_t mac_h, mac_l; @@ -788,16 +800,7 @@ smsc_attach(device_t parent, device_t se un->un_dev = self; un->un_udev = dev; un->un_sc = sc; - un->un_stop_cb = smsc_stop_cb; - un->un_ioctl_cb = smsc_ioctl_cb; - un->un_read_reg_cb = smsc_miibus_readreg; - un->un_write_reg_cb = smsc_miibus_writereg; - un->un_statchg_cb = smsc_miibus_statchg; - un->un_tx_prepare_cb = smsc_tx_prepare; - un->un_rx_loop_cb = smsc_rxeof_loop; - un->un_init_cb = smsc_init; - un->un_rx_xfer_flags = USBD_SHORT_XFER_OK; - un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER; + un->un_ops = &smsc_ops; devinfop = usbd_devinfo_alloc(un->un_udev, 0); aprint_normal_dev(self, "%s\n", devinfop); @@ -820,11 +823,9 @@ smsc_attach(device_t parent, device_t se id = usbd_get_interface_descriptor(un->un_iface); if (dev->ud_speed >= USB_SPEED_HIGH) { - un->un_cdata.uncd_rx_bufsz = SMSC_MAX_BUFSZ; - un->un_cdata.uncd_tx_bufsz = SMSC_MAX_BUFSZ; + bufsz = SMSC_MAX_BUFSZ; } else { - un->un_cdata.uncd_rx_bufsz = SMSC_MIN_BUFSZ; - un->un_cdata.uncd_tx_bufsz = SMSC_MIN_BUFSZ; + bufsz = SMSC_MIN_BUFSZ; } /* Find endpoints. */ @@ -848,7 +849,8 @@ smsc_attach(device_t parent, device_t se } } - usbnet_attach(un, "smscdet", SMSC_RX_LIST_CNT, SMSC_TX_LIST_CNT); + usbnet_attach(un, "smscdet", SMSC_RX_LIST_CNT, SMSC_TX_LIST_CNT, + USBD_SHORT_XFER_OK, USBD_FORCE_SHORT_XFER, bufsz, bufsz); #ifdef notyet /* @@ -860,7 +862,8 @@ smsc_attach(device_t parent, device_t se /*IFCAP_CSUM_TCPv4_Tx |*/ IFCAP_CSUM_TCPv4_Rx | /*IFCAP_CSUM_UDPv4_Tx |*/ IFCAP_CSUM_UDPv4_Rx; #endif - un->un_ec.ec_capabilities = ETHERCAP_VLAN_MTU; + struct ethercom *ec = usbnet_ec(un); + ec->ec_capabilities = ETHERCAP_VLAN_MTU; /* Setup some of the basics */ un->un_phyno = 1; @@ -907,7 +910,7 @@ smsc_rxeof_loop(struct usbnet * un, stru struct ifnet *ifp = usbnet_ifp(un); uint8_t *buf = c->unc_buf; - KASSERT(mutex_owned(&un->un_rxlock)); + usbnet_isowned_rx(un); while (total_len != 0) { uint32_t rxhdr; @@ -1052,7 +1055,7 @@ smsc_tx_prepare(struct usbnet *un, struc uint32_t txhdr; uint32_t frm_len = 0; - KASSERT(mutex_owned(&un->un_txlock)); + usbnet_isowned_tx(un); /* * Each frame is prefixed with two 32-bit values describing the Index: src/sys/dev/usb/if_udav.c diff -u src/sys/dev/usb/if_udav.c:1.62 src/sys/dev/usb/if_udav.c:1.63 --- src/sys/dev/usb/if_udav.c:1.62 Wed Aug 7 20:34:12 2019 +++ src/sys/dev/usb/if_udav.c Fri Aug 9 01:17:33 2019 @@ -1,4 +1,4 @@ -/* $NetBSD: if_udav.c,v 1.62 2019/08/07 20:34:12 skrll Exp $ */ +/* $NetBSD: if_udav.c,v 1.63 2019/08/09 01:17:33 mrg Exp $ */ /* $nabe: if_udav.c,v 1.3 2003/08/21 16:57:19 nabe Exp $ */ /* @@ -45,7 +45,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: if_udav.c,v 1.62 2019/08/07 20:34:12 skrll Exp $"); +__KERNEL_RCSID(0, "$NetBSD: if_udav.c,v 1.63 2019/08/09 01:17:33 mrg Exp $"); #ifdef _KERNEL_OPT #include "opt_usb.h" @@ -140,6 +140,16 @@ static const struct udav_type { }; #define udav_lookup(v, p) ((const struct udav_type *)usb_lookup(udav_devs, v, p)) +static struct usbnet_ops udav_ops = { + .uno_stop = udav_stop_cb, + .uno_ioctl = udav_ioctl_cb, + .uno_read_reg = udav_mii_read_reg, + .uno_write_reg = udav_mii_write_reg, + .uno_statchg = udav_mii_statchg, + .uno_tx_prepare = udav_tx_prepare, + .uno_rx_loop = udav_rxeof_loop, + .uno_init = udav_init, +}; /* Probe */ int @@ -178,16 +188,7 @@ udav_attach(device_t parent, device_t se un->un_dev = self; un->un_udev = dev; un->un_sc = sc; - un->un_stop_cb = udav_stop_cb; - un->un_ioctl_cb = udav_ioctl_cb; - un->un_read_reg_cb = udav_mii_read_reg; - un->un_write_reg_cb = udav_mii_write_reg; - un->un_statchg_cb = udav_mii_statchg; - un->un_tx_prepare_cb = udav_tx_prepare; - un->un_rx_loop_cb = udav_rxeof_loop; - un->un_init_cb = udav_init; - un->un_rx_xfer_flags = USBD_SHORT_XFER_OK; - un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER; + un->un_ops = &udav_ops; /* Move the device into the configured state. */ err = usbd_set_config_no(dev, UDAV_CONFIG_NO, 1); /* idx 0 */ @@ -242,12 +243,12 @@ udav_attach(device_t parent, device_t se /* Not supported yet. */ un->un_ed[USBNET_ENDPT_INTR] = 0; - un->un_cdata.uncd_rx_bufsz = un->un_cdata.uncd_tx_bufsz = UDAV_BUFSZ; - // /* reset the adapter */ // udav_reset(un); - usbnet_attach(un, "udavdet", UDAV_RX_LIST_CNT, UDAV_TX_LIST_CNT); + usbnet_attach(un, "udavdet", UDAV_RX_LIST_CNT, UDAV_TX_LIST_CNT, + USBD_SHORT_XFER_OK, USBD_FORCE_SHORT_XFER, + UDAV_BUFSZ, UDAV_BUFSZ); /* Get Ethernet Address */ usbnet_lock_mii(un); @@ -283,7 +284,7 @@ udav_mem_read(struct udav_softc *sc, int DPRINTFN(0x200, ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - if (un->un_dying) + if (usbnet_isdying(un)) return 0; offset &= 0xffff; @@ -317,7 +318,7 @@ udav_mem_write(struct udav_softc *sc, in DPRINTFN(0x200, ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - if (un->un_dying) + if (usbnet_isdying(un)) return 0; offset &= 0xffff; @@ -351,7 +352,7 @@ udav_mem_write1(struct udav_softc *sc, i DPRINTFN(0x200, ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - if (un->un_dying) + if (usbnet_isdying(un)) return 0; offset &= 0xffff; @@ -381,7 +382,7 @@ udav_csr_read(struct udav_softc *sc, int usbd_status err; usbnet_isowned_mii(un); - KASSERT(!un->un_dying); + KASSERT(!usbnet_isdying(un)); DPRINTFN(0x200, ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); @@ -413,7 +414,7 @@ udav_csr_write(struct udav_softc *sc, in usbd_status err; usbnet_isowned_mii(un); - KASSERT(!un->un_dying); + KASSERT(!usbnet_isdying(un)); DPRINTFN(0x200, ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); @@ -447,7 +448,7 @@ udav_csr_read1(struct udav_softc *sc, in DPRINTFN(0x200, ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); - if (un->un_dying) + if (usbnet_isdying(un)) return 0; return udav_csr_read(sc, offset, &val, 1) ? 0 : val; @@ -462,7 +463,7 @@ udav_csr_write1(struct udav_softc *sc, i usbd_status err; usbnet_isowned_mii(un); - KASSERT(!un->un_dying); + KASSERT(!usbnet_isdying(un)); DPRINTFN(0x200, ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); @@ -497,7 +498,7 @@ udav_init_locked(struct ifnet *ifp) usbnet_isowned(un); - if (un->un_dying) + if (usbnet_isdying(un)) return EIO; /* Cancel pending I/O and free all TX/RX buffers */ @@ -543,7 +544,7 @@ udav_init_locked(struct ifnet *ifp) if (rc != 0) return rc; - return usbnet_init_rx_tx(un, 0, USBD_FORCE_SHORT_XFER); + return usbnet_init_rx_tx(un); } static int @@ -563,7 +564,7 @@ udav_reset(struct usbnet *un) { usbnet_isowned(un); - if (un->un_dying) + if (usbnet_isdying(un)) return; DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__)); @@ -625,7 +626,7 @@ udav_setiff_locked(struct usbnet *un) usbnet_isowned_mii(un); - if (un->un_dying) + if (usbnet_isdying(un)) return; if (ISSET(sc->sc_flags, UDAV_NO_PHY)) { @@ -799,7 +800,7 @@ udav_mii_read_reg(struct usbnet *un, int DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x\n", device_xname(un->un_dev), __func__, phy, reg)); - if (un->un_dying) { + if (usbnet_isdying(un)) { #ifdef DIAGNOSTIC printf("%s: %s: dying\n", device_xname(un->un_dev), __func__); @@ -848,7 +849,7 @@ udav_mii_write_reg(struct usbnet *un, in DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x val=0x%04hx\n", device_xname(un->un_dev), __func__, phy, reg, val)); - if (un->un_dying) { + if (usbnet_isdying(un)) { #ifdef DIAGNOSTIC printf("%s: %s: dying\n", device_xname(un->un_dev), __func__); @@ -891,7 +892,7 @@ udav_mii_statchg(struct ifnet *ifp) DPRINTF(("%s: %s: enter\n", ifp->if_xname, __func__)); - if (un->un_dying) + if (usbnet_isdying(un)) return; un->un_link = false; Index: src/sys/dev/usb/if_ure.c diff -u src/sys/dev/usb/if_ure.c:1.18 src/sys/dev/usb/if_ure.c:1.19 --- src/sys/dev/usb/if_ure.c:1.18 Tue Aug 6 01:42:22 2019 +++ src/sys/dev/usb/if_ure.c Fri Aug 9 01:17:33 2019 @@ -1,4 +1,4 @@ -/* $NetBSD: if_ure.c,v 1.18 2019/08/06 01:42:22 mrg Exp $ */ +/* $NetBSD: if_ure.c,v 1.19 2019/08/09 01:17:33 mrg Exp $ */ /* $OpenBSD: if_ure.c,v 1.10 2018/11/02 21:32:30 jcs Exp $ */ /*- @@ -30,7 +30,7 @@ /* RealTek RTL8152/RTL8153 10/100/Gigabit USB Ethernet device */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: if_ure.c,v 1.18 2019/08/06 01:42:22 mrg Exp $"); +__KERNEL_RCSID(0, "$NetBSD: if_ure.c,v 1.19 2019/08/09 01:17:33 mrg Exp $"); #ifdef _KERNEL_OPT #include "opt_usb.h" @@ -73,25 +73,44 @@ static const struct usb_devno ure_devs[] { USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_RTL8153 } }; -static int ure_match(device_t, cfdata_t, void *); -static void ure_attach(device_t, device_t, void *); -static int ure_init(struct ifnet *); +#define URE_BUFSZ (16 * 1024) + static void ure_reset(struct usbnet *); -static void ure_miibus_statchg(struct ifnet *); static uint32_t ure_txcsum(struct mbuf *); static int ure_rxcsum(struct ifnet *, struct ure_rxpkt *); -static unsigned ure_tx_prepare(struct usbnet *, struct mbuf *, - struct usbnet_chain *); -static void ure_rxeof_loop(struct usbnet *, struct usbd_xfer *, - struct usbnet_chain *, uint32_t); static void ure_rtl8152_init(struct ure_softc *); static void ure_rtl8153_init(struct ure_softc *); static void ure_disable_teredo(struct ure_softc *); static void ure_init_fifo(struct ure_softc *); +static void ure_stop_cb(struct ifnet *, int); +static int ure_ioctl_cb(struct ifnet *, u_long, void *); +static usbd_status ure_mii_read_reg(struct usbnet *, int, int, uint16_t *); +static usbd_status ure_mii_write_reg(struct usbnet *, int, int, uint16_t); +static void ure_miibus_statchg(struct ifnet *); +static unsigned ure_tx_prepare(struct usbnet *, struct mbuf *, + struct usbnet_chain *); +static void ure_rxeof_loop(struct usbnet *, struct usbd_xfer *, + struct usbnet_chain *, uint32_t); +static int ure_init(struct ifnet *); + +static int ure_match(device_t, cfdata_t, void *); +static void ure_attach(device_t, device_t, void *); + CFATTACH_DECL_NEW(ure, sizeof(struct ure_softc), ure_match, ure_attach, usbnet_detach, usbnet_activate); +static struct usbnet_ops ure_ops = { + .uno_stop = ure_stop_cb, + .uno_ioctl = ure_ioctl_cb, + .uno_read_reg = ure_mii_read_reg, + .uno_write_reg = ure_mii_write_reg, + .uno_statchg = ure_miibus_statchg, + .uno_tx_prepare = ure_tx_prepare, + .uno_rx_loop = ure_rxeof_loop, + .uno_init = ure_init, +}; + static int ure_ctl(struct usbnet *un, uint8_t rw, uint16_t val, uint16_t index, void *buf, int len) @@ -99,7 +118,7 @@ ure_ctl(struct usbnet *un, uint8_t rw, u usb_device_request_t req; usbd_status err; - if (un->un_dying) + if (usbnet_isdying(un)) return 0; if (rw == URE_CTL_WRITE) @@ -281,7 +300,7 @@ ure_miibus_statchg(struct ifnet *ifp) struct ure_softc * const sc = usbnet_softc(un); struct mii_data * const mii = usbnet_mii(un); - if (un->un_dying) + if (usbnet_isdying(un)) return; un->un_link = false; @@ -316,7 +335,7 @@ ure_setiff_locked(struct usbnet *un) usbnet_isowned(un); - if (un->un_dying) + if (usbnet_isdying(un)) return; rxmode = ure_read_4(un, URE_PLA_RCR, URE_MCU_TYPE_PLA); @@ -407,7 +426,7 @@ ure_init_locked(struct ifnet *ifp) usbnet_isowned(un); - if (un->un_dying) + if (usbnet_isdying(un)) return EIO; /* Cancel pending I/O. */ @@ -442,7 +461,7 @@ ure_init_locked(struct ifnet *ifp) /* Load the multicast filter. */ ure_setiff_locked(un); - return usbnet_init_rx_tx(un, 0, 0); + return usbnet_init_rx_tx(un); } static int @@ -842,16 +861,7 @@ ure_attach(device_t parent, device_t sel un->un_dev = self; un->un_udev = dev; un->un_sc = sc; - un->un_stop_cb = ure_stop_cb; - un->un_ioctl_cb = ure_ioctl_cb; - un->un_read_reg_cb = ure_mii_read_reg; - un->un_write_reg_cb = ure_mii_write_reg; - un->un_statchg_cb = ure_miibus_statchg; - un->un_tx_prepare_cb = ure_tx_prepare; - un->un_rx_loop_cb = ure_rxeof_loop; - un->un_init_cb = ure_init; - un->un_rx_xfer_flags = USBD_SHORT_XFER_OK; - un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER; + un->un_ops = &ure_ops; #define URE_CONFIG_NO 1 /* XXX */ error = usbd_set_config_no(dev, URE_CONFIG_NO, 1); @@ -872,8 +882,6 @@ ure_attach(device_t parent, device_t sel return; /* XXX */ } - un->un_cdata.uncd_rx_bufsz = un->un_cdata.uncd_tx_bufsz = 16 * 1024; - id = usbd_get_interface_descriptor(un->un_iface); for (i = 0; i < id->bNumEndpoints; i++) { ed = usbd_interface2endpoint_descriptor(un->un_iface, i); @@ -891,7 +899,9 @@ ure_attach(device_t parent, device_t sel } /* Set these up now for ure_ctl(). */ - usbnet_attach(un, "uredet", URE_RX_LIST_CNT, URE_TX_LIST_CNT); + usbnet_attach(un, "uredet", URE_RX_LIST_CNT, URE_TX_LIST_CNT, + USBD_SHORT_XFER_OK, USBD_FORCE_SHORT_XFER, + URE_BUFSZ, URE_BUFSZ); un->un_phyno = 0; @@ -1008,7 +1018,7 @@ ure_rxeof_loop(struct usbnet *un, struct } while (total_len > 0); if (pkt_count) - rnd_add_uint32(&un->un_rndsrc, pkt_count); + rnd_add_uint32(usbnet_rndsrc(un), pkt_count); } static int Index: src/sys/dev/usb/usbnet.c diff -u src/sys/dev/usb/usbnet.c:1.9 src/sys/dev/usb/usbnet.c:1.10 --- src/sys/dev/usb/usbnet.c:1.9 Wed Aug 7 10:01:05 2019 +++ src/sys/dev/usb/usbnet.c Fri Aug 9 01:17:33 2019 @@ -1,4 +1,4 @@ -/* $NetBSD: usbnet.c,v 1.9 2019/08/07 10:01:05 maya Exp $ */ +/* $NetBSD: usbnet.c,v 1.10 2019/08/09 01:17:33 mrg Exp $ */ /* * Copyright (c) 2019 Matthew R. Green @@ -33,7 +33,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: usbnet.c,v 1.9 2019/08/07 10:01:05 maya Exp $"); +__KERNEL_RCSID(0, "$NetBSD: usbnet.c,v 1.10 2019/08/09 01:17:33 mrg Exp $"); #include <sys/param.h> #include <sys/kernel.h> @@ -88,6 +88,73 @@ fail: #define USBNETHIST_FUNC() USBHIST_FUNC() #define USBNETHIST_CALLED(name) USBHIST_CALLED(usbnetdebug) +/* Callback vectors. */ + +static void +uno_stop(struct usbnet *un, struct ifnet *ifp, int disable) +{ + if (un->un_ops->uno_stop) + (*un->un_ops->uno_stop)(ifp, disable); +} + +static int +uno_ioctl(struct usbnet *un, struct ifnet *ifp, u_long cmd, void *data) +{ + if (un->un_ops->uno_ioctl) + return (*un->un_ops->uno_ioctl)(ifp, cmd, data); + return 0; +} + +static int +uno_override_ioctl(struct usbnet *un, struct ifnet *ifp, u_long cmd, void *data) +{ + return (*un->un_ops->uno_override_ioctl)(ifp, cmd, data); +} + +static int +uno_init(struct usbnet *un, struct ifnet *ifp) +{ + return (*un->un_ops->uno_init)(ifp); +} + +static int +uno_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val) +{ + return (*un->un_ops->uno_read_reg)(un, phy, reg, val); +} + +static int +uno_write_reg(struct usbnet *un, int phy, int reg, uint16_t val) +{ + return (*un->un_ops->uno_write_reg)(un, phy, reg, val); +} + +static void +uno_mii_statchg(struct usbnet *un, struct ifnet *ifp) +{ + (*un->un_ops->uno_statchg)(ifp); +} + +static unsigned +uno_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) +{ + return (*un->un_ops->uno_tx_prepare)(un, m, c); +} + +static void +uno_rx_loop(struct usbnet *un, struct usbd_xfer *xfer, + struct usbnet_chain *c, uint32_t total_len) +{ + (*un->un_ops->uno_rx_loop)(un, xfer, c, total_len); +} + +static void +uno_intr(struct usbnet *un, usbd_status status) +{ + if (un->un_ops->uno_intr) + (*un->un_ops->uno_intr)(un, status); +} + /* Interrupt handling. */ static struct mbuf * @@ -178,6 +245,7 @@ usbnet_rxeof(struct usbd_xfer *xfer, voi USBNETHIST_FUNC(); USBNETHIST_CALLED(); struct usbnet_chain *c = priv; struct usbnet * const un = c->unc_un; + struct usbnet_cdata *cd = &un->un_cdata; struct ifnet * const ifp = usbnet_ifp(un); uint32_t total_len; @@ -199,14 +267,14 @@ usbnet_rxeof(struct usbd_xfer *xfer, voi usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); - if (total_len > un->un_cdata.uncd_rx_bufsz) { + if (total_len > cd->uncd_rx_bufsz) { aprint_error_dev(un->un_dev, "rxeof: too large transfer (%u > %u)\n", - total_len, un->un_cdata.uncd_rx_bufsz); + total_len, cd->uncd_rx_bufsz); goto done; } - (*un->un_rx_loop_cb)(un, xfer, c, total_len); + uno_rx_loop(un, xfer, c, total_len); KASSERT(mutex_owned(&un->un_rxlock)); done: @@ -216,8 +284,8 @@ done: mutex_exit(&un->un_rxlock); /* Setup new transfer. */ - usbd_setup_xfer(xfer, c, c->unc_buf, un->un_cdata.uncd_rx_bufsz, - un->un_rx_xfer_flags, USBD_NO_TIMEOUT, usbnet_rxeof); + usbd_setup_xfer(xfer, c, c->unc_buf, cd->uncd_rx_bufsz, + cd->uncd_rx_xfer_flags, USBD_NO_TIMEOUT, usbnet_rxeof); usbd_transfer(xfer); return; @@ -293,8 +361,7 @@ usbnet_intr(struct usbd_xfer *xfer, void return; } - if (un->un_intr_cb) - (*un->un_intr_cb)(un, status); + uno_intr(un, status); } static void @@ -319,9 +386,9 @@ usbnet_start_locked(struct ifnet *ifp) if (m == NULL) break; - struct usbnet_chain *c = &un->un_cdata.uncd_tx_chain[idx]; + struct usbnet_chain *c = &cd->uncd_tx_chain[idx]; - length = (*un->un_tx_prepare_cb)(un, m, c); + length = uno_tx_prepare(un, m, c); if (length == 0) { ifp->if_oerrors++; break; @@ -333,7 +400,7 @@ usbnet_start_locked(struct ifnet *ifp) } usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, length, - un->un_tx_xfer_flags, 10000, usbnet_txeof); + cd->uncd_tx_xfer_flags, 10000, usbnet_txeof); /* Transmit */ usbd_status err = usbd_transfer(c->unc_xfer); @@ -388,11 +455,10 @@ usbnet_rx_list_size(struct usbnet_cdata } static void -usbnet_rx_list_alloc(struct usbnet *un, unsigned cnt) +usbnet_rx_list_alloc(struct usbnet *un) { struct usbnet_cdata *cd = &un->un_cdata; - cd->uncd_rx_list_cnt = cnt; cd->uncd_rx_chain = kmem_zalloc(usbnet_rx_list_size(cd), KM_SLEEP); } @@ -408,7 +474,7 @@ usbnet_rx_list_free(struct usbnet *un) } static int -usbnet_rx_list_init(struct usbnet *un, unsigned xfer_flags) +usbnet_rx_list_init(struct usbnet *un) { struct usbnet_cdata *cd = &un->un_cdata; @@ -418,7 +484,8 @@ usbnet_rx_list_init(struct usbnet *un, u c->unc_un = un; if (c->unc_xfer == NULL) { int err = usbd_create_xfer(un->un_ep[USBNET_ENDPT_RX], - cd->uncd_rx_bufsz, xfer_flags, 0, &c->unc_xfer); + cd->uncd_rx_bufsz, cd->uncd_rx_xfer_flags, 0, + &c->unc_xfer); if (err) return err; c->unc_buf = usbd_get_buffer(c->unc_xfer); @@ -459,7 +526,7 @@ usbnet_rx_start_pipes(struct usbnet *un, struct usbnet_chain *c = &cd->uncd_rx_chain[i]; usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, cd->uncd_rx_bufsz, - un->un_rx_xfer_flags, USBD_NO_TIMEOUT, cb); + cd->uncd_rx_xfer_flags, USBD_NO_TIMEOUT, cb); usbd_transfer(c->unc_xfer); } @@ -476,11 +543,10 @@ usbnet_tx_list_size(struct usbnet_cdata } static void -usbnet_tx_list_alloc(struct usbnet *un, unsigned cnt) +usbnet_tx_list_alloc(struct usbnet *un) { struct usbnet_cdata *cd = &un->un_cdata; - cd->uncd_tx_list_cnt = cnt; cd->uncd_tx_chain = kmem_zalloc(usbnet_tx_list_size(cd), KM_SLEEP); } @@ -496,7 +562,7 @@ usbnet_tx_list_free(struct usbnet *un) } static int -usbnet_tx_list_init(struct usbnet *un, unsigned xfer_flags) +usbnet_tx_list_init(struct usbnet *un) { struct usbnet_cdata *cd = &un->un_cdata; @@ -506,7 +572,8 @@ usbnet_tx_list_init(struct usbnet *un, u c->unc_un = un; if (c->unc_xfer == NULL) { int err = usbd_create_xfer(un->un_ep[USBNET_ENDPT_TX], - cd->uncd_tx_bufsz, xfer_flags, 0, &c->unc_xfer); + cd->uncd_tx_bufsz, cd->uncd_tx_xfer_flags, 0, + &c->unc_xfer); if (err) return err; c->unc_buf = usbd_get_buffer(c->unc_xfer); @@ -592,7 +659,7 @@ usbnet_ep_stop_pipes(struct usbnet *un) } int -usbnet_init_rx_tx(struct usbnet * const un, unsigned rxflags, unsigned txflags) +usbnet_init_rx_tx(struct usbnet * const un) { USBNETHIST_FUNC(); USBNETHIST_CALLED(); struct ifnet * const ifp = usbnet_ifp(un); @@ -616,14 +683,14 @@ usbnet_init_rx_tx(struct usbnet * const } /* Init RX ring. */ - if (usbnet_rx_list_init(un, rxflags)) { + if (usbnet_rx_list_init(un)) { aprint_error_dev(un->un_dev, "rx list init failed\n"); error = ENOBUFS; goto out; } /* Init TX ring. */ - if (usbnet_tx_list_init(un, txflags)) { + if (usbnet_tx_list_init(un)) { aprint_error_dev(un->un_dev, "tx list init failed\n"); error = ENOBUFS; goto out; @@ -700,7 +767,7 @@ usbnet_unlock_mii_un_locked(struct usbne } int -usbnet_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val) +usbnet_mii_readreg(device_t dev, int phy, int reg, uint16_t *val) { struct usbnet * const un = device_private(dev); usbd_status err; @@ -713,7 +780,7 @@ usbnet_miibus_readreg(device_t dev, int mutex_exit(&un->un_lock); usbnet_lock_mii(un); - err = (*un->un_read_reg_cb)(un, phy, reg, val); + err = uno_read_reg(un, phy, reg, val); usbnet_unlock_mii(un); if (err) { @@ -725,7 +792,7 @@ usbnet_miibus_readreg(device_t dev, int } int -usbnet_miibus_writereg(device_t dev, int phy, int reg, uint16_t val) +usbnet_mii_writereg(device_t dev, int phy, int reg, uint16_t val) { struct usbnet * const un = device_private(dev); usbd_status err; @@ -738,7 +805,7 @@ usbnet_miibus_writereg(device_t dev, int mutex_exit(&un->un_lock); usbnet_lock_mii(un); - err = (*un->un_write_reg_cb)(un, phy, reg, val); + err = uno_write_reg(un, phy, reg, val); usbnet_unlock_mii(un); if (err) { @@ -750,12 +817,12 @@ usbnet_miibus_writereg(device_t dev, int } void -usbnet_miibus_statchg(struct ifnet *ifp) +usbnet_mii_statchg(struct ifnet *ifp) { USBNETHIST_FUNC(); USBNETHIST_CALLED(); struct usbnet * const un = ifp->if_softc; - (*un->un_statchg_cb)(ifp); + uno_mii_statchg(un, ifp); } static int @@ -813,16 +880,12 @@ usbnet_ioctl(struct ifnet *ifp, u_long c struct usbnet * const un = ifp->if_softc; int error; - if (un->un_override_ioctl_cb) - return (*un->un_override_ioctl_cb)(ifp, cmd, data); + if (un->un_ops->uno_override_ioctl) + return uno_override_ioctl(un, ifp, cmd, data); error = ether_ioctl(ifp, cmd, data); - if (error == ENETRESET) { - if (un->un_ioctl_cb) - error = (*un->un_ioctl_cb)(ifp, cmd, data); - else - error = 0; - } + if (error == ENETRESET) + error = uno_ioctl(un, ifp, cmd, data); return error; } @@ -853,8 +916,7 @@ usbnet_stop(struct usbnet *un, struct if mutex_exit(&un->un_txlock); mutex_exit(&un->un_rxlock); - if (un->un_stop_cb) - (*un->un_stop_cb)(ifp, disable); + uno_stop(un, ifp, disable); /* * XXXSMP Would like to @@ -974,7 +1036,7 @@ usbnet_init(struct ifnet *ifp) USBNETHIST_FUNC(); USBNETHIST_CALLED(); struct usbnet * const un = ifp->if_softc; - return (*un->un_init_cb)(ifp); + return uno_init(un, ifp); } /* Autoconf management. */ @@ -1003,18 +1065,31 @@ void usbnet_attach(struct usbnet *un, const char *detname, /* detach cv name */ unsigned rx_list_cnt, /* size of rx chain list */ - unsigned tx_list_cnt) /* size of tx chain list */ + unsigned tx_list_cnt, /* size of tx chain list */ + unsigned rx_flags, /* flags for rx xfer */ + unsigned tx_flags, /* flags for tx xfer */ + unsigned rx_bufsz, /* size of rx buffers */ + unsigned tx_bufsz) /* size of tx buffers */ { USBNETHIST_FUNC(); USBNETHIST_CALLED(); + struct usbnet_cdata *cd = &un->un_cdata; - KASSERT(un->un_tx_prepare_cb); - KASSERT(un->un_rx_loop_cb); - KASSERT(un->un_init_cb); - KASSERT(un->un_cdata.uncd_rx_bufsz); - KASSERT(un->un_cdata.uncd_tx_bufsz); + /* Required inputs. */ + KASSERT(un->un_ops->uno_tx_prepare); + KASSERT(un->un_ops->uno_rx_loop); + KASSERT(un->un_ops->uno_init); + KASSERT(rx_bufsz); + KASSERT(tx_bufsz); KASSERT(rx_list_cnt); KASSERT(tx_list_cnt); + cd->uncd_rx_xfer_flags = rx_flags; + cd->uncd_tx_xfer_flags = tx_flags; + cd->uncd_rx_list_cnt = rx_list_cnt; + cd->uncd_tx_list_cnt = tx_list_cnt; + cd->uncd_rx_bufsz = rx_bufsz; + cd->uncd_tx_bufsz = tx_bufsz; + ether_set_ifflags_cb(&un->un_ec, usbnet_ifflags_cb); usb_init_task(&un->un_ticktask, usbnet_tick_task, un, USB_TASKQ_MPSAFE); @@ -1030,8 +1105,8 @@ usbnet_attach(struct usbnet *un, rnd_attach_source(&un->un_rndsrc, device_xname(un->un_dev), RND_TYPE_NET, RND_FLAG_DEFAULT); - usbnet_rx_list_alloc(un, rx_list_cnt); - usbnet_tx_list_alloc(un, tx_list_cnt); + usbnet_rx_list_alloc(un); + usbnet_tx_list_alloc(un); un->un_attached = true; } @@ -1043,10 +1118,14 @@ usbnet_attach_mii(struct usbnet *un, int struct mii_data * const mii = &un->un_mii; struct ifnet *ifp = usbnet_ifp(un); + KASSERT(un->un_ops->uno_read_reg); + KASSERT(un->un_ops->uno_write_reg); + KASSERT(un->un_ops->uno_statchg); + mii->mii_ifp = ifp; - mii->mii_readreg = usbnet_miibus_readreg; - mii->mii_writereg = usbnet_miibus_writereg; - mii->mii_statchg = usbnet_miibus_statchg; + mii->mii_readreg = usbnet_mii_readreg; + mii->mii_writereg = usbnet_mii_writereg; + mii->mii_statchg = usbnet_mii_statchg; mii->mii_flags = MIIF_AUTOTSLEEP; un->un_ec.ec_mii = mii; Index: src/sys/dev/usb/usbnet.h diff -u src/sys/dev/usb/usbnet.h:1.6 src/sys/dev/usb/usbnet.h:1.7 --- src/sys/dev/usb/usbnet.h:1.6 Wed Aug 7 07:25:50 2019 +++ src/sys/dev/usb/usbnet.h Fri Aug 9 01:17:33 2019 @@ -1,4 +1,4 @@ -/* $NetBSD: usbnet.h,v 1.6 2019/08/07 07:25:50 skrll Exp $ */ +/* $NetBSD: usbnet.h,v 1.7 2019/08/09 01:17:33 mrg Exp $ */ /* * Copyright (c) 2019 Matthew R. Green @@ -122,6 +122,9 @@ struct usbnet_cdata { unsigned uncd_rx_list_cnt; unsigned uncd_tx_list_cnt; + int uncd_rx_xfer_flags; + int uncd_tx_xfer_flags; + int uncd_tx_prod; int uncd_tx_cnt; int uncd_rx_cnt; @@ -163,6 +166,19 @@ typedef void (*usbnet_rx_loop_cb)(struct /* Interrupt pipe callback. */ typedef void (*usbnet_intr_cb)(struct usbnet *, usbd_status); +struct usbnet_ops { + usbnet_stop_cb uno_stop; + usbnet_ioctl_cb uno_ioctl; + usbnet_ioctl_cb uno_override_ioctl; + usbnet_init_cb uno_init; + usbnet_mii_read_reg_cb uno_read_reg; + usbnet_mii_write_reg_cb uno_write_reg; + usbnet_mii_statchg_cb uno_statchg; + usbnet_tx_prepare_cb uno_tx_prepare; + usbnet_rx_loop_cb uno_rx_loop; + usbnet_intr_cb uno_intr; +}; + /* * Generic USB ethernet structure. Use this as ifp->if_softc and * set as device_private() in attach. @@ -177,6 +193,7 @@ struct usbnet { struct usbd_interface *un_iface; struct usbd_device * un_udev; krndsource_t un_rndsrc; + struct usbnet_ops *un_ops; struct ethercom un_ec; struct mii_data un_mii; @@ -217,21 +234,6 @@ struct usbnet { struct timeval un_tx_notice; struct timeval un_intr_notice; - usbnet_stop_cb un_stop_cb; - usbnet_ioctl_cb un_ioctl_cb; - usbnet_ioctl_cb un_override_ioctl_cb; - usbnet_init_cb un_init_cb; - usbnet_mii_read_reg_cb un_read_reg_cb; - usbnet_mii_write_reg_cb un_write_reg_cb; - usbnet_mii_statchg_cb un_statchg_cb; - usbnet_tx_prepare_cb un_tx_prepare_cb; - usbnet_rx_loop_cb un_rx_loop_cb; - usbnet_intr_cb un_intr_cb; - - /* Passed to usbd_setup_xfer(). */ - int un_rx_xfer_flags; - int un_tx_xfer_flags; - /* * if un_intr_buf is not NULL, use usbd_open_pipe_intr() not * usbd_open_pipe() for USBNET_ENDPT_INTR, with this buffer, @@ -243,9 +245,11 @@ struct usbnet { }; #define usbnet_ifp(un) (&(un)->un_ec.ec_if) -#define usbnet_mii(un) (un->un_ec.ec_mii) #define usbnet_ec(un) (&(un)->un_ec) -#define usbnet_softc(un) (un->un_sc) +#define usbnet_rndsrc(un) (&(un)->un_rndsrc) +#define usbnet_mii(un) ((un)->un_ec.ec_mii) +#define usbnet_softc(un) ((un)->un_sc) +#define usbnet_isdying(un) ((un)->un_dying) /* * Endpoint / rx/tx chain management: @@ -259,7 +263,7 @@ struct usbnet { * Setup un_ed[] with valid end points before calling usbnet_init_rx_tx(). * Will return with un_ep[] initialised upon success. */ -int usbnet_init_rx_tx(struct usbnet * const, unsigned, unsigned); +int usbnet_init_rx_tx(struct usbnet * const); /* locking */ static __inline__ void @@ -328,9 +332,9 @@ usbnet_isowned_mii(struct usbnet *un) KASSERT(mutex_owned(&un->un_miilock)); } -int usbnet_miibus_readreg(device_t, int, int, uint16_t *); -int usbnet_miibus_writereg(device_t, int, int, uint16_t); -void usbnet_miibus_statchg(struct ifnet *); +int usbnet_mii_readreg(device_t, int, int, uint16_t *); +int usbnet_mii_writereg(device_t, int, int, uint16_t); +void usbnet_mii_statchg(struct ifnet *); /* interrupt handling */ void usbnet_enqueue(struct usbnet * const, uint8_t *, size_t, int, @@ -338,7 +342,8 @@ void usbnet_enqueue(struct usbnet * cons void usbnet_input(struct usbnet * const, uint8_t *, size_t); /* autoconf */ -void usbnet_attach(struct usbnet *un, const char *, unsigned, unsigned); +void usbnet_attach(struct usbnet *un, const char *, unsigned, unsigned, + unsigned, unsigned, unsigned, unsigned); void usbnet_attach_ifp(struct usbnet *, bool, unsigned, unsigned, int); int usbnet_detach(device_t, int); int usbnet_activate(device_t, devact_t); Index: src/sys/sys/param.h diff -u src/sys/sys/param.h:1.603 src/sys/sys/param.h:1.604 --- src/sys/sys/param.h:1.603 Wed Aug 7 00:39:23 2019 +++ src/sys/sys/param.h Fri Aug 9 01:17:33 2019 @@ -1,4 +1,4 @@ -/* $NetBSD: param.h,v 1.603 2019/08/07 00:39:23 pgoyette Exp $ */ +/* $NetBSD: param.h,v 1.604 2019/08/09 01:17:33 mrg Exp $ */ /*- * Copyright (c) 1982, 1986, 1989, 1993 @@ -67,7 +67,7 @@ * 2.99.9 (299000900) */ -#define __NetBSD_Version__ 999000400 /* NetBSD 9.99.4 */ +#define __NetBSD_Version__ 999000500 /* NetBSD 9.99.5 */ #define __NetBSD_Prereq__(M,m,p) (((((M) * 100000000) + \ (m) * 1000000) + (p) * 100) <= __NetBSD_Version__)