.\" $NetBSD: autoconf.9,v 1.30 2013/03/13 00:09:21 riastradh Exp $
.\"
.\" Copyright (c) 2001, 2002 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Gregory McGarry.
.\"
.\" 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.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``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 FOUNDATION OR CONTRIBUTORS
.\" 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 February 17, 2012
.Dt AUTOCONF 9
.Os
.Sh NAME
.Nm autoconf ,
.Nm config_search_loc ,
.Nm config_search_ia ,
.Nm config_found_sm_loc ,
.Nm config_found_ia ,
.Nm config_found ,
.Nm config_match ,
.Nm config_attach_loc ,
.Nm config_attach ,
.Nm config_attach_pseudo ,
.Nm config_detach ,
.Nm config_detach_children ,
.Nm config_deactivate ,
.Nm config_defer ,
.Nm config_interrupts ,
.Nm config_mountroot ,
.Nm config_pending_incr ,
.Nm config_pending_decr ,
.Nm config_finalize_register
.Nd autoconfiguration framework
.Sh SYNOPSIS
.In sys/param.h
.In sys/device.h
.In sys/errno.h
.Ft cfdata_t
.Fn config_search_loc "cfsubmatch_t func" "device_t parent" "const char *ia" \
"const int *locs" "void *aux"
.Ft cfdata_t
.Fn config_search_ia "cfsubmatch_t func" "device_t parent" "const char *ia" \
"void *aux"
.Ft device_t
.Fn config_found_sm_loc "device_t parent" "const char *ia" "const int *locs" \
"void *aux" "cfprint_t print" "cfsubmatch_t submatch"
.Ft device_t
.Fn config_found_ia "device_t parent" "const char *ia" "void *aux" \
"cfprint_t print"
.Ft device_t
.Fn config_found "device_t parent" "void *aux" "cfprint_t print"
.Ft int
.Fn config_match "device_t parent" "cfdata_t cf" "void *aux"
.Ft device_t
.Fn config_attach_loc "device_t parent" "cfdata_t cf" "const int *locs" \
"void *aux" "cfprint_t print"
.Ft device_t
.Fn config_attach "device_t parent" "cfdata_t cf" "void *aux" \
"cfprint_t print"
.Ft device_t
.Fn config_attach_pseudo "cfdata_t cf"
.Ft int
.Fn config_detach "device_t dev" "int flags"
.Ft int
.Fn config_detach_children "device_t dev" "int flags"
.Ft int
.Fn config_deactivate "device_t dev"
.Ft int
.Fn config_defer "device_t dev" "void (*func)(device_t)"
.Ft void
.Fn config_interrupts "device_t dev" "void (*func)(device_t)"
.Ft void
.Fn config_mountroot "device_t dev" "void (*func)(device_t)"
.Ft void
.Fn config_pending_incr
.Ft void
.Fn config_pending_decr
.Ft int
.Fn config_finalize_register "device_t dev" "int (*func)(device_t)"
.Sh DESCRIPTION
Autoconfiguration is the process of matching hardware devices with an
appropriate device driver.
In its most basic form, autoconfiguration consists of the recursive process
of finding and attaching all devices on a bus, including other busses.
.Pp
The autoconfiguration framework supports
.Em direct configuration
where the bus driver can determine the devices present.
The autoconfiguration framework also supports
.Em indirect configuration
where the drivers must probe the bus looking for the presence of a device.
Direct configuration is preferred since it can find hardware
regardless of the presence of proper drivers.
.Pp
The autoconfiguration process occurs at system bootstrap and is driven
by a table generated from a
.Do
machine description
.Dc
file by
.Xr config 1 .
For a description of the
.Xr config 1
.Do
device definition
.Dc
language, see
.Xr config 9 .
.Pp
Each device must have a name consisting of an alphanumeric string that
ends with a unit number.
The unit number identifies an instance of the driver.
Device data structures are allocated dynamically during
autoconfiguration, giving a unique address for each instance.
.Sh FUNCTIONS
.Bl -tag -width compact
.It Fn config_search_loc "func" "parent" "ia" "locs" "aux"
Performs indirect configuration of physical devices.
.Fn config_search_loc
iterates over all potential children, calling the given
function
.Fa func
for each one.
If
.Fa func
is
.Dv NULL ,
.Fn config_search_loc
applies each child's match function instead.
The argument
.Fa parent
is the pointer to the parent's device structure.
The argument
.Fa ia
is the interface attribute on which the potential children should attach.
It can be
.Dv NULL ,
in which case all children attaching to any attribute are considered.
The
.Fa locs
argument lists the locator values for the device and are passed to function
.Fa func .
The given
.Fa aux
argument describes the device that has been found and is simply passed
on through
.Fa func
to the child.
.Fn config_search_loc
returns a pointer to the best-matched child or
.Dv NULL
otherwise.
.Pp
The role of
.Fa func
is to call
the match function for each device and call
.Fn config_attach_loc
for any positive matches.
If
.Fa func
is
.Dv NULL ,
then the parent should record the return value from
.Fn config_search_loc
and call
.Fn config_attach_loc
itself.
.Pp
Note that this function is designed so that it can be used to apply an
arbitrary function to all potential children.
In this case callers may choose to ignore the return value.
.It Fn config_search_ia "func" "parent" "ia" "aux"
This function is equivalent to calling
.Fn config_search_loc "func" "parent" "ia" "locs" "aux"
with
.Fa locs
set to
.Dv NULL .
.It Fn config_found_sm_loc "parent" "ia" "locs" "aux" "print" "submatch"
Performs direct configuration on a physical device.
.Fn config_found_sm_loc
is called by the parent and in turn calls the
.Fa submatch
function to call the match function as
determined by the configuration table.
If
.Fa submatch
is
.Dv NULL ,
the driver match functions are called directly.
The argument
.Fa parent
is the pointer to the parent's device structure.
The argument
.Fa ia
is the name of the interface attribute on which the child will attach,
per
.Xr config 5
syntax.
The argument
.Fa locs
lists the locator values for the device.
The given
.Fa aux
argument describes the device that has been found.
.Fn config_found_sm_loc
internally uses
.Fn config_search_loc ,
passing on
.Fa submatch , ia , locs
and
.Fa aux .
The
.Em softc
structure for the matched device will be allocated, and the
appropriate driver attach function will be called.
If the device is matched, the system prints the name of the child and
parent devices, and then calls the
.Fa print
function to produce additional information if desired.
If no driver takes a match, the same
.Fa print
function is called to complain.
The print function is called with the
.Fa aux
argument and, if the matches failed, the full name (including unit
number) of the parent device, otherwise
.Dv NULL .
The
.Fa print
function must return an integer value.
.Pp
Two special strings,
.Do
not configured
.Dc
and
.Do
unsupported
.Dc
will be appended automatically to non-driver reports if the return
value is UNCONF or UNSUPP respectively; otherwise the function should
return the value QUIET.
.Pp
.Fn config_found_sm_loc
returns a pointer to the attached device's
.Em softc
structure if the device is attached,
.Dv NULL
otherwise.
Most callers can ignore this value, since the system will already have
printed a diagnostic.
.It Fn config_found_ia "parent" "ia" "aux" "print"
This function is equivalent to calling
.Fn config_found_sm_loc "parent" "ia" "locs" "aux" "print" "submatch"
with
.Fa locs
and
.Fa submatch
set to
.Dv NULL .
It is provided for better source code readability with locator-less device
buses.
.It Fn config_found "parent" "aux" "print"
This function is equivalent to calling
.Fn config_found_sm_loc "parent" "ia" "locs" "aux" "print" "submatch"
with
.Fa ia , locs
and
.Fa submatch
set to
.Dv NULL
and is provided for compatibility with older drivers.
New code should either make the interface attribute explicit or prefer an
indirect method based on
.Fn config_search_loc .
.It Fn config_match "parent" "cf" "aux"
Match a device.
Invokes the drivers match function according to the
configuration table.
The
.Fn config_match
function returns a nonzero integer indicating the confidence of
supporting this device and a value of 0 if the driver doesn't support
the device.
.It Fn config_attach_loc "parent" "cf" "locs" "aux" "print"
Attach a found device.
Allocates the memory for the
.Em softc
structure and calls the drivers attach function according to the
configuration table.
If successful,
.Fn config_attach_loc
returns the
.Em softc .
If unsuccessful, it returns
.Dv NULL .
.It Fn config_attach "parent" "cf" "aux" "print"
This function is equivalent to calling
.Fn config_attach_loc "parent" "cf" "locs" "aux" "print"
with
.Fa locs
set to
.Dv NULL .
.It Fn config_attach_pseudo "cf"
Create an instance of a pseudo-device driver.
.Xr config 5
syntax allows the creation of pseudo-devices from which regular
.Ft device_t
instances can be created.
Such objects are similar to the devices that attach at the root of the device
tree.
.Pp
The caller is expected to allocate and fill the
.Ft cfdata_t
object and pass it to
.Fn config_attach_pseudo .
The content of that object is similar to what is returned by
.Fn config_search_loc
for regular devices.
.It Fn config_detach "dev" "flags"
Called by the parent to detach the child device.
The second argument
.Em flags
contains detachment flags.
Valid values are DETACH_FORCE (force detachment (e.g., because of hardware
removal)) and DETACH_QUIET (do not print a notice).
.Fn config_detach
returns zero if successful and an error code otherwise.
.Fn config_detach
is always called from a thread context, allowing condition variables
to be used while the device detaches itself.
.It Fn config_detach_children "dev" "flags"
Iterate through all attached devices, calling
.Fn config_detach
for each child of
.Fa dev ,
passing
.Fa flags .
If detaching any child results in an error, the iteration will halt
and any remaining devices will not be detached.
.Fn config_detach_children
returns zero if successful and an error code otherwise.
.It Fn config_deactivate "dev"
Called by the parent to deactivate the child device
.Fa dev .
.Fn config_deactivate
is called from interrupt context to immediately relinquish resources
and notify dependent kernel subsystems that the device is about to be
detached.
At some later point
.Fn config_detach
will be called to finalise the removal of the device.
.It Fn config_defer "dev" "func"
Called by the child to defer the remainder of its configuration until
all its parent's devices have been attached.
At this point, the function
.Fa func
is called with the argument
.Fa dev .
.It Fn config_interrupts "dev" "func"
Called by the child to defer the remainder of its configuration until
interrupts are enabled.
At this point, the function
.Fa func
is called with the argument
.Fa dev .
.It Fn config_mountroot "dev" "func"
Called by the child to defer the remainder of its configuration until
the root file system is mounted.
At this point, the function
.Fa func
is called with the argument
.Fa dev .
This is used for devices that need to load firmware image from
a mounted file system.
.It Fn config_pending_incr
Increment the
.Va config_pending
semaphore.
It is used to account for deferred configurations before
mounting the root file system.
.It Fn config_pending_decr
Decrement the
.Va config_pending
semaphore.
It is used to account for deferred configurations before
mounting the root file system.
.It Fn config_finalize_register "dev" "func"
Register a function to be called after all real devices have been found.
.Pp
Registered functions are all executed until all of them return 0.
The callbacks should return 0 to indicate they do not require to be called
another time, but they should be aware that they still might be in case one of
them returns 1.
.El
.Sh CODE REFERENCES
The autoconfiguration framework itself is implemented within the file
.Pa sys/kern/subr_autoconf.c .
Data structures and function prototypes for the framework are located in
.Pa sys/sys/device.h .
.Sh SEE ALSO
.Xr config 1 ,
.Xr config 5 ,
.Xr condvar 9 ,
.Xr config 9 ,
.Xr driver 9
.Sh HISTORY
Autoconfiguration first appeared in
.Bx 4.1 .
The autoconfiguration framework was completely revised in
.Bx 4.4 .
The detach and deactivate interfaces appeared in
.Nx 1.5 .