使用linux2.6 udev自动生成设备文件

    技术2022-05-20  55

    使用linux2.6 udev自动生成设备文件 内核函数 class_create(),class_destroy() device_create(),device_destroy() 定义在<linux/device.h>头文件中 示例如下: #include <linux/init.h> #include <linux/module.h> #include <linux/device.h> #include <linux/kdev_t.h> struct class *myclass=NULL; static dev_t mydevno=MKDEV(300,0); static int test_init(void){  myclass=class_create(THIS_MODULE,"test");  device_create(myclass,NULL,mydevno,NULL,"mydevice"); //创 建设备文件  printk("test_init ok,device create ok/n");  return 0; } static void test_exit(void){  device_destroy(myclass,mydevno);//删除设备文件  class_destroy(myclass);  printk("test_exit ok,delete device ok/n"); } module_init(test_init); moduel_exit(test_exit); MODULE_LICENSE("Dual BSD/GPL"); 编译加载后 ls -l /dev/mydevice 会发现生成了这个设备文件,主设备号为300,次设备号为0 卸载模块后 该设备文件被删除 这样多方便,以后再写驱动的时候就要这样写了。哈哈 udev太方便了。

    最新回复(0)