Writing Device Drivers

Header Files

Header files define data structures specific to the device (such as a structure representing the device registers), data structures defined by the driver for maintaining state information, defined constants (such as those representing the bits of the device registers), and macros (such as those defining the static mapping between the minor device number and the instance number).

Some of this information, such as the state structure, may only be needed 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.

There is no standard for naming private and public files. One possible convention is to name the private header file xximpl.h and the public header file xxio.h. Example E-1 and Example E-2 show the layout of these headers.

Example E-1 xximpl.h Header File

/* xximpl.h */
struct device_reg {
	 /* fields */
/* #define bits of the device registers...*/
struct xxstate {
	/* fields */
/* related #define statements */

Example E-2 xxio.h Header File

xxio.h Header File
/* xxio.h */
struct xxioctlreq {
	/* fields */
/* etc. */
#define XXIOC				(`b' << 8)
#define XXIOCTL_1 (XXIOC | 1)										/* description */
#define XXIOCTL_2 (XXIOC | 2)										/* description */