Device Drivers PDF
Device Drivers PDF
Device Drivers PDF
The interface:
simplifies the access
controls access from several clients
optimises the communication
ensures safety and error recovery
Policy vs. Mechanism
Mechanism: how to do it, possibly in a simple and robust manner. Lower
level task.
Policy: who can do it and to what extent, e.g. device access control and
exposure of capabilities. Implements restrictions on usage.
Drivers should be policy free: clear distinction of concerns. In some cases it
may not be the best way.
OS is responsible for policy decisions.
Clients of DDs
OS can be the main client to the device driver: driver interface is restricted
to common pattern followed by all such drivers.
OS provides another interface to clients to access the driver functionality.
Consider the network card driver and the socket interface.
Applications can be direct clients. Driver can provide a device specific
interface at a higher level of abstraction.
Firmware is a even lower level interface which resides on hardware itself.
}
Compiling
A "makefile" is used:
Note the calls to _cdevinit, _cdevadd (and _cdevdel not shown here)
cdev structure is populated here.
scull_dev and scull_fops objects
scull_dev is allocated for each client, tracking its state
scull_fops lists the functions that scull driver provides to each client.
Implementing the functions
Function definition follow prototypes
Open function shown below initialises the devices and handle any errors
Release of the device
Deallocate all memory requested earlier
Shutdown device on last close: release is called only when all files are
closed.
Allocating memory
Scull memory is organised in small "pages" called quantum
Allocating memory
This releases all the memory associated with a device.
}
Read and Write
Prototypes
if (!dptr->data) {
Vectored read and write
In many cases read and writes can be clubbed into batches using vector
version of read and write for improved efficiency.