Here is the full code of the driver that we have pieced together in the previous three chapters
Note: The code has been tested and found working on version 3.5.4
The above code being a kernel module, it needs a Makefile for compilation.
Makefile:
Note: The above makefile is taken from the Device Drivers book from Alessandro Rubini .
The make file checks if the KERNELRELEASE is set, because we need to know the location where the kernel Makefile is located, as it is that Makefile which we will use to compile our code.
If the KERNELRELEASE is not set then we set the KERNELDIR to the default location of the kernel tree, where we will find the Makefile.
After setting this we call make in the script, but this time we pass the "-c" option to indicate where the kernel makefile is located. The "M" option tells the make file where is the source code of the module that we want to compile into a kernel module. In our case the source code is present in the same folder so we pass value of present working directory.
Note that the name of the object code against "obj-m" in the makefile should be the name of your "c" file for the module, just replace the ".c" with ".o" .
Once you have created the file save it as Makefile (Note the capital "M"), in the same folder where your module is stored.
Open a terminal, and go to the file where you have the module and the Makefile, run the command
If the command does not throw any errors, then your code has been compiled successfully. Run "ls" and see that you should have a file by same name as your "c" code only with the ".ko" as the extension.
To insert the module into the kernel we will use the command "insmod", you will need superuser privileges to run the command.
If there are no errors your module is a part of the kernel now, as we have added a "printk" to print the major number in our init function, run the command "dmesg" and you will find the line printed, with the major number, for eg :
Make a note of the number, we will need it again.
Once we have inserted the module, we need to have a device which will use the driver or the module. So we can create an entry like a device in /dev using the command "mknod"
Run the command
eg:
If there are no errors we have a device ready that we can talk to using our driver.
NOTE: This code will not work with command "cat" , please use the application code in the next chapter. The updated code which will work with "cat" will be the chapter-6.
Next Chapter
User Access
Saturday, November 13, 2010
0 comments:
Post a Comment