Building a Simple Device Driver in Linux

The code, scripts, and some of the instructions in this post are adapted from the second chapter of _The Linux Kernel Module Programming Guide_, by Peter Jay Salzman, Michael Burian, and Ori Pomerantz. As of this writing, an electronic version of the book is available at the URL “http://www.tldp.org/LDP/lkmpg/2.6/lkmpg.pdf“.

1. Download and install Oracle VirtualBox. As of this writing, the latest version is available at the URL “https://www.virtualbox.org/wiki/Downloads“.

2. Download a copy of the Ubuntu operating system in .ISO format. As of this writing, the latest version is available at the URL “http://www.ubuntu.com/download/desktop“.

3. Run VirtualBox, create a new virtual machine instance, and install Ubuntu on it. Follow the prompts, and accept the default settings wherever possible.

4. Start up the new virtual instance of Ubuntu and allow it to boot. Log in when prompted and wait for the the desktop to appear. The remaining instructions in this document should be applied to the virtual Ubuntu instance running in VirtualBox, not to the host operating system.

5. In any convenient location, create a new folder named “DeviceDriverTest”.

6. In the newly created DeviceDriverTest directory, create a new file called “hello-1.c”, containing the following text.

#include <linux/module.h>
#include <linux/kernel.h>

int init_module(void)
{
	printk(KERN_INFO "Hello world 1.\n");
	return 0;
}

void cleanup_module(void)
{
	printk(KERN_INFO "Goodbye world 1.\n");
}

7. Still in the DeviceDriverTest directory, create a new file named “Makefile”, containing the following text.

obj-m += hello-1.o

all:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

8. Open a command prompt console (perhaps by selecting the menu item “Accessories ~ Console” from the Start Menu) and navigate to the DeviceDriverTest directory (perhaps by entering the command “cd Desktop/DeviceDriverTest”).

9. Still in the console window, enter the command “make”. The file ‘hello-1.c” will be compiled, and a new object file named “hello-1.ko” will be created.

10. Still in the console window, enter the command “sudo su” and provide the proper password to run commands as the root user.

11. Still in the console window, enter the command “insmod ./hello-1.ko”. The newly created module will be inserted into the Linux kernel.

12. Still in the console window, enter the command “nano /proc/modules”. A text file will be opened in the “nano” text editor. The name of the newly installed “hello-1” module should be visible at the top of the opened file.

13. If desired, remove the module from the Linux kernel by entering the command “rmmod hello-1”.

Advertisements
This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

One Response to Building a Simple Device Driver in Linux

  1. Reblogged this on Perfectly Opaque and commented:
    God Bless Open-source!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s