test_power.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Power supply driver for testing.
  4. *
  5. * Copyright 2010 Anton Vorontsov <cbouatmailru@gmail.com>
  6. *
  7. * Dynamic module parameter code from the Virtual Battery Driver
  8. * Copyright (C) 2008 Pylone, Inc.
  9. * By: Masashi YOKOTA <yokota@pylone.jp>
  10. * Originally found here:
  11. * http://downloads.pylone.jp/src/virtual_battery/virtual_battery-0.0.1.tar.bz2
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/power_supply.h>
  16. #include <linux/errno.h>
  17. #include <linux/delay.h>
  18. #include <generated/utsrelease.h>
  19. enum test_power_id {
  20. TEST_AC,
  21. TEST_BATTERY,
  22. TEST_USB,
  23. TEST_POWER_NUM,
  24. };
  25. static int ac_online = 1;
  26. static int usb_online = 1;
  27. static int battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
  28. static int battery_health = POWER_SUPPLY_HEALTH_GOOD;
  29. static int battery_present = 1; /* true */
  30. static int battery_technology = POWER_SUPPLY_TECHNOLOGY_LION;
  31. static int battery_capacity = 50;
  32. static int battery_voltage = 3300;
  33. static int battery_charge_counter = -1000;
  34. static int battery_current = -1600;
  35. static bool module_initialized;
  36. static int test_power_get_ac_property(struct power_supply *psy,
  37. enum power_supply_property psp,
  38. union power_supply_propval *val)
  39. {
  40. switch (psp) {
  41. case POWER_SUPPLY_PROP_ONLINE:
  42. val->intval = ac_online;
  43. break;
  44. default:
  45. return -EINVAL;
  46. }
  47. return 0;
  48. }
  49. static int test_power_get_usb_property(struct power_supply *psy,
  50. enum power_supply_property psp,
  51. union power_supply_propval *val)
  52. {
  53. switch (psp) {
  54. case POWER_SUPPLY_PROP_ONLINE:
  55. val->intval = usb_online;
  56. break;
  57. default:
  58. return -EINVAL;
  59. }
  60. return 0;
  61. }
  62. static int test_power_get_battery_property(struct power_supply *psy,
  63. enum power_supply_property psp,
  64. union power_supply_propval *val)
  65. {
  66. switch (psp) {
  67. case POWER_SUPPLY_PROP_MODEL_NAME:
  68. val->strval = "Test battery";
  69. break;
  70. case POWER_SUPPLY_PROP_MANUFACTURER:
  71. val->strval = "Linux";
  72. break;
  73. case POWER_SUPPLY_PROP_SERIAL_NUMBER:
  74. val->strval = UTS_RELEASE;
  75. break;
  76. case POWER_SUPPLY_PROP_STATUS:
  77. val->intval = battery_status;
  78. break;
  79. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  80. val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
  81. break;
  82. case POWER_SUPPLY_PROP_HEALTH:
  83. val->intval = battery_health;
  84. break;
  85. case POWER_SUPPLY_PROP_PRESENT:
  86. val->intval = battery_present;
  87. break;
  88. case POWER_SUPPLY_PROP_TECHNOLOGY:
  89. val->intval = battery_technology;
  90. break;
  91. case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
  92. val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
  93. break;
  94. case POWER_SUPPLY_PROP_CAPACITY:
  95. case POWER_SUPPLY_PROP_CHARGE_NOW:
  96. val->intval = battery_capacity;
  97. break;
  98. case POWER_SUPPLY_PROP_CHARGE_COUNTER:
  99. val->intval = battery_charge_counter;
  100. break;
  101. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  102. case POWER_SUPPLY_PROP_CHARGE_FULL:
  103. val->intval = 100;
  104. break;
  105. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
  106. case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
  107. val->intval = 3600;
  108. break;
  109. case POWER_SUPPLY_PROP_TEMP:
  110. val->intval = 26;
  111. break;
  112. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  113. val->intval = battery_voltage;
  114. break;
  115. case POWER_SUPPLY_PROP_CURRENT_AVG:
  116. case POWER_SUPPLY_PROP_CURRENT_NOW:
  117. val->intval = battery_current;
  118. break;
  119. default:
  120. pr_info("%s: some properties deliberately report errors.\n",
  121. __func__);
  122. return -EINVAL;
  123. }
  124. return 0;
  125. }
  126. static enum power_supply_property test_power_ac_props[] = {
  127. POWER_SUPPLY_PROP_ONLINE,
  128. };
  129. static enum power_supply_property test_power_battery_props[] = {
  130. POWER_SUPPLY_PROP_STATUS,
  131. POWER_SUPPLY_PROP_CHARGE_TYPE,
  132. POWER_SUPPLY_PROP_HEALTH,
  133. POWER_SUPPLY_PROP_PRESENT,
  134. POWER_SUPPLY_PROP_TECHNOLOGY,
  135. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  136. POWER_SUPPLY_PROP_CHARGE_FULL,
  137. POWER_SUPPLY_PROP_CHARGE_NOW,
  138. POWER_SUPPLY_PROP_CHARGE_COUNTER,
  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_CURRENT_AVG,
  149. POWER_SUPPLY_PROP_CURRENT_NOW,
  150. };
  151. static char *test_power_ac_supplied_to[] = {
  152. "test_battery",
  153. };
  154. static struct power_supply *test_power_supplies[TEST_POWER_NUM];
  155. static const struct power_supply_desc test_power_desc[] = {
  156. [TEST_AC] = {
  157. .name = "test_ac",
  158. .type = POWER_SUPPLY_TYPE_MAINS,
  159. .properties = test_power_ac_props,
  160. .num_properties = ARRAY_SIZE(test_power_ac_props),
  161. .get_property = test_power_get_ac_property,
  162. },
  163. [TEST_BATTERY] = {
  164. .name = "test_battery",
  165. .type = POWER_SUPPLY_TYPE_BATTERY,
  166. .properties = test_power_battery_props,
  167. .num_properties = ARRAY_SIZE(test_power_battery_props),
  168. .get_property = test_power_get_battery_property,
  169. },
  170. [TEST_USB] = {
  171. .name = "test_usb",
  172. .type = POWER_SUPPLY_TYPE_USB,
  173. .properties = test_power_ac_props,
  174. .num_properties = ARRAY_SIZE(test_power_ac_props),
  175. .get_property = test_power_get_usb_property,
  176. },
  177. };
  178. static const struct power_supply_config test_power_configs[] = {
  179. {
  180. /* test_ac */
  181. .supplied_to = test_power_ac_supplied_to,
  182. .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to),
  183. }, {
  184. /* test_battery */
  185. }, {
  186. /* test_usb */
  187. .supplied_to = test_power_ac_supplied_to,
  188. .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to),
  189. },
  190. };
  191. static int __init test_power_init(void)
  192. {
  193. int i;
  194. int ret;
  195. BUILD_BUG_ON(TEST_POWER_NUM != ARRAY_SIZE(test_power_supplies));
  196. BUILD_BUG_ON(TEST_POWER_NUM != ARRAY_SIZE(test_power_configs));
  197. for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++) {
  198. test_power_supplies[i] = power_supply_register(NULL,
  199. &test_power_desc[i],
  200. &test_power_configs[i]);
  201. if (IS_ERR(test_power_supplies[i])) {
  202. pr_err("%s: failed to register %s\n", __func__,
  203. test_power_desc[i].name);
  204. ret = PTR_ERR(test_power_supplies[i]);
  205. goto failed;
  206. }
  207. }
  208. module_initialized = true;
  209. return 0;
  210. failed:
  211. while (--i >= 0)
  212. power_supply_unregister(test_power_supplies[i]);
  213. return ret;
  214. }
  215. module_init(test_power_init);
  216. static void __exit test_power_exit(void)
  217. {
  218. int i;
  219. /* Let's see how we handle changes... */
  220. ac_online = 0;
  221. usb_online = 0;
  222. battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
  223. for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++)
  224. power_supply_changed(test_power_supplies[i]);
  225. pr_info("%s: 'changed' event sent, sleeping for 10 seconds...\n",
  226. __func__);
  227. ssleep(10);
  228. for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++)
  229. power_supply_unregister(test_power_supplies[i]);
  230. module_initialized = false;
  231. }
  232. module_exit(test_power_exit);
  233. #define MAX_KEYLENGTH 256
  234. struct battery_property_map {
  235. int value;
  236. char const *key;
  237. };
  238. static struct battery_property_map map_ac_online[] = {
  239. { 0, "off" },
  240. { 1, "on" },
  241. { -1, NULL },
  242. };
  243. static struct battery_property_map map_status[] = {
  244. { POWER_SUPPLY_STATUS_CHARGING, "charging" },
  245. { POWER_SUPPLY_STATUS_DISCHARGING, "discharging" },
  246. { POWER_SUPPLY_STATUS_NOT_CHARGING, "not-charging" },
  247. { POWER_SUPPLY_STATUS_FULL, "full" },
  248. { -1, NULL },
  249. };
  250. static struct battery_property_map map_health[] = {
  251. { POWER_SUPPLY_HEALTH_GOOD, "good" },
  252. { POWER_SUPPLY_HEALTH_OVERHEAT, "overheat" },
  253. { POWER_SUPPLY_HEALTH_DEAD, "dead" },
  254. { POWER_SUPPLY_HEALTH_OVERVOLTAGE, "overvoltage" },
  255. { POWER_SUPPLY_HEALTH_UNSPEC_FAILURE, "failure" },
  256. { -1, NULL },
  257. };
  258. static struct battery_property_map map_present[] = {
  259. { 0, "false" },
  260. { 1, "true" },
  261. { -1, NULL },
  262. };
  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" },
  270. { -1, NULL },
  271. };
  272. static int map_get_value(struct battery_property_map *map, const char *key,
  273. int def_val)
  274. {
  275. char buf[MAX_KEYLENGTH];
  276. int cr;
  277. strncpy(buf, key, MAX_KEYLENGTH);
  278. buf[MAX_KEYLENGTH-1] = '\0';
  279. cr = strnlen(buf, MAX_KEYLENGTH) - 1;
  280. if (cr < 0)
  281. return def_val;
  282. if (buf[cr] == '\n')
  283. buf[cr] = '\0';
  284. while (map->key) {
  285. if (strncasecmp(map->key, buf, MAX_KEYLENGTH) == 0)
  286. return map->value;
  287. map++;
  288. }
  289. return def_val;
  290. }
  291. static const char *map_get_key(struct battery_property_map *map, int value,
  292. const char *def_key)
  293. {
  294. while (map->key) {
  295. if (map->value == value)
  296. return map->key;
  297. map++;
  298. }
  299. return def_key;
  300. }
  301. static inline void signal_power_supply_changed(struct power_supply *psy)
  302. {
  303. if (module_initialized)
  304. power_supply_changed(psy);
  305. }
  306. static int param_set_ac_online(const char *key, const struct kernel_param *kp)
  307. {
  308. ac_online = map_get_value(map_ac_online, key, ac_online);
  309. signal_power_supply_changed(test_power_supplies[TEST_AC]);
  310. return 0;
  311. }
  312. static int param_get_ac_online(char *buffer, const struct kernel_param *kp)
  313. {
  314. return sprintf(buffer, "%s\n",
  315. map_get_key(map_ac_online, ac_online, "unknown"));
  316. }
  317. static int param_set_usb_online(const char *key, const struct kernel_param *kp)
  318. {
  319. usb_online = map_get_value(map_ac_online, key, usb_online);
  320. signal_power_supply_changed(test_power_supplies[TEST_USB]);
  321. return 0;
  322. }
  323. static int param_get_usb_online(char *buffer, const struct kernel_param *kp)
  324. {
  325. return sprintf(buffer, "%s\n",
  326. map_get_key(map_ac_online, usb_online, "unknown"));
  327. }
  328. static int param_set_battery_status(const char *key,
  329. const struct kernel_param *kp)
  330. {
  331. battery_status = map_get_value(map_status, key, battery_status);
  332. signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
  333. return 0;
  334. }
  335. static int param_get_battery_status(char *buffer, const struct kernel_param *kp)
  336. {
  337. return sprintf(buffer, "%s\n",
  338. map_get_key(map_ac_online, battery_status, "unknown"));
  339. }
  340. static int param_set_battery_health(const char *key,
  341. const struct kernel_param *kp)
  342. {
  343. battery_health = map_get_value(map_health, key, battery_health);
  344. signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
  345. return 0;
  346. }
  347. static int param_get_battery_health(char *buffer, const struct kernel_param *kp)
  348. {
  349. return sprintf(buffer, "%s\n",
  350. map_get_key(map_ac_online, battery_health, "unknown"));
  351. }
  352. static int param_set_battery_present(const char *key,
  353. const struct kernel_param *kp)
  354. {
  355. battery_present = map_get_value(map_present, key, battery_present);
  356. signal_power_supply_changed(test_power_supplies[TEST_AC]);
  357. return 0;
  358. }
  359. static int param_get_battery_present(char *buffer,
  360. const struct kernel_param *kp)
  361. {
  362. return sprintf(buffer, "%s\n",
  363. map_get_key(map_ac_online, battery_present, "unknown"));
  364. }
  365. static int param_set_battery_technology(const char *key,
  366. const struct kernel_param *kp)
  367. {
  368. battery_technology = map_get_value(map_technology, key,
  369. battery_technology);
  370. signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
  371. return 0;
  372. }
  373. static int param_get_battery_technology(char *buffer,
  374. const struct kernel_param *kp)
  375. {
  376. return sprintf(buffer, "%s\n",
  377. map_get_key(map_ac_online, battery_technology,
  378. "unknown"));
  379. }
  380. static int param_set_battery_capacity(const char *key,
  381. const struct kernel_param *kp)
  382. {
  383. int tmp;
  384. if (1 != sscanf(key, "%d", &tmp))
  385. return -EINVAL;
  386. battery_capacity = tmp;
  387. signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
  388. return 0;
  389. }
  390. #define param_get_battery_capacity param_get_int
  391. static int param_set_battery_voltage(const char *key,
  392. const struct kernel_param *kp)
  393. {
  394. int tmp;
  395. if (1 != sscanf(key, "%d", &tmp))
  396. return -EINVAL;
  397. battery_voltage = tmp;
  398. signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
  399. return 0;
  400. }
  401. #define param_get_battery_voltage param_get_int
  402. static int param_set_battery_charge_counter(const char *key,
  403. const struct kernel_param *kp)
  404. {
  405. int tmp;
  406. if (1 != sscanf(key, "%d", &tmp))
  407. return -EINVAL;
  408. battery_charge_counter = tmp;
  409. signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
  410. return 0;
  411. }
  412. #define param_get_battery_charge_counter param_get_int
  413. static int param_set_battery_current(const char *key,
  414. const struct kernel_param *kp)
  415. {
  416. int tmp;
  417. if (1 != sscanf(key, "%d", &tmp))
  418. return -EINVAL;
  419. battery_current = tmp;
  420. signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
  421. return 0;
  422. }
  423. #define param_get_battery_current param_get_int
  424. static const struct kernel_param_ops param_ops_ac_online = {
  425. .set = param_set_ac_online,
  426. .get = param_get_ac_online,
  427. };
  428. static const struct kernel_param_ops param_ops_usb_online = {
  429. .set = param_set_usb_online,
  430. .get = param_get_usb_online,
  431. };
  432. static const struct kernel_param_ops param_ops_battery_status = {
  433. .set = param_set_battery_status,
  434. .get = param_get_battery_status,
  435. };
  436. static const struct kernel_param_ops param_ops_battery_present = {
  437. .set = param_set_battery_present,
  438. .get = param_get_battery_present,
  439. };
  440. static const struct kernel_param_ops param_ops_battery_technology = {
  441. .set = param_set_battery_technology,
  442. .get = param_get_battery_technology,
  443. };
  444. static const struct kernel_param_ops param_ops_battery_health = {
  445. .set = param_set_battery_health,
  446. .get = param_get_battery_health,
  447. };
  448. static const struct kernel_param_ops param_ops_battery_capacity = {
  449. .set = param_set_battery_capacity,
  450. .get = param_get_battery_capacity,
  451. };
  452. static const struct kernel_param_ops param_ops_battery_voltage = {
  453. .set = param_set_battery_voltage,
  454. .get = param_get_battery_voltage,
  455. };
  456. static const struct kernel_param_ops param_ops_battery_charge_counter = {
  457. .set = param_set_battery_charge_counter,
  458. .get = param_get_battery_charge_counter,
  459. };
  460. static const struct kernel_param_ops param_ops_battery_current = {
  461. .set = param_set_battery_current,
  462. .get = param_get_battery_current,
  463. };
  464. #define param_check_ac_online(name, p) __param_check(name, p, void);
  465. #define param_check_usb_online(name, p) __param_check(name, p, void);
  466. #define param_check_battery_status(name, p) __param_check(name, p, void);
  467. #define param_check_battery_present(name, p) __param_check(name, p, void);
  468. #define param_check_battery_technology(name, p) __param_check(name, p, void);
  469. #define param_check_battery_health(name, p) __param_check(name, p, void);
  470. #define param_check_battery_capacity(name, p) __param_check(name, p, void);
  471. #define param_check_battery_voltage(name, p) __param_check(name, p, void);
  472. #define param_check_battery_charge_counter(name, p) __param_check(name, p, void);
  473. #define param_check_battery_current(name, p) __param_check(name, p, void);
  474. module_param(ac_online, ac_online, 0644);
  475. MODULE_PARM_DESC(ac_online, "AC charging state <on|off>");
  476. module_param(usb_online, usb_online, 0644);
  477. MODULE_PARM_DESC(usb_online, "USB charging state <on|off>");
  478. module_param(battery_status, battery_status, 0644);
  479. MODULE_PARM_DESC(battery_status,
  480. "battery status <charging|discharging|not-charging|full>");
  481. module_param(battery_present, battery_present, 0644);
  482. MODULE_PARM_DESC(battery_present,
  483. "battery presence state <good|overheat|dead|overvoltage|failure>");
  484. module_param(battery_technology, battery_technology, 0644);
  485. MODULE_PARM_DESC(battery_technology,
  486. "battery technology <NiMH|LION|LIPO|LiFe|NiCd|LiMn>");
  487. module_param(battery_health, battery_health, 0644);
  488. MODULE_PARM_DESC(battery_health,
  489. "battery health state <good|overheat|dead|overvoltage|failure>");
  490. module_param(battery_capacity, battery_capacity, 0644);
  491. MODULE_PARM_DESC(battery_capacity, "battery capacity (percentage)");
  492. module_param(battery_voltage, battery_voltage, 0644);
  493. MODULE_PARM_DESC(battery_voltage, "battery voltage (millivolts)");
  494. module_param(battery_charge_counter, battery_charge_counter, 0644);
  495. MODULE_PARM_DESC(battery_charge_counter,
  496. "battery charge counter (microampere-hours)");
  497. module_param(battery_current, battery_current, 0644);
  498. MODULE_PARM_DESC(battery_current, "battery current (milliampere)");
  499. MODULE_DESCRIPTION("Power supply driver for testing");
  500. MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>");
  501. MODULE_LICENSE("GPL");