open and release
Now that we’ve taken a quick look at the fields, we’ll start using them in real scull functions.
The open Method
The open method is provided for a driver to do any initialization in preparation for later operations. In addition, open usually increments the usage count for the device so that the module won’t be unloaded before the file is closed. The count, described in Section 2.4.2 in Chapter 2, is then decremented by the release method.
In most drivers, open should perform the following tasks:
Increment the usage count
Check for device-specific errors (such as device-not-ready or similar hardware problems)
Initialize the device, if it is being opened for the first time
Identify the minor number and update the
f_oppointer, if necessaryAllocate and fill any data structure to be put in
filp->private_data
In scull, most of the preceding tasks
depend on the minor number of the device being opened. Therefore, the
first thing to do is identify which device is involved. We can do that
by looking at inode->i_rdev.
We’ve already talked about how the kernel doesn’t use the minor number
of the device, so the driver is free to use it at will. In practice,
different minor numbers are used to access different devices or to
open the same device in a different way. For example,
/dev/st0 (minor number 0) and
/dev/st1 (minor 1) refer to different SCSI tape
drives, whereas /dev/nst0 (minor 128) is the same
physical device as /dev/st0, but it acts differently (it ...