xref: /netbsd-src/share/man/man9/usbnet.9 (revision 2026b7285b519b6985686c4f29b6309b5e58de6d)
1*2026b728Smrg.\"	$NetBSD: usbnet.9,v 1.22 2024/02/04 05:43:06 mrg Exp $
22e9fdde4Smrg.\"
32e9fdde4Smrg.\" Copyright (c) 2019 Matthew R. Green
42e9fdde4Smrg.\" All rights reserved.
52e9fdde4Smrg.\"
62e9fdde4Smrg.\" Redistribution and use in source and binary forms, with or without
72e9fdde4Smrg.\" modification, are permitted provided that the following conditions
82e9fdde4Smrg.\" are met:
92e9fdde4Smrg.\" 1. Redistributions of source code must retain the above copyright
102e9fdde4Smrg.\"    notice, this list of conditions and the following disclaimer.
112e9fdde4Smrg.\" 2. Redistributions in binary form must reproduce the above copyright
122e9fdde4Smrg.\"    notice, this list of conditions and the following disclaimer in the
132e9fdde4Smrg.\"    documentation and/or other materials provided with the distribution.
142e9fdde4Smrg.\"
152e9fdde4Smrg.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
162e9fdde4Smrg.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
172e9fdde4Smrg.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
182e9fdde4Smrg.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
192e9fdde4Smrg.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
202e9fdde4Smrg.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
212e9fdde4Smrg.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
222e9fdde4Smrg.\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
232e9fdde4Smrg.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
242e9fdde4Smrg.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
252e9fdde4Smrg.\" SUCH DAMAGE.
262e9fdde4Smrg.\"
27a3d4be7fSthorpej.Dd March 15, 2020
282e9fdde4Smrg.Dt USBNET 9
292e9fdde4Smrg.Os
302e9fdde4Smrg.Sh NAME
312e9fdde4Smrg.Nm usbnet
3250285510Swiz.Nd common USB Ethernet driver framework
332e9fdde4Smrg.Sh SYNOPSIS
342e9fdde4Smrg.In dev/usb/usbnet.h
352e9fdde4Smrg.Ss Functions offered by usbnet.h
362e9fdde4Smrg.Ft void
372e9fdde4Smrg.Fn usbnet_set_link "struct usbnet *un" "bool link"
382e9fdde4Smrg.Ft struct ifnet *
392e9fdde4Smrg.Fn usbnet_ifp "struct usbnet *un"
402e9fdde4Smrg.Ft struct ethercom *
412e9fdde4Smrg.Fn usbnet_ec "struct usbnet *un"
422e9fdde4Smrg.Ft struct mii_data *
432e9fdde4Smrg.Fn usbnet_mii "struct usbnet *un"
442e9fdde4Smrg.Ft krndsource_t *
452e9fdde4Smrg.Fn usbnet_rndsrc "struct usbnet *un"
462e9fdde4Smrg.Ft void *
472e9fdde4Smrg.Fn usbnet_softc "struct usbnet *un"
482e9fdde4Smrg.Ft bool
492e9fdde4Smrg.Fn usbnet_havelink "struct usbnet *un"
50a06a0449Sriastradh.Ft int
51a06a0449Sriastradh.Fn usbnet_ispromisc "struct usbnet *un"
522e9fdde4Smrg.Ft bool
532e9fdde4Smrg.Fn usbnet_isdying "struct usbnet *un"
542e9fdde4Smrg.Ft void
552e9fdde4Smrg.Fn usbnet_enqueue "struct usbnet *un" "uint8_t *buf" "size_t buflen" "int csum_flags" "uint32_t csum_data" "int mbuf_flags"
562e9fdde4Smrg.Ft void
572e9fdde4Smrg.Fn usbnet_input "struct usbnet *un" "uint8_t *buf" "size_t buflen"
582e9fdde4Smrg.Ft void
593165ccf8Sriastradh.Fn usbnet_attach "struct usbnet *un"
602e9fdde4Smrg.Ft void
61454a193cSmrg.Fn usbnet_attach_ifp "struct usbnet *un" "unsigned if_flags" "unsigned if_extflags" "const struct usbnet_mii *unm"
622e9fdde4Smrg.Ft int
632e9fdde4Smrg.Fn usbnet_detach "device_t dev" "int flags"
642e9fdde4Smrg.Ft int
652e9fdde4Smrg.Fn usbnet_activate "device_t dev" "devact_t act"
662e9fdde4Smrg.Sh DESCRIPTION
672e9fdde4SmrgThe
682e9fdde4Smrg.Nm
6950285510Swizframework provides methods usable for USB Ethernet drivers.
702e9fdde4SmrgThe framework has support for these features:
7150285510Swiz.Bl -bullet -offset 8n
722e9fdde4Smrg.It
732e9fdde4SmrgPartial autoconf handling
742e9fdde4Smrg.It
752e9fdde4SmrgUSB endpoint pipe handling
762e9fdde4Smrg.It
772e9fdde4SmrgRx and Tx chain handling
782e9fdde4Smrg.It
792e9fdde4SmrgGeneric handlers or support for several struct ifnet callbacks
802e9fdde4Smrg.It
81a3d4be7fSthorpejNetwork stack locking protocol
822e9fdde4Smrg.It
832e9fdde4SmrgInterrupt handling
842e9fdde4Smrg.El
852e9fdde4Smrg.Pp
862e9fdde4Smrg.Nm
872e9fdde4Smrgprovides many or all of the traditional
882e9fdde4Smrg.Dq softc
892e9fdde4Smrgmembers inside
902e9fdde4Smrg.Va struct usbnet ,
912e9fdde4Smrgwhich can be used directly as the device softc structure if
922e9fdde4Smrgno additional storage is required.
9350285510SwizA structure exists for receive and transmit chain management,
942e9fdde4Smrg.Va struct usbnet_chain ,
952e9fdde4Smrgthat tracks the metadata for each transfer descriptor available,
962e9fdde4Smrgminimum of one each for Rx and Tx slot, and will be passed
972e9fdde4Smrgto the Rx and Tx callbacks.
982e9fdde4Smrg.Pp
992e9fdde4SmrgThere is a
1002e9fdde4Smrg.Va struct usbnet_ops
1012e9fdde4Smrgstructure that provides a number of optional and required callbacks
1022e9fdde4Smrgthat will be described below.
1032e9fdde4Smrg.Pp
1042e9fdde4SmrgFor autoconfiguration the device attach routine is expected to
1052e9fdde4Smrgensure that this device's
1062e9fdde4Smrg.Va struct usbnet
10724747793Smrgis the first member of the device softc, if it can not be used directly
10824747793Smrgas the device softc, as well as set up the necessary structure members,
10924747793Smrgfind end-points, find the Ethernet address if relevant, call
1102e9fdde4Smrg.Fn usbnet_attach ,
11150285510Swizset up interface, Ethernet, and MII capabilities, and finally call
1122e9fdde4Smrg.Fn usbnet_attach_ifp .
1132e9fdde4SmrgThe device detach routine should free any resources allocated
1142e9fdde4Smrgby attach and then call
1152e9fdde4Smrg.Fn usbnet_detach ,
1162e9fdde4Smrgpossibly directly using
1172e9fdde4Smrg.Fn usbnet_detach
1182e9fdde4Smrgas most consumers have no additional resources not owned and
1192e9fdde4Smrgreleased by the
1202e9fdde4Smrg.Nm
1212e9fdde4Smrgframework itself.
1222e9fdde4SmrgThe device activate function should be set to
1232e9fdde4Smrg.Fn usbnet_activate .
1242e9fdde4Smrg.Pp
1253165ccf8SriastradhWhen bringing an interface up from
1263165ccf8Sriastradh.Xr if_init 9 ,
1273165ccf8Sriastradhwhich happens under
1283165ccf8Sriastradh.Xr IFNET_LOCK 9 ,
1292e9fdde4Smrg.Nm
1303165ccf8Sriastradhwill:
1313165ccf8Sriastradh.Bl -enum
1323165ccf8Sriastradh.It
1333165ccf8Sriastradhcall
1343165ccf8Sriastradh.Dq uno_init
1353165ccf8Sriastradhto initialize the hardware for sending and receiving packets,
1363165ccf8Sriastradh.It
1373165ccf8Sriastradhopen the USB pipes,
1383165ccf8Sriastradh.It
1393165ccf8Sriastradhallocate Rx and Tx buffers for transfers,
1403165ccf8Sriastradh.It
1413165ccf8Sriastradhcall
1423165ccf8Sriastradh.Dq uno_mcast
1433165ccf8Sriastradhto initially program the hardware multicast filter, and finally
1443165ccf8Sriastradh.It
1453165ccf8Sriastradhstart the Rx transfers so packets can be received.
1463165ccf8Sriastradh.El
1473165ccf8Sriastradh.Pp
1482e9fdde4SmrgSee the
1492e9fdde4Smrg.Sx RECEIVE AND SEND
1502e9fdde4Smrgsection for details on using the chains.
1512e9fdde4Smrg.Pp
1523165ccf8SriastradhWhen bringing an interface down from
1533165ccf8Sriastradh.Xr if_stop 9 ,
1543165ccf8Sriastradhwhich happens under
1553165ccf8Sriastradh.Xr IFNET_LOCK 9 ,
1563165ccf8Sriastradh.Nm
1573165ccf8Sriastradhwill:
1583165ccf8Sriastradh.Bl -enum
1593165ccf8Sriastradh.It
1603165ccf8Sriastradhabort the USB pipes,
1613165ccf8Sriastradh.It
1623165ccf8Sriastradhcall
1633165ccf8Sriastradh.Dq uno_stop
1643165ccf8Sriastradhto stop the hardware from receiving packets (unless the device is
1653165ccf8Sriastradhdetaching),
1663165ccf8Sriastradh.It
1673165ccf8Sriastradhfree Rx and Tx buffers for transfers, and
1683165ccf8Sriastradh.It
1693165ccf8Sriastradhclose the USB pipes.
1703165ccf8Sriastradh.El
1713165ccf8Sriastradh.Pp
1723165ccf8SriastradhFor interface ioctl, most of the handling is in the framework.
1733165ccf8SriastradhWhile the interface is running, the optional
1743165ccf8Sriastradh.Dq uno_mcast
1753165ccf8Sriastradhcallback is invoked after handling the
1763165ccf8Sriastradh.Dv SIOCADDMULTI
1773165ccf8Sriastradhand
1783165ccf8Sriastradh.Dv SIOCDELMULTI
1793165ccf8Sriastradhioctl commands to update the hardware's multicast filter from the
1803165ccf8Sriastradh.Xr ethersubr 9
1813165ccf8Sriastradhlists.
1823165ccf8SriastradhThe optional
1832e9fdde4Smrg.Dq uno_ioctl
1843165ccf8Sriastradhcallback, which is invoked under
1853165ccf8Sriastradh.Xr IFNET_LOCK 9 ,
1863165ccf8Sriastradhcan be used to program special settings like offload handling.
1873165ccf8Sriastradh.Pp
1882e9fdde4SmrgIf ioctl handling requires capturing device-specific ioctls then the
1892e9fdde4Smrg.Dq uno_override_ioctl
1902e9fdde4Smrgcallback may be used instead to replace the framework's
1912e9fdde4Smrgioctl handler completely (i.e., the replacement should call any generic
1922e9fdde4Smrgioctl handlers such as
1932e9fdde4Smrg.Fn ether_ioctl
1942e9fdde4Smrgas required.)
1953165ccf8SriastradhFor sending packets, the
1962e9fdde4Smrg.Dq uno_tx_prepare
1972e9fdde4Smrgcallback must be used to convert
1982e9fdde4Smrgan mbuf into a chain buffer ready for transmission.
1992e9fdde4Smrg.Pp
2002e9fdde4SmrgFor devices requiring MII handling there are callbacks for reading and
2012e9fdde4Smrgwriting registers, and for status change events.
2023165ccf8SriastradhAccess to all the MII functions is serialized by
2033165ccf8Sriastradh.Nm .
2042e9fdde4Smrg.Pp
2052e9fdde4SmrgAs receive must handle the case of multiple packets in one buffer,
2062e9fdde4Smrgthe support is split between the driver and the framework.
2072e9fdde4SmrgA
2082e9fdde4Smrg.Dq uno_rx_loop
2092e9fdde4Smrgcallback must be provided that loops over the incoming
2102e9fdde4Smrgpacket data found in a chain, performs necessary checking and passes
2112e9fdde4Smrgthe network frame up the stack via either
2122e9fdde4Smrg.Fn usbnet_enqueue
2132e9fdde4Smrgor
2142e9fdde4Smrg.Fn usbnet_input .
21550285510SwizTypically Ethernet devices prefer
2162e9fdde4Smrg.Fn usbnet_enqueue .
2172e9fdde4Smrg.Pp
2182e9fdde4SmrgGeneral accessor functions for
2192e9fdde4Smrg.Fa struct usbnet :
2202e9fdde4Smrg.Bl -tag -width 4n
2212e9fdde4Smrg.It Fn usbnet_set_link un link
2222e9fdde4SmrgSet the link status for this
2232e9fdde4Smrg.Fa un
2242e9fdde4Smrgto
2252e9fdde4Smrg.Fa link .
2262e9fdde4Smrg.It Fn usbnet_ifp un
2272e9fdde4SmrgReturns pointer to this
2283165ccf8Sriastradh.Fa un Ns 's
2292e9fdde4Smrg.Va struct ifnet .
2302e9fdde4Smrg.It Fn usbnet_ec un
2312e9fdde4SmrgReturns pointer to this
2323165ccf8Sriastradh.Fa un Ns 's
2332e9fdde4Smrg.Va struct ethercom .
2342e9fdde4Smrg.It Fn usbnet_mii un
2352e9fdde4SmrgReturns pointer to this
2363165ccf8Sriastradh.Fa un Ns 's
2372e9fdde4Smrg.Va struct mii_data .
2382e9fdde4Smrg.It Fn usbnet_rndsrc un
2392e9fdde4SmrgReturns pointer to this
2403165ccf8Sriastradh.Fa un Ns 's
2412e9fdde4Smrg.Va krndsource_t .
2422e9fdde4Smrg.It Fn usbnet_softc un
2432e9fdde4SmrgReturns pointer to this
2443165ccf8Sriastradh.Fa un Ns 's
2452e9fdde4Smrgdevice softc.
246a3d4be7fSthorpej.It Fn usbnet_havelink un
2472e9fdde4SmrgReturns true if link is active.
248a06a0449Sriastradh.It Fn usbnet_ispromisc un
249a06a0449SriastradhTrue if
250a06a0449Sriastradh.Dv IFF_PROMISC
251a06a0449Sriastradhis enabled, false if not.
252a06a0449Sriastradh.Pp
253a06a0449SriastradhMay be used only in
254a06a0449Sriastradh.Dq uno_init
255a06a0449Sriastradhand
256a06a0449Sriastradh.Dq uno_mcast .
257a06a0449Sriastradh.Pp
258a06a0449SriastradhDrivers must use this in
259a06a0449Sriastradh.Dq uno_mcast
260a06a0449Sriastradhinstead of reading
261a06a0449Sriastradh.Li ifp->if_flags .
262a3d4be7fSthorpej.It Fn usbnet_isdying un
2632e9fdde4SmrgReturns true if device is dying (has been pulled or deactivated,
2643165ccf8Sriastradhpending detach).
2653165ccf8SriastradhThis should be used only to abort timeout loops early.
2662e9fdde4Smrg.El
2672e9fdde4Smrg.Pp
2682e9fdde4SmrgBuffer enqueue handling for
2692e9fdde4Smrg.Fa struct usbnet :
2702e9fdde4Smrg.Bl -tag -width 4n
2712e9fdde4Smrg.It Fn usbnet_enqueue un buf buflen csum_flags csum_data mbuf_flags
2722e9fdde4SmrgEnqueue buffer
2732e9fdde4Smrg.Fa buf
2742e9fdde4Smrgfor length
2752e9fdde4Smrg.Fa buflen
2762e9fdde4Smrgwith higher layers, using the provided
2772e9fdde4Smrg.Fa csum_flags ,
2782e9fdde4Smrgand
2792e9fdde4Smrg.Fa csum_data ,
2802e9fdde4Smrgwhich are written directly to the mbuf packet header, and
2812e9fdde4Smrg.Fa mbuf_flags ,
2822e9fdde4Smrgwhich is or-ed into the mbuf flags for the created mbuf.
2832e9fdde4Smrg.It Fn usbnet_input un buf buflen
2842e9fdde4SmrgEnqueue buffer
2852e9fdde4Smrg.Fa buf
2862e9fdde4Smrgfor length
2872e9fdde4Smrg.Fa buflen
2883165ccf8Sriastradhwith higher layers.
2892e9fdde4Smrg.El
2902e9fdde4Smrg.Pp
2912e9fdde4SmrgAutoconfiguration handling for
2922e9fdde4Smrg.Fa struct usbnet .
2932e9fdde4SmrgSee the
2942e9fdde4Smrg.Sx AUTOCONFIGURATION
2952e9fdde4Smrgsection for more details about these functions.
2962e9fdde4Smrg.Bl -tag -width 4n
2973165ccf8Sriastradh.It Fn usbnet_attach un
2982e9fdde4SmrgInitial stage attach of a usb network device.
2993165ccf8SriastradhPerforms internal initialization and memory allocation only \(em
3003165ccf8Sriastradhnothing is published yet.
301454a193cSmrg.It Fn usbnet_attach_ifp un if_flags if_extflags unm
3022e9fdde4SmrgFinal stage attach of usb network device.
3033165ccf8SriastradhPublishes the network interface to the rest of the system.
3043165ccf8Sriastradh.Pp
305454a193cSmrgIf the passed in
306454a193cSmrg.Fa unm
307b480f76eSwizis
308b480f76eSwiz.Pf non- Dv NULL
309b480f76eSwizthen an MII interface will be created using the values
310454a193cSmrgprovided in the
311454a193cSmrg.Fa struct usbnet_mii
312454a193cSmrgstructure, which has these members passed to
313454a193cSmrg.Fn mii_attach :
3143165ccf8Sriastradh.Bl -tag -width "un_mii_capmask"
315454a193cSmrg.It un_mii_flags
316454a193cSmrgFlags.
317454a193cSmrg.It un_mii_capmask
318454a193cSmrgCapability mask.
319454a193cSmrg.It un_mii_phyloc
320454a193cSmrgPHY location.
321454a193cSmrg.It un_mii_offset
322454a193cSmrgPHY offset.
323454a193cSmrg.El
324454a193cSmrg.Pp
325454a193cSmrgA default
326454a193cSmrg.Fa unm
327454a193cSmrgcan be set using the
328454a193cSmrg.Fn USBNET_MII_DECL_DEFAULT
329454a193cSmrgmacro.
3302e9fdde4SmrgThe
3312e9fdde4Smrg.Fa if_flags
3322e9fdde4Smrgand
3332e9fdde4Smrg.Fa if_extflags
3342e9fdde4Smrgwill be or-ed into the interface flags and extflags.
3352e9fdde4Smrg.It Fn usbnet_detach dev flags
3362e9fdde4SmrgDevice detach.
3373165ccf8SriastradhStops all activity and frees memory.
3383165ccf8SriastradhUsable as
3393165ccf8Sriastradh.Xr driver 9
3403165ccf8Sriastradhdetach method.
3412e9fdde4Smrg.It Fn usbnet_activate dev act
3422e9fdde4SmrgDevice activate (deactivate) method.
3433165ccf8SriastradhUsable as
3443165ccf8Sriastradh.Xr driver 9
3453165ccf8Sriastradhactivate method.
34650285510Swiz.El
3472e9fdde4Smrg.Sh AUTOCONFIGURATION
3482e9fdde4SmrgThe framework expects the usbnet structure to have these members
3492e9fdde4Smrgfilled in with valid values or functions:
35050285510Swiz.Bl -tag -width 6n
3512e9fdde4Smrg.It un_sc
3522e9fdde4SmrgReal softc allocated by autoconf and provided to attach, should be
3532e9fdde4Smrgset to the usbnet structure if no device-specific softc is needed.
3542e9fdde4Smrg.It un_dev
3552e9fdde4Smrgdevice_t saved in attach, used for messages mostly.
3562e9fdde4Smrg.It un_iface
3572e9fdde4SmrgThe USB iface handle for data interactions, see
3582e9fdde4Smrg.Fn usbd_device2interface_handle
3592e9fdde4Smrgfor more details.
3602e9fdde4Smrg.It un_udev
3612e9fdde4SmrgThe struct usbd_device for this device, provided as the usb_attach_arg's
3622e9fdde4Smrg.Va uaa_device
3632e9fdde4Smrgmember.
3642e9fdde4Smrg.It un_ops
3652e9fdde4SmrgPoints to a
3662e9fdde4Smrg.Va struct usbnet_ops
3672e9fdde4Smrgstructure which contains these members:
3682e9fdde4Smrg.Bl -tag -width 4n
369d3025cbdSmrg.It Ft void Fn (*uno_stop) "struct ifnet *ifp" "int disable"
3703165ccf8SriastradhStop hardware activity
371a3d4be7fSthorpej.Pq optional .
3723165ccf8SriastradhCalled under
3733165ccf8Sriastradh.Xr IFNET_LOCK 9
374d77e27a1Sriastradhwhen bringing the interface down, but skipped when the device is
375d77e27a1Sriastradhdetaching.
376d3025cbdSmrg.It Ft int Fn (*uno_ioctl) "struct ifnet *ifp" "u_long cmd" "void *data"
3773165ccf8SriastradhHandle driver-specific ioctls
378a3d4be7fSthorpej.Pq optional .
3793165ccf8SriastradhCalled under
3803165ccf8Sriastradh.Xr IFNET_LOCK 9 .
3813165ccf8Sriastradh.It Ft void Fn (*uno_mcast) "struct ifnet *"
3823165ccf8SriastradhProgram hardware multicast filters from
3833165ccf8Sriastradh.Xr ethersubr 9
3843165ccf8Sriastradhlists
3853165ccf8Sriastradh.Pq optional .
3863165ccf8SriastradhCalled between, and not during,
3873165ccf8Sriastradh.Dq uno_init
3883165ccf8Sriastradhand
3893165ccf8Sriastradh.Dq uno_stop .
390377f156eSwiz.It Ft int Fn (*uno_override_ioctl) "struct ifnet *ifp" "u_long cmd" "void *data"
3913165ccf8SriastradhHandle all ioctls, including standard ethernet ioctls normally handled
3923165ccf8Sriastradhinternally by
3933165ccf8Sriastradh.Nm
394a3d4be7fSthorpej.Pq optional .
3953165ccf8SriastradhMay or may not be called under
3963165ccf8Sriastradh.Xr IFNET_LOCK 9 .
397d3025cbdSmrg.It Ft int Fn (*uno_init) "struct ifnet *ifp"
3981c13666aSriastradhInitialize hardware activity
3991c13666aSriastradh.Pq optional .
4003165ccf8SriastradhCalled under
4013165ccf8Sriastradh.Xr IFNET_LOCK 9
4023165ccf8Sriastradhwhen bringing the interface up.
403454a193cSmrg.It Ft int Fn (*uno_read_reg) "struct usbnet *un" "int phy" "int reg" "uint16_t *val"
4042e9fdde4SmrgRead MII register.
4052e9fdde4SmrgRequired with MII.
406c920c1eaSriastradhSerialized with other MII functions, and only called after
4073165ccf8Sriastradh.Dq uno_init
408c920c1eaSriastradhand before
4093165ccf8Sriastradh.Dq uno_stop .
410454a193cSmrg.It Ft int Fn (*uno_write_reg) "struct usbnet *un" "int phy" "int reg" "uint16_t val"
4112e9fdde4SmrgWrite MII register.
4122e9fdde4SmrgRequired with MII.
413c920c1eaSriastradhSerialized with other MII functions, and only called after
4143165ccf8Sriastradh.Dq uno_init
415c920c1eaSriastradhand before
4163165ccf8Sriastradh.Dq uno_stop .
417d3025cbdSmrg.It Ft usbd_status Fn (*uno_statchg) "struct ifnet *ifp"
4182e9fdde4SmrgHandle MII status change.
4192e9fdde4SmrgRequired with MII.
420c920c1eaSriastradhSerialized with other MII functions, and only called after
4213165ccf8Sriastradh.Dq uno_init
422c920c1eaSriastradhand before
4233165ccf8Sriastradh.Dq uno_stop .
424454a193cSmrg.It Ft unsigned Fn (*uno_tx_prepare) "struct usbnet *un" "struct mbuf *m" "struct usbnet_chain *c"
42550285510SwizPrepare an mbuf for transmit.
42650285510SwizRequired.
4273165ccf8SriastradhCalled sequentially between, and not during,
428c920c1eaSriastradh.Dq uno_init
4293165ccf8Sriastradhand
4303165ccf8Sriastradh.Dq uno_stop .
431454a193cSmrg.It Ft void Fn (*uno_rx_loop) "struct usbnet *un" "struct usbnet_chain *c" "uint32_t total_len"
43250285510SwizPrepare one or more chain for enqueue.
43350285510SwizRequired.
4343165ccf8SriastradhCalled sequentially between, and not during,
4353165ccf8Sriastradh.Dq uno_init
4363165ccf8Sriastradhand
4373165ccf8Sriastradh.Dq uno_stop .
438d3025cbdSmrg.It Ft void Fn (*uno_intr) "struct usbnet *un" "usbd_status status"
439a3d4be7fSthorpejProcess periodic interrupt
440a3d4be7fSthorpej.Pq optional .
4413165ccf8SriastradhCalled sequentially between, and not during,
4423165ccf8Sriastradh.Dq uno_init
4433165ccf8Sriastradhand
4443165ccf8Sriastradh.Dq uno_stop .
445d3025cbdSmrg.It Ft void Fn (*uno_tick) "struct usbnet *un"
446a3d4be7fSthorpejCalled every second with USB task thread context
447a3d4be7fSthorpej.Pq optional .
4483165ccf8SriastradhCalled sequentially between, and not during,
4493165ccf8Sriastradh.Dq uno_init
4503165ccf8Sriastradhand
4513165ccf8Sriastradh.Dq uno_stop .
4522e9fdde4Smrg.El
4532e9fdde4Smrg.It un_intr
4542e9fdde4SmrgPoints to a
4552e9fdde4Smrg.Va struct usbnet_intr
4562e9fdde4Smrgstructure which should have these members set:
4572e9fdde4Smrg.Bl -tag -width 4n
4583165ccf8Sriastradh.It uni_buf
45950285510SwizIf
46050285510Swiz.Pf non- Dv NULL ,
46150285510Swizpoints to a buffer passed to
462d3025cbdSmrg.Fn usbd_open_pipe_intr
4632e9fdde4Smrgin the device init callback, along with the size and interval.
4643165ccf8Sriastradh.It uni_bufsz
4652e9fdde4SmrgSize of interrupt pipe buffer.
4663165ccf8Sriastradh.It uni_interval
4672e9fdde4SmrgFrequency of the interrupt in milliseconds.
4682e9fdde4Smrg.El
4692e9fdde4Smrg.It un_ed
4702e9fdde4SmrgArray of endpoint descriptors.
47150285510SwizThere indexes are provided:
472d3025cbdSmrg.Dv USBNET_ENDPT_RX ,
473d3025cbdSmrg.Dv USBNET_ENDPT_TX ,
4742e9fdde4Smrgand
475d3025cbdSmrg.Dv USBNET_ENDPT_INTR .
4762e9fdde4SmrgThe Rx and Tx endpoints are required.
4772e9fdde4Smrg.It un_phyno
4782e9fdde4SmrgMII phy number.
479454a193cSmrgNot used by
480454a193cSmrg.Nm .
4812e9fdde4Smrg.It un_eaddr
48250285510Swiz6 bytes of Ethernet address that must be provided before calling
4832e9fdde4Smrg.Fn usbnet_attach_ifp
48450285510Swizif the device has Ethernet.
4852e9fdde4Smrg.It un_flags
4862e9fdde4SmrgDevice owned flags word.
4872e9fdde4SmrgThe
4882e9fdde4Smrg.Nm
4892e9fdde4Smrgframework will not touch this value.
4902e9fdde4Smrg.It un_rx_xfer_flags
49150285510SwizPassed to
49250285510Swiz.Fn usbd_setup_xfer
49350285510Swizfor receiving packets.
4942e9fdde4Smrg.It un_tx_xfer_flags
49550285510SwizPassed to
49650285510Swiz.Fn usbd_setup_xfer
49750285510Swizfor sending packets.
4982e9fdde4Smrg.It un_rx_list_cnt
4992e9fdde4SmrgNumber of chain elements to allocate for Rx.
5002e9fdde4Smrg.It un_tx_list_cnt
5012e9fdde4SmrgNumber of chain elements to allocate for Tx.
5022e9fdde4Smrg.It un_rx_bufsz
5032e9fdde4SmrgRx buffer size.
5042e9fdde4Smrg.It un_tx_bufsz
5052e9fdde4SmrgTx buffer size.
5062e9fdde4Smrg.El
5072e9fdde4Smrg.Pp
5082e9fdde4SmrgThe device detach and activate callbacks can typically be set to
5092e9fdde4Smrg.Fn usbnet_detach
5102e9fdde4Smrgand
5112e9fdde4Smrg.Fn usbnet_activate
5122e9fdde4Smrgunless device-specific handling is required, in which case, they
5132e9fdde4Smrgcan be called before or after such handling.
5142e9fdde4Smrg.Pp
5152e9fdde4SmrgThe capabilities described in both
5162e9fdde4Smrg.Va struct ifp
5172e9fdde4Smrgand
5182e9fdde4Smrg.Va struct ethercom
5192e9fdde4Smrgmust be set before calling
5202e9fdde4Smrg.Fn usbnet_attach_ifp .
5212e9fdde4Smrg.Sh RECEIVE AND SEND
5224fd6c3feSriastradhReceive and send routines are structured around the
5232e9fdde4Smrg.Va usbnet_cdata
5242e9fdde4Smrgand
5252e9fdde4Smrg.Va usbnet_chain
52650285510Swizstructures, the
5272e9fdde4Smrg.Dv un_ed ,
5282e9fdde4Smrg.Dv un_rx_xfer_flags ,
5292e9fdde4Smrgand
53050285510Swiz.Dv un_tx_xfer_flags
5312e9fdde4Smrgmembers, and the
5322e9fdde4Smrg.Fn uno_init ,
5332e9fdde4Smrg.Fn uno_tx_prepare ,
5344fd6c3feSriastradh.Fn uno_rx_loop ,
5352e9fdde4Smrgand
5364fd6c3feSriastradh.Fn uno_stop
5372e9fdde4Smrgcallbacks of
5382e9fdde4Smrg.Va usbnet_ops .
5392e9fdde4Smrg.Pp
5402e9fdde4SmrgTypically, the device attach routine will fill in members of the
5412e9fdde4Smrg.Va usbnet
5422e9fdde4Smrgstructure, as listed in
5432e9fdde4Smrg.Sx AUTOCONFIGURATION .
5442e9fdde4SmrgThe
54592a9ab1fSmrg.Dv un_ed
54692a9ab1fSmrgarray should have the
5472e9fdde4Smrg.Dv USBNET_ENDPT_RX
5482e9fdde4Smrgand
5492e9fdde4Smrg.Dv USBNET_ENDPT_TX
5502e9fdde4Smrgarray entries filled in, and optionally the
5512e9fdde4Smrg.Dv USBNET_ENDPT_INTR
5522e9fdde4Smrgentry filled in if applicable.
5532e9fdde4Smrg.Pp
5542e9fdde4SmrgThe
5552e9fdde4Smrg.Fn uno_init
5564fd6c3feSriastradhcallback enables the hardware, and if necessary reprograms the hardware
5574fd6c3feSriastradhmulticast filter, before the framework initiates USB Tx/Rx transfers.
5584fd6c3feSriastradhAll USB transfer setup is handled by the framework.
5594fd6c3feSriastradhThe driver callbacks merely copy data in or out of a chain entry using
5602e9fdde4Smrgwhat is typically a device-specific method.
5612e9fdde4Smrg.Pp
56250285510SwizThe
5632e9fdde4Smrg.Fn uno_rx_loop
5643165ccf8Sriastradhcallback, called sequentially, converts the provided
5652e9fdde4Smrg.Va usbnet_chain
5662e9fdde4Smrgdata and length into a series (one or more) of packets that are
5672e9fdde4Smrgenqueued with the higher layers using either
5682e9fdde4Smrg.Fn usbnet_enqueue
5692e9fdde4Smrg(for most devices) or
5702e9fdde4Smrg.Fn usbnet_input
571454a193cSmrgfor devices that use
5724fd6c3feSriastradh.Fn if_input .
5733165ccf8Sriastradh(This currently relies upon the
574454a193cSmrg.Va struct ifnet
575454a193cSmrghaving the
576454a193cSmrg.Dq _if_input
577454a193cSmrgmember set as well, which is true for current consumers.)
5782e9fdde4Smrg.Pp
5792e9fdde4SmrgThe
5802e9fdde4Smrg.Fn uno_tx_prepare
5812e9fdde4Smrgcallback must convert the provided
5822e9fdde4Smrg.Va struct mbuf
5832e9fdde4Smrginto the provided
5842e9fdde4Smrg.Va struct usbnet_chain
5852e9fdde4Smrgperforming any device-specific padding, checksum, header or other.
586fc57de45SmrgNote that this callback must check that it is not attempting to copy
587fc57de45Smrgmore than the chain buffer size, as set in the
588fc57de45Smrg.Va usbnet
589fc57de45Smrg.Dq un_tx_bufsz
590fc57de45Smrgmember.
5913165ccf8SriastradhThis callback is only called once per packet, sequentially.
5922e9fdde4Smrg.Pp
5932e9fdde4SmrgThe
5942e9fdde4Smrg.Fa struct usbnet_chain
5952e9fdde4Smrgstructure which contains a
5962e9fdde4Smrg.Dq unc_buf
5972e9fdde4Smrgmember which has the chain buffer allocated where data should be
5982e9fdde4Smrgcopied to or from for receive or transmit operations.
5992e9fdde4SmrgIt also contains pointers back to the owning
6002e9fdde4Smrg.Fa struct usbnet ,
6012e9fdde4Smrgand the
6022e9fdde4Smrg.Va struct usbd_xfer
6032e9fdde4Smrgassociated with this transfer.
6044fd6c3feSriastradh.Pp
6054fd6c3feSriastradhAfter aborting all USB Tx/Rx transfers when bringing an interface down,
6064fd6c3feSriastradhthe framework calls the optional
6074fd6c3feSriastradh.Fn uno_stop
6084fd6c3feSriastradhcallback to disable the hardware.
6092e9fdde4Smrg.Sh MII
6102e9fdde4SmrgFor devices that have MII support these callbacks in
6112e9fdde4Smrg.Fa struct usbnet_ops
6122e9fdde4Smrgmust be provided:
6132e9fdde4Smrg.Bl -tag -width 4n
6142e9fdde4Smrg.It uno_read_reg
6152e9fdde4SmrgRead an MII register for a particular PHY.
616454a193cSmrgReturns standard
617454a193cSmrg.Xr errno 2 .
6183165ccf8SriastradhMust initialize the result even on failure.
6192e9fdde4Smrg.It uno_write_reg
6202e9fdde4SmrgWrite an MII register for a particular PHY.
621454a193cSmrgReturns standard
622454a193cSmrg.Xr errno 2 .
6232e9fdde4Smrg.It uno_statchg
6242e9fdde4SmrgHandle a status change event for this interface.
6252e9fdde4Smrg.El
6262e9fdde4Smrg.Sh INTERRUPT PIPE
62750285510SwizThe interrupt specific callback,
6282e9fdde4Smrg.Dq uno_intr ,
6292e9fdde4Smrgis an optional callback that can be called periodically, registered by
6302e9fdde4Smrg.Nm
6312e9fdde4Smrgusing the
6322e9fdde4Smrg.Fn usbd_open_pipe_intr
6332e9fdde4Smrgfunction (instead of the
6342e9fdde4Smrg.Fn usbd_open_pipe
6352e9fdde4Smrgfunction.)
636d3025cbdSmrgThe
637d3025cbdSmrg.Nm
638d3025cbdSmrgframework provides most of the interrupt handling and the callback
639d3025cbdSmrgsimply inspects the returned buffer as necessary.
640fc57de45SmrgTo enable the this callback point the
6412e9fdde4Smrg.Va struct usbnet
6422e9fdde4Smrgmember
6432e9fdde4Smrg.Dq un_intr
644fc57de45Smrgto a
6452e9fdde4Smrg.Va struct usbnet_intr
646fc57de45Smrgstructure with these members set:
647fc57de45Smrg.Bl -tag -width 4n
648fc57de45Smrg.It uni_buf
649fc57de45SmrgData buffer for interrupt status relies.
650fc57de45Smrg.It uni_bufsz
651fc57de45SmrgSize of the above buffer.
652fc57de45Smrg.It uni_interval
653fc57de45SmrgInterval in millieconds.
654fc57de45Smrg.El
655fc57de45Smrg.Pp
656fc57de45SmrgThese values will be passed to
6572e9fdde4Smrg.Fn usbd_open_pipe_intr .
658fc57de45Smrg.Sh CONVERTING OLD-STYLE DRIVERS
659fc57de45SmrgThe porting of an older driver to the
660fc57de45Smrg.Nm
661fc57de45Smrgframework is largely an effort in deleting code.
662fc57de45SmrgThe process involves making these changes:
663fc57de45Smrg.Bl -tag -width 4n
664fc57de45Smrg.It Headers
665fc57de45SmrgMany headers are included in
666fc57de45Smrg.Pa usbnet.h
667fc57de45Smrgand can be removed from the driver, as well as headers no longer used,
668fc57de45Smrgsuch as
669fc57de45Smrg.Pa callout.h
670fc57de45Smrgand
671fc57de45Smrg.Pa rndsource.h ,
672fc57de45Smrgetc.
673fc57de45Smrg.It Device softc
674fc57de45SmrgThe majority of the driver's existing
675fc57de45Smrg.Dq softc
676fc57de45Smrgstructure can likely be replaced with usage of
677fc57de45Smrg.Va struct usbnet
678fc57de45Smrgand its related functionality.
67924747793SmrgThis includes at least the device_t pointer, Ethernet address, the
680fc57de45Smrgethercom and mii_data structures, end point descriptors, usbd device,
681fc57de45Smrginterface, and task and callout structures (both these probably go
682fc57de45Smrgaway entirely) and all the associated watchdog handling,
683fc57de45Smrgtimevals, list size, buffer size and xfer flags for
684fc57de45Smrgboth Rx, and Tx, and interrupt notices, interface flags, device link,
6853165ccf8SriastradhPHY number, chain data, locks including Rx, Tx, and MII.
686fc57de45SmrgThere is a driver-only
687fc57de45Smrg.Dq un_flags
688fc57de45Smrgin the
689fc57de45Smrg.Va usbnet
690fc57de45Smrgstructure available for drivers to use.
691fc57de45Smrg.Pp
692fc57de45SmrgMany drivers can use the
693fc57de45Smrg.Va usbnet
694fc57de45Smrgstructure as the device private storage passed to
695fc57de45Smrg.Dv CFATTACH_DECL_NEW .
696fc57de45SmrgMany internal functions to the driver may look better if switched to
697fc57de45Smrgoperate on the device's
698fc57de45Smrg.Va usbnet
699fc57de45Smrgas, for example, the
700fc57de45Smrg.Va usbd_device
701fc57de45Smrgvalue is now available (and must be set by the driver) in the
702fc57de45Smrg.Va usbnet ,
703fc57de45Smrgwhich may be needed for any call to
704fc57de45Smrg.Fn usbd_do_request .
705fc57de45SmrgThe standard endpoint values must be stored in the
706fc57de45Smrg.Nm
707fc57de45Smrg.Dq un_ed[]
708fc57de45Smrgarray.
709fc57de45Smrg.Pp
710fc57de45SmrgAs
711fc57de45Smrg.Nm
712fc57de45Smrgmanages xfer chains all code related to the opening, closing, aborting
713fc57de45Smrgand transferring of data on pipes is performed by the framework based
714fc57de45Smrgupon the buffer size and more provided in
715fc57de45Smrg.Va subnet ,
716fc57de45Smrgso all code related to them should be deleted.
717fc57de45Smrg.It Interface setup
718fc57de45SmrgThe vast majority of interface specific code should be deleted.
719fc57de45SmrgFor device-specific interface values, the
720fc57de45Smrg.Va ifnet
721fc57de45Smrgflags and exflags can be set, as well as the
722fc57de45Smrg.Va ethercom
723fc57de45Smrg.Dq ec_capabilities
724fc57de45Smrgmember, before calling
725fc57de45Smrg.Fn usbnet_attach_ifp .
726fc57de45SmrgAll calls to
727fc57de45Smrg.Fn ifmedia_init ,
728fc57de45Smrg.Fn mii_attach ,
729fc57de45Smrg.Fn ifmedia_add ,
730fc57de45Smrg.Fn ifmedia_set ,
731fc57de45Smrg.Fn if_attach ,
732fc57de45Smrg.Fn ether_ifattach ,
733fc57de45Smrg.Fn rnd_attach_source ,
734fc57de45Smrgand
735fc57de45Smrg.Fn usbd_add_drv_event
736fc57de45Smrgshould be eliminated.
737fc57de45SmrgThe device
738fc57de45Smrg.Dq ioctl
739fc57de45Smrgroutine can use the default handling with a callback for additional
740fc57de45Smrgdevice specific programming (multicast filters, etc.), which can be
741fc57de45Smrgempty, or, the override ioctl can be used for heavier requirements.
742fc57de45SmrgThe device
743fc57de45Smrg.Dq stop
744fc57de45Smrgroutine is replaced with a simple call that turns off the
745fc57de45Smrgdevice-specific transmitter and receiver if necessary, as the
746fc57de45Smrgframework handles pipes and transfers and buffers.
747fc57de45Smrg.It MII handling
748a3d4be7fSthorpejFor devices with MII support the three normal callbacks
749a3d4be7fSthorpej.Pq read, write, and status change
750a3d4be7fSthorpejmust be converted to
751fc57de45Smrg.Va usbnet .
752fc57de45SmrgLocal
753fc57de45Smrg.Dq link
754fc57de45Smrgvariables need to be replaced with accesses to
755fc57de45Smrg.Fn usbnet_set_link
756fc57de45Smrgand
757fc57de45Smrg.Fn usbnet_havelink .
758fc57de45SmrgOther ifmedia callbacks that were passed to
759fc57de45Smrg.Fn ifmedia_init
760fc57de45Smrgshould be deleted and any work moved into
761fc57de45Smrg.Dq uno_statchg .
762fc57de45Smrg.It Receive and Transmit
763fc57de45SmrgThe
764fc57de45Smrg.Nm
765fc57de45Smrgframework handles the majority of handling of both network directions.
766fc57de45SmrgThe interface init routine should keep all of the device specific setup
767fc57de45Smrgbut replace all pipe management with a call to
768fc57de45Smrg.Fn usbnet_init_rx_tx .
769fc57de45SmrgThe typical receive handling will normally be replaced with a receive
770fc57de45Smrgloop functions that can accept one or more packets,
771fc57de45Smrg.Dq uno_rx_loop ,
772fc57de45Smrgwhich can use either
773fc57de45Smrg.Fn usbnet_enqueue
774fc57de45Smrgor
775fc57de45Smrg.Fn usbnet_input
776fc57de45Smrgto pass the packets up to higher layers.
777fc57de45SmrgThe typical interface
778fc57de45Smrg.Dq if_start
779fc57de45Smrgfunction and any additional functions used will normal be replaced
780fc57de45Smrgwith a relatively simple
781fc57de45Smrg.Dq uno_tx_prepare
782fc57de45Smrgfunction that simply converts an
783fc57de45Smrg.Va mbuf
784fc57de45Smrginto a
785fc57de45Smrg.Va usbnet_chain
786fc57de45Smrguseful for this device that will be passed onto
787fc57de45Smrg.Fn usbd_transfer .
788fc57de45SmrgThe framework's handling of the Tx interrupt is all internal.
789d3025cbdSmrg.It Interrupt pipe handling
790d3025cbdSmrgFor devices requiring special handling of the interrupt pipe (i.e.,
791d3025cbdSmrgthey use the
792d3025cbdSmrg.Fn usbd_open_pipe_intr
793d3025cbdSmrgmethod), most of the interrupt handler should be deleted, leaving
794d3025cbdSmrgonly code that inspects the result of the interrupt transfer.
795fc57de45Smrg.It Common errors
796fc57de45SmrgIt's common to forget to set link active on devices with MII.
797fc57de45SmrgBe sure to call
798ff96b843Smrg.Fn usbnet_set_link
799fc57de45Smrgduring any status change event.
800fc57de45Smrg.Pp
8017c3a4c78SwizMany locking issues are hidden without
8027c3a4c78Swiz.Dv LOCKDEBUG ,
8037c3a4c78Swizincluding hard-hangs.
8047c3a4c78SwizIt's highly recommended to develop with
8057c3a4c78Swiz.Dv LOCKDEBUG .
806fc57de45Smrg.Pp
807fc57de45SmrgThe
808fc57de45Smrg.Va usbnet
809fc57de45Smrg.Dq un_ed
810fc57de45Smrgarray is unsigned and should use
811fc57de45Smrg.Dq 0
812fc57de45Smrgas the no-endpoint value.
813fc57de45Smrg.El
8142e9fdde4Smrg.Sh SEE ALSO
8152e9fdde4Smrg.Xr usb 4 ,
81650285510Swiz.Xr driver 9 ,
81750285510Swiz.Xr usbd_status 9 ,
8182e9fdde4Smrg.Xr usbdi 9
8192e9fdde4Smrg.Sh HISTORY
8202e9fdde4SmrgThis
8212e9fdde4Smrg.Nm
8222e9fdde4Smrginterface first appeared in
8232e9fdde4Smrg.Nx 9.0 .
824454a193cSmrgPortions of the original design are based upon ideas from
825b480f76eSwiz.An Nick Hudson Aq Mt skrll@NetBSD.org .
82650285510Swiz.Sh AUTHORS
827*2026b728Smrg.An Matthew R. Green Aq Mt mrg@eterna23.net
828