Difference between revisions of "BBB Kernel Modules"

From Klaus' wiki
Jump to: navigation, search
(The Linux Device Driver book examples)
(Working with the example drivers)
 
(22 intermediate revisions by the same user not shown)
Line 4: Line 4:
 
First you'll need to download a few things before starting to develop kernel modules.
 
First you'll need to download a few things before starting to develop kernel modules.
 
<source lang=bash>
 
<source lang=bash>
root ..$] apt-get update
+
$] apt-get update
root ..$] apt-cache search linux-headers-$(uname -r)
+
$] apt-cache search linux-headers-$(uname -r)
 
</source>
 
</source>
 
will update your local copy of the content on the remote repositories and then search through for the linux-headers.
 
will update your local copy of the content on the remote repositories and then search through for the linux-headers.
Line 14: Line 14:
 
Then
 
Then
 
<source lang=bash>
 
<source lang=bash>
root ..$] apt-get install linux-headers-$(uname -r)
+
$] apt-get install linux-headers-$(uname -r)
apt-get install linux-headers-$(uname -r)
+
 
  Reading package lists... Done
 
  Reading package lists... Done
 
  Building dependency tree       
 
  Building dependency tree       
Line 37: Line 36:
 
$] groupadd moduledev
 
$] groupadd moduledev
 
$] usermod <desired login name> -G moduledev
 
$] usermod <desired login name> -G moduledev
 +
$] mkdir -p /home/emb
 +
$] chown <desired login name>:moduledev /home/emb
 
</source>
 
</source>
First line adds the new user, the second forces a password on that account and the third adds a development group to use for development and finaly the login name just created is added to the moduledev group.
+
The first line adds the new user, the second forces a password on that account and the third adds a development group to use for development and finaly the login name just created is added to the moduledev group. Then we create a work directory to develop modules in - /home/emb and assigns it to the moduledev group and <desired login name> as the owner.
  
Login as this user now.
+
Login as this new user now.
  
 
==The Linux Device Driver book examples==
 
==The Linux Device Driver book examples==
 
Along with the book "Linux Device Drivers" came a bunch of example drivers - the scull drivers.
 
Along with the book "Linux Device Drivers" came a bunch of example drivers - the scull drivers.
  
Now create a directory for your copy of the examples of the scull drivers
+
===Preparation===
<source lang=bash>
+
So get a copy of the drivers to /home/emb - do use you own login name now!
$] mkdir -p /home/emb
+
$] cd /home
+
$] chown moduledev:moduledev emb
+
</source>
+
next copy the drivers from bren to /home/emb
+
 
<source lang =bash>
 
<source lang =bash>
 
$] cd /home/emb
 
$] cd /home/emb
$] scp klausk@bren.hih.au.dk:/home/emb/examples.tar.gz .
+
$] git clone https://github.com/martinezjavier/ldd3.git
 
</source>
 
</source>
if you don't have a DNS entry for bren.hih.au.dk then use the IP address. And of course use your own login name.
+
thanks to Javier Martinez Canillas, most of the drivers has been fixed so that they will compile in relatively resent kernels.
  
Unpack the examples
+
Change the group ownership if you expect more persons to use this directory, for instance when working on a shared server.
 
<source lang=bash>
 
<source lang=bash>
$] tar zxvf examples.tar.gz
+
$]chgrp -R moduledev examples
 
</source>
 
</source>
 +
 +
Enter the Linux Device Driver directory
 +
<source lang=bash>
 +
$] cd ldd3
 +
</source>
 +
 +
===Working with the example drivers===
 +
First create a little helper script in the examples directory. The script will setup an environment variable KERNELDIR, needed to locate the kernel development directory
 +
 +
<source lang=bash>
 +
#!/bin/bash
 +
#
 +
# Preparation for the compilation of the scull example code
 +
#
 +
KERNELDIR=/usr/src/linux-headers-`uname -r`/
 +
