power_supply_core.c 3.7 KB

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