Home | History | Annotate | Line # | Download | only in usb
usbnet.h revision 1.12
      1  1.12    mrg /*	$NetBSD: usbnet.h,v 1.12 2019/08/15 05:52:23 mrg Exp $	*/
      2   1.1    mrg 
      3   1.1    mrg /*
      4   1.1    mrg  * Copyright (c) 2019 Matthew R. Green
      5   1.1    mrg  * All rights reserved.
      6   1.1    mrg  *
      7   1.1    mrg  * Redistribution and use in source and binary forms, with or without
      8   1.1    mrg  * modification, are permitted provided that the following conditions
      9   1.1    mrg  * are met:
     10   1.1    mrg  * 1. Redistributions of source code must retain the above copyright
     11   1.1    mrg  *    notice, this list of conditions and the following disclaimer.
     12   1.1    mrg  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1    mrg  *    notice, this list of conditions and the following disclaimer in the
     14   1.1    mrg  *    documentation and/or other materials provided with the distribution.
     15   1.1    mrg  * 3. The name of the author may not be used to endorse or promote products
     16   1.1    mrg  *    derived from this software without specific prior written permission.
     17   1.1    mrg  *
     18   1.1    mrg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19   1.1    mrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20   1.1    mrg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21   1.1    mrg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22   1.1    mrg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23   1.1    mrg  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24   1.1    mrg  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25   1.1    mrg  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26   1.1    mrg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27   1.1    mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28   1.1    mrg  * SUCH DAMAGE.
     29   1.1    mrg  */
     30   1.1    mrg 
     31   1.1    mrg #ifndef _DEV_USB_USBNET_H
     32   1.1    mrg #define _DEV_USB_USBNET_H
     33   1.1    mrg 
     34   1.1    mrg /*
     35   1.1    mrg  * Common code/data shared by all USB ethernet drivers (using these routines.)
     36   1.1    mrg  *
     37   1.1    mrg  * This framework provides the following features for USB ethernet drivers:
     38   1.1    mrg  *
     39   1.1    mrg  * - USB endpoint pipe handling
     40   1.1    mrg  * - rx and tx chain handling
     41   1.1    mrg  * - generic handlers or support for several struct ifnet callbacks
     42   1.1    mrg  * - MII bus locking
     43   1.1    mrg  * - interrupt handling
     44   1.1    mrg  * - partial autoconf handling
     45   1.1    mrg  *
     46   1.1    mrg  * Consumers of this interface need to:
     47   1.1    mrg  *
     48   1.1    mrg  * - replace most softc members with "struct usbnet" usage, in particular
     49   1.1    mrg  *   use usbnet pointer for ifp->if_softc, and device_private (real softc
     50   1.1    mrg  *   can be stored in un_sc member)
     51   1.1    mrg  * - use MII bus lock / access methods
     52   1.1    mrg  * - usbnet_attach() to initialise and allocate rx/tx chains
     53   1.5    mrg  * - usbnet_attach_ifp() to attach the interface, and either ether_ifattach()
     54   1.5    mrg  *   for ethernet devices, or if_alloc_sadl()/bpf_attach() pair otherwise.
     55   1.1    mrg  * - usbnet_detach() to clean them up
     56   1.1    mrg  * - usbnet_activate() for autoconf
     57   1.1    mrg  * - interface ioctl and start have direct frontends with callbacks for
     58   1.1    mrg  *   device specific handling:
     59   1.6  skrll  *   - ioctl can use either a device-specific override (useful for special
     60   1.5    mrg  *     cases), but provides a normal handler with callback to handle
     61   1.5    mrg  *     ENETRESET conditions that should be sufficient for most users
     62   1.1    mrg  *   - start uses usbnet send callback
     63   1.1    mrg  * - interface init and stop have helper functions
     64   1.1    mrg  *   - device specific init should use usbnet_init_rx_tx() to open pipes
     65   1.1    mrg  *     to the device and setup the rx/tx chains for use after any device
     66   1.1    mrg  *     specific setup
     67   1.1    mrg  *   - usbnet_stop() must be called with the un_lock held, and will
     68   1.1    mrg  *     call the device-specific usbnet stop callback, which enables the
     69   1.1    mrg  *     standard init calls stop idiom.
     70   1.1    mrg  * - interrupt handling:
     71   1.1    mrg  *   - for rx, usbnet_init_rx_tx() will enable the receive pipes and
     72   1.1    mrg  *     call the rx_loop callback to handle device specific processing of
     73   1.1    mrg  *     packets, which can use usbnet_enqueue() to provide data to the
     74   1.1    mrg  *     higher layers
     75   1.1    mrg  *   - for tx, usbnet_start (if_start) will pull entries out of the
     76   1.1    mrg  *     transmit queue and use the send callback for the given mbuf.
     77   1.1    mrg  *     the usb callback will use usbnet_txeof() for the transmit
     78   1.1    mrg  *     completion function (internal to usbnet)
     79  1.12    mrg  *   - there is special interrupt pipe handling
     80  1.12    mrg  * - timer/tick:
     81  1.12    mrg  *   - the uno_tick callback will be called once a second if present.
     82   1.1    mrg  */
     83   1.1    mrg 
     84   1.1    mrg #include <sys/device.h>
     85   1.1    mrg #include <sys/mbuf.h>
     86   1.1    mrg #include <sys/rndsource.h>
     87   1.2    mrg #include <sys/mutex.h>
     88  1.11    mrg #include <sys/module.h>
     89   1.1    mrg 
     90   1.1    mrg #include <net/bpf.h>
     91   1.1    mrg #include <net/if.h>
     92   1.1    mrg #include <net/if_arp.h>
     93   1.3  skrll #include <net/if_dl.h>
     94   1.3  skrll #include <net/if_ether.h>
     95   1.1    mrg #include <net/if_media.h>
     96   1.1    mrg 
     97   1.1    mrg #include <dev/mii/mii.h>
     98   1.1    mrg #include <dev/mii/miivar.h>
     99   1.1    mrg 
    100   1.1    mrg #include <dev/usb/usb.h>
    101   1.1    mrg #include <dev/usb/usbdi.h>
    102   1.1    mrg #include <dev/usb/usbdivar.h>
    103   1.1    mrg #include <dev/usb/usbdi_util.h>
    104   1.1    mrg #include <dev/usb/usbdevs.h>
    105   1.1    mrg 
    106   1.1    mrg /*
    107  1.10    mrg  * Per-transfer data.
    108   1.1    mrg  *
    109  1.10    mrg  * Front-end must set un_rx_list_cnt and un_tx_list_cnt before
    110  1.10    mrg  * calling usbnet_attach(), and then call usbnet_rx_tx_init()
    111  1.10    mrg  * which will allocate the chain arrays, and must be NULL to
    112   1.4    mrg  * indicate the first call.
    113   1.1    mrg  */
    114   1.1    mrg struct usbnet;
    115   1.1    mrg struct usbnet_chain {
    116   1.1    mrg 	struct usbnet		*unc_un;
    117   1.1    mrg 	struct usbd_xfer	*unc_xfer;
    118   1.1    mrg 	uint8_t			*unc_buf;
    119   1.1    mrg };
    120   1.1    mrg 
    121   1.1    mrg /*
    122   1.1    mrg  * Extend this as necessary.  axe(4) claims to want 6 total, but
    123   1.1    mrg  * does not implement them.
    124   1.1    mrg  */
    125   1.1    mrg enum usbnet_ep {
    126   1.1    mrg 	USBNET_ENDPT_RX,
    127   1.1    mrg 	USBNET_ENDPT_TX,
    128   1.1    mrg 	USBNET_ENDPT_INTR,
    129   1.1    mrg 	USBNET_ENDPT_MAX,
    130   1.1    mrg };
    131   1.1    mrg 
    132   1.1    mrg /* Interface stop callback. */
    133   1.1    mrg typedef void (*usbnet_stop_cb)(struct ifnet *, int);
    134   1.1    mrg /* Interface ioctl callback. */
    135   1.1    mrg typedef int (*usbnet_ioctl_cb)(struct ifnet *, u_long, void *);
    136   1.1    mrg /* Initialise device callback. */
    137   1.1    mrg typedef int (*usbnet_init_cb)(struct ifnet *);
    138   1.4    mrg 
    139   1.1    mrg /* MII read register callback. */
    140   1.1    mrg typedef usbd_status (*usbnet_mii_read_reg_cb)(struct usbnet *, int reg,
    141   1.1    mrg 					      int phy, uint16_t *val);
    142   1.1    mrg /* MII write register callback. */
    143   1.1    mrg typedef usbd_status (*usbnet_mii_write_reg_cb)(struct usbnet *, int reg,
    144   1.1    mrg 					       int phy, uint16_t val);
    145   1.1    mrg /* MII status change callback. */
    146   1.1    mrg typedef void (*usbnet_mii_statchg_cb)(struct ifnet *);
    147   1.4    mrg 
    148   1.1    mrg /* Prepare packet to send callback, returns length. */
    149   1.1    mrg typedef unsigned (*usbnet_tx_prepare_cb)(struct usbnet *, struct mbuf *,
    150   1.1    mrg 					 struct usbnet_chain *);
    151   1.1    mrg /* Receive some packets callback. */
    152  1.12    mrg typedef void (*usbnet_rx_loop_cb)(struct usbnet *, struct usbnet_chain *,
    153  1.12    mrg 				  uint32_t);
    154  1.12    mrg /* Tick callback. */
    155  1.12    mrg typedef void (*usbnet_tick_cb)(struct usbnet *);
    156   1.4    mrg /* Interrupt pipe callback. */
    157   1.4    mrg typedef void (*usbnet_intr_cb)(struct usbnet *, usbd_status);
    158   1.1    mrg 
    159   1.7    mrg struct usbnet_ops {
    160   1.7    mrg 	usbnet_stop_cb		uno_stop;
    161   1.7    mrg 	usbnet_ioctl_cb		uno_ioctl;
    162   1.7    mrg 	usbnet_ioctl_cb		uno_override_ioctl;
    163   1.7    mrg 	usbnet_init_cb		uno_init;
    164   1.7    mrg 	usbnet_mii_read_reg_cb	uno_read_reg;
    165   1.7    mrg 	usbnet_mii_write_reg_cb uno_write_reg;
    166   1.7    mrg 	usbnet_mii_statchg_cb	uno_statchg;
    167   1.7    mrg 	usbnet_tx_prepare_cb	uno_tx_prepare;
    168   1.7    mrg 	usbnet_rx_loop_cb	uno_rx_loop;
    169  1.12    mrg 	usbnet_tick_cb		uno_tick;
    170   1.7    mrg 	usbnet_intr_cb		uno_intr;
    171   1.7    mrg };
    172   1.7    mrg 
    173   1.1    mrg /*
    174   1.9    mrg  * USB interrupt pipe support.  Use this if usbd_open_pipe_intr() should
    175   1.9    mrg  * be used for the interrupt pipe.
    176   1.9    mrg  */
    177   1.9    mrg struct usbnet_intr {
    178   1.9    mrg 	/*
    179   1.9    mrg 	 * Point un_intr to this structure to use usbd_open_pipe_intr() not
    180   1.9    mrg 	 * usbd_open_pipe() for USBNET_ENDPT_INTR, with this buffer, size,
    181   1.9    mrg 	 * and interval.
    182   1.9    mrg 	 */
    183   1.9    mrg 	void			*uni_buf;
    184   1.9    mrg 	unsigned		uni_bufsz;
    185   1.9    mrg 	unsigned		uni_interval;
    186   1.9    mrg };
    187   1.9    mrg 
    188   1.9    mrg /*
    189   1.9    mrg  * Generic USB ethernet structure.  Use this as ifp->if_softc and set as
    190   1.9    mrg  * device_private() in attach unless already using struct usbnet here.
    191   1.4    mrg  *
    192   1.4    mrg  * Devices without MII should call usbnet_attach_ifp() with have_mii set
    193   1.4    mrg  * to true, and should ensure that the un_statchg_cb callback sets the
    194   1.4    mrg  * un_link member.  Devices without MII have this forced to true.
    195   1.1    mrg  */
    196   1.9    mrg struct usbnet_private;
    197   1.1    mrg struct usbnet {
    198   1.9    mrg 	/*
    199   1.9    mrg 	 * This section should be filled in before calling
    200   1.9    mrg 	 * usbnet_attach().
    201   1.9    mrg 	 */
    202   1.1    mrg 	void			*un_sc;			/* real softc */
    203   1.1    mrg 	device_t		un_dev;
    204   1.1    mrg 	struct usbd_interface	*un_iface;
    205   1.9    mrg 	struct usbd_device	*un_udev;
    206   1.7    mrg 	struct usbnet_ops	*un_ops;
    207   1.9    mrg 	struct usbnet_intr	*un_intr;
    208   1.1    mrg 
    209   1.9    mrg 	/* Inputs for rx/tx chain control. */
    210   1.9    mrg 	unsigned		un_rx_bufsz;
    211   1.9    mrg 	unsigned		un_tx_bufsz;
    212   1.9    mrg 	unsigned		un_rx_list_cnt;
    213   1.9    mrg 	unsigned		un_tx_list_cnt;
    214   1.9    mrg 	int			un_rx_xfer_flags;
    215   1.9    mrg 	int			un_tx_xfer_flags;
    216   1.1    mrg 
    217   1.9    mrg 	/*
    218   1.9    mrg 	 * This section should be filled in before calling
    219   1.9    mrg 	 * usbnet_attach_ifp().
    220   1.9    mrg 	 */
    221   1.1    mrg 	enum usbnet_ep		un_ed[USBNET_ENDPT_MAX];
    222   1.1    mrg 
    223   1.9    mrg 	/* MII specific. Not used without MII. */
    224   1.9    mrg 	int			un_phyno;
    225   1.9    mrg 	/* Ethernet specific. All zeroes indicates non-Ethernet. */
    226   1.9    mrg 	uint8_t			un_eaddr[ETHER_ADDR_LEN];
    227   1.8    mrg 
    228   1.1    mrg 	/*
    229   1.9    mrg 	 * This section is for driver to use, not touched by usbnet.
    230   1.1    mrg 	 */
    231   1.9    mrg 	unsigned		un_flags;
    232   1.1    mrg 
    233   1.4    mrg 	/*
    234   1.9    mrg 	 * This section is private to usbnet. Don't touch.
    235   1.4    mrg 	 */
    236   1.9    mrg 	struct usbnet_private	*un_pri;
    237   1.1    mrg };
    238   1.1    mrg 
    239   1.9    mrg /* Various accessors. */
    240   1.1    mrg 
    241   1.9    mrg void usbnet_set_link(struct usbnet *, bool);
    242  1.11    mrg void usbnet_set_dying(struct usbnet *, bool);
    243   1.9    mrg 
    244   1.9    mrg struct ifnet *usbnet_ifp(struct usbnet *);
    245   1.9    mrg struct ethercom *usbnet_ec(struct usbnet *);
    246   1.9    mrg struct mii_data *usbnet_mii(struct usbnet *);
    247   1.9    mrg krndsource_t *usbnet_rndsrc(struct usbnet *);
    248   1.9    mrg void *usbnet_softc(struct usbnet *);
    249   1.1    mrg 
    250   1.9    mrg bool usbnet_havelink(struct usbnet *);
    251   1.9    mrg bool usbnet_isdying(struct usbnet *);
    252   1.4    mrg 
    253   1.4    mrg 
    254   1.9    mrg /*
    255   1.9    mrg  * Locking.  Note that the isowned() are implemented here so that
    256   1.9    mrg  * empty-KASSERT() causes them to be elided for non-DIAG builds.
    257   1.9    mrg  */
    258   1.9    mrg void	usbnet_lock(struct usbnet *);
    259   1.9    mrg void	usbnet_unlock(struct usbnet *);
    260   1.9    mrg kmutex_t *usbnet_mutex(struct usbnet *);
    261   1.4    mrg static __inline__ void
    262   1.4    mrg usbnet_isowned(struct usbnet *un)
    263   1.4    mrg {
    264   1.9    mrg 	KASSERT(mutex_owned(usbnet_mutex(un)));
    265   1.4    mrg }
    266   1.4    mrg 
    267   1.9    mrg void	usbnet_lock_rx(struct usbnet *);
    268   1.9    mrg void	usbnet_unlock_rx(struct usbnet *);
    269   1.9    mrg kmutex_t *usbnet_mutex_rx(struct usbnet *);
    270   1.4    mrg static __inline__ void
    271   1.4    mrg usbnet_isowned_rx(struct usbnet *un)
    272   1.4    mrg {
    273   1.9    mrg 	KASSERT(mutex_owned(usbnet_mutex_rx(un)));
    274   1.4    mrg }
    275   1.4    mrg 
    276   1.9    mrg void	usbnet_lock_tx(struct usbnet *);
    277   1.9    mrg void	usbnet_unlock_tx(struct usbnet *);
    278   1.9    mrg kmutex_t *usbnet_mutex_tx(struct usbnet *);
    279   1.4    mrg static __inline__ void
    280   1.9    mrg usbnet_isowned_tx(struct usbnet *un)
    281   1.4    mrg {
    282   1.9    mrg 	KASSERT(mutex_owned(usbnet_mutex_tx(un)));
    283   1.4    mrg }
    284   1.4    mrg 
    285   1.9    mrg /*
    286   1.9    mrg  * Endpoint / rx/tx chain management:
    287   1.9    mrg  *
    288  1.10    mrg  * usbnet_attach() initialises usbnet and allocates rx and tx chains
    289   1.9    mrg  * usbnet_init_rx_tx() open pipes, initialises the rx/tx chains for use
    290   1.9    mrg  * usbnet_stop() stops pipes, cleans (not frees) rx/tx chains, locked
    291   1.9    mrg  *               version assumes un_lock is held
    292   1.9    mrg  * usbnet_detach() frees the rx/tx chains
    293   1.9    mrg  *
    294   1.9    mrg  * Setup un_ed[] with valid end points before calling usbnet_attach().
    295   1.9    mrg  * Call usbnet_init_rx_tx() to initialise pipes, which will be open
    296   1.9    mrg  * upon success.
    297   1.9    mrg  */
    298   1.9    mrg int	usbnet_init_rx_tx(struct usbnet * const);
    299   1.4    mrg 
    300   1.9    mrg /* MII. */
    301   1.1    mrg void	usbnet_lock_mii(struct usbnet *);
    302   1.1    mrg void	usbnet_lock_mii_un_locked(struct usbnet *);
    303   1.1    mrg void	usbnet_unlock_mii(struct usbnet *);
    304   1.1    mrg void	usbnet_unlock_mii_un_locked(struct usbnet *);
    305   1.9    mrg kmutex_t *usbnet_mutex_mii(struct usbnet *);
    306   1.4    mrg static __inline__ void
    307   1.4    mrg usbnet_isowned_mii(struct usbnet *un)
    308   1.4    mrg {
    309   1.9    mrg 	KASSERT(mutex_owned(usbnet_mutex_mii(un)));
    310   1.4    mrg }
    311   1.4    mrg 
    312   1.9    mrg 
    313   1.7    mrg int	usbnet_mii_readreg(device_t, int, int, uint16_t *);
    314   1.7    mrg int	usbnet_mii_writereg(device_t, int, int, uint16_t);
    315   1.7    mrg void	usbnet_mii_statchg(struct ifnet *);
    316   1.1    mrg 
    317   1.1    mrg /* interrupt handling */
    318   1.5    mrg void	usbnet_enqueue(struct usbnet * const, uint8_t *, size_t, int,
    319   1.5    mrg 		       uint32_t, int);
    320   1.5    mrg void	usbnet_input(struct usbnet * const, uint8_t *, size_t);
    321   1.1    mrg 
    322   1.1    mrg /* autoconf */
    323   1.9    mrg void	usbnet_attach(struct usbnet *un, const char *);
    324   1.2    mrg void	usbnet_attach_ifp(struct usbnet *, bool, unsigned, unsigned, int);
    325   1.1    mrg int	usbnet_detach(device_t, int);
    326   1.1    mrg int	usbnet_activate(device_t, devact_t);
    327   1.1    mrg 
    328   1.1    mrg /* stop backend */
    329   1.1    mrg void	usbnet_stop(struct usbnet *, struct ifnet *, int);
    330   1.1    mrg 
    331  1.11    mrg /* module hook up */
    332  1.11    mrg 
    333  1.11    mrg #ifdef _MODULE
    334  1.11    mrg #define USENET_INIT(name)						\
    335  1.11    mrg 	error = config_init_component(cfdriver_ioconf_##name,		\
    336  1.11    mrg 	    cfattach_ioconf_##name, cfdata_ioconf_##name);
    337  1.11    mrg #define USENET_FINI(name)						\
    338  1.11    mrg 	error = config_fini_component(cfdriver_ioconf_##name,		\
    339  1.11    mrg 	    cfattach_ioconf_##name, cfdata_ioconf_##name);
    340  1.11    mrg #else
    341  1.11    mrg #define USENET_INIT(name)
    342  1.11    mrg #define USENET_FINI(name)
    343  1.11    mrg #endif
    344  1.11    mrg 
    345  1.11    mrg #define USBNET_MODULE(name)						\
    346  1.11    mrg 									\
    347  1.11    mrg MODULE(MODULE_CLASS_DRIVER, if_##name, "usbnet");			\
    348  1.11    mrg 									\
    349  1.11    mrg static int								\
    350  1.11    mrg if_##name##_modcmd(modcmd_t cmd, void *aux)				\
    351  1.11    mrg {									\
    352  1.11    mrg 	int error = 0;							\
    353  1.11    mrg 									\
    354  1.11    mrg 	switch (cmd) {							\
    355  1.11    mrg 	case MODULE_CMD_INIT:						\
    356  1.11    mrg 		USENET_INIT(name)					\
    357  1.11    mrg 		return error;						\
    358  1.11    mrg 	case MODULE_CMD_FINI:						\
    359  1.11    mrg 		USENET_FINI(name)					\
    360  1.11    mrg 		return error;						\
    361  1.11    mrg 	default:							\
    362  1.11    mrg 		return ENOTTY;						\
    363  1.11    mrg 	}								\
    364  1.11    mrg }
    365  1.11    mrg 
    366   1.1    mrg #endif /* _DEV_USB_USBNET_H */
    367