Linux中的file_operation结构

Character Device Drivers

The file_operations Structure

    The file_operations structure is defined in linux/fs.h, andholds pointers to functions defined by the driver that perform various operations on the device. Each field of thestructure corresponds to the address of some function defined by the driver to handle a requested operation.

    For example, every character driver needs to define a function that reads from the device. Thefile_operations structure holds the address of the module's function that performs that operation. Here iswhat the definition looks like for kernel 2.4.2:

 1     struct file_operations {  
 2           struct module *owner;  
 3           loff_t (*llseek) (struct file *, loff_t, int);  
 4           ssize_t (*read) (struct file *, char *, size_t, loff_t *);  
 5           ssize_t (*write) (struct file *, const char *, size_t, loff_t *);  
 6           int (*readdir) (struct file *, void *, filldir_t);  
 7           unsigned int (*poll) (struct file *, struct poll_table_struct *);  
 8           int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);  
 9           int (*mmap) (struct file *, struct vm_area_struct *);  
10           int (*open) (struct inode *, struct file *);  
11           int (*flush) (struct file *);  
12           int (*release) (struct inode *, struct file *);  
13           int (*fsync) (struct file *, struct dentry *, int datasync);  
14           int (*fasync) (int, struct file *, int);  
15           int (*lock) (struct file *, int, struct file_lock *);  
16          ssize_t (*readv) (struct file *, const struct iovec *, unsigned long,  
17              loff_t *);  
18          ssize_t (*writev) (struct file *, const struct iovec *, unsigned long,  
19              loff_t *);  
20        };  

 

    Some operations are not implemented by a driver. For example, a driver that handles a video card won't need to readfrom a directory structure. The corresponding entries in thefile_operations structure should be set toNULL.

    There is a gcc extension that makes assigning to this structure more convenient. You'll see it in modern drivers,and may catch you by surprise. This is what the new way of assigning to the structure looks like:

1     struct file_operations fops = {  
2           read: device_read,  
3           write: device_write,  
4           open: device_open,  
5           release: device_release  
6        };  
7           

    However, there's also a C99 way of assigning to elements of a structure, and this is definitely preferred over usingthe GNU extension. The version of gcc I'm currently using,2.95, supports the new C99 syntax. Youshould use this syntax in case someone wants to port your driver. It will help with compatibility:

1     struct file_operations fops = {  
2            .read = device_read,  
3            .write = device_write,  
4            .open = device_open,  
5            .release = device_release  
6         };  
7               

    The meaning is clear, and you should be aware that any member of the structure which you don't explicitly assignwill be initialized toNULL by gcc.

    A pointer to a struct file_operations is commonly namedfops.

The file structure

    Each device is represented in the kernel by a file structure, which is defined inlinux/fs.h. Be aware that afile is a kernel level structure and never appears in auser space program. It's not the same thing as aFILE, which is defined by glibc and would never appear in akernel space function. Also, its name is a bit misleading; it represents an abstract open `file', not a file on a disk,which is represented by a structure namedinode.

    A pointer to a struct file is commonly named filp. You'll also see itrefered to as struct file file. Resist the temptation.

    Go ahead and look at the definition of file. Most of the entries you see, likestruct dentry aren't used by device drivers, and you can ignore them. This is because drivers don'tfillfile directly; they only use structures contained in file which are createdelsewhere.

Registering A Device

    As discussed earlier, char devices are accessed through device files, usually located in/dev[1]. The major number tells you which driver handles which device file. The minor numberis used only by the driver itself to differentiate which device it's operating on, just in case the driver handles morethan one device.

    Adding a driver to your system means registering it with the kernel. This is synonymous with assigning it a majornumber during the module's initialization. You do this by using theregister_chrdev function,defined bylinux/fs.h.

  1. int register_chrdev(unsigned int major, const char *name,  
  2.        struct file_operations *fops);   

    where unsigned int major is the major number you want to request,const char*name is the name of the device as it'll appear in/proc/devices andstructfile_operations *fops is a pointer to thefile_operations table for your driver. A negativereturn value means the registertration failed. Note that we didn't pass the minor number toregister_chrdev. That's because the kernel doesn't care about the minor number; only our driver usesit.

    Now the question is, how do you get a major number without hijacking one that's already in use? The easiest waywould be to look throughDocumentation/devices.txt and pick an unused one. That's a bad way of doingthings because you'll never be sure if the number you picked will be assigned later. The answer is that you can ask thekernel to assign you a dynamic major number.

    If you pass a major number of 0 to register_chrdev, the return value will be the dynamicallyallocated major number.The downside is that you can't make a device file in advance, since you don't know what the majornumber will be. 它这里说了三种方法,最浅显的做法就是动态分配后,用printk打印出新分配的主设备号,用dmesg命令就可以产看到。然后创建设备文件的时候使用该主设备号。There are a couple of ways to do this. First, the driver itself can print the newly assigned number andwe can make the device file by hand. Second, the newly registered device will have an entry in/proc/devices, and we can either make the device file by hand or write a shell script to read thefile in and make the device file. The third method is we can have our driver make the the device file using themknod system call after a successful registration and rm during the call tocleanup_module.

