test_power.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Power supply driver for testing.
  3. *
  4. * Copyright 2010 Anton Vorontsov <cbouatmailru@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/power_supply.h>
  13. #include <linux/errno.h>
  14. #include <linux/delay.h>
  15. #include <linux/vermagic.h>
  16. static int test_power_ac_online = 1;
  17. static int test_power_battery_status = POWER_SUPPLY_STATUS_CHARGING;
  18. static int test_power_get_ac_property(struct power_supply *psy,
  19. enum power_supply_property psp,
  20. union power_supply_propval *val)
  21. {
  22. switch (psp) {
  23. case POWER_SUPPLY_PROP_ONLINE:
  24. val->intval = test_power_ac_online;
  25. break;
  26. default:
  27. return -EINVAL;
  28. }
  29. return 0;
  30. }
  31. static int test_power_get_battery_property(struct power_supply *psy,
  32. enum power_supply_property psp,
  33. union power_supply_propval *val)
  34. {
  35. switch (psp) {
  36. case POWER_SUPPLY_PROP_MODEL_NAME:
  37. val->strval = "Test battery";
  38. break;
  39. case POWER_SUPPLY_PROP_MANUFACTURER:
  40. val->strval = "Linux";
  41. break;
  42. case POWER_SUPPLY_PROP_SERIAL_NUMBER:
  43. val->strval = UTS_RELEASE;
  44. break;
  45. case POWER_SUPPLY_PROP_STATUS:
  46. val->intval = test_power_battery_status;
  47. break;
  48. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  49. val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
  50. break;
  51. case POWER_SUPPLY_PROP_HEALTH:
  52. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  53. break;
  54. case POWER_SUPPLY_PROP_TECHNOLOGY:
  55. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  56. break;
  57. case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
  58. val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
  59. break;
  60. case POWER_SUPPLY_PROP_CAPACITY:
  61. val->intval = 50;
  62. break;
  63. case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
  64. case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
  65. val->intval = 3600;
  66. break;
  67. default:
  68. pr_info("%s: some properties deliberately report errors.\n",
  69. __func__);
  70. return -EINVAL;
  71. }
  72. return 0;
  73. }
  74. static enum power_supply_property test_power_ac_props[] = {
  75. POWER_SUPPLY_PROP_ONLINE,
  76. };
  77. static enum power_supply_property test_power_battery_props[] = {
  78. POWER_SUPPLY_PROP_STATUS,
  79. POWER_SUPPLY_PROP_CHARGE_TYPE,
  80. POWER_SUPPLY_PROP_HEALTH,
  81. POWER_SUPPLY_PROP_TECHNOLOGY,
  82. POWER_SUPPLY_PROP_CHARGE_FULL,
  83. POWER_SUPPLY_PROP_CHARGE_EMPTY,
  84. POWER_SUPPLY_PROP_CAPACITY,
  85. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  86. POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
  87. POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
  88. POWER_SUPPLY_PROP_MODEL_NAME,
  89. POWER_SUPPLY_PROP_MANUFACTURER,
  90. POWER_SUPPLY_PROP_SERIAL_NUMBER,
  91. };
  92. static char *test_power_ac_supplied_to[] = {
  93. "test_battery",
  94. };
  95. static struct power_supply test_power_supplies[] = {
  96. {
  97. .name = "test_ac",
  98. .type = POWER_SUPPLY_TYPE_MAINS,
  99. .supplied_to = test_power_ac_supplied_to,
  100. .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to),
  101. .properties = test_power_ac_props,
  102. .num_properties = ARRAY_SIZE(test_power_ac_props),
  103. .get_property = test_power_get_ac_property,
  104. }, {
  105. .name = "test_battery",
  106. .type = POWER_SUPPLY_TYPE_BATTERY,
  107. .properties = test_power_battery_props,
  108. .num_properties = ARRAY_SIZE(test_power_battery_props),
  109. .get_property = test_power_get_battery_property,
  110. },
  111. };
  112. static int __init test_power_init(void)
  113. {
  114. int i;
  115. int ret;
  116. for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++) {
  117. ret = power_supply_register(NULL, &test_power_supplies[i]);
  118. if (ret) {
  119. pr_err("%s: failed to register %s\n", __func__,
  120. test_power_supplies[i].name);
  121. goto failed;
  122. }
  123. }
  124. return 0;
  125. failed:
  126. while (--i >= 0)
  127. power_supply_unregister(&test_power_supplies[i]);
  128. return ret;
  129. }
  130. module_init(test_power_init);
  131. static void __exit test_power_exit(void)
  132. {
  133. int i;
  134. /* Let's see how we handle changes... */
  135. test_power_ac_online = 0;
  136. test_power_battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
  137. for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++)
  138. power_supply_changed(&test_power_supplies[i]);
  139. pr_info("%s: 'changed' event sent, sleeping for 10 seconds...\n",
  140. __func__);
  141. ssleep(10);
  142. for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++)
  143. power_supply_unregister(&test_power_supplies[i]);
  144. }
  145. module_exit(test_power_exit);
  146. MODULE_DESCRIPTION("Power supply driver for testing");
  147. MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>");
  148. MODULE_LICENSE("GPL");