| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Power supply driver for testing. |
|---|
| 3 | 4 | * |
|---|
| .. | .. |
|---|
| 8 | 9 | * By: Masashi YOKOTA <yokota@pylone.jp> |
|---|
| 9 | 10 | * Originally found here: |
|---|
| 10 | 11 | * http://downloads.pylone.jp/src/virtual_battery/virtual_battery-0.0.1.tar.bz2 |
|---|
| 11 | | - * |
|---|
| 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 | 12 | */ |
|---|
| 16 | 13 | |
|---|
| 17 | 14 | #include <linux/kernel.h> |
|---|
| .. | .. |
|---|
| 19 | 16 | #include <linux/power_supply.h> |
|---|
| 20 | 17 | #include <linux/errno.h> |
|---|
| 21 | 18 | #include <linux/delay.h> |
|---|
| 22 | | -#include <linux/vermagic.h> |
|---|
| 19 | +#include <generated/utsrelease.h> |
|---|
| 23 | 20 | #include <linux/of.h> |
|---|
| 24 | 21 | |
|---|
| 25 | 22 | enum test_power_id { |
|---|
| .. | .. |
|---|
| 36 | 33 | static int battery_present = 1; /* true */ |
|---|
| 37 | 34 | static int battery_technology = POWER_SUPPLY_TECHNOLOGY_LION; |
|---|
| 38 | 35 | static int battery_capacity = 50; |
|---|
| 39 | | -static int battery_voltage = 3300; |
|---|
| 36 | +static int battery_voltage = 3300000; |
|---|
| 37 | +static int battery_charge_counter = -1000; |
|---|
| 38 | +static int battery_current = -1600; |
|---|
| 40 | 39 | |
|---|
| 41 | 40 | static bool module_initialized; |
|---|
| 42 | 41 | |
|---|
| .. | .. |
|---|
| 104 | 103 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
|---|
| 105 | 104 | val->intval = battery_capacity; |
|---|
| 106 | 105 | break; |
|---|
| 106 | + case POWER_SUPPLY_PROP_CHARGE_COUNTER: |
|---|
| 107 | + val->intval = battery_charge_counter; |
|---|
| 108 | + break; |
|---|
| 107 | 109 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
|---|
| 108 | 110 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
|---|
| 109 | 111 | val->intval = 100; |
|---|
| .. | .. |
|---|
| 117 | 119 | break; |
|---|
| 118 | 120 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
|---|
| 119 | 121 | val->intval = battery_voltage; |
|---|
| 122 | + break; |
|---|
| 123 | + case POWER_SUPPLY_PROP_CURRENT_AVG: |
|---|
| 124 | + case POWER_SUPPLY_PROP_CURRENT_NOW: |
|---|
| 125 | + val->intval = battery_current; |
|---|
| 120 | 126 | break; |
|---|
| 121 | 127 | default: |
|---|
| 122 | 128 | pr_info("%s: some properties deliberately report errors.\n", |
|---|
| .. | .. |
|---|
| 139 | 145 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
|---|
| 140 | 146 | POWER_SUPPLY_PROP_CHARGE_FULL, |
|---|
| 141 | 147 | POWER_SUPPLY_PROP_CHARGE_NOW, |
|---|
| 148 | + POWER_SUPPLY_PROP_CHARGE_COUNTER, |
|---|
| 142 | 149 | POWER_SUPPLY_PROP_CAPACITY, |
|---|
| 143 | 150 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
|---|
| 144 | 151 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, |
|---|
| .. | .. |
|---|
| 148 | 155 | POWER_SUPPLY_PROP_SERIAL_NUMBER, |
|---|
| 149 | 156 | POWER_SUPPLY_PROP_TEMP, |
|---|
| 150 | 157 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
|---|
| 158 | + POWER_SUPPLY_PROP_CURRENT_AVG, |
|---|
| 159 | + POWER_SUPPLY_PROP_CURRENT_NOW, |
|---|
| 151 | 160 | }; |
|---|
| 152 | 161 | |
|---|
| 153 | 162 | static char *test_power_ac_supplied_to[] = { |
|---|
| .. | .. |
|---|
| 357 | 366 | |
|---|
| 358 | 367 | static int param_get_ac_online(char *buffer, const struct kernel_param *kp) |
|---|
| 359 | 368 | { |
|---|
| 360 | | - strcpy(buffer, map_get_key(map_ac_online, ac_online, "unknown")); |
|---|
| 361 | | - strcat(buffer, "\n"); |
|---|
| 362 | | - return strlen(buffer); |
|---|
| 369 | + return sprintf(buffer, "%s\n", |
|---|
| 370 | + map_get_key(map_ac_online, ac_online, "unknown")); |
|---|
| 363 | 371 | } |
|---|
| 364 | 372 | |
|---|
| 365 | 373 | static int param_set_usb_online(const char *key, const struct kernel_param *kp) |
|---|
| .. | .. |
|---|
| 371 | 379 | |
|---|
| 372 | 380 | static int param_get_usb_online(char *buffer, const struct kernel_param *kp) |
|---|
| 373 | 381 | { |
|---|
| 374 | | - strcpy(buffer, map_get_key(map_ac_online, usb_online, "unknown")); |
|---|
| 375 | | - strcat(buffer, "\n"); |
|---|
| 376 | | - return strlen(buffer); |
|---|
| 382 | + return sprintf(buffer, "%s\n", |
|---|
| 383 | + map_get_key(map_ac_online, usb_online, "unknown")); |
|---|
| 377 | 384 | } |
|---|
| 378 | 385 | |
|---|
| 379 | 386 | static int param_set_battery_status(const char *key, |
|---|
| .. | .. |
|---|
| 386 | 393 | |
|---|
| 387 | 394 | static int param_get_battery_status(char *buffer, const struct kernel_param *kp) |
|---|
| 388 | 395 | { |
|---|
| 389 | | - strcpy(buffer, map_get_key(map_status, battery_status, "unknown")); |
|---|
| 390 | | - strcat(buffer, "\n"); |
|---|
| 391 | | - return strlen(buffer); |
|---|
| 396 | + return sprintf(buffer, "%s\n", |
|---|
| 397 | + map_get_key(map_ac_online, battery_status, "unknown")); |
|---|
| 392 | 398 | } |
|---|
| 393 | 399 | |
|---|
| 394 | 400 | static int param_set_battery_health(const char *key, |
|---|
| .. | .. |
|---|
| 401 | 407 | |
|---|
| 402 | 408 | static int param_get_battery_health(char *buffer, const struct kernel_param *kp) |
|---|
| 403 | 409 | { |
|---|
| 404 | | - strcpy(buffer, map_get_key(map_health, battery_health, "unknown")); |
|---|
| 405 | | - strcat(buffer, "\n"); |
|---|
| 406 | | - return strlen(buffer); |
|---|
| 410 | + return sprintf(buffer, "%s\n", |
|---|
| 411 | + map_get_key(map_ac_online, battery_health, "unknown")); |
|---|
| 407 | 412 | } |
|---|
| 408 | 413 | |
|---|
| 409 | 414 | static int param_set_battery_present(const char *key, |
|---|
| .. | .. |
|---|
| 417 | 422 | static int param_get_battery_present(char *buffer, |
|---|
| 418 | 423 | const struct kernel_param *kp) |
|---|
| 419 | 424 | { |
|---|
| 420 | | - strcpy(buffer, map_get_key(map_present, battery_present, "unknown")); |
|---|
| 421 | | - strcat(buffer, "\n"); |
|---|
| 422 | | - return strlen(buffer); |
|---|
| 425 | + return sprintf(buffer, "%s\n", |
|---|
| 426 | + map_get_key(map_ac_online, battery_present, "unknown")); |
|---|
| 423 | 427 | } |
|---|
| 424 | 428 | |
|---|
| 425 | 429 | static int param_set_battery_technology(const char *key, |
|---|
| .. | .. |
|---|
| 434 | 438 | static int param_get_battery_technology(char *buffer, |
|---|
| 435 | 439 | const struct kernel_param *kp) |
|---|
| 436 | 440 | { |
|---|
| 437 | | - strcpy(buffer, |
|---|
| 438 | | - map_get_key(map_technology, battery_technology, "unknown")); |
|---|
| 439 | | - strcat(buffer, "\n"); |
|---|
| 440 | | - return strlen(buffer); |
|---|
| 441 | + return sprintf(buffer, "%s\n", |
|---|
| 442 | + map_get_key(map_ac_online, battery_technology, |
|---|
| 443 | + "unknown")); |
|---|
| 441 | 444 | } |
|---|
| 442 | 445 | |
|---|
| 443 | 446 | static int param_set_battery_capacity(const char *key, |
|---|
| .. | .. |
|---|
| 469 | 472 | } |
|---|
| 470 | 473 | |
|---|
| 471 | 474 | #define param_get_battery_voltage param_get_int |
|---|
| 475 | + |
|---|
| 476 | +static int param_set_battery_charge_counter(const char *key, |
|---|
| 477 | + const struct kernel_param *kp) |
|---|
| 478 | +{ |
|---|
| 479 | + int tmp; |
|---|
| 480 | + |
|---|
| 481 | + if (1 != sscanf(key, "%d", &tmp)) |
|---|
| 482 | + return -EINVAL; |
|---|
| 483 | + |
|---|
| 484 | + battery_charge_counter = tmp; |
|---|
| 485 | + signal_power_supply_changed(test_power_supplies[TEST_BATTERY]); |
|---|
| 486 | + return 0; |
|---|
| 487 | +} |
|---|
| 488 | + |
|---|
| 489 | +#define param_get_battery_charge_counter param_get_int |
|---|
| 490 | + |
|---|
| 491 | +static int param_set_battery_current(const char *key, |
|---|
| 492 | + const struct kernel_param *kp) |
|---|
| 493 | +{ |
|---|
| 494 | + int tmp; |
|---|
| 495 | + |
|---|
| 496 | + if (1 != sscanf(key, "%d", &tmp)) |
|---|
| 497 | + return -EINVAL; |
|---|
| 498 | + |
|---|
| 499 | + battery_current = tmp; |
|---|
| 500 | + signal_power_supply_changed(test_power_supplies[TEST_BATTERY]); |
|---|
| 501 | + return 0; |
|---|
| 502 | +} |
|---|
| 503 | + |
|---|
| 504 | +#define param_get_battery_current param_get_int |
|---|
| 472 | 505 | |
|---|
| 473 | 506 | static const struct kernel_param_ops param_ops_ac_online = { |
|---|
| 474 | 507 | .set = param_set_ac_online, |
|---|
| .. | .. |
|---|
| 510 | 543 | .get = param_get_battery_voltage, |
|---|
| 511 | 544 | }; |
|---|
| 512 | 545 | |
|---|
| 546 | +static const struct kernel_param_ops param_ops_battery_charge_counter = { |
|---|
| 547 | + .set = param_set_battery_charge_counter, |
|---|
| 548 | + .get = param_get_battery_charge_counter, |
|---|
| 549 | +}; |
|---|
| 550 | + |
|---|
| 551 | +static const struct kernel_param_ops param_ops_battery_current = { |
|---|
| 552 | + .set = param_set_battery_current, |
|---|
| 553 | + .get = param_get_battery_current, |
|---|
| 554 | +}; |
|---|
| 555 | + |
|---|
| 513 | 556 | #define param_check_ac_online(name, p) __param_check(name, p, void); |
|---|
| 514 | 557 | #define param_check_usb_online(name, p) __param_check(name, p, void); |
|---|
| 515 | 558 | #define param_check_battery_status(name, p) __param_check(name, p, void); |
|---|
| .. | .. |
|---|
| 518 | 561 | #define param_check_battery_health(name, p) __param_check(name, p, void); |
|---|
| 519 | 562 | #define param_check_battery_capacity(name, p) __param_check(name, p, void); |
|---|
| 520 | 563 | #define param_check_battery_voltage(name, p) __param_check(name, p, void); |
|---|
| 564 | +#define param_check_battery_charge_counter(name, p) __param_check(name, p, void); |
|---|
| 565 | +#define param_check_battery_current(name, p) __param_check(name, p, void); |
|---|
| 521 | 566 | |
|---|
| 522 | 567 | |
|---|
| 523 | 568 | module_param(ac_online, ac_online, 0644); |
|---|
| .. | .. |
|---|
| 548 | 593 | module_param(battery_voltage, battery_voltage, 0644); |
|---|
| 549 | 594 | MODULE_PARM_DESC(battery_voltage, "battery voltage (millivolts)"); |
|---|
| 550 | 595 | |
|---|
| 596 | +module_param(battery_charge_counter, battery_charge_counter, 0644); |
|---|
| 597 | +MODULE_PARM_DESC(battery_charge_counter, |
|---|
| 598 | + "battery charge counter (microampere-hours)"); |
|---|
| 599 | + |
|---|
| 600 | +module_param(battery_current, battery_current, 0644); |
|---|
| 601 | +MODULE_PARM_DESC(battery_current, "battery current (milliampere)"); |
|---|
| 602 | + |
|---|
| 551 | 603 | MODULE_DESCRIPTION("Power supply driver for testing"); |
|---|
| 552 | 604 | MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>"); |
|---|
| 553 | 605 | MODULE_LICENSE("GPL"); |
|---|