Chapter 3-Character Driven Devices

3.1, character device driver foundation

 Device files are usually located in the / dev directory:

crw-rw-rw-  1 root      root           5,   0 2020-04-17 18:51 tty
crw-rw----  1 radio     radio        237,   3 2020-04-17 18:51 ttyC0
crw-rw----  1 radio     radio        237,   7 2020-04-17 18:51 ttyC1

 Where c represents character device. In the current Linux system, the device file is usually created automatically, but we can still manually create a device file through the mknod command:

# mknod /dev/vser0 c 256 0
# ls -li /dev/vser0
46347 crw-rw---- 1 root root 256,   0 2020-04-19 17:36 /dev/vser0

 The mknod command creates a node. In Linux, a node represents a file. The main job of creating a file is to allocate a new node (node), which contains the node number (46347 unique). Take the ext2 file system as an example:

/*fs/ext2/ext2.h*/
/*
 * Structure of an inode on the disk
 */
struct ext2_inode {
    __le16  i_mode;	/* File mode */
    __le16  i_uid;	/* Low 16 bits of Owner Uid */
    __le32  i_size;	/* Size in bytes */
    __le32  i_atime;	/* Access time */
    __le32  i_ctime;	/* Creation time */
    __le32  i_mtime;	/* Modification time */
......
    __le32  i_block[EXT2_N_BLOCKS];/* Pointers to blocks */
......
};

 The inode member structure is described in the above structure, in /fs/ext2/inode.c :

static int __ext2_write_inode(struct inode *inode, int do_sync) {
    struct ext2_inode *raw_inode = ext2_get_inode(sb, ino, &bh); //获得一个要写入磁盘的ext2_inode结构,并初始化了部分成员。
}

 Similarly, the directory itself is a file, and there is also a structure to describe the directory in fs / ext2 / ext2.h. The execution of mknod saves the file name, file type, major and minor device numbers and other information on the disk.struct ext2_dir_entry

ext2

3.2 How to open a file?

  1. A member of a process (task_struct) (task_struct-> file_struct) There is a pointer array fd_array in this structure, which is used to maintain the information of the open file. Each element of the array fd_array is a pointer to the file structure.
  2. The corresponding function of the open system call in the kernel is sys_open, and sys_open calls do_sys_open.
  3. do_sys_open calls the getname function to copy the file name from the user space to the kernel space, and then calls get_unused_fd_flags to obtain an unused file descriptor fd.
  4. Call do_filp_open to construct a file structure and initialize the members inside, the most important thing is to point the f_op member to the drive operation method file_operations. The open operation of the driver can be called through the open function pointer of file_operations.
  5. After the call to do_filp_open is successful, the fd_install function is called, which uses the obtained file descriptor as a subscript to access the fd_array array, and the element corresponding to the subscript points to the newly constructed file structure.
  6. Finally, the system call returns to the application layer, returning the array index just now as the file descriptor of the open file.

For character devices, the device number, cdev and file_operations are very important. After the kernel finds the inode corresponding to the path name, to establish a connection with the driver, the first thing to do is to find cdev according to the device number in the inode, and then find file_operations according to cdev set.

Guess you like

Origin www.cnblogs.com/hansenn/p/12738162.html