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