test_power.c 14 KB

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