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