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 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 156 | static struct power_supply *test_power_supplies[TEST_POWER_NUM]; |
| 157 | |
| 158 | static const struct power_supply_desc test_power_desc[] = { |
Krzysztof Kozlowski | a30067b | 2015-01-23 11:38:00 +0100 | [diff] [blame] | 159 | [TEST_AC] = { |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 160 | .name = "test_ac", |
| 161 | .type = POWER_SUPPLY_TYPE_MAINS, |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 162 | .properties = test_power_ac_props, |
| 163 | .num_properties = ARRAY_SIZE(test_power_ac_props), |
| 164 | .get_property = test_power_get_ac_property, |
Krzysztof Kozlowski | a30067b | 2015-01-23 11:38:00 +0100 | [diff] [blame] | 165 | }, |
| 166 | [TEST_BATTERY] = { |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 167 | .name = "test_battery", |
| 168 | .type = POWER_SUPPLY_TYPE_BATTERY, |
| 169 | .properties = test_power_battery_props, |
| 170 | .num_properties = ARRAY_SIZE(test_power_battery_props), |
| 171 | .get_property = test_power_get_battery_property, |
Krzysztof Kozlowski | a30067b | 2015-01-23 11:38:00 +0100 | [diff] [blame] | 172 | }, |
| 173 | [TEST_USB] = { |
Dmitry Eremin-Solenikov | 7384737 | 2012-05-29 13:37:43 +0400 | [diff] [blame] | 174 | .name = "test_usb", |
| 175 | .type = POWER_SUPPLY_TYPE_USB, |
Dmitry Eremin-Solenikov | 7384737 | 2012-05-29 13:37:43 +0400 | [diff] [blame] | 176 | .properties = test_power_ac_props, |
| 177 | .num_properties = ARRAY_SIZE(test_power_ac_props), |
| 178 | .get_property = test_power_get_usb_property, |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 179 | }, |
| 180 | }; |
| 181 | |
Krzysztof Kozlowski | 2dc9215 | 2015-03-12 08:44:02 +0100 | [diff] [blame] | 182 | static const struct power_supply_config test_power_configs[] = { |
| 183 | { |
| 184 | /* test_ac */ |
| 185 | .supplied_to = test_power_ac_supplied_to, |
| 186 | .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to), |
| 187 | }, { |
| 188 | /* test_battery */ |
| 189 | }, { |
| 190 | /* test_usb */ |
| 191 | .supplied_to = test_power_ac_supplied_to, |
| 192 | .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to), |
| 193 | }, |
| 194 | }; |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 195 | |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 196 | static int __init test_power_init(void) |
| 197 | { |
| 198 | int i; |
| 199 | int ret; |
| 200 | |
Krzysztof Kozlowski | a30067b | 2015-01-23 11:38:00 +0100 | [diff] [blame] | 201 | BUILD_BUG_ON(TEST_POWER_NUM != ARRAY_SIZE(test_power_supplies)); |
Krzysztof Kozlowski | 2dc9215 | 2015-03-12 08:44:02 +0100 | [diff] [blame] | 202 | BUILD_BUG_ON(TEST_POWER_NUM != ARRAY_SIZE(test_power_configs)); |
Krzysztof Kozlowski | a30067b | 2015-01-23 11:38:00 +0100 | [diff] [blame] | 203 | |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 204 | for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++) { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 205 | test_power_supplies[i] = power_supply_register(NULL, |
| 206 | &test_power_desc[i], |
Krzysztof Kozlowski | 2dc9215 | 2015-03-12 08:44:02 +0100 | [diff] [blame] | 207 | &test_power_configs[i]); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 208 | if (IS_ERR(test_power_supplies[i])) { |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 209 | pr_err("%s: failed to register %s\n", __func__, |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 210 | test_power_desc[i].name); |
| 211 | ret = PTR_ERR(test_power_supplies[i]); |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 212 | goto failed; |
| 213 | } |
| 214 | } |
| 215 | |
Andrey Gelman | 9239ebc | 2013-01-27 22:16:33 +0200 | [diff] [blame] | 216 | module_initialized = true; |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 217 | return 0; |
| 218 | failed: |
| 219 | while (--i >= 0) |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 220 | power_supply_unregister(test_power_supplies[i]); |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 221 | return ret; |
| 222 | } |
| 223 | module_init(test_power_init); |
| 224 | |
| 225 | static void __exit test_power_exit(void) |
| 226 | { |
| 227 | int i; |
| 228 | |
| 229 | /* Let's see how we handle changes... */ |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 230 | ac_online = 0; |
Dmitry Eremin-Solenikov | 7384737 | 2012-05-29 13:37:43 +0400 | [diff] [blame] | 231 | usb_online = 0; |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 232 | battery_status = POWER_SUPPLY_STATUS_DISCHARGING; |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 233 | for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++) |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 234 | power_supply_changed(test_power_supplies[i]); |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 235 | pr_info("%s: 'changed' event sent, sleeping for 10 seconds...\n", |
| 236 | __func__); |
| 237 | ssleep(10); |
| 238 | |
| 239 | for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++) |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 240 | power_supply_unregister(test_power_supplies[i]); |
Andrey Gelman | 9239ebc | 2013-01-27 22:16:33 +0200 | [diff] [blame] | 241 | |
| 242 | module_initialized = false; |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 243 | } |
| 244 | module_exit(test_power_exit); |
| 245 | |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 246 | |
| 247 | |
| 248 | #define MAX_KEYLENGTH 256 |
| 249 | struct battery_property_map { |
| 250 | int value; |
| 251 | char const *key; |
| 252 | }; |
| 253 | |
| 254 | static struct battery_property_map map_ac_online[] = { |
Andrey Gelman | 9239ebc | 2013-01-27 22:16:33 +0200 | [diff] [blame] | 255 | { 0, "off" }, |
| 256 | { 1, "on" }, |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 257 | { -1, NULL }, |
| 258 | }; |
| 259 | |
| 260 | static struct battery_property_map map_status[] = { |
| 261 | { POWER_SUPPLY_STATUS_CHARGING, "charging" }, |
| 262 | { POWER_SUPPLY_STATUS_DISCHARGING, "discharging" }, |
| 263 | { POWER_SUPPLY_STATUS_NOT_CHARGING, "not-charging" }, |
| 264 | { POWER_SUPPLY_STATUS_FULL, "full" }, |
| 265 | { -1, NULL }, |
| 266 | }; |
| 267 | |
| 268 | static struct battery_property_map map_health[] = { |
| 269 | { POWER_SUPPLY_HEALTH_GOOD, "good" }, |
| 270 | { POWER_SUPPLY_HEALTH_OVERHEAT, "overheat" }, |
| 271 | { POWER_SUPPLY_HEALTH_DEAD, "dead" }, |
| 272 | { POWER_SUPPLY_HEALTH_OVERVOLTAGE, "overvoltage" }, |
| 273 | { POWER_SUPPLY_HEALTH_UNSPEC_FAILURE, "failure" }, |
| 274 | { -1, NULL }, |
| 275 | }; |
| 276 | |
| 277 | static struct battery_property_map map_present[] = { |
| 278 | { 0, "false" }, |
| 279 | { 1, "true" }, |
| 280 | { -1, NULL }, |
| 281 | }; |
| 282 | |
| 283 | static struct battery_property_map map_technology[] = { |
| 284 | { POWER_SUPPLY_TECHNOLOGY_NiMH, "NiMH" }, |
| 285 | { POWER_SUPPLY_TECHNOLOGY_LION, "LION" }, |
| 286 | { POWER_SUPPLY_TECHNOLOGY_LIPO, "LIPO" }, |
| 287 | { POWER_SUPPLY_TECHNOLOGY_LiFe, "LiFe" }, |
| 288 | { POWER_SUPPLY_TECHNOLOGY_NiCd, "NiCd" }, |
| 289 | { POWER_SUPPLY_TECHNOLOGY_LiMn, "LiMn" }, |
| 290 | { -1, NULL }, |
| 291 | }; |
| 292 | |
| 293 | |
| 294 | static int map_get_value(struct battery_property_map *map, const char *key, |
| 295 | int def_val) |
| 296 | { |
| 297 | char buf[MAX_KEYLENGTH]; |
| 298 | int cr; |
| 299 | |
| 300 | strncpy(buf, key, MAX_KEYLENGTH); |
| 301 | buf[MAX_KEYLENGTH-1] = '\0'; |
| 302 | |
| 303 | cr = strnlen(buf, MAX_KEYLENGTH) - 1; |
Sasha Levin | 6b9140f | 2015-12-22 12:43:36 -0500 | [diff] [blame] | 304 | if (cr < 0) |
| 305 | return def_val; |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 306 | if (buf[cr] == '\n') |
| 307 | buf[cr] = '\0'; |
| 308 | |
| 309 | while (map->key) { |
| 310 | if (strncasecmp(map->key, buf, MAX_KEYLENGTH) == 0) |
| 311 | return map->value; |
| 312 | map++; |
| 313 | } |
| 314 | |
| 315 | return def_val; |
| 316 | } |
| 317 | |
| 318 | |
| 319 | static const char *map_get_key(struct battery_property_map *map, int value, |
| 320 | const char *def_key) |
| 321 | { |
| 322 | while (map->key) { |
| 323 | if (map->value == value) |
| 324 | return map->key; |
| 325 | map++; |
| 326 | } |
| 327 | |
| 328 | return def_key; |
| 329 | } |
| 330 | |
Andrey Gelman | 9239ebc | 2013-01-27 22:16:33 +0200 | [diff] [blame] | 331 | static inline void signal_power_supply_changed(struct power_supply *psy) |
| 332 | { |
| 333 | if (module_initialized) |
| 334 | power_supply_changed(psy); |
| 335 | } |
| 336 | |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 337 | static int param_set_ac_online(const char *key, const struct kernel_param *kp) |
| 338 | { |
| 339 | ac_online = map_get_value(map_ac_online, key, ac_online); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 340 | signal_power_supply_changed(test_power_supplies[TEST_AC]); |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | static int param_get_ac_online(char *buffer, const struct kernel_param *kp) |
| 345 | { |
| 346 | strcpy(buffer, map_get_key(map_ac_online, ac_online, "unknown")); |
| 347 | return strlen(buffer); |
| 348 | } |
| 349 | |
Dmitry Eremin-Solenikov | 7384737 | 2012-05-29 13:37:43 +0400 | [diff] [blame] | 350 | static int param_set_usb_online(const char *key, const struct kernel_param *kp) |
| 351 | { |
| 352 | usb_online = map_get_value(map_ac_online, key, usb_online); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 353 | signal_power_supply_changed(test_power_supplies[TEST_USB]); |
Dmitry Eremin-Solenikov | 7384737 | 2012-05-29 13:37:43 +0400 | [diff] [blame] | 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | static int param_get_usb_online(char *buffer, const struct kernel_param *kp) |
| 358 | { |
| 359 | strcpy(buffer, map_get_key(map_ac_online, usb_online, "unknown")); |
| 360 | return strlen(buffer); |
| 361 | } |
| 362 | |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 363 | static int param_set_battery_status(const char *key, |
| 364 | const struct kernel_param *kp) |
| 365 | { |
| 366 | battery_status = map_get_value(map_status, key, battery_status); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 367 | signal_power_supply_changed(test_power_supplies[TEST_BATTERY]); |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | static int param_get_battery_status(char *buffer, const struct kernel_param *kp) |
| 372 | { |
| 373 | strcpy(buffer, map_get_key(map_status, battery_status, "unknown")); |
| 374 | return strlen(buffer); |
| 375 | } |
| 376 | |
| 377 | static int param_set_battery_health(const char *key, |
| 378 | const struct kernel_param *kp) |
| 379 | { |
| 380 | battery_health = map_get_value(map_health, key, battery_health); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 381 | signal_power_supply_changed(test_power_supplies[TEST_BATTERY]); |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | static int param_get_battery_health(char *buffer, const struct kernel_param *kp) |
| 386 | { |
| 387 | strcpy(buffer, map_get_key(map_health, battery_health, "unknown")); |
| 388 | return strlen(buffer); |
| 389 | } |
| 390 | |
| 391 | static int param_set_battery_present(const char *key, |
| 392 | const struct kernel_param *kp) |
| 393 | { |
| 394 | battery_present = map_get_value(map_present, key, battery_present); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 395 | signal_power_supply_changed(test_power_supplies[TEST_AC]); |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | static int param_get_battery_present(char *buffer, |
| 400 | const struct kernel_param *kp) |
| 401 | { |
| 402 | strcpy(buffer, map_get_key(map_present, battery_present, "unknown")); |
| 403 | return strlen(buffer); |
| 404 | } |
| 405 | |
| 406 | static int param_set_battery_technology(const char *key, |
| 407 | const struct kernel_param *kp) |
| 408 | { |
| 409 | battery_technology = map_get_value(map_technology, key, |
| 410 | battery_technology); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 411 | signal_power_supply_changed(test_power_supplies[TEST_BATTERY]); |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | static int param_get_battery_technology(char *buffer, |
| 416 | const struct kernel_param *kp) |
| 417 | { |
| 418 | strcpy(buffer, |
| 419 | map_get_key(map_technology, battery_technology, "unknown")); |
| 420 | return strlen(buffer); |
| 421 | } |
| 422 | |
| 423 | static int param_set_battery_capacity(const char *key, |
| 424 | const struct kernel_param *kp) |
| 425 | { |
| 426 | int tmp; |
| 427 | |
| 428 | if (1 != sscanf(key, "%d", &tmp)) |
| 429 | return -EINVAL; |
| 430 | |
| 431 | battery_capacity = tmp; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 432 | signal_power_supply_changed(test_power_supplies[TEST_BATTERY]); |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | #define param_get_battery_capacity param_get_int |
| 437 | |
Dmitry Eremin-Solenikov | 85a392d | 2012-05-29 13:37:44 +0400 | [diff] [blame] | 438 | static int param_set_battery_voltage(const char *key, |
| 439 | const struct kernel_param *kp) |
| 440 | { |
| 441 | int tmp; |
| 442 | |
| 443 | if (1 != sscanf(key, "%d", &tmp)) |
| 444 | return -EINVAL; |
| 445 | |
| 446 | battery_voltage = tmp; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 447 | signal_power_supply_changed(test_power_supplies[TEST_BATTERY]); |
Dmitry Eremin-Solenikov | 85a392d | 2012-05-29 13:37:44 +0400 | [diff] [blame] | 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | #define param_get_battery_voltage param_get_int |
| 452 | |
Luis R. Rodriguez | 9c27847 | 2015-05-27 11:09:38 +0930 | [diff] [blame] | 453 | static const struct kernel_param_ops param_ops_ac_online = { |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 454 | .set = param_set_ac_online, |
| 455 | .get = param_get_ac_online, |
| 456 | }; |
| 457 | |
Luis R. Rodriguez | 9c27847 | 2015-05-27 11:09:38 +0930 | [diff] [blame] | 458 | static const struct kernel_param_ops param_ops_usb_online = { |
Dmitry Eremin-Solenikov | 7384737 | 2012-05-29 13:37:43 +0400 | [diff] [blame] | 459 | .set = param_set_usb_online, |
| 460 | .get = param_get_usb_online, |
| 461 | }; |
| 462 | |
Luis R. Rodriguez | 9c27847 | 2015-05-27 11:09:38 +0930 | [diff] [blame] | 463 | static const struct kernel_param_ops param_ops_battery_status = { |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 464 | .set = param_set_battery_status, |
| 465 | .get = param_get_battery_status, |
| 466 | }; |
| 467 | |
Luis R. Rodriguez | 9c27847 | 2015-05-27 11:09:38 +0930 | [diff] [blame] | 468 | static const struct kernel_param_ops param_ops_battery_present = { |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 469 | .set = param_set_battery_present, |
| 470 | .get = param_get_battery_present, |
| 471 | }; |
| 472 | |
Luis R. Rodriguez | 9c27847 | 2015-05-27 11:09:38 +0930 | [diff] [blame] | 473 | static const struct kernel_param_ops param_ops_battery_technology = { |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 474 | .set = param_set_battery_technology, |
| 475 | .get = param_get_battery_technology, |
| 476 | }; |
| 477 | |
Luis R. Rodriguez | 9c27847 | 2015-05-27 11:09:38 +0930 | [diff] [blame] | 478 | static const struct kernel_param_ops param_ops_battery_health = { |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 479 | .set = param_set_battery_health, |
| 480 | .get = param_get_battery_health, |
| 481 | }; |
| 482 | |
Luis R. Rodriguez | 9c27847 | 2015-05-27 11:09:38 +0930 | [diff] [blame] | 483 | static const struct kernel_param_ops param_ops_battery_capacity = { |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 484 | .set = param_set_battery_capacity, |
| 485 | .get = param_get_battery_capacity, |
| 486 | }; |
| 487 | |
Luis R. Rodriguez | 9c27847 | 2015-05-27 11:09:38 +0930 | [diff] [blame] | 488 | static const struct kernel_param_ops param_ops_battery_voltage = { |
Dmitry Eremin-Solenikov | 85a392d | 2012-05-29 13:37:44 +0400 | [diff] [blame] | 489 | .set = param_set_battery_voltage, |
| 490 | .get = param_get_battery_voltage, |
| 491 | }; |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 492 | |
| 493 | #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] | 494 | #define param_check_usb_online(name, p) __param_check(name, p, void); |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 495 | #define param_check_battery_status(name, p) __param_check(name, p, void); |
| 496 | #define param_check_battery_present(name, p) __param_check(name, p, void); |
| 497 | #define param_check_battery_technology(name, p) __param_check(name, p, void); |
| 498 | #define param_check_battery_health(name, p) __param_check(name, p, void); |
| 499 | #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] | 500 | #define param_check_battery_voltage(name, p) __param_check(name, p, void); |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 501 | |
| 502 | |
| 503 | module_param(ac_online, ac_online, 0644); |
| 504 | MODULE_PARM_DESC(ac_online, "AC charging state <on|off>"); |
| 505 | |
Dmitry Eremin-Solenikov | 7384737 | 2012-05-29 13:37:43 +0400 | [diff] [blame] | 506 | module_param(usb_online, usb_online, 0644); |
| 507 | MODULE_PARM_DESC(usb_online, "USB charging state <on|off>"); |
| 508 | |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 509 | module_param(battery_status, battery_status, 0644); |
| 510 | MODULE_PARM_DESC(battery_status, |
| 511 | "battery status <charging|discharging|not-charging|full>"); |
| 512 | |
| 513 | module_param(battery_present, battery_present, 0644); |
| 514 | MODULE_PARM_DESC(battery_present, |
| 515 | "battery presence state <good|overheat|dead|overvoltage|failure>"); |
| 516 | |
| 517 | module_param(battery_technology, battery_technology, 0644); |
| 518 | MODULE_PARM_DESC(battery_technology, |
| 519 | "battery technology <NiMH|LION|LIPO|LiFe|NiCd|LiMn>"); |
| 520 | |
| 521 | module_param(battery_health, battery_health, 0644); |
| 522 | MODULE_PARM_DESC(battery_health, |
| 523 | "battery health state <good|overheat|dead|overvoltage|failure>"); |
| 524 | |
| 525 | module_param(battery_capacity, battery_capacity, 0644); |
| 526 | MODULE_PARM_DESC(battery_capacity, "battery capacity (percentage)"); |
| 527 | |
Dmitry Eremin-Solenikov | 85a392d | 2012-05-29 13:37:44 +0400 | [diff] [blame] | 528 | module_param(battery_voltage, battery_voltage, 0644); |
| 529 | MODULE_PARM_DESC(battery_voltage, "battery voltage (millivolts)"); |
John Stultz | f17ef9b | 2011-04-25 20:25:24 -0700 | [diff] [blame] | 530 | |
Anton Vorontsov | a1e50fd | 2010-05-17 23:46:45 +0400 | [diff] [blame] | 531 | MODULE_DESCRIPTION("Power supply driver for testing"); |
| 532 | MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>"); |
| 533 | MODULE_LICENSE("GPL"); |