schorsch_76 schorsch_76 - 1 year ago 125
Linux Question

sysfs_create_group(): Where to call?

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
How to attach file operations to sysfs attribute in platform driver?

Random Thing i looked at:

b) In the driver struct.

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) ?

PDF page 24

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.

I am more confused than before .....

Best Regards

Answer Source

A device driver is a program that controls a particular type of device that is attached to your computer.

Platform devices are inherently not discoverable, i.e. the hardware cannot say "Hey! I'm present!" to the software. So for these kind of device we need a driver which call as Platform drivers. Drivers provide probe() and remove() methods.

 struct platform_driver {
   int (*probe)(struct platform_device *);
   int (*remove)(struct platform_device *);
   struct device_driver driver;// this file has 2 parameter name or    owner.

probe() should in general verify that the specified device hardware actually exists. First we register our driver. Once it found device then it'll call driver probe. It is using name for searching a device.

Ans : your device is available then you need sysfs entry for communication (To the user space). so conceptually you need to define you sysfs entry in probe.

sys_notify function on your attribute and it will cause your userspace code to wake up. It will trigger when sysfs is available for userspace. It just avoiding a blocking call. When kernel does not have sysfs then it'll not notify userspace.

sysfs is a virtual file system provided by the Linux kernel that exports information about various kernel subsystems, hardware devices, and associated device drivers from the kernel's device model to user space through virtual files. When your device is available then you need this entry to export your information.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download