platform device as parent of a miscdevice

Martin Kaiser lists at kaiser.cx
Tue Mar 30 12:04:09 EDT 2021


Hi all,

I have a driver that is bound to a platform device. It creates a
miscdevice to interact with user space. And it has a "global" structure
that stores info which are used throughout the driver. 

This works ok as far as I can see. The only issue is how to make the
"global" struct available to the miscdevice's file operations. Of
course, I can make the struct a global variable.

Recently, I realized that I could make the platform device the parent of
the miscdevice. I came up with something like

static int example_open(struct inode *inode, struct file *file)
{
    struct miscdevice *m = file->private_data;
    struct example_global_info *gbl =  dev_get_drvdata(m->parent);
    ...
}

static struct file_operations example_fops =
{
    .owner   = THIS_MODULE,
    .open    = example_open,
    ...
};

static struct miscdevice example_miscdev = {
    ...
    .fops  = &example_fops,
};

static int example_probe(struct platform_device *pdev)
{
    struct example_global_info *gbl = devm_kzalloc(&pdev->dev, ....);

    platform_set_drvdata(pdev, gbl);

    example_miscdev.parent = &pdev->dev;
    ret = misc_register(&example_miscdev);
    ...
}


Yet again, this seems to work ok. I was surprised to see that there's
very few (if any) mainline drivers that do something similar. Does this
approach make sense? Does it have any implications that I should be
aware of (e.g.  does the link between pdev and miscdevice cause problems
for suspend/resume)?

Thanks,
   Martin



More information about the Kernelnewbies mailing list