Kernel Newbies Archive on lore.kernel.org
 help / color / Atom feed
* platform device as parent of a miscdevice
@ 2021-03-30 16:04 Martin Kaiser
  2021-03-30 16:49 ` Greg KH
  0 siblings, 1 reply; 4+ messages in thread
From: Martin Kaiser @ 2021-03-30 16:04 UTC (permalink / raw)
  To: kernelnewbies

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

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: platform device as parent of a miscdevice
  2021-03-30 16:04 platform device as parent of a miscdevice Martin Kaiser
@ 2021-03-30 16:49 ` Greg KH
  2021-03-30 19:27   ` Martin Kaiser
  0 siblings, 1 reply; 4+ messages in thread
From: Greg KH @ 2021-03-30 16:49 UTC (permalink / raw)
  To: kernelnewbies

On Tue, Mar 30, 2021 at 06:04:09PM +0200, Martin Kaiser wrote:
> 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)?

Not at all, this is what you should be done, and is what many misc
devices do, that's why the parent pointer is there :)

What in-kernel misc drivers do not do that that you feel should?

thanks,

greg k-h

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: platform device as parent of a miscdevice
  2021-03-30 16:49 ` Greg KH
@ 2021-03-30 19:27   ` Martin Kaiser
  2021-03-30 20:09     ` Greg KH
  0 siblings, 1 reply; 4+ messages in thread
From: Martin Kaiser @ 2021-03-30 19:27 UTC (permalink / raw)
  To: Greg KH; +Cc: kernelnewbies

Hi Greg and all,

Thus wrote Greg KH (greg@kroah.com):

> Not at all, this is what you should be done, and is what many misc
> devices do, that's why the parent pointer is there :)

thanks for the confirmation.

> What in-kernel misc drivers do not do that that you feel should?

My misunderstanding was that quite a few drivers set the parent pointer
but hardly anyone uses the parent pointer in the file_operations.

It seems that most drivers have their miscdevice as part of their global
structure and use container_of in the file_operations, e.g. ipmb_read in
drivers/char/ipmi/ipmb_dev_int.c.

Best regards,
Martin

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: platform device as parent of a miscdevice
  2021-03-30 19:27   ` Martin Kaiser
@ 2021-03-30 20:09     ` Greg KH
  0 siblings, 0 replies; 4+ messages in thread
From: Greg KH @ 2021-03-30 20:09 UTC (permalink / raw)
  To: kernelnewbies

On Tue, Mar 30, 2021 at 09:27:17PM +0200, Martin Kaiser wrote:
> > What in-kernel misc drivers do not do that that you feel should?
> 
> My misunderstanding was that quite a few drivers set the parent pointer
> but hardly anyone uses the parent pointer in the file_operations.

What parent pointer in struct file_operations?  I don't see one...

> It seems that most drivers have their miscdevice as part of their global
> structure and use container_of in the file_operations, e.g. ipmb_read in
> drivers/char/ipmi/ipmb_dev_int.c.

That's one way to use it, there are I think at least 3 ways to use it,
maybe more...

Don't confuse the struct device parent pointer with the private pointer
to get back to your local device-specific data.  They are different
things.

thanks,

greg k-h

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-30 16:04 platform device as parent of a miscdevice Martin Kaiser
2021-03-30 16:49 ` Greg KH
2021-03-30 19:27   ` Martin Kaiser
2021-03-30 20:09     ` Greg KH

Kernel Newbies Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/kernelnewbies/0 kernelnewbies/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 kernelnewbies kernelnewbies/ https://lore.kernel.org/kernelnewbies \
		kernelnewbies@kernelnewbies.org
	public-inbox-index kernelnewbies

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernelnewbies.kernelnewbies


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git