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