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