Unregistering A Device

    We can't allow the kernel module to be rmmod'ed whenever root feels like it. If thedevice file is opened by a process and then we remove the kernel module, using the file would cause a call to the memorylocation where the appropriate function (read/write) used to be. If we're lucky, no other code was loaded there, andwe'll get an ugly error message. If we're unlucky, another kernel module was loaded into the same location, which means ajump into the middle of another function within the kernel. The results of this would be impossible to predict, but theycan't be very positive.

    Normally, when you don't want to allow something, you return an error code (a negative number) from the functionwhich is supposed to do it. Withcleanup_module that's impossible because it's a void function.However, there's a counter which keeps track of how many processes are using your module. You can see what it's value isby looking at the 3rd field of/proc/modules. If this number isn't zero,rmmodwill fail. Note that you don't have to check the counter from withincleanup_module because thecheck will be performed for you by the system callsys_delete_module, defined inlinux/module.c. You shouldn't use this counter directly, but there are macros defined inlinux/modules.h which let you increase, decrease and display this counter:

  • MOD_INC_USE_COUNT: Increment the use count.

  • MOD_DEC_USE_COUNT: Decrement the use count.

  • MOD_IN_USE: Display the use count.

    It's important to keep the counter accurate; if you ever do lose track of the correct usage count, you'll never beable to unload the module; it's now reboot time, boys and girls. This is bound to happen to you sooner or later during amodule's development.

chardev.c

    The next code sample creates a char driver named chardev. You cancat itsdevice file (oropen the file with a program) and the driver will put the number of times the devicefile has been read from into the file. We don't support writing to the file (likeecho "hi" >/dev/hello), but catch these attempts and tell the user that the operation isn't supported. Don't worry if youdon't see what we do with the data we read into the buffer; we don't do much with it. We simply read in the data andprint a message acknowledging that we received it.

