Skip to content

Commit 0c6c651

Browse files
committed
ASoC: Simplify code with cleanup.h
Merge series from Krzysztof Kozlowski <[email protected]>: Allocate the memory with scoped/cleanup.h to reduce error handling (simpler error paths) and make the code a bit smaller.
2 parents 1cc509e + 522133d commit 0c6c651

File tree

9 files changed

+87
-141
lines changed

9 files changed

+87
-141
lines changed

sound/soc/codecs/audio-iio-aux.c

Lines changed: 29 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
// Author: Herve Codina <[email protected]>
88

9+
#include <linux/cleanup.h>
910
#include <linux/iio/consumer.h>
1011
#include <linux/minmax.h>
1112
#include <linux/mod_devicetable.h>
@@ -131,51 +132,36 @@ static int audio_iio_aux_add_dapms(struct snd_soc_component *component,
131132
struct audio_iio_aux_chan *chan)
132133
{
133134
struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
134-
char *output_name;
135-
char *input_name;
136-
char *pga_name;
137135
int ret;
138136

139-
input_name = kasprintf(GFP_KERNEL, "%s IN", chan->name);
137+
/* Allocated names are not needed afterwards (duplicated in ASoC internals) */
138+
char *input_name __free(kfree) = kasprintf(GFP_KERNEL, "%s IN", chan->name);
140139
if (!input_name)
141140
return -ENOMEM;
142141

143-
output_name = kasprintf(GFP_KERNEL, "%s OUT", chan->name);
144-
if (!output_name) {
145-
ret = -ENOMEM;
146-
goto out_free_input_name;
147-
}
142+
char *output_name __free(kfree) = kasprintf(GFP_KERNEL, "%s OUT", chan->name);
143+
if (!output_name)
144+
return -ENOMEM;
148145

149-
pga_name = kasprintf(GFP_KERNEL, "%s PGA", chan->name);
150-
if (!pga_name) {
151-
ret = -ENOMEM;
152-
goto out_free_output_name;
153-
}
146+
char *pga_name __free(kfree) = kasprintf(GFP_KERNEL, "%s PGA", chan->name);
147+
if (!pga_name)
148+
return -ENOMEM;
154149

155150
widgets[0] = SND_SOC_DAPM_INPUT(input_name);
156151
widgets[1] = SND_SOC_DAPM_OUTPUT(output_name);
157152
widgets[2] = SND_SOC_DAPM_PGA(pga_name, SND_SOC_NOPM, 0, 0, NULL, 0);
158153
ret = snd_soc_dapm_new_controls(dapm, widgets, 3);
159154
if (ret)
160-
goto out_free_pga_name;
155+
return ret;
161156

162157
routes[0].sink = pga_name;
163158
routes[0].control = NULL;
164159
routes[0].source = input_name;
165160
routes[1].sink = output_name;
166161
routes[1].control = NULL;
167162
routes[1].source = pga_name;
168-
ret = snd_soc_dapm_add_routes(dapm, routes, 2);
169-
170-
/* Allocated names are no more needed (duplicated in ASoC internals) */
171163

172-
out_free_pga_name:
173-
kfree(pga_name);
174-
out_free_output_name:
175-
kfree(output_name);
176-
out_free_input_name:
177-
kfree(input_name);
178-
return ret;
164+
return snd_soc_dapm_add_routes(dapm, routes, 2);
179165
}
180166

181167
static int audio_iio_aux_component_probe(struct snd_soc_component *component)
@@ -244,8 +230,6 @@ static int audio_iio_aux_probe(struct platform_device *pdev)
244230
struct audio_iio_aux_chan *iio_aux_chan;
245231
struct device *dev = &pdev->dev;
246232
struct audio_iio_aux *iio_aux;
247-
const char **names;
248-
u32 *invert_ranges;
249233
int count;
250234
int ret;
251235
int i;
@@ -262,22 +246,22 @@ static int audio_iio_aux_probe(struct platform_device *pdev)
262246

263247
iio_aux->num_chans = count;
264248

265-
names = kcalloc(iio_aux->num_chans, sizeof(*names), GFP_KERNEL);
249+
const char **names __free(kfree) = kcalloc(iio_aux->num_chans,
250+
sizeof(*names),
251+
GFP_KERNEL);
266252
if (!names)
267253
return -ENOMEM;
268254

