The method structure of the device file registration in the Linxu driver is also the body that provides the operation interface to the user layer, my version is 3.1.10
Writing is not easy, reprint need to indicate the source: http://blog.csdn.net/jscese/article/details/43408625
The prototype is defined in the kernel source/include/linux/fs.h:
struct File_operations {struct module *owner; The first file_operations member is not an operation at all; It is a pointer to the module that owns the structure. This member is used to block the module from being unloaded when its operation is still in use. Almost all the time, it was simply initialized to This_module, a macro defined in <linux/module.h>. loff_t (*llseek) (struct file *, loff_t, int); The//llseek method is used to change the current read/write position in the file, and the new location is the (positive) return value. The loff_t parameter is a "long offset" and is at least 64 bits wide even on a 32-bit platform. The error is indicated by a negative return value. If the function pointer is NULL, the seek call modifies the position counter in the file structure in a potentially unpredictable way (described in the "File Structure" section). ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);//used to fetch data from the device. A null pointer at this location causes the read system call to fail with-einval ("Invalid argument"). A non-negative return value represents the number of bytes successfully read (The return value is a "signed size" type, which is often the integer type that is local to the target platform). ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);//Send data to the device. If NULL,-einval returns to the program that called the Write system call. If non-negative, the return value represents the number of bytes successfully written. ssize_t (*aio_read) (struct KIOCB *, const struct IOVEC *, unsigned long, loff_t);//Initializes an asynchronous read-a read operation that may not end before the function returns. If this method is NULL, all operations are performed by read instead (synchronously). ssize_t (*aio_write) (struct KIOCB *, const struct IOVEC *, unsigned long, loff_t);//initialization of an asynchronous write on the device. Int (*readdir) (struct file *, void *, filldir_t);//For device files This member should be NULL; It is used to read directories and is only useful for file systems. unsigned int (*poll) (struct file *, struct poll_table_struct *); The//poll method is the back end of 3 system calls: Poll, Epoll, and select, all used as queries against one or more Whether the read or write of the file descriptor is blocked. The poll method should return a bitmask indicating whether non-blocking reads or writes are possible, and, possibly, providing kernel information to make the calling process sleep until I/O becomes possible. If a driver's poll method is NULL, the device is assumed to be non-blocking readable and writable. Long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long); Long (*compat_ioctl) (struct file *, unsigned int, unsigned long);//2.6.35 version only these two ioctl, the Inode parameter is missing, the function is not changed to indicate that the system call provides a device-specific The method for ordering the command int (*mmap) (struct file *, struct vm_area_struct *);//mmap is used to request that the device memory be mapped to the address space of the process. If this method is NULL, the mmap system call returns-ENODEV. Int (*open) (struct inode *, struct file *);//Although this is often the first operation on a device file, the driver is not required to declare a corresponding method. If this item is NULL, the device is turned on successfully, but your driver will not be notified. Int (*flush) (struct file *, fl_owner_t ID);//flush operation is called when the process closes a copy of its device file descriptor; It should execute (and wait for) any unfinished operation of the device. This must not be confused with the fsync operation of the user query request. Currently, flush is used in very few drives; SCSI tape drives use it, for example, to ensure that all written data is turned off on the deviceWrite to the tape before closing. If Flush is NULL, the kernel simply ignores requests from the user application. Int (*release) (struct inode *, struct file *);//This operation is referenced when the file structure is freed. As open, release can be NULL. Int (*fsync) (struct file *, loff_t, loff_t, int datasync);//This method is the backend of the Fsync system call, and the user is called to refresh any data that hangs. If this pointer is NULL, the system call returns-EINVAL. Int (*aio_fsync) (struct KIOCB *, int datasync);//This is an asynchronous version of the Fsync method. Int (*fasync) (int, struct file *, int);//This operation is used to notify the device of the change of its fasync flag. Asynchronous notifications are an advanced topic, described in the 6th chapter. This member can be null if the driver does not support asynchronous notifications. Int (*lock) (struct file *, int, struct file_lock *);//lock method is used to implement file lock; Locking is an essential feature for regular files, but device drivers almost never implement it. ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);//sendpage is the other half of Sendfile; It is used by the kernel to send data, one page at a time, to the corresponding file. Device drivers do not actually implement Sendpage. unsigned long (*get_unmapped_area) (struct file *, unsigned long, unsigned long, unsigned long, unsigned long);//The purpose of this method is to enter the Address space of the process to find a suitable location to map the memory segments on the underlying device. This task is usually performed by memory management code; This method exists in order to enable the driver to force any alignment requests that a particular device may have. Most drivers can place this method as NULL. Int (*check_flags) (int);//This method allows module checks to be passed to Fnctl(F_SETFL ...) The flag to invoke. Int (*flock) (struct file *, int, struct file_lock *);//File Lock ssize_t (*splice_write) (struct pipe_inode_info *, struct F Ile *, loff_t *, size_t, unsigned int); ssize_t (*splice_read) (struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);//used to transfer file data from a pipeline, currently used only for splice 2 system call; int (*setlease) (struct file *, long, struct file_lock * *);//Set a file Long (*fallocate) (struct file) that opens the flag to open *file, int mode, loff_t offset, loff_t len);//By setting the mode flag Fa_allocate or Fa_deallocate, indicating preallocation and deal Location of preallocated blocks respectively, called by Sys_fallocate. };
Take the example mentioned in several blogs to the file_operations of event%d device files in evdev.c:
static const struct File_operations evdev_fops = {. owner= this_module,.read= evdev_read,.write= evdev_write,.poll= Evdev _poll,.open= evdev_open,.release= evdev_release,.unlocked_ioctl= evdev_ioctl, #ifdef config_compat.compat_ioctl= Evdev_ioctl_compat, #endif. fasync= evdev_fasync,.flush= evdev_flush,.llseek= No_llseek,};
File_operations in Linux device drivers