Home >Operation and Maintenance >Linux Operation and Maintenance >Where is the linux character device placed?

Where is the linux character device placed?

青灯夜游
青灯夜游Original
2023-04-07 16:29:571814browse

Linux character devices are placed in the "/dev" directory. Character devices refer to devices that can only be read and written byte by byte. Generally, each character device or block device will correspond to a device file in the "/dev" directory, and each device file must have a main/ Devices with the same minor device number and major device number are similar devices and use the same driver.

Where is the linux character device placed?

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

1. Linux device driver classification

The Linux system divides devices into three categories: Character devices, block devices, and network Device , among these three categories, character devices are relatively simple. Applications access character devices through character device files. This lecture mainly introduces character devices. If you are interested in block devices and network devices, you can refer to the related information and gain an in-depth understanding of it.

Where is the linux character device placed?


2. What is a character device?

Character device refers to a device that can only read and write byte by byte. Certain data in the device cannot be read randomly, and data must be read in order. Character devices are stream-oriented devices. Common character devices include mice, keyboards, serial ports, consoles, and LEDs.

Where is the linux character device placed?

Generally, each character device or block device will correspond to a device file in the /dev directory, and each device file must have a main/ Devices with the same minor device number and major device number are similar devices and use the same driver.

Linux user-level programs use drivers to operate character devices or block devices through device files.

You can use the

cat /proc/devices

command to view the major device number of the currently loaded device driver.

Where is the linux character device placed?

You can see the major and minor device numbers of all device files by executing the command

ls -l

in the /dev directory:

Where is the linux character device placed?

The common device files are explained as follows:

/dev/hd[a-t]:IDE设备

/dev/sd[a-z]:SCSI设备

/dev/fd[0-7]:标准软驱

/dev/md[0-31]:软raid设备

/dev/loop[0-7]:本地回环设备

/dev/mem:内存

/dev/null:无限数据接收设备,相当于黑洞

/dev/zero:无限零资源

/dev/tty[0-63]:虚拟终端

/dev/ttyS[0-3]:串口

/dev/lp[0-3]:并口

/dev/console:控制台

/dev/fb[0-31]:framebuffer

/dev/cdrom => /dev/hdc

/dev/modem => /dev/ttyS[0-9]

/dev/pilot => /dev/ttyS[0-9]

3. How to create a device file?

There are two ways to create a device file. One is through the system call mknod(). Calling this function in programming can create a new device file name. The other is through the mknod command. The first one of the command The parameter is the device file name, the second parameter is the device type, for example, c represents a character device, and the third and fourth parameters are the major device number and minor device number of the device file, such as 231 and 0. The combination of the major device number and the minor device number uniquely determines a device. The major device numbers of different types of the same device are the same, but the minor device numbers are different. For example, multiple partitions of a hard disk have different minor device numbers. The device number can associate the device file with the driver.

mknod filename type major minor

  • filename: the name of the device file to be created;
  • type: device type, c represents a character Device, b represents a block device;
  • major: major device number;
  • minor: minor device number;

## 4. How to describe character devices?

The Linux kernel abstracts the struct cdev structure to represent a character device. cdev is defined in . The most critical one is the file_operations structure, which implements characters. The device's set of operations.

struct cdev {
        struct kobject kobj;   //  内嵌内核对象
        struct module *owner;  //该字符设备所在的内核模块
        const struct file_operations *ops; //文件操作结构体
        struct list_head list;  //已注册字符设备链表
        dev_t dev; //由主、次设备号构成的设备号
        unsigned int count;//同一主设备号的次设备号的个数
};

Linux uses the file_operations structure to access driver functions. The name of each member of this structure corresponds to a system call.

struct file_operations {
       struct module *owner;
       loff_t (*llseek) (struct file *, loff_t, int);
       ssize_t (*read) (struct file *, char *, size_t, loff_t *);
       ssize_t (*write) (struct file *, const char *, size_t, loff_t *);
       int (*readdir) (struct file *, void *, filldir_t);
       unsigned int (*poll) (struct file *, struct poll_table_struct *);
       int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
       int (*mmap) (struct file *, struct vm_area_struct *);
       int (*open) (struct inode *, struct file *);
       int (*flush) (struct file *);
       int (*release) (struct inode *, struct file *);
       int (*fsync) (struct file *, struct dentry *, int datasync);
       int (*fasync) (int, struct file *, int);
       int (*lock) (struct file *, int, struct file_lock *);
       ssize_t (*readv) (struct file *, const struct iovec *, unsigned long,loff_t *);
       ssize_t (*writev) (struct file *, const struct iovec *, unsigned long,    loff_t *);
    };

When the user process performs operations such as read and write on the device file, the system call finds the corresponding device driver through the major device number of the device file, and then reads the corresponding function pointer of this data structure. Then the control is handed over to the function, which is the basic principle of how the Linux device driver works.

5. Interface between character device and file system

Where is the linux character device placed?

As shown in the figure, in the Linux kernel, on the far left, the cdev structure is used to describe the character device; its member dev_t is used to define the device number (divided into major and minor device numbers) to determine the uniqueness of the character device; through its member dev_t The member file_operations defines the interface functions provided by the character device driver to the virtual file system VFS, such as the common open(), read(), write(), etc. These functions actually operate the hardware device.

Where is the linux character device placed?

Let’s look at this picture based on the previous picture. The character device driver is loaded into the kernel in the form of a kernel module. First, the module loading function presses static or Dynamic method Get the device number ; then the character device initialization function establishes the connection between cdev and file_operations, and adds it to the system through registration function A cdev to complete the registration; when the module is unloaded, corresponding to the loading, the cdev needs to be logged out and the device number is released.

In the user program, these functions implemented by the driver in the kernel can be called through system calls such as open(), read(), write(), etc. In this way, the path between user mode and kernel driver is opened.

#6. Write a simple character device driver

Where is the linux character device placed?

As shown in the figure, write a character device driver. There are three major steps:

    driver initialization, which is divided into four steps and is achieved by calling relevant functions.
  1. Implement the operation of the device. The specific operation depends on the function you want to implement. Only the basic operations are listed here.
  2. Driver logout, logout is to release resources.
The functions of the interface functions called are as follows:

Where is the linux character device placed?

The first function is the allocation function, which dynamically applies for the memory of cdev and allocates it to the structure memory space.

The second function is the initialization function, initializes the members of cdev, and establishes the association between cdev and file_operations.

The third function registers the cdev device object, that is, adds the character device to In the character device table, just like everyone registers when they enter school.

The fourth function is to log out the driver call and delete the cdev object from the system.

The fifth function releases the memory occupied by the cdev data structure.

6.1 Application and release of device number

A character device or block device has a major device number and A minor device number.

The major device number is used to identify the driver connected to the device file and is used to reflect the device type. The secondary device number is used by the driver to identify which device is being operated and to distinguish devices of the same type . Apply for a device number when registering, and release the device number when logging out. Just like everyone has a student ID when they enroll, and this student ID is released when they graduate and leave.

Where is the linux character device placed?

6.2 Transmission of user space and kernel space data

When we When the read() function is called in the user program, it falls into the kernel space. In fact, the data in the kernel space buffer must be copied to the user space buffer through the kernel's copy_to_user() function. On the contrary, when we call the write() function When, the kernel copies the user space data to the kernel buffer by calling the copy_from_user() function.

Where is the linux character device placed?

Related recommendations: "

Linux Video Tutorial"

The above is the detailed content of Where is the linux character device placed?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn