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