test_power.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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[] = {
  146. [TEST_AC] = {
  147. .name = "test_ac",
  148. .type = POWER_SUPPLY_TYPE_MAINS,
  149. .supplied_to = test_power_ac_supplied_to,
  150. .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to),
  151. .properties = test_power_ac_props,
  152. .num_properties = ARRAY_SIZE(test_power_ac_props),
  153. .get_property = test_power_get_ac_property,
  154. },
  155. [TEST_BATTERY] = {
  156. .name = "test_battery",
  157. .type = POWER_SUPPLY_TYPE_BATTERY,
  158. .properties = test_power_battery_props,
  159. .num_properties = ARRAY_SIZE(test_power_battery_props),
  160. .get_property = test_power_get_battery_property,
  161. },
  162. [TEST_USB] = {
  163. .name = "test_usb",
  164. .type = POWER_SUPPLY_TYPE_USB,
  165. .supplied_to = test_power_ac_supplied_to,
  166. .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to),
  167. .properties = test_power_ac_props,
  168. .num_properties = ARRAY_SIZE(test_power_ac_props),
  169. .get_property = test_power_get_usb_property,
  170. },
  171. };
  172. static int __init test_power_init(void)
  173. {
  174. int i;
  175. int ret;
  176. BUILD_BUG_ON(TEST_POWER_NUM != ARRAY_SIZE(test_power_supplies));
  177. for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++) {
  178. ret = power_supply_register(NULL, &test_power_supplies[i]);
  179. if (ret) {
  180. pr_err("%s: failed to register %s\n", __func__,
  181. test_power_supplies[i].name);
  182. goto failed;
  183. }
  184. }
  185. module_initialized = true;
  186. return 0;
  187. failed:
  188. while (--i >= 0)
  189. power_supply_unregister(&test_power_supplies[i]);
  190. return ret;
  191. }
  192. module_init(test_power_init);
  193. static void __exit test_power_exit(void)
  194. {
  195. int i;
  196. /* Let's see how we handle changes... */
  197. ac_online = 0;
  198. usb_online = 0;
  199. battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
  200. for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++)
  201. power_supply_changed(&test_power_supplies[i]);
  202. pr_info("%s: 'changed' event sent, sleeping for 10 seconds...\n",
  203. __func__);
  204. ssleep(10);
  205. for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++)
  206. power_supply_unregister(&test_power_supplies[i]);
  207. module_initialized = false;
  208. }
  209. module_exit(test_power_exit);
  210. #define MAX_KEYLENGTH 256
  211. struct battery_property_map {
  212. int value;
  213. char const *key;
  214. };
  215. static struct battery_property_map map_ac_online[] = {
  216. { 0, "off" },
  217. { 1, "on" },
  218. { -1, NULL },
  219. };
  220. static struct battery_property_map map_status[] = {
  221. { POWER_SUPPLY_STATUS_CHARGING, "charging" },
  222. { POWER_SUPPLY_STATUS_DISCHARGING, "discharging" },
  223. { POWER_SUPPLY_STATUS_NOT_CHARGING, "not-charging" },
  224. { POWER_SUPPLY_STATUS_FULL, "full" },
  225. { -1, NULL },
  226. };
  227. static struct battery_property_map map_health[] = {
  228. { POWER_SUPPLY_HEALTH_GOOD, "good" },
  229. { POWER_SUPPLY_HEALTH_OVERHEAT, "overheat" },
  230. { POWER_SUPPLY_HEALTH_DEAD, "dead" },
  231. { POWER_SUPPLY_HEALTH_OVERVOLTAGE, "overvoltage" },
  232. { POWER_SUPPLY_HEALTH_UNSPEC_FAILURE, "failure" },
  233. { -1, NULL },
  234. };
  235. static struct battery_property_map map_present[] = {
  236. { 0, "false" },
  237. { 1, "true" },
  238. { -1, NULL },
  239. };
  240. static struct battery_property_map map_technology[] = {
  241. { POWER_SUPPLY_TECHNOLOGY_NiMH, "NiMH" },
  242. { POWER_SUPPLY_TECHNOLOGY_LION, "LION" },
  243. { POWER_SUPPLY_TECHNOLOGY_LIPO, "LIPO" },
  244. { POWER_SUPPLY_TECHNOLOGY_LiFe, "LiFe" },
  245. { POWER_SUPPLY_TECHNOLOGY_NiCd, "NiCd" },
  246. { POWER_SUPPLY_TECHNOLOGY_LiMn, "LiMn" },
  247. { -1, NULL },
  248. };
  249. static int map_get_value(struct battery_property_map *map, const char *key,
  250. int def_val)
  251. {
  252. char buf[MAX_KEYLENGTH];
  253. int cr;
  254. strncpy(buf, key, MAX_KEYLENGTH);
  255. buf[MAX_KEYLENGTH-1] = '\0';
  256. cr = strnlen(buf, MAX_KEYLENGTH) - 1;
  257. if (buf[cr] == '\n')
  258. buf[cr] = '\0';
  259. while (map->key) {
  260. if (strncasecmp(map->key, buf, MAX_KEYLENGTH) == 0)
  261. return map->value;
  262. map++;
  263. }
  264. return def_val;
  265. }
  266. static const char *map_get_key(struct battery_property_map *map, int value,
  267. const char *def_key)
  268. {
  269. while (map->key) {
  270. if (map->value == value)
  271. return map->key;
  272. map++;
  273. }
  274. return def_key;
  275. }
  276. static inline void signal_power_supply_changed(struct power_supply *psy)
  277. {
  278. if (module_initialized)
  279. power_supply_changed(psy);
  280. }
  281. static int param_set_ac_online(const char *key, const struct kernel_param *kp)
  282. {
  283. ac_online = map_get_value(map_ac_online, key, ac_online);
  284. signal_power_supply_changed(&test_power_supplies[TEST_AC]);
  285. return 0;
  286. }
  287. static int param_get_ac_online(char *buffer, const struct kernel_param *kp)
  288. {
  289. strcpy(buffer, map_get_key(map_ac_online, ac_online, "unknown"));
  290. return strlen(buffer);
  291. }
  292. static int param_set_usb_online(const char *key, const struct kernel_param *kp)
  293. {
  294. usb_online = map_get_value(map_ac_online, key, usb_online);
  295. signal_power_supply_changed(&test_power_supplies[TEST_USB]);
  296. return 0;
  297. }
  298. static int param_get_usb_online(char *buffer, const struct kernel_param *kp)
  299. {
  300. strcpy(buffer, map_get_key(map_ac_online, usb_online, "unknown"));
  301. return strlen(buffer);
  302. }
  303. static int param_set_battery_status(const char *key,
  304. const struct kernel_param *kp)
  305. {
  306. battery_status = map_get_value(map_status, key, battery_status);
  307. signal_power_supply_changed(&test_power_supplies[TEST_BATTERY]);
  308. return 0;
  309. }
  310. static int param_get_battery_status(char *buffer, const struct kernel_param *kp)
  311. {
  312. strcpy(buffer, map_get_key(map_status, battery_status, "unknown"));
  313. return strlen(buffer);
  314. }
  315. static int param_set_battery_health(const char *key,
  316. const struct kernel_param *kp)
  317. {
  318. battery_health = map_get_value(map_health, key, battery_health);
  319. signal_power_supply_changed(&test_power_supplies[TEST_BATTERY]);
  320. return 0;
  321. }
  322. static int param_get_battery_health(char *buffer, const struct kernel_param *kp)
  323. {
  324. strcpy(buffer, map_get_key(map_health, battery_health, "unknown"));
  325. return strlen(buffer);
  326. }
  327. static int param_set_battery_present(const char *key,
  328. const struct kernel_param *kp)
  329. {
  330. battery_present = map_get_value(map_present, key, battery_present);
  331. signal_power_supply_changed(&test_power_supplies[TEST_AC]);
  332. return 0;
  333. }
  334. static int param_get_battery_present(char *buffer,
  335. const struct kernel_param *kp)
  336. {
  337. strcpy(buffer, map_get_key(map_present, battery_present, "unknown"));
  338. return strlen(buffer);
  339. }
  340. static int param_set_battery_technology(const char *key,
  341. const struct kernel_param *kp)
  342. {
  343. battery_technology = map_get_value(map_technology, key,
  344. battery_technology);
  345. signal_power_supply_changed(&test_power_supplies[TEST_BATTERY]);
  346. return 0;
  347. }
  348. static int param_get_battery_technology(char *buffer,
  349. const struct kernel_param *kp)
  350. {
  351. strcpy(buffer,
  352. map_get_key(map_technology, battery_technology, "unknown"));
  353. return strlen(buffer);
  354. }
  355. static int param_set_battery_capacity(const char *key,
  356. const struct kernel_param *kp)
  357. {
  358. int tmp;
  359. if (1 != sscanf(key, "%d", &tmp))
  360. return -EINVAL;
  361. battery_capacity = tmp;
  362. signal_power_supply_changed(&test_power_supplies[TEST_BATTERY]);
  363. return 0;
  364. }
  365. #define param_get_battery_capacity param_get_int
  366. static int param_set_battery_voltage(const char *key,
  367. const struct kernel_param *kp)
  368. {
  369. int tmp;
  370. if (1 != sscanf(key, "%d", &tmp))
  371. return -EINVAL;
  372. battery_voltage = tmp;
  373. signal_power_supply_changed(&test_power_supplies[TEST_BATTERY]);
  374. return 0;
  375. }
  376. #define param_get_battery_voltage param_get_int
  377. static struct kernel_param_ops param_ops_ac_online = {
  378. .set = param_set_ac_online,
  379. .get = param_get_ac_online,
  380. };
  381. static struct kernel_param_ops param_ops_usb_online = {
  382. .set = param_set_usb_online,
  383. .get = param_get_usb_online,
  384. };
  385. static struct kernel_param_ops param_ops_battery_status = {
  386. .set = param_set_battery_status,
  387. .get = param_get_battery_status,
  388. };
  389. static struct kernel_param_ops param_ops_battery_present = {
  390. .set = param_set_battery_present,
  391. .get = param_get_battery_present,
  392. };
  393. static struct kernel_param_ops param_ops_battery_technology = {
  394. .set = param_set_battery_technology,
  395. .get = param_get_battery_technology,
  396. };
  397. static struct kernel_param_ops param_ops_battery_health = {
  398. .set = param_set_battery_health,
  399. .get = param_get_battery_health,
  400. };
  401. static struct kernel_param_ops param_ops_battery_capacity = {
  402. .set = param_set_battery_capacity,
  403. .get = param_get_battery_capacity,
  404. };
  405. static struct kernel_param_ops param_ops_battery_voltage = {
  406. .set = param_set_battery_voltage,
  407. .get = param_get_battery_voltage,
  408. };
  409. #define param_check_ac_online(name, p) __param_check(name, p, void);
  410. #define param_check_usb_online(name, p) __param_check(name, p, void);
  411. #define param_check_battery_status(name, p) __param_check(name, p, void);
  412. #define param_check_battery_present(name, p) __param_check(name, p, void);
  413. #define param_check_battery_technology(name, p) __param_check(name, p, void);
  414. #define param_check_battery_health(name, p) __param_check(name, p, void);
  415. #define param_check_battery_capacity(name, p) __param_check(name, p, void);
  416. #define param_check_battery_voltage(name, p) __param_check(name, p, void);
  417. module_param(ac_online, ac_online, 0644);
  418. MODULE_PARM_DESC(ac_online, "AC charging state <on|off>");
  419. module_param(usb_online, usb_online, 0644);
  420. MODULE_PARM_DESC(usb_online, "USB charging state <on|off>");
  421. module_param(battery_status, battery_status, 0644);
  422. MODULE_PARM_DESC(battery_status,
  423. "battery status <charging|discharging|not-charging|full>");
  424. module_param(battery_present, battery_present, 0644);
  425. MODULE_PARM_DESC(battery_present,
  426. "battery presence state <good|overheat|dead|overvoltage|failure>");
  427. module_param(battery_technology, battery_technology, 0644);
  428. MODULE_PARM_DESC(battery_technology,
  429. "battery technology <NiMH|LION|LIPO|LiFe|NiCd|LiMn>");
  430. module_param(battery_health, battery_health, 0644);
  431. MODULE_PARM_DESC(battery_health,
  432. "battery health state <good|overheat|dead|overvoltage|failure>");
  433. module_param(battery_capacity, battery_capacity, 0644);
  434. MODULE_PARM_DESC(battery_capacity, "battery capacity (percentage)");
  435. module_param(battery_voltage, battery_voltage, 0644);
  436. MODULE_PARM_DESC(battery_voltage, "battery voltage (millivolts)");
  437. MODULE_DESCRIPTION("Power supply driver for testing");
  438. MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>");
  439. MODULE_LICENSE("GPL");