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
静态分配设备号: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;
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
相关文章推荐
- linux命令基本功之mkdir
- 每天学一点linux(三)
- linux 常用基础命令 cd 详细介绍
- linux fork.c
- linux分区建议
- (错误处理)使用u盘安装centos后无法进入系统,开机启动项选择在U盘了
- Linux 更新apt-get源 出现错误
- linux 清空文件内容若干方法
- Linux之top命令......
- linux常用命令chmod,chown,chgrp
- CentOS下配置ssh免密码登录
- Java运行linux和windows系统命令
- Linux关闭防火墙
- Linux有问必答:如何找出Linux中内置模块的信息
- linux的僵尸进程和孤儿进程及解决方法
- linux新装的系统下su命令不能使用的解决办法
- Linux 键盘模拟测试
- Linux内存子系统基础及常用调优参数
- Linux 的文件权限与目录配置
- Linux常用命令总结及常用快捷键