<linux/cdev.h>
struct cdev {
struct kobject kobj; //内嵌的内核对象
struct module *owner;//内核模块的对象指针
const struct file_operations *ops;//向上提供的接口
struct list_head list;//已经向内核注册的所有字符设备形成链表
dev_t dev;//设备号
unsigned int count;//次设备号个数
};
void cdev_init(struct cdev *, const struct file_operations *);//用于初始化cdev的成员,并建立cdev和file_operations之间的连接
struct cdev *cdev_alloc(void);//用于动态申请一个cdev内存
void cdev_put(struct cdev *p);//用向系统添加和删除一个cdev,完成字符设备的注册和注销
int cdev_add(struct cdev *, dev_t, unsigned); // 通常在字符设备驱动模块加载函数中调用
void cdev_del(struct cdev *); // 字符设备驱动模块卸载函数中调用
void cd_forget(struct inode *);//结构节点索引
举例
static int __init xxx_init(void)
{
。。。
cdev_init(&xxx_dev.cdev, &xxx_fops); // 初始化 cdev
xxx_dev.cdev.owner = THIS_MODULE;
MKDEV(xxx, 0);
获取字符设备号(register_chrdev_region、alloc_chrdev_region)
ret = cdev_add(&xxx_dev.cdev, xxx_dev_no, 1); //注册设备
}
比较老的方法
<linux/fs.h>
int register_chrdev(unsigned int major, const char *name,
const struct file_operations *fops)
void unregister_chrdev(unsigned int major, const char *name)
举例
static int __init xxx_init(void)
{
register_chrdev(MAJOR, XXXname, &XXX_fops);
}