power_supply: Add core support for supplied_from
authorRhyland Klein <rklein@nvidia.com>
Mon, 1 Apr 2013 21:45:54 +0000 (17:45 -0400)
committerAnton Vorontsov <anton@enomsg.org>
Wed, 17 Apr 2013 01:35:31 +0000 (18:35 -0700)
This patch adds support for supplies to register a list of char *'s which
represent the list of supplies which supply them. This is the opposite as
the supplied_to list.

This change maintains support for supplied_to until all drivers which make
use of it already are converted.

Signed-off-by: Rhyland Klein <rklein@nvidia.com>
Reviewed-by: Stephen Warren <swarren@nvidia.com>
Signed-off-by: Anton Vorontsov <anton@enomsg.org>
drivers/power/power_supply_core.c
include/linux/power_supply.h

index 5deac432e2ae7b2025bc2e985ec5b6da6dff214d..d843cc9df0307210cd9234252cc4d9101814f565 100644 (file)
@@ -26,17 +26,42 @@ EXPORT_SYMBOL_GPL(power_supply_class);
 
 static struct device_type power_supply_dev_type;
 
+static bool __power_supply_is_supplied_by(struct power_supply *supplier,
+                                        struct power_supply *supply)
+{
+       int i;
+
+       if (!supply->supplied_from && !supplier->supplied_to)
+               return false;
+
+       /* Support both supplied_to and supplied_from modes */
+       if (supply->supplied_from) {
+               if (!supplier->name)
+                       return false;
+               for (i = 0; i < supply->num_supplies; i++)
+                       if (!strcmp(supplier->name, supply->supplied_from[i]))
+                               return true;
+       } else {
+               if (!supply->name)
+                       return false;
+               for (i = 0; i < supplier->num_supplicants; i++)
+                       if (!strcmp(supplier->supplied_to[i], supply->name))
+                               return true;
+       }
+
+       return false;
+}
+
 static int __power_supply_changed_work(struct device *dev, void *data)
 {
        struct power_supply *psy = (struct power_supply *)data;
        struct power_supply *pst = dev_get_drvdata(dev);
-       int i;
 
-       for (i = 0; i < psy->num_supplicants; i++)
-               if (!strcmp(psy->supplied_to[i], pst->name)) {
-                       if (pst->external_power_changed)
-                               pst->external_power_changed(pst);
-               }
+       if (__power_supply_is_supplied_by(psy, pst)) {
+               if (pst->external_power_changed)
+                       pst->external_power_changed(pst);
+       }
+
        return 0;
 }
 
@@ -68,17 +93,13 @@ static int __power_supply_am_i_supplied(struct device *dev, void *data)
        union power_supply_propval ret = {0,};
        struct power_supply *psy = (struct power_supply *)data;
        struct power_supply *epsy = dev_get_drvdata(dev);
-       int i;
 
-       for (i = 0; i < epsy->num_supplicants; i++) {
-               if (!strcmp(epsy->supplied_to[i], psy->name)) {
-                       if (epsy->get_property(epsy,
-                                 POWER_SUPPLY_PROP_ONLINE, &ret))
-                               continue;
+       if (__power_supply_is_supplied_by(epsy, psy))
+               if (!epsy->get_property(epsy, POWER_SUPPLY_PROP_ONLINE, &ret)) {
                        if (ret.intval)
                                return ret.intval;
                }
-       }
+
        return 0;
 }
 
index 002a99f96331dd2f7e939ae24713db9c351c0fff..c1cbd5e4e48452cc82aa7e9c8c598c8ac163d9fd 100644 (file)
@@ -171,6 +171,9 @@ struct power_supply {
        char **supplied_to;
        size_t num_supplicants;
 
+       char **supplied_from;
+       size_t num_supplies;
+
        int (*get_property)(struct power_supply *psy,
                            enum power_supply_property psp,
                            union power_supply_propval *val);