Hardware/ARMEBS/3/LinuxDriver

From UIT
Jump to: navigation, search

Contents

Linux Driver for ARMEBS3 development guide

What is a driver ? (or why do I need a new driver?)

In Linux (like most OSes) a simple application can't access hardware. If you want accessing the hardware, you must use the kernel to acess it. Userland applications generally access the hardware through a (pseudo-)file located in the /dev Drivers are not always hardware related, many drivers simply add new functionalities to the kernel.

Where to start

A good starting point for writing drivers is the Linux device drivers book download (available in printed form from O'Reilly).

Here are some informations specific to ARMEBS3 drivers.

Step-by-step hello world driver

  • Create a new directory (change actual directory to it)
 mkdir /tmp/hello-module
 cd /tmp/hello-module
 /tmp/hello-module
  • Create the file Makefile, containing this:
 obj-m := hello.o
  • Create the file hello.c, containing this:
 #include <linux/init.h>
 #include <linux/module.h>
 static int hello_init(void)
 {
       printk(KERN_ALERT "Hello, world\n");
       return 0;
 }
 static void hello_exit(void)
 {
       printk(KERN_ALERT "Goodbye, cruel world\n");
 }
 module_init(hello_init);
 module_exit(hello_exit);
 MODULE_LICENSE("GPL");
  • Compile it
 make -C /opt/armebs3/linux/linux-2.6.15.4-armebs3/ M={{{`pwd`}}} modules
 make: Entering directory `/opt/armebs3/linux/linux-2.6.15.4-armebs3'
 CC [M]  /tmp/hello-module/hello.o
 Building modules, stage 2.
 MODPOST
 CC      /tmp/hello-module/hello.mod.o
 LD [M]  /tmp/hello-module/hello.ko
 make: Leaving directory `/opt/armebs3/linux/linux-2.6.15.4-armebs3'
  • install it (supposes NFS development environment)
 su -c "make -C /opt/armebs3/linux/linux-2.6.15.4-armebs3/ M={{{`pwd`}}} INSTALL_MOD_PATH=/opt/armebs3/nfs modules_install"
   Password:
   make: Entering directory `/opt/armebs3/linux/linux-2.6.15.4-armebs3'
     INSTALL /tmp/hello-module/hello.ko
   make: Leaving directory `/opt/armebs3/linux/linux-2.6.15.4-armebs3'
  • Test it
 dmesg -c > /dev/null
 depmod -a
 modprobe hello
 Hello, world
 rmmod hello
 dmesg -c
 Goodbye, cruel world

You can download the source code here: hello.c. Here is the makefile to compile the driver: Makefile.

Including the driver into the kernel

Now we've got a new driver as a module, we're satisfied with it's functionnality, and we want to include it into the kernel. For doing this we must include it into the kernel sources.

Personal tools
Namespaces
Variants
Actions
Navigation
Browse
Toolbox