class_create() & device_create

本章节将介绍 class_create class_destroy 使用
========================================================================
struct class 定义在#include <linux/device.h>

1.宏class_create()
文件包含:#include<linux/device.h>

宏定义: 
   在内核源码中位置:2.6.35-src/include/linux/device.h
  1. 310    #define class_create(owner, name) \
  2.  311   ({ \
  3.  312          static struct lock_class_key __key; \
  4.  313          __class_create(owner, name, &__key); \
  5.  314   })
功能描述:
    宏class_create()用于动态创建设备的逻辑类,并完成部分字段的初始化,然后将其添加进linux内核系统中。此函数的执行效果就是在目录/sys/class/下创建一个新的文件夹,此文件夹的名字为此函数的第二个输入参数,单词文件夹是空的。宏class_create()在实现时,调用了函数__class_create(),作用和函数__class_create()基本相同。

输入参数说明:
    宏class_create()有两个输入参数
    宏owner是一个struct module结构体类型的指针,指向函数__class_create()即将创建的struct class类型对象的拥有者,一般赋值为THIS_MODULE,详细查看:src/include/linux/module.h
    参数name是char类型的指针,代表即将创建的struct class变量的名字

返回参数
    宏class_create()函数返回 struct class的逻辑类

2.函数class_destroy()
文件包含:#include <linux/device.h>

函数定义:
    函数实现: src/drivers/base/class.c
    函数定义格式: void class_destroy(struct class *cls);

==================================================================    
程序:简单在内核中建立自己的动态设备逻辑类

附件代码: class_create_destroy.rar   将rar修改为tar.bz2
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>

  4. #include <linux/device.h>//class_create

  5. struct class *mem_class;

  6. static int __init class_create_destroy_init(void)
  7. {
  8.     printk("class_create_destroy_init init..\n");
  9.     mem_class = class_create(THIS_MODULE,"ywx_dev");
  10.     if(IS_ERR(mem_class))
  11.     {
  12.         printk("err:failed create class\n");
  13.         return -1;
  14.     }
  15.     printk("class create suceess\n");
  16.     return 0;
  17. }

  18. static void __exit class_create_destroy_exit(void)
  19. {
  20.     printk("leaving class...\n");
  21.     class_destroy(mem_class);
  22.     printk("leaving finished.\n");
  23. }

  24. MODULE_LICENSE("GPL");
  25. module_init(class_create_destroy_init);
  26. module_exit(class_create_destroy_exit);
使用方法:
  1. root@ywx:/home/ywx/desktop/module/api_/class_create_destroy# insmod ./class_create_destroy.ko 
  2. root@ywx:/home/ywx/desktop/module/api_/class_create_destroy# dmesg | tail -8
  3. [ 2360.949466] class_create_destroy_init init..
  4. [ 2360.949956] class create suceess
  5. //在/sys/class/下生成我们自己建立的设备的逻辑类
  6. root@ywx:/home/ywx/desktop/module/api_/class_create_destroy# ls /sys/class | grep ywx
  7. ywx_dev
  8. root@ywx:/home/ywx/desktop/module/api_/class_create_destroy# rmmod class_create_destroy 
  9. root@ywx:/home/ywx/desktop/module/api_/class_create_destroy# dmesg | tail -8
  10. [ 2360.949466] class_create_destroy_init init..
  11. [ 2360.949956] class create suceess
  12. [ 2431.145922] leaving class...
  13. [ 2431.149030] leaving finished.
  14. root@ywx:/home/ywx/desktop/module/api_/class_create_destroy# ls /sys/class | grep ywx
  15. root@ywx:/home/ywx/desktop/module/api_/class_create_destroy#


前面章节“class 类 class_create class_destroy”了解了 class_create使用,这章节中介绍
device_create device_destroy使用,并通过这两章节内容 自动建立备文件节点
========================================================================

1. device_create()
文件包含: #include <linux/device.h>

函数定义:
     函数位置: src/drivers/base/core.c
     函数格式:
  1. extern struct device *device_create(struct class *cls, struct device *parent,dev_t devt, void *drvdataconst char *fmt, ...)
函数功能:
    函数device_create()用于动态的建立逻辑设备,并对新的逻辑设备类进行相应初始化,将其与函数的第一个参数所代表的逻辑类关联起来,然后将此逻辑设备加到linux内核系统的设备驱动程序模型中。函数能够自动在/sys/devices/virtual目录下创建新的逻辑设备目录,在/dev目录下创建于逻辑类对应的设备文件

参数说明:
   struct class cls:与即将创建额逻辑设备相关的逻辑类,在“class类 class_create使用”说明了。
   dev_t dev:设备号
   void *drvdata: void类型的指针,代表回调函数的输入参数
   const char *fmt: 逻辑设备的设备名,即在目录 /sys/devices/virtual创建的逻辑设备目录的目录名。

2.函数device_destroy()
文件包含:#include <linux/device.h>

函数定义: src/drivers/base/core.c
           void device_destroy(struct class *dev, dev_t devt);

