xref: /netbsd-src/share/man/man9/autoconf.9 (revision 5712d03cb7aa9fa35cd42aa0151cbbb2b823de33)
1.\"     $NetBSD: autoconf.9,v 1.35 2021/08/08 16:12:10 andvar 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 August 7, 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" "const struct cfargs *"
56.Ft device_t
57.Fn config_found "device_t parent" "void *aux" "cfprint_t print" \
58"const struct cfargs *"
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" "const struct cfargs *"
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 strongly-typed variadic
123list of arguments to pass information from driver autoconfiguration
124functions to the kernel's autoconfiguration system.
125This list is constructed using the
126.Fn CFARGS
127macro, like this example:
128.Bd -literal -offset indent
129config_search(self, NULL,
130    CFARGS(.search = mainbus_search,
131           .iattr = "mainbus"));
132.Ed
133.Pp
134Each tag is followed by a tag-specific value.
135.Bl -tag -offset indent -width ".Fa devhandle"
136.It Fa submatch
137A pointer to a
138.Sq submatch
139function used in
140.Em direct
141configuration.
142.\"
143.It Fa search
144A pointer to a
145.Sq search
146function used in
147.Em indirect
148configuration.
149.\"
150.It Fa iattr
151A pointer to a constant
152.Tn C
153string
154.Pq Vt "const char *"
155specifying an interface attribute.
156If a parent device carries only a single interface attribute, then this
157argument may be omitted.
158If an interface attribute is specified that the parent device does not
159carry, or no interface attribute is specified and the parent device carries
160more than one, behavior is undefined.
161On kernels built with the
162.Dv DIAGNOSTIC
163option, this may result in an assertion panic.
164.\"
165.It Fa locators
166A pointer to a constant array of integers
167.Pq Vt "const int *"
168containing interface attribute-specific locators.
169.\"
170.It Fa devhandle
171A
172.Vt devhandle_t
173(passed by value) corresponding to the device being attached.
174.El
175.Pp
176If no arguments are to be passed, the special value
177.Dv CFARGS_NONE
178may be used in place of the
179.Fn CFARGS
180macro.
181.Sh FUNCTIONS
182.Bl -tag -width ".Fn config"
183.\"
184.\"
185.It Fn config_search "parent" "aux" "cfargs"
186Cfargs consumed:
187.Fa search ,
188.Fa iattr ,
189.Fa locators .
190.\"
191.Pp
192Performs indirect configuration of physical devices.
193.Fn config_search
194iterates over all potential children, calling the given
195search function
196If no search function is specified,
197.fn config_search
198applies the potential child's match function instead.
199The argument
200.Fa parent
201is the pointer to the parent's device structure.
202If an interface attribute is specified, only potential children eligible to
203attach to that interface attribute will be consulted.
204If specified,
205the locators argument lists the locator values for the device and are passed
206to the search function.
207The given
208.Fa aux
209argument describes the device that has been found and is simply passed
210on through the search function to the child.
211.Fn config_search
212returns a pointer to the configuration data that indicates the best-matched
213child or
214.Dv NULL
215otherwise.
216.Pp
217The role of the search function is to call
218.Fn config_probe
219for each potential child and call
220.Fn config_attach
221for any positive matches.
222If no search function is specified,
223then the parent should record the return value from
224.Fn config_search
225and call
226.Fn config_attach
227itself.
228.Pp
229Note that this function is designed so that it can be used to apply an
230arbitrary function to all potential children.
231In this case callers may choose to ignore the return value.
232.\"
233.\"
234.It Fn config_found "parent" "aux" "print" "cfargs"
235Cfargs consumed:
236.Fa submatch ,
237.Fa iattr ,
238.Fa locators ,
239.Fa devhandle .
240.\"
241.Pp
242Performs direct configuration on a physical device.
243.Fn config_found
244is called by the parent and in turn calls the specified submatch function
245as determined by the configuration table.
246The submatch function compares user-specified locators from the
247machine description file against those specifying a found device,
248calling
249.Fn config_match
250if they match
251.Pq including wildcard matching .
252If a submatch function is not specified, then driver match functions are
253called directly.
254The argument
255.Fa parent
256is the pointer to the parent's device structure.
257If an interface attribute is specified, only potential children eligible to
258attach to that interface attribute will be consulted.
259If specified, the locators argument lists the locator values for the found
260device and may be used by the submatch function and will be recorded in the
261device structure of the child device.
262The given
263.Fa aux
264argument describes the device that has been found.
265.Fn config_found
266internally uses
267.Fn config_search .
268The
269.Vt softc
270structure for the matched device will be allocated, and the
271appropriate driver attach function will be called.
272If the device is matched, the system prints the name of the child and
273parent devices, and then calls the
274.Fa print
275function to produce additional information if desired.
276If no driver takes a match, the same
277.Fa print
278function is called to complain.
279The print function is called with the
280.Fa aux
281argument and, if the matches failed, the full name (including unit
282number) of the parent device, otherwise
283.Dv NULL .
284The
285.Fa print
286function must return an integer value.
287.Pp
288Two special strings,
289.Do
290not configured
291.Dc
292and
293.Do
294unsupported
295.Dc
296will be appended automatically to non-driver reports if the return
297value is
298.Dv UNCONF
299or
300.Dv UNSUPP
301respectively; otherwise the function should
302return the value
303.Dv QUIET .
304If a device handle is specified, that handle will be associated with
305the resulting child device structure if a driver matches.
306.Pp
307.Fn config_found
308returns a pointer to the attached device's
309.Vt device
310structure if the device is attached,
311.Dv NULL
312otherwise.
313Most callers can ignore this value, since the system will already have
314printed a diagnostic.
315.\"
316.\"
317.It Fn config_match "parent" "cf" "aux"
318.\"
319Match a device
320.Pq direct configuration .
321Invokes the driver's match function according to the configuration table.
322The
323.Fn config_match
324function returns a nonzero integer indicating the confidence of
325supporting this device and a value of 0 if the driver doesn't support
326the device.
327.\"
328.\"
329.It Fn config_probe "parent" "cf" "aux"
330.\"
331Probe for a device
332.Pq indirect configuration .
333Invokes the driver's match function according to the configuration table.
334The
335.Fn config_probe
336function returns a nonzero integer to indicate a successful probe
337and a value of 0 otherwise.
338Unlike
339.Fn config_match ,
340the return value of
341.Fn config_probe
342is not intended to reflect a confidence value.
343.\"
344.\"
345.It Fn config_attach "parent" "cf" "aux" "print" "cfargs"
346Cfargs consumed:
347.Fa locators ,
348.Fa devhandle .
349.\"
350.Pp
351Attach a found device.
352Allocates the memory for the
353.Vt softc
354structure and calls the drivers attach function according to the
355configuration table.
356If successful,
357.Fn config_attach
358returns a pointer to the
359.Vt device
360structure.
361If unsuccessful, it returns
362.Dv NULL .
363.\"
364.\"
365.It Fn config_attach_pseudo "cf"
366.\"
367Create an instance of a pseudo-device driver.
368.Xr config 5
369syntax allows the creation of pseudo-devices from which regular
370.Ft device_t
371instances can be created.
372Such objects are similar to the devices that attach at the root of the device
373tree.
374.Pp
375The caller is expected to allocate and fill the
376.Ft cfdata_t
377object and pass it to
378.Fn config_attach_pseudo .
379The content of that object is similar to what is returned by
380.Fn config_search
381for regular devices.
382.\"
383.\"
384.It Fn config_detach "dev" "flags"
385.\"
386Called by the parent to detach the child device.
387The second argument
388.Fa flags
389contains detachment flags.
390Valid values are
391.Dv DETACH_FORCE
392(force detachment, e.g., because of hardware removal)
393and
394.Dv DETACH_QUIET
395(do not print a notice).
396.Fn config_detach
397returns zero if successful and an error code otherwise.
398.Fn config_detach
399is always called from a thread context, allowing condition variables
400to be used while the device detaches itself.
401.\"
402.\"
403.It Fn config_detach_children "dev" "flags"
404.\"
405Iterate through all attached devices, calling
406.Fn config_detach
407for each child of
408.Fa dev ,
409passing
410.Fa flags .
411If detaching any child results in an error, the iteration will halt
412and any remaining devices will not be detached.
413.Fn config_detach_children
414returns zero if successful and an error code otherwise.
415.\"
416.\"
417.It Fn config_deactivate "dev"
418.\"
419Called by the parent to deactivate the child device
420.Fa dev .
421.Fn config_deactivate
422is called from interrupt context to immediately relinquish resources
423and notify dependent kernel subsystems that the device is about to be
424detached.
425At some later point
426.Fn config_detach
427will be called to finalise the removal of the device.
428.\"
429.\"
430.It Fn config_defer "dev" "func"
431.\"
432Called by the child to defer the remainder of its configuration until
433all its parent's devices have been attached.
434At this point, the function
435.Fa func
436is called with the argument
437.Fa dev .
438.\"
439.\"
440.It Fn config_interrupts "dev" "func"
441.\"
442Called by the child to defer the remainder of its configuration until
443interrupts are enabled.
444At this point, the function
445.Fa func
446is called with the argument
447.Fa dev .
448.\"
449.\"
450.It Fn config_mountroot "dev" "func"
451.\"
452Called by the child to defer the remainder of its configuration until
453the root file system is mounted.
454At this point, the function
455.Fa func
456is called with the argument
457.Fa dev .
458This is used for devices that need to load firmware image from
459a mounted file system.
460.\"
461.\"
462.It Fn config_pending_incr
463.\"
464Increment the
465.Va config_pending
466semaphore.
467It is used to account for deferred configurations before
468mounting the root file system.
469.\"
470.\"
471.It Fn config_pending_decr
472.\"
473Decrement the
474.Va config_pending
475semaphore.
476It is used to account for deferred configurations before
477mounting the root file system.
478.\"
479.\"
480.It Fn config_finalize_register "dev" "func"
481.\"
482.\"
483Register a function to be called after all real devices have been found.
484.Pp
485Registered functions are all executed until all of them return 0.
486The callbacks should return 0 to indicate they do not require to be called
487another time, but they should be aware that they still might be in case one of
488them returns 1.
489.El
490.Sh CODE REFERENCES
491The autoconfiguration framework itself is implemented within the file
492.Pa sys/kern/subr_autoconf.c .
493Data structures and function prototypes for the framework are located in
494.Pa sys/sys/device.h .
495.Sh SEE ALSO
496.Xr config 1 ,
497.Xr config 5 ,
498.Xr condvar 9 ,
499.Xr config 9 ,
500.Xr driver 9
501.Sh HISTORY
502Autoconfiguration first appeared in
503.Bx 4.1 .
504The autoconfiguration framework was completely revised in
505.Bx 4.4 .
506The detach and deactivate interfaces appeared in
507.Nx 1.5 .
508