Skip to content

Commit 869dfc8

Browse files
Andi Kleengregkh
authored andcommitted
driver core: Add class_attr_string for simple read-only string
Several drivers just export a static string as class attributes. Use the new extensible attribute support to define a simple CLASS_ATTR_STRING() macro for this. This will allow to remove code from drivers in followon patches. Signed-off-by: Andi Kleen <ak@linux.intel.com> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
1 parent 28812fe commit 869dfc8

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

‎drivers/base/class.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,16 @@ void class_interface_unregister(struct class_interface *class_intf)
490490
class_put(parent);
491491
}
492492

493+
ssize_t show_class_attr_string(struct class *class, struct class_attribute *attr,
494+
char *buf)
495+
{
496+
struct class_attribute_string *cs;
497+
cs = container_of(attr, struct class_attribute_string, attr);
498+
return snprintf(buf, PAGE_SIZE, "%s\n", cs->str);
499+
}
500+
501+
EXPORT_SYMBOL_GPL(show_class_attr_string);
502+
493503
struct class_compat {
494504
struct kobject *kobj;
495505
};

‎include/linux/device.h‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,23 @@ extern int __must_check class_create_file(struct class *class,
265265
extern void class_remove_file(struct class *class,
266266
const struct class_attribute *attr);
267267

268+
/* Simple class attribute that is just a static string */
269+
270+
struct class_attribute_string {
271+
struct class_attribute attr;
272+
char *str;
273+
};
274+
275+
/* Currently read-only only */
276+
#define _CLASS_ATTR_STRING(_name, _mode, _str) \
277+
{ __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
278+
#define CLASS_ATTR_STRING(_name, _mode, _str) \
279+
struct class_attribute_string class_attr_##_name = \
280+
_CLASS_ATTR_STRING(_name, _mode, _str)
281+
282+
extern ssize_t show_class_attr_string(struct class *class, struct class_attribute *attr,
283+
char *buf);
284+
268285
struct class_interface {
269286
struct list_head node;
270287
struct class *class;

0 commit comments

Comments
 (0)