linux/sound/core/pcm_native.c
<<
>>
Prefs
   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 */
   6
   7#include <linux/compat.h>
   8#include <linux/mm.h>
   9#include <linux/module.h>
  10#include <linux/file.h>
  11#include <linux/slab.h>
  12#include <linux/sched/signal.h>
  13#include <linux/time.h>
  14#include <linux/pm_qos.h>
  15#include <linux/io.h>
  16#include <linux/dma-mapping.h>
  17#include <linux/vmalloc.h>
  18#include <sound/core.h>
  19#include <sound/control.h>
  20#include <sound/info.h>
  21#include <sound/pcm.h>
  22#include <sound/pcm_params.h>
  23#include <sound/timer.h>
  24#include <sound/minors.h>
  25#include <linux/uio.h>
  26#include <linux/delay.h>
  27
  28#include "pcm_local.h"
  29
  30#ifdef CONFIG_SND_DEBUG
  31#define CREATE_TRACE_POINTS
  32#include "pcm_param_trace.h"
  33#else
  34#define trace_hw_mask_param_enabled()           0
  35#define trace_hw_interval_param_enabled()       0
  36#define trace_hw_mask_param(substream, type, index, prev, curr)
  37#define trace_hw_interval_param(substream, type, index, prev, curr)
  38#endif
  39
  40/*
  41 *  Compatibility
  42 */
  43
  44struct snd_pcm_hw_params_old {
  45        unsigned int flags;
  46        unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
  47                           SNDRV_PCM_HW_PARAM_ACCESS + 1];
  48        struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
  49                                        SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
  50        unsigned int rmask;
  51        unsigned int cmask;
  52        unsigned int info;
  53        unsigned int msbits;
  54        unsigned int rate_num;
  55        unsigned int rate_den;
  56        snd_pcm_uframes_t fifo_size;
  57        unsigned char reserved[64];
  58};
  59
  60#ifdef CONFIG_SND_SUPPORT_OLD_API
  61#define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
  62#define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
  63
  64static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
  65                                      struct snd_pcm_hw_params_old __user * _oparams);
  66static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
  67                                      struct snd_pcm_hw_params_old __user * _oparams);
  68#endif
  69static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
  70
  71/*
  72 *
  73 */
  74
  75static DECLARE_RWSEM(snd_pcm_link_rwsem);
  76
  77void snd_pcm_group_init(struct snd_pcm_group *group)
  78{
  79        spin_lock_init(&group->lock);
  80        mutex_init(&group->mutex);
  81        INIT_LIST_HEAD(&group->substreams);
  82        refcount_set(&group->refs, 1);
  83}
  84
  85/* define group lock helpers */
  86#define DEFINE_PCM_GROUP_LOCK(action, mutex_action) \
  87static void snd_pcm_group_ ## action(struct snd_pcm_group *group, bool nonatomic) \
  88{ \
  89        if (nonatomic) \
  90                mutex_ ## mutex_action(&group->mutex); \
  91        else \
  92                spin_ ## action(&group->lock); \
  93}
  94
  95DEFINE_PCM_GROUP_LOCK(lock, lock);
  96DEFINE_PCM_GROUP_LOCK(unlock, unlock);
  97DEFINE_PCM_GROUP_LOCK(lock_irq, lock);
  98DEFINE_PCM_GROUP_LOCK(unlock_irq, unlock);
  99
 100/**
 101 * snd_pcm_stream_lock - Lock the PCM stream
 102 * @substream: PCM substream
 103 *
 104 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
 105 * flag of the given substream.  This also takes the global link rw lock
 106 * (or rw sem), too, for avoiding the race with linked streams.
 107 */
 108void snd_pcm_stream_lock(struct snd_pcm_substream *substream)
 109{
 110        snd_pcm_group_lock(&substream->self_group, substream->pcm->nonatomic);
 111}
 112EXPORT_SYMBOL_GPL(snd_pcm_stream_lock);
 113
 114/**
 115 * snd_pcm_stream_unlock - Unlock the PCM stream
 116 * @substream: PCM substream
 117 *
 118 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
 119 */
 120void snd_pcm_stream_unlock(struct snd_pcm_substream *substream)
 121{
 122        snd_pcm_group_unlock(&substream->self_group, substream->pcm->nonatomic);
 123}
 124EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
 125
 126/**
 127 * snd_pcm_stream_lock_irq - Lock the PCM stream
 128 * @substream: PCM substream
 129 *
 130 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
 131 * IRQ (only when nonatomic is false).  In nonatomic case, this is identical
 132 * as snd_pcm_stream_lock().
 133 */
 134void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
 135{
 136        snd_pcm_group_lock_irq(&substream->self_group,
 137                               substream->pcm->nonatomic);
 138}
 139EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
 140
 141static void snd_pcm_stream_lock_nested(struct snd_pcm_substream *substream)
 142{
 143        struct snd_pcm_group *group = &substream->self_group;
 144
 145        if (substream->pcm->nonatomic)
 146                mutex_lock_nested(&group->mutex, SINGLE_DEPTH_NESTING);
 147        else
 148                spin_lock_nested(&group->lock, SINGLE_DEPTH_NESTING);
 149}
 150
 151/**
 152 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
 153 * @substream: PCM substream
 154 *
 155 * This is a counter-part of snd_pcm_stream_lock_irq().
 156 */
 157void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
 158{
 159        snd_pcm_group_unlock_irq(&substream->self_group,
 160                                 substream->pcm->nonatomic);
 161}
 162EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
 163
 164unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
 165{
 166        unsigned long flags = 0;
 167        if (substream->pcm->nonatomic)
 168                mutex_lock(&substream->self_group.mutex);
 169        else
 170                spin_lock_irqsave(&substream->self_group.lock, flags);
 171        return flags;
 172}
 173EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave);
 174
 175/**
 176 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
 177 * @substream: PCM substream
 178 * @flags: irq flags
 179 *
 180 * This is a counter-part of snd_pcm_stream_lock_irqsave().
 181 */
 182void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
 183                                      unsigned long flags)
 184{
 185        if (substream->pcm->nonatomic)
 186                mutex_unlock(&substream->self_group.mutex);
 187        else
 188                spin_unlock_irqrestore(&substream->self_group.lock, flags);
 189}
 190EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
 191
 192/* Run PCM ioctl ops */
 193static int snd_pcm_ops_ioctl(struct snd_pcm_substream *substream,
 194                             unsigned cmd, void *arg)
 195{
 196        if (substream->ops->ioctl)
 197                return substream->ops->ioctl(substream, cmd, arg);
 198        else
 199                return snd_pcm_lib_ioctl(substream, cmd, arg);
 200}
 201
 202int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
 203{
 204        struct snd_pcm *pcm = substream->pcm;
 205        struct snd_pcm_str *pstr = substream->pstr;
 206
 207        memset(info, 0, sizeof(*info));
 208        info->card = pcm->card->number;
 209        info->device = pcm->device;
 210        info->stream = substream->stream;
 211        info->subdevice = substream->number;
 212        strscpy(info->id, pcm->id, sizeof(info->id));
 213        strscpy(info->name, pcm->name, sizeof(info->name));
 214        info->dev_class = pcm->dev_class;
 215        info->dev_subclass = pcm->dev_subclass;
 216        info->subdevices_count = pstr->substream_count;
 217        info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
 218        strscpy(info->subname, substream->name, sizeof(info->subname));
 219
 220        return 0;
 221}
 222
 223int snd_pcm_info_user(struct snd_pcm_substream *substream,
 224                      struct snd_pcm_info __user * _info)
 225{
 226        struct snd_pcm_info *info;
 227        int err;
 228
 229        info = kmalloc(sizeof(*info), GFP_KERNEL);
 230        if (! info)
 231                return -ENOMEM;
 232        err = snd_pcm_info(substream, info);
 233        if (err >= 0) {
 234                if (copy_to_user(_info, info, sizeof(*info)))
 235                        err = -EFAULT;
 236        }
 237        kfree(info);
 238        return err;
 239}
 240
 241/* macro for simplified cast */
 242#define PARAM_MASK_BIT(b)       (1U << (__force int)(b))
 243
 244static bool hw_support_mmap(struct snd_pcm_substream *substream)
 245{
 246        if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
 247                return false;
 248
 249        if (substream->ops->mmap || substream->ops->page)
 250                return true;
 251
 252        switch (substream->dma_buffer.dev.type) {
 253        case SNDRV_DMA_TYPE_UNKNOWN:
 254                /* we can't know the device, so just assume that the driver does
 255                 * everything right
 256                 */
 257                return true;
 258        case SNDRV_DMA_TYPE_CONTINUOUS:
 259        case SNDRV_DMA_TYPE_VMALLOC:
 260                return true;
 261        default:
 262                return dma_can_mmap(substream->dma_buffer.dev.dev);
 263        }
 264}
 265
 266static int constrain_mask_params(struct snd_pcm_substream *substream,
 267                                 struct snd_pcm_hw_params *params)
 268{
 269        struct snd_pcm_hw_constraints *constrs =
 270                                        &substream->runtime->hw_constraints;
 271        struct snd_mask *m;
 272        unsigned int k;
 273        struct snd_mask old_mask;
 274        int changed;
 275
 276        for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
 277                m = hw_param_mask(params, k);
 278                if (snd_mask_empty(m))
 279                        return -EINVAL;
 280
 281                /* This parameter is not requested to change by a caller. */
 282                if (!(params->rmask & PARAM_MASK_BIT(k)))
 283                        continue;
 284
 285                if (trace_hw_mask_param_enabled())
 286                        old_mask = *m;
 287
 288                changed = snd_mask_refine(m, constrs_mask(constrs, k));
 289                if (changed < 0)
 290                        return changed;
 291                if (changed == 0)
 292                        continue;
 293
 294                /* Set corresponding flag so that the caller gets it. */
 295                trace_hw_mask_param(substream, k, 0, &old_mask, m);
 296                params->cmask |= PARAM_MASK_BIT(k);
 297        }
 298
 299        return 0;
 300}
 301
 302static int constrain_interval_params(struct snd_pcm_substream *substream,
 303                                     struct snd_pcm_hw_params *params)
 304{
 305        struct snd_pcm_hw_constraints *constrs =
 306                                        &substream->runtime->hw_constraints;
 307        struct snd_interval *i;
 308        unsigned int k;
 309        struct snd_interval old_interval;
 310        int changed;
 311
 312        for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
 313                i = hw_param_interval(params, k);
 314                if (snd_interval_empty(i))
 315                        return -EINVAL;
 316
 317                /* This parameter is not requested to change by a caller. */
 318                if (!(params->rmask & PARAM_MASK_BIT(k)))
 319                        continue;
 320
 321                if (trace_hw_interval_param_enabled())
 322                        old_interval = *i;
 323
 324                changed = snd_interval_refine(i, constrs_interval(constrs, k));
 325                if (changed < 0)
 326                        return changed;
 327                if (changed == 0)
 328                        continue;
 329
 330                /* Set corresponding flag so that the caller gets it. */
 331                trace_hw_interval_param(substream, k, 0, &old_interval, i);
 332                params->cmask |= PARAM_MASK_BIT(k);
 333        }
 334
 335        return 0;
 336}
 337
 338static int constrain_params_by_rules(struct snd_pcm_substream *substream,
 339                                     struct snd_pcm_hw_params *params)
 340{
 341        struct snd_pcm_hw_constraints *constrs =
 342                                        &substream->runtime->hw_constraints;
 343        unsigned int k;
 344        unsigned int *rstamps;
 345        unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
 346        unsigned int stamp;
 347        struct snd_pcm_hw_rule *r;
 348        unsigned int d;
 349        struct snd_mask old_mask;
 350        struct snd_interval old_interval;
 351        bool again;
 352        int changed, err = 0;
 353
 354        /*
 355         * Each application of rule has own sequence number.
 356         *
 357         * Each member of 'rstamps' array represents the sequence number of
 358         * recent application of corresponding rule.
 359         */
 360        rstamps = kcalloc(constrs->rules_num, sizeof(unsigned int), GFP_KERNEL);
 361        if (!rstamps)
 362                return -ENOMEM;
 363
 364        /*
 365         * Each member of 'vstamps' array represents the sequence number of
 366         * recent application of rule in which corresponding parameters were
 367         * changed.
 368         *
 369         * In initial state, elements corresponding to parameters requested by
 370         * a caller is 1. For unrequested parameters, corresponding members
 371         * have 0 so that the parameters are never changed anymore.
 372         */
 373        for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
 374                vstamps[k] = (params->rmask & PARAM_MASK_BIT(k)) ? 1 : 0;
 375
 376        /* Due to the above design, actual sequence number starts at 2. */
 377        stamp = 2;
 378retry:
 379        /* Apply all rules in order. */
 380        again = false;
 381        for (k = 0; k < constrs->rules_num; k++) {
 382                r = &constrs->rules[k];
 383
 384                /*
 385                 * Check condition bits of this rule. When the rule has
 386                 * some condition bits, parameter without the bits is
 387                 * never processed. SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
 388                 * is an example of the condition bits.
 389                 */
 390                if (r->cond && !(r->cond & params->flags))
 391                        continue;
 392
 393                /*
 394                 * The 'deps' array includes maximum four dependencies
 395                 * to SNDRV_PCM_HW_PARAM_XXXs for this rule. The fifth
 396                 * member of this array is a sentinel and should be
 397                 * negative value.
 398                 *
 399                 * This rule should be processed in this time when dependent
 400                 * parameters were changed at former applications of the other
 401                 * rules.
 402                 */
 403                for (d = 0; r->deps[d] >= 0; d++) {
 404                        if (vstamps[r->deps[d]] > rstamps[k])
 405                                break;
 406                }
 407                if (r->deps[d] < 0)
 408                        continue;
 409
 410                if (trace_hw_mask_param_enabled()) {
 411                        if (hw_is_mask(r->var))
 412                                old_mask = *hw_param_mask(params, r->var);
 413                }
 414                if (trace_hw_interval_param_enabled()) {
 415                        if (hw_is_interval(r->var))
 416                                old_interval = *hw_param_interval(params, r->var);
 417                }
 418
 419                changed = r->func(params, r);
 420                if (changed < 0) {
 421                        err = changed;
 422                        goto out;
 423                }
 424
 425                /*
 426                 * When the parameter is changed, notify it to the caller
 427                 * by corresponding returned bit, then preparing for next
 428                 * iteration.
 429                 */
 430                if (changed && r->var >= 0) {
 431                        if (hw_is_mask(r->var)) {
 432                                trace_hw_mask_param(substream, r->var,
 433                                        k + 1, &old_mask,
 434                                        hw_param_mask(params, r->var));
 435                        }
 436                        if (hw_is_interval(r->var)) {
 437                                trace_hw_interval_param(substream, r->var,
 438                                        k + 1, &old_interval,
 439                                        hw_param_interval(params, r->var));
 440                        }
 441
 442                        params->cmask |= PARAM_MASK_BIT(r->var);
 443                        vstamps[r->var] = stamp;
 444                        again = true;
 445                }
 446
 447                rstamps[k] = stamp++;
 448        }
 449
 450        /* Iterate to evaluate all rules till no parameters are changed. */
 451        if (again)
 452                goto retry;
 453
 454 out:
 455        kfree(rstamps);
 456        return err;
 457}
 458
 459static int fixup_unreferenced_params(struct snd_pcm_substream *substream,
 460                                     struct snd_pcm_hw_params *params)
 461{
 462        const struct snd_interval *i;
 463        const struct snd_mask *m;
 464        int err;
 465
 466        if (!params->msbits) {
 467                i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
 468                if (snd_interval_single(i))
 469                        params->msbits = snd_interval_value(i);
 470        }
 471
 472        if (!params->rate_den) {
 473                i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
 474                if (snd_interval_single(i)) {
 475                        params->rate_num = snd_interval_value(i);
 476                        params->rate_den = 1;
 477                }
 478        }
 479
 480        if (!params->fifo_size) {
 481                m = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT);
 482                i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
 483                if (snd_mask_single(m) && snd_interval_single(i)) {
 484                        err = snd_pcm_ops_ioctl(substream,
 485                                                SNDRV_PCM_IOCTL1_FIFO_SIZE,
 486                                                params);
 487                        if (err < 0)
 488                                return err;
 489                }
 490        }
 491
 492        if (!params->info) {
 493                params->info = substream->runtime->hw.info;
 494                params->info &= ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
 495                                  SNDRV_PCM_INFO_DRAIN_TRIGGER);
 496                if (!hw_support_mmap(substream))
 497                        params->info &= ~(SNDRV_PCM_INFO_MMAP |
 498                                          SNDRV_PCM_INFO_MMAP_VALID);
 499        }
 500
 501        return 0;
 502}
 503
 504int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
 505                      struct snd_pcm_hw_params *params)
 506{
 507        int err;
 508
 509        params->info = 0;
 510        params->fifo_size = 0;
 511        if (params->rmask & PARAM_MASK_BIT(SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
 512                params->msbits = 0;
 513        if (params->rmask & PARAM_MASK_BIT(SNDRV_PCM_HW_PARAM_RATE)) {
 514                params->rate_num = 0;
 515                params->rate_den = 0;
 516        }
 517
 518        err = constrain_mask_params(substream, params);
 519        if (err < 0)
 520                return err;
 521
 522        err = constrain_interval_params(substream, params);
 523        if (err < 0)
 524                return err;
 525
 526        err = constrain_params_by_rules(substream, params);
 527        if (err < 0)
 528                return err;
 529
 530        params->rmask = 0;
 531
 532        return 0;
 533}
 534EXPORT_SYMBOL(snd_pcm_hw_refine);
 535
 536static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
 537                                  struct snd_pcm_hw_params __user * _params)
 538{
 539        struct snd_pcm_hw_params *params;
 540        int err;
 541
 542        params = memdup_user(_params, sizeof(*params));
 543        if (IS_ERR(params))
 544                return PTR_ERR(params);
 545
 546        err = snd_pcm_hw_refine(substream, params);
 547        if (err < 0)
 548                goto end;
 549
 550        err = fixup_unreferenced_params(substream, params);
 551        if (err < 0)
 552                goto end;
 553
 554        if (copy_to_user(_params, params, sizeof(*params)))
 555                err = -EFAULT;
 556end:
 557        kfree(params);
 558        return err;
 559}
 560
 561static int period_to_usecs(struct snd_pcm_runtime *runtime)
 562{
 563        int usecs;
 564
 565        if (! runtime->rate)
 566                return -1; /* invalid */
 567
 568        /* take 75% of period time as the deadline */
 569        usecs = (750000 / runtime->rate) * runtime->period_size;
 570        usecs += ((750000 % runtime->rate) * runtime->period_size) /
 571                runtime->rate;
 572
 573        return usecs;
 574}
 575
 576static void snd_pcm_set_state(struct snd_pcm_substream *substream,
 577                              snd_pcm_state_t state)
 578{
 579        snd_pcm_stream_lock_irq(substream);
 580        if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
 581                substream->runtime->status->state = state;
 582        snd_pcm_stream_unlock_irq(substream);
 583}
 584
 585static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
 586                                        int event)
 587{
 588#ifdef CONFIG_SND_PCM_TIMER
 589        if (substream->timer)
 590                snd_timer_notify(substream->timer, event,
 591                                        &substream->runtime->trigger_tstamp);
 592#endif
 593}
 594
 595void snd_pcm_sync_stop(struct snd_pcm_substream *substream, bool sync_irq)
 596{
 597        if (substream->runtime && substream->runtime->stop_operating) {
 598                substream->runtime->stop_operating = false;
 599                if (substream->ops && substream->ops->sync_stop)
 600                        substream->ops->sync_stop(substream);
 601                else if (sync_irq && substream->pcm->card->sync_irq > 0)
 602                        synchronize_irq(substream->pcm->card->sync_irq);
 603        }
 604}
 605
 606/**
 607 * snd_pcm_hw_params_choose - choose a configuration defined by @params
 608 * @pcm: PCM instance
 609 * @params: the hw_params instance
 610 *
 611 * Choose one configuration from configuration space defined by @params.
 612 * The configuration chosen is that obtained fixing in this order:
 613 * first access, first format, first subformat, min channels,
 614 * min rate, min period time, max buffer size, min tick time
 615 *
 616 * Return: Zero if successful, or a negative error code on failure.
 617 */
 618static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
 619                                    struct snd_pcm_hw_params *params)
 620{
 621        static const int vars[] = {
 622                SNDRV_PCM_HW_PARAM_ACCESS,
 623                SNDRV_PCM_HW_PARAM_FORMAT,
 624                SNDRV_PCM_HW_PARAM_SUBFORMAT,
 625                SNDRV_PCM_HW_PARAM_CHANNELS,
 626                SNDRV_PCM_HW_PARAM_RATE,
 627                SNDRV_PCM_HW_PARAM_PERIOD_TIME,
 628                SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
 629                SNDRV_PCM_HW_PARAM_TICK_TIME,
 630                -1
 631        };
 632        const int *v;
 633        struct snd_mask old_mask;
 634        struct snd_interval old_interval;
 635        int changed;
 636
 637        for (v = vars; *v != -1; v++) {
 638                /* Keep old parameter to trace. */
 639                if (trace_hw_mask_param_enabled()) {
 640                        if (hw_is_mask(*v))
 641                                old_mask = *hw_param_mask(params, *v);
 642                }
 643                if (trace_hw_interval_param_enabled()) {
 644                        if (hw_is_interval(*v))
 645                                old_interval = *hw_param_interval(params, *v);
 646                }
 647                if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
 648                        changed = snd_pcm_hw_param_first(pcm, params, *v, NULL);
 649                else
 650                        changed = snd_pcm_hw_param_last(pcm, params, *v, NULL);
 651                if (changed < 0)
 652                        return changed;
 653                if (changed == 0)
 654                        continue;
 655
 656                /* Trace the changed parameter. */
 657                if (hw_is_mask(*v)) {
 658                        trace_hw_mask_param(pcm, *v, 0, &old_mask,
 659                                            hw_param_mask(params, *v));
 660                }
 661                if (hw_is_interval(*v)) {
 662                        trace_hw_interval_param(pcm, *v, 0, &old_interval,
 663                                                hw_param_interval(params, *v));
 664                }
 665        }
 666
 667        return 0;
 668}
 669
 670static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
 671                             struct snd_pcm_hw_params *params)
 672{
 673        struct snd_pcm_runtime *runtime;
 674        int err, usecs;
 675        unsigned int bits;
 676        snd_pcm_uframes_t frames;
 677
 678        if (PCM_RUNTIME_CHECK(substream))
 679                return -ENXIO;
 680        runtime = substream->runtime;
 681        snd_pcm_stream_lock_irq(substream);
 682        switch (runtime->status->state) {
 683        case SNDRV_PCM_STATE_OPEN:
 684        case SNDRV_PCM_STATE_SETUP:
 685        case SNDRV_PCM_STATE_PREPARED:
 686                break;
 687        default:
 688                snd_pcm_stream_unlock_irq(substream);
 689                return -EBADFD;
 690        }
 691        snd_pcm_stream_unlock_irq(substream);
 692#if IS_ENABLED(CONFIG_SND_PCM_OSS)
 693        if (!substream->oss.oss)
 694#endif
 695                if (atomic_read(&substream->mmap_count))
 696                        return -EBADFD;
 697
 698        snd_pcm_sync_stop(substream, true);
 699
 700        params->rmask = ~0U;
 701        err = snd_pcm_hw_refine(substream, params);
 702        if (err < 0)
 703                goto _error;
 704
 705        err = snd_pcm_hw_params_choose(substream, params);
 706        if (err < 0)
 707                goto _error;
 708
 709        err = fixup_unreferenced_params(substream, params);
 710        if (err < 0)
 711                goto _error;
 712
 713        if (substream->managed_buffer_alloc) {
 714                err = snd_pcm_lib_malloc_pages(substream,
 715                                               params_buffer_bytes(params));
 716                if (err < 0)
 717                        goto _error;
 718                runtime->buffer_changed = err > 0;
 719        }
 720
 721        if (substream->ops->hw_params != NULL) {
 722                err = substream->ops->hw_params(substream, params);
 723                if (err < 0)
 724                        goto _error;
 725        }
 726
 727        runtime->access = params_access(params);
 728        runtime->format = params_format(params);
 729        runtime->subformat = params_subformat(params);
 730        runtime->channels = params_channels(params);
 731        runtime->rate = params_rate(params);
 732        runtime->period_size = params_period_size(params);
 733        runtime->periods = params_periods(params);
 734        runtime->buffer_size = params_buffer_size(params);
 735        runtime->info = params->info;
 736        runtime->rate_num = params->rate_num;
 737        runtime->rate_den = params->rate_den;
 738        runtime->no_period_wakeup =
 739                        (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
 740                        (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
 741
 742        bits = snd_pcm_format_physical_width(runtime->format);
 743        runtime->sample_bits = bits;
 744        bits *= runtime->channels;
 745        runtime->frame_bits = bits;
 746        frames = 1;
 747        while (bits % 8 != 0) {
 748                bits *= 2;
 749                frames *= 2;
 750        }
 751        runtime->byte_align = bits / 8;
 752        runtime->min_align = frames;
 753
 754        /* Default sw params */
 755        runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
 756        runtime->period_step = 1;
 757        runtime->control->avail_min = runtime->period_size;
 758        runtime->start_threshold = 1;
 759        runtime->stop_threshold = runtime->buffer_size;
 760        runtime->silence_threshold = 0;
 761        runtime->silence_size = 0;
 762        runtime->boundary = runtime->buffer_size;
 763        while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
 764                runtime->boundary *= 2;
 765
 766        /* clear the buffer for avoiding possible kernel info leaks */
 767        if (runtime->dma_area && !substream->ops->copy_user) {
 768                size_t size = runtime->dma_bytes;
 769
 770                if (runtime->info & SNDRV_PCM_INFO_MMAP)
 771                        size = PAGE_ALIGN(size);
 772                memset(runtime->dma_area, 0, size);
 773        }
 774
 775        snd_pcm_timer_resolution_change(substream);
 776        snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
 777
 778        if (cpu_latency_qos_request_active(&substream->latency_pm_qos_req))
 779                cpu_latency_qos_remove_request(&substream->latency_pm_qos_req);
 780        if ((usecs = period_to_usecs(runtime)) >= 0)
 781                cpu_latency_qos_add_request(&substream->latency_pm_qos_req,
 782                                            usecs);
 783        return 0;
 784 _error:
 785        /* hardware might be unusable from this time,
 786           so we force application to retry to set
 787           the correct hardware parameter settings */
 788        snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
 789        if (substream->ops->hw_free != NULL)
 790                substream->ops->hw_free(substream);
 791        if (substream->managed_buffer_alloc)
 792                snd_pcm_lib_free_pages(substream);
 793        return err;
 794}
 795
 796static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
 797                                  struct snd_pcm_hw_params __user * _params)
 798{
 799        struct snd_pcm_hw_params *params;
 800        int err;
 801
 802        params = memdup_user(_params, sizeof(*params));
 803        if (IS_ERR(params))
 804                return PTR_ERR(params);
 805
 806        err = snd_pcm_hw_params(substream, params);
 807        if (err < 0)
 808                goto end;
 809
 810        if (copy_to_user(_params, params, sizeof(*params)))
 811                err = -EFAULT;
 812end:
 813        kfree(params);
 814        return err;
 815}
 816
 817static int do_hw_free(struct snd_pcm_substream *substream)
 818{
 819        int result = 0;
 820
 821        snd_pcm_sync_stop(substream, true);
 822        if (substream->ops->hw_free)
 823                result = substream->ops->hw_free(substream);
 824        if (substream->managed_buffer_alloc)
 825                snd_pcm_lib_free_pages(substream);
 826        return result;
 827}
 828
 829static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
 830{
 831        struct snd_pcm_runtime *runtime;
 832        int result;
 833
 834        if (PCM_RUNTIME_CHECK(substream))
 835                return -ENXIO;
 836        runtime = substream->runtime;
 837        snd_pcm_stream_lock_irq(substream);
 838        switch (runtime->status->state) {
 839        case SNDRV_PCM_STATE_SETUP:
 840        case SNDRV_PCM_STATE_PREPARED:
 841                break;
 842        default:
 843                snd_pcm_stream_unlock_irq(substream);
 844                return -EBADFD;
 845        }
 846        snd_pcm_stream_unlock_irq(substream);
 847        if (atomic_read(&substream->mmap_count))
 848                return -EBADFD;
 849        result = do_hw_free(substream);
 850        snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
 851        cpu_latency_qos_remove_request(&substream->latency_pm_qos_req);
 852        return result;
 853}
 854
 855static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
 856                             struct snd_pcm_sw_params *params)
 857{
 858        struct snd_pcm_runtime *runtime;
 859        int err;
 860
 861        if (PCM_RUNTIME_CHECK(substream))
 862                return -ENXIO;
 863        runtime = substream->runtime;
 864        snd_pcm_stream_lock_irq(substream);
 865        if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
 866                snd_pcm_stream_unlock_irq(substream);
 867                return -EBADFD;
 868        }
 869        snd_pcm_stream_unlock_irq(substream);
 870
 871        if (params->tstamp_mode < 0 ||
 872            params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
 873                return -EINVAL;
 874        if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
 875            params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
 876                return -EINVAL;
 877        if (params->avail_min == 0)
 878                return -EINVAL;
 879        if (params->silence_size >= runtime->boundary) {
 880                if (params->silence_threshold != 0)
 881                        return -EINVAL;
 882        } else {
 883                if (params->silence_size > params->silence_threshold)
 884                        return -EINVAL;
 885                if (params->silence_threshold > runtime->buffer_size)
 886                        return -EINVAL;
 887        }
 888        err = 0;
 889        snd_pcm_stream_lock_irq(substream);
 890        runtime->tstamp_mode = params->tstamp_mode;
 891        if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
 892                runtime->tstamp_type = params->tstamp_type;
 893        runtime->period_step = params->period_step;
 894        runtime->control->avail_min = params->avail_min;
 895        runtime->start_threshold = params->start_threshold;
 896        runtime->stop_threshold = params->stop_threshold;
 897        runtime->silence_threshold = params->silence_threshold;
 898        runtime->silence_size = params->silence_size;
 899        params->boundary = runtime->boundary;
 900        if (snd_pcm_running(substream)) {
 901                if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
 902                    runtime->silence_size > 0)
 903                        snd_pcm_playback_silence(substream, ULONG_MAX);
 904                err = snd_pcm_update_state(substream, runtime);
 905        }
 906        snd_pcm_stream_unlock_irq(substream);
 907        return err;
 908}
 909
 910static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
 911                                  struct snd_pcm_sw_params __user * _params)
 912{
 913        struct snd_pcm_sw_params params;
 914        int err;
 915        if (copy_from_user(&params, _params, sizeof(params)))
 916                return -EFAULT;
 917        err = snd_pcm_sw_params(substream, &params);
 918        if (copy_to_user(_params, &params, sizeof(params)))
 919                return -EFAULT;
 920        return err;
 921}
 922
 923static inline snd_pcm_uframes_t
 924snd_pcm_calc_delay(struct snd_pcm_substream *substream)
 925{
 926        snd_pcm_uframes_t delay;
 927
 928        if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
 929                delay = snd_pcm_playback_hw_avail(substream->runtime);
 930        else
 931                delay = snd_pcm_capture_avail(substream->runtime);
 932        return delay + substream->runtime->delay;
 933}
 934
 935int snd_pcm_status64(struct snd_pcm_substream *substream,
 936                     struct snd_pcm_status64 *status)
 937{
 938        struct snd_pcm_runtime *runtime = substream->runtime;
 939
 940        snd_pcm_stream_lock_irq(substream);
 941
 942        snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
 943                                        &runtime->audio_tstamp_config);
 944
 945        /* backwards compatible behavior */
 946        if (runtime->audio_tstamp_config.type_requested ==
 947                SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
 948                if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
 949                        runtime->audio_tstamp_config.type_requested =
 950                                SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
 951                else
 952                        runtime->audio_tstamp_config.type_requested =
 953                                SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
 954                runtime->audio_tstamp_report.valid = 0;
 955        } else
 956                runtime->audio_tstamp_report.valid = 1;
 957
 958        status->state = runtime->status->state;
 959        status->suspended_state = runtime->status->suspended_state;
 960        if (status->state == SNDRV_PCM_STATE_OPEN)
 961                goto _end;
 962        status->trigger_tstamp_sec = runtime->trigger_tstamp.tv_sec;
 963        status->trigger_tstamp_nsec = runtime->trigger_tstamp.tv_nsec;
 964        if (snd_pcm_running(substream)) {
 965                snd_pcm_update_hw_ptr(substream);
 966                if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
 967                        status->tstamp_sec = runtime->status->tstamp.tv_sec;
 968                        status->tstamp_nsec =
 969                                runtime->status->tstamp.tv_nsec;
 970                        status->driver_tstamp_sec =
 971                                runtime->driver_tstamp.tv_sec;
 972                        status->driver_tstamp_nsec =
 973                                runtime->driver_tstamp.tv_nsec;
 974                        status->audio_tstamp_sec =
 975                                runtime->status->audio_tstamp.tv_sec;
 976                        status->audio_tstamp_nsec =
 977                                runtime->status->audio_tstamp.tv_nsec;
 978                        if (runtime->audio_tstamp_report.valid == 1)
 979                                /* backwards compatibility, no report provided in COMPAT mode */
 980                                snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
 981                                                                &status->audio_tstamp_accuracy,
 982                                                                &runtime->audio_tstamp_report);
 983
 984                        goto _tstamp_end;
 985                }
 986        } else {
 987                /* get tstamp only in fallback mode and only if enabled */
 988                if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
 989                        struct timespec64 tstamp;
 990
 991                        snd_pcm_gettime(runtime, &tstamp);
 992                        status->tstamp_sec = tstamp.tv_sec;
 993                        status->tstamp_nsec = tstamp.tv_nsec;
 994                }
 995        }
 996 _tstamp_end:
 997        status->appl_ptr = runtime->control->appl_ptr;
 998        status->hw_ptr = runtime->status->hw_ptr;
 999        status->avail = snd_pcm_avail(substream);
