字元驅動裝置2.2--設備註冊
阿新 • • 發佈:2018-12-10
核心內部使用struct cdev
結構來標識字元裝置。在核心呼叫裝置的操作之前,必須註冊一個或
多個該結構。該結構定義在<linux/cdev.h>
中。
struct cdev {
struct kobject kobj;
struct module *owner; //持有cdev的模組,通常設定成THIS_MODULE
const struct file_operations *ops;
struct list_head list;
dev_t dev;
unsigned int count;
};
字元設備註冊使用流程
- 分配一個cdev
struct cdev *cdev_alloc(void);
- 初始化cdev結構
void cdev_init(struct cdev *, const struct file_operations *);
- 新增到核心。告知核心該結構可以使用了。
要檢查返回值,dev表示第一個裝置編號,count表示裝置編號數量,通常為1
int cdev_add(struct cdev *p, dev_t dev, unsigned count)
- 從系統移除字元裝置
void cdev_del(struct cdev *);