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