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