Viii. Specifying a callback function
The content of this section is critical. Regardless of how complex or "cool" the Linux drivers are, it is important to allow user-space applications to interact with kernel-space drivers. The most common way to interact is to read and write device files. The File_operations.read and File_operations.write member variables allow you to specify the callback function pointers to be invoked for the read-write device file, respectively.
In this section, you will add two functions for word_count.c: Word_count_read and Word_count_write. These two functions handle the actions of reading data from device files and writing data to device files, respectively. This section of the example first does not consider word_count to implement the statistical word number function, first uses the Word_count_read and the Word_count_write function to do a reading and writing device file data experiment, lets the reader know how to interact with the device file data. The word_count.c file written in this section is a branch where readers can find word_count.c files in the Word_count/read_write directory. You can use this file to overwrite the example in this section with a file with the same name under the Word_count directory.
The function of this example is to write the data to the device file/dev/wordcount, which can be read from the/dev/wordcount device file (only once). Now let's take a look at the complete code for this example.
#include <linux/module.h> #include <linux/init.h> #include <linux/kernel.h> #include <linux /fs.h> #include <linux/miscdevice.h> #include <asm/uaccess.h> #define DEVICE_NAME "Wordcou NT "///define device filename static unsigned char mem[10000]; Save data written to device file static char Read_flag = ' Y '; Y: Read data from Device file N: Data not read from device file static int written_count = 0; Bytes written to device file///file when reading data from device file: Pointing to Device files, buf: Saving readable data count: bytes readable ppos: Read data offset static s size_t word_count_read (struct file *file, char __user *buf, size_t count, loff_t *ppos) {//If the data in the device file is not yet read , you can read if (Read_flag = = ' n ') {//copy kernel-space data to user space, buf data is read from the device file Copy_to_use
R (buf, (void*) mem, written_count);
Output the number of bytes read PRINTK ("Read count:%d", (int) written_count) to the log; Set data Read status Read_flag = ' Y ';
return written_count;
//The data has been read from the device file and cannot be read again else {return 0; Call this function//file when writing data to device file: Point to Device file, buf: Save written Data count: bytes written to data PPOs: offset to write data static ssize_t Word_count_ Write (struct file *file, const char __user *buf, size_t count, loff_t *ppos) {//Copy user-space data to kernel space, the data in Mem is to
The device file writes the data copy_from_user (MEM, buf, Count);
Sets the unread state of the data Read_flag = ' n ';
The number of bytes to save write data Written_count = count;
Prints the number of bytes written to the log printk ("Written count:%d", (int) count);
return count;
}//Describe the callback function pointer corresponding to the event triggered by the device file//need to set the read and write member variables to invoke the function static struct file_operations to process the file action of the read and write device Dev_fops =
{. Owner = This_module,. Read = Word_count_read,. Write = Word_count_write}; Describes information for device files static struct Miscdevice misc = {. minor = Misc_dynamic_minor,. Name = Device_name,. FoPs = &dev_f
OPS}; Initialize Linux driver static intWord_count_init (void) {int ret;
Establish the equipment file ret = Misc_register (&MISC);
Output log information printk ("word_count_init_success\n");
return ret;
//uninstall Linux driver static void Word_count_exit (void) {///delete device file Misc_deregister (&MISC);
Output log information printk ("word_init_exit_success\n");
//Register initialization Linux-driven function module_init (word_count_init);
Register uninstall Linux driven function module_exit (word_count_exit);
Module_author ("lining");
Module_description ("Statistics of Word count.");
Module_alias ("Word Count module."); Module_license ("GPL");