你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux 中字符设备的注册

[日期:2013-01-22] 来源:Linux社区  作者:alanwu [字体:大 中 小]

需要注意的是2.6.12内核中创建系统设备时需要调用simple_device的接口class_simple_device_add()。在2.6.23中需要调用device_create()函数完成设备注册。在3.2内核中,simple_device的接口已经不存在了,所以必须调用device_create函数,另外,3.2内核不支持具有相同minor号的字符设备,在2.6.x内核中是支持的。

系统是如何完成fops函数调用的呢?回答这个问题需要分析Misc设备打开过程。在打开Misc设备驱动的时候,Misc设备驱动会根据访问设备的Minor号重定向fops函数集,该程序说明如下:

static int misc_open(struct inode * inode, struct file * file)
{
int minor = iminor(inode);
struct miscdevice *c;
int err = -ENODEV;
const struct file_operations *old_fops, *new_fops = NULL;

mutex_lock(&misc_mtx);

list_for_each_entry(c, &misc_list, list) { //通过minor号来匹配对应的fops函数集
if (c->minor == minor) {
new_fops = fops_get(c->fops);
break;
}
}

if (!new_fops) {
mutex_unlock(&misc_mtx);
request_module("char-major-%d-%d", MISC_MAJOR, minor);
mutex_lock(&misc_mtx);

list_for_each_entry(c, &misc_list, list) {
if (c->minor == minor) {
new_fops = fops_get(c->fops);
break;
}
}
if (!new_fops)
goto fail;
}

err = 0;
old_fops = file->f_op;
file->f_op = new_fops; //重定向fops函数
if (file->f_op->open) { //打开设备
err=file->f_op->open(inode,file);
if (err) {
fops_put(file->f_op);
file->f_op = fops_get(old_fops);
}
}
fops_put(old_fops);
fail:
mutex_unlock(&misc_mtx);
return err;
}

linux
相关资讯       Linux 字符设备 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款