usb: typec: anx7411: fix fwnode_handle reference leak

commit 645d56e4cc74e953284809d096532c1955918a28 upstream.

An fwnode_handle and usb_role_switch are obtained with an incremented
refcount in anx7411_typec_port_probe(), however the refcounts are not
decremented in the error path. The fwnode_handle is also not decremented
in the .remove() function. Therefore, call fwnode_handle_put() and
usb_role_switch_put() accordingly.

Fixes: fe6d8a9c8e ("usb: typec: anx7411: Add Analogix PD ANX7411 support")
Cc: stable@vger.kernel.org
Signed-off-by: Joe Hattori <joe@pf.is.s.u-tokyo.ac.jp>
Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Link: https://lore.kernel.org/r/20241121023429.962848-1-joe@pf.is.s.u-tokyo.ac.jp
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
Joe Hattori
2024-11-21 11:34:29 +09:00
committed by Greg Kroah-Hartman
parent 02ceda7f64
commit 3fa13f853d

View File

@@ -1020,6 +1020,16 @@ static void anx7411_port_unregister_altmodes(struct typec_altmode **adev)
} }
} }
static void anx7411_port_unregister(struct typec_params *typecp)
{
fwnode_handle_put(typecp->caps.fwnode);
anx7411_port_unregister_altmodes(typecp->port_amode);
if (typecp->port)
typec_unregister_port(typecp->port);
if (typecp->role_sw)
usb_role_switch_put(typecp->role_sw);
}
static int anx7411_usb_mux_set(struct typec_mux_dev *mux, static int anx7411_usb_mux_set(struct typec_mux_dev *mux,
struct typec_mux_state *state) struct typec_mux_state *state)
{ {
@@ -1153,34 +1163,34 @@ static int anx7411_typec_port_probe(struct anx7411_data *ctx,
ret = fwnode_property_read_string(fwnode, "power-role", &buf); ret = fwnode_property_read_string(fwnode, "power-role", &buf);
if (ret) { if (ret) {
dev_err(dev, "power-role not found: %d\n", ret); dev_err(dev, "power-role not found: %d\n", ret);
return ret; goto put_fwnode;
} }
ret = typec_find_port_power_role(buf); ret = typec_find_port_power_role(buf);
if (ret < 0) if (ret < 0)
return ret; goto put_fwnode;
cap->type = ret; cap->type = ret;
ret = fwnode_property_read_string(fwnode, "data-role", &buf); ret = fwnode_property_read_string(fwnode, "data-role", &buf);
if (ret) { if (ret) {
dev_err(dev, "data-role not found: %d\n", ret); dev_err(dev, "data-role not found: %d\n", ret);
return ret; goto put_fwnode;
} }
ret = typec_find_port_data_role(buf); ret = typec_find_port_data_role(buf);
if (ret < 0) if (ret < 0)
return ret; goto put_fwnode;
cap->data = ret; cap->data = ret;
ret = fwnode_property_read_string(fwnode, "try-power-role", &buf); ret = fwnode_property_read_string(fwnode, "try-power-role", &buf);
if (ret) { if (ret) {
dev_err(dev, "try-power-role not found: %d\n", ret); dev_err(dev, "try-power-role not found: %d\n", ret);
return ret; goto put_fwnode;
} }
ret = typec_find_power_role(buf); ret = typec_find_power_role(buf);
if (ret < 0) if (ret < 0)
return ret; goto put_fwnode;
cap->prefer_role = ret; cap->prefer_role = ret;
/* Get source pdos */ /* Get source pdos */
@@ -1192,7 +1202,7 @@ static int anx7411_typec_port_probe(struct anx7411_data *ctx,
typecp->src_pdo_nr); typecp->src_pdo_nr);
if (ret < 0) { if (ret < 0) {
dev_err(dev, "source cap validate failed: %d\n", ret); dev_err(dev, "source cap validate failed: %d\n", ret);
return -EINVAL; goto put_fwnode;
} }
typecp->caps_flags |= HAS_SOURCE_CAP; typecp->caps_flags |= HAS_SOURCE_CAP;
@@ -1206,7 +1216,7 @@ static int anx7411_typec_port_probe(struct anx7411_data *ctx,
typecp->sink_pdo_nr); typecp->sink_pdo_nr);
if (ret < 0) { if (ret < 0) {
dev_err(dev, "sink cap validate failed: %d\n", ret); dev_err(dev, "sink cap validate failed: %d\n", ret);
return -EINVAL; goto put_fwnode;
} }
for (i = 0; i < typecp->sink_pdo_nr; i++) { for (i = 0; i < typecp->sink_pdo_nr; i++) {
@@ -1250,13 +1260,21 @@ static int anx7411_typec_port_probe(struct anx7411_data *ctx,
ret = PTR_ERR(ctx->typec.port); ret = PTR_ERR(ctx->typec.port);
ctx->typec.port = NULL; ctx->typec.port = NULL;
dev_err(dev, "Failed to register type c port %d\n", ret); dev_err(dev, "Failed to register type c port %d\n", ret);
return ret; goto put_usb_role_switch;
} }
typec_port_register_altmodes(ctx->typec.port, NULL, ctx, typec_port_register_altmodes(ctx->typec.port, NULL, ctx,
ctx->typec.port_amode, ctx->typec.port_amode,
MAX_ALTMODE); MAX_ALTMODE);
return 0; return 0;
put_usb_role_switch:
if (ctx->typec.role_sw)
usb_role_switch_put(ctx->typec.role_sw);
put_fwnode:
fwnode_handle_put(fwnode);
return ret;
} }
static int anx7411_typec_check_connection(struct anx7411_data *ctx) static int anx7411_typec_check_connection(struct anx7411_data *ctx)
@@ -1528,8 +1546,7 @@ free_wq:
destroy_workqueue(plat->workqueue); destroy_workqueue(plat->workqueue);
free_typec_port: free_typec_port:
typec_unregister_port(plat->typec.port); anx7411_port_unregister(&plat->typec);
anx7411_port_unregister_altmodes(plat->typec.port_amode);
free_typec_switch: free_typec_switch:
anx7411_unregister_switch(plat); anx7411_unregister_switch(plat);
@@ -1554,17 +1571,11 @@ static void anx7411_i2c_remove(struct i2c_client *client)
if (plat->spi_client) if (plat->spi_client)
i2c_unregister_device(plat->spi_client); i2c_unregister_device(plat->spi_client);
if (plat->typec.role_sw)
usb_role_switch_put(plat->typec.role_sw);
anx7411_unregister_mux(plat); anx7411_unregister_mux(plat);
anx7411_unregister_switch(plat); anx7411_unregister_switch(plat);
if (plat->typec.port) anx7411_port_unregister(&plat->typec);
typec_unregister_port(plat->typec.port);
anx7411_port_unregister_altmodes(plat->typec.port_amode);
} }
static const struct i2c_device_id anx7411_id[] = { static const struct i2c_device_id anx7411_id[] = {