0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <linux/kernel.h>
0015 #include <linux/module.h>
0016 #include <linux/power_supply.h>
0017 #include <linux/errno.h>
0018 #include <linux/delay.h>
0019 #include <generated/utsrelease.h>
0020
0021 enum test_power_id {
0022 TEST_AC,
0023 TEST_BATTERY,
0024 TEST_USB,
0025 TEST_POWER_NUM,
0026 };
0027
0028 static int ac_online = 1;
0029 static int usb_online = 1;
0030 static int battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
0031 static int battery_health = POWER_SUPPLY_HEALTH_GOOD;
0032 static int battery_present = 1;
0033 static int battery_technology = POWER_SUPPLY_TECHNOLOGY_LION;
0034 static int battery_capacity = 50;
0035 static int battery_voltage = 3300;
0036 static int battery_charge_counter = -1000;
0037 static int battery_current = -1600;
0038
0039 static bool module_initialized;
0040
0041 static int test_power_get_ac_property(struct power_supply *psy,
0042 enum power_supply_property psp,
0043 union power_supply_propval *val)
0044 {
0045 switch (psp) {
0046 case POWER_SUPPLY_PROP_ONLINE:
0047 val->intval = ac_online;
0048 break;
0049 default:
0050 return -EINVAL;
0051 }
0052 return 0;
0053 }
0054
0055 static int test_power_get_usb_property(struct power_supply *psy,
0056 enum power_supply_property psp,
0057 union power_supply_propval *val)
0058 {
0059 switch (psp) {
0060 case POWER_SUPPLY_PROP_ONLINE:
0061 val->intval = usb_online;
0062 break;
0063 default:
0064 return -EINVAL;
0065 }
0066 return 0;
0067 }
0068
0069 static int test_power_get_battery_property(struct power_supply *psy,
0070 enum power_supply_property psp,
0071 union power_supply_propval *val)
0072 {
0073 switch (psp) {
0074 case POWER_SUPPLY_PROP_MODEL_NAME:
0075 val->strval = "Test battery";
0076 break;
0077 case POWER_SUPPLY_PROP_MANUFACTURER:
0078 val->strval = "Linux";
0079 break;
0080 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
0081 val->strval = UTS_RELEASE;
0082 break;
0083 case POWER_SUPPLY_PROP_STATUS:
0084 val->intval = battery_status;
0085 break;
0086 case POWER_SUPPLY_PROP_CHARGE_TYPE:
0087 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
0088 break;
0089 case POWER_SUPPLY_PROP_HEALTH:
0090 val->intval = battery_health;
0091 break;
0092 case POWER_SUPPLY_PROP_PRESENT:
0093 val->intval = battery_present;
0094 break;
0095 case POWER_SUPPLY_PROP_TECHNOLOGY:
0096 val->intval = battery_technology;
0097 break;
0098 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
0099 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
0100 break;
0101 case POWER_SUPPLY_PROP_CAPACITY:
0102 case POWER_SUPPLY_PROP_CHARGE_NOW:
0103 val->intval = battery_capacity;
0104 break;
0105 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
0106 val->intval = battery_charge_counter;
0107 break;
0108 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
0109 case POWER_SUPPLY_PROP_CHARGE_FULL:
0110 val->intval = 100;
0111 break;
0112 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
0113 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
0114 val->intval = 3600;
0115 break;
0116 case POWER_SUPPLY_PROP_TEMP:
0117 val->intval = 26;
0118 break;
0119 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
0120 val->intval = battery_voltage;
0121 break;
0122 case POWER_SUPPLY_PROP_CURRENT_AVG:
0123 case POWER_SUPPLY_PROP_CURRENT_NOW:
0124 val->intval = battery_current;
0125 break;
0126 default:
0127 pr_info("%s: some properties deliberately report errors.\n",
0128 __func__);
0129 return -EINVAL;
0130 }
0131 return 0;
0132 }
0133
0134 static enum power_supply_property test_power_ac_props[] = {
0135 POWER_SUPPLY_PROP_ONLINE,
0136 };
0137
0138 static enum power_supply_property test_power_battery_props[] = {
0139 POWER_SUPPLY_PROP_STATUS,
0140 POWER_SUPPLY_PROP_CHARGE_TYPE,
0141 POWER_SUPPLY_PROP_HEALTH,
0142 POWER_SUPPLY_PROP_PRESENT,
0143 POWER_SUPPLY_PROP_TECHNOLOGY,
0144 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
0145 POWER_SUPPLY_PROP_CHARGE_FULL,
0146 POWER_SUPPLY_PROP_CHARGE_NOW,
0147 POWER_SUPPLY_PROP_CHARGE_COUNTER,
0148 POWER_SUPPLY_PROP_CAPACITY,
0149 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
0150 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
0151 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
0152 POWER_SUPPLY_PROP_MODEL_NAME,
0153 POWER_SUPPLY_PROP_MANUFACTURER,
0154 POWER_SUPPLY_PROP_SERIAL_NUMBER,
0155 POWER_SUPPLY_PROP_TEMP,
0156 POWER_SUPPLY_PROP_VOLTAGE_NOW,
0157 POWER_SUPPLY_PROP_CURRENT_AVG,
0158 POWER_SUPPLY_PROP_CURRENT_NOW,
0159 };
0160
0161 static char *test_power_ac_supplied_to[] = {
0162 "test_battery",
0163 };
0164
0165 static struct power_supply *test_power_supplies[TEST_POWER_NUM];
0166
0167 static const struct power_supply_desc test_power_desc[] = {
0168 [TEST_AC] = {
0169 .name = "test_ac",
0170 .type = POWER_SUPPLY_TYPE_MAINS,
0171 .properties = test_power_ac_props,
0172 .num_properties = ARRAY_SIZE(test_power_ac_props),
0173 .get_property = test_power_get_ac_property,
0174 },
0175 [TEST_BATTERY] = {
0176 .name = "test_battery",
0177 .type = POWER_SUPPLY_TYPE_BATTERY,
0178 .properties = test_power_battery_props,
0179 .num_properties = ARRAY_SIZE(test_power_battery_props),
0180 .get_property = test_power_get_battery_property,
0181 },
0182 [TEST_USB] = {
0183 .name = "test_usb",
0184 .type = POWER_SUPPLY_TYPE_USB,
0185 .properties = test_power_ac_props,
0186 .num_properties = ARRAY_SIZE(test_power_ac_props),
0187 .get_property = test_power_get_usb_property,
0188 },
0189 };
0190
0191 static const struct power_supply_config test_power_configs[] = {
0192 {
0193
0194 .supplied_to = test_power_ac_supplied_to,
0195 .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to),
0196 }, {
0197
0198 }, {
0199
0200 .supplied_to = test_power_ac_supplied_to,
0201 .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to),
0202 },
0203 };
0204
0205 static int __init test_power_init(void)
0206 {
0207 int i;
0208 int ret;
0209
0210 BUILD_BUG_ON(TEST_POWER_NUM != ARRAY_SIZE(test_power_supplies));
0211 BUILD_BUG_ON(TEST_POWER_NUM != ARRAY_SIZE(test_power_configs));
0212
0213 for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++) {
0214 test_power_supplies[i] = power_supply_register(NULL,
0215 &test_power_desc[i],
0216 &test_power_configs[i]);
0217 if (IS_ERR(test_power_supplies[i])) {
0218 pr_err("%s: failed to register %s\n", __func__,
0219 test_power_desc[i].name);
0220 ret = PTR_ERR(test_power_supplies[i]);
0221 goto failed;
0222 }
0223 }
0224
0225 module_initialized = true;
0226 return 0;
0227 failed:
0228 while (--i >= 0)
0229 power_supply_unregister(test_power_supplies[i]);
0230 return ret;
0231 }
0232 module_init(test_power_init);
0233
0234 static void __exit test_power_exit(void)
0235 {
0236 int i;
0237
0238
0239 ac_online = 0;
0240 usb_online = 0;
0241 battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
0242 for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++)
0243 power_supply_changed(test_power_supplies[i]);
0244 pr_info("%s: 'changed' event sent, sleeping for 10 seconds...\n",
0245 __func__);
0246 ssleep(10);
0247
0248 for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++)
0249 power_supply_unregister(test_power_supplies[i]);
0250
0251 module_initialized = false;
0252 }
0253 module_exit(test_power_exit);
0254
0255
0256
0257 #define MAX_KEYLENGTH 256
0258 struct battery_property_map {
0259 int value;
0260 char const *key;
0261 };
0262
0263 static struct battery_property_map map_ac_online[] = {
0264 { 0, "off" },
0265 { 1, "on" },
0266 { -1, NULL },
0267 };
0268
0269 static struct battery_property_map map_status[] = {
0270 { POWER_SUPPLY_STATUS_CHARGING, "charging" },
0271 { POWER_SUPPLY_STATUS_DISCHARGING, "discharging" },
0272 { POWER_SUPPLY_STATUS_NOT_CHARGING, "not-charging" },
0273 { POWER_SUPPLY_STATUS_FULL, "full" },
0274 { -1, NULL },
0275 };
0276
0277 static struct battery_property_map map_health[] = {
0278 { POWER_SUPPLY_HEALTH_GOOD, "good" },
0279 { POWER_SUPPLY_HEALTH_OVERHEAT, "overheat" },
0280 { POWER_SUPPLY_HEALTH_DEAD, "dead" },
0281 { POWER_SUPPLY_HEALTH_OVERVOLTAGE, "overvoltage" },
0282 { POWER_SUPPLY_HEALTH_UNSPEC_FAILURE, "failure" },
0283 { -1, NULL },
0284 };
0285
0286 static struct battery_property_map map_present[] = {
0287 { 0, "false" },
0288 { 1, "true" },
0289 { -1, NULL },
0290 };
0291
0292 static struct battery_property_map map_technology[] = {
0293 { POWER_SUPPLY_TECHNOLOGY_NiMH, "NiMH" },
0294 { POWER_SUPPLY_TECHNOLOGY_LION, "LION" },
0295 { POWER_SUPPLY_TECHNOLOGY_LIPO, "LIPO" },
0296 { POWER_SUPPLY_TECHNOLOGY_LiFe, "LiFe" },
0297 { POWER_SUPPLY_TECHNOLOGY_NiCd, "NiCd" },
0298 { POWER_SUPPLY_TECHNOLOGY_LiMn, "LiMn" },
0299 { -1, NULL },
0300 };
0301
0302
0303 static int map_get_value(struct battery_property_map *map, const char *key,
0304 int def_val)
0305 {
0306 char buf[MAX_KEYLENGTH];
0307 int cr;
0308
0309 strncpy(buf, key, MAX_KEYLENGTH);
0310 buf[MAX_KEYLENGTH-1] = '\0';
0311
0312 cr = strnlen(buf, MAX_KEYLENGTH) - 1;
0313 if (cr < 0)
0314 return def_val;
0315 if (buf[cr] == '\n')
0316 buf[cr] = '\0';
0317
0318 while (map->key) {
0319 if (strncasecmp(map->key, buf, MAX_KEYLENGTH) == 0)
0320 return map->value;
0321 map++;
0322 }
0323
0324 return def_val;
0325 }
0326
0327
0328 static const char *map_get_key(struct battery_property_map *map, int value,
0329 const char *def_key)
0330 {
0331 while (map->key) {
0332 if (map->value == value)
0333 return map->key;
0334 map++;
0335 }
0336
0337 return def_key;
0338 }
0339
0340 static inline void signal_power_supply_changed(struct power_supply *psy)
0341 {
0342 if (module_initialized)
0343 power_supply_changed(psy);
0344 }
0345
0346 static int param_set_ac_online(const char *key, const struct kernel_param *kp)
0347 {
0348 ac_online = map_get_value(map_ac_online, key, ac_online);
0349 signal_power_supply_changed(test_power_supplies[TEST_AC]);
0350 return 0;
0351 }
0352
0353 static int param_get_ac_online(char *buffer, const struct kernel_param *kp)
0354 {
0355 return sprintf(buffer, "%s\n",
0356 map_get_key(map_ac_online, ac_online, "unknown"));
0357 }
0358
0359 static int param_set_usb_online(const char *key, const struct kernel_param *kp)
0360 {
0361 usb_online = map_get_value(map_ac_online, key, usb_online);
0362 signal_power_supply_changed(test_power_supplies[TEST_USB]);
0363 return 0;
0364 }
0365
0366 static int param_get_usb_online(char *buffer, const struct kernel_param *kp)
0367 {
0368 return sprintf(buffer, "%s\n",
0369 map_get_key(map_ac_online, usb_online, "unknown"));
0370 }
0371
0372 static int param_set_battery_status(const char *key,
0373 const struct kernel_param *kp)
0374 {
0375 battery_status = map_get_value(map_status, key, battery_status);
0376 signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
0377 return 0;
0378 }
0379
0380 static int param_get_battery_status(char *buffer, const struct kernel_param *kp)
0381 {
0382 return sprintf(buffer, "%s\n",
0383 map_get_key(map_ac_online, battery_status, "unknown"));
0384 }
0385
0386 static int param_set_battery_health(const char *key,
0387 const struct kernel_param *kp)
0388 {
0389 battery_health = map_get_value(map_health, key, battery_health);
0390 signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
0391 return 0;
0392 }
0393
0394 static int param_get_battery_health(char *buffer, const struct kernel_param *kp)
0395 {
0396 return sprintf(buffer, "%s\n",
0397 map_get_key(map_ac_online, battery_health, "unknown"));
0398 }
0399
0400 static int param_set_battery_present(const char *key,
0401 const struct kernel_param *kp)
0402 {
0403 battery_present = map_get_value(map_present, key, battery_present);
0404 signal_power_supply_changed(test_power_supplies[TEST_AC]);
0405 return 0;
0406 }
0407
0408 static int param_get_battery_present(char *buffer,
0409 const struct kernel_param *kp)
0410 {
0411 return sprintf(buffer, "%s\n",
0412 map_get_key(map_ac_online, battery_present, "unknown"));
0413 }
0414
0415 static int param_set_battery_technology(const char *key,
0416 const struct kernel_param *kp)
0417 {
0418 battery_technology = map_get_value(map_technology, key,
0419 battery_technology);
0420 signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
0421 return 0;
0422 }
0423
0424 static int param_get_battery_technology(char *buffer,
0425 const struct kernel_param *kp)
0426 {
0427 return sprintf(buffer, "%s\n",
0428 map_get_key(map_ac_online, battery_technology,
0429 "unknown"));
0430 }
0431
0432 static int param_set_battery_capacity(const char *key,
0433 const struct kernel_param *kp)
0434 {
0435 int tmp;
0436
0437 if (1 != sscanf(key, "%d", &tmp))
0438 return -EINVAL;
0439
0440 battery_capacity = tmp;
0441 signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
0442 return 0;
0443 }
0444
0445 #define param_get_battery_capacity param_get_int
0446
0447 static int param_set_battery_voltage(const char *key,
0448 const struct kernel_param *kp)
0449 {
0450 int tmp;
0451
0452 if (1 != sscanf(key, "%d", &tmp))
0453 return -EINVAL;
0454
0455 battery_voltage = tmp;
0456 signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
0457 return 0;
0458 }
0459
0460 #define param_get_battery_voltage param_get_int
0461
0462 static int param_set_battery_charge_counter(const char *key,
0463 const struct kernel_param *kp)
0464 {
0465 int tmp;
0466
0467 if (1 != sscanf(key, "%d", &tmp))
0468 return -EINVAL;
0469
0470 battery_charge_counter = tmp;
0471 signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
0472 return 0;
0473 }
0474
0475 #define param_get_battery_charge_counter param_get_int
0476
0477 static int param_set_battery_current(const char *key,
0478 const struct kernel_param *kp)
0479 {
0480 int tmp;
0481
0482 if (1 != sscanf(key, "%d", &tmp))
0483 return -EINVAL;
0484
0485 battery_current = tmp;
0486 signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
0487 return 0;
0488 }
0489
0490 #define param_get_battery_current param_get_int
0491
0492 static const struct kernel_param_ops param_ops_ac_online = {
0493 .set = param_set_ac_online,
0494 .get = param_get_ac_online,
0495 };
0496
0497 static const struct kernel_param_ops param_ops_usb_online = {
0498 .set = param_set_usb_online,
0499 .get = param_get_usb_online,
0500 };
0501
0502 static const struct kernel_param_ops param_ops_battery_status = {
0503 .set = param_set_battery_status,
0504 .get = param_get_battery_status,
0505 };
0506
0507 static const struct kernel_param_ops param_ops_battery_present = {
0508 .set = param_set_battery_present,
0509 .get = param_get_battery_present,
0510 };
0511
0512 static const struct kernel_param_ops param_ops_battery_technology = {
0513 .set = param_set_battery_technology,
0514 .get = param_get_battery_technology,
0515 };
0516
0517 static const struct kernel_param_ops param_ops_battery_health = {
0518 .set = param_set_battery_health,
0519 .get = param_get_battery_health,
0520 };
0521
0522 static const struct kernel_param_ops param_ops_battery_capacity = {
0523 .set = param_set_battery_capacity,
0524 .get = param_get_battery_capacity,
0525 };
0526
0527 static const struct kernel_param_ops param_ops_battery_voltage = {
0528 .set = param_set_battery_voltage,
0529 .get = param_get_battery_voltage,
0530 };
0531
0532 static const struct kernel_param_ops param_ops_battery_charge_counter = {
0533 .set = param_set_battery_charge_counter,
0534 .get = param_get_battery_charge_counter,
0535 };
0536
0537 static const struct kernel_param_ops param_ops_battery_current = {
0538 .set = param_set_battery_current,
0539 .get = param_get_battery_current,
0540 };
0541
0542 #define param_check_ac_online(name, p) __param_check(name, p, void);
0543 #define param_check_usb_online(name, p) __param_check(name, p, void);
0544 #define param_check_battery_status(name, p) __param_check(name, p, void);
0545 #define param_check_battery_present(name, p) __param_check(name, p, void);
0546 #define param_check_battery_technology(name, p) __param_check(name, p, void);
0547 #define param_check_battery_health(name, p) __param_check(name, p, void);
0548 #define param_check_battery_capacity(name, p) __param_check(name, p, void);
0549 #define param_check_battery_voltage(name, p) __param_check(name, p, void);
0550 #define param_check_battery_charge_counter(name, p) __param_check(name, p, void);
0551 #define param_check_battery_current(name, p) __param_check(name, p, void);
0552
0553
0554 module_param(ac_online, ac_online, 0644);
0555 MODULE_PARM_DESC(ac_online, "AC charging state <on|off>");
0556
0557 module_param(usb_online, usb_online, 0644);
0558 MODULE_PARM_DESC(usb_online, "USB charging state <on|off>");
0559
0560 module_param(battery_status, battery_status, 0644);
0561 MODULE_PARM_DESC(battery_status,
0562 "battery status <charging|discharging|not-charging|full>");
0563
0564 module_param(battery_present, battery_present, 0644);
0565 MODULE_PARM_DESC(battery_present,
0566 "battery presence state <good|overheat|dead|overvoltage|failure>");
0567
0568 module_param(battery_technology, battery_technology, 0644);
0569 MODULE_PARM_DESC(battery_technology,
0570 "battery technology <NiMH|LION|LIPO|LiFe|NiCd|LiMn>");
0571
0572 module_param(battery_health, battery_health, 0644);
0573 MODULE_PARM_DESC(battery_health,
0574 "battery health state <good|overheat|dead|overvoltage|failure>");
0575
0576 module_param(battery_capacity, battery_capacity, 0644);
0577 MODULE_PARM_DESC(battery_capacity, "battery capacity (percentage)");
0578
0579 module_param(battery_voltage, battery_voltage, 0644);
0580 MODULE_PARM_DESC(battery_voltage, "battery voltage (millivolts)");
0581
0582 module_param(battery_charge_counter, battery_charge_counter, 0644);
0583 MODULE_PARM_DESC(battery_charge_counter,
0584 "battery charge counter (microampere-hours)");
0585
0586 module_param(battery_current, battery_current, 0644);
0587 MODULE_PARM_DESC(battery_current, "battery current (milliampere)");
0588
0589 MODULE_DESCRIPTION("Power supply driver for testing");
0590 MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>");
0591 MODULE_LICENSE("GPL");