test_power.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. static int ac_online = 1;
  23. static int battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
  24. static int battery_health = POWER_SUPPLY_HEALTH_GOOD;
  25. static int battery_present = 1; /* true */
  26. static int battery_technology = POWER_SUPPLY_TECHNOLOGY_LION;
  27. static int battery_capacity = 50;
  28. static int test_power_get_ac_property(struct power_supply *psy,
  29. enum power_supply_property psp,
  30. union power_supply_propval *val)
  31. {
  32. switch (psp) {
  33. case POWER_SUPPLY_PROP_ONLINE:
  34. val->intval = ac_online;
  35. break;
  36. default:
  37. return -EINVAL;
  38. }
  39. return 0;
  40. }
  41. static int test_power_get_battery_property(struct power_supply *psy,
  42. enum power_supply_property psp,
  43. union power_supply_propval *val)
  44. {
  45. switch (psp) {
  46. case POWER_SUPPLY_PROP_MODEL_NAME:
  47. val->strval = "Test battery";
  48. break;
  49. case POWER_SUPPLY_PROP_MANUFACTURER:
  50. val->strval = "Linux";
  51. break;
  52. case POWER_SUPPLY_PROP_SERIAL_NUMBER:
  53. val->strval = UTS_RELEASE;
  54. break;
  55. case POWER_SUPPLY_PROP_STATUS:
  56. val->intval = battery_status;
  57. break;
  58. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  59. val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
  60. break;
  61. case POWER_SUPPLY_PROP_HEALTH:
  62. val->intval = battery_health;
  63. break;
  64. case POWER_SUPPLY_PROP_PRESENT:
  65. val->intval = battery_present;
  66. break;
  67. case POWER_SUPPLY_PROP_TECHNOLOGY:
  68. val->intval = battery_technology;
  69. break;
  70. case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
  71. val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
  72. break;
  73. case POWER_SUPPLY_PROP_CAPACITY:
  74. case POWER_SUPPLY_PROP_CHARGE_NOW:
  75. val->intval = battery_capacity;
  76. break;
  77. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  78. case POWER_SUPPLY_PROP_CHARGE_FULL:
  79. val->intval = 100;
  80. break;
  81. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
  82. case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
  83. val->intval = 3600;
  84. break;
  85. default:
  86. pr_info("%s: some properties deliberately report errors.\n",
  87. __func__);
  88. return -EINVAL;
  89. }
  90. return 0;
  91. }
  92. static enum power_supply_property test_power_ac_props[] = {
  93. POWER_SUPPLY_PROP_ONLINE,
  94. };
  95. static enum power_supply_property test_power_battery_props[] = {
  96. POWER_SUPPLY_PROP_STATUS,
  97. POWER_SUPPLY_PROP_CHARGE_TYPE,
  98. POWER_SUPPLY_PROP_HEALTH,
  99. POWER_SUPPLY_PROP_PRESENT,
  100. POWER_SUPPLY_PROP_TECHNOLOGY,
  101. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  102. POWER_SUPPLY_PROP_CHARGE_FULL,
  103. POWER_SUPPLY_PROP_CHARGE_NOW,
  104. POWER_SUPPLY_PROP_CAPACITY,
  105. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  106. POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
  107. POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
  108. POWER_SUPPLY_PROP_MODEL_NAME,
  109. POWER_SUPPLY_PROP_MANUFACTURER,
  110. POWER_SUPPLY_PROP_SERIAL_NUMBER,
  111. };
  112. static char *test_power_ac_supplied_to[] = {
  113. "test_battery",
  114. };
  115. static struct power_supply test_power_supplies[] = {
  116. {
  117. .name = "test_ac",
  118. .type = POWER_SUPPLY_TYPE_MAINS,
  119. .supplied_to = test_power_ac_supplied_to,
  120. .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to),
  121. .properties = test_power_ac_props,
  122. .num_properties = ARRAY_SIZE(test_power_ac_props),
  123. .get_property = test_power_get_ac_property,
  124. }, {
  125. .name = "test_battery",
  126. .type = POWER_SUPPLY_TYPE_BATTERY,
  127. .properties = test_power_battery_props,
  128. .num_properties = ARRAY_SIZE(test_power_battery_props),
  129. .get_property = test_power_get_battery_property,
  130. },
  131. };
  132. static int __init test_power_init(void)
  133. {
  134. int i;
  135. int ret;
  136. for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++) {
  137. ret = power_supply_register(NULL, &test_power_supplies[i]);
  138. if (ret) {
  139. pr_err("%s: failed to register %s\n", __func__,
  140. test_power_supplies[i].name);
  141. goto failed;
  142. }
  143. }
  144. return 0;
  145. failed:
  146. while (--i >= 0)
  147. power_supply_unregister(&test_power_supplies[i]);
  148. return ret;
  149. }
  150. module_init(test_power_init);
  151. static void __exit test_power_exit(void)
  152. {
  153. int i;
  154. /* Let's see how we handle changes... */
  155. ac_online = 0;
  156. battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
  157. for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++)
  158. power_supply_changed(&test_power_supplies[i]);
  159. pr_info("%s: 'changed' event sent, sleeping for 10 seconds...\n",
  160. __func__);
  161. ssleep(10);
  162. for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++)
  163. power_supply_unregister(&test_power_supplies[i]);
  164. }
  165. module_exit(test_power_exit);
  166. #define MAX_KEYLENGTH 256
  167. struct battery_property_map {
  168. int value;
  169. char const *key;
  170. };
  171. static struct battery_property_map map_ac_online[] = {
  172. { 0, "on" },
  173. { 1, "off" },
  174. { -1, NULL },
  175. };
  176. static struct battery_property_map map_status[] = {
  177. { POWER_SUPPLY_STATUS_CHARGING, "charging" },
  178. { POWER_SUPPLY_STATUS_DISCHARGING, "discharging" },
  179. { POWER_SUPPLY_STATUS_NOT_CHARGING, "not-charging" },
  180. { POWER_SUPPLY_STATUS_FULL, "full" },
  181. { -1, NULL },
  182. };
  183. static struct battery_property_map map_health[] = {
  184. { POWER_SUPPLY_HEALTH_GOOD, "good" },
  185. { POWER_SUPPLY_HEALTH_OVERHEAT, "overheat" },
  186. { POWER_SUPPLY_HEALTH_DEAD, "dead" },
  187. { POWER_SUPPLY_HEALTH_OVERVOLTAGE, "overvoltage" },
  188. { POWER_SUPPLY_HEALTH_UNSPEC_FAILURE, "failure" },
  189. { -1, NULL },
  190. };
  191. static struct battery_property_map map_present[] = {
  192. { 0, "false" },
  193. { 1, "true" },
  194. { -1, NULL },
  195. };
  196. static struct battery_property_map map_technology[] = {
  197. { POWER_SUPPLY_TECHNOLOGY_NiMH, "NiMH" },
  198. { POWER_SUPPLY_TECHNOLOGY_LION, "LION" },
  199. { POWER_SUPPLY_TECHNOLOGY_LIPO, "LIPO" },
  200. { POWER_SUPPLY_TECHNOLOGY_LiFe, "LiFe" },
  201. { POWER_SUPPLY_TECHNOLOGY_NiCd, "NiCd" },
  202. { POWER_SUPPLY_TECHNOLOGY_LiMn, "LiMn" },
  203. { -1, NULL },
  204. };
  205. static int map_get_value(struct battery_property_map *map, const char *key,
  206. int def_val)
  207. {
  208. char buf[MAX_KEYLENGTH];
  209. int cr;
  210. strncpy(buf, key, MAX_KEYLENGTH);
  211. buf[MAX_KEYLENGTH-1] = '\0';
  212. cr = strnlen(buf, MAX_KEYLENGTH) - 1;
  213. if (buf[cr] == '\n')
  214. buf[cr] = '\0';
  215. while (map->key) {
  216. if (strncasecmp(map->key, buf, MAX_KEYLENGTH) == 0)
  217. return map->value;
  218. map++;
  219. }
  220. return def_val;
  221. }
  222. static const char *map_get_key(struct battery_property_map *map, int value,
  223. const char *def_key)
  224. {
  225. while (map->key) {
  226. if (map->value == value)
  227. return map->key;
  228. map++;
  229. }
  230. return def_key;
  231. }
  232. static int param_set_ac_online(const char *key, const struct kernel_param *kp)
  233. {
  234. ac_online = map_get_value(map_ac_online, key, ac_online);
  235. power_supply_changed(&test_power_supplies[0]);
  236. return 0;
  237. }
  238. static int param_get_ac_online(char *buffer, const struct kernel_param *kp)
  239. {
  240. strcpy(buffer, map_get_key(map_ac_online, ac_online, "unknown"));
  241. return strlen(buffer);
  242. }
  243. static int param_set_battery_status(const char *key,
  244. const struct kernel_param *kp)
  245. {
  246. battery_status = map_get_value(map_status, key, battery_status);
  247. power_supply_changed(&test_power_supplies[1]);
  248. return 0;
  249. }
  250. static int param_get_battery_status(char *buffer, const struct kernel_param *kp)
  251. {
  252. strcpy(buffer, map_get_key(map_status, battery_status, "unknown"));
  253. return strlen(buffer);
  254. }
  255. static int param_set_battery_health(const char *key,
  256. const struct kernel_param *kp)
  257. {
  258. battery_health = map_get_value(map_health, key, battery_health);
  259. power_supply_changed(&test_power_supplies[1]);
  260. return 0;
  261. }
  262. static int param_get_battery_health(char *buffer, const struct kernel_param *kp)
  263. {
  264. strcpy(buffer, map_get_key(map_health, battery_health, "unknown"));
  265. return strlen(buffer);
  266. }
  267. static int param_set_battery_present(const char *key,
  268. const struct kernel_param *kp)
  269. {
  270. battery_present = map_get_value(map_present, key, battery_present);
  271. power_supply_changed(&test_power_supplies[0]);
  272. return 0;
  273. }
  274. static int param_get_battery_present(char *buffer,
  275. const struct kernel_param *kp)
  276. {
  277. strcpy(buffer, map_get_key(map_present, battery_present, "unknown"));
  278. return strlen(buffer);
  279. }
  280. static int param_set_battery_technology(const char *key,
  281. const struct kernel_param *kp)
  282. {
  283. battery_technology = map_get_value(map_technology, key,
  284. battery_technology);
  285. power_supply_changed(&test_power_supplies[1]);
  286. return 0;
  287. }
  288. static int param_get_battery_technology(char *buffer,
  289. const struct kernel_param *kp)
  290. {
  291. strcpy(buffer,
  292. map_get_key(map_technology, battery_technology, "unknown"));
  293. return strlen(buffer);
  294. }
  295. static int param_set_battery_capacity(const char *key,
  296. const struct kernel_param *kp)
  297. {
  298. int tmp;
  299. if (1 != sscanf(key, "%d", &tmp))
  300. return -EINVAL;
  301. battery_capacity = tmp;
  302. power_supply_changed(&test_power_supplies[1]);
  303. return 0;
  304. }
  305. #define param_get_battery_capacity param_get_int
  306. static struct kernel_param_ops param_ops_ac_online = {
  307. .set = param_set_ac_online,
  308. .get = param_get_ac_online,
  309. };
  310. static struct kernel_param_ops param_ops_battery_status = {
  311. .set = param_set_battery_status,
  312. .get = param_get_battery_status,
  313. };
  314. static struct kernel_param_ops param_ops_battery_present = {
  315. .set = param_set_battery_present,
  316. .get = param_get_battery_present,
  317. };
  318. static struct kernel_param_ops param_ops_battery_technology = {
  319. .set = param_set_battery_technology,
  320. .get = param_get_battery_technology,
  321. };
  322. static struct kernel_param_ops param_ops_battery_health = {
  323. .set = param_set_battery_health,
  324. .get = param_get_battery_health,
  325. };
  326. static struct kernel_param_ops param_ops_battery_capacity = {
  327. .set = param_set_battery_capacity,
  328. .get = param_get_battery_capacity,
  329. };
  330. #define param_check_ac_online(name, p) __param_check(name, p, void);
  331. #define param_check_battery_status(name, p) __param_check(name, p, void);
  332. #define param_check_battery_present(name, p) __param_check(name, p, void);
  333. #define param_check_battery_technology(name, p) __param_check(name, p, void);
  334. #define param_check_battery_health(name, p) __param_check(name, p, void);
  335. #define param_check_battery_capacity(name, p) __param_check(name, p, void);
  336. module_param(ac_online, ac_online, 0644);
  337. MODULE_PARM_DESC(ac_online, "AC charging state <on|off>");
  338. module_param(battery_status, battery_status, 0644);
  339. MODULE_PARM_DESC(battery_status,
  340. "battery status <charging|discharging|not-charging|full>");
  341. module_param(battery_present, battery_present, 0644);
  342. MODULE_PARM_DESC(battery_present,
  343. "battery presence state <good|overheat|dead|overvoltage|failure>");
  344. module_param(battery_technology, battery_technology, 0644);
  345. MODULE_PARM_DESC(battery_technology,
  346. "battery technology <NiMH|LION|LIPO|LiFe|NiCd|LiMn>");
  347. module_param(battery_health, battery_health, 0644);
  348. MODULE_PARM_DESC(battery_health,
  349. "battery health state <good|overheat|dead|overvoltage|failure>");
  350. module_param(battery_capacity, battery_capacity, 0644);
  351. MODULE_PARM_DESC(battery_capacity, "battery capacity (percentage)");
  352. MODULE_DESCRIPTION("Power supply driver for testing");
  353. MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>");
  354. MODULE_LICENSE("GPL");