Device Driver Tutorial

Building, Installing, and Using Quote Of The Day Version 2

Version 2 of the driver uses ASSERT(9F) statements to check the validity of data. To use ASSERT(9F) statements, include the sys/debug.h header file in your source and define the DEBUG preprocessor symbol.

Compile and link the driver. If you use ASSERT(9F) statements to check the validity of data, you must define the DEBUG preprocessor symbol:


% cc -D_KERNEL -DDEBUG -c qotd_2.c
% ld -r -o qotd_2 qotd_2.o

The following example shows compiling and linking for a 32-bit architecture if you are not using ASSERT(9F) statements:


% cc -D_KERNEL -c qotd_2.c
% ld -r -o qotd_2 qotd_2.o

Make sure you are user root when you install the driver.

Copy the driver binary to the /tmp directory as discussed in Building and Installing the Template Driver.


# cp qotd_2 /tmp
# ln -s /tmp/qotd_2 /usr/kernel/drv/qotd_2

Copy the configuration file to the kernel driver area of the system.


# cp qotd_2.conf /usr/kernel/drv

In a separate window, enter the following command:


% tail -f /var/adm/messages

Make sure you are user root when you load the driver. Use the add_drv(1M) command to load the driver:


# add_drv qotd_2

You should see the following messages in the window where you are viewing /var/adm/messages:


date time machine pseudo: [ID 129642 kern.info] pseudo-device: devinfo0
date time machine genunix: [ID 936769 kern.info] devinfo0 is /pseudo/devinfo@0
date time machine pseudo: [ID 129642 kern.info] pseudo-device: qotd_20
date time machine genunix: [ID 936769 kern.info] qotd_20 is /pseudo/qotd_2@0

When this version of the Quote Of The Day driver loads, it does not display its quotation. The qotd_1 driver wrote a message to a system log through its _init(9E) entry point. This qotd_2 driver stores its data and makes the data available through its read(9E) entry point.

You can use the modinfo(1M) command to display the module information for this version of the Quote Of The Day driver. The module name is the value you entered for the second member of the modldrv structure. The value 96 is the major number of this module.


% modinfo | grep qotd
182 ed115948    754  96   1  qotd_2 (Quote of the Day 2.0)
% grep qotd /etc/name_to_major
qotd_1 94
qotd_2 96

This driver also is the most recent module listed by prtconf(1M) in the pseudo device section:


% prtconf -P | grep qotd
        qotd_1, instance #0 (driver not attached)
        qotd_2, instance #0

When you access this qotd_2 device for reading, the command you use to access the device retrieves the data from the device node. The command then displays the data in the same way that the command displays any other input. To get the name of the device special file, look in the /devices directory:


% ls -l /devices/pseudo/qotd*
crw-------   1 root   sys   96, 0 date time /devices/pseudo/qotd_2@0:qotd

This output shows that qotd_2@0:qotd is a character device. This listing also shows that only the root user has permission to read or write this device. Make sure you are user root when you test this driver. To test the qotd_2 driver, you can use the more(1) command to access the device file for reading:


# more /devices/pseudo/qotd_2@0:qotd
You can't have everything. Where would you put it? - Steven Wright
You can't have everything. Where would you put it? - Steven Wright