如何使用Linux内核模块获取盒盖状态? [英] How to get lid state using linux kernel module?
问题描述
我可以通过读取/proc/acpi/button/lid/LID0/state
文件来读取笔记本电脑盖的状态.现在,我想从内核模块中读取它.
I can read the status of my laptop lid by reading /proc/acpi/button/lid/LID0/state
file. Now I want to read it from kernel module.
我在内核源代码中找到了源文件drivers/acpi/button.c
.但是我仍然不知道如何使用它.导出acpi_lid_notifier_register, acpi_lid_notifier_unregiste
和acpi_lid_open
函数.
I found the source file drivers/acpi/button.c
in kernel source. But still I didn't understand how to use it. It exporting acpi_lid_notifier_register, acpi_lid_notifier_unregiste
and acpi_lid_open
functions.
如何为盖状态编写模块?
How to write a module for lid state?
推荐答案
acpi_lid_open
returns 0 (closed), 1 (open), or a negative error number (unsupported).
要使用通知程序,您需要在模块中定义一个回调函数和一个通知程序块:
To use the notifier, you would define a callback function and a notifier block in your module:
static int yourmodule_lid_notify(struct notifier_block *nb, unsigned long val, void *unused) {
// Whatever you wanted to do here
}
static struct notifier_block lid_notifier;
// Somewhere in a function, likely your module init function:
lid_notifier.notifier_call = yourmodule_lid_notify;
if (acpi_lid_notifier_register(&lid_notifier)) {
// TODO: Handle the failure here
}
// TODO: Unregister the notifier when your module is unloaded:
acpi_lid_notifier_unregister(&lid_notifier);
这篇关于如何使用Linux内核模块获取盒盖状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!