Commit 9ade6d31 authored by Alex Elder's avatar Alex Elder Committed by Greg Kroah-Hartman

greybus: battery: free struct on error in caller

When a battery connection is initialized, a gb_battery structure for
it is allocated in gb_battery_connection_init().  Currently that
function ends by calling init_and_register(); in the event an error
occurs, init_and_register() is responsible for freeing the allocated
gb_battery structure.

Make the code a bit better balanced by having the function that
allocates the structure be responsible for freeing it in case of
error.
Signed-off-by: default avatarAlex Elder <elder@linaro.org>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@google.com>
parent c13c8bf0
...@@ -299,8 +299,6 @@ static enum power_supply_property battery_props[] = { ...@@ -299,8 +299,6 @@ static enum power_supply_property battery_props[] = {
static int init_and_register(struct gb_connection *connection, static int init_and_register(struct gb_connection *connection,
struct gb_battery *gb) struct gb_battery *gb)
{ {
int retval;
// FIXME - get a better (i.e. unique) name // FIXME - get a better (i.e. unique) name
// FIXME - anything else needs to be set? // FIXME - anything else needs to be set?
gb->bat.name = "gb_battery"; gb->bat.name = "gb_battery";
...@@ -309,18 +307,13 @@ static int init_and_register(struct gb_connection *connection, ...@@ -309,18 +307,13 @@ static int init_and_register(struct gb_connection *connection,
gb->bat.num_properties = ARRAY_SIZE(battery_props); gb->bat.num_properties = ARRAY_SIZE(battery_props);
gb->bat.get_property = get_property; gb->bat.get_property = get_property;
retval = power_supply_register(&connection->bundle->intf->dev, return power_supply_register(&connection->bundle->intf->dev, &gb->bat);
&gb->bat);
if (retval)
kfree(gb);
return retval;
} }
#else #else
static int init_and_register(struct gb_connection *connection, static int init_and_register(struct gb_connection *connection,
struct gb_battery *gb) struct gb_battery *gb)
{ {
struct power_supply_config cfg = {}; struct power_supply_config cfg = {};
int retval = 0;
cfg.drv_data = gb; cfg.drv_data = gb;
...@@ -334,11 +327,10 @@ static int init_and_register(struct gb_connection *connection, ...@@ -334,11 +327,10 @@ static int init_and_register(struct gb_connection *connection,
gb->bat = power_supply_register(&connection->bundle->intf->dev, gb->bat = power_supply_register(&connection->bundle->intf->dev,
&gb->desc, &cfg); &gb->desc, &cfg);
if (IS_ERR(gb->bat)) { if (IS_ERR(gb->bat))
retval = PTR_ERR(gb->bat); return PTR_ERR(gb->bat);
kfree(gb);
} return 0;
return retval;
} }
#endif #endif
...@@ -356,12 +348,14 @@ static int gb_battery_connection_init(struct gb_connection *connection) ...@@ -356,12 +348,14 @@ static int gb_battery_connection_init(struct gb_connection *connection)
/* Check the version */ /* Check the version */
retval = get_version(gb); retval = get_version(gb);
if (retval) { if (retval)
goto out;
retval = init_and_register(connection, gb);
out:
if (retval)
kfree(gb); kfree(gb);
return retval;
}
return init_and_register(connection, gb); return retval;
} }
static void gb_battery_connection_exit(struct gb_connection *connection) static void gb_battery_connection_exit(struct gb_connection *connection)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment