Search

Writing an Example Driver From Scratch: Chapter-1 Header files

Chapter-1 Header files 

The series of posts explains writing a small example driver right from the scratch. There is no physical device, but we will take an array and consider it as our device for which the driver will be written.
The code is inspired by the example codes provided with the book "The Device Drivers" by Alessandro Rubini
Prerequisites
Basic knowledge of kernel programming and c Programming.

The driver being described is for 2.6.x kernels.

We will call the device  char_dev , which will be a structure as follows

struct device {
        char array[100];
        struct semaphore sem;
}char_dev;

So our device can hold a maximum of 100 characters and its concurrent access can be controlled using the semaphore "sem".

In this driver we will implement the following 4 operations for the device
Open.
Read.
Write.
Close.

As this is a basic driver we will not be implementing any control over the data being accepted from the user.

To begin with let us look at the possible header files we might need to write the driver.
As we are going to write a kernel program and it is going to be a module we would need the following two header files

linux/module.h
linux/kernel.h

Next to do the read and write we will make use of the functions copy__to_user and copy_from_user, for these operations we will need the header file

asm/uaccess.h

To make use of the semaphores as and when needed, we will need

linux/semaphore.h

And as we are going to write a character driver, we will need the following file

linux/cdev.h


So we can begin writing our module by including the required above files




#include < linux/module.h >
#include < linux/kernel.h >
#include < linux/fs.h>
#include < asm/uaccess.h >
#include < linux/semaphore.h >
#include < linux/cdev.h >


The next step is to figure out how do we write the init function. Init function that gets called the moment we insert the module into kernel.


Next Chapter
The init function


Reference books


Linux Kernel Programming (3rd Edition)

Linux Device Drivers, 3rd Edition

Ubuntu 10.04 does not add the windows entry to grub menu

If you installed the Ubuntu 10.04 over your windows and did not see the grub menu list the windows entry here is a work around.

First hold the shift key or press "esc" while booting to view the grub menu and make sure that windows is not listed as some times the grub screen might move too fast to even view what is listed.

If windows is not listed then boot into the ubutnu.
Open a terminal and type
       sudo fdisk -l
It should output something like

  Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      102400    7  HPFS/NTFS
Partition 1 does not end on cylinder boundary.
/dev/sda2              13        6375    51097600    7  HPFS/NTFS
/dev/sda3            6375       12749    51200000    7  HPFS/NTFS
/dev/sda4           12749       19458    53888001    5  Extended
/dev/sda5           12749       19070    50777088   83  Linux
/dev/sda6           19071       19458     3109888   82  Linux swap / Solaris

The entries that have HPFS/NTFS  in the system column are your windows entries.
You can access these drives by clicking on "Places" on the top menubar and the then on the partition you want to mount.

You can download the bootinfo script from

http://sourceforge.net/projects/bootinfoscript/files/bootinfoscript/0.55/boot_info_script055.sh/download?use_mirror=biznetnetworks

Run this script and look at the output, it would tell you in which /dev/sdaX is the windows bootmgr installed. Note that number X down.

In the command prompt type
       gksudo gedit /etc/grub.d/40_custom
This should open a text editor with a few lines of text in it.  At the end of this file add the following lines


 menuentry "Windows" {
root='(hd0,X)   {replace X by the number we found above}
        chainloader +1
}

Save the file by clicking on save option on the top and close the file.

Now run the following command on the terminal
   sudo update-grub

Once the command runs successfully restart your system.
Hold the shift key or press "esc" while the system boots to view the grub menu.
You should see windows listed in the options now.
Move the cursor to windows and press "enter" to boot windows.

If you see the erroe "BOOT MGR MISSING" then press cntrl+alt+delte and come to grub menu again.
Move the cursor to windows option and press "e".
you should see a line that has "root='(hd0,1)'" that we entered previously.
Now press "c" which will take you to "grub>" prompt.

At the "grub>" prompt type "ls"
It will list out all the disks that grub is able to recognize for eg.
(hd0) (hd0,1) )(hd0,2) etc...

Type
ls (hd0,1)
and see what the output is, if it does not display as a NTFS  partition or displays the name of your other disks for eg: one of you other windows drives.
try out
ls (hd0,2)
and similarly for all the hard disks present and figure out which ones are your windows drives.
Now type exit which will take you back tou your grub menu.
Again go to windows option press "e"
and change the root=(hd0,X) to which ever new number you have found and press cntrl+X to boot.
This should boot your windows.

Hope this helps.

You tube videos watch and download tool

Minitube is a cool utility that lets you watch as well as down load you tube videos.
When you run this tool you are presented with a search window where you can enter the topic on which you want to watch the videos.
The tool will pull out all the videos related to the search topic and start playing them one after the other and if you want to download any of the videos, just click on the download option and it will be downloaded on to your system.

Sure is a very useful tool.

Installation instruction for debian systems


sudo add-apt-repository ppa:ferramroberto/minitube
sudo apt-get update && sudo apt-get install minitube

You can find out more at