[PATCH] soundwire: Use scope-based resource management in five functions

From: Markus Elfring
Date: Tue Jun 04 2024 - 07:31:44 EST


From: Markus Elfring <elfring@xxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 4 Jun 2024 12:30:40 +0200

Scope-based resource management became supported also for another
programming interface by contributions of Jonathan Cameron on 2024-02-17.
See also the commit 59ed5e2d505bf5f9b4af64d0021cd0c96aec1f7c ("device
property: Add cleanup.h based fwnode_handle_put() scope based cleanup.").

* Thus use the attribute “__free(fwnode_handle)” at more places.

* Reduce scopes for corresponding local variables.

* Omit explicit fwnode_handle_put() calls accordingly.

Signed-off-by: Markus Elfring <elfring@xxxxxxxxxxxxxxxxxxxxx>
---

Previous clarification approach:
soundwire: fix usages of device_get_named_child_node()
https://lore.kernel.org/lkml/a2036c46-0527-4ac3-a40b-c9c2cd3e185e@xxxxxx/
https://lkml.org/lkml/2024/5/28/1138


drivers/soundwire/amd_manager.c | 9 ++---
drivers/soundwire/intel_auxdevice.c | 12 ++----
drivers/soundwire/mipi_disco.c | 62 +++++++++++------------------
3 files changed, 31 insertions(+), 52 deletions(-)

diff --git a/drivers/soundwire/amd_manager.c b/drivers/soundwire/amd_manager.c
index 795e223f7e5c..94b867700b2d 100644
--- a/drivers/soundwire/amd_manager.c
+++ b/drivers/soundwire/amd_manager.c
@@ -15,6 +15,7 @@
#include <linux/soundwire/sdw.h>
#include <linux/soundwire/sdw_registers.h>
#include <linux/pm_runtime.h>
+#include <linux/property.h>
#include <linux/wait.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
@@ -546,7 +547,6 @@ static int amd_sdw_port_enable(struct sdw_bus *bus,
static int sdw_master_read_amd_prop(struct sdw_bus *bus)
{
struct amd_sdw_manager *amd_manager = to_amd_sdw(bus);
- struct fwnode_handle *link;
struct sdw_master_prop *prop;
u32 quirk_mask = 0;
u32 wake_en_mask = 0;
@@ -556,7 +556,9 @@ static int sdw_master_read_amd_prop(struct sdw_bus *bus)
prop = &bus->prop;
/* Find manager handle */
snprintf(name, sizeof(name), "mipi-sdw-link-%d-subproperties", bus->link_id);
- link = device_get_named_child_node(bus->dev, name);
+
+ struct fwnode_handle *link __free(fwnode_handle)
+ = device_get_named_child_node(bus->dev, name);
if (!link) {
dev_err(bus->dev, "Manager node %s not found\n", name);
return -EIO;
@@ -571,9 +573,6 @@ static int sdw_master_read_amd_prop(struct sdw_bus *bus)
amd_manager->wake_en_mask = wake_en_mask;
fwnode_property_read_u32(link, "amd-sdw-power-mode", &power_mode_mask);
amd_manager->power_mode_mask = power_mode_mask;
-
- fwnode_handle_put(link);
-
return 0;
}

diff --git a/drivers/soundwire/intel_auxdevice.c b/drivers/soundwire/intel_auxdevice.c
index fd29f4ee6970..17ad3191ecc3 100644
--- a/drivers/soundwire/intel_auxdevice.c
+++ b/drivers/soundwire/intel_auxdevice.c
@@ -14,6 +14,7 @@
#include <linux/auxiliary_bus.h>
#include <sound/pcm_params.h>
#include <linux/pm_runtime.h>
+#include <linux/property.h>
#include <sound/soc.h>
#include <linux/soundwire/sdw_registers.h>
#include <linux/soundwire/sdw.h>
@@ -123,7 +124,6 @@ static int sdw_master_read_intel_prop(struct sdw_bus *bus)
{
struct sdw_master_prop *prop = &bus->prop;
struct sdw_intel_prop *intel_prop;
- struct fwnode_handle *link;
char name[32];
u32 quirk_mask;

@@ -131,7 +131,8 @@ static int sdw_master_read_intel_prop(struct sdw_bus *bus)
snprintf(name, sizeof(name),
"mipi-sdw-link-%d-subproperties", bus->link_id);

- link = device_get_named_child_node(bus->dev, name);
+ struct fwnode_handle *link __free(fwnode_handle)
+ = device_get_named_child_node(bus->dev, name);
if (!link) {
dev_err(bus->dev, "Master node %s not found\n", name);
return -EIO;
@@ -155,10 +156,8 @@ static int sdw_master_read_intel_prop(struct sdw_bus *bus)
SDW_MASTER_QUIRKS_CLEAR_INITIAL_PARITY;

intel_prop = devm_kzalloc(bus->dev, sizeof(*intel_prop), GFP_KERNEL);
- if (!intel_prop) {
- fwnode_handle_put(link);
+ if (!intel_prop)
return -ENOMEM;
- }

/* initialize with hardware defaults, in case the properties are not found */
intel_prop->clde = 0x0;
@@ -205,9 +204,6 @@ static int sdw_master_read_intel_prop(struct sdw_bus *bus)
intel_prop->doais,
intel_prop->dodse,
intel_prop->dods);
-
- fwnode_handle_put(link);
-
return 0;
}

diff --git a/drivers/soundwire/mipi_disco.c b/drivers/soundwire/mipi_disco.c
index e5d9df26d4dc..4906532feaec 100644
--- a/drivers/soundwire/mipi_disco.c
+++ b/drivers/soundwire/mipi_disco.c
@@ -30,7 +30,6 @@
int sdw_master_read_prop(struct sdw_bus *bus)
{
struct sdw_master_prop *prop = &bus->prop;
- struct fwnode_handle *link;
char name[32];
int nval, i;

@@ -42,7 +41,8 @@ int sdw_master_read_prop(struct sdw_bus *bus)
snprintf(name, sizeof(name),
"mipi-sdw-link-%d-subproperties", bus->link_id);

- link = device_get_named_child_node(bus->dev, name);
+ struct fwnode_handle *link __free(fwnode_handle)
+ = device_get_named_child_node(bus->dev, name);
if (!link) {
dev_err(bus->dev, "Master node %s not found\n", name);
return -EIO;
@@ -66,10 +66,8 @@ int sdw_master_read_prop(struct sdw_bus *bus)
prop->clk_freq = devm_kcalloc(bus->dev, prop->num_clk_freq,
sizeof(*prop->clk_freq),
GFP_KERNEL);
- if (!prop->clk_freq) {
- fwnode_handle_put(link);
+ if (!prop->clk_freq)
return -ENOMEM;
- }

fwnode_property_read_u32_array(link,
"mipi-sdw-clock-frequencies-supported",
@@ -94,10 +92,8 @@ int sdw_master_read_prop(struct sdw_bus *bus)
prop->clk_gears = devm_kcalloc(bus->dev, prop->num_clk_gears,
sizeof(*prop->clk_gears),
GFP_KERNEL);
- if (!prop->clk_gears) {
- fwnode_handle_put(link);
+ if (!prop->clk_gears)
return -ENOMEM;
- }

fwnode_property_read_u32_array(link,
"mipi-sdw-supported-clock-gears",
@@ -119,9 +115,6 @@ int sdw_master_read_prop(struct sdw_bus *bus)

fwnode_property_read_u32(link, "mipi-sdw-command-error-threshold",
&prop->err_threshold);
-
- fwnode_handle_put(link);
-
return 0;
}
EXPORT_SYMBOL(sdw_master_read_prop);
@@ -169,7 +162,6 @@ static int sdw_slave_read_dpn(struct sdw_slave *slave,
struct sdw_dpn_prop *dpn, int count, int ports,
char *type)
{
- struct fwnode_handle *node;
u32 bit, i = 0;
int nval;
unsigned long addr;
@@ -185,7 +177,8 @@ static int sdw_slave_read_dpn(struct sdw_slave *slave,

dpn[i].num = bit;

- node = device_get_named_child_node(&slave->dev, name);
+ struct fwnode_handle *node __free(fwnode_handle)
+ = device_get_named_child_node(&slave->dev, name);
if (!node) {
dev_err(&slave->dev, "%s dpN not found\n", name);
return -EIO;
@@ -203,10 +196,8 @@ static int sdw_slave_read_dpn(struct sdw_slave *slave,
dpn[i].num_words,
sizeof(*dpn[i].words),
GFP_KERNEL);
- if (!dpn[i].words) {
- fwnode_handle_put(node);
+ if (!dpn[i].words)
return -ENOMEM;
- }

fwnode_property_read_u32_array(node,
"mipi-sdw-port-wordlength-configs",
@@ -244,10 +235,8 @@ static int sdw_slave_read_dpn(struct sdw_slave *slave,
dpn[i].num_channels,
sizeof(*dpn[i].channels),
GFP_KERNEL);
- if (!dpn[i].channels) {
- fwnode_handle_put(node);
+ if (!dpn[i].channels)
return -ENOMEM;
- }

fwnode_property_read_u32_array(node,
"mipi-sdw-channel-number-list",
@@ -261,10 +250,8 @@ static int sdw_slave_read_dpn(struct sdw_slave *slave,
dpn[i].num_ch_combinations,
sizeof(*dpn[i].ch_combinations),
GFP_KERNEL);
- if (!dpn[i].ch_combinations) {
- fwnode_handle_put(node);
+ if (!dpn[i].ch_combinations)
return -ENOMEM;
- }

fwnode_property_read_u32_array(node,
"mipi-sdw-channel-combination-list",
@@ -286,8 +273,6 @@ static int sdw_slave_read_dpn(struct sdw_slave *slave,

/* TODO: Read audio mode */

- fwnode_handle_put(node);
-
i++;
}

@@ -302,7 +287,6 @@ int sdw_slave_read_prop(struct sdw_slave *slave)
{
struct sdw_slave_prop *prop = &slave->prop;
struct device *dev = &slave->dev;
- struct fwnode_handle *port;
int nval;

device_property_read_u32(dev, "mipi-sdw-sw-interface-revision",
@@ -355,21 +339,21 @@ int sdw_slave_read_prop(struct sdw_slave *slave)
&prop->sink_ports);

/* Read dp0 properties */
- port = device_get_named_child_node(dev, "mipi-sdw-dp-0-subproperties");
- if (!port) {
- dev_dbg(dev, "DP0 node not found!!\n");
- } else {
- prop->dp0_prop = devm_kzalloc(&slave->dev,
- sizeof(*prop->dp0_prop),
- GFP_KERNEL);
- if (!prop->dp0_prop) {
- fwnode_handle_put(port);
- return -ENOMEM;
- }
-
- sdw_slave_read_dp0(slave, port, prop->dp0_prop);
+ {
+ struct fwnode_handle *port __free(fwnode_handle)
+ = device_get_named_child_node(dev,
+ "mipi-sdw-dp-0-subproperties");
+ if (!port) {
+ dev_dbg(dev, "DP0 node not found!!\n");
+ } else {
+ prop->dp0_prop = devm_kzalloc(&slave->dev,
+ sizeof(*prop->dp0_prop),
+ GFP_KERNEL);
+ if (!prop->dp0_prop)
+ return -ENOMEM;

- fwnode_handle_put(port);
+ sdw_slave_read_dp0(slave, port, prop->dp0_prop);
+ }
}

/*
--
2.45.1