sysfs_create_group(): Where to call?

From: georg
Date: Wed Jun 15 2016 - 10:44:53 EST


Hi,

currently i write an driver module which offers some entries in the sysfs.
I read a lot through the driver source tree and the internet. I found two
approches where the sysfs_create_group() is called:

a) most commonly: In the probe() function of the Driver. Like adviced here
http://stackoverflow.com/questions/37237835/how-to-attach-file-operations-to-sysfs-attribute-in-platform-driver

Random Thing i looked at:
https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/drivers/rtc/rtc-ds1307.c#n1580

b) In the driver struct.
http://kroah.com/log/blog/2013/06/26/how-to-create-a-sysfs-file-correctly/

I know, Greg KH is a very well known developer. So i tried to follow his
advice. In the bla_show()/bla_store() functions i tried to get my Driver
private data, but my printk()'s showed much different addresses than i
printed in the probe() function. My private data is (null). Which is ofc
wrong.

When i use approch a) it works as expected, but as Greg KH suggests it is
wrong too. I see it a lot in the stable tree in different Drivers. Greg
writes, the userspace already got the notification that there is a new
device, but the LDD3 book states, that the probe function is there to
determine if the device is present. So why get the userspace notified
about it, even when the kernel doesnt know if it can handle it?


LDD3: https://static.lwn.net/images/pdf/LDD3/ch14.pdf
PDF page 24
[Quote=LDD3 chapter 14]
probe is a function called to query the existence of a specific device
(and whether this driver can work with it), remove is called when the
device is removed from the system,and shutdown is called at shutdown time
to quiesce the device.
[/Quote]

I know, Greg KH is a very well known developer. So i tried to follow his
advice. In the bla_show()/bla_store() functions i tried to get my Driver
private data, but my printk()'s showed much different addresses than i
printed in the probe() function. My private data is (null). Which is ofc
wrong.

When i use approch a) it works as expected, but as Greg KH suggests it is
wrong too. I see it a lot in the stable tree in different drivers. Greg
writes, the userspace already got the notification that there is a new
device, but the LDD3 book states, that the probe function is there to
determine if the device is present.

To sum my question up:
1.Why get the userspace notified about it, even when the kernel doesnt
know if it can handle it?
2.Where is the right place to call sysfs_create_group()? Is it a) or b) ?


I am more confused than before .....

Best Regards
Georg