cdev动态和静态注册的理解错误

来源:互联网 发布:nba视频网络直播 编辑:程序博客网 时间:2024/05/29 03:40

先写下LDD3P59页字符设备的注册一节的原文:

我们前面提到,内核内部使用struct cdev结构来表示字符设备。。。

分配和初始化上述结构的方式有两种。如果读者打算在运行时获取一个独立的cdev结构,则应该如下编写代码:

struct cdev *my_cdev = cdev_alloc();

my_cdev->ops = &my_fops;

这时,你可以将cdev结构嵌入到自己的设备待定结构中,scull就是这样做的。这种情况下,我们需要用下面的代码来初始化已分配到的结果:

void cdev_init(struct cdev *cdev, struct file_operations *fops);

。。。

上面是部分原文,主要是”这时“让我产生了误解,以为是上面申请好后,下面进行了初始化。

查看了英文的原文:(多copy了两段)

There are two ways of allocating and initializing one of these structures. If you wish to obtain a standalone cdev structure at runtime, you may do so with code such as:
struct cdev *my_cdev = cdev_alloc( );
my_cdev->ops = &my_fops;

Chances are, however, that you will want to embed the cdev structure within a device-specific structure of your own; that is what scull does. In that case, you

should initialize the structure that you have already allocated with: 

void cdev_init(struct cdev *cdev, struct file_operations *fops);

Either way, there is one other struct cdev field that you need to initialize. Like the file_operations structure, struct cdev has an owner field that should be set to 

THIS_MODULE.

Once the cdev structure is set up, the final step is to tell the kernel about it with a call to: 

int cdev_add(struct cdev *dev, dev_t num, unsigned int count);

找到了有疑问的那一段,查了下解释:

chances are that:可能

initialize: vt  初始化

它的意思是指你可以在运行时动态的分配一个cdev结构,也可以把cdev结构内嵌到一个自己的设备结构中然而后者就需要使用cdev_init来初始化你已分配的结构。

所以

struct cdev *my_cdev = cdev_alloc();

my_cdev->ops = &my_fops;

void cdev_init(struct cdev *cdev, struct file_operations *fops);

是对cdev结构的两种初始化方式。

0 0