• Linux sysfs device_attribute


    /***************************************************************************                        
     *                    Linux sysfs device_attribute
     * 声明:                                                                                        
     *     本文主要是记录linux驱动中如何在sysfs中生成设备属性。
     *                                                                                                  
     *                                         2016-2-22 深圳 南山平山村 曾剑锋               
     **************************************************************************/                        
                                                                                                        
                                                                                                        
    static ssize_t show_firmware_version(struct device *dev,            <----------+                    
            struct device_attribute *attr, char *buf)                              |                    
    {                                                                              |                    
        struct bq27x00_device_info *di = dev_get_drvdata(dev);                     |                    
        int ver;                                                                   |                    
                                                                                   |                    
        ver = bq27x00_battery_read_fw_version(di);                                 |                    
                                                                                   |                    
        return sprintf(buf, "%d
    ", ver);                                          |                    
    }                                                                              |                    
                                                                                   |                    
    static ssize_t show_dataflash_version(struct device *dev,           <----------*-+                  
            struct device_attribute *attr, char *buf)                              | |                  
    {                                                                              | |                  
        struct bq27x00_device_info *di = dev_get_drvdata(dev);                     | |                  
        int ver;                                                                   | |                  
                                                                                   | |                  
        ver = bq27x00_battery_read_dataflash_version(di);                          | |                  
                                                                                   | |                  
        return sprintf(buf, "%d
    ", ver);                                          | |                  
    }                                                                              | |                  
                                                                                   | |                  
    static ssize_t show_device_type(struct device *dev,                <-----------*-*-+                
            struct device_attribute *attr, char *buf)                              | | |                
    {                                                                              | | |                
        struct bq27x00_device_info *di = dev_get_drvdata(dev);                     | | |                
        int dev_type;                                                              | | |                
                                                                                   | | |                
        dev_type = bq27x00_battery_read_device_type(di);                           | | |                
                                                                                   | | |                
        return sprintf(buf, "%d
    ", dev_type);                                     | | |                
    }                                                                              | | |                
                                                                                   | | |                
    static ssize_t show_reset(struct device *dev,                       <----------*-*-*-+              
            struct device_attribute *attr, char *buf)                              | | | |              
    {                                                                              | | | |              
        struct bq27x00_device_info *di = dev_get_drvdata(dev);                     | | | |              
                                                                                   | | | |              
        bq27x00_battery_reset(di);                                                 | | | |              
                                                                                   | | | |              
        return sprintf(buf, "okay
    ");                                             | | | |              
    }                                                                              | | | |              
                                                                                   | | | |              
    static DEVICE_ATTR(fw_version, S_IRUGO, show_firmware_version, NULL);   <------+ | | |              
    static DEVICE_ATTR(df_version, S_IRUGO, show_dataflash_version, NULL);  <------|-+ | |              
    static DEVICE_ATTR(device_type, S_IRUGO, show_device_type, NULL);       <------|-|-+ |              
    static DEVICE_ATTR(reset, S_IRUGO, show_reset, NULL);                   <------|-|-|-+              
                                                                                   | | | |              
    static struct attribute *bq27x00_attributes[] = {              <---------+     | | | |              
        &dev_attr_fw_version.attr,                                  ---------*-----+ | | |              
        &dev_attr_df_version.attr,                                  ---------*-------+ | |              
        &dev_attr_device_type.attr,                                 ---------*---------+ |              
        &dev_attr_reset.attr,                                       ---------*-----------+              
        NULL                                                                 |                          
    };                                                                       |                          
                                                                             |                          
    static const struct attribute_group bq27x00_attr_group = {     <---------*------+                   
        .attrs = bq27x00_attributes,                                   ------+      |                   
    };                                                                              |                   
                                                                                    |                   
    static int __init bq27x00_battery_probe(struct i2c_client *client,              |                   
                     const struct i2c_device_id *id)                                |                   
    {                                                                               |                   
        ......                                                                      |                   
        retval = sysfs_create_group(&client->dev.kobj, &bq27x00_attr_group); -------+                   
        if (retval)                                                                                     
            dev_err(&client->dev, "could not create sysfs files
    ");                                    
                                                                                                        
        return 0;                                                                                       
        ......
    }                                                                                                   
    
    /**
     *  root@android:/sys/bus/i2c/devices/2-0055 # ls
     *  device_type
     *  df_version
     *  driver
     *  fw_version
     *  modalias
     *  name
     *  power
     *  power_supply
     *  reset
     *  subsystem
     *  uevent
     *  root@android:/sys/bus/i2c/devices/2-0055 # 
     */
  • 相关阅读:
    TAdvStringGrid控件使用技巧[转]
    char, array of char, PChar
    C#操作xml文件入门
    用C#实现生成PDF文档(附源码)
    使用DDE技术,为您的应用程序增辉
    我要减肥了
    查询一个表中相同的记录
    获得Windows特殊目录
    用VB创建快捷方式(无需第三方DLL)
    为office编写插件
  • 原文地址:https://www.cnblogs.com/zengjfgit/p/5207631.html
Copyright © 2020-2023  润新知