Search code examples
unixlinux-kernellinux-device-driverfreebsdbsd

filp->private_data equivalent in freeBSD


I am porting my linux driver to freebsd. I use "filp->private_data" variable to store some information required by driver. (To avoid locks in multi-threaded cases). I found BSD does not use "file" structure in linux and instead uses cdev. cdev does not have "private_data" variable. But I am sure there must be some other variable in BSD?

Does anyone know how to achieve what I am trying to do.

Thanks.


Solution

  • You have an instance of your softc structure for each instance of your device. That would usually have a mutex. You can use the si_drv1 member of the cdev to keep a pointer back to the softc. Something like:

    struct mydev_softc {
        struct mtx m_lock;
        struct cdev* m_cdev;
        device_t m_dev;
        /* ... whatever else you need ... */
    };
    
    static int mydev_attach(device_t dev)
    {
        struct mydev_softc* sc = device_get_softc(dev);
    
        mtx_init(&sc->m_lock);
        sc->m_cdev = make_dev(...);
        sc->m_cdev->si_drv1 = sc;
        sc->m_dev = dev;
        /* Plus whatever else you need */
     }
    

    I have left out all the other driver initialisation stuff so you can see this part.