我正在学习如何注册内核模块register_chrdev_region(dev_t from, unsigned count, const char * name);
.
我注意到无论有没有这个函数,我的内核模块都按预期工作。我用于测试的代码:
first = MKDEV(MAJOR_NUM, MINOR_NUM);
register_chrdev_region(first, count, DEVICE_NAME);//<---with and without
mycdev=cdev_alloc();
mycdev->ops= &fops;
mycdev->owner = THIS_MODULE;
if (cdev_add(mycdev,first, count) == 0)
{printk(KERN_ALERT "driver loaded\n");}
我注释掉了该行register_chrdev_region(first, count, DEVICE_NAME);
,以及printk
消息仍然出现。我尝试从用户空间与驱动程序进行通信,无论有没有这个,两者都成功。
所以我的问题是,这个功能是register_chrdev_region()
只是用来让我的驱动程序成为一个好的内核公民,就像告诉其他人“我正在用完主号码,请不要使用”?
我试图查看内核源代码char_dev.c
理解这个功能,但我觉得太难理解了,有人熟悉这个吗?
Thanks!