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