问题
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.
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?
回答1:
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);
来源:https://stackoverflow.com/questions/30512998/how-to-get-lid-state-using-linux-kernel-module