利用udev在/dev/下動態產生/移除裝置檔案

來源:互聯網
上載者:User
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);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.