Skip to content

Commit 09cda84

Browse files
committed
[FROM-ML] 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: Mark Pearson <mpearson-lenovo@squebb.ca> Signed-off-by: Derek J. Clark <derekjohn.clark@gmail.com>
1 parent 05f7e89 commit 09cda84

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

include/linux/device.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,22 @@ ssize_t device_show_string(struct device *dev, struct device_attribute *attr,
189189
#define DEVICE_ATTR_ADMIN_RW(_name) \
190190
struct device_attribute dev_attr_##_name = __ATTR_RW_MODE(_name, 0600)
191191

192+
/**
193+
* DEVICE_ATTR_RW_NAMED - Define a read-write device attribute with a sysfs name
194+
* that differs from the function name.
195+
* @_name: Attribute function preface
196+
* @_attrname: Attribute name as it wil be exposed in the sysfs.
197+
*
198+
* Like DEVICE_ATTR_RW(), but allows for reusing names under separate paths in
199+
* the same driver.
200+
*/
201+
#define DEVICE_ATTR_RW_NAMED(_name, _attrname) \
202+
struct device_attribute dev_attr_##_name = { \
203+
.attr = { .name = _attrname, .mode = 0644 }, \
204+
.show = _name##_show, \
205+
.store = _name##_store, \
206+
}
207+
192208
/**
193209
* DEVICE_ATTR_RO - Define a readable device attribute.
194210
* @_name: Attribute name.
@@ -207,6 +223,21 @@ ssize_t device_show_string(struct device *dev, struct device_attribute *attr,
207223
#define DEVICE_ATTR_ADMIN_RO(_name) \
208224
struct device_attribute dev_attr_##_name = __ATTR_RO_MODE(_name, 0400)
209225

226+
/**
227+
* DEVICE_ATTR_RO_NAMED - Define a read-only device attribute with a sysfs name
228+
* that differs from the function name.
229+
* @_name: Attribute function preface
230+
* @_attrname: Attribute name as it wil be exposed in the sysfs.
231+
*
232+
* Like DEVICE_ATTR_RO(), but allows for reusing names under separate paths in
233+
* the same driver.
234+
*/
235+
#define DEVICE_ATTR_RO_NAMED(_name, _attrname) \
236+
struct device_attribute dev_attr_##_name = { \
237+
.attr = { .name = _attrname, .mode = 0444 }, \
238+
.show = _name##_show, \
239+
}
240+
210241
/**
211242
* DEVICE_ATTR_WO - Define an admin-only writable device attribute.
212243
* @_name: Attribute name.
@@ -216,6 +247,21 @@ ssize_t device_show_string(struct device *dev, struct device_attribute *attr,
216247
#define DEVICE_ATTR_WO(_name) \
217248
struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
218249

250+
/**
251+
* DEVICE_ATTR_WO_NAMED - Define a read-only device attribute with a sysfs name
252+
* that differs from the function name.
253+
* @_name: Attribute function preface
254+
* @_attrname: Attribute name as it wil be exposed in the sysfs.
255+
*
256+
* Like DEVICE_ATTR_WO(), but allows for reusing names under separate paths in
257+
* the same driver.
258+
*/
259+
#define DEVICE_ATTR_WO_NAMED(_name, _attrname) \
260+
struct device_attribute dev_attr_##_name = { \
261+
.attr = { .name = _attrname, .mode = 0200 }, \
262+
.store = _name##_store, \
263+
}
264+
219265
/**
220266
* DEVICE_ULONG_ATTR - Define a device attribute backed by an unsigned long.
221267
* @_name: Attribute name.

0 commit comments

Comments
 (0)