通过sysfs访问Linux驱动程序
我正在制作一个小型内核模块,以提供对 ARMv7 芯片的某些仅内核模式功能(特别是缓存控制)的用户空间访问。我正在阅读 Corbet、Rubini 和 Hartman 所著的 Linux 设备驱动程序。他们在其中描述了如何制作完整的驱动程序+设备+总线。我根本不想创建一个公交车司机。事实上,我正在制作的“驱动程序”根本不需要与设备定义匹配 - 它隐式地与平台的 CPU 匹配。谁能向我解释一下:
- 我的属性应该放在 sysfs 中的哪里?它应该在
/sysfs/modules/mymodule
下的模块条目中吗?/sys/devices/platform
似乎也很有前途,/sys/devices/system/cpu
也是如此。 - 如果有一个我应该放置
kobject
/attributes 的现有位置,我该如何将其插入其中?如何获取必要的kset
?我见过的所有示例都会创建一个kset
,然后从kobject
链接到它 - 我还没有看到用于请求现有命名kset< 的 API /代码>?
抱歉,如果这实在是太明显了,或者如果有一些非常简单且容易发现的示例,但由于某种原因我没有发现。任何人都可以阐明这一点吗?
I'm making a small kernel module to provide user-space access to some kernel-mode only features of an ARMv7 chip (specifically, cache control). I'm reading through Linux Device Drivers by Corbet, Rubini, and Hartman. In it they describe how to make a full driver+device+bus. I don't want to create a bus driver at all. In fact the 'driver' that I'm making doesn't really need to match against a device definition at all - it is implicitly matched to the platform's CPU. Can anyone explain to me:
- Where in sysfs should my attributes go? Should it be in my module entry under
/sysfs/modules/mymodule
?/sys/devices/platform
seems promising too, and so does/sys/devices/system/cpu
. - If there is an existing place where I should put my
kobject
/attributes, how do I plug it into it? How do I get the necessarykset
? All the examples I've seen create akset
and then link to it from thekobject
- I haven't seen an API for requesting an existing namedkset
?
Sorry if this is just impossibly obvious, or if there's some really straightforward and easily discovered example somewhere that I haven't discovered for some reason. Can anyone shed any light on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有太多使用 sysfs,但我发现了一个看起来简单的示例,它与您正在做的事情非常相似(当然,它也在 ARM 下)。查看
arch/arm/mach-omap1/pm.c
,特别是idle_show
/idle_store
sysfs 文件。它被注册(使用sysfs_create_file()
)为/sys/power/sleep_while_idle
并使用全局/sys/power
kobj(在<代码>include/linux/kobject.h)。那里还定义了一些其他的全局 kobj 可供您使用,尽管我认为其中任何一个都不适合您的驱动程序。这将成为平台驱动程序吗?作为一名不适合任何公交车的司机,这似乎很合适。平台驱动程序在 /sys/devices/platform 下有自己的目录,并且可以在那里拥有属性。看一下
drivers/hwmon/coretemp.c
,其中有temp1_crit
、temp1_crit_alarm
、temp1_input
等。作为属性。它看起来相当简单:创建属性(也许使用__ATTR()
?),将它们全部列在一个数组中,定义一个attribute_group
,使用sysfs_create_group( )
在probe()
函数中,并在remove()
函数中使用sysfs_remove_group()
取消注册。如果您需要其他示例,可能还有其他定义属性的平台驱动程序(搜索 sysfs_create_group)。希望这有帮助!
I haven't worked with sysfs much, but I found a simple-looking example that's pretty similar to what you are doing (naturally, it's also under ARM). Take a look at
arch/arm/mach-omap1/pm.c
, specifically theidle_show
/idle_store
sysfs file. It gets registered (usingsysfs_create_file()
) as/sys/power/sleep_while_idle
and uses the global/sys/power
kobj (defined ininclude/linux/kobject.h
). There are a few other global kobj's defined there that you could use, although I'm don't think any are a good fit for your driver.Is this going to be a platform driver? As a driver that doesn't fit under any bus, it seems like a good fit. Platform drivers get their own directory under /sys/devices/platform, and can have attributes there. Take a look at
drivers/hwmon/coretemp.c
, which hastemp1_crit
,temp1_crit_alarm
,temp1_input
, etc. as attributes. It looks fairly simple: create the attributes (maybe with__ATTR()
?), list them all in an array, define anattribute_group
, register it withsysfs_create_group()
in theprobe()
function, and unregister it withsysfs_remove_group()
in theremove()
function.There are probably other platform drivers that define attributes (search for
sysfs_create_group
) if you need other examples. Hope this helps!