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[] = {
static int init_and_register(struct gb_connection *connection,
struct gb_battery *gb)
{
int retval;
// FIXME - get a better (i.e. unique) name
// FIXME - anything else needs to be set?
gb->bat.name = "gb_battery";
......@@ -309,18 +307,13 @@ static int init_and_register(struct gb_connection *connection,
gb->bat.num_properties = ARRAY_SIZE(battery_props);
gb->bat.get_property = get_property;
retval = power_supply_register(&connection->bundle->intf->dev,
&gb->bat);
if (retval)
kfree(gb);
return retval;
return power_supply_register(&connection->bundle->intf->dev, &gb->bat);
}
#else
static int init_and_register(struct gb_connection *connection,
struct gb_battery *gb)
{
struct power_supply_config cfg = {};
int retval = 0;
cfg.drv_data = gb;
......@@ -334,11 +327,10 @@ static int init_and_register(struct gb_connection *connection,
gb->bat = power_supply_register(&connection->bundle->intf->dev,
&gb->desc, &cfg);
if (IS_ERR(gb->bat)) {
retval = PTR_ERR(gb->bat);
kfree(gb);
}
return retval;
if (IS_ERR(gb->bat))
return PTR_ERR(gb->bat);
return 0;
}
#endif
......@@ -356,12 +348,14 @@ static int gb_battery_connection_init(struct gb_connection *connection)
/* Check the version */
retval = get_version(gb);
if (retval) {
if (retval)
goto out;
retval = init_and_register(connection, gb);
out:
if (retval)
kfree(gb);
return retval;
}
return init_and_register(connection, gb);
return retval;
}
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