1. |
Preface:
A. |
Before proceeding to the following work, please read my article "compile a kernel module that can be loaded under the Linux 2.6 kernel" to configure a Linux environment that can be loaded with the kernel module |
B. |
Download the source code for the Linux Device Driver (Third edition). You can download it here. If you cannot download it, leave a message or send me an email. |
C. |
The working environment in this article is Fedora Core 5. Use "uname-R" to check whether the kernel version is 2.6.15-1.2054 _ fc5. |
D. |
This article focuses on the Linux Device Driver (version 3) Chapter 3: character Device Driver |
E. |
Some of the following operations require the root permission, so try to use the root user to execute the command. |
|
2. |
Decompress the source code package and enter the scull folder. The files include:
Access. c |
|
Main. c |
The main program, Scull device initialization, uninstallation, open, write, and Other implementations are all in this area. |
Makefile |
|
Pipe. c |
Chapter 6 "advanced character driver operations" will be used to explain blocking-type backup. |
Scull. h |
Header file |
Scull. init |
|
Scull_load |
Script for loading the scull Module |
Scull_unload |
Uninstall the scull module script |
|
3. |
Run the make command in the scull directory to compile and run scull. ko file, and then run the script scull_load. If no output is returned, the scull device will automatically load it to the kernel, in this case, you can view the/proc/devices file to find the newly loaded scull module and the master device number assigned to it by the inner core. Many characters starting with scull are added to/dev. |
4. |
Try scull device: Run the following command:
# Ls-L>/dev/scull # No output is returned. # Cat/dev/scull # Run the cat command to read the scull device, and the output information of the above command will be read. Total 900 -RW-1 baobaowu 10845 2005-02-01 access. c -RW-r -- 1 baobaowu 74580 06-15 access. o -RW-1 baobaowu 16631 06-15 main. c -RW-r -- 1 baobaowu 74512 06-15 main. o -RW-r -- 1 baobaowu 752 makefile -RW-1 baobaowu 11138 2005-02-01 pipe. c -RW-r -- 1 baobaowu 71576 06-15 pipe. o -RW-r -- 1 baobaowu 5153 2005-02-01 scull. h -Rwxr-XR-x 1 baobaowu 3309 2005-02-01 scull. init -RW-r -- 1 baobaowu 248556 06-15 scull. Ko -Rwxr-XR-x 1 baobaowu 1708 06-15 scull_load -RW-1 baobaowu 1852 06-15 scull. Mod. c -RW-r -- 1 baobaowu 33696 06-15 scull. Mod. o -RW-r -- 1 baobaowu 216043 06-15 scull. o -Rwxr-XR-x 1 baobaowu 335 2005-02-01 scull_unload |
Sure enough, the scull device, as described in the book, only exists in a buffer zone in the memory. |
5. |
Run scull_unload again. No output is returned, but the scull module has been deleted from the kernel, and no scull device exists in the/proc/devices file, /dev/also has no character device that starts with scull. |
6. |
Some debugging information is added in scull as follows: Open the main. c file and add a sentence to the header of the scull_init_module () function:
Printk (kern_alert "Debug by baobaowu: scull_init_module ()/n "); |
Add the following statement to the header of the scull_read () function:
Printk (kern_alert "Debug by baobaowu: scull_read ()/n "); |
Add the following statement to the header of the scull_write () function:
Printk (kern_alert "Debug by baobaowu: scull_write ()/n "); |
Save and run make to compile. |
7. |
Repeat Step 1 to load scull into the kernel. At this time, the kernel should call the scull_init_module () function in Main. c. is it called? Let's take a look at the/var/log/messages file. The debugging information is output at the end of the file:
Debug by baobaowu: scull_init_module () |
How can I call read and write? Next we will introduce it below. |
8. |
As you can imagine, writing data to a device calls the scull_write () function. We execute the following command to write data to the/dev/scull device using the output redirection:
After executing the command, check the/var/log/messages file. The debugging information is displayed at the end of the file:
Debug by baobaowu: scull_write () |
|
9. |
The scull_read () function should be called to read data from the device. We use the DD command to read the scull device:
# Dd If =/dev/scull of = temp # read data from/dev/scull and save it to the temp file in the current directory |
After executing the command, check the/var/log/messages file. The debugging information is displayed at the end of the file:
Debug by baobaowu: scull_read () |
|
10. |
The scull usage described in this article is helpful for reading the character device driver in Chapter 3 after successful debugging. When you encounter something you don't understand or are not sure about, printk will be fine. ^_^ |
|
|
|
|