Skip to content

Commit 5f487cd

Browse files
committed
power_supply: Use attribute groups
This fixes a race between power supply device and initial attributes creation, plus makes it possible to implement writable properties. [Daniel Mack - removed superflous return statement and dropped .mode attribute from POWER_SUPPLY_ATTR] Suggested-by: Greg KH <[email protected]> Suggested-by: Kay Sievers <[email protected]> Signed-off-by: Anton Vorontsov <[email protected]> Tested-by: Daniel Mack <[email protected]>
1 parent a1e50fd commit 5f487cd

File tree

4 files changed

+75
-96
lines changed

4 files changed

+75
-96
lines changed

drivers/power/power_supply.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,12 @@
1212

1313
#ifdef CONFIG_SYSFS
1414

15-
extern int power_supply_create_attrs(struct power_supply *psy);
16-
extern void power_supply_remove_attrs(struct power_supply *psy);
15+
extern void power_supply_init_attrs(struct device_type *dev_type);
1716
extern int power_supply_uevent(struct device *dev, struct kobj_uevent_env *env);
1817

1918
#else
2019

21-
static inline int power_supply_create_attrs(struct power_supply *psy)
22-
{ return 0; }
23-
static inline void power_supply_remove_attrs(struct power_supply *psy) {}
20+
static inline void power_supply_init_attrs(struct device_type *dev_type) {}
2421
#define power_supply_uevent NULL
2522

2623
#endif /* CONFIG_SYSFS */

drivers/power/power_supply_core.c

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/module.h>
1414
#include <linux/types.h>
1515
#include <linux/init.h>
16+
#include <linux/slab.h>
1617
#include <linux/device.h>
1718
#include <linux/err.h>
1819
#include <linux/power_supply.h>
@@ -22,6 +23,8 @@
2223
struct class *power_supply_class;
2324
EXPORT_SYMBOL_GPL(power_supply_class);
2425

26+
static struct device_type power_supply_dev_type;
27+
2528
static int __power_supply_changed_work(struct device *dev, void *data)
2629
{
2730
struct power_supply *psy = (struct power_supply *)data;
@@ -144,22 +147,39 @@ struct power_supply *power_supply_get_by_name(char *name)
144147
}
145148
EXPORT_SYMBOL_GPL(power_supply_get_by_name);
146149

150+
static void power_supply_dev_release(struct device *dev)
151+
{
152+
pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
153+
kfree(dev);
154+
}
155+
147156
int power_supply_register(struct device *parent, struct power_supply *psy)
148157
{
149-
int rc = 0;
158+
struct device *dev;
159+
int rc;
150160

151-
psy->dev = device_create(power_supply_class, parent, 0, psy,
152-
"%s", psy->name);
153-
if (IS_ERR(psy->dev)) {
154-
rc = PTR_ERR(psy->dev);
155-
goto dev_create_failed;
156-
}
161+
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
162+
if (!dev)
163+
return -ENOMEM;
157164

158-
INIT_WORK(&psy->changed_work, power_supply_changed_work);
165+
device_initialize(dev);
159166

160-
rc = power_supply_create_attrs(psy);
167+
dev->class = power_supply_class;
168+
dev->type = &power_supply_dev_type;
169+
dev->parent = parent;
170+
dev->release = power_supply_dev_release;
171+
dev_set_drvdata(dev, psy);
172+
psy->dev = dev;
173+
174+
rc = kobject_set_name(&dev->kobj, "%s", psy->name);
175+
if (rc)
176+
goto kobject_set_name_failed;
177+
178+
rc = device_add(dev);
161179
if (rc)
162-
goto create_attrs_failed;
180+
goto device_add_failed;
181+
182+
INIT_WORK(&psy->changed_work, power_supply_changed_work);
163183

164184
rc = power_supply_create_triggers(psy);
165185
if (rc)
@@ -170,10 +190,10 @@ int power_supply_register(struct device *parent, struct power_supply *psy)
170190
goto success;
171191

172192
create_triggers_failed:
173-
power_supply_remove_attrs(psy);
174-
create_attrs_failed:
175193
device_unregister(psy->dev);
176-
dev_create_failed:
194+
kobject_set_name_failed:
195+
device_add_failed:
196+
kfree(dev);
177197
success:
178198
return rc;
179199
}
@@ -183,7 +203,6 @@ void power_supply_unregister(struct power_supply *psy)
183203
{
184204
flush_scheduled_work();
185205
power_supply_remove_triggers(psy);
186-
power_supply_remove_attrs(psy);
187206
device_unregister(psy->dev);
188207
}
189208
EXPORT_SYMBOL_GPL(power_supply_unregister);
@@ -196,6 +215,7 @@ static int __init power_supply_class_init(void)
196215
return PTR_ERR(power_supply_class);
197216

