Tutorial On Linux Device Driver: 1 Basics
Tutorial On Linux Device Driver: 1 Basics
Basics
Function
List the currently loaded modules
Load the module specified by module
Load the module along with its dependencies
Remove/Unload the module
The Linux kernel offers support for different classes of device modules:
char devices are devices that can be accessed as a stream of bytes (like
a file).
block devices are accessed by filesystem nodes (example: disks).
network interfaces are able to exchange data with other hosts.
insmod ./ module1 . ko
dmesg
rmmod module1
dmesg
The command dmesg allows to see the output of the modules, printed
using the specific function printk, corresponding to the function printf in
user space. At the end of the output of dmesg the following lines are shown:
$ Module1 started ...
$ Module1 ended ...
A physical device, for example a I/O port, can be accessed like a file, using
the commands open, read, write and close provided by the prototypes
described under fcntl.h. First, a device must be configured in the folder /dev,
providing a class (for example c for a char device), a major and a minor
number in order to identify it. Following shell command will create a device
called mydev with major number 240 and minor number 0:
$ mknod -m 0666 / dev / mydev c 240 0
3
3.1
This function is automatically called when the user call for example
fd = open ("/ dev / mydev " , O_RDWR );
in its user space program. Following is a simple example of this procedure
tha only send a simple message to log file.
static int mydev_open ( struct inode * inode , struct file * filep )
{
printk (" Device opened \ n ");
return 0;
}
3.2
3.3
This function is associated to the function write in the user space program.
Using a driver allows to access the hardware or the resource without needing to
set particular permissions.
static ssize_t mydev_write ( struct file * filep , const char * buf ,
size_t count , loff_t * f_pos )
{
printk (" Writing to the device ...\ n ");
return 1;
}
3.4
3.5
The main task of the initialization procedure is to register the driver and to
associate it to a specific device. In particular, the function register chardev
is used to associate the written module to the device with a specific major
number, the name of the device. In our example the device will be registered
with the major number 240.
static int __init init_mydevDriver ( void )
{
int result ;
if (( result = register_chrdev (240 , " mydev " , & mydev_fops )) < 0)
goto fail ;
printk (" mydev device driver loaded ...\ n ");
return 0;
fail :
return -1;
}
This procedure is responsible of unregistering the driver module.
static void __exit end_pp ( void )
{
unr egiste r_chrd ev (240 , " mydev ");
printk (" mydev driver unloaded ...\ n ");
}
3.6
The following program can test the generated device and its driver. It writes
and reads a byte to and from the mydev character device.
# include < fcntl .h >
# include < stdio .h >
int main ()
{
char buffer [1];
int fd ;
fd = open ("/ dev / mydev " , O_RDWR );
buffer [0]=0 x00 ;
write ( fd , buffer ,1 , NULL );
read ( fd , buffer ,1 , NULL );
printf (" Value : 0 x %02 x \ n " , buffer [0]);
close ( fd );
}