2 * Power supply driver for testing.
4 * Copyright 2010 Anton Vorontsov <cbouatmailru@gmail.com>
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
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.
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>
24 static int ac_online = 1;
25 static int usb_online = 1;
26 static int battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
27 static int battery_health = POWER_SUPPLY_HEALTH_GOOD;
28 static int battery_present = 1; /* true */
29 static int battery_technology = POWER_SUPPLY_TECHNOLOGY_LION;
30 static int battery_capacity = 50;
31 static int battery_voltage = 3300;
32 static int battery_voltage_ocv = 4200000; /* uV */
33 static int battery_temp = 26;
35 static bool module_initialized;
37 static int test_power_get_ac_property(struct power_supply *psy,
38 enum power_supply_property psp,
39 union power_supply_propval *val)
42 case POWER_SUPPLY_PROP_ONLINE:
43 val->intval = ac_online;
51 static int test_power_get_usb_property(struct power_supply *psy,
52 enum power_supply_property psp,
53 union power_supply_propval *val)
56 case POWER_SUPPLY_PROP_ONLINE:
57 val->intval = usb_online;
65 static int test_power_get_battery_property(struct power_supply *psy,
66 enum power_supply_property psp,
67 union power_supply_propval *val)
70 case POWER_SUPPLY_PROP_MODEL_NAME:
71 val->strval = "Test battery";
73 case POWER_SUPPLY_PROP_MANUFACTURER:
74 val->strval = "Linux";
76 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
77 val->strval = UTS_RELEASE;
79 case POWER_SUPPLY_PROP_STATUS:
80 val->intval = battery_status;
82 case POWER_SUPPLY_PROP_CHARGE_TYPE:
83 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
85 case POWER_SUPPLY_PROP_HEALTH:
86 val->intval = battery_health;
88 case POWER_SUPPLY_PROP_PRESENT:
89 val->intval = battery_present;
91 case POWER_SUPPLY_PROP_TECHNOLOGY:
92 val->intval = battery_technology;
94 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
95 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
97 case POWER_SUPPLY_PROP_CAPACITY:
98 case POWER_SUPPLY_PROP_CHARGE_NOW:
99 val->intval = battery_capacity;
101 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
102 case POWER_SUPPLY_PROP_CHARGE_FULL:
105 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
106 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
109 case POWER_SUPPLY_PROP_TEMP:
110 val->intval = battery_temp;
112 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
113 val->intval = battery_voltage;
115 case POWER_SUPPLY_PROP_VOLTAGE_OCV:
116 val->intval = battery_voltage_ocv;
119 pr_info("%s: some properties deliberately report errors.\n",
126 static enum power_supply_property test_power_ac_props[] = {
127 POWER_SUPPLY_PROP_ONLINE,
130 static enum power_supply_property test_power_battery_props[] = {
131 POWER_SUPPLY_PROP_STATUS,
132 POWER_SUPPLY_PROP_CHARGE_TYPE,
133 POWER_SUPPLY_PROP_HEALTH,
134 POWER_SUPPLY_PROP_PRESENT,
135 POWER_SUPPLY_PROP_TECHNOLOGY,
136 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
137 POWER_SUPPLY_PROP_CHARGE_FULL,
138 POWER_SUPPLY_PROP_CHARGE_NOW,
139 POWER_SUPPLY_PROP_CAPACITY,
140 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
141 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
142 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
143 POWER_SUPPLY_PROP_MODEL_NAME,
144 POWER_SUPPLY_PROP_MANUFACTURER,
145 POWER_SUPPLY_PROP_SERIAL_NUMBER,
146 POWER_SUPPLY_PROP_TEMP,
147 POWER_SUPPLY_PROP_VOLTAGE_NOW,
148 POWER_SUPPLY_PROP_VOLTAGE_OCV,
151 static char *test_power_ac_supplied_to[] = {
155 static struct power_supply test_power_supplies[] = {
158 .type = POWER_SUPPLY_TYPE_MAINS,
159 .supplied_to = test_power_ac_supplied_to,
160 .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to),
161 .properties = test_power_ac_props,
162 .num_properties = ARRAY_SIZE(test_power_ac_props),
163 .get_property = test_power_get_ac_property,
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,
172 .type = POWER_SUPPLY_TYPE_USB,
173 .supplied_to = test_power_ac_supplied_to,
174 .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to),
175 .properties = test_power_ac_props,
176 .num_properties = ARRAY_SIZE(test_power_ac_props),
177 .get_property = test_power_get_usb_property,
182 static int __init test_power_init(void)
187 for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++) {
188 ret = power_supply_register(NULL, &test_power_supplies[i]);
190 pr_err("%s: failed to register %s\n", __func__,
191 test_power_supplies[i].name);
196 module_initialized = true;
200 power_supply_unregister(&test_power_supplies[i]);
203 module_init(test_power_init);
205 static void __exit test_power_exit(void)
209 /* Let's see how we handle changes... */
212 battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
213 for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++)
214 power_supply_changed(&test_power_supplies[i]);
215 pr_info("%s: 'changed' event sent, sleeping for 10 seconds...\n",
219 for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++)
220 power_supply_unregister(&test_power_supplies[i]);
222 module_initialized = false;
224 module_exit(test_power_exit);
228 #define MAX_KEYLENGTH 256
229 struct battery_property_map {
234 static struct battery_property_map map_ac_online[] = {
240 static struct battery_property_map map_status[] = {
241 { POWER_SUPPLY_STATUS_CHARGING, "charging" },
242 { POWER_SUPPLY_STATUS_DISCHARGING, "discharging" },
243 { POWER_SUPPLY_STATUS_NOT_CHARGING, "not-charging" },
244 { POWER_SUPPLY_STATUS_FULL, "full" },
248 static struct battery_property_map map_health[] = {
249 { POWER_SUPPLY_HEALTH_GOOD, "good" },
250 { POWER_SUPPLY_HEALTH_OVERHEAT, "overheat" },
251 { POWER_SUPPLY_HEALTH_DEAD, "dead" },
252 { POWER_SUPPLY_HEALTH_OVERVOLTAGE, "overvoltage" },
253 { POWER_SUPPLY_HEALTH_UNSPEC_FAILURE, "failure" },
257 static struct battery_property_map map_present[] = {
263 static struct battery_property_map map_technology[] = {
264 { POWER_SUPPLY_TECHNOLOGY_NiMH, "NiMH" },
265 { POWER_SUPPLY_TECHNOLOGY_LION, "LION" },
266 { POWER_SUPPLY_TECHNOLOGY_LIPO, "LIPO" },
267 { POWER_SUPPLY_TECHNOLOGY_LiFe, "LiFe" },
268 { POWER_SUPPLY_TECHNOLOGY_NiCd, "NiCd" },
269 { POWER_SUPPLY_TECHNOLOGY_LiMn, "LiMn" },
274 static int map_get_value(struct battery_property_map *map, const char *key,
277 char buf[MAX_KEYLENGTH];
280 strncpy(buf, key, MAX_KEYLENGTH);
281 buf[MAX_KEYLENGTH-1] = '\0';
283 cr = strnlen(buf, MAX_KEYLENGTH) - 1;
288 if (strncasecmp(map->key, buf, MAX_KEYLENGTH) == 0)
297 static const char *map_get_key(struct battery_property_map *map, int value,
301 if (map->value == value)
309 static inline void signal_power_supply_changed(struct power_supply *psy)
311 if (module_initialized)
312 power_supply_changed(psy);
315 static int param_set_ac_online(const char *key, const struct kernel_param *kp)
317 ac_online = map_get_value(map_ac_online, key, ac_online);
318 signal_power_supply_changed(&test_power_supplies[0]);
322 static int param_get_ac_online(char *buffer, const struct kernel_param *kp)
324 strcpy(buffer, map_get_key(map_ac_online, ac_online, "unknown"));
325 return strlen(buffer);
328 static int param_set_usb_online(const char *key, const struct kernel_param *kp)
330 usb_online = map_get_value(map_ac_online, key, usb_online);
331 signal_power_supply_changed(&test_power_supplies[2]);
335 static int param_get_usb_online(char *buffer, const struct kernel_param *kp)
337 strcpy(buffer, map_get_key(map_ac_online, usb_online, "unknown"));
338 return strlen(buffer);
341 static int param_set_battery_status(const char *key,
342 const struct kernel_param *kp)
344 battery_status = map_get_value(map_status, key, battery_status);
345 signal_power_supply_changed(&test_power_supplies[1]);
349 static int param_get_battery_status(char *buffer, const struct kernel_param *kp)
351 strcpy(buffer, map_get_key(map_status, battery_status, "unknown"));
352 return strlen(buffer);
355 static int param_set_battery_health(const char *key,
356 const struct kernel_param *kp)
358 battery_health = map_get_value(map_health, key, battery_health);
359 signal_power_supply_changed(&test_power_supplies[1]);
363 static int param_get_battery_health(char *buffer, const struct kernel_param *kp)
365 strcpy(buffer, map_get_key(map_health, battery_health, "unknown"));
366 return strlen(buffer);
369 static int param_set_battery_present(const char *key,
370 const struct kernel_param *kp)
372 battery_present = map_get_value(map_present, key, battery_present);
373 signal_power_supply_changed(&test_power_supplies[0]);
377 static int param_get_battery_present(char *buffer,
378 const struct kernel_param *kp)
380 strcpy(buffer, map_get_key(map_present, battery_present, "unknown"));
381 return strlen(buffer);
384 static int param_set_battery_technology(const char *key,
385 const struct kernel_param *kp)
387 battery_technology = map_get_value(map_technology, key,
389 signal_power_supply_changed(&test_power_supplies[1]);
393 static int param_get_battery_technology(char *buffer,
394 const struct kernel_param *kp)
397 map_get_key(map_technology, battery_technology, "unknown"));
398 return strlen(buffer);
401 static int param_set_battery_capacity(const char *key,
402 const struct kernel_param *kp)
406 if (1 != sscanf(key, "%d", &tmp))
409 battery_capacity = tmp;
410 signal_power_supply_changed(&test_power_supplies[1]);
414 #define param_get_battery_capacity param_get_int
416 static int param_set_battery_voltage(const char *key,
417 const struct kernel_param *kp)
421 if (1 != sscanf(key, "%d", &tmp))
424 battery_voltage = tmp;
425 signal_power_supply_changed(&test_power_supplies[1]);
429 #define param_get_battery_voltage param_get_int
431 static int param_set_battery_voltage_ocv(const char *key,
432 const struct kernel_param *kp)
436 if (1 != sscanf(key, "%d", &tmp))
439 battery_voltage_ocv = tmp;
440 power_supply_changed(&test_power_supplies[1]);
444 #define param_get_battery_voltage_ocv param_get_int
446 static int param_set_battery_temp(const char *key,
447 const struct kernel_param *kp)
451 if (1 != sscanf(key, "%d", &tmp))
455 power_supply_changed(&test_power_supplies[1]);
459 #define param_get_battery_temp param_get_int
462 static struct kernel_param_ops param_ops_ac_online = {
463 .set = param_set_ac_online,
464 .get = param_get_ac_online,
467 static struct kernel_param_ops param_ops_usb_online = {
468 .set = param_set_usb_online,
469 .get = param_get_usb_online,
472 static struct kernel_param_ops param_ops_battery_status = {
473 .set = param_set_battery_status,
474 .get = param_get_battery_status,
477 static struct kernel_param_ops param_ops_battery_present = {
478 .set = param_set_battery_present,
479 .get = param_get_battery_present,
482 static struct kernel_param_ops param_ops_battery_technology = {
483 .set = param_set_battery_technology,
484 .get = param_get_battery_technology,
487 static struct kernel_param_ops param_ops_battery_health = {
488 .set = param_set_battery_health,
489 .get = param_get_battery_health,
492 static struct kernel_param_ops param_ops_battery_capacity = {
493 .set = param_set_battery_capacity,
494 .get = param_get_battery_capacity,
497 static struct kernel_param_ops param_ops_battery_voltage = {
498 .set = param_set_battery_voltage,
499 .get = param_get_battery_voltage,
502 static struct kernel_param_ops param_ops_battery_voltage_ocv = {
503 .set = param_set_battery_voltage_ocv,
504 .get = param_get_battery_voltage_ocv,
507 static struct kernel_param_ops param_ops_battery_temp = {
508 .set = param_set_battery_temp,
509 .get = param_get_battery_temp,
512 #define param_check_ac_online(name, p) __param_check(name, p, void);
513 #define param_check_usb_online(name, p) __param_check(name, p, void);
514 #define param_check_battery_status(name, p) __param_check(name, p, void);
515 #define param_check_battery_present(name, p) __param_check(name, p, void);
516 #define param_check_battery_technology(name, p) __param_check(name, p, void);
517 #define param_check_battery_health(name, p) __param_check(name, p, void);
518 #define param_check_battery_capacity(name, p) __param_check(name, p, void);
519 #define param_check_battery_voltage(name, p) __param_check(name, p, void);
520 #define param_check_battery_voltage_ocv(name, p) __param_check(name, p, void);
521 #define param_check_battery_temp(name, p) __param_check(name, p, void);
524 module_param(ac_online, ac_online, 0644);
525 MODULE_PARM_DESC(ac_online, "AC charging state <on|off>");
527 module_param(usb_online, usb_online, 0644);
528 MODULE_PARM_DESC(usb_online, "USB charging state <on|off>");
530 module_param(battery_status, battery_status, 0644);
531 MODULE_PARM_DESC(battery_status,
532 "battery status <charging|discharging|not-charging|full>");
534 module_param(battery_present, battery_present, 0644);
535 MODULE_PARM_DESC(battery_present,
536 "battery presence state <good|overheat|dead|overvoltage|failure>");
538 module_param(battery_technology, battery_technology, 0644);
539 MODULE_PARM_DESC(battery_technology,
540 "battery technology <NiMH|LION|LIPO|LiFe|NiCd|LiMn>");
542 module_param(battery_health, battery_health, 0644);
543 MODULE_PARM_DESC(battery_health,
544 "battery health state <good|overheat|dead|overvoltage|failure>");
546 module_param(battery_capacity, battery_capacity, 0644);
547 MODULE_PARM_DESC(battery_capacity, "battery capacity (percentage)");
549 module_param(battery_voltage, battery_voltage, 0644);
550 MODULE_PARM_DESC(battery_voltage, "battery voltage (millivolts)");
552 module_param(battery_voltage_ocv, battery_voltage_ocv, 0644);
553 MODULE_PARM_DESC(battery_voltage_ocv, "battery open circuit voltage (microvolts)");
555 module_param(battery_temp, battery_temp, 0644);
556 MODULE_PARM_DESC(battery_temp, "battery temperature");
558 MODULE_DESCRIPTION("Power supply driver for testing");
559 MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>");
560 MODULE_LICENSE("GPL");