power_supply_core.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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", __FUNCTION__);
  37. class_for_each_device(power_supply_class, 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", __FUNCTION__);
  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, psy,
  68. __power_supply_am_i_supplied);
  69. dev_dbg(psy->dev, "%s %d\n", __FUNCTION__, 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(power_supply_class, parent, 0,
  76. "%s", psy->name);
  77. if (IS_ERR(psy->dev)) {
  78. rc = PTR_ERR(psy->dev);
  79. goto dev_create_failed;
  80. }
  81. dev_set_drvdata(psy->dev, psy);
  82. INIT_WORK(&psy->changed_work, power_supply_changed_work);
  83. rc = power_supply_create_attrs(psy);
  84. if (rc)
  85. goto create_attrs_failed;
  86. rc = power_supply_create_triggers(psy);
  87. if (rc)
  88. goto create_triggers_failed;
  89. power_supply_changed(psy);
  90. goto success;
  91. create_triggers_failed:
  92. power_supply_remove_attrs(psy);
  93. create_attrs_failed:
  94. device_unregister(psy->dev);
  95. dev_create_failed:
  96. success:
  97. return rc;
  98. }
  99. void power_supply_unregister(struct power_supply *psy)
  100. {
  101. flush_scheduled_work();
  102. power_supply_remove_triggers(psy);
  103. power_supply_remove_attrs(psy);
  104. device_unregister(psy->dev);
  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. }
  118. EXPORT_SYMBOL_GPL(power_supply_changed);
  119. EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
  120. EXPORT_SYMBOL_GPL(power_supply_register);
  121. EXPORT_SYMBOL_GPL(power_supply_unregister);
  122. /* exported for the APM Power driver, APM emulation */
  123. EXPORT_SYMBOL_GPL(power_supply_class);
  124. subsys_initcall(power_supply_class_init);
  125. module_exit(power_supply_class_exit);
  126. MODULE_DESCRIPTION("Universal power supply monitor class");
  127. MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
  128. "Szabolcs Gyurko, "
  129. "Anton Vorontsov <cbou@mail.ru>");
  130. MODULE_LICENSE("GPL");