Archive for the ‘Driver Programming’ Category

http://lwn.net/Articles/175432/

http://en.wikipedia.org/wiki/Radix_tree

#include<linux/module.h>

/*following header file needs to be included for all proc file system related function calls*/
#include<linux/proc_fs.h>

/*
Function : read_procmem
Desc : Following function performs the functionality to provide data to the page pointed by buf pointer. Whenever a read system call is issued to the files belonging to this driver in /proc file system , this is the function responsible to provide the data to the read system call.
*/
int read_procmem(char *buf, char **start, off_t offset,int count, int *eof, void *data){
char p[]="Hello Friend...I am from testFile but I am not in testFile\n";
printk("Test Function\n");
sprintf(buf,"%s",p);
*eof=1;
return sizeof(p);
}

static int __init mymodule_init(void)
{
/*following function call creates a file named /proc/testFile /proc file system
this function takes argument as fileName, file mode,base directory, read_proc function name and data pointer to be passed to read_proc function.
*/
create_proc_read_entry("testFile",0,NULL,read_procmem,NULL);
printk("My Module Has been loaded\n");
return 0;
}

static void __exit mymodule_exit(void)
{
/*following function call removes the file from the /proc file system
One must remove all the files from /proc while performing the cleanup otherwise it may leave the kernel in unstable state and may result in crash.
this function takes on argument as the fileName and the base directory structure pointer.
*/
remove_proc_entry("testFile",NULL);
printk("My Module has been unregistered\n");
return;
}

module_init(mymodule_init);
module_exit(mymodule_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("KAPIL JAIN");
MODULE_DESCRIPTION("Test Module");
MODULE_VERSION("0:0.1-1");

above is the sample code to generate the file named testFile in /rpoc directory. Save the above code to a file named mydriver.c and copy the file to the directory “drivers/misc” in your linux source code and add the entry in the in the same directory’s Makefile as :
obj-m += mydriver.o
Now compile the driver by issuing the following command from the same directory:
make -C ../../ SUBDIRS=$PWD modules
it will generate a mydriver.ko file in the same directory. load the driver to the memory :
insmod mydriver.ko
look for following message in dmesg output :
My Module Has been loaded
Now if you will look into the /proc directory, you will see the file named testFile. Check its size, it says 0Bytes. Now issue the cat command on the file:
cat /proc/testFile
It should give the following output:
Hello Friend...I am from testFile but I am not in testFile
In dmesg output also you will see following message:
Test Function
Whenever the file read is issued on the dynamic file from /proc, it’s output is generated by the corresponding driver’s function whose prototype is as follows:
int (*read_proc)(char *page, char **start, off_t offset, int count, int *eof, void *data);
Sine there is actually no static content in any of the file in proc file system and all the content is generated on request, hence it is called the virtual file system. It resides there but its not actually there. ­čÖé