我正在尝试编写一个自定义系统调用处理程序来操作 BeagleBone Black 板上的 LED。我已经浏览了内核 LED API (include/leds.h),但我无法找到任何可以让我处理可用 LED 设备或其他一些机制来操作(开/关/切换)它们的东西。大多数功能似乎都需要一个 led_classdev 对象,但我找不到获取它的方法。
我可以使用/sys/class/leds/... 界面从用户空间执行此操作。但我想从内核空间做一些类似的事情。我愿意硬编码特定于 BeagleBone Black 的任何设备名称,因为可移植性不是问题。
最佳答案
我找到了一种从内核空间访问/控制 LED 的方法 - 通过创建自定义 LED 触发器。它没有提供枚举可用设备的方法,并且它需要通过写入/sys/class/leds/foo/trigger 从用户空间为 LED 分配自定义触发器。但它允许您在分配触发器后从内核代码操作 LED。
struct led_trigger *trigger;
...
led_trigger_register_simple("myled", &trigger); //create and register the trigger
...
//assign the trigger "myled" to the desired LED(s) from user-space
//by writing to the corresponding trigger files in /sys/class/leds/foo/
...
led_trigger_event(trigger, LED_FULL); //turn-on the LED(s) connected to the trigger
led_trigger_event(trigger, LED_OFF); //turn-off
触发器的创建和注册可以在模块 init 中完成,例如。此外,如果需要独立操作多个 LED,那么据我所知,需要为每个 LED 创建单独的触发器。
关于linux - 如何从内核代码 (BeagleBone Black) 访问或枚举可用的 LED?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58527538/