Hardware/ARMEBS/3/LinuxDriver

From UIT
(Difference between revisions)
Jump to: navigation, search
(decide where to put it)
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{private}}
+
{{public}}
 
{{TOC right}}
 
{{TOC right}}
 
= Linux Driver for ARMEBS3 development guide =
 
= Linux Driver for ARMEBS3 development guide =
Line 10: Line 10:
 
== Where to start ==
 
== Where to start ==
  
A good starting point for writing drivers is the '''Linux device drivers''' book (available in printed form from O'Reilly).
+
A good starting point for writing drivers is the '''Linux device drivers''' book [http://vlegit.hevs.ch/ARMEBS3/linux%2520device%2520drivers%25203.pdf download] (available in printed form from O'Reilly).
  
 
Here are some informations specific to ARMEBS3 drivers.
 
Here are some informations specific to ARMEBS3 drivers.
Line 16: Line 16:
 
== Step-by-step hello world driver ==
 
== Step-by-step hello world driver ==
 
* Create a new directory (change actual directory to it)
 
* Create a new directory (change actual directory to it)
   #!shell
+
   mkdir /tmp/hello-module
  #u devuser@devmachine %w > ''' %r '''
+
   cd /tmp/hello-module
  u anywhere mkdir /tmp/hello-module
+
   /tmp/hello-module
   u anywhere cd /tmp/hello-module
+
   u /tmp/hello-module
+
 
* Create the file Makefile, containing this:
 
* Create the file Makefile, containing this:
  #!Makefile
 
 
   obj-m := hello.o
 
   obj-m := hello.o
 
* Create the file hello.c, containing this:
 
* Create the file hello.c, containing this:
  #!c
 
 
   #include <linux/init.h>
 
   #include <linux/init.h>
 
   #include <linux/module.h>
 
   #include <linux/module.h>
Line 44: Line 40:
  
 
* Compile it
 
* Compile it
   #!shell
+
   make -C /opt/armebs3/linux/linux-2.6.15.4-armebs3/ M={{{`pwd`}}} modules
  #u devuser@devmachine %w > ''' %r '''
+
  u /tmp/hello 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'
 
   make: Entering directory `/opt/armebs3/linux/linux-2.6.15.4-armebs3'
 
   CC [M]  /tmp/hello-module/hello.o
 
   CC [M]  /tmp/hello-module/hello.o
Line 54: Line 48:
 
   LD [M]  /tmp/hello-module/hello.ko
 
   LD [M]  /tmp/hello-module/hello.ko
 
   make: Leaving directory `/opt/armebs3/linux/linux-2.6.15.4-armebs3'
 
   make: Leaving directory `/opt/armebs3/linux/linux-2.6.15.4-armebs3'
  u /tmp/hello
 
 
* install it (supposes NFS development environment)
 
* install it (supposes NFS development environment)
   #!shell
+
   su -c "make -C /opt/armebs3/linux/linux-2.6.15.4-armebs3/ M={{{`pwd`}}} INSTALL_MOD_PATH=/opt/armebs3/nfs modules_install"
  #u devuser@devmachine %w > ''' %r '''
+
    Password:
  u /tmp/hello-module su -c "make -C /opt/armebs3/linux/linux-2.6.15.4-armebs3/ M={{{`pwd`}}} INSTALL_MOD_PATH=/opt/armebs3/nfs modules_install"
+
    make: Entering directory `/opt/armebs3/linux/linux-2.6.15.4-armebs3'
  Password:
+
      INSTALL /tmp/hello-module/hello.ko
  make: Entering directory `/opt/armebs3/linux/linux-2.6.15.4-armebs3'
+
    make: Leaving 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'
+
  u /tmp/hello-module
+
 
* Test it
 
* Test it
   #!shell
+
   dmesg -c > /dev/null
  #tr root@target %w > ''' %r '''
+
   depmod -a
  tr anywhere dmesg -c > /dev/null
+
   modprobe hello
   tr anywhere depmod -a
+
   tr anywhere modprobe hello
+
 
   Hello, world
 
   Hello, world
   tr anywhere rmmod hello
+
   rmmod hello
   tr anywhere dmesg -c
+
   dmesg -c
 
   Goodbye, cruel world
 
   Goodbye, cruel world
 +
 +
You can download the source code here: [[Media:ARMEBS3-driver-hello.c.zip|hello.c]].
 +
Here is the makefile to compile the driver: [[Media:ARMEBS3-driver-makefile.zip|Makefile]].
  
 
== Including the driver into the kernel ==
 
== Including the driver into the kernel ==
Line 79: Line 70:
 
For doing this we must include it into the kernel sources.
 
For doing this we must include it into the kernel sources.
  
=== decide where to put it ===
+
[[Category:Hardware]] [[Category:ARMEBS]]
The kernel sources are arranged as
+

Latest revision as of 14:42, 26 June 2015

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