Skip to content

About

Learning Linux Driver Development using a Raspberry Pi 5

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

8 Commits

Folders and files

Repository files navigation

The Linux Kernel

  • Kernel: hardware abstraction layer (System call API)
  • Task of kernel - process and resources management, scheduler, hardware access through driver
  • monolithic kernel with loadable modules

Kernel Modules

Improved Kernel Module

  • __init and __exit
aquila@raspberrypi:~/Documents/linux-driver-dev/02_better_hello $ modinfo hello.ko
filename:       /home/aquila/Documents/linux-driver-dev/02_better_hello/hello.ko
description:    A simple hello world Linux Kernel module
author:         aquila <aquila@mail.me>
license:        GPL
srcversion:     7A96C1FFCD5205411B62110
depends:        
name:           hello

aquila@raspberrypi:~/Documents/linux-driver-dev/02_better_hello $ lsmod | grep hello
hello                  49152  0
aquila@raspberrypi:~/Documents/linux-driver-dev/02_better_hello $ modinfo industrialio
filename:       /lib/modules/6.12.34+rpt-rpi-2712/kernel/drivers/iio/industrialio.ko.xz
license:        GPL
description:    Industrial I/O core
author:         Jonathan Cameron <jic23@kernel.org>
import_ns:      DMA_BUF
srcversion:     7E3195094E60FB9B5EB0AF4
depends:        
intree:         Y # intree
name:           industrialio
vermagic:       6.12.34+rpt-rpi-2712 SMP preempt mod_unload modversions aarch64

Basic GPIO Control

aquila@raspberrypi:/sys/class/gpio $ cat gpiochip569/label
pinctrl-rp1

Character and Block Devices

aquila@raspberrypi:~/Documents/linux-driver-dev/03_gpio_control $ ls -l /dev/mmcblk0 /dev/gpiochip0
crw-rw----+ 1 root gpio 254, 0 Sep  2 18:47 /dev/gpiochip0
brw-rw----  1 root disk 179, 0 Sep  2 18:47 /dev/mmcblk0

major device no. , minor device no.

c - character device file b - block device file

The system call mknod() creates a filesystem node (file, device special file, or named pipe) named pathname, with attributes specified by mode and dev.

Creating a Character Device

$ sudo insmod hello_cdev.ko
$ sudo mknod /dev/hello0 c 507 0
$ sudo mknod /dev/hello1 c 507 1
$ cat /dev/hello0
$ cat /dev/hello1

dmesg:
[ 5630.477112] hello_cdev - major device no.=507
[ 5674.524053] hello_cdev - read operation called
[ 5803.370351] hello_cdev - read operation called
  • Using register_chrdev to register the character device
  • Specify file_operations for the character device

Kernel Log Levels

dmesg:

-l, --level <list>
   Restrict output to the given (comma-separated) list of levels.
   For example:

   dmesg --level=err,warn

   will print error and warning messages only. For all supported
   levels see the --help output.

   Appending a plus + to a level name also includes all higher
   levels. For example:

   dmesg --level=err+

   will print levels err, crit, alert and emerg.

   Prepending it will include all lower levels.

-T, --ctime
   Print human-readable timestamps.

   Be aware that the timestamp could be inaccurate.

-W, --follow-new
   Wait and print only new messages.

Open and Release Functions

file struct: https://elixir.bootlin.com/linux/v6.17.1/source/include/linux/fs.h#L1099

$ sudo insmod hello_cdev.ko
# after check dmesg for major device number
$ sudo mknod /dev/hello0 c 509 0
$ sudo mknod /dev/hello10 c 509 10
$ gcc test.c -o test
$ sudo ./test /dev/hello0
$ sudo ./test /dev/hello10
$ sudo dmesg -W
[ 4008.348533] hello_cdev - goodbye kernel
[ 4011.550923] hello_cdev - major device no.=509
[ 4130.614831] hello_cdev  - Major: 509, Minor 0
[ 4130.614839] hello_cdev - filp->f_pos: 0
[ 4130.614842] hello_cdev - filp->f_mode: 0x1d
[ 4130.614844] hello_cdev - filp->f_flags: 0x20000
[ 4130.614848] hello_cdev - File is closed
[ 4130.614851] hello_cdev  - Major: 509, Minor 0
[ 4130.614853] hello_cdev - filp->f_pos: 0
[ 4130.614855] hello_cdev - filp->f_mode: 0x1f
[ 4130.614857] hello_cdev - filp->f_flags: 0x121002
[ 4130.614859] hello_cdev - File is closed
[ 4130.614862] hello_cdev  - Major: 509, Minor 0
[ 4130.614864] hello_cdev - filp->f_pos: 0
[ 4130.614865] hello_cdev - filp->f_mode: 0x1e
[ 4130.614867] hello_cdev - filp->f_flags: 0x20801
[ 4130.614869] hello_cdev - File is closed
[ 4139.348171] hello_cdev  - Major: 509, Minor 10
[ 4139.348179] hello_cdev - filp->f_pos: 0
[ 4139.348181] hello_cdev - filp->f_mode: 0x1d
[ 4139.348183] hello_cdev - filp->f_flags: 0x20000
[ 4139.348187] hello_cdev - File is closed
[ 4139.348190] hello_cdev  - Major: 509, Minor 10
[ 4139.348192] hello_cdev - filp->f_pos: 0
[ 4139.348193] hello_cdev - filp->f_mode: 0x1f
[ 4139.348195] hello_cdev - filp->f_flags: 0x121002
[ 4139.348197] hello_cdev - File is closed
[ 4139.348200] hello_cdev  - Major: 509, Minor 10
[ 4139.348202] hello_cdev - filp->f_pos: 0
[ 4139.348203] hello_cdev - filp->f_mode: 0x1e
[ 4139.348205] hello_cdev - filp->f_flags: 0x20801
[ 4139.348207] hello_cdev - File

About

Learning Linux Driver Development using a Raspberry Pi 5

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages