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