You can grab all required knowledge of device drivers like types, character driver, loading driver, unloading driver and more .To know more, get in touch with us by contacting us through our website.
1. What is a device driver?
Device driver is a software program that makes a particular piece of
hardware to respond to a well-defined internal programming interface. The
programming interface means whenever hardware is connected to the
system, that interface will invoke the specific driver for a specific device.
The interface loads the driver into the kernel whenever needed and remove
it when it finished its work. There are some drivers which are in built to the
kernel and some are loaded when require. In general, device driver is a
software layer that lies between the applications and the actual device.
Application uses the hardware and driver provides an interface between the
application and the hardware.
Types of device drivers:
1. Character driver
2. Block driver
3. Network driver
Introduction to character driver.
A character device driver provides a communication interface to a device
that can be accessed as a stream of bytes. These drivers generally perform
open,read,write and close operations. Data can be read and written to the
device as a stream of bytes. Eg., Serial port device driver.
Loading driver into the kernel.
A module is inserted into the kernel at run time using command 'insmod
name.ko'. This calles a function specified in the macro module_init().
module_init() is a driver initialization entry point, which must be called at
module insertion time. The program loads the module code and data into
the kernel and links any unresolved symbol in the module to the symbol
table of the kernel.
Unloading driver from the kernel.
When the driver finishes its work or the device plugged out from the
system, we should run rmmod command to remove the driver from the
kernel. when system executes rmmod command,it calles a function
specified in the macro module_exit(). module_exit() is a driver exit entry
point which must be called when removing a module. Module removal
program may fails if the module is still in use by any other application.
2. Operations to be performed in initialization function.
Use function alloc_chrdev_region() to register our driver. Device drivers are
identified using Major number and minor number. The major number
specifies which driver to use and devices connected to the driver are
identified by the minor numbers. There is only one Major number given to
the driver and it is the highest available number. Minor number varies in
accordance to the devices connected to the driver and starts from 0. The
alloc_chrdev_region(dev_t *dev, unsigned int firstminor, unsigned int
count, char *name), on successful completion provides the major and
minor number, holds in the first argument dev, count is device numbers
requested, and name is our driver name, appears in /proc/devices.
Operations to be performed in cleanup function.
When driver finishes its work we should remove it from the kernel and for
this undone whatever we did in the initialization function. Unregister the
driver using function call unregister_chrdev_region(dev_t first, unsigned int
count), where first is the dev number obtained by the call
alloc_chrdev_region() and count is number of minor numbers we requested
for device in alloc_chrdev_region() call.