Showing posts with label Kernel. Show all posts
Showing posts with label Kernel. Show all posts

Saturday, September 25, 2010

The Role of the Device Driver

As a programmer, you are able to make your own choices about your driver, and choose an acceptable trade-off between the programming time required and the flexibility of the result. Though it may appear strange to say that a driver is “flexible,” we like this word because it emphasizes that the role of a device driver is providing mechanism, not policy.
The distinction between mechanism and policy is one of the best ideas behind the Unix design. Most programming problems can indeed be split into two parts: “what capabilities are to be provided” (the mechanism) and “how those capabilities can be used” (the policy). If the two issues are addressed by different parts of the program, or even by different programs altogether, the software package is much easier to develop and to adapt to particular needs.

When writing drivers, a programmer should pay particular attention to this fundamental concept: write kernel code to access the hardware, but don’t force particular policies on the user, since different users have different needs. The driver should deal with making the hardware available, leaving all the issues about how to use the hardware to the applications. A driver, then, is flexible if it offers access to the hardware capabilities without adding constraints. Sometimes, however, some policy decisions must be made. For example, a digital I/O driver may only offer byte-wide access to the hardware in order to avoid the extra code needed to handle individual bits.
You can also look at your driver from a different perspective: it is a software layer that lies between the applications and the actual device. This privileged role of the driver allows the driver programmer to choose exactly how the device should appear: different drivers can offer different capabilities, even for the same device. The actual driver design should be a balance between many different considerations. For instance, a single device may be used concurrently by different programs, and the driver programmer has complete freedom to determine how to handle concurrency.
You could implement memory mapping on the device independently of its hardware capabilities, or you could provide a user library to help application programmers implement new policies on top of the available primitives, and so forth. One major consideration is the trade-off between the desire to present the user with as many options as possible and the time you have to write the driver, as well as the need to keep things simple so that errors don’t creep in.

Friday, September 24, 2010

Sample makefile to build a Loadable Kernel Module (LKM)

Step 1: Open a file with file name called qc (quick compile)
        
          vi qc
          #Write the below contents to the file
          #!/bin/bash
           KDIR=/lib/modules/`uname -r`/source
           echo "obj-m := $1.o" > Makefile
           make -C $KDIR M=`pwd` modules
          rm -f .$1*
          rm -rf .tmp_versions
          rm -f $1.o
          rm -f $1.mod*
         rm -f modules.order
         rm -f Module.symvers
         rm -f Makefile

Save the Makefile and close the same.
Now build your KLM by paasing your programe name as follows
 ./qc <programname>
Ex: ./qc driver

The output will be a driver.ko (kernel object file)

This can be loaded and removed into the kernel using the follwing commands
insmod - insert file into the kernel
rmmod - remove the module from kernel

example: insmod driver.ko  #Now you can see the driver in the kernel by running "lsmod" command
              rmmod driver  # Now this will not be seen in "lsmod"