Device Driver Basic structure

Driver Generic structure
Basic structure of driver for every device is similar for every device. We can divide driver structure in following sections.
1) Init and Exit Calls
2) fops structure
3)Interrupt Routine
lets discuss in brief about each section.

1) Init and Exit Calls
As we already discussed that our driver needs to interact with kernel. So driver has to notify to kernel that it also exist in the system. So to notify to the kernel driver has init calls which is used for hooking our driver routines into kernel. Besides notifying or registering with kernel it also allocates any kernel memory required for device and initialize hardware. This function is called at boot time.
In the same way we need to detach our driver from the system when our system is shutting down . To serve this purpose driver use Exit call which is called by kernel at the time of shutting down of system. This call unregister driver or free all the resources it allocated at the time of init call.
static int hello_init(void)
{
printk(KERN_ALERT “Hello world”);
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT “Goodby”);
}
module_init(hello_init);
module_exit(hello_exit);
Above example show init and exit calls of driver. module_init() and module_exit() macro is used to tell the kernel that these are the init and exit calls of our driver.
Above init and exit calls doing nothing but printing only Kernel Message.
2) fops structure
This structure defines the capability of our device. suppose if our device supports reading and writing operation then we need to give pointer of reading and writing functions of our driver into this structure.

driver_fops

3) Interrupt Routine

If our driver supports any hardware interrupt then we need to define interrupt routine also. This function will be called whenever we will receive interrupt for our device. We need to register the interrupt support in the init function by using request_irq() funtion provided by Linux kernel.

while registering for interrupt we mention interrupt routine which will actual handles interrupt.driver_irq

By merging all this section in a single program we get our complete driver. In next chapter we will discuss how we can build and execute our driver.

adana elektrikci

', 'auto'); ga('send', 'pageview');