1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Digital Audio (PCM) abstract layer 4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz> 5 * Abramo Bagnara <abramo@alsa-project.org> 6 */ 7 8 #include <linux/slab.h> 9 #include <linux/sched/signal.h> 10 #include <linux/time.h> 11 #include <linux/math64.h> 12 #include <linux/export.h> 13 #include <sound/core.h> 14 #include <sound/control.h> 15 #include <sound/tlv.h> 16 #include <sound/info.h> 17 #include <sound/pcm.h> 18 #include <sound/pcm_params.h> 19 #include <sound/timer.h> 20 21 #include "pcm_local.h" 22 23 #ifdef CONFIG_SND_PCM_XRUN_DEBUG 24 #define CREATE_TRACE_POINTS 25 #include "pcm_trace.h" 26 #else 27 #define trace_hwptr(substream, pos, in_interrupt) 28 #define trace_xrun(substream) 29 #define trace_hw_ptr_error(substream, reason) 30 #define trace_applptr(substream, prev, curr) 31 #endif 32 33 static int fill_silence_frames(struct snd_pcm_substream *substream, 34 snd_pcm_uframes_t off, snd_pcm_uframes_t frames); 35 36 37 static inline void update_silence_vars(struct snd_pcm_runtime *runtime, 38 snd_pcm_uframes_t ptr, 39 snd_pcm_uframes_t new_ptr) 40 { 41 snd_pcm_sframes_t delta; 42 43 delta = new_ptr - ptr; 44 if (delta == 0) 45 return; 46 if (delta < 0) 47 delta += runtime->boundary; 48 if ((snd_pcm_uframes_t)delta < runtime->silence_filled) 49 runtime->silence_filled -= delta; 50 else 51 runtime->silence_filled = 0; 52 runtime->silence_start = new_ptr; 53 } 54 55 /* 56 * fill ring buffer with silence 57 * runtime->silence_start: starting pointer to silence area 58 * runtime->silence_filled: size filled with silence 59 * runtime->silence_threshold: threshold from application 60 * runtime->silence_size: maximal size from application 61 * 62 * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately 63 */ 64 void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr) 65 { 66 struct snd_pcm_runtime *runtime = substream->runtime; 67 snd_pcm_uframes_t frames, ofs, transfer; 68 int err; 69 70 if (runtime->silence_size < runtime->boundary) { 71 snd_pcm_sframes_t noise_dist; 72 snd_pcm_uframes_t appl_ptr = READ_ONCE(runtime->control->appl_ptr); 73 update_silence_vars(runtime, runtime->silence_start, appl_ptr); 74 /* initialization outside pointer updates */ 75 if (new_hw_ptr == ULONG_MAX) 76 new_hw_ptr = runtime->status->hw_ptr; 77 /* get hw_avail with the boundary crossing */ 78 noise_dist = appl_ptr - new_hw_ptr; 79 if (noise_dist < 0) 80 noise_dist += runtime->boundary; 81 /* total noise distance */ 82 noise_dist += runtime->silence_filled; 83 if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold) 84 return; 85 frames = runtime->silence_threshold - noise_dist; 86 if (frames > runtime->silence_size) 87 frames = runtime->silence_size; 88 } else { 89 /* 90 * This filling mode aims at free-running mode (used for example by dmix), 91 * which doesn't update the application pointer. 92 */ 93 snd_pcm_uframes_t hw_ptr = runtime->status->hw_ptr; 94 if (new_hw_ptr == ULONG_MAX) { 95 /* 96 * Initialization, fill the whole unused buffer with silence. 97 * 98 * Usually, this is entered while stopped, before data is queued, 99 * so both pointers are expected to be zero. 100 */ 101 snd_pcm_sframes_t avail = runtime->control->appl_ptr - hw_ptr; 102 if (avail < 0) 103 avail += runtime->boundary; 104 /* 105 * In free-running mode, appl_ptr will be zero even while running, 106 * so we end up with a huge number. There is no useful way to 107 * handle this, so we just clear the whole buffer. 108 */ 109 runtime->silence_filled = avail > runtime->buffer_size ? 0 : avail; 110 runtime->silence_start = hw_ptr; 111 } else { 112 /* Silence the just played area immediately */ 113 update_silence_vars(runtime, hw_ptr, new_hw_ptr); 114 } 115 /* 116 * In this mode, silence_filled actually includes the valid 117 * sample data from the user. 118 */ 119 frames = runtime->buffer_size - runtime->silence_filled; 120 } 121 if (snd_BUG_ON(frames > runtime->buffer_size)) 122 return; 123 if (frames == 0) 124 return; 125 ofs = (runtime->silence_start + runtime->silence_filled) % runtime->buffer_size; 126 do { 127 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames; 128 err = fill_silence_frames(substream, ofs, transfer); 129 snd_BUG_ON(err < 0); 130 runtime->silence_filled += transfer; 131 frames -= transfer; 132 ofs = 0; 133 } while (frames > 0); 134 snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE); 135 } 136 137 #ifdef CONFIG_SND_DEBUG 138 void snd_pcm_debug_name(struct snd_pcm_substream *substream, 139 char *name, size_t len) 140 { 141 snprintf(name, len, "pcmC%dD%d%c:%d", 142 substream->pcm->card->number, 143 substream->pcm->device, 144 substream->stream ? 'c' : 'p', 145 substream->number); 146 } 147 EXPORT_SYMBOL(snd_pcm_debug_name); 148 #endif 149 150 #define XRUN_DEBUG_BASIC (1<<0) 151 #define XRUN_DEBUG_STACK (1<<1) /* dump also stack */ 152 #define XRUN_DEBUG_JIFFIESCHECK (1<<2) /* do jiffies check */ 153 154 #ifdef CONFIG_SND_PCM_XRUN_DEBUG 155 156 #define xrun_debug(substream, mask) \ 157 ((substream)->pstr->xrun_debug & (mask)) 158 #else 159 #define xrun_debug(substream, mask) 0 160 #endif 161 162 #define dump_stack_on_xrun(substream) do { \ 163 if (xrun_debug(substream, XRUN_DEBUG_STACK)) \ 164 dump_stack(); \ 165 } while (0) 166 167 /* call with stream lock held */ 168 void __snd_pcm_xrun(struct snd_pcm_substream *substream) 169 { 170 struct snd_pcm_runtime *runtime = substream->runtime; 171 172 trace_xrun(substream); 173 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) { 174 struct timespec64 tstamp; 175 176 snd_pcm_gettime(runtime, &tstamp); 177 runtime->status->tstamp.tv_sec = tstamp.tv_sec; 178 runtime->status->tstamp.tv_nsec = tstamp.tv_nsec; 179 } 180 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); 181 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { 182 char name[16]; 183 snd_pcm_debug_name(substream, name, sizeof(name)); 184 pcm_warn(substream->pcm, "XRUN: %s\n", name); 185 dump_stack_on_xrun(substream); 186 } 187 #ifdef CONFIG_SND_PCM_XRUN_DEBUG 188 substream->xrun_counter++; 189 #endif 190 } 191 192 #ifdef CONFIG_SND_PCM_XRUN_DEBUG 193 #define hw_ptr_error(substream, in_interrupt, reason, fmt, args...) \ 194 do { \ 195 trace_hw_ptr_error(substream, reason); \ 196 if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { \ 197 pr_err_ratelimited("ALSA: PCM: [%c] " reason ": " fmt, \ 198 (in_interrupt) ? 'Q' : 'P', ##args); \ 199 dump_stack_on_xrun(substream); \ 200 } \ 201 } while (0) 202 203 #else /* ! CONFIG_SND_PCM_XRUN_DEBUG */ 204 205 #define hw_ptr_error(substream, fmt, args...) do { } while (0) 206 207 #endif 208 209 int snd_pcm_update_state(struct snd_pcm_substream *substream, 210 struct snd_pcm_runtime *runtime) 211 { 212 snd_pcm_uframes_t avail; 213 214 avail = snd_pcm_avail(substream); 215 if (avail > runtime->avail_max) 216 runtime->avail_max = avail; 217 if (runtime->state == SNDRV_PCM_STATE_DRAINING) { 218 if (avail >= runtime->buffer_size) { 219 snd_pcm_drain_done(substream); 220 return -EPIPE; 221 } 222 } else { 223 if (avail >= runtime->stop_threshold) { 224 __snd_pcm_xrun(substream); 225 return -EPIPE; 226 } 227 } 228 if (runtime->twake) { 229 if (avail >= runtime->twake) 230 wake_up(&runtime->tsleep); 231 } else if (avail >= runtime->control->avail_min) 232 wake_up(&runtime->sleep); 233 return 0; 234 } 235 236 static void update_audio_tstamp(struct snd_pcm_substream *substream, 237 struct timespec64 *curr_tstamp, 238 struct timespec64 *audio_tstamp) 239 { 240 struct snd_pcm_runtime *runtime = substream->runtime; 241 u64 audio_frames, audio_nsecs; 242 struct timespec64 driver_tstamp; 243 244 if (runtime->tstamp_mode != SNDRV_PCM_TSTAMP_ENABLE) 245 return; 246 247 if (!(substream->ops->get_time_info) || 248 (runtime->audio_tstamp_config.type_requested == 249 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT) || 250 (runtime->audio_tstamp_report.actual_type == 251 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) { 252 253 /* 254 * provide audio timestamp derived from pointer position 255 * add delay only if requested 256 */ 257 258 audio_frames = runtime->hw_ptr_wrap + runtime->status->hw_ptr; 259 260 if (runtime->audio_tstamp_config.report_delay) { 261 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 262 audio_frames -= runtime->delay; 263 else 264 audio_frames += runtime->delay; 265 } 266 audio_nsecs = div_u64(audio_frames * 1000000000LL, 267 runtime->rate); 268 *audio_tstamp = ns_to_timespec64(audio_nsecs); 269 } 270 271 if (runtime->status->audio_tstamp.tv_sec != audio_tstamp->tv_sec || 272 runtime->status->audio_tstamp.tv_nsec != audio_tstamp->tv_nsec) { 273 runtime->status->audio_tstamp.tv_sec = audio_tstamp->tv_sec; 274 runtime->status->audio_tstamp.tv_nsec = audio_tstamp->tv_nsec; 275 runtime->status->tstamp.tv_sec = curr_tstamp->tv_sec; 276 runtime->status->tstamp.tv_nsec = curr_tstamp->tv_nsec; 277 } 278 279 280 /* 281 * re-take a driver timestamp to let apps detect if the reference tstamp 282 * read by low-level hardware was provided with a delay 283 */ 284 snd_pcm_gettime(substream->runtime, &driver_tstamp); 285 runtime->driver_tstamp = driver_tstamp; 286 } 287 288 static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream, 289 unsigned int in_interrupt) 290 { 291 struct snd_pcm_runtime *runtime = substream->runtime; 292 snd_pcm_uframes_t pos; 293 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base; 294 snd_pcm_sframes_t hdelta, delta; 295 unsigned long jdelta; 296 unsigned long curr_jiffies; 297 struct timespec64 curr_tstamp; 298 struct timespec64 audio_tstamp; 299 int crossed_boundary = 0; 300 301 old_hw_ptr = runtime->status->hw_ptr; 302 303 /* 304 * group pointer, time and jiffies reads to allow for more 305 * accurate correlations/corrections. 306 * The values are stored at the end of this routine after 307 * corrections for hw_ptr position 308 */ 309 pos = substream->ops->pointer(substream); 310 curr_jiffies = jiffies; 311 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) { 312 if ((substream->ops->get_time_info) && 313 (runtime->audio_tstamp_config.type_requested != SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) { 314 substream->ops->get_time_info(substream, &curr_tstamp, 315 &audio_tstamp, 316 &runtime->audio_tstamp_config, 317 &runtime->audio_tstamp_report); 318 319 /* re-test in case tstamp type is not supported in hardware and was demoted to DEFAULT */ 320 if (runtime->audio_tstamp_report.actual_type == SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT) 321 snd_pcm_gettime(runtime, &curr_tstamp); 322 } else 323 snd_pcm_gettime(runtime, &curr_tstamp); 324 } 325 326 if (pos == SNDRV_PCM_POS_XRUN) { 327 __snd_pcm_xrun(substream); 328 return -EPIPE; 329 } 330 if (pos >= runtime->buffer_size) { 331 if (printk_ratelimit()) { 332 char name[16]; 333 snd_pcm_debug_name(substream, name, sizeof(name)); 334 pcm_err(substream->pcm, 335 "invalid position: %s, pos = %ld, buffer size = %ld, period size = %ld\n", 336 name, pos, runtime->buffer_size, 337 runtime->period_size); 338 } 339 pos = 0; 340 } 341 pos -= pos % runtime->min_align; 342 trace_hwptr(substream, pos, in_interrupt); 343 hw_base = runtime->hw_ptr_base; 344 new_hw_ptr = hw_base + pos; 345 if (in_interrupt) { 346 /* we know that one period was processed */ 347 /* delta = "expected next hw_ptr" for in_interrupt != 0 */ 348 delta = runtime->hw_ptr_interrupt + runtime->period_size; 349 if (delta > new_hw_ptr) { 350 /* check for double acknowledged interrupts */ 351 hdelta = curr_jiffies - runtime->hw_ptr_jiffies; 352 if (hdelta > runtime->hw_ptr_buffer_jiffies/2 + 1) { 353 hw_base += runtime->buffer_size; 354 if (hw_base >= runtime->boundary) { 355 hw_base = 0; 356 crossed_boundary++; 357 } 358 new_hw_ptr = hw_base + pos; 359 goto __delta; 360 } 361 } 362 } 363 /* new_hw_ptr might be lower than old_hw_ptr in case when */ 364 /* pointer crosses the end of the ring buffer */ 365 if (new_hw_ptr < old_hw_ptr) { 366 hw_base += runtime->buffer_size; 367 if (hw_base >= runtime->boundary) { 368 hw_base = 0; 369 crossed_boundary++; 370 } 371 new_hw_ptr = hw_base + pos; 372 } 373 __delta: 374 delta = new_hw_ptr - old_hw_ptr; 375 if (delta < 0) 376 delta += runtime->boundary; 377 378 if (runtime->no_period_wakeup) { 379 snd_pcm_sframes_t xrun_threshold; 380 /* 381 * Without regular period interrupts, we have to check 382 * the elapsed time to detect xruns. 383 */ 384 jdelta = curr_jiffies - runtime->hw_ptr_jiffies; 385 if (jdelta < runtime->hw_ptr_buffer_jiffies / 2) 386 goto no_delta_check; 387 hdelta = jdelta - delta * HZ / runtime->rate; 388 xrun_threshold = runtime->hw_ptr_buffer_jiffies / 2 + 1; 389 while (hdelta > xrun_threshold) { 390 delta += runtime->buffer_size; 391 hw_base += runtime->buffer_size; 392 if (hw_base >= runtime->boundary) { 393 hw_base = 0; 394 crossed_boundary++; 395 } 396 new_hw_ptr = hw_base + pos; 397 hdelta -= runtime->hw_ptr_buffer_jiffies; 398 } 399 goto no_delta_check; 400 } 401 402 /* something must be really wrong */ 403 if (delta >= runtime->buffer_size + runtime->period_size) { 404 hw_ptr_error(substream, in_interrupt, "Unexpected hw_ptr", 405 "(stream=%i, pos=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n", 406 substream->stream, (long)pos, 407 (long)new_hw_ptr, (long)old_hw_ptr); 408 return 0; 409 } 410 411 /* Do jiffies check only in xrun_debug mode */ 412 if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK)) 413 goto no_jiffies_check; 414 415 /* Skip the jiffies check for hardwares with BATCH flag. 416 * Such hardware usually just increases the position at each IRQ, 417 * thus it can't give any strange position. 418 */ 419 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH) 420 goto no_jiffies_check; 421 hdelta = delta; 422 if (hdelta < runtime->delay) 423 goto no_jiffies_check; 424 hdelta -= runtime->delay; 425 jdelta = curr_jiffies - runtime->hw_ptr_jiffies; 426 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) { 427 delta = jdelta / 428 (((runtime->period_size * HZ) / runtime->rate) 429 + HZ/100); 430 /* move new_hw_ptr according jiffies not pos variable */ 431 new_hw_ptr = old_hw_ptr; 432 hw_base = delta; 433 /* use loop to avoid checks for delta overflows */ 434 /* the delta value is small or zero in most cases */ 435 while (delta > 0) { 436 new_hw_ptr += runtime->period_size; 437 if (new_hw_ptr >= runtime->boundary) { 438 new_hw_ptr -= runtime->boundary; 439 crossed_boundary--; 440 } 441 delta--; 442 } 443 /* align hw_base to buffer_size */ 444 hw_ptr_error(substream, in_interrupt, "hw_ptr skipping", 445 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu/%lu, hw_ptr=%ld/%ld)\n", 446 (long)pos, (long)hdelta, 447 (long)runtime->period_size, jdelta, 448 ((hdelta * HZ) / runtime->rate), hw_base, 449 (unsigned long)old_hw_ptr, 450 (unsigned long)new_hw_ptr); 451 /* reset values to proper state */ 452 delta = 0; 453 hw_base = new_hw_ptr - (new_hw_ptr % runtime->buffer_size); 454 } 455 no_jiffies_check: 456 if (delta > runtime->period_size + runtime->period_size / 2) { 457 hw_ptr_error(substream, in_interrupt, 458 "Lost interrupts?", 459 "(stream=%i, delta=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n", 460 substream->stream, (long)delta, 461 (long)new_hw_ptr, 462 (long)old_hw_ptr); 463 } 464 465 no_delta_check: 466 if (runtime->status->hw_ptr == new_hw_ptr) { 467 runtime->hw_ptr_jiffies = curr_jiffies; 468 update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp); 469 return 0; 470 } 471 472 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && 473 runtime->silence_size > 0) 474 snd_pcm_playback_silence(substream, new_hw_ptr); 475 476 if (in_interrupt) { 477 delta = new_hw_ptr - runtime->hw_ptr_interrupt; 478 if (delta < 0) 479 delta += runtime->boundary; 480 delta -= (snd_pcm_uframes_t)delta % runtime->period_size; 481 runtime->hw_ptr_interrupt += delta; 482 if (runtime->hw_ptr_interrupt >= runtime->boundary) 483 runtime->hw_ptr_interrupt -= runtime->boundary; 484 } 485 runtime->hw_ptr_base = hw_base; 486 runtime->status->hw_ptr = new_hw_ptr; 487 runtime->hw_ptr_jiffies = curr_jiffies; 488 if (crossed_boundary) { 489 snd_BUG_ON(crossed_boundary != 1); 490 runtime->hw_ptr_wrap += runtime->boundary; 491 } 492 493 update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp); 494 495 return snd_pcm_update_state(substream, runtime); 496 } 497 498 /* CAUTION: call it with irq disabled */ 499 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream) 500 { 501 return snd_pcm_update_hw_ptr0(substream, 0); 502 } 503 504 /** 505 * snd_pcm_set_ops - set the PCM operators 506 * @pcm: the pcm instance 507 * @direction: stream direction, SNDRV_PCM_STREAM_XXX 508 * @ops: the operator table 509 * 510 * Sets the given PCM operators to the pcm instance. 511 */ 512 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, 513 const struct snd_pcm_ops *ops) 514 { 515 struct snd_pcm_str *stream = &pcm->streams[direction]; 516 struct snd_pcm_substream *substream; 517 518 for (substream = stream->substream; substream != NULL; substream = substream->next) 519 substream->ops = ops; 520 } 521 EXPORT_SYMBOL(snd_pcm_set_ops); 522 523 /** 524 * snd_pcm_set_sync_per_card - set the PCM sync id with card number 525 * @substream: the pcm substream 526 * @params: modified hardware parameters 527 * @id: identifier (max 12 bytes) 528 * @len: identifier length (max 12 bytes) 529 * 530 * Sets the PCM sync identifier for the card with zero padding. 531 * 532 * User space or any user should use this 16-byte identifier for a comparison only 533 * to check if two IDs are similar or different. Special case is the identifier 534 * containing only zeros. Interpretation for this combination is - empty (not set). 535 * The contents of the identifier should not be interpreted in any other way. 536 * 537 * The synchronization ID must be unique per clock source (usually one sound card, 538 * but multiple soundcard may use one PCM word clock source which means that they 539 * are fully synchronized). 540 * 541 * This routine composes this ID using card number in first four bytes and 542 * 12-byte additional ID. When other ID composition is used (e.g. for multiple 543 * sound cards), make sure that the composition does not clash with this 544 * composition scheme. 545 */ 546 void snd_pcm_set_sync_per_card(struct snd_pcm_substream *substream, 547 struct snd_pcm_hw_params *params, 548 const unsigned char *id, unsigned int len) 549 { 550 *(__le32 *)params->sync = cpu_to_le32(substream->pcm->card->number); 551 len = min(12, len); 552 memcpy(params->sync + 4, id, len); 553 memset(params->sync + 4 + len, 0, 12 - len); 554 } 555 EXPORT_SYMBOL_GPL(snd_pcm_set_sync_per_card); 556 557 /* 558 * Standard ioctl routine 559 */ 560 561 static inline unsigned int div32(unsigned int a, unsigned int b, 562 unsigned int *r) 563 { 564 if (b == 0) { 565 *r = 0; 566 return UINT_MAX; 567 } 568 *r = a % b; 569 return a / b; 570 } 571 572 static inline unsigned int div_down(unsigned int a, unsigned int b) 573 { 574 if (b == 0) 575 return UINT_MAX; 576 return a / b; 577 } 578 579 static inline unsigned int div_up(unsigned int a, unsigned int b) 580 { 581 unsigned int r; 582 unsigned int q; 583 if (b == 0) 584 return UINT_MAX; 585 q = div32(a, b, &r); 586 if (r) 587 ++q; 588 return q; 589 } 590 591 static inline unsigned int mul(unsigned int a, unsigned int b) 592 { 593 if (a == 0) 594 return 0; 595 if (div_down(UINT_MAX, a) < b) 596 return UINT_MAX; 597 return a * b; 598 } 599 600 static inline unsigned int muldiv32(unsigned int a, unsigned int b, 601 unsigned int c, unsigned int *r) 602 { 603 u_int64_t n = (u_int64_t) a * b; 604 if (c == 0) { 605 *r = 0; 606 return UINT_MAX; 607 } 608 n = div_u64_rem(n, c, r); 609 if (n >= UINT_MAX) { 610 *r = 0; 611 return UINT_MAX; 612 } 613 return n; 614 } 615 616 /** 617 * snd_interval_refine - refine the interval value of configurator 618 * @i: the interval value to refine 619 * @v: the interval value to refer to 620 * 621 * Refines the interval value with the reference value. 622 * The interval is changed to the range satisfying both intervals. 623 * The interval status (min, max, integer, etc.) are evaluated. 624 * 625 * Return: Positive if the value is changed, zero if it's not changed, or a 626 * negative error code. 627 */ 628 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v) 629 { 630 int changed = 0; 631 if (snd_BUG_ON(snd_interval_empty(i))) 632 return -EINVAL; 633 if (i->min < v->min) { 634 i->min = v->min; 635 i->openmin = v->openmin; 636 changed = 1; 637 } else if (i->min == v->min && !i->openmin && v->openmin) { 638 i->openmin = 1; 639 changed = 1; 640 } 641 if (i->max > v->max) { 642 i->max = v->max; 643 i->openmax = v->openmax; 644 changed = 1; 645 } else if (i->max == v->max && !i->openmax && v->openmax) { 646 i->openmax = 1; 647 changed = 1; 648 } 649 if (!i->integer && v->integer) { 650 i->integer = 1; 651 changed = 1; 652 } 653 if (i->integer) { 654 if (i->openmin) { 655 i->min++; 656 i->openmin = 0; 657 } 658 if (i->openmax) { 659 i->max--; 660 i->openmax = 0; 661 } 662 } else if (!i->openmin && !i->openmax && i->min == i->max) 663 i->integer = 1; 664 if (snd_interval_checkempty(i)) { 665 snd_interval_none(i); 666 return -EINVAL; 667 } 668 return changed; 669 } 670 EXPORT_SYMBOL(snd_interval_refine); 671 672 static int snd_interval_refine_first(struct snd_interval *i) 673 { 674 const unsigned int last_max = i->max; 675 676 if (snd_BUG_ON(snd_interval_empty(i))) 677 return -EINVAL; 678 if (snd_interval_single(i)) 679 return 0; 680 i->max = i->min; 681 if (i->openmin) 682 i->max++; 683 /* only exclude max value if also excluded before refine */ 684 i->openmax = (i->openmax && i->max >= last_max); 685 return 1; 686 } 687 688 static int snd_interval_refine_last(struct snd_interval *i) 689 { 690 const unsigned int last_min = i->min; 691 692 if (snd_BUG_ON(snd_interval_empty(i))) 693 return -EINVAL; 694 if (snd_interval_single(i)) 695 return 0; 696 i->min = i->max; 697 if (i->openmax) 698 i->min--; 699 /* only exclude min value if also excluded before refine */ 700 i->openmin = (i->openmin && i->min <= last_min); 701 return 1; 702 } 703 704 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c) 705 { 706 if (a->empty || b->empty) { 707 snd_interval_none(c); 708 return; 709 } 710 c->empty = 0; 711 c->min = mul(a->min, b->min); 712 c->openmin = (a->openmin || b->openmin); 713 c->max = mul(a->max, b->max); 714 c->openmax = (a->openmax || b->openmax); 715 c->integer = (a->integer && b->integer); 716 } 717 718 /** 719 * snd_interval_div - refine the interval value with division 720 * @a: dividend 721 * @b: divisor 722 * @c: quotient 723 * 724 * c = a / b 725 * 726 * Returns non-zero if the value is changed, zero if not changed. 727 */ 728 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c) 729 { 730 unsigned int r; 731 if (a->empty || b->empty) { 732 snd_interval_none(c); 733 return; 734 } 735 c->empty = 0; 736 c->min = div32(a->min, b->max, &r); 737 c->openmin = (r || a->openmin || b->openmax); 738 if (b->min > 0) { 739 c->max = div32(a->max, b->min, &r); 740 if (r) { 741 c->max++; 742 c->openmax = 1; 743 } else 744 c->openmax = (a->openmax || b->openmin); 745 } else { 746 c->max = UINT_MAX; 747 c->openmax = 0; 748 } 749 c->integer = 0; 750 } 751 752 /** 753 * snd_interval_muldivk - refine the interval value 754 * @a: dividend 1 755 * @b: dividend 2 756 * @k: divisor (as integer) 757 * @c: result 758 * 759 * c = a * b / k 760 * 761 * Returns non-zero if the value is changed, zero if not changed. 762 */ 763 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b, 764 unsigned int k, struct snd_interval *c) 765 { 766 unsigned int r; 767 if (a->empty || b->empty) { 768 snd_interval_none(c); 769 return; 770 } 771 c->empty = 0; 772 c->min = muldiv32(a->min, b->min, k, &r); 773 c->openmin = (r || a->openmin || b->openmin); 774 c->max = muldiv32(a->max, b->max, k, &r); 775 if (r) { 776 c->max++; 777 c->openmax = 1; 778 } else 779 c->openmax = (a->openmax || b->openmax); 780 c->integer = 0; 781 } 782 783 /** 784 * snd_interval_mulkdiv - refine the interval value 785 * @a: dividend 1 786 * @k: dividend 2 (as integer) 787 * @b: divisor 788 * @c: result 789 * 790 * c = a * k / b 791 * 792 * Returns non-zero if the value is changed, zero if not changed. 793 */ 794 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k, 795 const struct snd_interval *b, struct snd_interval *c) 796 { 797 unsigned int r; 798 if (a->empty || b->empty) { 799 snd_interval_none(c); 800 return; 801 } 802 c->empty = 0; 803 c->min = muldiv32(a->min, k, b->max, &r); 804 c->openmin = (r || a->openmin || b->openmax); 805 if (b->min > 0) { 806 c->max = muldiv32(a->max, k, b->min, &r); 807 if (r) { 808 c->max++; 809 c->openmax = 1; 810 } else 811 c->openmax = (a->openmax || b->openmin); 812 } else { 813 c->max = UINT_MAX; 814 c->openmax = 0; 815 } 816 c->integer = 0; 817 } 818 819 /* ---- */ 820 821 822 /** 823 * snd_interval_ratnum - refine the interval value 824 * @i: interval to refine 825 * @rats_count: number of ratnum_t 826 * @rats: ratnum_t array 827 * @nump: pointer to store the resultant numerator 828 * @denp: pointer to store the resultant denominator 829 * 830 * Return: Positive if the value is changed, zero if it's not changed, or a 831 * negative error code. 832 */ 833 int snd_interval_ratnum(struct snd_interval *i, 834 unsigned int rats_count, const struct snd_ratnum *rats, 835 unsigned int *nump, unsigned int *denp) 836 { 837 unsigned int best_num, best_den; 838 int best_diff; 839 unsigned int k; 840 struct snd_interval t; 841 int err; 842 unsigned int result_num, result_den; 843 int result_diff; 844 845 best_num = best_den = best_diff = 0; 846 for (k = 0; k < rats_count; ++k) { 847 unsigned int num = rats[k].num; 848 unsigned int den; 849 unsigned int q = i->min; 850 int diff; 851 if (q == 0) 852 q = 1; 853 den = div_up(num, q); 854 if (den < rats[k].den_min) 855 continue; 856 if (den > rats[k].den_max) 857 den = rats[k].den_max; 858 else { 859 unsigned int r; 860 r = (den - rats[k].den_min) % rats[k].den_step; 861 if (r != 0) 862 den -= r; 863 } 864 diff = num - q * den; 865 if (diff < 0) 866 diff = -diff; 867 if (best_num == 0 || 868 diff * best_den < best_diff * den) { 869 best_diff = diff; 870 best_den = den; 871 best_num = num; 872 } 873 } 874 if (best_den == 0) { 875 i->empty = 1; 876 return -EINVAL; 877 } 878 t.min = div_down(best_num, best_den); 879 t.openmin = !!(best_num % best_den); 880 881 result_num = best_num; 882 result_diff = best_diff; 883 result_den = best_den; 884 best_num = best_den = best_diff = 0; 885 for (k = 0; k < rats_count; ++k) { 886 unsigned int num = rats[k].num; 887 unsigned int den; 888 unsigned int q = i->max; 889 int diff; 890 if (q == 0) { 891 i->empty = 1; 892 return -EINVAL; 893 } 894 den = div_down(num, q); 895 if (den > rats[k].den_max) 896 continue; 897 if (den < rats[k].den_min) 898 den = rats[k].den_min; 899 else { 900 unsigned int r; 901 r = (den - rats[k].den_min) % rats[k].den_step; 902 if (r != 0) 903 den += rats[k].den_step - r; 904 } 905 diff = q * den - num; 906 if (diff < 0) 907 diff = -diff; 908 if (best_num == 0 || 909 diff * best_den < best_diff * den) { 910 best_diff = diff; 911 best_den = den; 912 best_num = num; 913 } 914 } 915 if (best_den == 0) { 916 i->empty = 1; 917 return -EINVAL; 918 } 919 t.max = div_up(best_num, best_den); 920 t.openmax = !!(best_num % best_den); 921 t.integer = 0; 922 err = snd_interval_refine(i, &t); 923 if (err < 0) 924 return err; 925 926 if (snd_interval_single(i)) { 927 if (best_diff * result_den < result_diff * best_den) { 928 result_num = best_num; 929 result_den = best_den; 930 } 931 if (nump) 932 *nump = result_num; 933 if (denp) 934 *denp = result_den; 935 } 936 return err; 937 } 938 EXPORT_SYMBOL(snd_interval_ratnum); 939 940 /** 941 * snd_interval_ratden - refine the interval value 942 * @i: interval to refine 943 * @rats_count: number of struct ratden 944 * @rats: struct ratden array 945 * @nump: pointer to store the resultant numerator 946 * @denp: pointer to store the resultant denominator 947 * 948 * Return: Positive if the value is changed, zero if it's not changed, or a 949 * negative error code. 950 */ 951 static int snd_interval_ratden(struct snd_interval *i, 952 unsigned int rats_count, 953 const struct snd_ratden *rats, 954 unsigned int *nump, unsigned int *denp) 955 { 956 unsigned int best_num, best_diff, best_den; 957 unsigned int k; 958 struct snd_interval t; 959 int err; 960 961 best_num = best_den = best_diff = 0; 962 for (k = 0; k < rats_count; ++k) { 963 unsigned int num; 964 unsigned int den = rats[k].den; 965 unsigned int q = i->min; 966 int diff; 967 num = mul(q, den); 968 if (num > rats[k].num_max) 969 continue; 970 if (num < rats[k].num_min) 971 num = rats[k].num_max; 972 else { 973 unsigned int r; 974 r = (num - rats[k].num_min) % rats[k].num_step; 975 if (r != 0) 976 num += rats[k].num_step - r; 977 } 978 diff = num - q * den; 979 if (best_num == 0 || 980 diff * best_den < best_diff * den) { 981 best_diff = diff; 982 best_den = den; 983 best_num = num; 984 } 985 } 986 if (best_den == 0) { 987 i->empty = 1; 988 return -EINVAL; 989 } 990 t.min = div_down(best_num, best_den); 991 t.openmin = !!(best_num % best_den); 992 993 best_num = best_den = best_diff = 0; 994 for (k = 0; k < rats_count; ++k) { 995 unsigned int num; 996 unsigned int den = rats[k].den; 997 unsigned int q = i->max; 998 int diff; 999 num = mul(q, den); 1000 if (num < rats[k].num_min) 1001 continue; 1002 if (num > rats[k].num_max) 1003 num = rats[k].num_max; 1004 else { 1005 unsigned int r; 1006 r = (num - rats[k].num_min) % rats[k].num_step; 1007 if (r != 0) 1008 num -= r; 1009 } 1010 diff = q * den - num; 1011 if (best_num == 0 || 1012 diff * best_den < best_diff * den) { 1013 best_diff = diff; 1014 best_den = den; 1015 best_num = num; 1016 } 1017 } 1018 if (best_den == 0) { 1019 i->empty = 1; 1020 return -EINVAL; 1021 } 1022 t.max = div_up(best_num, best_den); 1023 t.openmax = !!(best_num % best_den); 1024 t.integer = 0; 1025 err = snd_interval_refine(i, &t); 1026 if (err < 0) 1027 return err; 1028 1029 if (snd_interval_single(i)) { 1030 if (nump) 1031 *nump = best_num; 1032 if (denp) 1033 *denp = best_den; 1034 } 1035 return err; 1036 } 1037 1038 /** 1039 * snd_interval_list - refine the interval value from the list 1040 * @i: the interval value to refine 1041 * @count: the number of elements in the list 1042 * @list: the value list 1043 * @mask: the bit-mask to evaluate 1044 * 1045 * Refines the interval value from the list. 1046 * When mask is non-zero, only the elements corresponding to bit 1 are 1047 * evaluated. 1048 * 1049 * Return: Positive if the value is changed, zero if it's not changed, or a 1050 * negative error code. 1051 */ 1052 int snd_interval_list(struct snd_interval *i, unsigned int count, 1053 const unsigned int *list, unsigned int mask) 1054 { 1055 unsigned int k; 1056 struct snd_interval list_range; 1057 1058 if (!count) { 1059 i->empty = 1; 1060 return -EINVAL; 1061 } 1062 snd_interval_any(&list_range); 1063 list_range.min = UINT_MAX; 1064 list_range.max = 0; 1065 for (k = 0; k < count; k++) { 1066 if (mask && !(mask & (1 << k))) 1067 continue; 1068 if (!snd_interval_test(i, list[k])) 1069 continue; 1070 list_range.min = min(list_range.min, list[k]); 1071 list_range.max = max(list_range.max, list[k]); 1072 } 1073 return snd_interval_refine(i, &list_range); 1074 } 1075 EXPORT_SYMBOL(snd_interval_list); 1076 1077 /** 1078 * snd_interval_ranges - refine the interval value from the list of ranges 1079 * @i: the interval value to refine 1080 * @count: the number of elements in the list of ranges 1081 * @ranges: the ranges list 1082 * @mask: the bit-mask to evaluate 1083 * 1084 * Refines the interval value from the list of ranges. 1085 * When mask is non-zero, only the elements corresponding to bit 1 are 1086 * evaluated. 1087 * 1088 * Return: Positive if the value is changed, zero if it's not changed, or a 1089 * negative error code. 1090 */ 1091 int snd_interval_ranges(struct snd_interval *i, unsigned int count, 1092 const struct snd_interval *ranges, unsigned int mask) 1093 { 1094 unsigned int k; 1095 struct snd_interval range_union; 1096 struct snd_interval range; 1097 1098 if (!count) { 1099 snd_interval_none(i); 1100 return -EINVAL; 1101 } 1102 snd_interval_any(&range_union); 1103 range_union.min = UINT_MAX; 1104 range_union.max = 0; 1105 for (k = 0; k < count; k++) { 1106 if (mask && !(mask & (1 << k))) 1107 continue; 1108 snd_interval_copy(&range, &ranges[k]); 1109 if (snd_interval_refine(&range, i) < 0) 1110 continue; 1111 if (snd_interval_empty(&range)) 1112 continue; 1113 1114 if (range.min < range_union.min) { 1115 range_union.min = range.min; 1116 range_union.openmin = 1; 1117 } 1118 if (range.min == range_union.min && !range.openmin) 1119 range_union.openmin = 0; 1120 if (range.max > range_union.max) { 1121 range_union.max = range.max; 1122 range_union.openmax = 1; 1123 } 1124 if (range.max == range_union.max && !range.openmax) 1125 range_union.openmax = 0; 1126 } 1127 return snd_interval_refine(i, &range_union); 1128 } 1129 EXPORT_SYMBOL(snd_interval_ranges); 1130 1131 static int snd_interval_step(struct snd_interval *i, unsigned int step) 1132 { 1133 unsigned int n; 1134 int changed = 0; 1135 n = i->min % step; 1136 if (n != 0 || i->openmin) { 1137 i->min += step - n; 1138 i->openmin = 0; 1139 changed = 1; 1140 } 1141 n = i->max % step; 1142 if (n != 0 || i->openmax) { 1143 i->max -= n; 1144 i->openmax = 0; 1145 changed = 1; 1146 } 1147 if (snd_interval_checkempty(i)) { 1148 i->empty = 1; 1149 return -EINVAL; 1150 } 1151 return changed; 1152 } 1153 1154 /* Info constraints helpers */ 1155 1156 /** 1157 * snd_pcm_hw_rule_add - add the hw-constraint rule 1158 * @runtime: the pcm runtime instance 1159 * @cond: condition bits 1160 * @var: the variable to evaluate 1161 * @func: the evaluation function 1162 * @private: the private data pointer passed to function 1163 * @dep: the dependent variables 1164 * 1165 * Return: Zero if successful, or a negative error code on failure. 1166 */ 1167 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond, 1168 int var, 1169 snd_pcm_hw_rule_func_t func, void *private, 1170 int dep, ...) 1171 { 1172 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; 1173 struct snd_pcm_hw_rule *c; 1174 unsigned int k; 1175 va_list args; 1176 va_start(args, dep); 1177 if (constrs->rules_num >= constrs->rules_all) { 1178 struct snd_pcm_hw_rule *new; 1179 unsigned int new_rules = constrs->rules_all + 16; 1180 new = krealloc_array(constrs->rules, new_rules, 1181 sizeof(*c), GFP_KERNEL); 1182 if (!new) { 1183 va_end(args); 1184 return -ENOMEM; 1185 } 1186 constrs->rules = new; 1187 constrs->rules_all = new_rules; 1188 } 1189 c = &constrs->rules[constrs->rules_num]; 1190 c->cond = cond; 1191 c->func = func; 1192 c->var = var; 1193 c->private = private; 1194 k = 0; 1195 while (1) { 1196 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps))) { 1197 va_end(args); 1198 return -EINVAL; 1199 } 1200 c->deps[k++] = dep; 1201 if (dep < 0) 1202 break; 1203 dep = va_arg(args, int); 1204 } 1205 constrs->rules_num++; 1206 va_end(args); 1207 return 0; 1208 } 1209 EXPORT_SYMBOL(snd_pcm_hw_rule_add); 1210 1211 /** 1212 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint 1213 * @runtime: PCM runtime instance 1214 * @var: hw_params variable to apply the mask 1215 * @mask: the bitmap mask 1216 * 1217 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter. 1218 * 1219 * Return: Zero if successful, or a negative error code on failure. 1220 */ 1221 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var, 1222 u_int32_t mask) 1223 { 1224 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; 1225 struct snd_mask *maskp = constrs_mask(constrs, var); 1226 *maskp->bits &= mask; 1227 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */ 1228 if (*maskp->bits == 0) 1229 return -EINVAL; 1230 return 0; 1231 } 1232 1233 /** 1234 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint 1235 * @runtime: PCM runtime instance 1236 * @var: hw_params variable to apply the mask 1237 * @mask: the 64bit bitmap mask 1238 * 1239 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter. 1240 * 1241 * Return: Zero if successful, or a negative error code on failure. 1242 */ 1243 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var, 1244 u_int64_t mask) 1245 { 1246 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; 1247 struct snd_mask *maskp = constrs_mask(constrs, var); 1248 maskp->bits[0] &= (u_int32_t)mask; 1249 maskp->bits[1] &= (u_int32_t)(mask >> 32); 1250 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */ 1251 if (! maskp->bits[0] && ! maskp->bits[1]) 1252 return -EINVAL; 1253 return 0; 1254 } 1255 EXPORT_SYMBOL(snd_pcm_hw_constraint_mask64); 1256 1257 /** 1258 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval 1259 * @runtime: PCM runtime instance 1260 * @var: hw_params variable to apply the integer constraint 1261 * 1262 * Apply the constraint of integer to an interval parameter. 1263 * 1264 * Return: Positive if the value is changed, zero if it's not changed, or a 1265 * negative error code. 1266 */ 1267 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var) 1268 { 1269 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; 1270 return snd_interval_setinteger(constrs_interval(constrs, var)); 1271 } 1272 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer); 1273 1274 /** 1275 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval 1276 * @runtime: PCM runtime instance 1277 * @var: hw_params variable to apply the range 1278 * @min: the minimal value 1279 * @max: the maximal value 1280 * 1281 * Apply the min/max range constraint to an interval parameter. 1282 * 1283 * Return: Positive if the value is changed, zero if it's not changed, or a 1284 * negative error code. 1285 */ 1286 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var, 1287 unsigned int min, unsigned int max) 1288 { 1289 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints; 1290 struct snd_interval t; 1291 t.min = min; 1292 t.max = max; 1293 t.openmin = t.openmax = 0; 1294 t.integer = 0; 1295 return snd_interval_refine(constrs_interval(constrs, var), &t); 1296 } 1297 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax); 1298 1299 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params, 1300 struct snd_pcm_hw_rule *rule) 1301 { 1302 struct snd_pcm_hw_constraint_list *list = rule->private; 1303 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask); 1304 } 1305 1306 1307 /** 1308 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter 1309 * @runtime: PCM runtime instance 1310 * @cond: condition bits 1311 * @var: hw_params variable to apply the list constraint 1312 * @l: list 1313 * 1314 * Apply the list of constraints to an interval parameter. 1315 * 1316 * Return: Zero if successful, or a negative error code on failure. 1317 */ 1318 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime, 1319 unsigned int cond, 1320 snd_pcm_hw_param_t var, 1321 const struct snd_pcm_hw_constraint_list *l) 1322 { 1323 return snd_pcm_hw_rule_add(runtime, cond, var, 1324 snd_pcm_hw_rule_list, (void *)l, 1325 var, -1); 1326 } 1327 EXPORT_SYMBOL(snd_pcm_hw_constraint_list); 1328 1329 static int snd_pcm_hw_rule_ranges(struct snd_pcm_hw_params *params, 1330 struct snd_pcm_hw_rule *rule) 1331 { 1332 struct snd_pcm_hw_constraint_ranges *r = rule->private; 1333 return snd_interval_ranges(hw_param_interval(params, rule->var), 1334 r->count, r->ranges, r->mask); 1335 } 1336 1337 1338 /** 1339 * snd_pcm_hw_constraint_ranges - apply list of range constraints to a parameter 1340 * @runtime: PCM runtime instance 1341 * @cond: condition bits 1342 * @var: hw_params variable to apply the list of range constraints 1343 * @r: ranges 1344 * 1345 * Apply the list of range constraints to an interval parameter. 1346 * 1347 * Return: Zero if successful, or a negative error code on failure. 1348 */ 1349 int snd_pcm_hw_constraint_ranges(struct snd_pcm_runtime *runtime, 1350 unsigned int cond, 1351 snd_pcm_hw_param_t var, 1352 const struct snd_pcm_hw_constraint_ranges *r) 1353 { 1354 return snd_pcm_hw_rule_add(runtime, cond, var, 1355 snd_pcm_hw_rule_ranges, (void *)r, 1356 var, -1); 1357 } 1358 EXPORT_SYMBOL(snd_pcm_hw_constraint_ranges); 1359 1360 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params, 1361 struct snd_pcm_hw_rule *rule) 1362 { 1363 const struct snd_pcm_hw_constraint_ratnums *r = rule->private; 1364 unsigned int num = 0, den = 0; 1365 int err; 1366 err = snd_interval_ratnum(hw_param_interval(params, rule->var), 1367 r->nrats, r->rats, &num, &den); 1368 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) { 1369 params->rate_num = num; 1370 params->rate_den = den; 1371 } 1372 return err; 1373 } 1374 1375 /** 1376 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter 1377 * @runtime: PCM runtime instance 1378 * @cond: condition bits 1379 * @var: hw_params variable to apply the ratnums constraint 1380 * @r: struct snd_ratnums constriants 1381 * 1382 * Return: Zero if successful, or a negative error code on failure. 1383 */ 1384 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime, 1385 unsigned int cond, 1386 snd_pcm_hw_param_t var, 1387 const struct snd_pcm_hw_constraint_ratnums *r) 1388 { 1389 return snd_pcm_hw_rule_add(runtime, cond, var, 1390 snd_pcm_hw_rule_ratnums, (void *)r, 1391 var, -1); 1392 } 1393 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums); 1394 1395 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params, 1396 struct snd_pcm_hw_rule *rule) 1397 { 1398 const struct snd_pcm_hw_constraint_ratdens *r = rule->private; 1399 unsigned int num = 0, den = 0; 1400 int err = snd_interval_ratden(hw_param_interval(params, rule->var), 1401 r->nrats, r->rats, &num, &den); 1402 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) { 1403 params->rate_num = num; 1404 params->rate_den = den; 1405 } 1406 return err; 1407 } 1408 1409 /** 1410 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter 1411 * @runtime: PCM runtime instance 1412 * @cond: condition bits 1413 * @var: hw_params variable to apply the ratdens constraint 1414 * @r: struct snd_ratdens constriants 1415 * 1416 * Return: Zero if successful, or a negative error code on failure. 1417 */ 1418 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime, 1419 unsigned int cond, 1420 snd_pcm_hw_param_t var, 1421 const struct snd_pcm_hw_constraint_ratdens *r) 1422 { 1423 return snd_pcm_hw_rule_add(runtime, cond, var, 1424 snd_pcm_hw_rule_ratdens, (void *)r, 1425 var, -1); 1426 } 1427 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens); 1428 1429 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params, 1430 struct snd_pcm_hw_rule *rule) 1431 { 1432 unsigned int l = (unsigned long) rule->private; 1433 int width = l & 0xffff; 1434 unsigned int msbits = l >> 16; 1435 const struct snd_interval *i = 1436 hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS); 1437 1438 if (!snd_interval_single(i)) 1439 return 0; 1440 1441 if ((snd_interval_value(i) == width) || 1442 (width == 0 && snd_interval_value(i) > msbits)) 1443 params->msbits = min_not_zero(params->msbits, msbits); 1444 1445 return 0; 1446 } 1447 1448 /** 1449 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule 1450 * @runtime: PCM runtime instance 1451 * @cond: condition bits 1452 * @width: sample bits width 1453 * @msbits: msbits width 1454 * 1455 * This constraint will set the number of most significant bits (msbits) if a 1456 * sample format with the specified width has been select. If width is set to 0 1457 * the msbits will be set for any sample format with a width larger than the 1458 * specified msbits. 1459 * 1460 * Return: Zero if successful, or a negative error code on failure. 1461 */ 1462 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime, 1463 unsigned int cond, 1464 unsigned int width, 1465 unsigned int msbits) 1466 { 1467 unsigned long l = (msbits << 16) | width; 1468 return snd_pcm_hw_rule_add(runtime, cond, -1, 1469 snd_pcm_hw_rule_msbits, 1470 (void*) l, 1471 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1); 1472 } 1473 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits); 1474 1475 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params, 1476 struct snd_pcm_hw_rule *rule) 1477 { 1478 unsigned long step = (unsigned long) rule->private; 1479 return snd_interval_step(hw_param_interval(params, rule->var), step); 1480 } 1481 1482 /** 1483 * snd_pcm_hw_constraint_step - add a hw constraint step rule 1484 * @runtime: PCM runtime instance 1485 * @cond: condition bits 1486 * @var: hw_params variable to apply the step constraint 1487 * @step: step size 1488 * 1489 * Return: Zero if successful, or a negative error code on failure. 1490 */ 1491 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime, 1492 unsigned int cond, 1493 snd_pcm_hw_param_t var, 1494 unsigned long step) 1495 { 1496 return snd_pcm_hw_rule_add(runtime, cond, var, 1497 snd_pcm_hw_rule_step, (void *) step, 1498 var, -1); 1499 } 1500 EXPORT_SYMBOL(snd_pcm_hw_constraint_step); 1501 1502 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule) 1503 { 1504 static const unsigned int pow2_sizes[] = { 1505 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7, 1506 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15, 1507 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23, 1508 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30 1509 }; 1510 return snd_interval_list(hw_param_interval(params, rule->var), 1511 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0); 1512 } 1513 1514 /** 1515 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule 1516 * @runtime: PCM runtime instance 1517 * @cond: condition bits 1518 * @var: hw_params variable to apply the power-of-2 constraint 1519 * 1520 * Return: Zero if successful, or a negative error code on failure. 1521 */ 1522 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime, 1523 unsigned int cond, 1524 snd_pcm_hw_param_t var) 1525 { 1526 return snd_pcm_hw_rule_add(runtime, cond, var, 1527 snd_pcm_hw_rule_pow2, NULL, 1528 var, -1); 1529 } 1530 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2); 1531 1532 static int snd_pcm_hw_rule_noresample_func(struct snd_pcm_hw_params *params, 1533 struct snd_pcm_hw_rule *rule) 1534 { 1535 unsigned int base_rate = (unsigned int)(uintptr_t)rule->private; 1536 struct snd_interval *rate; 1537 1538 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); 1539 return snd_interval_list(rate, 1, &base_rate, 0); 1540 } 1541 1542 /** 1543 * snd_pcm_hw_rule_noresample - add a rule to allow disabling hw resampling 1544 * @runtime: PCM runtime instance 1545 * @base_rate: the rate at which the hardware does not resample 1546 * 1547 * Return: Zero if successful, or a negative error code on failure. 1548 */ 1549 int snd_pcm_hw_rule_noresample(struct snd_pcm_runtime *runtime, 1550 unsigned int base_rate) 1551 { 1552 return snd_pcm_hw_rule_add(runtime, SNDRV_PCM_HW_PARAMS_NORESAMPLE, 1553 SNDRV_PCM_HW_PARAM_RATE, 1554 snd_pcm_hw_rule_noresample_func, 1555 (void *)(uintptr_t)base_rate, 1556 SNDRV_PCM_HW_PARAM_RATE, -1); 1557 } 1558 EXPORT_SYMBOL(snd_pcm_hw_rule_noresample); 1559 1560 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params, 1561 snd_pcm_hw_param_t var) 1562 { 1563 if (hw_is_mask(var)) { 1564 snd_mask_any(hw_param_mask(params, var)); 1565 params->cmask |= 1 << var; 1566 params->rmask |= 1 << var; 1567 return; 1568 } 1569 if (hw_is_interval(var)) { 1570 snd_interval_any(hw_param_interval(params, var)); 1571 params->cmask |= 1 << var; 1572 params->rmask |= 1 << var; 1573 return; 1574 } 1575 snd_BUG(); 1576 } 1577 1578 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params) 1579 { 1580 unsigned int k; 1581 memset(params, 0, sizeof(*params)); 1582 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) 1583 _snd_pcm_hw_param_any(params, k); 1584 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) 1585 _snd_pcm_hw_param_any(params, k); 1586 params->info = ~0U; 1587 } 1588 EXPORT_SYMBOL(_snd_pcm_hw_params_any); 1589 1590 /** 1591 * snd_pcm_hw_param_value - return @params field @var value 1592 * @params: the hw_params instance 1593 * @var: parameter to retrieve 1594 * @dir: pointer to the direction (-1,0,1) or %NULL 1595 * 1596 * Return: The value for field @var if it's fixed in configuration space 1597 * defined by @params. -%EINVAL otherwise. 1598 */ 1599 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params, 1600 snd_pcm_hw_param_t var, int *dir) 1601 { 1602 if (hw_is_mask(var)) { 1603 const struct snd_mask *mask = hw_param_mask_c(params, var); 1604 if (!snd_mask_single(mask)) 1605 return -EINVAL; 1606 if (dir) 1607 *dir = 0; 1608 return snd_mask_value(mask); 1609 } 1610 if (hw_is_interval(var)) { 1611 const struct snd_interval *i = hw_param_interval_c(params, var); 1612 if (!snd_interval_single(i)) 1613 return -EINVAL; 1614 if (dir) 1615 *dir = i->openmin; 1616 return snd_interval_value(i); 1617 } 1618 return -EINVAL; 1619 } 1620 EXPORT_SYMBOL(snd_pcm_hw_param_value); 1621 1622 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params, 1623 snd_pcm_hw_param_t var) 1624 { 1625 if (hw_is_mask(var)) { 1626 snd_mask_none(hw_param_mask(params, var)); 1627 params->cmask |= 1 << var; 1628 params->rmask |= 1 << var; 1629 } else if (hw_is_interval(var)) { 1630 snd_interval_none(hw_param_interval(params, var)); 1631 params->cmask |= 1 << var; 1632 params->rmask |= 1 << var; 1633 } else { 1634 snd_BUG(); 1635 } 1636 } 1637 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty); 1638 1639 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params, 1640 snd_pcm_hw_param_t var) 1641 { 1642 int changed; 1643 if (hw_is_mask(var)) 1644 changed = snd_mask_refine_first(hw_param_mask(params, var)); 1645 else if (hw_is_interval(var)) 1646 changed = snd_interval_refine_first(hw_param_interval(params, var)); 1647 else 1648 return -EINVAL; 1649 if (changed > 0) { 1650 params->cmask |= 1 << var; 1651 params->rmask |= 1 << var; 1652 } 1653 return changed; 1654 } 1655 1656 1657 /** 1658 * snd_pcm_hw_param_first - refine config space and return minimum value 1659 * @pcm: PCM instance 1660 * @params: the hw_params instance 1661 * @var: parameter to retrieve 1662 * @dir: pointer to the direction (-1,0,1) or %NULL 1663 * 1664 * Inside configuration space defined by @params remove from @var all 1665 * values > minimum. Reduce configuration space accordingly. 1666 * 1667 * Return: The minimum, or a negative error code on failure. 1668 */ 1669 int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm, 1670 struct snd_pcm_hw_params *params, 1671 snd_pcm_hw_param_t var, int *dir) 1672 { 1673 int changed = _snd_pcm_hw_param_first(params, var); 1674 if (changed < 0) 1675 return changed; 1676 if (params->rmask) { 1677 int err = snd_pcm_hw_refine(pcm, params); 1678 if (err < 0) 1679 return err; 1680 } 1681 return snd_pcm_hw_param_value(params, var, dir); 1682 } 1683 EXPORT_SYMBOL(snd_pcm_hw_param_first); 1684 1685 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params, 1686 snd_pcm_hw_param_t var) 1687 { 1688 int changed; 1689 if (hw_is_mask(var)) 1690 changed = snd_mask_refine_last(hw_param_mask(params, var)); 1691 else if (hw_is_interval(var)) 1692 changed = snd_interval_refine_last(hw_param_interval(params, var)); 1693 else 1694 return -EINVAL; 1695 if (changed > 0) { 1696 params->cmask |= 1 << var; 1697 params->rmask |= 1 << var; 1698 } 1699 return changed; 1700 } 1701 1702 1703 /** 1704 * snd_pcm_hw_param_last - refine config space and return maximum value 1705 * @pcm: PCM instance 1706 * @params: the hw_params instance 1707 * @var: parameter to retrieve 1708 * @dir: pointer to the direction (-1,0,1) or %NULL 1709 * 1710 * Inside configuration space defined by @params remove from @var all 1711 * values < maximum. Reduce configuration space accordingly. 1712 * 1713 * Return: The maximum, or a negative error code on failure. 1714 */ 1715 int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm, 1716 struct snd_pcm_hw_params *params, 1717 snd_pcm_hw_param_t var, int *dir) 1718 { 1719 int changed = _snd_pcm_hw_param_last(params, var); 1720 if (changed < 0) 1721 return changed; 1722 if (params->rmask) { 1723 int err = snd_pcm_hw_refine(pcm, params); 1724 if (err < 0) 1725 return err; 1726 } 1727 return snd_pcm_hw_param_value(params, var, dir); 1728 } 1729 EXPORT_SYMBOL(snd_pcm_hw_param_last); 1730 1731 /** 1732 * snd_pcm_hw_params_bits - Get the number of bits per the sample. 1733 * @p: hardware parameters 1734 * 1735 * Return: The number of bits per sample based on the format, 1736 * subformat and msbits the specified hw params has. 1737 */ 1738 int snd_pcm_hw_params_bits(const struct snd_pcm_hw_params *p) 1739 { 1740 snd_pcm_subformat_t subformat = params_subformat(p); 1741 snd_pcm_format_t format = params_format(p); 1742 1743 switch (format) { 1744 case SNDRV_PCM_FORMAT_S32_LE: 1745 case SNDRV_PCM_FORMAT_U32_LE: 1746 case SNDRV_PCM_FORMAT_S32_BE: 1747 case SNDRV_PCM_FORMAT_U32_BE: 1748 switch (subformat) { 1749 case SNDRV_PCM_SUBFORMAT_MSBITS_20: 1750 return 20; 1751 case SNDRV_PCM_SUBFORMAT_MSBITS_24: 1752 return 24; 1753 case SNDRV_PCM_SUBFORMAT_MSBITS_MAX: 1754 case SNDRV_PCM_SUBFORMAT_STD: 1755 default: 1756 break; 1757 } 1758 fallthrough; 1759 default: 1760 return snd_pcm_format_width(format); 1761 } 1762 } 1763 EXPORT_SYMBOL(snd_pcm_hw_params_bits); 1764 1765 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream, 1766 void *arg) 1767 { 1768 struct snd_pcm_runtime *runtime = substream->runtime; 1769 1770 guard(pcm_stream_lock_irqsave)(substream); 1771 if (snd_pcm_running(substream) && 1772 snd_pcm_update_hw_ptr(substream) >= 0) 1773 runtime->status->hw_ptr %= runtime->buffer_size; 1774 else { 1775 runtime->status->hw_ptr = 0; 1776 runtime->hw_ptr_wrap = 0; 1777 } 1778 return 0; 1779 } 1780 1781 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream, 1782 void *arg) 1783 { 1784 struct snd_pcm_channel_info *info = arg; 1785 struct snd_pcm_runtime *runtime = substream->runtime; 1786 int width; 1787 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) { 1788 info->offset = -1; 1789 return 0; 1790 } 1791 width = snd_pcm_format_physical_width(runtime->format); 1792 if (width < 0) 1793 return width; 1794 info->offset = 0; 1795 switch (runtime->access) { 1796 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED: 1797 case SNDRV_PCM_ACCESS_RW_INTERLEAVED: 1798 info->first = info->channel * width; 1799 info->step = runtime->channels * width; 1800 break; 1801 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED: 1802 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED: 1803 { 1804 size_t size = runtime->dma_bytes / runtime->channels; 1805 info->first = info->channel * size * 8; 1806 info->step = width; 1807 break; 1808 } 1809 default: 1810 snd_BUG(); 1811 break; 1812 } 1813 return 0; 1814 } 1815 1816 static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream, 1817 void *arg) 1818 { 1819 struct snd_pcm_hw_params *params = arg; 1820 snd_pcm_format_t format; 1821 int channels; 1822 ssize_t frame_size; 1823 1824 params->fifo_size = substream->runtime->hw.fifo_size; 1825 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) { 1826 format = params_format(params); 1827 channels = params_channels(params); 1828 frame_size = snd_pcm_format_size(format, channels); 1829 if (frame_size > 0) 1830 params->fifo_size /= frame_size; 1831 } 1832 return 0; 1833 } 1834 1835 static int snd_pcm_lib_ioctl_sync_id(struct snd_pcm_substream *substream, 1836 void *arg) 1837 { 1838 static const unsigned char id[12] = { 0xff, 0xff, 0xff, 0xff, 1839 0xff, 0xff, 0xff, 0xff, 1840 0xff, 0xff, 0xff, 0xff }; 1841 1842 if (substream->runtime->std_sync_id) 1843 snd_pcm_set_sync_per_card(substream, arg, id, sizeof(id)); 1844 return 0; 1845 } 1846 1847 /** 1848 * snd_pcm_lib_ioctl - a generic PCM ioctl callback 1849 * @substream: the pcm substream instance 1850 * @cmd: ioctl command 1851 * @arg: ioctl argument 1852 * 1853 * Processes the generic ioctl commands for PCM. 1854 * Can be passed as the ioctl callback for PCM ops. 1855 * 1856 * Return: Zero if successful, or a negative error code on failure. 1857 */ 1858 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream, 1859 unsigned int cmd, void *arg) 1860 { 1861 switch (cmd) { 1862 case SNDRV_PCM_IOCTL1_RESET: 1863 return snd_pcm_lib_ioctl_reset(substream, arg); 1864 case SNDRV_PCM_IOCTL1_CHANNEL_INFO: 1865 return snd_pcm_lib_ioctl_channel_info(substream, arg); 1866 case SNDRV_PCM_IOCTL1_FIFO_SIZE: 1867 return snd_pcm_lib_ioctl_fifo_size(substream, arg); 1868 case SNDRV_PCM_IOCTL1_SYNC_ID: 1869 return snd_pcm_lib_ioctl_sync_id(substream, arg); 1870 } 1871 return -ENXIO; 1872 } 1873 EXPORT_SYMBOL(snd_pcm_lib_ioctl); 1874 1875 /** 1876 * snd_pcm_period_elapsed_under_stream_lock() - update the status of runtime for the next period 1877 * under acquired lock of PCM substream. 1878 * @substream: the instance of pcm substream. 1879 * 1880 * This function is called when the batch of audio data frames as the same size as the period of 1881 * buffer is already processed in audio data transmission. 1882 * 1883 * The call of function updates the status of runtime with the latest position of audio data 1884 * transmission, checks overrun and underrun over buffer, awaken user processes from waiting for 1885 * available audio data frames, sampling audio timestamp, and performs stop or drain the PCM 1886 * substream according to configured threshold. 1887 * 1888 * The function is intended to use for the case that PCM driver operates audio data frames under 1889 * acquired lock of PCM substream; e.g. in callback of any operation of &snd_pcm_ops in process 1890 * context. In any interrupt context, it's preferrable to use ``snd_pcm_period_elapsed()`` instead 1891 * since lock of PCM substream should be acquired in advance. 1892 * 1893 * Developer should pay enough attention that some callbacks in &snd_pcm_ops are done by the call of 1894 * function: 1895 * 1896 * - .pointer - to retrieve current position of audio data transmission by frame count or XRUN state. 1897 * - .trigger - with SNDRV_PCM_TRIGGER_STOP at XRUN or DRAINING state. 1898 * - .get_time_info - to retrieve audio time stamp if needed. 1899 * 1900 * Even if more than one periods have elapsed since the last call, you have to call this only once. 1901 */ 1902 void snd_pcm_period_elapsed_under_stream_lock(struct snd_pcm_substream *substream) 1903 { 1904 struct snd_pcm_runtime *runtime; 1905 1906 if (PCM_RUNTIME_CHECK(substream)) 1907 return; 1908 runtime = substream->runtime; 1909 1910 if (!snd_pcm_running(substream) || 1911 snd_pcm_update_hw_ptr0(substream, 1) < 0) 1912 goto _end; 1913 1914 #ifdef CONFIG_SND_PCM_TIMER 1915 if (substream->timer_running) 1916 snd_timer_interrupt(substream->timer, 1); 1917 #endif 1918 _end: 1919 snd_kill_fasync(runtime->fasync, SIGIO, POLL_IN); 1920 } 1921 EXPORT_SYMBOL(snd_pcm_period_elapsed_under_stream_lock); 1922 1923 /** 1924 * snd_pcm_period_elapsed() - update the status of runtime for the next period by acquiring lock of 1925 * PCM substream. 1926 * @substream: the instance of PCM substream. 1927 * 1928 * This function is mostly similar to ``snd_pcm_period_elapsed_under_stream_lock()`` except for 1929 * acquiring lock of PCM substream voluntarily. 1930 * 1931 * It's typically called by any type of IRQ handler when hardware IRQ occurs to notify event that 1932 * the batch of audio data frames as the same size as the period of buffer is already processed in 1933 * audio data transmission. 1934 */ 1935 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream) 1936 { 1937 if (snd_BUG_ON(!substream)) 1938 return; 1939 1940 guard(pcm_stream_lock_irqsave)(substream); 1941 snd_pcm_period_elapsed_under_stream_lock(substream); 1942 } 1943 EXPORT_SYMBOL(snd_pcm_period_elapsed); 1944 1945 /* 1946 * Wait until avail_min data becomes available 1947 * Returns a negative error code if any error occurs during operation. 1948 * The available space is stored on availp. When err = 0 and avail = 0 1949 * on the capture stream, it indicates the stream is in DRAINING state. 1950 */ 1951 static int wait_for_avail(struct snd_pcm_substream *substream, 1952 snd_pcm_uframes_t *availp) 1953 { 1954 struct snd_pcm_runtime *runtime = substream->runtime; 1955 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 1956 wait_queue_entry_t wait; 1957 int err = 0; 1958 snd_pcm_uframes_t avail = 0; 1959 long wait_time, tout; 1960 1961 init_waitqueue_entry(&wait, current); 1962 set_current_state(TASK_INTERRUPTIBLE); 1963 add_wait_queue(&runtime->tsleep, &wait); 1964 1965 if (runtime->no_period_wakeup) 1966 wait_time = MAX_SCHEDULE_TIMEOUT; 1967 else { 1968 /* use wait time from substream if available */ 1969 if (substream->wait_time) { 1970 wait_time = substream->wait_time; 1971 } else { 1972 wait_time = 100; 1973 1974 if (runtime->rate) { 1975 long t = runtime->buffer_size * 1100 / runtime->rate; 1976 wait_time = max(t, wait_time); 1977 } 1978 } 1979 wait_time = msecs_to_jiffies(wait_time); 1980 } 1981 1982 for (;;) { 1983 if (signal_pending(current)) { 1984 err = -ERESTARTSYS; 1985 break; 1986 } 1987 1988 /* 1989 * We need to check if space became available already 1990 * (and thus the wakeup happened already) first to close 1991 * the race of space already having become available. 1992 * This check must happen after been added to the waitqueue 1993 * and having current state be INTERRUPTIBLE. 1994 */ 1995 avail = snd_pcm_avail(substream); 1996 if (avail >= runtime->twake) 1997 break; 1998 snd_pcm_stream_unlock_irq(substream); 1999 2000 tout = schedule_timeout(wait_time); 2001 2002 snd_pcm_stream_lock_irq(substream); 2003 set_current_state(TASK_INTERRUPTIBLE); 2004 switch (runtime->state) { 2005 case SNDRV_PCM_STATE_SUSPENDED: 2006 err = -ESTRPIPE; 2007 goto _endloop; 2008 case SNDRV_PCM_STATE_XRUN: 2009 err = -EPIPE; 2010 goto _endloop; 2011 case SNDRV_PCM_STATE_DRAINING: 2012 if (is_playback) 2013 err = -EPIPE; 2014 else 2015 avail = snd_pcm_avail(substream); 2016 goto _endloop; 2017 case SNDRV_PCM_STATE_OPEN: 2018 case SNDRV_PCM_STATE_SETUP: 2019 case SNDRV_PCM_STATE_DISCONNECTED: 2020 err = -EBADFD; 2021 goto _endloop; 2022 case SNDRV_PCM_STATE_PAUSED: 2023 continue; 2024 } 2025 if (!tout) { 2026 pcm_dbg(substream->pcm, 2027 "%s timeout (DMA or IRQ trouble?)\n", 2028 is_playback ? "playback write" : "capture read"); 2029 err = -EIO; 2030 break; 2031 } 2032 } 2033 _endloop: 2034 set_current_state(TASK_RUNNING); 2035 remove_wait_queue(&runtime->tsleep, &wait); 2036 *availp = avail; 2037 return err; 2038 } 2039 2040 typedef int (*pcm_transfer_f)(struct snd_pcm_substream *substream, 2041 int channel, unsigned long hwoff, 2042 struct iov_iter *iter, unsigned long bytes); 2043 2044 typedef int (*pcm_copy_f)(struct snd_pcm_substream *, snd_pcm_uframes_t, void *, 2045 snd_pcm_uframes_t, snd_pcm_uframes_t, pcm_transfer_f, 2046 bool); 2047 2048 /* calculate the target DMA-buffer position to be written/read */ 2049 static void *get_dma_ptr(struct snd_pcm_runtime *runtime, 2050 int channel, unsigned long hwoff) 2051 { 2052 return runtime->dma_area + hwoff + 2053 channel * (runtime->dma_bytes / runtime->channels); 2054 } 2055 2056 /* default copy ops for write; used for both interleaved and non- modes */ 2057 static int default_write_copy(struct snd_pcm_substream *substream, 2058 int channel, unsigned long hwoff, 2059 struct iov_iter *iter, unsigned long bytes) 2060 { 2061 if (copy_from_iter(get_dma_ptr(substream->runtime, channel, hwoff), 2062 bytes, iter) != bytes) 2063 return -EFAULT; 2064 return 0; 2065 } 2066 2067 /* fill silence instead of copy data; called as a transfer helper 2068 * from __snd_pcm_lib_write() or directly from noninterleaved_copy() when 2069 * a NULL buffer is passed 2070 */ 2071 static int fill_silence(struct snd_pcm_substream *substream, int channel, 2072 unsigned long hwoff, struct iov_iter *iter, 2073 unsigned long bytes) 2074 { 2075 struct snd_pcm_runtime *runtime = substream->runtime; 2076 2077 if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK) 2078 return 0; 2079 if (substream->ops->fill_silence) 2080 return substream->ops->fill_silence(substream, channel, 2081 hwoff, bytes); 2082 2083 snd_pcm_format_set_silence(runtime->format, 2084 get_dma_ptr(runtime, channel, hwoff), 2085 bytes_to_samples(runtime, bytes)); 2086 return 0; 2087 } 2088 2089 /* default copy ops for read; used for both interleaved and non- modes */ 2090 static int default_read_copy(struct snd_pcm_substream *substream, 2091 int channel, unsigned long hwoff, 2092 struct iov_iter *iter, unsigned long bytes) 2093 { 2094 if (copy_to_iter(get_dma_ptr(substream->runtime, channel, hwoff), 2095 bytes, iter) != bytes) 2096 return -EFAULT; 2097 return 0; 2098 } 2099 2100 /* call transfer with the filled iov_iter */ 2101 static int do_transfer(struct snd_pcm_substream *substream, int c, 2102 unsigned long hwoff, void *data, unsigned long bytes, 2103 pcm_transfer_f transfer, bool in_kernel) 2104 { 2105 struct iov_iter iter; 2106 int err, type; 2107 2108 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 2109 type = ITER_SOURCE; 2110 else 2111 type = ITER_DEST; 2112 2113 if (in_kernel) { 2114 struct kvec kvec = { data, bytes }; 2115 2116 iov_iter_kvec(&iter, type, &kvec, 1, bytes); 2117 return transfer(substream, c, hwoff, &iter, bytes); 2118 } 2119 2120 err = import_ubuf(type, (__force void __user *)data, bytes, &iter); 2121 if (err) 2122 return err; 2123 return transfer(substream, c, hwoff, &iter, bytes); 2124 } 2125 2126 /* call transfer function with the converted pointers and sizes; 2127 * for interleaved mode, it's one shot for all samples 2128 */ 2129 static int interleaved_copy(struct snd_pcm_substream *substream, 2130 snd_pcm_uframes_t hwoff, void *data, 2131 snd_pcm_uframes_t off, 2132 snd_pcm_uframes_t frames, 2133 pcm_transfer_f transfer, 2134 bool in_kernel) 2135 { 2136 struct snd_pcm_runtime *runtime = substream->runtime; 2137 2138 /* convert to bytes */ 2139 hwoff = frames_to_bytes(runtime, hwoff); 2140 off = frames_to_bytes(runtime, off); 2141 frames = frames_to_bytes(runtime, frames); 2142 2143 if (!data) 2144 return fill_silence(substream, 0, hwoff, NULL, frames); 2145 2146 return do_transfer(substream, 0, hwoff, data + off, frames, transfer, 2147 in_kernel); 2148 } 2149 2150 /* call transfer function with the converted pointers and sizes for each 2151 * non-interleaved channel; when buffer is NULL, silencing instead of copying 2152 */ 2153 static int noninterleaved_copy(struct snd_pcm_substream *substream, 2154 snd_pcm_uframes_t hwoff, void *data, 2155 snd_pcm_uframes_t off, 2156 snd_pcm_uframes_t frames, 2157 pcm_transfer_f transfer, 2158 bool in_kernel) 2159 { 2160 struct snd_pcm_runtime *runtime = substream->runtime; 2161 int channels = runtime->channels; 2162 void **bufs = data; 2163 int c, err; 2164 2165 /* convert to bytes; note that it's not frames_to_bytes() here. 2166 * in non-interleaved mode, we copy for each channel, thus 2167 * each copy is n_samples bytes x channels = whole frames. 2168 */ 2169 off = samples_to_bytes(runtime, off); 2170 frames = samples_to_bytes(runtime, frames); 2171 hwoff = samples_to_bytes(runtime, hwoff); 2172 for (c = 0; c < channels; ++c, ++bufs) { 2173 if (!data || !*bufs) 2174 err = fill_silence(substream, c, hwoff, NULL, frames); 2175 else 2176 err = do_transfer(substream, c, hwoff, *bufs + off, 2177 frames, transfer, in_kernel); 2178 if (err < 0) 2179 return err; 2180 } 2181 return 0; 2182 } 2183 2184 /* fill silence on the given buffer position; 2185 * called from snd_pcm_playback_silence() 2186 */ 2187 static int fill_silence_frames(struct snd_pcm_substream *substream, 2188 snd_pcm_uframes_t off, snd_pcm_uframes_t frames) 2189 { 2190 if (substream->runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED || 2191 substream->runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) 2192 return interleaved_copy(substream, off, NULL, 0, frames, 2193 fill_silence, true); 2194 else 2195 return noninterleaved_copy(substream, off, NULL, 0, frames, 2196 fill_silence, true); 2197 } 2198 2199 /* sanity-check for read/write methods */ 2200 static int pcm_sanity_check(struct snd_pcm_substream *substream) 2201 { 2202 struct snd_pcm_runtime *runtime; 2203 if (PCM_RUNTIME_CHECK(substream)) 2204 return -ENXIO; 2205 runtime = substream->runtime; 2206 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area)) 2207 return -EINVAL; 2208 if (runtime->state == SNDRV_PCM_STATE_OPEN) 2209 return -EBADFD; 2210 return 0; 2211 } 2212 2213 static int pcm_accessible_state(struct snd_pcm_substream *substream) 2214 { 2215 struct snd_pcm_runtime *runtime = substream->runtime; 2216 2217 switch (runtime->state) { 2218 case SNDRV_PCM_STATE_PREPARED: 2219 case SNDRV_PCM_STATE_RUNNING: 2220 case SNDRV_PCM_STATE_PAUSED: 2221 return 0; 2222 case SNDRV_PCM_STATE_DRAINING: 2223 return substream->stream == SNDRV_PCM_STREAM_CAPTURE ? 0 : -EBADFD; 2224 case SNDRV_PCM_STATE_XRUN: 2225 return -EPIPE; 2226 case SNDRV_PCM_STATE_SUSPENDED: 2227 return -ESTRPIPE; 2228 default: 2229 return -EBADFD; 2230 } 2231 } 2232 2233 /* update to the given appl_ptr and call ack callback if needed; 2234 * when an error is returned, take back to the original value 2235 */ 2236 int pcm_lib_apply_appl_ptr(struct snd_pcm_substream *substream, 2237 snd_pcm_uframes_t appl_ptr) 2238 { 2239 struct snd_pcm_runtime *runtime = substream->runtime; 2240 snd_pcm_uframes_t old_appl_ptr = runtime->control->appl_ptr; 2241 snd_pcm_sframes_t diff; 2242 int ret; 2243 2244 if (old_appl_ptr == appl_ptr) 2245 return 0; 2246 2247 if (appl_ptr >= runtime->boundary) 2248 return -EINVAL; 2249 /* 2250 * check if a rewind is requested by the application 2251 */ 2252 if (substream->runtime->info & SNDRV_PCM_INFO_NO_REWINDS) { 2253 diff = appl_ptr - old_appl_ptr; 2254 if (diff >= 0) { 2255 if (diff > runtime->buffer_size) 2256 return -EINVAL; 2257 } else { 2258 if (runtime->boundary + diff > runtime->buffer_size) 2259 return -EINVAL; 2260 } 2261 } 2262 2263 runtime->control->appl_ptr = appl_ptr; 2264 if (substream->ops->ack) { 2265 ret = substream->ops->ack(substream); 2266 if (ret < 0) { 2267 runtime->control->appl_ptr = old_appl_ptr; 2268 if (ret == -EPIPE) 2269 __snd_pcm_xrun(substream); 2270 return ret; 2271 } 2272 } 2273 2274 trace_applptr(substream, old_appl_ptr, appl_ptr); 2275 2276 return 0; 2277 } 2278 2279 /* the common loop for read/write data */ 2280 snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream, 2281 void *data, bool interleaved, 2282 snd_pcm_uframes_t size, bool in_kernel) 2283 { 2284 struct snd_pcm_runtime *runtime = substream->runtime; 2285 snd_pcm_uframes_t xfer = 0; 2286 snd_pcm_uframes_t offset = 0; 2287 snd_pcm_uframes_t avail; 2288 pcm_copy_f writer; 2289 pcm_transfer_f transfer; 2290 bool nonblock; 2291 bool is_playback; 2292 int err; 2293 2294 err = pcm_sanity_check(substream); 2295 if (err < 0) 2296 return err; 2297 2298 is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 2299 if (interleaved) { 2300 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED && 2301 runtime->channels > 1) 2302 return -EINVAL; 2303 writer = interleaved_copy; 2304 } else { 2305 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) 2306 return -EINVAL; 2307 writer = noninterleaved_copy; 2308 } 2309 2310 if (!data) { 2311 if (is_playback) 2312 transfer = fill_silence; 2313 else 2314 return -EINVAL; 2315 } else { 2316 if (substream->ops->copy) 2317 transfer = substream->ops->copy; 2318 else 2319 transfer = is_playback ? 2320 default_write_copy : default_read_copy; 2321 } 2322 2323 if (size == 0) 2324 return 0; 2325 2326 nonblock = !!(substream->f_flags & O_NONBLOCK); 2327 2328 snd_pcm_stream_lock_irq(substream); 2329 err = pcm_accessible_state(substream); 2330 if (err < 0) 2331 goto _end_unlock; 2332 2333 runtime->twake = runtime->control->avail_min ? : 1; 2334 if (runtime->state == SNDRV_PCM_STATE_RUNNING) 2335 snd_pcm_update_hw_ptr(substream); 2336 2337 /* 2338 * If size < start_threshold, wait indefinitely. Another 2339 * thread may start capture 2340 */ 2341 if (!is_playback && 2342 runtime->state == SNDRV_PCM_STATE_PREPARED && 2343 size >= runtime->start_threshold) { 2344 err = snd_pcm_start(substream); 2345 if (err < 0) 2346 goto _end_unlock; 2347 } 2348 2349 avail = snd_pcm_avail(substream); 2350 2351 while (size > 0) { 2352 snd_pcm_uframes_t frames, appl_ptr, appl_ofs; 2353 snd_pcm_uframes_t cont; 2354 if (!avail) { 2355 if (!is_playback && 2356 runtime->state == SNDRV_PCM_STATE_DRAINING) { 2357 snd_pcm_drain_done(substream); 2358 goto _end_unlock; 2359 } 2360 if (nonblock) { 2361 err = -EAGAIN; 2362 goto _end_unlock; 2363 } 2364 runtime->twake = min_t(snd_pcm_uframes_t, size, 2365 runtime->control->avail_min ? : 1); 2366 err = wait_for_avail(substream, &avail); 2367 if (err < 0) 2368 goto _end_unlock; 2369 if (!avail) 2370 continue; /* draining */ 2371 } 2372 frames = size > avail ? avail : size; 2373 appl_ptr = READ_ONCE(runtime->control->appl_ptr); 2374 appl_ofs = appl_ptr % runtime->buffer_size; 2375 cont = runtime->buffer_size - appl_ofs; 2376 if (frames > cont) 2377 frames = cont; 2378 if (snd_BUG_ON(!frames)) { 2379 err = -EINVAL; 2380 goto _end_unlock; 2381 } 2382 err = snd_pcm_buffer_access_get(runtime); 2383 if (err < 0) 2384 goto _end_unlock; 2385 snd_pcm_stream_unlock_irq(substream); 2386 if (!is_playback) 2387 snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_CPU); 2388 err = writer(substream, appl_ofs, data, offset, frames, 2389 transfer, in_kernel); 2390 if (is_playback) 2391 snd_pcm_dma_buffer_sync(substream, SNDRV_DMA_SYNC_DEVICE); 2392 snd_pcm_stream_lock_irq(substream); 2393 snd_pcm_buffer_access_put(runtime); 2394 if (err < 0) 2395 goto _end_unlock; 2396 err = pcm_accessible_state(substream); 2397 if (err < 0) 2398 goto _end_unlock; 2399 appl_ptr += frames; 2400 if (appl_ptr >= runtime->boundary) 2401 appl_ptr -= runtime->boundary; 2402 err = pcm_lib_apply_appl_ptr(substream, appl_ptr); 2403 if (err < 0) 2404 goto _end_unlock; 2405 2406 offset += frames; 2407 size -= frames; 2408 xfer += frames; 2409 avail -= frames; 2410 if (is_playback && 2411 runtime->state == SNDRV_PCM_STATE_PREPARED && 2412 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) { 2413 err = snd_pcm_start(substream); 2414 if (err < 0) 2415 goto _end_unlock; 2416 } 2417 } 2418 _end_unlock: 2419 runtime->twake = 0; 2420 if (xfer > 0 && err >= 0) 2421 snd_pcm_update_state(substream, runtime); 2422 snd_pcm_stream_unlock_irq(substream); 2423 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err; 2424 } 2425 EXPORT_SYMBOL(__snd_pcm_lib_xfer); 2426 2427 /* 2428 * standard channel mapping helpers 2429 */ 2430 2431 /* default channel maps for multi-channel playbacks, up to 8 channels */ 2432 const struct snd_pcm_chmap_elem snd_pcm_std_chmaps[] = { 2433 { .channels = 1, 2434 .map = { SNDRV_CHMAP_MONO } }, 2435 { .channels = 2, 2436 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } }, 2437 { .channels = 4, 2438 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, 2439 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } }, 2440 { .channels = 6, 2441 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, 2442 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR, 2443 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } }, 2444 { .channels = 8, 2445 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, 2446 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR, 2447 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE, 2448 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } }, 2449 { } 2450 }; 2451 EXPORT_SYMBOL_GPL(snd_pcm_std_chmaps); 2452 2453 /* alternative channel maps with CLFE <-> surround swapped for 6/8 channels */ 2454 const struct snd_pcm_chmap_elem snd_pcm_alt_chmaps[] = { 2455 { .channels = 1, 2456 .map = { SNDRV_CHMAP_MONO } }, 2457 { .channels = 2, 2458 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } }, 2459 { .channels = 4, 2460 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, 2461 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } }, 2462 { .channels = 6, 2463 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, 2464 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE, 2465 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } }, 2466 { .channels = 8, 2467 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, 2468 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE, 2469 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR, 2470 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } }, 2471 { } 2472 }; 2473 EXPORT_SYMBOL_GPL(snd_pcm_alt_chmaps); 2474 2475 static bool valid_chmap_channels(const struct snd_pcm_chmap *info, int ch) 2476 { 2477 if (ch > info->max_channels) 2478 return false; 2479 return !info->channel_mask || (info->channel_mask & (1U << ch)); 2480 } 2481 2482 static int pcm_chmap_ctl_info(struct snd_kcontrol *kcontrol, 2483 struct snd_ctl_elem_info *uinfo) 2484 { 2485 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol); 2486 2487 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2488 uinfo->count = info->max_channels; 2489 uinfo->value.integer.min = 0; 2490 uinfo->value.integer.max = SNDRV_CHMAP_LAST; 2491 return 0; 2492 } 2493 2494 /* get callback for channel map ctl element 2495 * stores the channel position firstly matching with the current channels 2496 */ 2497 static int pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol, 2498 struct snd_ctl_elem_value *ucontrol) 2499 { 2500 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol); 2501 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); 2502 struct snd_pcm_substream *substream; 2503 const struct snd_pcm_chmap_elem *map; 2504 unsigned int channels; 2505 2506 if (!info->chmap) 2507 return -EINVAL; 2508 substream = snd_pcm_chmap_substream(info, idx); 2509 if (!substream) 2510 return -ENODEV; 2511 memset(ucontrol->value.integer.value, 0, 2512 sizeof(long) * info->max_channels); 2513 /* 2514 * PCM disconnect takes open_mutex before removing controls. 2515 * Use trylock to avoid a deadlock with controls_rwsem. 2516 */ 2517 scoped_cond_guard(mutex_try, return -EBUSY, 2518 &info->pcm->open_mutex) { 2519 if (!substream->runtime) 2520 return 0; /* no channels set */ 2521 channels = substream->runtime->channels; 2522 } 2523 for (map = info->chmap; map->channels; map++) { 2524 int i; 2525 2526 if (map->channels == channels && 2527 valid_chmap_channels(info, map->channels)) { 2528 for (i = 0; i < map->channels; i++) 2529 ucontrol->value.integer.value[i] = map->map[i]; 2530 return 0; 2531 } 2532 } 2533 return -EINVAL; 2534 } 2535 2536 /* tlv callback for channel map ctl element 2537 * expands the pre-defined channel maps in a form of TLV 2538 */ 2539 static int pcm_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag, 2540 unsigned int size, unsigned int __user *tlv) 2541 { 2542 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol); 2543 const struct snd_pcm_chmap_elem *map; 2544 unsigned int __user *dst; 2545 int c, count = 0; 2546 2547 if (!info->chmap) 2548 return -EINVAL; 2549 if (size < 8) 2550 return -ENOMEM; 2551 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv)) 2552 return -EFAULT; 2553 size -= 8; 2554 dst = tlv + 2; 2555 for (map = info->chmap; map->channels; map++) { 2556 int chs_bytes = map->channels * 4; 2557 if (!valid_chmap_channels(info, map->channels)) 2558 continue; 2559 if (size < 8) 2560 return -ENOMEM; 2561 if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) || 2562 put_user(chs_bytes, dst + 1)) 2563 return -EFAULT; 2564 dst += 2; 2565 size -= 8; 2566 count += 8; 2567 if (size < chs_bytes) 2568 return -ENOMEM; 2569 size -= chs_bytes; 2570 count += chs_bytes; 2571 for (c = 0; c < map->channels; c++) { 2572 if (put_user(map->map[c], dst)) 2573 return -EFAULT; 2574 dst++; 2575 } 2576 } 2577 if (put_user(count, tlv + 1)) 2578 return -EFAULT; 2579 return 0; 2580 } 2581 2582 static void pcm_chmap_ctl_private_free(struct snd_kcontrol *kcontrol) 2583 { 2584 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol); 2585 info->pcm->streams[info->stream].chmap_kctl = NULL; 2586 kfree(info); 2587 } 2588 2589 /** 2590 * snd_pcm_add_chmap_ctls - create channel-mapping control elements 2591 * @pcm: the assigned PCM instance 2592 * @stream: stream direction 2593 * @chmap: channel map elements (for query) 2594 * @max_channels: the max number of channels for the stream 2595 * @private_value: the value passed to each kcontrol's private_value field 2596 * @info_ret: store struct snd_pcm_chmap instance if non-NULL 2597 * 2598 * Create channel-mapping control elements assigned to the given PCM stream(s). 2599 * Return: Zero if successful, or a negative error value. 2600 */ 2601 int snd_pcm_add_chmap_ctls(struct snd_pcm *pcm, int stream, 2602 const struct snd_pcm_chmap_elem *chmap, 2603 int max_channels, 2604 unsigned long private_value, 2605 struct snd_pcm_chmap **info_ret) 2606 { 2607 struct snd_pcm_chmap *info; 2608 struct snd_kcontrol_new knew = { 2609 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 2610 .access = SNDRV_CTL_ELEM_ACCESS_READ | 2611 SNDRV_CTL_ELEM_ACCESS_VOLATILE | 2612 SNDRV_CTL_ELEM_ACCESS_TLV_READ | 2613 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK, 2614 .info = pcm_chmap_ctl_info, 2615 .get = pcm_chmap_ctl_get, 2616 .tlv.c = pcm_chmap_ctl_tlv, 2617 }; 2618 int err; 2619 2620 if (WARN_ON(pcm->streams[stream].chmap_kctl)) 2621 return -EBUSY; 2622 info = kzalloc_obj(*info); 2623 if (!info) 2624 return -ENOMEM; 2625 info->pcm = pcm; 2626 info->stream = stream; 2627 info->chmap = chmap; 2628 info->max_channels = max_channels; 2629 if (stream == SNDRV_PCM_STREAM_PLAYBACK) 2630 knew.name = "Playback Channel Map"; 2631 else 2632 knew.name = "Capture Channel Map"; 2633 knew.device = pcm->device; 2634 knew.count = pcm->streams[stream].substream_count; 2635 knew.private_value = private_value; 2636 info->kctl = snd_ctl_new1(&knew, info); 2637 if (!info->kctl) { 2638 kfree(info); 2639 return -ENOMEM; 2640 } 2641 info->kctl->private_free = pcm_chmap_ctl_private_free; 2642 err = snd_ctl_add(pcm->card, info->kctl); 2643 if (err < 0) 2644 return err; 2645 pcm->streams[stream].chmap_kctl = info->kctl; 2646 if (info_ret) 2647 *info_ret = info; 2648 return 0; 2649 } 2650 EXPORT_SYMBOL_GPL(snd_pcm_add_chmap_ctls); 2651