==================================
本文系本站原创,欢迎转载!
转载请注明出处:http://blog.csdn.net/gdt_a20
==================================
时代不同了,连led都成子系统了,针对内核提供的通用模型,分析一下,好久没写文章了也!
代码位于drivers/leds下,看一下Makefile 模型文件主要是:
# LED Core
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)函数在/sys/class目录下产生了leds这个子目录,按照led模型注册的设备都会出现在该目录下
static struct device_attribute led_class_attrs[] = {};
可见,属性函数的通用设置函数为led_brightness_store,获得函数位led_brightness_show,大同小异:
static ssize_t led_brightness_store(struct device *dev,}
跟进一下led_set_brightness函数:
static inline void led_set_brightness(struct led_classdev *led_cdev,可见最终确实是回调的 led_cdev->brightness_set来完成的该属性的设置。
下面看一下给我们留的接口,注册函数:
int led_classdev_register(struct device *parent, struct led_classdev *led_cdev)总结:基本上简要分析了一下led子系统,代码不多,也很容易看,主要自己端的probe函数要配置好io口的功能,填充好led_cdev这个东东,然后注册就可以了 ^.^~