power_supply_core.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. int power_supply_register(struct device *parent, struct power_supply *psy)
  73. {
  74. int rc = 0;
  75. psy->dev = device_create_drvdata(power_supply_class, parent, 0,
  76. psy, "%s", psy->name);
  77. if (IS_ERR(psy->dev)) {
  78. rc = PTR_ERR(psy->dev);
  79. goto dev_create_failed;
  80. }
  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. }
  105. static int __init power_supply_class_init(void)
  106. {
  107. power_supply_class = class_create(THIS_MODULE, "power_supply");
  108. if (IS_ERR(power_supply_class))
  109. return PTR_ERR(power_supply_class);
  110. power_supply_class->dev_uevent = power_supply_uevent;
  111. return 0;
  112. }
  113. static void __exit power_supply_class_exit(void)
  114. {
  115. class_destroy(power_supply_class);
  116. }
  117. EXPORT_SYMBOL_GPL(power_supply_changed);
  118. EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
  119. EXPORT_SYMBOL_GPL(power_supply_register);
  120. EXPORT_SYMBOL_GPL(power_supply_unregister);
  121. /* exported for the APM Power driver, APM emulation */
  122. EXPORT_SYMBOL_GPL(power_supply_class);
  123. subsys_initcall(power_supply_class_init);
  124. module_exit(power_supply_class_exit);
  125. MODULE_DESCRIPTION("Universal power supply monitor class");
  126. MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
  127. "Szabolcs Gyurko, "
  128. "Anton Vorontsov <cbou@mail.ru>");
  129. MODULE_LICENSE("GPL");