misc设备
WatchDog Timer驱动
混杂设备
Misc(或miscellaneous)驱动是一些拥有着共同特性的简单字符设备驱动。内核抽象出这些特性而形成一些API(在文件drivers/char/misc.c中实现),以简化这些设备驱动程序的初始化。所有的misc设备被分配同一个主设备号MISC_MAJOR(10),但是每一个可以选择一个单独的次设备号。如果一个字符设备驱动要驱动多个设备,那么它就不应该用misc设备来实现。
通常情况下,一个字符设备都不得不在初始化的过程中进行下面的步骤:
通过alloc_chrdev_region()分配主/次设备号。
使用cdev_init()和cdev_add()来以一个字符设备注册自己。
而一个misc驱动,则可以只用一个调用misc_register()来完成这所有的步骤。
所有的miscdevice设备形成一个链表,对设备访问时,内核根据次设备号查找对应的miscdevice设备,然后调用其file_operations中注册的文件操作方法进行操作。
在Linux内核中,使用struct miscdevice来表示miscdevice。这个结构体的定义为:
struct miscdevice {
int minor;
const char *name;
const struct file_operations *fops;
struct list_head list;
struct device *parent;
struct device *this_device;
const char *nodename;
mode_t mode;
};
minor是这个混杂设备的次设备号,若由系统自动配置,则可以设置为MISC_DYNANIC_MINOR,name是设备名。
每一个misc驱动会自动出现在/sys/class/misc下,而不需要驱动程序作者明确的去做。Linux watchdog timer驱动被实现为misc 驱动,他们被放在drivers/char/watchdog/目录下。Watchdog 驱动也导出了一个标准设备接口到用户空间。这样就可以使符合这个接口的应用程序的实现独立于Watchdog硬件。这个API在内核树中的Documentation/watchdog/watchdog-api.txt文件中有详细的说明。
在Linux驱动中把无法归类的五花八门的设备定义为混杂设备(用miscdevice结构体表述)。miscdevice共享一个主设备号MISC_MAJOR(即10),但次设备号不同。 所有的miscdevice设备形成了一个链表,对设备访问时内核根据次设备号查找对应的miscdevice设备,然后调用其file_operations结构中注册的文件操作接口进行操作。 在内核中用struct miscdevice表示miscdevice设备,然后调用其file_operations结构中注册的文件操作接口进行操作。miscdevice的API实现在drivers/char/misc.c中。
下边是描述这个设备的结构体:
- struct miscdevice {
- int minor; //次设备号
- const char *name; //设备的名称
- const struct file_operations *fops; //文件操作
- struct list_head list; //misc_list的链表头
- struct device *parent; //父设备(Linux设备模型中的东东了,哈哈)
- struct device *this_device; //当前设备,是device_create的返回值,下边会看到
- };
然后来看看misc子系统的初始化函数:
- static int __init misc_init(void)
- {
- int err;
- #ifdef CONFIG_PROC_FS
- /*创建一个proc入口项*/
- proc_create("misc", 0, NULL, &misc_proc_fops);
- #endif
- /*在/sys/class/目录下创建一个名为misc的类*/
- misc_class = class_create(THIS_MODULE, "misc");
- err = PTR_ERR(misc_class);
- if (IS_ERR(misc_class))
- goto fail_remove;
- err = -EIO;
- /*注册设备,其中设备的主设备号为MISC_MAJOR,为10。设备名为misc,misc_fops是操作函数的集合*/
- if (register_chrdev(MISC_MAJOR,"misc",&misc_fops))
- goto fail_printk;
- return 0;
- fail_printk:
- printk("unable to get major %d for misc devices/n", MISC_MAJOR);
- class_destroy(misc_class);
- fail_remove:
- remove_proc_entry("misc", NULL);
- return err;
- }
- /*misc作为一个子系统被注册到linux内核中*/
- subsys_initcall(misc_init);
下边是register_chrdev函数的实现:
- int register_chrdev(unsigned int major, const char *name,
- const struct file_operations *fops)
- {
- struct char_device_struct *cd;
- struct cdev *cdev;
- char *s;
- int err = -ENOMEM;
- /*主设备号是10,次设备号为从0开始,分配256个设备*/
- cd = __register_chrdev_region(major, 0, 256, name);
- if (IS_ERR(cd))
- return PTR_ERR(cd);
- /*分配字符设备*/
- cdev = cdev_alloc();
- if (!cdev)
- goto out2;
- cdev->owner = fops->owner;
- cdev->ops = fops;
- /*Linux设备模型中的,设置kobject的名字*/
- kobject_set_name(&cdev->kobj, "%s", name);
- for (s = strchr(kobject_name(&cdev->kobj),'/'); s; s = strchr(s, '/'))
- *s = '!';
- /*把这个字符设备注册到系统中*/
- err = cdev_add(cdev, MKDEV(cd->major, 0), 256);
- if (err)
- goto out;
- cd->cdev = cdev;
- return major ? 0 : cd->major;
- out:
- kobject_put(&cdev->kobj);
- out2:
- kfree(__unregister_chrdev_region(cd->major, 0, 256));
- return err;
- }
来看看这个设备的操作函数的集合:
- static const struct file_operations misc_fops = {
- .owner = THIS_MODULE,
- .open = misc_open,
- };
可以看到这里只有一个打开函数,用户打开miscdevice设备是通过主设备号对应的打开函数,在这个函数中找到次设备号对应的相应的具体设备的open函数。它的实现如下:
- 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;
- lock_kernel();
- mutex_lock(&misc_mtx);
- /*找到次设备号对应的操作函数集合,让new_fops指向这个具体设备的操作函数集合*/
- list_for_each_entry(c, &misc_list, list) {
- 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);
- /*重新遍历misc_list链表,如果没有找到就退出,否则让new_fops指向这个具体设备的操作函数集合*/
- 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;
- 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);
- unlock_kernel();
- return err;
- }
再来看看misc子系统对外提供的两个重要的API,misc_register,misc_deregister:
- int misc_register(struct miscdevice * misc)
- {
- struct miscdevice *c;
- dev_t dev;
- int err = 0;
- /*初始化misc_list链表*/
- INIT_LIST_HEAD(&misc->list);
- mutex_lock(&misc_mtx);
- /*遍历misc_list链表,看这个次设备号以前有没有被用过,如果次设备号已被占有则退出*/
- list_for_each_entry(c, &misc_list, list) {
- if (c->minor == misc->minor) {
- mutex_unlock(&misc_mtx);
- return -EBUSY;
- }
- }
- /*看是否是需要动态分配次设备号*/
- if (misc->minor == MISC_DYNAMIC_MINOR) {
- /*
- *#define DYNAMIC_MINORS 64 /* like dynamic majors */
- *static unsigned char misc_minors[DYNAMIC_MINORS / 8];
- *这里存在一个次设备号的位图,一共64位。下边是遍历每一位,
- *如果这位为0,表示没有被占有,可以使用,为1表示被占用。
- */
- int i = DYNAMIC_MINORS;
- while (--i >= 0)
- if ( (misc_minors[i>>3] & (1 << (i&7))) == 0)
- break;
- if (i<0) {
- mutex_unlock(&misc_mtx);
- return -EBUSY;
- }
- /*得到这个次设备号*/
- misc->minor = i;
- }
- /*设置位图中相应位为1*/
- if (misc->minor < DYNAMIC_MINORS)
- misc_minors[misc->minor >> 3] |= 1 << (misc->minor & 7);
- /*计算出设备号*/
- dev = MKDEV(MISC_MAJOR, misc->minor);
- /*在/dev下创建设备节点,这就是有些驱动程序没有显式调用device_create,却出现了设备节点的原因*/
- misc->this_device = device_create(misc_class, misc->parent, dev, NULL,
- "%s", misc->name);
- if (IS_ERR(misc->this_device)) {
- err = PTR_ERR(misc->this_device);
- goto out;
- }
- /*
- * Add it to the front, so that later devices can "override"
- * earlier defaults
- */
- /*将这个miscdevice添加到misc_list链表中*/
- list_add(&misc->list, &misc_list);
- out:
- mutex_unlock(&misc_mtx);
- return err;
- }
这个是miscdevice的卸载函数:
- int misc_deregister(struct miscdevice *misc)
- {
- int i = misc->minor;
- if (list_empty(&misc->list))
- return -EINVAL;
- mutex_lock(&misc_mtx);
- /*在misc_list链表中删除miscdevice设备*/
- list_del(&misc->list);
- /*删除设备节点*/
- device_destroy(misc_class, MKDEV(MISC_MAJOR, misc->minor));
- if (i < DYNAMIC_MINORS && i>0) {
- /*释放位图相应位*/
- misc_minors[i>>3] &= ~(1 << (misc->minor & 7));
- }
- mutex_unlock(&misc_mtx);
- return 0;
- }
misc设备的更多相关文章
- 手把手教你从零实现Linux misc设备驱动一(基于友善之臂4412开发板)
关于怎样来写一个misc设备,在前面有篇文章已经介绍了大致的流程,如今就让我们来实现一个最简单的misc设备驱动. http://blog.csdn.net/morixinguan/article/d ...
- 【linux驱动分析】misc设备驱动
misc设备驱动.又称混杂设备驱动. misc设备驱动共享一个设备驱动号MISC_MAJOR.它在include\linux\major.h中定义: #define MISC_MAJO ...
- 跟着内核学框架-从misc子系统到3+2+1设备识别驱动框架
misc子系统在Linux中是一个非常简单的子系统,但是其清晰的框架结构非常适合用来研究设备识别模型.本文从misc子系统的使用出发,通过了解其机制来总结一套的设备识别的驱动框架,即使用使用同一个驱动 ...
- Linux驱动框架之misc类设备驱动框架
1.何为misc设备 (1)misc中文名就是杂项设备\杂散设备,因为现在的硬件设备多种多样,有好些设备不好对他们进行一个单独的分类,所以就将这些设备全部归属于 杂散设备,也就是misc设备,例如像a ...
- misc类设备
何为misc (1)中文名:杂项设备\杂散设备,它是一种典型的字符设,在一般情况下在内核中,所有的misc设备的主设备号是固定的,为10,它们的次设备号不一样:(2)可以在根文件系统中看到:/sys/ ...
- linux之misc及使用misc创建字符设备
1:linux字符设备及udev 1.1字符设备 字符设备就是:一个一个字节来进行访问的,不能对字符设备进行随机读写.简单字符设备创建实例如下: #include <linux/module.h ...
- 设备驱动基础学习--misc device简单实现
在Linux驱动中把无法归类的五花八门的设备定义为混杂设备(用miscdevice结构体表述).miscdevice共享一个主设备号MISC_MAJOR(即10),但次设备号不同. 所有的miscde ...
- linux 字符设备驱动写法
字符设备,块设备书 一.register_chrdev_region, register_chrdev, misc_register misc device(杂项设备) 在 Linux 内核的incl ...
- i2c总线,设备,驱动之间的关系
------ 总线上先添加好所有具体驱动,i2c.c遍历i2c_boardinfo链表,依次建立i2c_client, 并对每一个i2c_client与所有这个线上的驱动匹配,匹配上,就调用这个驱动的 ...
随机推荐
- hdwiki中model模块的应用
control中调用model原则是这样的,如果你的这个model在本control中大部分方法中都要用到,那么,就写在构造函数里面.例如,名字为doc的control的构造函数如下: functio ...
- ACM题目————二叉树的遍历
一.二叉树的后序遍历: 题目描述 给定一颗二叉树,要求输出二叉树的深度以及后序遍历二叉树得到的序列.本题假设二叉树的结点数不超过1000 输入 输 入数据分为多组,第一行是测试数据的组数n,下面的n行 ...
- ACM题目————马拦过河卒
题目描述 棋盘上A点有一个过河卒,需要走到目标B点.卒行走的规则:可以向下.或者向右.同时在棋盘上C点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点.因此称之为“马拦过河卒”. ...
- Android本机号码及Sim卡状态的获取
SIM卡存储的数据可分为四类:第一类是固定存放的数据.这类数据在移动电话机被出售之前由SIM卡中心写入,包括国际移动用户识别号(IMSI).鉴权密钥(KI).鉴权和加密算法等等.第二类是暂时存放的有关 ...
- The Blacksmith学习的相关资源
1.Unity官网的Blacksmith主页 https://unity3d.com/pages/the-blacksmith 2.WRINKLE MAPS IN THE BLACKSMITH 褶皱贴 ...
- AIM Tech Round 3 (Div. 2) B
Description Vasya takes part in the orienteering competition. There are n checkpoints located along ...
- 【Java】hashcode()和equals()
大家知道,在集合中判断集合中的两个元素是否相同,依赖的是hashcode()和equals()两个方法. > 一个简单的实验 public class Teacher { private Int ...
- ActiveMQ点对点的消息发送案例
公司最近会用MQ对某些业务进行处理,所以,这次我下载了apache-activemq-5.12.0-bin把玩下. 基于练习方便需要,使用Windows的版本. 参考的优秀文章: activemq的几 ...
- JAVA的名词释义
JDK : Java Development Toolkit (java 开发工具包). JDK是整个JAVA的核心,包括了java运行环境(Java Runtime Envirnmet),一堆jav ...
- centos7 安装 mariadb 的正确命令
使用的是linode的centos7系统,安装mysql发现已经默认的是mariadb. 但是不管是使用linode官网说明还是百度搜索到的的根本安装方法无法安装成功. 总是提示这一句: ERROR ...