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