How do cdev and its associated file operation work?

梦想与她 提交于 2019-12-23 02:39:27

问题


Actually working on a PCI driver. I have two PCIe cards with same device ID and vendor ID. So to make a difference, I assign these two cards with two different MINOR numbers.

//request for device numbers
error = alloc_chrdev_region(&devt, 0, cards_found, DEVICE_NAME);
if (error == 0)
{   
    major = MAJOR(devt);
    printk(KERN_INFO "(drv_init): MAJOR number is %d\n", major);
    printk(KERN_INFO "(drv_init): MINOR number range from 0 to %d\n", cards_found-1);

    cdevs = cdev_alloc();
    cdevs->owner = THIS_MODULE;
    cdev_init(cdevs, fops);

    for(i=0;i<cards_found,i++)
    {   
        devt = MKDEV(major, i); 
        error = cdev_add(cdevs, devt, 1); 
        if (error == 0)
        {   
            printk(KERN_INFO "(drv_init): cdev_add success for minor number: %d", i); 
        }   
        else
        {   
            printk(KERN_ALERT "(drv_init): cdev_add failed for minor number: %d,error code: %d, exit driver\n", i, error);
            devt = MKDEV(major, 0); 
            unregister_chrdev_region(devt, cards_found);
            pci_unregister_driver(&my_pci_driver);
            return(error);
        }   
    }   
}   `

I'm doing this because all docs I found on internet suggests that one MINOR number for one device.

But I can't understand how could OS know which card is targeted when I do a fops_open, since the fops is bundled to all devices.

Need your help, Thx everyone.

PS: fops = file operations


回答1:


Signature of .open operation is

int open(struct inode* inode, struct file* file)

Minor number of device opened can be obtained via

iminor(inode)

Other file operations also may obtain device number using file->f_inode as inode.

Alternatively, .open may store some device-specific data in file->f_private, and other operations may access them that way.



来源:https://stackoverflow.com/questions/36066635/how-do-cdev-and-its-associated-file-operation-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!