LCOV - code coverage report
Current view: top level - sound/core - pcm_lib.c (source / functions) Hit Total Coverage
Test: gcov_data_raspi2_real_modules_combined.info Lines: 4 882 0.5 %
Date: 2020-09-30 20:25:40 Functions: 1 72 1.4 %
Branches: 2 741 0.3 %

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

Generated by: LCOV version 1.14