9

这可能是一个新手问题,但请帮助我理解它。

为什么我们需要在字符驱动程序中注册 struct cdev?

4

2 回答 2

22

struct cdev表示内核中的字符设备。

所有流设备(例如:uart、keyboard)都属于字符设备类别,并且在用户空间中作为设备节点文件例如:/dev/ttyS0)可用。用户应用程序使用标准文件 I/O 操作访问设备。

在此处输入图像描述

在内核内部,字符驱动程序位于设备文件和流设备之间,该驱动程序层负责将文件 I/O 操作转换为设备操作,反之亦然。

在字符设备驱动程序开发struct file_operations中使用的最重要的数据结构。此结构用于实现基本文件 i/o - open()、read()、write()、close()、ioctl() 等...设备的功能。

struct cdev结构封装file_operations结构和一些其他重要的驱动信息(主要/次要没有)。它是向内核注册字符驱动程序的新方法。

内核通过以下 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

调用 后cdev_add(),您的设备立即启动。可以调用您定义的所有函数(通过 file_operations 结构)。

于 2013-02-05T18:16:44.057 回答
10

根据我对您问题的理解,您似乎在询问struct cdev. struct cdevinode结构的要素之一。您可能已经知道,inode内核内部使用一个结构来表示文件。struct cdev是表示字符设备的内核内部结构。所以cdev* i_cdevfield ofstruct inode是一个指向 cdev 结构的指针,而 theinode是指 char 设备文件。因此,如果内核必须调用字符设备,它必须注册一个cdev类型的结构。

于 2013-02-05T11:20:06.057 回答