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

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.

Character device driver module

5. Add parameters to your module

Starting from the skeleton found at drivers/nss/lab01/ex5/imx8mq_chardev.c, add an implementation for a character device driver:

  • a) Add file operations callbacks. Implement TODO1.
  • b) Implement the registration and deregistration of the device with the name imx8mq_chardev, respectively in the init and exit module functions. Implement TODO2 and TODO3.

Enable CONFIG_NSS_DRIVERS_LAB01_EX05.

  • c) Load the module into the kernel. Check character devices in /proc/devices - “cat /proc/devices | less”. Unload the kernel module.
  • d)Implement the open and release functions in the driver. Display a message in the open and release functions. Implement TODO4 and TODO5.
  • e) Implement the read function in the driver. Use the copy_to_user() function to copy information from kernel space to user space. Implement TODO6
  • f) Implement the write function in the driver. Implement TODO7
lkd/laboratoare/02.1720459813.txt.gz · Last modified: 2024/07/08 20:30 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