world leader in high performance signal processing
Trace: » basic-vfs-open-close

The Open Function

The Open device driver function is used to set up the process access to the device. Normally the following functions are performed.

  • Set up a private data structure to save the state of the driver
  • Create any memory areas used by the device drivers
  • Manage any locks and semaphores used to control access to the device

The Open function should return 0 to indicate success or a negative error code.

The parameters for the open function are as follows

  • struct inode *inode
  • struct file *file
The inode refers to the device node found on the file system 

The  file is a structure, created by the kernel, to hold the
state of our use of the device within the device driver

A typical Open Function is shown here

static unsigned long scmd_is_open;
static int scmd_open(struct inode *inode, struct file *file)
        // allow only one user
        if (test_and_set_bit(0, &scmd_is_open))
                return -EBUSY;
        // Activate the system
        return nonseekable_open(inode, file);

Note that nonseekable_open open is defined in fs/open.c

 * This is used by subsystems that don't want seekable
 * file descriptors
int nonseekable_open(struct inode *inode, struct file *filp)
        filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
        return 0;

This is a typical close function. It is sometimes called release.

static int scmd_release(struct inode *inode, struct file *file)
        // release the driver for others
        clear_bit(0, &scmd_is_open);
        // we are done
        return 0;