函数功能: 
    函数device_destroy()用于从linux内核系统设备驱动程序模型中移除一个设备,并删除/sys/devices/virtual目录下对应的设备目录及/dev/目录下对应的设备文件

======================================================================

class_create() 和 device_create()有什么关系呢??

在刚开始写Linux设备驱动程序的时候,很多时候都是利用mknod命令手动创建设备节点,实际上Linux内核为我们提供了一组函数,可以用来在模块加载的时候自动在/dev目录下创建相应设备节点,并在卸载模块时删除该节点,当然前提条件是用户空间移植了udev。什么事udev??请查看“  收集 3个介绍uedv 网址资料    ”

内核中定义了struct class结构体,顾名思义,一个struct class结构体类型变量对应一个类,内核同时提供了class_create(…)函数,可以用它来创建一个类,这个类存放于sysfs下面,一旦创建好了这个类,再调用device_create(…)函数来在/dev目录下创建相应的设备节点。这样,加载模块的时候,用户空间中的udev会自动响应device_create(…)函数,去/sysfs下寻找对应的类从而创建设备节点。

在下面的例子中具体说明关系!!!!!!!!

1. 首先通过  mem_class = class_create(THIS_MODULE,"ywx_class_char");语句建立了逻辑设备类,在 /sys/class/ 下新建了 ywx_class_char目录 查看:
  1. ywx@ywx:/sys/class$ ls ywx_class_char
  2. ywx_device_char
2. 通过语句 device_create(mem_class,NULL,MKDEV(MEM_MAJOR,MEM_MINOR),NULL,"ywx_device_char");阿在 /dev/ 下自动建立了 ywx_device_char 设备节点 ,具体查看:
  1. ywx@ywx:/dev$ ls -l ywx_device_char 
  2. crw------- 1 root root 240, 0 2012-01-03 15:40 ywx_device_char
3.那么 class_create 和 device_create还有什么关系呢??

在/sys/devices/virtual/建立了 “ywx_class_char”
在/sys/devices/virtual/ywx/class/char/下建立了 ywx_device_char 目录
  1. ywx@ywx:/sys/devices/virtual/ywx_class_char/ywx_device_char$ ls -la
  2. total 0
  3. drwxr-xr-x 3 root root 0 2012-01-03 15:40 .
  4. drwxr-xr-x 3 root root 0 2012-01-03 15:40 ..
  5. -r--r--r-- 1 root root 4096 2012-01-03 16:10 dev 这个就是我们在驱动中分配的大小4KB
  6. drwxr-xr-x 2 root root 0 2012-01-03 16:10 power
  7. lrwxrwxrwx 1 root root 0 2012-01-03 15:40 subsystem -> ../../../../class/ywx_class_char符号链接设备
  8. -rw-r--r-- 1 root root 4096 2012-01-03 15:40 uevent
======================================================================

