IMX257 混杂设备miscdevice驱动程序

发布者:science56最新更新时间:2024-08-14 来源: cnblogs关键字:混杂设备  驱动程序 手机看文章 扫描二维码
随时随地手机看文章

在Linux驱动中把无法归类的五花八门的设备定义为混杂设备(用miscdevice结构体表述)。miscdevice共享一个主设备号MISC_MAJOR(即10),但次设备号不同。 所有的miscdevice设备形成了一个链表,对设备访问时内核根据次设备号查找对应的miscdevice设备,然后调用其file_operations结构中注册的文件操作接口进行操作。 在内核中用struct miscdevice表示miscdevice设备,然后调用其file_operations结构中注册的文件操作接口进行操作。miscdevice的API实现在drivers/char/misc.c中。

 

一、混杂设备介绍

1. miscdevice结构体

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的返回值,下边会看到

};

 

2. misc子系统初始化函数

  1. static int __init misc_init(void)   

  2. {   

  3.     int err;   

  4.     

  5. #ifdef CONFIG_PROC_FS   

  6.     /*创建一个proc入口项*/  

  7.     proc_create('misc', 0, NULL, &misc_proc_fops);                   

  8. #endif   

  9.     /*在/sys/class/目录下创建一个名为misc的类*/  

  10.     misc_class = class_create(THIS_MODULE, 'misc');   

  11.     err = PTR_ERR(misc_class);   

  12.     if (IS_ERR(misc_class))   

  13.         goto fail_remove;   

  14.      

  15.     err = -EIO;  

  16.     /*注册设备,其中设备的主设备号为MISC_MAJOR,为10。设备名为misc,misc_fops是操作函数的集合*/   

  17.     if (register_chrdev(MISC_MAJOR,'misc',&misc_fops))   

  18.         goto fail_printk;   

  19.     return 0;   

  20.      

  21. fail_printk:   

  22.     printk('unable to get major %d for misc devices/n', MISC_MAJOR);   

  23.     class_destroy(misc_class);   

  24. fail_remove:   

  25.     remove_proc_entry('misc', NULL);   

  26.     return err;   

  27. }   

  28. /*misc作为一个子系统被注册到linux内核中*/  

  29. subsys_initcall(misc_init);   

 

下边是register_chrdev函数的实现:

  1. int register_chrdev(unsigned int major, const char *name,  

  2.             const struct file_operations *fops)  

  3. {  

  4.     struct char_device_struct *cd;  

  5.     struct cdev *cdev;  

  6.     char *s;  

  7.     int err = -ENOMEM;  

  8.     /*主设备号是10,次设备号为从0开始,分配256个设备*/  

  9.     cd = __register_chrdev_region(major, 0, 256, name);  

  10.     if (IS_ERR(cd))  

  11.         return PTR_ERR(cd);  

  12.     /*分配字符设备*/  

  13.     cdev = cdev_alloc();  

  14.     if (!cdev)  

  15.         goto out2;  

  16.     

  17.     cdev->owner = fops->owner;  

  18.     cdev->ops = fops;  

  19.     /*Linux设备模型中的,设置kobject的名字*/  

  20.     kobject_set_name(&cdev->kobj, '%s', name);  

  21.     for (s = strchr(kobject_name(&cdev->kobj),'/'); s; s = strchr(s, '/'))  

  22.         *s = '!';  

  23.     /*把这个字符设备注册到系统中*/     

  24.     err = cdev_add(cdev, MKDEV(cd->major, 0), 256);  

  25.     if (err)  

  26.         goto out;  

  27.     

  28.     cd->cdev = cdev;  

  29.     

  30.     return major ? 0 : cd->major;  

  31. out:  

  32.     kobject_put(&cdev->kobj);  

  33. out2:  

  34.     kfree(__unregister_chrdev_region(cd->major, 0, 256));  

  35.     return err;  

  36. }  

 

来看看这个设备的操作函数的集合:

  1. static const struct file_operations misc_fops = {   

  2.     .owner      = THIS_MODULE,   

  3.     .open       = misc_open,   

  4. };   

可以看到这里只有一个打开函数,用户打开miscdevice设备是通过主设备号对应的打开函数,在这个函数中找到次设备号对应的相应的具体设备的open函数。它的实现如下:

  1. static int misc_open(struct inode * inode, struct file * file)   

  2. {   

  3.     int minor = iminor(inode);   

  4.     struct miscdevice *c;   

  5.     int err = -ENODEV;   

  6.     const struct file_operations *old_fops, *new_fops = NULL;   

  7.         

  8.     lock_kernel();   

  9.     mutex_lock(&misc_mtx);   

  10.     /*找到次设备号对应的操作函数集合,让new_fops指向这个具体设备的操作函数集合*/  

  11.     list_for_each_entry(c, &misc_list, list) {   

  12.         if (c->minor == minor) {   

  13.             new_fops = fops_get(c->fops);           

  14.             break;   

  15.         }   

  16.     }   

  17.              

  18.     if (!new_fops) {   

  19.         mutex_unlock(&misc_mtx);   

  20.         /*如果没有找到,则请求加载这个次设备号对应的模块*/  

  21.         request_module('char-major-%d-%d', MISC_MAJOR, minor);   

  22.         mutex_lock(&misc_mtx);   

  23.         /*重新遍历misc_list链表,如果没有找到就退出,否则让new_fops指向这个具体设备的操作函数集合*/  

  24.         list_for_each_entry(c, &misc_list, list) {   

  25.             if (c->minor == minor) {   

  26.                 new_fops = fops_get(c->fops);   

  27.                 break;   

  28.             }   

  29.         }   

  30.         if (!new_fops)   

  31.             goto fail;   

  32.     }   

  33.      

  34.     err = 0;   

  35.     /*保存旧打开函数的地址*/  

  36.     old_fops = file->f_op;   

  37.     /*让主设备号的操作函数集合指针指向具体设备的操作函数集合*/  

  38.     file->f_op = new_fops;   

  39.     if (file->f_op->open) {  

  40.         /*使用具体设备的打开函数打开设备*/   

  41.         err=file->f_op->open(inode,file);   

  42.         if (err) {   

  43.             fops_put(file->f_op);   

  44.             file->f_op = fops_get(old_fops);   

  45.         }   

  46.     }   

  47.     fops_put(old_fops);   

  48. fail:   

  49.     mutex_unlock(&misc_mtx);   

  50.     unlock_kernel();   

  51.     return err;   

  52. }   

 

3. misc子注册函数

并且会自动生成设备节点

  1. int misc_register(struct miscdevice * misc)   

  2. {   

  3.     struct miscdevice *c;   

  4.     dev_t dev;   

  5.     int err = 0;   

  6.     /*初始化misc_list链表*/  

    [1] [2]
    关键字:混杂设备  驱动程序 引用地址:IMX257 混杂设备miscdevice驱动程序

    上一篇:IMX257 输入子系统
    下一篇:IMX257 总线设备驱动模型编程之平台总线设备platform

    小广播
    最新单片机文章
    何立民专栏 单片机及嵌入式宝典

    北京航空航天大学教授,20余年来致力于单片机与嵌入式系统推广工作。

    厂商技术中心

     
    EEWorld订阅号

     
    EEWorld服务号

     
    汽车开发圈

     
    机器人开发圈

    电子工程世界版权所有 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2026 EEWORLD.com.cn, Inc. All rights reserved