<pstyle='text-align: justify;'>Whenever i search for a particular implementation, of let's say GPIO, only user space implementation comes up in search results which use the sysfs interface. Being more interested in hardware and the kernel side of things, I thought I did start practicing writing some kernel module drivers. So, here is my first simple GPIO kernel module driver for the Beagleboard-xM.</p>
<pstyle='text-align: justify;'>I have set up my environment using Buildroot from the below link. I used this, because at some point I intend to use the camera module and Max Galemin has integrated those packages in his buildroot version. OpenCV 2.3.1 is supported in this buildroot, so that's a very good plus point. Set up your environment using the instructions given on the below link or I assume you can use the standard buildroot available as well.</p>
<pstyle='text-align: justify;'>You can do the default build or you might want to add support for php, python, lighthttpd, nfs, thttpd and a few other things as well.</p>
<pstyle='text-align: justify;'>Below I am posting the source code for the driver module, the user space application and the Makefile.</p>
<pstyle='text-align: justify;'>The path for your kernel source and path for the CodeSourcery toolchain can and mostly will be different. So change them, as per your environment.</p>
<pstyle='text-align: justify;'>After this, compile your kernel driver module by typing "sudo make" at the command line prompt. After this, cross compile your user space application by typing</p>
<pstyle='text-align: justify;'>I have assumed that the cross compiler path has been added to your path environment variable.</p>
<pstyle='text-align: justify;'>You will now have a gpio.ko file and a gpio_app executable. Transfer these files to your beagleboard. I use the "scp" command to transfer files to my board over the ssh connection.</p>
<pstyle='text-align: justify;'>On the command prompt of your beagleboard, do "insmod gpio.ko". Find out the major number by checking the dmesg output or /proc/devices.</p>
<pstyle='text-align: justify;'>Assuming for example, that the major number is 248, do "mknod /dev/gpio c 248 0".</p>
<pstyle='text-align: justify;'>The mknod command has to be run for creation of the device node. If mknod is not run, you can see your module loaded using lsmod, but, you won't notice any entry in the /dev directory with which to access it through your user space application. After running mknod you can see the gpio driver entry in the /dev directory with ls /dev.</p>
<pstyle='text-align: justify;'>By default, the user led's 0 and 1 which are connected on GPIO 149 and 150, are used for indicating mmc card access activity and heartbeat.</p>
<pstyle='text-align: justify;'>You can turn these dafault behaviour off by first entering the /sys/class/gpio/beagleboard::usr0 and /sys/class/gpio/beagleboard::usr1 directory. After this, do "cat trigger". You should see the default behaviour marked.</p>
<pstyle='text-align: justify;'>Now, change the default behaviour for both by doing "echo none > trigger" in their respective directories.</p>
<pstyle='text-align: justify;'>Now, run the user space application by typing ./gpio_app on the command line prompt. You will be prompted to enter the value "0" or "1" to turn off or turn on the led. You can observe the state of the led pin on your board after this.</p>
<pstyle='text-align: justify;'>Refer the Linux Device Drivers book by Corbet, Rubini and Greg Kroah Hartman. The second and third chapters are important for this tutorial.</p>