注册字符设备时的 class_create() 与 device_create()

在刚开始写Linux设备驱动程序的时候,很多时候都是利用mknod命令手动创建设备节点,实际上Linux内核为我们提供了一组函数,可以用来在模块加载的时候自动在/dev目录下创建相应设备节点,并在卸载模块时删除该节点,当然前提条件是用户空间移植了udev。

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

注意,在2.6较早的内核版本中,device_create(…)函数名称不同,是class_device_create(…),所以在新的内核中编译以前的模块程序有时会报错,就是因为函数名称不同,而且里面的参数设置也有一些变化。

struct class和device_create(…) 以及device_create(…)都定义在/include/linux/device.h中,使用的时候一定要包含这个头文件,否则编译器会报错。

1. 在2.6.26.6内核版本中,struct class定义在头文件include/linux/device.h中:

  1. /*
  2.       * device classes
  3.       */ 
  4.     struct class
  5.       const char        *name; 
  6.       struct module     *owner; 
  7.  
  8.   nbsp;struct kset         subsys; 
  9.       struct list_head         devices; 
  10.       struct list_head         interfaces; 
  11.       struct kset              class_dirs; 
  12.       struct semaphore sem;    /* locks children, devices, interfaces */ 
  13.       struct class_attribute   *class_attrs; 
  14.       struct device_attribute      *dev_attrs; 
  15.  
  16.   int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env); 
  17.  
  18.   void (*class_release)(struct class *class); 
  19.       void (*dev_release)(struct device *dev); 
  20.  
  21.   int (*suspend)(struct device *dev, pm_message_t state); 
  22.       int (*resume)(struct device *dev); 
  23.  
  24. }; 
/*
      * device classes
      */
    struct class {
      const char        *name;
      struct module     *owner;

  nbsp;struct kset         subsys;
      struct list_head         devices;
      struct list_head         interfaces;
      struct kset              class_dirs;
      struct semaphore sem;    /* locks children, devices, interfaces */
      struct class_attribute   *class_attrs;
      struct device_attribute      *dev_attrs;

  int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);

  void (*class_release)(struct class *class);
      void (*dev_release)(struct device *dev);

  int (*suspend)(struct device *dev, pm_message_t state);
      int (*resume)(struct device *dev);

};

2. class_create(…)在/drivers/base/class.c中实现:
  1.    /**
  2.   * class_create - create a struct class structure
  3.   * @owner: pointer to the module that is to "own" this struct class
  4.   * @name: pointer to a string for the name of this class.
  5.   *
  6.   * This is used to create a struct class pointer that can then be used
  7.   * in calls to device_create().
  8.   *
  9.   * Note, the pointer created here is to be destroyed when finished by
  10.   * making a call to class_destroy().
  11.   */ 
  12. struct class *class_create(struct module *owner, const char *name) 
  13.     struct class *cls; 
  14.     int retval; 
  15.     cls = kzalloc(sizeof(*cls), GFP_KERNEL); 
  16.     if (!cls) { 
  17.          retval = -ENOMEM; 
  18.          goto error; 
  19.     } 
  20.  
  21. cls->name = name; 
  22.     cls->owner = owner; 
  23.     cls->class_release = class_create_release; 
  24.  
  25. retval = class_register(cls); 
  26.     if (retval) 
  27.          goto error; 
  28.  
  29. return cls; 
  30.  
  31. ror: 
  32.     kfree(cls); 
  33.     return ERR_PTR(retval); 
  34.   } 
     /**
    * class_create - create a struct class structure
    * @owner: pointer to the module that is to "own" this struct class
    * @name: pointer to a string for the name of this class.
    *
    * This is used to create a struct class pointer that can then be used
    * in calls to device_create().
    *
    * Note, the pointer created here is to be destroyed when finished by
    * making a call to class_destroy().
    */
   struct class *class_create(struct module *owner, const char *name)
   {
      struct class *cls;
      int retval;
      cls = kzalloc(sizeof(*cls), GFP_KERNEL);
      if (!cls) {
           retval = -ENOMEM;
           goto error;
      }

  cls->name = name;
      cls->owner = owner;
      cls->class_release = class_create_release;

  retval = class_register(cls);
      if (retval)
           goto error;

  return cls;

error:
      kfree(cls);
      return ERR_PTR(retval);
    }

    第一个参数指定类的所有者是哪个模块,第二个参数指定类名。

    在class.c中,还定义了class_destroy(…)函数,用于在模块卸载时删除类。

