hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/drivers/power/supply/test_power.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Power supply driver for testing.
34 *
....@@ -8,10 +9,6 @@
89 * By: Masashi YOKOTA <yokota@pylone.jp>
910 * Originally found here:
1011 * 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.
1512 */
1613
1714 #include <linux/kernel.h>
....@@ -19,7 +16,7 @@
1916 #include <linux/power_supply.h>
2017 #include <linux/errno.h>
2118 #include <linux/delay.h>
22
-#include <linux/vermagic.h>
19
+#include <generated/utsrelease.h>
2320 #include <linux/of.h>
2421
2522 enum test_power_id {
....@@ -37,6 +34,8 @@
3734 static int battery_technology = POWER_SUPPLY_TECHNOLOGY_LION;
3835 static int battery_capacity = 50;
3936 static int battery_voltage = 3300;
37
+static int battery_charge_counter = -1000;
38
+static int battery_current = -1600;
4039
4140 static bool module_initialized;
4241
....@@ -104,6 +103,9 @@
104103 case POWER_SUPPLY_PROP_CHARGE_NOW:
105104 val->intval = battery_capacity;
106105 break;
106
+ case POWER_SUPPLY_PROP_CHARGE_COUNTER:
107
+ val->intval = battery_charge_counter;
108
+ break;
107109 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
108110 case POWER_SUPPLY_PROP_CHARGE_FULL:
109111 val->intval = 100;
....@@ -117,6 +119,10 @@
117119 break;
118120 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
119121 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;
120126 break;
121127 default:
122128 pr_info("%s: some properties deliberately report errors.\n",
....@@ -139,6 +145,7 @@
139145 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
140146 POWER_SUPPLY_PROP_CHARGE_FULL,
141147 POWER_SUPPLY_PROP_CHARGE_NOW,
148
+ POWER_SUPPLY_PROP_CHARGE_COUNTER,
142149 POWER_SUPPLY_PROP_CAPACITY,
143150 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
144151 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
....@@ -148,6 +155,8 @@
148155 POWER_SUPPLY_PROP_SERIAL_NUMBER,
149156 POWER_SUPPLY_PROP_TEMP,
150157 POWER_SUPPLY_PROP_VOLTAGE_NOW,
158
+ POWER_SUPPLY_PROP_CURRENT_AVG,
159
+ POWER_SUPPLY_PROP_CURRENT_NOW,
151160 };
152161
153162 static char *test_power_ac_supplied_to[] = {
....@@ -357,9 +366,8 @@
357366
358367 static int param_get_ac_online(char *buffer, const struct kernel_param *kp)
359368 {
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"));
363371 }
364372
365373 static int param_set_usb_online(const char *key, const struct kernel_param *kp)
....@@ -371,9 +379,8 @@
371379
372380 static int param_get_usb_online(char *buffer, const struct kernel_param *kp)
373381 {
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"));
377384 }
378385
379386 static int param_set_battery_status(const char *key,
....@@ -386,9 +393,8 @@
386393
387394 static int param_get_battery_status(char *buffer, const struct kernel_param *kp)
388395 {
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"));
392398 }
393399
394400 static int param_set_battery_health(const char *key,
....@@ -401,9 +407,8 @@
401407
402408 static int param_get_battery_health(char *buffer, const struct kernel_param *kp)
403409 {
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"));
407412 }
408413
409414 static int param_set_battery_present(const char *key,
....@@ -417,9 +422,8 @@
417422 static int param_get_battery_present(char *buffer,
418423 const struct kernel_param *kp)
419424 {
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"));
423427 }
424428
425429 static int param_set_battery_technology(const char *key,
....@@ -434,10 +438,9 @@
434438 static int param_get_battery_technology(char *buffer,
435439 const struct kernel_param *kp)
436440 {
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"));
441444 }
442445
443446 static int param_set_battery_capacity(const char *key,
....@@ -469,6 +472,36 @@
469472 }
470473
471474 #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
472505
473506 static const struct kernel_param_ops param_ops_ac_online = {
474507 .set = param_set_ac_online,
....@@ -510,6 +543,16 @@
510543 .get = param_get_battery_voltage,
511544 };
512545
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
+
513556 #define param_check_ac_online(name, p) __param_check(name, p, void);
514557 #define param_check_usb_online(name, p) __param_check(name, p, void);
515558 #define param_check_battery_status(name, p) __param_check(name, p, void);
....@@ -518,6 +561,8 @@
518561 #define param_check_battery_health(name, p) __param_check(name, p, void);
519562 #define param_check_battery_capacity(name, p) __param_check(name, p, void);
520563 #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);
521566
522567
523568 module_param(ac_online, ac_online, 0644);
....@@ -548,6 +593,13 @@
548593 module_param(battery_voltage, battery_voltage, 0644);
549594 MODULE_PARM_DESC(battery_voltage, "battery voltage (millivolts)");
550595
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
+
551603 MODULE_DESCRIPTION("Power supply driver for testing");
552604 MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>");
553605 MODULE_LICENSE("GPL");