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