test_power.c 13 KB

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