gdt-A20

导航

linux设备模型之led子系统

  

==================================
本文系本站原创,欢迎转载!
转载请注明出处: http://www.cnblogs.com/gdt-a20

==================================

时代不同了,连led都成子系统了,针对内核提供的通用模型,分析一下,好久没写文章了也!

代码位于drivers/leds下,看一下Makefile 模型文件主要是:

# LED Core

obj-$(CONFIG_NEW_LEDS)            += led-core.o

obj-$(CONFIG_LEDS_CLASS)        += led-class.o

obj-$(CONFIG_LEDS_TRIGGERS)        += led-triggers.o

     直接看led-core文件吧,这个文件无比个性,主体内容四行

 DECLARE_RWSEM(leds_list_lock);
EXPORT_SYMBOL_GPL(leds_list_lock);
LIST_HEAD(leds_list);                                         //链接所有led的全局链表
EXPORT_SYMBOL_GPL(leds_list);

    再来看下led-class.c函数,这里先来介绍一下描述led的核心结构体

struct led_classdev {
    const char        *name;                                                                                   //名字
    int             brightness;                                                                                     //亮度值,也可以用来表示开关特性
    int             max_brightness;                                                                           //允许的最大亮度值
    int             flags;                                                                                               //标志

    /* Lower 16 bits reflect status */
#define LED_SUSPENDED        (1 << 0)
    /* Upper 16 bits reflect control information */
#define LED_CORE_SUSPENDRESUME    (1 << 16)

    /* Set LED brightness level */
    /* Must not sleep, use a workqueue if needed */
    void        (*brightness_set)(struct led_classdev *led_cdev,                 //核心回调函数,当设置/sys/class/leds/下的led接口里的brightness属性文件时,会回调该函数
                      enum led_brightness brightness);
    /* Get LED brightness level */
    enum led_brightness (*brightness_get)(struct led_classdev *led_cdev);         //核心回调函数,当获得led当前值时会调用

    /* Activate hardware accelerated blink, delays are in
     * miliseconds and if none is provided then a sensible default
     * should be chosen. The call can adjust the timings if it can't
     * match the values specified exactly. */
    int        (*blink_set)(struct led_classdev *led_cdev,
                     unsigned long *delay_on,
                     unsigned long *delay_off);

    struct device        *dev;                                                                                  //嵌入的标准设备模型
    struct list_head     node;            /* LED Device list */                                //上面提到的全局led设备的挂接点
    const char        *default_trigger;    /* Trigger to use */

#ifdef CONFIG_LEDS_TRIGGERS
    /* Protects the trigger data below */
    struct rw_semaphore     trigger_lock;    

    struct led_trigger    *trigger;
    struct list_head     trig_list;
    void            *trigger_data;
#endif
};

 

好了,下面说下初始化函数:

static int __init leds_init(void)
{
    leds_class = class_create(THIS_MODULE, "leds");    
    if (IS_ERR(leds_class))
        return PTR_ERR(leds_class);
    leds_class->suspend = led_suspend;
    leds_class->resume = led_resume;
    leds_class->dev_attrs = led_class_attrs;                                                 //属性文件,sys下的接口,重点看一下
    return 0;
}

函数在/sys/class目录下产生了leds这个子目录,按照led模型注册的设备都会出现在该目录下

static struct device_attribute led_class_attrs[] = {
    __ATTR(brightness, 0644, led_brightness_show, led_brightness_store),
    __ATTR(max_brightness, 0644, led_max_brightness_show,
            led_max_brightness_store),
#ifdef CONFIG_LEDS_TRIGGERS
    __ATTR(trigger, 0644, led_trigger_show, led_trigger_store),
#endif
    __ATTR_NULL,

};

可见,属性函数的通用设置函数为led_brightness_store,获得函数位led_brightness_show,大同小异:

static ssize_t led_brightness_store(struct device *dev,
        struct device_attribute *attr, const char *buf, size_t size)
{
    struct led_classdev *led_cdev = dev_get_drvdata(dev);                    //获得led设备信息
    ssize_t ret = -EINVAL;
    char *after;
    unsigned long state = simple_strtoul(buf, &after, 10);
    size_t count = after - buf;

    if (isspace(*after))
        count++;

    if (count == size) {
        ret = count;

        if (state == LED_OFF)
            led_trigger_remove(led_cdev);
        led_set_brightness(led_cdev, state);                                             
    }

    return ret;

}

跟进一下led_set_brightness函数:

static inline void led_set_brightness(struct led_classdev *led_cdev,
                    enum led_brightness value)
{
    if (value > led_cdev->max_brightness)
        value = led_cdev->max_brightness;
    led_cdev->brightness = value;
    if (!(led_cdev->flags & LED_SUSPENDED)) {
#ifdef CONFIG_HAS_EARLYSUSPEND
        if (queue_brightness_change(led_cdev, value) != 0)
#endif
            led_cdev->brightness_set(led_cdev, value);
    }
}

可见最终确实是回调的 led_cdev->brightness_set来完成的该属性的设置。

下面看一下给我们留的接口,注册函数:

int led_classdev_register(struct device *parent, struct led_classdev *led_cdev)
{
    led_cdev->dev = device_create(leds_class, parent, 0, led_cdev,
                      "%s", led_cdev->name);                                                                           //在leds目录下产生该设备的目录项
    if (IS_ERR(led_cdev->dev))
        return PTR_ERR(led_cdev->dev);

#ifdef CONFIG_LEDS_TRIGGERS
    init_rwsem(&led_cdev->trigger_lock);
#endif
    /* add to the list of leds */
    down_write(&leds_list_lock);
    list_add_tail(&led_cdev->node, &leds_list);                                                           //把该设备加入全局链表
    up_write(&leds_list_lock);

    if (!led_cdev->max_brightness)
        led_cdev->max_brightness = LED_FULL;                                                         //最大值如果没设置,则设置成255

    led_update_brightness(led_cdev);                              

#ifdef CONFIG_LEDS_TRIGGERS
    led_trigger_set_default(led_cdev);
#endif

    printk(KERN_DEBUG "Registered led device: %s\n",
            led_cdev->name);

    return 0;
}

总结:基本上简要分析了一下led子系统,代码不多,也很容易看,主要自己端的probe函数要配置好io口的功能,填充好led_cdev这个东东,然后注册就可以了 ^.^~

posted on 2011-07-11 16:24  gdt-A20  阅读(2332)  评论(0编辑  收藏  举报