1. 程式人生 > >字元驅動裝置2.2--設備註冊

字元驅動裝置2.2--設備註冊

核心內部使用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;
};

字元設備註冊使用流程

  1. 分配一個cdev struct cdev *cdev_alloc(void);
  2. 初始化cdev結構 void cdev_init(struct cdev *, const struct file_operations *);
  3. 新增到核心。告知核心該結構可以使用了。 要檢查返回值,dev表示第一個裝置編號,count表示裝置編號數量,通常為1 int cdev_add(struct cdev *p, dev_t dev, unsigned count)
  4. 從系統移除字元裝置 void cdev_del(struct cdev *);