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