269-
invert_ranges = kcalloc(iio_aux->num_chans, sizeof(*invert_ranges), GFP_KERNEL);
270-
if (!invert_ranges) {
271-
ret = -ENOMEM;
272-
goto out_free_names;
273-
}
255+
u32 *invert_ranges __free(kfree) = kcalloc(iio_aux->num_chans,
256+
sizeof(*invert_ranges),
257+
GFP_KERNEL);
258+
if (!invert_ranges)
259+
return -ENOMEM;
274260

275261
ret = device_property_read_string_array(dev, "io-channel-names",
276262
names, iio_aux->num_chans);
277-
if (ret < 0) {
278-
dev_err_probe(dev, ret, "failed to read io-channel-names\n");
279-
goto out_free_invert_ranges;
280-
}
263+
if (ret < 0)
264+
return dev_err_probe(dev, ret, "failed to read io-channel-names\n");
281265

282266
/*
283267
* snd-control-invert-range is optional and can contain fewer items
@@ -288,10 +272,8 @@ static int audio_iio_aux_probe(struct platform_device *pdev)
288272
count = min_t(unsigned int, count, iio_aux->num_chans);
289273
ret = device_property_read_u32_array(dev, "snd-control-invert-range",
290274
invert_ranges, count);
291-
if (ret < 0) {
292-
dev_err_probe(dev, ret, "failed to read snd-control-invert-range\n");
293-
goto out_free_invert_ranges;
294-
}
275+
if (ret < 0)
276+
return dev_err_probe(dev, ret, "failed to read snd-control-invert-range\n");
295277
}
296278

297279
for (i = 0; i < iio_aux->num_chans; i++) {
@@ -300,23 +282,16 @@ static int audio_iio_aux_probe(struct platform_device *pdev)
300282
iio_aux_chan->is_invert_range = invert_ranges[i];
301283

302284
iio_aux_chan->iio_chan = devm_iio_channel_get(dev, iio_aux_chan->name);
303-
if (IS_ERR(iio_aux_chan->iio_chan)) {
304-
ret = PTR_ERR(iio_aux_chan->iio_chan);
305-
dev_err_probe(dev, ret, "get IIO channel '%s' failed\n",
306-
iio_aux_chan->name);
307-
goto out_free_invert_ranges;
308-
}
285+
if (IS_ERR(iio_aux_chan->iio_chan))
286+
return dev_err_probe(dev, PTR_ERR(iio_aux_chan->iio_chan),
287+
"get IIO channel '%s' failed\n",
288+
iio_aux_chan->name);
309289
}
310290

311291
platform_set_drvdata(pdev, iio_aux);
312292

313-
ret = devm_snd_soc_register_component(dev, &audio_iio_aux_component_driver,
314-
NULL, 0);
315-
out_free_invert_ranges:
316-
kfree(invert_ranges);
317-
out_free_names:
318-
kfree(names);
319-
return ret;
293+
return devm_snd_soc_register_component(dev, &audio_iio_aux_component_driver,
294+
NULL, 0);
320295
}
321296

322297
static const struct of_device_id audio_iio_aux_ids[] = {

sound/soc/codecs/wcd9335.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <linux/module.h>
66
#include <linux/init.h>
77
#include <linux/platform_device.h>
8+
#include <linux/cleanup.h>
89
#include <linux/device.h>
910
#include <linux/wait.h>
1011
#include <linux/bitops.h>
@@ -2714,42 +2715,38 @@ static int wcd9335_codec_enable_dec(struct snd_soc_dapm_widget *w,
27142715
struct snd_soc_component *comp = snd_soc_dapm_to_component(w->dapm);
27152716
unsigned int decimator;
27162717
char *dec_adc_mux_name = NULL;
2717-
char *widget_name = NULL;
2718-
char *wname;
2718+
char *widget_name;
27192719
int ret = 0, amic_n;
27202720
u16 tx_vol_ctl_reg, pwr_level_reg = 0, dec_cfg_reg, hpf_gate_reg;
27212721
u16 tx_gain_ctl_reg;
27222722
char *dec;
27232723
u8 hpf_coff_freq;
27242724

2725-
widget_name = kmemdup_nul(w->name, 15, GFP_KERNEL);
2726-
if (!widget_name)
2725+
char *wname __free(kfree) = kmemdup_nul(w->name, 15, GFP_KERNEL);
2726+
if (!wname)
27272727
return -ENOMEM;
27282728

2729-
wname = widget_name;
2729+
widget_name = wname;
27302730
dec_adc_mux_name = strsep(&widget_name, " ");
27312731
if (!dec_adc_mux_name) {
27322732
dev_err(comp->dev, "%s: Invalid decimator = %s\n",
27332733
__func__, w->name);
2734-
ret = -EINVAL;
2735-
goto out;
2734+
return -EINVAL;
27362735
}
27372736
dec_adc_mux_name = widget_name;
27382737

27392738
dec = strpbrk(dec_adc_mux_name, "012345678");
27402739
if (!dec) {
27412740
dev_err(comp->dev, "%s: decimator index not found\n",
27422741
__func__);
2743-
ret = -EINVAL;
2744-
goto out;
2742+
return -EINVAL;
27452743
}
27462744

27472745
ret = kstrtouint(dec, 10, &decimator);
27482746
if (ret < 0) {
27492747
dev_err(comp->dev, "%s: Invalid decimator = %s\n",
27502748
__func__, wname);
2751-
ret = -EINVAL;
2752-
goto out;
2749+
return -EINVAL;
27532750
}
27542751

27552752
tx_vol_ctl_reg = WCD9335_CDC_TX0_TX_PATH_CTL + 16 * decimator;
@@ -2836,8 +2833,7 @@ static int wcd9335_codec_enable_dec(struct snd_soc_dapm_widget *w,
28362833
snd_soc_component_update_bits(comp, tx_vol_ctl_reg, 0x10, 0x00);
28372834
break;
28382835
}
2839-
out:
2840-
kfree(wname);
2836+
28412837
return ret;
28422838
}
28432839

sound/soc/codecs/wcd934x.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: GPL-2.0
22
// Copyright (c) 2019, Linaro Limited
33

4+
#include <linux/cleanup.h>
45
#include <linux/clk.h>
56
#include <linux/clk-provider.h>
67
#include <linux/interrupt.h>
@@ -4973,42 +4974,38 @@ static int wcd934x_codec_enable_dec(struct snd_soc_dapm_widget *w,
49734974
struct snd_soc_component *comp = snd_soc_dapm_to_component(w->dapm);
49744975
unsigned int decimator;
49754976
char *dec_adc_mux_name = NULL;
4976-
char *widget_name = NULL;
4977-
char *wname;
4977+
char *widget_name;
49784978
int ret = 0, amic_n;
49794979
u16 tx_vol_ctl_reg, pwr_level_reg = 0, dec_cfg_reg, hpf_gate_reg;
49804980
u16 tx_gain_ctl_reg;
49814981
char *dec;
49824982
u8 hpf_coff_freq;
49834983

4984-
widget_name = kstrndup(w->name, 15, GFP_KERNEL);
4985-
if (!widget_name)
4984+
char *wname __free(kfree) = kstrndup(w->name, 15, GFP_KERNEL);
4985+
if (!wname)
49864986
return -ENOMEM;
49874987

4988-
wname = widget_name;
4988+
widget_name = wname;
49894989
dec_adc_mux_name = strsep(&widget_name, " ");
49904990
if (!dec_adc_mux_name) {
49914991
dev_err(comp->dev, "%s: Invalid decimator = %s\n",
49924992
__func__, w->name);
4993-
ret = -EINVAL;
4994-
goto out;
4993+
return -EINVAL;
49954994
}
49964995
dec_adc_mux_name = widget_name;
49974996

49984997
dec = strpbrk(dec_adc_mux_name, "012345678");
49994998
if (!dec) {
50004999
dev_err(comp->dev, "%s: decimator index not found\n",
50015000
__func__);
5002-
ret = -EINVAL;
5003-
goto out;
5001+
return -EINVAL;
50045002
}
50055003

50065004
ret = kstrtouint(dec, 10, &decimator);
50075005
if (ret < 0) {
50085006
dev_err(comp->dev, "%s: Invalid decimator = %s\n",
50095007
__func__, wname);
5010-
ret = -EINVAL;
5011-
goto out;
5008+
return -EINVAL;
50125009
}
50135010

50145011
tx_vol_ctl_reg = WCD934X_CDC_TX0_TX_PATH_CTL + 16 * decimator;
@@ -5101,8 +5098,7 @@ static int wcd934x_codec_enable_dec(struct snd_soc_dapm_widget *w,
51015098
WCD934X_DEC_PWR_LVL_DF);
51025099
break;
51035100
}
5104-
out:
5105-
kfree(wname);
5101+
51065102
return ret;
51075103
}
51085104

sound/soc/generic/audio-graph-card.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88
// based on ${LINUX}/sound/soc/generic/simple-card.c
99

10+
#include <linux/cleanup.h>
1011
#include <linux/clk.h>
1112
#include <linux/device.h>
1213
#include <linux/gpio/consumer.h>
@@ -573,10 +574,9 @@ static int graph_get_dais_count(struct simple_util_priv *priv,
573574
int audio_graph_parse_of(struct simple_util_priv *priv, struct device *dev)
574575
{
575576
struct snd_soc_card *card = simple_priv_to_card(priv);
576-
struct link_info *li;
577577
int ret;
578578

579-
li = devm_kzalloc(dev, sizeof(*li), GFP_KERNEL);
579+
struct link_info *li __free(kfree) = kzalloc(sizeof(*li), GFP_KERNEL);
580580
if (!li)
581581
return -ENOMEM;
582582

@@ -628,7 +628,6 @@ int audio_graph_parse_of(struct simple_util_priv *priv, struct device *dev)
628628
if (ret < 0)
629629
goto err;
630630

631-
devm_kfree(dev, li);
632631
return 0;
633632

634633
err:

sound/soc/generic/audio-graph-card2.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,10 +1350,9 @@ int audio_graph2_parse_of(struct simple_util_priv *priv, struct device *dev,
13501350
struct graph2_custom_hooks *hooks)
13511351
{
13521352
struct snd_soc_card *card = simple_priv_to_card(priv);
1353-
struct link_info *li;
13541353
int ret;
13551354

1356-
li = devm_kzalloc(dev, sizeof(*li), GFP_KERNEL);
1355+
struct link_info *li __free(kfree) = kzalloc(sizeof(*li), GFP_KERNEL);
13571356
if (!li)
13581357
return -ENOMEM;
13591358

@@ -1417,8 +1416,6 @@ int audio_graph2_parse_of(struct simple_util_priv *priv, struct device *dev,
14171416

14181417
ret = devm_snd_soc_register_card(dev, card);
14191418
err:
1420-
devm_kfree(dev, li);
1421-
14221419
if (ret < 0)
14231420
dev_err_probe(dev, ret, "parse error\n");
14241421

sound/soc/generic/simple-card-utils.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Copyright (c) 2016 Kuninori Morimoto <[email protected]>
66

77
#include <dt-bindings/sound/audio-graph.h>
8+
#include <linux/cleanup.h>
89
#include <linux/clk.h>
910
#include <linux/gpio/consumer.h>
1011
#include <linux/module.h>
@@ -135,8 +136,8 @@ EXPORT_SYMBOL_GPL(simple_util_parse_daifmt);
135136
int simple_util_parse_tdm_width_map(struct device *dev, struct device_node *np,
136137
struct simple_util_dai *dai)
137138
{
138-
u32 *array_values, *p;
139139
int n, i, ret;
140+
u32 *p;
140141

141142
if (!of_property_read_bool(np, "dai-tdm-slot-width-map"))
142143
return 0;
@@ -151,14 +152,15 @@ int simple_util_parse_tdm_width_map(struct device *dev, struct device_node *np,
151152
if (!dai->tdm_width_map)
152153
return -ENOMEM;
153154

154-
array_values = kcalloc(n, sizeof(*array_values), GFP_KERNEL);
155+
u32 *array_values __free(kfree) = kcalloc(n, sizeof(*array_values),
156+
GFP_KERNEL);
155157
if (!array_values)
156158
return -ENOMEM;
157159

158160
ret = of_property_read_u32_array(np, "dai-tdm-slot-width-map", array_values, n);
159161
if (ret < 0) {
160162
dev_err(dev, "Could not read dai-tdm-slot-width-map: %d\n", ret);
161-
goto out;
163+
return ret;
162164
}
163165

164166
p = array_values;
@@ -169,11 +171,8 @@ int simple_util_parse_tdm_width_map(struct device *dev, struct device_node *np,
169171
}
170172

171173
dai->n_tdm_widths = i;
172-
ret = 0;
173-
out:
174-
kfree(array_values);
175174

176-
return ret;
175+
return 0;
177176
}
178177
EXPORT_SYMBOL_GPL(simple_util_parse_tdm_width_map);
179178

0 commit comments

Comments
 (0)