为了在sysfs下生成可控节点,方便上层调用。
原型:
类似的还有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 = 0, write = 0; __u16 val = 0; 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); } 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; } }