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