Example 4-1. chardev.c

  1 /*  chardev.c: Creates a read-only char device that says how many times 
  2  *  you've read from the dev file 
  3  * 
  4  *  Copyright (C) 2001 by Peter Jay Salzman 
  5  * 
  6  *  08/02/2006 - Updated by Rodrigo Rubira Branco <[email protected]> 
  7  */  
  8   
  9 /* Kernel Programming */  
 10 #define MODULE  
 11 #define LINUX  
 12 #define __KERNEL__  
 13   
 14 #if defined(CONFIG_MODVERSIONS) && ! defined(MODVERSIONS)  
 15    #include <linux/modversions.h>  
 16    #define MODVERSIONS  
 17 #endif  
 18 #include <linux/kernel.h>  
 19 #include <linux/module.h>  
 20 #include <linux/fs.h>  
 21 #include <asm/uaccess.h>  /* for put_user */  
 22 #include <asm/errno.h>  
 23   
 24 /*  Prototypes - this would normally go in a .h file */  
 25 int init_module(void);  
 26 void cleanup_module(void);  
 27 static int device_open(struct inode *, struct file *);  
 28 static int device_release(struct inode *, struct file *);  
 29 static ssize_t device_read(struct file *, char *, size_t, loff_t *);  
 30 static ssize_t device_write(struct file *, const char *, size_t, loff_t *);  
 31   
 32 #define SUCCESS 0  
 33 #define DEVICE_NAME "chardev" /* Dev name as it appears in /proc/devices   */  
 34 #define BUF_LEN 80            /* Max length of the message from the device */  
 35   
 36   
 37 /* Global variables are declared as static, so are global within the file. */  
 38   
 39 static int Major;            /* Major number assigned to our device driver */  
 40 static int Device_Open = 0;  /* Is device open?  Used to prevent multiple 
 41                                         access to the device */  
 42 static char msg[BUF_LEN];    /* The msg the device will give when asked    */  
 43 static char *msg_Ptr;  
 44   
 45 static struct file_operations fops = {  
 46   .read = device_read,  
 47   .write = device_write,  
 48   .open = device_open,  
 49   .release = device_release  
 50 };  
 51   
 52   
 53 /* Functions */  
 54   
 55 int init_module(void)  
 56 {  
 57    Major = register_chrdev(0, DEVICE_NAME, &fops);  
 58   
 59    if (Major < 0) {  
 60      printk ("Registering the character device failed with %d\n", Major);  
 61      return Major;  
 62    }  
 63   
 64    printk("<1>I was assigned major number %d.  To talk to\n", Major);  
 65    printk("<1>the driver, create a dev file with\n");  
 66    printk("'mknod /dev/hello c %d 0'.\n", Major);  
 67    printk("<1>Try various minor numbers.  Try to cat and echo to\n");  
 68    printk("the device file.\n");  
 69    printk("<1>Remove the device file and module when done.\n");  
 70   
 71    return 0;  
 72 }  
 73   
 74   
 75 void cleanup_module(void)  
 76 {  
 77    /* Unregister the device */  
 78    int ret = unregister_chrdev(Major, DEVICE_NAME);  
 79    if (ret < 0) printk("Error in unregister_chrdev: %d\n", ret);  
 80 }  
 81   
 82   
 83 /* Methods */  
 84   
 85 /* Called when a process tries to open the device file, like 
 86  * "cat /dev/mycharfile" 
 87  */  
 88 static int device_open(struct inode *inode, struct file *file)  
 89 {  
 90    static int counter = 0;  
 91    if (Device_Open) return -EBUSY;  
 92   
 93    Device_Open++;  
 94    sprintf(msg,"I already told you %d times Hello world!\n", counter++);  
 95    msg_Ptr = msg;  
 96    MOD_INC_USE_COUNT;  
 97   
 98    return SUCCESS;  
 99 }  
100   
101   
102 /* Called when a process closes the device file */  
103 static int device_release(struct inode *inode, struct file *file)  
104 {  
105    Device_Open --;     /* We're now ready for our next caller */  
106   
107    /* Decrement the usage count, or else once you opened the file, you'll 
108                     never get get rid of the module. */  
109    MOD_DEC_USE_COUNT;  
110   
111    return 0;  
112 }  
113   
114   
115 /* Called when a process, which already opened the dev file, attempts to 
116    read from it. 
117 */  
118 static ssize_t device_read(struct file *filp,  
119    char *buffer,    /* The buffer to fill with data */  
120    size_t length,   /* The length of the buffer     */  
121    loff_t *offset)  /* Our offset in the file       */  
122 {  
123    /* Number of bytes actually written to the buffer */  
124    int bytes_read = 0;  
125   
126    /* If we're at the end of the message, return 0 signifying end of file */  
127    if (*msg_Ptr == 0) return 0;  
128   
129    /* Actually put the data into the buffer */  
130    while (length && *msg_Ptr)  {  
131   
132         /* The buffer is in the user data segment, not the kernel segment; 
133          * assignment won't work.  We have to use put_user which copies data from 
134          * the kernel data segment to the user data segment. */  
135          put_user(*(msg_Ptr++), buffer++);  
136   
137          length--;  
138          bytes_read++;  
139    }  
140   
141    /* Most read functions return the number of bytes put into the buffer */  
142    return bytes_read;  
143 }  
144   
145   
146 /*  Called when a process writes to dev file: echo "hi" > /dev/hello */  
147 static ssize_t device_write(struct file *filp,  
148    const char *buff,  
149    size_t len,  
150    loff_t *off)  
151 {  
152    printk ("<1>Sorry, this operation isn't supported.\n");  
153    return -EINVAL;  
154 }  
155   
156 MODULE_LICENSE("GPL");  

 

你可能感兴趣的:(linux)