| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 1 | /* | 
 | 2 |  *  Sysfs interface for the universal power supply monitor class | 
 | 3 |  * | 
 | 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> | 
 | 8 |  * | 
 | 9 |  *  Modified: 2004, Oct     Szabolcs Gyurko | 
 | 10 |  * | 
 | 11 |  *  You may use this code as per GPL version 2 | 
 | 12 |  */ | 
 | 13 |  | 
 | 14 | #include <linux/ctype.h> | 
 | 15 | #include <linux/power_supply.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/slab.h> | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 17 |  | 
| Adrian Bunk | 25f1214 | 2007-11-26 00:25:45 +0300 | [diff] [blame] | 18 | #include "power_supply.h" | 
 | 19 |  | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 20 | /* | 
 | 21 |  * This is because the name "current" breaks the device attr macro. | 
 | 22 |  * The "current" word resolves to "(get_current())" so instead of | 
 | 23 |  * "current" "(get_current())" appears in the sysfs. | 
 | 24 |  * | 
 | 25 |  * The source of this definition is the device.h which calls __ATTR | 
 | 26 |  * macro in sysfs.h which calls the __stringify macro. | 
 | 27 |  * | 
 | 28 |  * Only modification that the name is not tried to be resolved | 
 | 29 |  * (as a macro let's say). | 
 | 30 |  */ | 
 | 31 |  | 
 | 32 | #define POWER_SUPPLY_ATTR(_name)					\ | 
 | 33 | {									\ | 
| Parag Warudkar | 01e8ef1 | 2008-10-18 20:28:50 -0700 | [diff] [blame] | 34 | 	.attr = { .name = #_name, .mode = 0444 },	\ | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 35 | 	.show = power_supply_show_property,				\ | 
 | 36 | 	.store = NULL,							\ | 
 | 37 | } | 
 | 38 |  | 
 | 39 | static struct device_attribute power_supply_attrs[]; | 
 | 40 |  | 
 | 41 | static ssize_t power_supply_show_property(struct device *dev, | 
 | 42 | 					  struct device_attribute *attr, | 
 | 43 | 					  char *buf) { | 
 | 44 | 	static char *status_text[] = { | 
 | 45 | 		"Unknown", "Charging", "Discharging", "Not charging", "Full" | 
 | 46 | 	}; | 
| Andres Salomon | ee8076e | 2009-07-02 09:45:18 -0400 | [diff] [blame] | 47 | 	static char *charge_type[] = { | 
 | 48 | 		"Unknown", "N/A", "Trickle", "Fast" | 
 | 49 | 	}; | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 50 | 	static char *health_text[] = { | 
 | 51 | 		"Unknown", "Good", "Overheat", "Dead", "Over voltage", | 
| Mark Brown | 7e386e6 | 2008-11-30 22:43:21 +0100 | [diff] [blame] | 52 | 		"Unspecified failure", "Cold", | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 53 | 	}; | 
 | 54 | 	static char *technology_text[] = { | 
| Dmitry Baryshkov | c7cc930 | 2008-01-07 04:12:41 +0300 | [diff] [blame] | 55 | 		"Unknown", "NiMH", "Li-ion", "Li-poly", "LiFe", "NiCd", | 
 | 56 | 		"LiMn" | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 57 | 	}; | 
| Andres Salomon | b294a29 | 2009-06-30 02:13:01 -0400 | [diff] [blame] | 58 | 	static char *capacity_level_text[] = { | 
 | 59 | 		"Unknown", "Critical", "Low", "Normal", "High", "Full" | 
 | 60 | 	}; | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 61 | 	ssize_t ret; | 
 | 62 | 	struct power_supply *psy = dev_get_drvdata(dev); | 
 | 63 | 	const ptrdiff_t off = attr - power_supply_attrs; | 
 | 64 | 	union power_supply_propval value; | 
 | 65 |  | 
 | 66 | 	ret = psy->get_property(psy, off, &value); | 
 | 67 |  | 
 | 68 | 	if (ret < 0) { | 
| Anton Vorontsov | 9d233e8 | 2009-12-03 00:24:51 +0300 | [diff] [blame] | 69 | 		if (ret == -ENODATA) | 
 | 70 | 			dev_dbg(dev, "driver has no data for `%s' property\n", | 
 | 71 | 				attr->attr.name); | 
 | 72 | 		else if (ret != -ENODEV) | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 73 | 			dev_err(dev, "driver failed to report `%s' property\n", | 
 | 74 | 				attr->attr.name); | 
 | 75 | 		return ret; | 
 | 76 | 	} | 
 | 77 |  | 
 | 78 | 	if (off == POWER_SUPPLY_PROP_STATUS) | 
 | 79 | 		return sprintf(buf, "%s\n", status_text[value.intval]); | 
| Andres Salomon | ee8076e | 2009-07-02 09:45:18 -0400 | [diff] [blame] | 80 | 	else if (off == POWER_SUPPLY_PROP_CHARGE_TYPE) | 
 | 81 | 		return sprintf(buf, "%s\n", charge_type[value.intval]); | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 82 | 	else if (off == POWER_SUPPLY_PROP_HEALTH) | 
 | 83 | 		return sprintf(buf, "%s\n", health_text[value.intval]); | 
 | 84 | 	else if (off == POWER_SUPPLY_PROP_TECHNOLOGY) | 
 | 85 | 		return sprintf(buf, "%s\n", technology_text[value.intval]); | 
| Andres Salomon | b294a29 | 2009-06-30 02:13:01 -0400 | [diff] [blame] | 86 | 	else if (off == POWER_SUPPLY_PROP_CAPACITY_LEVEL) | 
 | 87 | 		return sprintf(buf, "%s\n", capacity_level_text[value.intval]); | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 88 | 	else if (off >= POWER_SUPPLY_PROP_MODEL_NAME) | 
 | 89 | 		return sprintf(buf, "%s\n", value.strval); | 
 | 90 |  | 
 | 91 | 	return sprintf(buf, "%d\n", value.intval); | 
 | 92 | } | 
 | 93 |  | 
 | 94 | /* Must be in the same order as POWER_SUPPLY_PROP_* */ | 
 | 95 | static struct device_attribute power_supply_attrs[] = { | 
 | 96 | 	/* Properties of type `int' */ | 
 | 97 | 	POWER_SUPPLY_ATTR(status), | 
| Andres Salomon | ee8076e | 2009-07-02 09:45:18 -0400 | [diff] [blame] | 98 | 	POWER_SUPPLY_ATTR(charge_type), | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 99 | 	POWER_SUPPLY_ATTR(health), | 
 | 100 | 	POWER_SUPPLY_ATTR(present), | 
 | 101 | 	POWER_SUPPLY_ATTR(online), | 
 | 102 | 	POWER_SUPPLY_ATTR(technology), | 
| Alexey Starikovskiy | c955fe8 | 2009-10-15 14:31:30 +0400 | [diff] [blame] | 103 | 	POWER_SUPPLY_ATTR(cycle_count), | 
| Dmitry Baryshkov | c7cc930 | 2008-01-07 04:12:41 +0300 | [diff] [blame] | 104 | 	POWER_SUPPLY_ATTR(voltage_max), | 
 | 105 | 	POWER_SUPPLY_ATTR(voltage_min), | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 106 | 	POWER_SUPPLY_ATTR(voltage_max_design), | 
 | 107 | 	POWER_SUPPLY_ATTR(voltage_min_design), | 
 | 108 | 	POWER_SUPPLY_ATTR(voltage_now), | 
 | 109 | 	POWER_SUPPLY_ATTR(voltage_avg), | 
 | 110 | 	POWER_SUPPLY_ATTR(current_now), | 
 | 111 | 	POWER_SUPPLY_ATTR(current_avg), | 
| Alexey Starikovskiy | 7faa144 | 2009-03-27 22:23:52 -0400 | [diff] [blame] | 112 | 	POWER_SUPPLY_ATTR(power_now), | 
 | 113 | 	POWER_SUPPLY_ATTR(power_avg), | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 114 | 	POWER_SUPPLY_ATTR(charge_full_design), | 
 | 115 | 	POWER_SUPPLY_ATTR(charge_empty_design), | 
 | 116 | 	POWER_SUPPLY_ATTR(charge_full), | 
 | 117 | 	POWER_SUPPLY_ATTR(charge_empty), | 
 | 118 | 	POWER_SUPPLY_ATTR(charge_now), | 
 | 119 | 	POWER_SUPPLY_ATTR(charge_avg), | 
| Andres Salomon | 8e552c3 | 2008-05-12 21:46:29 -0400 | [diff] [blame] | 120 | 	POWER_SUPPLY_ATTR(charge_counter), | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 121 | 	POWER_SUPPLY_ATTR(energy_full_design), | 
 | 122 | 	POWER_SUPPLY_ATTR(energy_empty_design), | 
 | 123 | 	POWER_SUPPLY_ATTR(energy_full), | 
 | 124 | 	POWER_SUPPLY_ATTR(energy_empty), | 
 | 125 | 	POWER_SUPPLY_ATTR(energy_now), | 
 | 126 | 	POWER_SUPPLY_ATTR(energy_avg), | 
 | 127 | 	POWER_SUPPLY_ATTR(capacity), | 
| Andres Salomon | b294a29 | 2009-06-30 02:13:01 -0400 | [diff] [blame] | 128 | 	POWER_SUPPLY_ATTR(capacity_level), | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 129 | 	POWER_SUPPLY_ATTR(temp), | 
 | 130 | 	POWER_SUPPLY_ATTR(temp_ambient), | 
 | 131 | 	POWER_SUPPLY_ATTR(time_to_empty_now), | 
 | 132 | 	POWER_SUPPLY_ATTR(time_to_empty_avg), | 
 | 133 | 	POWER_SUPPLY_ATTR(time_to_full_now), | 
 | 134 | 	POWER_SUPPLY_ATTR(time_to_full_avg), | 
 | 135 | 	/* Properties of type `const char *' */ | 
 | 136 | 	POWER_SUPPLY_ATTR(model_name), | 
 | 137 | 	POWER_SUPPLY_ATTR(manufacturer), | 
| maximilian attems | 7c2670b | 2008-01-22 18:46:50 +0100 | [diff] [blame] | 138 | 	POWER_SUPPLY_ATTR(serial_number), | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 139 | }; | 
 | 140 |  | 
 | 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 | }; | 
 | 153 |  | 
 | 154 | int power_supply_create_attrs(struct power_supply *psy) | 
 | 155 | { | 
 | 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 | 	} | 
 | 165 |  | 
 | 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; | 
 | 171 | 	} | 
 | 172 |  | 
 | 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--) | 
