This is an old revision of the document!


Basics of writing a Linux kernel module

Presentation

Overview

A loadable kernel module (LKM) is a mechanism for adding code to, or removing code from, the Linux kernel at runtime. They are ideal for device drivers, enabling the kernel to communicate with the hardware without it having to know how the hardware works. The alternative to LKMs would be to build the code for each and every driver into the Linux kernel. A kernel module is not an application — for a start there is no main() function.

A character device typically transfers data to and from a user application — they behave like pipes or serial ports, instantly reading or writing the byte data in a character-by-character stream. They provide the framework for many typical drivers, such as those that are required for interfacing to serial communications, video capture, and audio devices. The main alternative to a character device is a block device. Block devices behave in a similar fashion to regular files, allowing a buffered array of cached data to be viewed or manipulated with operations such as reads, writes, and seeks. Both device types can be accessed through device files that are attached to the file system tree.

Device drivers have an associated major and minor number. The major number is used by the kernel to identify the correct device driver when the device is accessed. The role of the minor number is device dependent, and is handled internally within the driver. You can see the major/minor number pair for each device if you perform a listing in the /dev directory.

Practical lab

Hello World Linux kernel Module

1. Explore hello.c file

Starting from the skeleton found at drivers/nss/lab01/ex1/hello.c , write a Linux kernel module. This must print a Hello world and Goodbye message in its initialization and cleanup function, respectively. Also, add some module information.

2. Compile hello.c kernel module

Update the Makefile from drivers/nss/lab01/Makefile to compile the hello.c and obtain the module. Enable CONFIG_NSS_DRIVERS_LAB01_EX01.

3. Test the kernel module on i.MX board

Revisit previous session and remember how to:

Now on the hardware board test your module:

# insert the module
$ insmod 
$ check if the modules is loaded
$ lsmod
$ show information about the module
# remove the module
$ check the log
4. Add parameters to your module

Character device driver module

lkd/laboratoare/02.1720459291.txt.gz · Last modified: 2024/07/08 20:21 by daniel.baluta
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0