Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

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