31
31
32
32
#define POWER_SUPPLY_ATTR (_name ) \
33
33
{ \
34
- .attr = { .name = #_name, .mode = 0444 }, \
34
+ .attr = { .name = #_name }, \
35
35
.show = power_supply_show_property, \
36
36
.store = NULL, \
37
37
}
@@ -41,6 +41,9 @@ static struct device_attribute power_supply_attrs[];
41
41
static ssize_t power_supply_show_property (struct device * dev ,
42
42
struct device_attribute * attr ,
43
43
char * buf ) {
44
+ static char * type_text [] = {
45
+ "Battery" , "UPS" , "Mains" , "USB"
46
+ };
44
47
static char * status_text [] = {
45
48
"Unknown" , "Charging" , "Discharging" , "Not charging" , "Full"
46
49
};
@@ -58,12 +61,15 @@ static ssize_t power_supply_show_property(struct device *dev,
58
61
static char * capacity_level_text [] = {
59
62
"Unknown" , "Critical" , "Low" , "Normal" , "High" , "Full"
60
63
};
61
- ssize_t ret ;
64
+ ssize_t ret = 0 ;
62
65
struct power_supply * psy = dev_get_drvdata (dev );
63
66
const ptrdiff_t off = attr - power_supply_attrs ;
64
67
union power_supply_propval value ;
65
68
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 );
67
73
68
74
if (ret < 0 ) {
69
75
if (ret == - ENODATA )
@@ -85,6 +91,8 @@ static ssize_t power_supply_show_property(struct device *dev,
85
91
return sprintf (buf , "%s\n" , technology_text [value .intval ]);
86
92
else if (off == POWER_SUPPLY_PROP_CAPACITY_LEVEL )
87
93
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 ]);
88
96
else if (off >= POWER_SUPPLY_PROP_MODEL_NAME )
89
97
return sprintf (buf , "%s\n" , value .strval );
90
98
@@ -132,67 +140,50 @@ static struct device_attribute power_supply_attrs[] = {
132
140
POWER_SUPPLY_ATTR (time_to_empty_avg ),
133
141
POWER_SUPPLY_ATTR (time_to_full_now ),
134
142
POWER_SUPPLY_ATTR (time_to_full_avg ),
143
+ POWER_SUPPLY_ATTR (type ),
135
144
/* Properties of type `const char *' */
136
145
POWER_SUPPLY_ATTR (model_name ),
137
146
POWER_SUPPLY_ATTR (manufacturer ),
138
147
POWER_SUPPLY_ATTR (serial_number ),
139
148
};
140
149
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 ];
153
152
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 )
155
156
{
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 ;
165
160
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 ;
171
164
}
172
165
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 ;
184
167
}
185
168
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 )
187
180
{
188
181
int i ;
189
182
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 ;
192
184
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 ;
196
187
}
197
188
198
189
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)
236
227
if (!prop_buf )
237
228
return - ENOMEM ;
238
229
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
-
269
230
for (j = 0 ; j < psy -> num_properties ; j ++ ) {
270
231
struct device_attribute * attr ;
271
232
char * line ;
0 commit comments