OSDN Git Service

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