Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get lid state using linux kernel module?

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?

like image 719
gangadhars Avatar asked Dec 06 '25 02:12

gangadhars


1 Answers

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);
like image 192
Dark Falcon Avatar answered Dec 08 '25 14:12

Dark Falcon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!