1000        status->delay = snd_pcm_running(substream) ?
1001                snd_pcm_calc_delay(substream) : 0;
1002        status->avail_max = runtime->avail_max;
1003        status->overrange = runtime->overrange;
1004        runtime->avail_max = 0;
1005        runtime->overrange = 0;
1006 _end:
1007        snd_pcm_stream_unlock_irq(substream);
1008        return 0;
1009}
1010
1011static int snd_pcm_status_user64(struct snd_pcm_substream *substream,
1012                                 struct snd_pcm_status64 __user * _status,
1013                                 bool ext)
1014{
1015        struct snd_pcm_status64 status;
1016        int res;
1017
1018        memset(&status, 0, sizeof(status));
1019        /*
1020         * with extension, parameters are read/write,
1021         * get audio_tstamp_data from user,
1022         * ignore rest of status structure
1023         */
1024        if (ext && get_user(status.audio_tstamp_data,
1025                                (u32 __user *)(&_status->audio_tstamp_data)))
1026                return -EFAULT;
1027        res = snd_pcm_status64(substream, &status);
1028        if (res < 0)
1029                return res;
1030        if (copy_to_user(_status, &status, sizeof(status)))
1031                return -EFAULT;
1032        return 0;
1033}
1034
1035static int snd_pcm_status_user32(struct snd_pcm_substream *substream,
1036                                 struct snd_pcm_status32 __user * _status,
1037                                 bool ext)
1038{
1039        struct snd_pcm_status64 status64;
1040        struct snd_pcm_status32 status32;
1041        int res;
1042
1043        memset(&status64, 0, sizeof(status64));
1044        memset(&status32, 0, sizeof(status32));
1045        /*
1046         * with extension, parameters are read/write,
1047         * get audio_tstamp_data from user,
1048         * ignore rest of status structure
1049         */
1050        if (ext && get_user(status64.audio_tstamp_data,
1051                            (u32 __user *)(&_status->audio_tstamp_data)))
1052                return -EFAULT;
1053        res = snd_pcm_status64(substream, &status64);
1054        if (res < 0)
1055                return res;
1056
1057        status32 = (struct snd_pcm_status32) {
1058                .state = status64.state,
1059                .trigger_tstamp_sec = status64.trigger_tstamp_sec,
1060                .trigger_tstamp_nsec = status64.trigger_tstamp_nsec,
1061                .tstamp_sec = status64.tstamp_sec,
1062                .tstamp_nsec = status64.tstamp_nsec,
1063                .appl_ptr = status64.appl_ptr,
1064                .hw_ptr = status64.hw_ptr,
1065                .delay = status64.delay,
1066                .avail = status64.avail,
1067                .avail_max = status64.avail_max,
1068                .overrange = status64.overrange,
1069                .suspended_state = status64.suspended_state,
1070                .audio_tstamp_data = status64.audio_tstamp_data,
1071                .audio_tstamp_sec = status64.audio_tstamp_sec,
1072                .audio_tstamp_nsec = status64.audio_tstamp_nsec,
1073                .driver_tstamp_sec = status64.audio_tstamp_sec,
1074                .driver_tstamp_nsec = status64.audio_tstamp_nsec,
1075                .audio_tstamp_accuracy = status64.audio_tstamp_accuracy,
1076        };
1077
1078        if (copy_to_user(_status, &status32, sizeof(status32)))
1079                return -EFAULT;
1080
1081        return 0;
1082}
1083
1084static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
1085                                struct snd_pcm_channel_info * info)
1086{
1087        struct snd_pcm_runtime *runtime;
1088        unsigned int channel;
1089        
1090        channel = info->channel;
1091        runtime = substream->runtime;
1092        snd_pcm_stream_lock_irq(substream);
1093        if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
1094                snd_pcm_stream_unlock_irq(substream);
1095                return -EBADFD;
1096        }
1097        snd_pcm_stream_unlock_irq(substream);
1098        if (channel >= runtime->channels)
1099                return -EINVAL;
1100        memset(info, 0, sizeof(*info));
1101        info->channel = channel;
1102        return snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
1103}
1104
1105static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
1106                                     struct snd_pcm_channel_info __user * _info)
1107{
1108        struct snd_pcm_channel_info info;
1109        int res;
1110        
1111        if (copy_from_user(&info, _info, sizeof(info)))
1112                return -EFAULT;
1113        res = snd_pcm_channel_info(substream, &info);
1114        if (res < 0)
1115                return res;
1116        if (copy_to_user(_info, &info, sizeof(info)))
1117                return -EFAULT;
1118        return 0;
1119}
1120
1121static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
1122{
1123        struct snd_pcm_runtime *runtime = substream->runtime;
1124        if (runtime->trigger_master == NULL)
1125                return;
1126        if (runtime->trigger_master == substream) {
1127                if (!runtime->trigger_tstamp_latched)
1128                        snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1129        } else {
1130                snd_pcm_trigger_tstamp(runtime->trigger_master);
1131                runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
1132        }
1133        runtime->trigger_master = NULL;
1134}
1135
1136#define ACTION_ARG_IGNORE       (__force snd_pcm_state_t)0
1137
1138struct action_ops {
1139        int (*pre_action)(struct snd_pcm_substream *substream,
1140                          snd_pcm_state_t state);
1141        int (*do_action)(struct snd_pcm_substream *substream,
1142                         snd_pcm_state_t state);
1143        void (*undo_action)(struct snd_pcm_substream *substream,
1144                            snd_pcm_state_t state);
1145        void (*post_action)(struct snd_pcm_substream *substream,
1146                            snd_pcm_state_t state);
1147};
1148
1149/*
1150 *  this functions is core for handling of linked stream
1151 *  Note: the stream state might be changed also on failure
1152 *  Note2: call with calling stream lock + link lock
1153 */
1154static int snd_pcm_action_group(const struct action_ops *ops,
1155                                struct snd_pcm_substream *substream,
1156                                snd_pcm_state_t state,
1157                                bool do_lock)
1158{
1159        struct snd_pcm_substream *s = NULL;
1160        struct snd_pcm_substream *s1;
1161        int res = 0, depth = 1;
1162
1163        snd_pcm_group_for_each_entry(s, substream) {
1164                if (do_lock && s != substream) {
1165                        if (s->pcm->nonatomic)
1166                                mutex_lock_nested(&s->self_group.mutex, depth);
1167                        else
1168                                spin_lock_nested(&s->self_group.lock, depth);
1169                        depth++;
1170                }
1171                res = ops->pre_action(s, state);
1172                if (res < 0)
1173                        goto _unlock;
1174        }
1175        snd_pcm_group_for_each_entry(s, substream) {
1176                res = ops->do_action(s, state);
1177                if (res < 0) {
1178                        if (ops->undo_action) {
1179                                snd_pcm_group_for_each_entry(s1, substream) {
1180                                        if (s1 == s) /* failed stream */
1181                                                break;
1182                                        ops->undo_action(s1, state);
1183                                }
1184                        }
1185                        s = NULL; /* unlock all */
1186                        goto _unlock;
1187                }
1188        }
1189        snd_pcm_group_for_each_entry(s, substream) {
1190                ops->post_action(s, state);
1191        }
1192 _unlock:
1193        if (do_lock) {
1194                /* unlock streams */
1195                snd_pcm_group_for_each_entry(s1, substream) {
1196                        if (s1 != substream) {
1197                                if (s1->pcm->nonatomic)
1198                                        mutex_unlock(&s1->self_group.mutex);
1199                                else
1200                                        spin_unlock(&s1->self_group.lock);
1201                        }
1202                        if (s1 == s)    /* end */
1203                                break;
1204                }
1205        }
1206        return res;
1207}
1208
1209/*
1210 *  Note: call with stream lock
1211 */
1212static int snd_pcm_action_single(const struct action_ops *ops,
1213                                 struct snd_pcm_substream *substream,
1214                                 snd_pcm_state_t state)
1215{
1216        int res;
1217        
1218        res = ops->pre_action(substream, state);
1219        if (res < 0)
1220                return res;
1221        res = ops->do_action(substream, state);
1222        if (res == 0)
1223                ops->post_action(substream, state);
1224        else if (ops->undo_action)
1225                ops->undo_action(substream, state);
1226        return res;
1227}
1228
1229static void snd_pcm_group_assign(struct snd_pcm_substream *substream,
1230                                 struct snd_pcm_group *new_group)
1231{
1232        substream->group = new_group;
1233        list_move(&substream->link_list, &new_group->substreams);
1234}
1235
1236/*
1237 * Unref and unlock the group, but keep the stream lock;
1238 * when the group becomes empty and no longer referred, destroy itself
1239 */
1240static void snd_pcm_group_unref(struct snd_pcm_group *group,
1241                                struct snd_pcm_substream *substream)
1242{
1243        bool do_free;
1244
1245        if (!group)
1246                return;
1247        do_free = refcount_dec_and_test(&group->refs);
1248        snd_pcm_group_unlock(group, substream->pcm->nonatomic);
1249        if (do_free)
1250                kfree(group);
1251}
1252
1253/*
1254 * Lock the group inside a stream lock and reference it;
1255 * return the locked group object, or NULL if not linked
1256 */
1257static struct snd_pcm_group *
1258snd_pcm_stream_group_ref(struct snd_pcm_substream *substream)
1259{
1260        bool nonatomic = substream->pcm->nonatomic;
1261        struct snd_pcm_group *group;
1262        bool trylock;
1263
1264        for (;;) {
1265                if (!snd_pcm_stream_linked(substream))
1266                        return NULL;
1267                group = substream->group;
1268                /* block freeing the group object */
1269                refcount_inc(&group->refs);
1270
1271                trylock = nonatomic ? mutex_trylock(&group->mutex) :
1272                        spin_trylock(&group->lock);
1273                if (trylock)
1274                        break; /* OK */
1275
1276                /* re-lock for avoiding ABBA deadlock */
1277                snd_pcm_stream_unlock(substream);
1278                snd_pcm_group_lock(group, nonatomic);
1279                snd_pcm_stream_lock(substream);
1280
1281                /* check the group again; the above opens a small race window */
1282                if (substream->group == group)
1283                        break; /* OK */
1284                /* group changed, try again */
1285                snd_pcm_group_unref(group, substream);
1286        }
1287        return group;
1288}
1289
1290/*
1291 *  Note: call with stream lock
1292 */
1293static int snd_pcm_action(const struct action_ops *ops,
1294                          struct snd_pcm_substream *substream,
1295                          snd_pcm_state_t state)
1296{
1297        struct snd_pcm_group *group;
1298        int res;
1299
1300        group = snd_pcm_stream_group_ref(substream);
1301        if (group)
1302                res = snd_pcm_action_group(ops, substream, state, true);
1303        else
1304                res = snd_pcm_action_single(ops, substream, state);
1305        snd_pcm_group_unref(group, substream);
1306        return res;
1307}
1308
1309/*
1310 *  Note: don't use any locks before
1311 */
1312static int snd_pcm_action_lock_irq(const struct action_ops *ops,
1313                                   struct snd_pcm_substream *substream,
1314                                   snd_pcm_state_t state)
1315{
1316        int res;
1317
1318        snd_pcm_stream_lock_irq(substream);
1319        res = snd_pcm_action(ops, substream, state);
1320        snd_pcm_stream_unlock_irq(substream);
1321        return res;
1322}
1323
1324/*
1325 */
1326static int snd_pcm_action_nonatomic(const struct action_ops *ops,
1327                                    struct snd_pcm_substream *substream,
1328                                    snd_pcm_state_t state)
1329{
1330        int res;
1331
1332        /* Guarantee the group members won't change during non-atomic action */
1333        down_read(&snd_pcm_link_rwsem);
1334        if (snd_pcm_stream_linked(substream))
1335                res = snd_pcm_action_group(ops, substream, state, false);
1336        else
1337                res = snd_pcm_action_single(ops, substream, state);
1338        up_read(&snd_pcm_link_rwsem);
1339        return res;
1340}
1341
1342/*
1343 * start callbacks
1344 */
1345static int snd_pcm_pre_start(struct snd_pcm_substream *substream,
1346                             snd_pcm_state_t state)
1347{
1348        struct snd_pcm_runtime *runtime = substream->runtime;
1349        if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1350                return -EBADFD;
1351        if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1352            !snd_pcm_playback_data(substream))
1353                return -EPIPE;
1354        runtime->trigger_tstamp_latched = false;
1355        runtime->trigger_master = substream;
1356        return 0;
1357}
1358
1359static int snd_pcm_do_start(struct snd_pcm_substream *substream,
1360                            snd_pcm_state_t state)
1361{
1362        if (substream->runtime->trigger_master != substream)
1363                return 0;
1364        return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1365}
1366
1367static void snd_pcm_undo_start(struct snd_pcm_substream *substream,
1368                               snd_pcm_state_t state)
1369{
1370        if (substream->runtime->trigger_master == substream)
1371                substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1372}
1373
1374static void snd_pcm_post_start(struct snd_pcm_substream *substream,
1375                               snd_pcm_state_t state)
1376{
1377        struct snd_pcm_runtime *runtime = substream->runtime;
1378        snd_pcm_trigger_tstamp(substream);
1379        runtime->hw_ptr_jiffies = jiffies;
1380        runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) / 
1381                                                            runtime->rate;
1382        runtime->status->state = state;
1383        if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1384            runtime->silence_size > 0)
1385                snd_pcm_playback_silence(substream, ULONG_MAX);
1386        snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
1387}
1388
1389static const struct action_ops snd_pcm_action_start = {
1390        .pre_action = snd_pcm_pre_start,
1391        .do_action = snd_pcm_do_start,
1392        .undo_action = snd_pcm_undo_start,
1393        .post_action = snd_pcm_post_start
1394};
1395
1396/**
1397 * snd_pcm_start - start all linked streams
1398 * @substream: the PCM substream instance
1399 *
1400 * Return: Zero if successful, or a negative error code.
1401 * The stream lock must be acquired before calling this function.
1402 */
1403int snd_pcm_start(struct snd_pcm_substream *substream)
1404{
1405        return snd_pcm_action(&snd_pcm_action_start, substream,
1406                              SNDRV_PCM_STATE_RUNNING);
1407}
1408
1409/* take the stream lock and start the streams */
1410static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream)
1411{
1412        return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream,
1413                                       SNDRV_PCM_STATE_RUNNING);
1414}
1415
1416/*
1417 * stop callbacks
1418 */
1419static int snd_pcm_pre_stop(struct snd_pcm_substream *substream,
1420                            snd_pcm_state_t state)
1421{
1422        struct snd_pcm_runtime *runtime = substream->runtime;
1423        if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1424                return -EBADFD;
1425        runtime->trigger_master = substream;
1426        return 0;
1427}
1428
1429static int snd_pcm_do_stop(struct snd_pcm_substream *substream,
1430                           snd_pcm_state_t state)
1431{
1432        if (substream->runtime->trigger_master == substream &&
1433            snd_pcm_running(substream)) {
1434                substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1435                substream->runtime->stop_operating = true;
1436        }
1437        return 0; /* unconditionally stop all substreams */
1438}
1439
1440static void snd_pcm_post_stop(struct snd_pcm_substream *substream,
1441                              snd_pcm_state_t state)
1442{
1443        struct snd_pcm_runtime *runtime = substream->runtime;
1444        if (runtime->status->state != state) {
1445                snd_pcm_trigger_tstamp(substream);
1446                runtime->status->state = state;
1447                snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
1448        }
1449        wake_up(&runtime->sleep);
1450        wake_up(&runtime->tsleep);
1451}
1452
1453static const struct action_ops snd_pcm_action_stop = {
1454        .pre_action = snd_pcm_pre_stop,
1455        .do_action = snd_pcm_do_stop,
1456        .post_action = snd_pcm_post_stop
1457};
1458
1459/**
1460 * snd_pcm_stop - try to stop all running streams in the substream group
1461 * @substream: the PCM substream instance
1462 * @state: PCM state after stopping the stream
1463 *
1464 * The state of each stream is then changed to the given state unconditionally.
1465 *
1466 * Return: Zero if successful, or a negative error code.
1467 */
1468int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
1469{
1470        return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1471}
1472EXPORT_SYMBOL(snd_pcm_stop);
1473
1474/**
1475 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1476 * @substream: the PCM substream
1477 *
1478 * After stopping, the state is changed to SETUP.
1479 * Unlike snd_pcm_stop(), this affects only the given stream.
1480 *
1481 * Return: Zero if successful, or a negative error code.
1482 */
1483int snd_pcm_drain_done(struct snd_pcm_substream *substream)
1484{
1485        return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1486                                     SNDRV_PCM_STATE_SETUP);
1487}
1488
1489/**
1490 * snd_pcm_stop_xrun - stop the running streams as XRUN
1491 * @substream: the PCM substream instance
1492 *
1493 * This stops the given running substream (and all linked substreams) as XRUN.
1494 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1495 *
1496 * Return: Zero if successful, or a negative error code.
1497 */
1498int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1499{
1500        unsigned long flags;
1501
1502        snd_pcm_stream_lock_irqsave(substream, flags);
1503        if (substream->runtime && snd_pcm_running(substream))
1504                __snd_pcm_xrun(substream);
1505        snd_pcm_stream_unlock_irqrestore(substream, flags);
1506        return 0;
1507}
1508EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1509
1510/*
1511 * pause callbacks: pass boolean (to start pause or resume) as state argument
1512 */
1513#define pause_pushed(state)     (__force bool)(state)
1514
1515static int snd_pcm_pre_pause(struct snd_pcm_substream *substream,
1516                             snd_pcm_state_t state)
1517{
1518        struct snd_pcm_runtime *runtime = substream->runtime;
1519        if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1520                return -ENOSYS;
1521        if (pause_pushed(state)) {
1522                if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1523                        return -EBADFD;
1524        } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1525                return -EBADFD;
1526        runtime->trigger_master = substream;
1527        return 0;
1528}
1529
1530static int snd_pcm_do_pause(struct snd_pcm_substream *substream,
1531                            snd_pcm_state_t state)
1532{
1533        if (substream->runtime->trigger_master != substream)
1534                return 0;
1535        /* some drivers might use hw_ptr to recover from the pause -
1536           update the hw_ptr now */
1537        if (pause_pushed(state))
1538                snd_pcm_update_hw_ptr(substream);
1539        /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1540         * a delta between the current jiffies, this gives a large enough
1541         * delta, effectively to skip the check once.
1542         */
1543        substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1544        return substream->ops->trigger(substream,
1545                                       pause_pushed(state) ?
1546                                       SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1547                                       SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1548}
1549
1550static void snd_pcm_undo_pause(struct snd_pcm_substream *substream,
1551                               snd_pcm_state_t state)
1552{
1553        if (substream->runtime->trigger_master == substream)
1554                substream->ops->trigger(substream,
1555                                        pause_pushed(state) ?
1556                                        SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1557                                        SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1558}
1559
1560static void snd_pcm_post_pause(struct snd_pcm_substream *substream,
1561                               snd_pcm_state_t state)
1562{
1563        struct snd_pcm_runtime *runtime = substream->runtime;
1564        snd_pcm_trigger_tstamp(substream);
1565        if (pause_pushed(state)) {
1566                runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1567                snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
1568                wake_up(&runtime->sleep);
1569                wake_up(&runtime->tsleep);
1570        } else {
1571                runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1572                snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
1573        }
1574}
1575
1576static const struct action_ops snd_pcm_action_pause = {
1577        .pre_action = snd_pcm_pre_pause,
1578        .do_action = snd_pcm_do_pause,
1579        .undo_action = snd_pcm_undo_pause,
1580        .post_action = snd_pcm_post_pause
1581};
1582
1583/*
1584 * Push/release the pause for all linked streams.
1585 */
1586static int snd_pcm_pause(struct snd_pcm_substream *substream, bool push)
1587{
1588        return snd_pcm_action(&snd_pcm_action_pause, substream,
1589                              (__force snd_pcm_state_t)push);
1590}
1591
1592static int snd_pcm_pause_lock_irq(struct snd_pcm_substream *substream,
1593                                  bool push)
1594{
1595        return snd_pcm_action_lock_irq(&snd_pcm_action_pause, substream,
1596                                       (__force snd_pcm_state_t)push);
1597}
1598
1599#ifdef CONFIG_PM
1600/* suspend callback: state argument ignored */
1601
1602static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream,
1603                               snd_pcm_state_t state)
1604{
1605        struct snd_pcm_runtime *runtime = substream->runtime;
1606        switch (runtime->status->state) {
1607        case SNDRV_PCM_STATE_SUSPENDED:
1608                return -EBUSY;
1609        /* unresumable PCM state; return -EBUSY for skipping suspend */
1610        case SNDRV_PCM_STATE_OPEN:
1611        case SNDRV_PCM_STATE_SETUP:
1612        case SNDRV_PCM_STATE_DISCONNECTED:
1613                return -EBUSY;
1614        }
1615        runtime->trigger_master = substream;
1616        return 0;
1617}
1618
1619static int snd_pcm_do_suspend(struct snd_pcm_substream *substream,
1620                              snd_pcm_state_t state)
1621{
1622        struct snd_pcm_runtime *runtime = substream->runtime;
1623        if (runtime->trigger_master != substream)
1624                return 0;
1625        if (! snd_pcm_running(substream))
1626                return 0;
1627        substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1628        runtime->stop_operating = true;
1629        return 0; /* suspend unconditionally */
1630}
1631
1632static void snd_pcm_post_suspend(struct snd_pcm_substream *substream,
1633                                 snd_pcm_state_t state)
1634{
1635        struct snd_pcm_runtime *runtime = substream->runtime;
1636        snd_pcm_trigger_tstamp(substream);
1637        runtime->status->suspended_state = runtime->status->state;
1638        runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1639        snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
1640        wake_up(&runtime->sleep);
1641        wake_up(&runtime->tsleep);
1642}
1643
1644static const struct action_ops snd_pcm_action_suspend = {
1645        .pre_action = snd_pcm_pre_suspend,
1646        .do_action = snd_pcm_do_suspend,
1647        .post_action = snd_pcm_post_suspend
1648};
1649
1650/*
1651 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1652 * @substream: the PCM substream
1653 *
1654 * After this call, all streams are changed to SUSPENDED state.
1655 *
1656 * Return: Zero if successful, or a negative error code.
1657 */
1658static int snd_pcm_suspend(struct snd_pcm_substream *substream)
1659{
1660        int err;
1661        unsigned long flags;
1662
1663        snd_pcm_stream_lock_irqsave(substream, flags);
1664        err = snd_pcm_action(&snd_pcm_action_suspend, substream,
1665                             ACTION_ARG_IGNORE);
1666        snd_pcm_stream_unlock_irqrestore(substream, flags);
1667        return err;
1668}
1669
1670/**
1671 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1672 * @pcm: the PCM instance
1673 *
1674 * After this call, all streams are changed to SUSPENDED state.
1675 *
1676 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1677 */
1678int snd_pcm_suspend_all(struct snd_pcm *pcm)
1679{
1680        struct snd_pcm_substream *substream;
1681        int stream, err = 0;
1682
1683        if (! pcm)
1684                return 0;
1685
1686        for_each_pcm_substream(pcm, stream, substream) {
1687                /* FIXME: the open/close code should lock this as well */
1688                if (!substream->runtime)
1689                        continue;
1690
1691                /*
1692                 * Skip BE dai link PCM's that are internal and may
1693                 * not have their substream ops set.
1694                 */
1695                if (!substream->ops)
1696                        continue;
1697
1698                err = snd_pcm_suspend(substream);
1699                if (err < 0 && err != -EBUSY)
1700                        return err;
1701        }
1702
1703        for_each_pcm_substream(pcm, stream, substream)
1704                snd_pcm_sync_stop(substream, false);
1705
1706        return 0;
1707}
1708EXPORT_SYMBOL(snd_pcm_suspend_all);
1709
1710/* resume callbacks: state argument ignored */
1711
1712static int snd_pcm_pre_resume(struct snd_pcm_substream *substream,
1713                              snd_pcm_state_t state)
1714{
1715        struct snd_pcm_runtime *runtime = substream->runtime;
1716        if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1717                return -ENOSYS;
1718        runtime->trigger_master = substream;
1719        return 0;
1720}
1721
1722static int snd_pcm_do_resume(struct snd_pcm_substream *substream,
1723                             snd_pcm_state_t state)
1724{
1725        struct snd_pcm_runtime *runtime = substream->runtime;
1726        if (runtime->trigger_master != substream)
1727                return 0;
1728        /* DMA not running previously? */
1729        if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1730            (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1731             substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1732                return 0;
1733        return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1734}
1735
1736static void snd_pcm_undo_resume(struct snd_pcm_substream *substream,
1737                                snd_pcm_state_t state)
1738{
1739        if (substream->runtime->trigger_master == substream &&
1740            snd_pcm_running(substream))
1741                substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1742}
1743
1744static void snd_pcm_post_resume(struct snd_pcm_substream *substream,
1745                                snd_pcm_state_t state)
1746{
1747        struct snd_pcm_runtime *runtime = substream->runtime;
1748        snd_pcm_trigger_tstamp(substream);
1749        runtime->status->state = runtime->status->suspended_state;
1750        snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
1751}
1752
1753static const struct action_ops snd_pcm_action_resume = {
1754        .pre_action = snd_pcm_pre_resume,
1755        .do_action = snd_pcm_do_resume,
1756        .undo_action = snd_pcm_undo_resume,
1757        .post_action = snd_pcm_post_resume
1758};
1759
1760static int snd_pcm_resume(struct snd_pcm_substream *substream)
1761{
1762        return snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream,
1763                                       ACTION_ARG_IGNORE);
1764}
1765
1766#else
1767
1768static int snd_pcm_resume(struct snd_pcm_substream *substream)
1769{
1770        return -ENOSYS;
1771}
1772
1773#endif /* CONFIG_PM */
1774
1775/*
1776 * xrun ioctl
1777 *
1778 * Change the RUNNING stream(s) to XRUN state.
1779 */
1780static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1781{
1782        struct snd_pcm_runtime *runtime = substream->runtime;
1783        int result;
1784
1785        snd_pcm_stream_lock_irq(substream);
1786        switch (runtime->status->state) {
1787        case SNDRV_PCM_STATE_XRUN:
1788                result = 0;     /* already there */
1789                break;
1790        case SNDRV_PCM_STATE_RUNNING:
1791                __snd_pcm_xrun(substream);
1792                result = 0;
1793                break;
1794        default:
1795                result = -EBADFD;
1796        }
1797        snd_pcm_stream_unlock_irq(substream);
1798        return result;
1799}
1800
1801/*
1802 * reset ioctl
1803 */
1804/* reset callbacks:  state argument ignored */
1805static int snd_pcm_pre_reset(struct snd_pcm_substream *substream,
1806                             snd_pcm_state_t state)
1807{
1808        struct snd_pcm_runtime *runtime = substream->runtime;
1809        switch (runtime->status->state) {
1810        case SNDRV_PCM_STATE_RUNNING:
1811        case SNDRV_PCM_STATE_PREPARED:
1812        case SNDRV_PCM_STATE_PAUSED:
1813        case SNDRV_PCM_STATE_SUSPENDED:
1814                return 0;
1815        default:
1816                return -EBADFD;
1817        }
1818}
1819
1820static int snd_pcm_do_reset(struct snd_pcm_substream *substream,
1821                            snd_pcm_state_t state)
1822{
1823        struct snd_pcm_runtime *runtime = substream->runtime;
1824        int err = snd_pcm_ops_ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1825        if (err < 0)
1826                return err;
1827        runtime->hw_ptr_base = 0;
1828        runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1829                runtime->status->hw_ptr % runtime->period_size;
1830        runtime->silence_start = runtime->status->hw_ptr;
1831        runtime->silence_filled = 0;
1832        return 0;
1833}
1834
1835static void snd_pcm_post_reset(struct snd_pcm_substream *substream,
1836                               snd_pcm_state_t state)
1837{
1838        struct snd_pcm_runtime *runtime = substream->runtime;
1839        runtime->control->appl_ptr = runtime->status->hw_ptr;
1840        if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1841            runtime->silence_size > 0)
1842                snd_pcm_playback_silence(substream, ULONG_MAX);
1843}
1844
1845static const struct action_ops snd_pcm_action_reset = {
1846        .pre_action = snd_pcm_pre_reset,
1847        .do_action = snd_pcm_do_reset,
1848        .post_action = snd_pcm_post_reset
1849};
1850
1851static int snd_pcm_reset(struct snd_pcm_substream *substream)
1852{
1853        return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream,
1854                                        ACTION_ARG_IGNORE);
1855}
1856
1857/*
1858 * prepare ioctl
1859 */
1860/* pass f_flags as state argument */
1861static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1862                               snd_pcm_state_t state)
1863{
1864        struct snd_pcm_runtime *runtime = substream->runtime;
1865        int f_flags = (__force int)state;
1866
1867        if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1868            runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1869                return -EBADFD;
1870        if (snd_pcm_running(substream))
1871                return -EBUSY;
1872        substream->f_flags = f_flags;
1873        return 0;
1874}
1875
1876static int snd_pcm_do_prepare(struct snd_pcm_substream *substream,
1877                              snd_pcm_state_t state)
1878{
1879        int err;
1880        snd_pcm_sync_stop(substream, true);
1881        err = substream->ops->prepare(substream);
1882        if (err < 0)
1883                return err;
1884        return snd_pcm_do_reset(substream, state);
1885}
1886
1887static void snd_pcm_post_prepare(struct snd_pcm_substream *substream,
1888                                 snd_pcm_state_t state)
1889{
1890        struct snd_pcm_runtime *runtime = substream->runtime;
1891        runtime->control->appl_ptr = runtime->status->hw_ptr;
1892        snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
1893}
1894
1895static const struct action_ops snd_pcm_action_prepare = {
1896        .pre_action = snd_pcm_pre_prepare,
1897        .do_action = snd_pcm_do_prepare,
1898        .post_action = snd_pcm_post_prepare
1899};
1900
1901/**
1902 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1903 * @substream: the PCM substream instance
1904 * @file: file to refer f_flags
1905 *
1906 * Return: Zero if successful, or a negative error code.
1907 */
1908static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1909                           struct file *file)
1910{
1911        int f_flags;
1912
1913        if (file)
1914                f_flags = file->f_flags;
1915        else
1916                f_flags = substream->f_flags;
1917
1918        snd_pcm_stream_lock_irq(substream);
1919        switch (substream->runtime->status->state) {
1920        case SNDRV_PCM_STATE_PAUSED:
1921                snd_pcm_pause(substream, false);
1922                fallthrough;
1923        case SNDRV_PCM_STATE_SUSPENDED:
1924                snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1925                break;
1926        }
1927        snd_pcm_stream_unlock_irq(substream);
1928
1929        return snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1930                                        substream,
1931                                        (__force snd_pcm_state_t)f_flags);
1932}
1933
1934/*
1935 * drain ioctl
1936 */
1937
1938/* drain init callbacks: state argument ignored */
1939static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream,
1940                                  snd_pcm_state_t state)
1941{
1942        struct snd_pcm_runtime *runtime = substream->runtime;
1943        switch (runtime->status->state) {
1944        case SNDRV_PCM_STATE_OPEN:
1945        case SNDRV_PCM_STATE_DISCONNECTED:
1946        case SNDRV_PCM_STATE_SUSPENDED:
1947                return -EBADFD;
1948        }
1949        runtime->trigger_master = substream;
1950        return 0;
1951}
1952
1953static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream,
1954                                 snd_pcm_state_t state)
1955{
1956        struct snd_pcm_runtime *runtime = substream->runtime;
1957        if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1958                switch (runtime->status->state) {
1959                case SNDRV_PCM_STATE_PREPARED:
1960                        /* start playback stream if possible */
1961                        if (! snd_pcm_playback_empty(substream)) {
1962                                snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1963                                snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1964                        } else {
1965                                runtime->status->state = SNDRV_PCM_STATE_SETUP;
1966                        }
1967                        break;
1968                case SNDRV_PCM_STATE_RUNNING:
1969                        runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1970                        break;
1971                case SNDRV_PCM_STATE_XRUN:
1972                        runtime->status->state = SNDRV_PCM_STATE_SETUP;
1973                        break;
1974                default:
1975                        break;
1976                }
1977        } else {
1978                /* stop running stream */
1979                if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1980                        snd_pcm_state_t new_state;
1981
1982                        new_state = snd_pcm_capture_avail(runtime) > 0 ?
1983                                SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1984                        snd_pcm_do_stop(substream, new_state);
1985                        snd_pcm_post_stop(substream, new_state);
1986                }
1987        }
1988
1989        if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1990            runtime->trigger_master == substream &&
1991            (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1992                return substream->ops->trigger(substream,
1993                                               SNDRV_PCM_TRIGGER_DRAIN);
1994
1995        return 0;
1996}
1997
1998static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream,
1999                                    snd_pcm_state_t state)
2000{
2001}
2002
2003static const struct action_ops snd_pcm_action_drain_init = {
2004        .pre_action = snd_pcm_pre_drain_init,
2005        .do_action = snd_pcm_do_drain_init,
2006        .post_action = snd_pcm_post_drain_init
2007};
2008
2009/*
2010 * Drain the stream(s).
2011 * When the substream is linked, sync until the draining of all playback streams
2012 * is finished.
2013 * After this call, all streams are supposed to be either SETUP or DRAINING
2014 * (capture only) state.
2015 */
2016static int snd_pcm_drain(struct snd_pcm_substream *substream,
2017                         struct file *file)
2018{
2019        struct snd_card *card;
2020        struct snd_pcm_runtime *runtime;
2021        struct snd_pcm_substream *s;
2022        struct snd_pcm_group *group;
2023        wait_queue_entry_t wait;
2024        int result = 0;
2025        int nonblock = 0;
2026
2027        card = substream->pcm->card;
2028        runtime = substream->runtime;
2029
2030        if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2031                return -EBADFD;
2032
2033        if (file) {
2034                if (file->f_flags & O_NONBLOCK)
2035                        nonblock = 1;
2036        } else if (substream->f_flags & O_NONBLOCK)
2037                nonblock = 1;
2038
2039        snd_pcm_stream_lock_irq(substream);
2040        /* resume pause */
2041        if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
2042                snd_pcm_pause(substream, false);
2043
2044        /* pre-start/stop - all running streams are changed to DRAINING state */
2045        result = snd_pcm_action(&snd_pcm_action_drain_init, substream,
2046                                ACTION_ARG_IGNORE);
2047        if (result < 0)
2048                goto unlock;
2049        /* in non-blocking, we don't wait in ioctl but let caller poll */
2050        if (nonblock) {
2051                result = -EAGAIN;
2052                goto unlock;
2053        }
2054
2055        for (;;) {
2056                long tout;
2057                struct snd_pcm_runtime *to_check;
2058                if (signal_pending(current)) {
2059                        result = -ERESTARTSYS;
2060                        break;
2061                }
2062                /* find a substream to drain */
2063                to_check = NULL;
2064                group = snd_pcm_stream_group_ref(substream);
2065                snd_pcm_group_for_each_entry(s, substream) {
2066                        if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
2067                                continue;
2068                        runtime = s->runtime;
2069                        if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
2070                                to_check = runtime;
2071                                break;
2072                        }
2073                }
2074                snd_pcm_group_unref(group, substream);
2075                if (!to_check)
2076                        break; /* all drained */
2077                init_waitqueue_entry(&wait, current);
2078                set_current_state(TASK_INTERRUPTIBLE);
2079                add_wait_queue(&to_check->sleep, &wait);
2080                snd_pcm_stream_unlock_irq(substream);
2081                if (runtime->no_period_wakeup)
2082                        tout = MAX_SCHEDULE_TIMEOUT;
2083                else {
2084                        tout = 10;
2085                        if (runtime->rate) {
2086                                long t = runtime->period_size * 2 / runtime->rate;
2087                                tout = max(t, tout);
2088                        }
2089                        tout = msecs_to_jiffies(tout * 1000);
2090                }
2091                tout = schedule_timeout(tout);
2092
2093                snd_pcm_stream_lock_irq(substream);
2094                group = snd_pcm_stream_group_ref(substream);
2095                snd_pcm_group_for_each_entry(s, substream) {
2096                        if (s->runtime == to_check) {
2097                                remove_wait_queue(&to_check->sleep, &wait);
2098                                break;
2099                        }
2100                }
2101                snd_pcm_group_unref(group, substream);
2102
2103                if (card->shutdown) {
2104                        result = -ENODEV;
2105                        break;
2106                }
2107                if (tout == 0) {
2108                        if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
2109                                result = -ESTRPIPE;
2110                        else {
2111                                dev_dbg(substream->pcm->card->dev,
2112                                        "playback drain error (DMA or IRQ trouble?)\n");
2113                                snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2114                                result = -EIO;
2115                        }
2116                        break;
2117                }
2118        }
2119
2120 unlock:
2121        snd_pcm_stream_unlock_irq(substream);
2122
2123        return result;
2124}
2125
2126/*
2127 * drop ioctl
2128 *
2129 * Immediately put all linked substreams into SETUP state.
2130 */
2131static int snd_pcm_drop(struct snd_pcm_substream *substream)
2132{
2133        struct snd_pcm_runtime *runtime;
2134        int result = 0;
2135        
2136        if (PCM_RUNTIME_CHECK(substream))
2137                return -ENXIO;
2138        runtime = substream->runtime;
2139
2140        if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
2141            runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
2142                return -EBADFD;
2143
2144        snd_pcm_stream_lock_irq(substream);
2145        /* resume pause */
2146        if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
2147                snd_pcm_pause(substream, false);
2148
2149        snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2150        /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
2151        snd_pcm_stream_unlock_irq(substream);
2152
2153        return result;
2154}
2155
2156
2157static bool is_pcm_file(struct file *file)
2158{
2159        struct inode *inode = file_inode(file);
2160        struct snd_pcm *pcm;
2161        unsigned int minor;
2162
2163        if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
2164                return false;
2165        minor = iminor(inode);
2166        pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2167        if (!pcm)
2168                pcm = snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2169        if (!pcm)
2170                return false;
2171        snd_card_unref(pcm->card);
2172        return true;
2173}
2174
2175/*
2176 * PCM link handling
2177 */
2178static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
2179{
2180        int res = 0;
2181        struct snd_pcm_file *pcm_file;
2182        struct snd_pcm_substream *substream1;
2183        struct snd_pcm_group *group, *target_group;
2184        bool nonatomic = substream->pcm->nonatomic;
2185        struct fd f = fdget(fd);
2186
2187        if (!f.file)
2188                return -EBADFD;
2189        if (!is_pcm_file(f.file)) {
2190                res = -EBADFD;
2191                goto _badf;
2192        }
2193        pcm_file = f.file->private_data;
2194        substream1 = pcm_file->substream;
2195
2196        if (substream == substream1) {
2197                res = -EINVAL;
2198                goto _badf;
2199        }
2200
2201        group = kzalloc(sizeof(*group), GFP_KERNEL);
2202        if (!group) {
2203                res = -ENOMEM;
2204                goto _nolock;
2205        }
2206        snd_pcm_group_init(group);
2207
2208        down_write(&snd_pcm_link_rwsem);
2209        if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
2210            substream->runtime->status->state != substream1->runtime->status->state ||
2211            substream->pcm->nonatomic != substream1->pcm->nonatomic) {
2212                res = -EBADFD;
2213                goto _end;
2214        }
2215        if (snd_pcm_stream_linked(substream1)) {
2216                res = -EALREADY;
2217                goto _end;
2218        }
2219
2220        snd_pcm_stream_lock_irq(substream);
2221        if (!snd_pcm_stream_linked(substream)) {
2222                snd_pcm_group_assign(substream, group);
2223                group = NULL; /* assigned, don't free this one below */
2224        }
2225        target_group = substream->group;
2226        snd_pcm_stream_unlock_irq(substream);
2227
2228        snd_pcm_group_lock_irq(target_group, nonatomic);
2229        snd_pcm_stream_lock_nested(substream1);
2230        snd_pcm_group_assign(substream1, target_group);
2231        refcount_inc(&target_group->refs);
2232        snd_pcm_stream_unlock(substream1);
2233        snd_pcm_group_unlock_irq(target_group, nonatomic);
2234 _end:
2235        up_write(&snd_pcm_link_rwsem);
2236 _nolock:
2237        kfree(group);
2238 _badf:
2239        fdput(f);
2240        return res;
2241}
2242
2243static void relink_to_local(struct snd_pcm_substream *substream)
2244{
2245        snd_pcm_stream_lock_nested(substream);
2246        snd_pcm_group_assign(substream, &substream->self_group);
2247        snd_pcm_stream_unlock(substream);
2248}
2249
2250static int snd_pcm_unlink(struct snd_pcm_substream *substream)
2251{
2252        struct snd_pcm_group *group;
2253        bool nonatomic = substream->pcm->nonatomic;
2254        bool do_free = false;
2255        int res = 0;
2256
2257        down_write(&snd_pcm_link_rwsem);
2258
2259        if (!snd_pcm_stream_linked(substream)) {
2260                res = -EALREADY;
2261                goto _end;
2262        }
2263
2264        group = substream->group;
2265        snd_pcm_group_lock_irq(group, nonatomic);
2266
2267        relink_to_local(substream);
2268        refcount_dec(&group->refs);
2269
2270        /* detach the last stream, too */
2271        if (list_is_singular(&group->substreams)) {
2272                relink_to_local(list_first_entry(&group->substreams,
2273                                                 struct snd_pcm_substream,
2274                                                 link_list));
2275                do_free = refcount_dec_and_test(&group->refs);
2276        }
2277
2278        snd_pcm_group_unlock_irq(group, nonatomic);
2279        if (do_free)
2280                kfree(group);
2281
2282       _end:
2283        up_write(&snd_pcm_link_rwsem);
2284        return res;
2285}
2286
2287/*
2288 * hw configurator
2289 */
2290static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
2291                               struct snd_pcm_hw_rule *rule)
2292{
2293        struct snd_interval t;
2294        snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
2295                     hw_param_interval_c(params, rule->deps[1]), &t);
2296        return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2297}
2298
2299static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
2300                               struct snd_pcm_hw_rule *rule)
2301{
2302        struct snd_interval t;
2303        snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
2304                     hw_param_interval_c(params, rule->deps[1]), &t);
2305        return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2306}
2307
2308static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
2309                                   struct snd_pcm_hw_rule *rule)
2310{
2311        struct snd_interval t;
2312        snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
2313                         hw_param_interval_c(params, rule->deps[1]),
2314                         (unsigned long) rule->private, &t);
2315        return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2316}
2317
2318static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
2319                                   struct snd_pcm_hw_rule *rule)
2320{
2321        struct snd_interval t;
2322        snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
2323                         (unsigned long) rule->private,
2324                         hw_param_interval_c(params, rule->deps[1]), &t);
2325        return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2326}
2327
2328static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
2329                                  struct snd_pcm_hw_rule *rule)
2330{
2331        snd_pcm_format_t k;
2332        const struct snd_interval *i =
2333                                hw_param_interval_c(params, rule->deps[0]);
2334        struct snd_mask m;
2335        struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
2336        snd_mask_any(&m);
2337        pcm_for_each_format(k) {
2338                int bits;
2339                if (!snd_mask_test_format(mask, k))
2340                        continue;
2341                bits = snd_pcm_format_physical_width(k);
2342                if (bits <= 0)
2343                        continue; /* ignore invalid formats */
2344                if ((unsigned)bits < i->min || (unsigned)bits > i->max)
2345                        snd_mask_reset(&m, (__force unsigned)k);
2346        }
2347        return snd_mask_refine(mask, &m);
2348}
2349
2350static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
2351                                       struct snd_pcm_hw_rule *rule)
2352{
2353        struct snd_interval t;
2354        snd_pcm_format_t k;
2355
2356        t.min = UINT_MAX;
2357        t.max = 0;
2358        t.openmin = 0;
2359        t.openmax = 0;
2360        pcm_for_each_format(k) {
2361                int bits;
2362                if (!snd_mask_test_format(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
2363                        continue;
2364                bits = snd_pcm_format_physical_width(k);
2365                if (bits <= 0)
2366                        continue; /* ignore invalid formats */
2367                if (t.min > (unsigned)bits)
2368                        t.min = bits;
2369                if (t.max < (unsigned)bits)
2370                        t.max = bits;
2371        }
2372        t.integer = 1;
2373        return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2374}
2375
2376#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
2377#error "Change this table"
2378#endif
2379
2380static const unsigned int rates[] = {
2381        5512, 8000, 11025, 16000, 22050, 32000, 44100,
2382        48000, 64000, 88200, 96000, 176400, 192000, 352800, 384000
2383};
2384
2385const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2386        .count = ARRAY_SIZE(rates),
2387        .list = rates,
2388};
2389
2390static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2391                                struct snd_pcm_hw_rule *rule)
2392{
2393        struct snd_pcm_hardware *hw = rule->private;
2394        return snd_interval_list(hw_param_interval(params, rule->var),
2395                                 snd_pcm_known_rates.count,
2396                                 snd_pcm_known_rates.list, hw->rates);
2397}               
2398
2399static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2400                                            struct snd_pcm_hw_rule *rule)
2401{
2402        struct snd_interval t;
2403        struct snd_pcm_substream *substream = rule->private;
2404        t.min = 0;
2405        t.max = substream->buffer_bytes_max;
2406        t.openmin = 0;
2407        t.openmax = 0;
2408        t.integer = 1;
2409        return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2410}               
2411
2412static int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
2413{
2414        struct snd_pcm_runtime *runtime = substream->runtime;
2415        struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
2416        int k, err;
2417
2418        for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2419                snd_mask_any(constrs_mask(constrs, k));
2420        }
2421
2422        for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2423                snd_interval_any(constrs_interval(constrs, k));
2424        }
2425
2426        snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2427        snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2428        snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2429        snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2430        snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2431
2432        err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2433                                   snd_pcm_hw_rule_format, NULL,
2434                                   SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2435        if (err < 0)
2436                return err;
2437        err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, 
2438                                  snd_pcm_hw_rule_sample_bits, NULL,
2439                                  SNDRV_PCM_HW_PARAM_FORMAT, 
2440                                  SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2441        if (err < 0)
2442                return err;
2443        err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, 
2444                                  snd_pcm_hw_rule_div, NULL,
2445                                  SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2446        if (err < 0)
2447                return err;
2448        err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, 
2449                                  snd_pcm_hw_rule_mul, NULL,
2450                                  SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2451        if (err < 0)
2452                return err;
2453        err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, 
2454                                  snd_pcm_hw_rule_mulkdiv, (void*) 8,
2455                                  SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2456        if (err < 0)
2457                return err;
2458        err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS, 
2459                                  snd_pcm_hw_rule_mulkdiv, (void*) 8,
2460                                  SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2461        if (err < 0)
2462                return err;
2463        err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, 
2464                                  snd_pcm_hw_rule_div, NULL,
2465                                  SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2466        if (err < 0)
2467                return err;
2468        err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 
2469                                  snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2470                                  SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2471        if (err < 0)
2472                return err;
2473        err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 
2474                                  snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2475                                  SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2476        if (err < 0)
2477                return err;
2478        err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS, 
2479                                  snd_pcm_hw_rule_div, NULL,
2480                                  SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2481        if (err < 0)
2482                return err;
2483        err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 
2484                                  snd_pcm_hw_rule_div, NULL,
2485                                  SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2486        if (err < 0)
2487                return err;
2488        err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 
2489                                  snd_pcm_hw_rule_mulkdiv, (void*) 8,
2490                                  SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2491        if (err < 0)
2492                return err;
2493        err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 
2494                                  snd_pcm_hw_rule_muldivk, (void*) 1000000,
2495                                  SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2496        if (err < 0)
2497                return err;
2498        err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 
2499                                  snd_pcm_hw_rule_mul, NULL,
2500                                  SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2501        if (err < 0)
2502                return err;
2503        err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 
2504                                  snd_pcm_hw_rule_mulkdiv, (void*) 8,
2505                                  SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2506        if (err < 0)
2507                return err;
2508        err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 
2509                                  snd_pcm_hw_rule_muldivk, (void*) 1000000,
2510                                  SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2511        if (err < 0)
2512                return err;
2513        err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 
2514                                  snd_pcm_hw_rule_muldivk, (void*) 8,
2515                                  SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2516        if (err < 0)
2517                return err;
2518        err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 
2519                                  snd_pcm_hw_rule_muldivk, (void*) 8,
2520                                  SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2521        if (err < 0)
2522                return err;
2523        err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 
2524                                  snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2525                                  SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2526        if (err < 0)
2527                return err;
2528        err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME, 
2529                                  snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2530                                  SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2531        if (err < 0)
2532                return err;
2533        return 0;
2534}
2535
2536static int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
2537{
2538        struct snd_pcm_runtime *runtime = substream->runtime;
2539        struct snd_pcm_hardware *hw = &runtime->hw;
2540        int err;
2541        unsigned int mask = 0;
2542
2543        if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2544                mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_RW_INTERLEAVED);
2545        if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2546                mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
2547        if (hw_support_mmap(substream)) {
2548                if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2549                        mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
2550                if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2551                        mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED);
2552                if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2553                        mask |= PARAM_MASK_BIT(SNDRV_PCM_ACCESS_MMAP_COMPLEX);
2554        }
2555        err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
2556        if (err < 0)
2557                return err;
2558
2559        err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
2560        if (err < 0)
2561                return err;
2562
2563        err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT,
2564                                         PARAM_MASK_BIT(SNDRV_PCM_SUBFORMAT_STD));
2565        if (err < 0)
2566                return err;
2567
2568        err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2569                                           hw->channels_min, hw->channels_max);
2570        if (err < 0)
2571                return err;
2572
2573        err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2574                                           hw->rate_min, hw->rate_max);
2575        if (err < 0)
2576                return err;
2577
2578        err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2579                                           hw->period_bytes_min, hw->period_bytes_max);
2580        if (err < 0)
2581                return err;
2582
2583        err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2584                                           hw->periods_min, hw->periods_max);
2585        if (err < 0)
2586                return err;
2587
2588        err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2589                                           hw->period_bytes_min, hw->buffer_bytes_max);
2590        if (err < 0)
2591                return err;
2592
2593        err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 
2594                                  snd_pcm_hw_rule_buffer_bytes_max, substream,
2595                                  SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2596        if (err < 0)
2597                return err;
2598
2599        /* FIXME: remove */
2600        if (runtime->dma_bytes) {
2601                err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2602                if (err < 0)
2603                        return err;
2604        }
2605
2606        if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2607                err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 
2608                                          snd_pcm_hw_rule_rate, hw,
2609                                          SNDRV_PCM_HW_PARAM_RATE, -1);
2610                if (err < 0)
2611                        return err;
2612        }
2613
2614        /* FIXME: this belong to lowlevel */
2615        snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2616
2617        return 0;
2618}
2619
2620static void pcm_release_private(struct snd_pcm_substream *substream)
2621{
2622        if (snd_pcm_stream_linked(substream))
2623                snd_pcm_unlink(substream);
2624}
2625
2626void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2627{
2628        substream->ref_count--;
2629        if (substream->ref_count > 0)
2630                return;
2631
2632        snd_pcm_drop(substream);
2633        if (substream->hw_opened) {
2634                if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
2635                        do_hw_free(substream);
2636                substream->ops->close(substream);
2637                substream->hw_opened = 0;
2638        }
2639        if (cpu_latency_qos_request_active(&substream->latency_pm_qos_req))
2640                cpu_latency_qos_remove_request(&substream->latency_pm_qos_req);
2641        if (substream->pcm_release) {
2642                substream->pcm_release(substream);
2643                substream->pcm_release = NULL;
2644        }
2645        snd_pcm_detach_substream(substream);
2646}
2647EXPORT_SYMBOL(snd_pcm_release_substream);
2648
2649int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2650                           struct file *file,
2651                           struct snd_pcm_substream **rsubstream)
2652{
2653        struct snd_pcm_substream *substream;
2654        int err;
2655
2656        err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2657        if (err < 0)
2658                return err;
2659        if (substream->ref_count > 1) {
2660                *rsubstream = substream;
2661                return 0;
2662        }
2663
2664        err = snd_pcm_hw_constraints_init(substream);
2665        if (err < 0) {
2666                pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
2667                goto error;
2668        }
2669
2670        if ((err = substream->ops->open(substream)) < 0)
2671                goto error;
2672
2673        substream->hw_opened = 1;
2674
2675        err = snd_pcm_hw_constraints_complete(substream);
2676        if (err < 0) {
2677                pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
2678                goto error;
2679        }
2680
2681        *rsubstream = substream;
2682        return 0;
2683
2684 error:
2685        snd_pcm_release_substream(substream);
2686        return err;
2687}
2688EXPORT_SYMBOL(snd_pcm_open_substream);
2689
2690static int snd_pcm_open_file(struct file *file,
2691                             struct snd_pcm *pcm,
2692                             int stream)
2693{
2694        struct snd_pcm_file *pcm_file;
2695        struct snd_pcm_substream *substream;
2696        int err;
2697
2698        err = snd_pcm_open_substream(pcm, stream, file, &substream);
2699        if (err < 0)
2700                return err;
2701
2702        pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2703        if (pcm_file == NULL) {
2704                snd_pcm_release_substream(substream);
2705                return -ENOMEM;
2706        }
2707        pcm_file->substream = substream;
2708        if (substream->ref_count == 1)
2709                substream->pcm_release = pcm_release_private;
2710        file->private_data = pcm_file;
2711
2712        return 0;
2713}
2714
2715static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2716{
2717        struct snd_pcm *pcm;
2718        int err = nonseekable_open(inode, file);
2719        if (err < 0)
2720                return err;
2721        pcm = snd_lookup_minor_data(iminor(inode),
2722                                    SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2723        err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2724        if (pcm)
2725                snd_card_unref(pcm->card);
2726        return err;
2727}
2728
2729static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2730{
2731        struct snd_pcm *pcm;
2732        int err = nonseekable_open(inode, file);
2733        if (err < 0)
2734                return err;
2735        pcm = snd_lookup_minor_data(iminor(inode),
2736                                    SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2737        err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2738        if (pcm)
2739                snd_card_unref(pcm->card);
2740        return err;
2741}
2742
2743static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2744{
2745        int err;
2746        wait_queue_entry_t wait;
2747
2748        if (pcm == NULL) {
2749                err = -ENODEV;
2750                goto __error1;
2751        }
2752        err = snd_card_file_add(pcm->card, file);
2753        if (err < 0)
2754                goto __error1;
2755        if (!try_module_get(pcm->card->module)) {
2756                err = -EFAULT;
2757                goto __error2;
2758        }
2759        init_waitqueue_entry(&wait, current);
2760        add_wait_queue(&pcm->open_wait, &wait);
2761        mutex_lock(&pcm->open_mutex);
2762        while (1) {
2763                err = snd_pcm_open_file(file, pcm, stream);
2764                if (err >= 0)
2765                        break;
2766                if (err == -EAGAIN) {
2767                        if (file->f_flags & O_NONBLOCK) {
2768                                err = -EBUSY;
2769                                break;
2770                        }
2771                } else
2772                        break;
2773                set_current_state(TASK_INTERRUPTIBLE);
2774                mutex_unlock(&pcm->open_mutex);
2775                schedule();
2776                mutex_lock(&pcm->open_mutex);
2777                if (pcm->card->shutdown) {
2778                        err = -ENODEV;
2779                        break;
2780                }
2781                if (signal_pending(current)) {
2782                        err = -ERESTARTSYS;
2783                        break;
2784                }
2785        }
2786        remove_wait_queue(&pcm->open_wait, &wait);
2787        mutex_unlock(&pcm->open_mutex);
2788        if (err < 0)
2789                goto __error;
2790        return err;
2791
2792      __error:
2793        module_put(pcm->card->module);
2794      __error2:
2795        snd_card_file_remove(pcm->card, file);
2796      __error1:
2797        return err;
2798}
2799
2800static int snd_pcm_release(struct inode *inode, struct file *file)
2801{
2802        struct snd_pcm *pcm;
2803        struct snd_pcm_substream *substream;
2804        struct snd_pcm_file *pcm_file;
2805
2806        pcm_file = file->private_data;
2807        substream = pcm_file->substream;
2808        if (snd_BUG_ON(!substream))
2809                return -ENXIO;
2810        pcm = substream->pcm;
2811        mutex_lock(&pcm->open_mutex);
2812        snd_pcm_release_substream(substream);
2813        kfree(pcm_file);
2814        mutex_unlock(&pcm->open_mutex);
2815        wake_up(&pcm->open_wait);
2816        module_put(pcm->card->module);
2817        snd_card_file_remove(pcm->card, file);
2818        return 0;
2819}
2820
2821/* check and update PCM state; return 0 or a negative error
2822 * call this inside PCM lock
2823 */
2824static int do_pcm_hwsync(struct snd_pcm_substream *substream)
2825{
2826        switch (substream->runtime->status->state) {
2827        case SNDRV_PCM_STATE_DRAINING:
2828                if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2829                        return -EBADFD;
2830                fallthrough;
2831        case SNDRV_PCM_STATE_RUNNING:
2832                return snd_pcm_update_hw_ptr(substream);
2833        case SNDRV_PCM_STATE_PREPARED:
2834        case SNDRV_PCM_STATE_PAUSED:
2835                return 0;
2836        case SNDRV_PCM_STATE_SUSPENDED:
2837                return -ESTRPIPE;
2838        case SNDRV_PCM_STATE_XRUN:
2839                return -EPIPE;
2840        default:
2841                return -EBADFD;
2842        }
2843}
2844
2845/* increase the appl_ptr; returns the processed frames or a negative error */
2846static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream,
2847                                          snd_pcm_uframes_t frames,
2848                                           snd_pcm_sframes_t avail)
2849{
2850        struct snd_pcm_runtime *runtime = substream->runtime;
2851        snd_pcm_sframes_t appl_ptr;
2852        int ret;
2853
2854        if (avail <= 0)
2855                return 0;
2856        if (frames > (snd_pcm_uframes_t)avail)
2857                frames = avail;
2858        appl_ptr = runtime->control->appl_ptr + frames;
2859        if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2860                appl_ptr -= runtime->boundary;
2861        ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2862        return ret < 0 ? ret : frames;
2863}
2864
2865/* decrease the appl_ptr; returns the processed frames or zero for error */
2866static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream,
2867                                         snd_pcm_uframes_t frames,
2868                                         snd_pcm_sframes_t avail)
2869{
2870        struct snd_pcm_runtime *runtime = substream->runtime;
2871        snd_pcm_sframes_t appl_ptr;
2872        int ret;
2873
2874        if (avail <= 0)
2875                return 0;
2876        if (frames > (snd_pcm_uframes_t)avail)
2877                frames = avail;
2878        appl_ptr = runtime->control->appl_ptr - frames;
2879        if (appl_ptr < 0)
2880                appl_ptr += runtime->boundary;
2881        ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
2882        /* NOTE: we return zero for errors because PulseAudio gets depressed
2883         * upon receiving an error from rewind ioctl and stops processing
2884         * any longer.  Returning zero means that no rewind is done, so
2885         * it's not absolutely wrong to answer like that.
2886         */
2887        return ret < 0 ? 0 : frames;
2888}
2889
2890static snd_pcm_sframes_t snd_pcm_rewind(struct snd_pcm_substream *substream,
2891                                        snd_pcm_uframes_t frames)
2892{
2893        snd_pcm_sframes_t ret;
2894
2895        if (frames == 0)
2896                return 0;
2897
2898        snd_pcm_stream_lock_irq(substream);
2899        ret = do_pcm_hwsync(substream);
2900        if (!ret)
2901                ret = rewind_appl_ptr(substream, frames,
2902                                      snd_pcm_hw_avail(substream));
2903        snd_pcm_stream_unlock_irq(substream);
2904        return ret;
2905}
2906
2907static snd_pcm_sframes_t snd_pcm_forward(struct snd_pcm_substream *substream,
2908                                         snd_pcm_uframes_t frames)
2909{
2910        snd_pcm_sframes_t ret;
2911
2912        if (frames == 0)
2913                return 0;
2914
2915        snd_pcm_stream_lock_irq(substream);
2916        ret = do_pcm_hwsync(substream);
2917        if (!ret)
2918                ret = forward_appl_ptr(substream, frames,
2919                                       snd_pcm_avail(substream));
2920        snd_pcm_stream_unlock_irq(substream);
2921        return ret;
2922}
2923
2924static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2925{
2926        int err;
2927
2928        snd_pcm_stream_lock_irq(substream);
2929        err = do_pcm_hwsync(substream);
2930        snd_pcm_stream_unlock_irq(substream);
2931        return err;
2932}
2933                
2934static int snd_pcm_delay(struct snd_pcm_substream *substream,
2935                         snd_pcm_sframes_t *delay)
2936{
2937        int err;
2938        snd_pcm_sframes_t n = 0;
2939
2940        snd_pcm_stream_lock_irq(substream);
2941        err = do_pcm_hwsync(substream);
2942        if (!err)
2943                n = snd_pcm_calc_delay(substream);
2944        snd_pcm_stream_unlock_irq(substream);
2945        if (!err)
2946                *delay = n;
2947        return err;
2948}
2949                
2950static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2951                            struct snd_pcm_sync_ptr __user *_sync_ptr)
2952{
2953        struct snd_pcm_runtime *runtime = substream->runtime;
2954        struct snd_pcm_sync_ptr sync_ptr;
2955        volatile struct snd_pcm_mmap_status *status;
2956        volatile struct snd_pcm_mmap_control *control;
2957        int err;
2958
2959        memset(&sync_ptr, 0, sizeof(sync_ptr));
2960        if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2961                return -EFAULT;
2962        if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2963                return -EFAULT; 
2964        status = runtime->status;
2965        control = runtime->control;
2966        if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2967                err = snd_pcm_hwsync(substream);
2968                if (err < 0)
2969                        return err;
2970        }
2971        snd_pcm_stream_lock_irq(substream);
2972        if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) {
2973                err = pcm_lib_apply_appl_ptr(substream,
2974                                             sync_ptr.c.control.appl_ptr);
2975                if (err < 0) {
2976                        snd_pcm_stream_unlock_irq(substream);
2977                        return err;
2978                }
2979        } else {
2980                sync_ptr.c.control.appl_ptr = control->appl_ptr;
2981        }
2982        if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2983                control->avail_min = sync_ptr.c.control.avail_min;
2984        else
2985                sync_ptr.c.control.avail_min = control->avail_min;
2986        sync_ptr.s.status.state = status->state;
2987        sync_ptr.s.status.hw_ptr = status->hw_ptr;
2988        sync_ptr.s.status.tstamp = status->tstamp;
2989        sync_ptr.s.status.suspended_state = status->suspended_state;
2990        sync_ptr.s.status.audio_tstamp = status->audio_tstamp;
2991        snd_pcm_stream_unlock_irq(substream);
2992        if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2993                return -EFAULT;
2994        return 0;
2995}
2996
2997struct snd_pcm_mmap_status32 {
2998        snd_pcm_state_t state;
2999        s32 pad1;
3000        u32 hw_ptr;
3001        s32 tstamp_sec;
3002        s32 tstamp_nsec;
3003        snd_pcm_state_t suspended_state;
3004        s32 audio_tstamp_sec;
3005        s32 audio_tstamp_nsec;
3006} __attribute__((packed));
3007
3008struct snd_pcm_mmap_control32 {
3009        u32 appl_ptr;
3010        u32 avail_min;
3011};
3012
3013struct snd_pcm_sync_ptr32 {
3014        u32 flags;
3015        union {
3016                struct snd_pcm_mmap_status32 status;
3017                unsigned char reserved[64];
3018        } s;
3019        union {
3020                struct snd_pcm_mmap_control32 control;
3021                unsigned char reserved[64];
3022        } c;
3023} __attribute__((packed));
3024
3025/* recalcuate the boundary within 32bit */
3026static snd_pcm_uframes_t recalculate_boundary(struct snd_pcm_runtime *runtime)
3027{
3028        snd_pcm_uframes_t boundary;
3029
3030        if (! runtime->buffer_size)
3031                return 0;
3032        boundary = runtime->buffer_size;
3033        while (boundary * 2 <= 0x7fffffffUL - runtime->buffer_size)
3034                boundary *= 2;
3035        return boundary;
3036}
3037
3038static int snd_pcm_ioctl_sync_ptr_compat(struct snd_pcm_substream *substream,
3039                                         struct snd_pcm_sync_ptr32 __user *src)
3040{
3041        struct snd_pcm_runtime *runtime = substream->runtime;
3042        volatile struct snd_pcm_mmap_status *status;
3043        volatile struct snd_pcm_mmap_control *control;
3044        u32 sflags;
3045        struct snd_pcm_mmap_control scontrol;
3046        struct snd_pcm_mmap_status sstatus;
3047        snd_pcm_uframes_t boundary;
3048        int err;
3049
3050        if (snd_BUG_ON(!runtime))
3051                return -EINVAL;
3052
3053        if (get_user(sflags, &src->flags) ||
3054            get_user(scontrol.appl_ptr, &src->c.control.appl_ptr) ||
3055            get_user(scontrol.avail_min, &src->c.control.avail_min))
3056                return -EFAULT;
3057        if (sflags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
3058                err = snd_pcm_hwsync(substream);
3059                if (err < 0)
3060                        return err;
3061        }
3062        status = runtime->status;
3063        control = runtime->control;
3064        boundary = recalculate_boundary(runtime);
3065        if (! boundary)
3066                boundary = 0x7fffffff;
3067        snd_pcm_stream_lock_irq(substream);
3068        /* FIXME: we should consider the boundary for the sync from app */
3069        if (!(sflags & SNDRV_PCM_SYNC_PTR_APPL)) {
3070                err = pcm_lib_apply_appl_ptr(substream,
3071                                scontrol.appl_ptr);
3072                if (err < 0) {
3073                        snd_pcm_stream_unlock_irq(substream);
3074                        return err;
3075                }
3076        } else
3077                scontrol.appl_ptr = control->appl_ptr % boundary;
3078        if (!(sflags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
3079                control->avail_min = scontrol.avail_min;
3080        else
3081                scontrol.avail_min = control->avail_min;
3082        sstatus.state = status->state;
3083        sstatus.hw_ptr = status->hw_ptr % boundary;
3084        sstatus.tstamp = status->tstamp;
3085        sstatus.suspended_state = status->suspended_state;
3086        sstatus.audio_tstamp = status->audio_tstamp;
3087        snd_pcm_stream_unlock_irq(substream);
3088        if (put_user(sstatus.state, &src->s.status.state) ||
3089            put_user(sstatus.hw_ptr, &src->s.status.hw_ptr) ||
3090            put_user(sstatus.tstamp.tv_sec, &src->s.status.tstamp_sec) ||
3091            put_user(sstatus.tstamp.tv_nsec, &src->s.status.tstamp_nsec) ||
3092            put_user(sstatus.suspended_state, &src->s.status.suspended_state) ||
3093            put_user(sstatus.audio_tstamp.tv_sec, &src->s.status.audio_tstamp_sec) ||
3094            put_user(sstatus.audio_tstamp.tv_nsec, &src->s.status.audio_tstamp_nsec) ||
3095            put_user(scontrol.appl_ptr, &src->c.control.appl_ptr) ||
3096            put_user(scontrol.avail_min, &src->c.control.avail_min))
3097                return -EFAULT;
3098
3099        return 0;
3100}
3101#define __SNDRV_PCM_IOCTL_SYNC_PTR32 _IOWR('A', 0x23, struct snd_pcm_sync_ptr32)
3102
3103static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
3104{
3105        struct snd_pcm_runtime *runtime = substream->runtime;
3106        int arg;
3107        
3108        if (get_user(arg, _arg))
3109                return -EFAULT;
3110        if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
3111                return -EINVAL;
3112        runtime->tstamp_type = arg;
3113        return 0;
3114}
3115
3116static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream *substream,
3117                                      struct snd_xferi __user *_xferi)
3118{
3119        struct snd_xferi xferi;
3120        struct snd_pcm_runtime *runtime = substream->runtime;
3121        snd_pcm_sframes_t result;
3122
3123        if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3124                return -EBADFD;
3125        if (put_user(0, &_xferi->result))
3126                return -EFAULT;
3127        if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
3128                return -EFAULT;
3129        if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3130                result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
3131        else
3132                result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
3133        if (put_user(result, &_xferi->result))
3134                return -EFAULT;
3135        return result < 0 ? result : 0;
3136}
3137
3138static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream,
3139                                      struct snd_xfern __user *_xfern)
3140{
3141        struct snd_xfern xfern;
3142        struct snd_pcm_runtime *runtime = substream->runtime;
3143        void *bufs;
3144        snd_pcm_sframes_t result;
3145
3146        if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3147                return -EBADFD;
3148        if (runtime->channels > 128)
3149                return -EINVAL;
3150        if (put_user(0, &_xfern->result))
3151                return -EFAULT;
3152        if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
3153                return -EFAULT;
3154
3155        bufs = memdup_user(xfern.bufs, sizeof(void *) * runtime->channels);
3156        if (IS_ERR(bufs))
3157                return PTR_ERR(bufs);
3158        if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3159                result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
3160        else
3161                result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
3162        kfree(bufs);
3163        if (put_user(result, &_xfern->result))
3164                return -EFAULT;
3165        return result < 0 ? result : 0;
3166}
3167
3168static int snd_pcm_rewind_ioctl(struct snd_pcm_substream *substream,
3169                                snd_pcm_uframes_t __user *_frames)
3170{
3171        snd_pcm_uframes_t frames;
3172        snd_pcm_sframes_t result;
3173
3174        if (get_user(frames, _frames))
3175                return -EFAULT;
3176        if (put_user(0, _frames))
3177                return -EFAULT;
3178        result = snd_pcm_rewind(substream, frames);
3179        if (put_user(result, _frames))
3180                return -EFAULT;
3181        return result < 0 ? result : 0;
3182}
3183
3184static int snd_pcm_forward_ioctl(struct snd_pcm_substream *substream,
3185                                 snd_pcm_uframes_t __user *_frames)
3186{
3187        snd_pcm_uframes_t frames;
3188        snd_pcm_sframes_t result;
3189
3190        if (get_user(frames, _frames))
3191                return -EFAULT;
3192        if (put_user(0, _frames))
3193                return -EFAULT;
3194        result = snd_pcm_forward(substream, frames);
3195        if (put_user(result, _frames))
3196                return -EFAULT;
3197        return result < 0 ? result : 0;
3198}
3199
3200static int snd_pcm_common_ioctl(struct file *file,
3201                                 struct snd_pcm_substream *substream,
3202                                 unsigned int cmd, void __user *arg)
3203{
3204        struct snd_pcm_file *pcm_file = file->private_data;
3205        int res;
3206
3207        if (PCM_RUNTIME_CHECK(substream))
3208                return -ENXIO;
3209
3210        res = snd_power_wait(substream->pcm->card, SNDRV_CTL_POWER_D0);
3211        if (res < 0)
3212                return res;
3213
3214        switch (cmd) {
3215        case SNDRV_PCM_IOCTL_PVERSION:
3216                return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
3217        case SNDRV_PCM_IOCTL_INFO:
3218                return snd_pcm_info_user(substream, arg);
3219        case SNDRV_PCM_IOCTL_TSTAMP:    /* just for compatibility */
3220                return 0;
3221        case SNDRV_PCM_IOCTL_TTSTAMP:
3222                return snd_pcm_tstamp(substream, arg);
3223        case SNDRV_PCM_IOCTL_USER_PVERSION:
3224                if (get_user(pcm_file->user_pversion,
3225                             (unsigned int __user *)arg))
3226                        return -EFAULT;
3227                return 0;
3228        case SNDRV_PCM_IOCTL_HW_REFINE:
3229                return snd_pcm_hw_refine_user(substream, arg);
3230        case SNDRV_PCM_IOCTL_HW_PARAMS:
3231                return snd_pcm_hw_params_user(substream, arg);
3232        case SNDRV_PCM_IOCTL_HW_FREE:
3233                return snd_pcm_hw_free(substream);
3234        case SNDRV_PCM_IOCTL_SW_PARAMS:
3235                return snd_pcm_sw_params_user(substream, arg);
3236        case SNDRV_PCM_IOCTL_STATUS32:
3237                return snd_pcm_status_user32(substream, arg, false);
3238        case SNDRV_PCM_IOCTL_STATUS_EXT32:
3239                return snd_pcm_status_user32(substream, arg, true);
3240        case SNDRV_PCM_IOCTL_STATUS64:
3241                return snd_pcm_status_user64(substream, arg, false);
3242        case SNDRV_PCM_IOCTL_STATUS_EXT64:
3243                return snd_pcm_status_user64(substream, arg, true);
3244        case SNDRV_PCM_IOCTL_CHANNEL_INFO:
3245                return snd_pcm_channel_info_user(substream, arg);
3246        case SNDRV_PCM_IOCTL_PREPARE:
3247                return snd_pcm_prepare(substream, file);
3248        case SNDRV_PCM_IOCTL_RESET:
3249                return snd_pcm_reset(substream);
3250        case SNDRV_PCM_IOCTL_START:
3251                return snd_pcm_start_lock_irq(substream);
3252        case SNDRV_PCM_IOCTL_LINK:
3253                return snd_pcm_link(substream, (int)(unsigned long) arg);
3254        case SNDRV_PCM_IOCTL_UNLINK:
3255                return snd_pcm_unlink(substream);
3256        case SNDRV_PCM_IOCTL_RESUME:
3257                return snd_pcm_resume(substream);
3258        case SNDRV_PCM_IOCTL_XRUN:
3259                return snd_pcm_xrun(substream);
3260        case SNDRV_PCM_IOCTL_HWSYNC:
3261                return snd_pcm_hwsync(substream);
3262        case SNDRV_PCM_IOCTL_DELAY:
3263        {
3264                snd_pcm_sframes_t delay;
3265                snd_pcm_sframes_t __user *res = arg;
3266                int err;
3267
3268                err = snd_pcm_delay(substream, &delay);
3269                if (err)
3270                        return err;
3271                if (put_user(delay, res))
3272                        return -EFAULT;
3273                return 0;
3274        }
3275        case __SNDRV_PCM_IOCTL_SYNC_PTR32:
3276                return snd_pcm_ioctl_sync_ptr_compat(substream, arg);
3277        case __SNDRV_PCM_IOCTL_SYNC_PTR64:
3278                return snd_pcm_sync_ptr(substream, arg);
3279#ifdef CONFIG_SND_SUPPORT_OLD_API
3280        case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
3281                return snd_pcm_hw_refine_old_user(substream, arg);
3282        case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
3283                return snd_pcm_hw_params_old_user(substream, arg);
3284#endif
3285        case SNDRV_PCM_IOCTL_DRAIN:
3286                return snd_pcm_drain(substream, file);
3287        case SNDRV_PCM_IOCTL_DROP:
3288                return snd_pcm_drop(substream);
3289        case SNDRV_PCM_IOCTL_PAUSE:
3290                return snd_pcm_pause_lock_irq(substream, (unsigned long)arg);
3291        case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
3292        case SNDRV_PCM_IOCTL_READI_FRAMES:
3293                return snd_pcm_xferi_frames_ioctl(substream, arg);
3294        case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
3295        case SNDRV_PCM_IOCTL_READN_FRAMES:
3296                return snd_pcm_xfern_frames_ioctl(substream, arg);
3297        case SNDRV_PCM_IOCTL_REWIND:
3298                return snd_pcm_rewind_ioctl(substream, arg);
3299        case SNDRV_PCM_IOCTL_FORWARD:
3300                return snd_pcm_forward_ioctl(substream, arg);
3301        }
3302        pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
3303        return -ENOTTY;
3304}
3305
3306static long snd_pcm_ioctl(struct file *file, unsigned int cmd,
3307                          unsigned long arg)
3308{
3309        struct snd_pcm_file *pcm_file;
3310
3311        pcm_file = file->private_data;
3312
3313        if (((cmd >> 8) & 0xff) != 'A')
3314                return -ENOTTY;
3315
3316        return snd_pcm_common_ioctl(file, pcm_file->substream, cmd,
3317                                     (void __user *)arg);
3318}
3319
3320/**
3321 * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
3322 * @substream: PCM substream
3323 * @cmd: IOCTL cmd
3324 * @arg: IOCTL argument
3325 *
3326 * The function is provided primarily for OSS layer and USB gadget drivers,
3327 * and it allows only the limited set of ioctls (hw_params, sw_params,
3328 * prepare, start, drain, drop, forward).
3329 */
3330int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
3331                         unsigned int cmd, void *arg)
3332{
3333        snd_pcm_uframes_t *frames = arg;
3334        snd_pcm_sframes_t result;
3335        
3336        switch (cmd) {
3337        case SNDRV_PCM_IOCTL_FORWARD:
3338        {
3339                /* provided only for OSS; capture-only and no value returned */
3340                if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
3341                        return -EINVAL;
3342                result = snd_pcm_forward(substream, *frames);
3343                return result < 0 ? result : 0;
3344        }
3345        case SNDRV_PCM_IOCTL_HW_PARAMS:
3346                return snd_pcm_hw_params(substream, arg);
3347        case SNDRV_PCM_IOCTL_SW_PARAMS:
3348                return snd_pcm_sw_params(substream, arg);
3349        case SNDRV_PCM_IOCTL_PREPARE:
3350                return snd_pcm_prepare(substream, NULL);
3351        case SNDRV_PCM_IOCTL_START:
3352                return snd_pcm_start_lock_irq(substream);
3353        case SNDRV_PCM_IOCTL_DRAIN:
3354                return snd_pcm_drain(substream, NULL);
3355        case SNDRV_PCM_IOCTL_DROP:
3356                return snd_pcm_drop(substream);
3357        case SNDRV_PCM_IOCTL_DELAY:
3358                return snd_pcm_delay(substream, frames);
3359        default:
3360                return -EINVAL;
3361        }
3362}
3363EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3364
3365static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3366                            loff_t * offset)
3367{
3368        struct snd_pcm_file *pcm_file;
3369        struct snd_pcm_substream *substream;
3370        struct snd_pcm_runtime *runtime;
3371        snd_pcm_sframes_t result;
3372
3373        pcm_file = file->private_data;
3374        substream = pcm_file->substream;
3375        if (PCM_RUNTIME_CHECK(substream))
3376                return -ENXIO;
3377        runtime = substream->runtime;
3378        if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3379                return -EBADFD;
3380        if (!frame_aligned(runtime, count))
3381                return -EINVAL;
3382        count = bytes_to_frames(runtime, count);
3383        result = snd_pcm_lib_read(substream, buf, count);
3384        if (result > 0)
3385                result = frames_to_bytes(runtime, result);
3386        return result;
3387}
3388
3389static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3390                             size_t count, loff_t * offset)
3391{
3392        struct snd_pcm_file *pcm_file;
3393        struct snd_pcm_substream *substream;
3394        struct snd_pcm_runtime *runtime;
3395        snd_pcm_sframes_t result;
3396
3397        pcm_file = file->private_data;
3398        substream = pcm_file->substream;
3399        if (PCM_RUNTIME_CHECK(substream))
3400                return -ENXIO;
3401        runtime = substream->runtime;
3402        if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3403                return -EBADFD;
3404        if (!frame_aligned(runtime, count))
3405                return -EINVAL;
3406        count = bytes_to_frames(runtime, count);
3407        result = snd_pcm_lib_write(substream, buf, count);
3408        if (result > 0)
3409                result = frames_to_bytes(runtime, result);
3410        return result;
3411}
3412
3413static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
3414{
3415        struct snd_pcm_file *pcm_file;
3416        struct snd_pcm_substream *substream;
3417        struct snd_pcm_runtime *runtime;
3418        snd_pcm_sframes_t result;
3419        unsigned long i;
3420        void __user **bufs;
3421        snd_pcm_uframes_t frames;
3422
3423        pcm_file = iocb->ki_filp->private_data;
3424        substream = pcm_file->substream;
3425        if (PCM_RUNTIME_CHECK(substream))
3426                return -ENXIO;
3427        runtime = substream->runtime;
3428        if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3429                return -EBADFD;
3430        if (!iter_is_iovec(to))
3431                return -EINVAL;
3432        if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
3433                return -EINVAL;
3434        if (!frame_aligned(runtime, to->iov->iov_len))
3435                return -EINVAL;
3436        frames = bytes_to_samples(runtime, to->iov->iov_len);
3437        bufs = kmalloc_array(to->nr_segs, sizeof(void *), GFP_KERNEL);
3438        if (bufs == NULL)
3439                return -ENOMEM;
3440        for (i = 0; i < to->nr_segs; ++i)
3441                bufs[i] = to->iov[i].iov_base;
3442        result = snd_pcm_lib_readv(substream, bufs, frames);
3443        if (result > 0)
3444                result = frames_to_bytes(runtime, result);
3445        kfree(bufs);
3446        return result;
3447}
3448
3449static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
3450{
3451        struct snd_pcm_file *pcm_file;
3452        struct snd_pcm_substream *substream;
3453        struct snd_pcm_runtime *runtime;
3454        snd_pcm_sframes_t result;
3455        unsigned long i;
3456        void __user **bufs;
3457        snd_pcm_uframes_t frames;
3458
3459        pcm_file = iocb->ki_filp->private_data;
3460        substream = pcm_file->substream;
3461        if (PCM_RUNTIME_CHECK(substream))
3462                return -ENXIO;
3463        runtime = substream->runtime;
3464        if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3465                return -EBADFD;
3466        if (!iter_is_iovec(from))
3467                return -EINVAL;
3468        if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3469            !frame_aligned(runtime, from->iov->iov_len))
3470                return -EINVAL;
3471        frames = bytes_to_samples(runtime, from->iov->iov_len);
3472        bufs = kmalloc_array(from->nr_segs, sizeof(void *), GFP_KERNEL);
3473        if (bufs == NULL)
3474                return -ENOMEM;
3475        for (i = 0; i < from->nr_segs; ++i)
3476                bufs[i] = from->iov[i].iov_base;
3477        result = snd_pcm_lib_writev(substream, bufs, frames);
3478        if (result > 0)
3479                result = frames_to_bytes(runtime, result);
3480        kfree(bufs);
3481        return result;
3482}
3483
3484static __poll_t snd_pcm_poll(struct file *file, poll_table *wait)
3485{
3486        struct snd_pcm_file *pcm_file;
3487        struct snd_pcm_substream *substream;
3488        struct snd_pcm_runtime *runtime;
3489        __poll_t mask, ok;
3490        snd_pcm_uframes_t avail;
3491
3492        pcm_file = file->private_data;
3493
3494        substream = pcm_file->substream;
3495        if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
3496                ok = EPOLLOUT | EPOLLWRNORM;
3497        else
3498                ok = EPOLLIN | EPOLLRDNORM;
3499        if (PCM_RUNTIME_CHECK(substream))
3500                return ok | EPOLLERR;
3501
3502        runtime = substream->runtime;
3503        poll_wait(file, &runtime->sleep, wait);
3504
3505        mask = 0;
3506        snd_pcm_stream_lock_irq(substream);
3507        avail = snd_pcm_avail(substream);
3508        switch (runtime->status->state) {
3509        case SNDRV_PCM_STATE_RUNNING:
3510        case SNDRV_PCM_STATE_PREPARED:
3511        case SNDRV_PCM_STATE_PAUSED:
3512                if (avail >= runtime->control->avail_min)
3513                        mask = ok;
3514                break;
3515        case SNDRV_PCM_STATE_DRAINING:
3516                if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
3517                        mask = ok;
3518                        if (!avail)
3519                                mask |= EPOLLERR;
3520                }
3521                break;
3522        default:
3523                mask = ok | EPOLLERR;
3524                break;
3525        }
3526        snd_pcm_stream_unlock_irq(substream);
3527        return mask;
3528}
3529
3530/*
3531 * mmap support
3532 */
3533
3534/*
3535 * Only on coherent architectures, we can mmap the status and the control records
3536 * for effcient data transfer.  On others, we have to use HWSYNC ioctl...
3537 */
3538#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3539/*
3540 * mmap status record
3541 */
3542static vm_fault_t snd_pcm_mmap_status_fault(struct vm_fault *vmf)
3543{
3544        struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3545        struct snd_pcm_runtime *runtime;
3546        
3547        if (substream == NULL)
3548                return VM_FAULT_SIGBUS;
3549        runtime = substream->runtime;
3550        vmf->page = virt_to_page(runtime->status);
3551        get_page(vmf->page);
3552        return 0;
3553}
3554
3555static const struct vm_operations_struct snd_pcm_vm_ops_status =
3556{
3557        .fault =        snd_pcm_mmap_status_fault,
3558};
3559
3560static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3561                               struct vm_area_struct *area)
3562{
3563        long size;
3564        if (!(area->vm_flags & VM_READ))
3565                return -EINVAL;
3566        size = area->vm_end - area->vm_start;
3567        if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3568                return -EINVAL;
3569        area->vm_ops = &snd_pcm_vm_ops_status;
3570        area->vm_private_data = substream;
3571        area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3572        return 0;
3573}
3574
3575/*
3576 * mmap control record
3577 */
3578static vm_fault_t snd_pcm_mmap_control_fault(struct vm_fault *vmf)
3579{
3580        struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3581        struct snd_pcm_runtime *runtime;
3582        
3583        if (substream == NULL)
3584                return VM_FAULT_SIGBUS;
3585        runtime = substream->runtime;
3586        vmf->page = virt_to_page(runtime->control);
3587        get_page(vmf->page);
3588        return 0;
3589}
3590
3591static const struct vm_operations_struct snd_pcm_vm_ops_control =
3592{
3593        .fault =        snd_pcm_mmap_control_fault,
3594};
3595
3596static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3597                                struct vm_area_struct *area)
3598{
3599        long size;
3600        if (!(area->vm_flags & VM_READ))
3601                return -EINVAL;
3602        size = area->vm_end - area->vm_start;
3603        if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3604                return -EINVAL;
3605        area->vm_ops = &snd_pcm_vm_ops_control;
3606        area->vm_private_data = substream;
3607        area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3608        return 0;
3609}
3610
3611static bool pcm_status_mmap_allowed(struct snd_pcm_file *pcm_file)
3612{
3613        /* See pcm_control_mmap_allowed() below.
3614         * Since older alsa-lib requires both status and control mmaps to be
3615         * coupled, we have to disable the status mmap for old alsa-lib, too.
3616         */
3617        if (pcm_file->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 14) &&
3618            (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR))
3619                return false;
3620        return true;
3621}
3622
3623static bool pcm_control_mmap_allowed(struct snd_pcm_file *pcm_file)
3624{
3625        if (pcm_file->no_compat_mmap)
3626                return false;
3627        /* Disallow the control mmap when SYNC_APPLPTR flag is set;
3628         * it enforces the user-space to fall back to snd_pcm_sync_ptr(),
3629         * thus it effectively assures the manual update of appl_ptr.
3630         */
3631        if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR)
3632                return false;
3633        return true;
3634}
3635
3636#else /* ! coherent mmap */
3637/*
3638 * don't support mmap for status and control records.
3639 */
3640#define pcm_status_mmap_allowed(pcm_file)       false
3641#define pcm_control_mmap_allowed(pcm_file)      false
3642
3643static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3644                               struct vm_area_struct *area)
3645{
3646        return -ENXIO;
3647}
3648static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3649                                struct vm_area_struct *area)
3650{
3651        return -ENXIO;
3652}
3653#endif /* coherent mmap */
3654
3655static inline struct page *
3656snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3657{
3658        void *vaddr = substream->runtime->dma_area + ofs;
3659
3660        switch (substream->dma_buffer.dev.type) {
3661#ifdef CONFIG_SND_DMA_SGBUF
3662        case SNDRV_DMA_TYPE_DEV_SG:
3663        case SNDRV_DMA_TYPE_DEV_UC_SG:
3664                return snd_pcm_sgbuf_ops_page(substream, ofs);
3665#endif /* CONFIG_SND_DMA_SGBUF */
3666        case SNDRV_DMA_TYPE_VMALLOC:
3667                return vmalloc_to_page(vaddr);
3668        default:
3669                return virt_to_page(vaddr);
3670        }
3671}
3672
3673/*
3674 * fault callback for mmapping a RAM page
3675 */
3676static vm_fault_t snd_pcm_mmap_data_fault(struct vm_fault *vmf)
3677{
3678        struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3679        struct snd_pcm_runtime *runtime;
3680        unsigned long offset;
3681        struct page * page;
3682        size_t dma_bytes;
3683        
3684        if (substream == NULL)
3685                return VM_FAULT_SIGBUS;
3686        runtime = substream->runtime;
3687        offset = vmf->pgoff << PAGE_SHIFT;
3688        dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3689        if (offset > dma_bytes - PAGE_SIZE)
3690                return VM_FAULT_SIGBUS;
3691        if (substream->ops->page)
3692                page = substream->ops->page(substream, offset);
3693        else
3694                page = snd_pcm_default_page_ops(substream, offset);
3695        if (!page)
3696                return VM_FAULT_SIGBUS;
3697        get_page(page);
3698        vmf->page = page;
3699        return 0;
3700}
3701
3702static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3703        .open =         snd_pcm_mmap_data_open,
3704        .close =        snd_pcm_mmap_data_close,
3705};
3706
3707static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3708        .open =         snd_pcm_mmap_data_open,
3709        .close =        snd_pcm_mmap_data_close,
3710        .fault =        snd_pcm_mmap_data_fault,
3711};
3712
3713/*
3714 * mmap the DMA buffer on RAM
3715 */
3716
3717/**
3718 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3719 * @substream: PCM substream
3720 * @area: VMA
3721 *
3722 * This is the default mmap handler for PCM data.  When mmap pcm_ops is NULL,
3723 * this function is invoked implicitly.
3724 */
3725int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3726                             struct vm_area_struct *area)
3727{
3728        area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3729#ifdef CONFIG_GENERIC_ALLOCATOR
3730        if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3731                area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3732                return remap_pfn_range(area, area->vm_start,
3733                                substream->dma_buffer.addr >> PAGE_SHIFT,
3734                                area->vm_end - area->vm_start, area->vm_page_prot);
3735        }
3736#endif /* CONFIG_GENERIC_ALLOCATOR */
3737        if (IS_ENABLED(CONFIG_HAS_DMA) && !substream->ops->page &&
3738            (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV ||
3739             substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_UC))
3740                return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3741                                         area,
3742                                         substream->runtime->dma_area,
3743                                         substream->runtime->dma_addr,
3744                                         substream->runtime->dma_bytes);
3745        /* mmap with fault handler */
3746        area->vm_ops = &snd_pcm_vm_ops_data_fault;
3747        return 0;
3748}
3749EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
3750
3751/*
3752 * mmap the DMA buffer on I/O memory area
3753 */
3754#if SNDRV_PCM_INFO_MMAP_IOMEM
3755/**
3756 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3757 * @substream: PCM substream
3758 * @area: VMA
3759 *
3760 * When your hardware uses the iomapped pages as the hardware buffer and
3761 * wants to mmap it, pass this function as mmap pcm_ops.  Note that this
3762 * is supposed to work only on limited architectures.
3763 */
3764int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3765                           struct vm_area_struct *area)
3766{
3767        struct snd_pcm_runtime *runtime = substream->runtime;
3768
3769        area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3770        return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
3771}
3772EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3773#endif /* SNDRV_PCM_INFO_MMAP */
3774
3775/*
3776 * mmap DMA buffer
3777 */
3778int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3779                      struct vm_area_struct *area)
3780{
3781        struct snd_pcm_runtime *runtime;
3782        long size;
3783        unsigned long offset;
3784        size_t dma_bytes;
3785        int err;
3786
3787        if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3788                if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3789                        return -EINVAL;
3790        } else {
3791                if (!(area->vm_flags & VM_READ))
3792                        return -EINVAL;
3793        }
3794        runtime = substream->runtime;
3795        if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3796                return -EBADFD;
3797        if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3798                return -ENXIO;
3799        if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3800            runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3801                return -EINVAL;
3802        size = area->vm_end - area->vm_start;
3803        offset = area->vm_pgoff << PAGE_SHIFT;
3804        dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3805        if ((size_t)size > dma_bytes)
3806                return -EINVAL;
3807        if (offset > dma_bytes - size)
3808                return -EINVAL;
3809
3810        area->vm_ops = &snd_pcm_vm_ops_data;
3811        area->vm_private_data = substream;
3812        if (substream->ops->mmap)
3813                err = substream->ops->mmap(substream, area);
3814        else
3815                err = snd_pcm_lib_default_mmap(substream, area);
3816        if (!err)
3817                atomic_inc(&substream->mmap_count);
3818        return err;
3819}
3820EXPORT_SYMBOL(snd_pcm_mmap_data);
3821
3822static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3823{
3824        struct snd_pcm_file * pcm_file;
3825        struct snd_pcm_substream *substream;    
3826        unsigned long offset;
3827        
3828        pcm_file = file->private_data;
3829        substream = pcm_file->substream;
3830        if (PCM_RUNTIME_CHECK(substream))
3831                return -ENXIO;
3832
3833        offset = area->vm_pgoff << PAGE_SHIFT;
3834        switch (offset) {
3835        case SNDRV_PCM_MMAP_OFFSET_STATUS_OLD:
3836                if (pcm_file->no_compat_mmap || !IS_ENABLED(CONFIG_64BIT))
3837                        return -ENXIO;
3838                fallthrough;
3839        case SNDRV_PCM_MMAP_OFFSET_STATUS_NEW:
3840                if (!pcm_status_mmap_allowed(pcm_file))
3841                        return -ENXIO;
3842                return snd_pcm_mmap_status(substream, file, area);
3843        case SNDRV_PCM_MMAP_OFFSET_CONTROL_OLD:
3844                if (pcm_file->no_compat_mmap || !IS_ENABLED(CONFIG_64BIT))
3845                        return -ENXIO;
3846                fallthrough;
3847        case SNDRV_PCM_MMAP_OFFSET_CONTROL_NEW:
3848                if (!pcm_control_mmap_allowed(pcm_file))
3849                        return -ENXIO;
3850                return snd_pcm_mmap_control(substream, file, area);
3851        default:
3852                return snd_pcm_mmap_data(substream, file, area);
3853        }
3854        return 0;
3855}
3856
3857static int snd_pcm_fasync(int fd, struct file * file, int on)
3858{
3859        struct snd_pcm_file * pcm_file;
3860        struct snd_pcm_substream *substream;
3861        struct snd_pcm_runtime *runtime;
3862
3863        pcm_file = file->private_data;
3864        substream = pcm_file->substream;
3865        if (PCM_RUNTIME_CHECK(substream))
3866                return -ENXIO;
3867        runtime = substream->runtime;
3868        return fasync_helper(fd, file, on, &runtime->fasync);
3869}
3870
3871/*
3872 * ioctl32 compat
3873 */
3874#ifdef CONFIG_COMPAT
3875#include "pcm_compat.c"
3876#else
3877#define snd_pcm_ioctl_compat    NULL
3878#endif
3879
3880/*
3881 *  To be removed helpers to keep binary compatibility
3882 */
3883
3884#ifdef CONFIG_SND_SUPPORT_OLD_API
3885#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3886#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3887
3888static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3889                                               struct snd_pcm_hw_params_old *oparams)
3890{
3891        unsigned int i;
3892
3893        memset(params, 0, sizeof(*params));
3894        params->flags = oparams->flags;
3895        for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3896                params->masks[i].bits[0] = oparams->masks[i];
3897        memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3898        params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3899        params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3900        params->info = oparams->info;
3901        params->msbits = oparams->msbits;
3902        params->rate_num = oparams->rate_num;
3903        params->rate_den = oparams->rate_den;
3904        params->fifo_size = oparams->fifo_size;
3905}
3906
3907static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3908                                             struct snd_pcm_hw_params *params)
3909{
3910        unsigned int i;
3911
3912        memset(oparams, 0, sizeof(*oparams));
3913        oparams->flags = params->flags;
3914        for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3915                oparams->masks[i] = params->masks[i].bits[0];
3916        memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3917        oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3918        oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3919        oparams->info = params->info;
3920        oparams->msbits = params->msbits;
3921        oparams->rate_num = params->rate_num;
3922        oparams->rate_den = params->rate_den;
3923        oparams->fifo_size = params->fifo_size;
3924}
3925
3926static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3927                                      struct snd_pcm_hw_params_old __user * _oparams)
3928{
3929        struct snd_pcm_hw_params *params;
3930        struct snd_pcm_hw_params_old *oparams = NULL;
3931        int err;
3932
3933        params = kmalloc(sizeof(*params), GFP_KERNEL);
3934        if (!params)
3935                return -ENOMEM;
3936
3937        oparams = memdup_user(_oparams, sizeof(*oparams));
3938        if (IS_ERR(oparams)) {
3939                err = PTR_ERR(oparams);
3940                goto out;
3941        }
3942        snd_pcm_hw_convert_from_old_params(params, oparams);
3943        err = snd_pcm_hw_refine(substream, params);
3944        if (err < 0)
3945                goto out_old;
3946
3947        err = fixup_unreferenced_params(substream, params);
3948        if (err < 0)
3949                goto out_old;
3950
3951        snd_pcm_hw_convert_to_old_params(oparams, params);
3952        if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3953                err = -EFAULT;
3954out_old:
3955        kfree(oparams);
3956out:
3957        kfree(params);
3958        return err;
3959}
3960
3961static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3962                                      struct snd_pcm_hw_params_old __user * _oparams)
3963{
3964        struct snd_pcm_hw_params *params;
3965        struct snd_pcm_hw_params_old *oparams = NULL;
3966        int err;
3967
3968        params = kmalloc(sizeof(*params), GFP_KERNEL);
3969        if (!params)
3970                return -ENOMEM;
3971
3972        oparams = memdup_user(_oparams, sizeof(*oparams));
3973        if (IS_ERR(oparams)) {
3974                err = PTR_ERR(oparams);
3975                goto out;
3976        }
3977
3978        snd_pcm_hw_convert_from_old_params(params, oparams);
3979        err = snd_pcm_hw_params(substream, params);
3980        if (err < 0)
3981                goto out_old;
3982
3983        snd_pcm_hw_convert_to_old_params(oparams, params);
3984        if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
3985                err = -EFAULT;
3986out_old:
3987        kfree(oparams);
3988out:
3989        kfree(params);
3990        return err;
3991}
3992#endif /* CONFIG_SND_SUPPORT_OLD_API */
3993
3994#ifndef CONFIG_MMU
3995static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3996                                               unsigned long addr,
3997                                               unsigned long len,
3998                                               unsigned long pgoff,
3999                                               unsigned long flags)
4000{
4001        struct snd_pcm_file *pcm_file = file->private_data;
4002        struct snd_pcm_substream *substream = pcm_file->substream;
4003        struct snd_pcm_runtime *runtime = substream->runtime;
4004        unsigned long offset = pgoff << PAGE_SHIFT;
4005
4006        switch (offset) {
4007        case SNDRV_PCM_MMAP_OFFSET_STATUS_NEW:
4008                return (unsigned long)runtime->status;
4009        case SNDRV_PCM_MMAP_OFFSET_CONTROL_NEW:
4010                return (unsigned long)runtime->control;
4011        default:
4012                return (unsigned long)runtime->dma_area + offset;
4013        }
4014}
4015#else
4016# define snd_pcm_get_unmapped_area NULL
4017#endif
4018
4019/*
4020 *  Register section
4021 */
4022
4023const struct file_operations snd_pcm_f_ops[2] = {
4024        {
4025                .owner =                THIS_MODULE,
4026                .write =                snd_pcm_write,
4027                .write_iter =           snd_pcm_writev,
4028                .open =                 snd_pcm_playback_open,
4029                .release =              snd_pcm_release,
4030                .llseek =               no_llseek,
4031                .poll =                 snd_pcm_poll,
4032                .unlocked_ioctl =       snd_pcm_ioctl,
4033                .compat_ioctl =         snd_pcm_ioctl_compat,
4034                .mmap =                 snd_pcm_mmap,
4035                .fasync =               snd_pcm_fasync,
4036                .get_unmapped_area =    snd_pcm_get_unmapped_area,
4037        },
4038        {
4039                .owner =                THIS_MODULE,
4040                .read =                 snd_pcm_read,
4041                .read_iter =            snd_pcm_readv,
4042                .open =                 snd_pcm_capture_open,
4043                .release =              snd_pcm_release,
4044                .llseek =               no_llseek,
4045                .poll =                 snd_pcm_poll,
4046                .unlocked_ioctl =       snd_pcm_ioctl,
4047                .compat_ioctl =         snd_pcm_ioctl_compat,
4048                .mmap =                 snd_pcm_mmap,
4049                .fasync =               snd_pcm_fasync,
4050                .get_unmapped_area =    snd_pcm_get_unmapped_area,
4051        }
4052};
4053