gpt4 book ai didi

linux - 无法创建次设备号为 0 和 1 的设备

转载 作者:太空狗 更新时间:2023-10-29 11:16:11 26 4
gpt4 key购买 nike

我无法在/proc 下创建两个次要编号为 0 和 1 的设备节点。 device_create 创建次设备号为 0 的第一个设备,但如何创建次设备号为 1 的第二个设备?

static int __init my_driver_init (void) {
int result;
int major;
printk("In init module");

result = alloc_chrdev_region(&dev, 0, 2, mydev_name);
if (result<0)
return result;

major = MAJOR(dev);

printk("The device is registered by Major no: %d\n", major);
my_driver_cdev = cdev_alloc();
cdev_init (my_driver_cdev, &my_driver_fops);
my_driver_cdev->owner = THIS_MODULE;
result = cdev_add(my_driver_cdev, 0, 2);
if (result<0){
printk("Error in registering the module\n");
unregister_chrdev_region(dev, 2);
return result;
}

printk(KERN_INFO "my_driver: %d\n",__LINE__);

my_driver_class = class_create(THIS_MODULE,mydev_name);
if (IS_ERR(my_driver_class)) {
printk(KERN_ERR "Error creating my_driver class.\n");
result = PTR_ERR(my_driver_class);
cdev_del(my_driver_cdev);
unregister_chrdev_region(dev, 2);
return -1;
}
device_create(my_driver_class,NULL,dev,NULL,"my_driver%d",0);

printk(KERN_INFO "my_driver: %d\n",__LINE__);

添加以下行不会创建次设备号为 1 的第二个设备。

device_create(my_driver_class,NULL,dev,NULL,"my_driver%d",1);

最佳答案

device_create 的第四个参数应该是主设备号和次设备号(在开发中),使用哪个设备文件将被创建。在您的情况下,第四个参数始终相同,您需要向 device_create 提供下一个次要编号。你可以这样写代码:

    for (minor = 0; minor < 2; minor++) 
device_create(my_driver_class,NULL,MKDEV(major, minor),NULL,"my_driver%d",minor);

关于linux - 无法创建次设备号为 0 和 1 的设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7062389/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com