3. device_create(…)函数在/drivers/base/core.c中实现:

  1. /**
  2. * device_create - creates a device and registers it with sysfs
  3. * @class: pointer to the struct class that this device should be registered to
  4. * @parent: pointer to the parent struct device of this new device, if any
  5. * @devt: the dev_t for the char device to be added
  6. * @fmt: string for the device's name
  7. *
  8. * This function can be used by char device classes. A struct device
  9. * will be created in sysfs, registered to the specified class.
  10. *
  11. * A "dev" file will be created, showing the dev_t for the device, if
  12. * the dev_t is not 0,0.
  13. * If a pointer to a parent struct device is passed in, the newly created
  14. * struct device will be a child of that device in sysfs.
  15. * The pointer to the struct device will be returned from the call.
  16. * Any further sysfs files that might be required can be created using this
  17. * pointer.
  18. *
  19. * Note: the struct class passed to this function must have previously
  20. * been created with a call to class_create().
  21. */ 
  22. struct device *device_create(struct class *class, struct device *parent, 
  23.                     dev_t devt, const char *fmt, ...) 
  24.      va_list vargs; 
  25.      struct device *dev; 
  26.  
  27. va_start(vargs, fmt); 
  28.      dev = device_create_vargs(class, parent, devt, NULL, fmt, vargs); 
  29.      va_end(vargs); 
  30.      return dev; 
    /**
     * device_create - creates a device and registers it with sysfs
     * @class: pointer to the struct class that this device should be registered to
     * @parent: pointer to the parent struct device of this new device, if any
     * @devt: the dev_t for the char device to be added
     * @fmt: string for the device's name
     *
     * This function can be used by char device classes. A struct device
     * will be created in sysfs, registered to the specified class.
     *
     * A "dev" file will be created, showing the dev_t for the device, if
     * the dev_t is not 0,0.
     * If a pointer to a parent struct device is passed in, the newly created
     * struct device will be a child of that device in sysfs.
     * The pointer to the struct device will be returned from the call.
     * Any further sysfs files that might be required can be created using this
     * pointer.
     *
     * Note: the struct class passed to this function must have previously
     * been created with a call to class_create().
     */
    struct device *device_create(struct class *class, struct device *parent,
                        dev_t devt, const char *fmt, ...)
    {
         va_list vargs;
         struct device *dev;

     va_start(vargs, fmt);
         dev = device_create_vargs(class, parent, devt, NULL, fmt, vargs);
         va_end(vargs);
         return dev;
    }
第一个参数指定所要创建的设备所从属的类,第二个参数是这个设备的父设备,如果没有就指定为NULL,第三个参数是设备号,第四个参数是设备名称,第五个参数是从设备号。

4. 下面以一个简单字符设备驱动来展示如何使用这几个函数

  1.     #include <linux/module.h> 
  2.     #include <linux/kernel.h> 
  3.     #include <linux/init.h> 
  4.     #include <linux/fs.h> 
  5.     #include <linux/cdev.h> 
  6.     #include <linux/device.h> 
  7.  
  8. MODULE_LICENSE ("GPL"); 
  9.  
  10. int hello_major = 555; 
  11.     int hello_minor = 0; 
  12.     int number_of_devices = 1; 
  13.  
  14. struct cdev cdev; 
  15.     dev_t dev = 0; 
  16.  
  17. struct file_operations hello_fops = { 
  18.       .owner = THIS_MODULE 
  19.     }; 
  20.  
  21. static void char_reg_setup_cdev (void
  22.     { 
  23.        int error, devno = MKDEV (hello_major, hello_minor); 
  24.        cdev_init (&cdev, &hello_fops); 
  25.        cdev.owner = THIS_MODULE; 
  26.        cdev.ops = &hello_fops; 
  27.        error = cdev_add (&cdev, devno , 1); 
  28.        if (error) 
  29.            printk (KERN_NOTICE "Error %d adding char_reg_setup_cdev", error); 
  30.  
  31.  
  32. struct class *my_class; 
  33.  
  34. static int __init hello_2_init (void
  35.     { 
  36.        int result; 
  37.        dev = MKDEV (hello_major, hello_minor); 
  38.        result = register_chrdev_region (dev, number_of_devices, "hello"); 
  39.        if (result<0) { 
  40.            printk (KERN_WARNING "hello: can't get major number %d/n", hello_major); 
  41.            return result; 
  42.      } 
  43.  
  44. char_reg_setup_cdev (); 
  45.  
  46. /* create your own class under /sysfs */ 
  47.      my_class = class_create(THIS_MODULE, "my_class"); 
  48.      if(IS_ERR(my_class)) 
  49.      { 
  50.           printk("Err: failed in creating class./n"); 
  51.           return -1; 
  52.       } 
  53.  
  54.   /* register your own device in sysfs, and this will cause udev to create corresponding device node */ 
  55.       device_create( my_class, NULL, MKDEV(hello_major, 0), "hello" "%d", 0 ); 
  56.  
  57.   printk (KERN_INFO "Registered character driver/n"); 
  58.       return 0; 
  59.     } 
  60.  
  61. static void __exit hello_2_exit (void
  62.     { 
  63.        dev_t devno = MKDEV (hello_major, hello_minor); 
  64.  
  65.        cdev_del (&cdev); 
  66.  
  67.    device_destroy(my_class, MKDEV(adc_major, 0));         //delete device node under /dev 
  68.        class_destroy(my_class);                               //delete class created by us 
  69.  
  70.    unregister_chrdev_region (devno, number_of_devices); 
  71.  
  72.    printk (KERN_INFO "char driver cleaned up/n"); 
  73.     } 
  74.  
  75. module_init (hello_2_init); 
  76. module_exit (hello_2_exit); 
    #include <linux/module.h>
    #include <linux/kernel.h>
    #include <linux/init.h>
    #include <linux/fs.h>
    #include <linux/cdev.h>
    #include <linux/device.h>

MODULE_LICENSE ("GPL");

int hello_major = 555;
    int hello_minor = 0;
    int number_of_devices = 1;

struct cdev cdev;
    dev_t dev = 0;

struct file_operations hello_fops = {
      .owner = THIS_MODULE
    };

static void char_reg_setup_cdev (void)
    {
       int error, devno = MKDEV (hello_major, hello_minor);
       cdev_init (&cdev, &hello_fops);
       cdev.owner = THIS_MODULE;
       cdev.ops = &hello_fops;
       error = cdev_add (&cdev, devno , 1);
       if (error)
           printk (KERN_NOTICE "Error %d adding char_reg_setup_cdev", error);

}

struct class *my_class;

static int __init hello_2_init (void)
    {
       int result;
       dev = MKDEV (hello_major, hello_minor);
       result = register_chrdev_region (dev, number_of_devices, "hello");
       if (result<0) {
           printk (KERN_WARNING "hello: can't get major number %d/n", hello_major);
           return result;
     }

 char_reg_setup_cdev ();

 /* create your own class under /sysfs */
     my_class = class_create(THIS_MODULE, "my_class");
     if(IS_ERR(my_class))
     {
          printk("Err: failed in creating class./n");
          return -1;
      }

  /* register your own device in sysfs, and this will cause udev to create corresponding device node */
      device_create( my_class, NULL, MKDEV(hello_major, 0), "hello" "%d", 0 );

  printk (KERN_INFO "Registered character driver/n");
      return 0;
    }

static void __exit hello_2_exit (void)
    {
       dev_t devno = MKDEV (hello_major, hello_minor);

       cdev_del (&cdev);

   device_destroy(my_class, MKDEV(adc_major, 0));         //delete device node under /dev
       class_destroy(my_class);                               //delete class created by us

   unregister_chrdev_region (devno, number_of_devices);

   printk (KERN_INFO "char driver cleaned up/n");
    }

module_init (hello_2_init);
module_exit (hello_2_exit);
这样,模块加载后,就能在/dev目录下找到hello0这个设备节点了。
  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值