NetBSD/share/man/man9/driver.9

390 lines
11 KiB
Groff
Raw Normal View History

.\" $NetBSD: driver.9,v 1.14 2005/06/20 13:25:25 peter Exp $
2001-07-01 08:11:13 +04:00
.\"
.\" Copyright (c) 2001 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.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the NetBSD
.\" Foundation, Inc. and its contributors.
.\" 4. Neither the name of The NetBSD Foundation nor the names of its
.\" contributors may be used to endorse or promote products derived
.\" from this software without specific prior written permission.
.\"
.\" 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
2001-09-04 06:51:15 +04:00
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2001-07-01 08:11:13 +04:00
.\" 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 September 6, 2002
2001-07-01 08:11:13 +04:00
.Dt DRIVER 9
.Os
.Sh NAME
.Nm driver
.Nd description of a device driver
.Sh SYNOPSIS
.In sys/param.h
.In sys/device.h
.In sys/errno.h
2001-07-01 08:11:13 +04:00
.Ft static int
.Fn foo_match "struct device *parent" "struct cfdata *match" "void *aux"
.Ft static void
.Fn foo_attach "struct device *parent" "struct device *self" "void *aux"
.Ft static int
.Fn foo_detach "struct device *self" "int flags"
.Ft static int
.Fn foo_activate "struct device *self" "enum devact act"
.Sh DESCRIPTION
This page briefly describes the basic
.Nx
autoconfiguration interface used by device drivers.
2002-10-14 17:43:14 +04:00
For a detailed overview of the autoconfiguration framework see
2001-07-01 08:11:13 +04:00
.Xr autoconf 9 .
.Pp
Each device driver must present to the system a standard
2002-10-14 17:43:14 +04:00
autoconfiguration interface.
This interface is provided by the
2001-07-01 08:11:13 +04:00
.Em cfattach
2002-10-14 17:43:14 +04:00
structure.
The interface to the driver is constant and is defined
statically inside the driver.
For example, the interface to driver
.Dq foo
2001-07-01 08:11:13 +04:00
is defined with:
.Pp
.Bd -literal
CFATTACH_DECL(foo, /* driver name */
2001-07-01 08:11:13 +04:00
sizeof(struct foo_softc), /* size of instance data */
foo_match, /* match/probe function */
foo_attach, /* attach function */
foo_detach, /* detach function */
foo_activate); /* activate function */
2001-07-01 08:11:13 +04:00
.Ed
.Pp
For each device instance controlled by the driver, the
autoconfiguration framework allocates a block of memory to record
2002-10-14 17:43:14 +04:00
device-instance-specific driver variables.
The size of this memory block is specified by the second argument in the
.Em CFATTACH_DECL
macro.
2002-10-14 17:43:14 +04:00
The memory block is referred to as the driver's
2001-07-01 08:11:13 +04:00
.Em softc
2002-10-14 17:43:14 +04:00
structure.
The
2001-07-01 08:11:13 +04:00
.Em softc
structure is only accessed within the driver, so its definition is
2002-10-14 17:43:14 +04:00
local to the driver.
Nevertheless, the
2001-07-01 08:11:13 +04:00
.Em softc
structure should adopt the standard
.Nx
2002-10-14 17:43:14 +04:00
configuration and naming conventions.
For example, the
2001-07-01 08:11:13 +04:00
.Em softc
structure for driver
.Dq foo
2001-07-01 08:11:13 +04:00
is defined with:
.Pp
.Bd -literal
struct foo_softc {
struct device sc_dev; /* generic device info */
/* device-specific state */
};
.Ed
.Pp
The autoconfiguration framework mandates that the first member of the
.Em softc
structure must be the driver-independent
.Em struct device .
Probably its most useful aspect to the driver is that it contains the
device-instance name
.Em dv_xname .
.Pp
If a driver has character device interfaces accessed from userland, the driver
must define the
.Em cdevsw
2002-10-14 17:43:14 +04:00
structure.
The structure is constant and is defined inside the driver.
For example, the
.Em cdevsw
structure for driver
.Dq foo
is defined with:
.Pp
.Bd -literal
const struct cdevsw foo_cdevsw {
int (*d_open)(dev_t, int, int, struct proc *);
int (*d_close)(dev_t, int, int, struct proc *);
int (*d_read)(dev_t, struct uio *, int);
int (*d_write)(dev_t, struct uio *, int);
int (*d_ioctl)(dev_t, u_long, caddr_t, int, struct proc *);
struct tty *(*d_tty)(dev_t);
int (*d_poll)(dev_t, int, struct proc *);
paddr_t (*d_mmap)(dev_t, off_t, int);
int d_type;
};
.Ed
.Pp
The structure variable must be named foo_cdevsw by appending the letters
.Dq _cdevsw
2002-10-14 17:43:14 +04:00
to the driver's base name.
This convention is mandated by the autoconfiguration framework.
.Pp
If the driver
.Dq foo
has also block device interfaces, the driver must define the
.Em bdevsw
2002-10-14 17:43:14 +04:00
structure.
The structure is constant and is defined inside the driver.
For example, the
.Em bdevsw
structure for driver
.Dq foo
is defined with:
.Pp
.Bd -literal
const struct bdevsw foo_bdevsw {
int (*d_open)(dev_t, int, int, struct proc *);
int (*d_close)(dev_t, int, int, struct proc *);
void (*d_strategy)(struct buf *);
int (*d_ioctl)(dev_t, u_long, caddr_t, int, struct proc *);
int (*d_dump)(dev_t, daddr_t, caddr_t, size_t);
int (*d_psize)(dev_t);
int d_type;
};
.Ed
.Pp
The structure variable must be named foo_bdevsw by appending the letters
.Dq _bdevsw
2002-10-14 17:43:14 +04:00
to the driver's base name.
This convention is mandated by the autoconfiguration framework.
.Pp
2001-07-01 08:11:13 +04:00
During system bootstrap, the autoconfiguration framework searches the
2002-10-14 17:43:14 +04:00
system for devices.
For each device driver, its match function is called
2001-09-04 06:51:15 +04:00
.Po
2001-07-01 08:11:13 +04:00
via its
.Em cfattach
2001-09-04 06:51:15 +04:00
structure
2001-07-01 08:11:13 +04:00
.Pc
2002-10-14 17:43:14 +04:00
to match the driver with a device instance.
The match function is called with three arguments.
This first argument
2001-07-01 08:11:13 +04:00
.Fa parent
2002-10-14 17:43:14 +04:00
is a pointer to the driver's parent device structure.
The second argument
2001-07-01 08:11:13 +04:00
.Fa match
is a pointer to a data structure describing the autoconfiguration
2002-10-14 17:43:14 +04:00
framework's understanding of the driver.
Both the
2001-07-01 08:11:13 +04:00
.Fa parent
and
.Fa match
2002-10-14 17:43:14 +04:00
arguments are ignored by most drivers.
The third argument
2001-07-01 08:11:13 +04:00
.Fa aux
contains a pointer to a structure describing a potential
2002-10-14 17:43:14 +04:00
device-instance.
It is passed to the driver from the parent.
2001-07-01 08:11:13 +04:00
The match function would type-cast the
.Fa aux
argument to its appropriate attachment structure and use its contents
2002-10-14 17:43:14 +04:00
to determine whether it supports the device.
Depending on the device hardware, the contents of the attachment
structure may contain
.Dq locators
2001-07-01 08:11:13 +04:00
to locate the device instance so that the driver can probe it for its
2002-10-14 17:43:14 +04:00
identity.
If the probe process identifies additional device properties, it may
modify the members of the attachment structure.
2001-07-01 08:11:13 +04:00
For these devices, the
.Nx
convention is to
call the match routine
.Fn foo_probe
instead of
.Fn foo_match
2002-10-14 17:43:14 +04:00
to make this distinction clear.
Either way, the 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.
2001-07-01 08:11:13 +04:00
Generally, only a single driver exists for a device, so the match
function returns 1 for a positive match.
.Pp
The autoconfiguration framework will call the attach function
2001-09-04 06:51:15 +04:00
.Po
2001-07-01 08:11:13 +04:00
via its
.Em cfattach
2001-09-04 06:51:15 +04:00
structure
2001-07-01 08:11:13 +04:00
.Pc
of the driver which returns the highest value from its match function.
2002-10-14 17:43:14 +04:00
The attach function is called with three arguments.
The attach function performs the necessary process to initialise the
device for operation.
The first argument
2001-07-01 08:11:13 +04:00
.Fa parent
2002-10-14 17:43:14 +04:00
is a pointer to the driver's parent device structure.
The second argument
2001-07-01 08:11:13 +04:00
.Fa self
2002-10-14 17:43:14 +04:00
is a pointer to the driver's device structure.
It is also a pointer to our
2001-07-01 08:11:13 +04:00
.Em softc
2002-10-14 17:43:14 +04:00
structure since the device structure is its first member.
The third argument
2001-07-01 08:11:13 +04:00
.Fa aux
2002-10-14 17:43:14 +04:00
is a pointer to the attachment structure.
The
2001-07-01 08:11:13 +04:00
.Fa parent
and
.Fa aux
2002-04-02 18:16:34 +04:00
arguments are the same as passed to the match function.
2001-07-01 08:11:13 +04:00
.Pp
The driver's attach function is called before system interrupts are
2002-10-14 17:43:14 +04:00
enabled.
If interrupts are required during initialisation, then the attach
function should make use of
2001-07-01 08:11:13 +04:00
.Fn config_interrupts
.Po
see
.Xr autoconf 9
.Pc .
.Pp
Some devices can be removed from the system without requiring a system
2002-10-14 17:43:14 +04:00
reboot.
The autoconfiguration framework calls the driver's detach function
2001-07-01 08:11:13 +04:00
.Po
via its
.Em cfattach
structure
.Pc
2002-10-14 17:43:14 +04:00
during device detachment.
If the device does not support detachment, then the driver does not
have to provide a detach function.
The detach function is used to relinquish resources allocated to the
driver which are no longer needed.
The first argument
2001-07-01 08:11:13 +04:00
.Fa self
2002-10-14 17:43:14 +04:00
is a pointer to the driver's device structure.
It is the same structure as passed to the attach function.
The second argument
2001-07-01 08:11:13 +04:00
.Fa flags
2002-10-14 17:43:14 +04:00
contains detachment flags.
Valid values are DETACH_FORCE
2001-07-01 08:11:13 +04:00
.Po
force detachment; hardware gone
.Pc and DETACH_QUIET
.Po
do not print a notice
.Pc .
.Pp
The autoconfiguration framework calls the driver's activate function
to notify the driver of a change in the resources that have been
2002-10-14 17:43:14 +04:00
allocated to it.
For example, an Ethernet driver has to be notified if the network stack
is being added or removed from the kernel.
The first argument to the activate function
2001-07-01 08:11:13 +04:00
.Fa self
2002-10-14 17:43:14 +04:00
is a pointer to the driver's device structure.
It is the same argument as passed to the attach function.
The second argument
2001-07-01 08:11:13 +04:00
.Fa act
2002-10-14 17:43:14 +04:00
describes the action.
Valid actions are DVACT_ACTIVATE
2001-07-01 08:11:13 +04:00
.Po
activate the device
2001-09-04 06:51:15 +04:00
.Pc and DVACT_DEACTIVATE
2001-07-01 08:11:13 +04:00
.Po
deactivate the device
.Pc .
If the action is not supported the activate function should return
EOPNOTSUPP.
2001-07-01 08:11:13 +04:00
The activate function is called in interrupt context.
.Pp
2002-10-14 17:43:14 +04:00
Most drivers will want to make use of interrupt facilities.
Interrupt locators provided through the attachment structure should be
used to establish interrupts within the system.
Generally, an interrupt interface is provided by the parent.
The interface will require a handler and a driver-specific argument
to be specified.
This argument is usually a pointer to the device-instance-specific
softc structure.
2001-07-01 08:11:13 +04:00
When a hardware interrupt for the device occurs the handler is called
2002-10-14 17:43:14 +04:00
with the argument.
Interrupt handlers should return 0 for
.Dq interrupt not for me ,
2001-07-01 08:11:13 +04:00
1 for
.Dq I took care of it ,
2001-07-01 08:11:13 +04:00
or -1 for
.Do
I guess it was mine, but I wasn't expecting it
.Dc .
.Pp
For a driver to be compiled into the kernel,
.Xr config 1
2002-10-14 17:43:14 +04:00
must be aware of its existence.
This is done by including an entry in files.\*[Lt]bus\*[Gt] in the
directory containing the driver.
For example, the driver
.Dq foo
2001-07-01 08:11:13 +04:00
attaching to bus
.Dq bar
2001-07-01 08:11:13 +04:00
with dependency on kernel module
.Dq baz
2001-07-01 08:11:13 +04:00
has the entry:
.Bd -literal
device foo: baz
attach foo at bar
file dev/bar/foo.c foo
.Ed
.Pp
An entry can now be added to the machine description file:
.Bd -literal
foo* at bar?
.Ed
.Pp
For device interfaces of a driver to be compiled into the kernel,
.Xr config 1
2002-10-14 17:43:14 +04:00
must be aware of its existence.
2004-05-16 20:56:01 +04:00
This is done by including an entry in majors.<arch>.
2002-10-14 17:43:14 +04:00
For example, the driver
.Dq foo
with character device interfaces, a character major device number
.Dq cmaj ,
block device interfaces, a block device major number
.Dq bmaj
and dependency on kernel module
.Dq baz
has the entry:
.Bd -literal
device-major foo char cmaj block bmaj baz
.Ed
.Pp
2001-07-01 08:11:13 +04:00
For a detailed description of the machine description file and the
.Dq device definition
2001-07-01 08:11:13 +04:00
language see
.Xr config 9 .
.Sh SEE ALSO
.Xr config 1 ,
2001-07-01 08:11:13 +04:00
.Xr autoconf 9 ,
.Xr config 9 ,
.Xr powerhook_establish 9 ,
2001-12-26 03:16:30 +03:00
.Xr shutdownhook_establish 9