198217
power_supply_class->dev_uevent = power_supply_uevent;
218+
power_supply_init_attrs(&power_supply_dev_type);
199219

200220
return 0;
201221
}

drivers/power/power_supply_sysfs.c

Lines changed: 38 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
#define POWER_SUPPLY_ATTR(_name) \
3333
{ \
34-
.attr = { .name = #_name, .mode = 0444 }, \
34+
.attr = { .name = #_name }, \
3535
.show = power_supply_show_property, \
3636
.store = NULL, \
3737
}
@@ -41,6 +41,9 @@ static struct device_attribute power_supply_attrs[];
4141
static ssize_t power_supply_show_property(struct device *dev,
4242
struct device_attribute *attr,
4343
char *buf) {
44+
static char *type_text[] = {
45+
"Battery", "UPS", "Mains", "USB"
46+
};
4447
static char *status_text[] = {
4548
"Unknown", "Charging", "Discharging", "Not charging", "Full"
4649
};
@@ -58,12 +61,15 @@ static ssize_t power_supply_show_property(struct device *dev,
5861
static char *capacity_level_text[] = {
5962
"Unknown", "Critical", "Low", "Normal", "High", "Full"
6063
};
61-
ssize_t ret;
64+
ssize_t ret = 0;
6265
struct power_supply *psy = dev_get_drvdata(dev);
6366
const ptrdiff_t off = attr - power_supply_attrs;
6467
union power_supply_propval value;
6568

66-
ret = psy->get_property(psy, off, &value);
69+
if (off == POWER_SUPPLY_PROP_TYPE)
70+
value.intval = psy->type;
71+
else
72+
ret = psy->get_property(psy, off, &value);
6773

6874
if (ret < 0) {
6975
if (ret == -ENODATA)
@@ -85,6 +91,8 @@ static ssize_t power_supply_show_property(struct device *dev,
8591
return sprintf(buf, "%s\n", technology_text[value.intval]);
8692
else if (off == POWER_SUPPLY_PROP_CAPACITY_LEVEL)
8793
return sprintf(buf, "%s\n", capacity_level_text[value.intval]);
94+
else if (off == POWER_SUPPLY_PROP_TYPE)
95+
return sprintf(buf, "%s\n", type_text[value.intval]);
8896
else if (off >= POWER_SUPPLY_PROP_MODEL_NAME)
8997
return sprintf(buf, "%s\n", value.strval);
9098

@@ -132,67 +140,50 @@ static struct device_attribute power_supply_attrs[] = {
132140
POWER_SUPPLY_ATTR(time_to_empty_avg),
133141
POWER_SUPPLY_ATTR(time_to_full_now),
134142
POWER_SUPPLY_ATTR(time_to_full_avg),
143+
POWER_SUPPLY_ATTR(type),
135144
/* Properties of type `const char *' */
136145
POWER_SUPPLY_ATTR(model_name),
137146
POWER_SUPPLY_ATTR(manufacturer),
138147
POWER_SUPPLY_ATTR(serial_number),
139148
};
140149

141-
static ssize_t power_supply_show_static_attrs(struct device *dev,
142-
struct device_attribute *attr,
143-
char *buf) {
144-
static char *type_text[] = { "Battery", "UPS", "Mains", "USB" };
145-
struct power_supply *psy = dev_get_drvdata(dev);
146-
147-
return sprintf(buf, "%s\n", type_text[psy->type]);
148-
}
149-
150-
static struct device_attribute power_supply_static_attrs[] = {
151-
__ATTR(type, 0444, power_supply_show_static_attrs, NULL),
152-
};
150+
static struct attribute *
151+
__power_supply_attrs[ARRAY_SIZE(power_supply_attrs) + 1];
153152

154-
int power_supply_create_attrs(struct power_supply *psy)
153+
static mode_t power_supply_attr_is_visible(struct kobject *kobj,
154+
struct attribute *attr,
155+
int attrno)
155156
{
156-
int rc = 0;
157-
int i, j;
158-
159-
for (i = 0; i < ARRAY_SIZE(power_supply_static_attrs); i++) {
160-
rc = device_create_file(psy->dev,
161-
&power_supply_static_attrs[i]);
162-
if (rc)
163-
goto statics_failed;
164-
}
157+
struct device *dev = container_of(kobj, struct device, kobj);
158+
struct power_supply *psy = dev_get_drvdata(dev);
159+
int i;
165160

166-
for (j = 0; j < psy->num_properties; j++) {
167-
rc = device_create_file(psy->dev,
168-
&power_supply_attrs[psy->properties[j]]);
169-
if (rc)
170-
goto dynamics_failed;
161+
for (i = 0; i < psy->num_properties; i++) {
162+
if (psy->properties[i] == attrno)
163+
return 0444;
171164
}
172165

173-
goto succeed;
174-
175-
dynamics_failed:
176-
while (j--)
177-
device_remove_file(psy->dev,
178-
&power_supply_attrs[psy->properties[j]]);
179-
statics_failed:
180-
while (i--)
181-
device_remove_file(psy->dev, &power_supply_static_attrs[i]);
182-
succeed:
183-
return rc;
166+
return 0;
184167
}
185168

186-
void power_supply_remove_attrs(struct power_supply *psy)
169+
static struct attribute_group power_supply_attr_group = {
170+
.attrs = __power_supply_attrs,
171+
.is_visible = power_supply_attr_is_visible,
172+
};
173+
174+
static const struct attribute_group *power_supply_attr_groups[] = {
175+
&power_supply_attr_group,
176+
NULL,
177+
};
178+
179+
void power_supply_init_attrs(struct device_type *dev_type)
187180
{
188181
int i;
189182

190-
for (i = 0; i < ARRAY_SIZE(power_supply_static_attrs); i++)
191-
device_remove_file(psy->dev, &power_supply_static_attrs[i]);
183+
dev_type->groups = power_supply_attr_groups;
192184

193-
for (i = 0; i < psy->num_properties; i++)
194-
device_remove_file(psy->dev,
195-
&power_supply_attrs[psy->properties[i]]);
185+
for (i = 0; i < ARRAY_SIZE(power_supply_attrs); i++)
186+
__power_supply_attrs[i] = &power_supply_attrs[i].attr;
196187
}
197188

198189
static char *kstruprdup(const char *str, gfp_t gfp)
@@ -236,36 +227,6 @@ int power_supply_uevent(struct device *dev, struct kobj_uevent_env *env)
236227
if (!prop_buf)
237228
return -ENOMEM;
238229

239-
for (j = 0; j < ARRAY_SIZE(power_supply_static_attrs); j++) {
240-
struct device_attribute *attr;
241-
char *line;
242-
243-
attr = &power_supply_static_attrs[j];
244-
245-
ret = power_supply_show_static_attrs(dev, attr, prop_buf);
246-
if (ret < 0)
247-
goto out;
248-
249-
line = strchr(prop_buf, '\n');
250-
if (line)
251-
*line = 0;
252-
253-
attrname = kstruprdup(attr->attr.name, GFP_KERNEL);
254-
if (!attrname) {
255-
ret = -ENOMEM;
256-
goto out;
257-
}
258-
259-
dev_dbg(dev, "Static prop %s=%s\n", attrname, prop_buf);
260-
261-
ret = add_uevent_var(env, "POWER_SUPPLY_%s=%s", attrname, prop_buf);
262-
kfree(attrname);
263-
if (ret)
264-
goto out;
265-
}
266-
267-
dev_dbg(dev, "%zd dynamic props\n", psy->num_properties);
268-
269230
for (j = 0; j < psy->num_properties; j++) {
270231
struct device_attribute *attr;
271232
char *line;

include/linux/power_supply.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ enum power_supply_property {
114114
POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
115115
POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
116116
POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
117+
POWER_SUPPLY_PROP_TYPE, /* use power_supply.type instead */
117118
/* Properties of type `const char *' */
118119
POWER_SUPPLY_PROP_MODEL_NAME,
119120
POWER_SUPPLY_PROP_MANUFACTURER,

0 commit comments

Comments
 (0)