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 the module won’t be unloaded before the file is closed.

In most drivers, open does the following:

  • Checks for device-specific errors (like device-not-ready or similar hardware problems).

  • Initializes the device, if it is being opened for the first time.

  • Identifies the minor number and updates the f_op pointer, if necessary.

  • Allocates and fills any data structure to be put in filp->private_data.

  • Increments the usage count.

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/ttyS0 and /dev/ttyS1 refer to different serial ports, whereas /dev/cua0 is the same physical device as /dev/ttyS0, but it acts differently. cuas are ``callout'' devices; they aren’t ttys, and they don’t get all the software support that is needed for terminals (i.e., they don’t have a line discipline ...

Get Linux Device Drivers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.