Commit 664f0549 authored by Srinivas Kandagatla's avatar Srinivas Kandagatla Committed by Greg Kroah-Hartman

nvmem: core: use is_bin_visible for permissions

By using is_bin_visible callback to set permissions will remove a
large list of attribute groups. These group permissions can be
dynamically derived in the callback.

Also add checks for read/write callbacks and set permissions accordingly.
Suggested-by: default avatarGreg KH <gregkh@linuxfoundation.org>
Signed-off-by: default avatarSrinivas Kandagatla <srinivas.kandagatla@linaro.org>
Link: https://lore.kernel.org/r/20200325131951.31887-2-srinivas.kandagatla@linaro.orgSigned-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent f60442dd
...@@ -396,7 +396,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) ...@@ -396,7 +396,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
nvmem->read_only = device_property_present(config->dev, "read-only") || nvmem->read_only = device_property_present(config->dev, "read-only") ||
config->read_only || !nvmem->reg_write; config->read_only || !nvmem->reg_write;
nvmem->dev.groups = nvmem_sysfs_get_groups(nvmem, config); nvmem->dev.groups = nvmem_sysfs_get_groups();
dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name); dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
......
...@@ -104,6 +104,28 @@ static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj, ...@@ -104,6 +104,28 @@ static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
return count; return count;
} }
static umode_t nvmem_bin_attr_is_visible(struct kobject *kobj,
struct bin_attribute *attr, int i)
{
struct device *dev = container_of(kobj, struct device, kobj);
struct nvmem_device *nvmem = to_nvmem_device(dev);
umode_t mode = 0400;
if (!nvmem->root_only)
mode |= 0044;
if (!nvmem->read_only)
mode |= 0200;
if (!nvmem->reg_write)
mode &= ~0200;
if (!nvmem->reg_read)
mode &= ~0444;
return mode;
}
/* default read/write permissions */ /* default read/write permissions */
static struct bin_attribute bin_attr_rw_nvmem = { static struct bin_attribute bin_attr_rw_nvmem = {
.attr = { .attr = {
...@@ -114,18 +136,19 @@ static struct bin_attribute bin_attr_rw_nvmem = { ...@@ -114,18 +136,19 @@ static struct bin_attribute bin_attr_rw_nvmem = {
.write = bin_attr_nvmem_write, .write = bin_attr_nvmem_write,
}; };
static struct bin_attribute *nvmem_bin_rw_attributes[] = { static struct bin_attribute *nvmem_bin_attributes[] = {
&bin_attr_rw_nvmem, &bin_attr_rw_nvmem,
NULL, NULL,
}; };
static const struct attribute_group nvmem_bin_rw_group = { static const struct attribute_group nvmem_bin_group = {
.bin_attrs = nvmem_bin_rw_attributes, .bin_attrs = nvmem_bin_attributes,
.attrs = nvmem_attrs, .attrs = nvmem_attrs,
.is_bin_visible = nvmem_bin_attr_is_visible,
}; };
static const struct attribute_group *nvmem_rw_dev_groups[] = { static const struct attribute_group *nvmem_dev_groups[] = {
&nvmem_bin_rw_group, &nvmem_bin_group,
NULL, NULL,
}; };
...@@ -138,21 +161,6 @@ static struct bin_attribute bin_attr_ro_nvmem = { ...@@ -138,21 +161,6 @@ static struct bin_attribute bin_attr_ro_nvmem = {
.read = bin_attr_nvmem_read, .read = bin_attr_nvmem_read,
}; };
static struct bin_attribute *nvmem_bin_ro_attributes[] = {
&bin_attr_ro_nvmem,
NULL,
};
static const struct attribute_group nvmem_bin_ro_group = {
.bin_attrs = nvmem_bin_ro_attributes,
.attrs = nvmem_attrs,
};
static const struct attribute_group *nvmem_ro_dev_groups[] = {
&nvmem_bin_ro_group,
NULL,
};
/* default read/write permissions, root only */ /* default read/write permissions, root only */
static struct bin_attribute bin_attr_rw_root_nvmem = { static struct bin_attribute bin_attr_rw_root_nvmem = {
.attr = { .attr = {
...@@ -163,21 +171,6 @@ static struct bin_attribute bin_attr_rw_root_nvmem = { ...@@ -163,21 +171,6 @@ static struct bin_attribute bin_attr_rw_root_nvmem = {
.write = bin_attr_nvmem_write, .write = bin_attr_nvmem_write,
}; };
static struct bin_attribute *nvmem_bin_rw_root_attributes[] = {
&bin_attr_rw_root_nvmem,
NULL,
};
static const struct attribute_group nvmem_bin_rw_root_group = {
.bin_attrs = nvmem_bin_rw_root_attributes,
.attrs = nvmem_attrs,
};
static const struct attribute_group *nvmem_rw_root_dev_groups[] = {
&nvmem_bin_rw_root_group,
NULL,
};
/* read only permission, root only */ /* read only permission, root only */
static struct bin_attribute bin_attr_ro_root_nvmem = { static struct bin_attribute bin_attr_ro_root_nvmem = {
.attr = { .attr = {
...@@ -187,31 +180,9 @@ static struct bin_attribute bin_attr_ro_root_nvmem = { ...@@ -187,31 +180,9 @@ static struct bin_attribute bin_attr_ro_root_nvmem = {
.read = bin_attr_nvmem_read, .read = bin_attr_nvmem_read,
}; };
static struct bin_attribute *nvmem_bin_ro_root_attributes[] = { const struct attribute_group **nvmem_sysfs_get_groups(void)
&bin_attr_ro_root_nvmem,
NULL,
};
static const struct attribute_group nvmem_bin_ro_root_group = {
.bin_attrs = nvmem_bin_ro_root_attributes,
.attrs = nvmem_attrs,
};
static const struct attribute_group *nvmem_ro_root_dev_groups[] = {
&nvmem_bin_ro_root_group,
NULL,
};
const struct attribute_group **nvmem_sysfs_get_groups(
struct nvmem_device *nvmem,
const struct nvmem_config *config)
{ {
if (config->root_only) return nvmem_dev_groups;
return nvmem->read_only ?
nvmem_ro_root_dev_groups :
nvmem_rw_root_dev_groups;
return nvmem->read_only ? nvmem_ro_dev_groups : nvmem_rw_dev_groups;
} }
/* /*
......
...@@ -36,17 +36,13 @@ struct nvmem_device { ...@@ -36,17 +36,13 @@ struct nvmem_device {
#define FLAG_COMPAT BIT(0) #define FLAG_COMPAT BIT(0)
#ifdef CONFIG_NVMEM_SYSFS #ifdef CONFIG_NVMEM_SYSFS
const struct attribute_group **nvmem_sysfs_get_groups( const struct attribute_group **nvmem_sysfs_get_groups(void);
struct nvmem_device *nvmem,
const struct nvmem_config *config);
int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem, int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
const struct nvmem_config *config); const struct nvmem_config *config);
void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem, void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem,
const struct nvmem_config *config); const struct nvmem_config *config);
#else #else
static inline const struct attribute_group **nvmem_sysfs_get_groups( static inline const struct attribute_group **nvmem_sysfs_get_groups(void)
struct nvmem_device *nvmem,
const struct nvmem_config *config)
{ {
return NULL; return NULL;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment