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