Commit 1b891f4c authored by Derek J. Clark's avatar Derek J. Clark Committed by Jiri Kosina
Browse files

include: device.h: Add named device attributes



Adds DEVICE_ATTR_[RW|RO|WO]_NAMED macros for adding attributes that
reuse the same sysfs name in a driver under separate subdirectories.

When dealing with some devices it can be useful to be able to reuse
the same name for similar attributes under a different subdirectory.
For example, a single logical HID endpoint may provide a configuration
interface for multiple physical devices. In such a case it is useful to
provide symmetrical attribute names under different subdirectories on
the configuration device. The Lenovo Legion Go is one such device,
providing configuration to a detachable left controller, detachable
right controller, the wireless transmission dongle, and the MCU. It is
therefore beneficial to treat each of these as individual devices in
the driver, providing a subdirectory for each physical device in the
sysfs. As some attributes are reused by each physical device, it
provides a much cleaner interface if the same driver can reuse the same
attribute name in sysfs while uniquely distinguishing the store/show
functions in the driver, rather than repeat string portions.

Example new WO attrs:
ATTRS{left_handle/reset}=="(not readable)"
ATTRS{right_handle/reset}=="(not readable)"
ATTRS{tx_dongle/reset}=="(not readable)"

vs old WO attrs in a subdir:
ATTRS{left_handle/left_handle_reset}=="(not readable)"
ATTRS{right_handle/right_handle_reset}=="(not readable)"
ATTRS{tx_dongle/tx_dongle_reset}=="(not readable)"

or old WO attrs with no subdir:
ATTRS{left_handle_reset}=="(not readable)"
ATTRS{right_handle_reset}=="(not readable)"
ATTRS{tx_dongle_reset}=="(not readable)"

While the third option is usable, it doesn't logically break up the
physical devices and creates a device directory with over 80 attributes
once all attrs are defined.

Reviewed-by: default avatarMark Pearson <mpearson-lenovo@squebb.ca>
Signed-off-by: default avatarDerek J. Clark <derekjohn.clark@gmail.com>
Acked-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: default avatarJiri Kosina <jkosina@suse.com>
parent 48976c0e
Loading
Loading
Loading
Loading
+46 −0
Original line number Diff line number Diff line
@@ -189,6 +189,22 @@ ssize_t device_show_string(struct device *dev, struct device_attribute *attr,
#define DEVICE_ATTR_ADMIN_RW(_name) \
	struct device_attribute dev_attr_##_name = __ATTR_RW_MODE(_name, 0600)

/**
 * DEVICE_ATTR_RW_NAMED - Define a read-write device attribute with a sysfs name
 * that differs from the function name.
 * @_name: Attribute function preface
 * @_attrname: Attribute name as it wil be exposed in the sysfs.
 *
 * Like DEVICE_ATTR_RW(), but allows for reusing names under separate paths in
 * the same driver.
 */
#define DEVICE_ATTR_RW_NAMED(_name, _attrname)                            \
	struct device_attribute dev_attr_##_name = {                      \
		.attr = { .name = _attrname, .mode = 0644 }, \
		.show = _name##_show,                                     \
		.store = _name##_store,                                   \
	}

/**
 * DEVICE_ATTR_RO - Define a readable device attribute.
 * @_name: Attribute name.
@@ -207,6 +223,21 @@ ssize_t device_show_string(struct device *dev, struct device_attribute *attr,
#define DEVICE_ATTR_ADMIN_RO(_name) \
	struct device_attribute dev_attr_##_name = __ATTR_RO_MODE(_name, 0400)

/**
 * DEVICE_ATTR_RO_NAMED - Define a read-only device attribute with a sysfs name
 * that differs from the function name.
 * @_name: Attribute function preface
 * @_attrname: Attribute name as it wil be exposed in the sysfs.
 *
 * Like DEVICE_ATTR_RO(), but allows for reusing names under separate paths in
 * the same driver.
 */
#define DEVICE_ATTR_RO_NAMED(_name, _attrname)                            \
	struct device_attribute dev_attr_##_name = {                      \
		.attr = { .name = _attrname, .mode = 0444 }, \
		.show = _name##_show,                                     \
	}

/**
 * DEVICE_ATTR_WO - Define an admin-only writable device attribute.
 * @_name: Attribute name.
@@ -216,6 +247,21 @@ ssize_t device_show_string(struct device *dev, struct device_attribute *attr,
#define DEVICE_ATTR_WO(_name) \
	struct device_attribute dev_attr_##_name = __ATTR_WO(_name)

/**
 * DEVICE_ATTR_WO_NAMED - Define a read-only device attribute with a sysfs name
 * that differs from the function name.
 * @_name: Attribute function preface
 * @_attrname: Attribute name as it wil be exposed in the sysfs.
 *
 * Like DEVICE_ATTR_WO(), but allows for reusing names under separate paths in
 * the same driver.
 */
#define DEVICE_ATTR_WO_NAMED(_name, _attrname)                            \
	struct device_attribute dev_attr_##_name = {                      \
		.attr = { .name = _attrname, .mode = 0200 }, \
		.store = _name##_store,                                   \
	}

/**
 * DEVICE_ULONG_ATTR - Define a device attribute backed by an unsigned long.
 * @_name: Attribute name.