1.\" $NetBSD: autoconf.9,v 1.31 2021/04/28 00:49:22 thorpej Exp $ 2.\" 3.\" Copyright (c) 2001, 2002, 2021 The NetBSD Foundation, Inc. 4.\" All rights reserved. 5.\" 6.\" This code is derived from software contributed to The NetBSD Foundation 7.\" by Gregory McGarry. 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in the 16.\" documentation and/or other materials provided with the distribution. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28.\" POSSIBILITY OF SUCH DAMAGE. 29.\" 30.Dd April 28, 2021 31.Dt AUTOCONF 9 32.Os 33.Sh NAME 34.Nm autoconf , 35.Nm config_search , 36.Nm config_found , 37.Nm config_match , 38.Nm config_attach , 39.Nm config_attach_pseudo , 40.Nm config_detach , 41.Nm config_detach_children , 42.Nm config_deactivate , 43.Nm config_defer , 44.Nm config_interrupts , 45.Nm config_mountroot , 46.Nm config_pending_incr , 47.Nm config_pending_decr , 48.Nm config_finalize_register 49.Nd autoconfiguration framework 50.Sh SYNOPSIS 51.In sys/param.h 52.In sys/device.h 53.In sys/errno.h 54.Ft cfdata_t 55.Fn config_search "device_t parent" "void *aux" "cfarg_t tag" "..." 56.Ft device_t 57.Fn config_found "device_t parent" "void *aux" "cfprint_t print" \ 58"cfarg_t tag" "..." 59.Ft int 60.Fn config_match "device_t parent" "cfdata_t cf" "void *aux" 61.Ft int 62.Fn config_probe "device_t parent" "cfdata_t cf" "void *aux" 63.Ft device_t 64.Fn config_attach "device_t parent" "cfdata_t cf" "void *aux" \ 65"cfprint_t print" "cfarg_t tag" "..." 66.Ft device_t 67.Fn config_attach_pseudo "cfdata_t cf" 68.Ft int 69.Fn config_detach "device_t dev" "int flags" 70.Ft int 71.Fn config_detach_children "device_t dev" "int flags" 72.Ft int 73.Fn config_deactivate "device_t dev" 74.Ft int 75.Fn config_defer "device_t dev" "void (*func)(device_t)" 76.Ft void 77.Fn config_interrupts "device_t dev" "void (*func)(device_t)" 78.Ft void 79.Fn config_mountroot "device_t dev" "void (*func)(device_t)" 80.Ft void 81.Fn config_pending_incr 82.Ft void 83.Fn config_pending_decr 84.Ft int 85.Fn config_finalize_register "device_t dev" "int (*func)(device_t)" 86.Sh DESCRIPTION 87Autoconfiguration is the process of matching hardware devices with an 88appropriate device driver. 89In its most basic form, autoconfiguration consists of the recursive process 90of finding and attaching all devices on a bus, including other busses. 91.Pp 92The autoconfiguration framework supports 93.Em direct configuration 94where the bus driver can determine the devices present. 95The autoconfiguration framework also supports 96.Em indirect configuration 97where the drivers must probe the bus looking for the presence of a device. 98Direct configuration is preferred since it can find hardware 99regardless of the presence of proper drivers. 100.Pp 101The autoconfiguration process occurs at system bootstrap and is driven 102by a table generated from a 103.Do 104machine description 105.Dc 106file by 107.Xr config 1 . 108For a description of the 109.Xr config 1 110.Do 111device definition 112.Dc 113language, see 114.Xr config 9 . 115.Pp 116Each device must have a name consisting of an alphanumeric string that 117ends with a unit number. 118The unit number identifies an instance of the driver. 119Device data structures are allocated dynamically during 120autoconfiguration, giving a unique address for each instance. 121.Pp 122Several of the autoconfiguration functions take a variadic list of 123tag-value arguments to pass information from driver autoconfiguration 124functions to the kernel's autoconfiguration system. 125Each tag is followed by a tag-specific value. 126The end of the argument list must be terminated with the tag 127.Dv CFARG_EOL . 128Passing an invalid tag or an inappropriate value for a tag results 129in undefined behavior and may cause a kernel panic. 130.Bl -tag -width "CFARG_DEVHANDLE" 131.It Dv CFARG_SUBMATCH 132A pointer to a 133.Sq submatch 134function used in direct configuration. 135.It Dv CFARG_SEARCH 136A pointer to a 137.Sq search 138function used in indirect configuration. 139.It Dv CFARG_IATTR 140A pointer to a constant C string 141.Pq const char * 142specifying an interface attribute. 143If a parent device carries only a single interface attribute, then this 144tag-value pair may be omitted. 145If an interface attribute is specified that the parent device does not 146carry, or no interface attribute is specifies and the parent device carries 147more than one, behavior is undefined. 148On kernels built with the 149.Dv DIAGNOSTIC 150option, this may result in an assertion panic. 151.It Dv CFARG_LOCATORS 152A pointer an a constant array of type 153.Sq int 154.Pq const int * 155containing interface attribute-specific locators. 156.It Dv CFARG_DEVHANDLE 157A devhandle_t 158.Pq passed by value 159corresponding to the device being attached. 160.El 161.Sh FUNCTIONS 162.Bl -tag -width compact 163.It Fn config_search "parent" "aux" "tag" "..." 164Performs indirect configuration of physical devices. 165Tag-value arguments consumed: 166.Dv CFARG_SEARCH , 167.Dv CFARG_IATTR , 168.Dv CFARG_LOCATORS . 169.Fn config_search 170iterates over all potential children, calling the given 171search function 172If no search function is specified, 173.fn config_search 174applies the potential child's match function instead. 175The argument 176.Fa parent 177is the pointer to the parent's device structure. 178If an interface attribute is specified, only potential children eligible to 179attach to that interface attribute will be consulted. 180If specified, 181the locators argument lists the locator values for the device and are passed 182to the search function. 183The given 184.Fa aux 185argument describes the device that has been found and is simply passed 186on through the search function to the child. 187.Fn config_search 188returns a pointer to the configuration data that indicates the best-matched 189child or 190.Dv NULL 191otherwise. 192.Pp 193The role of the search function is to call 194.Fn config_probe 195for each potential child and call 196.Fn config_attach 197for any positive matches. 198If no search function is specified, 199then the parent should record the return value from 200.Fn config_search 201and call 202.Fn config_attach 203itself. 204.Pp 205Note that this function is designed so that it can be used to apply an 206arbitrary function to all potential children. 207In this case callers may choose to ignore the return value. 208.It Fn config_found "parent" "aux" "print" "tag" "..." 209Performs direct configuration on a physical device. 210Tag-value arguments consumed: 211.Dv CFARG_SUBMATCH , 212.Dv CFARG_IATTR , 213.Dv CFARG_LOCATORS , 214.Dv CFARG_DEVHANDLE . 215.Fn config_found 216is called by the parent and in turn calls the specified submatch function 217as determined by the configuration table. 218The submatch function compares user-specified locators from the 219machine description file against those specifying a found device, 220calling 221.Fn config_match 222if they match 223.Pq including wildcard matching . 224If a submatch function is not specified, then driver match functions are 225called directly. 226The argument 227.Fa parent 228is the pointer to the parent's device structure. 229If an interface attribute is specified, only potential children eligible to 230attach to that interface attribute will be consulted. 231If specified, the locators argument lists the locator values for the found 232device and may be used by the submatch function and will be recorded in the 233device structure of the child device. 234The given 235.Fa aux 236argument describes the device that has been found. 237.Fn config_found 238internally uses 239.Fn config_search . 240The 241.Em softc 242structure for the matched device will be allocated, and the 243appropriate driver attach function will be called. 244If the device is matched, the system prints the name of the child and 245parent devices, and then calls the 246.Fa print 247function to produce additional information if desired. 248If no driver takes a match, the same 249.Fa print 250function is called to complain. 251The print function is called with the 252.Fa aux 253argument and, if the matches failed, the full name (including unit 254number) of the parent device, otherwise 255.Dv NULL . 256The 257.Fa print 258function must return an integer value. 259.Pp 260Two special strings, 261.Do 262not configured 263.Dc 264and 265.Do 266unsupported 267.Dc 268will be appended automatically to non-driver reports if the return 269value is UNCONF or UNSUPP respectively; otherwise the function should 270return the value QUIET. 271If a device handle is specified, that handle will be associated with 272the resulting child device structure if a driver matches. 273.Pp 274.Fn config_found 275returns a pointer to the attached device's 276.Em device 277structure if the device is attached, 278.Dv NULL 279otherwise. 280Most callers can ignore this value, since the system will already have 281printed a diagnostic. 282.It Fn config_match "parent" "cf" "aux" 283Match a device 284.Pq direct configuration . 285Invokes the driver's match function according to the configuration table. 286The 287.Fn config_match 288function returns a nonzero integer indicating the confidence of 289supporting this device and a value of 0 if the driver doesn't support 290the device. 291.It Fn config_probe "parent" "cf" "aux" 292Probe for a device 293.Pq indirect configuration . 294Invokes the driver's match function according to the configuraiton table. 295The 296.Fn config_probe 297function returns a nonzero integer to indicate a successful probe 298and a value of 0 othersize. 299Unlike 300.Fn config_match , 301the return value of 302.Fn config_probe 303is not intended to reflect a confidence value. 304.It Fn config_attach "parent" "cf" "aux" "print" "tag" "..." 305Attach a found device. 306Tag-value arguments consumed: 307.Dv CFARG_LOCATORS , 308.Dv CFARG_DEVHANDLE . 309Allocates the memory for the 310.Em softc 311structure and calls the drivers attach function according to the 312configuration table. 313If successful, 314.Fn config_attach 315returns a pointer to the 316.Em device 317structure. 318If unsuccessful, it returns 319.Dv NULL . 320.It Fn config_attach_pseudo "cf" 321Create an instance of a pseudo-device driver. 322.Xr config 5 323syntax allows the creation of pseudo-devices from which regular 324.Ft device_t 325instances can be created. 326Such objects are similar to the devices that attach at the root of the device 327tree. 328.Pp 329The caller is expected to allocate and fill the 330.Ft cfdata_t 331object and pass it to 332.Fn config_attach_pseudo . 333The content of that object is similar to what is returned by 334.Fn config_search 335for regular devices. 336.It Fn config_detach "dev" "flags" 337Called by the parent to detach the child device. 338The second argument 339.Em flags 340contains detachment flags. 341Valid values are DETACH_FORCE (force detachment (e.g., because of hardware 342removal)) and DETACH_QUIET (do not print a notice). 343.Fn config_detach 344returns zero if successful and an error code otherwise. 345.Fn config_detach 346is always called from a thread context, allowing condition variables 347to be used while the device detaches itself. 348.It Fn config_detach_children "dev" "flags" 349Iterate through all attached devices, calling 350.Fn config_detach 351for each child of 352.Fa dev , 353passing 354.Fa flags . 355If detaching any child results in an error, the iteration will halt 356and any remaining devices will not be detached. 357.Fn config_detach_children 358returns zero if successful and an error code otherwise. 359.It Fn config_deactivate "dev" 360Called by the parent to deactivate the child device 361.Fa dev . 362.Fn config_deactivate 363is called from interrupt context to immediately relinquish resources 364and notify dependent kernel subsystems that the device is about to be 365detached. 366At some later point 367.Fn config_detach 368will be called to finalise the removal of the device. 369.It Fn config_defer "dev" "func" 370Called by the child to defer the remainder of its configuration until 371all its parent's devices have been attached. 372At this point, the function 373.Fa func 374is called with the argument 375.Fa dev . 376.It Fn config_interrupts "dev" "func" 377Called by the child to defer the remainder of its configuration until 378interrupts are enabled. 379At this point, the function 380.Fa func 381is called with the argument 382.Fa dev . 383.It Fn config_mountroot "dev" "func" 384Called by the child to defer the remainder of its configuration until 385the root file system is mounted. 386At this point, the function 387.Fa func 388is called with the argument 389.Fa dev . 390This is used for devices that need to load firmware image from 391a mounted file system. 392.It Fn config_pending_incr 393Increment the 394.Va config_pending 395semaphore. 396It is used to account for deferred configurations before 397mounting the root file system. 398.It Fn config_pending_decr 399Decrement the 400.Va config_pending 401semaphore. 402It is used to account for deferred configurations before 403mounting the root file system. 404.It Fn config_finalize_register "dev" "func" 405Register a function to be called after all real devices have been found. 406.Pp 407Registered functions are all executed until all of them return 0. 408The callbacks should return 0 to indicate they do not require to be called 409another time, but they should be aware that they still might be in case one of 410them returns 1. 411.El 412.Sh CODE REFERENCES 413The autoconfiguration framework itself is implemented within the file 414.Pa sys/kern/subr_autoconf.c . 415Data structures and function prototypes for the framework are located in 416.Pa sys/sys/device.h . 417.Sh SEE ALSO 418.Xr config 1 , 419.Xr config 5 , 420.Xr condvar 9 , 421.Xr config 9 , 422.Xr driver 9 423.Sh HISTORY 424Autoconfiguration first appeared in 425.Bx 4.1 . 426The autoconfiguration framework was completely revised in 427.Bx 4.4 . 428The detach and deactivate interfaces appeared in 429.Nx 1.5 . 430