Writing Device Drivers

Driver Code Layout Structure

The code for a device driver is usually divided into the following files:

Header Files

Header files define:

Some of the header file definitions, such as the state structure, might be needed only by the device driver. This information should go in private header files that are only included by the device driver itself.

Any information that an application might require, such as the I/O control commands, should be in public header files. These are included by the driver and any applications that need information about the device.

While there is no standard for naming private and public files, one convention is to name the private header file xximpl.h and the public header file xxio.h.

.c Files

A .c file for a device driver contains the data declarations and the code for the entry points of the driver. It contains the #include statements the driver needs, declares extern references, declares local data, sets up the cb_ops and dev_ops structures, declares and initializes the module configuration section (the modlinkage(9S) and modldrv(9S) structures), makes any other necessary declarations, and defines the driver entry points.

driver.conf Files

driver.conf files are required for devices that are not self-identifying. Entries in the driver.conf file specify possible device instances the driver will probe for existence. For more information, see the scsi(4) and driver.conf(4) man pages. Driver global properties can also be set by entries in the driver.conf file. driver.conf files are optional for self-identifying devices (SID), where the entries can be used to add properties into SID nodes. See the sbus(4) and pci(4) man pages for details.