1 // SPDX-License-Identifier: GPL-2.0-only
3 * Power supply driver for testing.
5 * Copyright 2010 Anton Vorontsov <cbouatmailru@gmail.com>
7 * Dynamic module parameter code from the Virtual Battery Driver
8 * Copyright (C) 2008 Pylone, Inc.
9 * By: Masashi YOKOTA <yokota@pylone.jp>
10 * Originally found here:
11 * http://downloads.pylone.jp/src/virtual_battery/virtual_battery-0.0.1.tar.bz2
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/power_supply.h>
17 #include <linux/errno.h>
18 #include <linux/delay.h>
19 #include <generated/utsrelease.h>
28 static int ac_online = 1;
29 static int usb_online = 1;
30 static int battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
31 static int battery_health = POWER_SUPPLY_HEALTH_GOOD;
32 static int battery_present = 1; /* true */
33 static int battery_technology = POWER_SUPPLY_TECHNOLOGY_LION;
34 static int battery_capacity = 50;
35 static int battery_voltage = 3300;
36 static int battery_charge_counter = -1000;
37 static int battery_current = -1600;
39 static bool module_initialized;
41 static int test_power_get_ac_property(struct power_supply *psy,
42 enum power_supply_property psp,
43 union power_supply_propval *val)
46 case POWER_SUPPLY_PROP_ONLINE:
47 val->intval = ac_online;
55 static int test_power_get_usb_property(struct power_supply *psy,
56 enum power_supply_property psp,
57 union power_supply_propval *val)
60 case POWER_SUPPLY_PROP_ONLINE:
61 val->intval = usb_online;
69 static int test_power_get_battery_property(struct power_supply *psy,
70 enum power_supply_property psp,
71 union power_supply_propval *val)
74 case POWER_SUPPLY_PROP_MODEL_NAME:
75 val->strval = "Test battery";
77 case POWER_SUPPLY_PROP_MANUFACTURER:
78 val->strval = "Linux";
80 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
81 val->strval = UTS_RELEASE;
83 case POWER_SUPPLY_PROP_STATUS:
84 val->intval = battery_status;
86 case POWER_SUPPLY_PROP_CHARGE_TYPE:
87 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
89 case POWER_SUPPLY_PROP_HEALTH:
90 val->intval = battery_health;
92 case POWER_SUPPLY_PROP_PRESENT:
93 val->intval = battery_present;
95 case POWER_SUPPLY_PROP_TECHNOLOGY:
96 val->intval = battery_technology;
98 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
99 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
101 case POWER_SUPPLY_PROP_CAPACITY:
102 case POWER_SUPPLY_PROP_CHARGE_NOW:
103 val->intval = battery_capacity;
105 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
106 val->intval = battery_charge_counter;
108 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
109 case POWER_SUPPLY_PROP_CHARGE_FULL:
112 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
113 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
116 case POWER_SUPPLY_PROP_TEMP:
119 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
120 val->intval = battery_voltage;
122 case POWER_SUPPLY_PROP_CURRENT_AVG:
123 case POWER_SUPPLY_PROP_CURRENT_NOW:
124 val->intval = battery_current;
127 pr_info("%s: some properties deliberately report errors.\n",
134 static enum power_supply_property test_power_ac_props[] = {
135 POWER_SUPPLY_PROP_ONLINE,
138 static enum power_supply_property test_power_battery_props[] = {
139 POWER_SUPPLY_PROP_STATUS,
140 POWER_SUPPLY_PROP_CHARGE_TYPE,
141 POWER_SUPPLY_PROP_HEALTH,
142 POWER_SUPPLY_PROP_PRESENT,
143 POWER_SUPPLY_PROP_TECHNOLOGY,
144 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
145 POWER_SUPPLY_PROP_CHARGE_FULL,
146 POWER_SUPPLY_PROP_CHARGE_NOW,
147 POWER_SUPPLY_PROP_CHARGE_COUNTER,
148 POWER_SUPPLY_PROP_CAPACITY,
149 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
150 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
151 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
152 POWER_SUPPLY_PROP_MODEL_NAME,
153 POWER_SUPPLY_PROP_MANUFACTURER,
154 POWER_SUPPLY_PROP_SERIAL_NUMBER,
155 POWER_SUPPLY_PROP_TEMP,
156 POWER_SUPPLY_PROP_VOLTAGE_NOW,
157 POWER_SUPPLY_PROP_CURRENT_AVG,
158 POWER_SUPPLY_PROP_CURRENT_NOW,
161 static char *test_power_ac_supplied_to[] = {
165 static struct power_supply *test_power_supplies[TEST_POWER_NUM];
167 static const struct power_supply_desc test_power_desc[] = {
170 .type = POWER_SUPPLY_TYPE_MAINS,
171 .properties = test_power_ac_props,
172 .num_properties = ARRAY_SIZE(test_power_ac_props),
173 .get_property = test_power_get_ac_property,
176 .name = "test_battery",
177 .type = POWER_SUPPLY_TYPE_BATTERY,
178 .properties = test_power_battery_props,
179 .num_properties = ARRAY_SIZE(test_power_battery_props),
180 .get_property = test_power_get_battery_property,
184 .type = POWER_SUPPLY_TYPE_USB,
185 .properties = test_power_ac_props,
186 .num_properties = ARRAY_SIZE(test_power_ac_props),
187 .get_property = test_power_get_usb_property,
191 static const struct power_supply_config test_power_configs[] = {
194 .supplied_to = test_power_ac_supplied_to,
195 .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to),
200 .supplied_to = test_power_ac_supplied_to,
201 .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to),
205 static int __init test_power_init(void)
210 BUILD_BUG_ON(TEST_POWER_NUM != ARRAY_SIZE(test_power_supplies));
211 BUILD_BUG_ON(TEST_POWER_NUM != ARRAY_SIZE(test_power_configs));
213 for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++) {
214 test_power_supplies[i] = power_supply_register(NULL,
216 &test_power_configs[i]);
217 if (IS_ERR(test_power_supplies[i])) {
218 pr_err("%s: failed to register %s\n", __func__,
219 test_power_desc[i].name);
220 ret = PTR_ERR(test_power_supplies[i]);
225 module_initialized = true;
229 power_supply_unregister(test_power_supplies[i]);
232 module_init(test_power_init);
234 static void __exit test_power_exit(void)
238 /* Let's see how we handle changes... */
241 battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
242 for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++)
243 power_supply_changed(test_power_supplies[i]);
244 pr_info("%s: 'changed' event sent, sleeping for 10 seconds...\n",
248 for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++)
249 power_supply_unregister(test_power_supplies[i]);
251 module_initialized = false;
253 module_exit(test_power_exit);
257 #define MAX_KEYLENGTH 256
258 struct battery_property_map {
263 static struct battery_property_map map_ac_online[] = {
269 static struct battery_property_map map_status[] = {
270 { POWER_SUPPLY_STATUS_CHARGING, "charging" },
271 { POWER_SUPPLY_STATUS_DISCHARGING, "discharging" },
272 { POWER_SUPPLY_STATUS_NOT_CHARGING, "not-charging" },
273 { POWER_SUPPLY_STATUS_FULL, "full" },
277 static struct battery_property_map map_health[] = {
278 { POWER_SUPPLY_HEALTH_GOOD, "good" },
279 { POWER_SUPPLY_HEALTH_OVERHEAT, "overheat" },
280 { POWER_SUPPLY_HEALTH_DEAD, "dead" },
281 { POWER_SUPPLY_HEALTH_OVERVOLTAGE, "overvoltage" },
282 { POWER_SUPPLY_HEALTH_UNSPEC_FAILURE, "failure" },
286 static struct battery_property_map map_present[] = {
292 static struct battery_property_map map_technology[] = {
293 { POWER_SUPPLY_TECHNOLOGY_NiMH, "NiMH" },
294 { POWER_SUPPLY_TECHNOLOGY_LION, "LION" },
295 { POWER_SUPPLY_TECHNOLOGY_LIPO, "LIPO" },
296 { POWER_SUPPLY_TECHNOLOGY_LiFe, "LiFe" },
297 { POWER_SUPPLY_TECHNOLOGY_NiCd, "NiCd" },
298 { POWER_SUPPLY_TECHNOLOGY_LiMn, "LiMn" },
303 static int map_get_value(struct battery_property_map *map, const char *key,
306 char buf[MAX_KEYLENGTH];
309 strscpy(buf, key, MAX_KEYLENGTH);
311 cr = strnlen(buf, MAX_KEYLENGTH) - 1;
318 if (strncasecmp(map->key, buf, MAX_KEYLENGTH) == 0)
327 static const char *map_get_key(struct battery_property_map *map, int value,
331 if (map->value == value)
339 static inline void signal_power_supply_changed(struct power_supply *psy)
341 if (module_initialized)
342 power_supply_changed(psy);
345 static int param_set_ac_online(const char *key, const struct kernel_param *kp)
347 ac_online = map_get_value(map_ac_online, key, ac_online);
348 signal_power_supply_changed(test_power_supplies[TEST_AC]);
352 static int param_get_ac_online(char *buffer, const struct kernel_param *kp)
354 return sprintf(buffer, "%s\n",
355 map_get_key(map_ac_online, ac_online, "unknown"));
358 static int param_set_usb_online(const char *key, const struct kernel_param *kp)
360 usb_online = map_get_value(map_ac_online, key, usb_online);
361 signal_power_supply_changed(test_power_supplies[TEST_USB]);
365 static int param_get_usb_online(char *buffer, const struct kernel_param *kp)
367 return sprintf(buffer, "%s\n",
368 map_get_key(map_ac_online, usb_online, "unknown"));
371 static int param_set_battery_status(const char *key,
372 const struct kernel_param *kp)
374 battery_status = map_get_value(map_status, key, battery_status);
375 signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
379 static int param_get_battery_status(char *buffer, const struct kernel_param *kp)
381 return sprintf(buffer, "%s\n",
382 map_get_key(map_ac_online, battery_status, "unknown"));
385 static int param_set_battery_health(const char *key,
386 const struct kernel_param *kp)
388 battery_health = map_get_value(map_health, key, battery_health);
389 signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
393 static int param_get_battery_health(char *buffer, const struct kernel_param *kp)
395 return sprintf(buffer, "%s\n",
396 map_get_key(map_ac_online, battery_health, "unknown"));
399 static int param_set_battery_present(const char *key,
400 const struct kernel_param *kp)
402 battery_present = map_get_value(map_present, key, battery_present);
403 signal_power_supply_changed(test_power_supplies[TEST_AC]);
407 static int param_get_battery_present(char *buffer,
408 const struct kernel_param *kp)
410 return sprintf(buffer, "%s\n",
411 map_get_key(map_ac_online, battery_present, "unknown"));
414 static int param_set_battery_technology(const char *key,
415 const struct kernel_param *kp)
417 battery_technology = map_get_value(map_technology, key,
419 signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
423 static int param_get_battery_technology(char *buffer,
424 const struct kernel_param *kp)
426 return sprintf(buffer, "%s\n",
427 map_get_key(map_ac_online, battery_technology,
431 static int param_set_battery_capacity(const char *key,
432 const struct kernel_param *kp)
436 if (1 != sscanf(key, "%d", &tmp))
439 battery_capacity = tmp;
440 signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
444 #define param_get_battery_capacity param_get_int
446 static int param_set_battery_voltage(const char *key,
447 const struct kernel_param *kp)
451 if (1 != sscanf(key, "%d", &tmp))
454 battery_voltage = tmp;
455 signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
459 #define param_get_battery_voltage param_get_int
461 static int param_set_battery_charge_counter(const char *key,
462 const struct kernel_param *kp)
466 if (1 != sscanf(key, "%d", &tmp))
469 battery_charge_counter = tmp;
470 signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
474 #define param_get_battery_charge_counter param_get_int
476 static int param_set_battery_current(const char *key,
477 const struct kernel_param *kp)
481 if (1 != sscanf(key, "%d", &tmp))
484 battery_current = tmp;
485 signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
489 #define param_get_battery_current param_get_int
491 static const struct kernel_param_ops param_ops_ac_online = {
492 .set = param_set_ac_online,
493 .get = param_get_ac_online,
496 static const struct kernel_param_ops param_ops_usb_online = {
497 .set = param_set_usb_online,
498 .get = param_get_usb_online,
501 static const struct kernel_param_ops param_ops_battery_status = {
502 .set = param_set_battery_status,
503 .get = param_get_battery_status,
506 static const struct kernel_param_ops param_ops_battery_present = {
507 .set = param_set_battery_present,
508 .get = param_get_battery_present,
511 static const struct kernel_param_ops param_ops_battery_technology = {
512 .set = param_set_battery_technology,
513 .get = param_get_battery_technology,
516 static const struct kernel_param_ops param_ops_battery_health = {
517 .set = param_set_battery_health,
518 .get = param_get_battery_health,
521 static const struct kernel_param_ops param_ops_battery_capacity = {
522 .set = param_set_battery_capacity,
523 .get = param_get_battery_capacity,
526 static const struct kernel_param_ops param_ops_battery_voltage = {
527 .set = param_set_battery_voltage,
528 .get = param_get_battery_voltage,
531 static const struct kernel_param_ops param_ops_battery_charge_counter = {
532 .set = param_set_battery_charge_counter,
533 .get = param_get_battery_charge_counter,
536 static const struct kernel_param_ops param_ops_battery_current = {
537 .set = param_set_battery_current,
538 .get = param_get_battery_current,
541 #define param_check_ac_online(name, p) __param_check(name, p, void);
542 #define param_check_usb_online(name, p) __param_check(name, p, void);
543 #define param_check_battery_status(name, p) __param_check(name, p, void);
544 #define param_check_battery_present(name, p) __param_check(name, p, void);
545 #define param_check_battery_technology(name, p) __param_check(name, p, void);
546 #define param_check_battery_health(name, p) __param_check(name, p, void);
547 #define param_check_battery_capacity(name, p) __param_check(name, p, void);
548 #define param_check_battery_voltage(name, p) __param_check(name, p, void);
549 #define param_check_battery_charge_counter(name, p) __param_check(name, p, void);
550 #define param_check_battery_current(name, p) __param_check(name, p, void);
553 module_param(ac_online, ac_online, 0644);
554 MODULE_PARM_DESC(ac_online, "AC charging state <on|off>");
556 module_param(usb_online, usb_online, 0644);
557 MODULE_PARM_DESC(usb_online, "USB charging state <on|off>");
559 module_param(battery_status, battery_status, 0644);
560 MODULE_PARM_DESC(battery_status,
561 "battery status <charging|discharging|not-charging|full>");
563 module_param(battery_present, battery_present, 0644);
564 MODULE_PARM_DESC(battery_present,
565 "battery presence state <good|overheat|dead|overvoltage|failure>");
567 module_param(battery_technology, battery_technology, 0644);
568 MODULE_PARM_DESC(battery_technology,
569 "battery technology <NiMH|LION|LIPO|LiFe|NiCd|LiMn>");
571 module_param(battery_health, battery_health, 0644);
572 MODULE_PARM_DESC(battery_health,
573 "battery health state <good|overheat|dead|overvoltage|failure>");
575 module_param(battery_capacity, battery_capacity, 0644);
576 MODULE_PARM_DESC(battery_capacity, "battery capacity (percentage)");
578 module_param(battery_voltage, battery_voltage, 0644);
579 MODULE_PARM_DESC(battery_voltage, "battery voltage (millivolts)");
581 module_param(battery_charge_counter, battery_charge_counter, 0644);
582 MODULE_PARM_DESC(battery_charge_counter,
583 "battery charge counter (microampere-hours)");
585 module_param(battery_current, battery_current, 0644);
586 MODULE_PARM_DESC(battery_current, "battery current (milliampere)");
588 MODULE_DESCRIPTION("Power supply driver for testing");
589 MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>");
590 MODULE_LICENSE("GPL");