data structure
#define CHRDEV_MAJOR_HASH_SIZE 255
static struct char_device_struct {
struct char_device_struct *next;
unsigned int major;
unsigned int baseminor;
int minorct;
char name[64];
struct file_operations *fops;
struct cdev *cdev; /* will die */
} *chrdevs[CHRDEV_MAJOR_HASH_SIZE];
/* index in the above */
static inline int major_to_index(int major)
{
return major % CHRDEV_MAJOR_HASH_SIZE;
}
Chrdevs global variables, all the characters is a backup array device, by converting the primary index values in ascending order;
each char_device_struct element is a linked list of elements per-view device numbers in ascending order.
Relationship between the master device of the index number: major% CHRDEV_MAJOR_HASH_SIZE, namely: major = 1 major = 256 and use the same index value, index = 1;
Differences in the way the application equipment
----------------------------------------------- 支持多设备 -----------------------------------------------
int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, const char *name)
dev = 0 时,为动态申请设备,申请到的设备号保存到dev;
baseminor,次设备号起始编号;
count,申请连续设备号的数量;
name,设备名称;
----------------------------------------------- 只支持静态申请 -----------------------------------------------
int register_chrdev_region(dev_t from, unsigned count, const char *name)
from,申请的设备号起始编号;
count,申请连续设备号的数量;
name,设备名称;
----------------------------------------------- 单设备 -----------------------------------------------
int register_chrdev(unsigned int major, const char *name, const struct file_operations *fops)
major = 0 时,为动态申请设备,申请到的主设备号保存到major;
name,设备名称;
fops,关联设备的文件操作符;
NOTE: For sequential application device number, using reverse application. Eventually return to the starting device number.
Functional Differences
Complete the registration process
register_chrdev = { alloc_chrdev_region + cdev_init + cdev_add }