JavaScript is required to for searching.
Skip Navigation Links
Exit Print View
Device Driver Tutorial     Oracle Solaris 11 Express 11/10
search filter icon
search icon

Document Information

Preface

1.  Introduction to Device Drivers

2.  Template Driver Example

Overview of the Template Driver Example

Writing the Template Driver

Writing the Loadable Module Configuration Entry Points

Declaring the Loadable Module Configuration Entry Points

Defining the Module Initialization Entry Point

Defining the Module Information Entry Point

Defining the Module Unload Entry Point

Including Loadable Module Configuration Header Files

Writing the Autoconfiguration Entry Points

Declaring the Autoconfiguration Entry Points

Defining the Device Attach Entry Point

Defining the Device Detach Entry Point

Defining the Get Driver Information Entry Point

Defining the Report Driver Property Information Entry Point

Including Autoconfiguration Header Files

Writing the User Context Entry Points

Declaring the User Context Entry Points

Defining the Open Device Entry Point

Defining the Close Device Entry Point

Defining the Read Device Entry Point

Defining the Write Device Entry Point

Including User Context Header Files

Writing the Driver Data Structures

Defining the Character and Block Operations Structure

Defining the Device Operations Structure

Defining the Module Linkage Structures

Including Data Structures Header Files

Writing the Device Configuration File

Building and Installing the Template Driver

Testing the Template Driver

Adding the Template Driver

Reading and Writing the Device

Removing the Template Driver

Complete Template Driver Source

3.  Reading and Writing Data in Kernel Memory

4.  Tips for Developing Device Drivers

Index

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 machine dummy: [ID 513080 kern.notice] NOTICE: Inside _info
date time machine dummy: [ID 874762 kern.notice] NOTICE: Inside _init
date time machine 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:

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 machine dummy: [ID 136952 kern.notice] NOTICE: Inside dummy_open
date time machine dummy: [ID 623947 kern.notice] NOTICE: Inside dummy_getinfo
date time machine dummy: [ID 891851 kern.notice] NOTICE: Inside dummy_prop_op
date time machine dummy: [ID 623947 kern.notice] NOTICE: Inside dummy_getinfo
date time machine dummy: [ID 891851 kern.notice] NOTICE: Inside dummy_prop_op
date time machine dummy: [ID 623947 kern.notice] NOTICE: Inside dummy_getinfo
date time machine dummy: [ID 709590 kern.notice] NOTICE: Inside dummy_read
date time machine 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 machine dummy: [ID 136952 kern.notice] NOTICE: Inside dummy_open
date time machine dummy: [ID 623947 kern.notice] NOTICE: Inside dummy_getinfo
date time machine dummy: [ID 891851 kern.notice] NOTICE: Inside dummy_prop_op
date time machine dummy: [ID 623947 kern.notice] NOTICE: Inside dummy_getinfo
date time machine dummy: [ID 891851 kern.notice] NOTICE: Inside dummy_prop_op
date time machine dummy: [ID 623947 kern.notice] NOTICE: Inside dummy_getinfo
date time machine dummy: [ID 672780 kern.notice] NOTICE: Inside dummy_write
date time machine 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 machine dummy: [ID 513080 kern.notice] NOTICE: Inside _info
date time machine dummy: [ID 617648 kern.notice] NOTICE: Inside dummy_detach
date time machine 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.