Commit db372f60 authored by Federico Vaga's avatar Federico Vaga Committed by Federico Vaga

sysfs: add range value

Checking that a given value is in a range is a typical
operation. This patch include this control in the framework
Signed-off-by: Federico Vaga's avatarFederico Vaga <federico.vaga@cern.ch>
Acked-by: Alessandro Rubini's avatarAlessandro Rubini <rubini@gnudd.com>
parent 04a212e5
......@@ -32,8 +32,8 @@ ZIO_ATTR_DEFINE_STD(ZIO_DEV, zzero_zattr_cset8) = {
ZIO_ATTR_DEFINE_STD(ZIO_DEV, zzero_zattr_cset32) = {
/* 32 bit -> ssize = 4 */
ZIO_ATTR(zdev, ZIO_ATTR_NBITS, ZIO_RO_PERM, 0, 32),
ZIO_ATTR(zdev, ZIO_ATTR_OFFSET, ZIO_RW_PERM, 0, 0),
ZIO_ATTR(zdev, ZIO_ATTR_GAIN, ZIO_RW_PERM, 0, 1),
ZIO_ATTR_RNG(zdev, ZIO_ATTR_OFFSET, ZIO_RW_PERM, 0, 0, 0, 100),
ZIO_ATTR_RNG(zdev, ZIO_ATTR_GAIN, ZIO_RW_PERM, 0, 1, 1, 10),
};
/* This attribute is the sequence point for input channel number 0 of cset 2 */
......
......@@ -41,6 +41,8 @@ struct zio_attribute {
int index;
unsigned long id;
uint32_t value;
uint32_t min; /**< minimum valid value */
uint32_t max; /**< maximum valid value */
const struct zio_sysfs_operations *s_op;
};
#define ZIO_ATTR_INDEX_NONE -1
......@@ -91,30 +93,36 @@ extern const char zio_zbuf_attr_names[_ZIO_BUF_ATTR_STD_NUM][ZIO_NAME_LEN];
* @ZIO_ATTR_EXT: define a zio extended attribute
* @ZIO_PARAM_EXT: define a zio attribute parameter (not included in ctrl)
*/
#define ZIO_ATTR(zobj, _type, _mode, _add, _val)[_type] = { \
.attr = { \
.attr = { \
.name = zio_##zobj##_attr_names[_type], \
.mode = _mode \
}, \
}, \
.id = _add, \
.value = _val, \
.flags = ZIO_ATTR_CONTROL, \
}
#define ZIO_ATTR_EXT(_name, _mode, _add, _val) { \
.attr = { .attr = {.name = _name, .mode = _mode},}, \
.id = _add, \
.value = _val, \
.flags = ZIO_ATTR_CONTROL, \
}
#define ZIO_PARAM_EXT(_name, _mode, _add, _val) { \
#define ZIO_ATTR_DEC(_name, _mode, _add, _val, _min, _max, _flag) { \
.attr = { .attr = {.name = _name, .mode = _mode},}, \
.id = _add, \
.value = _val, \
.flags = 0, \
.min = _min, \
.max = _max, \
.flags = _flag, \
}
#define ZIO_ATTR(zobj, _type, _mode, _add, _val)[_type] = \
ZIO_ATTR_DEC(zio_##zobj##_attr_names[_type], _mode, _add, _val, \
0, 0, ZIO_ATTR_CONTROL)
#define ZIO_ATTR_EXT(_name, _mode, _add, _val) \
ZIO_ATTR_DEC(_name, _mode, _add, _val, 0, 0, ZIO_ATTR_CONTROL)
#define ZIO_PARAM_EXT(_name, _mode, _add, _val) \
ZIO_ATTR_DEC(_name, _mode, _add, _val, 0, 0, 0)
#define ZIO_ATTR_RNG(zobj, _type, _mode, _add, _val, _min, _max)[_type] = \
ZIO_ATTR_DEC(zio_##zobj##_attr_names[_type], _mode, _add, _val, \
_min, _max, ZIO_ATTR_CONTROL)
#define ZIO_ATTR_EXT_RNG(_name, _mode, _add, _val, _min, _max) \
ZIO_ATTR_DEC(_name, _mode, _add, _val, _min, _max, ZIO_ATTR_CONTROL)
#define ZIO_PARAM_RNG(_name, _mode, _add, _val, _min, _max) \
ZIO_ATTR_DEC(_name, _mode, _add, _val, _min, _max, 0)
/*
* This macro defines the standard attribute 'version' for an attribute_set of
* a zio object. In the macro there is an explicit reference to the device
......
......@@ -636,6 +636,7 @@ static ssize_t zattr_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
struct zio_obj_head *head = to_zio_head(dev);
struct zio_attribute *zattr = to_zio_zattr(attr);
spinlock_t *lock;
long val;
int err;
......@@ -643,9 +644,21 @@ static ssize_t zattr_store(struct device *dev, struct device_attribute *attr,
if (kstrtol(buf, 0, &val))
return -EINVAL;
/*
* If the given value exceed the attribute range, then it's
* an invalid value. When 'min == max' it means that the attribute
* hasn't a range
*/
if (zattr->min != zattr->max &&
(val < zattr->min || val > zattr->max)) {
dev_err(dev, "Value %u exceed range [%u, %u]\n",
(uint32_t)val, zattr->min, zattr->max);
return -EINVAL;
}
lock = __zio_get_dev_spinlock(head);
spin_lock(lock);
err = __zio_conf_set(head, to_zio_zattr(attr), (uint32_t)val);
err = __zio_conf_set(head, zattr, (uint32_t)val);
spin_unlock(lock);
return err ? err : count;
......
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