sysfs_create_group(): 去哪里打电话?

sysfs_create_group(): Where to call?

目前我正在编写一个驱动程序模块,它在 sysfs 中提供了一些条目。我通过驱动程序源代码树和互联网阅读了很多。我发现了两个调用 sysfs_create_group() 的方法:

a) 最常见:在驱动程序的 probe() 函数中。喜欢这里的建议

随机查看的内容: https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/drivers/rtc/rtc-ds1307.c#n1580

b) 在驱动程序结构中。 http://kroah.com/log/blog/2013/06/26/how-to-create-a-sysfs-file-correctly/

我知道,Greg KH 是一位非常知名的开发人员。所以我试着听从他的建议。在 bla_show()/bla_store() 函数中,我试图获取我的驱动程序私有数据,但我的 printk() 显示的地址与我在 probe() 函数中打印的地址有很大不同。我的私人数据是(空)。这是错误的。

当我使用方法 a) 时,它按预期工作,但正如 Greg KH 所建议的那样,它也是错误的。我在不同驱动程序的稳定树中看到了很多。 Greg 写道,用户空间已经收到有新设备的通知,但 LDD3 书中指出,探测功能用于确定设备是否存在。

总结一下我的问题:

  1. 为什么要通知用户空间,即使内核不知道它是否可以处理它?
  2. 调用 sysfs_create_group() 的正确位置在哪里?是 a) 还是 b)?

LDD3:https://static.lwn.net/images/pdf/LDD3/ch14.pdf PDF 第 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.

我比以前更糊涂了.....

最好的问候 乔治

设备驱动程序 是一种程序,用于控制连接到计算机的特定类型的设备。

平台设备本质上是不可发现的,即硬件不能对软件说"Hey! I'm present!"。因此,对于这类设备,我们需要一个称为 平台驱动程序 的驱动程序。驱动程序提供 probe() 和 remove() 方法。

 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() 一般应验证指定的设备硬件 实际存在。首先我们注册我们的驱动程序。一旦找到设备,它就会调用驱动程序探测。它正在使用名称搜索设备。


答案:您的设备可用,然后您需要 sysfs 条目进行通信(致用户 space)。所以从概念上讲,您需要在探测中定义您的 sysfs 条目。

sys_notify 对您的属性起作用,它会导致您的用户space 代码唤醒。当用户 space 可以使用 sysfs 时,它将触发。它只是避免了阻塞调用。当内核没有 sysfs 时,它不会通知用户space。

sysfs是Linux内核提供的一个虚拟文件系统,它从内核的设备模型中导出关于各种内核子系统、硬件设备和相关设备驱动程序的信息通过虚拟文件给用户 space。当您的设备可用时,您需要此条目来导出您的信息。