power_supply_core.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 void power_supply_changed_work(struct work_struct *work)
  21. {
  22. struct power_supply *psy = container_of(work, struct power_supply,
  23. changed_work);
  24. int i;
  25. dev_dbg(psy->dev, "%s\n", __FUNCTION__);
  26. for (i = 0; i < psy->num_supplicants; i++) {
  27. struct device *dev;
  28. down(&power_supply_class->sem);
  29. list_for_each_entry(dev, &power_supply_class->devices, node) {
  30. struct power_supply *pst = dev_get_drvdata(dev);
  31. if (!strcmp(psy->supplied_to[i], pst->name)) {
  32. if (pst->external_power_changed)
  33. pst->external_power_changed(pst);
  34. }
  35. }
  36. up(&power_supply_class->sem);
  37. }
  38. power_supply_update_leds(psy);
  39. kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE);
  40. }
  41. void power_supply_changed(struct power_supply *psy)
  42. {
  43. dev_dbg(psy->dev, "%s\n", __FUNCTION__);
  44. schedule_work(&psy->changed_work);
  45. }
  46. int power_supply_am_i_supplied(struct power_supply *psy)
  47. {
  48. union power_supply_propval ret = {0,};
  49. struct device *dev;
  50. down(&power_supply_class->sem);
  51. list_for_each_entry(dev, &power_supply_class->devices, node) {
  52. struct power_supply *epsy = dev_get_drvdata(dev);
  53. int i;
  54. for (i = 0; i < epsy->num_supplicants; i++) {
  55. if (!strcmp(epsy->supplied_to[i], psy->name)) {
  56. if (epsy->get_property(epsy,
  57. POWER_SUPPLY_PROP_ONLINE, &ret))
  58. continue;
  59. if (ret.intval)
  60. goto out;
  61. }
  62. }
  63. }
  64. out:
  65. up(&power_supply_class->sem);
  66. dev_dbg(psy->dev, "%s %d\n", __FUNCTION__, ret.intval);
  67. return ret.intval;
  68. }
  69. int power_supply_register(struct device *parent, struct power_supply *psy)
  70. {
  71. int rc = 0;
  72. psy->dev = device_create(power_supply_class, parent, 0,
  73. "%s", psy->name);
  74. if (IS_ERR(psy->dev)) {
  75. rc = PTR_ERR(psy->dev);
  76. goto dev_create_failed;
  77. }
  78. dev_set_drvdata(psy->dev, psy);
  79. INIT_WORK(&psy->changed_work, power_supply_changed_work);
  80. rc = power_supply_create_attrs(psy);
  81. if (rc)
  82. goto create_attrs_failed;
  83. rc = power_supply_create_triggers(psy);
  84. if (rc)
  85. goto create_triggers_failed;
  86. power_supply_changed(psy);
  87. goto success;
  88. create_triggers_failed:
  89. power_supply_remove_attrs(psy);
  90. create_attrs_failed:
  91. device_unregister(psy->dev);
  92. dev_create_failed:
  93. success:
  94. return rc;
  95. }
  96. void power_supply_unregister(struct power_supply *psy)
  97. {
  98. flush_scheduled_work();
  99. power_supply_remove_triggers(psy);
  100. power_supply_remove_attrs(psy);
  101. device_unregister(psy->dev);
  102. }
  103. static int __init power_supply_class_init(void)
  104. {
  105. power_supply_class = class_create(THIS_MODULE, "power_supply");
  106. if (IS_ERR(power_supply_class))
  107. return PTR_ERR(power_supply_class);
  108. power_supply_class->dev_uevent = power_supply_uevent;
  109. return 0;
  110. }
  111. static void __exit power_supply_class_exit(void)
  112. {
  113. class_destroy(power_supply_class);
  114. }
  115. EXPORT_SYMBOL_GPL(power_supply_changed);
  116. EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
  117. EXPORT_SYMBOL_GPL(power_supply_register);
  118. EXPORT_SYMBOL_GPL(power_supply_unregister);
  119. /* exported for the APM Power driver, APM emulation */
  120. EXPORT_SYMBOL_GPL(power_supply_class);
  121. subsys_initcall(power_supply_class_init);
  122. module_exit(power_supply_class_exit);
  123. MODULE_DESCRIPTION("Universal power supply monitor class");
  124. MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
  125. "Szabolcs Gyurko, "
  126. "Anton Vorontsov <cbou@mail.ru>");
  127. MODULE_LICENSE("GPL");