open 和 release

We started to use them in real scull functions.

 

open method

 

the driving method is provided to open to do any initialization operation to prepare for the subsequent driving in most, should be open following work:

 

  • Check the device-specific error (for example, the device is not ready or similar hardware error
  • If it first opened, the device is initialized
  • If necessary, update f_op pointer.
  • To put the filling and dispensing filp-> private_data any data structure

 

However, things are often the first step to determine which device is turned remember the open method prototype is:

 

 

 

int (*open)(struct inode *inode, struct file *filp);

 

inode parameters have the information we need, in the form of its i_cdev members, which contains cdev structure of our previously established. The only problem is that usually we do not want cdev structure itself, what we need is scull_dev structure contains cdev structure. C language It allows programmers to play with a variety of techniques to do this conversion; however, this technique programming is error-prone and difficult to lead others to read and understand the code Fortunately, in this case, the kernel hacker has achieved for us. this technique, in the form of macro container_of defined in <linux / kernel.h> of:

 

container_of(pointer, container_type, container_field);

 

This macro uses a pointing member container_field type pointer, it is a type of structure container_type, and returns a pointer to a structure containing for scull_open, this macro is used to find the appropriate device structure:

 

struct scull_dev *dev; /* device information */

dev = container_of(inode->i_cdev, struct scull_dev, cdev); filp->private_data = dev; /* for other methods */

 

Once it finds scull_dev structure, The scull stores a pointer to it in the file structure private_data members, easier access for the future.

 

Additional methods to identify open device that view is stored in the secondary struct inode number. If you use register_chrdev register your device, you have to use this technique was confirmed using iminor acquired minor number from the inode structure and determines that it corresponds to a you drivers are really ready to deal with the devices.

 

scull_open code (through a somewhat simplified) is:

 

int scull_open(struct inode *inode, struct file *filp)

{

struct scull_dev *dev; /* device information */

dev = container_of(inode->i_cdev, struct scull_dev, cdev); filp->private_data = dev; /* for other methods */

 

/* now trim to 0 the length of the device if open was write-only */ if ( (filp->f_flags & O_ACCMODE) == O_WRONLY)

{

scull_trim(dev); /* ignore errors */

}

return 0; /* success */

}

 

Code seems quite sparse, because when you call open it did not do any special handling equipment. It does not need, because scull device is designed as a global and permanent. In particular, there is no such as "when you first open the Initialize Device" etc, because we do not count for the scull remains open.

 

Only real operation on the device when the device is opened for writing, it will be truncated to a length of 0. This is because, in the design, with a short document covers a scull device causes a short to the data area. This opened for writing like a regular file, it is truncated to 0. If the device is opened for reading, this operation does nothing.

 

 

 

 

When we look at the code scull other properties will see how a real initialization works.

 

 release method

 

The role of the release method is the opposite of open and sometimes you will find that implementation is called device_close, rather than device_release Either way, the device method should perform the following tasks:

 

  • Release open distributed filp-> private_data of anything
  • In the final close off the device

 

The basic form of scull has no hardware to close, so the code required is minimal: [ 12 ] 12

 

int scull_release(struct inode *inode, struct file *filp)

{

return 0;

}

 

You might want to know when a device file Closes times more than what it was opened happen after all, dup and fork system call does not call open to create copies of open files; each copy is then closed at program termination, for example. most programs do not open their stdin file (or device), but they are close to the end of it. how do I know when driving an open device file has really been turned off?

 

The answer is simple: not every close call system call causes the release method only real release calls the device data structure calls this method - hence the name of the kernel file structure is to maintain a count of how many times a fork and dup not create.. the new file (only open so); they only count up being present in the structure of the close system call is performed only when the release method in the file structure count fell to 0, which occurs when the structure is destroyed release method and the close system call. this ensures that the relationship between your drive once open only see once release.

 

Note, flush method is invoked each time the application calls close. However, few drivers implement flush, because often there is nothing to do at the time of close, unless you call release.

 

As you would expect, the preceding discussion of the application is not even close it obviously also applies to open files: the kernel automatically closes any file at process exit, through the use of close calls within the system.

Guess you like

Origin www.cnblogs.com/fanweisheng/p/11106332.html