1.\" $NetBSD: autoconf.9,v 1.30 2013/03/13 00:09:21 riastradh Exp $ 2.\" 3.\" Copyright (c) 2001, 2002 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 February 17, 2012 31.Dt AUTOCONF 9 32.Os 33.Sh NAME 34.Nm autoconf , 35.Nm config_search_loc , 36.Nm config_search_ia , 37.Nm config_found_sm_loc , 38.Nm config_found_ia , 39.Nm config_found , 40.Nm config_match , 41.Nm config_attach_loc , 42.Nm config_attach , 43.Nm config_attach_pseudo , 44.Nm config_detach , 45.Nm config_detach_children , 46.Nm config_deactivate , 47.Nm config_defer , 48.Nm config_interrupts , 49.Nm config_mountroot , 50.Nm config_pending_incr , 51.Nm config_pending_decr , 52.Nm config_finalize_register 53.Nd autoconfiguration framework 54.Sh SYNOPSIS 55.In sys/param.h 56.In sys/device.h 57.In sys/errno.h 58.Ft cfdata_t 59.Fn config_search_loc "cfsubmatch_t func" "device_t parent" "const char *ia" \ 60"const int *locs" "void *aux" 61.Ft cfdata_t 62.Fn config_search_ia "cfsubmatch_t func" "device_t parent" "const char *ia" \ 63"void *aux" 64.Ft device_t 65.Fn config_found_sm_loc "device_t parent" "const char *ia" "const int *locs" \ 66"void *aux" "cfprint_t print" "cfsubmatch_t submatch" 67.Ft device_t 68.Fn config_found_ia "device_t parent" "const char *ia" "void *aux" \ 69"cfprint_t print" 70.Ft device_t 71.Fn config_found "device_t parent" "void *aux" "cfprint_t print" 72.Ft int 73.Fn config_match "device_t parent" "cfdata_t cf" "void *aux" 74.Ft device_t 75.Fn config_attach_loc "device_t parent" "cfdata_t cf" "const int *locs" \ 76"void *aux" "cfprint_t print" 77.Ft device_t 78.Fn config_attach "device_t parent" "cfdata_t cf" "void *aux" \ 79"cfprint_t print" 80.Ft device_t 81.Fn config_attach_pseudo "cfdata_t cf" 82.Ft int 83.Fn config_detach "device_t dev" "int flags" 84.Ft int 85.Fn config_detach_children "device_t dev" "int flags" 86.Ft int 87.Fn config_deactivate "device_t dev" 88.Ft int 89.Fn config_defer "device_t dev" "void (*func)(device_t)" 90.Ft void 91.Fn config_interrupts "device_t dev" "void (*func)(device_t)" 92.Ft void 93.Fn config_mountroot "device_t dev" "void (*func)(device_t)" 94.Ft void 95.Fn config_pending_incr 96.Ft void 97.Fn config_pending_decr 98.Ft int 99.Fn config_finalize_register "device_t dev" "int (*func)(device_t)" 100.Sh DESCRIPTION 101Autoconfiguration is the process of matching hardware devices with an 102appropriate device driver. 103In its most basic form, autoconfiguration consists of the recursive process 104of finding and attaching all devices on a bus, including other busses. 105.Pp 106The autoconfiguration framework supports 107.Em direct configuration 108where the bus driver can determine the devices present. 109The autoconfiguration framework also supports 110.Em indirect configuration 111where the drivers must probe the bus looking for the presence of a device. 112Direct configuration is preferred since it can find hardware 113regardless of the presence of proper drivers. 114.Pp 115The autoconfiguration process occurs at system bootstrap and is driven 116by a table generated from a 117.Do 118machine description 119.Dc 120file by 121.Xr config 1 . 122For a description of the 123.Xr config 1 124.Do 125device definition 126.Dc 127language, see 128.Xr config 9 . 129.Pp 130Each device must have a name consisting of an alphanumeric string that 131ends with a unit number. 132The unit number identifies an instance of the driver. 133Device data structures are allocated dynamically during 134autoconfiguration, giving a unique address for each instance. 135.Sh FUNCTIONS 136.Bl -tag -width compact 137.It Fn config_search_loc "func" "parent" "ia" "locs" "aux" 138Performs indirect configuration of physical devices. 139.Fn config_search_loc 140iterates over all potential children, calling the given 141function 142.Fa func 143for each one. 144If 145.Fa func 146is 147.Dv NULL , 148.Fn config_search_loc 149applies each child's match function instead. 150The argument 151.Fa parent 152is the pointer to the parent's device structure. 153The argument 154.Fa ia 155is the interface attribute on which the potential children should attach. 156It can be 157.Dv NULL , 158in which case all children attaching to any attribute are considered. 159The 160.Fa locs 161argument lists the locator values for the device and are passed to function 162.Fa func . 163The given 164.Fa aux 165argument describes the device that has been found and is simply passed 166on through 167.Fa func 168to the child. 169.Fn config_search_loc 170returns a pointer to the best-matched child or 171.Dv NULL 172otherwise. 173.Pp 174The role of 175.Fa func 176is to call 177the match function for each device and call 178.Fn config_attach_loc 179for any positive matches. 180If 181.Fa func 182is 183.Dv NULL , 184then the parent should record the return value from 185.Fn config_search_loc 186and call 187.Fn config_attach_loc 188itself. 189.Pp 190Note that this function is designed so that it can be used to apply an 191arbitrary function to all potential children. 192In this case callers may choose to ignore the return value. 193.It Fn config_search_ia "func" "parent" "ia" "aux" 194This function is equivalent to calling 195.Fn config_search_loc "func" "parent" "ia" "locs" "aux" 196with 197.Fa locs 198set to 199.Dv NULL . 200.It Fn config_found_sm_loc "parent" "ia" "locs" "aux" "print" "submatch" 201Performs direct configuration on a physical device. 202.Fn config_found_sm_loc 203is called by the parent and in turn calls the 204.Fa submatch 205function to call the match function as 206determined by the configuration table. 207If 208.Fa submatch 209is 210.Dv NULL , 211the driver match functions are called directly. 212The argument 213.Fa parent 214is the pointer to the parent's device structure. 215The argument 216.Fa ia 217is the name of the interface attribute on which the child will attach, 218per 219.Xr config 5 220syntax. 221The argument 222.Fa locs 223lists the locator values for the device. 224The given 225.Fa aux 226argument describes the device that has been found. 227.Fn config_found_sm_loc 228internally uses 229.Fn config_search_loc , 230passing on 231.Fa submatch , ia , locs 232and 233.Fa aux . 234The 235.Em softc 236structure for the matched device will be allocated, and the 237appropriate driver attach function will be called. 238If the device is matched, the system prints the name of the child and 239parent devices, and then calls the 240.Fa print 241function to produce additional information if desired. 242If no driver takes a match, the same 243.Fa print 244function is called to complain. 245The print function is called with the 246.Fa aux 247argument and, if the matches failed, the full name (including unit 248number) of the parent device, otherwise 249.Dv NULL . 250The 251.Fa print 252function must return an integer value. 253.Pp 254Two special strings, 255.Do 256not configured 257.Dc 258and 259.Do 260unsupported 261.Dc 262will be appended automatically to non-driver reports if the return 263value is UNCONF or UNSUPP respectively; otherwise the function should 264return the value QUIET. 265.Pp 266.Fn config_found_sm_loc 267returns a pointer to the attached device's 268.Em softc 269structure if the device is attached, 270.Dv NULL 271otherwise. 272Most callers can ignore this value, since the system will already have 273printed a diagnostic. 274.It Fn config_found_ia "parent" "ia" "aux" "print" 275This function is equivalent to calling 276.Fn config_found_sm_loc "parent" "ia" "locs" "aux" "print" "submatch" 277with 278.Fa locs 279and 280.Fa submatch 281set to 282.Dv NULL . 283It is provided for better source code readability with locator-less device 284buses. 285.It Fn config_found "parent" "aux" "print" 286This function is equivalent to calling 287.Fn config_found_sm_loc "parent" "ia" "locs" "aux" "print" "submatch" 288with 289.Fa ia , locs 290and 291.Fa submatch 292set to 293.Dv NULL 294and is provided for compatibility with older drivers. 295New code should either make the interface attribute explicit or prefer an 296indirect method based on 297.Fn config_search_loc . 298.It Fn config_match "parent" "cf" "aux" 299Match a device. 300Invokes the drivers match function according to the 301configuration table. 302The 303.Fn config_match 304function returns a nonzero integer indicating the confidence of 305supporting this device and a value of 0 if the driver doesn't support 306the device. 307.It Fn config_attach_loc "parent" "cf" "locs" "aux" "print" 308Attach a found device. 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_loc 315returns the 316.Em softc . 317If unsuccessful, it returns 318.Dv NULL . 319.It Fn config_attach "parent" "cf" "aux" "print" 320This function is equivalent to calling 321.Fn config_attach_loc "parent" "cf" "locs" "aux" "print" 322with 323.Fa locs 324set to 325.Dv NULL . 326.It Fn config_attach_pseudo "cf" 327Create an instance of a pseudo-device driver. 328.Xr config 5 329syntax allows the creation of pseudo-devices from which regular 330.Ft device_t 331instances can be created. 332Such objects are similar to the devices that attach at the root of the device 333tree. 334.Pp 335The caller is expected to allocate and fill the 336.Ft cfdata_t 337object and pass it to 338.Fn config_attach_pseudo . 339The content of that object is similar to what is returned by 340.Fn config_search_loc 341for regular devices. 342.It Fn config_detach "dev" "flags" 343Called by the parent to detach the child device. 344The second argument 345.Em flags 346contains detachment flags. 347Valid values are DETACH_FORCE (force detachment (e.g., because of hardware 348removal)) and DETACH_QUIET (do not print a notice). 349.Fn config_detach 350returns zero if successful and an error code otherwise. 351.Fn config_detach 352is always called from a thread context, allowing condition variables 353to be used while the device detaches itself. 354.It Fn config_detach_children "dev" "flags" 355Iterate through all attached devices, calling 356.Fn config_detach 357for each child of 358.Fa dev , 359passing 360.Fa flags . 361If detaching any child results in an error, the iteration will halt 362and any remaining devices will not be detached. 363.Fn config_detach_children 364returns zero if successful and an error code otherwise. 365.It Fn config_deactivate "dev" 366Called by the parent to deactivate the child device 367.Fa dev . 368.Fn config_deactivate 369is called from interrupt context to immediately relinquish resources 370and notify dependent kernel subsystems that the device is about to be 371detached. 372At some later point 373.Fn config_detach 374will be called to finalise the removal of the device. 375.It Fn config_defer "dev" "func" 376Called by the child to defer the remainder of its configuration until 377all its parent's devices have been attached. 378At this point, the function 379.Fa func 380is called with the argument 381.Fa dev . 382.It Fn config_interrupts "dev" "func" 383Called by the child to defer the remainder of its configuration until 384interrupts are enabled. 385At this point, the function 386.Fa func 387is called with the argument 388.Fa dev . 389.It Fn config_mountroot "dev" "func" 390Called by the child to defer the remainder of its configuration until 391the root file system is mounted. 392At this point, the function 393.Fa func 394is called with the argument 395.Fa dev . 396This is used for devices that need to load firmware image from 397a mounted file system. 398.It Fn config_pending_incr 399Increment the 400.Va config_pending 401semaphore. 402It is used to account for deferred configurations before 403mounting the root file system. 404.It Fn config_pending_decr 405Decrement the 406.Va config_pending 407semaphore. 408It is used to account for deferred configurations before 409mounting the root file system. 410.It Fn config_finalize_register "dev" "func" 411Register a function to be called after all real devices have been found. 412.Pp 413Registered functions are all executed until all of them return 0. 414The callbacks should return 0 to indicate they do not require to be called 415another time, but they should be aware that they still might be in case one of 416them returns 1. 417.El 418.Sh CODE REFERENCES 419The autoconfiguration framework itself is implemented within the file 420.Pa sys/kern/subr_autoconf.c . 421Data structures and function prototypes for the framework are located in 422.Pa sys/sys/device.h . 423.Sh SEE ALSO 424.Xr config 1 , 425.Xr config 5 , 426.Xr condvar 9 , 427.Xr config 9 , 428.Xr driver 9 429.Sh HISTORY 430Autoconfiguration first appeared in 431.Bx 4.1 . 432The autoconfiguration framework was completely revised in 433.Bx 4.4 . 434The detach and deactivate interfaces appeared in 435.Nx 1.5 . 436