Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 3cc50d0

Browse files
l1kgregkh
authored andcommitted
driver core: Add device_show_string() helper for sysfs attributes
For drivers wishing to expose an unsigned long, int or bool at a static memory location in sysfs, the driver core provides ready-made helpers such as device_show_ulong() to be used as ->show() callback. Some drivers need to expose a string and so far they all provide their own ->show() implementation. arch/powerpc/perf/hv-24x7.c went so far as to create a device_show_string() helper but kept it private. Make it public for reuse by other drivers. The pattern seems to be sufficiently frequent to merit a public helper. Add a DEVICE_STRING_ATTR_RO() macro in line with the existing DEVICE_ULONG_ATTR() and similar macros to ease declaration of string attributes. Signed-off-by: Lukas Wunner <lukas@wunner.de> Acked-by: Michael Ellerman <mpe@ellerman.id.au> Link: https://lore.kernel.org/r/2e3eaaf2600bb55c0415c23ba301e809403a7aa2.1713608122.git.lukas@wunner.de Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent e5019b1 commit 3cc50d0

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

arch/powerpc/perf/hv-24x7.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -425,16 +425,6 @@ static char *memdup_to_str(char *maybe_str, int max_len, gfp_t gfp)
425425
return kasprintf(gfp, "%.*s", max_len, maybe_str);
426426
}
427427

428-
static ssize_t device_show_string(struct device *dev,
429-
struct device_attribute *attr, char *buf)
430-
{
431-
struct dev_ext_attribute *d;
432-
433-
d = container_of(attr, struct dev_ext_attribute, attr);
434-
435-
return sprintf(buf, "%s\n", (char *)d->var);
436-
}
437-
438428
static ssize_t cpumask_show(struct device *dev,
439429
struct device_attribute *attr, char *buf)
440430
{

drivers/base/core.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2538,6 +2538,15 @@ ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
25382538
}
25392539
EXPORT_SYMBOL_GPL(device_show_bool);
25402540

2541+
ssize_t device_show_string(struct device *dev,
2542+
struct device_attribute *attr, char *buf)
2543+
{
2544+
struct dev_ext_attribute *ea = to_ext_attr(attr);
2545+
2546+
return sysfs_emit(buf, "%s\n", (char *)ea->var);
2547+
}
2548+
EXPORT_SYMBOL_GPL(device_show_string);
2549+
25412550
/**
25422551
* device_release - free device structure.
25432552
* @kobj: device's kobject.

include/linux/device.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
132132
char *buf);
133133
ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
134134
const char *buf, size_t count);
135+
ssize_t device_show_string(struct device *dev, struct device_attribute *attr,
136+
char *buf);
135137

136138
/**
137139
* DEVICE_ATTR - Define a device attribute.
@@ -251,6 +253,19 @@ ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
251253
struct dev_ext_attribute dev_attr_##_name = \
252254
{ __ATTR(_name, _mode, device_show_bool, device_store_bool), &(_var) }
253255

256+
/**
257+
* DEVICE_STRING_ATTR_RO - Define a device attribute backed by a r/o string.
258+
* @_name: Attribute name.
259+
* @_mode: File mode.
260+
* @_var: Identifier of string.
261+
*
262+
* Like DEVICE_ULONG_ATTR(), but @_var is a string. Because the length of the
263+
* string allocation is unknown, the attribute must be read-only.
264+
*/
265+
#define DEVICE_STRING_ATTR_RO(_name, _mode, _var) \
266+
struct dev_ext_attribute dev_attr_##_name = \
267+
{ __ATTR(_name, (_mode) & ~0222, device_show_string, NULL), (_var) }
268+
254269
#define DEVICE_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \
255270
struct device_attribute dev_attr_##_name = \
256271
__ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)

0 commit comments

Comments
 (0)