Difference between revisions of "EBC Exercise 26 Device Drivers"

From eLinux.org
Jump to: navigation, search
m (Compiling)
m (Added sudo's)
(46 intermediate revisions by 7 users not shown)
Line 1: Line 1:
 
[[Category:ECE497]]
 
[[Category:ECE497]]
 +
{{YoderHead}}
  
Chapter 8 of the text [http://www.amazon.com/Embedded-Linux-Primer-Practical-Real-World/dp/0137017839] gives a nice example of a minimal device driver.  The purpose of this lab is to implement that driver.
+
Derek Molloy's excellent book Exploring BeagleBone [http://exploringbeaglebone.com/] has an Extra Content section [http://exploringbeaglebone.com/kernelprogramming/] on Linux Kernel Programming.  Part 1 [http://derekmolloy.ie/writing-a-linux-kernel-module-part-1-introduction/] is a nice example of a writing a minimal kernel module.
  
== Minimal Device Driver Example ==
+
Here are instructions for compiling the example on the Bone.
  
=== Compiling ===
+
== Cloning Source and Compiling ==
 +
You need to load the correct kernel headers on the bone before you can compile the driver.
  
Follow the 5 steps given in section 8.1.4 on page 205. You can get a copy of Listing 8-1 here ([[ECE497_Listings_for_Embedded_Linux_Primer_Chapter_8]]).  Once finished you will have a file called <code>hello1.c</code> in <code>.../drivers/char/examples</code> and have the kernel configure file and Makefile updated for the new driver.  See section 4.4 on page 89 for help with modifying the config files.
+
  bone$ '''time sudo apt install linux-headers-`uname -r`'''
  
Note: There is a typo in Listing 8-2.
+
Note: Those are back quotes (top left on the keyboard, above the TAB key) around '''uname -r'''.  
  
If you have created the '''crossCompileEnv.sh''' file and sourced it, all you have to do to make the modules is cd to the top of the kernel directory and then:
+
The '''uname''' command looks up the number of the kernel that's currently running.  The back quotes
<pre>
+
take that number and past it after '''linux-headers-''' and does an '''apt install''' on it.  A couple minutes later you have all the headers loaded.
$ make modules
 
</pre>
 
Mine took a while the first time as it compiles all the modules. The second it only took 31 seconds.
 
  
=== Moving to Beagle ===
+
Now load the examples.
 +
 
 +
bone$ '''git clone https://github.com/derekmolloy/exploringBB.git'''
 +
 
 +
Now you are ready to run the examples.
 +
 
 +
== Part 1: Introduction - Minimal Device Driver Example ==
 +
Change to the correct directory and make.
 +
 
 +
bone$ '''cd exploringBB/extras/kernel/hello'''
 +
bone$ '''make'''
 +
make -C /lib/modules/4.14.67-ti-r73/build/ M=/home/debian/exploringBB/extras/kernel/hello modules
 +
make[1]: Entering directory '/usr/src/linux-headers-4.14.67-ti-r73'
 +
  CC [M]  /home/debian/exploringBB/extras/kernel/hello/hello.o
 +
  Building modules, stage 2.
 +
  MODPOST 1 modules
 +
  CC      /home/debian/exploringBB/extras/kernel/hello/hello.mod.o
 +
  LD [M]  /home/debian/exploringBB/extras/kernel/hello/hello.ko
 +
make[1]: Leaving directory '/usr/src/linux-headers-4.14.67-ti-r73'
 +
bone$ '''ls'''
 +
  hello.c  hello.mod.c  hello.o  modules.order
 +
hello.ko  hello.mod.o  Makefile  Module.symvers
 +
 
 +
Your newly compiled kernel module is in '''hello.ko'''.
 +
 
 +
=== Inserting your module ===
 +
 
 +
See if your module is there
 +
bone$ '''modinfo hello.ko'''
 +
filename:      /home/debian/exploringBB/extras/kernel/hello/hello.ko
 +
version:        0.1
 +
description:    A simple Linux driver for the BBB.
 +
author:        Derek Molloy
 +
license:        GPL
 +
srcversion:    0DD9FE0DE42157F9221E608
 +
depends:       
 +
name:          hello
 +
vermagic:      4.14.67-ti-r73 SMP preempt mod_unload modversions ARMv7 p2v8
 +
parm:          name:The name to display in /var/log/kern.log (charp)
 +
 
 +
That looks good, now '''insmod''' the module and check the log file.
 +
 
 +
bone$ '''sudo insmod hello.ko'''
 +
bone$ dmesg -H | tail -1
 +
[  +2.857480] EBB: Hello world from the BBB LKM!
  
On the beagle edit /lib/modules/2.6.32/modules.dep and add
 
<pre>
 
/lib/modules/2.6.32/kernel/drivers/char/examples/hello1.ko:
 
</pre>
 
Then copy the file <code>…/drivers/char/examples/hello1.ko</code> on the host computer to <code>/lib/modules/2.6.32/kernel/drivers/char/examples/</code> on your Beagle.  This can be done with a single command though you may have to mkdir the char/examples directory on the Beagle first.
 
<pre>
 
$ cd …/drivers/char/examples
 
$ rcp hello1.ko root@beagle:/lib/modules/2.6.32/kernel/drivers/char/examples
 
</pre>
 
Now, on the Beagle, modprobe the module and check the log file.
 
<pre>
 
# /sbin/modprobe hello1
 
# dmesg | tail -4
 
</pre>
 
 
You should see your Init message.  And then...
 
You should see your Init message.  And then...
<pre>
+
bone$ '''sudo rmmod hello'''
# /sbin/modprobe -r hello1
+
bone$ '''dmesg -H | tail -2'''
# dmesg | tail -4
+
[  +4.182591] EBB: Hello world from the BBB LKM!
</pre>
+
[  +3.542350] EBB: Goodbye world from the BBB LKM!
 +
 
 
should show your Exit message.
 
should show your Exit message.
 +
 +
=== Passing Parameters ===
 +
You can pass parameters when inserting a module. 
 +
 +
bone$ '''sudo insmod hello.ko name="Prof.Yoder"'''
 +
bone$ '''dmesg -H | tail -1'''
 +
[Sep24 16:53] EBB: Hello Prof.Yoder from the BBB LKM!
 +
bone$ '''sudo rmmod hello'''
 +
bone$ '''dmesg -H | tail -2'''
 +
[Sep24 16:53] EBB: Hello Prof.Yoder from the BBB LKM!
 +
[Sep24 16:54] EBB: Goodbye Prof.Yoder from the BBB LKM!
 +
 +
== Part 2: A Character Device ==
 +
 +
Part 2 of Molloy's example [http://derekmolloy.ie/writing-a-linux-kernel-module-part-2-a-character-device/] is a character device.  The code needs a slight change before it will run.
 +
 +
bone$ '''cd exploringBB/extras/kernel/ebbchar'''
 +
 +
Edit '''ebbchar.c''' and make the following changes.
 +
 +
bone$ '''git diff ebbchar.c'''
 +
diff --git a/extras/kernel/ebbchar/ebbchar.c b/extras/kernel/ebbchar/ebbchar.c
 +
index 771b859..f869859 100644
 +
--- a/extras/kernel/ebbchar/ebbchar.c
 +
+++ b/extras/kernel/ebbchar/ebbchar.c
 +
@@ -142,7 +142,9 @@ static ssize_t dev_read(struct file *filep, char *buffer, size_t len, loff_t *of
 +
  *  @param offset The offset if required
 +
  */
 +
  static ssize_t dev_write(struct file *filep, const char *buffer, size_t len, loff_t *offset){
 +
-  sprintf(message, "%s(%zu letters)", buffer, len);  // appending received string with its length
 +
+  unsigned long ret;
 +
+  // sprintf(message, "%s(%zu letters)", buffer, len);  // appending received string with its length
 +
+  ret = copy_from_user(message, buffer, len);
 +
+  size_of_message = len;                // store the length of the stored message
 +
    printk(KERN_INFO "EBBChar: Received %zu characters from the user\n", len);
 +
    return len;
 +
 +
Add the lines starting with '''+''' and comment out the '''sprintf''' starting with '''-'''.
 +
 +
Now make and insert.
 +
 +
bone$ '''make'''
 +
make -C /lib/modules/4.14.67-ti-r73/build/ M=/home/debian/exploringBB/extras/kernel/ebbchar modules
 +
make[1]: Entering directory '/usr/src/linux-headers-4.14.67-ti-r73'
 +
  CC [M]  /home/debian/exploringBB/extras/kernel/ebbchar/ebbchar.o
 +
  Building modules, stage 2.
 +
  MODPOST 1 modules
 +
  CC      /home/debian/exploringBB/extras/kernel/ebbchar/ebbchar.mod.o
 +
  LD [M]  /home/debian/exploringBB/extras/kernel/ebbchar/ebbchar.ko
 +
make[1]: Leaving directory '/usr/src/linux-headers-4.14.67-ti-r73'
 +
cc testebbchar.c -o test
 +
bone$ '''sudo insmod ebbchar.ko'''
 +
bone$ '''dmesg -H | tail -4
 +
[Sep24 17:08] EBBChar: Initializing the EBBChar LKM
 +
[  +0.011910] EBBChar: registered correctly with major number 241
 +
[  +0.016113] EBBChar: device class registered correctly
 +
[  +0.010024] EBBChar: device class created correctly
 +
 +
Now test it.
 +
bone$ '''sudo ./test'''
 +
Starting device test code example...
 +
Type in a short string to send to the kernel module:
 +
'''This is a test!'''
 +
Writing message to the device [This is a test!].
 +
Press ENTER to read back from the device...
 +
 +
Reading from the device...
 +
The received message is: [This is a test!]
 +
End of the program
 +
bone$ '''dmesg -H | tail -8'''
 +
[Sep24 17:08] EBBChar: Initializing the EBBChar LKM
 +
[  +0.011910] EBBChar: registered correctly with major number 241
 +
[  +0.016113] EBBChar: device class registered correctly
 +
[  +0.010024] EBBChar: device class created correctly
 +
[Sep24 17:09] EBBChar: Device has been opened 1 time(s)
 +
[  +9.333771] EBBChar: Received 15 characters from the user
 +
[  +1.185798] EBBChar: Sent 15 characters to the user
 +
[  +0.009778] EBBChar: Device successfully closed
 +
 +
Look over '''ebbchar.c''' and '''testebbchar.c''' to see how the user space and the kernel interact.
 +
 +
== Reference ==
 +
 +
[http://www.cnx-software.com/2011/08/19/how-to-write-and-submit-a-linux-kernel-patch/ How to Write and Submit a Linux Kernel Patch]
 +
 +
{{YoderFoot}}

Revision as of 09:35, 5 October 2020

thumb‎ Embedded Linux Class by Mark A. Yoder


Derek Molloy's excellent book Exploring BeagleBone [1] has an Extra Content section [2] on Linux Kernel Programming. Part 1 [3] is a nice example of a writing a minimal kernel module.

Here are instructions for compiling the example on the Bone.

Cloning Source and Compiling

You need to load the correct kernel headers on the bone before you can compile the driver.

bone$ time sudo apt install linux-headers-`uname -r`

Note: Those are back quotes (top left on the keyboard, above the TAB key) around uname -r.

The uname command looks up the number of the kernel that's currently running. The back quotes take that number and past it after linux-headers- and does an apt install on it. A couple minutes later you have all the headers loaded.

Now load the examples.

bone$ git clone https://github.com/derekmolloy/exploringBB.git

Now you are ready to run the examples.

Part 1: Introduction - Minimal Device Driver Example

Change to the correct directory and make.

bone$ cd exploringBB/extras/kernel/hello
bone$ make
make -C /lib/modules/4.14.67-ti-r73/build/ M=/home/debian/exploringBB/extras/kernel/hello modules
make[1]: Entering directory '/usr/src/linux-headers-4.14.67-ti-r73'
  CC [M]  /home/debian/exploringBB/extras/kernel/hello/hello.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/debian/exploringBB/extras/kernel/hello/hello.mod.o
  LD [M]  /home/debian/exploringBB/extras/kernel/hello/hello.ko
make[1]: Leaving directory '/usr/src/linux-headers-4.14.67-ti-r73'
bone$ ls
 hello.c   hello.mod.c  hello.o   modules.order
hello.ko  hello.mod.o  Makefile  Module.symvers

Your newly compiled kernel module is in hello.ko.

Inserting your module

See if your module is there

bone$ modinfo hello.ko
filename:       /home/debian/exploringBB/extras/kernel/hello/hello.ko
version:        0.1
description:    A simple Linux driver for the BBB.
author:         Derek Molloy
license:        GPL
srcversion:     0DD9FE0DE42157F9221E608
depends:        
name:           hello
vermagic:       4.14.67-ti-r73 SMP preempt mod_unload modversions ARMv7 p2v8 
parm:           name:The name to display in /var/log/kern.log (charp)

That looks good, now insmod the module and check the log file.

bone$ sudo insmod hello.ko
bone$ dmesg -H | tail -1

[ +2.857480] EBB: Hello world from the BBB LKM!

You should see your Init message. And then...

bone$ sudo rmmod hello
bone$ dmesg -H | tail -2
[  +4.182591] EBB: Hello world from the BBB LKM!
[  +3.542350] EBB: Goodbye world from the BBB LKM!

should show your Exit message.

Passing Parameters

You can pass parameters when inserting a module.

bone$ sudo insmod hello.ko name="Prof.Yoder"
bone$ dmesg -H | tail -1
[Sep24 16:53] EBB: Hello Prof.Yoder from the BBB LKM!
bone$ sudo rmmod hello
bone$ dmesg -H | tail -2
[Sep24 16:53] EBB: Hello Prof.Yoder from the BBB LKM!
[Sep24 16:54] EBB: Goodbye Prof.Yoder from the BBB LKM!

Part 2: A Character Device

Part 2 of Molloy's example [4] is a character device. The code needs a slight change before it will run.

bone$ cd exploringBB/extras/kernel/ebbchar

Edit ebbchar.c and make the following changes.

bone$ git diff ebbchar.c
diff --git a/extras/kernel/ebbchar/ebbchar.c b/extras/kernel/ebbchar/ebbchar.c
index 771b859..f869859 100644
--- a/extras/kernel/ebbchar/ebbchar.c
+++ b/extras/kernel/ebbchar/ebbchar.c
@@ -142,7 +142,9 @@ static ssize_t dev_read(struct file *filep, char *buffer, size_t len, loff_t *of
  *  @param offset The offset if required
  */
 static ssize_t dev_write(struct file *filep, const char *buffer, size_t len, loff_t *offset){
-   sprintf(message, "%s(%zu letters)", buffer, len);   // appending received string with its length
+   unsigned long ret;
+   // sprintf(message, "%s(%zu letters)", buffer, len);   // appending received string with its length
+   ret = copy_from_user(message, buffer, len);
+   size_of_message = len;                 // store the length of the stored message
    printk(KERN_INFO "EBBChar: Received %zu characters from the user\n", len);
    return len;

Add the lines starting with + and comment out the sprintf starting with -.

Now make and insert.

bone$ make
make -C /lib/modules/4.14.67-ti-r73/build/ M=/home/debian/exploringBB/extras/kernel/ebbchar modules
make[1]: Entering directory '/usr/src/linux-headers-4.14.67-ti-r73'
  CC [M]  /home/debian/exploringBB/extras/kernel/ebbchar/ebbchar.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/debian/exploringBB/extras/kernel/ebbchar/ebbchar.mod.o
  LD [M]  /home/debian/exploringBB/extras/kernel/ebbchar/ebbchar.ko
make[1]: Leaving directory '/usr/src/linux-headers-4.14.67-ti-r73'
cc testebbchar.c -o test
bone$ sudo insmod ebbchar.ko
bone$ dmesg -H | tail -4
[Sep24 17:08] EBBChar: Initializing the EBBChar LKM
[  +0.011910] EBBChar: registered correctly with major number 241
[  +0.016113] EBBChar: device class registered correctly
[  +0.010024] EBBChar: device class created correctly

Now test it.

bone$ sudo ./test
Starting device test code example...
Type in a short string to send to the kernel module:
This is a test!
Writing message to the device [This is a test!].
Press ENTER to read back from the device...

Reading from the device...
The received message is: [This is a test!]
End of the program
bone$ dmesg -H | tail -8
[Sep24 17:08] EBBChar: Initializing the EBBChar LKM
[  +0.011910] EBBChar: registered correctly with major number 241
[  +0.016113] EBBChar: device class registered correctly
[  +0.010024] EBBChar: device class created correctly
[Sep24 17:09] EBBChar: Device has been opened 1 time(s)
[  +9.333771] EBBChar: Received 15 characters from the user
[  +1.185798] EBBChar: Sent 15 characters to the user
[  +0.009778] EBBChar: Device successfully closed

Look over ebbchar.c and testebbchar.c to see how the user space and the kernel interact.

Reference

How to Write and Submit a Linux Kernel Patch




thumb‎ Embedded Linux Class by Mark A. Yoder