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\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 # */