1
0
Fork 0

hwmon: Introduce SENSOR_DEVICE_ATTR_{RO, RW, WO} and variants

Introduce SENSOR_DEVICE_ATTR_{RO,RW,WO} and SENSOR_DEVICE_ATTR_2_{RO,RW,WO}
as simplified variants of SENSOR_DEVICE_ATTR and SENSOR_DEVICE_ATTR_2 to
simplify the source code, improve readbility, and reduce the chance of
inconsistencies.

Signed-off-by: Guenter Roeck <linux@roeck-us.net>
hifive-unleashed-5.1
Guenter Roeck 2016-12-27 15:28:19 -08:00
parent 2e9a41bbc1
commit a5c47c0d38
2 changed files with 55 additions and 8 deletions

View File

@ -299,17 +299,25 @@ functions is used.
The header file linux/hwmon-sysfs.h provides a number of useful macros to
declare and use hardware monitoring sysfs attributes.
In many cases, you can use the exsting define DEVICE_ATTR to declare such
attributes. This is feasible if an attribute has no additional context. However,
in many cases there will be additional information such as a sensor index which
will need to be passed to the sysfs attribute handling function.
In many cases, you can use the exsting define DEVICE_ATTR or its variants
DEVICE_ATTR_{RW,RO,WO} to declare such attributes. This is feasible if an
attribute has no additional context. However, in many cases there will be
additional information such as a sensor index which will need to be passed
to the sysfs attribute handling function.
SENSOR_DEVICE_ATTR and SENSOR_DEVICE_ATTR_2 can be used to define attributes
which need such additional context information. SENSOR_DEVICE_ATTR requires
one additional argument, SENSOR_DEVICE_ATTR_2 requires two.
SENSOR_DEVICE_ATTR defines a struct sensor_device_attribute variable.
This structure has the following fields.
Simplified variants of SENSOR_DEVICE_ATTR and SENSOR_DEVICE_ATTR_2 are available
and should be used if standard attribute permissions and function names are
feasible. Standard permissions are 0644 for SENSOR_DEVICE_ATTR[_2]_RW,
0444 for SENSOR_DEVICE_ATTR[_2]_RO, and 0200 for SENSOR_DEVICE_ATTR[_2]_WO.
Standard functions, similar to DEVICE_ATTR_{RW,RO,WO}, have _show and _store
appended to the provided function name.
SENSOR_DEVICE_ATTR and its variants define a struct sensor_device_attribute
variable. This structure has the following fields.
struct sensor_device_attribute {
struct device_attribute dev_attr;
@ -320,8 +328,8 @@ You can use to_sensor_dev_attr to get the pointer to this structure from the
attribute read or write function. Its parameter is the device to which the
attribute is attached.
SENSOR_DEVICE_ATTR_2 defines a struct sensor_device_attribute_2 variable,
which is defined as follows.
SENSOR_DEVICE_ATTR_2 and its variants define a struct sensor_device_attribute_2
variable, which is defined as follows.
struct sensor_device_attribute_2 {
struct device_attribute dev_attr;

View File

@ -33,10 +33,28 @@ struct sensor_device_attribute{
{ .dev_attr = __ATTR(_name, _mode, _show, _store), \
.index = _index }
#define SENSOR_ATTR_RO(_name, _func, _index) \
SENSOR_ATTR(_name, 0444, _func##_show, NULL, _index)
#define SENSOR_ATTR_RW(_name, _func, _index) \
SENSOR_ATTR(_name, 0644, _func##_show, _func##_store, _index)
#define SENSOR_ATTR_WO(_name, _func, _index) \
SENSOR_ATTR(_name, 0200, NULL, _func##_store, _index)
#define SENSOR_DEVICE_ATTR(_name, _mode, _show, _store, _index) \
struct sensor_device_attribute sensor_dev_attr_##_name \
= SENSOR_ATTR(_name, _mode, _show, _store, _index)
#define SENSOR_DEVICE_ATTR_RO(_name, _func, _index) \
SENSOR_DEVICE_ATTR(_name, 0444, _func##_show, NULL, _index)
#define SENSOR_DEVICE_ATTR_RW(_name, _func, _index) \
SENSOR_DEVICE_ATTR(_name, 0644, _func##_show, _func##_store, _index)
#define SENSOR_DEVICE_ATTR_WO(_name, _func, _index) \
SENSOR_DEVICE_ATTR(_name, 0200, NULL, _func##_store, _index)
struct sensor_device_attribute_2 {
struct device_attribute dev_attr;
u8 index;
@ -50,8 +68,29 @@ struct sensor_device_attribute_2 {
.index = _index, \
.nr = _nr }
#define SENSOR_ATTR_2_RO(_name, _func, _nr, _index) \
SENSOR_ATTR_2(_name, 0444, _func##_show, NULL, _nr, _index)
#define SENSOR_ATTR_2_RW(_name, _func, _nr, _index) \
SENSOR_ATTR_2(_name, 0644, _func##_show, _func##_store, _nr, _index)
#define SENSOR_ATTR_2_WO(_name, _func, _nr, _index) \
SENSOR_ATTR_2(_name, 0200, NULL, _func##_store, _nr, _index)
#define SENSOR_DEVICE_ATTR_2(_name,_mode,_show,_store,_nr,_index) \
struct sensor_device_attribute_2 sensor_dev_attr_##_name \
= SENSOR_ATTR_2(_name, _mode, _show, _store, _nr, _index)
#define SENSOR_DEVICE_ATTR_2_RO(_name, _func, _nr, _index) \
SENSOR_DEVICE_ATTR_2(_name, 0444, _func##_show, NULL, \
_nr, _index)
#define SENSOR_DEVICE_ATTR_2_RW(_name, _func, _nr, _index) \
SENSOR_DEVICE_ATTR_2(_name, 0644, _func##_show, _func##_store, \
_nr, _index)
#define SENSOR_DEVICE_ATTR_2_WO(_name, _func, _nr, _index) \
SENSOR_DEVICE_ATTR_2(_name, 0200, NULL, _func##_store, \
_nr, _index)
#endif /* _LINUX_HWMON_SYSFS_H */