利用udev在/dev/下动态生成/移除设备文件

来源:互联网 发布:unitedstack 知乎 编辑:程序博客网 时间:2024/04/27 08:06
frome(http://blog.chinaunix.net/u/548/showart.php?id=261973)
利用udev在/dev/下动态生成设备文件,这样用户就不用手工调用mknod了。


利用的kernel API:
    
    class_create        :    创建class
    class_destroy        :    销毁class
    class_device_create    :    创建device
    class_device_destroy    :    销毁device


注意,这些API是2.6.13开始有的,在2.6.13之前,应当使用class_simple_create/class_simple_destroy/class_simple_device_add/class_simple_device_remove这一系列,也就是ldd3第14章描述的。 详见:
   
https://lwn.net/Articles/128644/
Output:
===========================================
[root@localhost dynamic_dev_node]# insmod ./dummy_dev.ko
[root@localhost dynamic_dev_node]# file /dev/dummy_dev0
/dev/dummy_dev0: character special (250/0)
[root@localhost dynamic_dev_node]# rmmod dummy_dev.ko
[root@localhost dynamic_dev_node]# file /dev/dummy_dev0
/dev/dummy_dev0: ERROR: cannot open `/dev/dummy_dev0' (No such file or directory)

  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/init.h>
  4. #include <linux/mm.h>
  5. #include <linux/fs.h>
  6. #include <linux/types.h>
  7. #include <linux/delay.h>
  8. #include <linux/moduleparam.h>
  9. #include <linux/slab.h>
  10. #include <linux/errno.h>
  11. #include <linux/ioctl.h>
  12. #include <linux/cdev.h>
  13. #include <linux/string.h>
  14. #include <linux/list.h>
  15. #include <linux/pci.h>
  16. #include <asm/uaccess.h>
  17. #include <asm/atomic.h>
  18. #include <asm/unistd.h>
  19. #define THIS_DESCRIPTION "/
  20. This module is a dummy device driver, it register/n/
  21. /t/ta char device, and utilize udev to create/destroy /n/
  22. /t/tdevice node under /dev/ dynamicallly."
  23. MODULE_LICENSE("GPL");
  24. MODULE_AUTHOR("albcamus <albcamus@gmail.com>");
  25. MODULE_DESCRIPTION(THIS_DESCRIPTION);
  26. #define DUMMY_MAJOR 250
  27. #define DUMMY_MINOR 0
  28. #define DUMMY_NAME "dummy_dev"
  29. /**
  30.  * the open routine of 'dummy_dev'
  31.  */
  32. static int dummy_open(struct inode *inode, struct file *file)
  33. {
  34.     printk("Open OK/n");
  35.     return 0;
  36. }
  37. /**
  38.  * the write routine of 'dummy_dev'
  39.  */
  40. static ssize_t dummy_write(struct file *filp, const char *bp, size_t count, loff_t *ppos)
  41. {
  42.     printk("Don't Write!/n");
  43.     return 0;
  44. }
  45. /**
  46.  * the read routine of 'dummy_dev'
  47.  */
  48. static ssize_t dummy_read(struct file *filp, char *bp, size_t count, loff_t *ppos)
  49. {
  50.     return 0;
  51. }
  52. /**
  53.  * the ioctl routine of 'dummy_dev'
  54.  */
  55. static int dummy_ioctl(struct inode *inode, struct file *filep,
  56.             unsigned int cmd, unsigned long arg)
  57. {
  58.     return 0;
  59. }
  60. /**
  61.  * file_operations of 'dummy_dev'
  62.  */
  63. static struct file_operations dummy_dev_ops = {
  64.     .owner = THIS_MODULE,
  65.     .open = dummy_open,
  66.     .read = dummy_read,
  67.     .write = dummy_write,
  68.     .ioctl = dummy_ioctl,
  69. };
  70. /**
  71.  * struct cdev of 'dummy_dev'
  72.  */
  73. struct cdev *my_cdev;
  74. struct class *my_class;
  75. static int __init my_init(void)
  76. {
  77.     int err, devno = MKDEV(DUMMY_MAJOR, DUMMY_MINOR);
  78.     /* register the 'dummy_dev' char device */
  79.     my_cdev = cdev_alloc();
  80.     cdev_init(my_cdev, &dummy_dev_ops);
  81.     my_cdev->owner = THIS_MODULE;
  82.     err = cdev_add(my_cdev, devno, 1);
  83.     if (err != 0)
  84.         printk("dummy pci device register failed!/n");
  85.     /* creating your own class */
  86.     my_class = class_create(THIS_MODULE, "dummy_class");
  87.     if(IS_ERR(my_class)) {
  88.         printk("Err: failed in creating class./n");
  89.         return -1;
  90.     }
  91.     /* register your own device in sysfs, and this will cause udevd to create corresponding device node */
  92.     class_device_create(my_class, NULL, MKDEV(DUMMY_MAJOR, DUMMY_MINOR), NULL, DUMMY_NAME "%d", DUMMY_MINOR );
  93.     return 0;
  94. }
  95. static void __exit my_fini(void)
  96. {
  97.     printk("bye/n");
  98.     cdev_del(my_cdev);
  99.     //kfree(my_cdev); no use. because that cdev_del() will call kfree if neccessary.
  100.     class_device_destroy(my_class, MKDEV(DUMMY_MAJOR, DUMMY_MINOR));
  101.     class_destroy(my_class);
  102. }
  103. module_init(my_init);
  104. module_exit(my_fini);

原创粉丝点击