DEVICE_ATTR设置设备属性
DEVICE_ATTR设置设备属性
为了在sysfs下生成可控节点,方便上层调用。
sysfs是一个基于RAM的文件系统,它和Kobject一起,可以将Kernel的数据结构导出到用户空间,以文件目录结构的形式,提供对这些数据结构(以及数据结构的属性)的访问支持。Linux设备模型(4)_sysfs
原型:
#define DEVICE_ATTR(_name, _mode, _show, _store) struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
类似的还有DRIVER_ATTR,BUS_ATTR,CLASS_ATTR。区别就是,DEVICE_ATTR对应的文件在/sys/devices/目录中对应的device下面。而其他几个分别在driver,bus,class中对应的目录下。(待确认)
我实际测试,用DEVICE_ATTR增加的I2C设备,节点是在/sys/bus/i2c/devices/0-0060/fm1288/下的。按照上面的说法,应该fm1288节点应该是在/sys/device下
用法:
static ssize_t fm1288_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
unsigned int reg = , write = ;
__u16 val = ;
int ret;
sscanf(buf,"%x %x %x", &write, ®, (unsigned int *)&val);//用sscanf读取sysfs写入的值 echo "1 2 3" > fm1288_mode
fm2018_write(i2c, reg, val);
fm2018_read(i2c, reg, &val);
}
fm1288_showstatic ssize_t fm1288_show(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
//read mode
ssprintf("%d", mode);
}
static DEVICE_ATTR(fm1288, S_IWUSR,//模式可以只读0444,只写0222,或者读写都行0666
fm1288_show, //NULL,不需要该函数可以写NULL
fm1288_store);
//注意_show和_store的命名一般习惯都是来自name+ _show / _store
static int fm2018_i2c_probe(struct i2c_client *i2c,
const struct i2c_device_id *id)
{
struct device *dev = &i2c->dev;
ret = device_create_file(&i2c->dev, &dev_attr_register);
if (ret) {
dev_err(&i2c->dev, "failed to create sysfs files\n");
return ret;
}
}
也可以写多个属性,这时,使用sysfs_create_group注册就很方便,代替device_create_file
static DEVICE_ATTR(xtilt,
S_IRUGO | S_IWUGO, show_tabletXtilt, store_tabletXtilt);
static DEVICE_ATTR(ytilt,
S_IRUGO | S_IWUGO, show_tabletYtilt, store_tabletYtilt);
static struct attribute *aiptek_attributes[] = {
&dev_attr_xtilt.attr,//名字 dev_attr_name.attr
&dev_attr_ytilt.attr,
NULL
};
static struct attribute_group aiptek_attribute_group = {
.attrs = aiptek_attributes,
};
aiptek_probe(struct usb_interface *intf, const struct usb_device_id *id)
{
int err = -ENOMEM;
err = sysfs_create_group(&intf->dev.kobj, &aiptek_attribute_group);
if (err) {
dev_warn(&intf->dev, "cannot create sysfs group err: %d\n",
err);
goto fail3;
}
fail3: return -;
}
DEVICE_ATTR设置设备属性的更多相关文章
- 阿里云物联网 .NET Core 客户端 | CZGL.AliIoTClient:5. 设置设备属性
文档目录: 说明 1. 连接阿里云物联网 2. IoT 客户端 3. 订阅Topic与响应Topic 4. 设备上报属性 4.1 上报位置信息 5. 设置设备属性 6. 设备事件上报 7. 服务调用 ...
- 设置background属性使用selector的时候内置?attr报错的解决方案
当我们设置background属性的时候可以设置background="@color/black" 也可以设置 background="@drawable/selecto ...
- CUDA Samples: 获取设备属性信息
通过调用CUDA的cudaGetDeviceProperties函数可以获得指定设备的相关信息,此函数会根据GPU显卡和CUDA版本的不同得到的结果也有所差异,下面code列出了经常用到的设备信息: ...
- CUDA开发:了解设备属性
原文链接 今天介绍一下CUDA设备的相关属性,只有熟悉了硬件是相关属性,是怎么工作的,就能写出更适合硬件工作的代码.cudaDeviceProp这个结构体记录了设备的相关属性. struct cuda ...
- termios, tcgetattr, tcsetattr, tcsendbreak, tcdrain, tcflush, tcflow, cfmakeraw, cfgetospeed, cfgetispeed, cfsetispeed, cfsetospeed - 获取和设置终端属性,行控制,获取和设置波特率
SYNOPSIS 总览 #include <termios.h> #include <unistd.h> int tcgetattr(int fd, struct termio ...
- 【转载】CentOS下查看电脑硬件设备属性命令
CentOS下查看电脑硬件设备属性命令2018年09月13日 17:48:31 乔烨 阅读数 510如何在linux下查看电脑硬件设备属性 # uname -a # 查看内核/操作系统/CPU信息 # ...
- 通过设置CSS属性让DIV水平居中
通过设置CSS属性让DIV水平居中 ---------------------- <html> <head> <title></title> <m ...
- 两种方法设置disabled属性
//两种方法设置disabled属性 $('#fileup').attr("disabled",true); $('#fileup').attr("disabled&qu ...
- java中的反射,知道类名创建类,还可以设置私有属性的值
刚刚学到了反射,感觉反射的功能好强大,所以想写篇博客记录下自己的学习成果. 利用反射来创建对象. Class c1=Class.forName("test.Person");//通 ...
随机推荐
- 剑指Offer(4)——替换空格
题目: 请实现一个函数,把字符串中的每个空格替换成"%20".例如输入“We are happy.”,则输出“We%20are%20happy.”. 思路: 如果按照顺序从前往后依 ...
- k8s基础环境搭建
环境准备 服务器之间时间同步 1. 关闭防火墙 systemctl stop firewalld setenforce 0 2. 设置yum源 三台机器都要设置一个master两个node节点 下 ...
- Educational Codeforces Round 65 (Rated for Div. 2)
A:签到. #include<bits/stdc++.h> using namespace std; #define ll long long #define inf 1000000010 ...
- Redis的bind的误区(转)
原文1:https://blog.csdn.net/cw_hello1/article/details/83444013 原文2:https://www.cnblogs.com/suiyueqiann ...
- .NET Core 发布部署问题
运行环境 操作系统 开发工具 frameworks .Net Core SDK 版本 托管运行 本地 ...
- python实现tcp代理
1.代理流程图 2.实现代码 #! usr/bin/python2 import sys import socket import thread #handle local buffer def re ...
- Redis面试题记录--缓存双写情况下导致数据不一致问题
转载自:https://blog.csdn.net/lzhcoder/article/details/79469123 https://blog.csdn.net/u013374645/article ...
- 面试总结关于Spring面试问题(精选)
1.什么是Spring? Spring是一个反转控制IOC和AOP的开发框架和平台. 2.解释一下Spring? 轻量 : Spring 在大小和透明度上是轻量的,Spring基本核心版本大概只有1M ...
- git日常命令
克隆项目:git clone addr dir 切换分支,分支并没有 git checkout -b production origin/production 分支已存在,单纯切换分支 git che ...
- Image Processing and Analysis_8_Edge Detection:Statistical edge detection_ learning and evaluating edge cues——2003
此主要讨论图像处理与分析.虽然计算机视觉部分的有些内容比如特 征提取等也可以归结到图像分析中来,但鉴于它们与计算机视觉的紧密联系,以 及它们的出处,没有把它们纳入到图像处理与分析中来.同样,这里面也有 ...