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