static struct class *led_class;static int major = 0; /*记得static 不然可能会出现外部.c调用这个变量的可能*/static struct file_operations led_drv = {.owner = THIS_MODULE,.open = led_drv_open,.read = led_drv_read,.write = led_drv_write,.release = led_drv_close,
}; major = register_chrdev(0, "sym", &led_drv); /*sym是设备名称,任取,led_drv是 file_operations的名字*//*可以实现静态和动态注册两种方法,主要是通过判断给定的主设备号是否为0来进行区别,为0的时候为 动态注册,否则静态注册*/if(major < 0){printk("register_chrdev famy\n");return major;}led_class = class_create(THIS_MODULE, "sym_class");/*sym_class是类的名字,任取*/if(IS_ERR(led_class)){printk("class_create failed\n");unregister_chrdev(major, "sym");return 1;}
一文搞懂内核中cdev的各种注册函数 - 知乎 (zhihu.com)
注册函数不仅仅有register_chrdev 还有其他,见上文