linux-kerneldriverlinux-device-driverpcipci-bus

How do cdev and its associated file operation work?


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


Solution

  • 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.