ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 * _oparams);
60 static int snd_pcm_hw_params_old_user(snd_pcm_substream_t * substream, struct sndrv_pcm_hw_params_old * _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 * _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 * _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 * _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 * _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 * _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 * _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                 return 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 *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_playback_ioctl1(snd_pcm_substream_t *substream,
2339                                    unsigned int cmd, void *arg);
2340 static int snd_pcm_capture_ioctl1(snd_pcm_substream_t *substream,
2341                                   unsigned int cmd, void *arg);
2342
2343 static int snd_pcm_common_ioctl1(snd_pcm_substream_t *substream,
2344                                  unsigned int cmd, void *arg)
2345 {
2346         snd_assert(substream != NULL, return -ENXIO);
2347
2348         switch (cmd) {
2349         case SNDRV_PCM_IOCTL_PVERSION:
2350                 return put_user(SNDRV_PCM_VERSION, (int *)arg) ? -EFAULT : 0;
2351         case SNDRV_PCM_IOCTL_INFO:
2352                 return snd_pcm_info_user(substream, (snd_pcm_info_t *) arg);
2353         case SNDRV_PCM_IOCTL_TSTAMP:
2354         {
2355                 int xarg;
2356                 if (get_user(xarg, (int *) arg))
2357                         return -EFAULT;
2358                 substream->runtime->tstamp_timespec = xarg ? 1 : 0;
2359                 return 0;
2360         }
2361         case SNDRV_PCM_IOCTL_HW_REFINE:
2362                 return snd_pcm_hw_refine_user(substream, (snd_pcm_hw_params_t *) arg);
2363         case SNDRV_PCM_IOCTL_HW_PARAMS:
2364                 return snd_pcm_hw_params_user(substream, (snd_pcm_hw_params_t *) arg);
2365         case SNDRV_PCM_IOCTL_HW_FREE:
2366                 return snd_pcm_hw_free(substream);
2367         case SNDRV_PCM_IOCTL_SW_PARAMS:
2368                 return snd_pcm_sw_params_user(substream, (snd_pcm_sw_params_t *) arg);
2369         case SNDRV_PCM_IOCTL_STATUS:
2370                 return snd_pcm_status_user(substream, (snd_pcm_status_t *) arg);
2371         case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2372                 return snd_pcm_channel_info(substream, (snd_pcm_channel_info_t *) arg);
2373         case SNDRV_PCM_IOCTL_PREPARE:
2374                 return snd_pcm_prepare(substream);
2375         case SNDRV_PCM_IOCTL_RESET:
2376                 return snd_pcm_reset(substream);
2377         case SNDRV_PCM_IOCTL_START:
2378                 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, 0, 0);
2379         case SNDRV_PCM_IOCTL_LINK:
2380                 return snd_pcm_link(substream, (long) arg);
2381         case SNDRV_PCM_IOCTL_UNLINK:
2382                 return snd_pcm_unlink(substream);
2383         case SNDRV_PCM_IOCTL_RESUME:
2384                 return snd_pcm_resume(substream);
2385         case SNDRV_PCM_IOCTL_XRUN:
2386                 return snd_pcm_xrun(substream);
2387         case SNDRV_PCM_IOCTL_HWSYNC:
2388                 return snd_pcm_hwsync(substream);
2389         case SNDRV_PCM_IOCTL_DELAY:
2390                 return snd_pcm_delay(substream, (snd_pcm_sframes_t *) arg);
2391         case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2392                 return snd_pcm_hw_refine_old_user(substream, (struct sndrv_pcm_hw_params_old *) arg);
2393         case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2394                 return snd_pcm_hw_params_old_user(substream, (struct sndrv_pcm_hw_params_old *) arg);
2395         }
2396         snd_printd("unknown ioctl = 0x%x\n", cmd);
2397         return -ENOTTY;
2398 }
2399
2400 static int snd_pcm_playback_ioctl1(snd_pcm_substream_t *substream,
2401                                    unsigned int cmd, void *arg)
2402 {
2403         snd_assert(substream != NULL, return -ENXIO);
2404         snd_assert(substream->stream == SNDRV_PCM_STREAM_PLAYBACK, return -EINVAL);
2405         switch (cmd) {
2406         case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2407         {
2408                 snd_xferi_t xferi, *_xferi = arg;
2409                 snd_pcm_runtime_t *runtime = substream->runtime;
2410                 snd_pcm_sframes_t result;
2411                 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2412                         return -EBADFD;
2413                 if (put_user(0, &_xferi->result))
2414                         return -EFAULT;
2415                 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2416                         return -EFAULT;
2417                 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2418                 __put_user(result, &_xferi->result);
2419                 return result < 0 ? result : 0;
2420         }
2421         case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2422         {
2423                 snd_xfern_t xfern, *_xfern = arg;
2424                 snd_pcm_runtime_t *runtime = substream->runtime;
2425                 void *bufs;
2426                 snd_pcm_sframes_t result;
2427                 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2428                         return -EBADFD;
2429                 if (runtime->channels > 128)
2430                         return -EINVAL;
2431                 if (put_user(0, &_xfern->result))
2432                         return -EFAULT;
2433                 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2434                         return -EFAULT;
2435                 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2436                 if (bufs == NULL)
2437                         return -ENOMEM;
2438                 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2439                         kfree(bufs);
2440                         return -EFAULT;
2441                 }
2442                 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2443                 kfree(bufs);
2444                 __put_user(result, &_xfern->result);
2445                 return result < 0 ? result : 0;
2446         }
2447         case SNDRV_PCM_IOCTL_REWIND:
2448         {
2449                 snd_pcm_uframes_t frames, *_frames = arg;
2450                 snd_pcm_sframes_t result;
2451                 if (get_user(frames, _frames))
2452                         return -EFAULT;
2453                 if (put_user(0, _frames))
2454                         return -EFAULT;
2455                 result = snd_pcm_playback_rewind(substream, frames);
2456                 __put_user(result, _frames);
2457                 return result < 0 ? result : 0;
2458         }
2459         case SNDRV_PCM_IOCTL_FORWARD:
2460         {
2461                 snd_pcm_uframes_t frames, *_frames = arg;
2462                 snd_pcm_sframes_t result;
2463                 if (get_user(frames, _frames))
2464                         return -EFAULT;
2465                 if (put_user(0, _frames))
2466                         return -EFAULT;
2467                 result = snd_pcm_playback_forward(substream, frames);
2468                 __put_user(result, _frames);
2469                 return result < 0 ? result : 0;
2470         }
2471         case SNDRV_PCM_IOCTL_PAUSE:
2472         {
2473                 int res;
2474                 snd_pcm_stream_lock_irq(substream);
2475                 res = snd_pcm_pause(substream, (long) arg);
2476                 snd_pcm_stream_unlock_irq(substream);
2477                 return res;
2478         }
2479         case SNDRV_PCM_IOCTL_DRAIN:
2480                 return snd_pcm_playback_drain(substream);
2481         case SNDRV_PCM_IOCTL_DROP:
2482                 return snd_pcm_playback_drop(substream);
2483         }
2484         return snd_pcm_common_ioctl1(substream, cmd, arg);
2485 }
2486
2487 static int snd_pcm_capture_ioctl1(snd_pcm_substream_t *substream,
2488                                   unsigned int cmd, void *arg)
2489 {
2490         snd_assert(substream != NULL, return -ENXIO);
2491         snd_assert(substream->stream == SNDRV_PCM_STREAM_CAPTURE, return -EINVAL);
2492         switch (cmd) {
2493         case SNDRV_PCM_IOCTL_READI_FRAMES:
2494         {
2495                 snd_xferi_t xferi, *_xferi = arg;
2496                 snd_pcm_runtime_t *runtime = substream->runtime;
2497                 snd_pcm_sframes_t result;
2498                 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2499                         return -EBADFD;
2500                 if (put_user(0, &_xferi->result))
2501                         return -EFAULT;
2502                 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2503                         return -EFAULT;
2504                 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2505                 __put_user(result, &_xferi->result);
2506                 return result < 0 ? result : 0;
2507         }
2508         case SNDRV_PCM_IOCTL_READN_FRAMES:
2509         {
2510                 snd_xfern_t xfern, *_xfern = arg;
2511                 snd_pcm_runtime_t *runtime = substream->runtime;
2512                 void *bufs;
2513                 snd_pcm_sframes_t result;
2514                 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2515                         return -EBADFD;
2516                 if (runtime->channels > 128)
2517                         return -EINVAL;
2518                 if (put_user(0, &_xfern->result))
2519                         return -EFAULT;
2520                 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2521                         return -EFAULT;
2522                 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2523                 if (bufs == NULL)
2524                         return -ENOMEM;
2525                 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2526                         kfree(bufs);
2527                         return -EFAULT;
2528                 }
2529                 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2530                 kfree(bufs);
2531                 __put_user(result, &_xfern->result);
2532                 return result < 0 ? result : 0;
2533         }
2534         case SNDRV_PCM_IOCTL_REWIND:
2535         {
2536                 snd_pcm_uframes_t frames, *_frames = arg;
2537                 snd_pcm_sframes_t result;
2538                 if (get_user(frames, _frames))
2539                         return -EFAULT;
2540                 if (put_user(0, _frames))
2541                         return -EFAULT;
2542                 result = snd_pcm_capture_rewind(substream, frames);
2543                 __put_user(result, _frames);
2544                 return result < 0 ? result : 0;
2545         }
2546         case SNDRV_PCM_IOCTL_FORWARD:
2547         {
2548                 snd_pcm_uframes_t frames, *_frames = arg;
2549                 snd_pcm_sframes_t result;
2550                 if (get_user(frames, _frames))
2551                         return -EFAULT;
2552                 if (put_user(0, _frames))
2553                         return -EFAULT;
2554                 result = snd_pcm_capture_forward(substream, frames);
2555                 __put_user(result, _frames);
2556                 return result < 0 ? result : 0;
2557         }
2558         case SNDRV_PCM_IOCTL_DRAIN:
2559                 return snd_pcm_capture_drain(substream);
2560         case SNDRV_PCM_IOCTL_DROP:
2561                 return snd_pcm_capture_drop(substream);
2562         }
2563         return snd_pcm_common_ioctl1(substream, cmd, arg);
2564 }
2565
2566 static int snd_pcm_playback_ioctl(struct inode *inode, struct file *file,
2567                                   unsigned int cmd, unsigned long arg)
2568 {
2569         snd_pcm_file_t *pcm_file;
2570
2571         pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return -ENXIO);
2572
2573         if (((cmd >> 8) & 0xff) != 'A')
2574                 return -ENOTTY;
2575
2576         return snd_pcm_playback_ioctl1(pcm_file->substream, cmd, (void *) arg);
2577 }
2578
2579 static int snd_pcm_capture_ioctl(struct inode *inode, struct file *file,
2580                                  unsigned int cmd, unsigned long arg)
2581 {
2582         snd_pcm_file_t *pcm_file;
2583
2584         pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return -ENXIO);
2585
2586         if (((cmd >> 8) & 0xff) != 'A')
2587                 return -ENOTTY;
2588
2589         return snd_pcm_capture_ioctl1(pcm_file->substream, cmd, (void *) arg);
2590 }
2591
2592 int snd_pcm_kernel_playback_ioctl(snd_pcm_substream_t *substream,
2593                                   unsigned int cmd, void *arg)
2594 {
2595         mm_segment_t fs;
2596         int result;
2597         
2598         fs = snd_enter_user();
2599         result = snd_pcm_playback_ioctl1(substream, cmd, arg);
2600         snd_leave_user(fs);
2601         return result;
2602 }
2603
2604 int snd_pcm_kernel_capture_ioctl(snd_pcm_substream_t *substream,
2605                                  unsigned int cmd, void *arg)
2606 {
2607         mm_segment_t fs;
2608         int result;
2609         
2610         fs = snd_enter_user();
2611         result = snd_pcm_capture_ioctl1(substream, cmd, arg);
2612         snd_leave_user(fs);
2613         return result;
2614 }
2615
2616 int snd_pcm_kernel_ioctl(snd_pcm_substream_t *substream,
2617                          unsigned int cmd, void *arg)
2618 {
2619         switch (substream->stream) {
2620         case SNDRV_PCM_STREAM_PLAYBACK:
2621                 return snd_pcm_kernel_playback_ioctl(substream, cmd, arg);
2622         case SNDRV_PCM_STREAM_CAPTURE:
2623                 return snd_pcm_kernel_capture_ioctl(substream, cmd, arg);
2624         default:
2625                 return -EINVAL;
2626         }
2627 }
2628
2629 static ssize_t snd_pcm_read(struct file *file, char *buf, size_t count, loff_t * offset)
2630 {
2631         snd_pcm_file_t *pcm_file;
2632         snd_pcm_substream_t *substream;
2633         snd_pcm_runtime_t *runtime;
2634         snd_pcm_sframes_t result;
2635
2636         pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return -ENXIO);
2637         substream = pcm_file->substream;
2638         snd_assert(substream != NULL, return -ENXIO);
2639         runtime = substream->runtime;
2640         if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2641                 return -EBADFD;
2642         if (!frame_aligned(runtime, count))
2643                 return -EINVAL;
2644         count = bytes_to_frames(runtime, count);
2645         result = snd_pcm_lib_read(substream, buf, count);
2646         if (result > 0)
2647                 result = frames_to_bytes(runtime, result);
2648         return result;
2649 }
2650
2651 static ssize_t snd_pcm_write(struct file *file, const char *buf, size_t count, loff_t * offset)
2652 {
2653         snd_pcm_file_t *pcm_file;
2654         snd_pcm_substream_t *substream;
2655         snd_pcm_runtime_t *runtime;
2656         snd_pcm_sframes_t result;
2657
2658         pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, result = -ENXIO; goto end);
2659         substream = pcm_file->substream;
2660         snd_assert(substream != NULL, result = -ENXIO; goto end);
2661         runtime = substream->runtime;
2662         if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2663                 result = -EBADFD;
2664                 goto end;
2665         }
2666         if (!frame_aligned(runtime, count)) {
2667                 result = -EINVAL;
2668                 goto end;
2669         }
2670         count = bytes_to_frames(runtime, count);
2671         result = snd_pcm_lib_write(substream, buf, count);
2672         if (result > 0)
2673                 result = frames_to_bytes(runtime, result);
2674  end:
2675         return result;
2676 }
2677
2678 static ssize_t snd_pcm_readv(struct file *file, const struct iovec *_vector,
2679                              unsigned long count, loff_t * offset)
2680
2681 {
2682         snd_pcm_file_t *pcm_file;
2683         snd_pcm_substream_t *substream;
2684         snd_pcm_runtime_t *runtime;
2685         snd_pcm_sframes_t result;
2686         unsigned long i;
2687         void **bufs;
2688         snd_pcm_uframes_t frames;
2689
2690         pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return -ENXIO);
2691         substream = pcm_file->substream;
2692         snd_assert(substream != NULL, return -ENXIO);
2693         runtime = substream->runtime;
2694         if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2695                 return -EBADFD;
2696         if (count > 1024 || count != runtime->channels)
2697                 return -EINVAL;
2698         if (!frame_aligned(runtime, _vector->iov_len))
2699                 return -EINVAL;
2700         frames = bytes_to_samples(runtime, _vector->iov_len);
2701         bufs = kmalloc(sizeof(void *) * count, GFP_KERNEL);
2702         if (bufs == NULL)
2703                 return -ENOMEM;
2704         for (i = 0; i < count; ++i)
2705                 bufs[i] = _vector[i].iov_base;
2706         result = snd_pcm_lib_readv(substream, bufs, frames);
2707         if (result > 0)
2708                 result = frames_to_bytes(runtime, result);
2709         kfree(bufs);
2710         return result;
2711 }
2712
2713 static ssize_t snd_pcm_writev(struct file *file, const struct iovec *_vector,
2714                               unsigned long count, loff_t * offset)
2715 {
2716         snd_pcm_file_t *pcm_file;
2717         snd_pcm_substream_t *substream;
2718         snd_pcm_runtime_t *runtime;
2719         snd_pcm_sframes_t result;
2720         unsigned long i;
2721         void **bufs;
2722         snd_pcm_uframes_t frames;
2723
2724         pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, result = -ENXIO; goto end);
2725         substream = pcm_file->substream;
2726         snd_assert(substream != NULL, result = -ENXIO; goto end);
2727         runtime = substream->runtime;
2728         if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2729                 result = -EBADFD;
2730                 goto end;
2731         }
2732         if (count > 128 || count != runtime->channels ||
2733             !frame_aligned(runtime, _vector->iov_len)) {
2734                 result = -EINVAL;
2735                 goto end;
2736         }
2737         frames = bytes_to_samples(runtime, _vector->iov_len);
2738         bufs = kmalloc(sizeof(void *) * count, GFP_KERNEL);
2739         if (bufs == NULL)
2740                 return -ENOMEM;
2741         for (i = 0; i < count; ++i)
2742                 bufs[i] = _vector[i].iov_base;
2743         result = snd_pcm_lib_writev(substream, bufs, frames);
2744         if (result > 0)
2745                 result = frames_to_bytes(runtime, result);
2746         kfree(bufs);
2747  end:
2748         return result;
2749 }
2750
2751 unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2752 {
2753         snd_pcm_file_t *pcm_file;
2754         snd_pcm_substream_t *substream;
2755         snd_pcm_runtime_t *runtime;
2756         unsigned int mask;
2757         snd_pcm_uframes_t avail;
2758
2759         pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return 0);
2760
2761         substream = pcm_file->substream;
2762         snd_assert(substream != NULL, return -ENXIO);
2763         runtime = substream->runtime;
2764
2765         poll_wait(file, &runtime->sleep, wait);
2766
2767         snd_pcm_stream_lock_irq(substream);
2768         avail = snd_pcm_playback_avail(runtime);
2769         switch (runtime->status->state) {
2770         case SNDRV_PCM_STATE_RUNNING:
2771         case SNDRV_PCM_STATE_PREPARED:
2772         case SNDRV_PCM_STATE_PAUSED:
2773                 if (avail >= runtime->control->avail_min) {
2774                         mask = POLLOUT | POLLWRNORM;
2775                         break;
2776                 }
2777                 /* Fall through */
2778         case SNDRV_PCM_STATE_DRAINING:
2779                 mask = 0;
2780                 break;
2781         default:
2782                 mask = POLLOUT | POLLWRNORM | POLLERR;
2783                 break;
2784         }
2785         snd_pcm_stream_unlock_irq(substream);
2786         return mask;
2787 }
2788
2789 unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2790 {
2791         snd_pcm_file_t *pcm_file;
2792         snd_pcm_substream_t *substream;
2793         snd_pcm_runtime_t *runtime;
2794         unsigned int mask;
2795         snd_pcm_uframes_t avail;
2796
2797         pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return 0);
2798
2799         substream = pcm_file->substream;
2800         snd_assert(substream != NULL, return -ENXIO);
2801         runtime = substream->runtime;
2802
2803         poll_wait(file, &runtime->sleep, wait);
2804
2805         snd_pcm_stream_lock_irq(substream);
2806         avail = snd_pcm_capture_avail(runtime);
2807         switch (runtime->status->state) {
2808         case SNDRV_PCM_STATE_RUNNING:
2809         case SNDRV_PCM_STATE_PREPARED:
2810         case SNDRV_PCM_STATE_PAUSED:
2811                 if (avail >= runtime->control->avail_min) {
2812                         mask = POLLIN | POLLRDNORM;
2813                         break;
2814                 }
2815                 mask = 0;
2816                 break;
2817         case SNDRV_PCM_STATE_DRAINING:
2818                 if (avail > 0) {
2819                         mask = POLLIN | POLLRDNORM;
2820                         break;
2821                 }
2822                 /* Fall through */
2823         default:
2824                 mask = POLLIN | POLLRDNORM | POLLERR;
2825                 break;
2826         }
2827         snd_pcm_stream_unlock_irq(substream);
2828         return mask;
2829 }
2830
2831 static struct page * snd_pcm_mmap_status_nopage(struct vm_area_struct *area, unsigned long address, int *type)
2832 {
2833         snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
2834         snd_pcm_runtime_t *runtime;
2835         struct page * page;
2836         
2837         if (substream == NULL)
2838                 return NOPAGE_OOM;
2839         runtime = substream->runtime;
2840         page = virt_to_page(runtime->status);
2841         if (!PageReserved(page))
2842                 get_page(page);
2843         if (type)
2844                 *type = VM_FAULT_MINOR;
2845         return page;
2846 }
2847
2848 static struct vm_operations_struct snd_pcm_vm_ops_status =
2849 {
2850         .nopage =       snd_pcm_mmap_status_nopage,
2851 };
2852
2853 int snd_pcm_mmap_status(snd_pcm_substream_t *substream, struct file *file,
2854                         struct vm_area_struct *area)
2855 {
2856         snd_pcm_runtime_t *runtime;
2857         long size;
2858         if (!(area->vm_flags & VM_READ))
2859                 return -EINVAL;
2860         runtime = substream->runtime;
2861         snd_assert(runtime != NULL, return -EAGAIN);
2862         size = area->vm_end - area->vm_start;
2863         if (size != PAGE_ALIGN(sizeof(snd_pcm_mmap_status_t)))
2864                 return -EINVAL;
2865         area->vm_ops = &snd_pcm_vm_ops_status;
2866         area->vm_private_data = substream;
2867         area->vm_flags |= VM_RESERVED;
2868         return 0;
2869 }
2870
2871 static struct page * snd_pcm_mmap_control_nopage(struct vm_area_struct *area, unsigned long address, int *type)
2872 {
2873         snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
2874         snd_pcm_runtime_t *runtime;
2875         struct page * page;
2876         
2877         if (substream == NULL)
2878                 return NOPAGE_OOM;
2879         runtime = substream->runtime;
2880         page = virt_to_page(runtime->control);
2881         if (!PageReserved(page))
2882                 get_page(page);
2883         if (type)
2884                 *type = VM_FAULT_MINOR;
2885         return page;
2886 }
2887
2888 static struct vm_operations_struct snd_pcm_vm_ops_control =
2889 {
2890         .nopage =       snd_pcm_mmap_control_nopage,
2891 };
2892
2893 static int snd_pcm_mmap_control(snd_pcm_substream_t *substream, struct file *file,
2894                                 struct vm_area_struct *area)
2895 {
2896         snd_pcm_runtime_t *runtime;
2897         long size;
2898         if (!(area->vm_flags & VM_READ))
2899                 return -EINVAL;
2900         runtime = substream->runtime;
2901         snd_assert(runtime != NULL, return -EAGAIN);
2902         size = area->vm_end - area->vm_start;
2903         if (size != PAGE_ALIGN(sizeof(snd_pcm_mmap_control_t)))
2904                 return -EINVAL;
2905         area->vm_ops = &snd_pcm_vm_ops_control;
2906         area->vm_private_data = substream;
2907         area->vm_flags |= VM_RESERVED;
2908         return 0;
2909 }
2910
2911 static void snd_pcm_mmap_data_open(struct vm_area_struct *area)
2912 {
2913         snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
2914         atomic_inc(&substream->runtime->mmap_count);
2915 }
2916
2917 static void snd_pcm_mmap_data_close(struct vm_area_struct *area)
2918 {
2919         snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
2920         atomic_dec(&substream->runtime->mmap_count);
2921 }
2922
2923 static struct page * snd_pcm_mmap_data_nopage(struct vm_area_struct *area, unsigned long address, int *type)
2924 {
2925         snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
2926         snd_pcm_runtime_t *runtime;
2927         unsigned long offset;
2928         struct page * page;
2929         void *vaddr;
2930         size_t dma_bytes;
2931         
2932         if (substream == NULL)
2933                 return NOPAGE_OOM;
2934         runtime = substream->runtime;
2935         offset = area->vm_pgoff << PAGE_SHIFT;
2936         offset += address - area->vm_start;
2937         snd_assert((offset % PAGE_SIZE) == 0, return NOPAGE_OOM);
2938         dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
2939         if (offset > dma_bytes - PAGE_SIZE)
2940                 return NOPAGE_SIGBUS;
2941         if (substream->ops->page) {
2942                 page = substream->ops->page(substream, offset);
2943                 if (! page)
2944                         return NOPAGE_OOM;
2945         } else {
2946                 vaddr = runtime->dma_area + offset;
2947                 page = virt_to_page(vaddr);
2948         }
2949         if (!PageReserved(page))
2950                 get_page(page);
2951         if (type)
2952                 *type = VM_FAULT_MINOR;
2953         return page;
2954 }
2955
2956 static struct vm_operations_struct snd_pcm_vm_ops_data =
2957 {
2958         .open =         snd_pcm_mmap_data_open,
2959         .close =        snd_pcm_mmap_data_close,
2960         .nopage =       snd_pcm_mmap_data_nopage,
2961 };
2962
2963 int snd_pcm_mmap_data(snd_pcm_substream_t *substream, struct file *file,
2964                       struct vm_area_struct *area)
2965 {
2966         snd_pcm_runtime_t *runtime;
2967         long size;
2968         unsigned long offset;
2969         size_t dma_bytes;
2970
2971         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
2972                 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
2973                         return -EINVAL;
2974         } else {
2975                 if (!(area->vm_flags & VM_READ))
2976                         return -EINVAL;
2977         }
2978         runtime = substream->runtime;
2979         snd_assert(runtime != NULL, return -EAGAIN);
2980         if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2981                 return -EBADFD;
2982         if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
2983                 return -ENXIO;
2984         if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
2985             runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2986                 return -EINVAL;
2987         size = area->vm_end - area->vm_start;
2988         offset = area->vm_pgoff << PAGE_SHIFT;
2989         dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
2990         if ((size_t)size > dma_bytes)
2991                 return -EINVAL;
2992         if (offset > dma_bytes - size)
2993                 return -EINVAL;
2994
2995         area->vm_ops = &snd_pcm_vm_ops_data;
2996         area->vm_private_data = substream;
2997         area->vm_flags |= VM_RESERVED;
2998         atomic_inc(&runtime->mmap_count);
2999         return 0;
3000 }
3001
3002 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3003 {
3004         snd_pcm_file_t * pcm_file;
3005         snd_pcm_substream_t *substream; 
3006         unsigned long offset;
3007         
3008         pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return -ENXIO);
3009         substream = pcm_file->substream;
3010         snd_assert(substream != NULL, return -ENXIO);
3011
3012         offset = area->vm_pgoff << PAGE_SHIFT;
3013         switch (offset) {
3014         case SNDRV_PCM_MMAP_OFFSET_STATUS:
3015                 return snd_pcm_mmap_status(substream, file, area);
3016         case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3017                 return snd_pcm_mmap_control(substream, file, area);
3018         default:
3019                 return snd_pcm_mmap_data(substream, file, area);
3020         }
3021         return 0;
3022 }
3023
3024 static int snd_pcm_fasync(int fd, struct file * file, int on)
3025 {
3026         snd_pcm_file_t * pcm_file;
3027         snd_pcm_substream_t *substream;
3028         snd_pcm_runtime_t *runtime;
3029         int err;
3030
3031         pcm_file = snd_magic_cast(snd_pcm_file_t, file->private_data, return -ENXIO);
3032         substream = pcm_file->substream;
3033         snd_assert(substream != NULL, return -ENXIO);
3034         runtime = substream->runtime;
3035
3036         err = fasync_helper(fd, file, on, &runtime->fasync);
3037         if (err < 0)
3038                 return err;
3039         return 0;
3040 }
3041
3042 /*
3043  *  To be removed helpers to keep binary compatibility
3044  */
3045
3046 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3047 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3048
3049 static void snd_pcm_hw_convert_from_old_params(snd_pcm_hw_params_t *params, struct sndrv_pcm_hw_params_old *oparams)
3050 {
3051         unsigned int i;
3052
3053         memset(params, 0, sizeof(*params));
3054         params->flags = oparams->flags;
3055         for (i = 0; i < sizeof(oparams->masks) / sizeof(unsigned int); i++)
3056                 params->masks[i].bits[0] = oparams->masks[i];
3057         memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3058         params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3059         params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3060         params->info = oparams->info;
3061         params->msbits = oparams->msbits;
3062         params->rate_num = oparams->rate_num;
3063         params->rate_den = oparams->rate_den;
3064         params->fifo_size = oparams->fifo_size;
3065 }
3066
3067 static void snd_pcm_hw_convert_to_old_params(struct sndrv_pcm_hw_params_old *oparams, snd_pcm_hw_params_t *params)
3068 {
3069         unsigned int i;
3070
3071         memset(oparams, 0, sizeof(*oparams));
3072         oparams->flags = params->flags;
3073         for (i = 0; i < sizeof(oparams->masks) / sizeof(unsigned int); i++)
3074                 oparams->masks[i] = params->masks[i].bits[0];
3075         memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3076         oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3077         oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3078         oparams->info = params->info;
3079         oparams->msbits = params->msbits;
3080         oparams->rate_num = params->rate_num;
3081         oparams->rate_den = params->rate_den;
3082         oparams->fifo_size = params->fifo_size;
3083 }
3084
3085 static int snd_pcm_hw_refine_old_user(snd_pcm_substream_t * substream, struct sndrv_pcm_hw_params_old * _oparams)
3086 {
3087         snd_pcm_hw_params_t params;
3088         struct sndrv_pcm_hw_params_old oparams;
3089         int err;
3090         if (copy_from_user(&oparams, _oparams, sizeof(oparams)))
3091                 return -EFAULT;
3092         snd_pcm_hw_convert_from_old_params(&params, &oparams);
3093         err = snd_pcm_hw_refine(substream, &params);
3094         snd_pcm_hw_convert_to_old_params(&oparams, &params);
3095         if (copy_to_user(_oparams, &oparams, sizeof(oparams)))
3096                 return -EFAULT;
3097         return err;
3098 }
3099
3100 static int snd_pcm_hw_params_old_user(snd_pcm_substream_t * substream, struct sndrv_pcm_hw_params_old * _oparams)
3101 {
3102         snd_pcm_hw_params_t params;
3103         struct sndrv_pcm_hw_params_old oparams;
3104         int err;
3105         if (copy_from_user(&oparams, _oparams, sizeof(oparams)))
3106                 return -EFAULT;
3107         snd_pcm_hw_convert_from_old_params(&params, &oparams);
3108         err = snd_pcm_hw_params(substream, &params);
3109         snd_pcm_hw_convert_to_old_params(&oparams, &params);
3110         if (copy_to_user(_oparams, &oparams, sizeof(oparams)))
3111                 return -EFAULT;
3112         return err;
3113 }
3114
3115 /*
3116  *  Register section
3117  */
3118
3119 static struct file_operations snd_pcm_f_ops_playback = {
3120         .owner =        THIS_MODULE,
3121         .write =        snd_pcm_write,
3122         .writev =       snd_pcm_writev,
3123         .open =         snd_pcm_open,
3124         .release =      snd_pcm_release,
3125         .poll =         snd_pcm_playback_poll,
3126         .ioctl =        snd_pcm_playback_ioctl,
3127         .mmap =         snd_pcm_mmap,
3128         .fasync =       snd_pcm_fasync,
3129 };
3130
3131 static struct file_operations snd_pcm_f_ops_capture = {
3132         .owner =        THIS_MODULE,
3133         .read =         snd_pcm_read,
3134         .readv =        snd_pcm_readv,
3135         .open =         snd_pcm_open,
3136         .release =      snd_pcm_release,
3137         .poll =         snd_pcm_capture_poll,
3138         .ioctl =        snd_pcm_capture_ioctl,
3139         .mmap =         snd_pcm_mmap,
3140         .fasync =       snd_pcm_fasync,
3141 };
3142
3143 snd_minor_t snd_pcm_reg[2] =
3144 {
3145         {
3146                 .comment =      "digital audio playback",
3147                 .f_ops =        &snd_pcm_f_ops_playback,
3148         },
3149         {
3150                 .comment =      "digital audio capture",
3151                 .f_ops =        &snd_pcm_f_ops_capture,
3152         }
3153 };