Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 1 | /* |
| 2 | * Power supply driver for testing. |
| 3 | * |
| 4 | * Copyright 2010 Anton Vorontsov <cbouatmailru@gmail.com> |
| 5 | * |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 6 | * Dynamic module parameter code from the Virtual Battery Driver |
| 7 | * Copyright (C) 2008 Pylone, Inc. |
| 8 | * By: Masashi YOKOTA <yokota@pylone.jp> |
| 9 | * Originally found here: |
| 10 | * http://downloads.pylone.jp/src/virtual_battery/virtual_battery-0.0.1.tar.bz2 |
| 11 | * |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License version 2 as |
| 14 | * published by the Free Software Foundation. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/power_supply.h> |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/delay.h> |
| 22 | #include <linux/vermagic.h> |
| 23 | |
Krzysztof Kozlowski | a30067b | 2015-01-23 11:38:00 +0100 | [diff] [blame] | 24 | enum test_power_id { |
| 25 | TEST_AC, |
| 26 | TEST_BATTERY, |
| 27 | TEST_USB, |
| 28 | TEST_POWER_NUM, |
| 29 | }; |
| 30 | |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 31 | static int ac_online = 1; |
Dmitry Eremin-Solenikov | 7384737 | 2012-05-29 13:37:43 +0400 | [diff] [blame] | 32 | static int usb_online = 1; |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 33 | static int battery_status = POWER_SUPPLY_STATUS_DISCHARGING; |
| 34 | static int battery_health = POWER_SUPPLY_HEALTH_GOOD; |
| 35 | static int battery_present = 1; /* true */ |
| 36 | static int battery_technology = POWER_SUPPLY_TECHNOLOGY_LION; |
| 37 | static int battery_capacity = 50; |
Dmitry Eremin-Solenikov | 85a392d | 2012-05-29 13:37:44 +0400 | [diff] [blame] | 38 | static int battery_voltage = 3300; |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 39 | |
Andrey Gelman | 9239ebc | 2013-01-27 22:16:33 +0200 | [diff] [blame] | 40 | static bool module_initialized; |
| 41 | |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 42 | static int test_power_get_ac_property(struct power_supply *psy, |
| 43 | enum power_supply_property psp, |
| 44 | union power_supply_propval *val) |
| 45 | { |
| 46 | switch (psp) { |
| 47 | case POWER_SUPPLY_PROP_ONLINE: |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 48 | val->intval = ac_online; |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 49 | break; |
| 50 | default: |
| 51 | return -EINVAL; |
| 52 | } |
| 53 | return 0; |
| 54 | } |
| 55 | |
Dmitry Eremin-Solenikov | 7384737 | 2012-05-29 13:37:43 +0400 | [diff] [blame] | 56 | static int test_power_get_usb_property(struct power_supply *psy, |
| 57 | enum power_supply_property psp, |
| 58 | union power_supply_propval *val) |
| 59 | { |
| 60 | switch (psp) { |
| 61 | case POWER_SUPPLY_PROP_ONLINE: |
| 62 | val->intval = usb_online; |
| 63 | break; |
| 64 | default: |
| 65 | return -EINVAL; |
| 66 | } |
| 67 | return 0; |
| 68 | } |
| 69 | |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 70 | static int test_power_get_battery_property(struct power_supply *psy, |
| 71 | enum power_supply_property psp, |
| 72 | union power_supply_propval *val) |
| 73 | { |
| 74 | switch (psp) { |
| 75 | case POWER_SUPPLY_PROP_MODEL_NAME: |
| 76 | val->strval = "Test battery"; |
| 77 | break; |
| 78 | case POWER_SUPPLY_PROP_MANUFACTURER: |
| 79 | val->strval = "Linux"; |
| 80 | break; |
| 81 | case POWER_SUPPLY_PROP_SERIAL_NUMBER: |
| 82 | val->strval = UTS_RELEASE; |
| 83 | break; |
| 84 | case POWER_SUPPLY_PROP_STATUS: |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 85 | val->intval = battery_status; |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 86 | break; |
| 87 | case POWER_SUPPLY_PROP_CHARGE_TYPE: |
| 88 | val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST; |
| 89 | break; |
| 90 | case POWER_SUPPLY_PROP_HEALTH: |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 91 | val->intval = battery_health; |
| 92 | break; |
| 93 | case POWER_SUPPLY_PROP_PRESENT: |
| 94 | val->intval = battery_present; |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 95 | break; |
| 96 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 97 | val->intval = battery_technology; |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 98 | break; |
| 99 | case POWER_SUPPLY_PROP_CAPACITY_LEVEL: |
| 100 | val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; |
| 101 | break; |
| 102 | case POWER_SUPPLY_PROP_CAPACITY: |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 103 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
| 104 | val->intval = battery_capacity; |
| 105 | break; |
| 106 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
| 107 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 108 | val->intval = 100; |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 109 | break; |
| 110 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: |
| 111 | case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW: |
| 112 | val->intval = 3600; |
| 113 | break; |
Dmitry Eremin-Solenikov | 85a392d | 2012-05-29 13:37:44 +0400 | [diff] [blame] | 114 | case POWER_SUPPLY_PROP_TEMP: |
| 115 | val->intval = 26; |
| 116 | break; |
| 117 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 118 | val->intval = battery_voltage; |
| 119 | break; |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 120 | default: |
| 121 | pr_info("%s: some properties deliberately report errors.\n", |
| 122 | __func__); |
| 123 | return -EINVAL; |
| 124 | } |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static enum power_supply_property test_power_ac_props[] = { |
| 129 | POWER_SUPPLY_PROP_ONLINE, |
| 130 | }; |
| 131 | |
| 132 | static enum power_supply_property test_power_battery_props[] = { |
| 133 | POWER_SUPPLY_PROP_STATUS, |
| 134 | POWER_SUPPLY_PROP_CHARGE_TYPE, |
| 135 | POWER_SUPPLY_PROP_HEALTH, |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 136 | POWER_SUPPLY_PROP_PRESENT, |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 137 | POWER_SUPPLY_PROP_TECHNOLOGY, |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 138 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 139 | POWER_SUPPLY_PROP_CHARGE_FULL, |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 140 | POWER_SUPPLY_PROP_CHARGE_NOW, |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 141 | POWER_SUPPLY_PROP_CAPACITY, |
| 142 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
| 143 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, |
| 144 | POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, |
| 145 | POWER_SUPPLY_PROP_MODEL_NAME, |
| 146 | POWER_SUPPLY_PROP_MANUFACTURER, |
| 147 | POWER_SUPPLY_PROP_SERIAL_NUMBER, |
Dmitry Eremin-Solenikov | 85a392d | 2012-05-29 13:37:44 +0400 | [diff] [blame] | 148 | POWER_SUPPLY_PROP_TEMP, |
| 149 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | static char *test_power_ac_supplied_to[] = { |
| 153 | "test_battery", |
| 154 | }; |
| 155 | |
| 156 | static struct power_supply test_power_supplies[] = { |
Krzysztof Kozlowski | a30067b | 2015-01-23 11:38:00 +0100 | [diff] [blame] | 157 | [TEST_AC] = { |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 158 | .name = "test_ac", |
| 159 | .type = POWER_SUPPLY_TYPE_MAINS, |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 160 | .properties = test_power_ac_props, |
| 161 | .num_properties = ARRAY_SIZE(test_power_ac_props), |
| 162 | .get_property = test_power_get_ac_property, |
Krzysztof Kozlowski | a30067b | 2015-01-23 11:38:00 +0100 | [diff] [blame] | 163 | }, |
| 164 | [TEST_BATTERY] = { |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 165 | .name = "test_battery", |
| 166 | .type = POWER_SUPPLY_TYPE_BATTERY, |
| 167 | .properties = test_power_battery_props, |
| 168 | .num_properties = ARRAY_SIZE(test_power_battery_props), |
| 169 | .get_property = test_power_get_battery_property, |
Krzysztof Kozlowski | a30067b | 2015-01-23 11:38:00 +0100 | [diff] [blame] | 170 | }, |
| 171 | [TEST_USB] = { |
Dmitry Eremin-Solenikov | 7384737 | 2012-05-29 13:37:43 +0400 | [diff] [blame] | 172 | .name = "test_usb", |
| 173 | .type = POWER_SUPPLY_TYPE_USB, |
Dmitry Eremin-Solenikov | 7384737 | 2012-05-29 13:37:43 +0400 | [diff] [blame] | 174 | .properties = test_power_ac_props, |
| 175 | .num_properties = ARRAY_SIZE(test_power_ac_props), |
| 176 | .get_property = test_power_get_usb_property, |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 177 | }, |
| 178 | }; |
| 179 | |
Krzysztof Kozlowski | 2dc9215 | 2015-03-12 08:44:02 +0100 | [diff] [blame^] | 180 | static const struct power_supply_config test_power_configs[] = { |
| 181 | { |
| 182 | /* test_ac */ |
| 183 | .supplied_to = test_power_ac_supplied_to, |
| 184 | .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to), |
| 185 | }, { |
| 186 | /* test_battery */ |
| 187 | }, { |
| 188 | /* test_usb */ |
| 189 | .supplied_to = test_power_ac_supplied_to, |
| 190 | .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to), |
| 191 | }, |
| 192 | }; |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 193 | |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 194 | static int __init test_power_init(void) |
| 195 | { |
| 196 | int i; |
| 197 | int ret; |
| 198 | |
Krzysztof Kozlowski | a30067b | 2015-01-23 11:38:00 +0100 | [diff] [blame] | 199 | BUILD_BUG_ON(TEST_POWER_NUM != ARRAY_SIZE(test_power_supplies)); |
Krzysztof Kozlowski | 2dc9215 | 2015-03-12 08:44:02 +0100 | [diff] [blame^] | 200 | BUILD_BUG_ON(TEST_POWER_NUM != ARRAY_SIZE(test_power_configs)); |
Krzysztof Kozlowski | a30067b | 2015-01-23 11:38:00 +0100 | [diff] [blame] | 201 | |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 202 | for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++) { |
Krzysztof Kozlowski | 2dc9215 | 2015-03-12 08:44:02 +0100 | [diff] [blame^] | 203 | ret = power_supply_register(NULL, &test_power_supplies[i], |
| 204 | &test_power_configs[i]); |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 205 | if (ret) { |
| 206 | pr_err("%s: failed to register %s\n", __func__, |
| 207 | test_power_supplies[i].name); |
| 208 | goto failed; |
| 209 | } |
| 210 | } |
| 211 | |
Andrey Gelman | 9239ebc | 2013-01-27 22:16:33 +0200 | [diff] [blame] | 212 | module_initialized = true; |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 213 | return 0; |
| 214 | failed: |
| 215 | while (--i >= 0) |
| 216 | power_supply_unregister(&test_power_supplies[i]); |
| 217 | return ret; |
| 218 | } |
| 219 | module_init(test_power_init); |
| 220 | |
| 221 | static void __exit test_power_exit(void) |
| 222 | { |
| 223 | int i; |
| 224 | |
| 225 | /* Let's see how we handle changes... */ |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 226 | ac_online = 0; |
Dmitry Eremin-Solenikov | 7384737 | 2012-05-29 13:37:43 +0400 | [diff] [blame] | 227 | usb_online = 0; |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 228 | battery_status = POWER_SUPPLY_STATUS_DISCHARGING; |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 229 | for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++) |
| 230 | power_supply_changed(&test_power_supplies[i]); |
| 231 | pr_info("%s: 'changed' event sent, sleeping for 10 seconds...\n", |
| 232 | __func__); |
| 233 | ssleep(10); |
| 234 | |
| 235 | for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++) |
| 236 | power_supply_unregister(&test_power_supplies[i]); |
Andrey Gelman | 9239ebc | 2013-01-27 22:16:33 +0200 | [diff] [blame] | 237 | |
| 238 | module_initialized = false; |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 239 | } |
| 240 | module_exit(test_power_exit); |
| 241 | |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 242 | |
| 243 | |
| 244 | #define MAX_KEYLENGTH 256 |
| 245 | struct battery_property_map { |
| 246 | int value; |
| 247 | char const *key; |
| 248 | }; |
| 249 | |
| 250 | static struct battery_property_map map_ac_online[] = { |
Andrey Gelman | 9239ebc | 2013-01-27 22:16:33 +0200 | [diff] [blame] | 251 | { 0, "off" }, |
| 252 | { 1, "on" }, |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 253 | { -1, NULL }, |
| 254 | }; |
| 255 | |
| 256 | static struct battery_property_map map_status[] = { |
| 257 | { POWER_SUPPLY_STATUS_CHARGING, "charging" }, |
| 258 | { POWER_SUPPLY_STATUS_DISCHARGING, "discharging" }, |
| 259 | { POWER_SUPPLY_STATUS_NOT_CHARGING, "not-charging" }, |
| 260 | { POWER_SUPPLY_STATUS_FULL, "full" }, |
| 261 | { -1, NULL }, |
| 262 | }; |
| 263 | |
| 264 | static struct battery_property_map map_health[] = { |
| 265 | { POWER_SUPPLY_HEALTH_GOOD, "good" }, |
| 266 | { POWER_SUPPLY_HEALTH_OVERHEAT, "overheat" }, |
| 267 | { POWER_SUPPLY_HEALTH_DEAD, "dead" }, |
| 268 | { POWER_SUPPLY_HEALTH_OVERVOLTAGE, "overvoltage" }, |
| 269 | { POWER_SUPPLY_HEALTH_UNSPEC_FAILURE, "failure" }, |
| 270 | { -1, NULL }, |
| 271 | }; |
| 272 | |
| 273 | static struct battery_property_map map_present[] = { |
| 274 | { 0, "false" }, |
| 275 | { 1, "true" }, |
| 276 | { -1, NULL }, |
| 277 | }; |
| 278 | |
| 279 | static struct battery_property_map map_technology[] = { |
| 280 | { POWER_SUPPLY_TECHNOLOGY_NiMH, "NiMH" }, |
| 281 | { POWER_SUPPLY_TECHNOLOGY_LION, "LION" }, |
| 282 | { POWER_SUPPLY_TECHNOLOGY_LIPO, "LIPO" }, |
| 283 | { POWER_SUPPLY_TECHNOLOGY_LiFe, "LiFe" }, |
| 284 | { POWER_SUPPLY_TECHNOLOGY_NiCd, "NiCd" }, |
| 285 | { POWER_SUPPLY_TECHNOLOGY_LiMn, "LiMn" }, |
| 286 | { -1, NULL }, |
| 287 | }; |
| 288 | |
| 289 | |
| 290 | static int map_get_value(struct battery_property_map *map, const char *key, |
| 291 | int def_val) |
| 292 | { |
| 293 | char buf[MAX_KEYLENGTH]; |
| 294 | int cr; |
| 295 | |
| 296 | strncpy(buf, key, MAX_KEYLENGTH); |
| 297 | buf[MAX_KEYLENGTH-1] = '\0'; |
| 298 | |
| 299 | cr = strnlen(buf, MAX_KEYLENGTH) - 1; |
| 300 | if (buf[cr] == '\n') |
| 301 | buf[cr] = '\0'; |
| 302 | |
| 303 | while (map->key) { |
| 304 | if (strncasecmp(map->key, buf, MAX_KEYLENGTH) == 0) |
| 305 | return map->value; |
| 306 | map++; |
| 307 | } |
| 308 | |
| 309 | return def_val; |
| 310 | } |
| 311 | |
| 312 | |
| 313 | static const char *map_get_key(struct battery_property_map *map, int value, |
| 314 | const char *def_key) |
| 315 | { |
| 316 | while (map->key) { |
| 317 | if (map->value == value) |
| 318 | return map->key; |
| 319 | map++; |
| 320 | } |
| 321 | |
| 322 | return def_key; |
| 323 | } |
| 324 | |
Andrey Gelman | 9239ebc | 2013-01-27 22:16:33 +0200 | [diff] [blame] | 325 | static inline void signal_power_supply_changed(struct power_supply *psy) |
| 326 | { |
| 327 | if (module_initialized) |
| 328 | power_supply_changed(psy); |
| 329 | } |
| 330 | |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 331 | static int param_set_ac_online(const char *key, const struct kernel_param *kp) |
| 332 | { |
| 333 | ac_online = map_get_value(map_ac_online, key, ac_online); |
Krzysztof Kozlowski | a30067b | 2015-01-23 11:38:00 +0100 | [diff] [blame] | 334 | signal_power_supply_changed(&test_power_supplies[TEST_AC]); |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | static int param_get_ac_online(char *buffer, const struct kernel_param *kp) |
| 339 | { |
| 340 | strcpy(buffer, map_get_key(map_ac_online, ac_online, "unknown")); |
| 341 | return strlen(buffer); |
| 342 | } |
| 343 | |
Dmitry Eremin-Solenikov | 7384737 | 2012-05-29 13:37:43 +0400 | [diff] [blame] | 344 | static int param_set_usb_online(const char *key, const struct kernel_param *kp) |
| 345 | { |
| 346 | usb_online = map_get_value(map_ac_online, key, usb_online); |
Krzysztof Kozlowski | a30067b | 2015-01-23 11:38:00 +0100 | [diff] [blame] | 347 | signal_power_supply_changed(&test_power_supplies[TEST_USB]); |
Dmitry Eremin-Solenikov | 7384737 | 2012-05-29 13:37:43 +0400 | [diff] [blame] | 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | static int param_get_usb_online(char *buffer, const struct kernel_param *kp) |
| 352 | { |
| 353 | strcpy(buffer, map_get_key(map_ac_online, usb_online, "unknown")); |
| 354 | return strlen(buffer); |
| 355 | } |
| 356 | |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 357 | static int param_set_battery_status(const char *key, |
| 358 | const struct kernel_param *kp) |
| 359 | { |
| 360 | battery_status = map_get_value(map_status, key, battery_status); |
Krzysztof Kozlowski | a30067b | 2015-01-23 11:38:00 +0100 | [diff] [blame] | 361 | signal_power_supply_changed(&test_power_supplies[TEST_BATTERY]); |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | static int param_get_battery_status(char *buffer, const struct kernel_param *kp) |
| 366 | { |
| 367 | strcpy(buffer, map_get_key(map_status, battery_status, "unknown")); |
| 368 | return strlen(buffer); |
| 369 | } |
| 370 | |
| 371 | static int param_set_battery_health(const char *key, |
| 372 | const struct kernel_param *kp) |
| 373 | { |
| 374 | battery_health = map_get_value(map_health, key, battery_health); |
Krzysztof Kozlowski | a30067b | 2015-01-23 11:38:00 +0100 | [diff] [blame] | 375 | signal_power_supply_changed(&test_power_supplies[TEST_BATTERY]); |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | static int param_get_battery_health(char *buffer, const struct kernel_param *kp) |
| 380 | { |
| 381 | strcpy(buffer, map_get_key(map_health, battery_health, "unknown")); |
| 382 | return strlen(buffer); |
| 383 | } |
| 384 | |
| 385 | static int param_set_battery_present(const char *key, |
| 386 | const struct kernel_param *kp) |
| 387 | { |
| 388 | battery_present = map_get_value(map_present, key, battery_present); |
Krzysztof Kozlowski | a30067b | 2015-01-23 11:38:00 +0100 | [diff] [blame] | 389 | signal_power_supply_changed(&test_power_supplies[TEST_AC]); |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | static int param_get_battery_present(char *buffer, |
| 394 | const struct kernel_param *kp) |
| 395 | { |
| 396 | strcpy(buffer, map_get_key(map_present, battery_present, "unknown")); |
| 397 | return strlen(buffer); |
| 398 | } |
| 399 | |
| 400 | static int param_set_battery_technology(const char *key, |
| 401 | const struct kernel_param *kp) |
| 402 | { |
| 403 | battery_technology = map_get_value(map_technology, key, |
| 404 | battery_technology); |
Krzysztof Kozlowski | a30067b | 2015-01-23 11:38:00 +0100 | [diff] [blame] | 405 | signal_power_supply_changed(&test_power_supplies[TEST_BATTERY]); |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | static int param_get_battery_technology(char *buffer, |
| 410 | const struct kernel_param *kp) |
| 411 | { |
| 412 | strcpy(buffer, |
| 413 | map_get_key(map_technology, battery_technology, "unknown")); |
| 414 | return strlen(buffer); |
| 415 | } |
| 416 | |
| 417 | static int param_set_battery_capacity(const char *key, |
| 418 | const struct kernel_param *kp) |
| 419 | { |
| 420 | int tmp; |
| 421 | |
| 422 | if (1 != sscanf(key, "%d", &tmp)) |
| 423 | return -EINVAL; |
| 424 | |
| 425 | battery_capacity = tmp; |
Krzysztof Kozlowski | a30067b | 2015-01-23 11:38:00 +0100 | [diff] [blame] | 426 | signal_power_supply_changed(&test_power_supplies[TEST_BATTERY]); |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | #define param_get_battery_capacity param_get_int |
| 431 | |
Dmitry Eremin-Solenikov | 85a392d | 2012-05-29 13:37:44 +0400 | [diff] [blame] | 432 | static int param_set_battery_voltage(const char *key, |
| 433 | const struct kernel_param *kp) |
| 434 | { |
| 435 | int tmp; |
| 436 | |
| 437 | if (1 != sscanf(key, "%d", &tmp)) |
| 438 | return -EINVAL; |
| 439 | |
| 440 | battery_voltage = tmp; |
Krzysztof Kozlowski | a30067b | 2015-01-23 11:38:00 +0100 | [diff] [blame] | 441 | signal_power_supply_changed(&test_power_supplies[TEST_BATTERY]); |
Dmitry Eremin-Solenikov | 85a392d | 2012-05-29 13:37:44 +0400 | [diff] [blame] | 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | #define param_get_battery_voltage param_get_int |
| 446 | |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 447 | static struct kernel_param_ops param_ops_ac_online = { |
| 448 | .set = param_set_ac_online, |
| 449 | .get = param_get_ac_online, |
| 450 | }; |
| 451 | |
Dmitry Eremin-Solenikov | 7384737 | 2012-05-29 13:37:43 +0400 | [diff] [blame] | 452 | static struct kernel_param_ops param_ops_usb_online = { |
| 453 | .set = param_set_usb_online, |
| 454 | .get = param_get_usb_online, |
| 455 | }; |
| 456 | |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 457 | static struct kernel_param_ops param_ops_battery_status = { |
| 458 | .set = param_set_battery_status, |
| 459 | .get = param_get_battery_status, |
| 460 | }; |
| 461 | |
| 462 | static struct kernel_param_ops param_ops_battery_present = { |
| 463 | .set = param_set_battery_present, |
| 464 | .get = param_get_battery_present, |
| 465 | }; |
| 466 | |
| 467 | static struct kernel_param_ops param_ops_battery_technology = { |
| 468 | .set = param_set_battery_technology, |
| 469 | .get = param_get_battery_technology, |
| 470 | }; |
| 471 | |
| 472 | static struct kernel_param_ops param_ops_battery_health = { |
| 473 | .set = param_set_battery_health, |
| 474 | .get = param_get_battery_health, |
| 475 | }; |
| 476 | |
| 477 | static struct kernel_param_ops param_ops_battery_capacity = { |
| 478 | .set = param_set_battery_capacity, |
| 479 | .get = param_get_battery_capacity, |
| 480 | }; |
| 481 | |
Dmitry Eremin-Solenikov | 85a392d | 2012-05-29 13:37:44 +0400 | [diff] [blame] | 482 | static struct kernel_param_ops param_ops_battery_voltage = { |
| 483 | .set = param_set_battery_voltage, |
| 484 | .get = param_get_battery_voltage, |
| 485 | }; |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 486 | |
| 487 | #define param_check_ac_online(name, p) __param_check(name, p, void); |
Dmitry Eremin-Solenikov | 7384737 | 2012-05-29 13:37:43 +0400 | [diff] [blame] | 488 | #define param_check_usb_online(name, p) __param_check(name, p, void); |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 489 | #define param_check_battery_status(name, p) __param_check(name, p, void); |
| 490 | #define param_check_battery_present(name, p) __param_check(name, p, void); |
| 491 | #define param_check_battery_technology(name, p) __param_check(name, p, void); |
| 492 | #define param_check_battery_health(name, p) __param_check(name, p, void); |
| 493 | #define param_check_battery_capacity(name, p) __param_check(name, p, void); |
Dmitry Eremin-Solenikov | 85a392d | 2012-05-29 13:37:44 +0400 | [diff] [blame] | 494 | #define param_check_battery_voltage(name, p) __param_check(name, p, void); |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 495 | |
| 496 | |
| 497 | module_param(ac_online, ac_online, 0644); |
| 498 | MODULE_PARM_DESC(ac_online, "AC charging state <on|off>"); |
| 499 | |
Dmitry Eremin-Solenikov | 7384737 | 2012-05-29 13:37:43 +0400 | [diff] [blame] | 500 | module_param(usb_online, usb_online, 0644); |
| 501 | MODULE_PARM_DESC(usb_online, "USB charging state <on|off>"); |
| 502 | |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 503 | module_param(battery_status, battery_status, 0644); |
| 504 | MODULE_PARM_DESC(battery_status, |
| 505 | "battery status <charging|discharging|not-charging|full>"); |
| 506 | |
| 507 | module_param(battery_present, battery_present, 0644); |
| 508 | MODULE_PARM_DESC(battery_present, |
| 509 | "battery presence state <good|overheat|dead|overvoltage|failure>"); |
| 510 | |
| 511 | module_param(battery_technology, battery_technology, 0644); |
| 512 | MODULE_PARM_DESC(battery_technology, |
| 513 | "battery technology <NiMH|LION|LIPO|LiFe|NiCd|LiMn>"); |
| 514 | |
| 515 | module_param(battery_health, battery_health, 0644); |
| 516 | MODULE_PARM_DESC(battery_health, |
| 517 | "battery health state <good|overheat|dead|overvoltage|failure>"); |
| 518 | |
| 519 | module_param(battery_capacity, battery_capacity, 0644); |
| 520 | MODULE_PARM_DESC(battery_capacity, "battery capacity (percentage)"); |
| 521 | |
Dmitry Eremin-Solenikov | 85a392d | 2012-05-29 13:37:44 +0400 | [diff] [blame] | 522 | module_param(battery_voltage, battery_voltage, 0644); |
| 523 | MODULE_PARM_DESC(battery_voltage, "battery voltage (millivolts)"); |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 524 | |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 525 | MODULE_DESCRIPTION("Power supply driver for testing"); |
| 526 | MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>"); |
| 527 | MODULE_LICENSE("GPL"); |