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