Showing posts with label Device Drivers. Show all posts
Showing posts with label Device Drivers. Show all posts

Linux: Device drivers

Device drivers interface applications to hardware.They are run in Kernel space. they are part of Kernel process and are loaded dynamically. 

Linux provides a feature called Kernel modules through which we can extend the functionality of the Kernel at run time.

Device Drivers in Linux are Kernel Modules. 
Device drivers are classified into three types:
  1. Character device driver
  2. Block device driver
  3. Network device driver
Character Device driver:
  • Device which works on stream of bytes. 
  • This type of device is exposed to client via character device driver. 
  • Driver implements – open, close, ioclt, read, write, mmap functionality. 
Block Device driver:
  • Devices which work on data chuncks/blocks. Example Hard disk/Flash drives. 
  • Block drivers have a completely different interface from character drivers. 
Network interfaces:
  • Any network transaction is made through this interface. 
  • Interface is defined and implemented for communication with network drivers by linux kernel.

Interrupt Handling in Linux

What is Interrupt?

An interrupt (also known as an exception or trap) is an event that causes the CPU to stop executing the current program and start executing a special piece of code called an interrupt handler or interrupt service routine (ISR).

There are two different kinds of interrupts:

         •Synchronous interrupt (Exception) produced by the CPU while processing instructions
         •Asynchronous interrupt (Interrupt) issued by other hardware devices
Handling interrupts:
   •Interrupts can occur at any time, the kernel tries to get it out of the way as soon as possible
   •An interrupt can be interrupted by another interrupt
   •There are regions in the kernel which must not be interrupted at all

Two different interrupt levels are defined:
   •Maskable interrupts issued by I/O devices; can be in two states, masked or unmasked. Only   
     unmasked interrupts are getting processed.
   •Nonmaskable interrupts; critical malfunctions (f.e. hardware failure); always processed by the 
    CPU.

Every hardware device has it's own Interrupt Request (IRQ) line. The IRQs are numbered starting from 0. All IRQ lines are connected to a Programmable Interrupt Controller (PIC). The PIC listens on IRQs and assigns them to the CPU. It is also possible to disable a specific IRQ line.

Top Half and Bottom Half:

One of the main problems with interrupt handling is how to perform lengthy tasks within a handler. Often a substantial amount of workmust be done in response to a device interrupt, but interrupt handlers need to finish up quickly and not keep interrupts blocked for long. These two needs (work and speed) conflict with each other,leaving the driver writer in a bit of a bind.

Linux (along with many other systems) resolves this problem by splitting the interrupt handler into two halves. The so-called top half is the routine that actually responds to the interrupt—the one you register with request_irq. The bottom half is a routine that is scheduled by the top half to be executed later, at a safer time. The big difference between the top-half handler and the bottom half is that all interrupts are enabled during execution of the bottom half—that’s why it runs at a safer time.

To implement bottom halves,two methods:
1) task lets
2) work queues