indention and spelling fixes.
This commit is contained in:
parent
53f5f0487c
commit
06e1a7b45d
Notes:
svn2git
2020-12-08 03:00:23 +00:00
svn path=/head/; revision=9153
3 changed files with 402 additions and 351 deletions
en_US.ISO8859-1/books
en_US.ISO_8859-1/books/developers-handbook/driverbasics
|
@ -1,65 +1,68 @@
|
|||
<!--
|
||||
The FreeBSD Documentation Project
|
||||
|
||||
$FreeBSD: doc/en_US.ISO_8859-1/books/developers-handbook/driverbasics/chapter.sgml,v 1.3 2000/12/04 11:39:41 alex Exp $
|
||||
$FreeBSD: doc/en_US.ISO_8859-1/books/developers-handbook/driverbasics/chapter.sgml,v 1.4 2001/04/09 00:33:42 dd Exp $
|
||||
-->
|
||||
|
||||
<chapter id="driverbasics">
|
||||
<title>Writing FreeBSD Device Drivers</title>
|
||||
<chapter id="driverbasics">
|
||||
<title>Writing FreeBSD Device Drivers</title>
|
||||
|
||||
<para>This chapter was written by Murray Stokely with selections from
|
||||
a variety of sources including the intro(4) man page by Joerg
|
||||
Wunsch.</para>
|
||||
<para>This chapter was written by &.murray with selections
|
||||
from a variety of sources including the intro(4) man page by Joerg
|
||||
Wunsch.</para>
|
||||
|
||||
<sect1>
|
||||
<title>Introduction</title>
|
||||
<para>
|
||||
This chapter provides a brief introduction to writing device
|
||||
drivers for FreeBSD. A device in this context is a term used
|
||||
mostly for hardware-related stuff that belongs to the system,
|
||||
like disks, printers, or a graphics display with its keyboard.
|
||||
A device driver is the software component of the operating
|
||||
system that controls a specific device. There are also
|
||||
so-called pseudo-devices where a device driver emulates the
|
||||
behaviour of a device in software without any particular
|
||||
underlying hardware. Device drivers can be compiled into the
|
||||
system statically or loaded on demand through the dynamic
|
||||
kernel linker facility `kld'.</para>
|
||||
<sect1>
|
||||
<title>Introduction</title>
|
||||
<para>This chapter provides a brief introduction to writing device
|
||||
drivers for FreeBSD. A device in this context is a term used
|
||||
mostly for hardware-related stuff that belongs to the system,
|
||||
like disks, printers, or a graphics display with its keyboard.
|
||||
A device driver is the software component of the operating
|
||||
system that controls a specific device. There are also
|
||||
so-called pseudo-devices where a device driver emulates the
|
||||
behaviour of a device in software without any particular
|
||||
underlying hardware. Device drivers can be compiled into the
|
||||
system statically or loaded on demand through the dynamic kernel
|
||||
linker facility `kld'.</para>
|
||||
|
||||
<para>Most devices in a Unix-like operating system are
|
||||
accessed through device-nodes, sometimes also called special
|
||||
files. These files are usually located under the directory
|
||||
<filename>/dev</filename> in the file system hierarchy. Until
|
||||
devfs is fully integrated into FreeBSD, each device node must
|
||||
be created statically and independent of the existence of the
|
||||
associated device driver. Most device nodes on the system are
|
||||
created by running <command>MAKEDEV</command>.</para>
|
||||
<para>Most devices in a Unix-like operating system are accessed
|
||||
through device-nodes, sometimes also called special files.
|
||||
These files are usually located under the directory
|
||||
<filename>/dev</filename> in the file system hierarchy. Until
|
||||
devfs is fully integrated into FreeBSD, each device node must be
|
||||
created statically and independent of the existence of the
|
||||
associated device driver. Most device nodes on the system are
|
||||
created by running <command>MAKEDEV</command>.</para>
|
||||
|
||||
<para>Device drivers can roughly be broken down into two
|
||||
categories; character and network device drivers.</para>
|
||||
</sect1>
|
||||
<para>Device drivers can roughly be broken down into two
|
||||
categories; character and network device drivers.</para>
|
||||
|
||||
<sect1>
|
||||
<title>Dynamic Kernel Linker Facility - KLD</title>
|
||||
<para>The kld interface allows system administrators to
|
||||
dynamically add and remove functionality from a running
|
||||
system. This allows device driver writers to load their new
|
||||
changes into a running kernel without constantly rebooting to
|
||||
test changes.</para>
|
||||
</sect1>
|
||||
|
||||
<para>The kld interface is used through the following
|
||||
administrator commands :
|
||||
<itemizedlist>
|
||||
<listitem><simpara><command>kldload</command> - loads a new kernel
|
||||
<sect1>
|
||||
<title>Dynamic Kernel Linker Facility - KLD</title>
|
||||
|
||||
<para>The kld interface allows system administrators to
|
||||
dynamically add and remove functionality from a running system.
|
||||
This allows device driver writers to load their new changes into
|
||||
a running kernel without constantly rebooting to test
|
||||
changes.</para>
|
||||
|
||||
<para>The kld interface is used through the following
|
||||
administrator commands :
|
||||
|
||||
<itemizedlist>
|
||||
<listitem><simpara><command>kldload</command> - loads a new kernel
|
||||
module</simpara></listitem>
|
||||
<listitem><simpara><command>kldunload</command> - unloads a kernel
|
||||
<listitem><simpara><command>kldunload</command> - unloads a kernel
|
||||
module</simpara></listitem>
|
||||
<listitem><simpara><command>kldstat</command> - lists the currently loadded
|
||||
<listitem><simpara><command>kldstat</command> - lists the currently loadded
|
||||
modules</simpara></listitem>
|
||||
</itemizedlist>
|
||||
</para>
|
||||
</itemizedlist>
|
||||
</para>
|
||||
|
||||
<para>Skeleton Layout of a kernel module</para>
|
||||
|
||||
<para>Skeleton Layout of a kernel module</para>
|
||||
<programlisting>/*
|
||||
* KLD Skeleton
|
||||
* Inspired by Andrew Reiter's Daemonnews article
|
||||
|
@ -100,67 +103,73 @@ skel_loader(struct module *m, int what, void *arg)
|
|||
DECLARE_MODULE(skeleton, skel_loader, SI_SUB_KLD, SI_ORDER_ANY);</programlisting>
|
||||
|
||||
|
||||
<sect2>
|
||||
<title>Makefile</title>
|
||||
<para>FreeBSD provides a makefile include that you can use
|
||||
to quickly compile your kernel addition.</para>
|
||||
<programlisting>SRCS=skeleton.c
|
||||
<sect2>
|
||||
<title>Makefile</title>
|
||||
|
||||
<para>FreeBSD provides a makefile include that you can use to
|
||||
quickly compile your kernel addition.</para>
|
||||
|
||||
<programlisting>SRCS=skeleton.c
|
||||
KMOD=skeleton
|
||||
|
||||
.include <bsd.kmod.mk></programlisting>
|
||||
|
||||
|
||||
<para>Simply running <command>make</command> with
|
||||
this makefile will create a file
|
||||
<filename>skeleton.ko</filename> that can be loaded into
|
||||
your system by typing :
|
||||
<screen>
|
||||
&prompt.root kldload -v ./skeleton.ko
|
||||
<para>Simply running <command>make</command> with this makefile
|
||||
will create a file <filename>skeleton.ko</filename> that can
|
||||
be loaded into your system by typing :
|
||||
<screen> &prompt.root
|
||||
kldload -v ./skeleton.ko
|
||||
</screen>
|
||||
</para>
|
||||
</sect2>
|
||||
</sect1>
|
||||
</para>
|
||||
</sect2>
|
||||
</sect1>
|
||||
|
||||
<sect1>
|
||||
<title>Accessing a device driver</title>
|
||||
<para>Unix provides a common set of system calls for user
|
||||
applications to use. The upper layers of the kernel dispatch
|
||||
these calls to the corresponding device driver when a user
|
||||
accesses a device node. The <command>/dev/MAKEDEV</command>
|
||||
script makes most of the device nodes for your system but if
|
||||
you are doing your own driver development it may be necessary
|
||||
to create your own device nodes with <command>mknod</command>
|
||||
</para>
|
||||
<sect1>
|
||||
<title>Accessing a device driver</title>
|
||||
|
||||
<sect2>
|
||||
<title>Creating static device nodes</title>
|
||||
<para>The <command>mknod</command> command requires four
|
||||
arguments to create a device node. You must specify the
|
||||
name of this device node, the type of device, the major number
|
||||
of the device, and the minor number of the device.</para>
|
||||
</sect2>
|
||||
<para>Unix provides a common set of system calls for user
|
||||
applications to use. The upper layers of the kernel dispatch
|
||||
these calls to the corresponding device driver when a user
|
||||
accesses a device node. The <command>/dev/MAKEDEV</command>
|
||||
script makes most of the device nodes for your system but if you
|
||||
are doing your own driver development it may be necessary to
|
||||
create your own device nodes with <command>mknod</command>
|
||||
</para>
|
||||
|
||||
<sect2>
|
||||
<title>Dynamic device nodes</title>
|
||||
<para>The device filesystem, or devfs, provides access to the
|
||||
<sect2>
|
||||
<title>Creating static device nodes</title>
|
||||
|
||||
<para>The <command>mknod</command> command requires four
|
||||
arguments to create a device node. You must specify the name
|
||||
of this device node, the type of device, the major number of
|
||||
the device, and the minor number of the device.</para>
|
||||
</sect2>
|
||||
|
||||
<sect2>
|
||||
<title>Dynamic device nodes</title>
|
||||
|
||||
<para>The device filesystem, or devfs, provides access to the
|
||||
kernel's device namespace in the global filesystem namespace.
|
||||
This eliminates the problems of potentially having a device
|
||||
driver without a static device node, or a device node without
|
||||
an installed device driver. Devfs is still a work in progress,
|
||||
but it is already working quite nice.</para>
|
||||
</sect2>
|
||||
an installed device driver. Devfs is still a work in
|
||||
progress, but it is already working quite nice.</para>
|
||||
</sect2>
|
||||
|
||||
</sect1>
|
||||
</sect1>
|
||||
|
||||
<sect1>
|
||||
<title>Character Devices</title>
|
||||
|
||||
<para>A character device driver is one that transfers data
|
||||
directly to and from a user process. This is the most common
|
||||
type of device driver and there are plenty of simple examples in
|
||||
the source tree.</para>
|
||||
|
||||
<para>This simple example pseudo-device remembers whatever values
|
||||
you write to it and can then supply them back to you when you
|
||||
read from it.</para>
|
||||
|
||||
<sect1>
|
||||
<title>Character Devices</title>
|
||||
<para>A character device driver is one that transfers data
|
||||
directly to and from a user process. This is the most common
|
||||
type of device driver and there are plenty of simple examples
|
||||
in the source tree.</para>
|
||||
<para>This simple example pseudo-device remembers whatever values you write
|
||||
to it and can then supply them back to you when you read from
|
||||
it.</para>
|
||||
<programlisting>/*
|
||||
* Simple `echo' pseudo-device KLD
|
||||
*
|
||||
|
@ -320,43 +329,51 @@ echo_write(dev_t dev, struct uio *uio, int ioflag)
|
|||
|
||||
DEV_MODULE(echo,echo_loader,NULL);</programlisting>
|
||||
|
||||
<para>To install this driver you will first need to make a node on
|
||||
your filesystem with a command such as : </para>
|
||||
<para>To install this driver you will first need to make a node on
|
||||
your filesystem with a command such as : </para>
|
||||
|
||||
<screen>
|
||||
&prompt.root mknod /dev/echo c 33 0
|
||||
</screen>
|
||||
<para>With this driver loaded you should now be able to type something
|
||||
like :</para>
|
||||
|
||||
<para>With this driver loaded you should now be able to type
|
||||
something like :</para>
|
||||
|
||||
<screen>
|
||||
&prompt.root echo -n "Test Data" > /dev/echo
|
||||
&prompt.root cat /dev/echo
|
||||
Test Data
|
||||
</screen>
|
||||
<para>Real hardware devices in the next chapter..</para>
|
||||
|
||||
<para>Additional Resources
|
||||
<itemizedlist>
|
||||
<listitem><simpara><ulink
|
||||
<para>Real hardware devices in the next chapter..</para>
|
||||
|
||||
<para>Additional Resources
|
||||
<itemizedlist>
|
||||
<listitem><simpara><ulink
|
||||
url="http://www.daemonnews.org/200010/blueprints.html">Dynamic
|
||||
Kernel Linker (KLD) Facility Programming Tutorial</ulink> -
|
||||
<ulink url="http://www.daemonnews.org">Daemonnews</ulink> October 2000</simpara></listitem>
|
||||
<listitem><simpara><ulink
|
||||
<listitem><simpara><ulink
|
||||
url="http://www.daemonnews.org/200007/newbus-intro.html">How
|
||||
to Write Kernel Drivers with NEWBUS</ulink> - <ulink
|
||||
url="http://www.daemonnews.org">Daemonnews</ulink> July
|
||||
2000</simpara></listitem>
|
||||
</itemizedlist>
|
||||
</para>
|
||||
</sect1>
|
||||
</itemizedlist>
|
||||
</para>
|
||||
</sect1>
|
||||
|
||||
<sect1>
|
||||
<title>Network Drivers</title>
|
||||
<para>Drivers for network devices do not use device nodes in
|
||||
ord to be accessed. Their selection is based on other
|
||||
decisions made inside the kernel and instead of calling
|
||||
open(), use of a network device is generally introduced by
|
||||
using the system call socket(2).</para>
|
||||
<para>man ifnet(), loopback device, Bill Pauls drivers, etc..</para>
|
||||
</sect1>
|
||||
<sect1>
|
||||
<title>Network Drivers</title>
|
||||
|
||||
</chapter>
|
||||
<para>Drivers for network devices do not use device nodes in order
|
||||
to be accessed. Their selection is based on other decisions
|
||||
made inside the kernel and instead of calling open(), use of a
|
||||
network device is generally introduced by using the system call
|
||||
socket(2).</para>
|
||||
|
||||
<para>man ifnet(), loopback device, Bill Paul's drivers,
|
||||
etc..</para>
|
||||
|
||||
</sect1>
|
||||
|
||||
</chapter>
|
||||
|
|
|
@ -1,65 +1,68 @@
|
|||
<!--
|
||||
The FreeBSD Documentation Project
|
||||
|
||||
$FreeBSD: doc/en_US.ISO_8859-1/books/developers-handbook/driverbasics/chapter.sgml,v 1.3 2000/12/04 11:39:41 alex Exp $
|
||||
$FreeBSD: doc/en_US.ISO_8859-1/books/developers-handbook/driverbasics/chapter.sgml,v 1.4 2001/04/09 00:33:42 dd Exp $
|
||||
-->
|
||||
|
||||
<chapter id="driverbasics">
|
||||
<title>Writing FreeBSD Device Drivers</title>
|
||||
<chapter id="driverbasics">
|
||||
<title>Writing FreeBSD Device Drivers</title>
|
||||
|
||||
<para>This chapter was written by Murray Stokely with selections from
|
||||
a variety of sources including the intro(4) man page by Joerg
|
||||
Wunsch.</para>
|
||||
<para>This chapter was written by &.murray with selections
|
||||
from a variety of sources including the intro(4) man page by Joerg
|
||||
Wunsch.</para>
|
||||
|
||||
<sect1>
|
||||
<title>Introduction</title>
|
||||
<para>
|
||||
This chapter provides a brief introduction to writing device
|
||||
drivers for FreeBSD. A device in this context is a term used
|
||||
mostly for hardware-related stuff that belongs to the system,
|
||||
like disks, printers, or a graphics display with its keyboard.
|
||||
A device driver is the software component of the operating
|
||||
system that controls a specific device. There are also
|
||||
so-called pseudo-devices where a device driver emulates the
|
||||
behaviour of a device in software without any particular
|
||||
underlying hardware. Device drivers can be compiled into the
|
||||
system statically or loaded on demand through the dynamic
|
||||
kernel linker facility `kld'.</para>
|
||||
<sect1>
|
||||
<title>Introduction</title>
|
||||
<para>This chapter provides a brief introduction to writing device
|
||||
drivers for FreeBSD. A device in this context is a term used
|
||||
mostly for hardware-related stuff that belongs to the system,
|
||||
like disks, printers, or a graphics display with its keyboard.
|
||||
A device driver is the software component of the operating
|
||||
system that controls a specific device. There are also
|
||||
so-called pseudo-devices where a device driver emulates the
|
||||
behaviour of a device in software without any particular
|
||||
underlying hardware. Device drivers can be compiled into the
|
||||
system statically or loaded on demand through the dynamic kernel
|
||||
linker facility `kld'.</para>
|
||||
|
||||
<para>Most devices in a Unix-like operating system are
|
||||
accessed through device-nodes, sometimes also called special
|
||||
files. These files are usually located under the directory
|
||||
<filename>/dev</filename> in the file system hierarchy. Until
|
||||
devfs is fully integrated into FreeBSD, each device node must
|
||||
be created statically and independent of the existence of the
|
||||
associated device driver. Most device nodes on the system are
|
||||
created by running <command>MAKEDEV</command>.</para>
|
||||
<para>Most devices in a Unix-like operating system are accessed
|
||||
through device-nodes, sometimes also called special files.
|
||||
These files are usually located under the directory
|
||||
<filename>/dev</filename> in the file system hierarchy. Until
|
||||
devfs is fully integrated into FreeBSD, each device node must be
|
||||
created statically and independent of the existence of the
|
||||
associated device driver. Most device nodes on the system are
|
||||
created by running <command>MAKEDEV</command>.</para>
|
||||
|
||||
<para>Device drivers can roughly be broken down into two
|
||||
categories; character and network device drivers.</para>
|
||||
</sect1>
|
||||
<para>Device drivers can roughly be broken down into two
|
||||
categories; character and network device drivers.</para>
|
||||
|
||||
<sect1>
|
||||
<title>Dynamic Kernel Linker Facility - KLD</title>
|
||||
<para>The kld interface allows system administrators to
|
||||
dynamically add and remove functionality from a running
|
||||
system. This allows device driver writers to load their new
|
||||
changes into a running kernel without constantly rebooting to
|
||||
test changes.</para>
|
||||
</sect1>
|
||||
|
||||
<para>The kld interface is used through the following
|
||||
administrator commands :
|
||||
<itemizedlist>
|
||||
<listitem><simpara><command>kldload</command> - loads a new kernel
|
||||
<sect1>
|
||||
<title>Dynamic Kernel Linker Facility - KLD</title>
|
||||
|
||||
<para>The kld interface allows system administrators to
|
||||
dynamically add and remove functionality from a running system.
|
||||
This allows device driver writers to load their new changes into
|
||||
a running kernel without constantly rebooting to test
|
||||
changes.</para>
|
||||
|
||||
<para>The kld interface is used through the following
|
||||
administrator commands :
|
||||
|
||||
<itemizedlist>
|
||||
<listitem><simpara><command>kldload</command> - loads a new kernel
|
||||
module</simpara></listitem>
|
||||
<listitem><simpara><command>kldunload</command> - unloads a kernel
|
||||
<listitem><simpara><command>kldunload</command> - unloads a kernel
|
||||
module</simpara></listitem>
|
||||
<listitem><simpara><command>kldstat</command> - lists the currently loadded
|
||||
<listitem><simpara><command>kldstat</command> - lists the currently loadded
|
||||
modules</simpara></listitem>
|
||||
</itemizedlist>
|
||||
</para>
|
||||
</itemizedlist>
|
||||
</para>
|
||||
|
||||
<para>Skeleton Layout of a kernel module</para>
|
||||
|
||||
<para>Skeleton Layout of a kernel module</para>
|
||||
<programlisting>/*
|
||||
* KLD Skeleton
|
||||
* Inspired by Andrew Reiter's Daemonnews article
|
||||
|
@ -100,67 +103,73 @@ skel_loader(struct module *m, int what, void *arg)
|
|||
DECLARE_MODULE(skeleton, skel_loader, SI_SUB_KLD, SI_ORDER_ANY);</programlisting>
|
||||
|
||||
|
||||
<sect2>
|
||||
<title>Makefile</title>
|
||||
<para>FreeBSD provides a makefile include that you can use
|
||||
to quickly compile your kernel addition.</para>
|
||||
<programlisting>SRCS=skeleton.c
|
||||
<sect2>
|
||||
<title>Makefile</title>
|
||||
|
||||
<para>FreeBSD provides a makefile include that you can use to
|
||||
quickly compile your kernel addition.</para>
|
||||
|
||||
<programlisting>SRCS=skeleton.c
|
||||
KMOD=skeleton
|
||||
|
||||
.include <bsd.kmod.mk></programlisting>
|
||||
|
||||
|
||||
<para>Simply running <command>make</command> with
|
||||
this makefile will create a file
|
||||
<filename>skeleton.ko</filename> that can be loaded into
|
||||
your system by typing :
|
||||
<screen>
|
||||
&prompt.root kldload -v ./skeleton.ko
|
||||
<para>Simply running <command>make</command> with this makefile
|
||||
will create a file <filename>skeleton.ko</filename> that can
|
||||
be loaded into your system by typing :
|
||||
<screen> &prompt.root
|
||||
kldload -v ./skeleton.ko
|
||||
</screen>
|
||||
</para>
|
||||
</sect2>
|
||||
</sect1>
|
||||
</para>
|
||||
</sect2>
|
||||
</sect1>
|
||||
|
||||
<sect1>
|
||||
<title>Accessing a device driver</title>
|
||||
<para>Unix provides a common set of system calls for user
|
||||
applications to use. The upper layers of the kernel dispatch
|
||||
these calls to the corresponding device driver when a user
|
||||
accesses a device node. The <command>/dev/MAKEDEV</command>
|
||||
script makes most of the device nodes for your system but if
|
||||
you are doing your own driver development it may be necessary
|
||||
to create your own device nodes with <command>mknod</command>
|
||||
</para>
|
||||
<sect1>
|
||||
<title>Accessing a device driver</title>
|
||||
|
||||
<sect2>
|
||||
<title>Creating static device nodes</title>
|
||||
<para>The <command>mknod</command> command requires four
|
||||
arguments to create a device node. You must specify the
|
||||
name of this device node, the type of device, the major number
|
||||
of the device, and the minor number of the device.</para>
|
||||
</sect2>
|
||||
<para>Unix provides a common set of system calls for user
|
||||
applications to use. The upper layers of the kernel dispatch
|
||||
these calls to the corresponding device driver when a user
|
||||
accesses a device node. The <command>/dev/MAKEDEV</command>
|
||||
script makes most of the device nodes for your system but if you
|
||||
are doing your own driver development it may be necessary to
|
||||
create your own device nodes with <command>mknod</command>
|
||||
</para>
|
||||
|
||||
<sect2>
|
||||
<title>Dynamic device nodes</title>
|
||||
<para>The device filesystem, or devfs, provides access to the
|
||||
<sect2>
|
||||
<title>Creating static device nodes</title>
|
||||
|
||||
<para>The <command>mknod</command> command requires four
|
||||
arguments to create a device node. You must specify the name
|
||||
of this device node, the type of device, the major number of
|
||||
the device, and the minor number of the device.</para>
|
||||
</sect2>
|
||||
|
||||
<sect2>
|
||||
<title>Dynamic device nodes</title>
|
||||
|
||||
<para>The device filesystem, or devfs, provides access to the
|
||||
kernel's device namespace in the global filesystem namespace.
|
||||
This eliminates the problems of potentially having a device
|
||||
driver without a static device node, or a device node without
|
||||
an installed device driver. Devfs is still a work in progress,
|
||||
but it is already working quite nice.</para>
|
||||
</sect2>
|
||||
an installed device driver. Devfs is still a work in
|
||||
progress, but it is already working quite nice.</para>
|
||||
</sect2>
|
||||
|
||||
</sect1>
|
||||
</sect1>
|
||||
|
||||
<sect1>
|
||||
<title>Character Devices</title>
|
||||
|
||||
<para>A character device driver is one that transfers data
|
||||
directly to and from a user process. This is the most common
|
||||
type of device driver and there are plenty of simple examples in
|
||||
the source tree.</para>
|
||||
|
||||
<para>This simple example pseudo-device remembers whatever values
|
||||
you write to it and can then supply them back to you when you
|
||||
read from it.</para>
|
||||
|
||||
<sect1>
|
||||
<title>Character Devices</title>
|
||||
<para>A character device driver is one that transfers data
|
||||
directly to and from a user process. This is the most common
|
||||
type of device driver and there are plenty of simple examples
|
||||
in the source tree.</para>
|
||||
<para>This simple example pseudo-device remembers whatever values you write
|
||||
to it and can then supply them back to you when you read from
|
||||
it.</para>
|
||||
<programlisting>/*
|
||||
* Simple `echo' pseudo-device KLD
|
||||
*
|
||||
|
@ -320,43 +329,51 @@ echo_write(dev_t dev, struct uio *uio, int ioflag)
|
|||
|
||||
DEV_MODULE(echo,echo_loader,NULL);</programlisting>
|
||||
|
||||
<para>To install this driver you will first need to make a node on
|
||||
your filesystem with a command such as : </para>
|
||||
<para>To install this driver you will first need to make a node on
|
||||
your filesystem with a command such as : </para>
|
||||
|
||||
<screen>
|
||||
&prompt.root mknod /dev/echo c 33 0
|
||||
</screen>
|
||||
<para>With this driver loaded you should now be able to type something
|
||||
like :</para>
|
||||
|
||||
<para>With this driver loaded you should now be able to type
|
||||
something like :</para>
|
||||
|
||||
<screen>
|
||||
&prompt.root echo -n "Test Data" > /dev/echo
|
||||
&prompt.root cat /dev/echo
|
||||
Test Data
|
||||
</screen>
|
||||
<para>Real hardware devices in the next chapter..</para>
|
||||
|
||||
<para>Additional Resources
|
||||
<itemizedlist>
|
||||
<listitem><simpara><ulink
|
||||
<para>Real hardware devices in the next chapter..</para>
|
||||
|
||||
<para>Additional Resources
|
||||
<itemizedlist>
|
||||
<listitem><simpara><ulink
|
||||
url="http://www.daemonnews.org/200010/blueprints.html">Dynamic
|
||||
Kernel Linker (KLD) Facility Programming Tutorial</ulink> -
|
||||
<ulink url="http://www.daemonnews.org">Daemonnews</ulink> October 2000</simpara></listitem>
|
||||
<listitem><simpara><ulink
|
||||
<listitem><simpara><ulink
|
||||
url="http://www.daemonnews.org/200007/newbus-intro.html">How
|
||||
to Write Kernel Drivers with NEWBUS</ulink> - <ulink
|
||||
url="http://www.daemonnews.org">Daemonnews</ulink> July
|
||||
2000</simpara></listitem>
|
||||
</itemizedlist>
|
||||
</para>
|
||||
</sect1>
|
||||
</itemizedlist>
|
||||
</para>
|
||||
</sect1>
|
||||
|
||||
<sect1>
|
||||
<title>Network Drivers</title>
|
||||
<para>Drivers for network devices do not use device nodes in
|
||||
ord to be accessed. Their selection is based on other
|
||||
decisions made inside the kernel and instead of calling
|
||||
open(), use of a network device is generally introduced by
|
||||
using the system call socket(2).</para>
|
||||
<para>man ifnet(), loopback device, Bill Pauls drivers, etc..</para>
|
||||
</sect1>
|
||||
<sect1>
|
||||
<title>Network Drivers</title>
|
||||
|
||||
</chapter>
|
||||
<para>Drivers for network devices do not use device nodes in order
|
||||
to be accessed. Their selection is based on other decisions
|
||||
made inside the kernel and instead of calling open(), use of a
|
||||
network device is generally introduced by using the system call
|
||||
socket(2).</para>
|
||||
|
||||
<para>man ifnet(), loopback device, Bill Paul's drivers,
|
||||
etc..</para>
|
||||
|
||||
</sect1>
|
||||
|
||||
</chapter>
|
||||
|
|
|
@ -1,65 +1,68 @@
|
|||
<!--
|
||||
The FreeBSD Documentation Project
|
||||
|
||||
$FreeBSD: doc/en_US.ISO_8859-1/books/developers-handbook/driverbasics/chapter.sgml,v 1.3 2000/12/04 11:39:41 alex Exp $
|
||||
$FreeBSD: doc/en_US.ISO_8859-1/books/developers-handbook/driverbasics/chapter.sgml,v 1.4 2001/04/09 00:33:42 dd Exp $
|
||||
-->
|
||||
|
||||
<chapter id="driverbasics">
|
||||
<title>Writing FreeBSD Device Drivers</title>
|
||||
<chapter id="driverbasics">
|
||||
<title>Writing FreeBSD Device Drivers</title>
|
||||
|
||||
<para>This chapter was written by Murray Stokely with selections from
|
||||
a variety of sources including the intro(4) man page by Joerg
|
||||
Wunsch.</para>
|
||||
<para>This chapter was written by &.murray with selections
|
||||
from a variety of sources including the intro(4) man page by Joerg
|
||||
Wunsch.</para>
|
||||
|
||||
<sect1>
|
||||
<title>Introduction</title>
|
||||
<para>
|
||||
This chapter provides a brief introduction to writing device
|
||||
drivers for FreeBSD. A device in this context is a term used
|
||||
mostly for hardware-related stuff that belongs to the system,
|
||||
like disks, printers, or a graphics display with its keyboard.
|
||||
A device driver is the software component of the operating
|
||||
system that controls a specific device. There are also
|
||||
so-called pseudo-devices where a device driver emulates the
|
||||
behaviour of a device in software without any particular
|
||||
underlying hardware. Device drivers can be compiled into the
|
||||
system statically or loaded on demand through the dynamic
|
||||
kernel linker facility `kld'.</para>
|
||||
<sect1>
|
||||
<title>Introduction</title>
|
||||
<para>This chapter provides a brief introduction to writing device
|
||||
drivers for FreeBSD. A device in this context is a term used
|
||||
mostly for hardware-related stuff that belongs to the system,
|
||||
like disks, printers, or a graphics display with its keyboard.
|
||||
A device driver is the software component of the operating
|
||||
system that controls a specific device. There are also
|
||||
so-called pseudo-devices where a device driver emulates the
|
||||
behaviour of a device in software without any particular
|
||||
underlying hardware. Device drivers can be compiled into the
|
||||
system statically or loaded on demand through the dynamic kernel
|
||||
linker facility `kld'.</para>
|
||||
|
||||
<para>Most devices in a Unix-like operating system are
|
||||
accessed through device-nodes, sometimes also called special
|
||||
files. These files are usually located under the directory
|
||||
<filename>/dev</filename> in the file system hierarchy. Until
|
||||
devfs is fully integrated into FreeBSD, each device node must
|
||||
be created statically and independent of the existence of the
|
||||
associated device driver. Most device nodes on the system are
|
||||
created by running <command>MAKEDEV</command>.</para>
|
||||
<para>Most devices in a Unix-like operating system are accessed
|
||||
through device-nodes, sometimes also called special files.
|
||||
These files are usually located under the directory
|
||||
<filename>/dev</filename> in the file system hierarchy. Until
|
||||
devfs is fully integrated into FreeBSD, each device node must be
|
||||
created statically and independent of the existence of the
|
||||
associated device driver. Most device nodes on the system are
|
||||
created by running <command>MAKEDEV</command>.</para>
|
||||
|
||||
<para>Device drivers can roughly be broken down into two
|
||||
categories; character and network device drivers.</para>
|
||||
</sect1>
|
||||
<para>Device drivers can roughly be broken down into two
|
||||
categories; character and network device drivers.</para>
|
||||
|
||||
<sect1>
|
||||
<title>Dynamic Kernel Linker Facility - KLD</title>
|
||||
<para>The kld interface allows system administrators to
|
||||
dynamically add and remove functionality from a running
|
||||
system. This allows device driver writers to load their new
|
||||
changes into a running kernel without constantly rebooting to
|
||||
test changes.</para>
|
||||
</sect1>
|
||||
|
||||
<para>The kld interface is used through the following
|
||||
administrator commands :
|
||||
<itemizedlist>
|
||||
<listitem><simpara><command>kldload</command> - loads a new kernel
|
||||
<sect1>
|
||||
<title>Dynamic Kernel Linker Facility - KLD</title>
|
||||
|
||||
<para>The kld interface allows system administrators to
|
||||
dynamically add and remove functionality from a running system.
|
||||
This allows device driver writers to load their new changes into
|
||||
a running kernel without constantly rebooting to test
|
||||
changes.</para>
|
||||
|
||||
<para>The kld interface is used through the following
|
||||
administrator commands :
|
||||
|
||||
<itemizedlist>
|
||||
<listitem><simpara><command>kldload</command> - loads a new kernel
|
||||
module</simpara></listitem>
|
||||
<listitem><simpara><command>kldunload</command> - unloads a kernel
|
||||
<listitem><simpara><command>kldunload</command> - unloads a kernel
|
||||
module</simpara></listitem>
|
||||
<listitem><simpara><command>kldstat</command> - lists the currently loadded
|
||||
<listitem><simpara><command>kldstat</command> - lists the currently loadded
|
||||
modules</simpara></listitem>
|
||||
</itemizedlist>
|
||||
</para>
|
||||
</itemizedlist>
|
||||
</para>
|
||||
|
||||
<para>Skeleton Layout of a kernel module</para>
|
||||
|
||||
<para>Skeleton Layout of a kernel module</para>
|
||||
<programlisting>/*
|
||||
* KLD Skeleton
|
||||
* Inspired by Andrew Reiter's Daemonnews article
|
||||
|
@ -100,67 +103,73 @@ skel_loader(struct module *m, int what, void *arg)
|
|||
DECLARE_MODULE(skeleton, skel_loader, SI_SUB_KLD, SI_ORDER_ANY);</programlisting>
|
||||
|
||||
|
||||
<sect2>
|
||||
<title>Makefile</title>
|
||||
<para>FreeBSD provides a makefile include that you can use
|
||||
to quickly compile your kernel addition.</para>
|
||||
<programlisting>SRCS=skeleton.c
|
||||
<sect2>
|
||||
<title>Makefile</title>
|
||||
|
||||
<para>FreeBSD provides a makefile include that you can use to
|
||||
quickly compile your kernel addition.</para>
|
||||
|
||||
<programlisting>SRCS=skeleton.c
|
||||
KMOD=skeleton
|
||||
|
||||
.include <bsd.kmod.mk></programlisting>
|
||||
|
||||
|
||||
<para>Simply running <command>make</command> with
|
||||
this makefile will create a file
|
||||
<filename>skeleton.ko</filename> that can be loaded into
|
||||
your system by typing :
|
||||
<screen>
|
||||
&prompt.root kldload -v ./skeleton.ko
|
||||
<para>Simply running <command>make</command> with this makefile
|
||||
will create a file <filename>skeleton.ko</filename> that can
|
||||
be loaded into your system by typing :
|
||||
<screen> &prompt.root
|
||||
kldload -v ./skeleton.ko
|
||||
</screen>
|
||||
</para>
|
||||
</sect2>
|
||||
</sect1>
|
||||
</para>
|
||||
</sect2>
|
||||
</sect1>
|
||||
|
||||
<sect1>
|
||||
<title>Accessing a device driver</title>
|
||||
<para>Unix provides a common set of system calls for user
|
||||
applications to use. The upper layers of the kernel dispatch
|
||||
these calls to the corresponding device driver when a user
|
||||
accesses a device node. The <command>/dev/MAKEDEV</command>
|
||||
script makes most of the device nodes for your system but if
|
||||
you are doing your own driver development it may be necessary
|
||||
to create your own device nodes with <command>mknod</command>
|
||||
</para>
|
||||
<sect1>
|
||||
<title>Accessing a device driver</title>
|
||||
|
||||
<sect2>
|
||||
<title>Creating static device nodes</title>
|
||||
<para>The <command>mknod</command> command requires four
|
||||
arguments to create a device node. You must specify the
|
||||
name of this device node, the type of device, the major number
|
||||
of the device, and the minor number of the device.</para>
|
||||
</sect2>
|
||||
<para>Unix provides a common set of system calls for user
|
||||
applications to use. The upper layers of the kernel dispatch
|
||||
these calls to the corresponding device driver when a user
|
||||
accesses a device node. The <command>/dev/MAKEDEV</command>
|
||||
script makes most of the device nodes for your system but if you
|
||||
are doing your own driver development it may be necessary to
|
||||
create your own device nodes with <command>mknod</command>
|
||||
</para>
|
||||
|
||||
<sect2>
|
||||
<title>Dynamic device nodes</title>
|
||||
<para>The device filesystem, or devfs, provides access to the
|
||||
<sect2>
|
||||
<title>Creating static device nodes</title>
|
||||
|
||||
<para>The <command>mknod</command> command requires four
|
||||
arguments to create a device node. You must specify the name
|
||||
of this device node, the type of device, the major number of
|
||||
the device, and the minor number of the device.</para>
|
||||
</sect2>
|
||||
|
||||
<sect2>
|
||||
<title>Dynamic device nodes</title>
|
||||
|
||||
<para>The device filesystem, or devfs, provides access to the
|
||||
kernel's device namespace in the global filesystem namespace.
|
||||
This eliminates the problems of potentially having a device
|
||||
driver without a static device node, or a device node without
|
||||
an installed device driver. Devfs is still a work in progress,
|
||||
but it is already working quite nice.</para>
|
||||
</sect2>
|
||||
an installed device driver. Devfs is still a work in
|
||||
progress, but it is already working quite nice.</para>
|
||||
</sect2>
|
||||
|
||||
</sect1>
|
||||
</sect1>
|
||||
|
||||
<sect1>
|
||||
<title>Character Devices</title>
|
||||
|
||||
<para>A character device driver is one that transfers data
|
||||
directly to and from a user process. This is the most common
|
||||
type of device driver and there are plenty of simple examples in
|
||||
the source tree.</para>
|
||||
|
||||
<para>This simple example pseudo-device remembers whatever values
|
||||
you write to it and can then supply them back to you when you
|
||||
read from it.</para>
|
||||
|
||||
<sect1>
|
||||
<title>Character Devices</title>
|
||||
<para>A character device driver is one that transfers data
|
||||
directly to and from a user process. This is the most common
|
||||
type of device driver and there are plenty of simple examples
|
||||
in the source tree.</para>
|
||||
<para>This simple example pseudo-device remembers whatever values you write
|
||||
to it and can then supply them back to you when you read from
|
||||
it.</para>
|
||||
<programlisting>/*
|
||||
* Simple `echo' pseudo-device KLD
|
||||
*
|
||||
|
@ -320,43 +329,51 @@ echo_write(dev_t dev, struct uio *uio, int ioflag)
|
|||
|
||||
DEV_MODULE(echo,echo_loader,NULL);</programlisting>
|
||||
|
||||
<para>To install this driver you will first need to make a node on
|
||||
your filesystem with a command such as : </para>
|
||||
<para>To install this driver you will first need to make a node on
|
||||
your filesystem with a command such as : </para>
|
||||
|
||||
<screen>
|
||||
&prompt.root mknod /dev/echo c 33 0
|
||||
</screen>
|
||||
<para>With this driver loaded you should now be able to type something
|
||||
like :</para>
|
||||
|
||||
<para>With this driver loaded you should now be able to type
|
||||
something like :</para>
|
||||
|
||||
<screen>
|
||||
&prompt.root echo -n "Test Data" > /dev/echo
|
||||
&prompt.root cat /dev/echo
|
||||
Test Data
|
||||
</screen>
|
||||
<para>Real hardware devices in the next chapter..</para>
|
||||
|
||||
<para>Additional Resources
|
||||
<itemizedlist>
|
||||
<listitem><simpara><ulink
|
||||
<para>Real hardware devices in the next chapter..</para>
|
||||
|
||||
<para>Additional Resources
|
||||
<itemizedlist>
|
||||
<listitem><simpara><ulink
|
||||
url="http://www.daemonnews.org/200010/blueprints.html">Dynamic
|
||||
Kernel Linker (KLD) Facility Programming Tutorial</ulink> -
|
||||
<ulink url="http://www.daemonnews.org">Daemonnews</ulink> October 2000</simpara></listitem>
|
||||
<listitem><simpara><ulink
|
||||
<listitem><simpara><ulink
|
||||
url="http://www.daemonnews.org/200007/newbus-intro.html">How
|
||||
to Write Kernel Drivers with NEWBUS</ulink> - <ulink
|
||||
url="http://www.daemonnews.org">Daemonnews</ulink> July
|
||||
2000</simpara></listitem>
|
||||
</itemizedlist>
|
||||
</para>
|
||||
</sect1>
|
||||
</itemizedlist>
|
||||
</para>
|
||||
</sect1>
|
||||
|
||||
<sect1>
|
||||
<title>Network Drivers</title>
|
||||
<para>Drivers for network devices do not use device nodes in
|
||||
ord to be accessed. Their selection is based on other
|
||||
decisions made inside the kernel and instead of calling
|
||||
open(), use of a network device is generally introduced by
|
||||
using the system call socket(2).</para>
|
||||
<para>man ifnet(), loopback device, Bill Pauls drivers, etc..</para>
|
||||
</sect1>
|
||||
<sect1>
|
||||
<title>Network Drivers</title>
|
||||
|
||||
</chapter>
|
||||
<para>Drivers for network devices do not use device nodes in order
|
||||
to be accessed. Their selection is based on other decisions
|
||||
made inside the kernel and instead of calling open(), use of a
|
||||
network device is generally introduced by using the system call
|
||||
socket(2).</para>
|
||||
|
||||
<para>man ifnet(), loopback device, Bill Paul's drivers,
|
||||
etc..</para>
|
||||
|
||||
</sect1>
|
||||
|
||||
</chapter>
|
||||
|
|
Loading…
Reference in a new issue