通过sysfs访问Linux驱动程序

发布于 2024-12-09 23:45:55 字数 681 浏览 2 评论 0原文

我正在制作一个小型内核模块,以提供对 ARMv7 芯片的某些仅内核模式功能(特别是缓存控制)的用户空间访问。我正在阅读 Corbet、Rubini 和 Hartman 所著的 Linux 设备驱动程序。他们在其中描述了如何制作完整的驱动程序+设备+总线。我根本不想创建一个公交车司机。事实上,我正在制作的“驱动程序”根本不需要与设备定义匹配 - 它隐式地与平台的 CPU 匹配。谁能向我解释一下:

  1. 我的属性应该放在 sysfs 中的哪里?它应该在 /sysfs/modules/mymodule 下的模块条目中吗? /sys/devices/platform 似乎也很有前途,/sys/devices/system/cpu 也是如此。
  2. 如果有一个我应该放置 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:

  1. 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.
  2. 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 necessary kset? All the examples I've seen create a kset and then link to it from the kobject - I haven't seen an API for requesting an existing named kset?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太阳男子 2024-12-16 23:45:55

我没有太多使用 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_crittemp1_crit_alarmtemp1_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 the idle_show/idle_store sysfs file. It gets registered (using sysfs_create_file()) as /sys/power/sleep_while_idle and uses the global /sys/power kobj (defined in include/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 has temp1_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 an attribute_group, register it with sysfs_create_group() in the probe() function, and unregister it with sysfs_remove_group() in the remove() function.

There are probably other platform drivers that define attributes (search for sysfs_create_group) if you need other examples. Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文