為什么要在驅動程序代碼中注冊struct cdev

[英]why to register struct cdev in driver code


This may be a novice question, but please help me to understand it.

這可能是一個新手問題,但請幫助我理解它。

Why exactly do we need to register the struct cdev in our character driver?

為什么我們需要在字符驅動程序中注冊struct cdev?

2 个解决方案

#1


10  

From what I understand of your question, you seem to be asking about the significance of struct cdev. struct cdev is one of the elements of the inode structure. As you probably may know already, an inode structure is used by the kernel internally to represent files. The struct cdev is the kernel's internal structure that represents char devices. So cdev* i_cdev field of struct inode is a pointer to cdev structure while the inode refers to the char device file. Therefore if the kernel has to invoke the character device, it has to register a structure of cdev type.

根據我對你的問題的理解,你似乎在詢問struct cdev的重要性。 struct cdev是inode結構的元素之一。您可能已經知道,內核使用inode結構來表示文件。 struct cdev是內核的內部結構,代表char設備。因此struct inode的cdev * i_cdev字段是指向cdev結構的指針,而inode指的是char設備文件。因此,如果內核必須調用字符設備,則必須注冊cdev類型的結構。

#2


15  

struct cdev represent a character device within the kernel.

struct cdev表示內核中的字符設備。

All streaming devices(ex: uart, keyboard) falls under the character device category and is available in user-space as a device node file (ex: /dev/ttyS0). User applications accesses the device by using the standard File I/O operations.

所有流設備(例如:uart,鍵盤)都屬於字符設備類別,並且在用戶空間中可用作設備節點文件(例如:/ dev / ttyS0)。用戶應用程序使用標准文件I / O操作訪問設備。

enter image description here

Inside kernel, character driver comes in between the device file and the streaming device, and this driver layer takes care of translating the File I/O operation to Device operations and vice-versa.

在內核中,字符驅動程序介於設備文件和流設備之間,此驅動程序層負責將文件I / O操作轉換為設備操作,反之亦然。

In character device driver development struct file_operations is the most important data structure used. This structure is used to implement the basic file i/o - open(), read(), write(), close(), ioctl(), etc... functionality for the the device.

在字符設備驅動程序開發中,struct file_operations是最重要的數據結構。此結構用於實現設備的基本文件i / o - open(),read(),write(),close(),ioctl()等功能。

struct cdev structure encapsulates the file_operations structure and some other important driver information(major/minor no). It is the new way of registering character driver with kernel.

struct cdev結構封裝了file_operations結構和一些其他重要的驅動程序信息(major / minor no)。這是用內核注冊字符驅動程序的新方法。

The cdev structure is accessed by the kernel through the following API's:

內核通過以下API訪問cdev結構:

cdev_init() - used to initialize struct cdev with the defined file_operations
cdev_add()  - used to add a character device to the system. 
cdev_del()  - used to remove a character device from the system

After a call to cdev_add(), your device is immediately alive. All functions you defined (through the file_operations structure) can be called.

在調用cdev_add()之后,您的設備立即處於活動狀態。您可以調用您定義的所有函數(通過file_operations結構)。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/02/05/72590ff55e7ab2a15f9a70a7f906f9d0.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com