export KERNELDIR
 +
</source>
 +
 +
Make it executable
 +
<source lang=bash>
 +
$] chmod +x settings.sh
 +
</source>
 +
 +
'''Every time''' you enter this directory execute this
 +
<source lang=bash>
 +
$] . settings.sh
 +
</source>
 +
 +
NOTE the '''.''' is the same as writing '''source'''
 +
 +
If you forget to set this environment variable it won't work as expected.
 +
 +
Compile the driver. (You may first have to cd into the specific drivers directory. In the example below it is the scull driver.)
 +
<source lang=bash>
 +
$] cd scull
 +
$] make modules
 +
</source>
 +
 +
If no errors you have '''SUCCESS'''.
 +
 +
Now try to insert the module into the kernel and remove it again. Do as the book instruct you to do.

Latest revision as of 08:36, 12 April 2016

Here you'll find information on kernel modules, i.e. the device drivers that extends the kernel so it will be able to handle specific hardware.

Getting started

First you'll need to download a few things before starting to develop kernel modules.

$] apt-get update
$] apt-cache search linux-headers-$(uname -r)

will update your local copy of the content on the remote repositories and then search through for the linux-headers.

Expect an output something like:

linux-headers-3.8.13-bone70 - Linux kernel headers for 3.8.13-bone70 on armhf

Then

$] apt-get install linux-headers-$(uname -r)
 Reading package lists... Done
 Building dependency tree       
 Reading state information... Done
 The following NEW packages will be installed:
   linux-headers-3.8.13-bone70
 0 upgraded, 1 newly installed, 0 to remove and 161 not upgraded.
 Need to get 8430 kB of archives.
...

will install these development files on your BBB.

If you look into /usr/src/linux-headers-3.8.13-bone70 you'll find a Makefile. This is the main makefile for the kernel development. This is the one being use by the scull examples.

Add a user

If you havn't added yourself as a user do that now. You can develop and compile the kernel modules as an ordinary user. And this is the preferable way.

$] useradd -d /home/<desired login name> -m -s `which bash` <desired login name>
$] passwd <desired login name>
 #enter your desired password twice
$] groupadd moduledev
$] usermod <desired login name> -G moduledev
$] mkdir -p /home/emb
$] chown <desired login name>:moduledev /home/emb

The first line adds the new user, the second forces a password on that account and the third adds a development group to use for development and finaly the login name just created is added to the moduledev group. Then we create a work directory to develop modules in - /home/emb and assigns it to the moduledev group and <desired login name> as the owner.

Login as this new user now.

The Linux Device Driver book examples

Along with the book "Linux Device Drivers" came a bunch of example drivers - the scull drivers.

Preparation

So get a copy of the drivers to /home/emb - do use you own login name now!

$] cd /home/emb
$] git clone https://github.com/martinezjavier/ldd3.git

thanks to Javier Martinez Canillas, most of the drivers has been fixed so that they will compile in relatively resent kernels.

Change the group ownership if you expect more persons to use this directory, for instance when working on a shared server.

$]chgrp -R moduledev examples

Enter the Linux Device Driver directory

$] cd ldd3

Working with the example drivers

First create a little helper script in the examples directory. The script will setup an environment variable KERNELDIR, needed to locate the kernel development directory

#!/bin/bash
#
# Preparation for the compilation of the scull example code
#
KERNELDIR=/usr/src/linux-headers-`uname -r`/
export KERNELDIR

Make it executable

$] chmod +x settings.sh

Every time you enter this directory execute this

$] . settings.sh

NOTE the . is the same as writing source

If you forget to set this environment variable it won't work as expected.

Compile the driver. (You may first have to cd into the specific drivers directory. In the example below it is the scull driver.)

$] cd scull
$] make modules

If no errors you have SUCCESS.

Now try to insert the module into the kernel and remove it again. Do as the book instruct you to do.