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