I would like to expose some settings of my device via sysfs. If I understand it right, a driver can have multiple devices, so there should be one instance of the settings variable per device. This should be easy enough using DEVICE_ATTR
macro.
Checking the sources I noticed there is also DEVICE_INT_ATTR
and other with different type. I wonder what is the intended usage, as they use device_show_int
functions that get pointer to device, but don't actually use it:
ssize_t device_store_int(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t size)
{
struct dev_ext_attribute *ea = to_ext_attr(attr);
int ret;
long new;
ret = kstrtol(buf, 0, &new);
if (ret)
return ret;
if (new > INT_MAX || new < INT_MIN)
return -EINVAL;
*(int *)(ea->var) = new;
/* Always return full write size even if we didn't consume all */
return size;
}
EXPORT_SYMBOL_GPL(device_store_int);
I searched kernel sources for those macros, and it seems that they work with a global variable. For example DEVICE_INT_ATTR
is used in drivers/base/core.c for mca_cfg.tolerant
:
static DEVICE_INT_ATTR(tolerant, 0644, mca_cfg.tolerant);
but the mca_cfg
varaible is actually global, not tied to a device:
struct mca_config mca_cfg __read_mostly = {
.bootlog = -1,
/* ... */
.tolerant = 1,
.monarch_timeout = -1
};
which makes it look like a driver (not device) attribute.
I also checked commit that adds these macros but it did not help me much.
You correctly take that
DEVICE_INT_ATTR
and other macros from that family are for "global" attributes, whichstore
andshow
methods doesn't usedev
parameter.If you want to define attribute, which can be bound to several devices, then you could write your own
store
andshow
methods which gets information about the value fromdev
.E.g. by having device
you could expose its
repetition
field in the attribute using followingshow
method:Structure of such attribute could be initialized using
__ATTR
macro:Making "generic" attributes
Assume your device struct contains many
int
fields, which you want to expose via attributes:In that case you could generalize attribute definition, so you don't need to create many
show
(andstore
) functions.E.g. you could store offset of the exposed field in your attribute structure:
Using this field, you could rewrite
show
method as follows:So attributes can be declared as follows: