This is an old revision of the document!
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.
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.
Update the Makefile
from drivers/nss/lab01/Makefile
to compile the hello.c
and obtain the module.
Enable CONFIG_NSS_DRIVERS_LAB01_EX01
.
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
Starting from the skeleton found at drivers/nss/lab01/ex4/hello_name.c
, add a parameter to Hello <name>
Linux kernel module and test it (see 3. above).
Enable CONFIG_NSS_DRIVERS_LAB01_EX04
.
Starting from the skeleton found at drivers/nss/lab01/ex5/imx8mq_chardev.c
, add an implementation for a character device driver:
TODO1
.TODO2
and TODO3
.
Enable CONFIG_NSS_DRIVERS_LAB01_EX05
.
TODO4
and TODO5
.TODO6
TODO7