test_power.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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 (buf[cr] == '\n')
  272. buf[cr] = '\0';
  273. while (map->key) {
  274. if (strncasecmp(map->key, buf, MAX_KEYLENGTH) == 0)
  275. return map->value;
  276. map++;
  277. }
  278. return def_val;
  279. }
  280. static const char *map_get_key(struct battery_property_map *map, int value,
  281. const char *def_key)
  282. {
  283. while (map->key) {
  284. if (map->value == value)
  285. return map->key;
  286. map++;
  287. }
  288. return def_key;
  289. }
  290. static inline void signal_power_supply_changed(struct power_supply *psy)
  291. {
  292. if (module_initialized)
  293. power_supply_changed(psy);
  294. }
  295. static int param_set_ac_online(const char *key, const struct kernel_param *kp)
  296. {
  297. ac_online = map_get_value(map_ac_online, key, ac_online);
  298. signal_power_supply_changed(test_power_supplies[TEST_AC]);
  299. return 0;
  300. }
  301. static int param_get_ac_online(char *buffer, const struct kernel_param *kp)
  302. {
  303. strcpy(buffer, map_get_key(map_ac_online, ac_online, "unknown"));
  304. return strlen(buffer);
  305. }
  306. static int param_set_usb_online(const char *key, const struct kernel_param *kp)
  307. {
  308. usb_online = map_get_value(map_ac_online, key, usb_online);
  309. signal_power_supply_changed(test_power_supplies[TEST_USB]);
  310. return 0;
  311. }
  312. static int param_get_usb_online(char *buffer, const struct kernel_param *kp)
  313. {
  314. strcpy(buffer, map_get_key(map_ac_online, usb_online, "unknown"));
  315. return strlen(buffer);
  316. }
  317. static int param_set_battery_status(const char *key,
  318. const struct kernel_param *kp)
  319. {
  320. battery_status = map_get_value(map_status, key, battery_status);
  321. signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
  322. return 0;
  323. }
  324. static int param_get_battery_status(char *buffer, const struct kernel_param *kp)
  325. {
  326. strcpy(buffer, map_get_key(map_status, battery_status, "unknown"));
  327. return strlen(buffer);
  328. }
  329. static int param_set_battery_health(const char *key,
  330. const struct kernel_param *kp)
  331. {
  332. battery_health = map_get_value(map_health, key, battery_health);
  333. signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
  334. return 0;
  335. }
  336. static int param_get_battery_health(char *buffer, const struct kernel_param *kp)
  337. {
  338. strcpy(buffer, map_get_key(map_health, battery_health, "unknown"));
  339. return strlen(buffer);
  340. }
  341. static int param_set_battery_present(const char *key,
  342. const struct kernel_param *kp)
  343. {
  344. battery_present = map_get_value(map_present, key, battery_present);
  345. signal_power_supply_changed(test_power_supplies[TEST_AC]);
  346. return 0;
  347. }
  348. static int param_get_battery_present(char *buffer,
  349. const struct kernel_param *kp)
  350. {
  351. strcpy(buffer, map_get_key(map_present, battery_present, "unknown"));
  352. return strlen(buffer);
  353. }
  354. static int param_set_battery_technology(const char *key,
  355. const struct kernel_param *kp)
  356. {
  357. battery_technology = map_get_value(map_technology, key,
  358. battery_technology);
  359. signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
  360. return 0;
  361. }
  362. static int param_get_battery_technology(char *buffer,
  363. const struct kernel_param *kp)
  364. {
  365. strcpy(buffer,
  366. map_get_key(map_technology, battery_technology, "unknown"));
  367. return strlen(buffer);
  368. }
  369. static int param_set_battery_capacity(const char *key,
  370. const struct kernel_param *kp)
  371. {
  372. int tmp;
  373. if (1 != sscanf(key, "%d", &tmp))
  374. return -EINVAL;
  375. battery_capacity = tmp;
  376. signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
  377. return 0;
  378. }
  379. #define param_get_battery_capacity param_get_int
  380. static int param_set_battery_voltage(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_voltage = tmp;
  387. signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
  388. return 0;
  389. }
  390. #define param_get_battery_voltage param_get_int
  391. static const struct kernel_param_ops param_ops_ac_online = {
  392. .set = param_set_ac_online,
  393. .get = param_get_ac_online,
  394. };
  395. static const struct kernel_param_ops param_ops_usb_online = {
  396. .set = param_set_usb_online,
  397. .get = param_get_usb_online,
  398. };
  399. static const struct kernel_param_ops param_ops_battery_status = {
  400. .set = param_set_battery_status,
  401. .get = param_get_battery_status,
  402. };
  403. static const struct kernel_param_ops param_ops_battery_present = {
  404. .set = param_set_battery_present,
  405. .get = param_get_battery_present,
  406. };
  407. static const struct kernel_param_ops param_ops_battery_technology = {
  408. .set = param_set_battery_technology,
  409. .get = param_get_battery_technology,
  410. };
  411. static const struct kernel_param_ops param_ops_battery_health = {
  412. .set = param_set_battery_health,
  413. .get = param_get_battery_health,
  414. };
  415. static const struct kernel_param_ops param_ops_battery_capacity = {
  416. .set = param_set_battery_capacity,
  417. .get = param_get_battery_capacity,
  418. };
  419. static const struct kernel_param_ops param_ops_battery_voltage = {
  420. .set = param_set_battery_voltage,
  421. .get = param_get_battery_voltage,
  422. };
  423. #define param_check_ac_online(name, p) __param_check(name, p, void);
  424. #define param_check_usb_online(name, p) __param_check(name, p, void);
  425. #define param_check_battery_status(name, p) __param_check(name, p, void);
  426. #define param_check_battery_present(name, p) __param_check(name, p, void);
  427. #define param_check_battery_technology(name, p) __param_check(name, p, void);
  428. #define param_check_battery_health(name, p) __param_check(name, p, void);
  429. #define param_check_battery_capacity(name, p) __param_check(name, p, void);
  430. #define param_check_battery_voltage(name, p) __param_check(name, p, void);
  431. module_param(ac_online, ac_online, 0644);
  432. MODULE_PARM_DESC(ac_online, "AC charging state <on|off>");
  433. module_param(usb_online, usb_online, 0644);
  434. MODULE_PARM_DESC(usb_online, "USB charging state <on|off>");
  435. module_param(battery_status, battery_status, 0644);
  436. MODULE_PARM_DESC(battery_status,
  437. "battery status <charging|discharging|not-charging|full>");
  438. module_param(battery_present, battery_present, 0644);
  439. MODULE_PARM_DESC(battery_present,
  440. "battery presence state <good|overheat|dead|overvoltage|failure>");
  441. module_param(battery_technology, battery_technology, 0644);
  442. MODULE_PARM_DESC(battery_technology,
  443. "battery technology <NiMH|LION|LIPO|LiFe|NiCd|LiMn>");
  444. module_param(battery_health, battery_health, 0644);
  445. MODULE_PARM_DESC(battery_health,
  446. "battery health state <good|overheat|dead|overvoltage|failure>");
  447. module_param(battery_capacity, battery_capacity, 0644);
  448. MODULE_PARM_DESC(battery_capacity, "battery capacity (percentage)");
  449. module_param(battery_voltage, battery_voltage, 0644);
  450. MODULE_PARM_DESC(battery_voltage, "battery voltage (millivolts)");
  451. MODULE_DESCRIPTION("Power supply driver for testing");
  452. MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>");
  453. MODULE_LICENSE("GPL");