523e54361de3fb348c7ce62411405b3be61a0975
[openwrt/staging/ynezz.git] /
1 From b19ed31a1ced7b6d4c4c04967a509d91a134e5bb Mon Sep 17 00:00:00 2001
2 From: Takashi Iwai <tiwai@suse.de>
3 Date: Tue, 4 Sep 2018 17:58:58 +0200
4 Subject: [PATCH] staging: bcm2835-audio: Simplify card object
5 management
6
7 commit 872ae2d63d516a2a3b9c833d8685afcfa7814542 upstream.
8
9 Instead of creating a dummy child device to manage the card object,
10 just use devm stuff directly for releasing with snd_card_free().
11 This results in a lot of code reduction.
12
13 Since the dummy child devices are gone, the device object to be passed
14 to the memory allocator needs to be adjusted as well.
15
16 Signed-off-by: Takashi Iwai <tiwai@suse.de>
17 Tested-by: Stefan Wahren <stefan.wahren@i2se.com>
18 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
19 ---
20 .../vc04_services/bcm2835-audio/bcm2835-pcm.c | 2 +-
21 .../vc04_services/bcm2835-audio/bcm2835.c | 120 +++++-------------
22 2 files changed, 33 insertions(+), 89 deletions(-)
23
24 --- a/drivers/staging/vc04_services/bcm2835-audio/bcm2835-pcm.c
25 +++ b/drivers/staging/vc04_services/bcm2835-audio/bcm2835-pcm.c
26 @@ -349,7 +349,7 @@ int snd_bcm2835_new_pcm(struct bcm2835_c
27 &snd_bcm2835_playback_ops);
28
29 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
30 - chip->card->dev->parent, 128 * 1024, 128 * 1024);
31 + chip->card->dev, 128 * 1024, 128 * 1024);
32
33 if (spdif)
34 chip->pcm_spdif = pcm;
35 --- a/drivers/staging/vc04_services/bcm2835-audio/bcm2835.c
36 +++ b/drivers/staging/vc04_services/bcm2835-audio/bcm2835.c
37 @@ -22,38 +22,6 @@ module_param(enable_compat_alsa, bool, 0
38 MODULE_PARM_DESC(enable_compat_alsa,
39 "Enables ALSA compatibility virtual audio device");
40
41 -static void snd_devm_unregister_child(struct device *dev, void *res)
42 -{
43 - struct device *childdev = *(struct device **)res;
44 - struct bcm2835_chip *chip = dev_get_drvdata(childdev);
45 - struct snd_card *card = chip->card;
46 -
47 - snd_card_free(card);
48 -
49 - device_unregister(childdev);
50 -}
51 -
52 -static int snd_devm_add_child(struct device *dev, struct device *child)
53 -{
54 - struct device **dr;
55 - int ret;
56 -
57 - dr = devres_alloc(snd_devm_unregister_child, sizeof(*dr), GFP_KERNEL);
58 - if (!dr)
59 - return -ENOMEM;
60 -
61 - ret = device_add(child);
62 - if (ret) {
63 - devres_free(dr);
64 - return ret;
65 - }
66 -
67 - *dr = child;
68 - devres_add(dev, dr);
69 -
70 - return 0;
71 -}
72 -
73 static void bcm2835_devm_free_vchi_ctx(struct device *dev, void *res)
74 {
75 struct bcm2835_vchi_ctx *vchi_ctx = res;
76 @@ -84,36 +52,6 @@ static int bcm2835_devm_add_vchi_ctx(str
77 return 0;
78 }
79
80 -static void snd_bcm2835_release(struct device *dev)
81 -{
82 -}
83 -
84 -static struct device *
85 -snd_create_device(struct device *parent,
86 - struct device_driver *driver,
87 - const char *name)
88 -{
89 - struct device *device;
90 - int ret;
91 -
92 - device = devm_kzalloc(parent, sizeof(*device), GFP_KERNEL);
93 - if (!device)
94 - return ERR_PTR(-ENOMEM);
95 -
96 - device_initialize(device);
97 - device->parent = parent;
98 - device->driver = driver;
99 - device->release = snd_bcm2835_release;
100 -
101 - dev_set_name(device, "%s", name);
102 -
103 - ret = snd_devm_add_child(parent, device);
104 - if (ret)
105 - return ERR_PTR(ret);
106 -
107 - return device;
108 -}
109 -
110 typedef int (*bcm2835_audio_newpcm_func)(struct bcm2835_chip *chip,
111 const char *name,
112 enum snd_bcm2835_route route,
113 @@ -216,40 +154,36 @@ static struct bcm2835_audio_drivers chil
114 },
115 };
116
117 -static int snd_add_child_device(struct device *device,
118 +static void bcm2835_card_free(void *data)
119 +{
120 + snd_card_free(data);
121 +}
122 +
123 +static int snd_add_child_device(struct device *dev,
124 struct bcm2835_audio_driver *audio_driver,
125 u32 numchans)
126 {
127 struct snd_card *card;
128 - struct device *child;
129 struct bcm2835_chip *chip;
130 int err;
131
132 - child = snd_create_device(device, &audio_driver->driver,
133 - audio_driver->driver.name);
134 - if (IS_ERR(child)) {
135 - dev_err(device,
136 - "Unable to create child device %p, error %ld",
137 - audio_driver->driver.name,
138 - PTR_ERR(child));
139 - return PTR_ERR(child);
140 - }
141 -
142 - err = snd_card_new(child, -1, NULL, THIS_MODULE, sizeof(*chip), &card);
143 + err = snd_card_new(dev, -1, NULL, THIS_MODULE, sizeof(*chip), &card);
144 if (err < 0) {
145 - dev_err(child, "Failed to create card");
146 + dev_err(dev, "Failed to create card");
147 return err;
148 }
149
150 chip = card->private_data;
151 chip->card = card;
152 - chip->dev = child;
153 + chip->dev = dev;
154 mutex_init(&chip->audio_mutex);
155
156 - chip->vchi_ctx = devres_find(device,
157 + chip->vchi_ctx = devres_find(dev,
158 bcm2835_devm_free_vchi_ctx, NULL, NULL);
159 - if (!chip->vchi_ctx)
160 - return -ENODEV;
161 + if (!chip->vchi_ctx) {
162 + err = -ENODEV;
163 + goto error;
164 + }
165
166 strcpy(card->driver, audio_driver->driver.name);
167 strcpy(card->shortname, audio_driver->shortname);
168 @@ -259,26 +193,36 @@ static int snd_add_child_device(struct d
169 audio_driver->route,
170 numchans);
171 if (err) {
172 - dev_err(child, "Failed to create pcm, error %d\n", err);
173 - return err;
174 + dev_err(dev, "Failed to create pcm, error %d\n", err);
175 + goto error;
176 }
177
178 err = audio_driver->newctl(chip);
179 if (err) {
180 - dev_err(child, "Failed to create controls, error %d\n", err);
181 - return err;
182 + dev_err(dev, "Failed to create controls, error %d\n", err);
183 + goto error;
184 }
185
186 err = snd_card_register(card);
187 if (err) {
188 - dev_err(child, "Failed to register card, error %d\n", err);
189 - return err;
190 + dev_err(dev, "Failed to register card, error %d\n", err);
191 + goto error;
192 }
193
194 - dev_set_drvdata(child, chip);
195 - dev_info(child, "card created with %d channels\n", numchans);
196 + dev_set_drvdata(dev, chip);
197
198 + err = devm_add_action(dev, bcm2835_card_free, card);
199 + if (err < 0) {
200 + dev_err(dev, "Failed to add devm action, err %d\n", err);
201 + goto error;
202 + }
203 +
204 + dev_info(dev, "card created with %d channels\n", numchans);
205 return 0;
206 +
207 + error:
208 + snd_card_free(card);
209 + return err;
210 }
211
212 static int snd_add_child_devices(struct device *device, u32 numchans)