drivers/i2c/i2c-dev.c 也有关于 class_create的例子
====================================================================
自己的字符设备驱动程序
序代码附加: cdev_init_add_del.rar   将rar修改为tar.bz2
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/init.h>

  4. #include <linux/cdev.h>
  5. #include <linux/device.h>
  6. #include <linux/slab.h>//kmalloc
  7. #include <linux/vmalloc.h>//vmalloc()
  8. #include <linux/types.h>//ssize_t
  9. #include <linux/fs.h>//file_operaiotns
  10. #include <linux/uaccess.h>//copy_from_user

  11. #define MEM_MALLOC_SIZE 4096 ////缓冲区大小
  12. #define MEM_MAJOR    240 ////主设备号
  13. #define MEM_MINOR    0

  14. char *mem_spvm = NULL; ////缓冲区指针,指向内存区
  15. struct cdev *mem_cdev = NULL; //字符设备对象指针
  16. struct class *mem_class = NULL; //设备类指针

  17. static int __init mem_init(void);
  18. static void __exit mem_exit(void);
  19. static int mem_open(struct inode *inode,struct file *filp);
  20. static int mem_release(struct inode *inode, struct file *filp);
  21. static ssize_t mem_read(struct file *filp,char __user *buf,size_t count,loff_t *fpos);
  22. static ssize_t mem_write(struct file *filp, char __user *buf,size_t count ,loff_t *fops);

  23. static const struct file_operations mem_fops={
  24.     .owner = THIS_MODULE,
  25.     .open = mem_open,
  26.     .release = mem_release,
  27.     .read = mem_read,
  28.     .write = mem_write,
  29. };

  30. static int __init mem_init(void)
  31. {
  32.     int ret;
  33.     //创建设备号 主次设备号
  34.     int devno = MKDEV(MEM_MAJOR,MEM_MINOR);
  35.     printk("mem_init initial...\n");

  36.     //开辟内核内存缓冲区
  37.     mem_spvm = (char *)vmalloc(MEM_MALLOC_SIZE);
  38.     if(mem_spvm == NULL)
  39.     {
  40.         printk("vmalloc mem_spvm error\n");
  41.         return -ENOMEM;//
  42.     }
  43.     
  44.     //
  45.     mem_cdev = cdev_alloc();
  46.     if(mem_cdev == NULL)
  47.     {
  48.         printk("cdev_alloc error\n");
  49.         return -ENOMEM;
  50.     }
  51.     cdev_init(mem_cdev,&mem_fops);
  52.     mem_cdev->owner = THIS_MODULE;
  53.     ret = cdev_add(mem_cdev,devno,1);//将字符设备键入内核系统
  54.     if(ret)
  55.     {
  56.         cdev_del(mem_cdev);
  57.         mem_cdev = NULL;
  58.         printk("cdev_add error\n");
  59.         return -1;
  60.     }

  61.     //
  62.     mem_class = class_create(THIS_MODULE,"ywx_class_char");
  63.     if(IS_ERR(mem_class))
  64.     {
  65.         printk("class_create error..\n");
  66.         return -1;
  67.     }
  68.     device_create(mem_class,NULL,MKDEV(MEM_MAJOR,MEM_MINOR),NULL,"ywx_device_char");
  69.     
  70.     printk("init finished..\n");
  71.     return 0;
  72. }

  73. static void __exit mem_exit(void)
  74. {
  75.     printk("mem_exit starting..\n");
  76.     if(mem_cdev != NULL)
  77.         cdev_del(mem_cdev);
  78.     printk("cdev_del ok\n");

  79.     device_destroy(mem_class,MKDEV(MEM_MAJOR,MEM_MINOR));
  80.     class_destroy(mem_class);

  81.     if(mem_spvm != NULL)
  82.         vfree(mem_spvm);

  83.     printk("vfree ok\n");
  84.     printk("mem_exit finished..\n");
  85. }

  86. static int mem_open(struct inode *inode,struct file *filp)
  87. {
  88.     printk("open vmalloc space..\n");
  89.     try_module_get(THIS_MODULE);//模块引用计数器自加
  90.     printk("open vamlloc space ok..\n");
  91.     return 0;
  92. }
  93. static int mem_release(struct inode *inode, struct file *filp)
  94. {
  95.     printk("close vmalloc space..\n");
  96.     module_put(THIS_MODULE);//模块引用计数器自减
  97.     return 0;
  98. }
  99. static ssize_t mem_read(struct file *filp,char __user *buf,size_t count,loff_t *fpos)
  100. {
  101.     int ret = -1;
  102.     char *tmp;
  103.     printk("copy data to the user space\n");
  104.     tmp = mem_spvm;
  105.     if(count > MEM_MALLOC_SIZE)
  106.         count = MEM_MALLOC_SIZE;
  107.     if(tmp != NULL)//将内核数据写入到用户空间
  108.         ret = copy_to_user(buf,tmp,count);
  109.     if(ret == 0)
  110.     {
  111.         printk("read copy data success\n");
  112.         return count;
  113.     }
  114.     else
  115.     {
  116.         printk("read copy data error\n");
  117.         return 0;
  118.     }
  119. }
  120. static ssize_t mem_write(struct file *filp, char __user *buf,size_t count ,loff_t *fops)
  121. {
  122.     int ret = -1;
  123.     char *tmp;
  124.     printk("read data from the user space.\n");
  125.     tmp = mem_spvm;
  126.     if(count > MEM_MALLOC_SIZE)
  127.         count = MEM_MALLOC_SIZE;
  128.     if(tmp != NULL)
  129.         ret = copy_from_user(tmp,buf,count);
  130.     if(ret == 0)
  131.     {
  132.         printk("write copy data success.\n");
  133.         return count;
  134.     }
  135.     else
  136.     {
  137.         printk("write copy data error.\n");
  138.         return 0;    
  139.     }
  140. }

  141. MODULE_LICENSE("GPL");
  142. module_init(mem_init);
  143. module_exit(mem_exit);
应用程序:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <string.h>//memset()

  6. int main(int argc, char *argv[])
  7. {
  8.     int fd,cnt;
  9.     char buf[256];
  10.     int i;
  11.     printf("char device testing..\n");
  12.     fd = open("/dev/ywx_device_char",O_RDWR);
  13.     if(fd == 0)
  14.     {
  15.         printf("open failed.\n");
  16.         return 1;
  17.     }
  18.     
  19.     printf("input the data for kernel:");
  20.     scanf("%s",buf);
  21.     cnt = write(fd,buf,256);
  22.     if(cnt == 0)
  23.         printf("write error\n");
  24.     
  25.     printf("clear buf,and will read from kernel...\n");
  26.     for(i=0;i<256;i++)
  27.         buf[i] = 32;//32 =" "

  28.     cnt = read(fd,buf,256);
  29.     if(cnt > 0)
  30.         printf("read data from kernel is:%s\n",buf);
  31.     else
  32.         printf("read data error\n");
  33.     close(fd);
  34.     printf("close app..\n");
  35.     return 0;
  36. }
应用程序测试:
  1. root@ywx:/home/ywx/desktop/module/api_/cdev_init_add_del/app# ./app 
  2. char device testing..
  3. input the data for kernel:yuweixian4230.blog.chinaunix.net
  4. clear buf,and will read from kernel...
  5. read data from kernel is:yuweixian4230.blog.chinaunix.net
  6. close app..

一些资料:







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章