2 * Sysfs interface for the universal power supply monitor class
4 * Copyright © 2007 David Woodhouse <dwmw2@infradead.org>
5 * Copyright © 2007 Anton Vorontsov <cbou@mail.ru>
6 * Copyright © 2004 Szabolcs Gyurko
7 * Copyright © 2003 Ian Molton <spyro@f2s.com>
9 * Modified: 2004, Oct Szabolcs Gyurko
11 * You may use this code as per GPL version 2
14 #include <linux/ctype.h>
15 #include <linux/power_supply.h>
17 #include "power_supply.h"
20 * This is because the name "current" breaks the device attr macro.
21 * The "current" word resolves to "(get_current())" so instead of
22 * "current" "(get_current())" appears in the sysfs.
24 * The source of this definition is the device.h which calls __ATTR
25 * macro in sysfs.h which calls the __stringify macro.
27 * Only modification that the name is not tried to be resolved
28 * (as a macro let's say).
31 #define POWER_SUPPLY_ATTR(_name) \
33 .attr = { .name = #_name, .mode = 0444 }, \
34 .show = power_supply_show_property, \
38 static struct device_attribute power_supply_attrs
[];
40 static ssize_t
power_supply_show_property(struct device
*dev
,
41 struct device_attribute
*attr
,
43 static char *status_text
[] = {
44 "Unknown", "Charging", "Discharging", "Not charging", "Full"
46 static char *charge_type
[] = {
47 "Unknown", "N/A", "Trickle", "Fast"
49 static char *health_text
[] = {
50 "Unknown", "Good", "Overheat", "Dead", "Over voltage",
51 "Unspecified failure", "Cold",
53 static char *technology_text
[] = {
54 "Unknown", "NiMH", "Li-ion", "Li-poly", "LiFe", "NiCd",
57 static char *capacity_level_text
[] = {
58 "Unknown", "Critical", "Low", "Normal", "High", "Full"
61 struct power_supply
*psy
= dev_get_drvdata(dev
);
62 const ptrdiff_t off
= attr
- power_supply_attrs
;
63 union power_supply_propval value
;
65 ret
= psy
->get_property(psy
, off
, &value
);
69 dev_err(dev
, "driver failed to report `%s' property\n",
74 if (off
== POWER_SUPPLY_PROP_STATUS
)
75 return sprintf(buf
, "%s\n", status_text
[value
.intval
]);
76 else if (off
== POWER_SUPPLY_PROP_CHARGE_TYPE
)
77 return sprintf(buf
, "%s\n", charge_type
[value
.intval
]);
78 else if (off
== POWER_SUPPLY_PROP_HEALTH
)
79 return sprintf(buf
, "%s\n", health_text
[value
.intval
]);
80 else if (off
== POWER_SUPPLY_PROP_TECHNOLOGY
)
81 return sprintf(buf
, "%s\n", technology_text
[value
.intval
]);
82 else if (off
== POWER_SUPPLY_PROP_CAPACITY_LEVEL
)
83 return sprintf(buf
, "%s\n", capacity_level_text
[value
.intval
]);
84 else if (off
>= POWER_SUPPLY_PROP_MODEL_NAME
)
85 return sprintf(buf
, "%s\n", value
.strval
);
87 return sprintf(buf
, "%d\n", value
.intval
);
90 /* Must be in the same order as POWER_SUPPLY_PROP_* */
91 static struct device_attribute power_supply_attrs
[] = {
92 /* Properties of type `int' */
93 POWER_SUPPLY_ATTR(status
),
94 POWER_SUPPLY_ATTR(charge_type
),
95 POWER_SUPPLY_ATTR(health
),
96 POWER_SUPPLY_ATTR(present
),
97 POWER_SUPPLY_ATTR(online
),
98 POWER_SUPPLY_ATTR(technology
),
99 POWER_SUPPLY_ATTR(voltage_max
),
100 POWER_SUPPLY_ATTR(voltage_min
),
101 POWER_SUPPLY_ATTR(voltage_max_design
),
102 POWER_SUPPLY_ATTR(voltage_min_design
),
103 POWER_SUPPLY_ATTR(voltage_now
),
104 POWER_SUPPLY_ATTR(voltage_avg
),
105 POWER_SUPPLY_ATTR(current_now
),
106 POWER_SUPPLY_ATTR(current_avg
),
107 POWER_SUPPLY_ATTR(power_now
),
108 POWER_SUPPLY_ATTR(power_avg
),
109 POWER_SUPPLY_ATTR(charge_full_design
),
110 POWER_SUPPLY_ATTR(charge_empty_design
),
111 POWER_SUPPLY_ATTR(charge_full
),
112 POWER_SUPPLY_ATTR(charge_empty
),
113 POWER_SUPPLY_ATTR(charge_now
),
114 POWER_SUPPLY_ATTR(charge_avg
),
115 POWER_SUPPLY_ATTR(charge_counter
),
116 POWER_SUPPLY_ATTR(energy_full_design
),
117 POWER_SUPPLY_ATTR(energy_empty_design
),
118 POWER_SUPPLY_ATTR(energy_full
),
119 POWER_SUPPLY_ATTR(energy_empty
),
120 POWER_SUPPLY_ATTR(energy_now
),
121 POWER_SUPPLY_ATTR(energy_avg
),
122 POWER_SUPPLY_ATTR(capacity
),
123 POWER_SUPPLY_ATTR(capacity_level
),
124 POWER_SUPPLY_ATTR(temp
),
125 POWER_SUPPLY_ATTR(temp_ambient
),
126 POWER_SUPPLY_ATTR(time_to_empty_now
),
127 POWER_SUPPLY_ATTR(time_to_empty_avg
),
128 POWER_SUPPLY_ATTR(time_to_full_now
),
129 POWER_SUPPLY_ATTR(time_to_full_avg
),
130 /* Properties of type `const char *' */
131 POWER_SUPPLY_ATTR(model_name
),
132 POWER_SUPPLY_ATTR(manufacturer
),
133 POWER_SUPPLY_ATTR(serial_number
),
136 static ssize_t
power_supply_show_static_attrs(struct device
*dev
,
137 struct device_attribute
*attr
,
139 static char *type_text
[] = { "Battery", "UPS", "Mains", "USB" };
140 struct power_supply
*psy
= dev_get_drvdata(dev
);
142 return sprintf(buf
, "%s\n", type_text
[psy
->type
]);
145 static struct device_attribute power_supply_static_attrs
[] = {
146 __ATTR(type
, 0444, power_supply_show_static_attrs
, NULL
),
149 int power_supply_create_attrs(struct power_supply
*psy
)
154 for (i
= 0; i
< ARRAY_SIZE(power_supply_static_attrs
); i
++) {
155 rc
= device_create_file(psy
->dev
,
156 &power_supply_static_attrs
[i
]);
161 for (j
= 0; j
< psy
->num_properties
; j
++) {
162 rc
= device_create_file(psy
->dev
,
163 &power_supply_attrs
[psy
->properties
[j
]]);
165 goto dynamics_failed
;
172 device_remove_file(psy
->dev
,
173 &power_supply_attrs
[psy
->properties
[j
]]);
176 device_remove_file(psy
->dev
, &power_supply_static_attrs
[i
]);
181 void power_supply_remove_attrs(struct power_supply
*psy
)
185 for (i
= 0; i
< ARRAY_SIZE(power_supply_static_attrs
); i
++)
186 device_remove_file(psy
->dev
, &power_supply_static_attrs
[i
]);
188 for (i
= 0; i
< psy
->num_properties
; i
++)
189 device_remove_file(psy
->dev
,
190 &power_supply_attrs
[psy
->properties
[i
]]);
193 static char *kstruprdup(const char *str
, gfp_t gfp
)
197 ustr
= ret
= kmalloc(strlen(str
) + 1, gfp
);
203 *ustr
++ = toupper(*str
++);
210 int power_supply_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
212 struct power_supply
*psy
= dev_get_drvdata(dev
);
217 dev_dbg(dev
, "uevent\n");
219 if (!psy
|| !psy
->dev
) {
220 dev_dbg(dev
, "No power supply yet\n");
224 dev_dbg(dev
, "POWER_SUPPLY_NAME=%s\n", psy
->name
);
226 ret
= add_uevent_var(env
, "POWER_SUPPLY_NAME=%s", psy
->name
);
230 prop_buf
= (char *)get_zeroed_page(GFP_KERNEL
);
234 for (j
= 0; j
< ARRAY_SIZE(power_supply_static_attrs
); j
++) {
235 struct device_attribute
*attr
;
238 attr
= &power_supply_static_attrs
[j
];
240 ret
= power_supply_show_static_attrs(dev
, attr
, prop_buf
);
244 line
= strchr(prop_buf
, '\n');
248 attrname
= kstruprdup(attr
->attr
.name
, GFP_KERNEL
);
254 dev_dbg(dev
, "Static prop %s=%s\n", attrname
, prop_buf
);
256 ret
= add_uevent_var(env
, "POWER_SUPPLY_%s=%s", attrname
, prop_buf
);
262 dev_dbg(dev
, "%zd dynamic props\n", psy
->num_properties
);
264 for (j
= 0; j
< psy
->num_properties
; j
++) {
265 struct device_attribute
*attr
;
268 attr
= &power_supply_attrs
[psy
->properties
[j
]];
270 ret
= power_supply_show_property(dev
, attr
, prop_buf
);
271 if (ret
== -ENODEV
) {
272 /* When a battery is absent, we expect -ENODEV. Don't abort;
273 send the uevent with at least the the PRESENT=0 property */
281 line
= strchr(prop_buf
, '\n');
285 attrname
= kstruprdup(attr
->attr
.name
, GFP_KERNEL
);
291 dev_dbg(dev
, "prop %s=%s\n", attrname
, prop_buf
);
293 ret
= add_uevent_var(env
, "POWER_SUPPLY_%s=%s", attrname
, prop_buf
);
300 free_page((unsigned long)prop_buf
);