How to create a device node from the init_module code of a Linux kernel module?

后端 未结 3 718
一整个雨季
一整个雨季 2020-11-30 17:41

I am writing a module for the linux kernel and I want to create some device nodes in the init function

int init_module(void)
{
    Major = register_chrdev(0,         


        
3条回答
  •  心在旅途
    2020-11-30 18:13

    static int __init ofcd_init(void) /* Constructor */
    {
        printk(KERN_INFO "Welcome!");
        if (alloc_chrdev_region(&first, 0, 1, "char_dev") < 0)  //$cat /proc/devices
        {
            return -1;
        }
        if ((cl = class_create(THIS_MODULE, "chardrv")) == NULL)    //$ls /sys/class
        {
            unregister_chrdev_region(first, 1);
            return -1;
        }
        if (device_create(cl, NULL, first, NULL, "mynull") == NULL) //$ls /dev/
        {
            class_destroy(cl);
            unregister_chrdev_region(first, 1);
            return -1;
        }
        cdev_init(&c_dev, &fops);
        if (cdev_add(&c_dev, first, 1) == -1)
        {
            device_destroy(cl, first);
            class_destroy(cl);
            unregister_chrdev_region(first, 1);
            return -1;
        }
        return 0;
    }
    

提交回复
热议问题