分析MTK leds驱动(引出 sysfs 的创建过程)

来源:互联网 发布:mac mini a1103 编辑:程序博客网 时间:2024/05/21 23:31

一. 在分析MTK leds驱动 leds_drv.c 时,注意到以下代码:

struct led_classdev {    struct device        *dev;    ……}struct mt65xx_led_data {    struct led_classdev cdev;    struct cust_mt65xx_led cust;    struct work_struct work;    int level;    int delay_on;    int delay_off;};enum mt65xx_led_type {    MT65XX_LED_TYPE_RED = 0,    MT65XX_LED_TYPE_GREEN,    MT65XX_LED_TYPE_BLUE,    MT65XX_LED_TYPE_JOGBALL,    MT65XX_LED_TYPE_KEYBOARD,    MT65XX_LED_TYPE_BUTTON,    MT65XX_LED_TYPE_LCD,    MT65XX_LED_TYPE_TOTAL,};struct mt65xx_led_data *g_leds_data[MT65XX_LED_TYPE_TOTAL];/* 上面是与代码相关的数据结构 */static ssize_t store_breath_enable(struct device *dev, struct device_attribute *attr,const char *buf, size_t size){char *pvalue = NULL;unsigned int reg_value = 0;if (buf != NULL && size != 0) {reg_value = simple_strtoul(buf, &pvalue, 0);if (*pvalue) {if(reg_value == 1)i2c_smbus_write_byte_data(breath_client, AW2013_GCR, 0x01);else if(reg_value == 0){i2c_smbus_write_byte_data(breath_client, AW2013_RSTR, 0x55);i2c_smbus_write_byte_data(breath_client, AW2013_GCR, 0x0);}elseLEDS_DRV_DEBUG("please no input unvaild value expect 1 and 0\n");}}return size;}static ssize_t show_breath_enable(struct device *dev, struct device_attribute *attr, char *buf){return 0;}static DEVICE_ATTR(breath_enable, 0664, show_breath_enable, store_breath_enable);static int mt65xx_leds_probe(struct platform_device *pdev){if (strcmp(g_leds_data[i]->cdev.name, "red") == 0) {        ret = device_create_file(g_leds_data[i]->cdev.dev, &dev_attr_breath_enable);/* 使用device_create_file函数, 会在 /sys/class/leds 目录下创建一个 breath_enable 接口,可以使用 cat 或 echo 来实现用户和内核的交互 */        if (ret)LEDS_DRV_DEBUG("[LED]device_create_file breath_enable fail!\n");        }}

二. 通过adb shell连接手机,cd /sys/class/leds/red,可以看到 breath_enable这个文件,使用 echo 或 cat 来操作这个文件。







原创粉丝点击