OSDN Git Service

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