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