| Andres Salomon | 839dc9f | 2007-12-12 14:12:59 -0500 | [diff] [blame] | 181 | 		device_remove_file(psy->dev, &power_supply_static_attrs[i]); | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 182 | succeed: | 
 | 183 | 	return rc; | 
 | 184 | } | 
 | 185 |  | 
 | 186 | void power_supply_remove_attrs(struct power_supply *psy) | 
 | 187 | { | 
 | 188 | 	int i; | 
 | 189 |  | 
 | 190 | 	for (i = 0; i < ARRAY_SIZE(power_supply_static_attrs); i++) | 
| Andres Salomon | 839dc9f | 2007-12-12 14:12:59 -0500 | [diff] [blame] | 191 | 		device_remove_file(psy->dev, &power_supply_static_attrs[i]); | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 192 |  | 
 | 193 | 	for (i = 0; i < psy->num_properties; i++) | 
 | 194 | 		device_remove_file(psy->dev, | 
 | 195 | 			    &power_supply_attrs[psy->properties[i]]); | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 196 | } | 
 | 197 |  | 
 | 198 | static char *kstruprdup(const char *str, gfp_t gfp) | 
 | 199 | { | 
 | 200 | 	char *ret, *ustr; | 
 | 201 |  | 
 | 202 | 	ustr = ret = kmalloc(strlen(str) + 1, gfp); | 
 | 203 |  | 
 | 204 | 	if (!ret) | 
 | 205 | 		return NULL; | 
 | 206 |  | 
 | 207 | 	while (*str) | 
 | 208 | 		*ustr++ = toupper(*str++); | 
 | 209 |  | 
 | 210 | 	*ustr = 0; | 
 | 211 |  | 
 | 212 | 	return ret; | 
 | 213 | } | 
 | 214 |  | 
| Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 215 | int power_supply_uevent(struct device *dev, struct kobj_uevent_env *env) | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 216 | { | 
 | 217 | 	struct power_supply *psy = dev_get_drvdata(dev); | 
| Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 218 | 	int ret = 0, j; | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 219 | 	char *prop_buf; | 
 | 220 | 	char *attrname; | 
 | 221 |  | 
 | 222 | 	dev_dbg(dev, "uevent\n"); | 
 | 223 |  | 
| Dmitry Baryshkov | 56fa18e | 2008-06-08 19:43:42 +0400 | [diff] [blame] | 224 | 	if (!psy || !psy->dev) { | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 225 | 		dev_dbg(dev, "No power supply yet\n"); | 
 | 226 | 		return ret; | 
 | 227 | 	} | 
 | 228 |  | 
 | 229 | 	dev_dbg(dev, "POWER_SUPPLY_NAME=%s\n", psy->name); | 
 | 230 |  | 
| Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 231 | 	ret = add_uevent_var(env, "POWER_SUPPLY_NAME=%s", psy->name); | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 232 | 	if (ret) | 
 | 233 | 		return ret; | 
 | 234 |  | 
 | 235 | 	prop_buf = (char *)get_zeroed_page(GFP_KERNEL); | 
 | 236 | 	if (!prop_buf) | 
 | 237 | 		return -ENOMEM; | 
 | 238 |  | 
 | 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 |  | 
| Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 261 | 		ret = add_uevent_var(env, "POWER_SUPPLY_%s=%s", attrname, prop_buf); | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 262 | 		kfree(attrname); | 
 | 263 | 		if (ret) | 
 | 264 | 			goto out; | 
 | 265 | 	} | 
 | 266 |  | 
 | 267 | 	dev_dbg(dev, "%zd dynamic props\n", psy->num_properties); | 
 | 268 |  | 
 | 269 | 	for (j = 0; j < psy->num_properties; j++) { | 
 | 270 | 		struct device_attribute *attr; | 
 | 271 | 		char *line; | 
 | 272 |  | 
 | 273 | 		attr = &power_supply_attrs[psy->properties[j]]; | 
 | 274 |  | 
 | 275 | 		ret = power_supply_show_property(dev, attr, prop_buf); | 
 | 276 | 		if (ret == -ENODEV) { | 
 | 277 | 			/* When a battery is absent, we expect -ENODEV. Don't abort; | 
 | 278 | 			   send the uevent with at least the the PRESENT=0 property */ | 
 | 279 | 			ret = 0; | 
 | 280 | 			continue; | 
 | 281 | 		} | 
 | 282 |  | 
 | 283 | 		if (ret < 0) | 
 | 284 | 			goto out; | 
 | 285 |  | 
 | 286 | 		line = strchr(prop_buf, '\n'); | 
 | 287 | 		if (line) | 
 | 288 | 			*line = 0; | 
 | 289 |  | 
 | 290 | 		attrname = kstruprdup(attr->attr.name, GFP_KERNEL); | 
 | 291 | 		if (!attrname) { | 
 | 292 | 			ret = -ENOMEM; | 
 | 293 | 			goto out; | 
 | 294 | 		} | 
 | 295 |  | 
 | 296 | 		dev_dbg(dev, "prop %s=%s\n", attrname, prop_buf); | 
 | 297 |  | 
| Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 298 | 		ret = add_uevent_var(env, "POWER_SUPPLY_%s=%s", attrname, prop_buf); | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 299 | 		kfree(attrname); | 
 | 300 | 		if (ret) | 
 | 301 | 			goto out; | 
 | 302 | 	} | 
| Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 303 |  | 
 | 304 | out: | 
 | 305 | 	free_page((unsigned long)prop_buf); | 
 | 306 |  | 
 | 307 | 	return ret; | 
 | 308 | } |