[Beijing Xunwei] i.MX6ULL Terminator Linux MISC driver misc device driver introduction

The major device number of all misc device drivers is 10, and different devices use different slave device numbers. When using the misc device driver, the misc device will automatically create cdev instead of manually creating it as before. Therefore, using the misc device driver can simplify the writing of character device drivers.
The misc device is represented by the miscdevice structure, which is specifically defined in include/linux/miscdevice.h, and the content is as follows:

57 struct miscdevice {
    
     
58         int minor; /* 子设备号 */ 
59         const char *name; /* 设备名字 */ 
60         const struct file_operations *fops; /* 设备操作集 */ 
61         struct list_head list; 
62         struct device *parent; 
63         struct device *this_device; 
64         const struct attribute_group **groups; 
65         const char *nodename; 
66         umode_t mode; 
67 };

When we create a miscdevice structure of a misc device, we need to specify the three member variables minor, name and fops. minor represents the sub-device number, which needs to be set by the user. There are some predefined sub-device numbers of misc devices in the Linux kernel, which are defined in the include/linux/miscdevice.h file, as shown below:

13 #define    PSMOUSE_MINOR          1 
14 #define    MS_BUSMOUSE_MINOR  2 /* unused */ 
15 #define    ATIXL_BUSMOUSE_MINOR   3 /* unused */ 
16 /*#define  AMIGAMOUSE_MINOR       4 FIXME OBSOLETE */ 
17 #define    ATARIMOUSE_MINOR       5 /* unused */ 
18 #define    SUN_MOUSE_MINOR        6 /* unused */ 
...... 
52 #define    MISC_DYNAMIC_MINOR     255

When we set the sub-device number, be careful not to reuse the sub-device numbers of other devices. You can choose one of these predefined sub-device numbers, or you can customize it.
name is the name of the misc device. When the device is successfully registered, a device file named name will be automatically generated in the /dev directory. fops is the operation set of this misc device.
After the miscdevice structure is created, use the misc_register function to register a misc device in the system. The function prototype is as follows:
int misc_register(struct miscdevice * misc) The
parameter misc is the miscdevice structure created before. It returns 0 on success, and a negative number on failure.

When creating a character device driver, we will use the following functions to complete the creation of the device:

1 alloc_chrdev_region();    /* 申请设备号 */ 
2 cdev_init();            /* 初始化 cdev */ 
3 cdev_add();             /* 添加 cdev */ 
4 class_create();         /* 创建类 */ 
5 device_create();        /* 创建设备 */

Now only one misc_register function is needed to replace these functions.
In the uninstall function of the device driver, use the misc_deregister function to unregister the misc device. The function prototype is as follows: the
int misc_deregister(struct miscdevice *misc)
parameter misc is the miscdevice structure to be cancelled.
The same misc_deregister function is used to replace the deregistration functions of the following character devices:

1 cdev_del(); 					/* 删除 cdev */ 
2 unregister_chrdev_region();	 /* 注销设备号 */ 
3 device_destroy(); 				/* 删除设备 */ 
4 class_destroy();				 /* 删除类 */ 

Insert picture description here

Guess you like

Origin blog.csdn.net/BeiJingXunWei/article/details/112002843