📜 ⬆️ ⬇️

How to write your first Linux device driver. Part 3

Good evening, habrochityateli!

In previous articles ( one , two ) we defined the concept of a character device and wrote the simplest example of a character driver. The last part is dedicated to testing its performance. On Habré there are already examples of how to test a driver, for example: tyk .

I will try to address this issue in a little more detail, I hope you enjoy it.
')


Please, if you have thoughts that you can add / correct, then I am waiting for your comments or letters in PM, thank you.

Kernel module assembly


In order to build our module, we need to write a small Makefile. You can read what a Makefile is here: one , two , three . I also wrote once an example of a Makefile for students, you can see it here: click .

In short, the Makefile is a set of instructions for the make program, and make is a utility that automates the process of converting files from one form to another. After a quick acquaintance with the Makefile, you can look at the code:

Makefile:

obj-m += fake.o all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean 

Let's take a look at the command:

 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules 

It starts with changing the directory (in my case to: /lib/modules/4.4.0-93-generic/build), in this directory are the sources of the kernel, as well as a Makefile that the make utility reads. The variable M, allows you to specify where our project is located and go back along the path indicated in it. That is, in fact, we use another Makefile to build our module.

We write the make command line and get the output:

 make -C /lib/modules/4.4.0-93-generic/build M=/home/alexey/Desktop/drivers/character modules make[1]: Entering directory '/usr/src/linux-headers-4.4.0-93-generic' CC [M] /home/alexey/Desktop/drivers/character/fake.o Building modules, stage 2. MODPOST 1 modules CC /home/alexey/Desktop/drivers/character/fake.mod.o LD [M] /home/alexey/Desktop/drivers/character/fake.ko make[1]: Leaving directory '/usr/src/linux-headers-4.4.0-93-generic' 

After the build, the output is a compiled module fake.ko. We will load it with the insmod command.

Next, you need to perform the following sequence of actions:

  1. Load module into kernel
    Run: sudo insmod fake.ko
  2. Check, with the help of the dmesg command, the expected output of the module
    Example: scull: register device major = 243 minor = 0
  3. Create a file of our device in the file system
    Example: sudo mknod /dev/scull c 243 0
  4. Change permissions
    Example: sudo chmod 777 /dev/scull

It remains the case for small, write a small program that allows you to simply read / write data.

 #include <fcntl.h> #include <stdio.h> #include <unistd.h> #define DEVICE "/dev/scull" #define BUFF_SIZE 100 int main() { int fd; char ch, write_buf[BUFF_SIZE], read_buf[BUFF_SIZE]; fd = open(DEVICE, O_RDWR); if (!fd) return -1; printf("r - read, w - write\n"); scanf("%c", &ch); switch (ch) { case 'w': printf("enter data: "); scanf(" %[^\n]", write_buf); write(fd, write_buf, sizeof(write_buf)); break; case 'r': read(fd, read_buf, sizeof(read_buf)); printf("scull: %s\n", read_buf); break; } return 0; } 

Health Check:

  1. Compile: gcc test.c -o test
  2. Call the executable file: ./test
  3. Write to the device: Hello world!
  4. Call the executable again: ./test
  5. Read the data: scull: Hello world!

This completes the testing of a simple symbolic driver, now you can come up with a new functionality, implement and test yourself by yourself :)

At the end of the previous article I conducted a survey, I want to say thanks to everyone who took part in it! Soon I will start writing about the process of porting device drivers from one kernel version to another.

PS If you find inaccuracies in the article, I will wait for your messages, thank you!

Source: https://habr.com/ru/post/342668/


All Articles