Linux sysfs device_attribute

简介: /*************************************************************************** * Linux sysfs device_attribute * 声明: * 本文主要是记录linux驱动中如何在sysfs中生成设备属性。
/***************************************************************************                        
 *                    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\n", 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\n", 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\n", 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\n");                                             | | | |              
}                                                                              | | | |              
                                                                               | | | |              
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\n");                                    
                                                                                                    
    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 # 
 */

 

目录
打赏
0
0
0
0
12
分享
相关文章
linux内核sysfs详解【转】
转自:http://blog.csdn.net/skyflying2012/article/details/11783847 "sysfs is a ram-based filesystem initially based on ramfs.
1479 0
|
10天前
|
Linux查看内存命令
1. free free命令是最常用的查看内存使用情况的命令。它显示系统的总内存、已使用内存、空闲内存和交换内存的总量。 free -h • -h 选项:以易读的格式(如GB、MB)显示内存大小。 输出示例: total used free shared buff/cache available Mem: 15Gi 4.7Gi 4.1Gi 288Mi 6.6Gi 9.9Gi Swap: 2.0Gi 0B 2.0Gi • to
25 2
|
11天前
|
Linux中yum、rpm、apt-get、wget的区别,yum、rpm、apt-get常用命令,CentOS、Ubuntu中安装wget
通过本文,我们详细了解了 `yum`、`rpm`、`apt-get`和 `wget`的区别、常用命令以及在CentOS和Ubuntu中安装 `wget`的方法。`yum`和 `apt-get`是高层次的包管理器,分别用于RPM系和Debian系发行版,能够自动解决依赖问题;而 `rpm`是低层次的包管理工具,适合处理单个包;`wget`则是一个功能强大的下载工具,适用于各种下载任务。在实际使用中,根据系统类型和任务需求选择合适的工具,可以大大提高工作效率和系统管理的便利性。
76 25
|
25天前
|
Linux系统之whereis命令的基本使用
Linux系统之whereis命令的基本使用
61 23
Linux系统之whereis命令的基本使用
深入解析:Linux网络配置工具ifconfig与ip命令的全面对比
虽然 `ifconfig`作为一个经典的网络配置工具,简单易用,但其功能已经不能满足现代网络配置的需求。相比之下,`ip`命令不仅功能全面,而且提供了一致且简洁的语法,适用于各种网络配置场景。因此,在实际使用中,推荐逐步过渡到 `ip`命令,以更好地适应现代网络管理需求。
51 11
|
3月前
|
linux查看目录下的文件夹命令,find查找某个目录,但是不包括这个目录本身?
通过本文的介绍,您应该对如何在 Linux 系统中查看目录下的文件夹以及使用 `find` 命令查找特定目录内容并排除该目录本身有了清晰的理解。掌握这些命令和技巧,可以大大提高日常文件管理和查找操作的效率。 在实际应用中,灵活使用这些命令和参数,可以帮助您快速定位和管理文件和目录,满足各种复杂的文件系统操作需求。
186 8
|
3月前
|
Linux 各发行版安装 ping 命令指南
如何在不同 Linux 发行版(Ubuntu/Debian、CentOS/RHEL/Fedora、Arch Linux、openSUSE、Alpine Linux)上安装 `ping` 命令,详细列出各发行版的安装步骤和验证方法,帮助系统管理员和网络工程师快速排查网络问题。
277 20
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等