July 30, 2013

Creating a linux device driver for the raspberry PI

first (i had hard times figuring out what missed me for correct compilation of a driver module ) ,try to Install the module-assistant for debian (i assume you’re logged in as ROOT ):
$ apt-get install module-assistant
$ m-a prepare(trys to get linux-headers )

At this epoch, you get an error : cant locate 3.6.11+ linux headers ( if you run the same version as mine is ..)
in doubt, you can check your linux version :

$ uname -r

thats bullshit.. GPL, linux, and so on… looks like it’s an adventure to get something going straight easy on the linux/raspberry Pi world.. :(
OK, lets get the linux headers (wget) from here : linux-headers-3611_3611-2_armhf (huge - i eihis.Com hosts it )
then, unpack it :

$ dpkg -i linux-headers-3.6.11+_3.6.11+-2_armhf.deb

At this point, the 3.6.11+ headers (tree) should be found here : /usr/src/linux-headers-3.6.11+
So, now let’s create a link to it for Kernel’s MAKE , after removing existing (bad) ones directories links :

$ rm -r /lib/modules/3.6.11+/build
$ rm -r /lib/modules/3.6.11+/src

[may be present. in my case, it was, but i tried many things before so, anyway, in a doubt , do it ]
Now, actually create the links :

$ ln -s /usr/src/linux-headers-3.6.11+ /lib/modules/3.6.11+/build
$ ln -s /usr/src/linux-headers-3.6.11+ /lib/modules/3.6.11+/src

now time to reboot ..

$ reboot

upon new login , CD to the directory of the module you want to compile, then ‘make’ to execute the Makefile.( beware of the Makefile name : case sensitive ! )
now, in the directory where your module.c code was, you get a module.ko file. thats the kernel module file created.
the Makefile should look like this :

obj-m :=yourmodule.o
KDIR :=/lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
[tab here] $(MAKE) -C $(KDIR) M=$(PWD) modules

where :
obj-m is the name of the object you want to build. the corresponding object.c file will be found by the MAKE command…
KDIR is the build directory (where parameters for kernel build,headers, and so on are located )
PWD is the destination (output .ko file) directory ( the current directory where your makefile and yourmodule.c file are located, for instance )

in case of problem, you can start by checking if the links have been created, actually.
Doing a :

$ ls -al /lib/modules/3.6.11+/build

should return :

/lib/modules/3.6.11+/build -> /usr/src/linux-headers-3.6.11+

If everything has run, you should now have a yourmodule.ko file into the current projects directory.
wich prove that the link is existing…
If everything has run, you should now have a yourmodule.ko file into the current projects directory.
you can test it by doing :

$ insmod yourmodule.ko

And stop it by doing :

$ rmmod yourmodule

I guess you now what to put in your ‘yourmodule.c’ file. hello world program examples can be found at many places dealing with linux driver’s compilation how-to websites.
This information post uses many sources (not listed) where i’ve found, finally, the way to compile drivers with no errors on the Raspberry Pi RASPBIAN , linux version 3.6.11+.
Hope this helps !

portions of code, with adaptations ,updates, modifications, and also Informations are coming from :

http://www.tldp.org/LDP/lkmpg/2.6/lkmpg.pdf (The linux kernel Module programming guide )

http://www.linuxforu.com/2011/04/character-device-files-creation-operations/ ( full of usefull articles )

http://gauss.ececs.uc.edu/Courses/c4029/lectures/work_queue.pdf ( work queues in Linux )

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.


cat{ } { post_423 } { } 2009-2015 EIhIS Powered by WordPress