Gavin_GC 发表于 2012-3-9 21:37:32

如何自动创建设备节点

采用2.6.28内核,编写一个简单的字符设备驱动程序。想让他自动在/dev目录下创建设备节点。现在通过insomd 加载内核模块,在proc/device文件中有对应的设备,但是/dev中没有想要的文件,需要通过mknod dev/xxx b 252 0 来创建,然后运行测试程序,有什么方面让系统自动创建,上电就可以自动运行测试程序(现在测试程序反应无法打开文件) 。请教下具体怎么操作, 还有一种方式是编入内核,按照操作后在proc/device里面都看不到。
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/fs.h>
#include<linux/init.h>
#include<linux/delay.h>
#include<linux/device.h>
#include<asm/irq.h>
#include<asm/io.h>
#include<mach/regs-gpio.h>
#include<mach/hardware.h>
static struct class *firstdrv_class;
static struct class_device *firstdrv_class_dev;

static int first_drv_open(struct inode *inode,struct file *file)
{
        printk("first_drv_open\n");
        return 0;
}

static void first_drv_write(struct file *file, const char __user *buf,size_t count,loff_t *offp)
{
        printk("first_drv_write\n");
}

static struct file_operations first_drv_fpos =
{
        .owner=THIS_MODULE,
        .open=first_drv_open,
        .write=first_drv_write,
};
static int major;
static int first_drv_init(void)
{
        major=register_chrdev(0,"first_drv",&first_drv_fpos);
        firstdrv_class=class_create(THIS_MODULE,"firstdrv");
        firstdrv_class_dev=device_create(firstdrv_class,NULL,MKDEV(major,0),NULL,"firstdrv");
       
}

static void first_drv_exit(void)
{
        unregister_chrdev(major,"first_drv");
        device_unregister(firstdrv_class_dev);
        device_destroy(firstdrv_class,MKDEV(major,0));
       
}

module_init(first_drv_init);
module_exit(first_drv_exit);

MODULE_LICENSE("GPL");
页: [1]
查看完整版本: 如何自动创建设备节点