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