cdev動態和靜態註冊的理解錯誤

先寫下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結構的兩種初始化方式。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章