device_create_file
DEVICE_ATTR
**DEVICE_ATTR宏展开**
#define DEVICE_ATTR(_name, _mode, _show, _store) \
struct device_attribute dev_attr_##_name = { \
.attr = {.name = __stringify(_name), .mode = _mode }, \
.show = _show, \
.store = _store, \
}
device_create_file要在probe中调用。
err = device_create_file(&dev->dev, &dev_attr_test);
if (err) {
dev_err(&dev->dev, "sys file creation failed\n");
return -ENODEV;
}
dev_attr_test的实现
static DEVICE_ATTR(test, 0664, test_show, test_store);
static ssize_t test_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
int temp[3];
struct test_data *gpio_data_ptr = dev_get_drvdata(dev);
temp[0] = gpio_get_value(gpio_data_ptr->gpio0);
temp[1] = gpio_get_value(gpio_data_ptr->gpio1);
temp[2] = gpio_get_value(gpio_data_ptr->gpio2);
return snprintf(buf, 4, "%d%d%d", temp[0], temp[1],
temp[2]);
}
static ssize_t test_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct test *gpio_data_ptr = dev_get_drvdata(dev);
gpio_set_value(gpio_data_ptr->gpio0, (buf[0] == '1') ? 1 : 0);
gpio_set_value(gpio_data_ptr->gpio1, (buf[1] == '1') ? 1 : 0);
gpio_set_value(gpio_data_ptr->gpio2, (buf[2] == '1') ? 1 : 0)
return size;
}
static DEVICE_ATTR(test, 0664, test_show, test_store);