power_supply_core.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Universal power supply monitor class
  3. *
  4. * Copyright © 2007 Anton Vorontsov <cbou@mail.ru>
  5. * Copyright © 2004 Szabolcs Gyurko
  6. * Copyright © 2003 Ian Molton <spyro@f2s.com>
  7. *
  8. * Modified: 2004, Oct Szabolcs Gyurko
  9. *
  10. * You may use this code as per GPL version 2
  11. */
  12. #include <linux/module.h>
  13. #include <linux/types.h>
  14. #include <linux/init.h>
  15. #include <linux/device.h>
  16. #include <linux/err.h>
  17. #include <linux/power_supply.h>
  18. #include "power_supply.h"
  19. struct class *power_supply_class;
  20. static int __power_supply_changed_work(struct device *dev, void *data)
  21. {
  22. struct power_supply *psy = (struct power_supply *)data;
  23. struct power_supply *pst = dev_get_drvdata(dev);
  24. int i;
  25. for (i = 0; i < psy->num_supplicants; i++)
  26. if (!strcmp(psy->supplied_to[i], pst->name)) {
  27. if (pst->external_power_changed)
  28. pst->external_power_changed(pst);
  29. }
  30. return 0;
  31. }
  32. static void power_supply_changed_work(struct work_struct *work)
  33. {
  34. struct power_supply *psy = container_of(work, struct power_supply,
  35. changed_work);
  36. dev_dbg(psy->dev, "%s\n", __func__);
  37. class_for_each_device(power_supply_class, NULL, psy,
  38. __power_supply_changed_work);
  39. power_supply_update_leds(psy);
  40. kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE);
  41. }
  42. void power_supply_changed(struct power_supply *psy)
  43. {
  44. dev_dbg(psy->dev, "%s\n", __func__);
  45. schedule_work(&psy->changed_work);
  46. }
  47. static int __power_supply_am_i_supplied(struct device *dev, void *data)
  48. {
  49. union power_supply_propval ret = {0,};
  50. struct power_supply *psy = (struct power_supply *)data;
  51. struct power_supply *epsy = dev_get_drvdata(dev);
  52. int i;
  53. for (i = 0; i < epsy->num_supplicants; i++) {
  54. if (!strcmp(epsy->supplied_to[i], psy->name)) {
  55. if (epsy->get_property(epsy,
  56. POWER_SUPPLY_PROP_ONLINE, &ret))
  57. continue;
  58. if (ret.intval)
  59. return ret.intval;
  60. }
  61. }
  62. return 0;
  63. }
  64. int power_supply_am_i_supplied(struct power_supply *psy)
  65. {
  66. int error;
  67. error = class_for_each_device(power_supply_class, NULL, psy,
  68. __power_supply_am_i_supplied);
  69. dev_dbg(psy->dev, "%s %d\n", __func__, error);
  70. return error;
  71. }
  72. static int __power_supply_is_system_supplied(struct device *dev, void *data)
  73. {
  74. union power_supply_propval ret = {0,};
  75. struct power_supply *psy = dev_get_drvdata(dev);
  76. if (psy->type != POWER_SUPPLY_TYPE_BATTERY) {
  77. if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret))
  78. return 0;
  79. if (ret.intval)
  80. return ret.intval;
  81. }
  82. return 0;
  83. }
  84. int power_supply_is_system_supplied(void)
  85. {
  86. int error;
  87. error = class_for_each_device(power_supply_class, NULL, NULL,
  88. __power_supply_is_system_supplied);
  89. return error;
  90. }
  91. int power_supply_register(struct device *parent, struct power_supply *psy)
  92. {
  93. int rc = 0;
  94. psy->dev = device_create(power_supply_class, parent, 0, psy,
  95. "%s", psy->name);
  96. if (IS_ERR(psy->dev)) {
  97. rc = PTR_ERR(psy->dev);
  98. goto dev_create_failed;
  99. }
  100. INIT_WORK(&psy->changed_work, power_supply_changed_work);
  101. rc = power_supply_create_attrs(psy);
  102. if (rc)
  103. goto create_attrs_failed;
  104. rc = power_supply_create_triggers(psy);
  105. if (rc)
  106. goto create_triggers_failed;
  107. power_supply_changed(psy);
  108. goto success;
  109. create_triggers_failed:
  110. power_supply_remove_attrs(psy);
  111. create_attrs_failed:
  112. device_unregister(psy->dev);
  113. dev_create_failed:
  114. success:
  115. return rc;
  116. }
  117. void power_supply_unregister(struct power_supply *psy)
  118. {
  119. flush_scheduled_work();
  120. power_supply_remove_triggers(psy);
  121. power_supply_remove_attrs(psy);
  122. device_unregister(psy->dev);
  123. }
  124. static int __init power_supply_class_init(void)
  125. {
  126. power_supply_class = class_create(THIS_MODULE, "power_supply");
  127. if (IS_ERR(power_supply_class))
  128. return PTR_ERR(power_supply_class);
  129. power_supply_class->dev_uevent = power_supply_uevent;
  130. return 0;
  131. }
  132. static void __exit power_supply_class_exit(void)
  133. {
  134. class_destroy(power_supply_class);
  135. }
  136. EXPORT_SYMBOL_GPL(power_supply_changed);
  137. EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
  138. EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
  139. EXPORT_SYMBOL_GPL(power_supply_register);
  140. EXPORT_SYMBOL_GPL(power_supply_unregister);
  141. /* exported for the APM Power driver, APM emulation */
  142. EXPORT_SYMBOL_GPL(power_supply_class);
  143. subsys_initcall(power_supply_class_init);
  144. module_exit(power_supply_class_exit);
  145. MODULE_DESCRIPTION("Universal power supply monitor class");
  146. MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
  147. "Szabolcs Gyurko, "
  148. "Anton Vorontsov <cbou@mail.ru>");
  149. MODULE_LICENSE("GPL");