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