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