利用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)
Code:
+++++++++++++++++++++++++++
#define THIS_DESCRIPTION " MODULE_LICENSE("GPL"); #define DUMMY_MAJOR 250 /** /** /** /** return 0; /** /** static int __init my_init(void) /* register the 'dummy_dev' char device */ my_cdev->owner = THIS_MODULE; err = cdev_add(my_cdev, devno, 1); /* creating your own class */ /* register your own device in sysfs, and this will cause udevd to create corresponding device node */ return 0; static void __exit my_fini(void) cdev_del(my_cdev); class_device_destroy(my_class, MKDEV(DUMMY_MAJOR, DUMMY_MINOR)); } module_init(my_init); |
注:这样实现之后,还需要去udev工具进行设置的,才能看到/dev下出现你要的设备的,详情参考:
http://linux.chinaunix.net/bbs/thread-1031291-2-1.html
转载请注明:在路上 » 【转】利用udev在/dev/下动态生成/移除设备文件
如何编写Linux设备驱动程序【转】
http://hi.baidu.com/serial_story/blog/item/82be2f385c2921f1b311c734.html
怎样用gcc编译的?
强烈关注!!