关于register_chrdev_region问题()在Linux设备驱动程序 [英] Questions about register_chrdev_region() in linux device driver

查看:327
本文介绍了关于register_chrdev_region问题()在Linux设备驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何使用 register_chrdev_region(从的dev_t,无符号数,为const char *名称)一个内核模块的登记;

我注意到,有或没有这个功能,我的内核模块和预期一样。在code我用于测试:

I notice that with or without this function, my kernel module worked as expected. The code I used for testing:

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(第一,计数,DEVICE_NAME); 的printk 消息仍未出现。我试图与具有或不具有这种从用户空间驾驶员进行通信,并且两个都是成功的。

I commented out the line register_chrdev_region(first, count, DEVICE_NAME);, and the printk message still appeared. I tried to communicate with the driver with or without this from user space, and both are successful.

所以我的问题是,这个函数 register_chrdev_region()只用来做我的司机一个好的内核的公民,就像告诉别人说:我使用了主号码,请不要使用?

So my question is, is this function register_chrdev_region() only used to make my driver a good kernel citizen, just like telling the others that "I'm using up the major number, please don't use"?

我试图在内核源代码 char_dev.c 来理解函数一看,但我觉得它太难理解,任何人是熟悉?

I tried to have a look in the kernel source char_dev.c to understand the function, but I find it too difficult to understand, anyone that's familiar with this?

谢谢!

推荐答案

这会工作,因为它不是真正的必要的分配设备的数字前面。许多内核开发者事实上,它被认为preferable使用动态(上即时,按需)配置功能 alloc_chrdev_region

That will work because it's not actually necessary to allocate your device numbers up front. In fact, it's considered preferable by many kernel developers to use the dynamic (on-the-fly, as-needed) allocation function alloc_chrdev_region.

无论你做静态前面或根据需要动态,它的的东西,你应该做的,以避免与可能的规则发挥和分配你的数字以外的设备驱动程序冲突尝试使用。即使你的驱动程序工作得很好,没有它,这并不一定是真实的每台机器上或者在将来的任何时间。

Whether you do it statically up front or dynamically as needed, it is something you should do to avoid conflict with other device drivers which may have played by the rules and been allocated the numbers you're trying to use. Even if your driver works perfectly well without it, that won't necessarily be true on every machine or at any time in the future.

的规则是有原因的,特别是低层次的东西,你经过深思熟虑,决定跟着他们。

The rules are there for a reason and, especially with low-level stuff, you are well advised to follow them.

请参阅这里在设置过程的更多细节。

See here for more details on the set-up process.

这篇关于关于register_chrdev_region问题()在Linux设备驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