20150226 IMX257 混杂设备miscdevice驱动程序

20150226 IMX257 混杂设备miscdevice驱动程序

2015-02-26 16:00 李海沿

在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。设备名为miscmisc_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链表*/  
  7.     INIT_LIST_HEAD(&misc->list);   
  8.     mutex_lock(&misc_mtx);   
  9.     /*遍历misc_list链表,看这个次设备号以前有没有被用过,如果次设备号已被占有则退出*/  
  10.     list_for_each_entry(c, &misc_list, list) {   
  11.         if (c->minor == misc->minor) {   
  12.             mutex_unlock(&misc_mtx);   
  13.             return -EBUSY;   
  14.         }   
  15.     }   
  16.     /*看是否是需要动态分配次设备号*/  
  17.     if (misc->minor == MISC_DYNAMIC_MINOR) {  
  18.         /* 
  19.          *#define DYNAMIC_MINORS 64 /* like dynamic majors */  
  20.          *static unsigned char misc_minors[DYNAMIC_MINORS / 8];   
  21.          *这里存在一个次设备号的位图,一共64位。下边是遍历每一位,  
  22.          *如果这位为0,表示没有被占有,可以使用,为1表示被占用。         
  23.          */  
  24.         int i = DYNAMIC_MINORS;   
  25.         while (--i >= 0)   
  26.             if ( (misc_minors[i>>3] & (1 << (i&7))) == 0)   
  27.                 break;   
  28.         if (i<0) {   
  29.             mutex_unlock(&misc_mtx);   
  30.             return -EBUSY;   
  31.         }   
  32.         /*得到这个次设备号*/  
  33.         misc->minor = i;                                           
  34.     }   
  35.     /*设置位图中相应位为1*/  
  36.     if (misc->minor < DYNAMIC_MINORS)   
  37.         misc_minors[misc->minor >> 3] |= 1 << (misc->minor & 7);   
  38.     /*计算出设备号*/  
  39.     dev = MKDEV(MISC_MAJOR, misc->minor);   
  40.     /*/dev下创建设备节点,这就是有些驱动程序没有显式调用device_create,却出现了设备节点的原因*/  
  41.     misc->this_device = device_create(misc_class, misc->parent, dev, NULL,   
  42.                       "%s", misc->name);   
  43.     if (IS_ERR(misc->this_device)) {   
  44.         err = PTR_ERR(misc->this_device);   
  45.         goto out;   
  46.     }   
  47.      
  48.     /*  
  49.      * Add it to the front, so that later devices can "override"  
  50.      * earlier defaults  
  51.      */   
  52.     /*将这个miscdevice添加到misc_list链表中*/  
  53.     list_add(&misc->list, &misc_list);   
  54.  out:   
  55.     mutex_unlock(&misc_mtx);   
  56.     return err;   
  57. }   

misc_register:

匹配次设备号->找到一个没有占用的次设备号(如果需要动态分配的话)->计算设号->创建设备文-

miscdevice结构体添加到misc_list链表中。

4. misc子卸载函数

  1. int misc_deregister(struct miscdevice *misc)   
  2. {   
  3.     int i = misc->minor;   
  4.      
  5.     if (list_empty(&misc->list))   
  6.         return -EINVAL;   
  7.      
  8.     mutex_lock(&misc_mtx);   
  9.     /*misc_list链表中删除miscdevice设备*/  
  10.     list_del(&misc->list);     
  11.     /*删除设备节点*/                            
  12.     device_destroy(misc_class, MKDEV(MISC_MAJOR, misc->minor));             
  13.     if (i < DYNAMIC_MINORS && i>0) {  
  14.         /*释放位图相应位*/   
  15.         misc_minors[i>>3] &= ~(1 << (misc->minor & 7));   
  16.     }   
  17.     mutex_unlock(&misc_mtx);   
  18.     return 0;   
  19. }   

misc_deregister:

mist_list中删除miscdevice->删除设备文件->位图位清零。

二、代码分析

1. 包含头文件:

#include <linux/miscdevice.h>

2. 定义混杂设备结构体以及实现相关的file_operation函数

3. 最后分别在init函数和exit函数中卸载

4. 编译测试

我们使用 ll /dev/key_misc 查看 其 设备的详细信息 可以发现其主设备号为10 次设备号110

然后 cat /proc/misc 查看当前混杂设备列表,发现我们的key_misc 110 当然还设有gpio dma等都使用混杂设备

 本文部分知识点摘自 http://tomhibolu.iteye.com/blog/1214940

附驱动程序代码:

 1 /******************************
 2     misc device
 3  *****************************/
 4 #include <linux/module.h>
 5 #include <linux/init.h>
 6 #include <linux/kernel.h>
 7 #include <linux/delay.h>
 8 #include <linux/types.h>
 9 #include <linux/ioctl.h>
10 #include <linux/gpio.h>
11 #include <linux/fs.h>
12 #include <linux/device.h>
13 #include <linux/miscdevice.h>
14 
15 #define Driver_NAME "key_misc"
16 #define Driver_minor 110
17 
18 /* 应用程序对设备文件/dev/key_query执行open(...)时,
19  * 就会调用key_open函数*/
20 static int key_open(struct inode *inode, struct file *file){
21     printk("<0>function open!
");    
22     return 0;
23 }
24 static int key_read(struct file *filp, char __user *buff, size_t count, loff_t *offp){
25     printk("<0>function read!
");    
26     return 0;
27 }
28 static ssize_t key_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos){
29     printk("<0>function write!
");    
30     return 1;
31 }
32 
33 /* 这个结构是字符设备驱动程序的核心
34  * 当应用程序操作设备文件时所调用的open、read、write等函数,
35  * 最终会调用这个结构中指定的对应函数
36  */
37 static struct file_operations key_fops = {
38     .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
39     .open   =   key_open,     
40     .read    =    key_read,       
41     .write    =    key_write,       
42 };
43 
44 /*混杂设备结构体*/
45 static struct miscdevice key_misc = {
46     .minor = Driver_minor,            //次设备号
47     .name = Driver_NAME,        //混杂设备名字
48     .fops = &key_fops,        //操作指针
49 };
50 
51 /*
52  * 执行insmod命令时就会调用这个函数 
53  */
54 static int __init key_init(void)
55 {
56     printk("<0>
Hello,this is %s module!

",Driver_NAME);
57     
58     misc_register(&key_misc);
59     return 0;
60 }
61 
62 /*
63  * 执行rmmod命令时就会调用这个函数 
64  */
65 static void __exit key_exit(void)
66 {
67     printk("<0>
Goodbye,%s!

",Driver_NAME);
68     
69     misc_register(&key_misc);
70 }
71 
72 /* 这两行指定驱动程序的初始化函数和卸载函数 */
73 module_init(key_init);
74 module_exit(key_exit);
75 
76 /* 描述驱动程序的一些信息,不是必须的 */
77 MODULE_AUTHOR("Lover雪");
78 MODULE_VERSION("0.1.0");
79 MODULE_DESCRIPTION("IMX257 key Driver");
80 MODULE_LICENSE("GPL");
View Code

注: 程序中有一个错误: 在exit函数中应该为  misc_unregister ,此处写错了,大家记得改过来 

原文地址:https://www.cnblogs.com/lihaiyan/p/4301579.html