1.\" $NetBSD: usbnet.9,v 1.13 2020/08/28 16:07:49 fcambus Exp $ 2.\" 3.\" Copyright (c) 2019 Matthew R. Green 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. The name of the author may not be used to endorse or promote products 15.\" derived from this software without specific prior written permission. 16.\" 17.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 22.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 24.\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27.\" SUCH DAMAGE. 28.\" 29.Dd March 15, 2020 30.Dt USBNET 9 31.Os 32.Sh NAME 33.Nm usbnet 34.Nd common USB Ethernet driver framework 35.Sh SYNOPSIS 36.In dev/usb/usbnet.h 37.Ss Functions offered by usbnet.h 38.Ft void 39.Fn usbnet_set_link "struct usbnet *un" "bool link" 40.Ft void 41.Fn usbnet_set_dying "struct usbnet *un" "bool dying" 42.Ft struct ifnet * 43.Fn usbnet_ifp "struct usbnet *un" 44.Ft struct ethercom * 45.Fn usbnet_ec "struct usbnet *un" 46.Ft struct mii_data * 47.Fn usbnet_mii "struct usbnet *un" 48.Ft krndsource_t * 49.Fn usbnet_rndsrc "struct usbnet *un" 50.Ft void * 51.Fn usbnet_softc "struct usbnet *un" 52.Ft bool 53.Fn usbnet_havelink "struct usbnet *un" 54.Ft bool 55.Fn usbnet_isdying "struct usbnet *un" 56.Ft void 57.Fn usbnet_lock_core "struct usbnet *un" 58.Ft void 59.Fn usbnet_unlock_core "struct usbnet *un" 60.Ft kmutex_t * 61.Fn usbnet_mutex_core "struct usbnet *un" 62.Ft void 63.Fn usbnet_isowned_core "struct usbnet *un" 64.Ft void 65.Fn usbnet_lock_rx "struct usbnet *un" 66.Ft void 67.Fn usbnet_unlock_rx "struct usbnet *un" 68.Ft kmutex_t * 69.Fn usbnet_mutex_rx "struct usbnet *un" 70.Ft void 71.Fn usbnet_isowned_rx "struct usbnet *un" 72.Ft void 73.Fn usbnet_lock_tx "struct usbnet *un" 74.Ft void 75.Fn usbnet_unlock_tx "struct usbnet *un" 76.Ft kmutex_t * 77.Fn usbnet_mutex_tx "struct usbnet *un" 78.Ft void 79.Fn usbnet_isowned_tx "struct usbnet *un" 80.Ft int 81.Fn usbnet_init_rx_tx "struct usbnet *un" "unsigned rxflags" "unsigned txflags" 82.Ft int 83.Fn usbnet_miibus_readreg "device_t dev" "int phy" "int reg" "uint16_t *val" 84.Ft int 85.Fn usbnet_miibus_writereg "device_t dev" "int phy" "int reg" "uint16_t val" 86.Ft void 87.Fn usbnet_miibus_statchg "struct ifnet *" 88.Ft void 89.Fn usbnet_busy "struct usbnet *un" 90.Ft void 91.Fn usbnet_unbusy "struct usbnet *un" 92.Ft void 93.Fn usbnet_enqueue "struct usbnet *un" "uint8_t *buf" "size_t buflen" "int csum_flags" "uint32_t csum_data" "int mbuf_flags" 94.Ft void 95.Fn usbnet_input "struct usbnet *un" "uint8_t *buf" "size_t buflen" 96.Ft void 97.Fn usbnet_attach "struct usbnet *un" "const char *detname" 98.Ft void 99.Fn usbnet_attach_ifp "struct usbnet *un" "unsigned if_flags" "unsigned if_extflags" "const struct usbnet_mii *unm" 100.Ft int 101.Fn usbnet_detach "device_t dev" "int flags" 102.Ft int 103.Fn usbnet_activate "device_t dev" "devact_t act" 104.Ft void 105.Fn usbnet_stop "struct usbnet *un" "struct ifnet *ifp" "int disable" 106.Sh DESCRIPTION 107The 108.Nm 109framework provides methods usable for USB Ethernet drivers. 110The framework has support for these features: 111.Bl -bullet -offset 8n 112.It 113Partial autoconf handling 114.It 115USB endpoint pipe handling 116.It 117Rx and Tx chain handling 118.It 119Generic handlers or support for several struct ifnet callbacks 120.It 121Network stack locking protocol 122.It 123Interrupt handling 124.El 125.Pp 126.Nm 127provides many or all of the traditional 128.Dq softc 129members inside 130.Va struct usbnet , 131which can be used directly as the device softc structure if 132no additional storage is required. 133A structure exists for receive and transmit chain management, 134.Va struct usbnet_chain , 135that tracks the metadata for each transfer descriptor available, 136minimum of one each for Rx and Tx slot, and will be passed 137to the Rx and Tx callbacks. 138.Pp 139There is a 140.Va struct usbnet_ops 141structure that provides a number of optional and required callbacks 142that will be described below. 143.Pp 144For autoconfiguration the device attach routine is expected to 145ensure that this device's 146.Va struct usbnet 147is the first member of the device softc, if it can not be used directly 148as the device softc, as well as set up the necessary structure members, 149find end-points, find the Ethernet address if relevant, call 150.Fn usbnet_attach , 151set up interface, Ethernet, and MII capabilities, and finally call 152.Fn usbnet_attach_ifp . 153The device detach routine should free any resources allocated 154by attach and then call 155.Fn usbnet_detach , 156possibly directly using 157.Fn usbnet_detach 158as most consumers have no additional resources not owned and 159released by the 160.Nm 161framework itself. 162The device activate function should be set to 163.Fn usbnet_activate . 164.Pp 165To manage all Rx and Tx chains the 166.Dq uno_init 167callback of 168.Va struct usbnet_ops 169should perform any device specific initialization and then call 170.Fn usbnet_init_rx_tx 171which will allocate chains, set up and open pipes, and start the 172Rx transfers so that packets can arrived. 173These allocations and pipes can be closed and destroyed by calling 174.Fn usbnet_stop . 175Both of 176.Fn usbnet_init_rx_tx 177and 178.Fn usbnet_stop 179must be called with the 180.Nm 181lock held, see 182.Fn usbnet_lock 183and 184.Fn usbnet_unlock . 185See the 186.Sx RECEIVE AND SEND 187section for details on using the chains. 188.Pp 189The interface init, ioctl, start, and stop, routines are handled by the 190framework with callbacks for device-specific handling. 191For interface init (i.e., when bringing the interface up), the 192.Dq uno_init 193callback should perform any device specific initialization and then call 194.Fn usbnet_init_rx_tx 195to finalize Rx and Tx queue initialization. 196For interface ioctl, most of the handling is in the framework and the 197optional 198.Dq uno_ioctl 199callback should be used to program special settings 200like multicast filters or offload handling. 201If ioctl handling requires capturing device-specific ioctls then the 202.Dq uno_override_ioctl 203callback may be used instead to replace the framework's 204ioctl handler completely (i.e., the replacement should call any generic 205ioctl handlers such as 206.Fn ether_ioctl 207as required.) 208For interface start, the 209.Dq uno_tx_prepare 210callback must be used to convert 211an mbuf into a chain buffer ready for transmission. 212For interface stop, there is an optional 213.Dq uno_stop 214callback to turn off any chipset specific values if required. 215.Pp 216For devices requiring MII handling there are callbacks for reading and 217writing registers, and for status change events. 218The framework serializes MII access with the core lock, which will be 219held when calling these functions, and this lock should be used by 220internal code that also requires serialized access to registers with the 221.Fn usbnet_lock_core 222and 223.Fn usbnet_unlock_core 224functions. 225The MII callbacks handle device detach events safely; a reference count 226is taken and released around calls to the callbacks as the MII callbacks 227usually block. 228.Pp 229As receive must handle the case of multiple packets in one buffer, 230the support is split between the driver and the framework. 231A 232.Dq uno_rx_loop 233callback must be provided that loops over the incoming 234packet data found in a chain, performs necessary checking and passes 235the network frame up the stack via either 236.Fn usbnet_enqueue 237or 238.Fn usbnet_input . 239Typically Ethernet devices prefer 240.Fn usbnet_enqueue . 241.Pp 242General accessor functions for 243.Fa struct usbnet : 244.Bl -tag -width 4n 245.It Fn usbnet_set_link un link 246Set the link status for this 247.Fa un 248to 249.Fa link . 250.It Fn usbnet_set_dying un dying 251Set the dying status for this 252.Fa un 253to 254.Fa dying . 255.It Fn usbnet_ifp un 256Returns pointer to this 257.Fa un's 258.Va struct ifnet . 259.It Fn usbnet_ec un 260Returns pointer to this 261.Fa un's 262.Va struct ethercom . 263.It Fn usbnet_mii un 264Returns pointer to this 265.Fa un's 266.Va struct mii_data . 267.It Fn usbnet_rndsrc un 268Returns pointer to this 269.Fa un's 270.Va krndsource_t . 271.It Fn usbnet_softc un 272Returns pointer to this 273.Fa un's 274device softc. 275.It Fn usbnet_havelink un 276Returns true if link is active. 277.It Fn usbnet_isdying un 278Returns true if device is dying (has been pulled or deactivated, 279pending detach.) 280.El 281.Pp 282Reference counting functions for 283.Fa struct usbnet : 284.Bl -tag -width 4n 285.It Fn usbnet_busy un 286Increases the reference count on the driver instance, preventing 287detach from occurring while the driver is blocked accessing the 288device. 289Must be called with the core lock held. 290.It Fn usbnet_unbusy un 291Decreases the reference count on the driver instance. 292Once the final reference has been dropped, if a detach event 293is pending, it is allowed to proceed. 294Must be called with the core lock held. 295.El 296.Pp 297Lock handling functions for 298.Fa struct usbnet : 299.Pp 300.Bl -tag -width 4n -compact 301.It Fn usbnet_lock_core un 302.It Fn usbnet_unlock_core un 303.It Fn usbnet_isowned_core un 304.It Fn usbnet_lock_rx un 305.It Fn usbnet_unlock_rx un 306.It Fn usbnet_isowned_rx un 307.It Fn usbnet_lock_tx un 308.It Fn usbnet_unlock_tx un 309.It Fn usbnet_isowned_tx un 310These groups of three functions provide methods to lock, 311unlock, and assert ownership of one of the three locks provided by 312.Nm . 313The three locks are the 314.Dq core 315lock, the 316.Dq Tx 317lock, and the 318.Dq Rx 319lock. 320.El 321.Pp 322MII access functions for 323.Fa struct usbnet : 324.Bl -tag -width 4n 325.It Fn usbnet_mii_readreg dev phy reg valp 326Read register 327.Fa reg 328on PHY number 329.Fa phy 330and return the value in 331.Fa valp . 332Called with the core lock held. 333.It Fn usbnet_mii_writereg dev phy reg val 334Write register 335.Fa reg 336on PHY number 337.Fa phy 338with 339.Fa val . 340Called with the core lock held. 341.It Fn usbnet_mii_statchg ifp 342Trigger a status change update for interface 343.Fa ifp . 344Called with the core lock held. 345.El 346.Pp 347Buffer enqueue handling for 348.Fa struct usbnet : 349.Bl -tag -width 4n 350.It Fn usbnet_enqueue un buf buflen csum_flags csum_data mbuf_flags 351Enqueue buffer 352.Fa buf 353for length 354.Fa buflen 355with higher layers, using the provided 356.Fa csum_flags , 357and 358.Fa csum_data , 359which are written directly to the mbuf packet header, and 360.Fa mbuf_flags , 361which is or-ed into the mbuf flags for the created mbuf. 362.It Fn usbnet_input un buf buflen 363Enqueue buffer 364.Fa buf 365for length 366.Fa buflen 367with higher layers 368.El 369.Pp 370Autoconfiguration handling for 371.Fa struct usbnet . 372See the 373.Sx AUTOCONFIGURATION 374section for more details about these functions. 375.Bl -tag -width 4n 376.It Fn usbnet_attach un detachname 377Initial stage attach of a usb network device. 378The 379.Fa detachname 380will be used while waiting for final references to drain when detaching. 381.It Fn usbnet_attach_ifp un if_flags if_extflags unm 382Final stage attach of usb network device. 383If the passed in 384.Fa unm 385is 386.Pf non- Dv NULL 387then an MII interface will be created using the values 388provided in the 389.Fa struct usbnet_mii 390structure, which has these members passed to 391.Fn mii_attach : 392.Bl -tag -width 4n 393.It un_mii_flags 394Flags. 395.It un_mii_capmask 396Capability mask. 397.It un_mii_phyloc 398PHY location. 399.It un_mii_offset 400PHY offset. 401.El 402.Pp 403A default 404.Fa unm 405can be set using the 406.Fn USBNET_MII_DECL_DEFAULT 407macro. 408The 409.Fa if_flags 410and 411.Fa if_extflags 412will be or-ed into the interface flags and extflags. 413.It Fn usbnet_detach dev flags 414Device detach. 415Usable as actual device method. 416.It Fn usbnet_activate dev act 417Device activate (deactivate) method. 418Usable as actual device method. 419.It Fn usbnet_stop un ifp disable 420Interface stop routine. 421.El 422.Sh AUTOCONFIGURATION 423The framework expects the usbnet structure to have these members 424filled in with valid values or functions: 425.Bl -tag -width 6n 426.It un_sc 427Real softc allocated by autoconf and provided to attach, should be 428set to the usbnet structure if no device-specific softc is needed. 429.It un_dev 430device_t saved in attach, used for messages mostly. 431.It un_iface 432The USB iface handle for data interactions, see 433.Fn usbd_device2interface_handle 434for more details. 435.It un_udev 436The struct usbd_device for this device, provided as the usb_attach_arg's 437.Va uaa_device 438member. 439.It un_ops 440Points to a 441.Va struct usbnet_ops 442structure which contains these members: 443.Bl -tag -width 4n 444.It Ft void Fn (*uno_stop) "struct ifnet *ifp" "int disable" 445Stop interface 446.Pq optional . 447Called with the core lock held and with a busy reference. 448.It Ft int Fn (*uno_ioctl) "struct ifnet *ifp" "u_long cmd" "void *data" 449Simple ioctl callback 450.Pq optional . 451May be called with the ifnet lock held. 452.It Ft int Fn (*uno_override_ioctl) "struct ifnet *ifp" "u_long cmd" "void *data" 453Full ioctl callback 454.Pq optional . 455May be called with the ifnet lock held. 456.It Ft int Fn (*uno_init) "struct ifnet *ifp" 457Initialize (bring up) interface. 458Required. 459Called with the ifnet lock held. 460Must call 461.Fn usbnet_rx_tx_init . 462.It Ft int Fn (*uno_read_reg) "struct usbnet *un" "int phy" "int reg" "uint16_t *val" 463Read MII register. 464Required with MII. 465Called with the core lock held and with a busy reference. 466.It Ft int Fn (*uno_write_reg) "struct usbnet *un" "int phy" "int reg" "uint16_t val" 467Write MII register. 468Required with MII. 469Called with the core lock held and with a busy reference. 470.It Ft usbd_status Fn (*uno_statchg) "struct ifnet *ifp" 471Handle MII status change. 472Required with MII. 473Called with the core lock held and with a busy reference. 474.It Ft unsigned Fn (*uno_tx_prepare) "struct usbnet *un" "struct mbuf *m" "struct usbnet_chain *c" 475Prepare an mbuf for transmit. 476Required. 477Called with the Tx lock held. 478.It Ft void Fn (*uno_rx_loop) "struct usbnet *un" "struct usbnet_chain *c" "uint32_t total_len" 479Prepare one or more chain for enqueue. 480Required. 481Called with the Rx lock held. 482.It Ft void Fn (*uno_intr) "struct usbnet *un" "usbd_status status" 483Process periodic interrupt 484.Pq optional . 485Called with no locks held. 486.It Ft void Fn (*uno_tick) "struct usbnet *un" 487Called every second with USB task thread context 488.Pq optional . 489Called with no locks held, but a busy reference is maintained across this call. 490.El 491.It un_intr 492Points to a 493.Va struct usbnet_intr 494structure which should have these members set: 495.Bl -tag -width 4n 496.It uni_intr_buf 497If 498.Pf non- Dv NULL , 499points to a buffer passed to 500.Fn usbd_open_pipe_intr 501in the device init callback, along with the size and interval. 502.It uni_intr_bufsz 503Size of interrupt pipe buffer. 504.It uni_intr_interval 505Frequency of the interrupt in milliseconds. 506.El 507.It un_ed 508Array of endpoint descriptors. 509There indexes are provided: 510.Dv USBNET_ENDPT_RX , 511.Dv USBNET_ENDPT_TX , 512and 513.Dv USBNET_ENDPT_INTR . 514The Rx and Tx endpoints are required. 515.It un_phyno 516MII phy number. 517Not used by 518.Nm . 519.It un_eaddr 5206 bytes of Ethernet address that must be provided before calling 521.Fn usbnet_attach_ifp 522if the device has Ethernet. 523.It un_flags 524Device owned flags word. 525The 526.Nm 527framework will not touch this value. 528.It un_rx_xfer_flags 529Passed to 530.Fn usbd_setup_xfer 531for receiving packets. 532.It un_tx_xfer_flags 533Passed to 534.Fn usbd_setup_xfer 535for sending packets. 536.It un_rx_list_cnt 537Number of chain elements to allocate for Rx. 538.It un_tx_list_cnt 539Number of chain elements to allocate for Tx. 540.It un_rx_bufsz 541Rx buffer size. 542.It un_tx_bufsz 543Tx buffer size. 544.El 545.Pp 546The device detach and activate callbacks can typically be set to 547.Fn usbnet_detach 548and 549.Fn usbnet_activate 550unless device-specific handling is required, in which case, they 551can be called before or after such handling. 552.Pp 553The capabilities described in both 554.Va struct ifp 555and 556.Va struct ethercom 557must be set before calling 558.Fn usbnet_attach_ifp . 559.Sh RECEIVE AND SEND 560Receive and send routines are structured around a the 561.Va usbnet_cdata 562and 563.Va usbnet_chain 564structures, the 565.Dv un_ed , 566.Dv un_rx_xfer_flags , 567and 568.Dv un_tx_xfer_flags 569members, and the 570.Fn uno_stop , 571.Fn uno_init , 572.Fn uno_tx_prepare , 573and 574.Fn uno_rx_loop 575callbacks of 576.Va usbnet_ops . 577.Pp 578Typically, the device attach routine will fill in members of the 579.Va usbnet 580structure, as listed in 581.Sx AUTOCONFIGURATION . 582The 583.Dv un_ed 584array should have the 585.Dv USBNET_ENDPT_RX 586and 587.Dv USBNET_ENDPT_TX 588array entries filled in, and optionally the 589.Dv USBNET_ENDPT_INTR 590entry filled in if applicable. 591.Pp 592The optional 593.Fn uno_stop 594callback performs device-specific operations to shutdown the 595transmit or receive handling. 596.Fn uno_stop 597will be called with the usbnet lock held. 598.Pp 599The 600.Fn uno_init 601callback both performs device-specific enablement and then calls 602.Fn usbnet_rx_tx_init , 603which sets up the receive, transmit, and, optionally, the interrupt 604pipes, as well as starting the receive pipes. 605All USB transfer setup is handled internally to the framework, and 606the driver callbacks merely copy data in or out of a chain entry using 607what is typically a device-specific method. 608.Pp 609The 610.Fn uno_rx_loop 611callback converts the provided 612.Va usbnet_chain 613data and length into a series (one or more) of packets that are 614enqueued with the higher layers using either 615.Fn usbnet_enqueue 616(for most devices) or 617.Fn usbnet_input 618for devices that use 619.Fn if_input 620(this currently relies upon the 621.Va struct ifnet 622having the 623.Dq _if_input 624member set as well, which is true for current consumers.) 625The Rx lock will be held during this call, see 626.Fn usbnet_lock_rx 627and 628.Fn usbnet_unlock_rx . 629.Pp 630The 631.Fn uno_tx_prepare 632callback must convert the provided 633.Va struct mbuf 634into the provided 635.Va struct usbnet_chain 636performing any device-specific padding, checksum, header or other. 637Note that this callback must check that it is not attempting to copy 638more than the chain buffer size, as set in the 639.Va usbnet 640.Dq un_tx_bufsz 641member. 642This callback is only called once per packet. 643The Tx lock will be held during this call, see 644.Fn usbnet_lock_tx 645and 646.Fn usbnet_unlock_tx . 647.Pp 648The 649.Fa struct usbnet_chain 650structure which contains a 651.Dq unc_buf 652member which has the chain buffer allocated where data should be 653copied to or from for receive or transmit operations. 654It also contains pointers back to the owning 655.Fa struct usbnet , 656and the 657.Va struct usbd_xfer 658associated with this transfer. 659.Sh MII 660For devices that have MII support these callbacks in 661.Fa struct usbnet_ops 662must be provided: 663.Bl -tag -width 4n 664.It uno_read_reg 665Read an MII register for a particular PHY. 666Returns standard 667.Xr errno 2 . 668.It uno_write_reg 669Write an MII register for a particular PHY. 670Returns standard 671.Xr errno 2 . 672.It uno_statchg 673Handle a status change event for this interface. 674.El 675.Pp 676The read and write callbacks are called with the core lock held. 677See 678.Fn usbnet_lock_mii 679and 680.Fn usbnet_unlock_mii . 681.Sh INTERRUPT PIPE 682The interrupt specific callback, 683.Dq uno_intr , 684is an optional callback that can be called periodically, registered by 685.Nm 686using the 687.Fn usbd_open_pipe_intr 688function (instead of the 689.Fn usbd_open_pipe 690function.) 691The 692.Nm 693framework provides most of the interrupt handling and the callback 694simply inspects the returned buffer as necessary. 695To enable the this callback point the 696.Va struct usbnet 697member 698.Dq un_intr 699to a 700.Va struct usbnet_intr 701structure with these members set: 702.Bl -tag -width 4n 703.It uni_buf 704Data buffer for interrupt status relies. 705.It uni_bufsz 706Size of the above buffer. 707.It uni_interval 708Interval in millieconds. 709.El 710.Pp 711These values will be passed to 712.Fn usbd_open_pipe_intr . 713.Sh CONVERTING OLD-STYLE DRIVERS 714The porting of an older driver to the 715.Nm 716framework is largely an effort in deleting code. 717The process involves making these changes: 718.Bl -tag -width 4n 719.It Headers 720Many headers are included in 721.Pa usbnet.h 722and can be removed from the driver, as well as headers no longer used, 723such as 724.Pa callout.h 725and 726.Pa rndsource.h , 727etc. 728.It Device softc 729The majority of the driver's existing 730.Dq softc 731structure can likely be replaced with usage of 732.Va struct usbnet 733and its related functionality. 734This includes at least the device_t pointer, Ethernet address, the 735ethercom and mii_data structures, end point descriptors, usbd device, 736interface, and task and callout structures (both these probably go 737away entirely) and all the associated watchdog handling, 738timevals, list size, buffer size and xfer flags for 739both Rx, and Tx, and interrupt notices, interface flags, device link, 740PHY number, chain data, locks including Rx, Tx, MII, and the 741base softc lock. 742There is a driver-only 743.Dq un_flags 744in the 745.Va usbnet 746structure available for drivers to use. 747.Pp 748Many drivers can use the 749.Va usbnet 750structure as the device private storage passed to 751.Dv CFATTACH_DECL_NEW . 752Many internal functions to the driver may look better if switched to 753operate on the device's 754.Va usbnet 755as, for example, the 756.Va usbd_device 757value is now available (and must be set by the driver) in the 758.Va usbnet , 759which may be needed for any call to 760.Fn usbd_do_request . 761The standard endpoint values must be stored in the 762.Nm 763.Dq un_ed[] 764array. 765.Pp 766As 767.Nm 768manages xfer chains all code related to the opening, closing, aborting 769and transferring of data on pipes is performed by the framework based 770upon the buffer size and more provided in 771.Va subnet , 772so all code related to them should be deleted. 773.It Interface setup 774The vast majority of interface specific code should be deleted. 775For device-specific interface values, the 776.Va ifnet 777flags and exflags can be set, as well as the 778.Va ethercom 779.Dq ec_capabilities 780member, before calling 781.Fn usbnet_attach_ifp . 782All calls to 783.Fn ifmedia_init , 784.Fn mii_attach , 785.Fn ifmedia_add , 786.Fn ifmedia_set , 787.Fn if_attach , 788.Fn ether_ifattach , 789.Fn rnd_attach_source , 790and 791.Fn usbd_add_drv_event 792should be eliminated. 793The device 794.Dq ioctl 795routine can use the default handling with a callback for additional 796device specific programming (multicast filters, etc.), which can be 797empty, or, the override ioctl can be used for heavier requirements. 798The device 799.Dq stop 800routine is replaced with a simple call that turns off the 801device-specific transmitter and receiver if necessary, as the 802framework handles pipes and transfers and buffers. 803.It Device locking 804The 805.Nm 806framework provides three locks for the system: core lock, 807receive lock, and transmit lock. 808The normal locking order 809for these locks is ifnet lock -> usbnet core lock -> usbnet rxlock -> usbnet 810txlock, or, ifnet lock -> usbnet core lock. 811Also note that the core lock may be taken when the ifnet lock is not 812held. 813.It MII handling 814For devices with MII support the three normal callbacks 815.Pq read, write, and status change 816must be converted to 817.Va usbnet . 818These functions are called with the core lock is held 819.Po 820see 821.Dq Fn usbnet_isowned_core 822.Pc , 823and with a busy reference held and do not require any checking for running, 824or up, or dying devices. 825Local 826.Dq link 827variables need to be replaced with accesses to 828.Fn usbnet_set_link 829and 830.Fn usbnet_havelink . 831Other ifmedia callbacks that were passed to 832.Fn ifmedia_init 833should be deleted and any work moved into 834.Dq uno_statchg . 835.It Receive and Transmit 836The 837.Nm 838framework handles the majority of handling of both network directions. 839The interface init routine should keep all of the device specific setup 840but replace all pipe management with a call to 841.Fn usbnet_init_rx_tx . 842The typical receive handling will normally be replaced with a receive 843loop functions that can accept one or more packets, 844.Dq uno_rx_loop , 845which can use either 846.Fn usbnet_enqueue 847or 848.Fn usbnet_input 849to pass the packets up to higher layers. 850The typical interface 851.Dq if_start 852function and any additional functions used will normal be replaced 853with a relatively simple 854.Dq uno_tx_prepare 855function that simply converts an 856.Va mbuf 857into a 858.Va usbnet_chain 859useful for this device that will be passed onto 860.Fn usbd_transfer . 861The framework's handling of the Tx interrupt is all internal. 862.It Interrupt pipe handling 863For devices requiring special handling of the interrupt pipe (i.e., 864they use the 865.Fn usbd_open_pipe_intr 866method), most of the interrupt handler should be deleted, leaving 867only code that inspects the result of the interrupt transfer. 868.It Common errors 869It's common to forget to set link active on devices with MII. 870Be sure to call 871.Fn usbent_set_link 872during any status change event. 873.Pp 874Many locking issues are hidden without 875.Dv LOCKDEBUG , 876including hard-hangs. 877It's highly recommended to develop with 878.Dv LOCKDEBUG . 879.Pp 880The 881.Va usbnet 882.Dq un_ed 883array is unsigned and should use 884.Dq 0 885as the no-endpoint value. 886.El 887.Sh SEE ALSO 888.Xr usb 4 , 889.Xr driver 9 , 890.Xr usbd_status 9 , 891.Xr usbdi 9 892.Sh HISTORY 893This 894.Nm 895interface first appeared in 896.Nx 9.0 . 897Portions of the original design are based upon ideas from 898.An Nick Hudson Aq Mt skrll@NetBSD.org . 899.Sh AUTHORS 900.An Matthew R. Green Aq Mt mrg@eterna.com.au 901