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