First Linux driver: Count the numbers of words
What a Linux driver is.
Programmers who have not been exposed to driver development may feel that Linux drivers are mysterious, but this is completely a misunderstanding. The Linux system maps each driver into a file, called a device file or a drive file, and is stored in/dev. This allows the exchange of data with the Linux driver to be equivalent to exchanging data with the device file. One of the concepts that is similar to an event is to write a callback function, otherwise data that interacts with the device file will not be processed.
Steps to write a Linux driver
- Build Linux driver skeleton (load and unload Linux drivers)
- Registering and unregistering device files
- Specify driver-related information
- Specifying a callback function
- Writing business logic
- Writing makefile Files
- Compiling Linux Drivers
- Install and uninstall Linux drivers.
Count the number of words:
1. Before writing the Linux driver: set up a directory to store Linux drivers and write makefile files. 2. Write the skeleton of the Linux driver: The printf function runs in user space, and the PRINTK function runs in kernel space. Install Linux drivers, see if Word_count installs successfully, uninstall Linux drivers, and view log information. 3. Specify driver-related information: #modinfo Word_count.ko to view word_count.ko information. 4. Registering and unregistering device files:
# define Device_name "WordCount" to define device files
Static struct Miscdevice misc = {}; Information describing the device file
ret = Misc_register (&MISC); Create a device file
Misc_deregister (&MISC); removing device files
5. Specify the callback function: the Word_count_read and Word_count_write functions separately handle reading data from the device file and writing data to the device file.
6. The algorithm that implements the statistic word number.
7. Compile, install, and uninstall Linux drivers.
Reading notes the sixth chapter