cdev_add alloc_chrdev_region 系列函数
2016-04-23 16:49
351 查看
cdev_add alloc_chrdev_region 系列函数
转载▼
在Linux 2.6内核中的字符设备用cdev结构来描述,其定义如下:
struct cdev
{
struct kobject kobj;
struct module *owner; //所属模块
const struct file_operations *ops; //文件操作结构
struct list_head list;
dev_t dev; //设备号,int 类型,高12位为主设备号,低20位为次设备号
unsigned int count;
};
下面一组函数用来对cdev结构进行操作:
struct cdev *cdev_alloc(void);//分配一个cdev
void cdev_init(struct cdev *, const struct file_operations *);//初始化cdev的file_operation
void cdev_put(struct cdev *p);// //减少使用计数
//注册设备,通常发生在驱动模块的加载函数中
int cdev_add(struct cdev *, dev_t, unsigned);
//注销设备,通常发生在驱动模块的卸载函数中
void cdev_del(struct cdev *);
使用cdev_add注册字符设备前应该先调用register_chrdev_region或alloc_chrdev_region分配设备号。register_chrdev_region函数用于指定设备号的情况,alloc_chrdev_region函数用于动态申请设备号,系统自动返回没有占用的设备号。
int register_chrdev_region(dev_t from, unsigned count, const char *name) ;
int alloc_chrdev_region(dev_t *dev,unsigned baseminor,unsigned count,const char *name);
alloc_chrdev_region申请一个动态主设备号,并申请一系列次设备号。baseminor为起始次设备号,count为次设备号的数量,此函数返回的是动态注册设备的主设备号。注销设备号(cdev_del)后使用unregister_chrdev_region:
void unregister_chrdev_region(dev_t from,unsigned count) ;
cdev_add注册字符设备实例
struct file_operations simple_fops = {
.owner = THIS_MODULE,
.read = simple_read,
.write = simple_write,
.open = simple_open,
.release = simple_release,
};
void simple_cleanup_module(void)
{
dev_t devno = MKDEV(simple_MAJOR, simple_MINOR);
if (simple_devices)
{
cdev_del(&simple_devices->cdev);
kfree(simple_devices);
}
unregister_chrdev_region(devno,1);
}
//模块初始化
int simple_init_module(void)
{
int result;
dev_t dev = 0;
dev = MKDEV(simple_MAJOR, simple_MINOR);
result = register_chrdev_region(dev, 1, "DEMO");//申请设备号
if (result <</SPAN> 0)
{
printk(KERN_WARNING "DEMO: can't get major %d\n", simple_MAJOR);
return result;
}
simple_devices = kmalloc(sizeof(struct simple_dev), GFP_KERNEL);
if (!simple_devices)
{
result = -ENOMEM;
goto fail;
}
memset(simple_devices, 0, sizeof(struct simple_dev));
//初始化设备结构
cdev_init(&simple_devices->cdev, &simple_fops);
simple_devices->cdev.owner = THIS_MODULE;
simple_devices->cdev.ops = &simple_fops;
result = cdev_add (&simple_devices->cdev, dev, 1);//添加字符设备
if(result)
{
printk(KERN_NOTICE "Error %d adding DEMO\n", result);
goto fail;
}
return 0;
fail:
simple_cleanup_module();
return result;
}
module_init(simple_init_module);
module_exit(simple_cleanup_module);
相关文章推荐
- 机器学习之——单变量线性回归
- TCP是如何保证包的顺序传输?
- Leetcode no. 5
- java中的compareto方法的详细介绍
- swift 网络请求&json解析
- 【并发编程】Executor架构介绍
- Android实战简易教程-第七十三枪(拨打、接听电话自动免提)
- Java动态绑定机制--笔记
- Android封装支付宝支付
- 机器学习之——多变量线性回归
- span元素的实际应用
- [从头学数学] 第192节 导数及其应用
- # swift2.0 数据结构:元组,枚举,结构体
- 机器学习之——多项式回归和正规方程
- 机器学习之——逻辑回归
- 简易版支付系统部署(单节点)
- cocos2d-x-3.0 的改变,由于变得太多,一点点累积吧!
- 2015國際初中科學奧林匹克 – 香港選拔賽 (比賽結果)
- Log4j
- Codeforces 389A (最大公约数)