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