• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // soc-pcm.c  --  ALSA SoC PCM
4 //
5 // Copyright 2005 Wolfson Microelectronics PLC.
6 // Copyright 2005 Openedhand Ltd.
7 // Copyright (C) 2010 Slimlogic Ltd.
8 // Copyright (C) 2010 Texas Instruments Inc.
9 //
10 // Authors: Liam Girdwood <lrg@ti.com>
11 //          Mark Brown <broonie@opensource.wolfsonmicro.com>
12 
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/delay.h>
16 #include <linux/pinctrl/consumer.h>
17 #include <linux/slab.h>
18 #include <linux/workqueue.h>
19 #include <linux/export.h>
20 #include <linux/debugfs.h>
21 #include <sound/core.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
24 #include <sound/soc.h>
25 #include <sound/soc-dpcm.h>
26 #include <sound/soc-link.h>
27 #include <sound/initval.h>
28 
29 #define soc_pcm_ret(rtd, ret) _soc_pcm_ret(rtd, __func__, ret)
_soc_pcm_ret(struct snd_soc_pcm_runtime * rtd,const char * func,int ret)30 static inline int _soc_pcm_ret(struct snd_soc_pcm_runtime *rtd,
31 			       const char *func, int ret)
32 {
33 	/* Positive, Zero values are not errors */
34 	if (ret >= 0)
35 		return ret;
36 
37 	/* Negative values might be errors */
38 	switch (ret) {
39 	case -EPROBE_DEFER:
40 	case -ENOTSUPP:
41 		break;
42 	default:
43 		dev_err(rtd->dev,
44 			"ASoC: error at %s on %s: %d\n",
45 			func, rtd->dai_link->name, ret);
46 	}
47 
48 	return ret;
49 }
50 
51 /* is the current PCM operation for this FE ? */
52 #if 0
53 static int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
54 {
55 	if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
56 		return 1;
57 	return 0;
58 }
59 #endif
60 
61 /* is the current PCM operation for this BE ? */
snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime * fe,struct snd_soc_pcm_runtime * be,int stream)62 static int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
63 			       struct snd_soc_pcm_runtime *be, int stream)
64 {
65 	if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
66 	    ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
67 	     be->dpcm[stream].runtime_update))
68 		return 1;
69 	return 0;
70 }
71 
snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime * fe,struct snd_soc_pcm_runtime * be,int stream,const enum snd_soc_dpcm_state * states,int num_states)72 static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe,
73 				    struct snd_soc_pcm_runtime *be,
74 				    int stream,
75 				    const enum snd_soc_dpcm_state *states,
76 				    int num_states)
77 {
78 	struct snd_soc_dpcm *dpcm;
79 	int state;
80 	int ret = 1;
81 	int i;
82 
83 	for_each_dpcm_fe(be, stream, dpcm) {
84 
85 		if (dpcm->fe == fe)
86 			continue;
87 
88 		state = dpcm->fe->dpcm[stream].state;
89 		for (i = 0; i < num_states; i++) {
90 			if (state == states[i]) {
91 				ret = 0;
92 				break;
93 			}
94 		}
95 	}
96 
97 	/* it's safe to do this BE DAI */
98 	return ret;
99 }
100 
101 /*
102  * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
103  * are not running, paused or suspended for the specified stream direction.
104  */
snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime * fe,struct snd_soc_pcm_runtime * be,int stream)105 static int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
106 					 struct snd_soc_pcm_runtime *be, int stream)
107 {
108 	const enum snd_soc_dpcm_state state[] = {
109 		SND_SOC_DPCM_STATE_START,
110 		SND_SOC_DPCM_STATE_PAUSED,
111 		SND_SOC_DPCM_STATE_SUSPEND,
112 	};
113 
114 	return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
115 }
116 
117 /*
118  * We can only change hw params a BE DAI if any of it's FE are not prepared,
119  * running, paused or suspended for the specified stream direction.
120  */
snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime * fe,struct snd_soc_pcm_runtime * be,int stream)121 static int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
122 				      struct snd_soc_pcm_runtime *be, int stream)
123 {
124 	const enum snd_soc_dpcm_state state[] = {
125 		SND_SOC_DPCM_STATE_START,
126 		SND_SOC_DPCM_STATE_PAUSED,
127 		SND_SOC_DPCM_STATE_SUSPEND,
128 		SND_SOC_DPCM_STATE_PREPARE,
129 	};
130 
131 	return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
132 }
133 
134 /*
135  * We can only prepare a BE DAI if any of it's FE are not prepared,
136  * running or paused for the specified stream direction.
137  */
snd_soc_dpcm_can_be_prepared(struct snd_soc_pcm_runtime * fe,struct snd_soc_pcm_runtime * be,int stream)138 static int snd_soc_dpcm_can_be_prepared(struct snd_soc_pcm_runtime *fe,
139 					struct snd_soc_pcm_runtime *be, int stream)
140 {
141 	const enum snd_soc_dpcm_state state[] = {
142 		SND_SOC_DPCM_STATE_START,
143 		SND_SOC_DPCM_STATE_PAUSED,
144 		SND_SOC_DPCM_STATE_PREPARE,
145 	};
146 
147 	return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
148 }
149 
150 #define DPCM_MAX_BE_USERS	8
151 
soc_cpu_dai_name(struct snd_soc_pcm_runtime * rtd)152 static inline const char *soc_cpu_dai_name(struct snd_soc_pcm_runtime *rtd)
153 {
154 	return (rtd)->dai_link->num_cpus == 1 ? snd_soc_rtd_to_cpu(rtd, 0)->name : "multicpu";
155 }
soc_codec_dai_name(struct snd_soc_pcm_runtime * rtd)156 static inline const char *soc_codec_dai_name(struct snd_soc_pcm_runtime *rtd)
157 {
158 	return (rtd)->dai_link->num_codecs == 1 ? snd_soc_rtd_to_codec(rtd, 0)->name : "multicodec";
159 }
160 
161 #ifdef CONFIG_DEBUG_FS
dpcm_state_string(enum snd_soc_dpcm_state state)162 static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
163 {
164 	switch (state) {
165 	case SND_SOC_DPCM_STATE_NEW:
166 		return "new";
167 	case SND_SOC_DPCM_STATE_OPEN:
168 		return "open";
169 	case SND_SOC_DPCM_STATE_HW_PARAMS:
170 		return "hw_params";
171 	case SND_SOC_DPCM_STATE_PREPARE:
172 		return "prepare";
173 	case SND_SOC_DPCM_STATE_START:
174 		return "start";
175 	case SND_SOC_DPCM_STATE_STOP:
176 		return "stop";
177 	case SND_SOC_DPCM_STATE_SUSPEND:
178 		return "suspend";
179 	case SND_SOC_DPCM_STATE_PAUSED:
180 		return "paused";
181 	case SND_SOC_DPCM_STATE_HW_FREE:
182 		return "hw_free";
183 	case SND_SOC_DPCM_STATE_CLOSE:
184 		return "close";
185 	}
186 
187 	return "unknown";
188 }
189 
dpcm_show_state(struct snd_soc_pcm_runtime * fe,int stream,char * buf,size_t size)190 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
191 			       int stream, char *buf, size_t size)
192 {
193 	struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
194 	struct snd_soc_dpcm *dpcm;
195 	ssize_t offset = 0;
196 
197 	/* FE state */
198 	offset += scnprintf(buf + offset, size - offset,
199 			   "[%s - %s]\n", fe->dai_link->name,
200 			   stream ? "Capture" : "Playback");
201 
202 	offset += scnprintf(buf + offset, size - offset, "State: %s\n",
203 			   dpcm_state_string(fe->dpcm[stream].state));
204 
205 	if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
206 	    (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
207 		offset += scnprintf(buf + offset, size - offset,
208 				   "Hardware Params: "
209 				   "Format = %s, Channels = %d, Rate = %d\n",
210 				   snd_pcm_format_name(params_format(params)),
211 				   params_channels(params),
212 				   params_rate(params));
213 
214 	/* BEs state */
215 	offset += scnprintf(buf + offset, size - offset, "Backends:\n");
216 
217 	if (list_empty(&fe->dpcm[stream].be_clients)) {
218 		offset += scnprintf(buf + offset, size - offset,
219 				   " No active DSP links\n");
220 		goto out;
221 	}
222 
223 	for_each_dpcm_be(fe, stream, dpcm) {
224 		struct snd_soc_pcm_runtime *be = dpcm->be;
225 		params = &be->dpcm[stream].hw_params;
226 
227 		offset += scnprintf(buf + offset, size - offset,
228 				   "- %s\n", be->dai_link->name);
229 
230 		offset += scnprintf(buf + offset, size - offset,
231 				   "   State: %s\n",
232 				   dpcm_state_string(be->dpcm[stream].state));
233 
234 		if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
235 		    (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
236 			offset += scnprintf(buf + offset, size - offset,
237 					   "   Hardware Params: "
238 					   "Format = %s, Channels = %d, Rate = %d\n",
239 					   snd_pcm_format_name(params_format(params)),
240 					   params_channels(params),
241 					   params_rate(params));
242 	}
243 out:
244 	return offset;
245 }
246 
dpcm_state_read_file(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)247 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
248 				    size_t count, loff_t *ppos)
249 {
250 	struct snd_soc_pcm_runtime *fe = file->private_data;
251 	ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
252 	int stream;
253 	char *buf;
254 
255 	if (fe->dai_link->num_cpus > 1) {
256 		dev_err(fe->dev,
257 			"%s doesn't support Multi CPU yet\n", __func__);
258 		return -EINVAL;
259 	}
260 
261 	buf = kmalloc(out_count, GFP_KERNEL);
262 	if (!buf)
263 		return -ENOMEM;
264 
265 	snd_soc_dpcm_mutex_lock(fe);
266 	for_each_pcm_streams(stream)
267 		if (snd_soc_dai_stream_valid(snd_soc_rtd_to_cpu(fe, 0), stream))
268 			offset += dpcm_show_state(fe, stream,
269 						  buf + offset,
270 						  out_count - offset);
271 	snd_soc_dpcm_mutex_unlock(fe);
272 
273 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
274 
275 	kfree(buf);
276 	return ret;
277 }
278 
279 static const struct file_operations dpcm_state_fops = {
280 	.open = simple_open,
281 	.read = dpcm_state_read_file,
282 	.llseek = default_llseek,
283 };
284 
soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime * rtd)285 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
286 {
287 	if (!rtd->dai_link->dynamic)
288 		return;
289 
290 	if (!rtd->card->debugfs_card_root)
291 		return;
292 
293 	rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
294 						    rtd->card->debugfs_card_root);
295 
296 	debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
297 			    rtd, &dpcm_state_fops);
298 }
299 
dpcm_create_debugfs_state(struct snd_soc_dpcm * dpcm,int stream)300 static void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, int stream)
301 {
302 	char *name;
303 
304 	name = kasprintf(GFP_KERNEL, "%s:%s", dpcm->be->dai_link->name,
305 			 snd_pcm_direction_name(stream));
306 	if (name) {
307 		dpcm->debugfs_state = debugfs_create_dir(
308 			name, dpcm->fe->debugfs_dpcm_root);
309 		debugfs_create_u32("state", 0644, dpcm->debugfs_state,
310 				   &dpcm->state);
311 		kfree(name);
312 	}
313 }
314 
dpcm_remove_debugfs_state(struct snd_soc_dpcm * dpcm)315 static void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
316 {
317 	debugfs_remove_recursive(dpcm->debugfs_state);
318 }
319 
320 #else
dpcm_create_debugfs_state(struct snd_soc_dpcm * dpcm,int stream)321 static inline void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm,
322 					     int stream)
323 {
324 }
325 
dpcm_remove_debugfs_state(struct snd_soc_dpcm * dpcm)326 static inline void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
327 {
328 }
329 #endif
330 
331 /* Set FE's runtime_update state; the state is protected via PCM stream lock
332  * for avoiding the race with trigger callback.
333  * If the state is unset and a trigger is pending while the previous operation,
334  * process the pending trigger action here.
335  */
336 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
dpcm_set_fe_update_state(struct snd_soc_pcm_runtime * fe,int stream,enum snd_soc_dpcm_update state)337 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
338 				     int stream, enum snd_soc_dpcm_update state)
339 {
340 	struct snd_pcm_substream *substream =
341 		snd_soc_dpcm_get_substream(fe, stream);
342 
343 	snd_pcm_stream_lock_irq(substream);
344 	if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
345 		dpcm_fe_dai_do_trigger(substream,
346 				       fe->dpcm[stream].trigger_pending - 1);
347 		fe->dpcm[stream].trigger_pending = 0;
348 	}
349 	fe->dpcm[stream].runtime_update = state;
350 	snd_pcm_stream_unlock_irq(substream);
351 }
352 
dpcm_set_be_update_state(struct snd_soc_pcm_runtime * be,int stream,enum snd_soc_dpcm_update state)353 static void dpcm_set_be_update_state(struct snd_soc_pcm_runtime *be,
354 				     int stream, enum snd_soc_dpcm_update state)
355 {
356 	be->dpcm[stream].runtime_update = state;
357 }
358 
359 /**
360  * snd_soc_runtime_action() - Increment/Decrement active count for
361  * PCM runtime components
362  * @rtd: ASoC PCM runtime that is activated
363  * @stream: Direction of the PCM stream
364  * @action: Activate stream if 1. Deactivate if -1.
365  *
366  * Increments/Decrements the active count for all the DAIs and components
367  * attached to a PCM runtime.
368  * Should typically be called when a stream is opened.
369  *
370  * Must be called with the rtd->card->pcm_mutex being held
371  */
snd_soc_runtime_action(struct snd_soc_pcm_runtime * rtd,int stream,int action)372 void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd,
373 			    int stream, int action)
374 {
375 	struct snd_soc_component *component;
376 	struct snd_soc_dai *dai;
377 	int i;
378 
379 	snd_soc_dpcm_mutex_assert_held(rtd);
380 
381 	for_each_rtd_dais(rtd, i, dai)
382 		snd_soc_dai_action(dai, stream, action);
383 
384 	/* Increments/Decrements the active count for components without DAIs */
385 	for_each_rtd_components(rtd, i, component) {
386 		if (component->num_dai)
387 			continue;
388 		component->active += action;
389 	}
390 }
391 EXPORT_SYMBOL_GPL(snd_soc_runtime_action);
392 
393 /**
394  * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
395  * @rtd: The ASoC PCM runtime that should be checked.
396  *
397  * This function checks whether the power down delay should be ignored for a
398  * specific PCM runtime. Returns true if the delay is 0, if the DAI link has
399  * been configured to ignore the delay, or if none of the components benefits
400  * from having the delay.
401  */
snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime * rtd)402 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
403 {
404 	struct snd_soc_component *component;
405 	int i;
406 
407 	if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
408 		return true;
409 
410 	for_each_rtd_components(rtd, i, component)
411 		if (component->driver->use_pmdown_time)
412 			/* No need to go through all components */
413 			return false;
414 
415 	return true;
416 }
417 
418 /**
419  * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
420  * @substream: the pcm substream
421  * @hw: the hardware parameters
422  *
423  * Sets the substream runtime hardware parameters.
424  */
snd_soc_set_runtime_hwparams(struct snd_pcm_substream * substream,const struct snd_pcm_hardware * hw)425 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
426 	const struct snd_pcm_hardware *hw)
427 {
428 	substream->runtime->hw = *hw;
429 
430 	return 0;
431 }
432 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
433 
434 /* DPCM stream event, send event to FE and all active BEs. */
dpcm_dapm_stream_event(struct snd_soc_pcm_runtime * fe,int dir,int event)435 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
436 	int event)
437 {
438 	struct snd_soc_dpcm *dpcm;
439 
440 	snd_soc_dpcm_mutex_assert_held(fe);
441 
442 	for_each_dpcm_be(fe, dir, dpcm) {
443 
444 		struct snd_soc_pcm_runtime *be = dpcm->be;
445 
446 		dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
447 				be->dai_link->name, event, dir);
448 
449 		if ((event == SND_SOC_DAPM_STREAM_STOP) &&
450 		    (be->dpcm[dir].users >= 1))
451 			continue;
452 
453 		snd_soc_dapm_stream_event(be, dir, event);
454 	}
455 
456 	snd_soc_dapm_stream_event(fe, dir, event);
457 
458 	return 0;
459 }
460 
soc_pcm_set_dai_params(struct snd_soc_dai * dai,struct snd_pcm_hw_params * params)461 static void soc_pcm_set_dai_params(struct snd_soc_dai *dai,
462 				   struct snd_pcm_hw_params *params)
463 {
464 	if (params) {
465 		dai->rate	 = params_rate(params);
466 		dai->channels	 = params_channels(params);
467 		dai->sample_bits = snd_pcm_format_physical_width(params_format(params));
468 	} else {
469 		dai->rate	 = 0;
470 		dai->channels	 = 0;
471 		dai->sample_bits = 0;
472 	}
473 }
474 
soc_pcm_apply_symmetry(struct snd_pcm_substream * substream,struct snd_soc_dai * soc_dai)475 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
476 					struct snd_soc_dai *soc_dai)
477 {
478 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
479 	int ret;
480 
481 	if (!snd_soc_dai_active(soc_dai))
482 		return 0;
483 
484 #define __soc_pcm_apply_symmetry(name, NAME)				\
485 	if (soc_dai->name && (soc_dai->driver->symmetric_##name ||	\
486 			      rtd->dai_link->symmetric_##name)) {	\
487 		dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %s to %d\n",\
488 			#name, soc_dai->name);				\
489 									\
490 		ret = snd_pcm_hw_constraint_single(substream->runtime,	\
491 						   SNDRV_PCM_HW_PARAM_##NAME,\
492 						   soc_dai->name);	\
493 		if (ret < 0) {						\
494 			dev_err(soc_dai->dev,				\
495 				"ASoC: Unable to apply %s constraint: %d\n",\
496 				#name, ret);				\
497 			return ret;					\
498 		}							\
499 	}
500 
501 	__soc_pcm_apply_symmetry(rate,		RATE);
502 	__soc_pcm_apply_symmetry(channels,	CHANNELS);
503 	__soc_pcm_apply_symmetry(sample_bits,	SAMPLE_BITS);
504 
505 	return 0;
506 }
507 
soc_pcm_params_symmetry(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)508 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
509 				struct snd_pcm_hw_params *params)
510 {
511 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
512 	struct snd_soc_dai d;
513 	struct snd_soc_dai *dai;
514 	struct snd_soc_dai *cpu_dai;
515 	unsigned int symmetry, i;
516 
517 	d.name = __func__;
518 	soc_pcm_set_dai_params(&d, params);
519 
520 #define __soc_pcm_params_symmetry(xxx)					\
521 	symmetry = rtd->dai_link->symmetric_##xxx;			\
522 	for_each_rtd_dais(rtd, i, dai)					\
523 		symmetry |= dai->driver->symmetric_##xxx;		\
524 									\
525 	if (symmetry)							\
526 		for_each_rtd_cpu_dais(rtd, i, cpu_dai)			\
527 			if (!snd_soc_dai_is_dummy(cpu_dai) &&		\
528 			    cpu_dai->xxx && cpu_dai->xxx != d.xxx) {	\
529 				dev_err(rtd->dev, "ASoC: unmatched %s symmetry: %s:%d - %s:%d\n", \
530 					#xxx, cpu_dai->name, cpu_dai->xxx, d.name, d.xxx); \
531 				return -EINVAL;				\
532 			}
533 
534 	/* reject unmatched parameters when applying symmetry */
535 	__soc_pcm_params_symmetry(rate);
536 	__soc_pcm_params_symmetry(channels);
537 	__soc_pcm_params_symmetry(sample_bits);
538 
539 	return 0;
540 }
541 
soc_pcm_update_symmetry(struct snd_pcm_substream * substream)542 static void soc_pcm_update_symmetry(struct snd_pcm_substream *substream)
543 {
544 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
545 	struct snd_soc_dai_link *link = rtd->dai_link;
546 	struct snd_soc_dai *dai;
547 	unsigned int symmetry, i;
548 
549 	symmetry = link->symmetric_rate ||
550 		link->symmetric_channels ||
551 		link->symmetric_sample_bits;
552 
553 	for_each_rtd_dais(rtd, i, dai)
554 		symmetry = symmetry ||
555 			dai->driver->symmetric_rate ||
556 			dai->driver->symmetric_channels ||
557 			dai->driver->symmetric_sample_bits;
558 
559 	if (symmetry)
560 		substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
561 }
562 
soc_pcm_set_msb(struct snd_pcm_substream * substream,int bits)563 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
564 {
565 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
566 	int ret;
567 
568 	if (!bits)
569 		return;
570 
571 	ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
572 	if (ret != 0)
573 		dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
574 				 bits, ret);
575 }
576 
soc_pcm_apply_msb(struct snd_pcm_substream * substream)577 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
578 {
579 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
580 	struct snd_soc_dai *cpu_dai;
581 	struct snd_soc_dai *codec_dai;
582 	int stream = substream->stream;
583 	int i;
584 	unsigned int bits = 0, cpu_bits = 0;
585 
586 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
587 		const struct snd_soc_pcm_stream *pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream);
588 
589 		if (pcm_codec->sig_bits == 0) {
590 			bits = 0;
591 			break;
592 		}
593 		bits = max(pcm_codec->sig_bits, bits);
594 	}
595 
596 	for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
597 		const struct snd_soc_pcm_stream *pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
598 
599 		if (pcm_cpu->sig_bits == 0) {
600 			cpu_bits = 0;
601 			break;
602 		}
603 		cpu_bits = max(pcm_cpu->sig_bits, cpu_bits);
604 	}
605 
606 	soc_pcm_set_msb(substream, bits);
607 	soc_pcm_set_msb(substream, cpu_bits);
608 }
609 
soc_pcm_hw_init(struct snd_pcm_hardware * hw)610 static void soc_pcm_hw_init(struct snd_pcm_hardware *hw)
611 {
612 	hw->rates		= UINT_MAX;
613 	hw->rate_min		= 0;
614 	hw->rate_max		= UINT_MAX;
615 	hw->channels_min	= 0;
616 	hw->channels_max	= UINT_MAX;
617 	hw->formats		= ULLONG_MAX;
618 }
619 
soc_pcm_hw_update_rate(struct snd_pcm_hardware * hw,const struct snd_soc_pcm_stream * p)620 static void soc_pcm_hw_update_rate(struct snd_pcm_hardware *hw,
621 				   const struct snd_soc_pcm_stream *p)
622 {
623 	hw->rates = snd_pcm_rate_mask_intersect(hw->rates, p->rates);
624 
625 	/* setup hw->rate_min/max via hw->rates first */
626 	snd_pcm_hw_limit_rates(hw);
627 
628 	/* update hw->rate_min/max by snd_soc_pcm_stream */
629 	hw->rate_min = max(hw->rate_min, p->rate_min);
630 	hw->rate_max = min_not_zero(hw->rate_max, p->rate_max);
631 }
632 
soc_pcm_hw_update_chan(struct snd_pcm_hardware * hw,const struct snd_soc_pcm_stream * p)633 static void soc_pcm_hw_update_chan(struct snd_pcm_hardware *hw,
634 				   const struct snd_soc_pcm_stream *p)
635 {
636 	hw->channels_min = max(hw->channels_min, p->channels_min);
637 	hw->channels_max = min(hw->channels_max, p->channels_max);
638 }
639 
soc_pcm_hw_update_format(struct snd_pcm_hardware * hw,const struct snd_soc_pcm_stream * p)640 static void soc_pcm_hw_update_format(struct snd_pcm_hardware *hw,
641 				     const struct snd_soc_pcm_stream *p)
642 {
643 	hw->formats &= p->formats;
644 }
645 
soc_pcm_hw_update_subformat(struct snd_pcm_hardware * hw,const struct snd_soc_pcm_stream * p)646 static void soc_pcm_hw_update_subformat(struct snd_pcm_hardware *hw,
647 					const struct snd_soc_pcm_stream *p)
648 {
649 	hw->subformats &= p->subformats;
650 }
651 
652 /**
653  * snd_soc_runtime_calc_hw() - Calculate hw limits for a PCM stream
654  * @rtd: ASoC PCM runtime
655  * @hw: PCM hardware parameters (output)
656  * @stream: Direction of the PCM stream
657  *
658  * Calculates the subset of stream parameters supported by all DAIs
659  * associated with the PCM stream.
660  */
snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_hardware * hw,int stream)661 int snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime *rtd,
662 			    struct snd_pcm_hardware *hw, int stream)
663 {
664 	struct snd_soc_dai *codec_dai;
665 	struct snd_soc_dai *cpu_dai;
666 	const struct snd_soc_pcm_stream *codec_stream;
667 	const struct snd_soc_pcm_stream *cpu_stream;
668 	unsigned int cpu_chan_min = 0, cpu_chan_max = UINT_MAX;
669 	int i;
670 
671 	soc_pcm_hw_init(hw);
672 
673 	/* first calculate min/max only for CPUs in the DAI link */
674 	for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
675 
676 		/*
677 		 * Skip CPUs which don't support the current stream type.
678 		 * Otherwise, since the rate, channel, and format values will
679 		 * zero in that case, we would have no usable settings left,
680 		 * causing the resulting setup to fail.
681 		 */
682 		if (!snd_soc_dai_stream_valid(cpu_dai, stream))
683 			continue;
684 
685 		cpu_stream = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
686 
687 		soc_pcm_hw_update_chan(hw, cpu_stream);
688 		soc_pcm_hw_update_rate(hw, cpu_stream);
689 		soc_pcm_hw_update_format(hw, cpu_stream);
690 		soc_pcm_hw_update_subformat(hw, cpu_stream);
691 	}
692 	cpu_chan_min = hw->channels_min;
693 	cpu_chan_max = hw->channels_max;
694 
695 	/* second calculate min/max only for CODECs in the DAI link */
696 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
697 
698 		/*
699 		 * Skip CODECs which don't support the current stream type.
700 		 * Otherwise, since the rate, channel, and format values will
701 		 * zero in that case, we would have no usable settings left,
702 		 * causing the resulting setup to fail.
703 		 */
704 		if (!snd_soc_dai_stream_valid(codec_dai, stream))
705 			continue;
706 
707 		codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream);
708 
709 		soc_pcm_hw_update_chan(hw, codec_stream);
710 		soc_pcm_hw_update_rate(hw, codec_stream);
711 		soc_pcm_hw_update_format(hw, codec_stream);
712 		soc_pcm_hw_update_subformat(hw, codec_stream);
713 	}
714 
715 	/* Verify both a valid CPU DAI and a valid CODEC DAI were found */
716 	if (!hw->channels_min)
717 		return -EINVAL;
718 
719 	/*
720 	 * chan min/max cannot be enforced if there are multiple CODEC DAIs
721 	 * connected to CPU DAI(s), use CPU DAI's directly and let
722 	 * channel allocation be fixed up later
723 	 */
724 	if (rtd->dai_link->num_codecs > 1) {
725 		hw->channels_min = cpu_chan_min;
726 		hw->channels_max = cpu_chan_max;
727 	}
728 
729 	return 0;
730 }
731 EXPORT_SYMBOL_GPL(snd_soc_runtime_calc_hw);
732 
soc_pcm_init_runtime_hw(struct snd_pcm_substream * substream)733 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
734 {
735 	struct snd_pcm_hardware *hw = &substream->runtime->hw;
736 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
737 	u64 formats = hw->formats;
738 
739 	/*
740 	 * At least one CPU and one CODEC should match. Otherwise, we should
741 	 * have bailed out on a higher level, since there would be no CPU or
742 	 * CODEC to support the transfer direction in that case.
743 	 */
744 	snd_soc_runtime_calc_hw(rtd, hw, substream->stream);
745 
746 	if (formats)
747 		hw->formats &= formats;
748 }
749 
soc_pcm_components_open(struct snd_pcm_substream * substream)750 static int soc_pcm_components_open(struct snd_pcm_substream *substream)
751 {
752 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
753 	struct snd_soc_component *component;
754 	int i, ret = 0;
755 
756 	for_each_rtd_components(rtd, i, component) {
757 		ret = snd_soc_component_module_get_when_open(component, substream);
758 		if (ret < 0)
759 			break;
760 
761 		ret = snd_soc_component_open(component, substream);
762 		if (ret < 0)
763 			break;
764 	}
765 
766 	return ret;
767 }
768 
soc_pcm_components_close(struct snd_pcm_substream * substream,int rollback)769 static int soc_pcm_components_close(struct snd_pcm_substream *substream,
770 				    int rollback)
771 {
772 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
773 	struct snd_soc_component *component;
774 	int i, ret = 0;
775 
776 	for_each_rtd_components(rtd, i, component) {
777 		int r = snd_soc_component_close(component, substream, rollback);
778 		if (r < 0)
779 			ret = r; /* use last ret */
780 
781 		snd_soc_component_module_put_when_close(component, substream, rollback);
782 	}
783 
784 	return ret;
785 }
786 
soc_pcm_clean(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_substream * substream,int rollback)787 static int soc_pcm_clean(struct snd_soc_pcm_runtime *rtd,
788 			 struct snd_pcm_substream *substream, int rollback)
789 {
790 	struct snd_soc_component *component;
791 	struct snd_soc_dai *dai;
792 	int i;
793 
794 	snd_soc_dpcm_mutex_assert_held(rtd);
795 
796 	if (!rollback) {
797 		snd_soc_runtime_deactivate(rtd, substream->stream);
798 
799 		/* Make sure DAI parameters cleared if the DAI becomes inactive */
800 		for_each_rtd_dais(rtd, i, dai) {
801 			if (snd_soc_dai_active(dai) == 0 &&
802 			    (dai->rate || dai->channels || dai->sample_bits))
803 				soc_pcm_set_dai_params(dai, NULL);
804 		}
805 	}
806 
807 	for_each_rtd_dais_reverse(rtd, i, dai)
808 		snd_soc_dai_shutdown(dai, substream, rollback);
809 
810 	snd_soc_link_shutdown(substream, rollback);
811 
812 	soc_pcm_components_close(substream, rollback);
813 
814 	snd_soc_pcm_component_pm_runtime_put(rtd, substream, rollback);
815 
816 	for_each_rtd_components(rtd, i, component)
817 		if (!snd_soc_component_active(component))
818 			pinctrl_pm_select_sleep_state(component->dev);
819 
820 	return 0;
821 }
822 
823 /*
824  * Called by ALSA when a PCM substream is closed. Private data can be
825  * freed here. The cpu DAI, codec DAI, machine and components are also
826  * shutdown.
827  */
__soc_pcm_close(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_substream * substream)828 static int __soc_pcm_close(struct snd_soc_pcm_runtime *rtd,
829 			   struct snd_pcm_substream *substream)
830 {
831 	return soc_pcm_clean(rtd, substream, 0);
832 }
833 
834 /* PCM close ops for non-DPCM streams */
soc_pcm_close(struct snd_pcm_substream * substream)835 static int soc_pcm_close(struct snd_pcm_substream *substream)
836 {
837 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
838 
839 	snd_soc_dpcm_mutex_lock(rtd);
840 	__soc_pcm_close(rtd, substream);
841 	snd_soc_dpcm_mutex_unlock(rtd);
842 	return 0;
843 }
844 
soc_hw_sanity_check(struct snd_pcm_substream * substream)845 static int soc_hw_sanity_check(struct snd_pcm_substream *substream)
846 {
847 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
848 	struct snd_pcm_hardware *hw = &substream->runtime->hw;
849 	const char *name_cpu = soc_cpu_dai_name(rtd);
850 	const char *name_codec = soc_codec_dai_name(rtd);
851 	const char *err_msg;
852 	struct device *dev = rtd->dev;
853 
854 	err_msg = "rates";
855 	if (!hw->rates)
856 		goto config_err;
857 
858 	err_msg = "formats";
859 	if (!hw->formats)
860 		goto config_err;
861 
862 	err_msg = "channels";
863 	if (!hw->channels_min || !hw->channels_max ||
864 	     hw->channels_min  >  hw->channels_max)
865 		goto config_err;
866 
867 	dev_dbg(dev, "ASoC: %s <-> %s info:\n",		name_codec,
868 							name_cpu);
869 	dev_dbg(dev, "ASoC: rate mask 0x%x\n",		hw->rates);
870 	dev_dbg(dev, "ASoC: ch   min %d max %d\n",	hw->channels_min,
871 							hw->channels_max);
872 	dev_dbg(dev, "ASoC: rate min %d max %d\n",	hw->rate_min,
873 							hw->rate_max);
874 
875 	return 0;
876 
877 config_err:
878 	dev_err(dev, "ASoC: %s <-> %s No matching %s\n",
879 		name_codec, name_cpu, err_msg);
880 	return -EINVAL;
881 }
882 
883 /*
884  * Called by ALSA when a PCM substream is opened, the runtime->hw record is
885  * then initialized and any private data can be allocated. This also calls
886  * startup for the cpu DAI, component, machine and codec DAI.
887  */
__soc_pcm_open(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_substream * substream)888 static int __soc_pcm_open(struct snd_soc_pcm_runtime *rtd,
889 			  struct snd_pcm_substream *substream)
890 {
891 	struct snd_soc_component *component;
892 	struct snd_soc_dai *dai;
893 	int i, ret = 0;
894 
895 	snd_soc_dpcm_mutex_assert_held(rtd);
896 
897 	for_each_rtd_components(rtd, i, component)
898 		pinctrl_pm_select_default_state(component->dev);
899 
900 	ret = snd_soc_pcm_component_pm_runtime_get(rtd, substream);
901 	if (ret < 0)
902 		goto err;
903 
904 	ret = soc_pcm_components_open(substream);
905 	if (ret < 0)
906 		goto err;
907 
908 	ret = snd_soc_link_startup(substream);
909 	if (ret < 0)
910 		goto err;
911 
912 	/* startup the audio subsystem */
913 	for_each_rtd_dais(rtd, i, dai) {
914 		ret = snd_soc_dai_startup(dai, substream);
915 		if (ret < 0)
916 			goto err;
917 	}
918 
919 	/* Dynamic PCM DAI links compat checks use dynamic capabilities */
920 	if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
921 		goto dynamic;
922 
923 	/* Check that the codec and cpu DAIs are compatible */
924 	soc_pcm_init_runtime_hw(substream);
925 
926 	soc_pcm_update_symmetry(substream);
927 
928 	ret = soc_hw_sanity_check(substream);
929 	if (ret < 0)
930 		goto err;
931 
932 	soc_pcm_apply_msb(substream);
933 
934 	/* Symmetry only applies if we've already got an active stream. */
935 	for_each_rtd_dais(rtd, i, dai) {
936 		ret = soc_pcm_apply_symmetry(substream, dai);
937 		if (ret != 0)
938 			goto err;
939 	}
940 dynamic:
941 	snd_soc_runtime_activate(rtd, substream->stream);
942 	ret = 0;
943 err:
944 	if (ret < 0)
945 		soc_pcm_clean(rtd, substream, 1);
946 
947 	return soc_pcm_ret(rtd, ret);
948 }
949 
950 /* PCM open ops for non-DPCM streams */
soc_pcm_open(struct snd_pcm_substream * substream)951 static int soc_pcm_open(struct snd_pcm_substream *substream)
952 {
953 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
954 	int ret;
955 
956 	snd_soc_dpcm_mutex_lock(rtd);
957 	ret = __soc_pcm_open(rtd, substream);
958 	snd_soc_dpcm_mutex_unlock(rtd);
959 	return ret;
960 }
961 
962 /*
963  * Called by ALSA when the PCM substream is prepared, can set format, sample
964  * rate, etc.  This function is non atomic and can be called multiple times,
965  * it can refer to the runtime info.
966  */
__soc_pcm_prepare(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_substream * substream)967 static int __soc_pcm_prepare(struct snd_soc_pcm_runtime *rtd,
968 			     struct snd_pcm_substream *substream)
969 {
970 	struct snd_soc_dai *dai;
971 	int i, ret = 0;
972 
973 	snd_soc_dpcm_mutex_assert_held(rtd);
974 
975 	ret = snd_soc_link_prepare(substream);
976 	if (ret < 0)
977 		goto out;
978 
979 	ret = snd_soc_pcm_component_prepare(substream);
980 	if (ret < 0)
981 		goto out;
982 
983 	ret = snd_soc_pcm_dai_prepare(substream);
984 	if (ret < 0)
985 		goto out;
986 
987 	/* cancel any delayed stream shutdown that is pending */
988 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
989 	    rtd->pop_wait) {
990 		rtd->pop_wait = 0;
991 		cancel_delayed_work(&rtd->delayed_work);
992 	}
993 
994 	snd_soc_dapm_stream_event(rtd, substream->stream,
995 			SND_SOC_DAPM_STREAM_START);
996 
997 	for_each_rtd_dais(rtd, i, dai) {
998 		if (dai->driver->ops && !dai->driver->ops->mute_unmute_on_trigger)
999 			snd_soc_dai_digital_mute(dai, 0, substream->stream);
1000 	}
1001 
1002 out:
1003 	/*
1004 	 * Don't use soc_pcm_ret() on .prepare callback to lower error log severity
1005 	 *
1006 	 * We don't want to log an error since we do not want to give userspace a way to do a
1007 	 * denial-of-service attack on the syslog / diskspace.
1008 	 */
1009 	return ret;
1010 }
1011 
1012 /* PCM prepare ops for non-DPCM streams */
soc_pcm_prepare(struct snd_pcm_substream * substream)1013 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
1014 {
1015 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1016 	int ret;
1017 
1018 	snd_soc_dpcm_mutex_lock(rtd);
1019 	ret = __soc_pcm_prepare(rtd, substream);
1020 	snd_soc_dpcm_mutex_unlock(rtd);
1021 
1022 	/*
1023 	 * Don't use soc_pcm_ret() on .prepare callback to lower error log severity
1024 	 *
1025 	 * We don't want to log an error since we do not want to give userspace a way to do a
1026 	 * denial-of-service attack on the syslog / diskspace.
1027 	 */
1028 	return ret;
1029 }
1030 
soc_pcm_codec_params_fixup(struct snd_pcm_hw_params * params,unsigned int mask)1031 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
1032 				       unsigned int mask)
1033 {
1034 	struct snd_interval *interval;
1035 	int channels = hweight_long(mask);
1036 
1037 	interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1038 	interval->min = channels;
1039 	interval->max = channels;
1040 }
1041 
soc_pcm_hw_clean(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_substream * substream,int rollback)1042 static int soc_pcm_hw_clean(struct snd_soc_pcm_runtime *rtd,
1043 			    struct snd_pcm_substream *substream, int rollback)
1044 {
1045 	struct snd_soc_dai *dai;
1046 	int i;
1047 
1048 	snd_soc_dpcm_mutex_assert_held(rtd);
1049 
1050 	/* clear the corresponding DAIs parameters when going to be inactive */
1051 	for_each_rtd_dais(rtd, i, dai) {
1052 		if (snd_soc_dai_active(dai) == 1)
1053 			soc_pcm_set_dai_params(dai, NULL);
1054 
1055 		if (snd_soc_dai_stream_active(dai, substream->stream) == 1) {
1056 			if (dai->driver->ops && !dai->driver->ops->mute_unmute_on_trigger)
1057 				snd_soc_dai_digital_mute(dai, 1, substream->stream);
1058 		}
1059 	}
1060 
1061 	/* run the stream event */
1062 	snd_soc_dapm_stream_stop(rtd, substream->stream);
1063 
1064 	/* free any machine hw params */
1065 	snd_soc_link_hw_free(substream, rollback);
1066 
1067 	/* free any component resources */
1068 	snd_soc_pcm_component_hw_free(substream, rollback);
1069 
1070 	/* now free hw params for the DAIs  */
1071 	for_each_rtd_dais(rtd, i, dai)
1072 		if (snd_soc_dai_stream_valid(dai, substream->stream))
1073 			snd_soc_dai_hw_free(dai, substream, rollback);
1074 
1075 	return 0;
1076 }
1077 
1078 /*
1079  * Frees resources allocated by hw_params, can be called multiple times
1080  */
__soc_pcm_hw_free(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_substream * substream)1081 static int __soc_pcm_hw_free(struct snd_soc_pcm_runtime *rtd,
1082 			     struct snd_pcm_substream *substream)
1083 {
1084 	return soc_pcm_hw_clean(rtd, substream, 0);
1085 }
1086 
1087 /* hw_free PCM ops for non-DPCM streams */
soc_pcm_hw_free(struct snd_pcm_substream * substream)1088 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1089 {
1090 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1091 	int ret;
1092 
1093 	snd_soc_dpcm_mutex_lock(rtd);
1094 	ret = __soc_pcm_hw_free(rtd, substream);
1095 	snd_soc_dpcm_mutex_unlock(rtd);
1096 	return ret;
1097 }
1098 
1099 /*
1100  * Called by ALSA when the hardware params are set by application. This
1101  * function can also be called multiple times and can allocate buffers
1102  * (using snd_pcm_lib_* ). It's non-atomic.
1103  */
__soc_pcm_hw_params(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)1104 static int __soc_pcm_hw_params(struct snd_soc_pcm_runtime *rtd,
1105 			       struct snd_pcm_substream *substream,
1106 			       struct snd_pcm_hw_params *params)
1107 {
1108 	struct snd_soc_dai *cpu_dai;
1109 	struct snd_soc_dai *codec_dai;
1110 	struct snd_pcm_hw_params tmp_params;
1111 	int i, ret = 0;
1112 
1113 	snd_soc_dpcm_mutex_assert_held(rtd);
1114 
1115 	ret = soc_pcm_params_symmetry(substream, params);
1116 	if (ret)
1117 		goto out;
1118 
1119 	ret = snd_soc_link_hw_params(substream, params);
1120 	if (ret < 0)
1121 		goto out;
1122 
1123 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
1124 		unsigned int tdm_mask = snd_soc_dai_tdm_mask_get(codec_dai, substream->stream);
1125 
1126 		/*
1127 		 * Skip CODECs which don't support the current stream type,
1128 		 * the idea being that if a CODEC is not used for the currently
1129 		 * set up transfer direction, it should not need to be
1130 		 * configured, especially since the configuration used might
1131 		 * not even be supported by that CODEC. There may be cases
1132 		 * however where a CODEC needs to be set up although it is
1133 		 * actually not being used for the transfer, e.g. if a
1134 		 * capture-only CODEC is acting as an LRCLK and/or BCLK master
1135 		 * for the DAI link including a playback-only CODEC.
1136 		 * If this becomes necessary, we will have to augment the
1137 		 * machine driver setup with information on how to act, so
1138 		 * we can do the right thing here.
1139 		 */
1140 		if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1141 			continue;
1142 
1143 		/* copy params for each codec */
1144 		tmp_params = *params;
1145 
1146 		/* fixup params based on TDM slot masks */
1147 		if (tdm_mask)
1148 			soc_pcm_codec_params_fixup(&tmp_params, tdm_mask);
1149 
1150 		ret = snd_soc_dai_hw_params(codec_dai, substream,
1151 					    &tmp_params);
1152 		if(ret < 0)
1153 			goto out;
1154 
1155 		soc_pcm_set_dai_params(codec_dai, &tmp_params);
1156 		snd_soc_dapm_update_dai(substream, &tmp_params, codec_dai);
1157 	}
1158 
1159 	for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1160 		struct snd_soc_dai_link_ch_map *ch_maps;
1161 		unsigned int ch_mask = 0;
1162 		int j;
1163 
1164 		/*
1165 		 * Skip CPUs which don't support the current stream
1166 		 * type. See soc_pcm_init_runtime_hw() for more details
1167 		 */
1168 		if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
1169 			continue;
1170 
1171 		/* copy params for each cpu */
1172 		tmp_params = *params;
1173 
1174 		/*
1175 		 * construct cpu channel mask by combining ch_mask of each
1176 		 * codec which maps to the cpu.
1177 		 * see
1178 		 *	soc.h :: [dai_link->ch_maps Image sample]
1179 		 */
1180 		for_each_rtd_ch_maps(rtd, j, ch_maps)
1181 			if (ch_maps->cpu == i)
1182 				ch_mask |= ch_maps->ch_mask;
1183 
1184 		/* fixup cpu channel number */
1185 		if (ch_mask)
1186 			soc_pcm_codec_params_fixup(&tmp_params, ch_mask);
1187 
1188 		ret = snd_soc_dai_hw_params(cpu_dai, substream, &tmp_params);
1189 		if (ret < 0)
1190 			goto out;
1191 
1192 		/* store the parameters for each DAI */
1193 		soc_pcm_set_dai_params(cpu_dai, &tmp_params);
1194 		snd_soc_dapm_update_dai(substream, &tmp_params, cpu_dai);
1195 	}
1196 
1197 	ret = snd_soc_pcm_component_hw_params(substream, params);
1198 out:
1199 	if (ret < 0)
1200 		soc_pcm_hw_clean(rtd, substream, 1);
1201 
1202 	return soc_pcm_ret(rtd, ret);
1203 }
1204 
1205 /* hw_params PCM ops for non-DPCM streams */
soc_pcm_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)1206 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
1207 			     struct snd_pcm_hw_params *params)
1208 {
1209 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1210 	int ret;
1211 
1212 	snd_soc_dpcm_mutex_lock(rtd);
1213 	ret = __soc_pcm_hw_params(rtd, substream, params);
1214 	snd_soc_dpcm_mutex_unlock(rtd);
1215 	return ret;
1216 }
1217 
1218 #define TRIGGER_MAX 3
1219 static int (* const trigger[][TRIGGER_MAX])(struct snd_pcm_substream *substream, int cmd, int rollback) = {
1220 	[SND_SOC_TRIGGER_ORDER_DEFAULT] = {
1221 		snd_soc_link_trigger,
1222 		snd_soc_pcm_component_trigger,
1223 		snd_soc_pcm_dai_trigger,
1224 	},
1225 	[SND_SOC_TRIGGER_ORDER_LDC] = {
1226 		snd_soc_link_trigger,
1227 		snd_soc_pcm_dai_trigger,
1228 		snd_soc_pcm_component_trigger,
1229 	},
1230 };
1231 
soc_pcm_trigger(struct snd_pcm_substream * substream,int cmd)1232 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1233 {
1234 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1235 	struct snd_soc_component *component;
1236 	int ret = 0, r = 0, i;
1237 	int rollback = 0;
1238 	int start = 0, stop = 0;
1239 
1240 	/*
1241 	 * select START/STOP sequence
1242 	 */
1243 	for_each_rtd_components(rtd, i, component) {
1244 		if (component->driver->trigger_start)
1245 			start = component->driver->trigger_start;
1246 		if (component->driver->trigger_stop)
1247 			stop = component->driver->trigger_stop;
1248 	}
1249 	if (rtd->dai_link->trigger_start)
1250 		start = rtd->dai_link->trigger_start;
1251 	if (rtd->dai_link->trigger_stop)
1252 		stop  = rtd->dai_link->trigger_stop;
1253 
1254 	if (start < 0 || start >= SND_SOC_TRIGGER_ORDER_MAX ||
1255 	    stop  < 0 || stop  >= SND_SOC_TRIGGER_ORDER_MAX)
1256 		return -EINVAL;
1257 
1258 	/*
1259 	 * START
1260 	 */
1261 	switch (cmd) {
1262 	case SNDRV_PCM_TRIGGER_START:
1263 	case SNDRV_PCM_TRIGGER_RESUME:
1264 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1265 		for (i = 0; i < TRIGGER_MAX; i++) {
1266 			r = trigger[start][i](substream, cmd, 0);
1267 			if (r < 0)
1268 				break;
1269 		}
1270 	}
1271 
1272 	/*
1273 	 * Rollback if START failed
1274 	 * find correspond STOP command
1275 	 */
1276 	if (r < 0) {
1277 		rollback = 1;
1278 		ret = r;
1279 		switch (cmd) {
1280 		case SNDRV_PCM_TRIGGER_START:
1281 			cmd = SNDRV_PCM_TRIGGER_STOP;
1282 			break;
1283 		case SNDRV_PCM_TRIGGER_RESUME:
1284 			cmd = SNDRV_PCM_TRIGGER_SUSPEND;
1285 			break;
1286 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1287 			cmd = SNDRV_PCM_TRIGGER_PAUSE_PUSH;
1288 			break;
1289 		}
1290 	}
1291 
1292 	/*
1293 	 * STOP
1294 	 */
1295 	switch (cmd) {
1296 	case SNDRV_PCM_TRIGGER_STOP:
1297 	case SNDRV_PCM_TRIGGER_SUSPEND:
1298 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1299 		for (i = TRIGGER_MAX; i > 0; i--) {
1300 			r = trigger[stop][i - 1](substream, cmd, rollback);
1301 			if (r < 0)
1302 				ret = r;
1303 		}
1304 	}
1305 
1306 	return ret;
1307 }
1308 
1309 /*
1310  * soc level wrapper for pointer callback
1311  * If cpu_dai, codec_dai, component driver has the delay callback, then
1312  * the runtime->delay will be updated via snd_soc_pcm_component/dai_delay().
1313  */
soc_pcm_pointer(struct snd_pcm_substream * substream)1314 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1315 {
1316 	struct snd_pcm_runtime *runtime = substream->runtime;
1317 	snd_pcm_uframes_t offset = 0;
1318 	snd_pcm_sframes_t codec_delay = 0;
1319 	snd_pcm_sframes_t cpu_delay = 0;
1320 
1321 	offset = snd_soc_pcm_component_pointer(substream);
1322 
1323 	/* should be called *after* snd_soc_pcm_component_pointer() */
1324 	snd_soc_pcm_dai_delay(substream, &cpu_delay, &codec_delay);
1325 	snd_soc_pcm_component_delay(substream, &cpu_delay, &codec_delay);
1326 
1327 	runtime->delay = cpu_delay + codec_delay;
1328 
1329 	return offset;
1330 }
1331 
1332 /* connect a FE and BE */
dpcm_be_connect(struct snd_soc_pcm_runtime * fe,struct snd_soc_pcm_runtime * be,int stream)1333 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1334 		struct snd_soc_pcm_runtime *be, int stream)
1335 {
1336 	struct snd_pcm_substream *fe_substream;
1337 	struct snd_pcm_substream *be_substream;
1338 	struct snd_soc_dpcm *dpcm;
1339 
1340 	snd_soc_dpcm_mutex_assert_held(fe);
1341 
1342 	/* only add new dpcms */
1343 	for_each_dpcm_be(fe, stream, dpcm) {
1344 		if (dpcm->be == be && dpcm->fe == fe)
1345 			return 0;
1346 	}
1347 
1348 	fe_substream = snd_soc_dpcm_get_substream(fe, stream);
1349 	be_substream = snd_soc_dpcm_get_substream(be, stream);
1350 
1351 	if (!fe_substream->pcm->nonatomic && be_substream->pcm->nonatomic) {
1352 		dev_err(be->dev, "%s: FE is atomic but BE is nonatomic, invalid configuration\n",
1353 			__func__);
1354 		return -EINVAL;
1355 	}
1356 	if (fe_substream->pcm->nonatomic && !be_substream->pcm->nonatomic) {
1357 		dev_dbg(be->dev, "FE is nonatomic but BE is not, forcing BE as nonatomic\n");
1358 		be_substream->pcm->nonatomic = 1;
1359 	}
1360 
1361 	dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1362 	if (!dpcm)
1363 		return -ENOMEM;
1364 
1365 	dpcm->be = be;
1366 	dpcm->fe = fe;
1367 	dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1368 	snd_pcm_stream_lock_irq(fe_substream);
1369 	list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1370 	list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1371 	snd_pcm_stream_unlock_irq(fe_substream);
1372 
1373 	dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1374 			snd_pcm_direction_name(stream),  fe->dai_link->name,
1375 			stream ? "<-" : "->", be->dai_link->name);
1376 
1377 	dpcm_create_debugfs_state(dpcm, stream);
1378 
1379 	return 1;
1380 }
1381 
1382 /* reparent a BE onto another FE */
dpcm_be_reparent(struct snd_soc_pcm_runtime * fe,struct snd_soc_pcm_runtime * be,int stream)1383 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1384 			struct snd_soc_pcm_runtime *be, int stream)
1385 {
1386 	struct snd_soc_dpcm *dpcm;
1387 	struct snd_pcm_substream *fe_substream, *be_substream;
1388 
1389 	/* reparent if BE is connected to other FEs */
1390 	if (!be->dpcm[stream].users)
1391 		return;
1392 
1393 	be_substream = snd_soc_dpcm_get_substream(be, stream);
1394 	if (!be_substream)
1395 		return;
1396 
1397 	for_each_dpcm_fe(be, stream, dpcm) {
1398 		if (dpcm->fe == fe)
1399 			continue;
1400 
1401 		dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1402 			snd_pcm_direction_name(stream),
1403 			dpcm->fe->dai_link->name,
1404 			stream ? "<-" : "->", dpcm->be->dai_link->name);
1405 
1406 		fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1407 		be_substream->runtime = fe_substream->runtime;
1408 		break;
1409 	}
1410 }
1411 
1412 /* disconnect a BE and FE */
dpcm_be_disconnect(struct snd_soc_pcm_runtime * fe,int stream)1413 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1414 {
1415 	struct snd_soc_dpcm *dpcm, *d;
1416 	struct snd_pcm_substream *substream = snd_soc_dpcm_get_substream(fe, stream);
1417 	LIST_HEAD(deleted_dpcms);
1418 
1419 	snd_soc_dpcm_mutex_assert_held(fe);
1420 
1421 	snd_pcm_stream_lock_irq(substream);
1422 	for_each_dpcm_be_safe(fe, stream, dpcm, d) {
1423 		dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1424 				snd_pcm_direction_name(stream),
1425 				dpcm->be->dai_link->name);
1426 
1427 		if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1428 			continue;
1429 
1430 		dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1431 			snd_pcm_direction_name(stream), fe->dai_link->name,
1432 			stream ? "<-" : "->", dpcm->be->dai_link->name);
1433 
1434 		/* BEs still alive need new FE */
1435 		dpcm_be_reparent(fe, dpcm->be, stream);
1436 
1437 		list_del(&dpcm->list_be);
1438 		list_move(&dpcm->list_fe, &deleted_dpcms);
1439 	}
1440 	snd_pcm_stream_unlock_irq(substream);
1441 
1442 	while (!list_empty(&deleted_dpcms)) {
1443 		dpcm = list_first_entry(&deleted_dpcms, struct snd_soc_dpcm,
1444 					list_fe);
1445 		list_del(&dpcm->list_fe);
1446 		dpcm_remove_debugfs_state(dpcm);
1447 		kfree(dpcm);
1448 	}
1449 }
1450 
1451 /* get BE for DAI widget and stream */
dpcm_get_be(struct snd_soc_card * card,struct snd_soc_dapm_widget * widget,int stream)1452 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1453 		struct snd_soc_dapm_widget *widget, int stream)
1454 {
1455 	struct snd_soc_pcm_runtime *be;
1456 	struct snd_soc_dapm_widget *w;
1457 	struct snd_soc_dai *dai;
1458 	int i;
1459 
1460 	dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1461 
1462 	for_each_card_rtds(card, be) {
1463 
1464 		if (!be->dai_link->no_pcm)
1465 			continue;
1466 
1467 		if (!snd_soc_dpcm_get_substream(be, stream))
1468 			continue;
1469 
1470 		for_each_rtd_dais(be, i, dai) {
1471 			w = snd_soc_dai_get_widget(dai, stream);
1472 
1473 			dev_dbg(card->dev, "ASoC: try BE : %s\n",
1474 				w ? w->name : "(not set)");
1475 
1476 			if (w == widget)
1477 				return be;
1478 		}
1479 	}
1480 
1481 	/* Widget provided is not a BE */
1482 	return NULL;
1483 }
1484 
widget_in_list(struct snd_soc_dapm_widget_list * list,struct snd_soc_dapm_widget * widget)1485 int widget_in_list(struct snd_soc_dapm_widget_list *list,
1486 		struct snd_soc_dapm_widget *widget)
1487 {
1488 	struct snd_soc_dapm_widget *w;
1489 	int i;
1490 
1491 	for_each_dapm_widgets(list, i, w)
1492 		if (widget == w)
1493 			return 1;
1494 
1495 	return 0;
1496 }
1497 EXPORT_SYMBOL_GPL(widget_in_list);
1498 
dpcm_end_walk_at_be(struct snd_soc_dapm_widget * widget,enum snd_soc_dapm_direction dir)1499 bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget, enum snd_soc_dapm_direction dir)
1500 {
1501 	struct snd_soc_card *card = widget->dapm->card;
1502 	struct snd_soc_pcm_runtime *rtd;
1503 	int stream;
1504 
1505 	/* adjust dir to stream */
1506 	if (dir == SND_SOC_DAPM_DIR_OUT)
1507 		stream = SNDRV_PCM_STREAM_PLAYBACK;
1508 	else
1509 		stream = SNDRV_PCM_STREAM_CAPTURE;
1510 
1511 	rtd = dpcm_get_be(card, widget, stream);
1512 	if (rtd)
1513 		return true;
1514 
1515 	return false;
1516 }
1517 EXPORT_SYMBOL_GPL(dpcm_end_walk_at_be);
1518 
dpcm_path_get(struct snd_soc_pcm_runtime * fe,int stream,struct snd_soc_dapm_widget_list ** list)1519 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1520 	int stream, struct snd_soc_dapm_widget_list **list)
1521 {
1522 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(fe, 0);
1523 	int paths;
1524 
1525 	if (fe->dai_link->num_cpus > 1) {
1526 		dev_err(fe->dev,
1527 			"%s doesn't support Multi CPU yet\n", __func__);
1528 		return -EINVAL;
1529 	}
1530 
1531 	/* get number of valid DAI paths and their widgets */
1532 	paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1533 			fe->card->component_chaining ?
1534 				NULL : dpcm_end_walk_at_be);
1535 
1536 	if (paths > 0)
1537 		dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1538 			snd_pcm_direction_name(stream));
1539 	else if (paths == 0)
1540 		dev_dbg(fe->dev, "ASoC: %s no valid %s path\n", fe->dai_link->name,
1541 			snd_pcm_direction_name(stream));
1542 
1543 	return paths;
1544 }
1545 
dpcm_path_put(struct snd_soc_dapm_widget_list ** list)1546 void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
1547 {
1548 	snd_soc_dapm_dai_free_widgets(list);
1549 }
1550 
dpcm_be_is_active(struct snd_soc_dpcm * dpcm,int stream,struct snd_soc_dapm_widget_list * list)1551 static bool dpcm_be_is_active(struct snd_soc_dpcm *dpcm, int stream,
1552 			      struct snd_soc_dapm_widget_list *list)
1553 {
1554 	struct snd_soc_dai *dai;
1555 	unsigned int i;
1556 
1557 	/* is there a valid DAI widget for this BE */
1558 	for_each_rtd_dais(dpcm->be, i, dai) {
1559 		struct snd_soc_dapm_widget *widget = snd_soc_dai_get_widget(dai, stream);
1560 
1561 		/*
1562 		 * The BE is pruned only if none of the dai
1563 		 * widgets are in the active list.
1564 		 */
1565 		if (widget && widget_in_list(list, widget))
1566 			return true;
1567 	}
1568 
1569 	return false;
1570 }
1571 
dpcm_prune_paths(struct snd_soc_pcm_runtime * fe,int stream,struct snd_soc_dapm_widget_list ** list_)1572 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1573 			    struct snd_soc_dapm_widget_list **list_)
1574 {
1575 	struct snd_soc_dpcm *dpcm;
1576 	int prune = 0;
1577 
1578 	/* Destroy any old FE <--> BE connections */
1579 	for_each_dpcm_be(fe, stream, dpcm) {
1580 		if (dpcm_be_is_active(dpcm, stream, *list_))
1581 			continue;
1582 
1583 		dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1584 			snd_pcm_direction_name(stream),
1585 			dpcm->be->dai_link->name, fe->dai_link->name);
1586 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1587 		dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_BE);
1588 		prune++;
1589 	}
1590 
1591 	dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1592 	return prune;
1593 }
1594 
dpcm_add_paths(struct snd_soc_pcm_runtime * fe,int stream,struct snd_soc_dapm_widget_list ** list_)1595 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1596 	struct snd_soc_dapm_widget_list **list_)
1597 {
1598 	struct snd_soc_card *card = fe->card;
1599 	struct snd_soc_dapm_widget_list *list = *list_;
1600 	struct snd_soc_pcm_runtime *be;
1601 	struct snd_soc_dapm_widget *widget;
1602 	struct snd_pcm_substream *fe_substream = snd_soc_dpcm_get_substream(fe, stream);
1603 	int i, new = 0, err;
1604 
1605 	/* don't connect if FE is not running */
1606 	if (!fe_substream->runtime && !fe->fe_compr)
1607 		return new;
1608 
1609 	/* Create any new FE <--> BE connections */
1610 	for_each_dapm_widgets(list, i, widget) {
1611 
1612 		switch (widget->id) {
1613 		case snd_soc_dapm_dai_in:
1614 			if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1615 				continue;
1616 			break;
1617 		case snd_soc_dapm_dai_out:
1618 			if (stream != SNDRV_PCM_STREAM_CAPTURE)
1619 				continue;
1620 			break;
1621 		default:
1622 			continue;
1623 		}
1624 
1625 		/* is there a valid BE rtd for this widget */
1626 		be = dpcm_get_be(card, widget, stream);
1627 		if (!be) {
1628 			dev_dbg(fe->dev, "ASoC: no BE found for %s\n",
1629 				widget->name);
1630 			continue;
1631 		}
1632 
1633 		/*
1634 		 * Filter for systems with 'component_chaining' enabled.
1635 		 * This helps to avoid unnecessary re-configuration of an
1636 		 * already active BE on such systems and ensures the BE DAI
1637 		 * widget is powered ON after hw_params() BE DAI callback.
1638 		 */
1639 		if (fe->card->component_chaining &&
1640 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1641 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1642 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1643 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1644 			continue;
1645 
1646 		/* newly connected FE and BE */
1647 		err = dpcm_be_connect(fe, be, stream);
1648 		if (err < 0) {
1649 			dev_err(fe->dev, "ASoC: can't connect %s\n",
1650 				widget->name);
1651 			break;
1652 		} else if (err == 0) /* already connected */
1653 			continue;
1654 
1655 		/* new */
1656 		dpcm_set_be_update_state(be, stream, SND_SOC_DPCM_UPDATE_BE);
1657 		new++;
1658 	}
1659 
1660 	dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1661 	return new;
1662 }
1663 
1664 /*
1665  * Find the corresponding BE DAIs that source or sink audio to this
1666  * FE substream.
1667  */
dpcm_process_paths(struct snd_soc_pcm_runtime * fe,int stream,struct snd_soc_dapm_widget_list ** list,int new)1668 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1669 	int stream, struct snd_soc_dapm_widget_list **list, int new)
1670 {
1671 	if (new)
1672 		return dpcm_add_paths(fe, stream, list);
1673 	else
1674 		return dpcm_prune_paths(fe, stream, list);
1675 }
1676 
dpcm_clear_pending_state(struct snd_soc_pcm_runtime * fe,int stream)1677 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1678 {
1679 	struct snd_soc_dpcm *dpcm;
1680 
1681 	for_each_dpcm_be(fe, stream, dpcm)
1682 		dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_NO);
1683 }
1684 
dpcm_be_dai_stop(struct snd_soc_pcm_runtime * fe,int stream,int do_hw_free,struct snd_soc_dpcm * last)1685 void dpcm_be_dai_stop(struct snd_soc_pcm_runtime *fe, int stream,
1686 		      int do_hw_free, struct snd_soc_dpcm *last)
1687 {
1688 	struct snd_soc_dpcm *dpcm;
1689 
1690 	/* disable any enabled and non active backends */
1691 	for_each_dpcm_be(fe, stream, dpcm) {
1692 		struct snd_soc_pcm_runtime *be = dpcm->be;
1693 		struct snd_pcm_substream *be_substream =
1694 			snd_soc_dpcm_get_substream(be, stream);
1695 
1696 		if (dpcm == last)
1697 			return;
1698 
1699 		/* is this op for this BE ? */
1700 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1701 			continue;
1702 
1703 		if (be->dpcm[stream].users == 0) {
1704 			dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1705 				snd_pcm_direction_name(stream),
1706 				be->dpcm[stream].state);
1707 			continue;
1708 		}
1709 
1710 		if (--be->dpcm[stream].users != 0)
1711 			continue;
1712 
1713 		if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) {
1714 			if (!do_hw_free)
1715 				continue;
1716 
1717 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) {
1718 				__soc_pcm_hw_free(be, be_substream);
1719 				be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1720 			}
1721 		}
1722 
1723 		__soc_pcm_close(be, be_substream);
1724 		be_substream->runtime = NULL;
1725 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1726 	}
1727 }
1728 
dpcm_be_dai_startup(struct snd_soc_pcm_runtime * fe,int stream)1729 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1730 {
1731 	struct snd_pcm_substream *fe_substream = snd_soc_dpcm_get_substream(fe, stream);
1732 	struct snd_soc_pcm_runtime *be;
1733 	struct snd_soc_dpcm *dpcm;
1734 	int err, count = 0;
1735 
1736 	/* only startup BE DAIs that are either sinks or sources to this FE DAI */
1737 	for_each_dpcm_be(fe, stream, dpcm) {
1738 		struct snd_pcm_substream *be_substream;
1739 
1740 		be = dpcm->be;
1741 		be_substream = snd_soc_dpcm_get_substream(be, stream);
1742 
1743 		if (!be_substream) {
1744 			dev_err(be->dev, "ASoC: no backend %s stream\n",
1745 				snd_pcm_direction_name(stream));
1746 			continue;
1747 		}
1748 
1749 		/* is this op for this BE ? */
1750 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1751 			continue;
1752 
1753 		/* first time the dpcm is open ? */
1754 		if (be->dpcm[stream].users == DPCM_MAX_BE_USERS) {
1755 			dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1756 				snd_pcm_direction_name(stream),
1757 				be->dpcm[stream].state);
1758 			continue;
1759 		}
1760 
1761 		if (be->dpcm[stream].users++ != 0)
1762 			continue;
1763 
1764 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1765 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1766 			continue;
1767 
1768 		dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1769 			snd_pcm_direction_name(stream), be->dai_link->name);
1770 
1771 		be_substream->runtime = fe_substream->runtime;
1772 		err = __soc_pcm_open(be, be_substream);
1773 		if (err < 0) {
1774 			be->dpcm[stream].users--;
1775 			if (be->dpcm[stream].users < 0)
1776 				dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1777 					snd_pcm_direction_name(stream),
1778 					be->dpcm[stream].state);
1779 
1780 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1781 			goto unwind;
1782 		}
1783 		be->dpcm[stream].be_start = 0;
1784 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1785 		count++;
1786 	}
1787 
1788 	return count;
1789 
1790 unwind:
1791 	dpcm_be_dai_startup_rollback(fe, stream, dpcm);
1792 
1793 	return soc_pcm_ret(fe, err);
1794 }
1795 
dpcm_runtime_setup_fe(struct snd_pcm_substream * substream)1796 static void dpcm_runtime_setup_fe(struct snd_pcm_substream *substream)
1797 {
1798 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
1799 	struct snd_pcm_runtime *runtime = substream->runtime;
1800 	struct snd_pcm_hardware *hw = &runtime->hw;
1801 	struct snd_soc_dai *dai;
1802 	int stream = substream->stream;
1803 	u64 formats = hw->formats;
1804 	int i;
1805 
1806 	soc_pcm_hw_init(hw);
1807 
1808 	if (formats)
1809 		hw->formats &= formats;
1810 
1811 	for_each_rtd_cpu_dais(fe, i, dai) {
1812 		const struct snd_soc_pcm_stream *cpu_stream;
1813 
1814 		/*
1815 		 * Skip CPUs which don't support the current stream
1816 		 * type. See soc_pcm_init_runtime_hw() for more details
1817 		 */
1818 		if (!snd_soc_dai_stream_valid(dai, stream))
1819 			continue;
1820 
1821 		cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1822 
1823 		soc_pcm_hw_update_rate(hw, cpu_stream);
1824 		soc_pcm_hw_update_chan(hw, cpu_stream);
1825 		soc_pcm_hw_update_format(hw, cpu_stream);
1826 		soc_pcm_hw_update_subformat(hw, cpu_stream);
1827 	}
1828 
1829 }
1830 
dpcm_runtime_setup_be_format(struct snd_pcm_substream * substream)1831 static void dpcm_runtime_setup_be_format(struct snd_pcm_substream *substream)
1832 {
1833 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
1834 	struct snd_pcm_runtime *runtime = substream->runtime;
1835 	struct snd_pcm_hardware *hw = &runtime->hw;
1836 	struct snd_soc_dpcm *dpcm;
1837 	struct snd_soc_dai *dai;
1838 	int stream = substream->stream;
1839 
1840 	if (!fe->dai_link->dpcm_merged_format)
1841 		return;
1842 
1843 	/*
1844 	 * It returns merged BE codec format
1845 	 * if FE want to use it (= dpcm_merged_format)
1846 	 */
1847 
1848 	for_each_dpcm_be(fe, stream, dpcm) {
1849 		struct snd_soc_pcm_runtime *be = dpcm->be;
1850 		const struct snd_soc_pcm_stream *codec_stream;
1851 		int i;
1852 
1853 		for_each_rtd_codec_dais(be, i, dai) {
1854 			/*
1855 			 * Skip CODECs which don't support the current stream
1856 			 * type. See soc_pcm_init_runtime_hw() for more details
1857 			 */
1858 			if (!snd_soc_dai_stream_valid(dai, stream))
1859 				continue;
1860 
1861 			codec_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1862 
1863 			soc_pcm_hw_update_format(hw, codec_stream);
1864 			soc_pcm_hw_update_subformat(hw, codec_stream);
1865 		}
1866 	}
1867 }
1868 
dpcm_runtime_setup_be_chan(struct snd_pcm_substream * substream)1869 static void dpcm_runtime_setup_be_chan(struct snd_pcm_substream *substream)
1870 {
1871 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
1872 	struct snd_pcm_runtime *runtime = substream->runtime;
1873 	struct snd_pcm_hardware *hw = &runtime->hw;
1874 	struct snd_soc_dpcm *dpcm;
1875 	int stream = substream->stream;
1876 
1877 	if (!fe->dai_link->dpcm_merged_chan)
1878 		return;
1879 
1880 	/*
1881 	 * It returns merged BE codec channel;
1882 	 * if FE want to use it (= dpcm_merged_chan)
1883 	 */
1884 
1885 	for_each_dpcm_be(fe, stream, dpcm) {
1886 		struct snd_soc_pcm_runtime *be = dpcm->be;
1887 		const struct snd_soc_pcm_stream *cpu_stream;
1888 		struct snd_soc_dai *dai;
1889 		int i;
1890 
1891 		for_each_rtd_cpu_dais(be, i, dai) {
1892 			/*
1893 			 * Skip CPUs which don't support the current stream
1894 			 * type. See soc_pcm_init_runtime_hw() for more details
1895 			 */
1896 			if (!snd_soc_dai_stream_valid(dai, stream))
1897 				continue;
1898 
1899 			cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1900 
1901 			soc_pcm_hw_update_chan(hw, cpu_stream);
1902 		}
1903 
1904 		/*
1905 		 * chan min/max cannot be enforced if there are multiple CODEC
1906 		 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1907 		 */
1908 		if (be->dai_link->num_codecs == 1) {
1909 			const struct snd_soc_pcm_stream *codec_stream = snd_soc_dai_get_pcm_stream(
1910 				snd_soc_rtd_to_codec(be, 0), stream);
1911 
1912 			soc_pcm_hw_update_chan(hw, codec_stream);
1913 		}
1914 	}
1915 }
1916 
dpcm_runtime_setup_be_rate(struct snd_pcm_substream * substream)1917 static void dpcm_runtime_setup_be_rate(struct snd_pcm_substream *substream)
1918 {
1919 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
1920 	struct snd_pcm_runtime *runtime = substream->runtime;
1921 	struct snd_pcm_hardware *hw = &runtime->hw;
1922 	struct snd_soc_dpcm *dpcm;
1923 	int stream = substream->stream;
1924 
1925 	if (!fe->dai_link->dpcm_merged_rate)
1926 		return;
1927 
1928 	/*
1929 	 * It returns merged BE codec channel;
1930 	 * if FE want to use it (= dpcm_merged_chan)
1931 	 */
1932 
1933 	for_each_dpcm_be(fe, stream, dpcm) {
1934 		struct snd_soc_pcm_runtime *be = dpcm->be;
1935 		const struct snd_soc_pcm_stream *pcm;
1936 		struct snd_soc_dai *dai;
1937 		int i;
1938 
1939 		for_each_rtd_dais(be, i, dai) {
1940 			/*
1941 			 * Skip DAIs which don't support the current stream
1942 			 * type. See soc_pcm_init_runtime_hw() for more details
1943 			 */
1944 			if (!snd_soc_dai_stream_valid(dai, stream))
1945 				continue;
1946 
1947 			pcm = snd_soc_dai_get_pcm_stream(dai, stream);
1948 
1949 			soc_pcm_hw_update_rate(hw, pcm);
1950 		}
1951 	}
1952 }
1953 
dpcm_apply_symmetry(struct snd_pcm_substream * fe_substream,int stream)1954 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1955 			       int stream)
1956 {
1957 	struct snd_soc_dpcm *dpcm;
1958 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(fe_substream);
1959 	struct snd_soc_dai *fe_cpu_dai;
1960 	int err = 0;
1961 	int i;
1962 
1963 	/* apply symmetry for FE */
1964 	soc_pcm_update_symmetry(fe_substream);
1965 
1966 	for_each_rtd_cpu_dais (fe, i, fe_cpu_dai) {
1967 		/* Symmetry only applies if we've got an active stream. */
1968 		err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1969 		if (err < 0)
1970 			goto error;
1971 	}
1972 
1973 	/* apply symmetry for BE */
1974 	for_each_dpcm_be(fe, stream, dpcm) {
1975 		struct snd_soc_pcm_runtime *be = dpcm->be;
1976 		struct snd_pcm_substream *be_substream =
1977 			snd_soc_dpcm_get_substream(be, stream);
1978 		struct snd_soc_pcm_runtime *rtd;
1979 		struct snd_soc_dai *dai;
1980 
1981 		/* A backend may not have the requested substream */
1982 		if (!be_substream)
1983 			continue;
1984 
1985 		rtd = snd_soc_substream_to_rtd(be_substream);
1986 		if (rtd->dai_link->be_hw_params_fixup)
1987 			continue;
1988 
1989 		soc_pcm_update_symmetry(be_substream);
1990 
1991 		/* Symmetry only applies if we've got an active stream. */
1992 		for_each_rtd_dais(rtd, i, dai) {
1993 			err = soc_pcm_apply_symmetry(fe_substream, dai);
1994 			if (err < 0)
1995 				goto error;
1996 		}
1997 	}
1998 error:
1999 	return soc_pcm_ret(fe, err);
2000 }
2001 
dpcm_fe_dai_startup(struct snd_pcm_substream * fe_substream)2002 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
2003 {
2004 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(fe_substream);
2005 	int stream = fe_substream->stream, ret = 0;
2006 
2007 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2008 
2009 	ret = dpcm_be_dai_startup(fe, stream);
2010 	if (ret < 0)
2011 		goto be_err;
2012 
2013 	dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
2014 
2015 	/* start the DAI frontend */
2016 	ret = __soc_pcm_open(fe, fe_substream);
2017 	if (ret < 0)
2018 		goto unwind;
2019 
2020 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
2021 
2022 	dpcm_runtime_setup_fe(fe_substream);
2023 
2024 	dpcm_runtime_setup_be_format(fe_substream);
2025 	dpcm_runtime_setup_be_chan(fe_substream);
2026 	dpcm_runtime_setup_be_rate(fe_substream);
2027 
2028 	ret = dpcm_apply_symmetry(fe_substream, stream);
2029 
2030 unwind:
2031 	if (ret < 0)
2032 		dpcm_be_dai_startup_unwind(fe, stream);
2033 be_err:
2034 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2035 
2036 	return soc_pcm_ret(fe, ret);
2037 }
2038 
dpcm_fe_dai_shutdown(struct snd_pcm_substream * substream)2039 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
2040 {
2041 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
2042 	int stream = substream->stream;
2043 
2044 	snd_soc_dpcm_mutex_assert_held(fe);
2045 
2046 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2047 
2048 	/* shutdown the BEs */
2049 	dpcm_be_dai_shutdown(fe, stream);
2050 
2051 	dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
2052 
2053 	/* now shutdown the frontend */
2054 	__soc_pcm_close(fe, substream);
2055 
2056 	/* run the stream stop event */
2057 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
2058 
2059 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2060 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2061 	return 0;
2062 }
2063 
dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime * fe,int stream)2064 void dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
2065 {
2066 	struct snd_soc_dpcm *dpcm;
2067 
2068 	/* only hw_params backends that are either sinks or sources
2069 	 * to this frontend DAI */
2070 	for_each_dpcm_be(fe, stream, dpcm) {
2071 
2072 		struct snd_soc_pcm_runtime *be = dpcm->be;
2073 		struct snd_pcm_substream *be_substream =
2074 			snd_soc_dpcm_get_substream(be, stream);
2075 
2076 		/* is this op for this BE ? */
2077 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2078 			continue;
2079 
2080 		/* only free hw when no longer used - check all FEs */
2081 		if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2082 				continue;
2083 
2084 		/* do not free hw if this BE is used by other FE */
2085 		if (be->dpcm[stream].users > 1)
2086 			continue;
2087 
2088 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2089 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2090 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2091 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
2092 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2093 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2094 			continue;
2095 
2096 		dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
2097 			be->dai_link->name);
2098 
2099 		__soc_pcm_hw_free(be, be_substream);
2100 
2101 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2102 	}
2103 }
2104 
dpcm_fe_dai_hw_free(struct snd_pcm_substream * substream)2105 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
2106 {
2107 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
2108 	int stream = substream->stream;
2109 
2110 	snd_soc_dpcm_mutex_lock(fe);
2111 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2112 
2113 	dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
2114 
2115 	/* call hw_free on the frontend */
2116 	soc_pcm_hw_clean(fe, substream, 0);
2117 
2118 	/* only hw_params backends that are either sinks or sources
2119 	 * to this frontend DAI */
2120 	dpcm_be_dai_hw_free(fe, stream);
2121 
2122 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2123 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2124 
2125 	snd_soc_dpcm_mutex_unlock(fe);
2126 	return 0;
2127 }
2128 
dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime * fe,int stream)2129 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
2130 {
2131 	struct snd_soc_pcm_runtime *be;
2132 	struct snd_pcm_substream *be_substream;
2133 	struct snd_soc_dpcm *dpcm;
2134 	int ret;
2135 
2136 	for_each_dpcm_be(fe, stream, dpcm) {
2137 		struct snd_pcm_hw_params hw_params;
2138 
2139 		be = dpcm->be;
2140 		be_substream = snd_soc_dpcm_get_substream(be, stream);
2141 
2142 		/* is this op for this BE ? */
2143 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2144 			continue;
2145 
2146 		/* copy params for each dpcm */
2147 		memcpy(&hw_params, &fe->dpcm[stream].hw_params,
2148 				sizeof(struct snd_pcm_hw_params));
2149 
2150 		/* perform any hw_params fixups */
2151 		ret = snd_soc_link_be_hw_params_fixup(be, &hw_params);
2152 		if (ret < 0)
2153 			goto unwind;
2154 
2155 		/* copy the fixed-up hw params for BE dai */
2156 		memcpy(&be->dpcm[stream].hw_params, &hw_params,
2157 		       sizeof(struct snd_pcm_hw_params));
2158 
2159 		/* only allow hw_params() if no connected FEs are running */
2160 		if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2161 			continue;
2162 
2163 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2164 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2165 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2166 			continue;
2167 
2168 		dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
2169 			be->dai_link->name);
2170 
2171 		ret = __soc_pcm_hw_params(be, be_substream, &hw_params);
2172 		if (ret < 0)
2173 			goto unwind;
2174 
2175 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2176 	}
2177 	return 0;
2178 
2179 unwind:
2180 	dev_dbg(fe->dev, "ASoC: %s() failed at %s (%d)\n",
2181 		__func__, be->dai_link->name, ret);
2182 
2183 	/* disable any enabled and non active backends */
2184 	for_each_dpcm_be_rollback(fe, stream, dpcm) {
2185 		be = dpcm->be;
2186 		be_substream = snd_soc_dpcm_get_substream(be, stream);
2187 
2188 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2189 			continue;
2190 
2191 		/* only allow hw_free() if no connected FEs are running */
2192 		if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2193 			continue;
2194 
2195 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2196 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2197 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2198 		   (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2199 			continue;
2200 
2201 		__soc_pcm_hw_free(be, be_substream);
2202 	}
2203 
2204 	return ret;
2205 }
2206 
dpcm_fe_dai_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)2207 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2208 				 struct snd_pcm_hw_params *params)
2209 {
2210 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
2211 	int ret, stream = substream->stream;
2212 
2213 	snd_soc_dpcm_mutex_lock(fe);
2214 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2215 
2216 	memcpy(&fe->dpcm[stream].hw_params, params,
2217 			sizeof(struct snd_pcm_hw_params));
2218 	ret = dpcm_be_dai_hw_params(fe, stream);
2219 	if (ret < 0)
2220 		goto out;
2221 
2222 	dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2223 			fe->dai_link->name, params_rate(params),
2224 			params_channels(params), params_format(params));
2225 
2226 	/* call hw_params on the frontend */
2227 	ret = __soc_pcm_hw_params(fe, substream, params);
2228 	if (ret < 0)
2229 		dpcm_be_dai_hw_free(fe, stream);
2230 	else
2231 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2232 
2233 out:
2234 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2235 	snd_soc_dpcm_mutex_unlock(fe);
2236 
2237 	return soc_pcm_ret(fe, ret);
2238 }
2239 
dpcm_be_dai_trigger(struct snd_soc_pcm_runtime * fe,int stream,int cmd)2240 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2241 			       int cmd)
2242 {
2243 	struct snd_soc_pcm_runtime *be;
2244 	bool pause_stop_transition;
2245 	struct snd_soc_dpcm *dpcm;
2246 	unsigned long flags;
2247 	int ret = 0;
2248 
2249 	for_each_dpcm_be(fe, stream, dpcm) {
2250 		struct snd_pcm_substream *be_substream;
2251 
2252 		be = dpcm->be;
2253 		be_substream = snd_soc_dpcm_get_substream(be, stream);
2254 
2255 		snd_pcm_stream_lock_irqsave_nested(be_substream, flags);
2256 
2257 		/* is this op for this BE ? */
2258 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2259 			goto next;
2260 
2261 		dev_dbg(be->dev, "ASoC: trigger BE %s cmd %d\n",
2262 			be->dai_link->name, cmd);
2263 
2264 		switch (cmd) {
2265 		case SNDRV_PCM_TRIGGER_START:
2266 			if (!be->dpcm[stream].be_start &&
2267 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2268 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2269 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2270 				goto next;
2271 
2272 			be->dpcm[stream].be_start++;
2273 			if (be->dpcm[stream].be_start != 1)
2274 				goto next;
2275 
2276 			if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_PAUSED)
2277 				ret = soc_pcm_trigger(be_substream,
2278 						      SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
2279 			else
2280 				ret = soc_pcm_trigger(be_substream,
2281 						      SNDRV_PCM_TRIGGER_START);
2282 			if (ret) {
2283 				be->dpcm[stream].be_start--;
2284 				goto next;
2285 			}
2286 
2287 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2288 			break;
2289 		case SNDRV_PCM_TRIGGER_RESUME:
2290 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2291 				goto next;
2292 
2293 			be->dpcm[stream].be_start++;
2294 			if (be->dpcm[stream].be_start != 1)
2295 				goto next;
2296 
2297 			ret = soc_pcm_trigger(be_substream, cmd);
2298 			if (ret) {
2299 				be->dpcm[stream].be_start--;
2300 				goto next;
2301 			}
2302 
2303 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2304 			break;
2305 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2306 			if (!be->dpcm[stream].be_start &&
2307 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
2308 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2309 				goto next;
2310 
2311 			fe->dpcm[stream].fe_pause = false;
2312 			be->dpcm[stream].be_pause--;
2313 
2314 			be->dpcm[stream].be_start++;
2315 			if (be->dpcm[stream].be_start != 1)
2316 				goto next;
2317 
2318 			ret = soc_pcm_trigger(be_substream, cmd);
2319 			if (ret) {
2320 				be->dpcm[stream].be_start--;
2321 				goto next;
2322 			}
2323 
2324 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2325 			break;
2326 		case SNDRV_PCM_TRIGGER_STOP:
2327 			if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
2328 			    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2329 				goto next;
2330 
2331 			if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_START)
2332 				be->dpcm[stream].be_start--;
2333 
2334 			if (be->dpcm[stream].be_start != 0)
2335 				goto next;
2336 
2337 			pause_stop_transition = false;
2338 			if (fe->dpcm[stream].fe_pause) {
2339 				pause_stop_transition = true;
2340 				fe->dpcm[stream].fe_pause = false;
2341 				be->dpcm[stream].be_pause--;
2342 			}
2343 
2344 			if (be->dpcm[stream].be_pause != 0)
2345 				ret = soc_pcm_trigger(be_substream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
2346 			else
2347 				ret = soc_pcm_trigger(be_substream, SNDRV_PCM_TRIGGER_STOP);
2348 
2349 			if (ret) {
2350 				if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_START)
2351 					be->dpcm[stream].be_start++;
2352 				if (pause_stop_transition) {
2353 					fe->dpcm[stream].fe_pause = true;
2354 					be->dpcm[stream].be_pause++;
2355 				}
2356 				goto next;
2357 			}
2358 
2359 			if (be->dpcm[stream].be_pause != 0)
2360 				be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2361 			else
2362 				be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2363 
2364 			break;
2365 		case SNDRV_PCM_TRIGGER_SUSPEND:
2366 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2367 				goto next;
2368 
2369 			be->dpcm[stream].be_start--;
2370 			if (be->dpcm[stream].be_start != 0)
2371 				goto next;
2372 
2373 			ret = soc_pcm_trigger(be_substream, cmd);
2374 			if (ret) {
2375 				be->dpcm[stream].be_start++;
2376 				goto next;
2377 			}
2378 
2379 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2380 			break;
2381 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2382 			if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2383 				goto next;
2384 
2385 			fe->dpcm[stream].fe_pause = true;
2386 			be->dpcm[stream].be_pause++;
2387 
2388 			be->dpcm[stream].be_start--;
2389 			if (be->dpcm[stream].be_start != 0)
2390 				goto next;
2391 
2392 			ret = soc_pcm_trigger(be_substream, cmd);
2393 			if (ret) {
2394 				be->dpcm[stream].be_start++;
2395 				goto next;
2396 			}
2397 
2398 			be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2399 			break;
2400 		}
2401 next:
2402 		snd_pcm_stream_unlock_irqrestore(be_substream, flags);
2403 		if (ret)
2404 			break;
2405 	}
2406 	return soc_pcm_ret(fe, ret);
2407 }
2408 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2409 
dpcm_dai_trigger_fe_be(struct snd_pcm_substream * substream,int cmd,bool fe_first)2410 static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2411 				  int cmd, bool fe_first)
2412 {
2413 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
2414 	int ret;
2415 
2416 	/* call trigger on the frontend before the backend. */
2417 	if (fe_first) {
2418 		dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2419 			fe->dai_link->name, cmd);
2420 
2421 		ret = soc_pcm_trigger(substream, cmd);
2422 		if (ret < 0)
2423 			return ret;
2424 
2425 		ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2426 		return ret;
2427 	}
2428 
2429 	/* call trigger on the frontend after the backend. */
2430 	ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2431 	if (ret < 0)
2432 		return ret;
2433 
2434 	dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2435 		fe->dai_link->name, cmd);
2436 
2437 	ret = soc_pcm_trigger(substream, cmd);
2438 
2439 	return ret;
2440 }
2441 
dpcm_fe_dai_do_trigger(struct snd_pcm_substream * substream,int cmd)2442 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2443 {
2444 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
2445 	int stream = substream->stream;
2446 	int ret = 0;
2447 	enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2448 
2449 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2450 
2451 	switch (trigger) {
2452 	case SND_SOC_DPCM_TRIGGER_PRE:
2453 		switch (cmd) {
2454 		case SNDRV_PCM_TRIGGER_START:
2455 		case SNDRV_PCM_TRIGGER_RESUME:
2456 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2457 		case SNDRV_PCM_TRIGGER_DRAIN:
2458 			ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2459 			break;
2460 		case SNDRV_PCM_TRIGGER_STOP:
2461 		case SNDRV_PCM_TRIGGER_SUSPEND:
2462 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2463 			ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2464 			break;
2465 		default:
2466 			ret = -EINVAL;
2467 			break;
2468 		}
2469 		break;
2470 	case SND_SOC_DPCM_TRIGGER_POST:
2471 		switch (cmd) {
2472 		case SNDRV_PCM_TRIGGER_START:
2473 		case SNDRV_PCM_TRIGGER_RESUME:
2474 		case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2475 		case SNDRV_PCM_TRIGGER_DRAIN:
2476 			ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2477 			break;
2478 		case SNDRV_PCM_TRIGGER_STOP:
2479 		case SNDRV_PCM_TRIGGER_SUSPEND:
2480 		case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2481 			ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2482 			break;
2483 		default:
2484 			ret = -EINVAL;
2485 			break;
2486 		}
2487 		break;
2488 	default:
2489 		dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2490 				fe->dai_link->name);
2491 		ret = -EINVAL;
2492 		goto out;
2493 	}
2494 
2495 	if (ret < 0) {
2496 		dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2497 			cmd, ret);
2498 		goto out;
2499 	}
2500 
2501 	switch (cmd) {
2502 	case SNDRV_PCM_TRIGGER_START:
2503 	case SNDRV_PCM_TRIGGER_RESUME:
2504 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2505 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2506 		break;
2507 	case SNDRV_PCM_TRIGGER_STOP:
2508 	case SNDRV_PCM_TRIGGER_SUSPEND:
2509 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2510 		break;
2511 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2512 		fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2513 		break;
2514 	}
2515 
2516 out:
2517 	fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2518 	return ret;
2519 }
2520 
dpcm_fe_dai_trigger(struct snd_pcm_substream * substream,int cmd)2521 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2522 {
2523 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
2524 	int stream = substream->stream;
2525 
2526 	/* if FE's runtime_update is already set, we're in race;
2527 	 * process this trigger later at exit
2528 	 */
2529 	if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2530 		fe->dpcm[stream].trigger_pending = cmd + 1;
2531 		return 0; /* delayed, assuming it's successful */
2532 	}
2533 
2534 	/* we're alone, let's trigger */
2535 	return dpcm_fe_dai_do_trigger(substream, cmd);
2536 }
2537 
dpcm_be_dai_prepare(struct snd_soc_pcm_runtime * fe,int stream)2538 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2539 {
2540 	struct snd_soc_dpcm *dpcm;
2541 	int ret = 0;
2542 
2543 	for_each_dpcm_be(fe, stream, dpcm) {
2544 
2545 		struct snd_soc_pcm_runtime *be = dpcm->be;
2546 		struct snd_pcm_substream *be_substream =
2547 			snd_soc_dpcm_get_substream(be, stream);
2548 
2549 		/* is this op for this BE ? */
2550 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2551 			continue;
2552 
2553 		if (!snd_soc_dpcm_can_be_prepared(fe, be, stream))
2554 			continue;
2555 
2556 		if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2557 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2558 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2559 		    (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2560 			continue;
2561 
2562 		dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2563 			be->dai_link->name);
2564 
2565 		ret = __soc_pcm_prepare(be, be_substream);
2566 		if (ret < 0)
2567 			break;
2568 
2569 		be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2570 	}
2571 
2572 	/*
2573 	 * Don't use soc_pcm_ret() on .prepare callback to lower error log severity
2574 	 *
2575 	 * We don't want to log an error since we do not want to give userspace a way to do a
2576 	 * denial-of-service attack on the syslog / diskspace.
2577 	 */
2578 	return ret;
2579 }
2580 
dpcm_fe_dai_prepare(struct snd_pcm_substream * substream)2581 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2582 {
2583 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(substream);
2584 	int stream = substream->stream, ret = 0;
2585 
2586 	snd_soc_dpcm_mutex_lock(fe);
2587 
2588 	dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2589 
2590 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2591 
2592 	/* there is no point preparing this FE if there are no BEs */
2593 	if (list_empty(&fe->dpcm[stream].be_clients)) {
2594 		/* dev_err_once() for visibility, dev_dbg() for debugging UCM profiles */
2595 		dev_err_once(fe->dev, "ASoC: no backend DAIs enabled for %s, possibly missing ALSA mixer-based routing or UCM profile\n",
2596 			     fe->dai_link->name);
2597 		dev_dbg(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2598 			fe->dai_link->name);
2599 		ret = -EINVAL;
2600 		goto out;
2601 	}
2602 
2603 	ret = dpcm_be_dai_prepare(fe, stream);
2604 	if (ret < 0)
2605 		goto out;
2606 
2607 	/* call prepare on the frontend */
2608 	ret = __soc_pcm_prepare(fe, substream);
2609 	if (ret < 0)
2610 		goto out;
2611 
2612 	fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2613 
2614 out:
2615 	dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2616 	snd_soc_dpcm_mutex_unlock(fe);
2617 
2618 	/*
2619 	 * Don't use soc_pcm_ret() on .prepare callback to lower error log severity
2620 	 *
2621 	 * We don't want to log an error since we do not want to give userspace a way to do a
2622 	 * denial-of-service attack on the syslog / diskspace.
2623 	 */
2624 	return ret;
2625 }
2626 
dpcm_run_update_shutdown(struct snd_soc_pcm_runtime * fe,int stream)2627 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2628 {
2629 	int err;
2630 
2631 	dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2632 		snd_pcm_direction_name(stream), fe->dai_link->name);
2633 
2634 	err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2635 
2636 	dpcm_be_dai_hw_free(fe, stream);
2637 
2638 	dpcm_be_dai_shutdown(fe, stream);
2639 
2640 	/* run the stream event for each BE */
2641 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2642 
2643 	return soc_pcm_ret(fe, err);
2644 }
2645 
dpcm_run_update_startup(struct snd_soc_pcm_runtime * fe,int stream)2646 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2647 {
2648 	struct snd_soc_dpcm *dpcm;
2649 	int ret = 0;
2650 
2651 	dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2652 		snd_pcm_direction_name(stream), fe->dai_link->name);
2653 
2654 	/* Only start the BE if the FE is ready */
2655 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2656 		fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE) {
2657 		dev_err(fe->dev, "ASoC: FE %s is not ready %d\n",
2658 			fe->dai_link->name, fe->dpcm[stream].state);
2659 		ret = -EINVAL;
2660 		goto disconnect;
2661 	}
2662 
2663 	/* startup must always be called for new BEs */
2664 	ret = dpcm_be_dai_startup(fe, stream);
2665 	if (ret < 0)
2666 		goto disconnect;
2667 
2668 	/* keep going if FE state is > open */
2669 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2670 		return 0;
2671 
2672 	ret = dpcm_be_dai_hw_params(fe, stream);
2673 	if (ret < 0)
2674 		goto close;
2675 
2676 	/* keep going if FE state is > hw_params */
2677 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2678 		return 0;
2679 
2680 	ret = dpcm_be_dai_prepare(fe, stream);
2681 	if (ret < 0)
2682 		goto hw_free;
2683 
2684 	/* run the stream event for each BE */
2685 	dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2686 
2687 	/* keep going if FE state is > prepare */
2688 	if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2689 		fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2690 		return 0;
2691 
2692 	ret = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_START);
2693 	if (ret < 0)
2694 		goto hw_free;
2695 
2696 	return 0;
2697 
2698 hw_free:
2699 	dpcm_be_dai_hw_free(fe, stream);
2700 close:
2701 	dpcm_be_dai_shutdown(fe, stream);
2702 disconnect:
2703 	/* disconnect any pending BEs */
2704 	for_each_dpcm_be(fe, stream, dpcm) {
2705 		struct snd_soc_pcm_runtime *be = dpcm->be;
2706 
2707 		/* is this op for this BE ? */
2708 		if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2709 			continue;
2710 
2711 		if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE ||
2712 			be->dpcm[stream].state == SND_SOC_DPCM_STATE_NEW)
2713 				dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2714 	}
2715 
2716 	return soc_pcm_ret(fe, ret);
2717 }
2718 
soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime * fe,int new)2719 static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2720 {
2721 	struct snd_soc_dapm_widget_list *list;
2722 	int stream;
2723 	int count, paths;
2724 
2725 	if (!fe->dai_link->dynamic)
2726 		return 0;
2727 
2728 	if (fe->dai_link->num_cpus > 1) {
2729 		dev_err(fe->dev,
2730 			"%s doesn't support Multi CPU yet\n", __func__);
2731 		return -EINVAL;
2732 	}
2733 
2734 	/* only check active links */
2735 	if (!snd_soc_dai_active(snd_soc_rtd_to_cpu(fe, 0)))
2736 		return 0;
2737 
2738 	/* DAPM sync will call this to update DSP paths */
2739 	dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2740 		new ? "new" : "old", fe->dai_link->name);
2741 
2742 	for_each_pcm_streams(stream) {
2743 
2744 		/* skip if FE doesn't have playback/capture capability */
2745 		if (!snd_soc_dai_stream_valid(snd_soc_rtd_to_cpu(fe, 0),   stream) ||
2746 		    !snd_soc_dai_stream_valid(snd_soc_rtd_to_codec(fe, 0), stream))
2747 			continue;
2748 
2749 		/* skip if FE isn't currently playing/capturing */
2750 		if (!snd_soc_dai_stream_active(snd_soc_rtd_to_cpu(fe, 0), stream) ||
2751 		    !snd_soc_dai_stream_active(snd_soc_rtd_to_codec(fe, 0), stream))
2752 			continue;
2753 
2754 		paths = dpcm_path_get(fe, stream, &list);
2755 		if (paths < 0)
2756 			return paths;
2757 
2758 		/* update any playback/capture paths */
2759 		count = dpcm_process_paths(fe, stream, &list, new);
2760 		if (count) {
2761 			dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2762 			if (new)
2763 				dpcm_run_update_startup(fe, stream);
2764 			else
2765 				dpcm_run_update_shutdown(fe, stream);
2766 			dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2767 
2768 			dpcm_clear_pending_state(fe, stream);
2769 			dpcm_be_disconnect(fe, stream);
2770 		}
2771 
2772 		dpcm_path_put(&list);
2773 	}
2774 
2775 	return 0;
2776 }
2777 
2778 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2779  * any DAI links.
2780  */
snd_soc_dpcm_runtime_update(struct snd_soc_card * card)2781 int snd_soc_dpcm_runtime_update(struct snd_soc_card *card)
2782 {
2783 	struct snd_soc_pcm_runtime *fe;
2784 	int ret = 0;
2785 
2786 	snd_soc_dpcm_mutex_lock(card);
2787 	/* shutdown all old paths first */
2788 	for_each_card_rtds(card, fe) {
2789 		ret = soc_dpcm_fe_runtime_update(fe, 0);
2790 		if (ret)
2791 			goto out;
2792 	}
2793 
2794 	/* bring new paths up */
2795 	for_each_card_rtds(card, fe) {
2796 		ret = soc_dpcm_fe_runtime_update(fe, 1);
2797 		if (ret)
2798 			goto out;
2799 	}
2800 
2801 out:
2802 	snd_soc_dpcm_mutex_unlock(card);
2803 	return ret;
2804 }
2805 EXPORT_SYMBOL_GPL(snd_soc_dpcm_runtime_update);
2806 
dpcm_fe_dai_cleanup(struct snd_pcm_substream * fe_substream)2807 static void dpcm_fe_dai_cleanup(struct snd_pcm_substream *fe_substream)
2808 {
2809 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(fe_substream);
2810 	struct snd_soc_dpcm *dpcm;
2811 	int stream = fe_substream->stream;
2812 
2813 	snd_soc_dpcm_mutex_assert_held(fe);
2814 
2815 	/* mark FE's links ready to prune */
2816 	for_each_dpcm_be(fe, stream, dpcm)
2817 		dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2818 
2819 	dpcm_be_disconnect(fe, stream);
2820 }
2821 
dpcm_fe_dai_close(struct snd_pcm_substream * fe_substream)2822 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2823 {
2824 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(fe_substream);
2825 	int ret;
2826 
2827 	snd_soc_dpcm_mutex_lock(fe);
2828 	ret = dpcm_fe_dai_shutdown(fe_substream);
2829 
2830 	dpcm_fe_dai_cleanup(fe_substream);
2831 
2832 	snd_soc_dpcm_mutex_unlock(fe);
2833 	return ret;
2834 }
2835 
dpcm_fe_dai_open(struct snd_pcm_substream * fe_substream)2836 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2837 {
2838 	struct snd_soc_pcm_runtime *fe = snd_soc_substream_to_rtd(fe_substream);
2839 	struct snd_soc_dapm_widget_list *list;
2840 	int ret;
2841 	int stream = fe_substream->stream;
2842 
2843 	snd_soc_dpcm_mutex_lock(fe);
2844 
2845 	ret = dpcm_path_get(fe, stream, &list);
2846 	if (ret < 0)
2847 		goto open_end;
2848 
2849 	/* calculate valid and active FE <-> BE dpcms */
2850 	dpcm_process_paths(fe, stream, &list, 1);
2851 
2852 	ret = dpcm_fe_dai_startup(fe_substream);
2853 	if (ret < 0)
2854 		dpcm_fe_dai_cleanup(fe_substream);
2855 
2856 	dpcm_clear_pending_state(fe, stream);
2857 	dpcm_path_put(&list);
2858 open_end:
2859 	snd_soc_dpcm_mutex_unlock(fe);
2860 	return ret;
2861 }
2862 
soc_get_playback_capture(struct snd_soc_pcm_runtime * rtd,int * playback,int * capture)2863 static int soc_get_playback_capture(struct snd_soc_pcm_runtime *rtd,
2864 				    int *playback, int *capture)
2865 {
2866 	struct snd_soc_dai_link *dai_link = rtd->dai_link;
2867 	struct snd_soc_dai *cpu_dai;
2868 	struct snd_soc_dai_link_ch_map *ch_maps;
2869 	int has_playback = 0;
2870 	int has_capture  = 0;
2871 	int i;
2872 
2873 	if (dai_link->dynamic && dai_link->num_cpus > 1) {
2874 		dev_err(rtd->dev, "DPCM doesn't support Multi CPU for Front-Ends yet\n");
2875 		return -EINVAL;
2876 	}
2877 
2878 	if (dai_link->dynamic || dai_link->no_pcm) {
2879 
2880 		for_each_rtd_ch_maps(rtd, i, ch_maps) {
2881 			cpu_dai	  = snd_soc_rtd_to_cpu(rtd,   ch_maps->cpu);
2882 
2883 			if (snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_PLAYBACK))
2884 				has_playback = 1;
2885 
2886 			if (snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_CAPTURE))
2887 				has_capture = 1;
2888 		}
2889 
2890 		/*
2891 		 * REMOVE ME
2892 		 *
2893 		 * dpcm_xxx flag will be removed soon, Indicates warning if dpcm_xxx flag was used
2894 		 * as availability limitation
2895 		 */
2896 		if (has_playback && has_capture) {
2897 			if ( dai_link->dpcm_playback &&
2898 			    !dai_link->dpcm_capture  &&
2899 			    !dai_link->playback_only) {
2900 				dev_warn(rtd->card->dev,
2901 					 "both playback/capture are available,"
2902 					 " but not using playback_only flag (%s)\n",
2903 					 dai_link->stream_name);
2904 				dev_warn(rtd->card->dev,
2905 					 "dpcm_playback/capture are no longer needed,"
2906 					 " please use playback/capture_only instead\n");
2907 				has_capture = 0;
2908 			}
2909 
2910 			if (!dai_link->dpcm_playback &&
2911 			     dai_link->dpcm_capture  &&
2912 			    !dai_link->capture_only) {
2913 				dev_warn(rtd->card->dev,
2914 					 "both playback/capture are available,"
2915 					 " but not using capture_only flag (%s)\n",
2916 					 dai_link->stream_name);
2917 				dev_warn(rtd->card->dev,
2918 					 "dpcm_playback/capture are no longer needed,"
2919 					 " please use playback/capture_only instead\n");
2920 				has_playback = 0;
2921 			}
2922 		}
2923 	} else {
2924 		struct snd_soc_dai *codec_dai;
2925 
2926 		/* Adapt stream for codec2codec links */
2927 		int cpu_capture  = snd_soc_get_stream_cpu(dai_link, SNDRV_PCM_STREAM_CAPTURE);
2928 		int cpu_playback = snd_soc_get_stream_cpu(dai_link, SNDRV_PCM_STREAM_PLAYBACK);
2929 
2930 		/*
2931 		 * see
2932 		 *	soc.h :: [dai_link->ch_maps Image sample]
2933 		 */
2934 		for_each_rtd_ch_maps(rtd, i, ch_maps) {
2935 			cpu_dai	  = snd_soc_rtd_to_cpu(rtd,   ch_maps->cpu);
2936 			codec_dai = snd_soc_rtd_to_codec(rtd, ch_maps->codec);
2937 
2938 			if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
2939 			    snd_soc_dai_stream_valid(cpu_dai,   cpu_playback))
2940 				has_playback = 1;
2941 			if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
2942 			    snd_soc_dai_stream_valid(cpu_dai,   cpu_capture))
2943 				has_capture = 1;
2944 		}
2945 	}
2946 
2947 	if (dai_link->playback_only)
2948 		has_capture = 0;
2949 
2950 	if (dai_link->capture_only)
2951 		has_playback = 0;
2952 
2953 	if (!has_playback && !has_capture) {
2954 		dev_err(rtd->dev, "substream %s has no playback, no capture\n",
2955 			dai_link->stream_name);
2956 
2957 		return -EINVAL;
2958 	}
2959 
2960 	*playback = has_playback;
2961 	*capture  = has_capture;
2962 
2963 	return 0;
2964 }
2965 
soc_create_pcm(struct snd_pcm ** pcm,struct snd_soc_pcm_runtime * rtd,int playback,int capture,int num)2966 static int soc_create_pcm(struct snd_pcm **pcm,
2967 			  struct snd_soc_pcm_runtime *rtd,
2968 			  int playback, int capture, int num)
2969 {
2970 	char new_name[64];
2971 	int ret;
2972 
2973 	/* create the PCM */
2974 	if (rtd->dai_link->c2c_params) {
2975 		snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2976 			 rtd->dai_link->stream_name);
2977 
2978 		ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2979 					   playback, capture, pcm);
2980 	} else if (rtd->dai_link->no_pcm) {
2981 		snprintf(new_name, sizeof(new_name), "(%s)",
2982 			rtd->dai_link->stream_name);
2983 
2984 		ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2985 				playback, capture, pcm);
2986 	} else {
2987 		if (rtd->dai_link->dynamic)
2988 			snprintf(new_name, sizeof(new_name), "%s (*)",
2989 				rtd->dai_link->stream_name);
2990 		else
2991 			snprintf(new_name, sizeof(new_name), "%s %s-%d",
2992 				rtd->dai_link->stream_name,
2993 				soc_codec_dai_name(rtd), num);
2994 
2995 		ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2996 			capture, pcm);
2997 	}
2998 	if (ret < 0) {
2999 		dev_err(rtd->card->dev, "ASoC: can't create pcm %s for dailink %s: %d\n",
3000 			new_name, rtd->dai_link->name, ret);
3001 		return ret;
3002 	}
3003 	dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
3004 
3005 	return 0;
3006 }
3007 
3008 /* create a new pcm */
soc_new_pcm(struct snd_soc_pcm_runtime * rtd,int num)3009 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
3010 {
3011 	struct snd_soc_component *component;
3012 	struct snd_pcm *pcm;
3013 	int ret = 0, playback = 0, capture = 0;
3014 	int i;
3015 
3016 	ret = soc_get_playback_capture(rtd, &playback, &capture);
3017 	if (ret < 0)
3018 		return ret;
3019 
3020 	ret = soc_create_pcm(&pcm, rtd, playback, capture, num);
3021 	if (ret < 0)
3022 		return ret;
3023 
3024 	/* DAPM dai link stream work */
3025 	/*
3026 	 * Currently nothing to do for c2c links
3027 	 * Since c2c links are internal nodes in the DAPM graph and
3028 	 * don't interface with the outside world or application layer
3029 	 * we don't have to do any special handling on close.
3030 	 */
3031 	if (!rtd->dai_link->c2c_params)
3032 		rtd->close_delayed_work_func = snd_soc_close_delayed_work;
3033 
3034 	rtd->pcm = pcm;
3035 	pcm->nonatomic = rtd->dai_link->nonatomic;
3036 	pcm->private_data = rtd;
3037 	pcm->no_device_suspend = true;
3038 
3039 	if (rtd->dai_link->no_pcm || rtd->dai_link->c2c_params) {
3040 		if (playback)
3041 			pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
3042 		if (capture)
3043 			pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
3044 		goto out;
3045 	}
3046 
3047 	/* ASoC PCM operations */
3048 	if (rtd->dai_link->dynamic) {
3049 		rtd->ops.open		= dpcm_fe_dai_open;
3050 		rtd->ops.hw_params	= dpcm_fe_dai_hw_params;
3051 		rtd->ops.prepare	= dpcm_fe_dai_prepare;
3052 		rtd->ops.trigger	= dpcm_fe_dai_trigger;
3053 		rtd->ops.hw_free	= dpcm_fe_dai_hw_free;
3054 		rtd->ops.close		= dpcm_fe_dai_close;
3055 		rtd->ops.pointer	= soc_pcm_pointer;
3056 	} else {
3057 		rtd->ops.open		= soc_pcm_open;
3058 		rtd->ops.hw_params	= soc_pcm_hw_params;
3059 		rtd->ops.prepare	= soc_pcm_prepare;
3060 		rtd->ops.trigger	= soc_pcm_trigger;
3061 		rtd->ops.hw_free	= soc_pcm_hw_free;
3062 		rtd->ops.close		= soc_pcm_close;
3063 		rtd->ops.pointer	= soc_pcm_pointer;
3064 	}
3065 
3066 	for_each_rtd_components(rtd, i, component) {
3067 		const struct snd_soc_component_driver *drv = component->driver;
3068 
3069 		if (drv->ioctl)
3070 			rtd->ops.ioctl		= snd_soc_pcm_component_ioctl;
3071 		if (drv->sync_stop)
3072 			rtd->ops.sync_stop	= snd_soc_pcm_component_sync_stop;
3073 		if (drv->copy)
3074 			rtd->ops.copy		= snd_soc_pcm_component_copy;
3075 		if (drv->page)
3076 			rtd->ops.page		= snd_soc_pcm_component_page;
3077 		if (drv->mmap)
3078 			rtd->ops.mmap		= snd_soc_pcm_component_mmap;
3079 		if (drv->ack)
3080 			rtd->ops.ack            = snd_soc_pcm_component_ack;
3081 	}
3082 
3083 	if (playback)
3084 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
3085 
3086 	if (capture)
3087 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
3088 
3089 	ret = snd_soc_pcm_component_new(rtd);
3090 	if (ret < 0)
3091 		return ret;
3092 out:
3093 	dev_dbg(rtd->card->dev, "%s <-> %s mapping ok\n",
3094 		soc_codec_dai_name(rtd), soc_cpu_dai_name(rtd));
3095 	return ret;
3096 }
3097 
3098 /* get the substream for this BE */
3099 struct snd_pcm_substream *
snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime * be,int stream)3100 	snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
3101 {
3102 	return be->pcm->streams[stream].substream;
3103 }
3104 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3105