您的位置:首页 > 运维架构 > Linux

linux驱动程序基础

2015-11-02 22:53 489 查看
1. 分配和释放设备号

静态分配设备号:int register_chrdev_region(dev_t first,unsigned int count,char *name)

动态分配设备号:int alloc_chrdev_region(dev_t *dev ,unsigned int firstminor,unsigned int count,char *name)

释放设备编号:void unregister_chrdev_region(dev_t first,unsigned int count)

2.字符设备的注册函数 应该包含的头文件是 <linux/cdev.h>

内核使用 struct cdev 表示字符设备

字符设备的结构如下:

struct cdev {

struct kobject kobj;

struct module *owner; // 指向实现驱动的模块

const struct file_operations *ops; // 操纵这个字符设备文件的方法

struct list_head list; // 与 cdev 对应的字符设备文件的 inode->i_devices 的链表头

dev_t dev; // 起始设备编号

unsigned int count; // 设备范围号大小

};

分配cdev 内存的方法:

静态分配:struct cdev my_cdev

动态分配:struct cdev *my_cdev = cdev_alloc();

cdev 结构的初始化函数void cdev_init(struct cdev *cdev, const struct file_operations *fops)

添加字符设备的函数:int cdev_add(struct cdev *cdev,dev_t num,unsigned int count );
注:成功返回 0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: