我正在从事嵌入式项目。我们的主板使用的是Linux内核v3.16.7。我正致力于支持一些监控活动的外围LED。我已成功将引导程序修改为/sys/class/leds/
中的load the drivers and create sysfs entries,这很棒。我还在{+ 3}}附加了一个oneshot trigger,因此我可以echo 1 > shot
内/sys/class/leds/actled1\:green/
和LED闪烁。正是我想要的。
但是,我想在启动过程中实例化驱动程序时配置每个LED的延迟,并且我不知道如何做到这一点。驱动程序在/sys/class/leds/actled1\:green/
中创建名为delay_on
和delay_off
的sysfs条目,我可以从用户空间写入它们来配置延迟,但应该可以从内核设置它们的初始值实例化期间的空间。我还希望能够设置invert
参数(这只是延迟时的另一个sysfs条目)。
当我从内核空间实例化驱动程序时,如何配置led触发器的参数?
以下是我实例化LED GPIO的方法。首先,我设置了所需的结构:
static struct gpio_led my_leds[] __initdata = {
{
.name = "actled1:green",
.default_trigger = "oneshot"
.gpio = ACTIVITY_LED_GPIO_BASE + 0,
.active_low = true,
},
{
.name = "actled2:red",
.default_trigger = "oneshot"
.gpio = ACTIVITY_LED_GPIO_BASE + 1,
.active_low = true,
},
};
static struct gpio_led_platform_data my_leds_pdata __initdata = {
.num_leds = ARRAY_SIZE(my_leds),
.leds = my_leds,
};
然后,我调用此函数来创建平台设备:
static int __init setup_my_leds (void)
{
struct platform_device *pdev;
int ret;
pdev = platform_device_alloc("leds-gpio", -1);
if (!pdev) {
return -ENOMEM;
}
ret = platform_device_add_data(pdev,
&my_leds_pdata,
sizeof(my_leds_pdata));
if (ret < 0) {
platform_device_put(pdev);
return ret;
}
ret = platform_device_add(pdev);
if (ret < 0) {
platform_device_put(pdev);
return ret;
}
return 0;
}
gpio_led
结构的定义位于include/linux/leds.h
line 327,gpio_led_platform_data
的定义位于line 341 of the same file。 platform_device_add_data
的定义位于drivers/base/platform.c
line 284。
查看单击触发器(drivers/leds/trigger/ledtrig-oneshot.c
)的来源以回答问题可能很有用。同样重要的是&#34; leds-gpio&#34;司机(drivers/leds/leds-gpio.c
)。
我怀疑答案在drivers/base/platform.c
及相关documentation的某处,但我没有看到任何处理我需要的数据的函数。
要解决我无意中遗漏的一些信息:
.ko
。delay_on
/ delay_off
。例如,oneshot&#39; invert
参数。答案 0 :(得分:3)
有一些问题,我认为我已经找到了解决方案,但即使您提供了大量信息,也有一些缺失,所以我会列举所有可能的方案,所以要耐心......
(1)获取要设置的初始值。我认为你已经想到了这一点,但是......你可以从内核cmdline解析中获取这些(例如你将值添加到/boot/grub2/grub.cfg为myleds.delay_on=...
。如果你正在加载通过modprobe
,您可以设置模块参数。这些参数也可以是myleds.config_file=/etc/sysconfig/myleds.conf
(2)您可以在setup_my_leds中设置它们[除了onehot_trig_activate的顽固性 - 我们很快就会处理它们]。来自drivers/base/platform.c
:
/**
* arch_setup_pdev_archdata - Allow manipulation of archdata before its used
* @pdev: platform device
*
* This is called before platform_device_add() such that any pdev_archdata may
* be setup before the platform_notifier is called. So if a user needs to
* manipulate any relevant information in the pdev_archdata they can do:
*
* platform_device_alloc()
* ... manipulate ...
* platform_device_add()
*
* And if they don't care they can just call platform_device_register() and
* everything will just work out.
*/
因此,考虑到这一点,让我们稍微更改您的设置功能:
static int __init setup_my_leds (void)
{
struct platform_device *pdev;
int ret;
// get initial values you want to set, possibly storing away for later use
my_leds_get_init_values(...);
pdev = platform_device_alloc("leds-gpio", -1);
if (!pdev) {
return -ENOMEM;
}
// Choice (1): set your initial values in my_leds_pdata here
my_leds_set_init_values(&my_leds_pdata);
// NOTE: just does kmemdup and sets pdev->dev.platform_data
ret = platform_device_add_data(pdev,
&my_leds_pdata,
sizeof(my_leds_pdata));
if (ret < 0) {
platform_device_put(pdev);
return ret;
}
// Choice (2): set your initial values in pdev->dev.platform_data here
my_leds_set_init_values(pdev->dev.platform_data);
ret = platform_device_add(pdev);
if (ret < 0) {
platform_device_put(pdev);
return ret;
}
return 0;
}
(3)遗憾的是,由于您使用的是.default_trigger = "oneshot"
,因此oneshot_trig_activate
中的drivers/leds/trigger/ledtrig-oneshot.c
会对上述数据进行抨击。所以,我们需要解决这个问题。
选项(A):假设您可以根据需要重建整个内核,只需修改oneshot_trig_activate
中的ledtrig-oneshot.c
并删除使用DEFAULT_DELAY
的行。只有当您知道 ,系统中其他任何可能需要默认值的 时,这才真正有用。
选项(B):如果您不允许修改ledtrig-oneshot.c
,但允许向drivers/leds/trigger
添加新触发器,请将文件复制到(例如)ledtrig-oneshot2.c
并在那里做改变。您需要将.name
更改为.name = "oneshot2"
。 [vi,当然:-)]的简单方法是:%s/oneshot/oneshot2/g
。您还需要在Kconfig和Makefile中添加一个新条目。然后,更改结构定义以使用新驱动程序:.default_trigger = "oneshot2"
选项(C):假设您无法[或者不想]触摸drivers/leds/trigger
目录,请将ledtrig-oneshot.c
复制到您的驱动程序目录[根据需要重命名]。从上面的选项(B)进行编辑。通过Makefile中的一些技巧,您可以使用它来构建my_led_driver.ko
和 ledtrig-oneshot2.ko
。您需要修改Kconfig,可能为led触发器驱动程序添加depends on LED_TRIGGERS
。您也可以将这两个子目录放在单独的子目录中,单个Makefile / Kconfig可能更简单:my_led/my_driver
和my_led/my_trigger
选项(C)可以提前做好工作,但从长远来看可能更清洁,更便携。当然,您可以选择(A)进行概念验证,然后执行选项(B),并执行&#34;最终发货&#34;作为选项(C)。
设置初始值时的另一种方法:请记住my_leds_get_init_values
的评论为possibly storing away for later use
。您可以更改oneshot2_trig_activate
来调用它,而不是使用DEFAULT_DELAY
。我不喜欢这样,并且更喜欢只是中性oneshot_trig_activate
的冒犯行为的解决方案。但是,通过一些测试,您可能会发现这是您必须这样做的方式。
希望以上情况有效。如果没有,请使用其他信息和/或限制编辑您的问题[并发送评论],我很乐意更新我的回答[我已经为40 +做了司机。]
更新:好的,这是一个完全注释和修改的LED触发器驱动程序,您可以将其用作drivers/led/trigger/ledtrig-oneshot.c
的替代品。
因为invert
参数可以不直接通过您在设置函数中访问的任何标准结构[即它存储在触发器驱动程序内的私有结构中,删除&#34; Choice(1)&#34;和&#34;选择(2)&#34;。我们会在[已修改] oneshot_trig_activate
内一次性设置它们。
此外,您需要的init参数必须由my_leds_get_init_values
设置并存储为 globals ,以便触发器驱动程序可以找到它们。也就是说,没有办法干净地执行此操作(例如,使用指向传递的私有结构的指针),因为您在安装程序中可以访问的结构不具有此字段。请参阅触发器驱动程序的顶部以进行讨论。
我的第一步是使用描述性注释来注释基本驱动程序。除了K&amp; R版权和单一版本之外,没有任何评论。我的注释是ANSI(&#34; //&#34;)注释。
如果我接管了这个驱动程序,我会添加这些并留下它们。但是,我的评论水平可能会被考虑在内,而且#34;过度评论&#34;根据内核风格指南,可能会考虑&#34; cruft&#34;,特别是对于这种直截了当的驱动程序。
下一步是添加必要的更改。所有具有添加/更改的地方都标有注释块,该注释块以&#34; C:&#34;开头。这些是值得关注的重要场所。请注意,这些评论是 in 的合法候选人。在其他更复杂的驱动程序中,评论的级别取决于作者。 &#34; C:&#34;只是为了突出你的地方。
使用注释,现在可以更容易地读取直线。此外,diff -u
也可能有所帮助。如果你在git
下得到了所有东西,那就更好了。
由于这一切,我删除了选项(A)&#34; [直接修改原始文件]并做&#34;选项(B)&#34;或&#34;选项(C)&#34;仅
触发器驱动程序使用所有static
个定义,因此我之前建议的全局编辑 not 是必需的。我确实做过.name = "myled_oneshot";
,因此您需要将其与.default_trigger = "myled_oneshot";
进行匹配。您可以随意使用my_leds_whatever
与您现有的命名约定保持一致。当我为自己这样做时,我通常会使用我的首字母缩写,因此它变为ce_leds_whatever
- YMMV
无论如何,这里是整个修改后的触发器驱动程序。请注意,我已完成编辑,但我不尝试编译/构建它。
/*
* One-shot LED Trigger
*
* Copyright 2012, Fabio Baltieri <fabio.baltieri@gmail.com>
*
* Based on ledtrig-timer.c by Richard Purdie <rpurdie@openedhand.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/ctype.h>
#include <linux/slab.h>
#include <linux/leds.h>
#include "../leds.h"
// C: we need to get access to the init data populated by the setup function
// we have the "clean way" with a struct definition inside a header file and
// the "dirty way" using three separate int globals
// in either case, the externs referenced here must be defined in the "my_leds"
// driver as global
// C: the "clean way"
// (1) requires that we have a path to the .h (e.g. -I<whatever)
// (2) this would be easier/preferable for the "Option (C)"
// (3) once done, easily extensible [probably not a consideration here]
#ifdef MYLED_USESTRUCT
#include "whatever/myled_init.h"
extern struct myled_init myled_init;
// C: the "ugly way"
// (1) no need to use a separate .h file
// (2) three separate global variables is wasteful
// (3) more than three, and we really should consider the "struct"
#else
extern int myled_init_delay_on;
extern int myled_init_delay_off;
extern int myled_init_invert;
#endif
#define DEFAULT_DELAY 100
// oneshot trigger driver private data
struct oneshot_trig_data {
unsigned int invert; // current invert state
};
// arm oneshot sequence from sysfs write to shot file
static ssize_t led_shot(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct led_classdev *led_cdev = dev_get_drvdata(dev);
struct oneshot_trig_data *oneshot_data = led_cdev->trigger_data;
led_blink_set_oneshot(led_cdev,
&led_cdev->blink_delay_on, &led_cdev->blink_delay_off,
oneshot_data->invert);
/* content is ignored */
return size;
}
// show invert state for "cat invert"
static ssize_t led_invert_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct led_classdev *led_cdev = dev_get_drvdata(dev);
struct oneshot_trig_data *oneshot_data = led_cdev->trigger_data;
return sprintf(buf, "%u\n", oneshot_data->invert);
}
// set invert from sysfs write to invert file (e.g. echo 1 > invert)
static ssize_t led_invert_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct led_classdev *led_cdev = dev_get_drvdata(dev);
struct oneshot_trig_data *oneshot_data = led_cdev->trigger_data;
unsigned long state;
int ret;
ret = kstrtoul(buf, 0, &state);
if (ret)
return ret;
oneshot_data->invert = !!state;
if (oneshot_data->invert)
led_set_brightness_async(led_cdev, LED_FULL);
else
led_set_brightness_async(led_cdev, LED_OFF);
return size;
}
// show delay_on state for "cat delay_on"
static ssize_t led_delay_on_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct led_classdev *led_cdev = dev_get_drvdata(dev);
return sprintf(buf, "%lu\n", led_cdev->blink_delay_on);
}
// set delay_on from sysfs write to delay_on file (e.g. echo 20 > delay_on)
static ssize_t led_delay_on_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct led_classdev *led_cdev = dev_get_drvdata(dev);
unsigned long state;
int ret;
ret = kstrtoul(buf, 0, &state);
if (ret)
return ret;
led_cdev->blink_delay_on = state;
return size;
}
// show delay_off state for "cat delay_off"
static ssize_t led_delay_off_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct led_classdev *led_cdev = dev_get_drvdata(dev);
return sprintf(buf, "%lu\n", led_cdev->blink_delay_off);
}
// set delay_off from sysfs write to delay_off file (e.g. echo 20 > delay_off)
static ssize_t led_delay_off_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct led_classdev *led_cdev = dev_get_drvdata(dev);
unsigned long state;
int ret;
ret = kstrtoul(buf, 0, &state);
if (ret)
return ret;
led_cdev->blink_delay_off = state;
return size;
}
// these are the "attribute" definitions -- one for each sysfs entry
// pointers to these show up in the above functions as the "attr" argument
static DEVICE_ATTR(delay_on, 0644, led_delay_on_show, led_delay_on_store);
static DEVICE_ATTR(delay_off, 0644, led_delay_off_show, led_delay_off_store);
static DEVICE_ATTR(invert, 0644, led_invert_show, led_invert_store);
static DEVICE_ATTR(shot, 0200, NULL, led_shot);
// activate the trigger device
static void oneshot_trig_activate(struct led_classdev *led_cdev)
{
struct oneshot_trig_data *oneshot_data;
int rc;
// create an instance of the private data we need
oneshot_data = kzalloc(sizeof(*oneshot_data), GFP_KERNEL);
if (!oneshot_data)
return;
// save the pointer in the led class struct so it's available to other
// functions above
led_cdev->trigger_data = oneshot_data;
// attach the sysfs entries
rc = device_create_file(led_cdev->dev, &dev_attr_delay_on);
if (rc)
goto err_out_trig_data;
rc = device_create_file(led_cdev->dev, &dev_attr_delay_off);
if (rc)
goto err_out_delayon;
rc = device_create_file(led_cdev->dev, &dev_attr_invert);
if (rc)
goto err_out_delayoff;
rc = device_create_file(led_cdev->dev, &dev_attr_shot);
if (rc)
goto err_out_invert;
// C: this is what the driver used to do
#if 0
led_cdev->blink_delay_on = DEFAULT_DELAY;
led_cdev->blink_delay_off = DEFAULT_DELAY;
#endif
led_cdev->activated = true;
// C: from here to the return is what the modified driver must do
#ifdef MYLED_USESTRUCT
led_cdev->blink_delay_on = myled_init.delay_on;
led_cdev->blink_delay_off = myled_init.delay_off;
oneshot_data->invert = myled_init.invert;
#else
led_cdev->blink_delay_on = myled_init_delay_on;
led_cdev->blink_delay_off = myled_init_delay_off;
oneshot_data->invert = myled_init_invert;
#endif
// C: if invert is off, nothing to do -- just like before
// if invert is set, we implement this as if we just got an instantaneous
// write to the sysfs "invert" file (which would call led_invert_store
// above)
// C: this is a direct rip-off of the above led_invert_store function which
// we can _not_ call here directly because we don't have access to the
// data it needs for its arguments [at least, not conveniently]
// so, we extract the one line we actually need
if (oneshot_data->invert)
led_set_brightness_async(led_cdev, LED_FULL);
return;
// release everything if an error occurs
err_out_invert:
device_remove_file(led_cdev->dev, &dev_attr_invert);
err_out_delayoff:
device_remove_file(led_cdev->dev, &dev_attr_delay_off);
err_out_delayon:
device_remove_file(led_cdev->dev, &dev_attr_delay_on);
err_out_trig_data:
kfree(led_cdev->trigger_data);
}
// deactivate the trigger device
static void oneshot_trig_deactivate(struct led_classdev *led_cdev)
{
struct oneshot_trig_data *oneshot_data = led_cdev->trigger_data;
// release/destroy all the sysfs entries [and free the private data]
if (led_cdev->activated) {
device_remove_file(led_cdev->dev, &dev_attr_delay_on);
device_remove_file(led_cdev->dev, &dev_attr_delay_off);
device_remove_file(led_cdev->dev, &dev_attr_invert);
device_remove_file(led_cdev->dev, &dev_attr_shot);
kfree(oneshot_data);
led_cdev->activated = false;
}
/* Stop blinking */
led_set_brightness(led_cdev, LED_OFF);
}
// definition/control for trigger device registration
// C: changed the name to "myled_oneshot"
static struct led_trigger oneshot_led_trigger = {
.name = "myled_oneshot",
.activate = oneshot_trig_activate,
.deactivate = oneshot_trig_deactivate,
};
// module init function -- register the trigger device
static int __init oneshot_trig_init(void)
{
return led_trigger_register(&oneshot_led_trigger);
}
// module exit function -- unregister the trigger device
static void __exit oneshot_trig_exit(void)
{
led_trigger_unregister(&oneshot_led_trigger);
}
module_init(oneshot_trig_init);
module_exit(oneshot_trig_exit);
MODULE_AUTHOR("Fabio Baltieri <fabio.baltieri@gmail.com>");
MODULE_DESCRIPTION("One-shot LED trigger");
MODULE_LICENSE("GPL");
答案 1 :(得分:0)
正如您在ledtrig-oneshot.c中所看到的,延迟始终使用DEFAULT_DELAY
进行初始化。不幸的是,如果您希望能够在启动时配置不同的值,这是您必须实现的机制..
答案 2 :(得分:0)
正如Craig所说,它应该来自内核命令行选项,但是嵌入式系统可能存在问题,其中boot-loader传递命令行参数并且无法修改引导加载程序,它们通常是OTP。在那种情况下,我只看到两个选项
内核init函数中的硬编码
因为mac地址存储在eeprom中供nic驱动程序读取,如果值可以存储在闪存中(也不是)并且值在引导时读取。这可以在内核启动期间创建mtd分区后完成。