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;
32 static int test_power_get_ac_property(struct power_supply *psy,
33 enum power_supply_property psp,
34 union power_supply_propval *val)
37 case POWER_SUPPLY_PROP_ONLINE:
38 val->intval = ac_online;
46 static int test_power_get_usb_property(struct power_supply *psy,
47 enum power_supply_property psp,
48 union power_supply_propval *val)
51 case POWER_SUPPLY_PROP_ONLINE:
52 val->intval = usb_online;
60 static int test_power_get_battery_property(struct power_supply *psy,
61 enum power_supply_property psp,
62 union power_supply_propval *val)
65 case POWER_SUPPLY_PROP_MODEL_NAME:
66 val->strval = "Test battery";
68 case POWER_SUPPLY_PROP_MANUFACTURER:
69 val->strval = "Linux";
71 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
72 val->strval = UTS_RELEASE;
74 case POWER_SUPPLY_PROP_STATUS:
75 val->intval = battery_status;
77 case POWER_SUPPLY_PROP_CHARGE_TYPE:
78 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
80 case POWER_SUPPLY_PROP_HEALTH:
81 val->intval = battery_health;
83 case POWER_SUPPLY_PROP_PRESENT:
84 val->intval = battery_present;
86 case POWER_SUPPLY_PROP_TECHNOLOGY:
87 val->intval = battery_technology;
89 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
90 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
92 case POWER_SUPPLY_PROP_CAPACITY:
93 case POWER_SUPPLY_PROP_CHARGE_NOW:
94 val->intval = battery_capacity;
96 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
97 case POWER_SUPPLY_PROP_CHARGE_FULL:
100 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
101 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
105 pr_info("%s: some properties deliberately report errors.\n",
112 static enum power_supply_property test_power_ac_props[] = {
113 POWER_SUPPLY_PROP_ONLINE,
116 static enum power_supply_property test_power_battery_props[] = {
117 POWER_SUPPLY_PROP_STATUS,
118 POWER_SUPPLY_PROP_CHARGE_TYPE,
119 POWER_SUPPLY_PROP_HEALTH,
120 POWER_SUPPLY_PROP_PRESENT,
121 POWER_SUPPLY_PROP_TECHNOLOGY,
122 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
123 POWER_SUPPLY_PROP_CHARGE_FULL,
124 POWER_SUPPLY_PROP_CHARGE_NOW,
125 POWER_SUPPLY_PROP_CAPACITY,
126 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
127 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
128 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
129 POWER_SUPPLY_PROP_MODEL_NAME,
130 POWER_SUPPLY_PROP_MANUFACTURER,
131 POWER_SUPPLY_PROP_SERIAL_NUMBER,
134 static char *test_power_ac_supplied_to[] = {
138 static struct power_supply test_power_supplies[] = {
141 .type = POWER_SUPPLY_TYPE_MAINS,
142 .supplied_to = test_power_ac_supplied_to,
143 .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to),
144 .properties = test_power_ac_props,
145 .num_properties = ARRAY_SIZE(test_power_ac_props),
146 .get_property = test_power_get_ac_property,
148 .name = "test_battery",
149 .type = POWER_SUPPLY_TYPE_BATTERY,
150 .properties = test_power_battery_props,
151 .num_properties = ARRAY_SIZE(test_power_battery_props),
152 .get_property = test_power_get_battery_property,
155 .type = POWER_SUPPLY_TYPE_USB,
156 .supplied_to = test_power_ac_supplied_to,
157 .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to),
158 .properties = test_power_ac_props,
159 .num_properties = ARRAY_SIZE(test_power_ac_props),
160 .get_property = test_power_get_usb_property,
165 static int __init test_power_init(void)
170 for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++) {
171 ret = power_supply_register(NULL, &test_power_supplies[i]);
173 pr_err("%s: failed to register %s\n", __func__,
174 test_power_supplies[i].name);
182 power_supply_unregister(&test_power_supplies[i]);
185 module_init(test_power_init);
187 static void __exit test_power_exit(void)
191 /* Let's see how we handle changes... */
194 battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
195 for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++)
196 power_supply_changed(&test_power_supplies[i]);
197 pr_info("%s: 'changed' event sent, sleeping for 10 seconds...\n",
201 for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++)
202 power_supply_unregister(&test_power_supplies[i]);
204 module_exit(test_power_exit);
208 #define MAX_KEYLENGTH 256
209 struct battery_property_map {
214 static struct battery_property_map map_ac_online[] = {
220 static struct battery_property_map map_status[] = {
221 { POWER_SUPPLY_STATUS_CHARGING, "charging" },
222 { POWER_SUPPLY_STATUS_DISCHARGING, "discharging" },
223 { POWER_SUPPLY_STATUS_NOT_CHARGING, "not-charging" },
224 { POWER_SUPPLY_STATUS_FULL, "full" },
228 static struct battery_property_map map_health[] = {
229 { POWER_SUPPLY_HEALTH_GOOD, "good" },
230 { POWER_SUPPLY_HEALTH_OVERHEAT, "overheat" },
231 { POWER_SUPPLY_HEALTH_DEAD, "dead" },
232 { POWER_SUPPLY_HEALTH_OVERVOLTAGE, "overvoltage" },
233 { POWER_SUPPLY_HEALTH_UNSPEC_FAILURE, "failure" },
237 static struct battery_property_map map_present[] = {
243 static struct battery_property_map map_technology[] = {
244 { POWER_SUPPLY_TECHNOLOGY_NiMH, "NiMH" },
245 { POWER_SUPPLY_TECHNOLOGY_LION, "LION" },
246 { POWER_SUPPLY_TECHNOLOGY_LIPO, "LIPO" },
247 { POWER_SUPPLY_TECHNOLOGY_LiFe, "LiFe" },
248 { POWER_SUPPLY_TECHNOLOGY_NiCd, "NiCd" },
249 { POWER_SUPPLY_TECHNOLOGY_LiMn, "LiMn" },
254 static int map_get_value(struct battery_property_map *map, const char *key,
257 char buf[MAX_KEYLENGTH];
260 strncpy(buf, key, MAX_KEYLENGTH);
261 buf[MAX_KEYLENGTH-1] = '\0';
263 cr = strnlen(buf, MAX_KEYLENGTH) - 1;
268 if (strncasecmp(map->key, buf, MAX_KEYLENGTH) == 0)
277 static const char *map_get_key(struct battery_property_map *map, int value,
281 if (map->value == value)
289 static int param_set_ac_online(const char *key, const struct kernel_param *kp)
291 ac_online = map_get_value(map_ac_online, key, ac_online);
292 power_supply_changed(&test_power_supplies[0]);
296 static int param_get_ac_online(char *buffer, const struct kernel_param *kp)
298 strcpy(buffer, map_get_key(map_ac_online, ac_online, "unknown"));
299 return strlen(buffer);
302 static int param_set_usb_online(const char *key, const struct kernel_param *kp)
304 usb_online = map_get_value(map_ac_online, key, usb_online);
305 power_supply_changed(&test_power_supplies[2]);
309 static int param_get_usb_online(char *buffer, const struct kernel_param *kp)
311 strcpy(buffer, map_get_key(map_ac_online, usb_online, "unknown"));
312 return strlen(buffer);
315 static int param_set_battery_status(const char *key,
316 const struct kernel_param *kp)
318 battery_status = map_get_value(map_status, key, battery_status);
319 power_supply_changed(&test_power_supplies[1]);
323 static int param_get_battery_status(char *buffer, const struct kernel_param *kp)
325 strcpy(buffer, map_get_key(map_status, battery_status, "unknown"));
326 return strlen(buffer);
329 static int param_set_battery_health(const char *key,
330 const struct kernel_param *kp)
332 battery_health = map_get_value(map_health, key, battery_health);
333 power_supply_changed(&test_power_supplies[1]);
337 static int param_get_battery_health(char *buffer, const struct kernel_param *kp)
339 strcpy(buffer, map_get_key(map_health, battery_health, "unknown"));
340 return strlen(buffer);
343 static int param_set_battery_present(const char *key,
344 const struct kernel_param *kp)
346 battery_present = map_get_value(map_present, key, battery_present);
347 power_supply_changed(&test_power_supplies[0]);
351 static int param_get_battery_present(char *buffer,
352 const struct kernel_param *kp)
354 strcpy(buffer, map_get_key(map_present, battery_present, "unknown"));
355 return strlen(buffer);
358 static int param_set_battery_technology(const char *key,
359 const struct kernel_param *kp)
361 battery_technology = map_get_value(map_technology, key,
363 power_supply_changed(&test_power_supplies[1]);
367 static int param_get_battery_technology(char *buffer,
368 const struct kernel_param *kp)
371 map_get_key(map_technology, battery_technology, "unknown"));
372 return strlen(buffer);
375 static int param_set_battery_capacity(const char *key,
376 const struct kernel_param *kp)
380 if (1 != sscanf(key, "%d", &tmp))
383 battery_capacity = tmp;
384 power_supply_changed(&test_power_supplies[1]);
388 #define param_get_battery_capacity param_get_int
390 static struct kernel_param_ops param_ops_ac_online = {
391 .set = param_set_ac_online,
392 .get = param_get_ac_online,
395 static struct kernel_param_ops param_ops_usb_online = {
396 .set = param_set_usb_online,
397 .get = param_get_usb_online,
400 static struct kernel_param_ops param_ops_battery_status = {
401 .set = param_set_battery_status,
402 .get = param_get_battery_status,
405 static struct kernel_param_ops param_ops_battery_present = {
406 .set = param_set_battery_present,
407 .get = param_get_battery_present,
410 static struct kernel_param_ops param_ops_battery_technology = {
411 .set = param_set_battery_technology,
412 .get = param_get_battery_technology,
415 static struct kernel_param_ops param_ops_battery_health = {
416 .set = param_set_battery_health,
417 .get = param_get_battery_health,
420 static struct kernel_param_ops param_ops_battery_capacity = {
421 .set = param_set_battery_capacity,
422 .get = param_get_battery_capacity,
426 #define param_check_ac_online(name, p) __param_check(name, p, void);
427 #define param_check_usb_online(name, p) __param_check(name, p, void);
428 #define param_check_battery_status(name, p) __param_check(name, p, void);
429 #define param_check_battery_present(name, p) __param_check(name, p, void);
430 #define param_check_battery_technology(name, p) __param_check(name, p, void);
431 #define param_check_battery_health(name, p) __param_check(name, p, void);
432 #define param_check_battery_capacity(name, p) __param_check(name, p, void);
435 module_param(ac_online, ac_online, 0644);
436 MODULE_PARM_DESC(ac_online, "AC charging state <on|off>");
438 module_param(usb_online, usb_online, 0644);
439 MODULE_PARM_DESC(usb_online, "USB charging state <on|off>");
441 module_param(battery_status, battery_status, 0644);
442 MODULE_PARM_DESC(battery_status,
443 "battery status <charging|discharging|not-charging|full>");
445 module_param(battery_present, battery_present, 0644);
446 MODULE_PARM_DESC(battery_present,
447 "battery presence state <good|overheat|dead|overvoltage|failure>");
449 module_param(battery_technology, battery_technology, 0644);
450 MODULE_PARM_DESC(battery_technology,
451 "battery technology <NiMH|LION|LIPO|LiFe|NiCd|LiMn>");
453 module_param(battery_health, battery_health, 0644);
454 MODULE_PARM_DESC(battery_health,
455 "battery health state <good|overheat|dead|overvoltage|failure>");
457 module_param(battery_capacity, battery_capacity, 0644);
458 MODULE_PARM_DESC(battery_capacity, "battery capacity (percentage)");
461 MODULE_DESCRIPTION("Power supply driver for testing");
462 MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>");
463 MODULE_LICENSE("GPL");