Go to main content

Device Driver Tutorial

Exit Print View

Updated: April 2019
 
 

Testing the Template Driver

This dummy driver merely writes a message to a system log each time an entry point routine is entered. To test this driver, watch for these messages to confirm that each entry point routine is successfully entered.

The cmn_err(9F) function writes low priority messages such as the messages defined in this dummy driver to /dev/log. The syslogd(1M) daemon reads messages from /dev/log and writes low priority messages to /var/adm/messages.

In a separate window, enter the following command and monitor the output as you perform the tests described in the remainder of this section:

$ tail -f /var/adm/messages

Adding the Template Driver

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

# add_drv dummy

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

date time system dummy: [ID 513080 kern.notice] NOTICE: Inside _info
date time system dummy: [ID 874762 kern.notice] NOTICE: Inside _init
date time system dummy: [ID 678704 kern.notice] NOTICE: Inside dummy_attach

The _info(9E), _init(9E), and attach(9E) entry points are called in that order when you add a driver.

The dummy driver has been added to the /devices directory:

$ ls -l /devices/pseudo | grep dummy
drwxr-xr-x   2 root     sys          512 date time dummy@0
crw-------   1 root     sys       92,  0 date time dummy@0:0

The dummy driver also is the most recent module listed by modinfo(1M):

$ modinfo
 Id Loadaddr   Size Info Rev Module Name
180 ed192b70    544  92   1  dummy (dummy driver)

The module name, dummy driver, is the value you entered for the second member of the modldrv(9S) structure. The value 92 is the major number of this module.

$ grep dummy /etc/name_to_major
dummy 92

The Loadaddr address of ed192b70 is the address of the first instruction in the dummy driver. This address might be useful, for example, in debugging.

$ mdb -k
> dummy`_init $m
    BASE    LIMIT     SIZE NAME
ed192b70 ed192ff0      480 dummy
> $q

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

$ prtconf -P
    pseudo, instance #0
        dummy, instance #0 (driver not attached)

A driver is automatically loaded when a device that the driver manages is accessed. A driver might be automatically unloaded when the driver is not in use.

    If your driver is in the /devices directory but modinfo(1M) does not list your driver, you can use either of the following methods to load your driver:

  • Use the modload(1M) command.

  • Access the device. The driver is loaded automatically when a device that the driver manages is accessed. The following section describes how to access the dummy device.

Reading and Writing the Device

Make sure you are user root when you perform the tests described in this section. If you are not user root, you will receive "Permission denied" error messages when you try to access the /devices/pseudo/dummy@0:0 special file. Notice the permissions that are shown for /devices/pseudo/dummy@0:0 in Adding the Template Driver.

Test reading from the device. Your dummy device probably is named /devices/pseudo/dummy@0:0. The following command reads from your dummy device even if it has a slightly different name:

# cat /devices/pseudo/dummy*

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

date time system dummy: [ID 136952 kern.notice] NOTICE: Inside dummy_open
date time system dummy: [ID 623947 kern.notice] NOTICE: Inside dummy_getinfo
date time system dummy: [ID 891851 kern.notice] NOTICE: Inside dummy_prop_op
date time system dummy: [ID 623947 kern.notice] NOTICE: Inside dummy_getinfo
date time system dummy: [ID 891851 kern.notice] NOTICE: Inside dummy_prop_op
date time system dummy: [ID 623947 kern.notice] NOTICE: Inside dummy_getinfo
date time system dummy: [ID 709590 kern.notice] NOTICE: Inside dummy_read
date time system dummy: [ID 550206 kern.notice] NOTICE: Inside dummy_close

Test writing to the device:

# echo hello > `ls /devices/pseudo/dummy*`

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

date time system dummy: [ID 136952 kern.notice] NOTICE: Inside dummy_open
date time system dummy: [ID 623947 kern.notice] NOTICE: Inside dummy_getinfo
date time system dummy: [ID 891851 kern.notice] NOTICE: Inside dummy_prop_op
date time system dummy: [ID 623947 kern.notice] NOTICE: Inside dummy_getinfo
date time system dummy: [ID 891851 kern.notice] NOTICE: Inside dummy_prop_op
date time system dummy: [ID 623947 kern.notice] NOTICE: Inside dummy_getinfo
date time system dummy: [ID 672780 kern.notice] NOTICE: Inside dummy_write
date time system dummy: [ID 550206 kern.notice] NOTICE: Inside dummy_close

As you can see, this output from the write test is almost identical to the output you saw from the read test. The only difference is in the seventh line of the output. Using the cat(1) command causes the kernel to access the read(9E) entry point of the driver. Using the echo(1) command causes the kernel to access the write(9E) entry point of the driver. The text argument that you give to echo(1) is ignored because this driver does not do anything with that data.

Removing the Template Driver

Make sure you are user root when you unload the driver. Use the rem_drv(1M) command to unload the driver and remove the device from the /devices directory:

# rem_drv dummy

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

date time system dummy: [ID 513080 kern.notice] NOTICE: Inside _info
date time system dummy: [ID 617648 kern.notice] NOTICE: Inside dummy_detach
date time system dummy: [ID 812373 kern.notice] NOTICE: Inside _fini

The dummy device is no longer in the /devices directory:

# ls /devices/pseudo/dummy*
/devices/pseudo/dummy*: No such file or directory

The next time you want to read from or write to the dummy device, you must load the driver again using add_drv(1M).

You can use the modunload(1M) command to unload the driver but not remove the device from /devices. Then the next time you read from or write to the dummy device, the driver is automatically loaded.

Press Control-C to stop tailing the /var/adm/messages messages.