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